12
12
// ===----------------------------------------------------------------------===//
13
13
14
14
#include " interception/interception.h"
15
+ #include " sanitizer_common/sanitizer_allocator_dlsym.h"
15
16
#include " sanitizer_common/sanitizer_common.h"
16
17
#include " tysan/tysan.h"
17
18
@@ -28,23 +29,11 @@ extern "C" int mallopt(int param, int value);
28
29
using namespace __sanitizer ;
29
30
using namespace __tysan ;
30
31
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
48
37
49
38
INTERCEPTOR (void *, memset, void *dst, int v, uptr size) {
50
39
if (!tysan_inited && REAL (memset) == nullptr )
@@ -111,16 +100,17 @@ INTERCEPTOR(char *, __strdup, const char *s) {
111
100
#endif // TYSAN_INTERCEPT___STRDUP
112
101
113
102
INTERCEPTOR (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);
117
105
void *res = REAL (malloc)(size);
118
106
if (res)
119
107
tysan_set_type_unknown (res, size);
120
108
return res;
121
109
}
122
110
123
111
INTERCEPTOR (void *, realloc, void *ptr, uptr size) {
112
+ if (DlsymAlloc::Use () || DlsymAlloc::PointerIsMine (ptr))
113
+ return DlsymAlloc::Realloc (ptr, size);
124
114
void *res = REAL (realloc)(ptr, size);
125
115
// We might want to copy the types from the original allocation (although
126
116
// that would require that we knew its size).
@@ -130,21 +120,18 @@ INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
130
120
}
131
121
132
122
INTERCEPTOR (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);
136
125
void *res = REAL (calloc)(nmemb, size);
137
126
if (res)
138
127
tysan_set_type_unknown (res, nmemb * size);
139
128
return res;
140
129
}
141
130
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);
148
135
}
149
136
150
137
INTERCEPTOR (void *, valloc, uptr size) {
0 commit comments