Help to understand LoadValueFromMemory

Hi all,
I’m trying to print to screen the value read by the fread function. I’m at the point where source refers to the GetElementPtrInst ( pointer to the buffer where fread stored the data - %5 in my case ) and the fread() has been already called.

I thought the correct approach to achieve what I need was:

ExecutionContext& SF = ECStack.back();
GenericValue SRC = getOperandValue(source, SF);
GenericValue* Ptr = (GenericValue*)GVTOP(SRC);
GenericValue Result;

source->getPointerOperand()->getType()->dump(); //[40 x i8]*

LoadValueFromMemory(Result, Ptr, source->getPointerOperand()->getType());

and use Result.PointerVal …

In the end I have the first 4 bytes within Result.PointrVal but I cannot see the other bytes… Unfortunately also Result.AggregateVal has size 0.

Could you help me to understand why please?

The core part of the .ll is the following:

define i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca %struct._IO_FILE*, align 8
%3 = alloca [40 x i8], align 16
store i32 0, i32* %1, align 4
%4 = call %struct._IO_FILE* @fopen(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0))
store %struct._IO_FILE* %4, %struct._IO_FILE** %2, align 8
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i32 0, i32 0
%6 = load %struct._IO_FILE*, %struct._IO_FILE** %2, align 8
%7 = call i64 @fread(i8* %5, i64 30, i64 1, %struct._IO_FILE* %6)
%8 = load %struct._IO_FILE*, %struct._IO_FILE** %2, align 8
%9 = call i32 @fclose(%struct._IO_FILE* %8)
%10 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%11 = load i8, i8* %10, align 16
%12 = sext i8 %11 to i32
%13 = call i32 (i8*, …) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i32 0, i32 0), i32 %12)
%14 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%15 = load i8, i8* %14, align 16
%16 = sext i8 %15 to i32
ret i32 %16
}

Thanks

Hi all,
sorry I forgot to mention that I understood that the LoadValueFromMemory would execute only:

case Type::PointerTyID:
Result.PointerVal = ((PointerTy)Ptr);
break;

I think this should not be the case considering that Type I think is a Vector.

Thanks

I think this should not be the case considering that Type I think is a Vector.

But from your own code:

source->getPointerOperand()->getType()->dump(); //[40 x i8]*

[40 x i8]* is a pointer to an array. LoadValueFromMemory doesn't seem
to support loading an array type directly, so you either have to load
all bytes individually or use "(char *) Ptr->PointerVal". At least it
looks like that second one ought to work from the implementation of
LoadValueFromMemory.

Cheers.

Tim.

Thanks Tim,
I’ll try tonight to do so and see if I can make it work.

Thanks