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