1 //===-- memprof_interceptors.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemProfiler, a memory profiler.
10 //
11 // Intercept various libc functions.
12 //===----------------------------------------------------------------------===//
13
14 #include "memprof_interceptors.h"
15 #include "memprof_allocator.h"
16 #include "memprof_internal.h"
17 #include "memprof_mapping.h"
18 #include "memprof_stack.h"
19 #include "memprof_stats.h"
20 #include "sanitizer_common/sanitizer_libc.h"
21 #include "sanitizer_common/sanitizer_posix.h"
22
23 namespace __memprof {
24
25 #define MEMPROF_READ_STRING(s, n) MEMPROF_READ_RANGE((s), (n))
26
MaybeRealStrnlen(const char * s,uptr maxlen)27 static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
28 #if SANITIZER_INTERCEPT_STRNLEN
29 if (REAL(strnlen)) {
30 return REAL(strnlen)(s, maxlen);
31 }
32 #endif
33 return internal_strnlen(s, maxlen);
34 }
35
SetThreadName(const char * name)36 void SetThreadName(const char *name) {
37 MemprofThread *t = GetCurrentThread();
38 if (t)
39 memprofThreadRegistry().SetThreadName(t->tid(), name);
40 }
41
OnExit()42 int OnExit() {
43 // FIXME: ask frontend whether we need to return failure.
44 return 0;
45 }
46
47 } // namespace __memprof
48
49 // ---------------------- Wrappers ---------------- {{{1
50 using namespace __memprof;
51
52 DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
53 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
54
55 #define MEMPROF_INTERCEPTOR_ENTER(ctx, func) \
56 ctx = 0; \
57 (void)ctx;
58
59 #define COMMON_INTERCEPT_FUNCTION(name) MEMPROF_INTERCEPT_FUNC(name)
60 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
61 MEMPROF_INTERCEPT_FUNC_VER(name, ver)
62 #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
63 MEMPROF_WRITE_RANGE(ptr, size)
64 #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
65 MEMPROF_READ_RANGE(ptr, size)
66 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
67 MEMPROF_INTERCEPTOR_ENTER(ctx, func); \
68 do { \
69 if (memprof_init_is_running) \
70 return REAL(func)(__VA_ARGS__); \
71 ENSURE_MEMPROF_INITED(); \
72 } while (false)
73 #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
74 do { \
75 } while (false)
76 #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
77 do { \
78 } while (false)
79 #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
80 do { \
81 } while (false)
82 #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
83 do { \
84 } while (false)
85 #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
86 // Should be memprofThreadRegistry().SetThreadNameByUserId(thread, name)
87 // But memprof does not remember UserId's for threads (pthread_t);
88 // and remembers all ever existed threads, so the linear search by UserId
89 // can be slow.
90 #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
91 do { \
92 } while (false)
93 #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
94 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \
95 do { \
96 CheckNoDeepBind(filename, flag); \
97 } while (false)
98 #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
99 #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
100 #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
101 #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!memprof_inited)
102 #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
103 if (MemprofThread *t = GetCurrentThread()) { \
104 *begin = t->tls_begin(); \
105 *end = t->tls_end(); \
106 } else { \
107 *begin = *end = 0; \
108 }
109
110 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
111 do { \
112 MEMPROF_INTERCEPTOR_ENTER(ctx, memmove); \
113 MEMPROF_MEMMOVE_IMPL(to, from, size); \
114 } while (false)
115
116 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
117 do { \
118 MEMPROF_INTERCEPTOR_ENTER(ctx, memcpy); \
119 MEMPROF_MEMCPY_IMPL(to, from, size); \
120 } while (false)
121
122 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
123 do { \
124 MEMPROF_INTERCEPTOR_ENTER(ctx, memset); \
125 MEMPROF_MEMSET_IMPL(block, c, size); \
126 } while (false)
127
128 #include "sanitizer_common/sanitizer_common_interceptors.inc"
129
130 #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) MEMPROF_READ_RANGE(p, s)
131 #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) MEMPROF_WRITE_RANGE(p, s)
132 #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
133 do { \
134 (void)(p); \
135 (void)(s); \
136 } while (false)
137 #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
138 do { \
139 (void)(p); \
140 (void)(s); \
141 } while (false)
142 #include "sanitizer_common/sanitizer_common_syscalls.inc"
143
144 struct ThreadStartParam {
145 atomic_uintptr_t t;
146 atomic_uintptr_t is_registered;
147 };
148
memprof_thread_start(void * arg)149 static thread_return_t THREAD_CALLING_CONV memprof_thread_start(void *arg) {
150 ThreadStartParam *param = reinterpret_cast<ThreadStartParam *>(arg);
151 MemprofThread *t = nullptr;
152 while ((t = reinterpret_cast<MemprofThread *>(
153 atomic_load(¶m->t, memory_order_acquire))) == nullptr)
154 internal_sched_yield();
155 SetCurrentThread(t);
156 return t->ThreadStart(GetTid(), ¶m->is_registered);
157 }
158
INTERCEPTOR(int,pthread_create,void * thread,void * attr,void * (* start_routine)(void *),void * arg)159 INTERCEPTOR(int, pthread_create, void *thread, void *attr,
160 void *(*start_routine)(void *), void *arg) {
161 EnsureMainThreadIDIsCorrect();
162 GET_STACK_TRACE_THREAD;
163 int detached = 0;
164 if (attr)
165 REAL(pthread_attr_getdetachstate)(attr, &detached);
166 ThreadStartParam param;
167 atomic_store(¶m.t, 0, memory_order_relaxed);
168 atomic_store(¶m.is_registered, 0, memory_order_relaxed);
169 int result;
170 {
171 // Ignore all allocations made by pthread_create: thread stack/TLS may be
172 // stored by pthread for future reuse even after thread destruction, and
173 // the linked list it's stored in doesn't even hold valid pointers to the
174 // objects, the latter are calculated by obscure pointer arithmetic.
175 result = REAL(pthread_create)(thread, attr, memprof_thread_start, ¶m);
176 }
177 if (result == 0) {
178 u32 current_tid = GetCurrentTidOrInvalid();
179 MemprofThread *t = MemprofThread::Create(start_routine, arg, current_tid,
180 &stack, detached);
181 atomic_store(¶m.t, reinterpret_cast<uptr>(t), memory_order_release);
182 // Wait until the MemprofThread object is initialized and the
183 // ThreadRegistry entry is in "started" state.
184 while (atomic_load(¶m.is_registered, memory_order_acquire) == 0)
185 internal_sched_yield();
186 }
187 return result;
188 }
189
INTERCEPTOR(int,pthread_join,void * t,void ** arg)190 INTERCEPTOR(int, pthread_join, void *t, void **arg) {
191 return real_pthread_join(t, arg);
192 }
193
194 DEFINE_REAL_PTHREAD_FUNCTIONS
195
196 INTERCEPTOR(char *, index, const char *string, int c)
197 ALIAS(WRAPPER_NAME(strchr));
198
199 // For both strcat() and strncat() we need to check the validity of |to|
200 // argument irrespective of the |from| length.
INTERCEPTOR(char *,strcat,char * to,const char * from)201 INTERCEPTOR(char *, strcat, char *to, const char *from) {
202 void *ctx;
203 MEMPROF_INTERCEPTOR_ENTER(ctx, strcat);
204 ENSURE_MEMPROF_INITED();
205 uptr from_length = REAL(strlen)(from);
206 MEMPROF_READ_RANGE(from, from_length + 1);
207 uptr to_length = REAL(strlen)(to);
208 MEMPROF_READ_STRING(to, to_length);
209 MEMPROF_WRITE_RANGE(to + to_length, from_length + 1);
210 return REAL(strcat)(to, from);
211 }
212
INTERCEPTOR(char *,strncat,char * to,const char * from,uptr size)213 INTERCEPTOR(char *, strncat, char *to, const char *from, uptr size) {
214 void *ctx;
215 MEMPROF_INTERCEPTOR_ENTER(ctx, strncat);
216 ENSURE_MEMPROF_INITED();
217 uptr from_length = MaybeRealStrnlen(from, size);
218 uptr copy_length = Min(size, from_length + 1);
219 MEMPROF_READ_RANGE(from, copy_length);
220 uptr to_length = REAL(strlen)(to);
221 MEMPROF_READ_STRING(to, to_length);
222 MEMPROF_WRITE_RANGE(to + to_length, from_length + 1);
223 return REAL(strncat)(to, from, size);
224 }
225
INTERCEPTOR(char *,strcpy,char * to,const char * from)226 INTERCEPTOR(char *, strcpy, char *to, const char *from) {
227 void *ctx;
228 MEMPROF_INTERCEPTOR_ENTER(ctx, strcpy);
229 if (memprof_init_is_running) {
230 return REAL(strcpy)(to, from);
231 }
232 ENSURE_MEMPROF_INITED();
233 uptr from_size = REAL(strlen)(from) + 1;
234 MEMPROF_READ_RANGE(from, from_size);
235 MEMPROF_WRITE_RANGE(to, from_size);
236 return REAL(strcpy)(to, from);
237 }
238
INTERCEPTOR(char *,strdup,const char * s)239 INTERCEPTOR(char *, strdup, const char *s) {
240 void *ctx;
241 MEMPROF_INTERCEPTOR_ENTER(ctx, strdup);
242 if (UNLIKELY(!memprof_inited))
243 return internal_strdup(s);
244 ENSURE_MEMPROF_INITED();
245 uptr length = REAL(strlen)(s);
246 MEMPROF_READ_RANGE(s, length + 1);
247 GET_STACK_TRACE_MALLOC;
248 void *new_mem = memprof_malloc(length + 1, &stack);
249 REAL(memcpy)(new_mem, s, length + 1);
250 return reinterpret_cast<char *>(new_mem);
251 }
252
INTERCEPTOR(char *,__strdup,const char * s)253 INTERCEPTOR(char *, __strdup, const char *s) {
254 void *ctx;
255 MEMPROF_INTERCEPTOR_ENTER(ctx, strdup);
256 if (UNLIKELY(!memprof_inited))
257 return internal_strdup(s);
258 ENSURE_MEMPROF_INITED();
259 uptr length = REAL(strlen)(s);
260 MEMPROF_READ_RANGE(s, length + 1);
261 GET_STACK_TRACE_MALLOC;
262 void *new_mem = memprof_malloc(length + 1, &stack);
263 REAL(memcpy)(new_mem, s, length + 1);
264 return reinterpret_cast<char *>(new_mem);
265 }
266
INTERCEPTOR(char *,strncpy,char * to,const char * from,uptr size)267 INTERCEPTOR(char *, strncpy, char *to, const char *from, uptr size) {
268 void *ctx;
269 MEMPROF_INTERCEPTOR_ENTER(ctx, strncpy);
270 ENSURE_MEMPROF_INITED();
271 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
272 MEMPROF_READ_RANGE(from, from_size);
273 MEMPROF_WRITE_RANGE(to, size);
274 return REAL(strncpy)(to, from, size);
275 }
276
INTERCEPTOR(long,strtol,const char * nptr,char ** endptr,int base)277 INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) {
278 void *ctx;
279 MEMPROF_INTERCEPTOR_ENTER(ctx, strtol);
280 ENSURE_MEMPROF_INITED();
281 char *real_endptr;
282 long result = REAL(strtol)(nptr, &real_endptr, base);
283 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
284 return result;
285 }
286
INTERCEPTOR(int,atoi,const char * nptr)287 INTERCEPTOR(int, atoi, const char *nptr) {
288 void *ctx;
289 MEMPROF_INTERCEPTOR_ENTER(ctx, atoi);
290 ENSURE_MEMPROF_INITED();
291 char *real_endptr;
292 // "man atoi" tells that behavior of atoi(nptr) is the same as
293 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
294 // parsed integer can't be stored in *long* type (even if it's
295 // different from int). So, we just imitate this behavior.
296 int result = REAL(strtol)(nptr, &real_endptr, 10);
297 FixRealStrtolEndptr(nptr, &real_endptr);
298 MEMPROF_READ_STRING(nptr, (real_endptr - nptr) + 1);
299 return result;
300 }
301
INTERCEPTOR(long,atol,const char * nptr)302 INTERCEPTOR(long, atol, const char *nptr) {
303 void *ctx;
304 MEMPROF_INTERCEPTOR_ENTER(ctx, atol);
305 ENSURE_MEMPROF_INITED();
306 char *real_endptr;
307 long result = REAL(strtol)(nptr, &real_endptr, 10);
308 FixRealStrtolEndptr(nptr, &real_endptr);
309 MEMPROF_READ_STRING(nptr, (real_endptr - nptr) + 1);
310 return result;
311 }
312
INTERCEPTOR(long long,strtoll,const char * nptr,char ** endptr,int base)313 INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
314 void *ctx;
315 MEMPROF_INTERCEPTOR_ENTER(ctx, strtoll);
316 ENSURE_MEMPROF_INITED();
317 char *real_endptr;
318 long long result = REAL(strtoll)(nptr, &real_endptr, base);
319 StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
320 return result;
321 }
322
INTERCEPTOR(long long,atoll,const char * nptr)323 INTERCEPTOR(long long, atoll, const char *nptr) {
324 void *ctx;
325 MEMPROF_INTERCEPTOR_ENTER(ctx, atoll);
326 ENSURE_MEMPROF_INITED();
327 char *real_endptr;
328 long long result = REAL(strtoll)(nptr, &real_endptr, 10);
329 FixRealStrtolEndptr(nptr, &real_endptr);
330 MEMPROF_READ_STRING(nptr, (real_endptr - nptr) + 1);
331 return result;
332 }
333
334 // ---------------------- InitializeMemprofInterceptors ---------------- {{{1
335 namespace __memprof {
InitializeMemprofInterceptors()336 void InitializeMemprofInterceptors() {
337 static bool was_called_once;
338 CHECK(!was_called_once);
339 was_called_once = true;
340 InitializeCommonInterceptors();
341
342 // Intercept str* functions.
343 MEMPROF_INTERCEPT_FUNC(strcat);
344 MEMPROF_INTERCEPT_FUNC(strcpy);
345 MEMPROF_INTERCEPT_FUNC(strncat);
346 MEMPROF_INTERCEPT_FUNC(strncpy);
347 MEMPROF_INTERCEPT_FUNC(strdup);
348 MEMPROF_INTERCEPT_FUNC(__strdup);
349 MEMPROF_INTERCEPT_FUNC(index);
350
351 MEMPROF_INTERCEPT_FUNC(atoi);
352 MEMPROF_INTERCEPT_FUNC(atol);
353 MEMPROF_INTERCEPT_FUNC(strtol);
354 MEMPROF_INTERCEPT_FUNC(atoll);
355 MEMPROF_INTERCEPT_FUNC(strtoll);
356
357 // Intercept threading-related functions
358 MEMPROF_INTERCEPT_FUNC(pthread_create);
359 MEMPROF_INTERCEPT_FUNC(pthread_join);
360
361 InitializePlatformInterceptors();
362
363 VReport(1, "MemProfiler: libc interceptors initialized\n");
364 }
365
366 } // namespace __memprof
367