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//===----------------------------------------------------------------------===//
11
12#ifndef SANITIZER_PLATFORM_H
13#define SANITIZER_PLATFORM_H
14
15#if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
16 !defined(__APPLE__) && !defined(_WIN32) && !defined(__Fuchsia__) && \
17 !(defined(__sun__) && defined(__svr4__)) && !defined(__HAIKU__) && \
18 !defined(__wasi__) && !defined(__NVPTX__) && !defined(__AMDGPU__) && \
19 !defined(__SPIRV__) && !defined(_AIX)
20# error "This operating system is not supported"
21#endif
22
23// Get __GLIBC__ on a glibc platform. Exclude Android: features.h includes C
24// function declarations into a .S file which doesn't compile.
25// https://crbug.com/1162741
26#if __has_include(<features.h>) && !defined(__ANDROID__)
27# include <features.h>
28#endif
29
30#if defined(__linux__)
31# define SANITIZER_LINUX 1
32#else
33# define SANITIZER_LINUX 0
34#endif
35
36#if defined(_AIX)
37# define SANITIZER_AIX 1
38#else
39# define SANITIZER_AIX 0
40#endif
41
42#if defined(__GLIBC__)
43# define SANITIZER_GLIBC 1
44#else
45# define SANITIZER_GLIBC 0
46#endif
47
48#if defined(__FreeBSD__)
49# define SANITIZER_FREEBSD 1
50#else
51# define SANITIZER_FREEBSD 0
52#endif
53
54#if defined(__NetBSD__)
55# define SANITIZER_NETBSD 1
56#else
57# define SANITIZER_NETBSD 0
58#endif
59
60#if defined(__sun__) && defined(__svr4__)
61# define SANITIZER_SOLARIS 1
62#else
63# define SANITIZER_SOLARIS 0
64#endif
65
66#if defined(__HAIKU__)
67# define SANITIZER_HAIKU 1
68#else
69# define SANITIZER_HAIKU 0
70#endif
71
72#if defined(__wasi__)
73# define SANITIZER_WASI 1
74#else
75# define SANITIZER_WASI 0
76#endif
77
78// - SANITIZER_APPLE: all Apple code
79// - SANITIZER_OSX: macOS
80// - SANITIZER_IOS: devices (iOS and iOS-like)
81// - SANITIZER_IPHONEOS: iOS only
82// - SANITIZER_WATCHOS
83// - SANITIZER_TVOS
84// - SANITIZER_VISIONOS
85// - SANITIZER_IOSSIM: simulators (iOS and iOS-like)
86// - SANITIZER_DRIVERKIT
87#if defined(__APPLE__)
88# define SANITIZER_APPLE 1
89# include <TargetConditionals.h>
90# if TARGET_OS_OSX
91# define SANITIZER_OSX 1
92# else
93# define SANITIZER_OSX 0
94# endif
95# if TARGET_OS_IPHONE
96# define SANITIZER_IOS 1
97# else
98# define SANITIZER_IOS 0
99# endif
100# if TARGET_OS_IOS
101# define SANITIZER_IPHONEOS 1
102# else
103# define SANITIZER_IPHONEOS 0
104# endif
105# if TARGET_OS_WATCH
106# define SANITIZER_WATCHOS 1
107# else
108# define SANITIZER_WATCHOS 0
109# endif
110# if TARGET_OS_TV
111# define SANITIZER_TVOS 1
112# else
113# define SANITIZER_TVOS 0
114# endif
115# if TARGET_OS_VISION
116# define SANITIZER_VISIONOS 1
117# else
118# define SANITIZER_VISIONOS 0
119# endif
120# if TARGET_OS_SIMULATOR
121# define SANITIZER_IOSSIM 1
122# else
123# define SANITIZER_IOSSIM 0
124# endif
125# if defined(TARGET_OS_DRIVERKIT) && TARGET_OS_DRIVERKIT
126# define SANITIZER_DRIVERKIT 1
127# else
128# define SANITIZER_DRIVERKIT 0
129# endif
130# define SANITIZER_IOSDEVICE (SANITIZER_IOS && !SANITIZER_IOSSIM)
131# define SANITIZER_EMBEDDED_VM_LAYOUT SANITIZER_IOSDEVICE
132#else
133# define SANITIZER_APPLE 0
134# define SANITIZER_OSX 0
135# define SANITIZER_IOS 0
136# define SANITIZER_IPHONEOS 0
137# define SANITIZER_WATCHOS 0
138# define SANITIZER_TVOS 0
139# define SANITIZER_VISIONOS 0
140# define SANITIZER_IOSSIM 0
141# define SANITIZER_DRIVERKIT 0
142# define SANITIZER_IOSDEVICE 0
143# define SANITIZER_EMBEDDED_VM_LAYOUT 0
144#endif
145
146#if defined(_WIN32)
147# define SANITIZER_WINDOWS 1
148#else
149# define SANITIZER_WINDOWS 0
150#endif
151
152#if defined(_WIN64)
153# define SANITIZER_WINDOWS64 1
154#else
155# define SANITIZER_WINDOWS64 0
156#endif
157
158#if defined(__ANDROID__)
159# define SANITIZER_ANDROID 1
160#else
161# define SANITIZER_ANDROID 0
162#endif
163
164#if defined(__Fuchsia__)
165# define SANITIZER_FUCHSIA 1
166#else
167# define SANITIZER_FUCHSIA 0
168#endif
169
170// Assume linux that is not glibc or android is musl libc.
171#if SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID
172# define SANITIZER_MUSL 1
173#else
174# define SANITIZER_MUSL 0
175#endif
176
177#define SANITIZER_POSIX \
178 (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_APPLE || \
179 SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU || SANITIZER_AIX)
180
181#if __LP64__ || defined(_WIN64)
182# define SANITIZER_WORDSIZE 64
183#else
184# define SANITIZER_WORDSIZE 32
185#endif
186
187#if SANITIZER_WORDSIZE == 64
188# define FIRST_32_SECOND_64(a, b) (b)
189#else
190# define FIRST_32_SECOND_64(a, b) (a)
191#endif
192
193#if defined(__x86_64__) && !defined(_LP64)
194# define SANITIZER_X32 1
195#else
196# define SANITIZER_X32 0
197#endif
198
199#if defined(__x86_64__) || defined(_M_X64)
200# define SANITIZER_X64 1
201#else
202# define SANITIZER_X64 0
203#endif
204
205#if defined(__i386__) || defined(_M_IX86)
206# define SANITIZER_I386 1
207#else
208# define SANITIZER_I386 0
209#endif
210
211#if defined(__mips__)
212# define SANITIZER_MIPS 1
213# if defined(__mips64) && _MIPS_SIM == _ABI64
214# define SANITIZER_MIPS32 0
215# define SANITIZER_MIPS64 1
216# else
217# define SANITIZER_MIPS32 1
218# define SANITIZER_MIPS64 0
219# endif
220#else
221# define SANITIZER_MIPS 0
222# define SANITIZER_MIPS32 0
223# define SANITIZER_MIPS64 0
224#endif
225
226#if defined(__s390__)
227# define SANITIZER_S390 1
228# if defined(__s390x__)
229# define SANITIZER_S390_31 0
230# define SANITIZER_S390_64 1
231# else
232# define SANITIZER_S390_31 1
233# define SANITIZER_S390_64 0
234# endif
235#else
236# define SANITIZER_S390 0
237# define SANITIZER_S390_31 0
238# define SANITIZER_S390_64 0
239#endif
240
241#if defined(__sparc__)
242# define SANITIZER_SPARC 1
243# if defined(__arch64__)
244# define SANITIZER_SPARC32 0
245# define SANITIZER_SPARC64 1
246# else
247# define SANITIZER_SPARC32 1
248# define SANITIZER_SPARC64 0
249# endif
250#else
251# define SANITIZER_SPARC 0
252# define SANITIZER_SPARC32 0
253# define SANITIZER_SPARC64 0
254#endif
255
256#if defined(__powerpc__)
257# define SANITIZER_PPC 1
258# if defined(__powerpc64__)
259# define SANITIZER_PPC32 0
260# define SANITIZER_PPC64 1
261// 64-bit PPC has two ABIs (v1 and v2). The old powerpc64 target is
262// big-endian, and uses v1 ABI (known for its function descriptors),
263// while the new powerpc64le target is little-endian and uses v2.
264// In theory, you could convince gcc to compile for their evil twins
265// (eg. big-endian v2), but you won't find such combinations in the wild
266// (it'd require bootstrapping a whole system, which would be quite painful
267// - there's no target triple for that). LLVM doesn't support them either.
268# if _CALL_ELF == 2
269# define SANITIZER_PPC64V1 0
270# define SANITIZER_PPC64V2 1
271# else
272# define SANITIZER_PPC64V1 1
273# define SANITIZER_PPC64V2 0
274# endif
275# else
276# define SANITIZER_PPC32 1
277# define SANITIZER_PPC64 0
278# define SANITIZER_PPC64V1 0
279# define SANITIZER_PPC64V2 0
280# endif
281#else
282# define SANITIZER_PPC 0
283# define SANITIZER_PPC32 0
284# define SANITIZER_PPC64 0
285# define SANITIZER_PPC64V1 0
286# define SANITIZER_PPC64V2 0
287#endif
288
289#if defined(__arm__) || defined(_M_ARM)
290# define SANITIZER_ARM 1
291#else
292# define SANITIZER_ARM 0
293#endif
294
295#if defined(__aarch64__) || defined(_M_ARM64)
296# define SANITIZER_ARM64 1
297#else
298# define SANITIZER_ARM64 0
299#endif
300
301#if SANITIZER_WINDOWS64 && SANITIZER_ARM64
302# define SANITIZER_WINDOWS_ARM64 1
303# define SANITIZER_WINDOWS_x64 0
304#elif SANITIZER_WINDOWS64 && !SANITIZER_ARM64
305# define SANITIZER_WINDOWS_ARM64 0
306# define SANITIZER_WINDOWS_x64 1
307#else
308# define SANITIZER_WINDOWS_ARM64 0
309# define SANITIZER_WINDOWS_x64 0
310#endif
311
312#if SANITIZER_SOLARIS && SANITIZER_WORDSIZE == 32
313# define SANITIZER_SOLARIS32 1
314#else
315# define SANITIZER_SOLARIS32 0
316#endif
317
318#if defined(__riscv) && (__riscv_xlen == 64)
319# define SANITIZER_RISCV64 1
320#else
321# define SANITIZER_RISCV64 0
322#endif
323
324#if defined(__loongarch_lp64)
325# define SANITIZER_LOONGARCH64 1
326#else
327# define SANITIZER_LOONGARCH64 0
328#endif
329
330#if defined(__alpha__)
331# define SANITIZER_ALPHA 1
332#else
333# define SANITIZER_ALPHA 0
334#endif
335
336#if defined(__hppa__)
337# define SANITIZER_HPPA 1
338#else
339# define SANITIZER_HPPA 0
340#endif
341
342#if defined(__AMDGPU__)
343# define SANITIZER_AMDGPU 1
344#else
345# define SANITIZER_AMDGPU 0
346#endif
347
348#if defined(__NVPTX__)
349# define SANITIZER_NVPTX 1
350#else
351# define SANITIZER_NVPTX 0
352#endif
353
354#if defined(__SPIRV__)
355# define SANITIZER_SPIRV 1
356#else
357# define SANITIZER_SPIRV 0
358#endif
359
360#if SANITIZER_AMDGPU || SANITIZER_NVPTX || SANITIZER_SPIRV
361# define SANITIZER_GPU 1
362#else
363# define SANITIZER_GPU 0
364#endif
365
366// By default we allow to use SizeClassAllocator64 on 64-bit platform.
367// But in some cases SizeClassAllocator64 does not work well and we need to
368// fallback to SizeClassAllocator32.
369// For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
370// change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
371#ifndef SANITIZER_CAN_USE_ALLOCATOR64
372# if (SANITIZER_RISCV64 && !SANITIZER_FUCHSIA && !SANITIZER_LINUX) || \
373 SANITIZER_IOS || SANITIZER_DRIVERKIT
374# define SANITIZER_CAN_USE_ALLOCATOR64 0
375# elif defined(__mips64) || defined(__hexagon__)
376# define SANITIZER_CAN_USE_ALLOCATOR64 0
377# else
378# define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64)
379# endif
380#endif
381
382// The first address that can be returned by mmap.
383#if SANITIZER_AIX && SANITIZER_WORDSIZE == 64
384# define SANITIZER_MMAP_BEGIN 0x0a00'0000'0000'0000ULL
385#else
386# define SANITIZER_MMAP_BEGIN 0
387#endif
388
389// The range of addresses which can be returned my mmap.
390// FIXME: this value should be different on different platforms. Larger values
391// will still work but will consume more memory for TwoLevelByteMap.
392#if defined(__mips__)
393# if SANITIZER_GO && defined(__mips64)
394# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
395# else
396# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 40)
397# endif
398#elif SANITIZER_RISCV64
399// FIXME: Rather than hardcoding the VMA here, we should rely on
400// GetMaxUserVirtualAddress(). This will require some refactoring though since
401// many places either hardcode some value or SANITIZER_MMAP_RANGE_SIZE is
402// assumed to be some constant integer.
403# if SANITIZER_FUCHSIA
404# define SANITIZER_MMAP_RANGE_SIZE (1ULL << 38)
405# else
406# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 56)
407# endif
408#elif defined(__aarch64__)
409# if SANITIZER_APPLE
410# if SANITIZER_OSX || SANITIZER_IOSSIM
411# define SANITIZER_MMAP_RANGE_SIZE \
412 FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
413# else
414// Darwin iOS/ARM64 has a 39-bit VMA, 512GiB VM
415# define SANITIZER_MMAP_RANGE_SIZE \
416 FIRST_32_SECOND_64(1ULL << 32, 1ULL << 39)
417# endif
418# else
419# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48)
420# endif
421#elif defined(__sparc__)
422# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 52)
423#else
424# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
425#endif
426
427// Whether the addresses are sign-extended from the VMA range to the word.
428// The SPARC64 Linux port implements this to split the VMA space into two
429// non-contiguous halves with a huge hole in the middle.
430#if defined(__sparc__) && SANITIZER_WORDSIZE == 64
431# define SANITIZER_SIGN_EXTENDED_ADDRESSES 1
432#else
433# define SANITIZER_SIGN_EXTENDED_ADDRESSES 0
434#endif
435
436// udi16 syscalls can only be used when the following conditions are
437// met:
438// * target is one of arm32, x86-32, sparc32, sh or m68k
439// * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15
440// built against > linux-2.2 kernel headers
441// Since we don't want to include libc headers here, we check the
442// target only.
443#if defined(__arm__) || SANITIZER_X32 || defined(__sparc__)
444# define SANITIZER_USES_UID16_SYSCALLS 1
445#else
446# define SANITIZER_USES_UID16_SYSCALLS 0
447#endif
448
449#if defined(__mips__)
450# define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10)
451#else
452# define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
453#endif
454
455/// \macro MSC_PREREQ
456/// \brief Is the compiler MSVC of at least the specified version?
457/// The common \param version values to check for are:
458/// * 1800: Microsoft Visual Studio 2013 / 12.0
459/// * 1900: Microsoft Visual Studio 2015 / 14.0
460#ifdef _MSC_VER
461# define MSC_PREREQ(version) (_MSC_VER >= (version))
462#else
463# define MSC_PREREQ(version) 0
464#endif
465
466#if SANITIZER_APPLE && defined(__x86_64__)
467# define SANITIZER_NON_UNIQUE_TYPEINFO 0
468#else
469# define SANITIZER_NON_UNIQUE_TYPEINFO 1
470#endif
471
472// On linux, some architectures had an ABI transition from 64-bit long double
473// (ie. same as double) to 128-bit long double. On those, glibc symbols
474// involving long doubles come in two versions, and we need to pass the
475// correct one to dlvsym when intercepting them.
476#if SANITIZER_LINUX && (SANITIZER_S390 || SANITIZER_PPC32 || SANITIZER_PPC64V1)
477# define SANITIZER_NLDBL_VERSION "GLIBC_2.4"
478#endif
479
480#if SANITIZER_GO == 0
481# define SANITIZER_GO 0
482#endif
483
484// On PowerPC and ARM Thumb, calling pthread_exit() causes LSan to detect leaks.
485// pthread_exit() performs unwinding that leads to dlopen'ing libgcc_s.so.
486// dlopen mallocs "libgcc_s.so" string which confuses LSan, it fails to realize
487// that this allocation happens in dynamic linker and should be ignored.
488#if SANITIZER_PPC || defined(__thumb__)
489# define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 1
490#else
491# define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 0
492#endif
493
494#if SANITIZER_FREEBSD || SANITIZER_APPLE || SANITIZER_NETBSD || \
495 SANITIZER_SOLARIS || SANITIZER_HAIKU
496# define SANITIZER_MADVISE_DONTNEED MADV_FREE
497#else
498# define SANITIZER_MADVISE_DONTNEED MADV_DONTNEED
499#endif
500
501// Older gcc have issues aligning to a constexpr, and require an integer.
502// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859 among others.
503#if defined(__powerpc__) || defined(__powerpc64__)
504# define SANITIZER_CACHE_LINE_SIZE 128
505#else
506# define SANITIZER_CACHE_LINE_SIZE 64
507#endif
508
509// Enable offline markup symbolizer for Fuchsia.
510#if SANITIZER_FUCHSIA
511# define SANITIZER_SYMBOLIZER_MARKUP 1
512#else
513# define SANITIZER_SYMBOLIZER_MARKUP 0
514#endif
515
516// Enable ability to support sanitizer initialization that is
517// compatible with the sanitizer library being loaded via
518// `dlopen()`.
519#if SANITIZER_APPLE
520# define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 1
521#else
522# define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 0
523#endif
524
525// SANITIZER_SUPPORTS_THREADLOCAL
526// 1 - THREADLOCAL macro is supported by target
527// 0 - THREADLOCAL macro is not supported by target
528#ifndef __has_feature
529// TODO: Support other compilers here
530# define SANITIZER_SUPPORTS_THREADLOCAL 1
531#else
532# if __has_feature(tls)
533# define SANITIZER_SUPPORTS_THREADLOCAL 1
534# else
535# define SANITIZER_SUPPORTS_THREADLOCAL 0
536# endif
537#endif
538
539#if defined(__thumb__) && defined(__linux__)
540// Workaround for
541// https://lab.llvm.org/buildbot/#/builders/clang-thumbv7-full-2stage
542// or
543// https://lab.llvm.org/staging/#/builders/clang-thumbv7-full-2stage
544// It fails *rss_limit_mb_test* without meaningful errors.
545# define SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 1
546#else
547# define SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 0
548#endif
549
550#if SANITIZER_LINUX
551# if SANITIZER_GLIBC
552// Workaround for
553// glibc/commit/3d3572f59059e2b19b8541ea648a6172136ec42e
554// Linux: Keep termios ioctl constants strictly internal
555# if __GLIBC_PREREQ(2, 41)
556# define SANITIZER_TERMIOS_IOCTL_CONSTANTS 0
557# else
558# define SANITIZER_TERMIOS_IOCTL_CONSTANTS 1
559# endif
560# else
561# define SANITIZER_TERMIOS_IOCTL_CONSTANTS 1
562# endif
563#endif
564
565#if SANITIZER_APPLE && SANITIZER_WORDSIZE == 64
566// MTE uses the lower half of the top byte.
567# define STRIP_MTE_TAG(addr) ((addr) & ~((uptr)0x0f << 56))
568#else
569# define STRIP_MTE_TAG(addr) (addr)
570#endif
571
572#endif // SANITIZER_PLATFORM_H
573