如何检查 LLVM AllocaInst 的目标是否为函数指针

How to check if a target of an LLVM AllocaInst is a function pointer

%pointer = alloca void (i32)*, align 8 如何判断%pointer是否为函数指针?能否获取函数指针的参数列表?

让我们创建一个函数来检查 Alloca Instruction Type 是否是一个函数指针。

bool  isFunctionPointerType(Type *type){
      // Check the type here
     if(PointerType *pointerType=dyn_cast<PointerType>(type)){
        return isFunctionPointerType(pointerType->getElementType());
     }
        //Exit Condition
        else if(type->isFunctionTy()){
        return  true;
        }
       return false;
  }

在你的runOnModule/runOnFunction通过

if(AllocaInst *allocaInst=dyn_cast<AllocaInst>(inst)){
     if(isFunctionPointerType(allocaInst->getType())){
     errs()<<"Funtion Pointer Type\n";
     }
  }

以上通过在以下source.c代码

上进行测试
#include <stdio.h>

void fun(int a)
{
    printf("Value of a is %d\n", a);
}

int main()
{

    void (*fun_ptr)(int) = &fun;

    (*fun_ptr)(10);

    return 0;
}

没有任何优化的相应 LLVM Bitcode

entry:
%retval = alloca i32, align 4
%fun_ptr = alloca void (i32)*, align 8
store i32 0, i32* %retval, align 4
call void @llvm.dbg.declare(metadata void (i32)** %fun_ptr, metadata !11,
... metadata !15), !dbg !16
store void (i32)* @_Z3funi, void (i32)** %fun_ptr, align 8, !dbg !16
%0 = load void (i32)*, void (i32)** %fun_ptr, align 8, !dbg !17
call void %0(i32 10), !dbg !18
ret i32 0, !dbg !19

成功将 func_ptr 检测为函数指针。

请注意,代码使用 recursion 来查找类型 recursively

另一种方法是在 LLVM 中使用 def-use 链来跟踪 func_ptr 的使用,即通过跟踪 StoreInst 并检查源操作数是否是指向函数的指针:haven试试吧。

希望这对您有所帮助... 如果有帮助,请将其标记为正确的解决方案或点赞..谢谢..