[LLVMdev] LLVM Exception Handling

Reid Kleckner rnk at google.com
Tue Mar 24 09:32:36 PDT 2015


I haven't dug into your example, but I would say exceptions don't work on
Windows yet.

I don't know why it doesn't work on Mac.

On Fri, Mar 20, 2015 at 9:33 AM, Anil Can Akay <anilck at gmail.com> wrote:

> Hi,
>
>
>
> I am trying to implement a scenario similar to running ExceptionDemo.cpp
> with parameter -1 (where the JITed code calls the C++ function which throws
> an exception)
>
> Different from the given example I am using IRParser instead of IRBuilder.
> I can successfully catch the exception in Linux but the program crashes in
> Mac and Windows.
>
>
>
> The code is similar to as follows:
>
>
>
> ### The test.cpp : (clang++ -O0 -S -emit-llvm test.cpp –c)
>
>
>
> extern void test() ;
>
> extern "C" void exec(void*) {
>
>         test();
>
> }
>
>
>
> ### main.cpp
>
> // necessary includes here....
>
>
>
> static void test() {
>
>     throw 1;
>
> }
>
>
>
> int main(int, const char **) {
>
>     llvm::InitializeNativeTarget();
>
>     llvm::InitializeNativeTargetAsmPrinter();
>
>     llvm::InitializeNativeTargetAsmParser();
>
>
>
>     llvm::LLVMContext &Context = llvm::getGlobalContext();
>
>     llvm::SMDiagnostic Err;
>
>     std::unique_ptr<llvm::Module> Mod = llvm::parseIRFile("test.ll", Err,
> Context);
>
>
>
>     std::string triple = llvm::sys::getProcessTriple();
>
>     Mod->setTargetTriple(triple);
>
>     llvm::Function* f = Mod->getFunction("exec");
>
>
>
>     llvm::TargetOptions Opts;
>
>     Opts.NoFramePointerElim = true;
>
>
>
>     // Build engine with JIT
>
>     std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new
> llvm::SectionMemoryManager());
>
>
>
>     std::string err;
>
>     llvm::EngineBuilder factory(std::move(Mod));
>
>     factory.setErrorStr(&err);
>
>     factory.setEngineKind(llvm::EngineKind::JIT);
>
>     factory.setTargetOptions(Opts);
>
>     factory.setMCJITMemoryManager(std::move(MemMgr));
>
>     llvm::ExecutionEngine* EE = factory.create();
>
>
>
>     llvm::sys::DynamicLibrary::AddSymbol("_Z4testv",
> reinterpret_cast<void*>(test));
>
>
>
>     EE->finalizeObject();
>
>
>
>     void* poi = EE->getPointerToFunction(f);
>
>     void (*exec)(void*) = reinterpret_cast<void (*)(void*)>(poi);
>
>
>
>     try {
>
>         exec(NULL);
>
>     } catch (int e) {
>
>         std::cout << "catched " << e << std::endl;
>
>     }
>
>     return 0;
>
> }
>
>
>
> ###
>
>
>
> Crash like below:
>
> libc++abi.dylib: terminating with uncaught exception of type int
>
> [1]    15639 abort (core dumped)  ./main
>
>
>
> What can be the reason for the different behaviour in different platforms?
> In order to make it work, do I need to wrap the llvm::Function* f with the
> unwindResume and externalException blocks ? Any other suggestions ? By the
> way I use LLVM 3.6.0 compiled with LLVM_ENABLE_EH and RTTI enabled.
>
> Thanks in advance for your help.
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150324/9233decb/attachment.html>


More information about the llvm-dev mailing list