From 8b062b61606270950645d73b68036c9ab2c7c4bc Mon Sep 17 00:00:00 2001 From: Kostya Kortchinsky Date: Wed, 16 Jun 2021 10:51:51 -0700 Subject: [PATCH] [scudo] Ensure proper allocator alignment in TSD test The `MockAllocator` used in `ScudoTSDTest` wasn't allocated properly aligned, which resulted in the `TSDs` of the shared registry not being aligned either. This lead to some failures like: https://reviews.llvm.org/D103119#2822008 This changes how the `MockAllocator` is allocated, same as Vitaly did in the combined tests, properly aligning it, which results in the `TSDs` being aligned as well. Add a `DCHECK` in the shared registry to check that it is. Differential Revision: https://reviews.llvm.org/D104402 --- compiler-rt/lib/scudo/standalone/primary32.h | 2 +- compiler-rt/lib/scudo/standalone/primary64.h | 1 + compiler-rt/lib/scudo/standalone/quarantine.h | 2 ++ compiler-rt/lib/scudo/standalone/tests/tsd_test.cpp | 9 +++++++++ compiler-rt/lib/scudo/standalone/tsd.h | 2 ++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/scudo/standalone/primary32.h b/compiler-rt/lib/scudo/standalone/primary32.h index 36ae083cfc73..326c10a32a85 100644 --- a/compiler-rt/lib/scudo/standalone/primary32.h +++ b/compiler-rt/lib/scudo/standalone/primary32.h @@ -67,8 +67,8 @@ public: if (SCUDO_TRUSTY) reportError("SizeClassAllocator32 is not supported on Trusty"); + DCHECK(isAligned(reinterpret_cast(this), alignof(ThisT))); PossibleRegions.init(); - u32 Seed; const u64 Time = getMonotonicTime(); if (!getRandom(reinterpret_cast(&Seed), sizeof(Seed))) diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h index 27634c96c71e..13420bf3d222 100644 --- a/compiler-rt/lib/scudo/standalone/primary64.h +++ b/compiler-rt/lib/scudo/standalone/primary64.h @@ -59,6 +59,7 @@ public: static bool canAllocate(uptr Size) { return Size <= SizeClassMap::MaxSize; } void init(s32 ReleaseToOsInterval) { + DCHECK(isAligned(reinterpret_cast(this), alignof(ThisT))); DCHECK_EQ(PrimaryBase, 0U); // Reserve the space required for the Primary. PrimaryBase = reinterpret_cast( diff --git a/compiler-rt/lib/scudo/standalone/quarantine.h b/compiler-rt/lib/scudo/standalone/quarantine.h index 84eb90cc0b3f..2d231c3a28db 100644 --- a/compiler-rt/lib/scudo/standalone/quarantine.h +++ b/compiler-rt/lib/scudo/standalone/quarantine.h @@ -170,8 +170,10 @@ private: template class GlobalQuarantine { public: typedef QuarantineCache CacheT; + using ThisT = GlobalQuarantine; void init(uptr Size, uptr CacheSize) { + DCHECK(isAligned(reinterpret_cast(this), alignof(ThisT))); DCHECK_EQ(atomic_load_relaxed(&MaxSize), 0U); DCHECK_EQ(atomic_load_relaxed(&MinSize), 0U); DCHECK_EQ(atomic_load_relaxed(&MaxCacheSize), 0U); diff --git a/compiler-rt/lib/scudo/standalone/tests/tsd_test.cpp b/compiler-rt/lib/scudo/standalone/tests/tsd_test.cpp index 35e579d5dc58..17387ee7c570 100644 --- a/compiler-rt/lib/scudo/standalone/tests/tsd_test.cpp +++ b/compiler-rt/lib/scudo/standalone/tests/tsd_test.cpp @@ -11,6 +11,8 @@ #include "tsd_exclusive.h" #include "tsd_shared.h" +#include + #include #include #include @@ -40,6 +42,13 @@ public: bool isInitialized() { return Initialized; } + void *operator new(size_t Size) { + void *P = nullptr; + EXPECT_EQ(0, posix_memalign(&P, alignof(ThisT), Size)); + return P; + } + void operator delete(void *P) { free(P); } + private: bool Initialized = false; TSDRegistryT TSDRegistry; diff --git a/compiler-rt/lib/scudo/standalone/tsd.h b/compiler-rt/lib/scudo/standalone/tsd.h index e376df05af5e..b400a3b56da9 100644 --- a/compiler-rt/lib/scudo/standalone/tsd.h +++ b/compiler-rt/lib/scudo/standalone/tsd.h @@ -26,10 +26,12 @@ namespace scudo { template struct alignas(SCUDO_CACHE_LINE_SIZE) TSD { typename Allocator::CacheT Cache; typename Allocator::QuarantineCacheT QuarantineCache; + using ThisT = TSD; u8 DestructorIterations = 0; void init(Allocator *Instance) { DCHECK_EQ(DestructorIterations, 0U); + DCHECK(isAligned(reinterpret_cast(this), alignof(ThisT))); Instance->initCache(&Cache); DestructorIterations = PTHREAD_DESTRUCTOR_ITERATIONS; }