Skip to content

Commit 943b0c8

Browse files
committed
[NFC][Asan] Remove chunk pointer from metadata
kAllocBegMagic should be enough. kAllocBegMagic is already set for the Secondary allocations. kAllocBegMagic is good enough for the Primary, but it's even safer for the Secondary allocator as all allocated block are from mmap. Depends on D87646. Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D87647
1 parent 86ccf4f commit 943b0c8

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ class LargeChunkHeader {
167167
AsanChunk *chunk_header;
168168

169169
public:
170-
AsanChunk *Get() {
170+
AsanChunk *Get() const {
171171
return atomic_load(&magic, memory_order_acquire) == kAllocBegMagic
172172
? chunk_header
173-
: reinterpret_cast<AsanChunk *>(this);
173+
: nullptr;
174174
}
175175

176176
void Set(AsanChunk *p) {
@@ -510,13 +510,10 @@ struct Allocator {
510510
uptr needed_size = rounded_size + rz_size;
511511
if (alignment > min_alignment)
512512
needed_size += alignment;
513-
bool using_primary_allocator = true;
514513
// If we are allocating from the secondary allocator, there will be no
515514
// automatic right redzone, so add the right redzone manually.
516-
if (!PrimaryAllocator::CanAllocate(needed_size, alignment)) {
515+
if (!PrimaryAllocator::CanAllocate(needed_size, alignment))
517516
needed_size += rz_size;
518-
using_primary_allocator = false;
519-
}
520517
CHECK(IsAligned(needed_size, min_alignment));
521518
if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize ||
522519
size > max_user_defined_malloc_size) {
@@ -568,13 +565,6 @@ struct Allocator {
568565
m->alloc_type = alloc_type;
569566
CHECK(size);
570567
m->SetUsedSize(size);
571-
if (using_primary_allocator) {
572-
CHECK(allocator.FromPrimary(allocated));
573-
} else {
574-
CHECK(!allocator.FromPrimary(allocated));
575-
uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(allocated));
576-
meta[1] = chunk_beg;
577-
}
578568
m->user_requested_alignment_log = user_requested_alignment_log;
579569

580570
m->SetAllocContext(t ? t->tid() : 0, StackDepotPut(*stack));
@@ -782,15 +772,12 @@ struct Allocator {
782772
AsanChunk *GetAsanChunk(void *alloc_beg) {
783773
if (!alloc_beg)
784774
return nullptr;
785-
AsanChunk *p = nullptr;
786-
if (!allocator.FromPrimary(alloc_beg)) {
787-
uptr *meta = reinterpret_cast<uptr *>(allocator.GetMetaData(alloc_beg));
788-
p = reinterpret_cast<AsanChunk *>(meta[1]);
789-
} else {
790-
p = reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Get();
775+
AsanChunk *p = reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Get();
776+
if (!p) {
777+
if (!allocator.FromPrimary(alloc_beg))
778+
return nullptr;
779+
p = reinterpret_cast<AsanChunk *>(alloc_beg);
791780
}
792-
if (!p)
793-
return nullptr;
794781
u8 state = atomic_load(&p->chunk_state, memory_order_relaxed);
795782
// It does not guaranty that Chunk is initialized, but it's
796783
// definitely not for any other value.

0 commit comments

Comments
 (0)