asan_interceptors.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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_MAC && 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. // Align to page size.
  218. uptr PageSize = GetPageSizeCached();
  219. uptr bottom = stack & ~(PageSize - 1);
  220. ssize += stack - bottom;
  221. ssize = RoundUpTo(ssize, PageSize);
  222. static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
  223. if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) {
  224. PoisonShadow(bottom, ssize, 0);
  225. }
  226. }
  227. INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
  228. struct ucontext_t *ucp) {
  229. static bool reported_warning = false;
  230. if (!reported_warning) {
  231. Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
  232. "functions and may produce false positives in some cases!\n");
  233. reported_warning = true;
  234. }
  235. // Clear shadow memory for new context (it may share stack
  236. // with current context).
  237. uptr stack, ssize;
  238. ReadContextStack(ucp, &stack, &ssize);
  239. ClearShadowMemoryForContextStack(stack, ssize);
  240. #if __has_attribute(__indirect_return__) && \
  241. (defined(__x86_64__) || defined(__i386__))
  242. int (*real_swapcontext)(struct ucontext_t *, struct ucontext_t *)
  243. __attribute__((__indirect_return__))
  244. = REAL(swapcontext);
  245. int res = real_swapcontext(oucp, ucp);
  246. #else
  247. int res = REAL(swapcontext)(oucp, ucp);
  248. #endif
  249. // swapcontext technically does not return, but program may swap context to
  250. // "oucp" later, that would look as if swapcontext() returned 0.
  251. // We need to clear shadow for ucp once again, as it may be in arbitrary
  252. // state.
  253. ClearShadowMemoryForContextStack(stack, ssize);
  254. return res;
  255. }
  256. #endif // ASAN_INTERCEPT_SWAPCONTEXT
  257. #if SANITIZER_NETBSD
  258. #define longjmp __longjmp14
  259. #define siglongjmp __siglongjmp14
  260. #endif
  261. INTERCEPTOR(void, longjmp, void *env, int val) {
  262. __asan_handle_no_return();
  263. REAL(longjmp)(env, val);
  264. }
  265. #if ASAN_INTERCEPT__LONGJMP
  266. INTERCEPTOR(void, _longjmp, void *env, int val) {
  267. __asan_handle_no_return();
  268. REAL(_longjmp)(env, val);
  269. }
  270. #endif
  271. #if ASAN_INTERCEPT___LONGJMP_CHK
  272. INTERCEPTOR(void, __longjmp_chk, void *env, int val) {
  273. __asan_handle_no_return();
  274. REAL(__longjmp_chk)(env, val);
  275. }
  276. #endif
  277. #if ASAN_INTERCEPT_SIGLONGJMP
  278. INTERCEPTOR(void, siglongjmp, void *env, int val) {
  279. __asan_handle_no_return();
  280. REAL(siglongjmp)(env, val);
  281. }
  282. #endif
  283. #if ASAN_INTERCEPT___CXA_THROW
  284. INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
  285. CHECK(REAL(__cxa_throw));
  286. __asan_handle_no_return();
  287. REAL(__cxa_throw)(a, b, c);
  288. }
  289. #endif
  290. #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
  291. INTERCEPTOR(void, __cxa_rethrow_primary_exception, void *a) {
  292. CHECK(REAL(__cxa_rethrow_primary_exception));
  293. __asan_handle_no_return();
  294. REAL(__cxa_rethrow_primary_exception)(a);
  295. }
  296. #endif
  297. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
  298. INTERCEPTOR(_Unwind_Reason_Code, _Unwind_RaiseException,
  299. _Unwind_Exception *object) {
  300. CHECK(REAL(_Unwind_RaiseException));
  301. __asan_handle_no_return();
  302. return REAL(_Unwind_RaiseException)(object);
  303. }
  304. #endif
  305. #if ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
  306. INTERCEPTOR(_Unwind_Reason_Code, _Unwind_SjLj_RaiseException,
  307. _Unwind_Exception *object) {
  308. CHECK(REAL(_Unwind_SjLj_RaiseException));
  309. __asan_handle_no_return();
  310. return REAL(_Unwind_SjLj_RaiseException)(object);
  311. }
  312. #endif
  313. #if ASAN_INTERCEPT_INDEX
  314. # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
  315. INTERCEPTOR(char*, index, const char *string, int c)
  316. ALIAS(WRAPPER_NAME(strchr));
  317. # else
  318. # if SANITIZER_MAC
  319. DECLARE_REAL(char*, index, const char *string, int c)
  320. OVERRIDE_FUNCTION(index, strchr);
  321. # else
  322. DEFINE_REAL(char*, index, const char *string, int c)
  323. # endif
  324. # endif
  325. #endif // ASAN_INTERCEPT_INDEX
  326. // For both strcat() and strncat() we need to check the validity of |to|
  327. // argument irrespective of the |from| length.
  328. INTERCEPTOR(char *, strcat, char *to, const char *from) {
  329. void *ctx;
  330. ASAN_INTERCEPTOR_ENTER(ctx, strcat);
  331. ENSURE_ASAN_INITED();
  332. if (flags()->replace_str) {
  333. uptr from_length = internal_strlen(from);
  334. ASAN_READ_RANGE(ctx, from, from_length + 1);
  335. uptr to_length = internal_strlen(to);
  336. ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
  337. ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
  338. // If the copying actually happens, the |from| string should not overlap
  339. // with the resulting string starting at |to|, which has a length of
  340. // to_length + from_length + 1.
  341. if (from_length > 0) {
  342. CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, from,
  343. from_length + 1);
  344. }
  345. }
  346. return REAL(strcat)(to, from);
  347. }
  348. INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
  349. void *ctx;
  350. ASAN_INTERCEPTOR_ENTER(ctx, strncat);
  351. ENSURE_ASAN_INITED();
  352. if (flags()->replace_str) {
  353. uptr from_length = MaybeRealStrnlen(from, size);
  354. uptr copy_length = Min(size, from_length + 1);
  355. ASAN_READ_RANGE(ctx, from, copy_length);
  356. uptr to_length = internal_strlen(to);
  357. ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
  358. ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
  359. if (from_length > 0) {
  360. CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
  361. from, copy_length);
  362. }
  363. }
  364. return REAL(strncat)(to, from, size);
  365. }
  366. INTERCEPTOR(char *, strcpy, char *to, const char *from) {
  367. void *ctx;
  368. ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
  369. #if SANITIZER_MAC
  370. if (UNLIKELY(!asan_inited))
  371. return REAL(strcpy)(to, from);
  372. #endif
  373. // strcpy is called from malloc_default_purgeable_zone()
  374. // in __asan::ReplaceSystemAlloc() on Mac.
  375. if (asan_init_is_running) {
  376. return REAL(strcpy)(to, from);
  377. }
  378. ENSURE_ASAN_INITED();
  379. if (flags()->replace_str) {
  380. uptr from_size = internal_strlen(from) + 1;
  381. CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
  382. ASAN_READ_RANGE(ctx, from, from_size);
  383. ASAN_WRITE_RANGE(ctx, to, from_size);
  384. }
  385. return REAL(strcpy)(to, from);
  386. }
  387. INTERCEPTOR(char*, strdup, const char *s) {
  388. void *ctx;
  389. ASAN_INTERCEPTOR_ENTER(ctx, strdup);
  390. if (UNLIKELY(!asan_inited)) return internal_strdup(s);
  391. ENSURE_ASAN_INITED();
  392. uptr length = internal_strlen(s);
  393. if (flags()->replace_str) {
  394. ASAN_READ_RANGE(ctx, s, length + 1);
  395. }
  396. GET_STACK_TRACE_MALLOC;
  397. void *new_mem = asan_malloc(length + 1, &stack);
  398. REAL(memcpy)(new_mem, s, length + 1);
  399. return reinterpret_cast<char*>(new_mem);
  400. }
  401. #if ASAN_INTERCEPT___STRDUP
  402. INTERCEPTOR(char*, __strdup, const char *s) {
  403. void *ctx;
  404. ASAN_INTERCEPTOR_ENTER(ctx, strdup);
  405. if (UNLIKELY(!asan_inited)) return internal_strdup(s);
  406. ENSURE_ASAN_INITED();
  407. uptr length = internal_strlen(s);
  408. if (flags()->replace_str) {
  409. ASAN_READ_RANGE(ctx, s, length + 1);
  410. }
  411. GET_STACK_TRACE_MALLOC;
  412. void *new_mem = asan_malloc(length + 1, &stack);
  413. REAL(memcpy)(new_mem, s, length + 1);
  414. return reinterpret_cast<char*>(new_mem);
  415. }
  416. #endif // ASAN_INTERCEPT___STRDUP
  417. INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
  418. void *ctx;
  419. ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
  420. ENSURE_ASAN_INITED();
  421. if (flags()->replace_str) {
  422. uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
  423. CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
  424. ASAN_READ_RANGE(ctx, from, from_size);
  425. ASAN_WRITE_RANGE(ctx, to, size);
  426. }
  427. return REAL(strncpy)(to, from, size);
  428. }
  429. INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) {
  430. void *ctx;
  431. ASAN_INTERCEPTOR_ENTER(ctx, strtol);
  432. ENSURE_ASAN_INITED();
  433. if (!flags()->replace_str) {
  434. return REAL(strtol)(nptr, endptr, base);
  435. }
  436. char *real_endptr;
  437. long result = REAL(strtol)(nptr, &real_endptr, base);
  438. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  439. return result;
  440. }
  441. INTERCEPTOR(int, atoi, const char *nptr) {
  442. void *ctx;
  443. ASAN_INTERCEPTOR_ENTER(ctx, atoi);
  444. #if SANITIZER_MAC
  445. if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
  446. #endif
  447. ENSURE_ASAN_INITED();
  448. if (!flags()->replace_str) {
  449. return REAL(atoi)(nptr);
  450. }
  451. char *real_endptr;
  452. // "man atoi" tells that behavior of atoi(nptr) is the same as
  453. // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
  454. // parsed integer can't be stored in *long* type (even if it's
  455. // different from int). So, we just imitate this behavior.
  456. int result = REAL(strtol)(nptr, &real_endptr, 10);
  457. FixRealStrtolEndptr(nptr, &real_endptr);
  458. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  459. return result;
  460. }
  461. INTERCEPTOR(long, atol, const char *nptr) {
  462. void *ctx;
  463. ASAN_INTERCEPTOR_ENTER(ctx, atol);
  464. #if SANITIZER_MAC
  465. if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
  466. #endif
  467. ENSURE_ASAN_INITED();
  468. if (!flags()->replace_str) {
  469. return REAL(atol)(nptr);
  470. }
  471. char *real_endptr;
  472. long result = REAL(strtol)(nptr, &real_endptr, 10);
  473. FixRealStrtolEndptr(nptr, &real_endptr);
  474. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  475. return result;
  476. }
  477. #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  478. INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
  479. void *ctx;
  480. ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
  481. ENSURE_ASAN_INITED();
  482. if (!flags()->replace_str) {
  483. return REAL(strtoll)(nptr, endptr, base);
  484. }
  485. char *real_endptr;
  486. long long result = REAL(strtoll)(nptr, &real_endptr, base);
  487. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  488. return result;
  489. }
  490. INTERCEPTOR(long long, atoll, const char *nptr) {
  491. void *ctx;
  492. ASAN_INTERCEPTOR_ENTER(ctx, atoll);
  493. ENSURE_ASAN_INITED();
  494. if (!flags()->replace_str) {
  495. return REAL(atoll)(nptr);
  496. }
  497. char *real_endptr;
  498. long long result = REAL(strtoll)(nptr, &real_endptr, 10);
  499. FixRealStrtolEndptr(nptr, &real_endptr);
  500. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  501. return result;
  502. }
  503. #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  504. #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT
  505. static void AtCxaAtexit(void *unused) {
  506. (void)unused;
  507. StopInitOrderChecking();
  508. }
  509. #endif
  510. #if ASAN_INTERCEPT___CXA_ATEXIT
  511. INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
  512. void *dso_handle) {
  513. #if SANITIZER_MAC
  514. if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
  515. #endif
  516. ENSURE_ASAN_INITED();
  517. #if CAN_SANITIZE_LEAKS
  518. __lsan::ScopedInterceptorDisabler disabler;
  519. #endif
  520. int res = REAL(__cxa_atexit)(func, arg, dso_handle);
  521. REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
  522. return res;
  523. }
  524. #endif // ASAN_INTERCEPT___CXA_ATEXIT
  525. #if ASAN_INTERCEPT_ATEXIT
  526. INTERCEPTOR(int, atexit, void (*func)()) {
  527. ENSURE_ASAN_INITED();
  528. #if CAN_SANITIZE_LEAKS
  529. __lsan::ScopedInterceptorDisabler disabler;
  530. #endif
  531. // Avoid calling real atexit as it is unreachable on at least on Linux.
  532. int res = REAL(__cxa_atexit)((void (*)(void *a))func, nullptr, nullptr);
  533. REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
  534. return res;
  535. }
  536. #endif
  537. #if ASAN_INTERCEPT_PTHREAD_ATFORK
  538. extern "C" {
  539. extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
  540. void (*child)());
  541. };
  542. INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
  543. void (*child)()) {
  544. #if CAN_SANITIZE_LEAKS
  545. __lsan::ScopedInterceptorDisabler disabler;
  546. #endif
  547. // REAL(pthread_atfork) cannot be called due to symbol indirections at least
  548. // on NetBSD
  549. return _pthread_atfork(prepare, parent, child);
  550. }
  551. #endif
  552. #if ASAN_INTERCEPT_VFORK
  553. DEFINE_REAL(int, vfork)
  554. DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork)
  555. #endif
  556. // ---------------------- InitializeAsanInterceptors ---------------- {{{1
  557. namespace __asan {
  558. void InitializeAsanInterceptors() {
  559. static bool was_called_once;
  560. CHECK(!was_called_once);
  561. was_called_once = true;
  562. InitializeCommonInterceptors();
  563. InitializeSignalInterceptors();
  564. // Intercept str* functions.
  565. ASAN_INTERCEPT_FUNC(strcat);
  566. ASAN_INTERCEPT_FUNC(strcpy);
  567. ASAN_INTERCEPT_FUNC(strncat);
  568. ASAN_INTERCEPT_FUNC(strncpy);
  569. ASAN_INTERCEPT_FUNC(strdup);
  570. #if ASAN_INTERCEPT___STRDUP
  571. ASAN_INTERCEPT_FUNC(__strdup);
  572. #endif
  573. #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
  574. ASAN_INTERCEPT_FUNC(index);
  575. #endif
  576. ASAN_INTERCEPT_FUNC(atoi);
  577. ASAN_INTERCEPT_FUNC(atol);
  578. ASAN_INTERCEPT_FUNC(strtol);
  579. #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  580. ASAN_INTERCEPT_FUNC(atoll);
  581. ASAN_INTERCEPT_FUNC(strtoll);
  582. #endif
  583. // Intecept jump-related functions.
  584. ASAN_INTERCEPT_FUNC(longjmp);
  585. #if ASAN_INTERCEPT_SWAPCONTEXT
  586. ASAN_INTERCEPT_FUNC(swapcontext);
  587. #endif
  588. #if ASAN_INTERCEPT__LONGJMP
  589. ASAN_INTERCEPT_FUNC(_longjmp);
  590. #endif
  591. #if ASAN_INTERCEPT___LONGJMP_CHK
  592. ASAN_INTERCEPT_FUNC(__longjmp_chk);
  593. #endif
  594. #if ASAN_INTERCEPT_SIGLONGJMP
  595. ASAN_INTERCEPT_FUNC(siglongjmp);
  596. #endif
  597. // Intercept exception handling functions.
  598. #if ASAN_INTERCEPT___CXA_THROW
  599. ASAN_INTERCEPT_FUNC(__cxa_throw);
  600. #endif
  601. #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
  602. ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception);
  603. #endif
  604. // Indirectly intercept std::rethrow_exception.
  605. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
  606. INTERCEPT_FUNCTION(_Unwind_RaiseException);
  607. #endif
  608. // Indirectly intercept std::rethrow_exception.
  609. #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION
  610. INTERCEPT_FUNCTION(_Unwind_SjLj_RaiseException);
  611. #endif
  612. // Intercept threading-related functions
  613. #if ASAN_INTERCEPT_PTHREAD_CREATE
  614. // TODO: this should probably have an unversioned fallback for newer arches?
  615. #if defined(ASAN_PTHREAD_CREATE_VERSION)
  616. ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
  617. #else
  618. ASAN_INTERCEPT_FUNC(pthread_create);
  619. #endif
  620. ASAN_INTERCEPT_FUNC(pthread_join);
  621. #endif
  622. // Intercept atexit function.
  623. #if ASAN_INTERCEPT___CXA_ATEXIT
  624. ASAN_INTERCEPT_FUNC(__cxa_atexit);
  625. #endif
  626. #if ASAN_INTERCEPT_ATEXIT
  627. ASAN_INTERCEPT_FUNC(atexit);
  628. #endif
  629. #if ASAN_INTERCEPT_PTHREAD_ATFORK
  630. ASAN_INTERCEPT_FUNC(pthread_atfork);
  631. #endif
  632. #if ASAN_INTERCEPT_VFORK
  633. ASAN_INTERCEPT_FUNC(vfork);
  634. #endif
  635. InitializePlatformInterceptors();
  636. VReport(1, "AddressSanitizer: libc interceptors initialized\n");
  637. }
  638. } // namespace __asan
  639. #endif // !SANITIZER_FUCHSIA