asan_interceptors.cc revision b99228de65b408b0acd9618b92774fb8add7e482
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_poisoning.h" 21#include "asan_report.h" 22#include "asan_stack.h" 23#include "asan_stats.h" 24#include "interception/interception.h" 25#include "sanitizer_common/sanitizer_libc.h" 26 27namespace __asan { 28 29// Return true if we can quickly decide that the region is unpoisoned. 30static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) { 31 if (size == 0) return true; 32 if (size <= 32) 33 return !AddressIsPoisoned(beg) && 34 !AddressIsPoisoned(beg + size - 1) && 35 !AddressIsPoisoned(beg + size / 2); 36 return false; 37} 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// We check all shadow bytes. 44#define ACCESS_MEMORY_RANGE(offset, size, isWrite) do { \ 45 uptr __offset = (uptr)(offset); \ 46 uptr __size = (uptr)(size); \ 47 uptr __bad = 0; \ 48 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \ 49 (__bad = __asan_region_is_poisoned(__offset, __size))) { \ 50 GET_CURRENT_PC_BP_SP; \ 51 __asan_report_error(pc, bp, sp, __bad, isWrite, __size); \ 52 } \ 53 } while (0) 54 55#define ASAN_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, false) 56#define ASAN_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size, true) 57 58// Behavior of functions like "memcpy" or "strcpy" is undefined 59// if memory intervals overlap. We report error in this case. 60// Macro is used to avoid creation of new frames. 61static inline bool RangesOverlap(const char *offset1, uptr length1, 62 const char *offset2, uptr length2) { 63 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1)); 64} 65#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \ 66 const char *offset1 = (const char*)_offset1; \ 67 const char *offset2 = (const char*)_offset2; \ 68 if (RangesOverlap(offset1, length1, offset2, length2)) { \ 69 GET_STACK_TRACE_FATAL_HERE; \ 70 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \ 71 offset2, length2, &stack); \ 72 } \ 73} while (0) 74 75#define ENSURE_ASAN_INITED() do { \ 76 CHECK(!asan_init_is_running); \ 77 if (!asan_inited) { \ 78 __asan_init(); \ 79 } \ 80} while (0) 81 82static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) { 83#if ASAN_INTERCEPT_STRNLEN 84 if (REAL(strnlen) != 0) { 85 return REAL(strnlen)(s, maxlen); 86 } 87#endif 88 return internal_strnlen(s, maxlen); 89} 90 91void SetThreadName(const char *name) { 92 AsanThread *t = GetCurrentThread(); 93 if (t) 94 asanThreadRegistry().SetThreadName(t->tid(), name); 95} 96 97} // namespace __asan 98 99// ---------------------- Wrappers ---------------- {{{1 100using namespace __asan; // NOLINT 101 102DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr) 103DECLARE_REAL_AND_INTERCEPTOR(void, free, void *) 104 105#define COMMON_INTERCEPTOR_UNPOISON_PARAM(ctx, count) \ 106 do { \ 107 } while (false) 108#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ 109 ASAN_WRITE_RANGE(ptr, size) 110#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size) 111#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ 112 do { \ 113 if (asan_init_is_running) return REAL(func)(__VA_ARGS__); \ 114 ctx = 0; \ 115 (void) ctx; \ 116 ENSURE_ASAN_INITED(); \ 117 } while (false) 118#define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ 119 do { \ 120 } while (false) 121#define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ 122 do { \ 123 } while (false) 124#define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ 125 do { \ 126 } while (false) 127#define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name) 128#define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name) 129#include "sanitizer_common/sanitizer_common_interceptors.inc" 130 131#define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(p, s) 132#define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(p, s) 133#define COMMON_SYSCALL_POST_READ_RANGE(p, s) \ 134 do { \ 135 } while (false) 136#define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \ 137 do { \ 138 } while (false) 139#include "sanitizer_common/sanitizer_common_syscalls.inc" 140 141static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { 142 AsanThread *t = (AsanThread*)arg; 143 SetCurrentThread(t); 144 return t->ThreadStart(GetTid()); 145} 146 147#if ASAN_INTERCEPT_PTHREAD_CREATE 148extern "C" int pthread_attr_getdetachstate(void *attr, int *v); 149 150INTERCEPTOR(int, pthread_create, void *thread, 151 void *attr, void *(*start_routine)(void*), void *arg) { 152 EnsureMainThreadIDIsCorrect(); 153 // Strict init-order checking in thread-hostile. 154 if (flags()->strict_init_order) 155 StopInitOrderChecking(); 156 GET_STACK_TRACE_THREAD; 157 int detached = 0; 158 if (attr != 0) 159 pthread_attr_getdetachstate(attr, &detached); 160 161 u32 current_tid = GetCurrentTidOrInvalid(); 162 AsanThread *t = AsanThread::Create(start_routine, arg); 163 CreateThreadContextArgs args = { t, &stack }; 164 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args); 165 return REAL(pthread_create)(thread, attr, asan_thread_start, t); 166} 167#endif // ASAN_INTERCEPT_PTHREAD_CREATE 168 169#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 170INTERCEPTOR(void*, signal, int signum, void *handler) { 171 if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) { 172 return REAL(signal)(signum, handler); 173 } 174 return 0; 175} 176 177INTERCEPTOR(int, sigaction, int signum, const struct sigaction *act, 178 struct sigaction *oldact) { 179 if (!AsanInterceptsSignal(signum) || flags()->allow_user_segv_handler) { 180 return REAL(sigaction)(signum, act, oldact); 181 } 182 return 0; 183} 184#elif SANITIZER_POSIX 185// We need to have defined REAL(sigaction) on posix systems. 186DEFINE_REAL(int, sigaction, int signum, const struct sigaction *act, 187 struct sigaction *oldact) 188#endif // ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 189 190#if ASAN_INTERCEPT_SWAPCONTEXT 191static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) { 192 // Align to page size. 193 uptr PageSize = GetPageSizeCached(); 194 uptr bottom = stack & ~(PageSize - 1); 195 ssize += stack - bottom; 196 ssize = RoundUpTo(ssize, PageSize); 197 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb 198 if (ssize && ssize <= kMaxSaneContextStackSize) { 199 PoisonShadow(bottom, ssize, 0); 200 } 201} 202 203INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp, 204 struct ucontext_t *ucp) { 205 static bool reported_warning = false; 206 if (!reported_warning) { 207 Report("WARNING: ASan doesn't fully support makecontext/swapcontext " 208 "functions and may produce false positives in some cases!\n"); 209 reported_warning = true; 210 } 211 // Clear shadow memory for new context (it may share stack 212 // with current context). 213 uptr stack, ssize; 214 ReadContextStack(ucp, &stack, &ssize); 215 ClearShadowMemoryForContextStack(stack, ssize); 216 int res = REAL(swapcontext)(oucp, ucp); 217 // swapcontext technically does not return, but program may swap context to 218 // "oucp" later, that would look as if swapcontext() returned 0. 219 // We need to clear shadow for ucp once again, as it may be in arbitrary 220 // state. 221 ClearShadowMemoryForContextStack(stack, ssize); 222 return res; 223} 224#endif // ASAN_INTERCEPT_SWAPCONTEXT 225 226INTERCEPTOR(void, longjmp, void *env, int val) { 227 __asan_handle_no_return(); 228 REAL(longjmp)(env, val); 229} 230 231#if ASAN_INTERCEPT__LONGJMP 232INTERCEPTOR(void, _longjmp, void *env, int val) { 233 __asan_handle_no_return(); 234 REAL(_longjmp)(env, val); 235} 236#endif 237 238#if ASAN_INTERCEPT_SIGLONGJMP 239INTERCEPTOR(void, siglongjmp, void *env, int val) { 240 __asan_handle_no_return(); 241 REAL(siglongjmp)(env, val); 242} 243#endif 244 245#if ASAN_INTERCEPT___CXA_THROW 246INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) { 247 CHECK(REAL(__cxa_throw)); 248 __asan_handle_no_return(); 249 REAL(__cxa_throw)(a, b, c); 250} 251#endif 252 253// intercept mlock and friends. 254// Since asan maps 16T of RAM, mlock is completely unfriendly to asan. 255// All functions return 0 (success). 256static void MlockIsUnsupported() { 257 static bool printed = false; 258 if (printed) return; 259 printed = true; 260 if (flags()->verbosity > 0) { 261 Printf("INFO: AddressSanitizer ignores " 262 "mlock/mlockall/munlock/munlockall\n"); 263 } 264} 265 266INTERCEPTOR(int, mlock, const void *addr, uptr len) { 267 MlockIsUnsupported(); 268 return 0; 269} 270 271INTERCEPTOR(int, munlock, const void *addr, uptr len) { 272 MlockIsUnsupported(); 273 return 0; 274} 275 276INTERCEPTOR(int, mlockall, int flags) { 277 MlockIsUnsupported(); 278 return 0; 279} 280 281INTERCEPTOR(int, munlockall, void) { 282 MlockIsUnsupported(); 283 return 0; 284} 285 286static inline int CharCmp(unsigned char c1, unsigned char c2) { 287 return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1; 288} 289 290INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) { 291 if (!asan_inited) return internal_memcmp(a1, a2, size); 292 ENSURE_ASAN_INITED(); 293 if (flags()->replace_intrin) { 294 if (flags()->strict_memcmp) { 295 // Check the entire regions even if the first bytes of the buffers are 296 // different. 297 ASAN_READ_RANGE(a1, size); 298 ASAN_READ_RANGE(a2, size); 299 // Fallthrough to REAL(memcmp) below. 300 } else { 301 unsigned char c1 = 0, c2 = 0; 302 const unsigned char *s1 = (const unsigned char*)a1; 303 const unsigned char *s2 = (const unsigned char*)a2; 304 uptr i; 305 for (i = 0; i < size; i++) { 306 c1 = s1[i]; 307 c2 = s2[i]; 308 if (c1 != c2) break; 309 } 310 ASAN_READ_RANGE(s1, Min(i + 1, size)); 311 ASAN_READ_RANGE(s2, Min(i + 1, size)); 312 return CharCmp(c1, c2); 313 } 314 } 315 return REAL(memcmp(a1, a2, size)); 316} 317 318INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) { 319 if (!asan_inited) return internal_memcpy(to, from, size); 320 // memcpy is called during __asan_init() from the internals 321 // of printf(...). 322 if (asan_init_is_running) { 323 return REAL(memcpy)(to, from, size); 324 } 325 ENSURE_ASAN_INITED(); 326 if (flags()->replace_intrin) { 327 if (to != from) { 328 // We do not treat memcpy with to==from as a bug. 329 // See http://llvm.org/bugs/show_bug.cgi?id=11763. 330 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); 331 } 332 ASAN_READ_RANGE(from, size); 333 ASAN_WRITE_RANGE(to, size); 334 } 335 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8. 336 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116. 337 return internal_memcpy(to, from, size); 338} 339 340INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) { 341 if (!asan_inited) return internal_memmove(to, from, size); 342 if (asan_init_is_running) { 343 return REAL(memmove)(to, from, size); 344 } 345 ENSURE_ASAN_INITED(); 346 if (flags()->replace_intrin) { 347 ASAN_READ_RANGE(from, size); 348 ASAN_WRITE_RANGE(to, size); 349 } 350 // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8. 351 // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116. 352 return internal_memmove(to, from, size); 353} 354 355INTERCEPTOR(void*, memset, void *block, int c, uptr size) { 356 if (!asan_inited) return internal_memset(block, c, size); 357 // memset is called inside Printf. 358 if (asan_init_is_running) { 359 return REAL(memset)(block, c, size); 360 } 361 ENSURE_ASAN_INITED(); 362 if (flags()->replace_intrin) { 363 ASAN_WRITE_RANGE(block, size); 364 } 365 return REAL(memset)(block, c, size); 366} 367 368INTERCEPTOR(char*, strchr, const char *str, int c) { 369 if (!asan_inited) return internal_strchr(str, c); 370 // strchr is called inside create_purgeable_zone() when MallocGuardEdges=1 is 371 // used. 372 if (asan_init_is_running) { 373 return REAL(strchr)(str, c); 374 } 375 ENSURE_ASAN_INITED(); 376 char *result = REAL(strchr)(str, c); 377 if (flags()->replace_str) { 378 uptr bytes_read = (result ? result - str : REAL(strlen)(str)) + 1; 379 ASAN_READ_RANGE(str, bytes_read); 380 } 381 return result; 382} 383 384#if ASAN_INTERCEPT_INDEX 385# if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 386INTERCEPTOR(char*, index, const char *string, int c) 387 ALIAS(WRAPPER_NAME(strchr)); 388# else 389# if SANITIZER_MAC 390DECLARE_REAL(char*, index, const char *string, int c) 391OVERRIDE_FUNCTION(index, strchr); 392# else 393DEFINE_REAL(char*, index, const char *string, int c) 394# endif 395# endif 396#endif // ASAN_INTERCEPT_INDEX 397 398// For both strcat() and strncat() we need to check the validity of |to| 399// argument irrespective of the |from| length. 400INTERCEPTOR(char*, strcat, char *to, const char *from) { // NOLINT 401 ENSURE_ASAN_INITED(); 402 if (flags()->replace_str) { 403 uptr from_length = REAL(strlen)(from); 404 ASAN_READ_RANGE(from, from_length + 1); 405 uptr to_length = REAL(strlen)(to); 406 ASAN_READ_RANGE(to, to_length); 407 ASAN_WRITE_RANGE(to + to_length, from_length + 1); 408 // If the copying actually happens, the |from| string should not overlap 409 // with the resulting string starting at |to|, which has a length of 410 // to_length + from_length + 1. 411 if (from_length > 0) { 412 CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, 413 from, from_length + 1); 414 } 415 } 416 return REAL(strcat)(to, from); // NOLINT 417} 418 419INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) { 420 ENSURE_ASAN_INITED(); 421 if (flags()->replace_str) { 422 uptr from_length = MaybeRealStrnlen(from, size); 423 uptr copy_length = Min(size, from_length + 1); 424 ASAN_READ_RANGE(from, copy_length); 425 uptr to_length = REAL(strlen)(to); 426 ASAN_READ_RANGE(to, to_length); 427 ASAN_WRITE_RANGE(to + to_length, from_length + 1); 428 if (from_length > 0) { 429 CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1, 430 from, copy_length); 431 } 432 } 433 return REAL(strncat)(to, from, size); 434} 435 436INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT 437#if SANITIZER_MAC 438 if (!asan_inited) return REAL(strcpy)(to, from); // NOLINT 439#endif 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 455#if ASAN_INTERCEPT_STRDUP 456INTERCEPTOR(char*, strdup, const char *s) { 457 if (!asan_inited) return internal_strdup(s); 458 ENSURE_ASAN_INITED(); 459 uptr length = REAL(strlen)(s); 460 if (flags()->replace_str) { 461 ASAN_READ_RANGE(s, length + 1); 462 } 463 GET_STACK_TRACE_MALLOC; 464 void *new_mem = asan_malloc(length + 1, &stack); 465 REAL(memcpy)(new_mem, s, length + 1); 466 return reinterpret_cast<char*>(new_mem); 467} 468#endif 469 470INTERCEPTOR(uptr, strlen, const char *s) { 471 if (!asan_inited) return internal_strlen(s); 472 // strlen is called from malloc_default_purgeable_zone() 473 // in __asan::ReplaceSystemAlloc() on Mac. 474 if (asan_init_is_running) { 475 return REAL(strlen)(s); 476 } 477 ENSURE_ASAN_INITED(); 478 uptr length = REAL(strlen)(s); 479 if (flags()->replace_str) { 480 ASAN_READ_RANGE(s, length + 1); 481 } 482 return length; 483} 484 485INTERCEPTOR(uptr, wcslen, const wchar_t *s) { 486 uptr length = REAL(wcslen)(s); 487 if (!asan_init_is_running) { 488 ENSURE_ASAN_INITED(); 489 ASAN_READ_RANGE(s, (length + 1) * sizeof(wchar_t)); 490 } 491 return length; 492} 493 494INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) { 495 ENSURE_ASAN_INITED(); 496 if (flags()->replace_str) { 497 uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1); 498 CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size); 499 ASAN_READ_RANGE(from, from_size); 500 ASAN_WRITE_RANGE(to, size); 501 } 502 return REAL(strncpy)(to, from, size); 503} 504 505#if ASAN_INTERCEPT_STRNLEN 506INTERCEPTOR(uptr, strnlen, const char *s, uptr maxlen) { 507 ENSURE_ASAN_INITED(); 508 uptr length = REAL(strnlen)(s, maxlen); 509 if (flags()->replace_str) { 510 ASAN_READ_RANGE(s, Min(length + 1, maxlen)); 511 } 512 return length; 513} 514#endif // ASAN_INTERCEPT_STRNLEN 515 516static inline bool IsValidStrtolBase(int base) { 517 return (base == 0) || (2 <= base && base <= 36); 518} 519 520static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) { 521 CHECK(endptr); 522 if (nptr == *endptr) { 523 // No digits were found at strtol call, we need to find out the last 524 // symbol accessed by strtoll on our own. 525 // We get this symbol by skipping leading blanks and optional +/- sign. 526 while (IsSpace(*nptr)) nptr++; 527 if (*nptr == '+' || *nptr == '-') nptr++; 528 *endptr = (char*)nptr; 529 } 530 CHECK(*endptr >= nptr); 531} 532 533INTERCEPTOR(long, strtol, const char *nptr, // NOLINT 534 char **endptr, int base) { 535 ENSURE_ASAN_INITED(); 536 if (!flags()->replace_str) { 537 return REAL(strtol)(nptr, endptr, base); 538 } 539 char *real_endptr; 540 long result = REAL(strtol)(nptr, &real_endptr, base); // NOLINT 541 if (endptr != 0) { 542 *endptr = real_endptr; 543 } 544 if (IsValidStrtolBase(base)) { 545 FixRealStrtolEndptr(nptr, &real_endptr); 546 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 547 } 548 return result; 549} 550 551INTERCEPTOR(int, atoi, const char *nptr) { 552#if SANITIZER_MAC 553 if (!asan_inited) return REAL(atoi)(nptr); 554#endif 555 ENSURE_ASAN_INITED(); 556 if (!flags()->replace_str) { 557 return REAL(atoi)(nptr); 558 } 559 char *real_endptr; 560 // "man atoi" tells that behavior of atoi(nptr) is the same as 561 // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the 562 // parsed integer can't be stored in *long* type (even if it's 563 // different from int). So, we just imitate this behavior. 564 int result = REAL(strtol)(nptr, &real_endptr, 10); 565 FixRealStrtolEndptr(nptr, &real_endptr); 566 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 567 return result; 568} 569 570INTERCEPTOR(long, atol, const char *nptr) { // NOLINT 571#if SANITIZER_MAC 572 if (!asan_inited) return REAL(atol)(nptr); 573#endif 574 ENSURE_ASAN_INITED(); 575 if (!flags()->replace_str) { 576 return REAL(atol)(nptr); 577 } 578 char *real_endptr; 579 long result = REAL(strtol)(nptr, &real_endptr, 10); // NOLINT 580 FixRealStrtolEndptr(nptr, &real_endptr); 581 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 582 return result; 583} 584 585#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL 586INTERCEPTOR(long long, strtoll, const char *nptr, // NOLINT 587 char **endptr, int base) { 588 ENSURE_ASAN_INITED(); 589 if (!flags()->replace_str) { 590 return REAL(strtoll)(nptr, endptr, base); 591 } 592 char *real_endptr; 593 long long result = REAL(strtoll)(nptr, &real_endptr, base); // NOLINT 594 if (endptr != 0) { 595 *endptr = real_endptr; 596 } 597 // If base has unsupported value, strtoll can exit with EINVAL 598 // without reading any characters. So do additional checks only 599 // if base is valid. 600 if (IsValidStrtolBase(base)) { 601 FixRealStrtolEndptr(nptr, &real_endptr); 602 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 603 } 604 return result; 605} 606 607INTERCEPTOR(long long, atoll, const char *nptr) { // NOLINT 608 ENSURE_ASAN_INITED(); 609 if (!flags()->replace_str) { 610 return REAL(atoll)(nptr); 611 } 612 char *real_endptr; 613 long long result = REAL(strtoll)(nptr, &real_endptr, 10); // NOLINT 614 FixRealStrtolEndptr(nptr, &real_endptr); 615 ASAN_READ_RANGE(nptr, (real_endptr - nptr) + 1); 616 return result; 617} 618#endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL 619 620static void AtCxaAtexit(void *unused) { 621 (void)unused; 622 StopInitOrderChecking(); 623} 624 625#if ASAN_INTERCEPT___CXA_ATEXIT 626INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg, 627 void *dso_handle) { 628 ENSURE_ASAN_INITED(); 629 int res = REAL(__cxa_atexit)(func, arg, dso_handle); 630 REAL(__cxa_atexit)(AtCxaAtexit, 0, 0); 631 return res; 632} 633#endif // ASAN_INTERCEPT___CXA_ATEXIT 634 635#if !SANITIZER_MAC 636#define ASAN_INTERCEPT_FUNC(name) do { \ 637 if (!INTERCEPT_FUNCTION(name) && flags()->verbosity > 0) \ 638 Report("AddressSanitizer: failed to intercept '" #name "'\n"); \ 639 } while (0) 640#else 641// OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION. 642#define ASAN_INTERCEPT_FUNC(name) 643#endif // SANITIZER_MAC 644 645#if SANITIZER_WINDOWS 646INTERCEPTOR_WINAPI(DWORD, CreateThread, 647 void* security, uptr stack_size, 648 DWORD (__stdcall *start_routine)(void*), void* arg, 649 DWORD thr_flags, void* tid) { 650 // Strict init-order checking in thread-hostile. 651 if (flags()->strict_init_order) 652 StopInitOrderChecking(); 653 GET_STACK_TRACE_THREAD; 654 u32 current_tid = GetCurrentTidOrInvalid(); 655 AsanThread *t = AsanThread::Create(start_routine, arg); 656 CreateThreadContextArgs args = { t, &stack }; 657 bool detached = false; // FIXME: how can we determine it on Windows? 658 asanThreadRegistry().CreateThread(*(uptr*)t, detached, current_tid, &args); 659 return REAL(CreateThread)(security, stack_size, 660 asan_thread_start, t, thr_flags, tid); 661} 662 663namespace __asan { 664void InitializeWindowsInterceptors() { 665 ASAN_INTERCEPT_FUNC(CreateThread); 666} 667 668} // namespace __asan 669#endif 670 671// ---------------------- InitializeAsanInterceptors ---------------- {{{1 672namespace __asan { 673void InitializeAsanInterceptors() { 674 static bool was_called_once; 675 CHECK(was_called_once == false); 676 was_called_once = true; 677 SANITIZER_COMMON_INTERCEPTORS_INIT; 678 679 // Intercept mem* functions. 680 ASAN_INTERCEPT_FUNC(memcmp); 681 ASAN_INTERCEPT_FUNC(memmove); 682 ASAN_INTERCEPT_FUNC(memset); 683 if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { 684 ASAN_INTERCEPT_FUNC(memcpy); 685 } 686 687 // Intercept str* functions. 688 ASAN_INTERCEPT_FUNC(strcat); // NOLINT 689 ASAN_INTERCEPT_FUNC(strchr); 690 ASAN_INTERCEPT_FUNC(strcpy); // NOLINT 691 ASAN_INTERCEPT_FUNC(strlen); 692 ASAN_INTERCEPT_FUNC(wcslen); 693 ASAN_INTERCEPT_FUNC(strncat); 694 ASAN_INTERCEPT_FUNC(strncpy); 695#if ASAN_INTERCEPT_STRDUP 696 ASAN_INTERCEPT_FUNC(strdup); 697#endif 698#if ASAN_INTERCEPT_STRNLEN 699 ASAN_INTERCEPT_FUNC(strnlen); 700#endif 701#if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 702 ASAN_INTERCEPT_FUNC(index); 703#endif 704 705 ASAN_INTERCEPT_FUNC(atoi); 706 ASAN_INTERCEPT_FUNC(atol); 707 ASAN_INTERCEPT_FUNC(strtol); 708#if ASAN_INTERCEPT_ATOLL_AND_STRTOLL 709 ASAN_INTERCEPT_FUNC(atoll); 710 ASAN_INTERCEPT_FUNC(strtoll); 711#endif 712 713#if ASAN_INTERCEPT_MLOCKX 714 // Intercept mlock/munlock. 715 ASAN_INTERCEPT_FUNC(mlock); 716 ASAN_INTERCEPT_FUNC(munlock); 717 ASAN_INTERCEPT_FUNC(mlockall); 718 ASAN_INTERCEPT_FUNC(munlockall); 719#endif 720 721 // Intecept signal- and jump-related functions. 722 ASAN_INTERCEPT_FUNC(longjmp); 723#if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 724 ASAN_INTERCEPT_FUNC(sigaction); 725 ASAN_INTERCEPT_FUNC(signal); 726#endif 727#if ASAN_INTERCEPT_SWAPCONTEXT 728 ASAN_INTERCEPT_FUNC(swapcontext); 729#endif 730#if ASAN_INTERCEPT__LONGJMP 731 ASAN_INTERCEPT_FUNC(_longjmp); 732#endif 733#if ASAN_INTERCEPT_SIGLONGJMP 734 ASAN_INTERCEPT_FUNC(siglongjmp); 735#endif 736 737 // Intercept exception handling functions. 738#if ASAN_INTERCEPT___CXA_THROW 739 INTERCEPT_FUNCTION(__cxa_throw); 740#endif 741 742 // Intercept threading-related functions 743#if ASAN_INTERCEPT_PTHREAD_CREATE 744 ASAN_INTERCEPT_FUNC(pthread_create); 745#endif 746 747 // Intercept atexit function. 748#if ASAN_INTERCEPT___CXA_ATEXIT 749 ASAN_INTERCEPT_FUNC(__cxa_atexit); 750#endif 751 752 // Some Windows-specific interceptors. 753#if SANITIZER_WINDOWS 754 InitializeWindowsInterceptors(); 755#endif 756 757 if (flags()->verbosity > 0) { 758 Report("AddressSanitizer: libc interceptors initialized\n"); 759 } 760} 761 762} // namespace __asan 763