1010
1111#include " rtsan/rtsan_interceptors.h"
1212
13+ #include " sanitizer_common/sanitizer_allocator_internal.h"
1314#include " sanitizer_common/sanitizer_platform.h"
1415#include " sanitizer_common/sanitizer_platform_interceptors.h"
1516
@@ -39,26 +40,6 @@ using namespace __sanitizer;
3940using __rtsan::rtsan_init_is_running;
4041using __rtsan::rtsan_initialized;
4142
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-
6243void ExpectNotRealtime (const char *intercepted_function_name) {
6344 __rtsan::GetContextForThisThread ().ExpectNotRealtime (
6445 intercepted_function_name);
@@ -262,18 +243,16 @@ INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
262243// Memory
263244
264245INTERCEPTOR (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);
269248
270249 ExpectNotRealtime (" calloc" );
271250 return REAL (calloc)(num, size);
272251}
273252
274253INTERCEPTOR (void , free, void *ptr) {
275- if (IsInEarlyAllocBuf (ptr))
276- return ;
254+ if (__sanitizer::internal_allocator ()-> PointerIsMine (ptr))
255+ return __sanitizer::InternalFree (ptr) ;
277256
278257 if (ptr != NULL ) {
279258 ExpectNotRealtime (" free" );
@@ -282,15 +261,17 @@ INTERCEPTOR(void, free, void *ptr) {
282261}
283262
284263INTERCEPTOR (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);
288266
289267 ExpectNotRealtime (" malloc" );
290268 return REAL (malloc)(size);
291269}
292270
293271INTERCEPTOR (void *, realloc, void *ptr, SIZE_T size) {
272+ if (rtsan_init_is_running && REAL (realloc) == nullptr )
273+ return __sanitizer::InternalRealloc (ptr, size);
274+
294275 ExpectNotRealtime (" realloc" );
295276 return REAL (realloc)(ptr, size);
296277}
0 commit comments