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