Catching exceptions passed through a JIT ExecutionEngine

Hi All,

I'm trying to catch an exception that is "passed through" an LLVM ExecutionEngine but I am unable to do so. Specifically, in C++ code, inside a try/catch block, I call a JITted function, which in turn calls back into my code. Everything works fine unless an exception is thrown; I would except the outermost try/catch(...) block to catch the exception thrown in my innermost C++ code, but instead I get the abort "terminate called after throwing an instance of 'MyException'". I am compiling the system with g++ (on Linux and OSX); I haven't tried MSVC++.

Is there something that I need to do to get this behaviour to work? I admit that I have only a fairly basic understanding of how exception handling is implemented in C++ but from what I've read it seems like the exception should unwind through what is, for all intents and purposes, a simple call stack...

Peter Zion

Sorry Peter, just saw this.

If you are still having the problem:

Did you set: llvm::JITExceptionHandling = true; ?


Yes, I did -- it made no difference. Should it?

Note that I have since discovered that this is not a problem on Windows -- the exception drops through as expected.


There was an example program detailing how to do exactly this on the
wiki. Was the wiki content archived before it was taken down?

Ok, see llvm/examples/ExceptionDemo/ExceptionDemo.cpp

For OS X and Linux, build llvm with the environmental variable BUILD_EXAMPLES set to 1(csh: setenv BUILD_EXAMPLES 1). If llvm is already built, it will only build the examples from clang and llvm, ExceptionDemo being one of those.

If I understand your case, running ExceptionDemo with an arg of -1 emulates your scenario. Note that the ExceptionDemo example is built by the llvm make (cmake worked last I checked) with exceptions on. By default exceptions are turned off for most llvm libs.

I would think that if:

1) setting: llvm::JITExceptionHandling = true;
2) building your process with exceptions on.

your program should work.

Hope this helps


Actually, I see that the example code I was thinking of is in
examples/ExceptionDemo/ExceptionDemo.cpp of the LLVM source
distribution. The wiki page, as I recall, had some additional exposition
that's not in the source file.

Although I did not archive the wiki entry, the code is at llvm/examples/ExceptionDemo/ExceptionDemo.cpp and is built (for OS X and Linux) by building llvm with the environment var BUILD_EXAMPLES set to 1.


One additional note:

For your case you do not need to use or implement what the ExceptionDemo
program is doing. Its purpose is to show how JITed code can pass execution
to the unwind block of an invoke instruction via throwing an exception via _Unwind_RaiseException.
Because this involves using the C++ ABI for Itanium exception handling specification,
this is quite involved.

However passing an argument of -1 to ExceptionDemo causes this program to
instead call a pre-compiled C++ function (not JITed, but called from JITed code), which throws
a C++ exception, unwinds through the JITed code, and lands back in the callee of JITed outermost
function. This callee has a try catch, and catches the said C++ exception. This is your scenario I believe.