asan_interceptors.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. //===-- asan_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 AddressSanitizer, an address sanity checker.
  10. //
  11. // Intercept various libc functions.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_interceptors.h"
  14. #include "asan_allocator.h"
  15. #include "asan_internal.h"
  16. #include "asan_mapping.h"
  17. #include "asan_poisoning.h"
  18. #include "asan_report.h"
  19. #include "asan_stack.h"
  20. #include "asan_stats.h"
  21. #include "asan_suppressions.h"
  22. #include "lsan/lsan_common.h"
  23. #include "sanitizer_common/sanitizer_libc.h"
  24. // There is no general interception at all on Fuchsia.
  25. // Only the functions in asan_interceptors_memintrinsics.cpp are
  26. // really defined to replace libc functions.
  27. #if !SANITIZER_FUCHSIA
  28. # if SANITIZER_POSIX
  29. # include "sanitizer_common/sanitizer_posix.h"
  30. # endif
  31. # if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \
  32. ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
  33. # include <unwind.h>
  34. # endif
  35. # if defined(__i386) && SANITIZER_LINUX
  36. # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1"
  37. # elif defined(__mips__) && SANITIZER_LINUX
  38. # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2"
  39. # endif
  40. namespace __asan {
  41. #define ASAN_READ_STRING_OF_LEN(ctx, s, len, n) \
  42. ASAN_READ_RANGE((ctx), (s), \
  43. common_flags()->strict_string_checks ? (len) + 1 : (n))
  44. # define ASAN_READ_STRING(ctx, s, n) \
  45. ASAN_READ_STRING_OF_LEN((ctx), (s), internal_strlen(s), (n))
  46. static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
  47. #if SANITIZER_INTERCEPT_STRNLEN
  48. if (REAL(strnlen)) {
  49. return REAL(strnlen)(s, maxlen);
  50. }
  51. #endif
  52. return internal_strnlen(s, maxlen);
  53. }
  54. void SetThreadName(const char *name) {
  55. AsanThread *t = GetCurrentThread();
  56. if (t)
  57. asanThreadRegistry().SetThreadName(t->tid(), name);
  58. }
  59. int OnExit() {
  60. if (CAN_SANITIZE_LEAKS && common_flags()->detect_leaks &&
  61. __lsan::HasReportedLeaks()) {
  62. return common_flags()->exitcode;
  63. }
  64. // FIXME: ask frontend whether we need to return failure.
  65. return 0;
  66. }
  67. } // namespace __asan
  68. // ---------------------- Wrappers ---------------- {{{1
  69. using namespace __asan;
  70. DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr)
  71. DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
  72. #define ASAN_INTERCEPTOR_ENTER(ctx, func) \
  73. AsanInterceptorContext _ctx = {#func}; \
  74. ctx = (void *)&_ctx; \
  75. (void) ctx; \
  76. #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)
  77. #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \
  78. ASAN_INTERCEPT_FUNC_VER(name, ver)
  79. #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \
  80. ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver)
  81. #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
  82. ASAN_WRITE_RANGE(ctx, ptr, size)
  83. #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
  84. ASAN_READ_RANGE(ctx, ptr, size)
  85. #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
  86. ASAN_INTERCEPTOR_ENTER(ctx, func); \
  87. do { \
  88. if (asan_init_is_running) \
  89. return REAL(func)(__VA_ARGS__); \
  90. if (SANITIZER_APPLE && UNLIKELY(!asan_inited)) \
  91. return REAL(func)(__VA_ARGS__); \
  92. ENSURE_ASAN_INITED(); \
  93. } while (false)
  94. #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
  95. do { \
  96. } while (false)
  97. #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
  98. do { \
  99. } while (false)
  100. #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \
  101. do { \
  102. } while (false)
  103. #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \
  104. do { \
  105. } while (false)
  106. #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name)
  107. // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name)
  108. // But asan does not remember UserId's for threads (pthread_t);
  109. // and remembers all ever existed threads, so the linear search by UserId
  110. // can be slow.
  111. #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \
  112. do { \
  113. } while (false)
  114. #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name)
  115. // Strict init-order checking is dlopen-hostile:
  116. // https://github.com/google/sanitizers/issues/178
  117. # define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
  118. ({ \
  119. if (flags()->strict_init_order) \
  120. StopInitOrderChecking(); \
  121. CheckNoDeepBind(filename, flag); \
  122. REAL(dlopen)(filename, flag); \
  123. })
  124. # define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
  125. # define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
  126. # define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
  127. # define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
  128. # define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
  129. if (AsanThread *t = GetCurrentThread()) { \
  130. *begin = t->tls_begin(); \
  131. *end = t->tls_end(); \
  132. } else { \
  133. *begin = *end = 0; \
  134. }
  135. #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
  136. do { \
  137. ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
  138. ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
  139. } while (false)
  140. #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
  141. do { \
  142. ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
  143. ASAN_MEMCPY_IMPL(ctx, to, from, size); \
  144. } while (false)
  145. #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
  146. do { \
  147. ASAN_INTERCEPTOR_ENTER(ctx, memset); \
  148. ASAN_MEMSET_IMPL(ctx, block, c, size); \
  149. } while (false)
  150. #if CAN_SANITIZE_LEAKS
  151. #define COMMON_INTERCEPTOR_STRERROR() \
  152. __lsan::ScopedInterceptorDisabler disabler
  153. #endif
  154. #include "sanitizer_common/sanitizer_common_interceptors.inc"
  155. #include "sanitizer_common/sanitizer_signal_interceptors.inc"
  156. // Syscall interceptors don't have contexts, we don't support suppressions
  157. // for them.
  158. #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
  159. #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
  160. #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
  161. do { \
  162. (void)(p); \
  163. (void)(s); \
  164. } while (false)
  165. #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
  166. do { \
  167. (void)(p); \
  168. (void)(s); \
  169. } while (false)
  170. #include "sanitizer_common/sanitizer_common_syscalls.inc"
  171. #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
  172. #if ASAN_INTERCEPT_PTHREAD_CREATE
  173. static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
  174. AsanThread *t = (AsanThread *)arg;
  175. SetCurrentThread(t);
  176. return t->ThreadStart(GetTid());
  177. }
  178. INTERCEPTOR(int, pthread_create, void *thread,
  179. void *attr, void *(*start_routine)(void*), void *arg) {
  180. EnsureMainThreadIDIsCorrect();
  181. // Strict init-order checking is thread-hostile.
  182. if (flags()->strict_init_order)
  183. StopInitOrderChecking();
  184. GET_STACK_TRACE_THREAD;
  185. int detached = 0;
  186. if (attr)
  187. REAL(pthread_attr_getdetachstate)(attr, &detached);
  188. u32 current_tid = GetCurrentTidOrInvalid();
  189. AsanThread *t =
  190. AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
  191. int result;
  192. {
  193. // Ignore all allocations made by pthread_create: thread stack/TLS may be
  194. // stored by pthread for future reuse even after thread destruction, and
  195. // the linked list it's stored in doesn't even hold valid pointers to the
  196. // objects, the latter are calculated by obscure pointer arithmetic.
  197. #if CAN_SANITIZE_LEAKS
  198. __lsan::ScopedInterceptorDisabler disabler;
  199. #endif
  200. result = REAL(pthread_create)(thread, attr, asan_thread_start, t);
  201. }
  202. if (result != 0) {
  203. // If the thread didn't start delete the AsanThread to avoid leaking it.
  204. // Note AsanThreadContexts never get destroyed so the AsanThreadContext
  205. // that was just created for the AsanThread is wasted.
  206. t->Destroy();
  207. }
  208. return result;
  209. }
  210. INTERCEPTOR(int, pthread_join, void *t, void **arg) {
  211. return real_pthread_join(t, arg);
  212. }
  213. DEFINE_REAL_PTHREAD_FUNCTIONS
  214. #endif // ASAN_INTERCEPT_PTHREAD_CREATE
  215. #if ASAN_INTERCEPT_SWAPCONTEXT
  216. static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
  217. // Only clear if we know the stack. This should be true only for contexts
  218. // created with makecontext().
  219. if (!ssize)
  220. return;
  221. // Align to page size.
  222. uptr PageSize = GetPageSizeCached();
  223. uptr bottom = RoundDownTo(stack, PageSize);
  224. if (!AddrIsInMem(bottom))
  225. return;
  226. ssize += stack - bottom;
  227. ssize = RoundUpTo(ssize, PageSize);
  228. PoisonShadow(bottom, ssize, 0);
  229. }
  230. INTERCEPTOR(void, makecontext, struct ucontext_t *ucp, void (*func)(), int argc,
  231. ...) {
  232. va_list ap;
  233. uptr args[64];
  234. // We don't know a better way to forward ... into REAL function. We can
  235. // increase args size if neccecary.
  236. CHECK_LE(argc, ARRAY_SIZE(args));
  237. internal_memset(args, 0, sizeof(args));
  238. va_start(ap, argc);
  239. for (int i = 0; i < argc; ++i) args[i] = va_arg(ap, uptr);
  240. va_end(ap);
  241. # define ENUMERATE_ARRAY_4(start) \
  242. args[start], args[start + 1], args[start + 2], args[start + 3]
  243. # define ENUMERATE_ARRAY_16(start) \
  244. ENUMERATE_ARRAY_4(start), ENUMERATE_ARRAY_4(start + 4), \
  245. ENUMERATE_ARRAY_4(start + 8), ENUMERATE_ARRAY_4(start + 12)
  246. # define ENUMERATE_ARRAY_64() \
  247. ENUMERATE_ARRAY_16(0), ENUMERATE_ARRAY_16(16), ENUMERATE_ARRAY_16(32), \
  248. ENUMERATE_ARRAY_16(48)
  249. REAL(makecontext)
  250. ((struct ucontext_t *)ucp, func, argc, ENUMERATE_ARRAY_64());
  251. # undef ENUMERATE_ARRAY_4
  252. # undef ENUMERATE_ARRAY_16
  253. # undef ENUMERATE_ARRAY_64
  254. // Sign the stack so we can identify it for unpoisoning.
  255. SignContextStack(ucp);
  256. }
  257. INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
  258. struct ucontext_t *ucp) {
  259. static bool reported_warning = false;
  260. if (!reported_warning) {
  261. Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
  262. "functions and may produce false positives in some cases!\n");
  263. reported_warning = true;
  264. }
  265. // Clear shadow memory for new context (it may share stack
  266. // with current context).
  267. uptr stack, ssize;
  268. ReadContextStack(ucp, &stack, &ssize);
  269. ClearShadowMemoryForContextStack(stack, ssize);
  270. # if __has_attribute(__indirect_return__) && \
  271. (defined(__x86_64__) || defined(__i386__))
  272. int (*real_swapcontext)(struct ucontext_t *, struct ucontext_t *)
  273. __attribute__((__indirect_return__)) = REAL(swapcontext);
  274. int res = real_swapcontext(oucp, ucp);
  275. # else
  276. int res = REAL(swapcontext)(oucp, ucp);
  277. # endif
  278. // swapcontext technically does not return, but program may swap context to
  279. // "oucp" later, that would look as if swapcontext() returned 0.
  280. // We need to clear shadow for ucp once again, as it may be in arbitrary
  281. // state.
  282. ClearShadowMemoryForContextStack(stack, ssize);
  283. return res;
  284. }
  285. #endif // ASAN_INTERCEPT_SWAPCONTEXT
  286. #if SANITIZER_NETBSD
  287. #define longjmp __longjmp14
  288. #define siglongjmp __siglongjmp14
  289. #endif
  290. INTERCEPTOR(void, longjmp, void *env, int val) {
  291. __asan_handle_no_return();
  292. REAL(longjmp)(env, val);
  293. }
  294. #if ASAN_INTERCEPT__LONGJMP
  295. INTERCEPTOR(void, _longjmp, void *env, int val) {
  296. __asan_handle_no_return();
  297. REAL(_longjmp)(env, val);
  298. }
  299. #endif
  300. #if ASAN_INTERCEPT___LONGJMP_CHK
  301. INTERCEPTOR(void, __longjmp_chk, void *env, int val) {
  302. __asan_handle_no_return();
  303. REAL(__longjmp_chk)(env, val);
  304. }
  305. #endif
  306. #if ASAN_INTERCEPT_SIGLONGJMP
  307. INTERCEPTOR(void, siglongjmp, void *env, int val) {
  308. __asan_handle_no_return();
  309. REAL(siglongjmp)(env, val);
  310. }
  311. #endif
  312. #if ASAN_INTERCEPT___CXA_THROW
  313. INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
  314. CHECK(REAL(__cxa_throw));
  315. __asan_handle_no_return();
  316. REAL(__cxa_throw)(a, b, c);
  317. }
  318. #endif
  319. #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
  320. INTERCEPTOR(void, __cxa_rethrow_primary_exception, void *a) {
  321. CHECK(REAL(__cxa_rethrow_primary_exception));
  322. __asan_handle_no_return();
  323. REAL(__cxa_rethrow_primary_exception)(a);
  324. }
  325. #endif
  326. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
  327. INTERCEPTOR(_Unwind_Reason_Code, _Unwind_RaiseException,
  328. _Unwind_Exception *object) {
  329. CHECK(REAL(_Unwind_RaiseException));
  330. __asan_handle_no_return();
  331. return REAL(_Unwind_RaiseException)(object);
  332. }
  333. #endif
  334. #if ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
  335. INTERCEPTOR(_Unwind_Reason_Code, _Unwind_SjLj_RaiseException,
  336. _Unwind_Exception *object) {
  337. CHECK(REAL(_Unwind_SjLj_RaiseException));
  338. __asan_handle_no_return();
  339. return REAL(_Unwind_SjLj_RaiseException)(object);
  340. }
  341. #endif
  342. #if ASAN_INTERCEPT_INDEX
  343. # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
  344. INTERCEPTOR(char*, index, const char *string, int c)
  345. ALIAS(WRAPPER_NAME(strchr));
  346. # else
  347. # if SANITIZER_APPLE
  348. DECLARE_REAL(char*, index, const char *string, int c)
  349. OVERRIDE_FUNCTION(index, strchr);
  350. # else
  351. DEFINE_REAL(char*, index, const char *string, int c)
  352. # endif
  353. # endif
  354. #endif // ASAN_INTERCEPT_INDEX
  355. // For both strcat() and strncat() we need to check the validity of |to|
  356. // argument irrespective of the |from| length.
  357. INTERCEPTOR(char *, strcat, char *to, const char *from) {
  358. void *ctx;
  359. ASAN_INTERCEPTOR_ENTER(ctx, strcat);
  360. ENSURE_ASAN_INITED();
  361. if (flags()->replace_str) {
  362. uptr from_length = internal_strlen(from);
  363. ASAN_READ_RANGE(ctx, from, from_length + 1);
  364. uptr to_length = internal_strlen(to);
  365. ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
  366. ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
  367. // If the copying actually happens, the |from| string should not overlap
  368. // with the resulting string starting at |to|, which has a length of
  369. // to_length + from_length + 1.
  370. if (from_length > 0) {
  371. CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, from,
  372. from_length + 1);
  373. }
  374. }
  375. return REAL(strcat)(to, from);
  376. }
  377. INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
  378. void *ctx;
  379. ASAN_INTERCEPTOR_ENTER(ctx, strncat);
  380. ENSURE_ASAN_INITED();
  381. if (flags()->replace_str) {
  382. uptr from_length = MaybeRealStrnlen(from, size);
  383. uptr copy_length = Min(size, from_length + 1);
  384. ASAN_READ_RANGE(ctx, from, copy_length);
  385. uptr to_length = internal_strlen(to);
  386. ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
  387. ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
  388. if (from_length > 0) {
  389. CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
  390. from, copy_length);
  391. }
  392. }
  393. return REAL(strncat)(to, from, size);
  394. }
  395. INTERCEPTOR(char *, strcpy, char *to, const char *from) {
  396. void *ctx;
  397. ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
  398. #if SANITIZER_APPLE
  399. if (UNLIKELY(!asan_inited))
  400. return REAL(strcpy)(to, from);
  401. #endif
  402. // strcpy is called from malloc_default_purgeable_zone()
  403. // in __asan::ReplaceSystemAlloc() on Mac.
  404. if (asan_init_is_running) {
  405. return REAL(strcpy)(to, from);
  406. }
  407. ENSURE_ASAN_INITED();
  408. if (flags()->replace_str) {
  409. uptr from_size = internal_strlen(from) + 1;
  410. CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
  411. ASAN_READ_RANGE(ctx, from, from_size);
  412. ASAN_WRITE_RANGE(ctx, to, from_size);
  413. }
  414. return REAL(strcpy)(to, from);
  415. }
  416. INTERCEPTOR(char*, strdup, const char *s) {
  417. void *ctx;
  418. ASAN_INTERCEPTOR_ENTER(ctx, strdup);
  419. if (UNLIKELY(!asan_inited)) return internal_strdup(s);
  420. ENSURE_ASAN_INITED();
  421. uptr length = internal_strlen(s);
  422. if (flags()->replace_str) {
  423. ASAN_READ_RANGE(ctx, s, length + 1);
  424. }
  425. GET_STACK_TRACE_MALLOC;
  426. void *new_mem = asan_malloc(length + 1, &stack);
  427. REAL(memcpy)(new_mem, s, length + 1);
  428. return reinterpret_cast<char*>(new_mem);
  429. }
  430. #if ASAN_INTERCEPT___STRDUP
  431. INTERCEPTOR(char*, __strdup, const char *s) {
  432. void *ctx;
  433. ASAN_INTERCEPTOR_ENTER(ctx, strdup);
  434. if (UNLIKELY(!asan_inited)) return internal_strdup(s);
  435. ENSURE_ASAN_INITED();
  436. uptr length = internal_strlen(s);
  437. if (flags()->replace_str) {
  438. ASAN_READ_RANGE(ctx, s, length + 1);
  439. }
  440. GET_STACK_TRACE_MALLOC;
  441. void *new_mem = asan_malloc(length + 1, &stack);
  442. REAL(memcpy)(new_mem, s, length + 1);
  443. return reinterpret_cast<char*>(new_mem);
  444. }
  445. #endif // ASAN_INTERCEPT___STRDUP
  446. INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
  447. void *ctx;
  448. ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
  449. ENSURE_ASAN_INITED();
  450. if (flags()->replace_str) {
  451. uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
  452. CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
  453. ASAN_READ_RANGE(ctx, from, from_size);
  454. ASAN_WRITE_RANGE(ctx, to, size);
  455. }
  456. return REAL(strncpy)(to, from, size);
  457. }
  458. INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) {
  459. void *ctx;
  460. ASAN_INTERCEPTOR_ENTER(ctx, strtol);
  461. ENSURE_ASAN_INITED();
  462. if (!flags()->replace_str) {
  463. return REAL(strtol)(nptr, endptr, base);
  464. }
  465. char *real_endptr;
  466. long result = REAL(strtol)(nptr, &real_endptr, base);
  467. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  468. return result;
  469. }
  470. INTERCEPTOR(int, atoi, const char *nptr) {
  471. void *ctx;
  472. ASAN_INTERCEPTOR_ENTER(ctx, atoi);
  473. #if SANITIZER_APPLE
  474. if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
  475. #endif
  476. ENSURE_ASAN_INITED();
  477. if (!flags()->replace_str) {
  478. return REAL(atoi)(nptr);
  479. }
  480. char *real_endptr;
  481. // "man atoi" tells that behavior of atoi(nptr) is the same as
  482. // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
  483. // parsed integer can't be stored in *long* type (even if it's
  484. // different from int). So, we just imitate this behavior.
  485. int result = REAL(strtol)(nptr, &real_endptr, 10);
  486. FixRealStrtolEndptr(nptr, &real_endptr);
  487. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  488. return result;
  489. }
  490. INTERCEPTOR(long, atol, const char *nptr) {
  491. void *ctx;
  492. ASAN_INTERCEPTOR_ENTER(ctx, atol);
  493. #if SANITIZER_APPLE
  494. if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
  495. #endif
  496. ENSURE_ASAN_INITED();
  497. if (!flags()->replace_str) {
  498. return REAL(atol)(nptr);
  499. }
  500. char *real_endptr;
  501. long result = REAL(strtol)(nptr, &real_endptr, 10);
  502. FixRealStrtolEndptr(nptr, &real_endptr);
  503. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  504. return result;
  505. }
  506. #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  507. INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
  508. void *ctx;
  509. ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
  510. ENSURE_ASAN_INITED();
  511. if (!flags()->replace_str) {
  512. return REAL(strtoll)(nptr, endptr, base);
  513. }
  514. char *real_endptr;
  515. long long result = REAL(strtoll)(nptr, &real_endptr, base);
  516. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  517. return result;
  518. }
  519. INTERCEPTOR(long long, atoll, const char *nptr) {
  520. void *ctx;
  521. ASAN_INTERCEPTOR_ENTER(ctx, atoll);
  522. ENSURE_ASAN_INITED();
  523. if (!flags()->replace_str) {
  524. return REAL(atoll)(nptr);
  525. }
  526. char *real_endptr;
  527. long long result = REAL(strtoll)(nptr, &real_endptr, 10);
  528. FixRealStrtolEndptr(nptr, &real_endptr);
  529. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  530. return result;
  531. }
  532. #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  533. #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT
  534. static void AtCxaAtexit(void *unused) {
  535. (void)unused;
  536. StopInitOrderChecking();
  537. }
  538. #endif
  539. #if ASAN_INTERCEPT___CXA_ATEXIT
  540. INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
  541. void *dso_handle) {
  542. #if SANITIZER_APPLE
  543. if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
  544. #endif
  545. ENSURE_ASAN_INITED();
  546. #if CAN_SANITIZE_LEAKS
  547. __lsan::ScopedInterceptorDisabler disabler;
  548. #endif
  549. int res = REAL(__cxa_atexit)(func, arg, dso_handle);
  550. REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
  551. return res;
  552. }
  553. #endif // ASAN_INTERCEPT___CXA_ATEXIT
  554. #if ASAN_INTERCEPT_ATEXIT
  555. INTERCEPTOR(int, atexit, void (*func)()) {
  556. ENSURE_ASAN_INITED();
  557. #if CAN_SANITIZE_LEAKS
  558. __lsan::ScopedInterceptorDisabler disabler;
  559. #endif
  560. // Avoid calling real atexit as it is unreachable on at least on Linux.
  561. int res = REAL(__cxa_atexit)((void (*)(void *a))func, nullptr, nullptr);
  562. REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
  563. return res;
  564. }
  565. #endif
  566. #if ASAN_INTERCEPT_PTHREAD_ATFORK
  567. extern "C" {
  568. extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
  569. void (*child)());
  570. };
  571. INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
  572. void (*child)()) {
  573. #if CAN_SANITIZE_LEAKS
  574. __lsan::ScopedInterceptorDisabler disabler;
  575. #endif
  576. // REAL(pthread_atfork) cannot be called due to symbol indirections at least
  577. // on NetBSD
  578. return _pthread_atfork(prepare, parent, child);
  579. }
  580. #endif
  581. #if ASAN_INTERCEPT_VFORK
  582. DEFINE_REAL(int, vfork)
  583. DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork)
  584. #endif
  585. // ---------------------- InitializeAsanInterceptors ---------------- {{{1
  586. namespace __asan {
  587. void InitializeAsanInterceptors() {
  588. static bool was_called_once;
  589. CHECK(!was_called_once);
  590. was_called_once = true;
  591. InitializeCommonInterceptors();
  592. InitializeSignalInterceptors();
  593. // Intercept str* functions.
  594. ASAN_INTERCEPT_FUNC(strcat);
  595. ASAN_INTERCEPT_FUNC(strcpy);
  596. ASAN_INTERCEPT_FUNC(strncat);
  597. ASAN_INTERCEPT_FUNC(strncpy);
  598. ASAN_INTERCEPT_FUNC(strdup);
  599. #if ASAN_INTERCEPT___STRDUP
  600. ASAN_INTERCEPT_FUNC(__strdup);
  601. #endif
  602. #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
  603. ASAN_INTERCEPT_FUNC(index);
  604. #endif
  605. ASAN_INTERCEPT_FUNC(atoi);
  606. ASAN_INTERCEPT_FUNC(atol);
  607. ASAN_INTERCEPT_FUNC(strtol);
  608. #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  609. ASAN_INTERCEPT_FUNC(atoll);
  610. ASAN_INTERCEPT_FUNC(strtoll);
  611. #endif
  612. // Intecept jump-related functions.
  613. ASAN_INTERCEPT_FUNC(longjmp);
  614. # if ASAN_INTERCEPT_SWAPCONTEXT
  615. ASAN_INTERCEPT_FUNC(swapcontext);
  616. ASAN_INTERCEPT_FUNC(makecontext);
  617. # endif
  618. # if ASAN_INTERCEPT__LONGJMP
  619. ASAN_INTERCEPT_FUNC(_longjmp);
  620. #endif
  621. #if ASAN_INTERCEPT___LONGJMP_CHK
  622. ASAN_INTERCEPT_FUNC(__longjmp_chk);
  623. #endif
  624. #if ASAN_INTERCEPT_SIGLONGJMP
  625. ASAN_INTERCEPT_FUNC(siglongjmp);
  626. #endif
  627. // Intercept exception handling functions.
  628. #if ASAN_INTERCEPT___CXA_THROW
  629. ASAN_INTERCEPT_FUNC(__cxa_throw);
  630. #endif
  631. #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
  632. ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception);
  633. #endif
  634. // Indirectly intercept std::rethrow_exception.
  635. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
  636. INTERCEPT_FUNCTION(_Unwind_RaiseException);
  637. #endif
  638. // Indirectly intercept std::rethrow_exception.
  639. #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION
  640. INTERCEPT_FUNCTION(_Unwind_SjLj_RaiseException);
  641. #endif
  642. // Intercept threading-related functions
  643. #if ASAN_INTERCEPT_PTHREAD_CREATE
  644. // TODO: this should probably have an unversioned fallback for newer arches?
  645. #if defined(ASAN_PTHREAD_CREATE_VERSION)
  646. ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
  647. #else
  648. ASAN_INTERCEPT_FUNC(pthread_create);
  649. #endif
  650. ASAN_INTERCEPT_FUNC(pthread_join);
  651. #endif
  652. // Intercept atexit function.
  653. #if ASAN_INTERCEPT___CXA_ATEXIT
  654. ASAN_INTERCEPT_FUNC(__cxa_atexit);
  655. #endif
  656. #if ASAN_INTERCEPT_ATEXIT
  657. ASAN_INTERCEPT_FUNC(atexit);
  658. #endif
  659. #if ASAN_INTERCEPT_PTHREAD_ATFORK
  660. ASAN_INTERCEPT_FUNC(pthread_atfork);
  661. #endif
  662. #if ASAN_INTERCEPT_VFORK
  663. ASAN_INTERCEPT_FUNC(vfork);
  664. #endif
  665. InitializePlatformInterceptors();
  666. VReport(1, "AddressSanitizer: libc interceptors initialized\n");
  667. }
  668. } // namespace __asan
  669. #endif // !SANITIZER_FUCHSIA