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