aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang14-rt/lib/memprof/memprof_interceptors.cpp
blob: 459ad03e8dfe2378ac50efee2b63e5ebfa6e44c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//===-- memprof_interceptors.cpp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of MemProfiler, a memory profiler.
//
// Intercept various libc functions.
//===----------------------------------------------------------------------===//

#include "memprof_interceptors.h"
#include "memprof_allocator.h"
#include "memprof_internal.h"
#include "memprof_mapping.h"
#include "memprof_stack.h"
#include "memprof_stats.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_posix.h"

namespace __memprof {

#define MEMPROF_READ_STRING(s, n) MEMPROF_READ_RANGE((s), (n))

static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
#if SANITIZER_INTERCEPT_STRNLEN
  if (REAL(strnlen)) {
    return REAL(strnlen)(s, maxlen);
  }
#endif
  return internal_strnlen(s, maxlen);
}

void SetThreadName(const char *name) {
  MemprofThread *t = GetCurrentThread();
  if (t)
    memprofThreadRegistry().SetThreadName(t->tid(), name);
}

int OnExit() {
  // FIXME: ask frontend whether we need to return failure.
  return 0;
}

} // namespace __memprof

// ---------------------- Wrappers ---------------- {{{1
using namespace __memprof;

DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)

#define MEMPROF_INTERCEPTOR_ENTER(ctx, func)                                   \
  ctx = 0;                                                                     \
  (void)ctx;

#define COMMON_INTERCEPT_FUNCTION(name) MEMPROF_INTERCEPT_FUNC(name)
#define COMMON_INTERCEPT_FUNCTION_VER(name, ver)                               \
  MEMPROF_INTERCEPT_FUNC_VER(name, ver)
#define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver)          \
  MEMPROF_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver)
#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size)                         \
  MEMPROF_WRITE_RANGE(ptr, size)
#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size)                          \
  MEMPROF_READ_RANGE(ptr, size)
#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)                               \
  MEMPROF_INTERCEPTOR_ENTER(ctx, func);                                        \
  do {                                                                         \
    if (memprof_init_is_running)                                               \
      return REAL(func)(__VA_ARGS__);                                          \
    ENSURE_MEMPROF_INITED();                                                   \
  } while (false)
#define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path)                              \
  do {                                                                         \
  } while (false)
#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd)                                 \
  do {                                                                         \
  } while (false)
#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd)                                 \
  do {                                                                         \
  } while (false)
#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd)                    \
  do {                                                                         \
  } while (false)
#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
// Should be memprofThreadRegistry().SetThreadNameByUserId(thread, name)
// But memprof does not remember UserId's for threads (pthread_t);
// and remembers all ever existed threads, so the linear search by UserId
// can be slow.
#define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name)                 \
  do {                                                                         \
  } while (false)
#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
#define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
#define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!memprof_inited)
#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end)                           \
  if (MemprofThread *t = GetCurrentThread()) {                                 \
    *begin = t->tls_begin();                                                   \
    *end = t->tls_end();                                                       \
  } else {                                                                     \
    *begin = *end = 0;                                                         \
  }

#define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size)                   \
  do {                                                                         \
    MEMPROF_INTERCEPTOR_ENTER(ctx, memmove);                                   \
    MEMPROF_MEMMOVE_IMPL(to, from, size);                                      \
  } while (false)

#define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size)                    \
  do {                                                                         \
    MEMPROF_INTERCEPTOR_ENTER(ctx, memcpy);                                    \
    MEMPROF_MEMCPY_IMPL(to, from, size);                                       \
  } while (false)

#define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size)                    \
  do {                                                                         \
    MEMPROF_INTERCEPTOR_ENTER(ctx, memset);                                    \
    MEMPROF_MEMSET_IMPL(block, c, size);                                       \
  } while (false)

#include "sanitizer_common/sanitizer_common_interceptors.inc"

#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) MEMPROF_READ_RANGE(p, s)
#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) MEMPROF_WRITE_RANGE(p, s)
#define COMMON_SYSCALL_POST_READ_RANGE(p, s)                                   \
  do {                                                                         \
    (void)(p);                                                                 \
    (void)(s);                                                                 \
  } while (false)
#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s)                                  \
  do {                                                                         \
    (void)(p);                                                                 \
    (void)(s);                                                                 \
  } while (false)
