1

I'm building very small test program and I wanted to have the program access the same memory address every time(I know its not a good practice) to simulate some behaviors. How can I just pick a memory address to hard code in the program an try it out? Is there a way to see unused blocks of memory addresses and just block them ?

I totally understand that this might create unwanted conditions/situation.

Cac3a
  • 117
  • 1
  • 10
  • On an embedded system this might be normal, and peripherals may be memory mapped and accessible. On a PC you won't get the same address space every time due to "address space randomisation" intended to thwart attackers. – Weather Vane May 05 '20 at 19:39
  • what about using memory map file ? (mmap) like in https://stackoverflow.com/questions/29210851/c-c-memory-map-file-using-mmap ? each time you restart program you use values saved in the file rather than than reset values – bruno May 05 '20 at 19:40
  • 1
    I suggest you allocate a block of memory either with a static array or with `malloc` and work with a pointer to a fixed offset within that space. – Weather Vane May 05 '20 at 19:42

1 Answers1

1

You can use ampersand operator (&) to point a pointer to a specific memory address. However, your program must be able to able to legally access that address which is decided by what address range your OS has assigned to your program otherwise you will a segmentation fault.

Sample code:

void * p1 = (void *)0x28ff44;

Or if you want it as a char pointer:

char * p2 = (char *)0x28ff44;

PS

You can find out the address allocated to your program and take one of the addresses from it into your program. For a single run, your program will access the same memory location but for another run, it will be different one assigned to your process but same for that run.

You can refer here to check how you can read memory address assigned to your process. You can take input at runtime to provide your process id to get the filepath.

Work around

Since you mentioned it is small test program, you can also save yourself your efforts by just disabling randomization of memory addresses by disabling ASLR for your testing your program, you just disable ASLR in linux using

echo 0 > /proc/sys/kernel/randomize_va_space

and then run your program, declare and initialize a variable, print its address and then hardcode that address in your program. Bingo!! Everytime that address will be used untill you enable ASLR again.

However it is not secure to turn off ASLR and after testing you should enable ASLR again by

echo 1 > /proc/sys/kernel/randomize_va_space
CodeTalker
  • 1,683
  • 2
  • 21
  • 31
  • What if I wanted to have two programs read/write to single memory location and exchange information through it. Is that possible or only one will be able to write to it? – Cac3a May 05 '20 at 19:57
  • 1
    Well, that is not allowed using the above approaches as OS will not allow you to access and molest memory addresses of other programs this way and cause `segmentation fault`. You need to *legally* **share memory location** between your programs, refer https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c here for that. After that, you do whatever with that memory location using those programs. – CodeTalker May 05 '20 at 20:03
  • Is it possible to disable randomization of memory address on windows? – Cac3a May 15 '20 at 01:47
  • Yes it is. For windows 10 you can use https://gist.github.com/trietptm/b84ccad9db01f459ac7e (credit goes to the person owning git repo) or you can manually edit registry files. The sequence of steps can be found simply by google searching "disable ASLR in windows". – CodeTalker May 15 '20 at 02:50