Question

Certain kinds of events in 64-bit Windows programs e.g. division by zero, null pointer reference, stack overflow if you detect that with a guard page, are normally handled (in a program that finds it necessary to handle them) with structured or vectored exceptions.

In the context of a JIT compiler, structured exceptions are slightly awkward due to the need to provide unwinding information and conform to the unwinding protocol for all functions, which might not interact well with e.g. a copying garbage collector that moves code blocks, which suggests the use of vectored exceptions.

While vectored exception handlers are normally expected to return, using them for this purpose would involve exiting the handler with longjmp or some equivalent code that resets the stack pointer without doing any unwinding.

Is this legal, or is there some stumbling block I'm overlooking?

The equivalent in Unix seems to be legal, for what that's worth: Longjmp out of signal handler?

Was it helpful?

Solution

Well, it seems to work fine in the test I ran.

#include <setjmp.h>
#include <stdio.h>
#include <windows.h>

jmp_buf jmp;

LONG CALLBACK eh(PEXCEPTION_POINTERS e) {
    longjmp(jmp, 1);
}

void main() {
    AddVectoredExceptionHandler(1, eh);
    for (int i = 0; i != 10; ++i)
        if (!setjmp(jmp))
            *(char*)0 = 0;
        else
            puts("ok");
    getchar();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top