forked from GJDuck/e9patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe9alloc.cpp
More file actions
370 lines (338 loc) · 9.33 KB
/
e9alloc.cpp
File metadata and controls
370 lines (338 loc) · 9.33 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* e9alloc.cpp
* Copyright (C) 2020 National University of Singapore
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cassert>
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/mman.h>
#include "e9rbtree.h"
#include "e9alloc.h"
#include "e9patch.h"
#include "e9trampoline.h"
#define BLACK 0 // RB-tree black
#define RED 1 // RB-tree red
#define MIN_SIZE /*sizeof(jmpq)=*/5
/*
* Interval tree node.
*/
struct Node
{
Alloc alloc; // Allocation
RB_ENTRY(Node) entry; // RB-tree entry
uint64_t full:63; // Number of allocation bytes in (ub - lb)
uint64_t color:1; // RB-tree node color
intptr_t lb; // tree lower bound
intptr_t ub; // tree upper bound
};
/*
* Node comparison.
*/
static inline int compare(const Node *a, const Node *b)
{
if (a->alloc.ub <= b->alloc.lb)
return -1;
else if (a->alloc.lb >= b->alloc.ub)
return 1;
return 0;
}
/*
* Restore interval-tree invariants after a node after child node modification.
*/
static void fix(Node *n)
{
if (n == nullptr)
return;
Node *l = n->entry.left, *r = n->entry.right;
intptr_t llb = (l != nullptr? l->lb: INTPTR_MAX);
intptr_t rub = (r != nullptr? r->ub: INTPTR_MIN);
n->lb = std::min(llb, n->alloc.lb);
n->ub = std::max(rub, n->alloc.ub);
uint64_t lfull = (l == nullptr? 0: l->full);
uint64_t rfull = (r == nullptr? 0: r->full);
n->full = (n->alloc.ub - n->alloc.lb) + lfull + rfull;
}
/*
* Interval-tree definition.
*/
#define RB_AUGMENT(n) fix(n)
RB_GENERATE_STATIC(Tree, Node, entry, compare);
#define rebalanceInsert(t, n) Tree_RB_INSERT_COLOR((t), (n))
#define rebalanceRemove(t, n) Tree_RB_REMOVE((t), (n))
template <bool LEFT>
static Node *insert(Node *root, intptr_t lb, intptr_t ub, size_t size);
/*
* Allocate a node.
*/
static Node *alloc()
{
Node *n = (Node *)malloc(sizeof(Node));
if (n == nullptr)
error("failed to allocate %zu bytes for interval tree node: %s",
sizeof(Node), strerror(ENOMEM));
n->alloc.T = nullptr;
n->alloc.I = nullptr;
return n;
}
/*
* Allocate and initialize a new interval tree node.
*/
static Node *node(Node *parent, bool left, intptr_t lb, intptr_t ub,
size_t size)
{
Node *n = alloc();
if (left)
{
n->alloc.lb = lb;
n->alloc.ub = lb + size;
n->lb = lb;
n->ub = lb + size;
}
else
{
n->alloc.lb = ub - size;
n->alloc.ub = ub;
n->lb = ub - size;
n->ub = ub;
}
n->entry.parent = parent;
n->entry.left = nullptr;
n->entry.right = nullptr;
n->color = RB_RED;
n->full = size;
return n;
}
/*
* Insert left-child helper.
*/
template <bool LEFT>
static Node *insertLeftChild(Node *root, intptr_t lb, intptr_t ub, size_t size)
{
if ((intptr_t)size > ub - lb)
return nullptr;
Node *n;
if (root->entry.left == nullptr)
n = root->entry.left = node(root, LEFT, lb, ub, size);
else
n = insert<LEFT>(root->entry.left, lb, ub, size);
return n;
}
/*
* Insert right-child helper.
*/
template <bool LEFT>
static Node *insertRightChild(Node *root, intptr_t lb, intptr_t ub, size_t size)
{
if ((intptr_t)size > ub - lb)
return nullptr;
Node *n;
if (root->entry.right == nullptr)
n = root->entry.right = node(root, LEFT, lb, ub, size);
else
n = insert<LEFT>(root->entry.right, lb, ub, size);
return n;
}
/*
* Insert a new allocation or reservation into the interval tree node `root`.
*/
template <bool LEFT>
static Node *insert(Node *root, intptr_t lb, intptr_t ub, size_t size)
{
if ((intptr_t)size > ub - lb)
return nullptr;
if (root == nullptr)
return node(nullptr, LEFT, lb, ub, size);
Node *n = nullptr;
if (n == nullptr && ub <= root->lb)
n = insertLeftChild<LEFT>(root, lb, ub, size);
if (n == nullptr && lb >= root->ub)
n = insertRightChild<LEFT>(root, lb, ub, size);
if (n == nullptr && lb < root->lb)
n = insertLeftChild</*LEFT=*/false>(root, lb, root->lb, size);
if (n == nullptr && ub > root->ub)
n = insertRightChild</*LEFT=*/true>(root, root->ub, ub, size);
if (n == nullptr)
{
uint64_t range = (uint64_t)root->ub - (uint64_t)root->lb;
uint64_t full = root->full;
bool internal = (range - full >= size);
if (internal)
{
// As an optimization, we may only explore one branch if the node
// is too full:
bool once = (range >= UINT32_MAX? false:
100 * full >= option_aggressiveness * range);
if (LEFT)
{
if (n == nullptr && internal)
n = insertLeftChild</*LEFT=*/false>(root,
lb, std::min(ub, root->alloc.lb), size);
if (n == nullptr && internal && !once)
n = insertRightChild</*LEFT=*/true>(root,
std::max(lb, root->alloc.ub), ub, size);
}
else
{
if (n == nullptr && internal)
n = insertRightChild</*LEFT=*/true>(root,
std::max(lb, root->alloc.ub), ub, size);
if (n == nullptr && internal && !once)
n = insertLeftChild</*LEFT=*/false>(root,
lb, std::min(ub, root->alloc.lb), size);
}
}
}
if (n != nullptr)
fix(root);
return n;
}
/*
* Verify bounds.
*/
static bool verify(intptr_t lb, intptr_t ub)
{
if (lb > ub)
return false;
if (IS_RELATIVE(lb))
return IS_RELATIVE(ub);
if (IS_ABSOLUTE(lb))
return IS_ABSOLUTE(ub);
return false;
}
/*
* Allocates a chunk of virtual address space of size `size` and within the
* range [lb..ub]. Returns the allocation, or nullptr on failure.
*/
const Alloc *allocate(Allocator &allocator, intptr_t lb, intptr_t ub,
const Trampoline *T, const Instr *I)
{
if (!verify(lb, ub + TRAMPOLINE_MAX))
return nullptr;
int r = getTrampolineSize(T, I);
if (r < 0)
return nullptr;
size_t size = (size_t)r;
ub += size;
Node *n = insert</*LEFT=*/true>(allocator.tree.root, lb, ub, size);
if (n == nullptr)
return nullptr;
if (allocator.tree.root == nullptr)
allocator.tree.root = n;
rebalanceInsert(&allocator.tree, n);
Alloc *A = &n->alloc;
A->T = T;
A->I = I;
return A;
}
/*
* Reserves a chunk of the virtual address space spanning the range [lb..ub].
* Returns `true` on success, `false` on failure.
*/
bool reserve(Allocator &allocator, intptr_t lb, intptr_t ub)
{
if (!verify(lb, ub))
return false;
lb -= (lb % PAGE_SIZE);
ub += (ub % PAGE_SIZE == 0? 0: PAGE_SIZE - ub % PAGE_SIZE);
if (ub - lb <= 0)
return false;
Node *n = insert</*LEFT=*/true>(allocator.tree.root, lb, ub, (ub - lb));
if (n == nullptr)
return false;
if (allocator.tree.root == nullptr)
allocator.tree.root = n;
rebalanceInsert(&allocator.tree, n);
Alloc *A = &n->alloc;
A->T = nullptr;
A->I = nullptr;
return true;
}
/*
* Deallocate an existing allocation.
*/
void deallocate(Allocator &allocator, const Alloc *a)
{
if (a == nullptr)
return;
Node *n = (Node *)(a);
assert(n->alloc.T != nullptr);
rebalanceRemove(&allocator.tree, n);
free(n);
}
/*
* Iterators.
*/
const Alloc *Allocator::iterator::operator*()
{
return &node->alloc;
}
static Node *next(Node *n)
{
if (n == nullptr)
return n;
if (n->entry.right != nullptr)
{
n = n->entry.right;
while (n->entry.left != nullptr)
n = n->entry.left;
return n;
}
else
{
while (true)
{
Node *parent = n->entry.parent;
if (parent == nullptr)
return nullptr;
if (parent->entry.left == n)
return parent;
n = parent;
}
}
}
void Allocator::iterator::operator++()
{
node = next(node);
}
Allocator::iterator Allocator::begin() const
{
Node *n = tree.root;
if (n == nullptr)
return end();
while (n->entry.left != nullptr)
n = n->entry.left;
Allocator::iterator i = {n};
return i;
}
Allocator::iterator Allocator::find(intptr_t addr) const
{
Node *n = this->tree.root;
while (n != nullptr)
{
if (addr < n->alloc.lb)
n = n->entry.left;
else if (addr >= n->alloc.ub)
n = n->entry.right;
else
break;
}
Allocator::iterator i = {n};
return i;
}