Skip to content

[scudo] Apply filling option when realloc grows a block in-place too #93212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler-rt/lib/scudo/standalone/combined.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,20 @@ class Allocator {
storeSecondaryAllocationStackMaybe(Options, OldPtr, NewSize);
}
}

// 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);
}
}

return OldTaggedPtr;
}
}
Expand Down
23 changes: 18 additions & 5 deletions compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,32 @@ SCUDO_TYPED_TEST(ScudoCombinedDeathTest, ReallocateSame) {
// returns the same chunk. This requires that all the sizes we iterate on use
// the same block size, but that should be the case for MaxSize - 64 with our
// default class size maps.
constexpr scudo::uptr ReallocSize =
constexpr scudo::uptr InitialSize =
TypeParam::Primary::SizeClassMap::MaxSize - 64;
void *P = Allocator->allocate(ReallocSize, Origin);
const char Marker = 'A';
memset(P, Marker, ReallocSize);
Allocator->setFillContents(scudo::PatternOrZeroFill);

void *P = Allocator->allocate(InitialSize, Origin);
scudo::uptr CurrentSize = InitialSize;
for (scudo::sptr Delta = -32; Delta < 32; Delta += 8) {
memset(P, Marker, CurrentSize);
const scudo::uptr NewSize =
static_cast<scudo::uptr>(static_cast<scudo::sptr>(ReallocSize) + Delta);
static_cast<scudo::uptr>(static_cast<scudo::sptr>(InitialSize) + Delta);
void *NewP = Allocator->reallocate(P, NewSize);
EXPECT_EQ(NewP, P);
for (scudo::uptr I = 0; I < ReallocSize - 32; I++)

// Verify that existing contents have been preserved.
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++) {
EXPECT_EQ((reinterpret_cast<unsigned char *>(NewP))[I],
scudo::PatternFillByte);
}

Comment on lines +468 to +473
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we only want to include this when it doesn't have GWP_ASAN_HOOKS defined.

checkMemoryTaggingMaybe(Allocator, NewP, NewSize, 0);
CurrentSize = NewSize;
}
Allocator->deallocate(P, Origin);
}
Expand Down
Loading