Skip to content

Commit 9a2f449

Browse files
authored
Merge pull request #40139 from mikeash/metadataallocator-null-check
[Runtime] Add a NULL check to MetadataAllocator.
2 parents c63a93c + 64f8bca commit 9a2f449

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5942,9 +5942,9 @@ void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
59425942
static OnceToken_t getenvToken;
59435943
SWIFT_ONCE_F(getenvToken, checkAllocatorDebugEnvironmentVariables, nullptr);
59445944

5945-
// If the size is larger than the maximum, just use malloc.
5945+
// If the size is larger than the maximum, just do a normal heap allocation.
59465946
if (size > PoolRange::MaxPoolAllocationSize) {
5947-
void *allocation = malloc(size);
5947+
void *allocation = swift_slowAlloc(size, alignment - 1);
59485948
memsetScribble(allocation, size);
59495949
return allocation;
59505950
}
@@ -5984,6 +5984,21 @@ void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
59845984
__asan_poison_memory_region(allocation, newState.Remaining);
59855985
}
59865986

5987+
// NULL should be impossible, but check anyway in case of bugs or corruption
5988+
if (SWIFT_UNLIKELY(!allocation)) {
5989+
PoolRange curStateReRead = AllocationPool.load(std::memory_order_relaxed);
5990+
swift::fatalError(
5991+
0,
5992+
"Metadata allocator corruption: allocation is NULL. "
5993+
"curState: {%p, %zu} - curStateReRead: {%p, %zu} - "
5994+
"newState: {%p, %zu} - allocatedNewPage: %s - requested size: %zu - "
5995+
"sizeWithHeader: %zu - alignment: %zu - Tag: %d\n",
5996+
curState.Begin, curState.Remaining, curStateReRead.Begin,
5997+
curStateReRead.Remaining, newState.Begin, newState.Remaining,
5998+
allocatedNewPage ? "true" : "false", size, sizeWithHeader, alignment,
5999+
Tag);
6000+
}
6001+
59876002
// Swap in the new state.
59886003
if (std::atomic_compare_exchange_weak_explicit(&AllocationPool,
59896004
&curState, newState,
@@ -6024,7 +6039,7 @@ void MetadataAllocator::Deallocate(const void *allocation, size_t size,
60246039
__asan_poison_memory_region(allocation, size);
60256040

60266041
if (size > PoolRange::MaxPoolAllocationSize) {
6027-
free(const_cast<void*>(allocation));
6042+
swift_slowDealloc(const_cast<void *>(allocation), size, Alignment - 1);
60286043
return;
60296044
}
60306045

0 commit comments

Comments
 (0)