[compiler-rt] 1d767b1 - [scudo] Align objects with alignas

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed May 5 13:29:30 PDT 2021


Author: Vitaly Buka
Date: 2021-05-05T13:29:21-07:00
New Revision: 1d767b13bfad806bf584e0b054eb7d00a494591d

URL: https://github.com/llvm/llvm-project/commit/1d767b13bfad806bf584e0b054eb7d00a494591d
DIFF: https://github.com/llvm/llvm-project/commit/1d767b13bfad806bf584e0b054eb7d00a494591d.diff

LOG: [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

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
    compiler-rt/lib/scudo/standalone/tests/primary_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
index 5c5f646538bbb..5db249d0a85ad 100644
--- a/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/combined_test.cpp
@@ -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 {

diff  --git a/compiler-rt/lib/scudo/standalone/tests/primary_test.cpp b/compiler-rt/lib/scudo/standalone/tests/primary_test.cpp
index d90b6a34c0bcf..e7aa6f795b66c 100644
--- a/compiler-rt/lib/scudo/standalone/tests/primary_test.cpp
+++ b/compiler-rt/lib/scudo/standalone/tests/primary_test.cpp
@@ -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 {};


        


More information about the llvm-commits mailing list