LLVM Pass 插入对 LLVM 位码的外部函数调用

Ali*_*i94 7 c instrumentation llvm llvm-ir

我正在编写一个LLVM通行证来检测C源程序。我想在每个调用外部函数的分支指令之前插入一个函数调用,如下所示:

void print(int x){
    printf("x = %d\n", x);

    return;
}
Run Code Online (Sandbox Code Playgroud)

我想C使用llvm-link工具将此外部函数链接到源代码,然后使用工具检测代码opt

我实施的传球是这样的:

#include "llvm/Pass.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/IRBuilder.h"
#include <vector>

using namespace llvm;

namespace{
    struct ir_instrumentation : public ModulePass{
    static char ID;
    Function *monitor;

    ir_instrumentation() : ModulePass(ID) {}

    virtual bool runOnModule(Module &M)
    {
        std::vector<Type *> args;
        args.push_back(Type::getInt32Ty(M.getContext()));
        ArrayRef<Type*>  argsRef(args);
        FunctionType *FT = FunctionType::get(Type::getVoidTy(M.getContext()), args, false);
        Constant* myFunc = M.getOrInsertFunction("print", FT, NULL);
        minitor = cast<Function>(myFunc);


        for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
        {
            for(Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
            {
                for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI)
                {
                    if(isa<BranchInst>(&(*BI)) )
                    {
                        errs() << "found a branch instruction!\n";
                        ArrayRef< Value* > arguments(ConstantInt::get(Type::getInt32Ty(M.getContext()), 5, true));
                        Instruction *newInst = CallInst::Create(monitor, arguments, "");
                        BB->getInstList().insert(BI, newInst); 
                        errs() << "Inserted the function!\n";
                    }

                }
            }
        }

        return true;
    }
};
char ir_instrumentation::ID = 0;
static RegisterPass<ir_instrumentation> X("ir-instrumentation", "LLVM IR Instrumentation Pass");

}
Run Code Online (Sandbox Code Playgroud)

LLVM使用此 pass 配置和构建得很好,但是当我使用时opt,我得到了这个error

选择:/llvm/lib/IR/Type.cpp:281:

llvm::FunctionType::FunctionType(llvm::Type*, llvm::ArrayRefllvm::Type*, bool):

断言 `isValidReturnType(Result) && "无效的函数返回类型"' 失败。

我认为问题类似于我声明的函数类型与外部函数(如上下文)之间的不匹配。

LLVM 版本:LLVM 版本 7.0.0svn

直到现在我还没有解决这个问题。

谢谢

Ali*_*i94 5

我终于可以解决这个问题并成功检测 LLVM 位码。在使用 function 遇到很多麻烦之后getOrInsertFunction,我发现在我的情况下没有必要使用这种方法。我只是简单地将我的通行证更改为:

#include "llvm/Pass.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/raw_ostream.h"

#include "llvm/IR/IRBuilder.h"

#include <vector>

using namespace llvm;

namespace{
struct ir_instrumentation : public ModulePass{
    static char ID;
    Function *monitor;

    ir_instrumentation() : ModulePass(ID) {}

    virtual bool runOnModule(Module &M)
    {
        errs() << "====----- Entered Module " << M.getName() << ".\n";

        int counter = 0;

        for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
        {
            errs() << "Function name: " << F->getName() << ".\n";
            if(F->getName() == "print"){
                monitor = cast<Function>(F);
                continue;
            }

            for(Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
            {
                for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI)
                {
                    if(isa<BranchInst>(&(*BI)) )
                    {
                        errs() << "found a brach instruction!\n";
                        ArrayRef< Value* > arguments(ConstantInt::get(Type::getInt32Ty(M.getContext()), counter, true));
                        counter++;
                        Instruction *newInst = CallInst::Create(monitor, arguments, "");
                        BB->getInstList().insert(BI, newInst); 
                        errs() << "Inserted the function!\n";
                    }

                }
            }
        }

        return true;
    }
};
char ir_instrumentation::ID = 0;
static RegisterPass<ir_instrumentation> X("ir-instrumentation", "LLVM IR Instrumentation Pass");

}
Run Code Online (Sandbox Code Playgroud)

由于我知道外部函数的名称,我可以通过迭代模块的所有函数来简单地找到它,然后以所需的方式使用它。

显然问题是由调用module->getOrInsertFunction和函数类型引起的。我的经验是,当你想插入一个新函数并声明你自己函数的原型时,这个方法更有用。使用它来获取现有功能具有挑战性(例如设置正确的原型,...)

谢谢