-
Notifications
You must be signed in to change notification settings - Fork 0
/
spp.c
707 lines (605 loc) · 22.7 KB
/
spp.c
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
///////////////////////////////////////////////
////// spp.c ///////////////
///////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stddef.h>
#include <unistd.h>
#include <assert.h>
#include <math.h>
#include <execinfo.h>
#include <signal.h>
#include <string.h>
#include "./spp.h" ///
#if defined(__x86_64__) && defined(__BMI2__) && !defined(HW_OFF)
#include "x86intrin.h"
#endif
///////////////////////////////
/// Debug Macro ///
///////////////////////////////
// #define SPP_PRINT_DEBUG
#ifdef SPP_PRINT_DEBUG
# define dbg(x) x
#else
# define dbg(x)
#endif
// #define SPP_DEBUG
#ifdef SPP_DEBUG
# define __SPP_INLINE __attribute__((__optnone__))
#else
# define __SPP_INLINE __attribute__((always_inline))
#endif
#define __SPP_USED __attribute__((__used__))
#define __SPP_ATTR __SPP_INLINE __SPP_USED
///////////////////////////////
/// End of Debug Macro ///
///////////////////////////////
///////////////////////////////
/// Stats Macro ///
///////////////////////////////
// #define SPP_STATS
#ifdef SPP_STATS
#define stats(x) x
long int __spp_extract_tagval_cnt = 0;
long int __spp_is_pm_ptr_cnt = 0;
long int __spp_vol_ptr_check_cnt = 0;
long int __spp_cleantag_cnt = 0;
long int __spp_cleantag_cnt_direct = 0;
long int useless__spp_cleantag_cnt = 0;
long int __spp_cleantag_external_cnt = 0;
long int __spp_cleantag_external_cnt_direct = 0;
long int useless__spp_cleantag_external_cnt = 0;
long int __spp_checkbound_cnt = 0;
long int __spp_checkbound_cnt_direct = 0;
long int useless__spp_checkbound_cnt = 0;
long int __spp_updatetag_cnt = 0;
long int __spp_updatetag_cnt_direct = 0;
long int useless__spp_updatetag_cnt = 0;
long int __spp_memintr_check_and_clean_cnt = 0;
long int __spp_memintr_check_and_clean_cnt_direct = 0;
// accummulators
long int __spp_extract_tagval_cnt_acc = 0;
long int __spp_is_pm_ptr_cnt_acc = 0;
long int __spp_vol_ptr_check_cnt_acc = 0;
long int __spp_cleantag_cnt_acc = 0;
long int __spp_cleantag_cnt_direct_acc = 0;
long int useless__spp_cleantag_cnt_acc = 0;
long int __spp_cleantag_external_cnt_acc = 0;
long int __spp_cleantag_external_cnt_direct_acc = 0;
long int useless__spp_cleantag_external_cnt_acc = 0;
long int __spp_checkbound_cnt_acc = 0;
long int __spp_checkbound_cnt_direct_acc = 0;
long int useless__spp_checkbound_cnt_acc = 0;
long int __spp_updatetag_cnt_acc = 0;
long int __spp_updatetag_cnt_direct_acc = 0;
long int useless__spp_updatetag_cnt_acc = 0;
long int __spp_memintr_check_and_clean_cnt_acc = 0;
long int __spp_memintr_check_and_clean_cnt_direct_acc = 0;
struct func_cnt {
char* fname;
int cnt;
struct func_cnt* next;
};
struct func_cnt* head = NULL;
void __spp_stats_reset() {
__spp_extract_tagval_cnt = 0;
__spp_is_pm_ptr_cnt = 0;
__spp_vol_ptr_check_cnt = 0;
__spp_cleantag_cnt = 0;
__spp_cleantag_cnt_direct = 0;
useless__spp_cleantag_cnt = 0;
__spp_cleantag_external_cnt = 0;
__spp_cleantag_external_cnt_direct = 0;
useless__spp_cleantag_external_cnt = 0;
__spp_checkbound_cnt = 0;
__spp_checkbound_cnt_direct = 0;
useless__spp_checkbound_cnt = 0;
__spp_updatetag_cnt = 0;
__spp_updatetag_cnt_direct = 0;
useless__spp_updatetag_cnt = 0;
__spp_memintr_check_and_clean_cnt = 0;
__spp_memintr_check_and_clean_cnt_direct = 0;
}
void __spp_gather_stats() {
__spp_extract_tagval_cnt_acc += __spp_extract_tagval_cnt;
__spp_is_pm_ptr_cnt_acc += __spp_is_pm_ptr_cnt;
__spp_vol_ptr_check_cnt_acc += __spp_vol_ptr_check_cnt;
__spp_cleantag_cnt_acc += __spp_cleantag_cnt;
__spp_cleantag_cnt_direct_acc += __spp_cleantag_cnt_direct;
useless__spp_cleantag_cnt_acc += useless__spp_cleantag_cnt;
__spp_cleantag_external_cnt_acc += __spp_cleantag_external_cnt;
__spp_cleantag_external_cnt_direct_acc += __spp_cleantag_external_cnt_direct;
useless__spp_cleantag_external_cnt_acc += useless__spp_cleantag_external_cnt;
__spp_checkbound_cnt_acc += __spp_checkbound_cnt;
__spp_checkbound_cnt_direct_acc += __spp_checkbound_cnt_direct;
useless__spp_checkbound_cnt_acc += useless__spp_checkbound_cnt;
__spp_updatetag_cnt_acc += __spp_updatetag_cnt;
__spp_updatetag_cnt_direct_acc += __spp_updatetag_cnt_direct;
useless__spp_updatetag_cnt_acc += useless__spp_updatetag_cnt;
__spp_memintr_check_and_clean_cnt_acc += __spp_memintr_check_and_clean_cnt;
__spp_memintr_check_and_clean_cnt_direct_acc += __spp_memintr_check_and_clean_cnt_direct;
}
void __spp_print_mean_stats(int repeats) {
printf("***** SPP runtime statistics *****\n");
printf("__spp_extract_tagval_cnt\t\t: %ld \n", __spp_extract_tagval_cnt_acc/repeats);
printf("__spp_is_pm_ptr_cnt\t\t\t: %ld \n", __spp_is_pm_ptr_cnt_acc/repeats);
printf("__spp_vol_ptr_check_cnt\t\t\t: %ld \n", __spp_vol_ptr_check_cnt_acc/repeats);
printf("__spp_cleantag_cnt\t\t\t: %ld \n", __spp_cleantag_cnt_acc/repeats);
printf("USELESS__spp_cleantag_cnt\t\t: %ld \n", useless__spp_cleantag_cnt_acc/repeats);
printf("__spp_cleantag_external_cnt\t\t: %ld \n", __spp_cleantag_external_cnt_acc/repeats);
printf("USELESS__spp_cleantag_external_cnt\t: %ld \n", useless__spp_cleantag_external_cnt_acc/repeats);
printf("__spp_checkbound_cnt\t\t\t: %ld \n", __spp_checkbound_cnt_acc/repeats);
printf("USELESS__spp_checkbound_cnt\t\t: %ld \n", useless__spp_checkbound_cnt_acc/repeats);
printf("__spp_updatetag_cnt\t\t\t: %ld \n", __spp_updatetag_cnt_acc/repeats);
printf("USELESS__spp_updatetag_cnt\t\t: %ld \n", useless__spp_updatetag_cnt_acc/repeats);
printf("__spp_memintr_check_and_clean_cnt\t: %ld \n", __spp_memintr_check_and_clean_cnt_acc/repeats);
printf("\n");
printf("***** SPP direct runtime statistics *****\n");
printf("__spp_cleantag_cnt_direct\t\t: %ld \n", __spp_cleantag_cnt_direct_acc/repeats);
printf("__spp_cleantag_external_cnt_direct\t: %ld \n", __spp_cleantag_external_cnt_direct_acc/repeats);
printf("__spp_checkbound_cnt_direct\t\t: %ld \n", __spp_checkbound_cnt_direct_acc/repeats);
printf("__spp_updatetag_cnt_direct\t\t: %ld \n", __spp_updatetag_cnt_direct_acc/repeats);
printf("__spp_memintr_check_and_clean_cnt_direct: %ld \n", __spp_memintr_check_and_clean_cnt_direct_acc/repeats);
printf("\n");
__spp_extract_tagval_cnt_acc = 0;
__spp_is_pm_ptr_cnt_acc = 0;
__spp_vol_ptr_check_cnt_acc = 0;
__spp_cleantag_cnt_acc = 0;
__spp_cleantag_cnt_direct_acc = 0;
useless__spp_cleantag_cnt_acc = 0;
__spp_cleantag_external_cnt_acc = 0;
__spp_cleantag_external_cnt_direct_acc = 0;
useless__spp_cleantag_external_cnt_acc = 0;
__spp_checkbound_cnt_acc = 0;
__spp_checkbound_cnt_direct_acc = 0;
useless__spp_checkbound_cnt_acc = 0;
__spp_updatetag_cnt_acc = 0;
__spp_updatetag_cnt_direct_acc = 0;
useless__spp_updatetag_cnt_acc = 0;
__spp_memintr_check_and_clean_cnt_acc = 0;
__spp_memintr_check_and_clean_cnt_direct_acc = 0;
}
void __spp_runtime_stats() {
printf("***** SPP runtime statistics *****\n");
printf("__spp_extract_tagval_cnt\t\t: %ld \n", __spp_extract_tagval_cnt);
printf("__spp_is_pm_ptr_cnt\t\t\t: %ld \n", __spp_is_pm_ptr_cnt);
printf("__spp_vol_ptr_check_cnt\t\t\t: %ld \n", __spp_vol_ptr_check_cnt);
printf("__spp_cleantag_cnt\t\t\t: %ld \n", __spp_cleantag_cnt);
printf("USELESS__spp_cleantag_cnt\t\t: %ld \n", useless__spp_cleantag_cnt);
printf("__spp_cleantag_external_cnt\t\t: %ld \n", __spp_cleantag_external_cnt);
printf("USELESS__spp_cleantag_external_cnt\t: %ld \n", useless__spp_cleantag_external_cnt);
printf("__spp_checkbound_cnt\t\t\t: %ld \n", __spp_checkbound_cnt);
printf("USELESS__spp_checkbound_cnt\t\t: %ld \n", useless__spp_checkbound_cnt);
printf("__spp_updatetag_cnt\t\t\t: %ld \n", __spp_updatetag_cnt);
printf("USELESS__spp_updatetag_cnt\t\t: %ld \n", useless__spp_updatetag_cnt);
printf("__spp_memintr_check_and_clean_cnt\t: %ld \n", __spp_memintr_check_and_clean_cnt);
printf("\n");
printf("***** SPP direct runtime statistics *****\n");
printf("__spp_cleantag_cnt_direct\t\t: %ld \n", __spp_cleantag_cnt_direct);
printf("__spp_cleantag_external_cnt_direct\t: %ld \n", __spp_cleantag_external_cnt_direct);
printf("__spp_checkbound_cnt_direct\t\t: %ld \n", __spp_checkbound_cnt_direct);
printf("__spp_updatetag_cnt_direct\t\t: %ld \n", __spp_updatetag_cnt_direct);
printf("__spp_memintr_check_and_clean_cnt_direct: %ld \n", __spp_memintr_check_and_clean_cnt_direct);
printf("\n");
struct func_cnt* curr = head;
struct func_cnt* temp;
while (curr) {
temp = curr;
printf("%d in check %s\n", curr->cnt, curr->fname);
curr = curr->next;
free(temp->fname);
free(temp);
}
return;
}
// to print runtime stats at exit
// needs to be combined with SPP_STATS
#ifdef PRINT_STATS
int declared = 0;
#else
int declared = 1;
#endif
__SPP_ATTR
void
__spp_update_checkcnt(char *fname) {
if (head == NULL)
{
head = (struct func_cnt*)malloc(sizeof(struct func_cnt));
head->fname = (char*) malloc(strlen(fname)+1);
strcpy(head->fname, fname);
head->cnt = 1;
head->next = NULL;
}
else
{
struct func_cnt* curr = head;
while(strcmp(curr->fname, fname) != 0)
{
if (curr->next != NULL)
{
curr = curr->next;
}
else
{
curr->next = (struct func_cnt*)malloc(sizeof(struct func_cnt));
curr->next->fname = (char*)malloc(strlen(fname)+1);
strcpy(curr->next->fname, fname);
curr->next->cnt = 1;
curr->next->next = NULL;
}
}
curr->cnt++;
}
return;
}
#else
#define stats(x)
#endif
///////////////////////////////
/// End of Stats Macro ///
///////////////////////////////
/*
* https://stackoverflow.com/questions/3899870/print-call-stack-in-c-or-c
*/
static void
error_report(const char* func, void* ptr)
{
printf("\033[0;31m");
printf("!!!!> OVERFLOW detected at %s for %p\n", __func__, ptr);
printf("\033[0;34m");
char **strings;
size_t i, size;
enum Constexpr { MAX_SIZE = 1024 };
void *array[MAX_SIZE];
size = backtrace(array, MAX_SIZE);
strings = backtrace_symbols(array, size);
for (i = 0; i < size; i++)
printf("%s\n", strings[i]);
puts("");
free(strings);
printf("\033[0m");
fflush(stdout);
raise(SIGINT);
exit(1);
}
__SPP_ATTR
uintptr_t
__spp_extract_tagval(void *ptr)
{
stats(__spp_extract_tagval_cnt++;)
#if defined(__x86_64__) && defined(__BMI2__) && !defined(HW_OFF)
return _pext_u64((uintptr_t)ptr, TAG_MASK);
#else
return ((uintptr_t)ptr & TAG_MASK) >> NUM_PTR_BITS;
#endif
}
__SPP_ATTR
uintptr_t
__spp_is_pm_ptr(void *ptr)
{
stats(__spp_is_pm_ptr_cnt++;)
#if defined(__x86_64__) && defined(__BMI2__) && !defined(HW_OFF)
return _pext_u64((uintptr_t)ptr, PM_PTR_MASK);
#else
return ((uintptr_t)ptr) & PM_PTR_MASK;
#endif
}
__SPP_ATTR
void
__spp_manual_checkbound(void *ptr)
{
dbg(printf("%s %p\n", __func__, ptr);)
#if defined(__x86_64__) && defined(__BMI2__) && !defined(HW_OFF)
if (_pext_u64((uintptr_t)ptr, OVERFLOW_MASK))
#else
if ((uintptr_t)ptr & OVERFLOW_MASK)
#endif
{
//simply print it in red
dbg(printf("\033[0;31m");)
dbg(printf("!!!!> OVERFLOW detected at %s for %p\n", __func__, ptr);)
error_report(__func__, ptr);
}
}
__SPP_ATTR
void*
__spp_cleantag(void *ptr)
{
stats(__spp_cleantag_cnt++;)
dbg(printf(">>%s with %p\n", __func__, ptr);)
if (!__spp_is_pm_ptr(ptr))
{
stats(useless__spp_cleantag_cnt++;)
stats(__spp_vol_ptr_check_cnt++;)
//if ptr is not tagged, return!
return ptr;
}
uintptr_t ptrval = (uintptr_t)ptr & PTR_MASK;
dbg(printf(">>%s new_ptr: %p\n", __func__, ptrval);)
return (void*)ptrval;
}
__SPP_ATTR
void*
__spp_cleantag_direct(void *ptr)
{
stats(__spp_cleantag_cnt++;)
stats(__spp_cleantag_cnt_direct++;)
dbg(printf(">>%s with %p\n", __func__, ptr);)
uintptr_t ptrval = (uintptr_t)ptr & PTR_MASK;
dbg(printf(">>%s new_ptr: %p\n", __func__, ptrval);)
return (void*)ptrval;
}
__SPP_ATTR
void*
__spp_cleantag_external(void *ptr)
{
stats(
if (!declared)
{
atexit(__spp_runtime_stats);
declared=1;
}
)
stats(__spp_cleantag_external_cnt++;)
dbg(printf(">>%s with %p\n", __func__, ptr);)
if (!__spp_is_pm_ptr(ptr))
{
stats(useless__spp_cleantag_external_cnt++;)
stats(__spp_vol_ptr_check_cnt++;)
//if ptr is not tagged, return!
return ptr;
}
uintptr_t ptrval = (uintptr_t)ptr & PTR_MASK;
dbg(printf(">>%s old_ptr: %p new_ptr: %p\n", __func__, ptr, ptrval);)
return (void*)ptrval;
}
__SPP_ATTR
void*
__spp_cleantag_external_direct(void *ptr)
{
stats(
if (!declared)
{
atexit(__spp_runtime_stats);
declared=1;
}
)
stats(__spp_cleantag_external_cnt++;)
stats(__spp_cleantag_external_cnt_direct++;)
dbg(printf(">>%s with %p\n", __func__, ptr);)
uintptr_t ptrval = (uintptr_t)ptr & PTR_MASK;
dbg(printf(">>%s old_ptr: %p new_ptr: %p\n", __func__, ptr, ptrval);)
return (void*)ptrval;
}
__SPP_ATTR
void*
__spp_checkbound(void *ptr, int64_t deref_size)
{
#ifdef MOCK_SPP_FUNCS
return __spp_cleantag(ptr);
#endif
stats(__spp_checkbound_cnt++;)
dbg(printf(">>%s with %p and deref size: %ld\n", __func__, ptr, deref_size);)
if (!__spp_is_pm_ptr(ptr))
{
stats(__spp_vol_ptr_check_cnt++;)
//if ptr is not tagged, return!
stats(useless__spp_checkbound_cnt++;)
return ptr;
}
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
tag = tag + deref_size; // -1 to the deref size is applied from the pass
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
tempval = tempval & OVERFLOW_MASK; // keep only the tag overflow bit
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
void* updated_clean_ptr = (void*)(untagged | tempval);
#ifdef FAIL_OBL_COMP
__spp_manual_checkbound(updated_clean_ptr);
#endif
return updated_clean_ptr;
}
__SPP_ATTR
void*
__spp_checkbound_direct(void *ptr, int64_t deref_size)
{
#ifdef MOCK_SPP_FUNCS
return __spp_cleantag_direct(ptr);
#endif
stats(__spp_checkbound_cnt++;)
stats(__spp_checkbound_cnt_direct++;)
dbg(printf(">>%s with %p and deref size: %ld\n", __func__, ptr, deref_size);)
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
tag = tag + deref_size; // -1 to the deref size is applied from the pass
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
tempval = tempval & OVERFLOW_MASK; // keep only the tag overflow bit
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
void* updated_clean_ptr = (void*)(untagged | tempval);
#ifdef FAIL_OBL_COMP
__spp_manual_checkbound(updated_clean_ptr);
#endif
return updated_clean_ptr;
}
__SPP_ATTR
void*
__spp_updatetag(void *ptr, int64_t off)
{
#ifdef MOCK_SPP_FUNCS
return ptr;
#endif
stats(__spp_updatetag_cnt++;)
// ptr: after pointer arithmetic (i.e. GEP itself)
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
if (!__spp_is_pm_ptr(ptr))
{
stats(__spp_vol_ptr_check_cnt++;)
//if ptr is not tagged, return!
stats(useless__spp_updatetag_cnt++;)
return ptr;
}
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
tag = tag + off;
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
uintptr_t ptrval = untagged | tempval;
dbg(printf(">>%s new_ptr: %p\n", __func__, ptrval);)
return (void*)ptrval;
}
__SPP_ATTR
void*
__spp_updatetag_direct(void *ptr, int64_t off)
{
#ifdef MOCK_SPP_FUNCS
return ptr;
#endif
stats(__spp_updatetag_cnt++;)
stats(__spp_updatetag_cnt_direct++;)
// ptr: after pointer arithmetic (i.e. GEP itself)
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
tag = tag + off;
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
uintptr_t ptrval = untagged | tempval;
dbg(printf(">>%s new_ptr: %p\n", __func__, ptrval);)
return (void*)ptrval;
}
__SPP_ATTR
void*
__spp_update_check_clean(void *ptr, int64_t off)
{
#ifdef MOCK_SPP_FUNCS
return __spp_cleantag(ptr);
#endif
stats(__spp_updatetag_cnt++;)
stats(__spp_checkbound_cnt++;)
if (!__spp_is_pm_ptr(ptr))
{
stats(__spp_vol_ptr_check_cnt++;)
//if ptr is not tagged, return!
stats(useless__spp_updatetag_cnt++;)
stats(useless__spp_checkbound_cnt++;)
return ptr;
}
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
tag = tag + off;
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
tempval = tempval & OVERFLOW_MASK; // keep only the tag overflow bit
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
#ifdef FAIL_OBL_COMP
void* updatedPtr = (void*)(untagged | tempval);
__spp_manual_checkbound(updatedPtr);
#endif
return (void*)(untagged | tempval);
}
__SPP_ATTR
void*
__spp_update_check_clean_direct(void *ptr, int64_t off)
{
#ifdef MOCK_SPP_FUNCS
return __spp_cleantag_direct(ptr);
#endif
stats(__spp_updatetag_cnt++;)
stats(__spp_updatetag_cnt_direct++;)
stats(__spp_checkbound_cnt++;)
stats(__spp_checkbound_cnt_direct++;)
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
tag = tag + off;
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
tempval = tempval & OVERFLOW_MASK; // keep only the tag overflow bit
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
#ifdef FAIL_OBL_COMP
void* updatedPtr = (void*)(untagged | tempval);
__spp_manual_checkbound(updatedPtr);
#endif
return (void*)(untagged | tempval);
}
__SPP_ATTR
void*
__spp_memintr_check_and_clean(void *ptr, int64_t off)
{
#ifdef MOCK_SPP_FUNCS
return __spp_cleantag(ptr);
#endif
stats(__spp_memintr_check_and_clean_cnt++;)
// ptr: pointer pass to LLVM memory intrinsic
// off: offset to be added and checked
// calculates the final address of the ptr according to the offset
// performs bounds check
// returns the clean ptr for the memory intrinsic function
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
if (!__spp_is_pm_ptr(ptr))
{
stats(__spp_vol_ptr_check_cnt++;)
//if ptr is not tagged, return!
return ptr;
}
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
// -1 is applied due to addressing starting from 0
dbg(printf(">>%s with %p and offset %ld and tag %lx\n", __func__, ptr, off, tag);)
tag = tag + (off - 1);
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
tempval = tempval & OVERFLOW_MASK; // keep only the tag overflow bit
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
dbg(printf(">>%s checked ptr: tag %p addr %p\n", __func__, tempval, untagged);)
#ifdef FAIL_OBL_COMP
void* updatedPtr = (void*)(untagged | tempval);
__spp_manual_checkbound(updatedPtr);
#endif
return (void*)(untagged | tempval);
}
__SPP_ATTR
void*
__spp_memintr_check_and_clean_direct(void *ptr, int64_t off)
{
#ifdef MOCK_SPP_FUNCS
return __spp_cleantag_direct(ptr);
#endif
stats(__spp_memintr_check_and_clean_cnt++;)
stats(__spp_memintr_check_and_clean_cnt_direct++;)
// ptr: pointer pass to LLVM memory intrinsic
// off: offset to be added and checked
// calculates the final address of the ptr according to the offset
// performs bounds check
// returns the clean ptr for the memory intrinsic function
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
int64_t tag = (int64_t)__spp_extract_tagval(ptr);
// -1 is applied due to addressing starting from 0
dbg(printf(">>%s with %p and offset %ld and tag %lx\n", __func__, ptr, off, tag);)
tag = tag + (off - 1);
uintptr_t tempval = ((uintptr_t)tag) << NUM_PTR_BITS; // | PM_PTR_SET;
tempval = tempval & OVERFLOW_MASK; // keep only the tag overflow bit
uintptr_t untagged = (uintptr_t)__spp_cleantag_direct(ptr);
untagged = untagged & ~OVERFLOW_MASK; // remove previous overflow bit
dbg(printf(">>%s checked ptr: tag %p addr %p\n", __func__, tempval, untagged);)
#ifdef FAIL_OBL_COMP
void* updatedPtr = (void*)(untagged | tempval);
__spp_manual_checkbound(updatedPtr);
#endif
return (void*)(untagged | tempval);
}
// it is not desirable that this func is called
// normally, it SHOULDN'T be called
__SPP_ATTR
void*
__spp_update_pointer(void *ptr, int64_t off) {
dbg(printf(">>%s with %p and offset %ld \n", __func__, ptr, off);)
void *newptr = (void*)((int64_t)ptr + off);
return __spp_updatetag(newptr, off);
}
#ifdef __cplusplus
}
#endif