Jump back to/return from runFunction

How can I properly exit from code being executed via
"ExecutionEngine::runFunction"? My JIT'd code is executing and it calls
a function in the host program. This host function then decides the
executing code should be stopped and wants to return from runFunction.

I've considered setjmp/longjmp, but I'm not sure if this would properly
clean up the ExecutionEngine internals.

If the executing code wants to return, it should just return. I take it that doesn't work for you? You probably need to modify things so that your JITed code can tolerate the host code returning, or it can return some kind of error condition.

I'm not sure what's required to allow an exception to be thrown through the JITed code. I'd consider doing that if I could.

My own code has a similar need. I got around it by making my code very asynchronous. That is, I call runFunction, my JITed code executes, and eventually calls a method in my host with a callback. That method returns immediately, and so does runFunction. Eventually, my host code calls the callback it was given to continue processing.

However, I haven't fully tested this, in that currently my host function is stubbed out to just immediately call the callback, so I don't know if this can actually work. I better test it…

The guest code in this case doesn't know that it will be returning. This
"abort" conditions are detected and controlled by the host to detect
misbehaving guest code. Therefore the guest can't be expected to
cooperate in this situation.

I'm not concerned about any tracking in the guest code, it will handle a
premature finish just fine. I'm more worried about the LLVM internals:
if I longjmp out of runFunction I'm assuming LLVM may leak resources, or
be left in a dirty state.

My guess is that exceptions won't actually work. This is because the
guest code can actually use exceptions (or will, not yet in my case). I
assume that if the host is also compiled with LLVM then the exception
handling will interfere and the guest code will end up catching the
exception and nonsense will ensue.

I'll have to defer to the experts on the list. I don't know enough, nor am I even sure it's possible to test conclusively.