Parsing a bit code file

I am trying to parse LLVM IR from a bit code file. I went through the following steps.

hello.cpp

#include <iostream>

int main() {
  std::cout << "Hello world!" << "\n";
  return 0;
}

dump.cpp

#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>

using namespace llvm;
int main()
{
  LLVMContext context;
  SMDiagnostic error;
  Module *m = parseIRFile("hello.bc", error, context).get();
  if(m)
  {
    m->dump();
  } 

  return 0;
}

$ clang++ -S -O3 -emit-llvm hello.cpp -c -o hello.bc

$ clang++ -g -O3 dump.cpp llvm-config --cxxflags --ldflags --system-libs --libs all-o dump

$ ./dump

But I am getting a segmentation fault as given below. What may be causing this? I am on llvm-6.0 rc2.

  • thread #1, queue = ‘com.apple.main-thread’, stop reason = signal SIGABRT
  • frame #0: 0x00007fff71902e3e libsystem_kernel.dylib__pthread_kill + 10 frame #1: 0x00007fff71a41150 libsystem_pthread.dylibpthread_kill + 333
    frame #2: 0x00007fff7185f312 libsystem_c.dylibabort + 127 frame #3: 0x00007fff71827368 libsystem_c.dylib__assert_rtn + 320
    frame #4: 0x0000000100477eb9 dumpllvm::isa_impl_cl<llvm::PointerType, llvm::Type const*>::doit(Val=0x0000000000000000) at Casting.h:106 frame #5: 0x0000000100477e28 dumpllvm::isa_impl_wrap<llvm::PointerType, llvm::Type const*, llvm::Type const*>::doit(Val=0x00007ffeefbfee20) at Casting.h:133
    frame #6: 0x0000000100477e02 dumpllvm::isa_impl_wrap<llvm::PointerType, llvm::Type* const, llvm::Type const*>::doit(Val=0x00007ffeefbfee68) at Casting.h:123 frame #7: 0x0000000100477db5 dumpbool llvm::isa<llvm::PointerType, llvm::Type*>(Val=0x00007ffeefbfee68) at Casting.h:143
    frame #8: 0x0000000100477d18 dumpllvm::cast_retty<llvm::PointerType, llvm::Type*>::ret_type llvm::cast<llvm::PointerType, llvm::Type>(Val=0x0000000000000000) at Casting.h:255 frame #9: 0x000000010047227d dumpllvm::GlobalValue::getType(this=0x00000001013ffff6) const at GlobalValue.h:265
    frame #10: 0x00000001004bad34 dumpllvm::TypeFinder::run(this=0x00007ffeefbff530, M=0x0000000101401e80, onlyNamed=false) at TypeFinder.cpp:38 frame #11: 0x00000001001a97c9 dump(anonymous namespace)::TypePrinting::incorporateTypes(this=0x00007ffeefbff530, M=0x0000000101401e80) at AsmWriter.cpp:491
    frame #12: 0x00000001001a964f dump(anonymous namespace)::AssemblyWriter::AssemblyWriter(this=0x00007ffeefbff510, o=0x00007ffeefbff3f8, Mac=0x00007ffeefbff438, M=0x0000000101401e80, AAW=0x0000000000000000, IsForDebug=true, ShouldPreserveUseListOrder=false) at AsmWriter.cpp:2210 frame #13: 0x000000010019ce22 dump(anonymous namespace)::AssemblyWriter::AssemblyWriter(this=0x00007ffeefbff510, o=0x00007ffeefbff3f8, Mac=0x00007ffeefbff438, M=0x0000000101401e80, AAW=0x0000000000000000, IsForDebug=true, ShouldPreserveUseListOrder=false) at AsmWriter.cpp:2207
    frame #14: 0x000000010019e3e3 dumpllvm::Module::print(this=0x0000000101401e80, ROS=0x00000001007d1a70, AAW=0x0000000000000000, ShouldPreserveUseListOrder=false, IsForDebug=true) const at AsmWriter.cpp:3415 frame #15: 0x00000001001a77e1 dumpllvm::Module::dump(this=0x0000000101401e80) const at AsmWriter.cpp:3650
    frame #16: 0x000000010000250e dumpmain at dump.cpp:17 [opt] frame #17: 0x00007fff717b3115 libdyld.dylibstart + 1

Thanks

Buddhika

Change
   Module *m = parseIRFile("hello.bc", error, context).get();
to
   std::unique_ptr<Module> m = parseIRFile("hello.bc", error, context);

Otherwise the unique_ptr can be deleted before the call to "dump" (which would delete the module as well).

-Krzysztof