Is there a way for me to detect/initiate-creash-on a write into a string without using mprotect (which I can’t use)?
Currently I can detect the write only in the following read, but that’s too late (the following read can come from a completely different lib).
Note: Using gdb with watchpoints failed due to optimizer moving the string around in the process memory.
Edit: The variable in question is a class member (char*) that contains some metadata as a prefix to a string. The string is the part that needs to be immutable, and the prefix must be writable. I’ve got a few millions of these objects in a class-static hash, and they are accessed from just about anywhere in our code.
You can try to wrap all the code that writes to memory in preprocessor macros which check the address that you’re using but since most people love to use bare bones pointers (instead of library calls that encapsulate things), it will probably be a lot of effort.
The only other option is
mprotect(2)or GDB which all use special parts of the CPU to watch the address bus for accesses to the memory in question.Since you can’t use that either, the last option is to print the code on paper and sit down in a quiet corner for a couple of days to read it. This will usually work but most people shun the effort (and because it doesn’t look like “real” work ;-).