blob: 1087f8190e4d9a21ac8615a76f1088757a7c9f7a [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
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000018// Only use SANITIZER_*ATTRIBUTE* before the function return type!
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +000019#if SANITIZER_WINDOWS
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000020# define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
21// FIXME find out what we need on Windows, if anything.
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000022# 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
Alexey Samsonov78b580f2013-04-03 13:22:54 +000031#if SANITIZER_LINUX && !defined(SANITIZER_GO)
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000032# define SANITIZER_SUPPORTS_WEAK_HOOKS 1
33#else
34# define SANITIZER_SUPPORTS_WEAK_HOOKS 0
35#endif
36
Kostya Serebryanye31eca92013-02-15 12:00:24 +000037// GCC does not understand __has_feature
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000038#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.
45namespace __sanitizer {
46
47#if defined(_WIN64)
48// 64-bit Windows uses LLP64 data model.
49typedef unsigned long long uptr; // NOLINT
50typedef signed long long sptr; // NOLINT
51#else
52typedef unsigned long uptr; // NOLINT
53typedef 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.
58typedef unsigned long long uhwptr; // NOLINT
59#else
60typedef uptr uhwptr; // NOLINT
61#endif
62typedef unsigned char u8;
63typedef unsigned short u16; // NOLINT
64typedef unsigned int u32;
65typedef unsigned long long u64; // NOLINT
66typedef signed char s8;
67typedef signed short s16; // NOLINT
68typedef signed int s32;
69typedef signed long long s64; // NOLINT
Alexey Samsonovee7cc442013-02-01 15:58:46 +000070typedef int fd_t;
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000071
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000072// 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.
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +000076#if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__))
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000077typedef u64 OFF_T;
78#else
79typedef uptr OFF_T;
80#endif
81typedef u64 OFF64_T;
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000082} // namespace __sanitizer
83
84extern "C" {
85 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
Dmitry Vyukovb48c2b22013-10-15 12:25:29 +000086 // The special values are "stdout" and "stderr".
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000087 SANITIZER_INTERFACE_ATTRIBUTE
88 void __sanitizer_set_report_path(const char *path);
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000089
Evgeniy Stepanov250f2212013-01-30 13:12:08 +000090 // Notify the tools that the sandbox is going to be turned on. The reserved
91 // parameter will be used in the future to hold a structure with functions
92 // that the tools may call to bypass the sandbox.
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000093 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
94 void __sanitizer_sandbox_on_notify(void *reserved);
Kostya Serebryany2673fd82013-02-06 12:36:49 +000095
96 // This function is called by the tool when it has just finished reporting
97 // an error. 'error_summary' is a one-line string that summarizes
98 // the error message. This function can be overridden by the client.
Timur Iskhodzhanov3c80c6c2013-08-13 11:42:45 +000099 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
100 void __sanitizer_report_error_summary(const char *error_summary);
Evgeniy Stepanov250f2212013-01-30 13:12:08 +0000101} // extern "C"
102
103
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000104using namespace __sanitizer; // NOLINT
Kostya Serebryany9aead372012-05-31 14:11:07 +0000105// ----------- ATTENTION -------------
106// This header should NOT include any other headers to avoid portability issues.
107
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000108// Common defs.
Timur Iskhodzhanov40e16682013-04-12 09:37:20 +0000109#define INLINE inline
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000110#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
111#define WEAK SANITIZER_WEAK_ATTRIBUTE
112
113// Platform-specific defs.
Dmitry Vyukovbfa45e12012-11-06 12:47:42 +0000114#if defined(_MSC_VER)
Timur Iskhodzhanov40e16682013-04-12 09:37:20 +0000115# define ALWAYS_INLINE __forceinline
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000116// FIXME(timurrrr): do we need this on Windows?
117# define ALIAS(x)
118# define ALIGNED(x) __declspec(align(x))
Alexey Samsonov15503b02012-06-06 13:37:02 +0000119# define FORMAT(f, a)
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000120# define NOINLINE __declspec(noinline)
121# define NORETURN __declspec(noreturn)
122# define THREADLOCAL __declspec(thread)
Dmitry Vyukovd51a1a12012-06-27 21:00:23 +0000123# define NOTHROW
Dmitry Vyukove2462f72012-11-06 12:54:16 +0000124# define LIKELY(x) (x)
125# define UNLIKELY(x) (x)
126# define UNUSED
127# define USED
Dmitry Vyukovb1bd2082013-01-11 16:40:24 +0000128# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
Dmitry Vyukovbfa45e12012-11-06 12:47:42 +0000129#else // _MSC_VER
Kostya Serebryanyd475aa82013-03-29 09:44:16 +0000130# define ALWAYS_INLINE inline __attribute__((always_inline))
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000131# define ALIAS(x) __attribute__((alias(x)))
Timur Iskhodzhanov7c9ffde2013-06-04 08:25:17 +0000132// Please only use the ALIGNED macro before the type.
133// Using ALIGNED after the variable declaration is not portable!
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000134# define ALIGNED(x) __attribute__((aligned(x)))
Alexey Samsonov15503b02012-06-06 13:37:02 +0000135# define FORMAT(f, a) __attribute__((format(printf, f, a)))
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000136# define NOINLINE __attribute__((noinline))
137# define NORETURN __attribute__((noreturn))
138# define THREADLOCAL __thread
Dmitry Vyukov97daee82012-11-06 12:49:22 +0000139# define NOTHROW throw()
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000140# define LIKELY(x) __builtin_expect(!!(x), 1)
141# define UNLIKELY(x) __builtin_expect(!!(x), 0)
Alexey Samsonov2ea97872012-06-14 14:02:32 +0000142# define UNUSED __attribute__((unused))
Alexey Samsonov0a4c9062012-06-05 13:50:57 +0000143# define USED __attribute__((used))
Dmitry Vyukov0c7f61c2013-01-16 14:35:13 +0000144# if defined(__i386__) || defined(__x86_64__)
145// __builtin_prefetch(x) generates prefetchnt0 on x86
146# define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
147# else
148# define PREFETCH(x) __builtin_prefetch(x)
149# endif
Dmitry Vyukove2462f72012-11-06 12:54:16 +0000150#endif // _MSC_VER
Kostya Serebryany9aead372012-05-31 14:11:07 +0000151
Evgeniy Stepanov7202e792013-06-04 14:06:16 +0000152// Unaligned versions of basic types.
153typedef ALIGNED(1) u16 uu16;
154typedef ALIGNED(1) u32 uu32;
155typedef ALIGNED(1) u64 uu64;
156typedef ALIGNED(1) s16 us16;
157typedef ALIGNED(1) s32 us32;
158typedef ALIGNED(1) s64 us64;
159
Evgeniy Stepanov30e110e2013-03-19 14:54:17 +0000160#if SANITIZER_WINDOWS
Dmitry Vyukovbb192942012-11-06 12:50:13 +0000161typedef unsigned long DWORD; // NOLINT
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000162typedef DWORD thread_return_t;
163# define THREAD_CALLING_CONV __stdcall
164#else // _WIN32
165typedef void* thread_return_t;
166# define THREAD_CALLING_CONV
167#endif // _WIN32
168typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
169
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000170#if __LP64__ || defined(_WIN64)
171# define SANITIZER_WORDSIZE 64
172#else
173# define SANITIZER_WORDSIZE 32
174#endif
Kostya Serebryany9aead372012-05-31 14:11:07 +0000175
Alexey Samsonov15a77612012-06-06 15:22:20 +0000176// NOTE: Functions below must be defined in each run-time.
177namespace __sanitizer {
178void NORETURN Die();
Timur Iskhodzhanovf1092672013-08-13 12:03:51 +0000179
180// FIXME: No, this shouldn't be in the sanitizer interface.
181SANITIZER_INTERFACE_ATTRIBUTE
Alexey Samsonov15a77612012-06-06 15:22:20 +0000182void NORETURN CheckFailed(const char *file, int line, const char *cond,
183 u64 v1, u64 v2);
184} // namespace __sanitizer
185
186// Check macro
Alexey Samsonov230c3be2012-06-06 09:26:25 +0000187#define RAW_CHECK_MSG(expr, msg) do { \
188 if (!(expr)) { \
189 RawWrite(msg); \
190 Die(); \
191 } \
192} while (0)
193
194#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
195
Alexey Samsonov15a77612012-06-06 15:22:20 +0000196#define CHECK_IMPL(c1, op, c2) \
197 do { \
198 __sanitizer::u64 v1 = (u64)(c1); \
199 __sanitizer::u64 v2 = (u64)(c2); \
200 if (!(v1 op v2)) \
201 __sanitizer::CheckFailed(__FILE__, __LINE__, \
202 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
203 } while (false) \
204/**/
205
206#define CHECK(a) CHECK_IMPL((a), !=, 0)
207#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
208#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
209#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
210#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
211#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
212#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
213
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000214#if TSAN_DEBUG
215#define DCHECK(a) CHECK(a)
216#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
217#define DCHECK_NE(a, b) CHECK_NE(a, b)
218#define DCHECK_LT(a, b) CHECK_LT(a, b)
219#define DCHECK_LE(a, b) CHECK_LE(a, b)
220#define DCHECK_GT(a, b) CHECK_GT(a, b)
221#define DCHECK_GE(a, b) CHECK_GE(a, b)
222#else
223#define DCHECK(a)
224#define DCHECK_EQ(a, b)
225#define DCHECK_NE(a, b)
226#define DCHECK_LT(a, b)
227#define DCHECK_LE(a, b)
228#define DCHECK_GT(a, b)
229#define DCHECK_GE(a, b)
230#endif
231
Alexey Samsonov45418d12012-10-09 08:42:07 +0000232#define UNREACHABLE(msg) do { \
233 CHECK(0 && msg); \
234 Die(); \
Kostya Serebryany5759d922012-10-16 04:50:32 +0000235} while (0)
Alexey Samsonov45418d12012-10-09 08:42:07 +0000236
237#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
Alexey Samsonov8c53e542012-06-06 15:47:40 +0000238
Kostya Serebryanyc3756572012-06-21 10:04:36 +0000239#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
240
Kostya Serebryany4c2ddda2012-08-28 11:54:51 +0000241#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
242
Kostya Serebryanyc3756572012-06-21 10:04:36 +0000243#define IMPL_PASTE(a, b) a##b
244#define IMPL_COMPILER_ASSERT(pred, line) \
Alexey Samsonovd7ed1f02012-09-11 10:31:28 +0000245 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
Kostya Serebryanyc3756572012-06-21 10:04:36 +0000246
Alexey Samsonovc9256972012-06-15 13:09:52 +0000247// Limits for integral types. We have to redefine it in case we don't
248// have stdint.h (like in Visual Studio 9).
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000249#undef __INT64_C
250#undef __UINT64_C
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000251#if SANITIZER_WORDSIZE == 64
Alexey Samsonovc9256972012-06-15 13:09:52 +0000252# define __INT64_C(c) c ## L
253# define __UINT64_C(c) c ## UL
254#else
255# define __INT64_C(c) c ## LL
256# define __UINT64_C(c) c ## ULL
Kostya Serebryany5af39e52012-11-21 12:38:58 +0000257#endif // SANITIZER_WORDSIZE == 64
Alexey Samsonovc9256972012-06-15 13:09:52 +0000258#undef INT32_MIN
259#define INT32_MIN (-2147483647-1)
260#undef INT32_MAX
261#define INT32_MAX (2147483647)
262#undef UINT32_MAX
263#define UINT32_MAX (4294967295U)
264#undef INT64_MIN
265#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
266#undef INT64_MAX
267#define INT64_MAX (__INT64_C(9223372036854775807))
268#undef UINT64_MAX
269#define UINT64_MAX (__UINT64_C(18446744073709551615))
270
Alexey Samsonov70e177e2012-08-27 09:30:58 +0000271enum LinkerInitialized { LINKER_INITIALIZED = 0 };
272
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000273#if !defined(_MSC_VER) || defined(__clang__)
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +0000274# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
275# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
276#else
Kostya Serebryany80acccf2012-08-28 15:25:07 +0000277extern "C" void* _ReturnAddress(void);
278# pragma intrinsic(_ReturnAddress)
Kostya Serebryany1b5ea8f2012-08-28 14:11:57 +0000279# define GET_CALLER_PC() (uptr)_ReturnAddress()
280// CaptureStackBackTrace doesn't need to know BP on Windows.
281// FIXME: This macro is still used when printing error reports though it's not
282// clear if the BP value is needed in the ASan reports on Windows.
283# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
284#endif
285
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000286#define HANDLE_EINTR(res, f) \
287 { \
288 int rverrno; \
289 do { \
290 res = (f); \
291 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
Evgeniy Stepanov3334e122012-10-02 13:41:40 +0000292 }
293
Kostya Serebryany9aead372012-05-31 14:11:07 +0000294#endif // SANITIZER_DEFS_H