Custom SEH handler with / SAFESEH

I'm currently trying to create a C ++ compiler that generates machine code at runtime. However, I'm currently trying to enable safe exception handling (compilation with / SAFESEH). My custom exception handler works in debug mode, but when I run the same code in release mode, my process just ends.

I am pretty sure that the problem is that I cannot register my own exception handler as such, because when I compile my code with / SAFESEH: NO, everything works fine even in release mode.

My custom exception handler is written in my other C ++ code, and I tried to register it as an exception handler by adding the project .asm file to the project:

.386
.model flat
_MyExceptionHandler@16 proto
.safeseh _MyExceptionHandler@16
end

as described here . Then the asm file was compiled with the / safeseh option (among others).

The function of my handler currently has the following declaration:

extern "C" EXCEPTION_DISPOSITION __stdcall MyExceptionHandler(struct
_EXCEPTION_RECORD *ExceptionRecord, void * EstablisherFrame, struct 
_CONTEXT *ContextRecord, void * DispatcherContext);

What would be the proper way to register this function as an exception handler?

Thanks for any suggestions!

+5
source share
3 answers

I finally found a page describing the problem: here . However, the sample code on the page did not work without changes.

The problem is that registering external procedures as an exception handler does not work properly, so you need to register the local assembly procedure as an exception handler.

, , , :

.386
.model flat, stdcall
option casemap :none

extern MyExceptionHandler@16:near

MyExceptionHandlerAsm proto
.SAFESEH MyExceptionHandlerAsm

.code
MyExceptionHandlerAsm proc
    jmp MyExceptionHandler@16
MyExceptionHandlerAsm endp
end

, , , . : MyExceptionHandlerAsm C/++, :

extern "C" int __stdcall MyExceptionHandlerAsm();

, - MyExceptionHandlerAsm C/++, MyExceptionHandler.

+4

MSDN , ASM . /safeseh ml.exe?

+1

ExceptionHandler :

typedef EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE) (
    __in struct _EXCEPTION_RECORD *ExceptionRecord,
    __in PVOID EstablisherFrame,
    __inout struct _CONTEXT *ContextRecord,
    __inout PVOID DispatcherContext
    );

. MSDN. 32- , , x64.

0

All Articles