Daily bump.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_platform.h
blob3e1b078a0212f5e2e2733d765bbae58527b17c0a
1 //===-- sanitizer_platform.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common platform macros.
10 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_PLATFORM_H
13 #define SANITIZER_PLATFORM_H
15 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
16 !defined(__APPLE__) && !defined(_WIN32) && !defined(__Fuchsia__) && \
17 !(defined(__sun__) && defined(__svr4__))
18 # error "This operating system is not supported"
19 #endif
21 // Get __GLIBC__ on a glibc platform. Exclude Android: features.h includes C
22 // function declarations into a .S file which doesn't compile.
23 // https://crbug.com/1162741
24 #if __has_include(<features.h>) && !defined(__ANDROID__)
25 # include <features.h>
26 #endif
28 #if defined(__linux__)
29 # define SANITIZER_LINUX 1
30 #else
31 # define SANITIZER_LINUX 0
32 #endif
34 #if defined(__GLIBC__)
35 # define SANITIZER_GLIBC 1
36 #else
37 # define SANITIZER_GLIBC 0
38 #endif
40 #if defined(__FreeBSD__)
41 # define SANITIZER_FREEBSD 1
42 #else
43 # define SANITIZER_FREEBSD 0
44 #endif
46 #if defined(__NetBSD__)
47 # define SANITIZER_NETBSD 1
48 #else
49 # define SANITIZER_NETBSD 0
50 #endif
52 #if defined(__sun__) && defined(__svr4__)
53 # define SANITIZER_SOLARIS 1
54 #else
55 # define SANITIZER_SOLARIS 0
56 #endif
58 // - SANITIZER_APPLE: all Apple code
59 // - TARGET_OS_OSX: macOS
60 // - SANITIZER_IOS: devices (iOS and iOS-like)
61 // - SANITIZER_WATCHOS
62 // - SANITIZER_TVOS
63 // - SANITIZER_IOSSIM: simulators (iOS and iOS-like)
64 // - SANITIZER_DRIVERKIT
65 #if defined(__APPLE__)
66 # define SANITIZER_APPLE 1
67 # include <TargetConditionals.h>
68 # if TARGET_OS_OSX
69 # define SANITIZER_OSX 1
70 # else
71 # define SANITIZER_OSX 0
72 # endif
73 # if TARGET_OS_IPHONE
74 # define SANITIZER_IOS 1
75 # else
76 # define SANITIZER_IOS 0
77 # endif
78 # if TARGET_OS_WATCH
79 # define SANITIZER_WATCHOS 1
80 # else
81 # define SANITIZER_WATCHOS 0
82 # endif
83 # if TARGET_OS_TV
84 # define SANITIZER_TVOS 1
85 # else
86 # define SANITIZER_TVOS 0
87 # endif
88 # if TARGET_OS_SIMULATOR
89 # define SANITIZER_IOSSIM 1
90 # else
91 # define SANITIZER_IOSSIM 0
92 # endif
93 # if defined(TARGET_OS_DRIVERKIT) && TARGET_OS_DRIVERKIT
94 # define SANITIZER_DRIVERKIT 1
95 # else
96 # define SANITIZER_DRIVERKIT 0
97 # endif
98 #else
99 # define SANITIZER_APPLE 0
100 # define SANITIZER_OSX 0
101 # define SANITIZER_IOS 0
102 # define SANITIZER_WATCHOS 0
103 # define SANITIZER_TVOS 0
104 # define SANITIZER_IOSSIM 0
105 # define SANITIZER_DRIVERKIT 0
106 #endif
108 #if defined(_WIN32)
109 # define SANITIZER_WINDOWS 1
110 #else
111 # define SANITIZER_WINDOWS 0
112 #endif
114 #if defined(_WIN64)
115 # define SANITIZER_WINDOWS64 1
116 #else
117 # define SANITIZER_WINDOWS64 0
118 #endif
120 #if defined(__ANDROID__)
121 # define SANITIZER_ANDROID 1
122 #else
123 # define SANITIZER_ANDROID 0
124 #endif
126 #if defined(__Fuchsia__)
127 # define SANITIZER_FUCHSIA 1
128 #else
129 # define SANITIZER_FUCHSIA 0
130 #endif
132 // Assume linux that is not glibc or android is musl libc.
133 #if SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID
134 # define SANITIZER_MUSL 1
135 #else
136 # define SANITIZER_MUSL 0
137 #endif
139 #define SANITIZER_POSIX \
140 (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_APPLE || \
141 SANITIZER_NETBSD || SANITIZER_SOLARIS)
143 #if __LP64__ || defined(_WIN64)
144 # define SANITIZER_WORDSIZE 64
145 #else
146 # define SANITIZER_WORDSIZE 32
147 #endif
149 #if SANITIZER_WORDSIZE == 64
150 # define FIRST_32_SECOND_64(a, b) (b)
151 #else
152 # define FIRST_32_SECOND_64(a, b) (a)
153 #endif
155 #if defined(__x86_64__) && !defined(_LP64)
156 # define SANITIZER_X32 1
157 #else
158 # define SANITIZER_X32 0
159 #endif
161 #if defined(__x86_64__) || defined(_M_X64)
162 # define SANITIZER_X64 1
163 #else
164 # define SANITIZER_X64 0
165 #endif
167 #if defined(__i386__) || defined(_M_IX86)
168 # define SANITIZER_I386 1
169 #else
170 # define SANITIZER_I386 0
171 #endif
173 #if defined(__mips__)
174 # define SANITIZER_MIPS 1
175 # if defined(__mips64) && _MIPS_SIM == _ABI64
176 # define SANITIZER_MIPS32 0
177 # define SANITIZER_MIPS64 1
178 # else
179 # define SANITIZER_MIPS32 1
180 # define SANITIZER_MIPS64 0
181 # endif
182 #else
183 # define SANITIZER_MIPS 0
184 # define SANITIZER_MIPS32 0
185 # define SANITIZER_MIPS64 0
186 #endif
188 #if defined(__s390__)
189 # define SANITIZER_S390 1
190 # if defined(__s390x__)
191 # define SANITIZER_S390_31 0
192 # define SANITIZER_S390_64 1
193 # else
194 # define SANITIZER_S390_31 1
195 # define SANITIZER_S390_64 0
196 # endif
197 #else
198 # define SANITIZER_S390 0
199 # define SANITIZER_S390_31 0
200 # define SANITIZER_S390_64 0
201 #endif
203 #if defined(__sparc__)
204 # define SANITIZER_SPARC 1
205 # if defined(__arch64__)
206 # define SANITIZER_SPARC32 0
207 # define SANITIZER_SPARC64 1
208 # else
209 # define SANITIZER_SPARC32 1
210 # define SANITIZER_SPARC64 0
211 # endif
212 #else
213 # define SANITIZER_SPARC 0
214 # define SANITIZER_SPARC32 0
215 # define SANITIZER_SPARC64 0
216 #endif
218 #if defined(__powerpc__)
219 # define SANITIZER_PPC 1
220 # if defined(__powerpc64__)
221 # define SANITIZER_PPC32 0
222 # define SANITIZER_PPC64 1
223 // 64-bit PPC has two ABIs (v1 and v2). The old powerpc64 target is
224 // big-endian, and uses v1 ABI (known for its function descriptors),
225 // while the new powerpc64le target is little-endian and uses v2.
226 // In theory, you could convince gcc to compile for their evil twins
227 // (eg. big-endian v2), but you won't find such combinations in the wild
228 // (it'd require bootstrapping a whole system, which would be quite painful
229 // - there's no target triple for that). LLVM doesn't support them either.
230 # if _CALL_ELF == 2
231 # define SANITIZER_PPC64V1 0
232 # define SANITIZER_PPC64V2 1
233 # else
234 # define SANITIZER_PPC64V1 1
235 # define SANITIZER_PPC64V2 0
236 # endif
237 # else
238 # define SANITIZER_PPC32 1
239 # define SANITIZER_PPC64 0
240 # define SANITIZER_PPC64V1 0
241 # define SANITIZER_PPC64V2 0
242 # endif
243 #else
244 # define SANITIZER_PPC 0
245 # define SANITIZER_PPC32 0
246 # define SANITIZER_PPC64 0
247 # define SANITIZER_PPC64V1 0
248 # define SANITIZER_PPC64V2 0
249 #endif
251 #if defined(__arm__) || defined(_M_ARM)
252 # define SANITIZER_ARM 1
253 #else
254 # define SANITIZER_ARM 0
255 #endif
257 #if defined(__aarch64__) || defined(_M_ARM64)
258 # define SANITIZER_ARM64 1
259 #else
260 # define SANITIZER_ARM64 0
261 #endif
263 #if SANITIZER_SOLARIS && SANITIZER_WORDSIZE == 32
264 # define SANITIZER_SOLARIS32 1
265 #else
266 # define SANITIZER_SOLARIS32 0
267 #endif
269 #if defined(__riscv) && (__riscv_xlen == 64)
270 # define SANITIZER_RISCV64 1
271 #else
272 # define SANITIZER_RISCV64 0
273 #endif
275 #if defined(__loongarch_lp64)
276 # define SANITIZER_LOONGARCH64 1
277 #else
278 # define SANITIZER_LOONGARCH64 0
279 #endif
281 // By default we allow to use SizeClassAllocator64 on 64-bit platform.
282 // But in some cases SizeClassAllocator64 does not work well and we need to
283 // fallback to SizeClassAllocator32.
284 // For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
285 // change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
286 #ifndef SANITIZER_CAN_USE_ALLOCATOR64
287 # if (SANITIZER_RISCV64 && !SANITIZER_FUCHSIA) || SANITIZER_IOS || \
288 SANITIZER_DRIVERKIT
289 # define SANITIZER_CAN_USE_ALLOCATOR64 0
290 # elif defined(__mips64) || defined(__hexagon__)
291 # define SANITIZER_CAN_USE_ALLOCATOR64 0
292 # else
293 # define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64)
294 # endif
295 #endif
297 // The range of addresses which can be returned my mmap.
298 // FIXME: this value should be different on different platforms. Larger values
299 // will still work but will consume more memory for TwoLevelByteMap.
300 #if defined(__mips__)
301 # if SANITIZER_GO && defined(__mips64)
302 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
303 # else
304 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 40)
305 # endif
306 #elif SANITIZER_RISCV64
307 // FIXME: Rather than hardcoding the VMA here, we should rely on
308 // GetMaxUserVirtualAddress(). This will require some refactoring though since
309 // many places either hardcode some value or SANITIZER_MMAP_RANGE_SIZE is
310 // assumed to be some constant integer.
311 # if SANITIZER_FUCHSIA
312 # define SANITIZER_MMAP_RANGE_SIZE (1ULL << 38)
313 # else
314 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
315 # endif
316 #elif defined(__aarch64__)
317 # if SANITIZER_APPLE
318 # if SANITIZER_OSX || SANITIZER_IOSSIM
319 # define SANITIZER_MMAP_RANGE_SIZE \
320 FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
321 # else
322 // Darwin iOS/ARM64 has a 36-bit VMA, 64GiB VM
323 # define SANITIZER_MMAP_RANGE_SIZE \
324 FIRST_32_SECOND_64(1ULL << 32, 1ULL << 36)
325 # endif
326 # else
327 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48)
328 # endif
329 #elif defined(__sparc__)
330 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 52)
331 #else
332 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
333 #endif
335 // Whether the addresses are sign-extended from the VMA range to the word.
336 // The SPARC64 Linux port implements this to split the VMA space into two
337 // non-contiguous halves with a huge hole in the middle.
338 #if defined(__sparc__) && SANITIZER_WORDSIZE == 64
339 # define SANITIZER_SIGN_EXTENDED_ADDRESSES 1
340 #else
341 # define SANITIZER_SIGN_EXTENDED_ADDRESSES 0
342 #endif
344 // udi16 syscalls can only be used when the following conditions are
345 // met:
346 // * target is one of arm32, x86-32, sparc32, sh or m68k
347 // * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15
348 // built against > linux-2.2 kernel headers
349 // Since we don't want to include libc headers here, we check the
350 // target only.
351 #if defined(__arm__) || SANITIZER_X32 || defined(__sparc__)
352 # define SANITIZER_USES_UID16_SYSCALLS 1
353 #else
354 # define SANITIZER_USES_UID16_SYSCALLS 0
355 #endif
357 #if defined(__mips__)
358 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10)
359 #else
360 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
361 #endif
363 /// \macro MSC_PREREQ
364 /// \brief Is the compiler MSVC of at least the specified version?
365 /// The common \param version values to check for are:
366 /// * 1800: Microsoft Visual Studio 2013 / 12.0
367 /// * 1900: Microsoft Visual Studio 2015 / 14.0
368 #ifdef _MSC_VER
369 # define MSC_PREREQ(version) (_MSC_VER >= (version))
370 #else
371 # define MSC_PREREQ(version) 0
372 #endif
374 #if SANITIZER_APPLE && defined(__x86_64__)
375 # define SANITIZER_NON_UNIQUE_TYPEINFO 0
376 #else
377 # define SANITIZER_NON_UNIQUE_TYPEINFO 1
378 #endif
380 // On linux, some architectures had an ABI transition from 64-bit long double
381 // (ie. same as double) to 128-bit long double. On those, glibc symbols
382 // involving long doubles come in two versions, and we need to pass the
383 // correct one to dlvsym when intercepting them.
384 #if SANITIZER_LINUX && (SANITIZER_S390 || SANITIZER_PPC32 || SANITIZER_PPC64V1)
385 # define SANITIZER_NLDBL_VERSION "GLIBC_2.4"
386 #endif
388 #if SANITIZER_GO == 0
389 # define SANITIZER_GO 0
390 #endif
392 // On PowerPC and ARM Thumb, calling pthread_exit() causes LSan to detect leaks.
393 // pthread_exit() performs unwinding that leads to dlopen'ing libgcc_s.so.
394 // dlopen mallocs "libgcc_s.so" string which confuses LSan, it fails to realize
395 // that this allocation happens in dynamic linker and should be ignored.
396 #if SANITIZER_PPC || defined(__thumb__)
397 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 1
398 #else
399 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 0
400 #endif
402 #if SANITIZER_FREEBSD || SANITIZER_APPLE || SANITIZER_NETBSD || SANITIZER_SOLARIS
403 # define SANITIZER_MADVISE_DONTNEED MADV_FREE
404 #else
405 # define SANITIZER_MADVISE_DONTNEED MADV_DONTNEED
406 #endif
408 // Older gcc have issues aligning to a constexpr, and require an integer.
409 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859 among others.
410 #if defined(__powerpc__) || defined(__powerpc64__)
411 # define SANITIZER_CACHE_LINE_SIZE 128
412 #else
413 # define SANITIZER_CACHE_LINE_SIZE 64
414 #endif
416 // Enable offline markup symbolizer for Fuchsia.
417 #if SANITIZER_FUCHSIA
418 # define SANITIZER_SYMBOLIZER_MARKUP 1
419 #else
420 # define SANITIZER_SYMBOLIZER_MARKUP 0
421 #endif
423 // Enable ability to support sanitizer initialization that is
424 // compatible with the sanitizer library being loaded via
425 // `dlopen()`.
426 #if SANITIZER_APPLE
427 # define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 1
428 #else
429 # define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 0
430 #endif
432 // SANITIZER_SUPPORTS_THREADLOCAL
433 // 1 - THREADLOCAL macro is supported by target
434 // 0 - THREADLOCAL macro is not supported by target
435 #ifndef __has_feature
436 // TODO: Support other compilers here
437 # define SANITIZER_SUPPORTS_THREADLOCAL 1
438 #else
439 # if __has_feature(tls)
440 # define SANITIZER_SUPPORTS_THREADLOCAL 1
441 # else
442 # define SANITIZER_SUPPORTS_THREADLOCAL 0
443 # endif
444 #endif
446 #if defined(__thumb__) && defined(__linux__)
447 // Workaround for
448 // https://lab.llvm.org/buildbot/#/builders/clang-thumbv7-full-2stage
449 // or
450 // https://lab.llvm.org/staging/#/builders/clang-thumbv7-full-2stage
451 // It fails *rss_limit_mb_test* without meaningful errors.
452 # define SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 1
453 #else
454 # define SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 0
455 #endif
457 #endif // SANITIZER_PLATFORM_H