From f3e085da6fe9a0943d9bbea66a19967c81c3b20e Mon Sep 17 00:00:00 2001 From: TCMalloc Team Date: Tue, 1 Dec 2026 20:59:29 -0800 Subject: [PATCH] Fix realloc moving sampled partition-1 allocations into partition 0. Bug: under kFull heap partitioning, realloc() of a sampled allocation from security partition 1 placed the new block in partition 0, so the contents crossed the security-partition boundary. Cause: sampled allocations of security partition 1 are tagged kSampledOrColdP1 (today the only allocations with that tag), but PartitionFromPointer() mapped that tag to partition 0. realloc() always reallocates sampled allocations and uses PartitionFromPointer() to pick the partition of the new block. Fix: map kSampledOrColdP1 to partition 1. partitioning_test now reallocs sampled allocations of both partitions and checks that each new block stays in its original partition. PiperOrigin-RevId: 1034090513 --- tcmalloc/common.h | 4 ++++ tcmalloc/testing/partitioning_test_helper.cc | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tcmalloc/common.h b/tcmalloc/common.h index 1c9aa2db9..4ef8b9ea2 100644 --- a/tcmalloc/common.h +++ b/tcmalloc/common.h @@ -277,6 +277,10 @@ enum class AllocationAccess { switch (GetMemoryTag(ptr)) { case MemoryTag::kNormalP1: return 1; + case MemoryTag::kSampledOrColdP1: + // Only sampled allocations of security partition 1 under kFull heap + // partitioning are tagged kSampledOrColdP1. + return 1; default: return 0; } diff --git a/tcmalloc/testing/partitioning_test_helper.cc b/tcmalloc/testing/partitioning_test_helper.cc index 77d2a0205..ee784ebd6 100644 --- a/tcmalloc/testing/partitioning_test_helper.cc +++ b/tcmalloc/testing/partitioning_test_helper.cc @@ -82,8 +82,16 @@ int main() { absl::PrintF("sampled_0_tag:%s\n", sampled_0_tag); absl::PrintF("sampled_1_tag:%s\n", sampled_1_tag); - free(sampled_0); - free(sampled_1); + // realloc() keeps the security partition of the old pointer. + void* realloc_0 = realloc(sampled_0, 64); + void* realloc_1 = realloc(sampled_1, 64); + absl::PrintF("realloc_sampled_0_tag:%s\n", + MemoryTagToLabel(GetMemoryTag(realloc_0))); + absl::PrintF("realloc_sampled_1_tag:%s\n", + MemoryTagToLabel(GetMemoryTag(realloc_1))); + + free(realloc_0); + free(realloc_1); } return 0;