如何使用llvm pass工具获取动态分配的堆地址和malloc大小?

问题描述 投票:1回答:1

遍历基本块以获得malloc大小的args和返回地址。

for (auto &BB : F) {
    for (auto Inst = BB.begin(); Inst != BB.end(); Inst++) {
        Instruction &inst = *Inst;    
        if(CallInst* call_inst = dyn_cast<CallInst>(&inst)) {         
           Function* fn = call_inst->getCalledFunction();
           if(fn == "malloc"){
                /* do something to get heap address and malloc size*/
           }
        }
    }
}
dynamic llvm instrumentation
1个回答
0
投票

malloc()本身在运行时选择其地址(某些实现保证每次运行程序时返回值都会有所不同),因此,如果要获取堆地址,则必须用自己的实现替换它。 malloc。

更容易获得malloc的大小:如果callInst-> getArgOperand(0)ConstantInt,则具有该大小。如果不是,您也许可以fold it,但这也许超出您的兴趣了?

© www.soinside.com 2019 - 2024. All rights reserved.