Skip to content

Commit

Permalink
sanitizer_common: remove debugging logic from the internal allocator
Browse files Browse the repository at this point in the history
The internal allocator adds 8-byte header for debugging purposes.
The problem with it is that it's not possible to allocate nicely-sized
objects without a significant overhead. For example, if we allocate
512-byte objects, that will be rounded up to 768 or something.
This logic migrated from tsan where it was added during initial development,
I don't remember that it ever caught anything (we don't do bugs!).
Remove it so that it's possible to allocate nicely-sized objects
without overheads.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D105777
  • Loading branch information
dvyukov committed Jul 12, 2021
1 parent de59f56 commit fde34d9
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions compiler-rt/lib/sanitizer_common/sanitizer_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,6 @@ static void RawInternalFree(void *ptr, InternalAllocatorCache *cache) {

#endif // SANITIZER_GO || defined(SANITIZER_USE_MALLOC)

namespace {
const u64 kBlockMagic = 0x6A6CB03ABCEBC041ull;

struct BlockHeader {
u64 magic;
};
} // namespace

static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
SetAllocatorOutOfMemory();
Report("FATAL: %s: internal allocator is out of memory trying to allocate "
Expand All @@ -153,28 +145,17 @@ static void NORETURN ReportInternalAllocatorOutOfMemory(uptr requested_size) {
}

void *InternalAlloc(uptr size, InternalAllocatorCache *cache, uptr alignment) {
uptr s = size + sizeof(BlockHeader);
if (s < size)
return nullptr;
BlockHeader *p = (BlockHeader *)RawInternalAlloc(s, cache, alignment);
void *p = RawInternalAlloc(size, cache, alignment);
if (UNLIKELY(!p))
ReportInternalAllocatorOutOfMemory(s);
p->magic = kBlockMagic;
return p + 1;
ReportInternalAllocatorOutOfMemory(size);
return p;
}

void *InternalRealloc(void *addr, uptr size, InternalAllocatorCache *cache) {
if (!addr)
return InternalAlloc(size, cache);
uptr s = size + sizeof(BlockHeader);
if (s < size)
return nullptr;
BlockHeader *p = (BlockHeader *)addr - 1;
CHECK_EQ(kBlockMagic, p->magic);
p = (BlockHeader *)RawInternalRealloc(p, s, cache);
void *p = RawInternalRealloc(addr, size, cache);
if (UNLIKELY(!p))
ReportInternalAllocatorOutOfMemory(s);
return p + 1;
ReportInternalAllocatorOutOfMemory(size);
return p;
}

void *InternalReallocArray(void *addr, uptr count, uptr size,
Expand Down Expand Up @@ -203,12 +184,7 @@ void *InternalCalloc(uptr count, uptr size, InternalAllocatorCache *cache) {
}

void InternalFree(void *addr, InternalAllocatorCache *cache) {
if (!addr)
return;
BlockHeader *p = (BlockHeader *)addr - 1;
CHECK_EQ(kBlockMagic, p->magic);
p->magic = 0;
RawInternalFree(p, cache);
RawInternalFree(addr, cache);
}

// LowLevelAllocator
Expand Down

0 comments on commit fde34d9

Please sign in to comment.