]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
Merge llvm trunk r321017 to contrib/llvm.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_internal_defs.h
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 #ifndef SANITIZER_DEBUG
19 # define SANITIZER_DEBUG 0
20 #endif
21
22 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
23 #if SANITIZER_WINDOWS
24 #if SANITIZER_IMPORT_INTERFACE
25 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
26 #else
27 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
28 #endif
29 # define SANITIZER_WEAK_ATTRIBUTE
30 #elif SANITIZER_GO
31 # define SANITIZER_INTERFACE_ATTRIBUTE
32 # define SANITIZER_WEAK_ATTRIBUTE
33 #else
34 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
35 # define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))
36 #endif
37
38 //--------------------------- WEAK FUNCTIONS ---------------------------------//
39 // When working with weak functions, to simplify the code and make it more
40 // portable, when possible define a default implementation using this macro:
41 //
42 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
43 //
44 // For example:
45 //   SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
46 //
47 #if SANITIZER_WINDOWS
48 #include "sanitizer_win_defs.h"
49 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...)                   \
50   WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
51 #else
52 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...)                   \
53   extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE            \
54   ReturnType Name(__VA_ARGS__)
55 #endif
56
57 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
58 // will evaluate to a null pointer when not defined.
59 #if (SANITIZER_LINUX || SANITIZER_MAC) && !SANITIZER_GO
60 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
61 #else
62 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
63 #endif
64 // For some weak hooks that will be called very often and we want to avoid the
65 // overhead of executing the default implementation when it is not necessary,
66 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
67 // implementation for platforms that doesn't support weak symbols. For example:
68 //
69 //   #if !SANITIZER_SUPPORT_WEAK_HOOKS
70 //     SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
71 //       return a > b;
72 //     }
73 //   #endif
74 //
75 // And then use it as: if (compare_hook) compare_hook(a, b);
76 //----------------------------------------------------------------------------//
77
78
79 // We can use .preinit_array section on Linux to call sanitizer initialization
80 // functions very early in the process startup (unless PIC macro is defined).
81 // FIXME: do we have anything like this on Mac?
82 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
83 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
84 #else
85 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
86 #endif
87
88 // GCC does not understand __has_feature
89 #if !defined(__has_feature)
90 # define __has_feature(x) 0
91 #endif
92
93 // For portability reasons we do not include stddef.h, stdint.h or any other
94 // system header, but we do need some basic types that are not defined
95 // in a portable way by the language itself.
96 namespace __sanitizer {
97
98 #if defined(_WIN64)
99 // 64-bit Windows uses LLP64 data model.
100 typedef unsigned long long uptr;  // NOLINT
101 typedef signed   long long sptr;  // NOLINT
102 #else
103 typedef unsigned long uptr;  // NOLINT
104 typedef signed   long sptr;  // NOLINT
105 #endif  // defined(_WIN64)
106 #if defined(__x86_64__)
107 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
108 // 64-bit pointer to unwind stack frame.
109 typedef unsigned long long uhwptr;  // NOLINT
110 #else
111 typedef uptr uhwptr;   // NOLINT
112 #endif
113 typedef unsigned char u8;
114 typedef unsigned short u16;  // NOLINT
115 typedef unsigned int u32;
116 typedef unsigned long long u64;  // NOLINT
117 typedef signed   char s8;
118 typedef signed   short s16;  // NOLINT
119 typedef signed   int s32;
120 typedef signed   long long s64;  // NOLINT
121 #if SANITIZER_WINDOWS
122 // On Windows, files are HANDLE, which is a synonim of void*.
123 // Use void* to avoid including <windows.h> everywhere.
124 typedef void* fd_t;
125 typedef unsigned error_t;
126 #else
127 typedef int fd_t;
128 typedef int error_t;
129 #endif
130 typedef int pid_t;
131
132 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
133 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
134 // like pread and mmap, as opposed to pread64 and mmap64.
135 // FreeBSD, Mac and Linux/x86-64 are special.
136 #if SANITIZER_FREEBSD || SANITIZER_MAC || \
137   (SANITIZER_LINUX && defined(__x86_64__))
138 typedef u64 OFF_T;
139 #else
140 typedef uptr OFF_T;
141 #endif
142 typedef u64  OFF64_T;
143
144 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
145 typedef uptr operator_new_size_type;
146 #else
147 # if defined(__s390__) && !defined(__s390x__)
148 // Special case: 31-bit s390 has unsigned long as size_t.
149 typedef unsigned long operator_new_size_type;
150 # else
151 typedef u32 operator_new_size_type;
152 # endif
153 #endif
154
155 #if SANITIZER_MAC
156 // On Darwin, thread IDs are 64-bit even on 32-bit systems.
157 typedef u64 tid_t;
158 #else
159 typedef uptr tid_t;
160 #endif
161
162 // ----------- ATTENTION -------------
163 // This header should NOT include any other headers to avoid portability issues.
164
165 // Common defs.
166 #define INLINE inline
167 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
168 #define SANITIZER_WEAK_DEFAULT_IMPL \
169   extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
170 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
171   extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
172
173 // Platform-specific defs.
174 #if defined(_MSC_VER)
175 # define ALWAYS_INLINE __forceinline
176 // FIXME(timurrrr): do we need this on Windows?
177 # define ALIAS(x)
178 # define ALIGNED(x) __declspec(align(x))
179 # define FORMAT(f, a)
180 # define NOINLINE __declspec(noinline)
181 # define NORETURN __declspec(noreturn)
182 # define THREADLOCAL   __declspec(thread)
183 # define LIKELY(x) (x)
184 # define UNLIKELY(x) (x)
185 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
186 #else  // _MSC_VER
187 # define ALWAYS_INLINE inline __attribute__((always_inline))
188 # define ALIAS(x) __attribute__((alias(x)))
189 // Please only use the ALIGNED macro before the type.
190 // Using ALIGNED after the variable declaration is not portable!
191 # define ALIGNED(x) __attribute__((aligned(x)))
192 # define FORMAT(f, a)  __attribute__((format(printf, f, a)))
193 # define NOINLINE __attribute__((noinline))
194 # define NORETURN  __attribute__((noreturn))
195 # define THREADLOCAL   __thread
196 # define LIKELY(x)     __builtin_expect(!!(x), 1)
197 # define UNLIKELY(x)   __builtin_expect(!!(x), 0)
198 # if defined(__i386__) || defined(__x86_64__)
199 // __builtin_prefetch(x) generates prefetchnt0 on x86
200 #  define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
201 # else
202 #  define PREFETCH(x) __builtin_prefetch(x)
203 # endif
204 #endif  // _MSC_VER
205
206 #if !defined(_MSC_VER) || defined(__clang__)
207 # define UNUSED __attribute__((unused))
208 # define USED __attribute__((used))
209 #else
210 # define UNUSED
211 # define USED
212 #endif
213
214 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
215 # define NOEXCEPT noexcept
216 #else
217 # define NOEXCEPT throw()
218 #endif
219
220 // Unaligned versions of basic types.
221 typedef ALIGNED(1) u16 uu16;
222 typedef ALIGNED(1) u32 uu32;
223 typedef ALIGNED(1) u64 uu64;
224 typedef ALIGNED(1) s16 us16;
225 typedef ALIGNED(1) s32 us32;
226 typedef ALIGNED(1) s64 us64;
227
228 #if SANITIZER_WINDOWS
229 }  // namespace __sanitizer
230 typedef unsigned long DWORD;  // NOLINT
231 namespace __sanitizer {
232 typedef DWORD thread_return_t;
233 # define THREAD_CALLING_CONV __stdcall
234 #else  // _WIN32
235 typedef void* thread_return_t;
236 # define THREAD_CALLING_CONV
237 #endif  // _WIN32
238 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
239
240 // NOTE: Functions below must be defined in each run-time.
241 void NORETURN Die();
242
243 // FIXME: No, this shouldn't be in the sanitizer interface.
244 SANITIZER_INTERFACE_ATTRIBUTE
245 void NORETURN CheckFailed(const char *file, int line, const char *cond,
246                           u64 v1, u64 v2);
247
248 // Check macro
249 #define RAW_CHECK_MSG(expr, msg) do { \
250   if (UNLIKELY(!(expr))) { \
251     RawWrite(msg); \
252     Die(); \
253   } \
254 } while (0)
255
256 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
257
258 #define CHECK_IMPL(c1, op, c2) \
259   do { \
260     __sanitizer::u64 v1 = (u64)(c1); \
261     __sanitizer::u64 v2 = (u64)(c2); \
262     if (UNLIKELY(!(v1 op v2))) \
263       __sanitizer::CheckFailed(__FILE__, __LINE__, \
264         "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
265   } while (false) \
266 /**/
267
268 #define CHECK(a)       CHECK_IMPL((a), !=, 0)
269 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
270 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
271 #define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
272 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
273 #define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
274 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
275
276 #if SANITIZER_DEBUG
277 #define DCHECK(a)       CHECK(a)
278 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
279 #define DCHECK_NE(a, b) CHECK_NE(a, b)
280 #define DCHECK_LT(a, b) CHECK_LT(a, b)
281 #define DCHECK_LE(a, b) CHECK_LE(a, b)
282 #define DCHECK_GT(a, b) CHECK_GT(a, b)
283 #define DCHECK_GE(a, b) CHECK_GE(a, b)
284 #else
285 #define DCHECK(a)
286 #define DCHECK_EQ(a, b)
287 #define DCHECK_NE(a, b)
288 #define DCHECK_LT(a, b)
289 #define DCHECK_LE(a, b)
290 #define DCHECK_GT(a, b)
291 #define DCHECK_GE(a, b)
292 #endif
293
294 #define UNREACHABLE(msg) do { \
295   CHECK(0 && msg); \
296   Die(); \
297 } while (0)
298
299 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
300
301 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
302
303 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
304
305 #define IMPL_PASTE(a, b) a##b
306 #define IMPL_COMPILER_ASSERT(pred, line) \
307     typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
308
309 // Limits for integral types. We have to redefine it in case we don't
310 // have stdint.h (like in Visual Studio 9).
311 #undef __INT64_C
312 #undef __UINT64_C
313 #if SANITIZER_WORDSIZE == 64
314 # define __INT64_C(c)  c ## L
315 # define __UINT64_C(c) c ## UL
316 #else
317 # define __INT64_C(c)  c ## LL
318 # define __UINT64_C(c) c ## ULL
319 #endif  // SANITIZER_WORDSIZE == 64
320 #undef INT32_MIN
321 #define INT32_MIN              (-2147483647-1)
322 #undef INT32_MAX
323 #define INT32_MAX              (2147483647)
324 #undef UINT32_MAX
325 #define UINT32_MAX             (4294967295U)
326 #undef INT64_MIN
327 #define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
328 #undef INT64_MAX
329 #define INT64_MAX              (__INT64_C(9223372036854775807))
330 #undef UINT64_MAX
331 #define UINT64_MAX             (__UINT64_C(18446744073709551615))
332
333 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
334
335 #if !defined(_MSC_VER) || defined(__clang__)
336 #if SANITIZER_S390_31
337 #define GET_CALLER_PC() \
338   (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
339 #else
340 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
341 #endif
342 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
343 inline void Trap() {
344   __builtin_trap();
345 }
346 #else
347 extern "C" void* _ReturnAddress(void);
348 extern "C" void* _AddressOfReturnAddress(void);
349 # pragma intrinsic(_ReturnAddress)
350 # pragma intrinsic(_AddressOfReturnAddress)
351 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
352 // CaptureStackBackTrace doesn't need to know BP on Windows.
353 #define GET_CURRENT_FRAME() \
354   (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
355
356 extern "C" void __ud2(void);
357 # pragma intrinsic(__ud2)
358 inline void Trap() {
359   __ud2();
360 }
361 #endif
362
363 #define HANDLE_EINTR(res, f)                                       \
364   {                                                                \
365     int rverrno;                                                   \
366     do {                                                           \
367       res = (f);                                                   \
368     } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
369   }
370
371 // Forces the compiler to generate a frame pointer in the function.
372 #define ENABLE_FRAME_POINTER              \
373   do {                                    \
374     volatile __sanitizer::uptr enable_fp; \
375     enable_fp = GET_CURRENT_FRAME();      \
376     (void)enable_fp;                      \
377   } while (0)
378
379 }  // namespace __sanitizer
380
381 namespace __asan  { using namespace __sanitizer; }  // NOLINT
382 namespace __dsan  { using namespace __sanitizer; }  // NOLINT
383 namespace __dfsan { using namespace __sanitizer; }  // NOLINT
384 namespace __esan  { using namespace __sanitizer; }  // NOLINT
385 namespace __lsan  { using namespace __sanitizer; }  // NOLINT
386 namespace __msan  { using namespace __sanitizer; }  // NOLINT
387 namespace __tsan  { using namespace __sanitizer; }  // NOLINT
388 namespace __scudo { using namespace __sanitizer; }  // NOLINT
389 namespace __ubsan { using namespace __sanitizer; }  // NOLINT
390 namespace __xray  { using namespace __sanitizer; }  // NOLINT
391 namespace __interception  { using namespace __sanitizer; }  // NOLINT
392
393
394 #endif  // SANITIZER_DEFS_H