summaryrefslogtreecommitdiff
path: root/lib/scudo/scudo_utils.cpp
blob: 093eafe8edc70ffc505813fcae95d64d6a8d9f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===-- scudo_utils.cpp -----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// Platform specific utility functions.
///
//===----------------------------------------------------------------------===//

#include "scudo_utils.h"

#include <stdarg.h>
#if defined(__x86_64__) || defined(__i386__)
# include <cpuid.h>
#endif
#if defined(__arm__) || defined(__aarch64__)
# if SANITIZER_ANDROID && __ANDROID_API__ < 18
// getauxval() was introduced with API level 18 on Android. Emulate it using
// /proc/self/auxv for lower API levels.
#  include "sanitizer_common/sanitizer_posix.h"

#  include <fcntl.h>

#  define AT_HWCAP 16

namespace __sanitizer {

uptr getauxval(uptr Type) {
  uptr F = internal_open("/proc/self/auxv", O_RDONLY);
  if (internal_iserror(F))
    return 0;
  struct { uptr Tag; uptr Value; } Entry;
  uptr Result = 0;
  for (;;) {
    uptr N = internal_read(F, &Entry, sizeof(Entry));
    if (internal_iserror(N))
      break;
    if (N == 0 || N != sizeof(Entry) || (Entry.Tag == 0 && Entry.Value == 0))
      break;
    if (Entry.Tag == Type) {
      Result =  Entry.Value;
      break;
    }
  }
  internal_close(F);
  return Result;
}

}  // namespace __sanitizer
# else
#  include <sys/auxv.h>
# endif
#endif

// TODO(kostyak): remove __sanitizer *Printf uses in favor for our own less
//                complicated string formatting code. The following is a
//                temporary workaround to be able to use __sanitizer::VSNPrintf.
namespace __sanitizer {

extern int VSNPrintf(char *buff, int buff_length, const char *format,
                     va_list args);

}  // namespace __sanitizer

namespace __scudo {

FORMAT(1, 2)
void NORETURN dieWithMessage(const char *Format, ...) {
  // Our messages are tiny, 256 characters is more than enough.
  char Message[256];
  va_list Args;
  va_start(Args, Format);
  __sanitizer::VSNPrintf(Message, sizeof(Message), Format, Args);
  va_end(Args);
  RawWrite(Message);
  Die();
}

#if defined(__x86_64__) || defined(__i386__)
// i386 and x86_64 specific code to detect CRC32 hardware support via CPUID.
// CRC32 requires the SSE 4.2 instruction set.
typedef struct {
  u32 Eax;
  u32 Ebx;
  u32 Ecx;
  u32 Edx;
} CPUIDRegs;

static void getCPUID(CPUIDRegs *Regs, u32 Level)
{
  __get_cpuid(Level, &Regs->Eax, &Regs->Ebx, &Regs->Ecx, &Regs->Edx);
}

CPUIDRegs getCPUFeatures() {
  CPUIDRegs VendorRegs = {};
  getCPUID(&VendorRegs, 0);
  bool IsIntel =
      (VendorRegs.Ebx == signature_INTEL_ebx) &&
      (VendorRegs.Edx == signature_INTEL_edx) &&
      (VendorRegs.Ecx == signature_INTEL_ecx);
  bool IsAMD =
      (VendorRegs.Ebx == signature_AMD_ebx) &&
      (VendorRegs.Edx == signature_AMD_edx) &&
      (VendorRegs.Ecx == signature_AMD_ecx);
  // Default to an empty feature set if not on a supported CPU.
  CPUIDRegs FeaturesRegs = {};
  if (IsIntel || IsAMD) {
    getCPUID(&FeaturesRegs, 1);
  }
  return FeaturesRegs;
}

# ifndef bit_SSE4_2
#  define bit_SSE4_2 bit_SSE42  // clang and gcc have different defines.
# endif

bool testCPUFeature(CPUFeature Feature)
{
  CPUIDRegs FeaturesRegs = getCPUFeatures();

  switch (Feature) {
    case CRC32CPUFeature:  // CRC32 is provided by SSE 4.2.
      return !!(FeaturesRegs.Ecx & bit_SSE4_2);
    default:
      break;
  }
  return false;
}
#elif defined(__arm__) || defined(__aarch64__)
// For ARM and AArch64, hardware CRC32 support is indicated in the AT_HWVAL
// auxiliary vector.

# ifndef HWCAP_CRC32
#  define HWCAP_CRC32 (1 << 7)  // HWCAP_CRC32 is missing on older platforms.
# endif

bool testCPUFeature(CPUFeature Feature) {
  uptr HWCap = getauxval(AT_HWCAP);

  switch (Feature) {
    case CRC32CPUFeature:
      return !!(HWCap & HWCAP_CRC32);
    default:
      break;
  }
  return false;
}
#else
bool testCPUFeature(CPUFeature Feature) {
  return false;
}
#endif  // defined(__x86_64__) || defined(__i386__)

}  // namespace __scudo