LLVM MCJIT SEH Exception handling


I apologize if I’m posting this to the wrong list; if this is the case, please let me know.

For some time now, I’ve been trying to get SEH exception handling to work in LLVM MCJIT (x64). While reading up on LLVM 3.8, I decided to pick it up again, because a lot has changed which simplifies things greatly.

As a toy project, I’m attempting to catch an exception that’s thrown in a Windows x64 C++ library call from within an LLVM x64 MCJIT instance. The version I’m working with is LLVM 3.8.1, and the code I’ve created is based on the documentation http://llvm.org/docs/ExceptionHandling.html . I’ve also been reading up on the Exception toy/example code that’s shipped with LLVM, as well as several mail discussions, the CGException class from clang and of course the ever useful ‘clang --emit-llvm’. My aim here is to create a minimum working test case.

My first attempt was to simply compile a piece of C++ code with an external function call and run that through LLC. The code is pretty straight-forward:

Someone needs to implement https://llvm.org/bugs/show_bug.cgi?id=24233 before MCJIT can support EH on Win64.

The llc command probably failed because the Cpp backend was unmaintained. It was removed from the last release.

For catching the exception you may use VEH as a fallback on Windows as discussed earlier this year:
So far that worked for me. The problem was that I could not manage to escape from this handler and resume execution, because setjmp/longjmp didn’t to work (fails with an exception and re-enters the VEH handler). However, there seems to be some magic behind, as it sometimes works with trivial examples. Thanks Reid for pointing to the bug. It explains most of it.