使用 LLVM pass 加载和存储变量

Loading and storing variables using an LLVM pass

我正在尝试这样做:

1-插入一个变量t1.

2- 插入对 chrono 函数的调用(以获取时间)。

3- 将 chrono 的 return 值存储在 t1

4- 插入对 "function" 的调用(我编写的函数)并将 t1 作为其参数传递。函数对 t1 进行一些计算。

在代码中是:

float t1 = std::chrono::duration_cast<chrono::nanoseconds(chrono_steady_clock::now().time_since_epoch().count());
function(t1);

但是我想在我试图修改的程序中插入带有 LLVM 传递的上述内容。 我不确定该怎么做,但我的想法是:

Constant *TFunc = M.getOrInsertFunction("std::chrono::duration_cast<chrono::nanoseconds(chrono_steady_clock::now().time_since_epoch().count())", Type::getFloatTy(M.getContext()),NULL);
tfunc = cast<Function>(TFunc);
for (*certain type of instructions Inst*){
    CallInst *CurrInst = dyn_cast<CallInst>(Inst);
    AllocaInst *Talloc = new AllocaInst(Type::getFloatTy((*Inst).getContext()),"t1");
    Instruction *Tcall = CallInst::Create(tfunc,"");
    StoreInst* store_t = new StoreInst(Tcall,Talloc,(Instruction*)CurrInst);
    if(storeT != NULL) {
        LoadInst* loadT = new LoadInst(storeT,"",false);
        Value* t1 = loadT;
        Instruction *newInst = CallInst::Create(hook,loadT, "");
        Inst->getInstList().insert((Instruction*)CurrInst, newInst);
    }
}

我得到的错误是:

0 libLLVM-3.4.so.1 0x40f8150f llvm::sys::PrintStackTrace(_IO_FILE*) + 47 1 libLLVM-3.4.so.1 0x40f8177f 2 libLLVM-3.4.so.1 0x40f812ec 3
0x40022400 __kernel_sigreturn + 0 4 libLLVM-3.4.so.1 0x40864ee7 llvm::LoadInst::LoadInst(llvm::Value*, char const*, bool, llvm::Instruction*) + 71 5 pass.so 0x400265b2 Stack dump: 0. Program arguments: /usr/bin/clang -cc1 -triple i386-pc-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -main-file-name mtd.cc -mrelocation-model pic -pic-level 2 -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.24 -momit-leaf-frame-pointer -g -coverage-file /../mtd.o -resource-dir /usr/bin/../lib/clang/3.4 -dependency-file .deps/mtd.d -MT mtd.o -sys-header-deps -MP -include config.h -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D NDEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /usr/lib/llvm-3.4/include -I /usr/lib/llvm-3.4/include -I /usr/lib/llvm-3.4/include -internal-isystem /usr/include//c++/4.8 -internal-isystem /usr/include//c++/4.8/i386-linux-gnu -internal-isystem /usr/include//c++/4.8/backward -internal-isystem /usr/include//i386-linux-gnu/c++/4.8 -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/c++/4.8 -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/c++/4.8/i686-linux-gnu -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/c++/4.8/backward -internal-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/../../../../include/i686-linux-gnu/c++/4.8 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.4/include -internal-externc-isystem /usr/bin/../lib/gcc/i686-linux-gnu/4.8/include -internal-externc-isystem /usr/include/i386-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Woverloaded-virtual -Wcast-qual -W -Wall -w -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /.../masstree-beta-master -ferror-limit 19 -fmessage-length 80 -fvisibility-inlines-hidden -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -load /../pass.so -o mtd.o -x c++ mtd.cc 1. parser at end of file 2. Per-module optimization passes 3. Running pass 'Synchronization profiler' on module 'mtd.cc'. clang: error: unable to execute command: Segmentation fault (core dumped) clang: error: clang frontend command failed due to signal (use -v to see invocation) Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4) Target: i386-pc-linux-gnu Thread model: posix clang: note: diagnostic msg: PLEASE submit a bug report to http://bugs.debian.org/ and include the crash backtrace, preprocessed source, and associated run script. clang: note: diagnostic msg:


PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang: note: diagnostic msg: /tmp/mtd-7d9a20.cpp clang: note: diagnostic msg: /tmp/mtd-7d9a20.sh clang: note: diagnostic msg:

******************** make: *** [mtd.o] Error 254

除了生成上述代码外,阅读文档对我没有太大帮助。我有以下问题:

1- 我的通行证代码有什么问题?换句话说,如何添加我想要的代码?

2- 这个错误是什么意思?除了分段错误和命令无法执行之外,我在其中看不到任何有意义的消息(可能是因为我是新手?)。

3- 我知道如何插入对我在另一个 C++ 文件(如 "function")中编写的函数的调用,而不是对函数而不是对 C++ 库中定义的函数的调用,例如这个 chrono 函数,这就是为什么我写道“std::chrono::duration_cast

抱歉,如果我的问题太基础了。帮助和指导将不胜感激!

由于 Instruction 继承自 LLVM 中的 Value,在将函数作为调用指令插入后,您只需将该指令传递给下一个函数即可。不需要您尝试执行的加载存储操作。所以调用你的时间函数并将你用来调用它的指令对象传递给"function".