-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmalloc.cpp
More file actions
792 lines (723 loc) · 27.8 KB
/
smalloc.cpp
File metadata and controls
792 lines (723 loc) · 27.8 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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#include <smalloc-cpp.h>
/* heap address => size */
/* static */ std::map<int64_t, int64_t> *tree = NULL;
/* global var address => address of second half in heap */
/* static */ std::map<int64_t, int64_t> *global_tree = NULL;
/* stack address => address of second half in heap, size of stack allocation */
/* static */ std::vector<std::map<int64_t, std::pair<int64_t, int64_t> > *>
*stack_tree_list = NULL;
struct MemoryBounds {
std::pair<u_int64_t, u_int64_t> global;
std::pair<u_int64_t, u_int64_t> stack;
std::pair<u_int64_t, u_int64_t> heap;
} memory_bounds = {std::make_pair(18446744073709551615, 0), std::make_pair(18446744073709551615, 0), std::make_pair(18446744073709551615, 0)};
/* static */ void /* __attribute__((optnone)) */
updateAddr(std::map<int64_t, int64_t> *tree, int64_t start_addr, int64_t size) {
assert(tree != NULL && "");
tree->insert(std::make_pair(start_addr, size));
// tree->_root = smalloc__insert_node(tree->_root, start_addr, size);
}
/* static */ void /* __attribute__((optnone)) */
updateAddrWithSize(std::map<int64_t, std::pair<int64_t, int64_t> > *tree,
int64_t start_addr, int64_t heap_addr, int64_t size) {
assert(tree != NULL && "");
tree->insert(std::make_pair(start_addr, std::make_pair(heap_addr, size)));
// tree->_root = smalloc__insert_node(tree->_root, start_addr, size);
}
/* static */ void * /* __attribute__((optnone)) */ setBit(void *addr) {
/* assert(checkBit(addr) == 0 && "setBit expect a non secret address"); */
int64_t int_addr = (int64_t)addr;
int_addr |= 1UL << smalloc__position;
return (void *)int_addr;
}
/* static */ void * /* __attribute__((optnone)) */ unsetBit(void *addr) {
// printf("running unsetBit for %ld ", addr);
/* assert(checkBit(addr) == 1 && "unsetBit expect a secret address"); */
/* if (checkBit(addr) != 1) printf("WARN: unsetBit expect a secret address\n"); */
if (checkBit(addr) == 0) {
return addr;
}
int64_t int_addr = (int64_t)addr;
int_addr &= ~(1UL << smalloc__position);
// printf("and returning %ld \n", int_addr);
return (void *)int_addr;
}
/* static */ int /* __attribute__((optnone)) */ checkBit(void *addr) {
int64_t int_addr = (int64_t)addr;
if (memory_bounds.global.first <= int_addr && memory_bounds.global.second >= int_addr)
return 1U;
// secret return 1
// not secret return 0
return ((int_addr >> smalloc__position) & 1U);
}
/* static */ void * /* __attribute__((optnone)) */ identity(void *addr) {
printf("running identity for %ld ", addr);
int64_t int_addr = (int64_t)addr;
int_addr |= 1UL << smalloc__position;
int_addr &= ~(1UL << smalloc__position);
return (void *)int_addr;
}
/* static */ void * /* __attribute__((optnone)) */ getAddress(void *addr) {
assert(addr != NULL && "getAddress received null");
int64_t int_addr = (int64_t)addr;
// Finally check if it's a global address (lowest addresses)
if (memory_bounds.global.first <= int_addr && memory_bounds.global.second >= int_addr)
if (global_tree) {
auto res = global_tree->lower_bound(int_addr);
if (res != global_tree->end() && res->first == int_addr) {
return (void *)res->second;
} else if (res != global_tree->begin()) {
res = std::prev(res, 1);
int64_t offset = (int64_t)int_addr - (int64_t)res->first;
int64_t result = res->second + offset;
return (void *)result;
}
}
// First check if it's a stack address (highest addresses)
if (stack_tree_list) {
for (auto I = stack_tree_list->rbegin(); I != stack_tree_list->rend(); I++) {
auto res = (*I)->lower_bound(int_addr);
if (res != stack_tree_list->back()->end() && res->first == int_addr) {
return (void *)res->second.first;
} else if (res != (*I)->begin()) {
res = std::prev(res, 1);
int64_t start_addr = res->first;
int64_t size = res->second.second;
if (int_addr > start_addr + size) {
continue;
} else {
void *heap_addr = (void *)(res->second.first + int_addr - start_addr);
return heap_addr;
}
}
}
}
// Then check if it's a heap address (middle addresses)
if (tree) {
auto res = tree->lower_bound(int_addr);
if (res != tree->end() && res->first == int_addr)
return (void *)(int_addr + res->second / 2);
else if (res != tree->begin()) {
res = std::prev(res, 1);
assert(int_addr <= res->first + res->second / 2 && "out of bound address");
void *result = (void *)(int_addr + res->second / 2);
return result;
}
}
return NULL;
}
/* static */ void* /* __attribute__((optnone)) */ getBaseAddr(void *addr) {
int64_t int_addr = (int64_t)addr;
int64_t base_addr = -1;
if (memory_bounds.global.first <= int_addr && memory_bounds.global.second >= int_addr)
/* printf("getBaseAddr: 0x%lx \n", int_addr); */
if (global_tree) {
auto res = global_tree->lower_bound(int_addr);
if (res != global_tree->end() && res->first == int_addr) {
base_addr = res->first;
return (void *)base_addr;
} else if (res != global_tree->begin()) {
auto res1 = std::prev(res, 1);
base_addr = res1->first;
return (void *)base_addr;
}
}
if (stack_tree_list) {
for (auto I = stack_tree_list->rbegin(); I != stack_tree_list->rend();
I++) {
auto res = (*I)->lower_bound(int_addr);
if (res != stack_tree_list->back()->end() &&
res->first == int_addr) {
base_addr = res->first;
return (void *)base_addr;
} else if (res != (*I)->begin()) {
res = std::prev(res, 1);
base_addr = res->first;
return (void *)base_addr;
}
}
}
if (tree) {
auto res = tree->lower_bound(int_addr);
if (res != tree->end() && res->first == int_addr){
base_addr = res->first;
return (void *)base_addr;
}
else if (res != tree->begin()) {
res = std::prev(res, 1);
base_addr = res->first;
return (void *)base_addr;
}
}
/* assert(base_addr != -1 && "getBaseAddr returned null"); */
return nullptr;
/* return (void *)base_addr; */
}
/* static */ int64_t* /* __attribute__((optnone)) */ getAlignedBaseAddr(void *addr) {
// cast addr into integer
int64_t int_addr = (int64_t)addr;
// get the base address
int64_t base_addr = (int64_t)getBaseAddr(addr);
// get the offset
int64_t offset = int_addr - base_addr;
// get 8 byte aligned base
int64_t base8 = base_addr + (offset / 8) * 8;
return (int64_t*)base8;
}
/* static */ int /* __attribute__((optnone)) */ isShadowAddr(void *addr, int size) {
void *base_addr = getBaseAddr(addr);
int64_t int_addr = (int64_t)addr;
int64_t base_addr_int = (int64_t)base_addr;
assert(base_addr_int != -1 && "base addr not found");
int64_t offset = int_addr - base_addr_int;
int64_t idx = offset / 4;
return idx % 2;
}
/* static */ int /* __attribute__((optnone)) */ getFirstChunkSize(void *addr, int size) {
// cast addr into integer
int64_t int_addr = (int64_t)addr;
// get 8 byte aligned base
int64_t base8 = (int64_t)getAlignedBaseAddr(addr);
// get offset from 8 byte aligned base
int64_t offset8 = int_addr - base8;
// get the size of the first chunk
int chunk_size = 4 - (offset8 % 4);
int result = chunk_size;
if (chunk_size > size)
result = size;
return result;
}
/* static */ int /* __attribute__((optnone)) */ getLastChunkSize(void *addr, int size) {
// get the first chunk size
int first_chunk_size = getFirstChunkSize(addr, size);
return (size - first_chunk_size) % 4;
}
/* static */ int /* __attribute__((optnone)) */ getNumFullChunk(void *addr, int size) {
// get the first chunk size
int first_chunk_size = getFirstChunkSize(addr, size);
// get the last chunk size
int last_chunk_size = getLastChunkSize(addr, size);
// get the number of full chunks
return (size - first_chunk_size - last_chunk_size) / 4;
}
/* static */ int64_t /* __attribute__((optnone)) */ getFirstValue(unsigned int Data, int size, int offset) {
// get lowest size bits of Data
int64_t temp = (1LL << size * 8) - 1;
Data &= (int)temp;
Data = Data << offset * 8;
int64_t mask = ~0 ^ temp;
int64_t junk = 0xdeadceef00000000;
junk = junk & mask;
return junk | Data;
}
/* static */ int64_t /* __attribute__((optnone)) */ getFirstValueBit(unsigned int Data, int size, int offset) {
// get lowest size bits of Data
int64_t temp = (1LL << size) - 1;
Data &= (int)temp;
Data = Data << offset * 8;
int64_t mask = ~0 ^ temp;
int64_t junk = 0xdeadceef00000000;
junk = junk & mask;
return junk | Data;
}
/* static */ int64_t /* __attribute__((optnone)) */ maskLoadedData(int Data, int64_t loadedData, int size, void* addr) {
// cast addr into integer
int64_t int_addr = (int64_t)addr;
// get 8 byte aligned base
int64_t base8 = (int64_t)getAlignedBaseAddr(addr);
// get offset from 8 byte aligned base
int64_t offset8 = (int_addr - base8) % 4;
// get highest size bits of Data
// print all the arguments in hex
/* printf("Data: 0x%x, loadedData: 0x%lx, size: 0x%x\n", Data, loadedData, size); */
int64_t mask = ~0LL ^ ((1LL << size * 8) - 1);
mask = (mask << offset8 * 8) | ((1LL << offset8 * 8) - 1);
int64_t maskedLoadedData = loadedData & (mask & 0xffffffff);
int64_t dataWithJunk = getFirstValue(Data, size, offset8 % 4);
return maskedLoadedData | dataWithJunk;
}
/* static */ int64_t /* __attribute__((optnone)) */ maskLoadedDataBit(int Data, int64_t loadedData, int size, void* addr) {
// cast addr into integer
int64_t int_addr = (int64_t)addr;
// get 8 byte aligned base
int64_t base8 = (int64_t)getAlignedBaseAddr(addr);
// get offset from 8 byte aligned base
int64_t offset8 = (int_addr - base8) % 4;
// get highest size bits of Data
// print all the arguments in hex
/* printf("Data: 0x%x, loadedData: 0x%lx, size: 0x%x\n", Data, loadedData, size); */
int64_t mask = ~0LL ^ ((1LL << size) - 1);
mask = (mask << offset8 * 8) | ((1LL << offset8 * 8) - 1);
int64_t maskedLoadedData = loadedData & (mask & 0xffffffff);
int64_t dataWithJunk = getFirstValueBit(Data, size, offset8 % 4);
return maskedLoadedData | dataWithJunk;
}
/* static */ int64_t /* __attribute__((optnone)) */ unmaskLoadedData(int64_t loadedData, int size, void* addr, int64_t secret, int64_t oldOffset) {
int Data = 0;
// cast addr into integer
int64_t int_addr = (int64_t)addr;
// get 8 byte aligned base
int64_t base8 = (int64_t)getAlignedBaseAddr(addr);
// get offset from 8 byte aligned base
int64_t offset8 = (int_addr - base8) % 4;
// get highest size bits of Data
// print all the arguments in hex
/* printf("Data: 0x%x, loadedData: 0x%lx, size: 0x%x\n", Data, loadedData, size); */
int64_t mask = ~0LL ^ ((1LL << size * 8) - 1);
mask = (mask << offset8 * 8) | ((1LL << offset8 * 8) - 1);
mask = ~mask;
int64_t maskedLoadedData = loadedData & mask;
maskedLoadedData = maskedLoadedData >> offset8 * 8;
/* maskedLoadedData = maskedLoadedData << oldOffset; */
return maskedLoadedData;
}
/* static */ void * /* __attribute__((optnone)) */ incrementAddress(void *addr, int __size) {
return (void *)((int64_t)addr + __size);
}
/* static */ void * /* __attribute__((optnone)) */ getStoreAddress(void *addr, int __size) {
// update address map with size
return getAddress(addr);
}
/* static */ int /* __attribute__((optnone)) */ getLoadSplitSize(void *addr) {
// return the size fron the address map
return 0;
}
/* static */ void * /* __attribute__((optnone)) */ smalloc(size_t __size) {
/* printf("running smalloc "); */
// align size to 8
if (__size % 8 != 0) {
__size += 8 - (__size % 8);
}
void *start_addr = malloc(2 * __size);
int64_t int_addr = (int64_t)start_addr;
if (memory_bounds.heap.first > int_addr)
memory_bounds.heap.first = int_addr;
if (memory_bounds.heap.second < int_addr + 2 * __size)
memory_bounds.heap.second = int_addr + 2 * __size;
if (!tree)
tree = new std::map<int64_t, int64_t>;
updateAddr(tree, int_addr, 2 * __size);
void *addr = (void *)int_addr;
/* printf("returned 0x%lx \n", addr); */
addr = setBit(addr);
return addr;
}
/* static */ void * /* __attribute__((optnone)) */
smmap(void *addr, size_t __size, int prot, int flags, int fd, size_t offset) {
/* printf("running smalloc\n"); */
// align size to 8
if (__size % 8 != 0) {
__size += 8 - (__size % 8);
}
void *start_addr = mmap(addr, 2 * __size, prot, flags, fd, offset);
int64_t int_addr = (int64_t)start_addr;
if (memory_bounds.heap.first > int_addr)
memory_bounds.heap.first = int_addr;
if (memory_bounds.heap.second < int_addr + 2 * __size)
memory_bounds.heap.second = int_addr + 2 * __size;
/* printf("Debug: 0x%lx and 0x%lx \n", int_addr, int_addr + 2 * __size); */
if (!tree)
tree = new std::map<int64_t, int64_t>;
updateAddr(tree, int_addr, 2 * __size);
addr = (void *)int_addr;
addr = setBit(addr);
return addr;
}
/* static */ void /* __attribute__((optnone)) */ sfree(void *addr) {
/* printf("running sfree\n"); */
assert(addr != NULL && "do not free a null ptr");
int64_t int_addr = (int64_t)addr;
// assert(int_addr & (1UL << smalloc__position) &&
// "non secret address used with secret free");
int_addr = (int64_t)unsetBit((void *)int_addr);
tree->erase(int_addr);
free((void *)int_addr);
}
/* static */ int /* __attribute__((optnone)) */ smunmap(void *addr,
size_t __size) {
/* printf("running sfree\n"); */
assert(addr != NULL && "do not free a null ptr");
int64_t int_addr = (int64_t)addr;
// assert(int_addr & (1UL << smalloc__position) &&
// "non secret address used with secret free");
int_addr = (int64_t)unsetBit((void *)int_addr);
if(tree) tree->erase(int_addr);
// align size to 8
if (__size % 8 != 0) {
__size += 8 - (__size % 8);
}
return munmap((void *)int_addr, 2 * __size);
}
void initStack() {
/* printf("init stack\n"); */
std::map<int64_t, std::pair<int64_t, int64_t> > *tree =
new std::map<int64_t, std::pair<int64_t, int64_t> >;
if (!stack_tree_list) {
stack_tree_list =
new std::vector<std::map<int64_t, std::pair<int64_t, int64_t> > *>;
}
stack_tree_list->push_back(tree);
}
void finiStack() {
/* printf("fini stack\n"); */
assert(stack_tree_list != NULL && "list must not be null");
std::map<int64_t, std::pair<int64_t, int64_t> > *heap =
stack_tree_list->back();
auto idx = heap->begin();
while (idx != heap->end()) {
void *addr = (void *)idx->second.first;
// free(addr);
idx = std::next(idx, 1);
}
// for(std::pair<int64_t, int64_t> element : *heap) {
// free((void*)element.second);
// }
stack_tree_list->pop_back();
}
bool isAddrInStackMap(void *addr) {
assert(stack_tree_list != NULL && "list must not be null");
std::map<int64_t, std::pair<int64_t, int64_t> > *stackMap =
stack_tree_list->back();
if (stackMap->find((int64_t)addr) != stackMap->end())
return true;
return false;
}
void *updateStackAddr(void *addr, int64_t size) {
// expects size in byte
/* printf("update stack size: %ld \n", size); */
assert(addr != NULL && "addr must not be null");
if (size % 8 != 0) {
size += 8 - (size % 8);
}
void *heap_address = malloc(size);
/* printf("updateStackAddr heap : 0x%lx \n", addr); */
/* printf("Debug: 0x%lx and ... \n", heap_address); */
updateAddrWithSize(stack_tree_list->back(), (int64_t)addr,
(int64_t)heap_address, size);
if (memory_bounds.stack.first > (int64_t)addr)
memory_bounds.stack.first = (int64_t)addr;
if (memory_bounds.stack.second < (int64_t)addr + size)
memory_bounds.stack.second = (int64_t)addr + size;
return setBit(addr);
}
void *updateGlobalAddr(void *addr, int64_t size, int64_t heap_value) {
addr = unsetBit(addr);
if (!global_tree)
global_tree = new std::map<int64_t, int64_t>;
void *heap_address = malloc(size);
/* *(int64_t *)heap_address = heap_value; */
updateAddr(global_tree, (int64_t)addr, (int64_t)heap_address);
/* printf("updateGlobalAddr Query : 0x%lx and 0x%lx \n", addr, size); */
/* printf("Debug: 0x%lx and 0x%lx \n", heap_address, heap_value); */
if (memory_bounds.global.first > (int64_t)addr)
memory_bounds.global.first = (int64_t)addr;
if (memory_bounds.global.second < (int64_t)addr + size)
memory_bounds.global.second = (int64_t)addr + size;
return setBit(addr);
}
void destroyGlobalAddr() {
if (!global_tree)
return;
for (std::pair<int64_t, int64_t> element : *global_tree) {
free((void *)element.second);
}
}
int32_t handle32BitLoad(void* addr) {
int size = 32;
int firstSize = 0;
int oldFirstSize = 0;
int32_t secret = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0; i < 2; i++) {
addr = (void *)((int64_t)addr + firstSize);
oldFirstSize += firstSize;
requiredSize -= firstSize;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t value __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int offset = oldFirstSize * 8;
int32_t secret0 = (int32_t)unmaskLoadedData(value, firstSize, addr, offset, offset);
secret0 = secret0 << offset;
secret = secret | secret0;
}
return secret;
}
int16_t handle16BitLoad(void* addr) {
int size = 16;
int firstSize = 0;
int oldFirstSize = 0;
int16_t secret = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0; i < 1; i++) {
addr = (void *)((int64_t)addr + firstSize);
oldFirstSize += firstSize;
requiredSize -= firstSize;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t value __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int offset = oldFirstSize * 8;
int16_t secret0 = (int16_t)unmaskLoadedData(value, firstSize, addr, offset, offset);
secret0 = secret0 << offset;
secret = secret | secret0;
}
return secret;
}
int8_t handle8BitLoad(void* addr) {
int size = 8;
int firstSize = 0;
int oldFirstSize = 0;
int8_t secret = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0; i < 1; i++) {
addr = (void *)((int64_t)addr + firstSize);
oldFirstSize += firstSize;
requiredSize -= firstSize;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t value __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int offset = oldFirstSize * 8;
int8_t secret0 = (int8_t)unmaskLoadedData(value, firstSize, addr, offset, offset);
secret0 = secret0 << offset;
secret = secret | secret0;
}
return secret;
}
int64_t handle64BitSpillLoad(void* addr) {
printf("Spill load %p\n", addr);
int size = 64;
int firstSize = 0;
int oldFirstSize = 0;
int64_t secret = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0; i < 3; i++) {
addr = (void *)((int64_t)addr + firstSize);
oldFirstSize += firstSize;
requiredSize -= firstSize;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t value =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int offset = oldFirstSize * 8;
int64_t secret0 =
unmaskLoadedData(value, firstSize, addr, offset, offset);
secret0 = secret0 << offset;
secret = secret | secret0;
}
printf("load\n");
return secret;
}
int64_t handle64BitLoad(void* addr) {
int size = 64;
int firstSize = 0;
int oldFirstSize = 0;
int64_t secret = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0; i < 3; i++) {
addr = (void *)((int64_t)addr + firstSize);
oldFirstSize += firstSize;
requiredSize -= firstSize;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t value =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int offset = oldFirstSize * 8;
int64_t secret0 =
unmaskLoadedData(value, firstSize, addr, offset, offset);
secret0 = secret0 << offset;
secret = secret | secret0;
}
return secret;
}
void handle64BitStore(void *addr, int64_t secret) {
int size = 64;
int firstSize = 0;
int oldFirstSize = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0;i < 3;i++){
addr = (void *)((int64_t)addr + firstSize);
requiredSize -= firstSize;
secret = secret >> firstSize * 8;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
int32_t secret0 = (int32_t)secret;
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t alignedLoad __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int64_t firstValue = maskLoadedData(secret0, alignedLoad, firstSize, addr);
*(int64_t __attribute__((aligned(8))) *)alignedAddr = firstValue;
}
}
void handle64BitSpillStore(void *addr, int64_t secret) {
printf("Spill store %p\n", addr);
int size = 64;
int firstSize = 0;
int oldFirstSize = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
if(!isAddrInStackMap(addr)){
updateStackAddr(addr, size);
}
for (int i = 0;i < 3;i++){
addr = (void *)((int64_t)addr + firstSize);
requiredSize -= firstSize;
secret = secret >> firstSize * 8;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
int32_t secret0 = (int32_t)secret;
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t alignedLoad __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int64_t firstValue = maskLoadedData(secret0, alignedLoad, firstSize, addr);
*(int64_t __attribute__((aligned(8))) *)alignedAddr = firstValue;
}
printf("store\n");
}
void handle32BitStore(void *addr, int32_t secret) {
int size = 32;
int firstSize = 0;
int oldFirstSize = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0;i < 2;i++){
addr = (void *)((int64_t)addr + firstSize);
requiredSize -= firstSize;
secret = secret >> firstSize * 8;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
int32_t secret0 = (int32_t)secret;
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t alignedLoad __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int64_t firstValue = maskLoadedData(secret0, alignedLoad, firstSize, addr);
*(int64_t __attribute__((aligned(8))) *)alignedAddr = firstValue;
}
}
void handle16BitStore(void *addr, int16_t secret) {
int size = 16;
int firstSize = 0;
int oldFirstSize = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0;i < 1;i++){
addr = (void *)((int64_t)addr + firstSize);
requiredSize -= firstSize;
secret = secret >> firstSize * 8;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
int16_t secret0 = (int16_t)secret;
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t alignedLoad __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int64_t firstValue = maskLoadedData(secret0, alignedLoad, firstSize, addr);
*(int64_t __attribute__((aligned(8))) *)alignedAddr = firstValue;
}
}
void handle8BitStore(void *addr, int8_t secret) {
int size = 8;
int firstSize = 0;
int oldFirstSize = 0;
int requiredSize = size / 8;
addr = unsetBit(addr);
for (int i = 0;i < 1;i++){
addr = (void *)((int64_t)addr + firstSize);
requiredSize -= firstSize;
secret = secret >> firstSize * 8;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
int8_t secret0 = (int8_t)secret;
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t alignedLoad __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int64_t firstValue = maskLoadedData(secret0, alignedLoad, firstSize, addr);
*(int64_t __attribute__((aligned(8))) *)alignedAddr = firstValue;
}
}
void handle1BitStore(void *addr, int8_t secret) {
int size = 1;
int firstSize = 0;
int oldFirstSize = 0;
int requiredSize = 1; // in bits
addr = unsetBit(addr);
for (int i = 0;i < 1;i++){
addr = (void *)((int64_t)addr + firstSize);
requiredSize -= firstSize;
secret = secret >> firstSize * 8;
firstSize = getFirstChunkSize(addr, requiredSize);
if (firstSize <= 0) {
break;
}
int8_t secret0 = (int8_t)secret;
void *alignedAddr = getAlignedBaseAddr(addr);
if (isShadowAddr(addr, size)) {
alignedAddr = getAddress(alignedAddr);
}
int64_t alignedLoad __attribute__((aligned(8))) =
*(int64_t __attribute__((aligned(8))) *)alignedAddr;
int64_t firstValue = maskLoadedDataBit(secret0, alignedLoad, firstSize, addr);
*(int64_t __attribute__((aligned(8))) *)alignedAddr = firstValue;
}
}