使用RegisterStandardPasses时,llvm中的分段错误通过

时间:2017-12-08 10:42:40

标签: llvm llvm-ir

我正在尝试运行LLVM传递,但是我遇到了分段错误。下面是我的LLVM传递的代码。

编译器运行并生成输出。 runOnBasicBlock有效,但之后我们收到此错误:

  

分段错误(核心转储)

当我删除RegisterStandardPassesloadPass(最后一行)时,代码运行正常。知道是什么原因引起的吗?谢谢

我们使用以下行加载编译器传递。

opt -load ./plugin_build/NvmInstrumenter.so -NvmInstrumenter -stats < simple.bc

我们使用LLVM和Clang 5.0.0同时下载和构建。

编译器的代码传递:

#include "llvm/Pass.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/PassRegistry.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
// For output
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

namespace {
  struct NvmInstrumenter : public BasicBlockPass {
    static char ID;
    NvmInstrumenter() : BasicBlockPass(ID) {}
    virtual bool runOnBasicBlock(BasicBlock &BB) {
      BasicBlock::iterator i;
      errs() << "Basic Block\n";
      for( i=BB.begin(); i!=BB.end(); i++ ) {
    errs() << "  " << i->getOpcodeName() << "\n";
      }
      return false;
    }
  };
}

// Pass info
char NvmInstrumenter::ID = 0; // LLVM ignores the actual value
static RegisterPass<NvmInstrumenter> X("NvmInstrumenter", "Example pass", false, false);

static void loadPass(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
  PM.add(new NvmInstrumenter());
}

static RegisterStandardPasses clangtoolLoader_Ox(PassManagerBuilder::EP_OptimizerLast, loadPass);
static RegisterStandardPasses clangtoolLoader_O0(PassManagerBuilder::EP_EnabledOnOptLevel0, loadPass);

这是回溯:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ff4900 in ?? ()
(gdb) bt
#0  0x00007ffff7ff4900 in ?? ()
#1  0x00007ffff58fb4ab in llvm::object_deleter<llvm::SmallVector<std::pair<llvm::PassManagerBuilder::ExtensionPointTy, std::function<void (llvm::PassManagerBuilder const&, llvm::legacy::PassManagerBase&)> >, 8u> >::call(void*) () from /usr/lib/llvm-5.0/bin/../lib/libLLVM-5.0.so.1
#2  0x00007ffff4cef87d in llvm::ManagedStaticBase::destroy() const () from /usr/lib/llvm-5.0/bin/../lib/libLLVM-5.0.so.1  
#3  0x00007ffff4cefa15 in llvm::llvm_shutdown() () from /usr/lib/llvm-5.0/bin/../lib/libLLVM-5.0.so.1
#4  0x00005555555f0ddf in main ()

1 个答案:

答案 0 :(得分:0)

并非只有一个错误(https://bugs.llvm.org/show_bug.cgi?id=34573),从LLVM 5开始使用RegisterStandardPasses时LLVM似乎在程序末尾崩溃。

根据以下答案:https://github.com/sampsyo/llvm-pass-skeleton/issues/7#issuecomment-401834287,一种解决方案是在链接程序时将-Wl,-znodelete添加到编译器标志中。它对我有用。