[LLVMdev] LLVM Exception Handling

Anil Can Akay anilck at gmail.com
Fri Mar 20 09:33:15 PDT 2015


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>


More information about the llvm-dev mailing list