[scudo] Align objects with alignas

Operator new must align allocations for types with large alignment.

Before c++17 behavior was implementation defined and both clang and gc++
before 11 ignored alignment. Miss-aligned objects mysteriously crashed
tests on Ubuntu 14.

Alternatives are compile with -std=c++17 or -faligned-new, but they were
discarded as less portable.

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D101874
This commit is contained in:
Vitaly Buka 2021-05-04 16:34:59 -07:00
parent 9ea2db2c51
commit 1d767b13bf
2 changed files with 18 additions and 0 deletions

View File

@ -15,6 +15,7 @@
#include <memory>
#include <mutex>
#include <set>
#include <stdlib.h>
#include <thread>
#include <vector>
@ -74,6 +75,14 @@ template <typename Config> struct TestAllocator : scudo::Allocator<Config> {
this->disableMemoryTagging();
}
~TestAllocator() { this->unmapTestOnly(); }
void *operator new(size_t size) {
void *p = nullptr;
EXPECT_EQ(0, posix_memalign(&p, alignof(TestAllocator), size));
return p;
}
void operator delete(void *ptr) { free(ptr); }
};
template <class TypeParam> struct ScudoCombinedTest : public Test {

View File

@ -14,6 +14,7 @@
#include <condition_variable>
#include <mutex>
#include <stdlib.h>
#include <thread>
#include <vector>
@ -63,6 +64,14 @@ struct SizeClassAllocator<TestConfig1, SizeClassMapT>
template <typename BaseConfig, typename SizeClassMapT>
struct TestAllocator : public SizeClassAllocator<BaseConfig, SizeClassMapT> {
~TestAllocator() { this->unmapTestOnly(); }
void *operator new(size_t size) {
void *p = nullptr;
EXPECT_EQ(0, posix_memalign(&p, alignof(TestAllocator), size));
return p;
}
void operator delete(void *ptr) { free(ptr); }
};
template <class BaseConfig> struct ScudoPrimaryTest : public Test {};