I need help figuring out why I am crashing at runtime when I am trying to run my machine code. The code crashes in between "3" and "result = %li".
NOTE: Assume all code is error checked. I removed error checking code for the sake of people reading this.
Machine specs:
Windows 10 Home 64-bit
Intel(R) Core(TM) i7-4712MQ
Code I am running:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char** argv) {
printf("Loading code\n");
LPVOID m = VirtualAlloc(NULL, 16, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
printf("1\n");
unsigned char code[] = {
//return x+2;
0x55, //push %rbp
0xe5, 0x89, 0x48, //mov %rsp,%rbp
0xc8, 0x89, //movl %ecx,%eax
0x02, 0xc0, 0x83, //add $0x2,%eax
0xc9, //leaveq
0xc3, //retq
0x90 //nop
};
memcpy(m, code, sizeof(code));
printf("2\n");
PDWORD trash;
VirtualProtect(m, 16, PAGE_EXECUTE_READ, trash);
printf("3\n");
long int (*addTwo)(long int) = m;
long int answer = addTwo(2);
printf("result = %li\n", answer);
VirtualFree(m, 0, MEM_RELEASE);
return 0;
}