memprof_interceptors.cpp 14 KB

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