Hello,
for my JIT-compiled scripting-backend, I've been implementing custom exception-handler according to the specc (https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=msvc-170).
However, I'm having trouble accessing some of the register-values inside this handler. All my compiled functions store an “ExexecutionState”-variable inside the RBX register, which is needed to interface with the different bindings etc… so what I need to do, is access that “state” inside the exception-handler as well. I've been doing it like this:
EXCEPTION_DISPOSITION handleException(PEXCEPTION_RECORD exceptionRecord, ULONG64 establisherFrame, PCONTEXT contextRecord, PDISPATCHER_CONTEXT dispatcherContext)
{
auto& state = *reinterpret_cast<ExecutionStateJIT*>(contextRecord->Rbx);
The main problem here is that this is the RBX at the function that raises the exception, which is mostly a c++-function that might override rbx as it sees fit.
So, how do I access the RBX inside the function where the exception should be handled? From the limited documentation, I assumed that it would the ContextRecord stored in DispatcherContext:
EXCEPTION_DISPOSITION handleException(PEXCEPTION_RECORD exceptionRecord, ULONG64 establisherFrame, PCONTEXT contextRecord, PDISPATCHER_CONTEXT dispatcherContext)
{
auto& state = *reinterpret_cast<ExecutionStateJIT*>(dispatcherContext->ContextRecord->Rbx);
Which does not seem to be the same contextRecord as is passed to the function, however, here RBX is “0”. I'm assuming that an exception-handler has to restore the non-volatile registers it uses, via the UNWIND_OPCODES, so RBX should then be at the old value when my exception-handler is invoked - correct? Or am I missing something?