[compiler-rt] [scudo] Remove fill when realloc to smaller size. (PR #191321)
Christopher Ferris via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 9 16:16:13 PDT 2026
https://github.com/cferris1000 created https://github.com/llvm/llvm-project/pull/191321
In the reallocate function, when there is a realloc smaller than the current size, the code would attempt to fill in the bytes after the new size. This doesn't really add any extra security and is mostly a waste of time, so skip it.
Remove the test that verifies this functionality.
>From d587cdc4b00e1960f57cd3a127d12222ceb11c56 Mon Sep 17 00:00:00 2001
From: Christopher Ferris <cferris at google.com>
Date: Thu, 9 Apr 2026 16:07:39 -0700
Subject: [PATCH] [scudo] Remove fill when realloc to smaller size.
In the reallocate function, when there is a realloc smaller than
the current size, the code would attempt to fill in the bytes
after the new size. This doesn't really add any extra security
and is mostly a waste of time, so skip it.
Remove the test that verifies this functionality.
---
compiler-rt/lib/scudo/standalone/combined.h | 70 +++++++------------
.../scudo/standalone/tests/combined_test.cpp | 6 --
2 files changed, 27 insertions(+), 49 deletions(-)
diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index c7f3b493a4af8..1c1a92aefd461 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -614,59 +614,40 @@ class Allocator {
void *BlockBegin = getBlockBegin(OldTaggedPtr, &Header);
uptr BlockEnd;
- bool ExactSize = AllocatorConfig::getExactUsableSize() ||
- useMemoryTagging<AllocatorConfig>(Options);
const uptr ClassId = Header.ClassId;
uptr OldSize;
+ uptr UsableSize;
if (LIKELY(ClassId)) {
BlockEnd = reinterpret_cast<uptr>(BlockBegin) +
SizeClassMap::getSizeByClassId(ClassId);
- if (ExactSize)
- OldSize = Header.SizeOrUnusedBytes;
- else
- OldSize = BlockEnd - reinterpret_cast<uptr>(OldTaggedPtr);
+ OldSize = Header.SizeOrUnusedBytes;
+ UsableSize = BlockEnd - reinterpret_cast<uptr>(OldTaggedPtr);
} else {
BlockEnd = SecondaryT::getBlockEnd(BlockBegin);
- OldSize = BlockEnd - reinterpret_cast<uptr>(OldTaggedPtr);
- if (ExactSize)
- OldSize -= Header.SizeOrUnusedBytes;
+ UsableSize = BlockEnd - reinterpret_cast<uptr>(OldTaggedPtr);
+ OldSize = UsableSize - Header.SizeOrUnusedBytes;
}
- // If the new chunk still fits in the previously allocated block (with a
- // reasonable delta), we just keep the old block, and update the chunk
- // header to reflect the size change.
- if (reinterpret_cast<uptr>(OldTaggedPtr) + NewSize <= BlockEnd) {
- if (NewSize > OldSize || (OldSize - NewSize) < getPageSizeCached()) {
- // If we have reduced the size, set the extra bytes to the fill value
- // so that we are ready to grow it again in the future.
- if (NewSize < OldSize) {
- const FillContentsMode FillContents =
- TSDRegistry.getDisableMemInit() ? NoFill
- : Options.getFillContentsMode();
- if (FillContents != NoFill) {
- memset(reinterpret_cast<char *>(OldTaggedPtr) + NewSize,
- FillContents == ZeroFill ? 0 : PatternFillByte,
- OldSize - NewSize);
- }
- }
- Header.SizeOrUnusedBytes =
- (ClassId ? NewSize
- : BlockEnd -
- (reinterpret_cast<uptr>(OldTaggedPtr) + NewSize)) &
- Chunk::SizeOrUnusedBytesMask;
- Chunk::storeHeader(Cookie, OldPtr, &Header);
- if (UNLIKELY(useMemoryTagging<AllocatorConfig>(Options))) {
- if (ClassId) {
- resizeTaggedChunk(reinterpret_cast<uptr>(OldTaggedPtr) + OldSize,
- reinterpret_cast<uptr>(OldTaggedPtr) + NewSize,
- NewSize, untagPointer(BlockEnd));
- storePrimaryAllocationStackMaybe(Options, OldPtr);
- } else {
- storeSecondaryAllocationStackMaybe(Options, OldPtr, NewSize);
- }
+ // If the new chunk fits in the previously allocated block, do nothing
+ // but update the header and return immediately.
+ if (NewSize <= UsableSize) {
+ Header.SizeOrUnusedBytes =
+ (ClassId
+ ? NewSize
+ : BlockEnd - (reinterpret_cast<uptr>(OldTaggedPtr) + NewSize)) &
+ Chunk::SizeOrUnusedBytesMask;
+ Chunk::storeHeader(Cookie, OldPtr, &Header);
+ if (UNLIKELY(useMemoryTagging<AllocatorConfig>(Options))) {
+ if (ClassId) {
+ resizeTaggedChunk(reinterpret_cast<uptr>(OldTaggedPtr) + OldSize,
+ reinterpret_cast<uptr>(OldTaggedPtr) + NewSize,
+ NewSize, untagPointer(BlockEnd));
+ storePrimaryAllocationStackMaybe(Options, OldPtr);
+ } else {
+ storeSecondaryAllocationStackMaybe(Options, OldPtr, NewSize);
}
- return OldTaggedPtr;
}
+ return OldTaggedPtr;
}
// Otherwise we allocate a new one, and deallocate the old one. Some
@@ -675,7 +656,10 @@ class Allocator {
// are currently unclear.
void *NewPtr = allocate(NewSize, Chunk::Origin::Malloc, Alignment);
if (LIKELY(NewPtr)) {
- memcpy(NewPtr, OldTaggedPtr, Min(NewSize, OldSize));
+ bool ExactSize = AllocatorConfig::getExactUsableSize() ||
+ useMemoryTagging<AllocatorConfig>(Options);
+ memcpy(NewPtr, OldTaggedPtr,
+ Min(NewSize, ExactSize ? OldSize : UsableSize));
quarantineOrDeallocateChunk(Options, OldTaggedPtr, &Header, OldSize);
}
return NewPtr;
diff --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
index 268fea81feca7..6a34e8be11151 100644
--- a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
@@ -517,12 +517,6 @@ SCUDO_TYPED_TEST(ScudoCombinedDeathTest, ReallocateSame) {
for (scudo::uptr I = 0; I < scudo::Min(CurrentSize, NewSize); I++)
EXPECT_EQ((reinterpret_cast<char *>(NewP))[I], Marker);
- // Verify that new bytes are set according to FillContentsMode.
- for (scudo::uptr I = CurrentSize; I < NewSize; I++) {
- unsigned char V = (reinterpret_cast<unsigned char *>(NewP))[I];
- EXPECT_TRUE(V == scudo::PatternFillByte || V == 0);
- }
-
checkMemoryTaggingMaybe(Allocator, NewP, NewSize, 0);
CurrentSize = NewSize;
}
More information about the llvm-commits
mailing list