xref: /aosp_15_r20/external/abseil-cpp/absl/random/internal/randen_detect.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker // HERMETIC NOTE: The randen_hwaes target must not introduce duplicate
16*9356374aSAndroid Build Coastguard Worker // symbols from arbitrary system and other headers, since it may be built
17*9356374aSAndroid Build Coastguard Worker // with different flags from other targets, using different levels of
18*9356374aSAndroid Build Coastguard Worker // optimization, potentially introducing ODR violations.
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/randen_detect.h"
21*9356374aSAndroid Build Coastguard Worker 
22*9356374aSAndroid Build Coastguard Worker #include <cstdint>
23*9356374aSAndroid Build Coastguard Worker #include <cstring>
24*9356374aSAndroid Build Coastguard Worker 
25*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/platform.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker #if !defined(__UCLIBC__) && defined(__GLIBC__) && \
28*9356374aSAndroid Build Coastguard Worker     (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
29*9356374aSAndroid Build Coastguard Worker #define ABSL_HAVE_GETAUXVAL
30*9356374aSAndroid Build Coastguard Worker #endif
31*9356374aSAndroid Build Coastguard Worker 
32*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_ARCH_X86_64)
33*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_USE_X86_CPUID
34*9356374aSAndroid Build Coastguard Worker #elif defined(ABSL_ARCH_PPC) || defined(ABSL_ARCH_ARM) || \
35*9356374aSAndroid Build Coastguard Worker     defined(ABSL_ARCH_AARCH64)
36*9356374aSAndroid Build Coastguard Worker #if defined(__ANDROID__)
37*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_USE_ANDROID_GETAUXVAL
38*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_USE_GETAUXVAL
39*9356374aSAndroid Build Coastguard Worker #elif defined(__linux__) && defined(ABSL_HAVE_GETAUXVAL)
40*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_USE_LINUX_GETAUXVAL
41*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_USE_GETAUXVAL
42*9356374aSAndroid Build Coastguard Worker #endif
43*9356374aSAndroid Build Coastguard Worker #endif
44*9356374aSAndroid Build Coastguard Worker 
45*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_INTERNAL_USE_X86_CPUID)
46*9356374aSAndroid Build Coastguard Worker #if defined(_WIN32) || defined(_WIN64)
47*9356374aSAndroid Build Coastguard Worker #include <intrin.h>  // NOLINT(build/include_order)
48*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_BUILTIN(__cpuid)
49*9356374aSAndroid Build Coastguard Worker // MSVC-equivalent __cpuid intrinsic declaration for clang-like compilers
50*9356374aSAndroid Build Coastguard Worker // for non-Windows build environments.
51*9356374aSAndroid Build Coastguard Worker extern void __cpuid(int[4], int);
52*9356374aSAndroid Build Coastguard Worker #else
53*9356374aSAndroid Build Coastguard Worker // MSVC-equivalent __cpuid intrinsic function.
__cpuid(int cpu_info[4],int info_type)54*9356374aSAndroid Build Coastguard Worker static void __cpuid(int cpu_info[4], int info_type) {
55*9356374aSAndroid Build Coastguard Worker   __asm__ volatile("cpuid \n\t"
56*9356374aSAndroid Build Coastguard Worker                    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
57*9356374aSAndroid Build Coastguard Worker                      "=d"(cpu_info[3])
58*9356374aSAndroid Build Coastguard Worker                    : "a"(info_type), "c"(0));
59*9356374aSAndroid Build Coastguard Worker }
60*9356374aSAndroid Build Coastguard Worker #endif
61*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_INTERNAL_USE_X86_CPUID
62*9356374aSAndroid Build Coastguard Worker 
63*9356374aSAndroid Build Coastguard Worker // On linux, just use the c-library getauxval call.
64*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_INTERNAL_USE_LINUX_GETAUXVAL)
65*9356374aSAndroid Build Coastguard Worker 
66*9356374aSAndroid Build Coastguard Worker extern "C" unsigned long getauxval(unsigned long type);  // NOLINT(runtime/int)
67*9356374aSAndroid Build Coastguard Worker 
GetAuxval(uint32_t hwcap_type)68*9356374aSAndroid Build Coastguard Worker static uint32_t GetAuxval(uint32_t hwcap_type) {
69*9356374aSAndroid Build Coastguard Worker   return static_cast<uint32_t>(getauxval(hwcap_type));
70*9356374aSAndroid Build Coastguard Worker }
71*9356374aSAndroid Build Coastguard Worker 
72*9356374aSAndroid Build Coastguard Worker #endif
73*9356374aSAndroid Build Coastguard Worker 
74*9356374aSAndroid Build Coastguard Worker // On android, probe the system's C library for getauxval().
75*9356374aSAndroid Build Coastguard Worker // This is the same technique used by the android NDK cpu features library
76*9356374aSAndroid Build Coastguard Worker // as well as the google open-source cpu_features library.
77*9356374aSAndroid Build Coastguard Worker //
78*9356374aSAndroid Build Coastguard Worker // TODO(absl-team): Consider implementing a fallback of directly reading
79*9356374aSAndroid Build Coastguard Worker // /proc/self/auxval.
80*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_INTERNAL_USE_ANDROID_GETAUXVAL)
81*9356374aSAndroid Build Coastguard Worker #include <dlfcn.h>
82*9356374aSAndroid Build Coastguard Worker 
GetAuxval(uint32_t hwcap_type)83*9356374aSAndroid Build Coastguard Worker static uint32_t GetAuxval(uint32_t hwcap_type) {
84*9356374aSAndroid Build Coastguard Worker   // NOLINTNEXTLINE(runtime/int)
85*9356374aSAndroid Build Coastguard Worker   typedef unsigned long (*getauxval_func_t)(unsigned long);
86*9356374aSAndroid Build Coastguard Worker 
87*9356374aSAndroid Build Coastguard Worker   dlerror();  // Cleaning error state before calling dlopen.
88*9356374aSAndroid Build Coastguard Worker   void* libc_handle = dlopen("libc.so", RTLD_NOW);
89*9356374aSAndroid Build Coastguard Worker   if (!libc_handle) {
90*9356374aSAndroid Build Coastguard Worker     return 0;
91*9356374aSAndroid Build Coastguard Worker   }
92*9356374aSAndroid Build Coastguard Worker   uint32_t result = 0;
93*9356374aSAndroid Build Coastguard Worker   void* sym = dlsym(libc_handle, "getauxval");
94*9356374aSAndroid Build Coastguard Worker   if (sym) {
95*9356374aSAndroid Build Coastguard Worker     getauxval_func_t func;
96*9356374aSAndroid Build Coastguard Worker     memcpy(&func, &sym, sizeof(func));
97*9356374aSAndroid Build Coastguard Worker     result = static_cast<uint32_t>((*func)(hwcap_type));
98*9356374aSAndroid Build Coastguard Worker   }
99*9356374aSAndroid Build Coastguard Worker   dlclose(libc_handle);
100*9356374aSAndroid Build Coastguard Worker   return result;
101*9356374aSAndroid Build Coastguard Worker }
102*9356374aSAndroid Build Coastguard Worker 
103*9356374aSAndroid Build Coastguard Worker #endif
104*9356374aSAndroid Build Coastguard Worker 
105*9356374aSAndroid Build Coastguard Worker namespace absl {
106*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
107*9356374aSAndroid Build Coastguard Worker namespace random_internal {
108*9356374aSAndroid Build Coastguard Worker 
109*9356374aSAndroid Build Coastguard Worker // The default return at the end of the function might be unreachable depending
110*9356374aSAndroid Build Coastguard Worker // on the configuration. Ignore that warning.
111*9356374aSAndroid Build Coastguard Worker #if defined(__clang__)
112*9356374aSAndroid Build Coastguard Worker #pragma clang diagnostic push
113*9356374aSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wunreachable-code-return"
114*9356374aSAndroid Build Coastguard Worker #endif
115*9356374aSAndroid Build Coastguard Worker 
116*9356374aSAndroid Build Coastguard Worker // CPUSupportsRandenHwAes returns whether the CPU is a microarchitecture
117*9356374aSAndroid Build Coastguard Worker // which supports the crpyto/aes instructions or extensions necessary to use the
118*9356374aSAndroid Build Coastguard Worker // accelerated RandenHwAes implementation.
119*9356374aSAndroid Build Coastguard Worker //
120*9356374aSAndroid Build Coastguard Worker // 1. For x86 it is sufficient to use the CPUID instruction to detect whether
121*9356374aSAndroid Build Coastguard Worker //    the cpu supports AES instructions. Done.
122*9356374aSAndroid Build Coastguard Worker //
123*9356374aSAndroid Build Coastguard Worker // Fon non-x86 it is much more complicated.
124*9356374aSAndroid Build Coastguard Worker //
125*9356374aSAndroid Build Coastguard Worker // 2. When ABSL_INTERNAL_USE_GETAUXVAL is defined, use getauxval() (either
126*9356374aSAndroid Build Coastguard Worker //    the direct c-library version, or the android probing version which loads
127*9356374aSAndroid Build Coastguard Worker //    libc), and read the hardware capability bits.
128*9356374aSAndroid Build Coastguard Worker //    This is based on the technique used by boringssl uses to detect
129*9356374aSAndroid Build Coastguard Worker //    cpu capabilities, and should allow us to enable crypto in the android
130*9356374aSAndroid Build Coastguard Worker //    builds where it is supported.
131*9356374aSAndroid Build Coastguard Worker //
132*9356374aSAndroid Build Coastguard Worker // 3. Use the default for the compiler architecture.
133*9356374aSAndroid Build Coastguard Worker //
134*9356374aSAndroid Build Coastguard Worker 
CPUSupportsRandenHwAes()135*9356374aSAndroid Build Coastguard Worker bool CPUSupportsRandenHwAes() {
136*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_INTERNAL_USE_X86_CPUID)
137*9356374aSAndroid Build Coastguard Worker   // 1. For x86: Use CPUID to detect the required AES instruction set.
138*9356374aSAndroid Build Coastguard Worker   int regs[4];
139*9356374aSAndroid Build Coastguard Worker   __cpuid(reinterpret_cast<int*>(regs), 1);
140*9356374aSAndroid Build Coastguard Worker   return regs[2] & (1 << 25);  // AES
141*9356374aSAndroid Build Coastguard Worker 
142*9356374aSAndroid Build Coastguard Worker #elif defined(ABSL_INTERNAL_USE_GETAUXVAL)
143*9356374aSAndroid Build Coastguard Worker   // 2. Use getauxval() to read the hardware bits and determine
144*9356374aSAndroid Build Coastguard Worker   // cpu capabilities.
145*9356374aSAndroid Build Coastguard Worker 
146*9356374aSAndroid Build Coastguard Worker #define AT_HWCAP 16
147*9356374aSAndroid Build Coastguard Worker #define AT_HWCAP2 26
148*9356374aSAndroid Build Coastguard Worker #if defined(ABSL_ARCH_PPC)
149*9356374aSAndroid Build Coastguard Worker   // For Power / PPC: Expect that the cpu supports VCRYPTO
150*9356374aSAndroid Build Coastguard Worker   // See https://members.openpowerfoundation.org/document/dl/576
151*9356374aSAndroid Build Coastguard Worker   // VCRYPTO should be present in POWER8 >= 2.07.
152*9356374aSAndroid Build Coastguard Worker   // Uses Linux kernel constants from arch/powerpc/include/uapi/asm/cputable.h
153*9356374aSAndroid Build Coastguard Worker   static const uint32_t kVCRYPTO = 0x02000000;
154*9356374aSAndroid Build Coastguard Worker   const uint32_t hwcap = GetAuxval(AT_HWCAP2);
155*9356374aSAndroid Build Coastguard Worker   return (hwcap & kVCRYPTO) != 0;
156*9356374aSAndroid Build Coastguard Worker 
157*9356374aSAndroid Build Coastguard Worker #elif defined(ABSL_ARCH_ARM)
158*9356374aSAndroid Build Coastguard Worker   // For ARM: Require crypto+neon
159*9356374aSAndroid Build Coastguard Worker   // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0500f/CIHBIBBA.html
160*9356374aSAndroid Build Coastguard Worker   // Uses Linux kernel constants from arch/arm64/include/asm/hwcap.h
161*9356374aSAndroid Build Coastguard Worker   static const uint32_t kNEON = 1 << 12;
162*9356374aSAndroid Build Coastguard Worker   uint32_t hwcap = GetAuxval(AT_HWCAP);
163*9356374aSAndroid Build Coastguard Worker   if ((hwcap & kNEON) == 0) {
164*9356374aSAndroid Build Coastguard Worker     return false;
165*9356374aSAndroid Build Coastguard Worker   }
166*9356374aSAndroid Build Coastguard Worker 
167*9356374aSAndroid Build Coastguard Worker   // And use it again to detect AES.
168*9356374aSAndroid Build Coastguard Worker   static const uint32_t kAES = 1 << 0;
169*9356374aSAndroid Build Coastguard Worker   const uint32_t hwcap2 = GetAuxval(AT_HWCAP2);
170*9356374aSAndroid Build Coastguard Worker   return (hwcap2 & kAES) != 0;
171*9356374aSAndroid Build Coastguard Worker 
172*9356374aSAndroid Build Coastguard Worker #elif defined(ABSL_ARCH_AARCH64)
173*9356374aSAndroid Build Coastguard Worker   // For AARCH64: Require crypto+neon
174*9356374aSAndroid Build Coastguard Worker   // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0500f/CIHBIBBA.html
175*9356374aSAndroid Build Coastguard Worker   static const uint32_t kNEON = 1 << 1;
176*9356374aSAndroid Build Coastguard Worker   static const uint32_t kAES = 1 << 3;
177*9356374aSAndroid Build Coastguard Worker   const uint32_t hwcap = GetAuxval(AT_HWCAP);
178*9356374aSAndroid Build Coastguard Worker   return ((hwcap & kNEON) != 0) && ((hwcap & kAES) != 0);
179*9356374aSAndroid Build Coastguard Worker #endif
180*9356374aSAndroid Build Coastguard Worker 
181*9356374aSAndroid Build Coastguard Worker #else  // ABSL_INTERNAL_USE_GETAUXVAL
182*9356374aSAndroid Build Coastguard Worker   // 3. By default, assume that the compiler default.
183*9356374aSAndroid Build Coastguard Worker   return ABSL_HAVE_ACCELERATED_AES ? true : false;
184*9356374aSAndroid Build Coastguard Worker 
185*9356374aSAndroid Build Coastguard Worker #endif
186*9356374aSAndroid Build Coastguard Worker   // NOTE: There are some other techniques that may be worth trying:
187*9356374aSAndroid Build Coastguard Worker   //
188*9356374aSAndroid Build Coastguard Worker   // * Use an environment variable: ABSL_RANDOM_USE_HWAES
189*9356374aSAndroid Build Coastguard Worker   //
190*9356374aSAndroid Build Coastguard Worker   // * Rely on compiler-generated target-based dispatch.
191*9356374aSAndroid Build Coastguard Worker   // Using x86/gcc it might look something like this:
192*9356374aSAndroid Build Coastguard Worker   //
193*9356374aSAndroid Build Coastguard Worker   // int __attribute__((target("aes"))) HasAes() { return 1; }
194*9356374aSAndroid Build Coastguard Worker   // int __attribute__((target("default"))) HasAes() { return 0; }
195*9356374aSAndroid Build Coastguard Worker   //
196*9356374aSAndroid Build Coastguard Worker   // This does not work on all architecture/compiler combinations.
197*9356374aSAndroid Build Coastguard Worker   //
198*9356374aSAndroid Build Coastguard Worker   // * On Linux consider reading /proc/cpuinfo and/or /proc/self/auxv.
199*9356374aSAndroid Build Coastguard Worker   // These files have lines which are easy to parse; for ARM/AARCH64 it is quite
200*9356374aSAndroid Build Coastguard Worker   // easy to find the Features: line and extract aes / neon. Likewise for
201*9356374aSAndroid Build Coastguard Worker   // PPC.
202*9356374aSAndroid Build Coastguard Worker   //
203*9356374aSAndroid Build Coastguard Worker   // * Fork a process and test for SIGILL:
204*9356374aSAndroid Build Coastguard Worker   //
205*9356374aSAndroid Build Coastguard Worker   // * Many architectures have instructions to read the ISA. Unfortunately
206*9356374aSAndroid Build Coastguard Worker   //   most of those require that the code is running in ring 0 /
207*9356374aSAndroid Build Coastguard Worker   //   protected-mode.
208*9356374aSAndroid Build Coastguard Worker   //
209*9356374aSAndroid Build Coastguard Worker   //   There are several examples. e.g. Valgrind detects PPC ISA 2.07:
210*9356374aSAndroid Build Coastguard Worker   //   https://github.com/lu-zero/valgrind/blob/master/none/tests/ppc64/test_isa_2_07_part1.c
211*9356374aSAndroid Build Coastguard Worker   //
212*9356374aSAndroid Build Coastguard Worker   //   MRS <Xt>, ID_AA64ISAR0_EL1 ; Read ID_AA64ISAR0_EL1 into Xt
213*9356374aSAndroid Build Coastguard Worker   //
214*9356374aSAndroid Build Coastguard Worker   //   uint64_t val;
215*9356374aSAndroid Build Coastguard Worker   //   __asm __volatile("mrs %0, id_aa64isar0_el1" :"=&r" (val));
216*9356374aSAndroid Build Coastguard Worker   //
217*9356374aSAndroid Build Coastguard Worker   // * Use a CPUID-style heuristic database.
218*9356374aSAndroid Build Coastguard Worker   //
219*9356374aSAndroid Build Coastguard Worker   // * On Apple (__APPLE__), AES is available on Arm v8.
220*9356374aSAndroid Build Coastguard Worker   //   https://stackoverflow.com/questions/45637888/how-to-determine-armv8-features-at-runtime-on-ios
221*9356374aSAndroid Build Coastguard Worker }
222*9356374aSAndroid Build Coastguard Worker 
223*9356374aSAndroid Build Coastguard Worker #if defined(__clang__)
224*9356374aSAndroid Build Coastguard Worker #pragma clang diagnostic pop
225*9356374aSAndroid Build Coastguard Worker #endif
226*9356374aSAndroid Build Coastguard Worker 
227*9356374aSAndroid Build Coastguard Worker }  // namespace random_internal
228*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
229*9356374aSAndroid Build Coastguard Worker }  // namespace absl
230