-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit_virtual_test.cpp
38 lines (31 loc) · 1.11 KB
/
jit_virtual_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <windows.h>
#include <vector>
#include <iostream>
#include <cstring>
void test(long long int val) {
std::cout << "Calling test function:" << val << std::endl;
}
int main() {
unsigned char code[] = {
// Load the address of the target into RAX register.
0x48, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, // mov rax, imm64
0x48, 0xB9, 0,0, 0, 0, 0, 0, 0, 0, // mov rcx, imm64
0x48, 0x83, 0xEC, 0x28, // sub rsp, 40 (Handling of shadow space & alignment)
0xFF, 0xD0,
0x48, 0x83, 0xC4, 0x28,
0xC3 // ret
};
*((void **) &code[2]) = (void *) &test;
*((long long int *) &code[12]) = 12345L;
// Preparation
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
auto const page_size = sys_info.dwPageSize;
auto const code_buffer = VirtualAlloc(nullptr, page_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
::memcpy(code_buffer, code, sizeof(code));
using func_ptr_t = void (*)();
auto const func_ptr = reinterpret_cast<func_ptr_t>(code_buffer);
func_ptr();
VirtualFree(code_buffer, 0, MEM_RELEASE);
return 0;
}