Skip to content

Commit fe5f66d

Browse files
committed
[HWASan][NFC] Introduce constants for tag bits and masks.
x86_64 aliasing mode will use fewer than 8 bits for tags, so refactor existing code to remove hard-coded 0xff and 8 values. Reviewed By: vitalybuka, eugenis Differential Revision: https://reviews.llvm.org/D98072
1 parent 3e32e8c commit fe5f66d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

compiler-rt/lib/hwasan/hwasan.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ typedef u8 tag_t;
3737

3838
// TBI (Top Byte Ignore) feature of AArch64: bits [63:56] are ignored in address
3939
// translation and can be used to store a tag.
40-
const unsigned kAddressTagShift = 56;
41-
const uptr kAddressTagMask = 0xFFUL << kAddressTagShift;
40+
constexpr unsigned kAddressTagShift = 56;
41+
constexpr unsigned kTagBits = 8;
42+
43+
// Mask for extracting tag bits from the lower 8 bits.
44+
constexpr uptr kTagMask = (1UL << kTagBits) - 1;
45+
46+
// Masks for extracting and removing tags from full pointers.
47+
constexpr uptr kAddressTagMask = kTagMask << kAddressTagShift;
4248

4349
// Minimal alignment of the shadow base address. Determines the space available
4450
// for threads and stack histories. This is an ABI constant.
@@ -50,7 +56,7 @@ const unsigned kRecordFPLShift = 4;
5056
const unsigned kRecordFPModulus = 1 << (64 - kRecordFPShift + kRecordFPLShift);
5157

5258
static inline tag_t GetTagFromPointer(uptr p) {
53-
return p >> kAddressTagShift;
59+
return (p >> kAddressTagShift) & kTagMask;
5460
}
5561

5662
static inline uptr UntagAddr(uptr tagged_addr) {

compiler-rt/lib/hwasan/hwasan_thread.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,21 @@ static u32 xorshift(u32 state) {
113113
}
114114

115115
// Generate a (pseudo-)random non-zero tag.
116-
tag_t Thread::GenerateRandomTag() {
116+
tag_t Thread::GenerateRandomTag(uptr num_bits) {
117+
DCHECK_GT(num_bits, 0);
117118
if (tagging_disabled_) return 0;
118119
tag_t tag;
120+
const uptr tag_mask = (1ULL << num_bits) - 1;
119121
do {
120122
if (flags()->random_tags) {
121123
if (!random_buffer_)
122124
random_buffer_ = random_state_ = xorshift(random_state_);
123125
CHECK(random_buffer_);
124-
tag = random_buffer_ & 0xFF;
125-
random_buffer_ >>= 8;
126+
tag = random_buffer_ & tag_mask;
127+
random_buffer_ >>= num_bits;
126128
} else {
127-
tag = random_state_ = (random_state_ + 1) & 0xFF;
129+
random_state_ += 1;
130+
tag = random_state_ & tag_mask;
128131
}
129132
} while (!tag);
130133
return tag;

compiler-rt/lib/hwasan/hwasan_thread.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Thread {
4242
HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }
4343
StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }
4444

45-
tag_t GenerateRandomTag();
45+
tag_t GenerateRandomTag(uptr num_bits = kTagBits);
4646

4747
void DisableTagging() { tagging_disabled_++; }
4848
void EnableTagging() { tagging_disabled_--; }

0 commit comments

Comments
 (0)