sanitizer_internal_defs.h revision 250f221ae0dee295098da8aa631977b6c2ebc99b
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#if defined(_WIN32) 17// FIXME find out what we need on Windows. __declspec(dllexport) ? 18# define SANITIZER_INTERFACE_ATTRIBUTE 19# define SANITIZER_WEAK_ATTRIBUTE 20#elif defined(SANITIZER_GO) 21# define SANITIZER_INTERFACE_ATTRIBUTE 22# define SANITIZER_WEAK_ATTRIBUTE 23#else 24# define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) 25# define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak)) 26#endif 27 28#ifdef __linux__ 29# define SANITIZER_SUPPORTS_WEAK_HOOKS 1 30#else 31# define SANITIZER_SUPPORTS_WEAK_HOOKS 0 32#endif 33 34// __has_feature 35#if !defined(__has_feature) 36# define __has_feature(x) 0 37#endif 38 39// For portability reasons we do not include stddef.h, stdint.h or any other 40// system header, but we do need some basic types that are not defined 41// in a portable way by the language itself. 42namespace __sanitizer { 43 44#if defined(_WIN64) 45// 64-bit Windows uses LLP64 data model. 46typedef unsigned long long uptr; // NOLINT 47typedef signed long long sptr; // NOLINT 48#else 49typedef unsigned long uptr; // NOLINT 50typedef signed long sptr; // NOLINT 51#endif // defined(_WIN64) 52#if defined(__x86_64__) 53// Since x32 uses ILP32 data model in 64-bit hardware mode, we must use 54// 64-bit pointer to unwind stack frame. 55typedef unsigned long long uhwptr; // NOLINT 56#else 57typedef uptr uhwptr; // NOLINT 58#endif 59typedef unsigned char u8; 60typedef unsigned short u16; // NOLINT 61typedef unsigned int u32; 62typedef unsigned long long u64; // NOLINT 63typedef signed char s8; 64typedef signed short s16; // NOLINT 65typedef signed int s32; 66typedef signed long long s64; // NOLINT 67 68} // namespace __sanitizer 69 70extern "C" { 71 // Tell the tools to write their reports to "path.<pid>" instead of stderr. 72 void __sanitizer_set_report_path(const char *path) 73 SANITIZER_INTERFACE_ATTRIBUTE; 74 75 // Tell the tools to write their reports to given file descriptor instead of 76 // stderr. 77 void __sanitizer_set_report_fd(int fd) 78 SANITIZER_INTERFACE_ATTRIBUTE; 79 80 // Notify the tools that the sandbox is going to be turned on. The reserved 81 // parameter will be used in the future to hold a structure with functions 82 // that the tools may call to bypass the sandbox. 83 void __sanitizer_sandbox_on_notify(void *reserved) 84 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE; 85} // extern "C" 86 87 88using namespace __sanitizer; // NOLINT 89// ----------- ATTENTION ------------- 90// This header should NOT include any other headers to avoid portability issues. 91 92// Common defs. 93#define INLINE static inline 94#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE 95#define WEAK SANITIZER_WEAK_ATTRIBUTE 96 97// Platform-specific defs. 98#if defined(_MSC_VER) 99# define ALWAYS_INLINE __declspec(forceinline) 100// FIXME(timurrrr): do we need this on Windows? 101# define ALIAS(x) 102# define ALIGNED(x) __declspec(align(x)) 103# define FORMAT(f, a) 104# define NOINLINE __declspec(noinline) 105# define NORETURN __declspec(noreturn) 106# define THREADLOCAL __declspec(thread) 107# define NOTHROW 108# define LIKELY(x) (x) 109# define UNLIKELY(x) (x) 110# define UNUSED 111# define USED 112# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ 113#else // _MSC_VER 114# define ALWAYS_INLINE __attribute__((always_inline)) 115# define ALIAS(x) __attribute__((alias(x))) 116# define ALIGNED(x) __attribute__((aligned(x))) 117# define FORMAT(f, a) __attribute__((format(printf, f, a))) 118# define NOINLINE __attribute__((noinline)) 119# define NORETURN __attribute__((noreturn)) 120# define THREADLOCAL __thread 121# define NOTHROW throw() 122# define LIKELY(x) __builtin_expect(!!(x), 1) 123# define UNLIKELY(x) __builtin_expect(!!(x), 0) 124# define UNUSED __attribute__((unused)) 125# define USED __attribute__((used)) 126# if defined(__i386__) || defined(__x86_64__) 127// __builtin_prefetch(x) generates prefetchnt0 on x86 128# define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x)) 129# else 130# define PREFETCH(x) __builtin_prefetch(x) 131# endif 132#endif // _MSC_VER 133 134#if defined(_WIN32) 135typedef unsigned long DWORD; // NOLINT 136typedef DWORD thread_return_t; 137# define THREAD_CALLING_CONV __stdcall 138#else // _WIN32 139typedef void* thread_return_t; 140# define THREAD_CALLING_CONV 141#endif // _WIN32 142typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); 143 144#if __LP64__ || defined(_WIN64) 145# define SANITIZER_WORDSIZE 64 146#else 147# define SANITIZER_WORDSIZE 32 148#endif 149 150// NOTE: Functions below must be defined in each run-time. 151namespace __sanitizer { 152void NORETURN Die(); 153void NORETURN CheckFailed(const char *file, int line, const char *cond, 154 u64 v1, u64 v2); 155} // namespace __sanitizer 156 157// Check macro 158#define RAW_CHECK_MSG(expr, msg) do { \ 159 if (!(expr)) { \ 160 RawWrite(msg); \ 161 Die(); \ 162 } \ 163} while (0) 164 165#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) 166 167#define CHECK_IMPL(c1, op, c2) \ 168 do { \ 169 __sanitizer::u64 v1 = (u64)(c1); \ 170 __sanitizer::u64 v2 = (u64)(c2); \ 171 if (!(v1 op v2)) \ 172 __sanitizer::CheckFailed(__FILE__, __LINE__, \ 173 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ 174 } while (false) \ 175/**/ 176 177#define CHECK(a) CHECK_IMPL((a), !=, 0) 178#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) 179#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) 180#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) 181#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) 182#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) 183#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) 184 185#if TSAN_DEBUG 186#define DCHECK(a) CHECK(a) 187#define DCHECK_EQ(a, b) CHECK_EQ(a, b) 188#define DCHECK_NE(a, b) CHECK_NE(a, b) 189#define DCHECK_LT(a, b) CHECK_LT(a, b) 190#define DCHECK_LE(a, b) CHECK_LE(a, b) 191#define DCHECK_GT(a, b) CHECK_GT(a, b) 192#define DCHECK_GE(a, b) CHECK_GE(a, b) 193#else 194#define DCHECK(a) 195#define DCHECK_EQ(a, b) 196#define DCHECK_NE(a, b) 197#define DCHECK_LT(a, b) 198#define DCHECK_LE(a, b) 199#define DCHECK_GT(a, b) 200#define DCHECK_GE(a, b) 201#endif 202 203#define UNREACHABLE(msg) do { \ 204 CHECK(0 && msg); \ 205 Die(); \ 206} while (0) 207 208#define UNIMPLEMENTED() UNREACHABLE("unimplemented") 209 210#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__) 211 212#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 213 214#define IMPL_PASTE(a, b) a##b 215#define IMPL_COMPILER_ASSERT(pred, line) \ 216 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1] 217 218// Limits for integral types. We have to redefine it in case we don't 219// have stdint.h (like in Visual Studio 9). 220#undef __INT64_C 221#undef __UINT64_C 222#if SANITIZER_WORDSIZE == 64 223# define __INT64_C(c) c ## L 224# define __UINT64_C(c) c ## UL 225#else 226# define __INT64_C(c) c ## LL 227# define __UINT64_C(c) c ## ULL 228#endif // SANITIZER_WORDSIZE == 64 229#undef INT32_MIN 230#define INT32_MIN (-2147483647-1) 231#undef INT32_MAX 232#define INT32_MAX (2147483647) 233#undef UINT32_MAX 234#define UINT32_MAX (4294967295U) 235#undef INT64_MIN 236#define INT64_MIN (-__INT64_C(9223372036854775807)-1) 237#undef INT64_MAX 238#define INT64_MAX (__INT64_C(9223372036854775807)) 239#undef UINT64_MAX 240#define UINT64_MAX (__UINT64_C(18446744073709551615)) 241 242enum LinkerInitialized { LINKER_INITIALIZED = 0 }; 243 244#if !defined(_MSC_VER) || defined(__clang__) 245# define GET_CALLER_PC() (uptr)__builtin_return_address(0) 246# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) 247#else 248extern "C" void* _ReturnAddress(void); 249# pragma intrinsic(_ReturnAddress) 250# define GET_CALLER_PC() (uptr)_ReturnAddress() 251// CaptureStackBackTrace doesn't need to know BP on Windows. 252// FIXME: This macro is still used when printing error reports though it's not 253// clear if the BP value is needed in the ASan reports on Windows. 254# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF 255#endif 256 257#define HANDLE_EINTR(res, f) { \ 258 do { \ 259 res = (f); \ 260 } while (res == -1 && errno == EINTR); \ 261 } 262 263#endif // SANITIZER_DEFS_H 264