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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
---
title: "Just-in-time compilation"
date: 2018-12-05T17:45:53+03:00
draft: false
outputs: ["Reveal"]
---
# Just-in-time compilation
---
### Why
- Interpreter are slow
- Flexibility
- Optimization
---
### What we need
- Executable memory
- Generate code
- Execute code
---
### Executable memory
- Why
- Security concerns
### Create executable memory Linux
virtualCodeAddress = mmap(
NULL,
codeBytes,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE,
0,
0);
---
### Create executable memory Windows
auto const buffer = VirtualAlloc(nullptr, page_size, MEM_COMMIT, PAGE_READWRITE);
VirtualProtect(buffer, code.size(), PAGE_EXECUTE_READ, &dummy);
---
### Create executable memory other platforms
- Consolse
- Mobile
- Pic
---
### How to create and execute code
typedef unsigned (*asmFunc)(void);
....
unsigned char * memory = (unsigned char *) (virtualCodeAddress);
// mov %rdi, %rax
memory[i++] = 0x48; // REX.W prefix
memory[i++] = 0x8b; // MOV opcode, register/register
memory[i++] = 0xc7; // MOD/RM byte for %rdi -> %rax
// ret
memory[i++] = 0xc3; // RET opcode
((asmFunc) (virtualCodeAddress))();
---
### Calling convention
- Standart
- Custom
# ?
|