#include "sanitizer_common/sanitizer_common_syscalls.inc"

struct ThreadStartParam {
  atomic_uintptr_t t;
  atomic_uintptr_t is_registered;
};

static thread_return_t THREAD_CALLING_CONV memprof_thread_start(void *arg) {
  ThreadStartParam *param = reinterpret_cast<ThreadStartParam *>(arg);
  MemprofThread *t = nullptr;
  while ((t = reinterpret_cast<MemprofThread *>(
              atomic_load(&param->t, memory_order_acquire))) == nullptr)
    internal_sched_yield();
  SetCurrentThread(t);
  return t->ThreadStart(GetTid(), &param->is_registered);
}

INTERCEPTOR(int, pthread_create, void *thread, void *attr,
            void *(*start_routine)(void *), void *arg) {
  EnsureMainThreadIDIsCorrect();
  GET_STACK_TRACE_THREAD;
  int detached = 0;
  if (attr)
    REAL(pthread_attr_getdetachstate)(attr, &detached);
  ThreadStartParam param;
  atomic_store(&param.t, 0, memory_order_relaxed);
  atomic_store(&param.is_registered, 0, memory_order_relaxed);
  int result;
  {
    // Ignore all allocations made by pthread_create: thread stack/TLS may be
    // stored by pthread for future reuse even after thread destruction, and
    // the linked list it's stored in doesn't even hold valid pointers to the
    // objects, the latter are calculated by obscure pointer arithmetic.
    result = REAL(pthread_create)(thread, attr, memprof_thread_start, &param);
  }
  if (result == 0) {
    u32 current_tid = GetCurrentTidOrInvalid();
    MemprofThread *t = MemprofThread::Create(start_routine, arg, current_tid,
                                             &stack, detached);
    atomic_store(&param.t, reinterpret_cast<uptr>(t), memory_order_release);
    // Wait until the MemprofThread object is initialized and the
    // ThreadRegistry entry is in "started" state.
    while (atomic_load(&param.is_registered, memory_order_acquire) == 0)
      internal_sched_yield();
  }
  return result;
}

INTERCEPTOR(int, pthread_join, void *t, void **arg) {
  return real_pthread_join(t, arg);
}

DEFINE_REAL_PTHREAD_FUNCTIONS

INTERCEPTOR(char *, index, const char *string, int c)
ALIAS(WRAPPER_NAME(strchr));

// For both strcat() and strncat() we need to check the validity of |to|
// argument irrespective of the |from| length.
INTERCEPTOR(char *, strcat, char *to, const char *from) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strcat);
  ENSURE_MEMPROF_INITED();
  uptr from_length = internal_strlen(from);
  MEMPROF_READ_RANGE(from, from_length + 1);
  uptr to_length = internal_strlen(to);
  MEMPROF_READ_STRING(to, to_length);
  MEMPROF_WRITE_RANGE(to + to_length, from_length + 1);
  return REAL(strcat)(to, from);
}

INTERCEPTOR(char *, strncat, char *to, const char *from, uptr size) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strncat);
  ENSURE_MEMPROF_INITED();
  uptr from_length = MaybeRealStrnlen(from, size);
  uptr copy_length = Min(size, from_length + 1);
  MEMPROF_READ_RANGE(from, copy_length);
  uptr to_length = internal_strlen(to);
  MEMPROF_READ_STRING(to, to_length);
  MEMPROF_WRITE_RANGE(to + to_length, from_length + 1);
  return REAL(strncat)(to, from, size);
}

INTERCEPTOR(char *, strcpy, char *to, const char *from) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strcpy);
  if (memprof_init_is_running) {
    return REAL(strcpy)(to, from);
  }
  ENSURE_MEMPROF_INITED();
  uptr from_size = internal_strlen(from) + 1;
  MEMPROF_READ_RANGE(from, from_size);
  MEMPROF_WRITE_RANGE(to, from_size);
  return REAL(strcpy)(to, from);
}

INTERCEPTOR(char *, strdup, const char *s) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strdup);
  if (UNLIKELY(!memprof_inited))
    return internal_strdup(s);
  ENSURE_MEMPROF_INITED();
  uptr length = internal_strlen(s);
  MEMPROF_READ_RANGE(s, length + 1);
  GET_STACK_TRACE_MALLOC;
  void *new_mem = memprof_malloc(length + 1, &stack);
  REAL(memcpy)(new_mem, s, length + 1);
  return reinterpret_cast<char *>(new_mem);
}

