xref: /aosp_15_r20/external/boringssl/src/include/openssl/target.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2023, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef OPENSSL_HEADER_TARGET_H
16 #define OPENSSL_HEADER_TARGET_H
17 
18 // Preprocessor symbols that define the target platform.
19 //
20 // This file may be included in C, C++, and assembler and must be compatible
21 // with each environment. It is separated out only to share code between
22 // <openssl/base.h> and <openssl/asm_base.h>. Prefer to include those headers
23 // instead.
24 
25 #if defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64)
26 #define OPENSSL_64_BIT
27 #define OPENSSL_X86_64
28 #elif defined(__x86) || defined(__i386) || defined(__i386__) || defined(_M_IX86)
29 #define OPENSSL_32_BIT
30 #define OPENSSL_X86
31 #elif defined(__AARCH64EL__) || defined(_M_ARM64)
32 #define OPENSSL_64_BIT
33 #define OPENSSL_AARCH64
34 #elif defined(__ARMEL__) || defined(_M_ARM)
35 #define OPENSSL_32_BIT
36 #define OPENSSL_ARM
37 #elif defined(__MIPSEL__) && !defined(__LP64__)
38 #define OPENSSL_32_BIT
39 #define OPENSSL_MIPS
40 #elif defined(__MIPSEL__) && defined(__LP64__)
41 #define OPENSSL_64_BIT
42 #define OPENSSL_MIPS64
43 #elif defined(__riscv) && __SIZEOF_POINTER__ == 8
44 #define OPENSSL_64_BIT
45 #define OPENSSL_RISCV64
46 #elif defined(__riscv) && __SIZEOF_POINTER__ == 4
47 #define OPENSSL_32_BIT
48 #elif defined(__pnacl__)
49 #define OPENSSL_32_BIT
50 #define OPENSSL_PNACL
51 #elif defined(__wasm__)
52 #define OPENSSL_32_BIT
53 #elif defined(__asmjs__)
54 #define OPENSSL_32_BIT
55 #elif defined(__myriad2__)
56 #define OPENSSL_32_BIT
57 #else
58 // The list above enumerates the platforms that BoringSSL supports. For these
59 // platforms we keep a reasonable bar of not breaking them: automated test
60 // coverage, for one, but also we need access to these types for machines for
61 // fixing them.
62 //
63 // However, we know that anything that seems to work will soon be expected
64 // to work and, quickly, the implicit expectation is that every machine will
65 // always work. So this list serves to mark the boundary of what we guarantee.
66 // Of course, you can run the code any many more machines, but then you're
67 // taking on the burden of fixing it and, if you're doing that, then you must
68 // be able to carry local patches. In which case patching this list is trivial.
69 //
70 // BoringSSL will only possibly work on standard 32-bit and 64-bit
71 // two's-complement, little-endian architectures. Functions will not produce
72 // the correct answer on other systems. Run the crypto_test binary, notably
73 // crypto/compiler_test.cc, before trying a new architecture.
74 #error "Unknown target CPU"
75 #endif
76 
77 #if defined(__APPLE__)
78 #define OPENSSL_APPLE
79 #endif
80 
81 #if defined(_WIN32)
82 #define OPENSSL_WINDOWS
83 #endif
84 
85 // Trusty and Android baremetal aren't Linux but currently define __linux__.
86 // As a workaround, we exclude them here.
87 // We also exclude nanolibc/CrOS EC. nanolibc/CrOS EC sometimes build for a
88 // non-Linux target (which should not define __linux__), but also sometimes
89 // build for Linux. Although technically running in Linux userspace, this lacks
90 // all the libc APIs we'd normally expect on Linux, so we treat it as a
91 // non-Linux target.
92 //
93 // TODO(b/169780122): Remove this workaround once Trusty no longer defines it.
94 // TODO(b/291101350): Remove this workaround once Android baremetal no longer
95 // defines it.
96 #if defined(__linux__) && !defined(__TRUSTY__) && \
97     !defined(ANDROID_BAREMETAL) && !defined(OPENSSL_NANOLIBC) && \
98     !defined(CROS_EC)
99 #define OPENSSL_LINUX
100 #endif
101 
102 #if defined(__Fuchsia__)
103 #define OPENSSL_FUCHSIA
104 #endif
105 
106 // Trusty is Android's TEE target. See
107 // https://source.android.com/docs/security/features/trusty
108 //
109 // Defining this on any other platform is not supported. Other embedded
110 // platforms must introduce their own defines.
111 #if defined(__TRUSTY__)
112 #define OPENSSL_TRUSTY
113 #define OPENSSL_NO_FILESYSTEM
114 #define OPENSSL_NO_POSIX_IO
115 #define OPENSSL_NO_SOCK
116 #define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED
117 #endif
118 
119 // nanolibc is a particular minimal libc implementation. Defining this on any
120 // other platform is not supported. Other embedded platforms must introduce
121 // their own defines.
122 #if defined(OPENSSL_NANOLIBC)
123 #define OPENSSL_NO_FILESYSTEM
124 #define OPENSSL_NO_POSIX_IO
125 #define OPENSSL_NO_SOCK
126 #define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED
127 #endif
128 
129 // Android baremetal is an embedded target that uses a subset of bionic.
130 // Defining this on any other platform is not supported. Other embedded
131 // platforms must introduce their own defines.
132 #if defined(ANDROID_BAREMETAL)
133 #define OPENSSL_NO_FILESYSTEM
134 #define OPENSSL_NO_POSIX_IO
135 #define OPENSSL_NO_SOCK
136 #define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED
137 #endif
138 
139 // CROS_EC is an embedded target for ChromeOS Embedded Controller. Defining
140 // this on any other platform is not supported. Other embedded platforms must
141 // introduce their own defines.
142 //
143 // https://chromium.googlesource.com/chromiumos/platform/ec/+/HEAD/README.md
144 #if defined(CROS_EC)
145 #define OPENSSL_NO_FILESYSTEM
146 #define OPENSSL_NO_POSIX_IO
147 #define OPENSSL_NO_SOCK
148 #define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED
149 #endif
150 
151 // Zephyr is an open source RTOS, optimized for embedded devices.
152 // Defining this on any other platform is not supported. Other embedded
153 // platforms must introduce their own defines.
154 //
155 // Zephyr supports multithreading with cooperative and preemptive scheduling.
156 // It also implements POSIX Threads (pthread) API, so it's not necessary to
157 // implement BoringSSL internal threading API using some custom API.
158 //
159 // https://www.zephyrproject.org/
160 #if defined(__ZEPHYR__)
161 #define OPENSSL_NO_FILESYSTEM
162 #define OPENSSL_NO_POSIX_IO
163 #define OPENSSL_NO_SOCK
164 #endif
165 
166 #if defined(__ANDROID_API__)
167 #define OPENSSL_ANDROID
168 #endif
169 
170 #if defined(__FreeBSD__)
171 #define OPENSSL_FREEBSD
172 #endif
173 
174 #if defined(__OpenBSD__)
175 #define OPENSSL_OPENBSD
176 #endif
177 
178 // BoringSSL requires platform's locking APIs to make internal global state
179 // thread-safe, including the PRNG. On some single-threaded embedded platforms,
180 // locking APIs may not exist, so this dependency may be disabled with the
181 // following build flag.
182 //
183 // IMPORTANT: Doing so means the consumer promises the library will never be
184 // used in any multi-threaded context. It causes BoringSSL to be globally
185 // thread-unsafe. Setting it inappropriately will subtly and unpredictably
186 // corrupt memory and leak secret keys.
187 //
188 // Do not set this flag on any platform where threads are possible. BoringSSL
189 // maintainers will not provide support for any consumers that do so. Changes
190 // which break such unsupported configurations will not be reverted.
191 #if !defined(OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED)
192 #define OPENSSL_THREADS
193 #endif
194 
195 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) && \
196     !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
197 #define BORINGSSL_UNSAFE_DETERMINISTIC_MODE
198 #endif
199 
200 #if defined(__has_feature)
201 #if __has_feature(address_sanitizer)
202 #define OPENSSL_ASAN
203 #endif
204 #if __has_feature(thread_sanitizer)
205 #define OPENSSL_TSAN
206 #endif
207 #if __has_feature(memory_sanitizer)
208 #define OPENSSL_MSAN
209 #define OPENSSL_ASM_INCOMPATIBLE
210 #endif
211 #if __has_feature(hwaddress_sanitizer)
212 #define OPENSSL_HWASAN
213 #endif
214 #endif
215 
216 // Disable 32-bit Arm assembly on Apple platforms. The last iOS version that
217 // supported 32-bit Arm was iOS 10.
218 #if defined(OPENSSL_APPLE) && defined(OPENSSL_ARM)
219 #define OPENSSL_ASM_INCOMPATIBLE
220 #endif
221 
222 #if defined(OPENSSL_ASM_INCOMPATIBLE)
223 #undef OPENSSL_ASM_INCOMPATIBLE
224 #if !defined(OPENSSL_NO_ASM)
225 #define OPENSSL_NO_ASM
226 #endif
227 #endif  // OPENSSL_ASM_INCOMPATIBLE
228 
229 #endif  // OPENSSL_HEADER_TARGET_H
230