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 kLinux,
106 };
107
108 } // namespace os
109
110 namespace host_os {
111
112 // Returns the current host OS platform if supported. If not supported,
113 // returns platforms::kUnknown.
Platform()114 constexpr os::Platform Platform() {
115 #if defined(__linux__)
116 return os::kLinux;
117 #else
118 return os::kUnknown;
119 #endif
120 }
121
IsLinux()122 constexpr bool IsLinux() { return Platform() == os::kLinux; }
123
124 } // namespace host_os
125
126 namespace sanitizers {
127
IsMSan()128 constexpr bool IsMSan() {
129 #ifdef ABSL_HAVE_MEMORY_SANITIZER
130 return true;
131 #else
132 return false;
133 #endif
134 }
135
IsTSan()136 constexpr bool IsTSan() {
137 #ifdef ABSL_HAVE_THREAD_SANITIZER
138 return true;
139 #else
140 return false;
141 #endif
142 }
143
IsASan()144 constexpr bool IsASan() {
145 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
146 return true;
147 #else
148 return false;
149 #endif
150 }
151
IsHwASan()152 constexpr bool IsHwASan() {
153 #ifdef ABSL_HAVE_HWADDRESS_SANITIZER
154 return true;
155 #else
156 return false;
157 #endif
158 }
159
IsLSan()160 constexpr bool IsLSan() {
161 #ifdef ABSL_HAVE_LEAK_SANITIZER
162 return true;
163 #else
164 return false;
165 #endif
166 }
167
168 // Returns whether any of the sanitizers is enabled.
IsAny()169 constexpr bool IsAny() {
170 return IsMSan() || IsTSan() || IsASan() || IsHwASan() || IsLSan();
171 }
172
173 } // namespace sanitizers
174
175 } // namespace sapi
176
177 #endif // SANDBOXED_API_CONFIG_H_
178