13cab2bb3Spatrick //===-- asan_internal.h -----------------------------------------*- C++ -*-===// 23cab2bb3Spatrick // 33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63cab2bb3Spatrick // 73cab2bb3Spatrick //===----------------------------------------------------------------------===// 83cab2bb3Spatrick // 93cab2bb3Spatrick // This file is a part of AddressSanitizer, an address sanity checker. 103cab2bb3Spatrick // 113cab2bb3Spatrick // ASan-private header which defines various general utilities. 123cab2bb3Spatrick //===----------------------------------------------------------------------===// 133cab2bb3Spatrick #ifndef ASAN_INTERNAL_H 143cab2bb3Spatrick #define ASAN_INTERNAL_H 153cab2bb3Spatrick 163cab2bb3Spatrick #include "asan_flags.h" 173cab2bb3Spatrick #include "asan_interface_internal.h" 183cab2bb3Spatrick #include "sanitizer_common/sanitizer_common.h" 193cab2bb3Spatrick #include "sanitizer_common/sanitizer_internal_defs.h" 203cab2bb3Spatrick #include "sanitizer_common/sanitizer_libc.h" 21*810390e3Srobert #include "sanitizer_common/sanitizer_stacktrace.h" 223cab2bb3Spatrick 233cab2bb3Spatrick #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 24*810390e3Srobert # error \ 25*810390e3Srobert "The AddressSanitizer run-time should not be instrumented by AddressSanitizer" 263cab2bb3Spatrick #endif 273cab2bb3Spatrick 283cab2bb3Spatrick // Build-time configuration options. 293cab2bb3Spatrick 303cab2bb3Spatrick // If set, asan will intercept C++ exception api call(s). 313cab2bb3Spatrick #ifndef ASAN_HAS_EXCEPTIONS 323cab2bb3Spatrick # define ASAN_HAS_EXCEPTIONS 1 333cab2bb3Spatrick #endif 343cab2bb3Spatrick 353cab2bb3Spatrick // If set, values like allocator chunk size, as well as defaults for some flags 363cab2bb3Spatrick // will be changed towards less memory overhead. 373cab2bb3Spatrick #ifndef ASAN_LOW_MEMORY 38d89ec533Spatrick # if SANITIZER_IOS || SANITIZER_ANDROID 393cab2bb3Spatrick # define ASAN_LOW_MEMORY 1 403cab2bb3Spatrick # else 413cab2bb3Spatrick # define ASAN_LOW_MEMORY 0 423cab2bb3Spatrick # endif 433cab2bb3Spatrick #endif 443cab2bb3Spatrick 453cab2bb3Spatrick #ifndef ASAN_DYNAMIC 463cab2bb3Spatrick # ifdef PIC 473cab2bb3Spatrick # define ASAN_DYNAMIC 1 483cab2bb3Spatrick # else 493cab2bb3Spatrick # define ASAN_DYNAMIC 0 503cab2bb3Spatrick # endif 513cab2bb3Spatrick #endif 523cab2bb3Spatrick 533cab2bb3Spatrick // All internal functions in asan reside inside the __asan namespace 543cab2bb3Spatrick // to avoid namespace collisions with the user programs. 553cab2bb3Spatrick // Separate namespace also makes it simpler to distinguish the asan run-time 563cab2bb3Spatrick // functions from the instrumented user code in a profile. 573cab2bb3Spatrick namespace __asan { 583cab2bb3Spatrick 593cab2bb3Spatrick class AsanThread; 603cab2bb3Spatrick using __sanitizer::StackTrace; 613cab2bb3Spatrick 623cab2bb3Spatrick void AsanInitFromRtl(); 633cab2bb3Spatrick 643cab2bb3Spatrick // asan_win.cpp 653cab2bb3Spatrick void InitializePlatformExceptionHandlers(); 663cab2bb3Spatrick // Returns whether an address is a valid allocated system heap block. 673cab2bb3Spatrick // 'addr' must point to the beginning of the block. 683cab2bb3Spatrick bool IsSystemHeapAddress(uptr addr); 693cab2bb3Spatrick 703cab2bb3Spatrick // asan_rtl.cpp 713cab2bb3Spatrick void PrintAddressSpaceLayout(); 723cab2bb3Spatrick void NORETURN ShowStatsAndAbort(); 733cab2bb3Spatrick 743cab2bb3Spatrick // asan_shadow_setup.cpp 753cab2bb3Spatrick void InitializeShadowMemory(); 763cab2bb3Spatrick 773cab2bb3Spatrick // asan_malloc_linux.cpp / asan_malloc_mac.cpp 783cab2bb3Spatrick void ReplaceSystemMalloc(); 793cab2bb3Spatrick 80d89ec533Spatrick // asan_linux.cpp / asan_mac.cpp / asan_win.cpp 813cab2bb3Spatrick uptr FindDynamicShadowStart(); 823cab2bb3Spatrick void *AsanDoesNotSupportStaticLinkage(); 833cab2bb3Spatrick void AsanCheckDynamicRTPrereqs(); 843cab2bb3Spatrick void AsanCheckIncompatibleRT(); 853cab2bb3Spatrick 861f9cb04fSpatrick // Unpoisons platform-specific stacks. 871f9cb04fSpatrick // Returns true if all stacks have been unpoisoned. 881f9cb04fSpatrick bool PlatformUnpoisonStacks(); 891f9cb04fSpatrick 901f9cb04fSpatrick // asan_rtl.cpp 911f9cb04fSpatrick // Unpoison a region containing a stack. 921f9cb04fSpatrick // Performs a sanity check and warns if the bounds don't look right. 931f9cb04fSpatrick // The warning contains the type string to identify the stack type. 941f9cb04fSpatrick void UnpoisonStack(uptr bottom, uptr top, const char *type); 951f9cb04fSpatrick 963cab2bb3Spatrick // asan_thread.cpp 973cab2bb3Spatrick AsanThread *CreateMainThread(); 983cab2bb3Spatrick 993cab2bb3Spatrick // Support function for __asan_(un)register_image_globals. Searches for the 1003cab2bb3Spatrick // loaded image containing `needle' and then enumerates all global metadata 1013cab2bb3Spatrick // structures declared in that image, applying `op' (e.g., 1023cab2bb3Spatrick // __asan_(un)register_globals) to them. 1033cab2bb3Spatrick typedef void (*globals_op_fptr)(__asan_global *, uptr); 1043cab2bb3Spatrick void AsanApplyToGlobals(globals_op_fptr op, const void *needle); 1053cab2bb3Spatrick 1063cab2bb3Spatrick void AsanOnDeadlySignal(int, void *siginfo, void *context); 1073cab2bb3Spatrick 108*810390e3Srobert void SignContextStack(void *context); 1093cab2bb3Spatrick void ReadContextStack(void *context, uptr *stack, uptr *ssize); 1103cab2bb3Spatrick void StopInitOrderChecking(); 1113cab2bb3Spatrick 1123cab2bb3Spatrick // Wrapper for TLS/TSD. 1133cab2bb3Spatrick void AsanTSDInit(void (*destructor)(void *tsd)); 1143cab2bb3Spatrick void *AsanTSDGet(); 1153cab2bb3Spatrick void AsanTSDSet(void *tsd); 1163cab2bb3Spatrick void PlatformTSDDtor(void *tsd); 1173cab2bb3Spatrick 1183cab2bb3Spatrick void AppendToErrorMessageBuffer(const char *buffer); 1193cab2bb3Spatrick 1203cab2bb3Spatrick void *AsanDlSymNext(const char *sym); 1213cab2bb3Spatrick 1223cab2bb3Spatrick // Returns `true` iff most of ASan init process should be skipped due to the 1233cab2bb3Spatrick // ASan library being loaded via `dlopen()`. Platforms may perform any 1243cab2bb3Spatrick // `dlopen()` specific initialization inside this function. 1253cab2bb3Spatrick bool HandleDlopenInit(); 1263cab2bb3Spatrick 127*810390e3Srobert void InstallAtExitCheckLeaks(); 128*810390e3Srobert 1293cab2bb3Spatrick #define ASAN_ON_ERROR() \ 130*810390e3Srobert if (&__asan_on_error) \ 131*810390e3Srobert __asan_on_error() 1323cab2bb3Spatrick 1333cab2bb3Spatrick extern int asan_inited; 1343cab2bb3Spatrick // Used to avoid infinite recursion in __asan_init(). 1353cab2bb3Spatrick extern bool asan_init_is_running; 136*810390e3Srobert extern bool replace_intrin_cached; 1373cab2bb3Spatrick extern void (*death_callback)(void); 138*810390e3Srobert // These magic values are written to shadow for better error 139*810390e3Srobert // reporting. 1403cab2bb3Spatrick const int kAsanHeapLeftRedzoneMagic = 0xfa; 1413cab2bb3Spatrick const int kAsanHeapFreeMagic = 0xfd; 1423cab2bb3Spatrick const int kAsanStackLeftRedzoneMagic = 0xf1; 1433cab2bb3Spatrick const int kAsanStackMidRedzoneMagic = 0xf2; 1443cab2bb3Spatrick const int kAsanStackRightRedzoneMagic = 0xf3; 1453cab2bb3Spatrick const int kAsanStackAfterReturnMagic = 0xf5; 1463cab2bb3Spatrick const int kAsanInitializationOrderMagic = 0xf6; 1473cab2bb3Spatrick const int kAsanUserPoisonedMemoryMagic = 0xf7; 1483cab2bb3Spatrick const int kAsanContiguousContainerOOBMagic = 0xfc; 1493cab2bb3Spatrick const int kAsanStackUseAfterScopeMagic = 0xf8; 1503cab2bb3Spatrick const int kAsanGlobalRedzoneMagic = 0xf9; 1513cab2bb3Spatrick const int kAsanInternalHeapMagic = 0xfe; 1523cab2bb3Spatrick const int kAsanArrayCookieMagic = 0xac; 1533cab2bb3Spatrick const int kAsanIntraObjectRedzone = 0xbb; 1543cab2bb3Spatrick const int kAsanAllocaLeftMagic = 0xca; 1553cab2bb3Spatrick const int kAsanAllocaRightMagic = 0xcb; 1563cab2bb3Spatrick 1573cab2bb3Spatrick static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 1583cab2bb3Spatrick static const uptr kRetiredStackFrameMagic = 0x45E0360E; 1593cab2bb3Spatrick 1603cab2bb3Spatrick } // namespace __asan 1613cab2bb3Spatrick 1623cab2bb3Spatrick #endif // ASAN_INTERNAL_H 163