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_ptrllvm::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_ptrllvm::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;
}