Razer
Razer

Reputation: 8211

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

How to check if the store target of an LLVM StoreInst is a function pointer?

Upvotes: 2

Views: 1094

Answers (1)

Brian
Brian

Reputation: 2813

Given a LLVM load / store instruction, there are two separate pieces to compute. First, what is the type of the location. Second, does the type meet certain properties, et cetera.

if (StoreInst *si = dyn_cast<StoreInst>(&*I))
{
    Value* v = si->getPointerOperand();
    Type* ptrType = v->getType()->getPointerElementType();

Now, the pointer type is just that into which the data is being stored. But we want to know if the underlying type is actually a function, thereby making this a function pointer (or pointer pointer, et cetera).

    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 type\n";
                break;
            }

            // This may be a pointer to a pointer to ...
            ptrType = pointedType;
        } while (pt = dyn_cast<PointerType>(ptrType));

This code detects the following store - store i8* (i8*)* @tFunc, i8* (i8*)** %8, align 8, which stores a pointer to function tFunc into another location.

Upvotes: 3

Related Questions