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 | |
Chandler Carruth | d865fec | 2012-08-29 02:27:54 +0000 | [diff] [blame] | 16 | #include "sanitizer/common_interface_defs.h" |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 17 | using namespace __sanitizer; // NOLINT |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 18 | // ----------- ATTENTION ------------- |
| 19 | // This header should NOT include any other headers to avoid portability issues. |
| 20 | |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 21 | // Common defs. |
| 22 | #define INLINE static inline |
| 23 | #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE |
| 24 | #define WEAK SANITIZER_WEAK_ATTRIBUTE |
| 25 | |
| 26 | // Platform-specific defs. |
Dmitry Vyukov | bfa45e1 | 2012-11-06 12:47:42 +0000 | [diff] [blame^] | 27 | #if defined(_MSC_VER) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 28 | typedef unsigned long DWORD; // NOLINT |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 29 | # define ALWAYS_INLINE __declspec(forceinline) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 30 | // FIXME(timurrrr): do we need this on Windows? |
| 31 | # define ALIAS(x) |
| 32 | # define ALIGNED(x) __declspec(align(x)) |
Alexey Samsonov | 15503b0 | 2012-06-06 13:37:02 +0000 | [diff] [blame] | 33 | # define FORMAT(f, a) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 34 | # define NOINLINE __declspec(noinline) |
| 35 | # define NORETURN __declspec(noreturn) |
| 36 | # define THREADLOCAL __declspec(thread) |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 37 | # define NOTHROW |
Dmitry Vyukov | bfa45e1 | 2012-11-06 12:47:42 +0000 | [diff] [blame^] | 38 | #else // _MSC_VER |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 39 | # define ALWAYS_INLINE __attribute__((always_inline)) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 40 | # define ALIAS(x) __attribute__((alias(x))) |
| 41 | # define ALIGNED(x) __attribute__((aligned(x))) |
Alexey Samsonov | 15503b0 | 2012-06-06 13:37:02 +0000 | [diff] [blame] | 42 | # define FORMAT(f, a) __attribute__((format(printf, f, a))) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 43 | # define NOINLINE __attribute__((noinline)) |
| 44 | # define NORETURN __attribute__((noreturn)) |
| 45 | # define THREADLOCAL __thread |
Dmitry Vyukov | d51a1a1 | 2012-06-27 21:00:23 +0000 | [diff] [blame] | 46 | # ifdef __cplusplus |
| 47 | # define NOTHROW throw() |
| 48 | # else |
| 49 | # define NOTHROW __attribute__((__nothrow__)) |
| 50 | #endif |
Dmitry Vyukov | bfa45e1 | 2012-11-06 12:47:42 +0000 | [diff] [blame^] | 51 | #endif // _MSC_VER |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 52 | |
| 53 | // We have no equivalent of these on Windows. |
| 54 | #ifndef _WIN32 |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 55 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
| 56 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
Alexey Samsonov | 2ea9787 | 2012-06-14 14:02:32 +0000 | [diff] [blame] | 57 | # define UNUSED __attribute__((unused)) |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 58 | # define USED __attribute__((used)) |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 61 | #if defined(_WIN32) |
| 62 | typedef DWORD thread_return_t; |
| 63 | # define THREAD_CALLING_CONV __stdcall |
| 64 | #else // _WIN32 |
| 65 | typedef void* thread_return_t; |
| 66 | # define THREAD_CALLING_CONV |
| 67 | #endif // _WIN32 |
| 68 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); |
| 69 | |
Alexey Samsonov | 0a4c906 | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 70 | // If __WORDSIZE was undefined by the platform, define it in terms of the |
| 71 | // compiler built-ins __LP64__ and _WIN64. |
| 72 | #ifndef __WORDSIZE |
| 73 | # if __LP64__ || defined(_WIN64) |
| 74 | # define __WORDSIZE 64 |
| 75 | # else |
| 76 | # define __WORDSIZE 32 |
| 77 | # endif |
| 78 | #endif // __WORDSIZE |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 79 | |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 80 | // NOTE: Functions below must be defined in each run-time. |
| 81 | namespace __sanitizer { |
| 82 | void NORETURN Die(); |
| 83 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
| 84 | u64 v1, u64 v2); |
| 85 | } // namespace __sanitizer |
| 86 | |
| 87 | // Check macro |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 88 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 89 | if (!(expr)) { \ |
| 90 | RawWrite(msg); \ |
| 91 | Die(); \ |
| 92 | } \ |
| 93 | } while (0) |
| 94 | |
| 95 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 96 | |
Alexey Samsonov | 15a7761 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 97 | #define CHECK_IMPL(c1, op, c2) \ |
| 98 | do { \ |
| 99 | __sanitizer::u64 v1 = (u64)(c1); \ |
| 100 | __sanitizer::u64 v2 = (u64)(c2); \ |
| 101 | if (!(v1 op v2)) \ |
| 102 | __sanitizer::CheckFailed(__FILE__, __LINE__, \ |
| 103 | "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ |
| 104 | } while (false) \ |
| 105 | /**/ |
| 106 | |
| 107 | #define CHECK(a) CHECK_IMPL((a), !=, 0) |
| 108 | #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) |
| 109 | #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) |
| 110 | #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) |
| 111 | #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) |
| 112 | #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) |
| 113 | #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) |
| 114 | |
Dmitry Vyukov | fce5bd4 | 2012-06-29 16:58:33 +0000 | [diff] [blame] | 115 | #if TSAN_DEBUG |
| 116 | #define DCHECK(a) CHECK(a) |
| 117 | #define DCHECK_EQ(a, b) CHECK_EQ(a, b) |
| 118 | #define DCHECK_NE(a, b) CHECK_NE(a, b) |
| 119 | #define DCHECK_LT(a, b) CHECK_LT(a, b) |
| 120 | #define DCHECK_LE(a, b) CHECK_LE(a, b) |
| 121 | #define DCHECK_GT(a, b) CHECK_GT(a, b) |
| 122 | #define DCHECK_GE(a, b) CHECK_GE(a, b) |
| 123 | #else |
| 124 | #define DCHECK(a) |
| 125 | #define DCHECK_EQ(a, b) |
| 126 | #define DCHECK_NE(a, b) |
| 127 | #define DCHECK_LT(a, b) |
| 128 | #define DCHECK_LE(a, b) |
| 129 | #define DCHECK_GT(a, b) |
| 130 | #define DCHECK_GE(a, b) |
| 131 | #endif |
| 132 | |
Alexey Samsonov | 45418d1 | 2012-10-09 08:42:07 +0000 | [diff] [blame] | 133 | #define UNREACHABLE(msg) do { \ |
| 134 | CHECK(0 && msg); \ |
| 135 | Die(); \ |
Kostya Serebryany | 5759d92 | 2012-10-16 04:50:32 +0000 | [diff] [blame] | 136 | } while (0) |
Alexey Samsonov | 45418d1 | 2012-10-09 08:42:07 +0000 | [diff] [blame] | 137 | |
| 138 | #define UNIMPLEMENTED() UNREACHABLE("unimplemented") |
Alexey Samsonov | 8c53e54 | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 139 | |
Kostya Serebryany | c375657 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 140 | #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__) |
| 141 | |
Kostya Serebryany | 4c2ddda | 2012-08-28 11:54:51 +0000 | [diff] [blame] | 142 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
| 143 | |
Kostya Serebryany | c375657 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 144 | #define IMPL_PASTE(a, b) a##b |
| 145 | #define IMPL_COMPILER_ASSERT(pred, line) \ |
Alexey Samsonov | d7ed1f0 | 2012-09-11 10:31:28 +0000 | [diff] [blame] | 146 | typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1] |
Kostya Serebryany | c375657 | 2012-06-21 10:04:36 +0000 | [diff] [blame] | 147 | |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 148 | // Limits for integral types. We have to redefine it in case we don't |
| 149 | // have stdint.h (like in Visual Studio 9). |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 150 | #undef __INT64_C |
| 151 | #undef __UINT64_C |
Alexey Samsonov | c925697 | 2012-06-15 13:09:52 +0000 | [diff] [blame] | 152 | #if __WORDSIZE == 64 |
| 153 | # define __INT64_C(c) c ## L |
| 154 | # define __UINT64_C(c) c ## UL |
| 155 | #else |
| 156 | # define __INT64_C(c) c ## LL |
| 157 | # define __UINT64_C(c) c ## ULL |
| 158 | #endif // __WORDSIZE == 64 |
| 159 | #undef INT32_MIN |
| 160 | #define INT32_MIN (-2147483647-1) |
| 161 | #undef INT32_MAX |
| 162 | #define INT32_MAX (2147483647) |
| 163 | #undef UINT32_MAX |
| 164 | #define UINT32_MAX (4294967295U) |
| 165 | #undef INT64_MIN |
| 166 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) |
| 167 | #undef INT64_MAX |
| 168 | #define INT64_MAX (__INT64_C(9223372036854775807)) |
| 169 | #undef UINT64_MAX |
| 170 | #define UINT64_MAX (__UINT64_C(18446744073709551615)) |
| 171 | |
Alexey Samsonov | 70e177e | 2012-08-27 09:30:58 +0000 | [diff] [blame] | 172 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
| 173 | |
Alexey Samsonov | b46941a | 2012-09-24 11:43:40 +0000 | [diff] [blame] | 174 | #if !defined(_MSC_VER) || defined(__clang__) |
Kostya Serebryany | 1b5ea8f | 2012-08-28 14:11:57 +0000 | [diff] [blame] | 175 | # define GET_CALLER_PC() (uptr)__builtin_return_address(0) |
| 176 | # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) |
| 177 | #else |
Kostya Serebryany | 80acccf | 2012-08-28 15:25:07 +0000 | [diff] [blame] | 178 | extern "C" void* _ReturnAddress(void); |
| 179 | # pragma intrinsic(_ReturnAddress) |
Kostya Serebryany | 1b5ea8f | 2012-08-28 14:11:57 +0000 | [diff] [blame] | 180 | # define GET_CALLER_PC() (uptr)_ReturnAddress() |
| 181 | // CaptureStackBackTrace doesn't need to know BP on Windows. |
| 182 | // FIXME: This macro is still used when printing error reports though it's not |
| 183 | // clear if the BP value is needed in the ASan reports on Windows. |
| 184 | # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF |
| 185 | #endif |
| 186 | |
Evgeniy Stepanov | 3334e12 | 2012-10-02 13:41:40 +0000 | [diff] [blame] | 187 | #define HANDLE_EINTR(res, f) { \ |
| 188 | do { \ |
| 189 | res = (f); \ |
| 190 | } while (res == -1 && errno == EINTR); \ |
| 191 | } |
| 192 | |
Kostya Serebryany | 9aead37 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 193 | #endif // SANITIZER_DEFS_H |