LLVM 检测 C++ 锁定和解锁函数

问题描述

意图:给定一些源代码,我的意图是检测临界区和相关锁的进入/退出

方法:我使用了一个基本上遍历所有直接函数调用函数传递,并根据我的 {mutex}.lock(){mutex}.unlock() 错误名称检查名称

问题:我不知道如何获取调用 lock() 的互斥体实例,然后将其作为参数传递给我的钩子函数(例如 enter_cs(mtx))。>

我查看了 .ll 代码并看到了这个

call void @_ZNSt3__15mutex4lockEv(%"class.std::__1::mutex"* nonnull dereferenceable(64) @mtx)

我不知道如何访问@mtx。 我是 LLVM 的新手,所以请帮忙。另外如果有更好的方法解决整体问题,请解释!

示例代码

mutex mtx;

void enter_cs(mutex mtx) {
    /** some code **/
    return;
}

void exit_cs(mutex mtx) {
    /** some code **/
    return;
}

int main() {
    mtx.lock();
    /** LLVM inserted call: enter_cs(mtx) **/
    // critical section code //
    mtx.unlock();
    /** LLVM inserted call: exit_cs(mtx) **/
    return 0;
}

LLVM 通行证:

static StringRef mutexLock = "_ZNSt3__15mutex4lockEv";
bool runOnFunction(Function &F) override {
    for(BasicBlock &bb : F) {
            for(Instruction &i : bb) {
                    if(!isa<CallInst>(i)) continue; // should be a call instruction
                    CallInst *ci = cast<CallInst>(&i);
                    Function* f = ci->getCalledFunction();
                    if(!f) continue; // should be a direct call
                    if(f->getName()==mutexLock) {
                        errs() << "locked" << ",From: " << F.getName() << "\n";
                        /** enter_cs({mutex object on which lock is called}) **/
                    }
                }
            }
            return false;
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)