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