[scudo] Fix reallocate for MTE. (#190086)

For MTE, we can't use the whole size or we might trigger a segfault.
Therefore, use the exact size when MTE is enabled or the exact usable
size parameter is true.

Also, optimize out the call to getUsableSize and use a simpler
calculation.
This commit is contained in:
Christopher Ferris 2026-04-01 23:44:31 +00:00 committed by GitHub
parent 29391328ab
commit 7c260d3966
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -614,13 +614,22 @@ public:
void *BlockBegin = getBlockBegin(OldTaggedPtr, &Header);
uptr BlockEnd;
uptr OldSize = getUsableSize(OldTaggedPtr, &Header);
bool ExactSize = AllocatorConfig::getExactUsableSize() ||
useMemoryTagging<AllocatorConfig>(Options);
const uptr ClassId = Header.ClassId;
uptr OldSize;
if (LIKELY(ClassId)) {
BlockEnd = reinterpret_cast<uptr>(BlockBegin) +
SizeClassMap::getSizeByClassId(ClassId);
if (ExactSize)
OldSize = Header.SizeOrUnusedBytes;
else
OldSize = BlockEnd - reinterpret_cast<uptr>(OldTaggedPtr);
} else {
BlockEnd = SecondaryT::getBlockEnd(BlockBegin);
OldSize = BlockEnd - reinterpret_cast<uptr>(OldTaggedPtr);
if (ExactSize)
OldSize -= 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