Skip to content

Commit d74e1f3

Browse files
committed
[NFC][Asan] Don't use MetaData for size
Now we have enough space in the ChunkHeader. 45 bit is enough for kMaxAllowedMallocSize. Depends on D87642. Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D87643
1 parent c6aadd2 commit d74e1f3

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,32 @@ class ChunkHeader {
9494
u8 rz_log : 3;
9595
u8 lsan_tag : 2;
9696

97-
// This field is used for small sizes. For large sizes it is equal to
98-
// SizeClassMap::kMaxSize and the actual size is stored in the
99-
// SecondaryAllocator's metadata.
100-
u32 user_requested_size : 29;
10197
// align < 8 -> 0
10298
// else -> log2(min(align, 512)) - 2
103-
u32 user_requested_alignment_log : 3;
99+
u16 user_requested_alignment_log : 3;
104100

105101
private:
102+
u16 user_requested_size_hi : 13;
103+
u32 user_requested_size_lo;
106104
atomic_uint64_t alloc_context_id;
107105

108106
public:
107+
uptr UsedSize() const {
108+
uptr R = user_requested_size_lo;
109+
if (sizeof(uptr) > sizeof(user_requested_size_lo))
110+
R += (uptr)user_requested_size_hi << (8 * sizeof(user_requested_size_lo));
111+
return R;
112+
}
113+
114+
void SetUsedSize(uptr size) {
115+
user_requested_size_lo = size;
116+
if (sizeof(uptr) > sizeof(user_requested_size_lo)) {
117+
size >>= (8 * sizeof(user_requested_size_lo));
118+
user_requested_size_hi = size;
119+
CHECK_EQ(user_requested_size_hi, size);
120+
}
121+
}
122+
109123
void SetAllocContext(u32 tid, u32 stack) {
110124
AtomicContextStore(&alloc_context_id, tid, stack);
111125
}
@@ -147,19 +161,10 @@ enum {
147161
class AsanChunk : public ChunkBase {
148162
public:
149163
uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; }
150-
uptr UsedSize(bool locked_version = false) {
151-
if (user_requested_size != SizeClassMap::kMaxSize)
152-
return user_requested_size;
153-
return *reinterpret_cast<uptr *>(
154-
get_allocator().GetMetaData(AllocBeg(locked_version)));
155-
}
156-
void *AllocBeg(bool locked_version = false) {
157-
if (from_memalign) {
158-
if (locked_version)
159-
return get_allocator().GetBlockBeginFastLocked(
160-
reinterpret_cast<void *>(this));
164+
165+
void *AllocBeg() {
166+
if (from_memalign)
161167
return get_allocator().GetBlockBegin(reinterpret_cast<void *>(this));
162-
}
163168
return reinterpret_cast<void*>(Beg() - RZLog2Size(rz_log));
164169
}
165170
};
@@ -337,7 +342,7 @@ struct Allocator {
337342
if (ac && atomic_load(&ac->chunk_state, memory_order_acquire) ==
338343
CHUNK_ALLOCATED) {
339344
uptr beg = ac->Beg();
340-
uptr end = ac->Beg() + ac->UsedSize(true);
345+
uptr end = ac->Beg() + ac->UsedSize();
341346
uptr chunk_end = chunk + allocated_size;
342347
if (chunk < beg && beg < end && end <= chunk_end) {
343348
// Looks like a valid AsanChunk in use, poison redzones only.
@@ -552,15 +557,13 @@ struct Allocator {
552557
reinterpret_cast<uptr *>(alloc_beg)[0] = kAllocBegMagic;
553558
reinterpret_cast<uptr *>(alloc_beg)[1] = chunk_beg;
554559
}
560+
CHECK(size);
561+
m->SetUsedSize(size);
555562
if (using_primary_allocator) {
556-
CHECK(size);
557-
m->user_requested_size = size;
558563
CHECK(allocator.FromPrimary(allocated));
559564
} else {
560565
CHECK(!allocator.FromPrimary(allocated));
561-
m->user_requested_size = SizeClassMap::kMaxSize;
562566
uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(allocated));
563-
meta[0] = size;
564567
meta[1] = chunk_beg;
565568
}
566569
m->user_requested_alignment_log = user_requested_alignment_log;
@@ -1151,7 +1154,7 @@ void LsanMetadata::set_tag(ChunkTag value) {
11511154

11521155
uptr LsanMetadata::requested_size() const {
11531156
__asan::AsanChunk *m = reinterpret_cast<__asan::AsanChunk *>(metadata_);
1154-
return m->UsedSize(/*locked_version=*/true);
1157+
return m->UsedSize();
11551158
}
11561159

11571160
u32 LsanMetadata::stack_trace_id() const {

0 commit comments

Comments
 (0)