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