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