-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmmap-demo.c
35 lines (31 loc) · 1.03 KB
/
mmap-demo.c
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
#define _GNU_SOURCE
#include <assert.h> /* for assert */
#include <stddef.h> /* for NULL */
#include <string.h> /* for memcpy */
#include <sys/mman.h> /* for mmap */
#undef _GNU_SOURCE
// clang-format off
const unsigned char program[] = {
// mov eax, 42 (0x2a)
0xb8, 0x2a, 0x00, 0x00, 0x00,
// ret
0xc3,
};
// clang-format on
const int kProgramSize = sizeof program;
typedef int (*JitFunction)();
int main() {
void *memory = mmap(/*addr=*/NULL, kProgramSize, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
/*filedes=*/-1, /*off=*/0);
memcpy(memory, program, kProgramSize);
int result = mprotect(memory, kProgramSize, PROT_EXEC);
assert(result == 0 && "mprotect failed");
JitFunction function = *(JitFunction *)&memory;
int return_code = function();
assert(return_code == 42 && "the assembly was wrong");
result = munmap(memory, kProgramSize);
assert(result == 0 && "munmap failed");
// Return 0 so we can run this as a test and not stop Make
return 0;
}