取消迭代器时出现Segfault

Segfault when derefing iterator

本文关键字:Segfault 迭代器 取消      更新时间:2023-10-16

下面是我的示例。我的完整代码是

#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/IR/InstIterator.h"
using namespace llvm;
namespace {
  struct Hello2 : public FunctionPass {
    static char ID; // Pass identification, replacement for typeid
    Hello2() : FunctionPass(ID) {}
    bool runOnFunction(Function &F) override {
      inst_iterator iter = inst_begin(F); // causes a segfault    
      return false;
    }
  };
}
char Hello2::ID = 0;
static RegisterPass<Hello2> Y("hello2", "Hello World Pass");

遵循此处解释的"HelloWorld"通行证示例。当我完全按照示例进行操作时,它运行得很好,但通过上面修改的传递代码,我在运行opt时得到了segfault。(我使用与"HelloWorld"pass示例中相同的"hello.c"文件作为输入,使用clang编译它,运行make,并使用opt调用我的库,与示例中一样。)

是什么导致了我的segfault,有什么方法可以测试/避免它吗?

编辑

我追踪到InstIterator.h:BI = BB->begin();的61号线。当我的代码到达那个点时,BBs是非NULL,但BB=BBs->begin()是NULL。因此,BB的取消引用导致segfault。为什么BB为NULL(以及为什么构造函数不检查这一点)的问题仍然存在。

当我的系统进行自动更新并获得几个llvm-3.5包的新版本时(今天),这个问题得到了解决。此前,opt --version返回

LLVM version 3.5
Optimized build.
Built Mar 23 2014 (21:41:30).
Default target: x86_64-pc-linux-gnu
Host CPU: corei7

现在它返回

LLVM version 3.5.0
Optimized build.
Built Jan 27 2015 (00:14:48).
Default target: x86_64-pc-linux-gnu
Host CPU: corei7