| 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 | |
| Kostya Serebryany | 16e0075 | 2012-05-31 13:42:53 +0000 | [diff] [blame^] | 17 | #include "sanitizer_common/sanitizer_libc.h" |
| Kostya Serebryany | b3cedf9 | 2012-05-29 12:18:18 +0000 | [diff] [blame] | 18 | |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 19 | #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) |
| Kostya Serebryany | d6567c5 | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 20 | # error "This operating system is not supported by AddressSanitizer" |
| 21 | #endif |
| 22 | |
| Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 23 | #include <stddef.h> // for size_t, uintptr_t, etc. |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 24 | |
| Kostya Serebryany | 8582208 | 2012-01-30 20:55:02 +0000 | [diff] [blame] | 25 | #if defined(_WIN32) |
| Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 26 | # if defined(__clang__) |
| 27 | typedef int intptr_t; |
| 28 | typedef unsigned int uintptr_t; |
| 29 | # endif |
| 30 | |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 31 | // There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t. |
| 32 | typedef unsigned __int8 uint8_t; |
| 33 | typedef unsigned __int16 uint16_t; |
| 34 | typedef unsigned __int32 uint32_t; |
| 35 | typedef unsigned __int64 uint64_t; |
| 36 | typedef __int8 int8_t; |
| 37 | typedef __int16 int16_t; |
| 38 | typedef __int32 int32_t; |
| 39 | typedef __int64 int64_t; |
| Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 40 | typedef unsigned long DWORD; // NOLINT |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 41 | |
| Alexander Potapenko | 71d3b39 | 2012-02-08 14:14:18 +0000 | [diff] [blame] | 42 | extern "C" void* _ReturnAddress(void); |
| 43 | # pragma intrinsic(_ReturnAddress) |
| 44 | |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 45 | # define ALIAS(x) // TODO(timurrrr): do we need this on Windows? |
| 46 | # define ALIGNED(x) __declspec(align(x)) |
| 47 | # define NOINLINE __declspec(noinline) |
| Timur Iskhodzhanov | 23bd2bb | 2012-03-13 16:12:03 +0000 | [diff] [blame] | 48 | # define NORETURN __declspec(noreturn) |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 49 | |
| 50 | # define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win? |
| 51 | #else // defined(_WIN32) |
| Kostya Serebryany | 8582208 | 2012-01-30 20:55:02 +0000 | [diff] [blame] | 52 | # include <stdint.h> // for __WORDSIZE |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 53 | |
| 54 | # define ALIAS(x) __attribute__((alias(x))) |
| 55 | # define ALIGNED(x) __attribute__((aligned(x))) |
| 56 | # define NOINLINE __attribute__((noinline)) |
| Alexey Samsonov | 95d6b33 | 2012-03-20 10:14:55 +0000 | [diff] [blame] | 57 | # define NORETURN __attribute__((noreturn)) |
| Alexey Samsonov | adf2b03 | 2012-02-03 08:37:19 +0000 | [diff] [blame] | 58 | |
| 59 | # define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) |
| 60 | #endif // defined(_WIN32) |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 61 | |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 62 | // 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] | 63 | // compiler built-ins __LP64__ and _WIN64. |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 64 | #ifndef __WORDSIZE |
| Kostya Serebryany | 669f543 | 2012-01-31 18:13:50 +0000 | [diff] [blame] | 65 | #if __LP64__ || defined(_WIN64) |
| Daniel Dunbar | 4616633 | 2011-12-02 01:32:27 +0000 | [diff] [blame] | 66 | #define __WORDSIZE 64 |
| 67 | #else |
| 68 | #define __WORDSIZE 32 |
| 69 | #endif |
| 70 | #endif |
| 71 | |
| Alexey Samsonov | e4092f6 | 2012-02-22 12:54:04 +0000 | [diff] [blame] | 72 | // Limits for integral types. We have to redefine it in case we don't |
| 73 | // have stdint.h (like in Visual Studio 9). |
| 74 | #if __WORDSIZE == 64 |
| 75 | # define __INT64_C(c) c ## L |
| 76 | # define __UINT64_C(c) c ## UL |
| 77 | #else |
| 78 | # define __INT64_C(c) c ## LL |
| 79 | # define __UINT64_C(c) c ## ULL |
| 80 | #endif // __WORDSIZE == 64 |
| Alexey Samsonov | bfc694d | 2012-02-22 16:12:46 +0000 | [diff] [blame] | 81 | #undef INT32_MIN |
| 82 | #define INT32_MIN (-2147483647-1) |
| 83 | #undef INT32_MAX |
| 84 | #define INT32_MAX (2147483647) |
| 85 | #undef UINT32_MAX |
| 86 | #define UINT32_MAX (4294967295U) |
| 87 | #undef INT64_MIN |
| 88 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) |
| 89 | #undef INT64_MAX |
| 90 | #define INT64_MAX (__INT64_C(9223372036854775807)) |
| 91 | #undef UINT64_MAX |
| 92 | #define UINT64_MAX (__UINT64_C(18446744073709551615)) |
| Alexey Samsonov | e4092f6 | 2012-02-22 12:54:04 +0000 | [diff] [blame] | 93 | |
| Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 94 | #define ASAN_DEFAULT_FAILURE_EXITCODE 1 |
| 95 | |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 96 | #if defined(__linux__) |
| 97 | # define ASAN_LINUX 1 |
| 98 | #else |
| 99 | # define ASAN_LINUX 0 |
| 100 | #endif |
| 101 | |
| 102 | #if defined(__APPLE__) |
| 103 | # define ASAN_MAC 1 |
| 104 | #else |
| 105 | # define ASAN_MAC 0 |
| 106 | #endif |
| 107 | |
| 108 | #if defined(_WIN32) |
| 109 | # define ASAN_WINDOWS 1 |
| 110 | #else |
| 111 | # define ASAN_WINDOWS 0 |
| 112 | #endif |
| 113 | |
| Alexey Samsonov | 34a3202 | 2012-03-26 09:07:29 +0000 | [diff] [blame] | 114 | #define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) |
| 115 | |
| Alexander Potapenko | c836523 | 2012-01-27 10:52:37 +0000 | [diff] [blame] | 116 | #if !defined(__has_feature) |
| 117 | #define __has_feature(x) 0 |
| 118 | #endif |
| 119 | |
| Kostya Serebryany | 850a49e | 2012-04-06 20:36:18 +0000 | [diff] [blame] | 120 | #if __has_feature(address_sanitizer) |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 121 | # error "The AddressSanitizer run-time should not be" |
| 122 | " instrumented by AddressSanitizer" |
| 123 | #endif |
| 124 | |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 125 | // Build-time configuration options. |
| 126 | |
| Kostya Serebryany | c6f2223 | 2011-12-08 18:30:42 +0000 | [diff] [blame] | 127 | // If set, asan will install its own SEGV signal handler. |
| 128 | #ifndef ASAN_NEEDS_SEGV |
| 129 | # define ASAN_NEEDS_SEGV 1 |
| 130 | #endif |
| 131 | |
| 132 | // If set, asan will intercept C++ exception api call(s). |
| 133 | #ifndef ASAN_HAS_EXCEPTIONS |
| 134 | # define ASAN_HAS_EXCEPTIONS 1 |
| 135 | #endif |
| 136 | |
| 137 | // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET |
| 138 | // provided by the instrumented objects. Otherwise constants are used. |
| 139 | #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET |
| 140 | # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 |
| 141 | #endif |
| 142 | |
| Evgeniy Stepanov | 8ae44ac | 2012-02-27 13:07:29 +0000 | [diff] [blame] | 143 | // If set, values like allocator chunk size, as well as defaults for some flags |
| 144 | // will be changed towards less memory overhead. |
| 145 | #ifndef ASAN_LOW_MEMORY |
| 146 | # define ASAN_LOW_MEMORY 0 |
| 147 | #endif |
| 148 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 149 | // All internal functions in asan reside inside the __asan namespace |
| 150 | // to avoid namespace collisions with the user programs. |
| 151 | // Seperate namespace also makes it simpler to distinguish the asan run-time |
| 152 | // functions from the instrumented user code in a profile. |
| 153 | namespace __asan { |
| 154 | |
| 155 | class AsanThread; |
| 156 | struct AsanStackTrace; |
| 157 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 158 | // asan_rtl.cc |
| Timur Iskhodzhanov | 23bd2bb | 2012-03-13 16:12:03 +0000 | [diff] [blame] | 159 | void NORETURN CheckFailed(const char *cond, const char *file, int line); |
| Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 160 | void NORETURN ShowStatsAndAbort(); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 161 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 162 | // asan_globals.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 163 | bool DescribeAddrIfGlobal(uintptr_t addr); |
| 164 | |
| Alexey Samsonov | 4d5f98d | 2012-04-06 08:21:08 +0000 | [diff] [blame] | 165 | void ReplaceOperatorsNewAndDelete(); |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 166 | // asan_malloc_linux.cc / asan_malloc_mac.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 167 | void ReplaceSystemMalloc(); |
| 168 | |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 169 | void OutOfMemoryMessageAndDie(const char *mem_type, size_t size); |
| 170 | |
| Alexander Potapenko | f73a6a3 | 2012-02-13 17:09:40 +0000 | [diff] [blame] | 171 | // asan_linux.cc / asan_mac.cc / asan_win.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 172 | void *AsanDoesNotSupportStaticLinkage(); |
| Alexander Potapenko | f73a6a3 | 2012-02-13 17:09:40 +0000 | [diff] [blame] | 173 | bool AsanShadowRangeIsAvailable(); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 174 | int AsanOpenReadonly(const char* filename); |
| Alexander Potapenko | 1e316d7 | 2012-01-13 12:59:48 +0000 | [diff] [blame] | 175 | const char *AsanGetEnv(const char *name); |
| Alexander Potapenko | 99d17eb | 2012-02-22 09:11:55 +0000 | [diff] [blame] | 176 | void AsanDumpProcessMap(); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 177 | |
| Kostya Serebryany | a874fe5 | 2011-12-28 23:28:54 +0000 | [diff] [blame] | 178 | void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size); |
| 179 | void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size); |
| 180 | void *AsanMprotect(uintptr_t fixed_addr, size_t size); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 181 | void *AsanMmapSomewhereOrDie(size_t size, const char *where); |
| 182 | void AsanUnmapOrDie(void *ptr, size_t size); |
| 183 | |
| Kostya Serebryany | ef14ff6 | 2012-01-06 02:12:25 +0000 | [diff] [blame] | 184 | void AsanDisableCoreDumper(); |
| Kostya Serebryany | 9107c26 | 2012-01-06 19:11:09 +0000 | [diff] [blame] | 185 | 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] | 186 | |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 187 | size_t AsanRead(int fd, void *buf, size_t count); |
| 188 | size_t AsanWrite(int fd, const void *buf, size_t count); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 189 | int AsanClose(int fd); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 190 | |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 191 | bool AsanInterceptsSignal(int signum); |
| Alexander Potapenko | f03d8af | 2012-04-05 10:54:52 +0000 | [diff] [blame] | 192 | void SetAlternateSignalStack(); |
| 193 | void UnsetAlternateSignalStack(); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 194 | void InstallSignalHandlers(); |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 195 | int GetPid(); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 196 | uintptr_t GetThreadSelf(); |
| Kostya Serebryany | dde7c33 | 2012-01-11 02:39:16 +0000 | [diff] [blame] | 197 | int AtomicInc(int *a); |
| Kostya Serebryany | f1e82b8 | 2012-04-05 15:55:09 +0000 | [diff] [blame] | 198 | uint16_t AtomicExchange(uint16_t *a, uint16_t new_val); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 199 | |
| 200 | // Wrapper for TLS/TSD. |
| Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 201 | void AsanTSDInit(void (*destructor)(void *tsd)); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 202 | void *AsanTSDGet(); |
| 203 | void AsanTSDSet(void *tsd); |
| Kostya Serebryany | 4803ab9 | 2012-01-09 18:53:15 +0000 | [diff] [blame] | 204 | |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 205 | // Opens the file 'file_name" and reads up to 'max_len' bytes. |
| 206 | // The resulting buffer is mmaped and stored in '*buff'. |
| 207 | // The size of the mmaped region is stored in '*buff_size', |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 208 | // Returns the number of read bytes or 0 if file can not be opened. |
| 209 | size_t ReadFileToBuffer(const char *file_name, char **buff, |
| 210 | size_t *buff_size, size_t max_len); |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 211 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 212 | // asan_printf.cc |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 213 | void RawWrite(const char *buffer); |
| Alexander Potapenko | a0935fa | 2012-02-08 11:45:09 +0000 | [diff] [blame] | 214 | int SNPrintf(char *buffer, size_t length, const char *format, ...); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 215 | void Printf(const char *format, ...); |
| Kostya Serebryany | df499b4 | 2012-01-05 00:44:33 +0000 | [diff] [blame] | 216 | int SScanf(const char *str, const char *format, ...); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 217 | void Report(const char *format, ...); |
| 218 | |
| Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 219 | // Don't use std::min and std::max, to minimize dependency on libstdc++. |
| 220 | template<class T> T Min(T a, T b) { return a < b ? a : b; } |
| 221 | template<class T> T Max(T a, T b) { return a > b ? a : b; } |
| 222 | |
| Kostya Serebryany | 25c7178 | 2012-03-10 01:30:01 +0000 | [diff] [blame] | 223 | void SortArray(uintptr_t *array, size_t size); |
| 224 | |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 225 | // asan_poisoning.cc |
| 226 | // Poisons the shadow memory for "size" bytes starting from "addr". |
| 227 | void PoisonShadow(uintptr_t addr, size_t size, uint8_t value); |
| 228 | // Poisons the shadow memory for "redzone_size" bytes starting from |
| 229 | // "addr + size". |
| 230 | void PoisonShadowPartialRightRedzone(uintptr_t addr, |
| 231 | uintptr_t size, |
| 232 | uintptr_t redzone_size, |
| 233 | uint8_t value); |
| 234 | |
| Alexey Samsonov | 38dd4ed | 2012-03-20 10:54:40 +0000 | [diff] [blame] | 235 | // Platfrom-specific options. |
| 236 | #ifdef __APPLE__ |
| 237 | bool PlatformHasDifferentMemcpyAndMemmove(); |
| 238 | # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ |
| 239 | (PlatformHasDifferentMemcpyAndMemmove()) |
| 240 | #else |
| 241 | # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true |
| 242 | #endif // __APPLE__ |
| 243 | |
| Alexander Potapenko | 62f10e7 | 2012-05-28 16:21:19 +0000 | [diff] [blame] | 244 | extern size_t FLAG_quarantine_size; |
| 245 | extern int64_t FLAG_demangle; |
| 246 | extern bool FLAG_symbolize; |
| 247 | extern int64_t FLAG_v; |
| 248 | extern size_t FLAG_redzone; |
| 249 | extern int64_t FLAG_debug; |
| 250 | extern bool FLAG_poison_shadow; |
| 251 | extern int64_t FLAG_report_globals; |
| 252 | extern size_t FLAG_malloc_context_size; |
| 253 | extern bool FLAG_replace_str; |
| 254 | extern bool FLAG_replace_intrin; |
| 255 | extern bool FLAG_replace_cfallocator; |
| 256 | extern bool FLAG_fast_unwind; |
| 257 | extern bool FLAG_use_fake_stack; |
| 258 | extern size_t FLAG_max_malloc_fill_size; |
| 259 | extern int64_t FLAG_exitcode; |
| 260 | extern bool FLAG_allow_user_poisoning; |
| 261 | extern int64_t FLAG_sleep_before_dying; |
| 262 | extern bool FLAG_handle_segv; |
| 263 | extern bool FLAG_use_sigaltstack; |
| 264 | extern bool FLAG_check_malloc_usable_size; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 265 | |
| 266 | extern int asan_inited; |
| 267 | // Used to avoid infinite recursion in __asan_init(). |
| 268 | extern bool asan_init_is_running; |
| 269 | |
| 270 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 271 | |
| Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 272 | void NORETURN AsanDie(); |
| Kostya Serebryany | e1fe0fd | 2012-02-13 21:24:29 +0000 | [diff] [blame] | 273 | void SleepForSeconds(int seconds); |
| Timur Iskhodzhanov | b55c88d | 2012-03-13 16:29:25 +0000 | [diff] [blame] | 274 | void NORETURN Exit(int exitcode); |
| Kostya Serebryany | f8e6fee | 2012-04-06 01:27:11 +0000 | [diff] [blame] | 275 | void NORETURN Abort(); |
| Alexey Samsonov | b823e3c | 2012-02-22 14:07:06 +0000 | [diff] [blame] | 276 | int Atexit(void (*function)(void)); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 277 | |
| 278 | #define CHECK(cond) do { if (!(cond)) { \ |
| 279 | CheckFailed(#cond, __FILE__, __LINE__); \ |
| 280 | }}while(0) |
| 281 | |
| 282 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 283 | if (!(expr)) { \ |
| 284 | RawWrite(msg); \ |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 285 | AsanDie(); \ |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 286 | } \ |
| 287 | } while (0) |
| 288 | |
| 289 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 290 | |
| 291 | #define UNIMPLEMENTED() CHECK("unimplemented" && 0) |
| 292 | |
| 293 | #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 294 | |
| 295 | const size_t kWordSize = __WORDSIZE / 8; |
| 296 | const size_t kWordSizeInBits = 8 * kWordSize; |
| 297 | const size_t kPageSizeBits = 12; |
| 298 | const size_t kPageSize = 1UL << kPageSizeBits; |
| 299 | |
| Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 300 | #if !defined(_WIN32) || defined(__clang__) |
| Kostya Serebryany | 1c83ae3 | 2012-02-07 18:23:54 +0000 | [diff] [blame] | 301 | # define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0) |
| 302 | # define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0) |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 303 | #else |
| Kostya Serebryany | 1c83ae3 | 2012-02-07 18:23:54 +0000 | [diff] [blame] | 304 | # define GET_CALLER_PC() (uintptr_t)_ReturnAddress() |
| 305 | // CaptureStackBackTrace doesn't need to know BP on Windows. |
| 306 | // FIXME: This macro is still used when printing error reports though it's not |
| 307 | // clear if the BP value is needed in the ASan reports on Windows. |
| 308 | # define GET_CURRENT_FRAME() (uintptr_t)0xDEADBEEF |
| Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 309 | #endif |
| 310 | |
| 311 | #ifndef _WIN32 |
| 312 | const size_t kMmapGranularity = kPageSize; |
| 313 | # define THREAD_CALLING_CONV |
| 314 | typedef void* thread_return_t; |
| 315 | #else |
| 316 | const size_t kMmapGranularity = 1UL << 16; |
| Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 317 | # define THREAD_CALLING_CONV __stdcall |
| 318 | typedef DWORD thread_return_t; |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 319 | |
| 320 | # ifndef ASAN_USE_EXTERNAL_SYMBOLIZER |
| Timur Iskhodzhanov | 8c505ef | 2012-05-21 14:25:36 +0000 | [diff] [blame] | 321 | # define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize |
| 322 | bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size); |
| Timur Iskhodzhanov | 3e81fe4 | 2012-02-09 17:20:14 +0000 | [diff] [blame] | 323 | # endif |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 324 | #endif |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 325 | |
| Timur Iskhodzhanov | 600972e | 2012-02-24 15:28:43 +0000 | [diff] [blame] | 326 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); |
| 327 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 328 | // These magic values are written to shadow for better error reporting. |
| 329 | const int kAsanHeapLeftRedzoneMagic = 0xfa; |
| 330 | const int kAsanHeapRightRedzoneMagic = 0xfb; |
| 331 | const int kAsanHeapFreeMagic = 0xfd; |
| 332 | const int kAsanStackLeftRedzoneMagic = 0xf1; |
| 333 | const int kAsanStackMidRedzoneMagic = 0xf2; |
| 334 | const int kAsanStackRightRedzoneMagic = 0xf3; |
| 335 | const int kAsanStackPartialRedzoneMagic = 0xf4; |
| 336 | const int kAsanStackAfterReturnMagic = 0xf5; |
| 337 | const int kAsanUserPoisonedMemoryMagic = 0xf7; |
| 338 | const int kAsanGlobalRedzoneMagic = 0xf9; |
| Kostya Serebryany | 6b30e2c | 2011-12-15 17:41:30 +0000 | [diff] [blame] | 339 | const int kAsanInternalHeapMagic = 0xfe; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 340 | |
| 341 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 342 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 343 | |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 344 | // --------------------------- Bit twiddling ------- {{{1 |
| 345 | inline bool IsPowerOfTwo(size_t x) { |
| 346 | return (x & (x - 1)) == 0; |
| 347 | } |
| 348 | |
| 349 | inline size_t RoundUpTo(size_t size, size_t boundary) { |
| 350 | CHECK(IsPowerOfTwo(boundary)); |
| 351 | return (size + boundary - 1) & ~(boundary - 1); |
| 352 | } |
| 353 | |
| Kostya Serebryany | b89567c | 2011-12-02 21:02:20 +0000 | [diff] [blame] | 354 | // -------------------------- LowLevelAllocator ----- {{{1 |
| 355 | // A simple low-level memory allocator for internal use. |
| 356 | class LowLevelAllocator { |
| 357 | public: |
| 358 | explicit LowLevelAllocator(LinkerInitialized) {} |
| 359 | // 'size' must be a power of two. |
| 360 | // Requires an external lock. |
| 361 | void *Allocate(size_t size); |
| 362 | private: |
| 363 | char *allocated_end_; |
| 364 | char *allocated_current_; |
| 365 | }; |
| 366 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 367 | } // namespace __asan |
| 368 | |
| 369 | #endif // ASAN_INTERNAL_H |