blob: e83eed0830d848bb2364e7c363992981759c78f8 [file] [log] [blame]
Alexey Samsonov94b50362012-06-05 14:25:27 +00001//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
Kostya Serebryany9aead372012-05-31 14:11:07 +00002//
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.
Alexey Samsonov0a4c9062012-06-05 13:50:57 +000011// It contains macro used in run-time libraries code.
Kostya Serebryany9aead372012-05-31 14:11:07 +000012//===----------------------------------------------------------------------===//
13#ifndef SANITIZER_DEFS_H
14#define SANITIZER_DEFS_H
15
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000016#include "sanitizer_platform.h"
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000017
Stephen Hines86277eb2015-03-23 12:06:32 -070018#ifndef SANITIZER_DEBUG
19# define SANITIZER_DEBUG 0
20#endif
21
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000022// Only use SANITIZER_*ATTRIBUTE* before the function return type!
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +000023#if SANITIZER_WINDOWS
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000024# define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
25// FIXME find out what we need on Windows, if anything.
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000026# define SANITIZER_WEAK_ATTRIBUTE
27#elif defined(SANITIZER_GO)
28# define SANITIZER_INTERFACE_ATTRIBUTE
29# define SANITIZER_WEAK_ATTRIBUTE
30#else
31# define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
32# define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
33#endif
34
Stephen Hines86277eb2015-03-23 12:06:32 -070035#if (SANITIZER_LINUX || SANITIZER_WINDOWS) && !defined(SANITIZER_GO)
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000036# define SANITIZER_SUPPORTS_WEAK_HOOKS 1
37#else
38# define SANITIZER_SUPPORTS_WEAK_HOOKS 0
39#endif
40
Stephen Hines6d186232014-11-26 17:56:19 -080041// We can use .preinit_array section on Linux to call sanitizer initialization
42// functions very early in the process startup (unless PIC macro is defined).
43// FIXME: do we have anything like this on Mac?
44#if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
45# define SANITIZER_CAN_USE_PREINIT_ARRAY 1
46#else
47# define SANITIZER_CAN_USE_PREINIT_ARRAY 0
48#endif
49
Kostya Serebryanye31eca92013-02-15 12:00:24 +000050// GCC does not understand __has_feature
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000051#if !defined(__has_feature)
52# define __has_feature(x) 0
53#endif
54
55// For portability reasons we do not include stddef.h, stdint.h or any other
56// system header, but we do need some basic types that are not defined
57// in a portable way by the language itself.
58namespace __sanitizer {
59
60#if defined(_WIN64)
61// 64-bit Windows uses LLP64 data model.
62typedef unsigned long long uptr; // NOLINT
63typedef signed long long sptr; // NOLINT
64#else
65typedef unsigned long uptr; // NOLINT
66typedef signed long sptr; // NOLINT
67#endif // defined(_WIN64)
Bill Wendling7f790ca2013-12-16 02:36:46 +000068#if defined(__x86_64__)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070069// Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
Bill Wendling7f790ca2013-12-16 02:36:46 +000070// 64-bit pointer to unwind stack frame.
71typedef unsigned long long uhwptr; // NOLINT
72#else
73typedef uptr uhwptr; // NOLINT
74#endif
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000075typedef unsigned char u8;
76typedef unsigned short u16; // NOLINT
77typedef unsigned int u32;
78typedef unsigned long long u64; // NOLINT
79typedef signed char s8;
80typedef signed short s16; // NOLINT
81typedef signed int s32;
82typedef signed long long s64; // NOLINT
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070083#if SANITIZER_WINDOWS
84// On Windows, files are HANDLE, which is a synonim of void*.
85// Use void* to avoid including <windows.h> everywhere.
86typedef void* fd_t;
87typedef unsigned error_t;
88#else
Alexey Samsonovee7cc442013-02-01 15:58:46 +000089typedef int fd_t;
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070090typedef int error_t;
91#endif
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000092
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000093// WARNING: OFF_T may be different from OS type off_t, depending on the value of
94// _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
95// like pread and mmap, as opposed to pread64 and mmap64.
Stephen Hines86277eb2015-03-23 12:06:32 -070096// FreeBSD, Mac and Linux/x86-64 are special.
97#if SANITIZER_FREEBSD || SANITIZER_MAC || \
98 (SANITIZER_LINUX && defined(__x86_64__))
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000099typedef u64 OFF_T;
100#else
101typedef uptr OFF_T;
102#endif
103typedef u64 OFF64_T;
Peter Collingbourne53177242013-10-24 06:23:39 +0000104
105#if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
106typedef uptr operator_new_size_type;
107#else
108typedef u32 operator_new_size_type;
109#endif
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000110} // namespace __sanitizer
111
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000112
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000113using namespace __sanitizer; // NOLINT
Kostya Serebryany9aead372012-05-31 14:11:07 +0000114// ----------- ATTENTION -------------
115// This header should NOT include any other headers to avoid portability issues.
116
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000117// Common defs.
Timur Iskhodzhanov40e16682013-04-12 09:37:20 +0000118#define INLINE inline
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000119#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800120#define SANITIZER_WEAK_DEFAULT_IMPL \
121 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
122#define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
123 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000124
125// Platform-specific defs.
Dmitry Vyukovbfa45e12012-11-06 12:47:42 +0000126#if defined(_MSC_VER)
Timur Iskhodzhanov40e16682013-04-12 09:37:20 +0000127# define ALWAYS_INLINE __forceinline
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000128// FIXME(timurrrr): do we need this on Windows?
129# define ALIAS(x)
130# define ALIGNED(x) __declspec(align(x))
Alexey Samsonov15503b02012-06-06 13:37:02 +0000131# define FORMAT(f, a)
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000132# define NOINLINE __declspec(noinline)
133# define NORETURN __declspec(noreturn)
134# define THREADLOCAL __declspec(thread)
Dmitry Vyukove2462f72012-11-06 12:54:16 +0000135# define LIKELY(x) (x)
136# define UNLIKELY(x) (x)
Dmitry Vyukovb1bd2082013-01-11 16:40:24 +0000137# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
Dmitry Vyukovbfa45e12012-11-06 12:47:42 +0000138#else // _MSC_VER
Kostya Serebryanyd475aa82013-03-29 09:44:16 +0000139# define ALWAYS_INLINE inline __attribute__((always_inline))
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000140# define ALIAS(x) __attribute__((alias(x)))
Timur Iskhodzhanov7c9ffde2013-06-04 08:25:17 +0000141// Please only use the ALIGNED macro before the type.
142// Using ALIGNED after the variable declaration is not portable!
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000143# define ALIGNED(x) __attribute__((aligned(x)))
Alexey Samsonov15503b02012-06-06 13:37:02 +0000144# define FORMAT(f, a) __attribute__((format(printf, f, a)))
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000145# define NOINLINE __attribute__((noinline))
146# define NORETURN __attribute__((noreturn))
147# define THREADLOCAL __thread
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000148# define LIKELY(x) __builtin_expect(!!(x), 1)
149# define UNLIKELY(x) __builtin_expect(!!(x), 0)
Dmitry Vyukov0c7f61c2013-01-16 14:35:13 +0000150# 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
Dmitry Vyukove2462f72012-11-06 12:54:16 +0000156#endif // _MSC_VER
Kostya Serebryany9aead372012-05-31 14:11:07 +0000157
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700158#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
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800166#if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
167# define NOEXCEPT noexcept
168#else
169# define NOEXCEPT throw()
170#endif
171
Evgeniy Stepanov7202e792013-06-04 14:06:16 +0000172// Unaligned versions of basic types.
173typedef ALIGNED(1) u16 uu16;
174typedef ALIGNED(1) u32 uu32;
175typedef ALIGNED(1) u64 uu64;
176typedef ALIGNED(1) s16 us16;
177typedef ALIGNED(1) s32 us32;
178typedef ALIGNED(1) s64 us64;
179
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +0000180#if SANITIZER_WINDOWS
Dmitry Vyukovbb192942012-11-06 12:50:13 +0000181typedef unsigned long DWORD; // NOLINT
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000182typedef DWORD thread_return_t;
183# define THREAD_CALLING_CONV __stdcall
184#else // _WIN32
185typedef void* thread_return_t;
186# define THREAD_CALLING_CONV
187#endif // _WIN32
188typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
189
Alexey Samsonov15a77612012-06-06 15:22:20 +0000190// NOTE: Functions below must be defined in each run-time.
191namespace __sanitizer {
192void NORETURN Die();
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000193
194// FIXME: No, this shouldn't be in the sanitizer interface.
195SANITIZER_INTERFACE_ATTRIBUTE
Alexey Samsonov15a77612012-06-06 15:22:20 +0000196void NORETURN CheckFailed(const char *file, int line, const char *cond,
197 u64 v1, u64 v2);
198} // namespace __sanitizer
199
200// Check macro
Alexey Samsonov230c3be2012-06-06 09:26:25 +0000201#define RAW_CHECK_MSG(expr, msg) do { \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700202 if (UNLIKELY(!(expr))) { \
Alexey Samsonov230c3be2012-06-06 09:26:25 +0000203 RawWrite(msg); \
204 Die(); \
205 } \
206} while (0)
207
208#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
209
Alexey Samsonov15a77612012-06-06 15:22:20 +0000210#define CHECK_IMPL(c1, op, c2) \
211 do { \
212 __sanitizer::u64 v1 = (u64)(c1); \
213 __sanitizer::u64 v2 = (u64)(c2); \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700214 if (UNLIKELY(!(v1 op v2))) \
Alexey Samsonov15a77612012-06-06 15:22:20 +0000215 __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
Stephen Hines86277eb2015-03-23 12:06:32 -0700228#if SANITIZER_DEBUG
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000229#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
Alexey Samsonov45418d12012-10-09 08:42:07 +0000246#define UNREACHABLE(msg) do { \
247 CHECK(0 && msg); \
248 Die(); \
Kostya Serebryany5759d922012-10-16 04:50:32 +0000249} while (0)
Alexey Samsonov45418d12012-10-09 08:42:07 +0000250
251#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000252
Kostya Serebryanyc3756572012-06-21 10:04:36 +0000253#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
254
Kostya Serebryany4c2ddda2012-08-28 11:54:51 +0000255#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
256
Kostya Serebryanyc3756572012-06-21 10:04:36 +0000257#define IMPL_PASTE(a, b) a##b
258#define IMPL_COMPILER_ASSERT(pred, line) \
Alexey Samsonovd7ed1f02012-09-11 10:31:28 +0000259 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
Kostya Serebryanyc3756572012-06-21 10:04:36 +0000260
Alexey Samsonovc9256972012-06-15 13:09:52 +0000261// Limits for integral types. We have to redefine it in case we don't
262// have stdint.h (like in Visual Studio 9).
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000263#undef __INT64_C
264#undef __UINT64_C
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000265#if SANITIZER_WORDSIZE == 64
Alexey Samsonovc9256972012-06-15 13:09:52 +0000266# 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
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000271#endif // SANITIZER_WORDSIZE == 64
Alexey Samsonovc9256972012-06-15 13:09:52 +0000272#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
Alexey Samsonov70e177e2012-08-27 09:30:58 +0000285enum LinkerInitialized { LINKER_INITIALIZED = 0 };
286
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000287#if !defined(_MSC_VER) || defined(__clang__)
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +0000288# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
289# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
290#else
Kostya Serebryany80acccf2012-08-28 15:25:07 +0000291extern "C" void* _ReturnAddress(void);
292# pragma intrinsic(_ReturnAddress)
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +0000293# 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
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000300#define HANDLE_EINTR(res, f) \
301 { \
302 int rverrno; \
303 do { \
304 res = (f); \
305 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
Evgeniy Stepanov3334e122012-10-02 13:41:40 +0000306 }
307
Stephen Hines86277eb2015-03-23 12:06:32 -0700308// 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
Kostya Serebryany9aead372012-05-31 14:11:07 +0000316#endif // SANITIZER_DEFS_H