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