[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
Thu May 23 09:33:13 PDT 2024


https://github.com/fabio-d created https://github.com/llvm/llvm-project/pull/93212

None

>From 1e55c8156bae97d6847865459141df8488a0953b Mon Sep 17 00:00:00 2001
From: Fabio D'Urso <fdurso at google.com>
Date: Thu, 23 May 2024 17:43:00 +0200
Subject: [PATCH] [scudo] Apply filling option when realloc grows a block
 in-place too

---
 compiler-rt/lib/scudo/standalone/combined.h   | 13 ++++++++++++
 .../scudo/standalone/tests/combined_test.cpp  | 21 +++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h
index 15a199ae0349b..60c7fae1d57d2 100644
--- a/compiler-rt/lib/scudo/standalone/combined.h
+++ b/compiler-rt/lib/scudo/standalone/combined.h
@@ -565,6 +565,19 @@ class Allocator {
             storeSecondaryAllocationStackMaybe(Options, OldPtr, NewSize);
           }
         }
+
+        // If we've increased the size, fill the extra bytes.
+        if (NewSize > OldSize) {
+          const FillContentsMode FillContents =
+              TSDRegistry.getDisableMemInit() ? NoFill
+                                              : Options.getFillContentsMode();
+          if (FillContents != NoFill) {
+            memset(reinterpret_cast<char *>(OldTaggedPtr) + OldSize,
+                   FillContents == ZeroFill ? 0 : PatternFillByte,
+                   NewSize - OldSize);
+          }
+        }
+
         return OldTaggedPtr;
       }
     }
diff --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
index 1a36155bcd423..4af0d44493b2a 100644
--- a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
@@ -347,7 +347,17 @@ SCUDO_TYPED_TEST(ScudoCombinedTest, ZeroFill) {
       EXPECT_NE(P, nullptr);
       for (scudo::uptr I = 0; I < Size; I++)
         ASSERT_EQ((reinterpret_cast<char *>(P))[I], '\0');
+
+      // Fill with a non-zero pattern.
       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], '\0');
+
       Allocator->deallocate(P, Origin, Size);
     }
   }
@@ -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);
+
       Allocator->deallocate(P, Origin, Size);
     }
   }



More information about the llvm-commits mailing list