微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。