xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/sanitizer_common/sanitizer_platform.h (revision a7c257b03e4462df2b1020128fb82716512d7856)
1 //===-- sanitizer_platform.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 // Common platform macros.
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef SANITIZER_PLATFORM_H
14 #define SANITIZER_PLATFORM_H
15 
16 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
17   !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(_WIN32) && \
18   !defined(__Fuchsia__) && !defined(__rtems__) && \
19   !(defined(__sun__) && defined(__svr4__))
20 # error "This operating system is not supported"
21 #endif
22 
23 #if defined(__linux__)
24 # define SANITIZER_LINUX   1
25 #else
26 # define SANITIZER_LINUX   0
27 #endif
28 
29 #if defined(__FreeBSD__)
30 # define SANITIZER_FREEBSD 1
31 #else
32 # define SANITIZER_FREEBSD 0
33 #endif
34 
35 #if defined(__NetBSD__)
36 # define SANITIZER_NETBSD 1
37 #else
38 # define SANITIZER_NETBSD 0
39 #endif
40 
41 #if defined(__OpenBSD__)
42 # define SANITIZER_OPENBSD 1
43 #else
44 # define SANITIZER_OPENBSD 0
45 #endif
46 
47 #if defined(__sun__) && defined(__svr4__)
48 # define SANITIZER_SOLARIS 1
49 #else
50 # define SANITIZER_SOLARIS 0
51 #endif
52 
53 #if defined(__APPLE__)
54 # define SANITIZER_MAC     1
55 # include <TargetConditionals.h>
56 # if TARGET_OS_IPHONE
57 #  define SANITIZER_IOS    1
58 # else
59 #  define SANITIZER_IOS    0
60 # endif
61 # if TARGET_OS_SIMULATOR
62 #  define SANITIZER_IOSSIM 1
63 # else
64 #  define SANITIZER_IOSSIM 0
65 # endif
66 #else
67 # define SANITIZER_MAC     0
68 # define SANITIZER_IOS     0
69 # define SANITIZER_IOSSIM  0
70 #endif
71 
72 #if defined(__APPLE__) && TARGET_OS_IPHONE && TARGET_OS_WATCH
73 # define SANITIZER_WATCHOS 1
74 #else
75 # define SANITIZER_WATCHOS 0
76 #endif
77 
78 #if defined(__APPLE__) && TARGET_OS_IPHONE && TARGET_OS_TV
79 # define SANITIZER_TVOS 1
80 #else
81 # define SANITIZER_TVOS 0
82 #endif
83 
84 #if defined(_WIN32)
85 # define SANITIZER_WINDOWS 1
86 #else
87 # define SANITIZER_WINDOWS 0
88 #endif
89 
90 #if defined(_WIN64)
91 # define SANITIZER_WINDOWS64 1
92 #else
93 # define SANITIZER_WINDOWS64 0
94 #endif
95 
96 #if defined(__ANDROID__)
97 # define SANITIZER_ANDROID 1
98 #else
99 # define SANITIZER_ANDROID 0
100 #endif
101 
102 #if defined(__Fuchsia__)
103 # define SANITIZER_FUCHSIA 1
104 #else
105 # define SANITIZER_FUCHSIA 0
106 #endif
107 
108 #if defined(__rtems__)
109 # define SANITIZER_RTEMS 1
110 #else
111 # define SANITIZER_RTEMS 0
112 #endif
113 
114 #define SANITIZER_POSIX \
115   (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC || \
116     SANITIZER_NETBSD || SANITIZER_OPENBSD || SANITIZER_SOLARIS)
117 
118 #if __LP64__ || defined(_WIN64)
119 #  define SANITIZER_WORDSIZE 64
120 #else
121 #  define SANITIZER_WORDSIZE 32
122 #endif
123 
124 #if SANITIZER_WORDSIZE == 64
125 # define FIRST_32_SECOND_64(a, b) (b)
126 #else
127 # define FIRST_32_SECOND_64(a, b) (a)
128 #endif
129 
130 #if defined(__x86_64__) && !defined(_LP64)
131 # define SANITIZER_X32 1
132 #else
133 # define SANITIZER_X32 0
134 #endif
135 
136 #if defined(__mips__)
137 # define SANITIZER_MIPS 1
138 # if defined(__mips64)
139 #  define SANITIZER_MIPS32 0
140 #  define SANITIZER_MIPS64 1
141 # else
142 #  define SANITIZER_MIPS32 1
143 #  define SANITIZER_MIPS64 0
144 # endif
145 #else
146 # define SANITIZER_MIPS 0
147 # define SANITIZER_MIPS32 0
148 # define SANITIZER_MIPS64 0
149 #endif
150 
151 #if defined(__s390__)
152 # define SANITIZER_S390 1
153 # if defined(__s390x__)
154 #  define SANITIZER_S390_31 0
155 #  define SANITIZER_S390_64 1
156 # else
157 #  define SANITIZER_S390_31 1
158 #  define SANITIZER_S390_64 0
159 # endif
160 #else
161 # define SANITIZER_S390 0
162 # define SANITIZER_S390_31 0
163 # define SANITIZER_S390_64 0
164 #endif
165 
166 #if defined(__powerpc__)
167 # define SANITIZER_PPC 1
168 # if defined(__powerpc64__)
169 #  define SANITIZER_PPC32 0
170 #  define SANITIZER_PPC64 1
171 // 64-bit PPC has two ABIs (v1 and v2).  The old powerpc64 target is
172 // big-endian, and uses v1 ABI (known for its function descriptors),
173 // while the new powerpc64le target is little-endian and uses v2.
174 // In theory, you could convince gcc to compile for their evil twins
175 // (eg. big-endian v2), but you won't find such combinations in the wild
176 // (it'd require bootstrapping a whole system, which would be quite painful
177 // - there's no target triple for that).  LLVM doesn't support them either.
178 #  if _CALL_ELF == 2
179 #   define SANITIZER_PPC64V1 0
180 #   define SANITIZER_PPC64V2 1
181 #  else
182 #   define SANITIZER_PPC64V1 1
183 #   define SANITIZER_PPC64V2 0
184 #  endif
185 # else
186 #  define SANITIZER_PPC32 1
187 #  define SANITIZER_PPC64 0
188 #  define SANITIZER_PPC64V1 0
189 #  define SANITIZER_PPC64V2 0
190 # endif
191 #else
192 # define SANITIZER_PPC 0
193 # define SANITIZER_PPC32 0
194 # define SANITIZER_PPC64 0
195 # define SANITIZER_PPC64V1 0
196 # define SANITIZER_PPC64V2 0
197 #endif
198 
199 #if defined(__arm__)
200 # define SANITIZER_ARM 1
201 #else
202 # define SANITIZER_ARM 0
203 #endif
204 
205 #if SANITIZER_SOLARIS && SANITIZER_WORDSIZE == 32
206 # define SANITIZER_SOLARIS32 1
207 #else
208 # define SANITIZER_SOLARIS32 0
209 #endif
210 
211 #if defined(__myriad2__)
212 # define SANITIZER_MYRIAD2 1
213 #else
214 # define SANITIZER_MYRIAD2 0
215 #endif
216 
217 // By default we allow to use SizeClassAllocator64 on 64-bit platform.
218 // But in some cases (e.g. AArch64's 39-bit address space) SizeClassAllocator64
219 // does not work well and we need to fallback to SizeClassAllocator32.
220 // For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
221 // change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
222 #ifndef SANITIZER_CAN_USE_ALLOCATOR64
223 # if (SANITIZER_ANDROID && defined(__aarch64__)) || SANITIZER_FUCHSIA
224 #  define SANITIZER_CAN_USE_ALLOCATOR64 1
225 # elif defined(__mips64) || defined(__aarch64__)
226 #  define SANITIZER_CAN_USE_ALLOCATOR64 0
227 # else
228 #  define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64)
229 # endif
230 #endif
231 
232 // The range of addresses which can be returned my mmap.
233 // FIXME: this value should be different on different platforms.  Larger values
234 // will still work but will consume more memory for TwoLevelByteMap.
235 #if defined(__mips__)
236 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 40)
237 #elif defined(__aarch64__)
238 # if SANITIZER_MAC
239 // Darwin iOS/ARM64 has a 36-bit VMA, 64GiB VM
240 #  define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 36)
241 # else
242 #  define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48)
243 # endif
244 #else
245 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
246 #endif
247 
248 // The AArch64 linux port uses the canonical syscall set as mandated by
249 // the upstream linux community for all new ports. Other ports may still
250 // use legacy syscalls.
251 #ifndef SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
252 # if defined(__aarch64__) && SANITIZER_LINUX
253 # define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 1
254 # else
255 # define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 0
256 # endif
257 #endif
258 
259 // udi16 syscalls can only be used when the following conditions are
260 // met:
261 // * target is one of arm32, x86-32, sparc32, sh or m68k
262 // * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15
263 //   built against > linux-2.2 kernel headers
264 // Since we don't want to include libc headers here, we check the
265 // target only.
266 #if defined(__arm__) || SANITIZER_X32 || defined(__sparc__)
267 #define SANITIZER_USES_UID16_SYSCALLS 1
268 #else
269 #define SANITIZER_USES_UID16_SYSCALLS 0
270 #endif
271 
272 #if defined(__mips__)
273 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10)
274 #else
275 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
276 #endif
277 
278 // Assume obsolete RPC headers are available by default
279 #if !defined(HAVE_RPC_XDR_H) && !defined(HAVE_TIRPC_RPC_XDR_H)
280 # define HAVE_RPC_XDR_H (SANITIZER_LINUX && !SANITIZER_ANDROID)
281 # define HAVE_TIRPC_RPC_XDR_H 0
282 #endif
283 
284 /// \macro MSC_PREREQ
285 /// \brief Is the compiler MSVC of at least the specified version?
286 /// The common \param version values to check for are:
287 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
288 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
289 #ifdef _MSC_VER
290 # define MSC_PREREQ(version) (_MSC_VER >= (version))
291 #else
292 # define MSC_PREREQ(version) 0
293 #endif
294 
295 #if defined(__arm64__) && SANITIZER_IOS
296 # define SANITIZER_NON_UNIQUE_TYPEINFO 1
297 #else
298 # define SANITIZER_NON_UNIQUE_TYPEINFO 0
299 #endif
300 
301 // On linux, some architectures had an ABI transition from 64-bit long double
302 // (ie. same as double) to 128-bit long double.  On those, glibc symbols
303 // involving long doubles come in two versions, and we need to pass the
304 // correct one to dlvsym when intercepting them.
305 #if SANITIZER_LINUX && (SANITIZER_S390 || SANITIZER_PPC32 || SANITIZER_PPC64V1)
306 #define SANITIZER_NLDBL_VERSION "GLIBC_2.4"
307 #endif
308 
309 #if SANITIZER_GO == 0
310 # define SANITIZER_GO 0
311 #endif
312 
313 // On PowerPC and ARM Thumb, calling pthread_exit() causes LSan to detect leaks.
314 // pthread_exit() performs unwinding that leads to dlopen'ing libgcc_s.so.
315 // dlopen mallocs "libgcc_s.so" string which confuses LSan, it fails to realize
316 // that this allocation happens in dynamic linker and should be ignored.
317 #if SANITIZER_PPC || defined(__thumb__)
318 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 1
319 #else
320 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 0
321 #endif
322 
323 #if SANITIZER_FREEBSD || SANITIZER_MAC || SANITIZER_NETBSD || \
324   SANITIZER_OPENBSD || SANITIZER_SOLARIS
325 # define SANITIZER_MADVISE_DONTNEED MADV_FREE
326 #else
327 # define SANITIZER_MADVISE_DONTNEED MADV_DONTNEED
328 #endif
329 
330 // Older gcc have issues aligning to a constexpr, and require an integer.
331 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859 among others.
332 #if defined(__powerpc__) || defined(__powerpc64__)
333 # define SANITIZER_CACHE_LINE_SIZE 128
334 #else
335 # define SANITIZER_CACHE_LINE_SIZE 64
336 #endif
337 
338 // Enable offline markup symbolizer for Fuchsia and RTEMS.
339 #if SANITIZER_FUCHSIA || SANITIZER_RTEMS
340 #define SANITIZER_SYMBOLIZER_MARKUP 1
341 #else
342 #define SANITIZER_SYMBOLIZER_MARKUP 0
343 #endif
344 
345 // Enable ability to support sanitizer initialization that is
346 // compatible with the sanitizer library being loaded via
347 // `dlopen()`.
348 #if SANITIZER_MAC
349 #define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 1
350 #else
351 #define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 0
352 #endif
353 
354 #endif // SANITIZER_PLATFORM_H
355