-
-
Save mmozeiko/91a698249fdb9ba1c69dd1e0023552ce to your computer and use it in GitHub Desktop.
exception emulation with vectored exception handler & restoring CPU context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
static bool InsideTry; | |
static CONTEXT ResumeContext; | |
static LONG WINAPI Handler(struct _EXCEPTION_POINTERS* ExceptionInfo) | |
{ | |
printf("inside vectored ex handler\n"); | |
InsideTry = false; | |
RtlRestoreContext(&ResumeContext, NULL); | |
} | |
void badstuff() | |
{ | |
volatile int* a = 0; | |
*a = 1; | |
volatile int x = 0; | |
x = 1 / x; | |
volatile char* p = 0; | |
volatile char z = *p; | |
} | |
int main() | |
{ | |
printf("beginning of main\n"); | |
PVOID h; | |
InsideTry = true; | |
RtlCaptureContext(&ResumeContext); | |
if (InsideTry) | |
{ | |
// this is "try" part | |
printf("setting vectored ex handler\n"); | |
h = AddVectoredExceptionHandler(1, &Handler); | |
printf("calling bad stuff\n"); | |
badstuff(); | |
printf("after bad stuff\n"); // never gets printed | |
} | |
else | |
{ | |
// this is "except" part | |
} | |
printf("removing vectored ex handler\n"); | |
RemoveVectoredExceptionHandler(h); // cleanup | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment