How does Gem5 handle application pointers for an X86 build?

  architecture, c++, gem5, x86

I have a very simple C code where I am initializing a char variable using malloc and storing a string inside it using strcpy. Then I am simply trying to change the first character of the string. Here is a snippet of what my code looks like:

FILE *filp
char *fname = (char*)malloc(strlen("Working.txt")*sizeof(char)+1);
strcpy(fname,"Working.txt");
printf("Address of working.txt: %pn", fname);
fname[0] = 'x';

My purpose is to access the value of the string using the pointer (i.e., fname) inside of gem5. I am trying to do this from src/cpu/o3/dyn_inst_impl.hh in the execute() function. After it calls staticInst->execute(), I have created a condition where if the PC is the instruction doing 'movb $0x78,(%rax)' (i.e., placing "x" at the data pointed to by the address inside rax), I read the value from rax using readIntReg() function and then I am trying to print the string ("Working.txt") using the pointer value I get from rax via this code:

char *ptr = (char*)addr_in_reg;
printf("File name at %p is %cn",addr_in_reg, *ptr);

Where addr_in_reg holds the address of "Working.txt" (I cross-checked it by printing the same address from my binary, and it matches). At this point, gem5 segfaults.

Can someone please help me understand how gem5 deals with application pointers, and do they even point to something? When I debugged this using gdb, it said that variable ptr cannot access the memory location at 0x3260.

P.S. I have also observed that for any application I run in gem5, the heap variable addresses always start from 3260 and then increment according to the size allocated to it.

P.P.S. I run my binary using this command:

build/X86/gem5.opt configs/example/se.py --cpu-type=DerivO3CPU --caches --mem-type=DDR4_2400_8x8 --mem-size=8GB --cmd=test

Source: Windows Questions C++

LEAVE A COMMENT