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