[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
GitOrigin-RevId: 8b062b61606270950645d73b68036c9ab2c7c4bc
diff --git a/primary32.h b/primary32.h
index 36ae083..326c10a 100644
--- a/primary32.h
+++ b/primary32.h
@@ -67,8 +67,8 @@
if (SCUDO_TRUSTY)
reportError("SizeClassAllocator32 is not supported on Trusty");
+ DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
PossibleRegions.init();
-
u32 Seed;
const u64 Time = getMonotonicTime();
if (!getRandom(reinterpret_cast<void *>(&Seed), sizeof(Seed)))
diff --git a/primary64.h b/primary64.h
index 27634c9..13420bf 100644
--- a/primary64.h
+++ b/primary64.h
@@ -59,6 +59,7 @@
static bool canAllocate(uptr Size) { return Size <= SizeClassMap::MaxSize; }
void init(s32 ReleaseToOsInterval) {
+ DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
DCHECK_EQ(PrimaryBase, 0U);
// Reserve the space required for the Primary.
PrimaryBase = reinterpret_cast<uptr>(
diff --git a/quarantine.h b/quarantine.h
index 84eb90c..2d231c3 100644
--- a/quarantine.h
+++ b/quarantine.h
@@ -170,8 +170,10 @@
template <typename Callback, typename Node> class GlobalQuarantine {
public:
typedef QuarantineCache<Callback> CacheT;
+ using ThisT = GlobalQuarantine<Callback, Node>;
void init(uptr Size, uptr CacheSize) {
+ DCHECK(isAligned(reinterpret_cast<uptr>(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/tests/tsd_test.cpp b/tests/tsd_test.cpp
index 35e579d..17387ee 100644
--- a/tests/tsd_test.cpp
+++ b/tests/tsd_test.cpp
@@ -11,6 +11,8 @@
#include "tsd_exclusive.h"
#include "tsd_shared.h"
+#include <stdlib.h>
+
#include <condition_variable>
#include <mutex>
#include <set>
@@ -40,6 +42,13 @@
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/tsd.h b/tsd.h
index e376df0..b400a3b 100644
--- a/tsd.h
+++ b/tsd.h
@@ -26,10 +26,12 @@
template <class Allocator> struct alignas(SCUDO_CACHE_LINE_SIZE) TSD {
typename Allocator::CacheT Cache;
typename Allocator::QuarantineCacheT QuarantineCache;
+ using ThisT = TSD<Allocator>;
u8 DestructorIterations = 0;
void init(Allocator *Instance) {
DCHECK_EQ(DestructorIterations, 0U);
+ DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
Instance->initCache(&Cache);
DestructorIterations = PTHREAD_DESTRUCTOR_ITERATIONS;
}