1212// ===----------------------------------------------------------------------===//
1313
1414#include " interception/interception.h"
15+ #include " sanitizer_common/sanitizer_allocator_dlsym.h"
1516#include " sanitizer_common/sanitizer_common.h"
1617#include " tysan/tysan.h"
1718
@@ -28,23 +29,11 @@ extern "C" int mallopt(int param, int value);
2829using namespace __sanitizer ;
2930using namespace __tysan ;
3031
31- static const uptr early_alloc_buf_size = 16384 ;
32- static uptr allocated_bytes;
33- static char early_alloc_buf[early_alloc_buf_size];
34-
35- static bool isInEarlyAllocBuf (const void *ptr) {
36- return ((uptr)ptr >= (uptr)early_alloc_buf &&
37- ((uptr)ptr - (uptr)early_alloc_buf) < sizeof (early_alloc_buf));
38- }
39-
40- // Handle allocation requests early (before all interceptors are setup). dlsym,
41- // for example, calls calloc.
42- static void *handleEarlyAlloc (uptr size) {
43- void *mem = (void *)&early_alloc_buf[allocated_bytes];
44- allocated_bytes += size;
45- CHECK_LT (allocated_bytes, early_alloc_buf_size);
46- return mem;
47- }
32+ namespace {
33+ struct DlsymAlloc : public DlSymAllocator <DlsymAlloc> {
34+ static bool UseImpl () { return !tysan_inited; }
35+ };
36+ } // namespace
4837
4938INTERCEPTOR (void *, memset, void *dst, int v, uptr size) {
5039 if (!tysan_inited && REAL (memset) == nullptr )
@@ -111,16 +100,17 @@ INTERCEPTOR(char *, __strdup, const char *s) {
111100#endif // TYSAN_INTERCEPT___STRDUP
112101
113102INTERCEPTOR (void *, malloc, uptr size) {
114- if (tysan_init_is_running && REAL (malloc) == nullptr )
115- return handleEarlyAlloc (size);
116-
103+ if (DlsymAlloc::Use ())
104+ return DlsymAlloc::Allocate (size);
117105 void *res = REAL (malloc)(size);
118106 if (res)
119107 tysan_set_type_unknown (res, size);
120108 return res;
121109}
122110
123111INTERCEPTOR (void *, realloc, void *ptr, uptr size) {
112+ if (DlsymAlloc::Use () || DlsymAlloc::PointerIsMine (ptr))
113+ return DlsymAlloc::Realloc (ptr, size);
124114 void *res = REAL (realloc)(ptr, size);
125115 // We might want to copy the types from the original allocation (although
126116 // that would require that we knew its size).
@@ -130,21 +120,18 @@ INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
130120}
131121
132122INTERCEPTOR (void *, calloc, uptr nmemb, uptr size) {
133- if (tysan_init_is_running && REAL (calloc) == nullptr )
134- return handleEarlyAlloc (nmemb * size);
135-
123+ if (DlsymAlloc::Use ())
124+ return DlsymAlloc::Callocate (nmemb, size);
136125 void *res = REAL (calloc)(nmemb, size);
137126 if (res)
138127 tysan_set_type_unknown (res, nmemb * size);
139128 return res;
140129}
141130
142- INTERCEPTOR (void , free, void *p) {
143- // There are only a few early allocation requests,
144- // so we simply skip the free.
145- if (isInEarlyAllocBuf (p))
146- return ;
147- REAL (free)(p);
131+ INTERCEPTOR (void , free, void *ptr) {
132+ if (DlsymAlloc::PointerIsMine (ptr))
133+ return DlsymAlloc::Free (ptr);
134+ REAL (free)(ptr);
148135}
149136
150137INTERCEPTOR (void *, valloc, uptr size) {
0 commit comments