Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LLVM IR中插入对函数的调用_Llvm - Fatal编程技术网

在LLVM IR中插入对函数的调用

在LLVM IR中插入对函数的调用,llvm,Llvm,我想在比特码级别插入对程序每个函数的调用。 假设有一个函数void f(int a),我需要在主函数的开头插入以下代码 int a; klee_make_symbolic(&a, sizeof(a), "a"); f(a); 我写了一张通行证来达到这个目的 for (Module::iterator f = M.begin(), fe = M.end(); f != fe; ++f) { std::vector<llvm::Value*> args; for(Fu

我想在比特码级别插入对程序每个函数的调用。 假设有一个函数
void f(int a)
,我需要在主函数的开头插入以下代码

int a;
klee_make_symbolic(&a, sizeof(a), "a");
f(a);
我写了一张通行证来达到这个目的

 for (Module::iterator f = M.begin(), fe = M.end(); f != fe; ++f) {
  std::vector<llvm::Value*> args;
  for(Function::arg_iterator ai = f->arg_begin(), ae = f->arg_end(); ai != ae; ++ai){
   Type* tp = ai->getType();   
    AllocaInst* arg = new AllocaInst(tp, "name", firstInst);
    args.push_back(arg);
    LLVM_TYPE_Q llvm::Type *i8Ty = Type::getInt8Ty(getGlobalContext());
    Constant *fc = M.getOrInsertFunction("klee_make_symbolic",
                                               PointerType::getUnqual(i8Ty),
                                               Type::getInt64Ty(getGlobalContext()),
                                               PointerType::getUnqual(i8Ty),
                                               NULL);
    Function* kleeMakeSymbolic = cast<Function>(fc);

    std::vector<Value* > klee_args;
    klee_args.push_back(arg);
    klee_args.push_back(ConstantInt::get(Type::getInt64Ty(getGlobalContext()),
                       dl->getTypeAllocSizeInBits(tp)));// dl is DataLayout
    klee_args.push_back(arg);//I dont't know how to pass a argument of "const char *"

    // Inject a call to klee_make_symbolic
    CallInst::Create(kleeMakeSymbolic, klee_args, "", firstInst);

  }

  // Inject a call to the function
  CallInst::Create(f, args, "", firstInst);

}
for(模块::迭代器f=M.begin(),fe=M.end();f!=fe;++f){
std::向量args;
对于(函数::arg_迭代器ai=f->arg_begin(),ae=f->arg_end();ai!=ae;++ai){
类型*tp=ai->getType();
AllocaInst*arg=新的AllocaInst(tp,“名称”,firstInst);
args。推回(arg);
LLVM_TYPE_Q LLVM::TYPE*i8Ty=TYPE::getInt8Ty(getGlobalContext());
常量*fc=M.getOrInsertFunction(“klee_make_symbol”,
指针类型::getUnqual(i8Ty),
类型::getInt64Ty(getGlobalContext()),
指针类型::getUnqual(i8Ty),
无效);
函数*kleeMakeSymbolic=cast(fc);
std::向量klee_args;
klee_args.向后推(arg);
klee_args.push_back(ConstantInt::get(Type::getInt64Ty(getGlobalContext()),
dl->getTypeAllocSizeInBits(tp));//dl是数据布局
klee_args.push_back(arg)//我不知道如何传递一个参数“const char*”
//给klee_打电话,使其具有象征意义
CallInst::Create(kleeMakeSymbolic,klee_args,“,firstInst);
}
//插入对函数的调用
CallInst::Create(f,args,“,firstInst);
}
}

但我得到了一个断言失败:

 llvm::CallInst::init(llvm::Value*, llvm::ArrayRef<llvm::Value*>, const llvm::Twine&): Assertion `(Args.size() == FTy->getNumParams() || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Calling a function with bad signature!"' failed.
llvm::CallInst::init(llvm::Value*,llvm::ArrayRef,const llvm::Twine&):断言`(Args.size()==FTy->getNumParams()| |(FTy->isVarArg()&&Args.size()>FTy->getNumParams())&&&&“调用签名错误的函数”失败。

我是LLVm新手,有人能告诉我我的实现有什么问题吗?

您正在将
a
的指针传递给函数
f
。这就是您实现的问题

在代码中:

for (Module::iterator f = M.begin(), fe = M.end(); f != fe; ++f) {
  std::vector<llvm::Value*> args;
  for(Function::arg_iterator ai = f->arg_begin(), ae = f->arg_end(); ai != ae; ++ai){
   Type* tp = ai->getType();   
    AllocaInst* arg = new AllocaInst(tp, "name", firstInst);
    args.push_back(arg);
    ...

  }

  // Inject a call to the function
  CallInst::Create(f, args, "", firstInst);
}
查看
IRBuilder
中的
CreateGlobalStringPtr
函数。文件
IRBuilder
是一个很好的助手类,它使LLVM IR的使用变得更简单

 klee_args.push_back(arg);//I dont't know how to pass a argument of "const char *"