asan_interceptors.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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_ON_DLOPEN(filename, flag) \
  118. do { \
  119. if (flags()->strict_init_order) \
  120. StopInitOrderChecking(); \
  121. CheckNoDeepBind(filename, flag); \
  122. } while (false)
  123. #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
  124. #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
  125. #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
  126. #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
  127. #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
  128. if (AsanThread *t = GetCurrentThread()) { \
  129. *begin = t->tls_begin(); \
  130. *end = t->tls_end(); \
  131. } else { \
  132. *begin = *end = 0; \
  133. }
  134. #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
  135. do { \
  136. ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
  137. ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
  138. } while (false)
  139. #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
  140. do { \
  141. ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
  142. ASAN_MEMCPY_IMPL(ctx, to, from, size); \
  143. } while (false)
  144. #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
  145. do { \
  146. ASAN_INTERCEPTOR_ENTER(ctx, memset); \
  147. ASAN_MEMSET_IMPL(ctx, block, c, size); \
  148. } while (false)
  149. #if CAN_SANITIZE_LEAKS
  150. #define COMMON_INTERCEPTOR_STRERROR() \
  151. __lsan::ScopedInterceptorDisabler disabler
  152. #endif
  153. #include "sanitizer_common/sanitizer_common_interceptors.inc"
  154. #include "sanitizer_common/sanitizer_signal_interceptors.inc"
  155. // Syscall interceptors don't have contexts, we don't support suppressions
  156. // for them.
  157. #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s)
  158. #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s)
  159. #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \
  160. do { \
  161. (void)(p); \
  162. (void)(s); \
  163. } while (false)
  164. #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \
  165. do { \
  166. (void)(p); \
  167. (void)(s); \
  168. } while (false)
  169. #include "sanitizer_common/sanitizer_common_syscalls.inc"
  170. #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
  171. #if ASAN_INTERCEPT_PTHREAD_CREATE
  172. static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
  173. AsanThread *t = (AsanThread *)arg;
  174. SetCurrentThread(t);
  175. return t->ThreadStart(GetTid());
  176. }
  177. INTERCEPTOR(int, pthread_create, void *thread,
  178. void *attr, void *(*start_routine)(void*), void *arg) {
  179. EnsureMainThreadIDIsCorrect();
  180. // Strict init-order checking is thread-hostile.
  181. if (flags()->strict_init_order)
  182. StopInitOrderChecking();
  183. GET_STACK_TRACE_THREAD;
  184. int detached = 0;
  185. if (attr)
  186. REAL(pthread_attr_getdetachstate)(attr, &detached);
  187. u32 current_tid = GetCurrentTidOrInvalid();
  188. AsanThread *t =
  189. AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
  190. int result;
  191. {
  192. // Ignore all allocations made by pthread_create: thread stack/TLS may be
  193. // stored by pthread for future reuse even after thread destruction, and
  194. // the linked list it's stored in doesn't even hold valid pointers to the
  195. // objects, the latter are calculated by obscure pointer arithmetic.
  196. #if CAN_SANITIZE_LEAKS
  197. __lsan::ScopedInterceptorDisabler disabler;
  198. #endif
  199. result = REAL(pthread_create)(thread, attr, asan_thread_start, t);
  200. }
  201. if (result != 0) {
  202. // If the thread didn't start delete the AsanThread to avoid leaking it.
  203. // Note AsanThreadContexts never get destroyed so the AsanThreadContext
  204. // that was just created for the AsanThread is wasted.
  205. t->Destroy();
  206. }
  207. return result;
  208. }
  209. INTERCEPTOR(int, pthread_join, void *t, void **arg) {
  210. return real_pthread_join(t, arg);
  211. }
  212. DEFINE_REAL_PTHREAD_FUNCTIONS
  213. #endif // ASAN_INTERCEPT_PTHREAD_CREATE
  214. #if ASAN_INTERCEPT_SWAPCONTEXT
  215. static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) {
  216. // Align to page size.
  217. uptr PageSize = GetPageSizeCached();
  218. uptr bottom = stack & ~(PageSize - 1);
  219. ssize += stack - bottom;
  220. ssize = RoundUpTo(ssize, PageSize);
  221. static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb
  222. if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) {
  223. PoisonShadow(bottom, ssize, 0);
  224. }
  225. }
  226. INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp,
  227. struct ucontext_t *ucp) {
  228. static bool reported_warning = false;
  229. if (!reported_warning) {
  230. Report("WARNING: ASan doesn't fully support makecontext/swapcontext "
  231. "functions and may produce false positives in some cases!\n");
  232. reported_warning = true;
  233. }
  234. // Clear shadow memory for new context (it may share stack
  235. // with current context).
  236. uptr stack, ssize;
  237. ReadContextStack(ucp, &stack, &ssize);
  238. ClearShadowMemoryForContextStack(stack, ssize);
  239. #if __has_attribute(__indirect_return__) && \
  240. (defined(__x86_64__) || defined(__i386__))
  241. int (*real_swapcontext)(struct ucontext_t *, struct ucontext_t *)
  242. __attribute__((__indirect_return__))
  243. = REAL(swapcontext);
  244. int res = real_swapcontext(oucp, ucp);
  245. #else
  246. int res = REAL(swapcontext)(oucp, ucp);
  247. #endif
  248. // swapcontext technically does not return, but program may swap context to
  249. // "oucp" later, that would look as if swapcontext() returned 0.
  250. // We need to clear shadow for ucp once again, as it may be in arbitrary
  251. // state.
  252. ClearShadowMemoryForContextStack(stack, ssize);
  253. return res;
  254. }
  255. #endif // ASAN_INTERCEPT_SWAPCONTEXT
  256. #if SANITIZER_NETBSD
  257. #define longjmp __longjmp14
  258. #define siglongjmp __siglongjmp14
  259. #endif
  260. INTERCEPTOR(void, longjmp, void *env, int val) {
  261. __asan_handle_no_return();
  262. REAL(longjmp)(env, val);
  263. }
  264. #if ASAN_INTERCEPT__LONGJMP
  265. INTERCEPTOR(void, _longjmp, void *env, int val) {
  266. __asan_handle_no_return();
  267. REAL(_longjmp)(env, val);
  268. }
  269. #endif
  270. #if ASAN_INTERCEPT___LONGJMP_CHK
  271. INTERCEPTOR(void, __longjmp_chk, void *env, int val) {
  272. __asan_handle_no_return();
  273. REAL(__longjmp_chk)(env, val);
  274. }
  275. #endif
  276. #if ASAN_INTERCEPT_SIGLONGJMP
  277. INTERCEPTOR(void, siglongjmp, void *env, int val) {
  278. __asan_handle_no_return();
  279. REAL(siglongjmp)(env, val);
  280. }
  281. #endif
  282. #if ASAN_INTERCEPT___CXA_THROW
  283. INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) {
  284. CHECK(REAL(__cxa_throw));
  285. __asan_handle_no_return();
  286. REAL(__cxa_throw)(a, b, c);
  287. }
  288. #endif
  289. #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
  290. INTERCEPTOR(void, __cxa_rethrow_primary_exception, void *a) {
  291. CHECK(REAL(__cxa_rethrow_primary_exception));
  292. __asan_handle_no_return();
  293. REAL(__cxa_rethrow_primary_exception)(a);
  294. }
  295. #endif
  296. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
  297. INTERCEPTOR(_Unwind_Reason_Code, _Unwind_RaiseException,
  298. _Unwind_Exception *object) {
  299. CHECK(REAL(_Unwind_RaiseException));
  300. __asan_handle_no_return();
  301. return REAL(_Unwind_RaiseException)(object);
  302. }
  303. #endif
  304. #if ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION
  305. INTERCEPTOR(_Unwind_Reason_Code, _Unwind_SjLj_RaiseException,
  306. _Unwind_Exception *object) {
  307. CHECK(REAL(_Unwind_SjLj_RaiseException));
  308. __asan_handle_no_return();
  309. return REAL(_Unwind_SjLj_RaiseException)(object);
  310. }
  311. #endif
  312. #if ASAN_INTERCEPT_INDEX
  313. # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
  314. INTERCEPTOR(char*, index, const char *string, int c)
  315. ALIAS(WRAPPER_NAME(strchr));
  316. # else
  317. # if SANITIZER_MAC
  318. DECLARE_REAL(char*, index, const char *string, int c)
  319. OVERRIDE_FUNCTION(index, strchr);
  320. # else
  321. DEFINE_REAL(char*, index, const char *string, int c)
  322. # endif
  323. # endif
  324. #endif // ASAN_INTERCEPT_INDEX
  325. // For both strcat() and strncat() we need to check the validity of |to|
  326. // argument irrespective of the |from| length.
  327. INTERCEPTOR(char *, strcat, char *to, const char *from) {
  328. void *ctx;
  329. ASAN_INTERCEPTOR_ENTER(ctx, strcat);
  330. ENSURE_ASAN_INITED();
  331. if (flags()->replace_str) {
  332. uptr from_length = internal_strlen(from);
  333. ASAN_READ_RANGE(ctx, from, from_length + 1);
  334. uptr to_length = internal_strlen(to);
  335. ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
  336. ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
  337. // If the copying actually happens, the |from| string should not overlap
  338. // with the resulting string starting at |to|, which has a length of
  339. // to_length + from_length + 1.
  340. if (from_length > 0) {
  341. CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, from,
  342. from_length + 1);
  343. }
  344. }
  345. return REAL(strcat)(to, from);
  346. }
  347. INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
  348. void *ctx;
  349. ASAN_INTERCEPTOR_ENTER(ctx, strncat);
  350. ENSURE_ASAN_INITED();
  351. if (flags()->replace_str) {
  352. uptr from_length = MaybeRealStrnlen(from, size);
  353. uptr copy_length = Min(size, from_length + 1);
  354. ASAN_READ_RANGE(ctx, from, copy_length);
  355. uptr to_length = internal_strlen(to);
  356. ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length);
  357. ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1);
  358. if (from_length > 0) {
  359. CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1,
  360. from, copy_length);
  361. }
  362. }
  363. return REAL(strncat)(to, from, size);
  364. }
  365. INTERCEPTOR(char *, strcpy, char *to, const char *from) {
  366. void *ctx;
  367. ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
  368. #if SANITIZER_MAC
  369. if (UNLIKELY(!asan_inited))
  370. return REAL(strcpy)(to, from);
  371. #endif
  372. // strcpy is called from malloc_default_purgeable_zone()
  373. // in __asan::ReplaceSystemAlloc() on Mac.
  374. if (asan_init_is_running) {
  375. return REAL(strcpy)(to, from);
  376. }
  377. ENSURE_ASAN_INITED();
  378. if (flags()->replace_str) {
  379. uptr from_size = internal_strlen(from) + 1;
  380. CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);
  381. ASAN_READ_RANGE(ctx, from, from_size);
  382. ASAN_WRITE_RANGE(ctx, to, from_size);
  383. }
  384. return REAL(strcpy)(to, from);
  385. }
  386. INTERCEPTOR(char*, strdup, const char *s) {
  387. void *ctx;
  388. ASAN_INTERCEPTOR_ENTER(ctx, strdup);
  389. if (UNLIKELY(!asan_inited)) return internal_strdup(s);
  390. ENSURE_ASAN_INITED();
  391. uptr length = internal_strlen(s);
  392. if (flags()->replace_str) {
  393. ASAN_READ_RANGE(ctx, s, length + 1);
  394. }
  395. GET_STACK_TRACE_MALLOC;
  396. void *new_mem = asan_malloc(length + 1, &stack);
  397. REAL(memcpy)(new_mem, s, length + 1);
  398. return reinterpret_cast<char*>(new_mem);
  399. }
  400. #if ASAN_INTERCEPT___STRDUP
  401. INTERCEPTOR(char*, __strdup, const char *s) {
  402. void *ctx;
  403. ASAN_INTERCEPTOR_ENTER(ctx, strdup);
  404. if (UNLIKELY(!asan_inited)) return internal_strdup(s);
  405. ENSURE_ASAN_INITED();
  406. uptr length = internal_strlen(s);
  407. if (flags()->replace_str) {
  408. ASAN_READ_RANGE(ctx, s, length + 1);
  409. }
  410. GET_STACK_TRACE_MALLOC;
  411. void *new_mem = asan_malloc(length + 1, &stack);
  412. REAL(memcpy)(new_mem, s, length + 1);
  413. return reinterpret_cast<char*>(new_mem);
  414. }
  415. #endif // ASAN_INTERCEPT___STRDUP
  416. INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) {
  417. void *ctx;
  418. ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
  419. ENSURE_ASAN_INITED();
  420. if (flags()->replace_str) {
  421. uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1);
  422. CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size);
  423. ASAN_READ_RANGE(ctx, from, from_size);
  424. ASAN_WRITE_RANGE(ctx, to, size);
  425. }
  426. return REAL(strncpy)(to, from, size);
  427. }
  428. INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) {
  429. void *ctx;
  430. ASAN_INTERCEPTOR_ENTER(ctx, strtol);
  431. ENSURE_ASAN_INITED();
  432. if (!flags()->replace_str) {
  433. return REAL(strtol)(nptr, endptr, base);
  434. }
  435. char *real_endptr;
  436. long result = REAL(strtol)(nptr, &real_endptr, base);
  437. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  438. return result;
  439. }
  440. INTERCEPTOR(int, atoi, const char *nptr) {
  441. void *ctx;
  442. ASAN_INTERCEPTOR_ENTER(ctx, atoi);
  443. #if SANITIZER_MAC
  444. if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
  445. #endif
  446. ENSURE_ASAN_INITED();
  447. if (!flags()->replace_str) {
  448. return REAL(atoi)(nptr);
  449. }
  450. char *real_endptr;
  451. // "man atoi" tells that behavior of atoi(nptr) is the same as
  452. // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the
  453. // parsed integer can't be stored in *long* type (even if it's
  454. // different from int). So, we just imitate this behavior.
  455. int result = REAL(strtol)(nptr, &real_endptr, 10);
  456. FixRealStrtolEndptr(nptr, &real_endptr);
  457. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  458. return result;
  459. }
  460. INTERCEPTOR(long, atol, const char *nptr) {
  461. void *ctx;
  462. ASAN_INTERCEPTOR_ENTER(ctx, atol);
  463. #if SANITIZER_MAC
  464. if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
  465. #endif
  466. ENSURE_ASAN_INITED();
  467. if (!flags()->replace_str) {
  468. return REAL(atol)(nptr);
  469. }
  470. char *real_endptr;
  471. long result = REAL(strtol)(nptr, &real_endptr, 10);
  472. FixRealStrtolEndptr(nptr, &real_endptr);
  473. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  474. return result;
  475. }
  476. #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  477. INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) {
  478. void *ctx;
  479. ASAN_INTERCEPTOR_ENTER(ctx, strtoll);
  480. ENSURE_ASAN_INITED();
  481. if (!flags()->replace_str) {
  482. return REAL(strtoll)(nptr, endptr, base);
  483. }
  484. char *real_endptr;
  485. long long result = REAL(strtoll)(nptr, &real_endptr, base);
  486. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  487. return result;
  488. }
  489. INTERCEPTOR(long long, atoll, const char *nptr) {
  490. void *ctx;
  491. ASAN_INTERCEPTOR_ENTER(ctx, atoll);
  492. ENSURE_ASAN_INITED();
  493. if (!flags()->replace_str) {
  494. return REAL(atoll)(nptr);
  495. }
  496. char *real_endptr;
  497. long long result = REAL(strtoll)(nptr, &real_endptr, 10);
  498. FixRealStrtolEndptr(nptr, &real_endptr);
  499. ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1);
  500. return result;
  501. }
  502. #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  503. #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT
  504. static void AtCxaAtexit(void *unused) {
  505. (void)unused;
  506. StopInitOrderChecking();
  507. }
  508. #endif
  509. #if ASAN_INTERCEPT___CXA_ATEXIT
  510. INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
  511. void *dso_handle) {
  512. #if SANITIZER_MAC
  513. if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle);
  514. #endif
  515. ENSURE_ASAN_INITED();
  516. #if CAN_SANITIZE_LEAKS
  517. __lsan::ScopedInterceptorDisabler disabler;
  518. #endif
  519. int res = REAL(__cxa_atexit)(func, arg, dso_handle);
  520. REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
  521. return res;
  522. }
  523. #endif // ASAN_INTERCEPT___CXA_ATEXIT
  524. #if ASAN_INTERCEPT_ATEXIT
  525. INTERCEPTOR(int, atexit, void (*func)()) {
  526. ENSURE_ASAN_INITED();
  527. #if CAN_SANITIZE_LEAKS
  528. __lsan::ScopedInterceptorDisabler disabler;
  529. #endif
  530. // Avoid calling real atexit as it is unreachable on at least on Linux.
  531. int res = REAL(__cxa_atexit)((void (*)(void *a))func, nullptr, nullptr);
  532. REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
  533. return res;
  534. }
  535. #endif
  536. #if ASAN_INTERCEPT_PTHREAD_ATFORK
  537. extern "C" {
  538. extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
  539. void (*child)());
  540. };
  541. INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
  542. void (*child)()) {
  543. #if CAN_SANITIZE_LEAKS
  544. __lsan::ScopedInterceptorDisabler disabler;
  545. #endif
  546. // REAL(pthread_atfork) cannot be called due to symbol indirections at least
  547. // on NetBSD
  548. return _pthread_atfork(prepare, parent, child);
  549. }
  550. #endif
  551. #if ASAN_INTERCEPT_VFORK
  552. DEFINE_REAL(int, vfork)
  553. DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork)
  554. #endif
  555. // ---------------------- InitializeAsanInterceptors ---------------- {{{1
  556. namespace __asan {
  557. void InitializeAsanInterceptors() {
  558. static bool was_called_once;
  559. CHECK(!was_called_once);
  560. was_called_once = true;
  561. InitializeCommonInterceptors();
  562. InitializeSignalInterceptors();
  563. // Intercept str* functions.
  564. ASAN_INTERCEPT_FUNC(strcat);
  565. ASAN_INTERCEPT_FUNC(strcpy);
  566. ASAN_INTERCEPT_FUNC(strncat);
  567. ASAN_INTERCEPT_FUNC(strncpy);
  568. ASAN_INTERCEPT_FUNC(strdup);
  569. #if ASAN_INTERCEPT___STRDUP
  570. ASAN_INTERCEPT_FUNC(__strdup);
  571. #endif
  572. #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX
  573. ASAN_INTERCEPT_FUNC(index);
  574. #endif
  575. ASAN_INTERCEPT_FUNC(atoi);
  576. ASAN_INTERCEPT_FUNC(atol);
  577. ASAN_INTERCEPT_FUNC(strtol);
  578. #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
  579. ASAN_INTERCEPT_FUNC(atoll);
  580. ASAN_INTERCEPT_FUNC(strtoll);
  581. #endif
  582. // Intecept jump-related functions.
  583. ASAN_INTERCEPT_FUNC(longjmp);
  584. #if ASAN_INTERCEPT_SWAPCONTEXT
  585. ASAN_INTERCEPT_FUNC(swapcontext);
  586. #endif
  587. #if ASAN_INTERCEPT__LONGJMP
  588. ASAN_INTERCEPT_FUNC(_longjmp);
  589. #endif
  590. #if ASAN_INTERCEPT___LONGJMP_CHK
  591. ASAN_INTERCEPT_FUNC(__longjmp_chk);
  592. #endif
  593. #if ASAN_INTERCEPT_SIGLONGJMP
  594. ASAN_INTERCEPT_FUNC(siglongjmp);
  595. #endif
  596. // Intercept exception handling functions.
  597. #if ASAN_INTERCEPT___CXA_THROW
  598. ASAN_INTERCEPT_FUNC(__cxa_throw);
  599. #endif
  600. #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION
  601. ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception);
  602. #endif
  603. // Indirectly intercept std::rethrow_exception.
  604. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION
  605. INTERCEPT_FUNCTION(_Unwind_RaiseException);
  606. #endif
  607. // Indirectly intercept std::rethrow_exception.
  608. #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION
  609. INTERCEPT_FUNCTION(_Unwind_SjLj_RaiseException);
  610. #endif
  611. // Intercept threading-related functions
  612. #if ASAN_INTERCEPT_PTHREAD_CREATE
  613. // TODO: this should probably have an unversioned fallback for newer arches?
  614. #if defined(ASAN_PTHREAD_CREATE_VERSION)
  615. ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION);
  616. #else
  617. ASAN_INTERCEPT_FUNC(pthread_create);
  618. #endif
  619. ASAN_INTERCEPT_FUNC(pthread_join);
  620. #endif
  621. // Intercept atexit function.
  622. #if ASAN_INTERCEPT___CXA_ATEXIT
  623. ASAN_INTERCEPT_FUNC(__cxa_atexit);
  624. #endif
  625. #if ASAN_INTERCEPT_ATEXIT
  626. ASAN_INTERCEPT_FUNC(atexit);
  627. #endif
  628. #if ASAN_INTERCEPT_PTHREAD_ATFORK
  629. ASAN_INTERCEPT_FUNC(pthread_atfork);
  630. #endif
  631. #if ASAN_INTERCEPT_VFORK
  632. ASAN_INTERCEPT_FUNC(vfork);
  633. #endif
  634. InitializePlatformInterceptors();
  635. VReport(1, "AddressSanitizer: libc interceptors initialized\n");
  636. }
  637. } // namespace __asan
  638. #endif // !SANITIZER_FUCHSIA