|
20 | 20 | #include <cassert>
|
21 | 21 | #include <cstdint>
|
22 | 22 |
|
| 23 | +namespace llvm { |
| 24 | +class hash_code; |
| 25 | +} |
| 26 | + |
23 | 27 | namespace clang {
|
24 | 28 |
|
25 |
| -using SanitizerMask = uint64_t; |
| 29 | +class SanitizerMask { |
| 30 | + /// Number of array elements. |
| 31 | + static constexpr unsigned kNumElem = 2; |
| 32 | + /// Mask value initialized to 0. |
| 33 | + uint64_t maskLoToHigh[kNumElem]{}; |
| 34 | + /// Number of bits in a mask. |
| 35 | + static constexpr unsigned kNumBits = sizeof(decltype(maskLoToHigh)) * 8; |
| 36 | + /// Number of bits in a mask element. |
| 37 | + static constexpr unsigned kNumBitElem = sizeof(decltype(maskLoToHigh[0])) * 8; |
| 38 | + |
| 39 | +public: |
| 40 | + static constexpr bool checkBitPos(const unsigned Pos) { |
| 41 | + return Pos < kNumBits; |
| 42 | + } |
26 | 43 |
|
27 |
| -namespace SanitizerKind { |
| 44 | + /// Create a mask with a bit enabled at position Pos. |
| 45 | + static SanitizerMask bitPosToMask(const unsigned Pos) { |
| 46 | + assert(Pos < kNumBits && "Bit position too big."); |
| 47 | + SanitizerMask mask; |
| 48 | + mask.maskLoToHigh[Pos / kNumBitElem] = 1ULL << Pos % kNumBitElem; |
| 49 | + return mask; |
| 50 | + } |
28 | 51 |
|
29 |
| -// Assign ordinals to possible values of -fsanitize= flag, which we will use as |
30 |
| -// bit positions. |
31 |
| -enum SanitizerOrdinal : uint64_t { |
32 |
| -#define SANITIZER(NAME, ID) SO_##ID, |
33 |
| -#define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group, |
34 |
| -#include "clang/Basic/Sanitizers.def" |
35 |
| - SO_Count |
| 52 | + unsigned countPopulation() const { |
| 53 | + unsigned total = 0; |
| 54 | + for (const auto &Val : maskLoToHigh) |
| 55 | + total += llvm::countPopulation(Val); |
| 56 | + return total; |
| 57 | + } |
| 58 | + |
| 59 | + void flipAllBits() { |
| 60 | + for (auto &Val : maskLoToHigh) |
| 61 | + Val = ~Val; |
| 62 | + } |
| 63 | + |
| 64 | + bool isPowerOf2() const { |
| 65 | + return countPopulation() == 1; |
| 66 | + } |
| 67 | + |
| 68 | + llvm::hash_code hash_value() const; |
| 69 | + |
| 70 | + explicit operator bool() const { |
| 71 | + for (const auto &Val : maskLoToHigh) |
| 72 | + if (Val) |
| 73 | + return true; |
| 74 | + return false; |
| 75 | + }; |
| 76 | + |
| 77 | + bool operator==(const SanitizerMask &V) const { |
| 78 | + for (unsigned k = 0; k < kNumElem; k++) { |
| 79 | + if (maskLoToHigh[k] != V.maskLoToHigh[k]) |
| 80 | + return false; |
| 81 | + } |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + SanitizerMask &operator&=(const SanitizerMask &RHS) { |
| 86 | + for (unsigned k = 0; k < kNumElem; k++) |
| 87 | + maskLoToHigh[k] &= RHS.maskLoToHigh[k]; |
| 88 | + return *this; |
| 89 | + } |
| 90 | + |
| 91 | + SanitizerMask &operator|=(const SanitizerMask &RHS) { |
| 92 | + for (unsigned k = 0; k < kNumElem; k++) |
| 93 | + maskLoToHigh[k] |= RHS.maskLoToHigh[k]; |
| 94 | + return *this; |
| 95 | + } |
| 96 | + |
| 97 | + bool operator!() const { |
| 98 | + for (const auto &Val : maskLoToHigh) |
| 99 | + if (Val) |
| 100 | + return false; |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + bool operator!=(const SanitizerMask &RHS) const { return !((*this) == RHS); } |
36 | 105 | };
|
37 | 106 |
|
| 107 | +// Declaring in clang namespace so that it can be found by ADL. |
| 108 | +llvm::hash_code hash_value(const clang::SanitizerMask &Arg); |
| 109 | + |
| 110 | +inline SanitizerMask operator~(SanitizerMask v) { |
| 111 | + v.flipAllBits(); |
| 112 | + return v; |
| 113 | +} |
| 114 | + |
| 115 | +inline SanitizerMask operator&(SanitizerMask a, const SanitizerMask &b) { |
| 116 | + a &= b; |
| 117 | + return a; |
| 118 | +} |
| 119 | + |
| 120 | +inline SanitizerMask operator|(SanitizerMask a, const SanitizerMask &b) { |
| 121 | + a |= b; |
| 122 | + return a; |
| 123 | +} |
| 124 | + |
38 | 125 | // Define the set of sanitizer kinds, as well as the set of sanitizers each
|
39 | 126 | // sanitizer group expands into.
|
40 |
| -#define SANITIZER(NAME, ID) \ |
41 |
| - const SanitizerMask ID = 1ULL << SO_##ID; |
42 |
| -#define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
43 |
| - const SanitizerMask ID = ALIAS; \ |
44 |
| - const SanitizerMask ID##Group = 1ULL << SO_##ID##Group; |
| 127 | +// Uses static data member of a class template as recommended in second |
| 128 | +// workaround from n4424 to avoid odr issues. |
| 129 | +// FIXME: Can be marked as constexpr once c++14 can be used in llvm. |
| 130 | +// FIXME: n4424 workaround can be replaced by c++17 inline variable. |
| 131 | +template <typename T = void> struct SanitizerMasks { |
| 132 | + |
| 133 | + // Assign ordinals to possible values of -fsanitize= flag, which we will use |
| 134 | + // as bit positions. |
| 135 | + enum SanitizerOrdinal : uint64_t { |
| 136 | +#define SANITIZER(NAME, ID) SO_##ID, |
| 137 | +#define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group, |
| 138 | +#include "clang/Basic/Sanitizers.def" |
| 139 | + SO_Count |
| 140 | + }; |
| 141 | + |
| 142 | +#define SANITIZER(NAME, ID) \ |
| 143 | + static const SanitizerMask ID; \ |
| 144 | + static_assert(SanitizerMask::checkBitPos(SO_##ID), "Bit position too big."); |
| 145 | +#define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
| 146 | + static const SanitizerMask ID; \ |
| 147 | + static const SanitizerMask ID##Group; \ |
| 148 | + static_assert(SanitizerMask::checkBitPos(SO_##ID##Group), \ |
| 149 | + "Bit position too big."); |
45 | 150 | #include "clang/Basic/Sanitizers.def"
|
| 151 | +}; // SanitizerMasks |
| 152 | + |
| 153 | +#define SANITIZER(NAME, ID) \ |
| 154 | + template <typename T> \ |
| 155 | + const SanitizerMask SanitizerMasks<T>::ID = \ |
| 156 | + SanitizerMask::bitPosToMask(SO_##ID); |
| 157 | +#define SANITIZER_GROUP(NAME, ID, ALIAS) \ |
| 158 | + template <typename T> \ |
| 159 | + const SanitizerMask SanitizerMasks<T>::ID = SanitizerMask(ALIAS); \ |
| 160 | + template <typename T> \ |
| 161 | + const SanitizerMask SanitizerMasks<T>::ID##Group = \ |
| 162 | + SanitizerMask::bitPosToMask(SO_##ID##Group); |
| 163 | +#include "clang/Basic/Sanitizers.def" |
| 164 | + |
| 165 | +// Explicit instantiation here to ensure correct initialization order. |
| 166 | +template struct SanitizerMasks<>; |
46 | 167 |
|
47 |
| -} // namespace SanitizerKind |
| 168 | +using SanitizerKind = SanitizerMasks<>; |
48 | 169 |
|
49 | 170 | struct SanitizerSet {
|
50 | 171 | /// Check if a certain (single) sanitizer is enabled.
|
51 | 172 | bool has(SanitizerMask K) const {
|
52 |
| - assert(llvm::isPowerOf2_64(K)); |
53 |
| - return Mask & K; |
| 173 | + assert(K.isPowerOf2() && "Has to be a single sanitizer."); |
| 174 | + return static_cast<bool>(Mask & K); |
54 | 175 | }
|
55 | 176 |
|
56 | 177 | /// Check if one or more sanitizers are enabled.
|
57 |
| - bool hasOneOf(SanitizerMask K) const { return Mask & K; } |
| 178 | + bool hasOneOf(SanitizerMask K) const { return static_cast<bool>(Mask & K); } |
58 | 179 |
|
59 | 180 | /// Enable or disable a certain (single) sanitizer.
|
60 | 181 | void set(SanitizerMask K, bool Value) {
|
61 |
| - assert(llvm::isPowerOf2_64(K)); |
| 182 | + assert(K.isPowerOf2() && "Has to be a single sanitizer."); |
62 | 183 | Mask = Value ? (Mask | K) : (Mask & ~K);
|
63 | 184 | }
|
64 | 185 |
|
65 | 186 | /// Disable the sanitizers specified in \p K.
|
66 | 187 | void clear(SanitizerMask K = SanitizerKind::All) { Mask &= ~K; }
|
67 | 188 |
|
68 | 189 | /// Returns true if no sanitizers are enabled.
|
69 |
| - bool empty() const { return Mask == 0; } |
| 190 | + bool empty() const { return !Mask; } |
70 | 191 |
|
71 | 192 | /// Bitmask of enabled sanitizers.
|
72 |
| - SanitizerMask Mask = 0; |
| 193 | + SanitizerMask Mask; |
73 | 194 | };
|
74 | 195 |
|
75 | 196 | /// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
|
|
0 commit comments