1 //===-- scudo_utils.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// Platform specific utility functions.
12 //===----------------------------------------------------------------------===//
14 #include "scudo_utils.h"
16 #if defined(__x86_64__) || defined(__i386__)
18 #elif defined(__arm__) || defined(__aarch64__)
19 # include "sanitizer_common/sanitizer_getauxval.h"
20 # if SANITIZER_FUCHSIA
21 # include <zircon/syscalls.h>
22 # include <zircon/features.h>
23 # elif SANITIZER_POSIX
24 # include "sanitizer_common/sanitizer_posix.h"
31 // TODO(kostyak): remove __sanitizer *Printf uses in favor for our own less
32 // complicated string formatting code. The following is a
33 // temporary workaround to be able to use __sanitizer::VSNPrintf.
34 namespace __sanitizer {
36 extern int VSNPrintf(char *buff, int buff_length, const char *format,
39 } // namespace __sanitizer
43 FORMAT(1, 2) void NORETURN dieWithMessage(const char *Format, ...) {
44 static const char ScudoError[] = "Scudo ERROR: ";
45 static constexpr uptr PrefixSize = sizeof(ScudoError) - 1;
46 // Our messages are tiny, 256 characters is more than enough.
49 va_start(Args, Format);
50 internal_memcpy(Message, ScudoError, PrefixSize);
51 VSNPrintf(Message + PrefixSize, sizeof(Message) - PrefixSize, Format, Args);
53 LogMessageOnPrintf(Message);
54 if (common_flags()->abort_on_error)
55 SetAbortMessage(Message);
60 #if defined(__x86_64__) || defined(__i386__)
61 // i386 and x86_64 specific code to detect CRC32 hardware support via CPUID.
62 // CRC32 requires the SSE 4.2 instruction set.
64 # define bit_SSE4_2 bit_SSE42 // clang and gcc have different defines.
66 bool hasHardwareCRC32() {
67 u32 Eax, Ebx, Ecx, Edx;
68 __get_cpuid(0, &Eax, &Ebx, &Ecx, &Edx);
69 const bool IsIntel = (Ebx == signature_INTEL_ebx) &&
70 (Edx == signature_INTEL_edx) &&
71 (Ecx == signature_INTEL_ecx);
72 const bool IsAMD = (Ebx == signature_AMD_ebx) &&
73 (Edx == signature_AMD_edx) &&
74 (Ecx == signature_AMD_ecx);
75 if (!IsIntel && !IsAMD)
77 __get_cpuid(1, &Eax, &Ebx, &Ecx, &Edx);
78 return !!(Ecx & bit_SSE4_2);
80 #elif defined(__arm__) || defined(__aarch64__)
81 // For ARM and AArch64, hardware CRC32 support is indicated in the AT_HWCAP
87 # define HWCAP_CRC32 (1 << 7) // HWCAP_CRC32 is missing on older platforms.
90 bool hasHardwareCRC32ARMPosix() {
91 uptr F = internal_open("/proc/self/auxv", O_RDONLY);
92 if (internal_iserror(F))
94 struct { uptr Tag; uptr Value; } Entry = { 0, 0 };
96 uptr N = internal_read(F, &Entry, sizeof(Entry));
97 if (internal_iserror(N) || N != sizeof(Entry) ||
98 (Entry.Tag == 0 && Entry.Value == 0) || Entry.Tag == AT_HWCAP)
102 return (Entry.Tag == AT_HWCAP && (Entry.Value & HWCAP_CRC32) != 0);
105 bool hasHardwareCRC32ARMPosix() { return false; }
106 # endif // SANITIZER_POSIX
108 // Bionic doesn't initialize its globals early enough. This causes issues when
109 // trying to access them from a preinit_array (b/25751302) or from another
110 // constructor called before the libc one (b/68046352). __progname is
111 // initialized after the other globals, so we can check its value to know if
112 // calling getauxval is safe.
113 extern "C" SANITIZER_WEAK_ATTRIBUTE char *__progname;
114 INLINE bool areBionicGlobalsInitialized() {
115 return !SANITIZER_ANDROID || (&__progname && __progname);
118 bool hasHardwareCRC32() {
119 #if SANITIZER_FUCHSIA
121 zx_status_t Status = zx_system_get_features(ZX_FEATURE_KIND_CPU, &HWCap);
122 if (Status != ZX_OK || (HWCap & ZX_ARM64_FEATURE_ISA_CRC32) == 0)
126 if (&getauxval && areBionicGlobalsInitialized())
127 return !!(getauxval(AT_HWCAP) & HWCAP_CRC32);
128 return hasHardwareCRC32ARMPosix();
129 #endif // SANITIZER_FUCHSIA
132 bool hasHardwareCRC32() { return false; }
133 #endif // defined(__x86_64__) || defined(__i386__)
135 } // namespace __scudo