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