From 0e987eaa1f4cfdb8c78e366deb57821370d5da39 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 20 Nov 2025 01:43:58 +0000 Subject: [PATCH 1/4] [scudo] Small cleanup of memory tagging code part 2. Make the systemSupportsMemoryTagging() function return even on system that don't support memory tagging. This avoids the need to always check if memory tagging is supported before calling the function. Modify iterateOverChunks() to call useMemoryTagging<>(Options) to determine if mte is supported. This already uses the cached check of systemSupportsMemoryTagging() rather than directly calling that function. Updated the code that calls systemSupportsMemoryTagging(). --- compiler-rt/lib/scudo/standalone/combined.h | 10 ++++------ compiler-rt/lib/scudo/standalone/memtag.h | 4 ++-- compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp | 1 - .../lib/scudo/standalone/tests/secondary_test.cpp | 4 +--- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/combined.h b/compiler-rt/lib/scudo/standalone/combined.h index ffe9554203241..5108f02f2dcbb 100644 --- a/compiler-rt/lib/scudo/standalone/combined.h +++ b/compiler-rt/lib/scudo/standalone/combined.h @@ -171,8 +171,7 @@ class Allocator { Primary.Options.set(OptionBit::DeallocTypeMismatch); if (getFlags()->delete_size_mismatch) Primary.Options.set(OptionBit::DeleteSizeMismatch); - if (allocatorSupportsMemoryTagging() && - systemSupportsMemoryTagging()) + if (systemSupportsMemoryTagging()) Primary.Options.set(OptionBit::UseMemoryTagging); QuarantineMaxChunkSize = @@ -689,16 +688,15 @@ class Allocator { Base = untagPointer(Base); const uptr From = Base; const uptr To = Base + Size; - bool MayHaveTaggedPrimary = - allocatorSupportsMemoryTagging() && - systemSupportsMemoryTagging(); + const Options Options = Primary.Options.load(); + bool MayHaveTaggedPrimary = useMemoryTagging(Options); auto Lambda = [this, From, To, MayHaveTaggedPrimary, Callback, Arg](uptr Block) { if (Block < From || Block >= To) return; uptr Chunk; Chunk::UnpackedHeader Header; - if (MayHaveTaggedPrimary) { + if (UNLIKELY(MayHaveTaggedPrimary)) { // A chunk header can either have a zero tag (tagged primary) or the // header tag (secondary, or untagged primary). We don't know which so // try both. diff --git a/compiler-rt/lib/scudo/standalone/memtag.h b/compiler-rt/lib/scudo/standalone/memtag.h index 83ebe676433eb..a9d04e554767c 100644 --- a/compiler-rt/lib/scudo/standalone/memtag.h +++ b/compiler-rt/lib/scudo/standalone/memtag.h @@ -261,8 +261,8 @@ inline uptr loadTag(uptr Ptr) { #else -inline NORETURN bool systemSupportsMemoryTagging() { - UNREACHABLE("memory tagging not supported"); +inline bool systemSupportsMemoryTagging() { + return false; } inline NORETURN bool systemDetectsMemoryTagFaultsTestOnly() { diff --git a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp index 09093e11452dd..d0d93316f212e 100644 --- a/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/memtag_test.cpp @@ -28,7 +28,6 @@ TEST(MemtagBasicDeathTest, Unsupported) { EXPECT_DEATH(untagPointer((uptr)0), "not supported"); EXPECT_DEATH(extractTag((uptr)0), "not supported"); - EXPECT_DEATH(systemSupportsMemoryTagging(), "not supported"); EXPECT_DEATH(systemDetectsMemoryTagFaultsTestOnly(), "not supported"); EXPECT_DEATH(enableSystemMemoryTaggingTestOnly(), "not supported"); diff --git a/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp b/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp index 855a3e6e6109f..8741c8299b57c 100644 --- a/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/secondary_test.cpp @@ -27,9 +27,7 @@ const scudo::uptr PageSize = scudo::getPageSizeCached(); template static scudo::Options getOptionsForConfig() { - if (!Config::getMaySupportMemoryTagging() || - !scudo::archSupportsMemoryTagging() || - !scudo::systemSupportsMemoryTagging()) + if (!scudo::systemSupportsMemoryTagging()) return {}; scudo::AtomicOptions AO; AO.set(scudo::OptionBit::UseMemoryTagging); From 2e23682d6ee9634390d892b42d74dd108aa64f58 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 20 Nov 2025 01:58:44 +0000 Subject: [PATCH 2/4] Clang format. --- compiler-rt/lib/scudo/standalone/memtag.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/memtag.h b/compiler-rt/lib/scudo/standalone/memtag.h index a9d04e554767c..a798cc9a4bdb0 100644 --- a/compiler-rt/lib/scudo/standalone/memtag.h +++ b/compiler-rt/lib/scudo/standalone/memtag.h @@ -261,9 +261,7 @@ inline uptr loadTag(uptr Ptr) { #else -inline bool systemSupportsMemoryTagging() { - return false; -} +inline bool systemSupportsMemoryTagging() { return false; } inline NORETURN bool systemDetectsMemoryTagFaultsTestOnly() { UNREACHABLE("memory tagging not supported"); From ce548365b399a4746337bdd7da6d389f37bcec6c Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 20 Nov 2025 22:26:46 +0000 Subject: [PATCH 3/4] consexprs added --- compiler-rt/lib/scudo/standalone/memtag.h | 4 ++-- compiler-rt/lib/scudo/standalone/options.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/memtag.h b/compiler-rt/lib/scudo/standalone/memtag.h index a798cc9a4bdb0..073e72c46f68a 100644 --- a/compiler-rt/lib/scudo/standalone/memtag.h +++ b/compiler-rt/lib/scudo/standalone/memtag.h @@ -108,7 +108,7 @@ inline void enableSystemMemoryTaggingTestOnly() { #else // !SCUDO_CAN_USE_MTE -inline bool systemSupportsMemoryTagging() { return false; } +inline constexpr bool systemSupportsMemoryTagging() { return false; } inline NORETURN bool systemDetectsMemoryTagFaultsTestOnly() { UNREACHABLE("memory tagging not supported"); @@ -261,7 +261,7 @@ inline uptr loadTag(uptr Ptr) { #else -inline bool systemSupportsMemoryTagging() { return false; } +inline constexpr bool systemSupportsMemoryTagging() { return false; } inline NORETURN bool systemDetectsMemoryTagFaultsTestOnly() { UNREACHABLE("memory tagging not supported"); diff --git a/compiler-rt/lib/scudo/standalone/options.h b/compiler-rt/lib/scudo/standalone/options.h index b20142a415903..54de1a7add27f 100644 --- a/compiler-rt/lib/scudo/standalone/options.h +++ b/compiler-rt/lib/scudo/standalone/options.h @@ -38,7 +38,8 @@ struct Options { } }; -template bool useMemoryTagging(const Options &Options) { +template +constexpr bool useMemoryTagging(const Options &Options) { return allocatorSupportsMemoryTagging() && Options.get(OptionBit::UseMemoryTagging); } From 34ffed3a0c95eaee8c4b804291f90dccd73da81e Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 20 Nov 2025 22:42:27 +0000 Subject: [PATCH 4/4] Go back to no constexpr. --- compiler-rt/lib/scudo/standalone/options.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/options.h b/compiler-rt/lib/scudo/standalone/options.h index 54de1a7add27f..b20142a415903 100644 --- a/compiler-rt/lib/scudo/standalone/options.h +++ b/compiler-rt/lib/scudo/standalone/options.h @@ -38,8 +38,7 @@ struct Options { } }; -template -constexpr bool useMemoryTagging(const Options &Options) { +template bool useMemoryTagging(const Options &Options) { return allocatorSupportsMemoryTagging() && Options.get(OptionBit::UseMemoryTagging); }