mprotect and SIGSEGV
I'm working on a program which needs to alter the access rights of the memory that the executable isloaded into.
So at runtime the program finds the addresses of the area of memory it wants to change (within the executable), sets it to a page boundary and then calls mprotect with PROT_READ | PROT_WRITE | PROT_EXEC. This seems fine in all of the cases I've tried it, mprotect doesn't ever seem to fail and everything seems hunky-dork.
However, in some cases, if I then come to try and alter the memory I've set the permissions for, I get a signal 11 SIGSEGV. And I'm confused. Surely if I couldn't set the access rights mprotect would fail? And if mprotect doesn't fail, how come I can't change the memory which I've set to PROT_WRITE?