SegFault creating a ExecutionEngine

My code to create an ExecutionEngine is segfaulting:

    std::string errStr;
    llvm::ExecutionEngine * ee = llvm::EngineBuilder(
unique_ptr<llvm::Module>(module) )
        .setErrorStr( &errStr ) //line 1618
        .setEngineKind( llvm::EngineKind::JIT )

Where module is a `llvm::Module*`. This is code I'm migrating from 3.3
to 3.8. Since the deletion error is happening during constructor I'm at
a bit of a loss as to what I'm doing wrong.

The stack trace from gdb is:

#0 0x00007ffff1195a02 in llvm::SmallPtrSetImplBase::isSmall (this=0x0)
at /opt/llvm/include/llvm/ADT/SmallPtrSet.h:128
#1 0x00007ffff11e37d3 in llvm::SmallPtrSetImplBase::erase_imp
(this=0x0, Ptr=0x68c200) at /opt/llvm/lib/Support/SmallPtrSet.cpp:78
#2 0x00007ffff14c9174 in llvm::SmallPtrSetImpl<llvm::Module*>::erase
(this=0x0, Ptr=0x68c200) at /opt/llvm/include/llvm/ADT/SmallPtrSet.h:275
#3 0x00007ffff14c81cb in llvm::LLVMContext::removeModule
(this=0x691910, M=0x68c200) at /opt/llvm/lib/IR/LLVMContext.cpp:148
#4 0x00007ffff1527b7b in llvm::Module::~Module (this=0x68c200,
__in_chrg=<optimised out>) at /opt/llvm/lib/IR/Module.cpp:57
#5 0x00007ffff70f9511 in std::default_delete<llvm::Module>::operator()
(this=<optimised out>, __ptr=0x68c200) at
#6 std::unique_ptr<llvm::Module, std::default_delete<llvm::Module>

::~unique_ptr (this=<optimised out>, __in_chrg=<optimised out>) at

#7 0x00007ffff26318ba in llvm::EngineBuilder::~EngineBuilder
(this=0x7fffffffced0, __in_chrg=<optimised out>) at
#8 0x00007ffff70f75a1 in ir_llvm::gen::create_engine
(this=this@entry=0x7fffffffd1d0) at src/ir/llvm/gen.cpp:1618
#9 0x00007ffff70f7757 in ir_llvm::gen::execute
(this=this@entry=0x7fffffffd1d0, m=..., sc="") at src/ir/llvm/gen.cpp:252
#10 0x000000000041736b in runner::execute
(this=this@entry=0x7fffffffdbc0, m=std::shared_ptr (count 2, weak 0)
0x682200) at src/bin/leaf.cpp:544
#11 0x0000000000415ac8 in main (argc=3, argv=0x7fffffffdde8) at

A small correction, the code to create looks like this:

    llvm::ExecutionEngine * ee = llvm::EngineBuilder(
unique_ptr<llvm::Module>(module) )
        .setErrorStr( &errStr ) //line 1618
        .setEngineKind( llvm::EngineKind::JIT )
        .setTargetOptions( topts )

I presume the exception actually happens on the `create` line, but GDB
is reporting the line marked 1618.

The EngineBuilder shouldn't be trying to destroy the module. Looking
at EngineBuilder::create's control flow, the most plausible problem is
that the TargetMachine is null, so EngineBuilder::selectTarget has
gone wrong.

If so, maybe the Module's triple is wrong or your LLVM is compiled
strangely so no targets can be found? Either way, it's probably time
to pull out the debugger.


I'm not sure I've passed an invalid value; I've changed my code to not
delete the engine builder and it works:

    auto ebuild = new llvm::EngineBuilder(
unique_ptr<llvm::Module>(module) );
    llvm::ExecutionEngine * ee = ebuild->
        setErrorStr( &errStr ).
        setEngineKind( llvm::EngineKind::JIT ).
        setTargetOptions( topts ).

My code then runs fine. This is pointing towards destruction of the
`EngineBuilder` itself causing the problem (like it's deleting the
module although it shouldn't, since it's been given to the created

Hi edA-qa,

EngineBuilder is using unique_ptr, so if it constructed a valid engine it should be passing ownership off just fine.

That backtrace contains a call to erase on a null SmallPtrSetImpl in the LLVMContext. What LLVMContext are you building the Module with, and are you making sure that the context outlives the module?

  • Lang.