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