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