From 146f4c26ac5097c4cc145ce3a6f700c3748affd1 Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Wed, 7 Jun 2023 00:15:02 +0000 Subject: [PATCH] [scudo] Enable MTE in Trusty Trusty now has MTE support. Back-ported from https://r.android.com/2332745. Reviewed By: Chia-hungDuan Differential Revision: https://reviews.llvm.org/D152219 --- compiler-rt/lib/scudo/standalone/allocator_config.h | 2 +- compiler-rt/lib/scudo/standalone/memtag.h | 10 +++++----- compiler-rt/lib/scudo/standalone/platform.h | 4 ++++ compiler-rt/lib/scudo/standalone/trusty.cpp | 3 +++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/allocator_config.h b/compiler-rt/lib/scudo/standalone/allocator_config.h index 1150157..315a04f 100644 --- a/compiler-rt/lib/scudo/standalone/allocator_config.h +++ b/compiler-rt/lib/scudo/standalone/allocator_config.h @@ -271,7 +271,7 @@ struct FuchsiaConfig { }; struct TrustyConfig { - static const bool MaySupportMemoryTagging = false; + static const bool MaySupportMemoryTagging = true; template using TSDRegistryT = TSDRegistrySharedT; // Shared, max 1 TSD. diff --git a/compiler-rt/lib/scudo/standalone/memtag.h b/compiler-rt/lib/scudo/standalone/memtag.h index 7f14a30..aaed219 100644 --- a/compiler-rt/lib/scudo/standalone/memtag.h +++ b/compiler-rt/lib/scudo/standalone/memtag.h @@ -11,7 +11,7 @@ #include "internal_defs.h" -#if SCUDO_LINUX +#if SCUDO_CAN_USE_MTE #include #include #endif @@ -25,7 +25,7 @@ namespace scudo { // tagging. Not all operating systems enable TBI, so we only claim architectural // support for memory tagging if the operating system enables TBI. // HWASan uses the top byte for its own purpose and Scudo should not touch it. -#if SCUDO_LINUX && !defined(SCUDO_DISABLE_TBI) && \ +#if SCUDO_CAN_USE_MTE && !defined(SCUDO_DISABLE_TBI) && \ !__has_feature(hwaddress_sanitizer) inline constexpr bool archSupportsMemoryTagging() { return true; } #else @@ -60,7 +60,7 @@ inline NORETURN uint8_t extractTag(uptr Ptr) { #if __clang_major__ >= 12 && defined(__aarch64__) && !defined(__ILP32__) -#if SCUDO_LINUX +#if SCUDO_CAN_USE_MTE inline bool systemSupportsMemoryTagging() { #ifndef HWCAP2_MTE @@ -106,7 +106,7 @@ inline void enableSystemMemoryTaggingTestOnly() { 0, 0, 0); } -#else // !SCUDO_LINUX +#else // !SCUDO_CAN_USE_MTE inline bool systemSupportsMemoryTagging() { return false; } @@ -118,7 +118,7 @@ inline NORETURN void enableSystemMemoryTaggingTestOnly() { UNREACHABLE("memory tagging not supported"); } -#endif // SCUDO_LINUX +#endif // SCUDO_CAN_USE_MTE class ScopedDisableMemoryTagChecks { uptr PrevTCO; diff --git a/compiler-rt/lib/scudo/standalone/platform.h b/compiler-rt/lib/scudo/standalone/platform.h index aae3b9a..7c7024f 100644 --- a/compiler-rt/lib/scudo/standalone/platform.h +++ b/compiler-rt/lib/scudo/standalone/platform.h @@ -59,6 +59,10 @@ #define SCUDO_CAN_USE_PRIMARY64 (SCUDO_WORDSIZE == 64U) #endif +#ifndef SCUDO_CAN_USE_MTE +#define SCUDO_CAN_USE_MTE (SCUDO_LINUX || SCUDO_TRUSTY) +#endif + #ifndef SCUDO_MIN_ALIGNMENT_LOG // We force malloc-type functions to be aligned to std::max_align_t, but there // is no reason why the minimum alignment for all other functions can't be 8 diff --git a/compiler-rt/lib/scudo/standalone/trusty.cpp b/compiler-rt/lib/scudo/standalone/trusty.cpp index 8d73eeb..3191091 100644 --- a/compiler-rt/lib/scudo/standalone/trusty.cpp +++ b/compiler-rt/lib/scudo/standalone/trusty.cpp @@ -43,6 +43,9 @@ void *map(void *Addr, uptr Size, const char *Name, uptr Flags, if (Addr) MmapFlags |= MMAP_FLAG_FIXED_NOREPLACE; + if (Flags & MAP_MEMTAG) + MmapFlags |= MMAP_FLAG_PROT_MTE; + void *P = (void *)_trusty_mmap(Addr, Size, MmapFlags, 0); if (IS_ERR(P)) { -- 2.7.4