asan_interceptors.cc revision e933c9fc831073560e8731ac5a5e8414549387b5
1//===-- asan_interceptors.cc ----------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12// Intercept various libc functions. 13//===----------------------------------------------------------------------===// 14#include "asan_interceptors.h" 15 16#include "asan_allocator.h" 17#include "asan_intercepted_functions.h" 18#include "asan_internal.h" 19#include "asan_mapping.h" 20#include "asan_report.h" 21#include "asan_stack.h" 22#include "asan_stats.h" 23#include "asan_thread_registry.h" 24#include "interception/interception.h" 25#include "sanitizer/asan_interface.h" 26#include "sanitizer_common/sanitizer_libc.h" 27 28namespace __asan { 29 30// Instruments read/write access to a single byte in memory. 31// On error calls __asan_report_error, which aborts the program. 32#define ACCESS_ADDRESS(address, isWrite) do { \ 33 if (!AddrIsInMem(address) || AddressIsPoisoned(address)) { \ 34 GET_CURRENT_PC_BP_SP; \ 35 __asan_report_error(pc, bp, sp, address, isWrite, /* access_size */ 1); \ 36 } \ 37} while (0) 38 39// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE, 40// and ASAN_WRITE_RANGE as macro instead of function so 41// that no extra frames are created, and stack trace contains 42// relevant information only. 43 44// Instruments read/write access to a memory range. 45// More complex implementation is possible, for now just 46// checking the first and the last byte of a range. 47#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \ 48 if (size > 0) { \ 49 uptr ptr = (uptr)(offset); \ 50 ACCESS_ADDRESS(ptr, isWrite); \ 51 ACCESS_ADDRESS(ptr + (size) - 1, isWrite); \ 52 } \ 53} while (0) 54 55#define ASAN_READ_RANGE(offset, size) do { \ 56 ACCESS_MEMORY_RANGE(offset, size, false); \ 57} while (0) 58 59#define ASAN_WRITE_RANGE(offset, size) do { \ 60 ACCESS_MEMORY_RANGE(offset, size, true); \ 61} while (0) 62 63// Behavior of functions like "memcpy" or "strcpy" is undefined 64// if memory intervals overlap. We report error in this case. 65// Macro is used to avoid creation of new frames. 66static inline bool RangesOverlap(const char *offset1, uptr length1, 67 const char *offset2, uptr length2) { 68 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1)); 69} 70#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \ 71 const char *offset1 = (const char*)_offset1; \ 72 const char *offset2 = (const char*)_offset2; \ 73 if (RangesOverlap(offset1, length1, offset2, length2)) { \ 74 GET_STACK_TRACE_HERE(kStackTraceMax); \ 75 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \ 76 offset2, length2, &stack); \ 77 } \ 78} while (0) 79 80#define ENSURE_ASAN_INITED() do { \ 81 CHECK(!asan_init_is_running); \ 82 if (!asan_inited) { \ 83 __asan_init(); \ 84 } \ 85} while (0) 86 87static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) { 88#if ASAN_INTERCEPT_STRNLEN 89 if (REAL(strnlen) != 0) { 90 return REAL(strnlen)(s, maxlen); 91 } 92#endif 93 return internal_strnlen(s, maxlen); 94} 95 96} // namespace __asan 97 98// ---------------------- Wrappers ---------------- {{{1 99using namespace __asan; // NOLINT 100 101static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { 102 AsanThread *t = (AsanThread*)arg; 103 asanThreadRegistry().SetCurrent(t); 104 return t->ThreadStart(); 105} 106 107#if ASAN_INTERCEPT_PTHREAD_CREATE 108INTERCEPTOR(int, pthread_create, void *thread, 109 void *attr, void *(*start_routine)(void*), void *arg) { 110 GET_STACK_TRACE_HERE(kStackTraceMax); 111 u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); 112 AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack); 113 asanThreadRegistry().RegisterThread(t); 114 return REAL(pthread_create)(thread, attr, asan_thread_start, t); 115} 116#endif // ASAN_INTERCEPT_PTHREAD_CREATE 117 118#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 119INTERCEPTOR(void*, signal, int signum, void *handler) { 120 if (!AsanInterceptsSignal(signum)) { 121 return REAL(signal)(signum, handler); 122 } 123 return 0; 124} 125 126INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act, 127 struct sigaction *oldact) { 128 if (!AsanInterceptsSignal(signum)) { 129 return REAL(sigaction)(signum, act, oldact); 130 } 131 return 0; 132} 133#elif ASAN_POSIX 134// We need to have defined REAL(sigaction) on posix systems. 135DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act, 136 struct sigaction *oldact); 137#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 138 139INTERCEPTOR(void, longjmp, void *env, int val) { 140 __asan_handle_no_return(); 141 REAL(longjmp)(env, val); 142} 143 144#if ASAN_INTERCEPT__LONGJMP 145INTERCEPTOR(void, _longjmp, void *env, int val) { 146 __asan_handle_no_return(); 147 REAL(_longjmp)(env, val); 148} 149#endif 150 151#if ASAN_INTERCEPT_SIGLONGJMP 152INTERCEPTOR(void, siglongjmp, void *env, int val) { 153 __asan_handle_no_return(); 154 REAL(siglongjmp)(env, val); 155} 156#endif 157 158#if ASAN_INTERCEPT___CXA_THROW 159INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) { 160 CHECK(REAL(__cxa_throw)); 161 __asan_handle_no_return(); 162 REAL(__cxa_throw)(a, b, c); 163} 164#endif 165 166// intercept mlock and friends. 167// Since asan maps 16T of RAM, mlock is completely unfriendly to asan. 168// All functions return 0 (success). 169static void MlockIsUnsupported() { 170 static bool printed = 0; 171 if (printed) return; 172 printed = true; 173 Printf("INFO: AddressSanitizer ignores mlock/mlockall/munlock/munlockall\n"); 174} 175 176extern "C" { 177INTERCEPTOR(int, mlock, const void *addr, uptr len) { 178 MlockIsUnsupported(); 179 return 0; 180} 181 182INTERCEPTOR(int, munlock, const void *addr, uptr len) { 183 MlockIsUnsupported(); 184 return 0; 185} 186 187INTERCEPTOR(int, mlockall, int flags) { 188 MlockIsUnsupported(); 189 return 0; 190} 191 192INTERCEPTOR(int, munlockall, void) { 193 MlockIsUnsupported(); 194 return 0; 195} 196} // extern "C" 197 198static inline int CharCmp(unsigned char c1, unsigned char c2) { 199 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1; 200} 201 202static inline int CharCaseCmp(unsigned char c1, unsigned char c2) { 203 int c1_low = ToLower(c1); 204 int c2_low = ToLower(c2); 205 return c1_low - c2_low; 206} 207 208INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) { 209#if MAC_INTERPOSE_FUNCTIONS 210 if (!asan_inited) return REAL(memcmp)(a1, a2, size); 211#endif 212 ENSURE_ASAN_INITED(); 213 unsigned char c1 = 0, c2 = 0; 214 const unsigned char *s1 = (const unsigned char*)a1; 215 const unsigned char *s2 = (const unsigned char*)a2; 216 uptr i; 217 for (i = 0; i < size; i++) { 218 c1 = s1[i]; 219 c2 = s2[i]; 220 if (c1 != c2) break; 221 } 222 ASAN_READ_RANGE(s1, Min(i + 1, size)); 223 ASAN_READ_RANGE(s2, Min(i + 1, size)); 224 return CharCmp(c1, c2); 225} 226 227INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) { 228#if MAC_INTERPOSE_FUNCTIONS 229 if (!asan_inited) return REAL(memcpy)(to, from, size); 230#endif 231 // memcpy is called during __asan_init() from the internals 232 // of printf(...). 233 if (asan_init_is_running) { 234 return REAL(memcpy)(to, from, size); 235 } 236 ENSURE_ASAN_INITED(); 237 if (flags()->replace_intrin) { 238 if (to != from) { 239 // We do not treat memcpy with to==from as a bug. 240 // See http://llvm.org/bugs/show_bug.cgi?id=11763. 241 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); 242 } 243 ASAN_WRITE_RANGE(from, size); 244 ASAN_READ_RANGE(to, size); 245 } 246 return REAL(memcpy)(to, from, size); 247} 248 249INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) { 250#if MAC_INTERPOSE_FUNCTIONS 251 if (!asan_inited) return REAL(memmove)(to, from, size); 252#endif 253 if (asan_init_is_running) { 254 return REAL(memmove)(to, from, size); 255 } 256 ENSURE_ASAN_INITED(); 257 if (flags()->replace_intrin) { 258 ASAN_WRITE_RANGE(from, size); 259 ASAN_READ_RANGE(to, size); 260 } 261 return REAL(memmove)(to, from, size); 262} 263 264INTERCEPTOR(void*, memset, void *block, int c, uptr size) { 265#if MAC_INTERPOSE_FUNCTIONS 266 if (!asan_inited) return REAL(memset)(block, c, size); 267#endif 268 // memset is called inside Printf. 269 if (asan_init_is_running) { 270 return REAL(memset)(block, c, size); 271 } 272 ENSURE_ASAN_INITED(); 273 if (flags()->replace_intrin) { 274 ASAN_WRITE_RANGE(block, size); 275 } 276 return REAL(memset)(block, c, size); 277} 278 279INTERCEPTOR(char*, strchr, const char *str, int c) { 280#if MAC_INTERPOSE_FUNCTIONS 281 if (!asan_inited) return REAL(strchr)(str, c); 282#endif 283 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is 284 // used. 285 if (asan_init_is_running) { 286 return REAL(strchr)(str, c); 287 } 288 ENSURE_ASAN_INITED(); 289 char *result = REAL(strchr)(str, c); 290 if (flags()->replace_str) { 291 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1; 292 ASAN_READ_RANGE(str, bytes_read); 293 } 294 return result; 295} 296 297#if ASAN_INTERCEPT_INDEX 298# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 299INTERCEPTOR(char*, index, const char *string, int c) 300 ALIAS(WRAPPER_NAME(strchr)); 301# else 302DEFINE_REAL(char*, index, const char *string, int c) 303# endif 304#endif // ASAN_INTERCEPT_INDEX 305 306// For both strcat() and strncat() we need to check the validity of |to| 307// argument irrespective of the |from| length. 308INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT 309 ENSURE_ASAN_INITED(); 310 if (flags()->replace_str) { 311 uptr from_length = REAL(strlen)(from); 312 ASAN_READ_RANGE(from, from_length + 1); 313 uptr to_length = REAL(strlen)(to); 314 ASAN_READ_RANGE(to, to_length); 315 ASAN_WRITE_RANGE(to + to_length, from_length + 1); 316 // If the copying actually happens, the |from| string should not overlap 317 // with the resulting string starting at |to|, which has a length of 318 // to_length + from_length + 1. 319 if (from_length > 0) { 320 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, 321 from, from_length + 1); 322 } 323 } 324 return REAL(strcat)(to, from); // NOLINT 325} 326 327INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) { 328 ENSURE_ASAN_INITED(); 329 if (flags()->replace_str) { 330 uptr from_length = MaybeRealStrnlen(from, size); 331 uptr copy_length = Min(size, from_length + 1); 332 ASAN_READ_RANGE(from, copy_length); 333 uptr to_length = REAL(strlen)(to); 334 ASAN_READ_RANGE(to, to_length); 335 ASAN_WRITE_RANGE(to + to_length, from_length + 1); 336 if (from_length > 0) { 337 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1, 338 from, copy_length); 339 } 340 } 341 return REAL(strncat)(to, from, size); 342} 343 344INTERCEPTOR(int, strcmp, const char *s1, const char *s2) { 345#if MAC_INTERPOSE_FUNCTIONS 346 if (!asan_inited) return REAL(strcmp)(s1, s2); 347#endif 348 if (asan_init_is_running) { 349 return REAL(strcmp)(s1, s2); 350 } 351 ENSURE_ASAN_INITED(); 352 unsigned char c1, c2; 353 uptr i; 354 for (i = 0; ; i++) { 355 c1 = (unsigned char)s1[i]; 356 c2 = (unsigned char)s2[i]; 357 if (c1 != c2 || c1 == '\0') break; 358 } 359 ASAN_READ_RANGE(s1, i + 1); 360 ASAN_READ_RANGE(s2, i + 1); 361 return CharCmp(c1, c2); 362} 363 364INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT 365#if MAC_INTERPOSE_FUNCTIONS 366 if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT 367#endif 368 // strcpy is called from malloc_default_purgeable_zone() 369 // in __asan::ReplaceSystemAlloc() on Mac. 370 if (asan_init_is_running) { 371 return REAL(strcpy)(to, from); // NOLINT 372 } 373 ENSURE_ASAN_INITED(); 374 if (flags()->replace_str) { 375 uptr from_size = REAL(strlen)(from) + 1; 376 CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size); 377 ASAN_READ_RANGE(from, from_size); 378 ASAN_WRITE_RANGE(to, from_size); 379 } 380 return REAL(strcpy)(to, from); // NOLINT 381} 382 383#if ASAN_INTERCEPT_STRDUP 384INTERCEPTOR(char*, strdup, const char *s) { 385#if MAC_INTERPOSE_FUNCTIONS 386 if (!asan_inited) return REAL(strdup)(s); 387#endif 388 ENSURE_ASAN_INITED(); 389 if (flags()->replace_str) { 390 uptr length = REAL(strlen)(s); 391 ASAN_READ_RANGE(s, length + 1); 392 } 393 return REAL(strdup)(s); 394} 395#endif 396 397INTERCEPTOR(uptr, strlen, const char *s) { 398#if MAC_INTERPOSE_FUNCTIONS 399 if (!asan_inited) return REAL(strlen)(s); 400#endif 401 // strlen is called from malloc_default_purgeable_zone() 402 // in __asan::ReplaceSystemAlloc() on Mac. 403 if (asan_init_is_running) { 404 return REAL(strlen)(s); 405 } 406 ENSURE_ASAN_INITED(); 407 uptr length = REAL(strlen)(s); 408 if (flags()->replace_str) { 409 ASAN_READ_RANGE(s, length + 1); 410 } 411 return length; 412} 413 414#if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 415INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) { 416 ENSURE_ASAN_INITED(); 417 unsigned char c1, c2; 418 uptr i; 419 for (i = 0; ; i++) { 420 c1 = (unsigned char)s1[i]; 421 c2 = (unsigned char)s2[i]; 422 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break; 423 } 424 ASAN_READ_RANGE(s1, i + 1); 425 ASAN_READ_RANGE(s2, i + 1); 426 return CharCaseCmp(c1, c2); 427} 428 429INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, uptr n) { 430 ENSURE_ASAN_INITED(); 431 unsigned char c1 = 0, c2 = 0; 432 uptr i; 433 for (i = 0; i < n; i++) { 434 c1 = (unsigned char)s1[i]; 435 c2 = (unsigned char)s2[i]; 436 if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break; 437 } 438 ASAN_READ_RANGE(s1, Min(i + 1, n)); 439 ASAN_READ_RANGE(s2, Min(i + 1, n)); 440 return CharCaseCmp(c1, c2); 441} 442#endif // ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 443 444INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) { 445#if MAC_INTERPOSE_FUNCTIONS 446 if (!asan_inited) return REAL(strncmp)(s1, s2, size); 447#endif 448 // strncmp is called from malloc_default_purgeable_zone() 449 // in __asan::ReplaceSystemAlloc() on Mac. 450 if (asan_init_is_running) { 451 return REAL(strncmp)(s1, s2, size); 452 } 453 ENSURE_ASAN_INITED(); 454 unsigned char c1 = 0, c2 = 0; 455 uptr i; 456 for (i = 0; i < size; i++) { 457 c1 = (unsigned char)s1[i]; 458 c2 = (unsigned char)s2[i]; 459 if (c1 != c2 || c1 == '\0') break; 460 } 461 ASAN_READ_RANGE(s1, Min(i + 1, size)); 462 ASAN_READ_RANGE(s2, Min(i + 1, size)); 463 return CharCmp(c1, c2); 464} 465 466INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) { 467 ENSURE_ASAN_INITED(); 468 if (flags()->replace_str) { 469 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1); 470 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size); 471 ASAN_READ_RANGE(from, from_size); 472 ASAN_WRITE_RANGE(to, size); 473 } 474 return REAL(strncpy)(to, from, size); 475} 476 477#if ASAN_INTERCEPT_STRNLEN 478INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) { 479 ENSURE_ASAN_INITED(); 480 uptr length = REAL(strnlen)(s, maxlen); 481 if (flags()->replace_str) { 482 ASAN_READ_RANGE(s, Min(length + 1, maxlen)); 483 } 484 return length; 485} 486#endif // ASAN_INTERCEPT_STRNLEN 487 488static inline bool IsValidStrtolBase(int base) { 489 return (base == 0) || (2 <= base && base <= 36); 490} 491 492static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) { 493 CHECK(endptr != 0); 494 if (nptr == *endptr) { 495 // No digits were found at strtol call, we need to find out the last 496 // symbol accessed by strtoll on our own. 497 // We get this symbol by skipping leading blanks and optional +/- sign. 498 while (IsSpace(*nptr)) nptr++; 499 if (*nptr == '+' || *nptr == '-') nptr++; 500 *endptr = (char*)nptr; 501 } 502 CHECK(*endptr >= nptr); 503} 504 505INTERCEPTOR(long, strtol, const char *nptr, // NOLINT 506 char **endptr, int base) { 507 ENSURE_ASAN_INITED(); 508 if (!flags()->replace_str) { 509 return REAL(strtol)(nptr, endptr, base); 510 } 511 char *real_endptr; 512 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT 513 if (endptr != 0) { 514 *endptr = real_endptr; 515 } 516 if (IsValidStrtolBase(base)) { 517 FixRealStrtolEndptr(nptr, &real_endptr); 518 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 519 } 520 return result; 521} 522 523INTERCEPTOR(int, atoi, const char *nptr) { 524#if MAC_INTERPOSE_FUNCTIONS 525 if (!asan_inited) return REAL(atoi)(nptr); 526#endif 527 ENSURE_ASAN_INITED(); 528 if (!flags()->replace_str) { 529 return REAL(atoi)(nptr); 530 } 531 char *real_endptr; 532 // "man atoi" tells that behavior of atoi(nptr) is the same as 533 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the 534 // parsed integer can't be stored in *long* type (even if it's 535 // different from int). So, we just imitate this behavior. 536 int result = REAL(strtol)(nptr, &real_endptr, 10); 537 FixRealStrtolEndptr(nptr, &real_endptr); 538 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 539 return result; 540} 541 542INTERCEPTOR(long, atol, const char *nptr) { // NOLINT 543#if MAC_INTERPOSE_FUNCTIONS 544 if (!asan_inited) return REAL(atol)(nptr); 545#endif 546 ENSURE_ASAN_INITED(); 547 if (!flags()->replace_str) { 548 return REAL(atol)(nptr); 549 } 550 char *real_endptr; 551 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT 552 FixRealStrtolEndptr(nptr, &real_endptr); 553 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 554 return result; 555} 556 557#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL 558INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT 559 char **endptr, int base) { 560 ENSURE_ASAN_INITED(); 561 if (!flags()->replace_str) { 562 return REAL(strtoll)(nptr, endptr, base); 563 } 564 char *real_endptr; 565 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT 566 if (endptr != 0) { 567 *endptr = real_endptr; 568 } 569 // If base has unsupported value, strtoll can exit with EINVAL 570 // without reading any characters. So do additional checks only 571 // if base is valid. 572 if (IsValidStrtolBase(base)) { 573 FixRealStrtolEndptr(nptr, &real_endptr); 574 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 575 } 576 return result; 577} 578 579INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT 580 ENSURE_ASAN_INITED(); 581 if (!flags()->replace_str) { 582 return REAL(atoll)(nptr); 583 } 584 char *real_endptr; 585 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT 586 FixRealStrtolEndptr(nptr, &real_endptr); 587 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 588 return result; 589} 590#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL 591 592#define ASAN_INTERCEPT_FUNC(name) do { \ 593 if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \ 594 Report("AddressSanitizer: failed to intercept '" #name "'\n"); \ 595 } while (0) 596 597#if defined(_WIN32) 598INTERCEPTOR_WINAPI(DWORD, CreateThread, 599 void* security, uptr stack_size, 600 DWORD (__stdcall *start_routine)(void*), void* arg, 601 DWORD flags, void* tid) { 602 GET_STACK_TRACE_HERE(kStackTraceMax); 603 u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid(); 604 AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack); 605 asanThreadRegistry().RegisterThread(t); 606 return REAL(CreateThread)(security, stack_size, 607 asan_thread_start, t, flags, tid); 608} 609 610namespace __asan { 611void InitializeWindowsInterceptors() { 612 ASAN_INTERCEPT_FUNC(CreateThread); 613} 614 615} // namespace __asan 616#endif 617 618// ---------------------- InitializeAsanInterceptors ---------------- {{{1 619namespace __asan { 620void InitializeAsanInterceptors() { 621 static bool was_called_once; 622 CHECK(was_called_once == false); 623 was_called_once = true; 624#if MAC_INTERPOSE_FUNCTIONS 625 return; 626#endif 627 // Intercept mem* functions. 628 ASAN_INTERCEPT_FUNC(memcmp); 629 ASAN_INTERCEPT_FUNC(memmove); 630 ASAN_INTERCEPT_FUNC(memset); 631 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { 632 ASAN_INTERCEPT_FUNC(memcpy); 633 } else { 634#if !MAC_INTERPOSE_FUNCTIONS 635 // If we're using dynamic interceptors on Mac, these two are just plain 636 // functions. 637 internal_memcpy(&REAL(memcpy), &REAL(memmove), sizeof(REAL(memmove))); 638#endif 639 } 640 641 // Intercept str* functions. 642 ASAN_INTERCEPT_FUNC(strcat); // NOLINT 643 ASAN_INTERCEPT_FUNC(strchr); 644 ASAN_INTERCEPT_FUNC(strcmp); 645 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT 646 ASAN_INTERCEPT_FUNC(strlen); 647 ASAN_INTERCEPT_FUNC(strncat); 648 ASAN_INTERCEPT_FUNC(strncmp); 649 ASAN_INTERCEPT_FUNC(strncpy); 650#if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 651 ASAN_INTERCEPT_FUNC(strcasecmp); 652 ASAN_INTERCEPT_FUNC(strncasecmp); 653#endif 654#if ASAN_INTERCEPT_STRDUP 655 ASAN_INTERCEPT_FUNC(strdup); 656#endif 657#if ASAN_INTERCEPT_STRNLEN 658 ASAN_INTERCEPT_FUNC(strnlen); 659#endif 660#if ASAN_INTERCEPT_INDEX 661# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 662 ASAN_INTERCEPT_FUNC(index); 663# else 664 CHECK(OVERRIDE_FUNCTION(index, WRAP(strchr))); 665# endif 666#endif 667 668 ASAN_INTERCEPT_FUNC(atoi); 669 ASAN_INTERCEPT_FUNC(atol); 670 ASAN_INTERCEPT_FUNC(strtol); 671#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL 672 ASAN_INTERCEPT_FUNC(atoll); 673 ASAN_INTERCEPT_FUNC(strtoll); 674#endif 675 676#if ASAN_INTERCEPT_MLOCKX 677 // Intercept mlock/munlock. 678 ASAN_INTERCEPT_FUNC(mlock); 679 ASAN_INTERCEPT_FUNC(munlock); 680 ASAN_INTERCEPT_FUNC(mlockall); 681 ASAN_INTERCEPT_FUNC(munlockall); 682#endif 683 684 // Intecept signal- and jump-related functions. 685 ASAN_INTERCEPT_FUNC(longjmp); 686#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 687 ASAN_INTERCEPT_FUNC(sigaction); 688 ASAN_INTERCEPT_FUNC(signal); 689#endif 690#if ASAN_INTERCEPT__LONGJMP 691 ASAN_INTERCEPT_FUNC(_longjmp); 692#endif 693#if ASAN_INTERCEPT_SIGLONGJMP 694 ASAN_INTERCEPT_FUNC(siglongjmp); 695#endif 696 697 // Intercept exception handling functions. 698#if ASAN_INTERCEPT___CXA_THROW 699 INTERCEPT_FUNCTION(__cxa_throw); 700#endif 701 702 // Intercept threading-related functions 703#if ASAN_INTERCEPT_PTHREAD_CREATE 704 ASAN_INTERCEPT_FUNC(pthread_create); 705#endif 706 707 // Some Windows-specific interceptors. 708#if defined(_WIN32) 709 InitializeWindowsInterceptors(); 710#endif 711 712 // Some Mac-specific interceptors. 713#if defined(__APPLE__) 714 InitializeMacInterceptors(); 715#endif 716 717 if (flags()->verbosity > 0) { 718 Report("AddressSanitizer: libc interceptors initialized\n"); 719 } 720} 721 722} // namespace __asan 723