[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
This commit is contained in:
Kostya Kortchinsky 2021-06-16 10:51:51 -07:00
parent 47f18af55f
commit 8b062b6160
5 changed files with 15 additions and 1 deletions

View File

@ -67,8 +67,8 @@ public:
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)))

View File

@ -59,6 +59,7 @@ public:
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>(

View File

@ -170,8 +170,10 @@ private:
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);

View File

@ -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 @@ 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;

View File

@ -26,10 +26,12 @@ namespace scudo {
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;
}