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