INTERCEPTOR(char *, __strdup, const char *s) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strdup);
  if (UNLIKELY(!memprof_inited))
    return internal_strdup(s);
  ENSURE_MEMPROF_INITED();
  uptr length = internal_strlen(s);
  MEMPROF_READ_RANGE(s, length + 1);
  GET_STACK_TRACE_MALLOC;
  void *new_mem = memprof_malloc(length + 1, &stack);
  REAL(memcpy)(new_mem, s, length + 1);
  return reinterpret_cast<char *>(new_mem);
}

INTERCEPTOR(char *, strncpy, char *to, const char *from, uptr size) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strncpy);
  ENSURE_MEMPROF_INITED();
  uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
  MEMPROF_READ_RANGE(from, from_size);
  MEMPROF_WRITE_RANGE(to, size);
  return REAL(strncpy)(to, from, size);
}

INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strtol);
  ENSURE_MEMPROF_INITED();
  char *real_endptr;
  long result = REAL(strtol)(nptr, &real_endptr, base);
  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  return result;
}

INTERCEPTOR(int, atoi, const char *nptr) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, atoi);
  ENSURE_MEMPROF_INITED();
  char *real_endptr;
  // "man atoi" tells that behavior of atoi(nptr) is the same as
  // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
  // parsed integer can't be stored in *long* type (even if it's
  // different from int). So, we just imitate this behavior.
  int result = REAL(strtol)(nptr, &real_endptr, 10);
  FixRealStrtolEndptr(nptr, &real_endptr);
  MEMPROF_READ_STRING(nptr, (real_endptr - nptr) + 1);
  return result;
}

INTERCEPTOR(long, atol, const char *nptr) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, atol);
  ENSURE_MEMPROF_INITED();
  char *real_endptr;
  long result = REAL(strtol)(nptr, &real_endptr, 10);
  FixRealStrtolEndptr(nptr, &real_endptr);
  MEMPROF_READ_STRING(nptr, (real_endptr - nptr) + 1);
  return result;
}

INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, strtoll);
  ENSURE_MEMPROF_INITED();
  char *real_endptr;
  long long result = REAL(strtoll)(nptr, &real_endptr, base);
  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  return result;
}

INTERCEPTOR(long long, atoll, const char *nptr) {
  void *ctx;
  MEMPROF_INTERCEPTOR_ENTER(ctx, atoll);
  ENSURE_MEMPROF_INITED();
  char *real_endptr;
  long long result = REAL(strtoll)(nptr, &real_endptr, 10);
  FixRealStrtolEndptr(nptr, &real_endptr);
  MEMPROF_READ_STRING(nptr, (real_endptr - nptr) + 1);
  return result;
}

// ---------------------- InitializeMemprofInterceptors ---------------- {{{1
namespace __memprof {
void InitializeMemprofInterceptors() {
  static bool was_called_once;
  CHECK(!was_called_once);
  was_called_once = true;
  InitializeCommonInterceptors();

  // Intercept str* functions.
  MEMPROF_INTERCEPT_FUNC(strcat);
  MEMPROF_INTERCEPT_FUNC(strcpy);
  MEMPROF_INTERCEPT_FUNC(strncat);
  MEMPROF_INTERCEPT_FUNC(strncpy);
  MEMPROF_INTERCEPT_FUNC(strdup);
  MEMPROF_INTERCEPT_FUNC(__strdup);
  MEMPROF_INTERCEPT_FUNC(index);

  MEMPROF_INTERCEPT_FUNC(atoi);
  MEMPROF_INTERCEPT_FUNC(atol);
  MEMPROF_INTERCEPT_FUNC(strtol);
  MEMPROF_INTERCEPT_FUNC(atoll);
  MEMPROF_INTERCEPT_FUNC(strtoll);

  // Intercept threading-related functions
  MEMPROF_INTERCEPT_FUNC(pthread_create);
  MEMPROF_INTERCEPT_FUNC(pthread_join);

  InitializePlatformInterceptors();

  VReport(1, "MemProfiler: libc interceptors initialized\n");
}

} // namespace __memprof