forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathignore_free_hook.cpp
118 lines (104 loc) · 3.57 KB
/
ignore_free_hook.cpp
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
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=basic_hook_works && not %run %t \
// RUN: |& FileCheck %s -check-prefix=CHECK-BASIC
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=ignore && %run %t \
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=ignore_twice && not %run %t \
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE-2
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=mismatch && not %run %t \
// RUN: |& FileCheck %s -check-prefix=CHECK-MISMATCH
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=ignore_mismatch && %run %t \
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE-MISMATCH
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=double_delete && not %run %t \
// RUN: |& FileCheck %s -check-prefix=CHECK-DOUBLE-DELETE
#include <stdio.h>
#include <stdlib.h>
static char *volatile glob_ptr;
bool ignore_free = false;
#if (__APPLE__)
// Required for dyld macOS 12.0+
# define WEAK_ON_APPLE __attribute__((weak))
#else // !(__APPLE__)
# define WEAK_ON_APPLE
#endif // (__APPLE__)
extern "C" {
WEAK_ON_APPLE void __sanitizer_free_hook(const volatile void *ptr) {
if (ptr == glob_ptr)
fprintf(stderr, "Free Hook\n");
}
WEAK_ON_APPLE int __sanitizer_ignore_free_hook(const volatile void *ptr) {
if (ptr != glob_ptr)
return 0;
fprintf(stderr, ignore_free ? "Free Ignored\n" : "Free Respected\n");
return ignore_free;
}
} // extern "C"
void allocate() { glob_ptr = reinterpret_cast<char *volatile>(malloc(100)); }
void deallocate() { free(reinterpret_cast<void *>(glob_ptr)); }
void basic_hook_works() {
allocate();
deallocate(); // CHECK-BASIC-NOT: Free Ignored
// CHECK-BASIC: Free Respected
// CHECK-BASIC: Free Hook
*glob_ptr = 0; // CHECK-BASIC: AddressSanitizer: heap-use-after-free
}
void ignore() {
allocate();
ignore_free = true;
deallocate();
// CHECK-IGNORE: Free Ignored
// CHECK-IGNORE-NOT: Free Respected
// CHECK-IGNORE-NOT: Free Hook
// CHECK-IGNORE-NOT: AddressSanitizer
*glob_ptr = 0;
}
void ignore_twice() {
allocate();
ignore_free = true;
deallocate(); // CHECK-IGNORE-2: Free Ignored
*glob_ptr = 0;
ignore_free = false;
deallocate(); // CHECK-IGNORE-2-NOT: Free Ignored
// CHECK-IGNORE-2: Free Respected
// CHECK-IGNORE-2: Free Hook
*glob_ptr = 0; // CHECK-IGNORE-2: AddressSanitizer: heap-use-after-free
}
void ignore_a_lot() {
allocate();
ignore_free = true;
for (int i = 0; i < 10000; ++i) {
deallocate(); // CHECK-IGNORE-3: Free Ignored
*glob_ptr = 0;
}
ignore_free = false;
deallocate(); // CHECK-IGNORE-3: Free Respected
// CHECK-IGNORE-3: Free Hook
*glob_ptr = 0; // CHECK-IGNORE-3: AddressSanitizer: heap-use-after-free
}
void mismatch() {
glob_ptr = new char;
deallocate(); // CHECK-MISMATCH: AddressSanitizer: alloc-dealloc-mismatch
}
void ignore_mismatch() {
glob_ptr = new char;
ignore_free = true;
// Mismatch isn't detected when the free() is ignored.
deallocate();
deallocate();
ignore_free = false;
// And also isn't detected when the memory is free()-d for real.
deallocate(); // CHECK-IGNORE-MISMATCH-NOT: AddressSanitizer: alloc-dealloc-mismatch
}
void double_delete() {
allocate();
ignore_free = true;
deallocate(); // CHECK-DOUBLE-DELETE: Free Ignored
deallocate(); // CHECK-DOUBLE-DELETE: Free Ignored
ignore_free = false;
deallocate(); // CHECK-DOUBLE-DELETE: Free Respected
// CHECK-DOUBLE-DELETE: Free Hook
deallocate(); // CHECK-DOUBLE-DELETE: AddressSanitizer: attempting double-free
}
int main() {
TEST();
return 0;
}