user3090584
user3090584

Reputation: 1

how to debug memory overwriting on linux

I met a issue. The value of pointer was modified and the pointer pointed to a address which can't be accessed. BTW, the address of pointer is on heap. I'm wondering is there any way to set the pointer read-only after it's initialized, and when someone try to change the value of pointer, the process will crash.

Thanks. Jerry

Upvotes: 0

Views: 347

Answers (3)

Siva
Siva

Reputation: 141

Use the following command to generate map file in your terminal window in Linux :

gcc -o demo -Wl,-Map,demo.map demo.c

You can easily find out where your memory usage goes wrong or in which place the overwritting takes place. This map file will help you know about each section of the memory in detail.

Upvotes: 0

You can use valgrind to detect invalid pointer access as well as many other invalid memory usages.

Upvotes: 2

Sergey L.
Sergey L.

Reputation: 22542

You can use mprotect, but I would advise against modifying heap pages since those are managed my libc.

 mprotect(page_aligned_address, len, PROT_READ);

If you intend to use that I recommend you allocate your memory using mmap in order not to mess up your heap.

Upvotes: 0

Related Questions