#Implementation of Structured Exception Handler using types in `std.os.windows`

1 messages · Page 1 of 1 (latest)

small siren
#

Hello, I would like to implement my own Windows Exception Handlers (more precisely, something like the __try/ __except C++ language extension).

Context

From what I understood, it can be done by updating the pointer in the first element of the TIB (std.os.windows.teb().NtTib.ExceptionList) to a custom EXCEPTION_REGISTRATION_RECORD, which should contain a pointer to my new exception handler and to the previous one.

My issue

When creating a new EXCEPTION_REGISTRATION_RECORD (see type definition below), the type of the .Handler property is defined as an optional pointer to EXCEPTION_DISPOSITION (i32).

pub const EXCEPTION_REGISTRATION_RECORD = extern struct {
    Next: ?*EXCEPTION_REGISTRATION_RECORD,
    Handler: ?*EXCEPTION_DISPOSITION,
};

But I thought it had to be a function pointer to the new exception handler, something like the type EXCEPTION_ROUTINE, which is defined as

pub const EXCEPTION_ROUTINE = *const fn (
    ExceptionRecord: ?*EXCEPTION_RECORD,
    EstablisherFrame: PVOID,
    ContextRecord: *(Self.CONTEXT),
    DispatcherContext: PVOID,
) callconv(WINAPI) EXCEPTION_DISPOSITION;

Did I miss some critical information ?
Should I attempt to constCast/ptrCast/allignCast a function pointer to a *i32 ? Or could it be the windows bindings are somewhat incomplete in this regard ?

grave pewter
# small siren Hello, I would like to implement my own Windows Exception Handlers (more precise...

std.os.windows bindings are hand-written so they could always be wrong

it looks like there is some evidence that this ?*EXCEPTION_DISPOSITION field has some precedence: https://www.nirsoft.net/kernel_struct/vista/EXCEPTION_REGISTRATION_RECORD.html but that may be outdated

my suggestion would be to copy over the definition of EXCEPTION_REGISTRATION_RECORD into your code and make the necessary corrections (and then use your local definition instead of the one in std.os.windows)

if you're confident the Zig definition is incorrect, then feel free to send a PR with the fix/your reasoning

small siren
#

Thank you for your answer,

I will try and look into some more resources/documentation and get back to this post with more info when I can