Expand|Select|Wrap|Line Numbers
- int mem_set_permissions(void* src, int flags) {
- int pagesize = getpagesize();
- if (mprotect(((unsigned char *)src - ((long)src % pagesize)), 100, flags) == -1) {
- return 0;
- }
- return 1;
- }
- void test() {
- unsigned char *code = (unsigned char *)malloc(100);
- mem_set_permissions(code, PROT_READ | PROT_WRITE | PROT_EXEC);
- void(*codeptr)()=(void(*)())code;
- float f[4] __attribute__ ((aligned (16)));
- f[0]=f[1]=f[2]=f[3]=0;
- float **tmp = NULL;
- // set XMM[0] = 0
- *code++ = 0x0F; //Opcode for MOVAPS = OF 28
- *code++ = 0x28;
- *code++ = 0x05; //XMM[0]
- tmp = (float**)(code);
- *tmp = f;
- code += 4;
- *code++ = 0xc3; // OP-code for Return
- (codeptr)();
- }
I then execute the array with the last call.
This works fine on 32-bit architectures (linux), I don't even have to use the mem_set_permissions instruction.
But on 64-bit it crashes on the execution of the array, (segmentation fault), even though I change
code += 4;
to
code += 8;
It works when I only run instructions like:
*code++ = 0xC3
But when I try the more complex instructions like XMM[0] = 0; it crashes.
I compile using gcc:
gcc test_perm_exec.c -o test_perm_exec
I would be very grateful for any help on this!
/Gustav