I've been trying to use mprotect() to change protections on a certain region of memory on Android. However whatever I do I cannot cause the region to be writeable (whatever I do it still seems to remain PROT_READ|PROT_EXEC. Maybe there's something I'm not understanding about Android's memory protections? Will it absolutely always refuse me the right to write into executable memory regions? If so there a system option to disable this?
The code looks like something like this:
int sub() { return 0; }
void main()
{
int pagesize = sysconf(_SC_PAGESIZE); /* 0x1000 */
mprotect( (void *)((int)sub - ((int)sub % pagesize), pagesize, PROT_WRITE);
*((unsigned char *)sub) = 0; /* fails here */
}
I'v tried using mmap and calling the mprotect syscall directly but to no avail. I've also tried PROT_READ|PROT_WRITE.
I've done a similar thing on iOS which works absolutely fine...