[Sanitizer] extend internal libc with stat/fstat/lstat functions
[blocksruntime.git] / lib / sanitizer_common / sanitizer_internal_defs.h
blobb0ce886e3564c58c264c09e263dfcd43b8bad0b9
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
16 #if defined(_WIN32)
17 // FIXME find out what we need on Windows. __declspec(dllexport) ?
18 # define SANITIZER_INTERFACE_ATTRIBUTE
19 # define SANITIZER_WEAK_ATTRIBUTE
20 #elif defined(SANITIZER_GO)
21 # define SANITIZER_INTERFACE_ATTRIBUTE
22 # define SANITIZER_WEAK_ATTRIBUTE
23 #else
24 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
25 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
26 #endif
28 #ifdef __linux__
29 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
30 #else
31 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
32 #endif
34 // __has_feature
35 #if !defined(__has_feature)
36 # define __has_feature(x) 0
37 #endif
39 // For portability reasons we do not include stddef.h, stdint.h or any other
40 // system header, but we do need some basic types that are not defined
41 // in a portable way by the language itself.
42 namespace __sanitizer {
44 #if defined(_WIN64)
45 // 64-bit Windows uses LLP64 data model.
46 typedef unsigned long long uptr; // NOLINT
47 typedef signed long long sptr; // NOLINT
48 #else
49 typedef unsigned long uptr; // NOLINT
50 typedef signed long sptr; // NOLINT
51 #endif // defined(_WIN64)
52 #if defined(__x86_64__)
53 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
54 // 64-bit pointer to unwind stack frame.
55 typedef unsigned long long uhwptr; // NOLINT
56 #else
57 typedef uptr uhwptr; // NOLINT
58 #endif
59 typedef unsigned char u8;
60 typedef unsigned short u16; // NOLINT
61 typedef unsigned int u32;
62 typedef unsigned long long u64; // NOLINT
63 typedef signed char s8;
64 typedef signed short s16; // NOLINT
65 typedef signed int s32;
66 typedef signed long long s64; // NOLINT
67 typedef int fd_t;
69 } // namespace __sanitizer
71 extern "C" {
72 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
73 void __sanitizer_set_report_path(const char *path)
74 SANITIZER_INTERFACE_ATTRIBUTE;
76 // Tell the tools to write their reports to given file descriptor instead of
77 // stderr.
78 void __sanitizer_set_report_fd(int fd)
79 SANITIZER_INTERFACE_ATTRIBUTE;
81 // Notify the tools that the sandbox is going to be turned on. The reserved
82 // parameter will be used in the future to hold a structure with functions
83 // that the tools may call to bypass the sandbox.
84 void __sanitizer_sandbox_on_notify(void *reserved)
85 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
86 } // extern "C"
89 using namespace __sanitizer; // NOLINT
90 // ----------- ATTENTION -------------
91 // This header should NOT include any other headers to avoid portability issues.
93 // Common defs.
94 #define INLINE static inline
95 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
96 #define WEAK SANITIZER_WEAK_ATTRIBUTE
98 // Platform-specific defs.
99 #if defined(_MSC_VER)
100 # define ALWAYS_INLINE __declspec(forceinline)
101 // FIXME(timurrrr): do we need this on Windows?
102 # define ALIAS(x)
103 # define ALIGNED(x) __declspec(align(x))
104 # define FORMAT(f, a)
105 # define NOINLINE __declspec(noinline)
106 # define NORETURN __declspec(noreturn)
107 # define THREADLOCAL __declspec(thread)
108 # define NOTHROW
109 # define LIKELY(x) (x)
110 # define UNLIKELY(x) (x)
111 # define UNUSED
112 # define USED
113 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
114 #else // _MSC_VER
115 # define ALWAYS_INLINE __attribute__((always_inline))
116 # define ALIAS(x) __attribute__((alias(x)))
117 # define ALIGNED(x) __attribute__((aligned(x)))
118 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
119 # define NOINLINE __attribute__((noinline))
120 # define NORETURN __attribute__((noreturn))
121 # define THREADLOCAL __thread
122 # define NOTHROW throw()
123 # define LIKELY(x) __builtin_expect(!!(x), 1)
124 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
125 # define UNUSED __attribute__((unused))
126 # define USED __attribute__((used))
127 # if defined(__i386__) || defined(__x86_64__)
128 // __builtin_prefetch(x) generates prefetchnt0 on x86
129 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
130 # else
131 # define PREFETCH(x) __builtin_prefetch(x)
132 # endif
133 #endif // _MSC_VER
135 #if defined(_WIN32)
136 typedef unsigned long DWORD; // NOLINT
137 typedef DWORD thread_return_t;
138 # define THREAD_CALLING_CONV __stdcall
139 #else // _WIN32
140 typedef void* thread_return_t;
141 # define THREAD_CALLING_CONV
142 #endif // _WIN32
143 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
145 #if __LP64__ || defined(_WIN64)
146 # define SANITIZER_WORDSIZE 64
147 #else
148 # define SANITIZER_WORDSIZE 32
149 #endif
151 // NOTE: Functions below must be defined in each run-time.
152 namespace __sanitizer {
153 void NORETURN Die();
154 void NORETURN CheckFailed(const char *file, int line, const char *cond,
155 u64 v1, u64 v2);
156 } // namespace __sanitizer
158 // Check macro
159 #define RAW_CHECK_MSG(expr, msg) do { \
160 if (!(expr)) { \
161 RawWrite(msg); \
162 Die(); \
164 } while (0)
166 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
168 #define CHECK_IMPL(c1, op, c2) \
169 do { \
170 __sanitizer::u64 v1 = (u64)(c1); \
171 __sanitizer::u64 v2 = (u64)(c2); \
172 if (!(v1 op v2)) \
173 __sanitizer::CheckFailed(__FILE__, __LINE__, \
174 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
175 } while (false) \
176 /**/
178 #define CHECK(a) CHECK_IMPL((a), !=, 0)
179 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
180 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
181 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
182 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
183 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
184 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
186 #if TSAN_DEBUG
187 #define DCHECK(a) CHECK(a)
188 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
189 #define DCHECK_NE(a, b) CHECK_NE(a, b)
190 #define DCHECK_LT(a, b) CHECK_LT(a, b)
191 #define DCHECK_LE(a, b) CHECK_LE(a, b)
192 #define DCHECK_GT(a, b) CHECK_GT(a, b)
193 #define DCHECK_GE(a, b) CHECK_GE(a, b)
194 #else
195 #define DCHECK(a)
196 #define DCHECK_EQ(a, b)
197 #define DCHECK_NE(a, b)
198 #define DCHECK_LT(a, b)
199 #define DCHECK_LE(a, b)
200 #define DCHECK_GT(a, b)
201 #define DCHECK_GE(a, b)
202 #endif
204 #define UNREACHABLE(msg) do { \
205 CHECK(0 && msg); \
206 Die(); \
207 } while (0)
209 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
211 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
213 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
215 #define IMPL_PASTE(a, b) a##b
216 #define IMPL_COMPILER_ASSERT(pred, line) \
217 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
219 // Limits for integral types. We have to redefine it in case we don't
220 // have stdint.h (like in Visual Studio 9).
221 #undef __INT64_C
222 #undef __UINT64_C
223 #if SANITIZER_WORDSIZE == 64
224 # define __INT64_C(c) c ## L
225 # define __UINT64_C(c) c ## UL
226 #else
227 # define __INT64_C(c) c ## LL
228 # define __UINT64_C(c) c ## ULL
229 #endif // SANITIZER_WORDSIZE == 64
230 #undef INT32_MIN
231 #define INT32_MIN (-2147483647-1)
232 #undef INT32_MAX
233 #define INT32_MAX (2147483647)
234 #undef UINT32_MAX
235 #define UINT32_MAX (4294967295U)
236 #undef INT64_MIN
237 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
238 #undef INT64_MAX
239 #define INT64_MAX (__INT64_C(9223372036854775807))
240 #undef UINT64_MAX
241 #define UINT64_MAX (__UINT64_C(18446744073709551615))
243 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
245 #if !defined(_MSC_VER) || defined(__clang__)
246 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
247 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
248 #else
249 extern "C" void* _ReturnAddress(void);
250 # pragma intrinsic(_ReturnAddress)
251 # define GET_CALLER_PC() (uptr)_ReturnAddress()
252 // CaptureStackBackTrace doesn't need to know BP on Windows.
253 // FIXME: This macro is still used when printing error reports though it's not
254 // clear if the BP value is needed in the ASan reports on Windows.
255 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
256 #endif
258 #define HANDLE_EINTR(res, f) { \
259 do { \
260 res = (f); \
261 } while (res == -1 && errno == EINTR); \
264 #endif // SANITIZER_DEFS_H