LoadInst和StoreInst值和地址LLVM [英] LoadInst and StoreInst Values and addresses LLVM
问题描述
我有一个文件print.c,它具有两个功能:
I have a file print.c, which has two functions:
void printLoad(...) {
// print address and value of memory location from which value
printf("address=... value=...", ...);
}
void printStore(...) {
// print address and value of memory location from which value
}
我有一个 LLVM 传递,它遍历指令并在当前指令(加载/存储指令)之后添加 CallInst 指令 printLoad 或 printStore(取决于指令类型).
I have an LLVM pass which iterates over the instructions and adds CallInst instruction either printLoad or printStore (depending on the instruction type) after the current one (load/store inst).
为了调用此printStore或printLoad,我需要向CallInst :: Create函数添加适当的参数,这些参数是内存位置的地址和值.
In order to call this printStore or printLoad I need to add appropriate arguments to CallInst::Create function, which are the address and the value of the memory location.
这是我要实现的示例:
define void @mains() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
store i32 5, i32* %1, align 4
store i32 2, i32* %2, align 4
store i32 4, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = add nsw i32 %3, 5
store i32 %4, i32* %1, align 4
ret void
}
The output should be:
store instruction:
address=... // address of %1
value=0
...
...
...
load instruction:
address=... // address of %2
value=4
store instruction:
address=... // address of %1
value=9
到目前为止的进展:
我可以使用LoadInst/StoreInst上的getPointerOperand()获取操作数的地址.
I am able to get the addresses of the operands using getPointerOperand() on LoadInst/StoreInst.
我也可以通过将操作数强制转换为ConstantInt来获取前4条存储指令中StoreInst的值,但是我不知道如何提取最后一条StoreInst中的值.甚至有可能吗?
I can also get the value of StoreInst in the first 4 store instructions by casting the operand to ConstantInt, but I don't know how to extract the value in the last StoreInst. Is it even possible?
已编辑
使用
void printLoad(int32_t p)
和
Constant *hookLoadFunc = M.getOrInsertFunction("printLoad", Type::getVoidTy(M.getContext()), Type::getInt32Ty(M.getContext()));
.
%1 = alloca i32, align 4
%2 = alloca i32, align 4
%3 = alloca i32, align 4
store i32 0, i32* %1, align 4
call void @printStore(i32 0)
store i32 0, i32* %2, align 4
call void @printStore(i32 0)
store i32 5, i32* %2, align 4
call void @printStore(i32 5)
store i32 2, i32* %3, align 4
call void @printStore(i32 2)
store i32 4, i32* %3, align 4
call void @printStore(i32 4)
%4 = load i32, i32* %3, align 4
%5 = add nsw i32 %4, 5
store i32 %5, i32* %2, align 4
call void @printStore(i32 %5)
ret i32 0
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
call void @printStore(i32 %0)
%3 = load i32, i32* %2, align 4
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i32 0, i32 0), i32 %3)
ret void
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
call void @printStore(i32 %0)
%3 = load i32, i32* %2, align 4
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i32 0, i32 0), i32 %3)
ret void
这会导致分段错误:运行时为11.
This causes Segmentation fault: 11 when run.
已解决:
弄清楚我有无限循环(由于递归). printStore 实际上使用加载/存储指令,从而创建另一个对 printStore 的调用,依此类推.
Figured out that I had infinity loop (due to recursion). printStore actually uses load/store instructions, thus creating another call to printStore and so on.
推荐答案
假设您有一个表示 printLoad()
和 printStore的
: llvm :: Function
()
Assuming that you have an llvm::Function
that represents printLoad()
and printStore()
:
llvm::Function * print_load = ....
llvm::Function * print_store = ...
您可以为每个 LoadInst
和 StoreInst
发出 CallInst
.
对于LoadInst
:
LoadInst * some_load = ...
Value * address_of_load = some_load->getOperand(0);
Value * print_load_arguments[] = { address_of_load, some_load };
// Insert a CallInst just after the load.
CallInst::Create(print_load, print_load_arguments )->insertAfter( some_load );
请记住,在llvm中, LoadInst
加载的值与 LoadInst
本身相同.
Remember that in llvm the value loaded by the LoadInst
is the same thing as the LoadInst
itself.
对于 StoreInst
:
StoreInst * some_store = ...
Value * value_to_store = some_store->getOperand(0);
Value * address_of_store = some_store->getOperand(1);
Value * print_store_arguments[] = { address_of_store, value_to_store };
// Insert a CallInst just after the store.
CallInst::Create(print_store, print_store_arguments)->insertAfter(some_store);
如果所有类型都匹配,这将起作用.否则,您必须在调用 printStore()
或 printLoad()
之前插入 BitCast
指令.
This will work if all the types match. Otherwise, you have to insert BitCast
instructions just before calling printStore()
or printLoad()
.
这篇关于LoadInst和StoreInst值和地址LLVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!