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