Can I execute some code(asm -> hex) in the heap memory?The x86 architecture was successful, but armv8 failed. (tested on Android os)Is it because the code is not designed to run in the heap area in the ARM cpu?
thanks for reading
test code
usinged char prefix[] = { 0xE0,0x01,0x80,0x42,0xc0,0x03,0x5F,0xD6}; // ARMv8 asm codechar *func = (char *)mmap(0, 8, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1,0);memcpy (func, prefix, sizeof(prefix));int result = ((int (*)(void))test_function)();return result;
usinged char prefix[] = { 0xE0,0x01,0x80,0x42,0xc0,0x03,0x5F,0xD6}; // ARMv8 asm code
char *func = (char *)mmap(0, 8, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1,0);
memcpy (func, prefix, sizeof(prefix));
int result = ((int (*)(void))test_function)();
return result;
The problem is that the heap is in a data segment, and CPU operating systems nowadays have measures to prevent exactly this kind of behavior. If a segement is marked as NX, you can't execute code from it.
Official Site