blob: 0c709fed17d7453618d29080525104cde967f0d7 [file] [log] [blame]
Alexey Samsonov5bbf8292012-06-05 14:25:27 +00001//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
Kostya Serebryany1b712072012-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 Samsonovef2e2cf2012-06-05 13:50:57 +000011// It contains macro used in run-time libraries code.
Kostya Serebryany1b712072012-05-31 14:11:07 +000012//===----------------------------------------------------------------------===//
13#ifndef SANITIZER_DEFS_H
14#define SANITIZER_DEFS_H
15
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000016#include "sanitizer_interface_defs.h"
17using namespace __sanitizer; // NOLINT
Kostya Serebryany1b712072012-05-31 14:11:07 +000018// ----------- ATTENTION -------------
19// This header should NOT include any other headers to avoid portability issues.
20
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000021// Common defs.
22#define INLINE static inline
23#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
24#define WEAK SANITIZER_WEAK_ATTRIBUTE
25
26// Platform-specific defs.
Kostya Serebryany1b712072012-05-31 14:11:07 +000027#if defined(_WIN32)
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000028typedef unsigned long DWORD; // NOLINT
Dmitry Vyukov090f3452012-06-27 21:00:23 +000029# define ALWAYS_INLINE __declspec(forceinline)
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000030// FIXME(timurrrr): do we need this on Windows?
31# define ALIAS(x)
32# define ALIGNED(x) __declspec(align(x))
Alexey Samsonov8bafdd02012-06-06 13:37:02 +000033# define FORMAT(f, a)
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000034# define NOINLINE __declspec(noinline)
35# define NORETURN __declspec(noreturn)
36# define THREADLOCAL __declspec(thread)
Dmitry Vyukov090f3452012-06-27 21:00:23 +000037# define NOTHROW
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000038#else // _WIN32
Dmitry Vyukov090f3452012-06-27 21:00:23 +000039# define ALWAYS_INLINE __attribute__((always_inline))
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000040# define ALIAS(x) __attribute__((alias(x)))
41# define ALIGNED(x) __attribute__((aligned(x)))
Alexey Samsonov8bafdd02012-06-06 13:37:02 +000042# define FORMAT(f, a) __attribute__((format(printf, f, a)))
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000043# define NOINLINE __attribute__((noinline))
44# define NORETURN __attribute__((noreturn))
45# define THREADLOCAL __thread
Dmitry Vyukov090f3452012-06-27 21:00:23 +000046# ifdef __cplusplus
47# define NOTHROW throw()
48# else
49# define NOTHROW __attribute__((__nothrow__))
50#endif
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000051#endif // _WIN32
52
53// We have no equivalent of these on Windows.
54#ifndef _WIN32
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000055# define LIKELY(x) __builtin_expect(!!(x), 1)
56# define UNLIKELY(x) __builtin_expect(!!(x), 0)
Alexey Samsonovaac5d0c2012-06-14 14:02:32 +000057# define UNUSED __attribute__((unused))
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000058# define USED __attribute__((used))
Kostya Serebryany1b712072012-05-31 14:11:07 +000059#endif
60
Alexey Samsonov40e51282012-06-15 07:29:14 +000061#if defined(_WIN32)
62typedef DWORD thread_return_t;
63# define THREAD_CALLING_CONV __stdcall
64#else // _WIN32
65typedef void* thread_return_t;
66# define THREAD_CALLING_CONV
67#endif // _WIN32
68typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
69
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000070// If __WORDSIZE was undefined by the platform, define it in terms of the
71// compiler built-ins __LP64__ and _WIN64.
72#ifndef __WORDSIZE
73# if __LP64__ || defined(_WIN64)
74# define __WORDSIZE 64
75# else
76# define __WORDSIZE 32
77# endif
78#endif // __WORDSIZE
Kostya Serebryany1b712072012-05-31 14:11:07 +000079
Alexey Samsonove4287792012-06-06 15:22:20 +000080// NOTE: Functions below must be defined in each run-time.
81namespace __sanitizer {
82void NORETURN Die();
83void NORETURN CheckFailed(const char *file, int line, const char *cond,
84 u64 v1, u64 v2);
85} // namespace __sanitizer
86
87// Check macro
Alexey Samsonovee072902012-06-06 09:26:25 +000088#define RAW_CHECK_MSG(expr, msg) do { \
89 if (!(expr)) { \
90 RawWrite(msg); \
91 Die(); \
92 } \
93} while (0)
94
95#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
96
Alexey Samsonove4287792012-06-06 15:22:20 +000097#define CHECK_IMPL(c1, op, c2) \
98 do { \
99 __sanitizer::u64 v1 = (u64)(c1); \
100 __sanitizer::u64 v2 = (u64)(c2); \
101 if (!(v1 op v2)) \
102 __sanitizer::CheckFailed(__FILE__, __LINE__, \
103 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
104 } while (false) \
105/**/
106
107#define CHECK(a) CHECK_IMPL((a), !=, 0)
108#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
109#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
110#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
111#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
112#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
113#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
114
Dmitry Vyukov6fa46f72012-06-29 16:58:33 +0000115#if TSAN_DEBUG
116#define DCHECK(a) CHECK(a)
117#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
118#define DCHECK_NE(a, b) CHECK_NE(a, b)
119#define DCHECK_LT(a, b) CHECK_LT(a, b)
120#define DCHECK_LE(a, b) CHECK_LE(a, b)
121#define DCHECK_GT(a, b) CHECK_GT(a, b)
122#define DCHECK_GE(a, b) CHECK_GE(a, b)
123#else
124#define DCHECK(a)
125#define DCHECK_EQ(a, b)
126#define DCHECK_NE(a, b)
127#define DCHECK_LT(a, b)
128#define DCHECK_LE(a, b)
129#define DCHECK_GT(a, b)
130#define DCHECK_GE(a, b)
131#endif
132
Alexey Samsonove95e29c2012-06-06 15:47:40 +0000133#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
134
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000135#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
136
Kostya Serebryanyccc470c2012-08-28 11:54:51 +0000137#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
138
Kostya Serebryany6e26fa92012-06-21 10:04:36 +0000139#define IMPL_PASTE(a, b) a##b
140#define IMPL_COMPILER_ASSERT(pred, line) \
141 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1];
142
Alexey Samsonov156958d2012-06-15 13:09:52 +0000143// Limits for integral types. We have to redefine it in case we don't
144// have stdint.h (like in Visual Studio 9).
145#if __WORDSIZE == 64
146# define __INT64_C(c) c ## L
147# define __UINT64_C(c) c ## UL
148#else
149# define __INT64_C(c) c ## LL
150# define __UINT64_C(c) c ## ULL
151#endif // __WORDSIZE == 64
152#undef INT32_MIN
153#define INT32_MIN (-2147483647-1)
154#undef INT32_MAX
155#define INT32_MAX (2147483647)
156#undef UINT32_MAX
157#define UINT32_MAX (4294967295U)
158#undef INT64_MIN
159#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
160#undef INT64_MAX
161#define INT64_MAX (__INT64_C(9223372036854775807))
162#undef UINT64_MAX
163#define UINT64_MAX (__UINT64_C(18446744073709551615))
164
Alexey Samsonovdc8d1f12012-08-27 09:30:58 +0000165enum LinkerInitialized { LINKER_INITIALIZED = 0 };
166
Kostya Serebryany1b712072012-05-31 14:11:07 +0000167#endif // SANITIZER_DEFS_H