我目前正在研究如何实现一个沙盒(类似于谷歌的NaCl项目),在那里我可以运行不受信任的x86代码(受限制的指令集),这样它就不会损害我的其余进程.
与NaCl不同,不受信任的代码不会在单独的进程中运行,而是与主机应用程序运行相同的进程.因此,一个关键步骤是使Windows的结构化异常处理正确,以便捕获错误(如无效的内存访问或div为0),并在Windows杀死我的主机应用程序之前正常终止沙箱.(NaCl不会遇到这些问题.沙箱是一个单独的过程,如果出现错误就会被杀死.)
此外,沙盒代码不应使用宿主应用程序堆栈,而是在我自己分配的某个单独的"堆栈"上运行.
正是这种组合(存在自定义分配堆栈时的异常处理)扭曲了我的想法.我已经检查了Go和Factor的语言实现,它们做了类似的事情并且通过这个帮助得到了一些运行.
但仍有一些悬而未决的问题和不确定因素.所以我想我会利用Stack Overflow的神奇知识来获得一些意见:-)
以下是一个工作代码片段,内容涉及核心问题:
code.cpp
#include <Windows.h>
extern "C" void Sandbox();
// just a low level helper to print "msg"
extern "C" void Write(const char* msg)
{
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
msg, (DWORD)strlen(msg), NULL, NULL);
}
// should be called first on error and continue exception handling
LONG __stdcall GlobalExceptionHandler(_EXCEPTION_POINTERS*)
{
Write("GEH ");
return EXCEPTION_CONTINUE_SEARCH;
}
// should be called afterwards on error and terminate the process
// of course this is just a …Run Code Online (Sandbox Code Playgroud) 我遇到了奇怪的崩溃.我想知道它是否是我的代码或编译器中的错误.当我使用Microsoft Visual Studio 2010将以下C++代码编译为优化的发布版本时,它会在标记的行中崩溃:
struct tup { int x; int y; };
class C
{
public:
struct tup* p;
struct tup* operator--() { return --p; }
struct tup* operator++(int) { return p++; }
virtual void Reset() { p = 0;}
};
int main ()
{
C c;
volatile int x = 0;
struct tup v1;
struct tup v2 = {0, x};
c.p = &v1;
(*(c++)) = v2;
struct tup i = (*(--c)); // crash! (dereferencing a NULL-pointer)
return i.x;
} …Run Code Online (Sandbox Code Playgroud) c++ crash visual-studio-2010 compiler-optimization visual-c++