gpt4 book ai didi

llvm - 在 LLVM IR 中检测函数调用

转载 作者:行者123 更新时间:2023-12-05 01:31:21 25 4
gpt4 key购买 nike

我想在位代码级别检测对程序每个函数的调用。假设有一个函数 void f(int a),我需要在 main 函数的开头检测以下代码。

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);

}

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

 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 的新手,有人能告诉我我的实现有什么问题吗?

最佳答案

您正在将 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);
}

您正在将 arg 推送到您的 args 向量中。 argAllocaInst 的值,因此它是一个指针。您需要适合您的函数签名的值。您需要发出从 AllocaInst 加载的 LoadInst 并将 LoadInst 推送到您的向量中。

针对您的问题:

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

查看 IRBuilder 中的 CreateGlobalStringPtr 函数。文档 here . IRBuilder 是一个很好的辅助类,它使 LLVM IR 的使用变得更容易一些。

关于llvm - 在 LLVM IR 中检测函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com