1

I am quite new to LLVM and KLEE and I am trying to understand whether it is possible to access the content of a structure that is referred to by a pointer. The code I am working on is quite long and complicated, but the problem is the following:

In one of the classes I have the statementes below:

static my_struct * pointer2Structure;

typedef struct {
    <primitive data types variables>
    <other structures>
    <pointers to other structures>
} my_struct;

So far I can access the content of other simple global variables with the following code:

...
ExecutionState state = ...;
const Module *m = ...;
for (const GlobalVariable &v : m->globals()) {
      std::string name = v.getName().data();
      if(!v.isConstant()) {
          globalVariableCounter++;
          Type *type = v.getValueType();
          std::string strType;
          llvm::raw_string_ostream typeInfo(strType);
          type->print(typeInfo);
          strType = typeInfo.str(); 
          
          MemoryObject *mo = globalObjects.find(&v)->second;
          const ObjectState *os = state.addressSpace.findObject(mo);

          strValue = os->getValue(strType);

          std::string info = "\nGlobal variable info:\n\t name = \t" + name
                          + "\n\t typeID = \t" + getTypeName(type->getTypeID())
                          + "\n\t type = \t" + strType
                          + "\n\t value = \t" + strValue;

        }
      }
...

ExecutionState, MemoryObject, and ObjectState are classes defined in KLEE. The function os->getValue(strType) is a user defined function to get the content of the ObjectState.

I can see pointer2Structure based on the variable name, but since it is a pointer (PointerType) I don't know how to access its content.

Is there any way to iterate over the elements of the structure knowing the name of the pointer to the structure? If so, is it possible to recursively examine the content of nested structures?

I am using LLVM 9.0 and I am compiling the code with -O0 -d to enable debug information.

Thank you very much for your help.

fabCic
  • 11
  • 2
  • In LLVM 9.0, PointerType still offers a metod called getElementType, which returns the type pointed to. It's gone in 10.0 IIRC, which is a long story, but for now, that's probably the method you want. – arnt Nov 08 '20 at 10:27
  • Thank you very much, it helps me obtain the structure with all the field types. Do you know how to iterate over the structure's elements? Thank you very much for your help. – fabCic Nov 08 '20 at 21:57
  • please post the solution if you managed to iterate the elements of nested struct? – Mohannad Jun 30 '21 at 03:19

0 Answers0