如何构建一个捕获所有异常的C ++ Dll包装? [英] How to build a C++ Dll wrapper that catches all exceptions?

查看:181
本文介绍了如何构建一个捕获所有异常的C ++ Dll包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我们正在寻找一种方法来捕获一个C ++代码的所有异常,并将其包装在dll中。这样我们可以屏蔽的应用程序使用这个DLL,从这个DLL中发生的任何错误。

Like the title says, we’re looking for a way to catch all exceptions from a piece of C++ code, and wrap this in a dll. This way we can shield of the application that uses this dll, from any errors occurring in this dll.

但是,这似乎不可能在Windows下的C + +。

However, this does not seem possible with C++ under Windows.

示例:

void function()
{  
    try  
    {    
        std::list<int>::iterator fd_it;
        fd_it++;  
    } catch(...) {}
}

发生不被标准C ++ try / catch块捕获,也不被 _set_se_translator()设置的任何SEH翻译器函数捕获。相反,DLL崩溃,并且使用DLL的程序中止。我们使用Visual C ++ 2005编译,使用选项/ SHa。有没有人知道是否可能在C ++ / Win32捕获这些类型的问题,并制作一个rocksolid的DLL包装?

The exception that occurs is not caught by the standard C++ try/catch block, nor by any SEH translator function set by _set_se_translator(). Instead, the DLL crashes, and the program that uses the DLL is aborted. We compiled with Visual C++ 2005, with the option /SHa. Does anyone know if it’s possible in C++/Win32 to catch these kind of problems and make a rocksolid DLL wrapper?

推荐答案

方式使一个岩石固体DLL包装器是加载buggy DLL在另一个进程,所以,如果它崩溃,它不采取你的主要进程与它。

The only way to make a rock solid DLL wrapper is to load the buggy DLL in another process, so that if it crashes it doesn't take your primary process down with it.

捕获所有C ++异常似乎是合理的,但捕捉所有结构化异常是另一个故事。 SEH可能会 让你大部分的方式在那里,因为它允许你捕获访问违规,零除异常等。

Catching all C++ exceptions seems reasonable, but catching all structured exceptions is another story. SEH might seem to get you most of the way there, because it allows you to catch access violations, divide-by-zero exceptions, etc.

但是如果错误的DLL碰巧从另一个线程的堆栈触摸未提交的页面怎么办?内存访问将页面错误,异常处理程序将被调用,现在该页面不再是保护页面。当该线程需要增长堆栈时,它将获得访问冲突,并且该进程将崩溃。 (这些 posts 更详细地描述此案例。)

But what if the buggy DLL happens to touch an uncommitted page from another thread's stack? The memory access will page fault, the exception handler will be invoked, and now that page is no longer a guard page. When that thread needs to grow the stack, it will get an access violation, and the process will crash. (These posts describe this case in more detail.)

另一个可能的问题:越野车DLL在保持同步对象时崩溃,但是您使用SEH捕获异常。如果您的进程尝试获取同一个同步对象,那么它会死锁而不是崩溃。共享同步对象可以是C运行时或操作系统的一部分:如果有错误的DLL 1加载了错误的DLL 2,它在其 DllMain()拿着装载机锁?

Another likely problem: the buggy DLL crashes while holding a synchronization object, but you use SEH to catch the exception. If your process attempts to acquire the same synchronization object, then it deadlocks instead of crashing. The shared synchronization object may be part of the C runtime or the OS: what if buggy DLL 1 loads buggy DLL 2, which crashes in its DllMain() while buggy DLL 1 is holding the loader lock? Will your process deadlock the next time it loads a DLL?

有更多的信息,为什么这个(和函数像 IsBadReadPtr(),这有类似的问题)是滥用SEH:

For more information on why this (and functions like IsBadReadPtr(), which have similar problems) is a misuse of SEH:

  • Larry Osterman's WebLog: Structured Exception Handling Considered Harmful
  • Larry Osterman's WebLog: Should I check the parameters to my function?
  • Larry Osterman's WebLog: Resilience is NOT necessarily a good thing
  • The Old New Thing: IsBadXxxPtr should really be called CrashProgramRandomly

这篇关于如何构建一个捕获所有异常的C ++ Dll包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