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. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150320/9b85b68c/attachment.html>
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>
On 3/24/15 10:32 AM, Reid Kleckner wrote:> 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.The symptoms look like what happens when you have two copies of the rtti each in different SOs. If that's the problem, then throwing from one SO and trying to catch in the other will terminate this way when the first's typeinfos don't match against any in the second. I don't know the JIT well at all, but I'd suggest printing out the address of the type info for int, _ZTIi, and check that they're the same in both the JIT-ee and the JIT-er. Jon> > On Fri, Mar 20, 2015 at 9:33 AM, Anil Can Akay <anilck at gmail.com > <mailto: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 <mailto:LLVMdev at cs.uiuc.edu> http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- Jon Roelofs jonathan at codesourcery.com CodeSourcery / Mentor Embedded
Possibly Parallel Threads
- [LLVMdev] Fwd: error while linking modules with exception handling demo code
- [ORC JIT][MLIR] GDBRegistrationListener "second attempt to perform debug registration" assert
- [ORC JIT][MLIR] GDBRegistrationListener "second attempt to perform debug registration" assert
- [LLVMdev] [PATCH] Fix for bug in JIT exception table allocation (no test yet)
- [LLVMdev] [PATCH] Fix for bug in JIT exception table allocation (no test yet)