| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 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 | |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 17 | #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) |
| Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 18 | # error "This operating system is not supported by AddressSanitizer" |
| 19 | #endif |
| 20 | |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 21 | #include <stdlib.h> // for size_t, uintptr_t, etc. |
| 22 | |
| Kostya Serebryany | 8582208 | 2012-01-30 20:55:02 +0000 | [diff] [blame] | 23 | #if defined(_WIN32) |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 24 | // There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t. |
| 25 | typedef unsigned __int8 uint8_t; |
| 26 | typedef unsigned __int16 uint16_t; |
| 27 | typedef unsigned __int32 uint32_t; |
| 28 | typedef unsigned __int64 uint64_t; |
| 29 | typedef __int8 int8_t; |
| 30 | typedef __int16 int16_t; |
| 31 | typedef __int32 int32_t; |
| 32 | typedef __int64 int64_t; |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 33 | |
| 34 | # define ALIAS(x) // TODO(timurrrr): do we need this on Windows? |
| 35 | # define ALIGNED(x) __declspec(align(x)) |
| 36 | # define NOINLINE __declspec(noinline) |
| 37 | |
| 38 | # define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win? |
| 39 | #else // defined(_WIN32) |
| Kostya Serebryany | 8582208 | 2012-01-30 20:55:02 +0000 | [diff] [blame] | 40 | # include <stdint.h> // for __WORDSIZE |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 41 | |
| 42 | # define ALIAS(x) __attribute__((alias(x))) |
| 43 | # define ALIGNED(x) __attribute__((aligned(x))) |
| 44 | # define NOINLINE __attribute__((noinline)) |
| 45 | |
| 46 | # define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) |
| 47 | #endif // defined(_WIN32) |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 48 | |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 49 | // If __WORDSIZE was undefined by the platform, define it in terms of the |
| Kostya Serebryany | 669f543 | 2012-01-31 18:13:50 +0000 | [diff] [blame] | 50 | // compiler built-ins __LP64__ and _WIN64. |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 51 | #ifndef __WORDSIZE |
| Kostya Serebryany | 669f543 | 2012-01-31 18:13:50 +0000 | [diff] [blame] | 52 | #if __LP64__ || defined(_WIN64) |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 53 | #define __WORDSIZE 64 |
| 54 | #else |
| 55 | #define __WORDSIZE 32 |
| 56 | #endif |
| 57 | #endif |
| 58 | |
| Alexander Potapenko | c836523 | 2012-01-27 10:52:37 +0000 | [diff] [blame] | 59 | #if !defined(__has_feature) |
| 60 | #define __has_feature(x) 0 |
| 61 | #endif |
| 62 | |
| Kostya Serebryany | 13ebae6 | 2011-12-27 21:57:12 +0000 | [diff] [blame] | 63 | #if defined(__has_feature) && __has_feature(address_sanitizer) |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 64 | # error "The AddressSanitizer run-time should not be" |
| 65 | " instrumented by AddressSanitizer" |
| 66 | #endif |
| 67 | |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 68 | // Build-time configuration options. |
| 69 | |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 70 | // If set, asan will install its own SEGV signal handler. |
| 71 | #ifndef ASAN_NEEDS_SEGV |
| 72 | # define ASAN_NEEDS_SEGV 1 |
| 73 | #endif |
| 74 | |
| 75 | // If set, asan will intercept C++ exception api call(s). |
| 76 | #ifndef ASAN_HAS_EXCEPTIONS |
| 77 | # define ASAN_HAS_EXCEPTIONS 1 |
| 78 | #endif |
| 79 | |
| 80 | // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET |
| 81 | // provided by the instrumented objects. Otherwise constants are used. |
| 82 | #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET |
| 83 | # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 |
| 84 | #endif |
| 85 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 86 | // All internal functions in asan reside inside the __asan namespace |
| 87 | // to avoid namespace collisions with the user programs. |
| 88 | // Seperate namespace also makes it simpler to distinguish the asan run-time |
| 89 | // functions from the instrumented user code in a profile. |
| 90 | namespace __asan { |
| 91 | |
| 92 | class AsanThread; |
| 93 | struct AsanStackTrace; |
| 94 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 95 | // asan_rtl.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | void CheckFailed(const char *cond, const char *file, int line); |
| 97 | void ShowStatsAndAbort(); |
| 98 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 99 | // asan_globals.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 100 | bool DescribeAddrIfGlobal(uintptr_t addr); |
| 101 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 102 | // asan_malloc_linux.cc / asan_malloc_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 103 | void ReplaceSystemMalloc(); |
| 104 | |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 105 | void OutOfMemoryMessageAndDie(const char *mem_type, size_t size); |
| 106 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 107 | // asan_linux.cc / asan_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 108 | void *AsanDoesNotSupportStaticLinkage(); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 109 | int AsanOpenReadonly(const char* filename); |
| Alexander Potapenko | 1e316d7 | 2012-01-13 12:59:48 +0000 | [diff] [blame] | 110 | const char *AsanGetEnv(const char *name); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 111 | |
| Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 112 | void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size); |
| 113 | void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size); |
| 114 | void *AsanMprotect(uintptr_t fixed_addr, size_t size); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 115 | void *AsanMmapSomewhereOrDie(size_t size, const char *where); |
| 116 | void AsanUnmapOrDie(void *ptr, size_t size); |
| 117 | |
| Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 118 | void AsanDisableCoreDumper(); |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 119 | void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp); |
| Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 120 | |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 121 | size_t AsanRead(int fd, void *buf, size_t count); |
| 122 | size_t AsanWrite(int fd, const void *buf, size_t count); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 123 | int AsanClose(int fd); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 124 | |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 125 | bool AsanInterceptsSignal(int signum); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 126 | void InstallSignalHandlers(); |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 127 | int GetPid(); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 128 | uintptr_t GetThreadSelf(); |
| Kostya Serebryany | dde7c33 | 2012-01-11 02:39:16 +0000 | [diff] [blame] | 129 | int AtomicInc(int *a); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 130 | |
| 131 | // Wrapper for TLS/TSD. |
| Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame^] | 132 | void AsanTSDInit(void (*destructor)(void *tsd)); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 133 | void *AsanTSDGet(); |
| 134 | void AsanTSDSet(void *tsd); |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 135 | |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 136 | // Opens the file 'file_name" and reads up to 'max_len' bytes. |
| 137 | // The resulting buffer is mmaped and stored in '*buff'. |
| 138 | // The size of the mmaped region is stored in '*buff_size', |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 139 | // Returns the number of read bytes or 0 if file can not be opened. |
| 140 | size_t ReadFileToBuffer(const char *file_name, char **buff, |
| 141 | size_t *buff_size, size_t max_len); |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 142 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 143 | // asan_printf.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 144 | void RawWrite(const char *buffer); |
| 145 | int SNPrint(char *buffer, size_t length, const char *format, ...); |
| 146 | void Printf(const char *format, ...); |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 147 | int SScanf(const char *str, const char *format, ...); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 148 | void Report(const char *format, ...); |
| 149 | |
| Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 150 | // Don't use std::min and std::max, to minimize dependency on libstdc++. |
| 151 | template<class T> T Min(T a, T b) { return a < b ? a : b; } |
| 152 | template<class T> T Max(T a, T b) { return a > b ? a : b; } |
| 153 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 154 | // asan_poisoning.cc |
| 155 | // Poisons the shadow memory for "size" bytes starting from "addr". |
| 156 | void PoisonShadow(uintptr_t addr, size_t size, uint8_t value); |
| 157 | // Poisons the shadow memory for "redzone_size" bytes starting from |
| 158 | // "addr + size". |
| 159 | void PoisonShadowPartialRightRedzone(uintptr_t addr, |
| 160 | uintptr_t size, |
| 161 | uintptr_t redzone_size, |
| 162 | uint8_t value); |
| 163 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 164 | extern size_t FLAG_quarantine_size; |
| 165 | extern int FLAG_demangle; |
| 166 | extern bool FLAG_symbolize; |
| 167 | extern int FLAG_v; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 168 | extern size_t FLAG_redzone; |
| 169 | extern int FLAG_debug; |
| 170 | extern bool FLAG_poison_shadow; |
| 171 | extern int FLAG_report_globals; |
| 172 | extern size_t FLAG_malloc_context_size; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 173 | extern bool FLAG_replace_str; |
| 174 | extern bool FLAG_replace_intrin; |
| 175 | extern bool FLAG_replace_cfallocator; |
| 176 | extern bool FLAG_fast_unwind; |
| 177 | extern bool FLAG_use_fake_stack; |
| 178 | extern size_t FLAG_max_malloc_fill_size; |
| 179 | extern int FLAG_exitcode; |
| 180 | extern bool FLAG_allow_user_poisoning; |
| Kostya Serebryany | cb00d13 | 2012-01-31 00:52:18 +0000 | [diff] [blame] | 181 | extern int FLAG_sleep_before_dying; |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 182 | extern bool FLAG_handle_segv; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 183 | |
| 184 | extern int asan_inited; |
| 185 | // Used to avoid infinite recursion in __asan_init(). |
| 186 | extern bool asan_init_is_running; |
| 187 | |
| 188 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 189 | |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 190 | void AsanDie(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 191 | |
| 192 | #define CHECK(cond) do { if (!(cond)) { \ |
| 193 | CheckFailed(#cond, __FILE__, __LINE__); \ |
| 194 | }}while(0) |
| 195 | |
| 196 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 197 | if (!(expr)) { \ |
| 198 | RawWrite(msg); \ |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 199 | AsanDie(); \ |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 200 | } \ |
| 201 | } while (0) |
| 202 | |
| 203 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 204 | |
| 205 | #define UNIMPLEMENTED() CHECK("unimplemented" && 0) |
| 206 | |
| 207 | #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 208 | |
| 209 | const size_t kWordSize = __WORDSIZE / 8; |
| 210 | const size_t kWordSizeInBits = 8 * kWordSize; |
| 211 | const size_t kPageSizeBits = 12; |
| 212 | const size_t kPageSize = 1UL << kPageSizeBits; |
| 213 | |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 214 | #ifndef _WIN32 |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 215 | #define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0) |
| 216 | #define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0) |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 217 | #else |
| 218 | // TODO(timurrrr): implement. |
| 219 | #define GET_CALLER_PC() (uintptr_t)0 |
| 220 | #define GET_CURRENT_FRAME() (uintptr_t)0 |
| 221 | #endif |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 222 | |
| 223 | #define GET_BP_PC_SP \ |
| 224 | uintptr_t bp = GET_CURRENT_FRAME(); \ |
| 225 | uintptr_t pc = GET_CALLER_PC(); \ |
| 226 | uintptr_t local_stack; \ |
| 227 | uintptr_t sp = (uintptr_t)&local_stack; |
| 228 | |
| 229 | // These magic values are written to shadow for better error reporting. |
| 230 | const int kAsanHeapLeftRedzoneMagic = 0xfa; |
| 231 | const int kAsanHeapRightRedzoneMagic = 0xfb; |
| 232 | const int kAsanHeapFreeMagic = 0xfd; |
| 233 | const int kAsanStackLeftRedzoneMagic = 0xf1; |
| 234 | const int kAsanStackMidRedzoneMagic = 0xf2; |
| 235 | const int kAsanStackRightRedzoneMagic = 0xf3; |
| 236 | const int kAsanStackPartialRedzoneMagic = 0xf4; |
| 237 | const int kAsanStackAfterReturnMagic = 0xf5; |
| 238 | const int kAsanUserPoisonedMemoryMagic = 0xf7; |
| 239 | const int kAsanGlobalRedzoneMagic = 0xf9; |
| Kostya Serebryany | 6b30e2c | 2011-12-15 17:41:30 +0000 | [diff] [blame] | 240 | const int kAsanInternalHeapMagic = 0xfe; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 241 | |
| 242 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 243 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 244 | |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 245 | // --------------------------- Bit twiddling ------- {{{1 |
| 246 | inline bool IsPowerOfTwo(size_t x) { |
| 247 | return (x & (x - 1)) == 0; |
| 248 | } |
| 249 | |
| 250 | inline size_t RoundUpTo(size_t size, size_t boundary) { |
| 251 | CHECK(IsPowerOfTwo(boundary)); |
| 252 | return (size + boundary - 1) & ~(boundary - 1); |
| 253 | } |
| 254 | |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 255 | // -------------------------- LowLevelAllocator ----- {{{1 |
| 256 | // A simple low-level memory allocator for internal use. |
| 257 | class LowLevelAllocator { |
| 258 | public: |
| 259 | explicit LowLevelAllocator(LinkerInitialized) {} |
| 260 | // 'size' must be a power of two. |
| 261 | // Requires an external lock. |
| 262 | void *Allocate(size_t size); |
| 263 | private: |
| 264 | char *allocated_end_; |
| 265 | char *allocated_current_; |
| 266 | }; |
| 267 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 268 | } // namespace __asan |
| 269 | |
| 270 | #endif // ASAN_INTERNAL_H |