HI all,
Here in the JUCE team, we are working on porting the Projucer C++ live coding engine to Windows. Our main challenge now is how to catch C++ exceptions in runtime-compiled code on Windows. This doesn’t work for us and we’re quite stuck at this point.
I recently had a chat about this with Chandler Carruth at C++Now. Chandler suggested that we post our problem to this mailing list, and that perhaps Reid Kleckner and/or other people here may have some hints. Any ideas would be highly appreciated!
In our case the RaiseException API function does not find the correct catch pad, which may be caused by a ThrowInfo structure handed to _CxxThrowException being empty. Apart from getting this right, is there anything else we possibly miss? Windows API functions to be called for registering catch pads or so?
Some more details:
We’re using Clang for compiling C++ code at runtime and the ORC libraries to load and execute it in our application, which is itself compiled with MSVC. We work with the release_38 branches of LLVM and Clang.
Static compilation of a simple test program with try/catch and throw works fine. When compiling the same code at runtime and loading it with the ORC libraries, every throw results in an unhandled exception. The point of interest seems to be the throw handler for C++ exceptions (throw.cpp):
__declspec(noreturn) extern “C” void __stdcall
_CxxThrowException(void* pExceptionObject, _ThrowInfo* pThrowInfo)
{
…
RaiseException( ThisException.ExceptionCode,
ThisException.ExceptionFlags,
ThisException.NumberParameters,
(PULONG_PTR)&ThisException.params );
}
For some reason the call to RaiseException does not find or consider the existing catch handler correctly. Comparing execution states when entering the function between the two cases, static- vs. runtime-compiled, turns out that pThrowInfo points to nulled memory in the runtime-compiled case, while it has some data in the static-compiled case. Detailed information about the types can be found here:
To find out why the ThrowInfo structure is empty in the runtime-compiled case, we compared what happens during code generation. It looks like CodeGenFunction::EmitCXXThrowExpr is the relevant function here and it does the right thing, i.e. generating the global variable for the ThrowInfo in MicrosoftCXXABI::emitThrow. Also the WinEHPrepare pass is running correctly as it seems. Is there any extra work to do (at runtime/link-time?) for filling this data structure? Any idea what else to check?
Our Clang language options do include: Exceptions, CXXExceptions, RTTI, RTTIData
Please reply to my colleague Stefan Gränitz (in Cc) who is in charge of this project. Any kind of help or ideas would be really awesome.
Many thanks!
Timur