1//===-- sanitizer_platform.h ------------------------------------*- C++ -*-===//
2//
3// This file is distributed under the University of Illinois Open Source
4// License. See LICENSE.TXT for details.
5//
6//===----------------------------------------------------------------------===//
7//
8// Common platform macros.
9//===----------------------------------------------------------------------===//
10
11#ifndef SANITIZER_PLATFORM_H
12#define SANITIZER_PLATFORM_H
13
14#if !defined(__linux__) && !defined(__FreeBSD__) && \
15  !defined(__APPLE__) && !defined(_WIN32)
16# error "This operating system is not supported"
17#endif
18
19#if defined(__linux__)
20# define SANITIZER_LINUX   1
21#else
22# define SANITIZER_LINUX   0
23#endif
24
25#if defined(__FreeBSD__)
26# define SANITIZER_FREEBSD 1
27#else
28# define SANITIZER_FREEBSD 0
29#endif
30
31#if defined(__APPLE__)
32# define SANITIZER_MAC     1
33# include <TargetConditionals.h>
34# if TARGET_OS_IPHONE
35#  define SANITIZER_IOS    1
36# else
37#  define SANITIZER_IOS    0
38# endif
39#else
40# define SANITIZER_MAC     0
41# define SANITIZER_IOS     0
42#endif
43
44#if defined(_WIN32)
45# define SANITIZER_WINDOWS 1
46#else
47# define SANITIZER_WINDOWS 0
48#endif
49
50#if defined(__ANDROID__)
51# define SANITIZER_ANDROID 1
52#else
53# define SANITIZER_ANDROID 0
54#endif
55
56#define SANITIZER_POSIX (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC)
57
58#if __LP64__ || defined(_WIN64)
59#  define SANITIZER_WORDSIZE 64
60#else
61#  define SANITIZER_WORDSIZE 32
62#endif
63
64#if SANITIZER_WORDSIZE == 64
65# define FIRST_32_SECOND_64(a, b) (b)
66#else
67# define FIRST_32_SECOND_64(a, b) (a)
68#endif
69
70#if defined(__x86_64__) && !defined(_LP64)
71# define SANITIZER_X32 1
72#else
73# define SANITIZER_X32 0
74#endif
75
76// By default we allow to use SizeClassAllocator64 on 64-bit platform.
77// But in some cases (e.g. AArch64's 39-bit address space) SizeClassAllocator64
78// does not work well and we need to fallback to SizeClassAllocator32.
79// For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
80// change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
81#ifndef SANITIZER_CAN_USE_ALLOCATOR64
82# if defined(__aarch64__) || defined(__mips64)
83#  define SANITIZER_CAN_USE_ALLOCATOR64 0
84# else
85#  define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64)
86# endif
87#endif
88
89// The range of addresses which can be returned my mmap.
90// FIXME: this value should be different on different platforms,
91// e.g. on AArch64 it is most likely (1ULL << 39). Larger values will still work
92// but will consume more memory for TwoLevelByteMap.
93#if defined(__aarch64__)
94# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 39)
95#else
96# define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
97#endif
98
99// The AArch64 linux port uses the canonical syscall set as mandated by
100// the upstream linux community for all new ports. Other ports may still
101// use legacy syscalls.
102#ifndef SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
103# if defined(__aarch64__) && SANITIZER_LINUX
104# define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 1
105# else
106# define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 0
107# endif
108#endif
109
110// udi16 syscalls can only be used when the following conditions are
111// met:
112// * target is one of arm32, x86-32, sparc32, sh or m68k
113// * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15
114//   built against > linux-2.2 kernel headers
115// Since we don't want to include libc headers here, we check the
116// target only.
117#if defined(__arm__) || SANITIZER_X32 || defined(__sparc__)
118#define SANITIZER_USES_UID16_SYSCALLS 1
119#else
120#define SANITIZER_USES_UID16_SYSCALLS 0
121#endif
122
123#ifdef __mips__
124# define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10)
125#else
126# define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
127#endif
128
129// Assume obsolete RPC headers are available by default
130#if !defined(HAVE_RPC_XDR_H) && !defined(HAVE_TIRPC_RPC_XDR_H)
131# define HAVE_RPC_XDR_H (SANITIZER_LINUX && !SANITIZER_ANDROID)
132# define HAVE_TIRPC_RPC_XDR_H 0
133#endif
134
135#endif // SANITIZER_PLATFORM_H
136