Alexey Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 1 | //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===// |
Kostya Serebryany | 1b71207 | 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 | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 11 | // It contains macro used in run-time libraries code. |
Kostya Serebryany | 1b71207 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef SANITIZER_DEFS_H |
| 14 | #define SANITIZER_DEFS_H |
| 15 | |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 16 | #include "sanitizer_interface_defs.h" |
| 17 | using namespace __sanitizer; // NOLINT |
Kostya Serebryany | 1b71207 | 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 | ef2e2cf | 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. |
Kostya Serebryany | 1b71207 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 27 | #if defined(_WIN32) |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 28 | typedef unsigned long DWORD; // NOLINT |
| 29 | // FIXME(timurrrr): do we need this on Windows? |
| 30 | # define ALIAS(x) |
| 31 | # define ALIGNED(x) __declspec(align(x)) |
Alexey Samsonov | 8bafdd0 | 2012-06-06 13:37:02 +0000 | [diff] [blame] | 32 | # define FORMAT(f, a) |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 33 | # define NOINLINE __declspec(noinline) |
| 34 | # define NORETURN __declspec(noreturn) |
| 35 | # define THREADLOCAL __declspec(thread) |
| 36 | #else // _WIN32 |
| 37 | # define ALIAS(x) __attribute__((alias(x))) |
| 38 | # define ALIGNED(x) __attribute__((aligned(x))) |
Alexey Samsonov | 8bafdd0 | 2012-06-06 13:37:02 +0000 | [diff] [blame] | 39 | # define FORMAT(f, a) __attribute__((format(printf, f, a))) |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 40 | # define NOINLINE __attribute__((noinline)) |
| 41 | # define NORETURN __attribute__((noreturn)) |
| 42 | # define THREADLOCAL __thread |
| 43 | #endif // _WIN32 |
| 44 | |
| 45 | // We have no equivalent of these on Windows. |
| 46 | #ifndef _WIN32 |
| 47 | # define ALWAYS_INLINE __attribute__((always_inline)) |
| 48 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
| 49 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
Alexey Samsonov | aac5d0c | 2012-06-14 14:02:32 +0000 | [diff] [blame] | 50 | # define UNUSED __attribute__((unused)) |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 51 | # define USED __attribute__((used)) |
Kostya Serebryany | 1b71207 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Alexey Samsonov | 40e5128 | 2012-06-15 07:29:14 +0000 | [diff] [blame^] | 54 | #if defined(_WIN32) |
| 55 | typedef DWORD thread_return_t; |
| 56 | # define THREAD_CALLING_CONV __stdcall |
| 57 | #else // _WIN32 |
| 58 | typedef void* thread_return_t; |
| 59 | # define THREAD_CALLING_CONV |
| 60 | #endif // _WIN32 |
| 61 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); |
| 62 | |
Alexey Samsonov | ef2e2cf | 2012-06-05 13:50:57 +0000 | [diff] [blame] | 63 | // If __WORDSIZE was undefined by the platform, define it in terms of the |
| 64 | // compiler built-ins __LP64__ and _WIN64. |
| 65 | #ifndef __WORDSIZE |
| 66 | # if __LP64__ || defined(_WIN64) |
| 67 | # define __WORDSIZE 64 |
| 68 | # else |
| 69 | # define __WORDSIZE 32 |
| 70 | # endif |
| 71 | #endif // __WORDSIZE |
Kostya Serebryany | 1b71207 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 72 | |
Alexey Samsonov | e428779 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 73 | // NOTE: Functions below must be defined in each run-time. |
| 74 | namespace __sanitizer { |
| 75 | void NORETURN Die(); |
| 76 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
| 77 | u64 v1, u64 v2); |
| 78 | } // namespace __sanitizer |
| 79 | |
| 80 | // Check macro |
Alexey Samsonov | ee07290 | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 81 | #define RAW_CHECK_MSG(expr, msg) do { \ |
| 82 | if (!(expr)) { \ |
| 83 | RawWrite(msg); \ |
| 84 | Die(); \ |
| 85 | } \ |
| 86 | } while (0) |
| 87 | |
| 88 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) |
| 89 | |
Alexey Samsonov | e428779 | 2012-06-06 15:22:20 +0000 | [diff] [blame] | 90 | #define CHECK_IMPL(c1, op, c2) \ |
| 91 | do { \ |
| 92 | __sanitizer::u64 v1 = (u64)(c1); \ |
| 93 | __sanitizer::u64 v2 = (u64)(c2); \ |
| 94 | if (!(v1 op v2)) \ |
| 95 | __sanitizer::CheckFailed(__FILE__, __LINE__, \ |
| 96 | "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ |
| 97 | } while (false) \ |
| 98 | /**/ |
| 99 | |
| 100 | #define CHECK(a) CHECK_IMPL((a), !=, 0) |
| 101 | #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) |
| 102 | #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) |
| 103 | #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) |
| 104 | #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) |
| 105 | #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) |
| 106 | #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) |
| 107 | |
Alexey Samsonov | e95e29c | 2012-06-06 15:47:40 +0000 | [diff] [blame] | 108 | #define UNIMPLEMENTED() CHECK("unimplemented" && 0) |
| 109 | |
Kostya Serebryany | 1b71207 | 2012-05-31 14:11:07 +0000 | [diff] [blame] | 110 | #endif // SANITIZER_DEFS_H |