-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
350 lines (289 loc) · 9.63 KB
/
code.c
File metadata and controls
350 lines (289 loc) · 9.63 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "code.h"
#include "memory.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/mman.h>
#include <errno.h>
#define ROUND_UP_PAGE(pointer, pageSize) ((void *) (((uintptr_t) pointer + pageSize - 1) & ~(pageSize - 1)))
#define ROUND_DOWN_PAGE(pointer, pageSize) ((void *) ((uintptr_t) pointer & ~(pageSize - 1)))
#define MINIMUM_SEGMENT_SIZE (128 << 10)
#define UNDEFINED_INSTRUCTION 0xFF
static inline
size_t roundToPageSize(size_t size, size_t pageSize)
{
size_t mask = pageSize - 1;
return ~mask & (size + mask);
}
static
int changeMemoryProtection(void *address, size_t size, int protection)
{
int result = mprotect(address, size, protection);
if (result == -1) {
return errno;
}
return 0;
}
static
void setWritable(Code *code, void *from)
{
if (code->writable > from) {
if (changeMemoryProtection(from, code->writable - from, PROT_WRITE)) {
printf("Unable to move write border left. Exiting...");
exit(1);
}
code->writable = from;
} else if (code->writable < from) {
if (changeMemoryProtection(code->writable, from - code->writable, PROT_EXEC)) {
printf("Unable to move write border right. Exiting...");
exit(1);
}
code->writable = from;
}
}
static
int changeSegmentProtection(Code *code, int protection)
{
return changeMemoryProtection(code->start, code->end - code->start, protection);
}
static
void makeSegmentWritable(Code *code)
{
if (changeSegmentProtection(code, PROT_WRITE)) {
printf("Unable to make code segment writable. Exiting...");
exit(1);
}
code->writable = code->start;
}
static
void makeSegmentExecutable(Code *code)
{
if (changeSegmentProtection(code, PROT_EXEC)) {
printf("Unable to make code segment writable. Exiting...");
exit(1);
}
code->writable = code->end;
}
static
void *createNewMapping(size_t size)
{
void *space = mmap(NULL, size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (space == (void *) -1) {
printf("Unable to create mapping for JIT code. Exiting...");
exit(1);
}
return space;
}
static
void createInitialSegment(Code *code, size_t size)
{
size_t pageSize = getpagesize();
assert((size & (pageSize - 1)) == 0 && "Size should be a multiple of page size.");
void *space = createNewMapping(size);
code->start = space;
code->end = space + size;
code->free = space;
code->writable = space;
code->available = size;
size_t initialRedZone = JIT_RED_ZONE * sizeof(uint32_t);
memset(code->free, UNDEFINED_INSTRUCTION, initialRedZone);
code->free += initialRedZone;
code->available -= initialRedZone;
}
static void *createFixedMapping(void *address, size_t size)
{
size_t pageSize = getpagesize();
assert(((uintptr_t) address & (pageSize - 1)) == 0 && "address should be a multiple of page-aligned.");
assert((size & (pageSize - 1)) == 0 && "Size should be a multiple of page size.");
#ifdef MAP_FIXED_NOREPLACE
void *space = mmap(address, size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
#else
void *space = mmap(address, size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
if (space != (void *) -1 && space != address) {
munmap(space, size);
errno = EEXIST;
return (void *) -1;
} else if (space == (void *) -1) {
return (void *) -1;
}
return space;
}
static
int expandSegment(Code *code, size_t size)
{
void *space = createFixedMapping(code->end, size);
if (space == (void *) -1) {
return errno;
}
return 0;
}
static
void expandOrMoveSegment(Code *code, size_t additionalSize)
{
assert(false);
if (expandSegment(code, additionalSize) == 0) {
code->available += additionalSize;
code->end += additionalSize;
return;
}
assert(false);
// We need to move the code.
// Create a new segment with the desired size.
// Sort function list.
// Write functions into new segment.
// (If compiling while code is running) Modify return addresses.
// unmap old segment and set on code.
}
static
void *copyFunctionCode(ObjFunction **functions, size_t functionCount, void *destination)
{
uint32_t *write = destination;
write += JIT_RED_ZONE;
ObjFunction **current = functions;
ObjFunction **const end = functions + functionCount;
ObjFunction *function;
for (; (function = *current)->chunk.code == write; current++) {
write += function->chunk.count + JIT_RED_ZONE;
}
for (; current != end; current++) {
ObjFunction *function = *current;
uint32_t *writeEnd = write + function->chunk.count;
size_t count = function->chunk.count;
size_t size = count * sizeof(uint32_t);
if (function->chunk.code < writeEnd) {
memmove(write, function->chunk.code, size);
} else {
memcpy(write, function->chunk.code, size);
}
function->chunk.code = write;
write += count;
memset(write, UNDEFINED_INSTRUCTION, JIT_RED_ZONE * sizeof(uint32_t));
write += JIT_RED_ZONE;
current++;
}
return write;
}
static
void compactExisting(Code *code)
{
// At this point we could trigger a GC, if the mutator has done sufficient
// work before compilation started, to not copy any unreachable functions.
makeSegmentWritable(code);
code->free = copyFunctionCode(code->functions, code->functionCount, code->start);
code->available = code->end - code->free;
}
void *writeNewFunctions(ObjFunction **functions, size_t functionCount, void *destination)
{
uint32_t *write = destination;
ObjFunction **current = functions;
ObjFunction **const end = functions + functionCount;
for (; current != end; current++) {
ObjFunction *function = *current;
memcpy(write, function->chunk.code, function->chunk.count * sizeof(uint32_t));
// TODO: Move to helper function.
FREE_ARRAY(uint32_t, function->chunk.code, function->chunk.capacity);
function->chunk.code = write;
function->chunk.isExecutable = true;
write += function->chunk.count;
memset(write, UNDEFINED_INSTRUCTION, JIT_RED_ZONE * sizeof(uint32_t));
write += JIT_RED_ZONE;
}
return write;
}
void initCode(Code *code)
{
code->start = NULL;
code->end = NULL;
code->free = NULL;
code->writable = NULL;
code->available = 0;
code->functions = NULL;
code->functionCount = 0;
code->functionCapacity = 0;
}
void freeCode(Code *code)
{
if (code->start) {
munmap(code->start, code->end - code->start);
}
code->start = NULL;
code->end = NULL;
code->free = NULL;
code->writable = NULL;
code->available = 0;
free(code->functions);
code->functions = NULL;
code->functionCount = 0;
code->functionCapacity = 0;
}
void codePushFunctions(Code *code, ObjFunction **functions, size_t count, size_t combinedSize, bool isREPL)
{
size_t pageSize = getpagesize();
size_t requiredSize = (count * JIT_RED_ZONE + combinedSize) * sizeof(uint32_t);
if (code->start == NULL) {
size_t size = requiredSize + JIT_RED_ZONE * sizeof(uint32_t);
if (isREPL) {
if (size < MINIMUM_SEGMENT_SIZE) {
size = MINIMUM_SEGMENT_SIZE;
} else { // Exceedingly unlikely for REPL.
size = size * 2;
}
} else {
// We currently don't expect file runs to grow the code segment.
}
size = roundToPageSize(size, pageSize);
createInitialSegment(code, size);
}
if (code->end - code->free > requiredSize) {
setWritable(code, ROUND_DOWN_PAGE(code->free, pageSize));
void *newFree = writeNewFunctions(functions, count, code->free);
code->available -= newFree - code->free;
code->free = newFree;
setWritable(code, ROUND_UP_PAGE(code->free, pageSize));
// mprotect(exec) from first page to page with free in it.
size_t requiredCapacity = code->functionCount + count;
if (code->functionCapacity < requiredCapacity) {
size_t newCapacity = code->functionCapacity;
while (newCapacity < requiredCapacity) {
newCapacity = GROW_CAPACITY(code->functionCapacity);
}
code->functions = realloc(code->functions, newCapacity * sizeof(ObjFunction *));
code->functionCapacity = newCapacity;
if (code->functions == NULL) {
exit(1);
}
}
memcpy(code->functions + code->functionCount, functions, count * sizeof(ObjFunction *));
code->functionCount += count;
} else if (code->available >= requiredSize) {
compactExisting(code);
void *newFree = writeNewFunctions(functions, count, code->free);
code->available -= newFree - code->free;
code->free = newFree;
setWritable(code, ROUND_UP_PAGE(code->free, pageSize));
} else {
// TODO: Expand segment.
assert(false);
}
}
void codeRemoveWhite(Code *code)
{
ObjFunction **write = code->functions;
ObjFunction **read = code->functions;
ObjFunction **const end = code->functions + code->functionCount;
size_t freed = 0;
while (read != end) {
ObjFunction *function = *read;
if (function->obj.isMarked) {
*write++ = *read;
} else {
freed += (function->chunk.count + JIT_RED_ZONE) * sizeof(uint32_t);
}
read++;
}
code->functionCount -= read - write;
code->available += freed;
}