diff --git a/compiler-rt/lib/scudo/standalone/memtag.h b/compiler-rt/lib/scudo/standalone/memtag.h --- a/compiler-rt/lib/scudo/standalone/memtag.h +++ b/compiler-rt/lib/scudo/standalone/memtag.h @@ -67,28 +67,56 @@ } inline bool systemDetectsMemoryTagFaultsTestOnly() { +#ifndef PR_SET_TAGGED_ADDR_CTRL +#define PR_SET_TAGGED_ADDR_CTRL 54 +#endif #ifndef PR_GET_TAGGED_ADDR_CTRL #define PR_GET_TAGGED_ADDR_CTRL 56 #endif #ifndef PR_MTE_TCF_SHIFT #define PR_MTE_TCF_SHIFT 1 #endif +#ifndef PR_MTE_TAG_SHIFT +#define PR_MTE_TAG_SHIFT 3 +#endif #ifndef PR_MTE_TCF_NONE #define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) #endif +#ifndef PR_MTE_TCF_SYNC +#define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) +#endif #ifndef PR_MTE_TCF_MASK #define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT) #endif - return (static_cast( - prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0)) & - PR_MTE_TCF_MASK) != PR_MTE_TCF_NONE; -} +#ifndef PR_TAGGED_ADDR_ENABLE +#define PR_TAGGED_ADDR_ENABLE (1UL << 0) +#endif + + inline bool systemDetectsMemoryTagFaultsTestOnly() { + return (static_cast( + prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0)) & + PR_MTE_TCF_MASK) != PR_MTE_TCF_NONE; + } + + inline void setSystemDetectsMemoryTagFaultsTestOnly(bool Enabled) { + prctl(PR_SET_TAGGED_ADDR_CTRL, + PR_TAGGED_ADDR_ENABLE | + (Enabled ? PR_MTE_TCF_SYNC : PR_MTE_TCF_NONE) | + (0xffff << PR_MTE_TAG_SHIFT), + 0, 0, 0); + } #else // !SCUDO_LINUX inline bool systemSupportsMemoryTagging() { return false; } -inline bool systemDetectsMemoryTagFaultsTestOnly() { return false; } +inline bool systemDetectsMemoryTagFaultsTestOnly() { + UNREACHABLE("memory tagging not supported"); +} + +inline void setSystemDetectsMemoryTagFaultsTestOnly(bool Enabled) { + UNREACHABLE("memory tagging not supported"); +} #endif // SCUDO_LINUX @@ -251,6 +279,10 @@ UNREACHABLE("memory tagging not supported"); } +inline void setSystemDetectsMemoryTagFaultsTestOnly(bool Enabled) { + UNREACHABLE("memory tagging not supported"); +} + inline void disableMemoryTagChecksTestOnly() { UNREACHABLE("memory tagging not supported"); } diff --git a/compiler-rt/lib/scudo/standalone/tests/scudo_unit_test_main.cpp b/compiler-rt/lib/scudo/standalone/tests/scudo_unit_test_main.cpp --- a/compiler-rt/lib/scudo/standalone/tests/scudo_unit_test_main.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/scudo_unit_test_main.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "memtag.h" #include "tests/scudo_unit_test.h" // Match Android's default configuration, which disables Scudo's mismatch @@ -33,6 +34,8 @@ // for Fuchsia builds. #if !SCUDO_FUCHSIA int main(int argc, char **argv) { + if (scudo::archSupportsMemoryTagging()) + scudo::setSystemDetectsMemoryTagFaultsTestOnly(true); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }