xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/config.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SANDBOXED_API_CONFIG_H_
16 #define SANDBOXED_API_CONFIG_H_
17 
18 #include <features.h>
19 #include <cstdint>
20 
21 #include "absl/base/config.h"  // IWYU pragma: keep
22 
23 // GCC/Clang define __x86_64__, Visual Studio uses _M_X64
24 #if defined(__x86_64__) || defined(_M_X64)
25 #define SAPI_X86_64 1
26 
27 // Check various spellings for 64-bit POWER. Not checking for Visual Studio, as
28 // it does not support 64-bit POWER.
29 #elif (defined(__PPC64__) || defined(__powerpc64__) || defined(__ppc64__)) && \
30     defined(ABSL_IS_LITTLE_ENDIAN)
31 #define SAPI_PPC64_LE 1
32 
33 // Spellings for AArch64
34 #elif defined(__aarch64__) || defined(_M_ARM64)
35 #define SAPI_ARM64 1
36 
37 // 32-bit ARM
38 #elif defined(__arm__) || defined(_M_ARM)
39 #define SAPI_ARM 1
40 
41 #endif
42 
43 namespace sapi {
44 
45 // Returns whether the executable running under code coverage.
46 bool IsCoverageRun();
47 
48 namespace cpu {
49 
50 // CPU architectures known to Sandbox2
51 enum Architecture : uint16_t {
52   // Linux: Use a magic value, so it can be easily spotted in the seccomp-bpf
53   // bytecode decompilation stream. Must be < (1<<15), as that is the size of
54   // data which can be returned by BPF.
55   kUnknown = 0xCAF0,
56   kX8664,
57   kX86,
58   kPPC64LE,
59   kArm64,
60   kArm,
61   kMax = kArm
62 };
63 
64 }  // namespace cpu
65 
66 namespace host_cpu {
67 
68 // Returns the current host CPU architecture if supported. If not supported,
69 // returns cpu::kUnknown.
Architecture()70 constexpr cpu::Architecture Architecture() {
71 #if defined(SAPI_X86_64)
72   return cpu::kX8664;
73 #elif defined(SAPI_PPC64_LE)
74   return cpu::kPPC64LE;
75 #elif defined(SAPI_ARM64)
76   return cpu::kArm64;
77 #elif defined(SAPI_ARM)
78   return cpu::kArm;
79 #else
80   return cpu::kUnknown;
81 #endif
82 }
83 
IsX8664()84 constexpr bool IsX8664() { return Architecture() == cpu::kX8664; }
85 
IsPPC64LE()86 constexpr bool IsPPC64LE() { return Architecture() == cpu::kPPC64LE; }
87 
IsArm64()88 constexpr bool IsArm64() { return Architecture() == cpu::kArm64; }
89 
IsArm()90 constexpr bool IsArm() { return Architecture() == cpu::kArm; }
91 
Is64Bit()92 constexpr bool Is64Bit() { return sizeof(uintptr_t) == 8; }
93 
94 }  // namespace host_cpu
95 
96 static_assert(host_cpu::Architecture() != cpu::kUnknown,
97               "Host CPU architecture is not supported: One of x86-64, POWER64 "
98               "(little endian), ARM or AArch64 is required.");
99 
100 namespace os {
101 
102 // Operating Systems known to Sandbox2
103 enum Platform : uint16_t {
104   kUnknown,
105   kAndroid,
106   kLinux,
107 };
108 
109 }  // namespace os
110 
111 namespace host_os {
112 
113 // Returns the current host OS platform if supported. If not supported,
114 // returns platforms::kUnknown.
Platform()115 constexpr os::Platform Platform() {
116 #if defined(__ANDROID__)
117   return os::kAndroid;
118 #elif defined(__linux__)
119   return os::kLinux;
120 #else
121   return os::kUnknown;
122 #endif
123 }
124 
IsAndroid()125 constexpr bool IsAndroid() { return Platform() == os::kAndroid; }
126 
IsLinux()127 constexpr bool IsLinux() { return Platform() == os::kLinux; }
128 
129 }  // namespace host_os
130 
131 namespace sanitizers {
132 
IsMSan()133 constexpr bool IsMSan() {
134 #ifdef ABSL_HAVE_MEMORY_SANITIZER
135   return true;
136 #else
137   return false;
138 #endif
139 }
140 
IsTSan()141 constexpr bool IsTSan() {
142 #ifdef ABSL_HAVE_THREAD_SANITIZER
143   return true;
144 #else
145   return false;
146 #endif
147 }
148 
IsASan()149 constexpr bool IsASan() {
150 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
151   return true;
152 #else
153   return false;
154 #endif
155 }
156 
IsHwASan()157 constexpr bool IsHwASan() {
158 #ifdef ABSL_HAVE_HWADDRESS_SANITIZER
159   return true;
160 #else
161   return false;
162 #endif
163 }
164 
IsLSan()165 constexpr bool IsLSan() {
166 #ifdef ABSL_HAVE_LEAK_SANITIZER
167   return true;
168 #else
169   return false;
170 #endif
171 }
172 
173 // Returns whether any of the sanitizers is enabled.
IsAny()174 constexpr bool IsAny() {
175   return IsMSan() || IsTSan() || IsASan() || IsHwASan() || IsLSan();
176 }
177 
178 }  // namespace sanitizers
179 
180 }  // namespace sapi
181 
182 #endif  // SANDBOXED_API_CONFIG_H_
183