如何检查LLVM StoreInst
的存储目标是否是函数指针?
给定一个LLVM加载/存储指令,有两个独立的部分需要计算。首先,是什么类型的位置。第二,该类型是否满足某些属性,等等。
if (StoreInst *si = dyn_cast<StoreInst>(&*I))
{
Value* v = si->getPointerOperand();
Type* ptrType = v->getType()->getPointerElementType();
指针类型就是存储数据的类型。但我们想知道底层类型是否实际上是一个函数,从而使其成为函数指针(或指针指针,等等)。
if (PointerType* pt = dyn_cast<PointerType>(ptrType))
{
do {
// The call to getTypeAtIndex has to be made on a composite type
// And needs explicitly an unsigned int, otherwise 0
// can ambiguously be NULL.
Type* pointedType = pt->getTypeAtIndex((unsigned int)0);
if (pointedType->isFunctionTy())
{
errs() << "Found the underlying function typen";
break;
}
// This may be a pointer to a pointer to ...
ptrType = pointedType;
} while (pt = dyn_cast<PointerType>(ptrType));
这段代码检测到下面的store - store i8* (i8*)* @tFunc, i8* (i8*)** %8, align 8
,它将指向函数tFunc的指针存储到另一个位置。