10
10
11
11
#include " rtsan/rtsan_interceptors.h"
12
12
13
+ #include " sanitizer_common/sanitizer_allocator_internal.h"
13
14
#include " sanitizer_common/sanitizer_platform.h"
14
15
#include " sanitizer_common/sanitizer_platform_interceptors.h"
15
16
@@ -39,26 +40,6 @@ using namespace __sanitizer;
39
40
using __rtsan::rtsan_init_is_running;
40
41
using __rtsan::rtsan_initialized;
41
42
42
- constexpr uptr kEarlyAllocBufSize = 16384 ;
43
- static uptr allocated_bytes;
44
- static char early_alloc_buf[kEarlyAllocBufSize ];
45
-
46
- static bool IsInEarlyAllocBuf (const void *ptr) {
47
- return ((uptr)ptr >= (uptr)early_alloc_buf &&
48
- ((uptr)ptr - (uptr)early_alloc_buf) < sizeof (early_alloc_buf));
49
- }
50
-
51
- template <typename T> T min (T a, T b) { return a < b ? a : b; }
52
-
53
- // Handle allocation requests early (before all interceptors are setup). dlsym,
54
- // for example, calls calloc.
55
- static void *HandleEarlyAlloc (uptr size) {
56
- void *Mem = (void *)&early_alloc_buf[allocated_bytes];
57
- allocated_bytes += size;
58
- CHECK_LT (allocated_bytes, kEarlyAllocBufSize );
59
- return Mem;
60
- }
61
-
62
43
void ExpectNotRealtime (const char *intercepted_function_name) {
63
44
__rtsan::GetContextForThisThread ().ExpectNotRealtime (
64
45
intercepted_function_name);
@@ -262,18 +243,16 @@ INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
262
243
// Memory
263
244
264
245
INTERCEPTOR (void *, calloc, SIZE_T num, SIZE_T size) {
265
- if (rtsan_init_is_running && REAL (calloc) == nullptr ) {
266
- // Note: EarlyAllocBuf is initialized with zeros.
267
- return HandleEarlyAlloc (num * size);
268
- }
246
+ if (rtsan_init_is_running && REAL (calloc) == nullptr )
247
+ return __sanitizer::InternalCalloc (num, size);
269
248
270
249
ExpectNotRealtime (" calloc" );
271
250
return REAL (calloc)(num, size);
272
251
}
273
252
274
253
INTERCEPTOR (void , free, void *ptr) {
275
- if (IsInEarlyAllocBuf (ptr))
276
- return ;
254
+ if (__sanitizer::internal_allocator ()-> PointerIsMine (ptr))
255
+ return __sanitizer::InternalFree (ptr) ;
277
256
278
257
if (ptr != NULL ) {
279
258
ExpectNotRealtime (" free" );
@@ -282,15 +261,17 @@ INTERCEPTOR(void, free, void *ptr) {
282
261
}
283
262
284
263
INTERCEPTOR (void *, malloc, SIZE_T size) {
285
- if (rtsan_init_is_running && REAL (malloc) == nullptr ) {
286
- return HandleEarlyAlloc (size);
287
- }
264
+ if (rtsan_init_is_running && REAL (malloc) == nullptr )
265
+ return __sanitizer::InternalAlloc (size);
288
266
289
267
ExpectNotRealtime (" malloc" );
290
268
return REAL (malloc)(size);
291
269
}
292
270
293
271
INTERCEPTOR (void *, realloc, void *ptr, SIZE_T size) {
272
+ if (rtsan_init_is_running && REAL (realloc) == nullptr )
273
+ return __sanitizer::InternalRealloc (ptr, size);
274
+
294
275
ExpectNotRealtime (" realloc" );
295
276
return REAL (realloc)(ptr, size);
296
277
}
0 commit comments