forked from GJDuck/e9patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe9mapping.cpp
More file actions
751 lines (687 loc) · 19.2 KB
/
e9mapping.cpp
File metadata and controls
751 lines (687 loc) · 19.2 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
* e9mapping.cpp
* Copyright (C) 2021 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/>.
*/
/*
* Physical address space allocation and optimization.
*
* Here, a "mapping" represents a chunk of memory that is mmap'ed from the
* modified binary file into the program's virtual address space.
*/
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <sys/mman.h>
#include "e9alloc.h"
#include "e9mapping.h"
#include "e9patch.h"
#include "e9trampoline.h"
#define BRANCH_BITS 2
#define BRANCH_MAX (1 << BRANCH_BITS)
/*
* Size.
*/
template <typename Key>
static size_t size(Key key)
{
return key.size();
}
template <>
size_t size<Key128>(Key128 key)
{
return 8 * sizeof(key);
}
/*
* Trailing zero count.
*/
template <typename Key>
static unsigned tzcount(Key key)
{
if (key.none())
return key.size();
Key mask = 0xFFFFFFFFFFFFFFFFull;
for (unsigned count = 0; ; count += 64)
{
uint64_t key64 = (key & mask).to_ullong();
if (key64 != 0)
return count + __builtin_ctzll(key64);
key >>= 64;
}
}
template <>
unsigned tzcount<Key128>(Key128 key)
{
uint64_t lo = (uint64_t)key;
uint64_t hi = (uint64_t)(key >> 64);
return (lo == 0? __builtin_ctzll(hi) + 64:
__builtin_ctzll(lo));
}
/*
* Bits to string.
*/
template <typename Key>
static void bitstring(Key key, std::string &str)
{
Key mask = 0xFFFFFFFFFFFFFFFFull;
for (unsigned i = 0; i < size(key); i += 2 * 64)
{
const char digs[] = "0123456789ABCDEF";
size_t count = 0;
for (unsigned j = 0; j < 2; j++)
{
uint64_t key64 = (key & mask).to_ullong();
count += __builtin_popcountll(key64);
key >>= 64;
}
switch (count)
{
case 0: case 1:
break;
default:
count = 1 + (count - 2) / 9; break;
}
str += digs[count];
}
}
template <>
void bitstring<Key128>(Key128 key, std::string &str)
{
for (unsigned i = 0; i < size(key); i += 64)
{
char buf[32];
snprintf(buf, sizeof(buf)-1, "%.16lX", (uint64_t)key);
str += buf;
key >>= 64;
}
}
/*
* Calculate the occupancy key of a mapping.
*/
template <typename Key>
static Key calculateKey(const Allocator &allocator, const size_t MAPPING_SIZE,
const Mapping *mapping)
{
const Key KEY_ZERO = 0;
const size_t KEY_BITS = size(KEY_ZERO);
const size_t UNIT_SIZE = MAPPING_SIZE / KEY_BITS;
const intptr_t BASE = mapping->base;
const intptr_t END = BASE + (ssize_t)MAPPING_SIZE;
const Key KEY_ONES = ~KEY_ZERO;
Key key = 0;
auto i = mapping->i, iend = allocator.end();
const Alloc *a = *i;
if (a->lb < BASE)
{
if (a->ub >= END)
return KEY_ONES;
size_t overlap = MAPPING_SIZE - (END - a->ub);
overlap = (overlap + UNIT_SIZE - 1) / UNIT_SIZE;
if (overlap >= KEY_BITS)
return KEY_ONES;
key = ~(KEY_ONES << overlap);
++i;
}
for (; i != iend; ++i)
{
a = *i;
assert(a->lb >= BASE);
if (a->lb >= END)
return key;
if (a->T == nullptr)
continue;
size_t prologue = a->lb - BASE;
size_t postscript = (a->ub >= END? 0: END - a->ub);
prologue = prologue / UNIT_SIZE;
postscript = postscript / UNIT_SIZE;
assert(prologue < KEY_BITS);
assert(postscript < KEY_BITS);
Key tmp = (KEY_ONES << prologue);
tmp &= (KEY_ONES >> postscript);
key |= tmp;
}
return key;
}
/*
* Calculate the occupancy bounds of a mapping.
*/
static Bounds calculateBounds(const Mapping *mapping)
{
intptr_t lb = INTPTR_MAX, ub = INTPTR_MIN;
const size_t SIZE = mapping->size;
const intptr_t BASE = mapping->base;
const intptr_t END = BASE + SIZE;
auto iend = Allocator::end();
for (auto i = mapping->i; i != iend; ++i)
{
const Alloc *a = *i;
if (a->lb >= END)
break;
if (a->T == nullptr)
continue;
intptr_t lb1 = (a->lb < BASE? 0: a->lb - BASE);
intptr_t ub1 = (a->ub > END ? END - BASE: a->ub - BASE);
lb = std::min(lb, lb1);
ub = std::max(ub, ub1);
}
return {lb, ub};
}
/*
* Calculate the protections of a mapping.
*/
static int calculateProtections(const Mapping *mapping)
{
int prot = PROT_NONE;
const size_t SIZE = mapping->size;
const intptr_t BASE = mapping->base;
const intptr_t END = BASE + SIZE;
auto iend = Allocator::end();
for (auto i = mapping->i; i != iend; ++i)
{
const Alloc *a = *i;
if (a->lb >= END)
break;
if (a->T == nullptr)
continue;
prot |= a->T->prot;
}
return prot;
}
/*
* Calculate the PRELOAD flag of a mapping.
*/
static bool calculatePreload(const Mapping *mapping)
{
const size_t SIZE = mapping->size;
const intptr_t BASE = mapping->base;
const intptr_t END = BASE + SIZE;
auto iend = Allocator::end();
for (auto i = mapping->i; i != iend; ++i)
{
const Alloc *a = *i;
if (a->lb >= END)
break;
if (a->T == nullptr)
continue;
if (a->T->preload)
return true;
}
return false;
}
/*
* Allocate a new mapping.
*/
static Mapping *allocMapping(Allocator::iterator i, size_t size, intptr_t base)
{
Mapping *mapping = new Mapping;
mapping->base = base;
mapping->size = size;
mapping->lb = 0;
mapping->ub = size;
mapping->offset = -1;
mapping->prot = PROT_NONE;
mapping->preload = false;
mapping->i = i;
mapping->next = nullptr;
mapping->merged = nullptr;
return mapping;
}
/*
* Insert a mapping into the set.
*/
static void insertMapping(Mapping *mapping, MappingSet &mappings)
{
mappings.push_back(mapping);
}
/*
* Save a mapping into the set.
*/
static void saveMapping(Mapping *mapping, MappingSet &mappings)
{
if (mapping == nullptr)
return;
Bounds b = calculateBounds(mapping);
mapping->lb = b.lb;
mapping->ub = b.ub;
mapping->prot = calculateProtections(mapping);
mapping->preload = calculatePreload(mapping);
insertMapping(mapping, mappings);
stat_num_virtual_mappings++;
}
/*
* Build the initial set of (unmerged) mappings from the virtual address
* layout described by `allocator`.
*/
void buildMappings(const Allocator &allocator, const size_t MAPPING_SIZE,
MappingSet &mappings)
{
intptr_t base = INTPTR_MIN;
Mapping *mapping = nullptr;
mappings.clear();
for (auto i = allocator.begin(), iend = allocator.end(); i != iend; ++i)
{
const Alloc *a = *i;
if (a->T == nullptr)
continue;
if (a->lb >= base + (intptr_t)MAPPING_SIZE)
{
base = a->lb - a->lb % MAPPING_SIZE;
saveMapping(mapping, mappings);
mapping = allocMapping(i, MAPPING_SIZE, base);
}
while (base + (ssize_t)MAPPING_SIZE < a->ub)
{
base += MAPPING_SIZE;
saveMapping(mapping, mappings);
mapping = allocMapping(i, MAPPING_SIZE, base);
}
}
saveMapping(mapping, mappings);
}
/**************************************************************************/
/* PHYSICAL PAGE GROUPING */
/**************************************************************************/
/*
* For physical page grouping, we need to merge mappings that do not
* overlap. For speed, we approximate the occupancy using a bitmap, and
* merge based on bit complement ((key1 & key2) == 0). We also arrange the
* mappings into a radix tree based on the occupancy bitmap. This makes it
* possible to efficiently find candidates for merging. The algorithm is
* greedy and not theoretically optimal, but is fast and gives reasonable
* results in practice.
*/
namespace Radix
{
/*
* Radix tree leaf.
*/
struct Leaf
{
Mapping *mappings; // List of all mappings with same key
};
/*
* Radix-tree node.
*/
template <typename Key>
struct Node
{
Key key; // Node key
uint64_t inner:1; // Inner or leaf node?
uint64_t shift:63; // Shift for branch mask
union
{
Node *child[BRANCH_MAX]; // Inner node children
Leaf leaf; // Leaf node data
};
};
/*
* Given a node and key, return the child index.
*/
template <typename Key>
static unsigned index(Radix::Node<Key> *node, Key key)
{
size_t shift = BRANCH_BITS * node->shift;
Key BRANCH_MASK = BRANCH_MAX - 1;
return ((key & (BRANCH_MASK << shift)) >> shift).to_ullong();
}
template <>
unsigned index<Key128>(Radix::Node<Key128> *node, Key128 key)
{
size_t shift = BRANCH_BITS * node->shift;
Key128 BRANCH_MASK = BRANCH_MAX - 1;
return (unsigned)((key & (BRANCH_MASK << shift)) >> shift);
}
} // namespace Mapping
/*
* Fix the tree invariant after insertion/deletion.
*/
template <typename Key>
static void fix(Radix::Node<Key> *node)
{
Key key = ~(Key)0;
for (unsigned i = 0; i < BRANCH_MAX; i++)
{
Radix::Node<Key> *child = node->child[i];
if (child == nullptr)
continue;
key = (key & child->key);
}
node->key = key;
}
/*
* Find the leaf node for the given key.
*/
template <typename Key>
static Radix::Node<Key> *find(Radix::Node<Key> *node, Key key)
{
while (true)
{
if (node == nullptr)
return nullptr;
if (!node->inner)
return (node->key == key? node: nullptr);
unsigned idx = index(node, key);
node = node->child[idx];
}
}
/*
* Find any leaf node matching (key & leaf->key) == 0.
*/
template <typename Key>
static Radix::Node<Key> *findAnyComplement(Radix::Node<Key> *node, Key key)
{
if (node == nullptr)
return nullptr;
if (!node->inner)
return ((node->key & key) == 0? node: nullptr);
for (unsigned i = 0; i < BRANCH_MAX; i++)
{
Radix::Node<Key> *child = node->child[i];
if (child == nullptr)
continue;
if ((key & child->key) != 0)
continue;
Radix::Node<Key> *result = findAnyComplement(child, key);
if (result != nullptr)
return result;
}
return nullptr;
}
/*
* Insert a new mapping into the tree.
*/
template <typename Key>
static Radix::Node<Key> *insert(Radix::Node<Key> *node, Key key,
Mapping *mapping)
{
if (node == nullptr)
{
Radix::Node<Key> *leaf = new Radix::Node<Key>();
leaf->inner = false;
leaf->shift = 0;
leaf->key = key;
leaf->leaf.mappings = mapping;
return leaf;
}
if (!node->inner)
{
Key diff = node->key ^ key;
if (diff == 0)
{
// Add to existing node:
mapping->next = node->leaf.mappings;
node->leaf.mappings = mapping;
return node;
}
// Add new branch:
unsigned shift = tzcount(diff) / BRANCH_BITS;
Radix::Node<Key> *inner = new Radix::Node<Key>();
inner->inner = true;
inner->shift = shift;
for (unsigned i = 0; i < BRANCH_MAX; i++)
inner->child[i] = nullptr;
inner->child[index(inner, node->key)] = node;
Radix::Node<Key> *leaf = new Radix::Node<Key>();
leaf->inner = false;
leaf->shift = 0;
leaf->key = key;
leaf->leaf.mappings = mapping;
inner->child[index(inner, leaf->key)] = leaf;
fix(inner);
return inner;
}
unsigned idx = index(node, key);
node->child[idx] = insert(node->child[idx], key, mapping);
fix(node);
return node;
}
/*
* Remove the leaf node matching `key`.
*/
template <typename Key>
static Radix::Node<Key> *remove(Radix::Node<Key> *node, Key key)
{
if (node == nullptr)
return nullptr;
if (!node->inner)
{
if (node->key != key)
return node;
delete node;
return nullptr;
}
unsigned idx = index(node, key);
Radix::Node<Key> *child = remove(node->child[idx], key);
node->child[idx] = child;
if (child != nullptr)
{
fix(node);
return node;
}
// If the number of child is reduced to 1, then delete the inner node:
Radix::Node<Key> *seen = nullptr;
for (int i = 0; i < BRANCH_MAX; i++)
{
if (node->child[i] == nullptr)
continue;
if (seen != nullptr)
return node; // 2 or more...
seen = node->child[i];
}
delete node;
return seen;
}
/*
* Merge a mapping with an existing mapping (if possible).
*/
template <typename Key>
static Radix::Node<Key> *merge(Radix::Node<Key> *tree, Key key,
Mapping *mapping)
{
Radix::Node<Key> *node = find(tree, key);
if (node != nullptr)
{
// Add to existing node for key:
mapping->next = node->leaf.mappings;
node->leaf.mappings = mapping;
log(COLOR_NONE, '+');
return tree;
}
node = findAnyComplement(tree, key);
if (node != nullptr)
{
// Merge with negated node:
Mapping *mappingCmp = node->leaf.mappings;
node->leaf.mappings = mappingCmp->next;
mappingCmp->next = nullptr;
mapping->merged = mappingCmp;
key |= node->key;
if (node->leaf.mappings == nullptr)
{
// Leaf node is now empty, so remove it.
tree = remove(tree, node->key);
}
log(COLOR_GREEN, 'M');
}
else
log(COLOR_NONE, '+');
// Insert a new node:
tree = insert(tree, key, mapping);
return tree;
}
/*
* Collect all (optimized) mappings and free the tree.
*/
template <typename Key>
static void collectMappings(Radix::Node<Key> *node, MappingSet &mappings)
{
if (node == nullptr)
return;
if (!node->inner)
{
for (auto mapping = node->leaf.mappings; mapping != nullptr;
mapping = mapping->next)
{
std::string str;
bitstring(node->key, str);
log(COLOR_NONE, '[');
log(COLOR_YELLOW, str.c_str());
log(COLOR_NONE, ']');
insertMapping(mapping, mappings);
stat_num_physical_mappings++;
}
}
else
{
for (unsigned i = 0; i < BRANCH_MAX; i++)
collectMappings(node->child[i], mappings);
}
delete node;
}
/*
* Shrink a mapping (if possible).
*/
static void shrinkMapping(Mapping *mapping0, size_t granularity)
{
if (mapping0->size == granularity)
return;
intptr_t lb = INTPTR_MAX, ub = INTPTR_MIN;
for (auto mapping = mapping0; mapping != nullptr;
mapping = mapping->merged)
{
lb = std::min(lb, mapping->lb);
ub = std::max(ub, mapping->ub);
}
lb = lb - lb % granularity;
if (ub % granularity != 0)
{
ub += granularity;
ub = ub - ub % granularity;
}
size_t size = ub - lb;
if (size >= mapping0->size)
return;
for (auto mapping = mapping0; mapping != nullptr;
mapping = mapping->merged)
{
mapping->base += lb;
mapping->size = size;
mapping->lb -= lb;
mapping->ub -= lb;
}
}
/*
* Optimize the given set of mappings.
*/
template <typename Key>
void optimizeMappings(const Allocator &allocator, const size_t MAPPING_SIZE,
size_t granularity, MappingSet &mappings)
{
Radix::Node<Key> *tree = nullptr;
for (auto mapping: mappings)
{
Key key = calculateKey<Key>(allocator, MAPPING_SIZE, mapping);
tree = merge(tree, key, mapping);
}
log(COLOR_NONE, '\n');
mappings.clear();
collectMappings(tree, mappings);
log(COLOR_NONE, '\n');
for (auto mapping: mappings)
shrinkMapping(mapping, granularity);
}
template
void optimizeMappings<Key128>(const Allocator &allocator,
const size_t MAPPING_SIZE, size_t granularity, MappingSet &mappings);
template
void optimizeMappings<Key4096>(const Allocator &allocator,
const size_t MAPPING_SIZE, size_t granularity, MappingSet &mappings);
/**************************************************************************/
/* FLATTEN MAPPINGS */
/**************************************************************************/
/*
* Flatten a mapping into a memory buffer.
*/
void flattenMapping(const Binary *B, uint8_t *buf, const Mapping *mapping,
uint8_t fill)
{
memset(buf, fill, mapping->size);
auto iend = Allocator::end();
for (; mapping != nullptr; mapping = mapping->merged)
{
auto i = mapping->i;
const Alloc *a = *i;
const size_t SIZE = mapping->size;
const intptr_t BASE = mapping->base;
const intptr_t END = BASE + SIZE;
for (; i != iend; ++i)
{
a = *i;
if (a->lb >= END)
break;
if (a->bytes == nullptr)
continue;
intptr_t lb = a->lb, ub = a->ub;
off_t offset = (lb < BASE? BASE - lb: 0);
lb = (lb < BASE? BASE: lb);
ub = (ub > END? END: ub);
memcpy(buf + (lb - BASE), a->bytes + offset, (ub - lb));
}
}
}
/*
* Get the virtual bounds of a mapping.
*/
static void pushBounds(intptr_t lb, intptr_t ub, size_t granularity,
std::vector<Bounds> &bounds)
{
if (lb == INTPTR_MAX || ub == INTPTR_MIN)
return;
lb = lb - lb % granularity;
if (ub % granularity != 0)
ub = (ub + granularity) - (ub % granularity);
bounds.push_back({lb, ub});
}
void getVirtualBounds(const Mapping *mapping, size_t granularity,
std::vector<Bounds> &bounds)
{
intptr_t lb = INTPTR_MAX, ub = INTPTR_MIN;
const size_t SIZE = mapping->size;
const intptr_t BASE = mapping->base;
const intptr_t END = BASE + SIZE;
auto iend = Allocator::end();
for (auto i = mapping->i; i != iend; ++i)
{
const Alloc *a = *i;
if (a->lb >= END)
break;
if (a->bytes == nullptr)
{
// Reserved memory. We must split into two separate mappings.
pushBounds(lb, ub, granularity, bounds);
lb = INTPTR_MAX;
ub = INTPTR_MIN;
continue;
}
intptr_t lb1 = (a->lb < BASE? 0: a->lb - BASE);
intptr_t ub1 = (a->ub > END ? END - BASE: a->ub - BASE);
lb = std::min(lb, lb1);
ub = std::max(ub, ub1);
}
pushBounds(lb, ub, granularity, bounds);
}