1 //===-- scudo_utils.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// Platform specific utility functions.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "scudo_utils.h"
15 
16 #if defined(__x86_64__) || defined(__i386__)
17 # include <cpuid.h>
18 #elif defined(__arm__) || defined(__aarch64__)
19 # include "sanitizer_common/sanitizer_getauxval.h"
20 # if SANITIZER_POSIX
21 #  include "sanitizer_common/sanitizer_posix.h"
22 #  include <fcntl.h>
23 # endif
24 #endif
25 
26 #include <stdarg.h>
27 
28 // TODO(kostyak): remove __sanitizer *Printf uses in favor for our own less
29 //                complicated string formatting code. The following is a
30 //                temporary workaround to be able to use __sanitizer::VSNPrintf.
31 namespace __sanitizer {
32 
33 extern int VSNPrintf(char *buff, int buff_length, const char *format,
34                      va_list args);
35 
36 }  // namespace __sanitizer
37 
38 namespace __scudo {
39 
40 FORMAT(1, 2) void NORETURN dieWithMessage(const char *Format, ...) {
41   static const char ScudoError[] = "Scudo ERROR: ";
42   static constexpr uptr PrefixSize = sizeof(ScudoError) - 1;
43   // Our messages are tiny, 256 characters is more than enough.
44   char Message[256];
45   va_list Args;
46   va_start(Args, Format);
47   internal_memcpy(Message, ScudoError, PrefixSize);
48   VSNPrintf(Message + PrefixSize, sizeof(Message) - PrefixSize, Format, Args);
49   va_end(Args);
50   RawWrite(Message);
51   Die();
52 }
53 
54 #if defined(__x86_64__) || defined(__i386__)
55 // i386 and x86_64 specific code to detect CRC32 hardware support via CPUID.
56 // CRC32 requires the SSE 4.2 instruction set.
57 # ifndef bit_SSE4_2
58 #  define bit_SSE4_2 bit_SSE42  // clang and gcc have different defines.
59 # endif
60 bool hasHardwareCRC32() {
61   u32 Eax, Ebx, Ecx, Edx;
62   __get_cpuid(0, &Eax, &Ebx, &Ecx, &Edx);
63   const bool IsIntel = (Ebx == signature_INTEL_ebx) &&
64                        (Edx == signature_INTEL_edx) &&
65                        (Ecx == signature_INTEL_ecx);
66   const bool IsAMD = (Ebx == signature_AMD_ebx) &&
67                      (Edx == signature_AMD_edx) &&
68                      (Ecx == signature_AMD_ecx);
69   if (!IsIntel && !IsAMD)
70     return false;
71   __get_cpuid(1, &Eax, &Ebx, &Ecx, &Edx);
72   return !!(Ecx & bit_SSE4_2);
73 }
74 #elif defined(__arm__) || defined(__aarch64__)
75 // For ARM and AArch64, hardware CRC32 support is indicated in the AT_HWCAP
76 // auxiliary vector.
77 # ifndef AT_HWCAP
78 #  define AT_HWCAP 16
79 # endif
80 # ifndef HWCAP_CRC32
81 #  define HWCAP_CRC32 (1 << 7)  // HWCAP_CRC32 is missing on older platforms.
82 # endif
83 # if SANITIZER_POSIX
84 bool hasHardwareCRC32ARMPosix() {
85   uptr F = internal_open("/proc/self/auxv", O_RDONLY);
86   if (internal_iserror(F))
87     return false;
88   struct { uptr Tag; uptr Value; } Entry = { 0, 0 };
89   for (;;) {
90     uptr N = internal_read(F, &Entry, sizeof(Entry));
91     if (internal_iserror(N) || N != sizeof(Entry) ||
92         (Entry.Tag == 0 && Entry.Value == 0) || Entry.Tag == AT_HWCAP)
93       break;
94   }
95   internal_close(F);
96   return (Entry.Tag == AT_HWCAP && (Entry.Value & HWCAP_CRC32) != 0);
97 }
98 # else
99 bool hasHardwareCRC32ARMPosix() { return false; }
100 # endif  // SANITIZER_POSIX
101 
102 // Bionic doesn't initialize its globals early enough. This causes issues when
103 // trying to access them from a preinit_array (b/25751302) or from another
104 // constructor called before the libc one (b/68046352). __progname is
105 // initialized after the other globals, so we can check its value to know if
106 // calling getauxval is safe.
107 extern "C" SANITIZER_WEAK_ATTRIBUTE char *__progname;
108 INLINE bool areBionicGlobalsInitialized() {
109   return !SANITIZER_ANDROID || (&__progname && __progname);
110 }
111 
112 bool hasHardwareCRC32() {
113   if (&getauxval && areBionicGlobalsInitialized())
114     return !!(getauxval(AT_HWCAP) & HWCAP_CRC32);
115   return hasHardwareCRC32ARMPosix();
116 }
117 #else
118 bool hasHardwareCRC32() { return false; }
119 #endif  // defined(__x86_64__) || defined(__i386__)
120 
121 }  // namespace __scudo
122