[compiler-rt] [scudo] Apply filling option when realloc grows a block in-place too (PR #93212)
Fabio D'Urso via llvm-commits
llvm-commits at lists.llvm.org
Mon May 27 05:12:27 PDT 2024
================
@@ -374,7 +384,18 @@ SCUDO_TYPED_TEST(ScudoCombinedTest, PatternOrZeroFill) {
else
ASSERT_TRUE(V == scudo::PatternFillByte || V == 0);
}
+
+ // Fill with a known pattern different from PatternFillByte.
memset(P, 0xaa, Size);
+
+ // Shrink and then grow by one byte, verifying that it gets re-filled in
+ // the process. We assume that changing the size by just 1 is done in
+ // place.
+ ASSERT_EQ(Allocator->reallocate(P, Size - 1), P);
+ ASSERT_EQ(Allocator->reallocate(P, Size), P);
+ EXPECT_EQ((reinterpret_cast<unsigned char *>(P))[Size - 1],
+ scudo::PatternFillByte);
+
----------------
fabio-d wrote:
I'm seeing that there's already a similar assumption in `ScudoCombinedDeathTest::ReallocateSame`, so another possibility could maybe be to extend that test instead of having to maintain a new config. What about this?
```c++
SCUDO_TYPED_TEST(ScudoCombinedDeathTest, ReallocateSame) {
auto *Allocator = this->Allocator.get();
// Check that reallocating a chunk to a slightly smaller or larger size
// 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 =
TypeParam::Primary::SizeClassMap::MaxSize - 64;
void *P = Allocator->allocate(ReallocSize, Origin);
const char Marker = 'A';
memset(P, Marker, ReallocSize);
+ Allocator->setFillContents(scudo::PatternOrZeroFill);
for (scudo::sptr Delta = -32; Delta < 32; Delta += 8) {
const scudo::uptr NewSize =
static_cast<scudo::uptr>(static_cast<scudo::sptr>(ReallocSize) + Delta);
void *NewP = Allocator->reallocate(P, NewSize);
EXPECT_EQ(NewP, P);
+ // Verify that existing contents have been preserved.
for (scudo::uptr I = 0; I < ReallocSize - 32; I++)
EXPECT_EQ((reinterpret_cast<char *>(NewP))[I], Marker);
+
+ // Verify that, if we have grown the allocation, new bytes have been set
+ // according to FillContentsMode.
+ for (scudo::uptr I = ReallocSize - 32; I < NewSize; I++)
+ EXPECT_EQ((reinterpret_cast<unsigned char *>(NewP))[I],
+ scudo::PatternFillByte);
checkMemoryTaggingMaybe(Allocator, NewP, NewSize, 0);
}
Allocator->deallocate(P, Origin);
}
```
https://github.com/llvm/llvm-project/pull/93212
More information about the llvm-commits
mailing list