I am reading this article about the SEH on Windows. and here is the source code of myseh.cpp
I debugged myseh.cpp. I set 2 breakpoints at printf("Hello from an exception handler\n");
at line:24 and DWORD handler = (DWORD)_except_handler;
at line: 36 respectively.
Then I ran it and it broke at line:36. I saw the stack trace as follows.
As going, AccessViolationException occurred because of
mov [eax], 1
Then it broke at line:24. I saw the stack trace as follows.
The same thread but the frame of main
was gone! Instead of _except_handle
. And ESP jumped from 0018f6c8
to 0018ef34
;it's a big gap between 0018f6c8
and 0018ef34
After Exception handled.
I know that _except_handle
must be run at user mode rather than kernel mode.
After _except_handle
returned, the thread turned to ring0 and then windows kernel modified CONTEXT EAX
to &scratch
& and then returned to ring3 . Thus thread ran continually.
I am curious about the mechanism of windows dealing with exception:
WHY the frame calling main
was gone?
WHY the ESP jumped from 0018f6c8
to 0018ef34
?(I mean a big pitch), Do those ESP address belong to same thread's stack??? Did the kernel play some tricks on ESP in ring3??? If so, WHY did it choose the address of 0018ef34
as handler callback's frame? Many thanks!