Home | History | Annotate | Download | only in asan
      1 //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
      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 // ASan-private header which defines various general utilities.
     13 //===----------------------------------------------------------------------===//
     14 #ifndef ASAN_INTERNAL_H
     15 #define ASAN_INTERNAL_H
     16 
     17 #include "asan_flags.h"
     18 #include "asan_interface_internal.h"
     19 #include "sanitizer_common/sanitizer_common.h"
     20 #include "sanitizer_common/sanitizer_internal_defs.h"
     21 #include "sanitizer_common/sanitizer_stacktrace.h"
     22 #include "sanitizer_common/sanitizer_libc.h"
     23 
     24 #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
     25 # error "This operating system is not supported by AddressSanitizer"
     26 #endif
     27 
     28 #define ASAN_DEFAULT_FAILURE_EXITCODE 1
     29 
     30 #if defined(__linux__)
     31 # define ASAN_LINUX   1
     32 #else
     33 # define ASAN_LINUX   0
     34 #endif
     35 
     36 #if defined(__APPLE__)
     37 # define ASAN_MAC     1
     38 #else
     39 # define ASAN_MAC     0
     40 #endif
     41 
     42 #if defined(_WIN32)
     43 # define ASAN_WINDOWS 1
     44 #else
     45 # define ASAN_WINDOWS 0
     46 #endif
     47 
     48 #if defined(__ANDROID__) || defined(ANDROID)
     49 # define ASAN_ANDROID 1
     50 #else
     51 # define ASAN_ANDROID 0
     52 #endif
     53 
     54 
     55 #define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
     56 
     57 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
     58 # error "The AddressSanitizer run-time should not be"
     59         " instrumented by AddressSanitizer"
     60 #endif
     61 
     62 // Build-time configuration options.
     63 
     64 // If set, asan will install its own SEGV signal handler.
     65 #ifndef ASAN_NEEDS_SEGV
     66 # if ASAN_ANDROID == 1
     67 #  define ASAN_NEEDS_SEGV 0
     68 # else
     69 #  define ASAN_NEEDS_SEGV 1
     70 # endif
     71 #endif
     72 
     73 // If set, asan will intercept C++ exception api call(s).
     74 #ifndef ASAN_HAS_EXCEPTIONS
     75 # define ASAN_HAS_EXCEPTIONS 1
     76 #endif
     77 
     78 // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
     79 // provided by the instrumented objects. Otherwise constants are used.
     80 #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
     81 # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
     82 #endif
     83 
     84 // If set, values like allocator chunk size, as well as defaults for some flags
     85 // will be changed towards less memory overhead.
     86 #ifndef ASAN_LOW_MEMORY
     87 #if SANITIZER_WORDSIZE == 32
     88 #  define ASAN_LOW_MEMORY 1
     89 #else
     90 #  define ASAN_LOW_MEMORY 0
     91 # endif
     92 #endif
     93 
     94 #ifndef ASAN_USE_PREINIT_ARRAY
     95 # define ASAN_USE_PREINIT_ARRAY (ASAN_LINUX && !ASAN_ANDROID)
     96 #endif
     97 
     98 // All internal functions in asan reside inside the __asan namespace
     99 // to avoid namespace collisions with the user programs.
    100 // Seperate namespace also makes it simpler to distinguish the asan run-time
    101 // functions from the instrumented user code in a profile.
    102 namespace __asan {
    103 
    104 class AsanThread;
    105 using __sanitizer::StackTrace;
    106 
    107 // asan_rtl.cc
    108 void NORETURN ShowStatsAndAbort();
    109 
    110 void ReplaceOperatorsNewAndDelete();
    111 // asan_malloc_linux.cc / asan_malloc_mac.cc
    112 void ReplaceSystemMalloc();
    113 
    114 // asan_linux.cc / asan_mac.cc / asan_win.cc
    115 void *AsanDoesNotSupportStaticLinkage();
    116 
    117 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
    118 
    119 void MaybeReexec();
    120 bool AsanInterceptsSignal(int signum);
    121 void SetAlternateSignalStack();
    122 void UnsetAlternateSignalStack();
    123 void InstallSignalHandlers();
    124 void ReadContextStack(void *context, uptr *stack, uptr *ssize);
    125 void AsanPlatformThreadInit();
    126 
    127 // Wrapper for TLS/TSD.
    128 void AsanTSDInit(void (*destructor)(void *tsd));
    129 void *AsanTSDGet();
    130 void AsanTSDSet(void *tsd);
    131 
    132 void AppendToErrorMessageBuffer(const char *buffer);
    133 
    134 // asan_poisoning.cc
    135 // Poisons the shadow memory for "size" bytes starting from "addr".
    136 void PoisonShadow(uptr addr, uptr size, u8 value);
    137 // Poisons the shadow memory for "redzone_size" bytes starting from
    138 // "addr + size".
    139 void PoisonShadowPartialRightRedzone(uptr addr,
    140                                      uptr size,
    141                                      uptr redzone_size,
    142                                      u8 value);
    143 
    144 // Platfrom-specific options.
    145 #ifdef __APPLE__
    146 bool PlatformHasDifferentMemcpyAndMemmove();
    147 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
    148     (PlatformHasDifferentMemcpyAndMemmove())
    149 #else
    150 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
    151 #endif  // __APPLE__
    152 
    153 // Add convenient macro for interface functions that may be represented as
    154 // weak hooks.
    155 #define ASAN_MALLOC_HOOK(ptr, size) \
    156   if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size)
    157 #define ASAN_FREE_HOOK(ptr) \
    158   if (&__asan_free_hook) __asan_free_hook(ptr)
    159 #define ASAN_ON_ERROR() \
    160   if (&__asan_on_error) __asan_on_error()
    161 
    162 extern int asan_inited;
    163 // Used to avoid infinite recursion in __asan_init().
    164 extern bool asan_init_is_running;
    165 extern void (*death_callback)(void);
    166 
    167 // These magic values are written to shadow for better error reporting.
    168 const int kAsanHeapLeftRedzoneMagic = 0xfa;
    169 const int kAsanHeapRightRedzoneMagic = 0xfb;
    170 const int kAsanHeapFreeMagic = 0xfd;
    171 const int kAsanStackLeftRedzoneMagic = 0xf1;
    172 const int kAsanStackMidRedzoneMagic = 0xf2;
    173 const int kAsanStackRightRedzoneMagic = 0xf3;
    174 const int kAsanStackPartialRedzoneMagic = 0xf4;
    175 const int kAsanStackAfterReturnMagic = 0xf5;
    176 const int kAsanInitializationOrderMagic = 0xf6;
    177 const int kAsanUserPoisonedMemoryMagic = 0xf7;
    178 const int kAsanStackUseAfterScopeMagic = 0xf8;
    179 const int kAsanGlobalRedzoneMagic = 0xf9;
    180 const int kAsanInternalHeapMagic = 0xfe;
    181 
    182 static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
    183 static const uptr kRetiredStackFrameMagic = 0x45E0360E;
    184 
    185 }  // namespace __asan
    186 
    187 #endif  // ASAN_INTERNAL_H
    188