Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 1 | //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===// |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 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 shared between AddressSanitizer and ThreadSanitizer. |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 11 | // It contains macro used in run-time libraries code. |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef SANITIZER_DEFS_H |
| 14 | #define SANITIZER_DEFS_H |
| 15 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 16 | #include "sanitizer_platform.h" |
Evgeniy Stepanov | 83cb787 | 2013-03-19 13:54:41 +0000 | [diff] [blame] | 17 | |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 18 | #if SANITIZER_WINDOWS |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 19 | // FIXME find out what we need on Windows. __declspec(dllexport) ? |
| 20 | # define SANITIZER_INTERFACE_ATTRIBUTE |
| 21 | # define SANITIZER_WEAK_ATTRIBUTE |
| 22 | #elif defined(SANITIZER_GO) |
| 23 | # define SANITIZER_INTERFACE_ATTRIBUTE |
| 24 | # define SANITIZER_WEAK_ATTRIBUTE |
| 25 | #else |
| 26 | # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) |
| 27 | # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak)) |
| 28 | #endif |
| 29 | |
Alexey Samsonov | 78b580f | 2013-04-03 13:22:54 +0000 | [diff] [blame] | 30 | #if SANITIZER_LINUX && !defined(SANITIZER_GO) |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 31 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 1 |
| 32 | #else |
| 33 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 0 |
| 34 | #endif |
| 35 | |
Kostya Serebryany | e31eca9 | 2013-02-15 12:00:24 +0000 | [diff] [blame] | 36 | // GCC does not understand __has_feature |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 37 | #if !defined(__has_feature) |
| 38 | # define __has_feature(x) 0 |
| 39 | #endif |
| 40 | |
| 41 | // For portability reasons we do not include stddef.h, stdint.h or any other |
| 42 | // system header, but we do need some basic types that are not defined |
| 43 | // in a portable way by the language itself. |
| 44 | namespace __sanitizer { |
| 45 | |
| 46 | #if defined(_WIN64) |
| 47 | // 64-bit Windows uses LLP64 data model. |
| 48 | typedef unsigned long long uptr; // NOLINT |
| 49 | typedef signed long long sptr; // NOLINT |
| 50 | #else |
| 51 | typedef unsigned long uptr; // NOLINT |
| 52 | typedef signed long sptr; // NOLINT |
| 53 | #endif // defined(_WIN64) |
| 54 | #if defined(__x86_64__) |
| 55 | // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use |
| 56 | // 64-bit pointer to unwind stack frame. |
| 57 | typedef unsigned long long uhwptr; // NOLINT |
| 58 | #else |
| 59 | typedef uptr uhwptr; // NOLINT |
| 60 | #endif |
| 61 | typedef unsigned char u8; |
| 62 | typedef unsigned short u16; // NOLINT |
| 63 | typedef unsigned int u32; |
| 64 | typedef unsigned long long u64; // NOLINT |
| 65 | typedef signed char s8; |
| 66 | typedef signed short s16; // NOLINT |
| 67 | typedef signed int s32; |
| 68 | typedef signed long long s64; // NOLINT |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 69 | typedef int fd_t; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 70 | |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 71 | // WARNING: OFF_T may be different from OS type off_t, depending on the value of |
| 72 | // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls |
| 73 | // like pread and mmap, as opposed to pread64 and mmap64. |
| 74 | // Mac and Linux/x86-64 are special. |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 75 | #if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__)) |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 76 | typedef u64 OFF_T; |
| 77 | #else |
| 78 | typedef uptr OFF_T; |
| 79 | #endif |
| 80 | typedef u64 OFF64_T; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 81 | } // namespace __sanitizer |
| 82 | |
| 83 | extern "C" { |
| 84 | // Tell the tools to write their reports to "path.<pid>" instead of stderr. |
| 85 | void __sanitizer_set_report_path(const char *path) |
| 86 | SANITIZER_INTERFACE_ATTRIBUTE; |
| 87 | |
| 88 | // Tell the tools to write their reports to given file descriptor instead of |
| 89 | // stderr. |
| 90 | void __sanitizer_set_report_fd(int fd) |
| 91 | SANITIZER_INTERFACE_ATTRIBUTE; |
| 92 | |
| 93 | // Notify the tools that the sandbox is going to be turned on. The reserved |
| 94 | // parameter will be used in the future to hold a structure with functions |
| 95 | // that the tools may call to bypass the sandbox. |
| 96 | void __sanitizer_sandbox_on_notify(void *reserved) |
| 97 | SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE; |
Kostya Serebryany | 2673fd8 | 2013-02-06 12:36:49 +0000 | [diff] [blame] | 98 | |
| 99 | // This function is called by the tool when it has just finished reporting |
| 100 | // an error. 'error_summary' is a one-line string that summarizes |
| 101 | // the error message. This function can be overridden by the client. |
| 102 | void __sanitizer_report_error_summary(const char *error_summary) |
| 103 | SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE; |
Evgeniy Stepanov | 250f221 | 2013-01-30 13:12:08 +0000 | [diff] [blame] | 104 | } // extern "C" |
| 105 | |
| 106 | |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 107 | using namespace __sanitizer; // NOLINT |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 108 | // ----------- ATTENTION ------------- |
| 109 | // This header should NOT include any other headers to avoid portability issues. |
| 110 | |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 111 | // Common defs. |
Timur Iskhodzhanov | 40e1668 | 2013-04-12 09:37:20 +0000 | [diff] [blame] | 112 | #define INLINE inline |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 113 | #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE |
| 114 | #define WEAK SANITIZER_WEAK_ATTRIBUTE |
| 115 | |
| 116 | // Platform-specific defs. |
Dmitry Vyukov | bfa45e1 | 2012-11-06 12:47:42 +0000 | [diff] [blame] | 117 | #if defined(_MSC_VER) |
Timur Iskhodzhanov | 40e1668 | 2013-04-12 09:37:20 +0000 | [diff] [blame] | 118 | # define ALWAYS_INLINE __forceinline |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 119 | // FIXME(timurrrr): do we need this on Windows? |
| 120 | # define ALIAS(x) |
| 121 | # define ALIGNED(x) __declspec(align(x)) |
Alexey Samsonov | 15503b0 | 2012-06-06 13:37:02 +0000 | [diff] [blame] | 122 | # define FORMAT(f, a) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 123 | # define NOINLINE __declspec(noinline) |
| 124 | # define NORETURN __declspec(noreturn) |
| 125 | # define THREADLOCAL __declspec(thread) |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 126 | # define NOTHROW |
Dmitry Vyukov | e2462f7 | 2012-11-06 12:54:16 +0000 | [diff] [blame] | 127 | # define LIKELY(x) (x) |
| 128 | # define UNLIKELY(x) (x) |
| 129 | # define UNUSED |
| 130 | # define USED |
Dmitry Vyukov | b1bd208 | 2013-01-11 16:40:24 +0000 | [diff] [blame] | 131 | # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ |
Dmitry Vyukov | bfa45e1 | 2012-11-06 12:47:42 +0000 | [diff] [blame] | 132 | #else // _MSC_VER |
Kostya Serebryany | d475aa8 | 2013-03-29 09:44:16 +0000 | [diff] [blame] | 133 | # define ALWAYS_INLINE inline __attribute__((always_inline)) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 134 | # define ALIAS(x) __attribute__((alias(x))) |
Timur Iskhodzhanov | 7c9ffde | 2013-06-04 08:25:17 +0000 | [diff] [blame] | 135 | // Please only use the ALIGNED macro before the type. |
| 136 | // Using ALIGNED after the variable declaration is not portable! |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 137 | # define ALIGNED(x) __attribute__((aligned(x))) |
Alexey Samsonov | 15503b0 | 2012-06-06 13:37:02 +0000 | [diff] [blame] | 138 | # define FORMAT(f, a) __attribute__((format(printf, f, a))) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 139 | # define NOINLINE __attribute__((noinline)) |
| 140 | # define NORETURN __attribute__((noreturn)) |
| 141 | # define THREADLOCAL __thread |
Dmitry Vyukov | 97daee8 | 2012-11-06 12:49:22 +0000 | [diff] [blame] | 142 | # define NOTHROW throw() |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 143 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
| 144 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
Alexey Samsonov | 2ea9787 | 2012-06-14 14:02:32 +0000 | [diff] [blame] | 145 | # define UNUSED __attribute__((unused)) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 146 | # define USED __attribute__((used)) |
Dmitry Vyukov | 0c7f61c | 2013-01-16 14:35:13 +0000 | [diff] [blame] | 147 | # if defined(__i386__) || defined(__x86_64__) |
| 148 | // __builtin_prefetch(x) generates prefetchnt0 on x86 |
| 149 | # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x)) |
| 150 | # else |
| 151 | # define PREFETCH(x) __builtin_prefetch(x) |
| 152 | # endif |
Dmitry Vyukov | e2462f7 | 2012-11-06 12:54:16 +0000 | [diff] [blame] | 153 | #endif // _MSC_VER |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 154 | |
Evgeniy Stepanov | 7202e79 | 2013-06-04 14:06:16 +0000 | [diff] [blame] | 155 | // Unaligned versions of basic types. |
| 156 | typedef ALIGNED(1) u16 uu16; |
| 157 | typedef ALIGNED(1) u32 uu32; |
| 158 | typedef ALIGNED(1) u64 uu64; |
| 159 | typedef ALIGNED(1) s16 us16; |
| 160 | typedef ALIGNED(1) s32 us32; |
| 161 | typedef ALIGNED(1) s64 us64; |
| 162 | |
Evgeniy Stepanov | 30e110e | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 163 | #if SANITIZER_WINDOWS |
Dmitry Vyukov | bb19294 | 2012-11-06 12:50:13 +0000 | [diff] [blame] | 164 | typedef unsigned long DWORD; // NOLINT |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 165 | typedef DWORD thread_return_t; |
| 166 | # define THREAD_CALLING_CONV __stdcall |
| 167 | #else // _WIN32 |
| 168 | typedef void* thread_return_t; |
| 169 | # define THREAD_CALLING_CONV |
| 170 | #endif // _WIN32 |
| 171 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); |
| 172 | |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 173 | #if __LP64__ || defined(_WIN64) |
| 174 | # define SANITIZER_WORDSIZE 64 |
| 175 | #else |
| 176 | # define SANITIZER_WORDSIZE 32 |
| 177 | #endif |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 178 | |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 179 | // NOTE: Functions below must be defined in each run-time. |
| 180 | namespace __sanitizer { |
| 181 | void NORETURN Die(); |
| 182 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
| 183 | u64 v1, u64 v2); |
| 184 | } // namespace __sanitizer |
| 185 | |
| 186 | // Check macro |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 187 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 188 | if (!(expr)) { \ |
| 189 | RawWrite(msg); \ |
| 190 | Die(); \ |
| 191 | } \ |
| 192 | } while (0) |
| 193 | |
| 194 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 195 | |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 196 | #define CHECK_IMPL(c1, op, c2) \ |
| 197 | do { \ |
| 198 | __sanitizer::u64 v1 = (u64)(c1); \ |
| 199 | __sanitizer::u64 v2 = (u64)(c2); \ |
| 200 | if (!(v1 op v2)) \ |
| 201 | __sanitizer::CheckFailed(__FILE__, __LINE__, \ |
| 202 | "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ |
| 203 | } while (false) \ |
| 204 | /**/ |
| 205 | |
| 206 | #define CHECK(a) CHECK_IMPL((a), !=, 0) |
| 207 | #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) |
| 208 | #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) |
| 209 | #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) |
| 210 | #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) |
| 211 | #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) |
| 212 | #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) |
| 213 | |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 214 | #if TSAN_DEBUG |
| 215 | #define DCHECK(a) CHECK(a) |
| 216 | #define DCHECK_EQ(a, b) CHECK_EQ(a, b) |
| 217 | #define DCHECK_NE(a, b) CHECK_NE(a, b) |
| 218 | #define DCHECK_LT(a, b) CHECK_LT(a, b) |
| 219 | #define DCHECK_LE(a, b) CHECK_LE(a, b) |
| 220 | #define DCHECK_GT(a, b) CHECK_GT(a, b) |
| 221 | #define DCHECK_GE(a, b) CHECK_GE(a, b) |
| 222 | #else |
| 223 | #define DCHECK(a) |
| 224 | #define DCHECK_EQ(a, b) |
| 225 | #define DCHECK_NE(a, b) |
| 226 | #define DCHECK_LT(a, b) |
| 227 | #define DCHECK_LE(a, b) |
| 228 | #define DCHECK_GT(a, b) |
| 229 | #define DCHECK_GE(a, b) |
| 230 | #endif |
| 231 | |
Alexey Samsonov | 45418d1 | 2012-10-09 08:42:07 +0000 | [diff] [blame] | 232 | #define UNREACHABLE(msg) do { \ |
| 233 | CHECK(0 && msg); \ |
| 234 | Die(); \ |
Kostya Serebryany | 5759d92 | 2012-10-16 04:50:32 +0000 | [diff] [blame] | 235 | } while (0) |
Alexey Samsonov | 45418d1 | 2012-10-09 08:42:07 +0000 | [diff] [blame] | 236 | |
| 237 | #define UNIMPLEMENTED() UNREACHABLE("unimplemented") |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 238 | |
Kostya Serebryany | c375657 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 239 | #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__) |
| 240 | |
Kostya Serebryany | 4c2ddda | 2012-08-28 11:54:51 +0000 | [diff] [blame] | 241 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 242 | |
Kostya Serebryany | c375657 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 243 | #define IMPL_PASTE(a, b) a##b |
| 244 | #define IMPL_COMPILER_ASSERT(pred, line) \ |
Alexey Samsonov | d7ed1f0 | 2012-09-11 10:31:28 +0000 | [diff] [blame] | 245 | typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1] |
Kostya Serebryany | c375657 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 246 | |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 247 | // Limits for integral types. We have to redefine it in case we don't |
| 248 | // have stdint.h (like in Visual Studio 9). |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 249 | #undef __INT64_C |
| 250 | #undef __UINT64_C |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 251 | #if SANITIZER_WORDSIZE == 64 |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 252 | # define __INT64_C(c) c ## L |
| 253 | # define __UINT64_C(c) c ## UL |
| 254 | #else |
| 255 | # define __INT64_C(c) c ## LL |
| 256 | # define __UINT64_C(c) c ## ULL |
Kostya Serebryany | 5af39e5 | 2012-11-21 12:38:58 +0000 | [diff] [blame] | 257 | #endif // SANITIZER_WORDSIZE == 64 |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 258 | #undef INT32_MIN |
| 259 | #define INT32_MIN (-2147483647-1) |
| 260 | #undef INT32_MAX |
| 261 | #define INT32_MAX (2147483647) |
| 262 | #undef UINT32_MAX |
| 263 | #define UINT32_MAX (4294967295U) |
| 264 | #undef INT64_MIN |
| 265 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) |
| 266 | #undef INT64_MAX |
| 267 | #define INT64_MAX (__INT64_C(9223372036854775807)) |
| 268 | #undef UINT64_MAX |
| 269 | #define UINT64_MAX (__UINT64_C(18446744073709551615)) |
| 270 | |
Alexey Samsonov | 70e177e | 2012-08-27 09:30:58 +0000 | [diff] [blame] | 271 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 272 | |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 273 | #if !defined(_MSC_VER) || defined(__clang__) |
Kostya Serebryany | 1b5ea8f | 2012-08-28 14:11:57 +0000 | [diff] [blame] | 274 | # define GET_CALLER_PC() (uptr)__builtin_return_address(0) |
| 275 | # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) |
| 276 | #else |
Kostya Serebryany | 80acccf | 2012-08-28 15:25:07 +0000 | [diff] [blame] | 277 | extern "C" void* _ReturnAddress(void); |
| 278 | # pragma intrinsic(_ReturnAddress) |
Kostya Serebryany | 1b5ea8f | 2012-08-28 14:11:57 +0000 | [diff] [blame] | 279 | # define GET_CALLER_PC() (uptr)_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() (uptr)0xDEADBEEF |
| 284 | #endif |
| 285 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 286 | #define HANDLE_EINTR(res, f) \ |
| 287 | { \ |
| 288 | int rverrno; \ |
| 289 | do { \ |
| 290 | res = (f); \ |
| 291 | } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \ |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 294 | #endif // SANITIZER_DEFS_H |