|
1 |
| -//===-- nsan_interceptors.cc ----------------------------------------------===// |
| 1 | +//===- nsan_interceptors.cpp ----------------------------------------------===// |
2 | 2 | //
|
3 | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
4 | 4 | // See https://llvm.org/LICENSE.txt for license information.
|
@@ -29,26 +29,8 @@ using namespace __sanitizer;
|
29 | 29 | using __nsan::nsan_init_is_running;
|
30 | 30 | using __nsan::nsan_initialized;
|
31 | 31 |
|
32 |
| -constexpr uptr kEarlyAllocBufSize = 16384; |
33 |
| -static uptr allocated_bytes; |
34 |
| -static char early_alloc_buf[kEarlyAllocBufSize]; |
35 |
| - |
36 |
| -static bool isInEarlyAllocBuf(const void *ptr) { |
37 |
| - return ((uptr)ptr >= (uptr)early_alloc_buf && |
38 |
| - ((uptr)ptr - (uptr)early_alloc_buf) < sizeof(early_alloc_buf)); |
39 |
| -} |
40 |
| - |
41 | 32 | template <typename T> T min(T a, T b) { return a < b ? a : b; }
|
42 | 33 |
|
43 |
| -// Handle allocation requests early (before all interceptors are setup). dlsym, |
44 |
| -// for example, calls calloc. |
45 |
| -static void *HandleEarlyAlloc(uptr size) { |
46 |
| - void *Mem = (void *)&early_alloc_buf[allocated_bytes]; |
47 |
| - allocated_bytes += size; |
48 |
| - CHECK_LT(allocated_bytes, kEarlyAllocBufSize); |
49 |
| - return Mem; |
50 |
| -} |
51 |
| - |
52 | 34 | INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
|
53 | 35 | // NOTE: This guard is needed because nsan's initialization code might call
|
54 | 36 | // memset.
|
@@ -105,90 +87,6 @@ INTERCEPTOR(wchar_t *, wmemcpy, wchar_t *dst, const wchar_t *src, uptr size) {
|
105 | 87 | return res;
|
106 | 88 | }
|
107 | 89 |
|
108 |
| -INTERCEPTOR(void *, malloc, uptr size) { |
109 |
| - // NOTE: This guard is needed because nsan's initialization code might call |
110 |
| - // malloc. |
111 |
| - if (nsan_init_is_running && REAL(malloc) == nullptr) |
112 |
| - return HandleEarlyAlloc(size); |
113 |
| - |
114 |
| - void *res = REAL(malloc)(size); |
115 |
| - if (res) |
116 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
117 |
| - return res; |
118 |
| -} |
119 |
| - |
120 |
| -INTERCEPTOR(void *, realloc, void *ptr, uptr size) { |
121 |
| - void *res = REAL(realloc)(ptr, size); |
122 |
| - // FIXME: We might want to copy the types from the original allocation |
123 |
| - // (although that would require that we know its size). |
124 |
| - if (res) |
125 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
126 |
| - return res; |
127 |
| -} |
128 |
| - |
129 |
| -INTERCEPTOR(void *, calloc, uptr Nmemb, uptr size) { |
130 |
| - // NOTE: This guard is needed because nsan's initialization code might call |
131 |
| - // calloc. |
132 |
| - if (nsan_init_is_running && REAL(calloc) == nullptr) { |
133 |
| - // Note: EarlyAllocBuf is initialized with zeros. |
134 |
| - return HandleEarlyAlloc(Nmemb * size); |
135 |
| - } |
136 |
| - |
137 |
| - void *res = REAL(calloc)(Nmemb, size); |
138 |
| - if (res) |
139 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), Nmemb * size); |
140 |
| - return res; |
141 |
| -} |
142 |
| - |
143 |
| -INTERCEPTOR(void, free, void *P) { |
144 |
| - // There are only a few early allocation requests, so we simply skip the free. |
145 |
| - if (isInEarlyAllocBuf(P)) |
146 |
| - return; |
147 |
| - REAL(free)(P); |
148 |
| -} |
149 |
| - |
150 |
| -INTERCEPTOR(void *, valloc, uptr size) { |
151 |
| - void *const res = REAL(valloc)(size); |
152 |
| - if (res) |
153 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
154 |
| - return res; |
155 |
| -} |
156 |
| - |
157 |
| -INTERCEPTOR(void *, memalign, uptr align, uptr size) { |
158 |
| - void *const res = REAL(memalign)(align, size); |
159 |
| - if (res) |
160 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
161 |
| - return res; |
162 |
| -} |
163 |
| - |
164 |
| -INTERCEPTOR(void *, __libc_memalign, uptr align, uptr size) { |
165 |
| - void *const res = REAL(__libc_memalign)(align, size); |
166 |
| - if (res) |
167 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
168 |
| - return res; |
169 |
| -} |
170 |
| - |
171 |
| -INTERCEPTOR(void *, pvalloc, uptr size) { |
172 |
| - void *const res = REAL(pvalloc)(size); |
173 |
| - if (res) |
174 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
175 |
| - return res; |
176 |
| -} |
177 |
| - |
178 |
| -INTERCEPTOR(void *, aligned_alloc, uptr align, uptr size) { |
179 |
| - void *const res = REAL(aligned_alloc)(align, size); |
180 |
| - if (res) |
181 |
| - __nsan_set_value_unknown(static_cast<u8 *>(res), size); |
182 |
| - return res; |
183 |
| -} |
184 |
| - |
185 |
| -INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr size) { |
186 |
| - int res = REAL(posix_memalign)(memptr, align, size); |
187 |
| - if (res == 0 && *memptr) |
188 |
| - __nsan_set_value_unknown(static_cast<u8 *>(*memptr), size); |
189 |
| - return res; |
190 |
| -} |
191 |
| - |
192 | 90 | INTERCEPTOR(char *, strfry, char *s) {
|
193 | 91 | const auto Len = internal_strlen(s);
|
194 | 92 | char *res = REAL(strfry)(s);
|
@@ -317,16 +215,7 @@ void __nsan::InitializeInterceptors() {
|
317 | 215 | mallopt(-3, 32 * 1024); // M_MMAP_THRESHOLD
|
318 | 216 | #endif
|
319 | 217 |
|
320 |
| - INTERCEPT_FUNCTION(malloc); |
321 |
| - INTERCEPT_FUNCTION(calloc); |
322 |
| - INTERCEPT_FUNCTION(free); |
323 |
| - INTERCEPT_FUNCTION(realloc); |
324 |
| - INTERCEPT_FUNCTION(valloc); |
325 |
| - INTERCEPT_FUNCTION(memalign); |
326 |
| - INTERCEPT_FUNCTION(__libc_memalign); |
327 |
| - INTERCEPT_FUNCTION(pvalloc); |
328 |
| - INTERCEPT_FUNCTION(aligned_alloc); |
329 |
| - INTERCEPT_FUNCTION(posix_memalign); |
| 218 | + InitializeMallocInterceptors(); |
330 | 219 |
|
331 | 220 | INTERCEPT_FUNCTION(memset);
|
332 | 221 | INTERCEPT_FUNCTION(wmemset);
|
|
0 commit comments