Skip to content

Commit 8dbb739

Browse files
authored
[NFC][hwasan] Use enum class in ShadowMapping (#109617)
1 parent 3000511 commit 8dbb739

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ const char kHwasanShadowMemoryDynamicAddress[] =
8383
static const size_t kNumberOfAccessSizes = 5;
8484

8585
static const size_t kDefaultShadowScale = 4;
86-
static const uint64_t kDynamicShadowSentinel =
87-
std::numeric_limits<uint64_t>::max();
8886

8987
static const unsigned kShadowBaseAlignment = 32;
9088

@@ -385,44 +383,44 @@ class HWAddressSanitizer {
385383
std::unique_ptr<RandomNumberGenerator> Rng;
386384

387385
/// This struct defines the shadow mapping using the rule:
386+
/// If `kFixed`, then
388387
/// shadow = (mem >> Scale) + Offset.
389-
/// If InGlobal is true, then
388+
/// If `kGlobal`, then
389+
/// extern char* __hwasan_shadow_memory_dynamic_address;
390+
/// shadow = (mem >> Scale) + __hwasan_shadow_memory_dynamic_address
391+
/// If `kIfunc`, then
390392
/// extern char __hwasan_shadow[];
391393
/// shadow = (mem >> Scale) + &__hwasan_shadow
392-
/// If InTls is true, then
394+
/// If `kTls`, then
393395
/// extern char *__hwasan_tls;
394396
/// shadow = (mem>>Scale) + align_up(__hwasan_shadow, kShadowBaseAlignment)
395397
///
396398
/// If WithFrameRecord is true, then __hwasan_tls will be used to access the
397399
/// ring buffer for storing stack allocations on targets that support it.
398400
class ShadowMapping {
399-
uint8_t Scale;
401+
enum class OffsetKind {
402+
kFixed = 0,
403+
kGlobal,
404+
kIfunc,
405+
kTls,
406+
};
407+
OffsetKind Kind;
400408
uint64_t Offset;
401-
bool InGlobal;
402-
bool InTls;
409+
uint8_t Scale;
403410
bool WithFrameRecord;
404411

412+
void SetFixed(uint64_t O) {
413+
Kind = OffsetKind::kFixed;
414+
Offset = O;
415+
}
416+
405417
public:
406418
void init(Triple &TargetTriple, bool InstrumentWithCalls);
407419
Align getObjectAlignment() const { return Align(1ULL << Scale); }
408-
bool isInGlobal() const {
409-
return !InGlobal && !InTls && Offset == kDynamicShadowSentinel;
410-
}
411-
bool isInIfunc() const {
412-
assert(!InGlobal || !InTls);
413-
assert(!InGlobal || Offset == kDynamicShadowSentinel);
414-
return InGlobal;
415-
}
416-
bool isInTls() const {
417-
assert(!InTls || !InGlobal);
418-
assert(!InTls || Offset == kDynamicShadowSentinel);
419-
return InTls;
420-
}
421-
bool isFixed() const {
422-
assert(Offset == kDynamicShadowSentinel || !InTls);
423-
assert(Offset == kDynamicShadowSentinel || !InGlobal);
424-
return Offset != kDynamicShadowSentinel;
425-
}
420+
bool isInGlobal() const { return Kind == OffsetKind::kGlobal; }
421+
bool isInIfunc() const { return Kind == OffsetKind::kIfunc; }
422+
bool isInTls() const { return Kind == OffsetKind::kTls; }
423+
bool isFixed() const { return Kind == OffsetKind::kFixed; }
426424
uint8_t scale() const { return Scale; };
427425
uint64_t offset() const {
428426
assert(isFixed());
@@ -1930,34 +1928,22 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
19301928
if (TargetTriple.isOSFuchsia()) {
19311929
// Fuchsia is always PIE, which means that the beginning of the address
19321930
// space is always available.
1933-
InGlobal = false;
1934-
InTls = false;
1935-
Offset = 0;
1931+
SetFixed(0);
19361932
WithFrameRecord = true;
19371933
} else if (ClMappingOffset.getNumOccurrences() > 0) {
1938-
InGlobal = false;
1939-
InTls = false;
1940-
Offset = ClMappingOffset;
1934+
SetFixed(ClMappingOffset);
19411935
WithFrameRecord = false;
19421936
} else if (ClEnableKhwasan || InstrumentWithCalls) {
1943-
InGlobal = false;
1944-
InTls = false;
1945-
Offset = 0;
1937+
SetFixed(0);
19461938
WithFrameRecord = false;
19471939
} else if (ClWithIfunc) {
1948-
InGlobal = true;
1949-
InTls = false;
1950-
Offset = kDynamicShadowSentinel;
1940+
Kind = OffsetKind::kIfunc;
19511941
WithFrameRecord = false;
19521942
} else if (ClWithTls) {
1953-
InGlobal = false;
1954-
InTls = true;
1955-
Offset = kDynamicShadowSentinel;
1943+
Kind = OffsetKind::kTls;
19561944
WithFrameRecord = true;
19571945
} else {
1958-
InGlobal = false;
1959-
InTls = false;
1960-
Offset = kDynamicShadowSentinel;
1946+
Kind = OffsetKind::kGlobal;
19611947
WithFrameRecord = false;
19621948
}
19631949
}

0 commit comments

Comments
 (0)