[libcxx-commits] [libcxx] 6596778 - [libcxx] [test] Fix mismatches between aligned operator new and std::free

Martin Storsjö via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 15 13:38:43 PDT 2021


Author: Martin Storsjö
Date: 2021-07-15T23:37:56+03:00
New Revision: 6596778b46ba69517191e7397289228168064ff4

URL: https://github.com/llvm/llvm-project/commit/6596778b46ba69517191e7397289228168064ff4
DIFF: https://github.com/llvm/llvm-project/commit/6596778b46ba69517191e7397289228168064ff4.diff

LOG: [libcxx] [test] Fix mismatches between aligned operator new and std::free

The XFAIL comments about VCRuntime not providing aligned operator new
are outdated; these days VCRuntime does provide them.

However, the tests used to fail on Windows, as the pointers allocated
with an aligned operator new (which is implemented with _aligned_malloc
on Windows) can't be freed using std::free() on Windows (but they need
to be freed with the corresponding function _aligned_free instead).

Instead override the aligned operator new to return a dummy suitably
aligned pointer instead, like other tests that override aligned operator
new.

Also override `operator delete[]` instead of plain `operator delete`
in the array testcase; the fallback from `operator delete[]` to
user defined `operator delete` doesn't work in all DLL build
configurations on Windows.

Also expand the TEST_NOEXCEPT macros, as these tests only are built
in C++17 mode.

By providing the aligned operator new within the tests, this also makes
these test cases pass when testing back deployment on macOS 10.9.

Differential Revision: https://reviews.llvm.org/D105962

Added: 
    

Modified: 
    libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/delete_align_val_t_replace.pass.cpp
    libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/delete_align_val_t_replace.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/delete_align_val_t_replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/delete_align_val_t_replace.pass.cpp
index 875e20c7fdb0f..00f2b444fc095 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/delete_align_val_t_replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/delete_align_val_t_replace.pass.cpp
@@ -18,12 +18,6 @@
 // However, support for that was broken prior to Clang 8 and AppleClang 11.
 // UNSUPPORTED: apple-clang-9, apple-clang-10
 // UNSUPPORTED: clang-5, clang-6, clang-7
-// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13}}
-
-// On Windows libc++ doesn't provide its own definitions for new/delete
-// but instead depends on the ones in VCRuntime. However VCRuntime does not
-// yet provide aligned new/delete definitions so this test fails to compile/link.
-// XFAIL: LIBCXX-WINDOWS-FIXME
 
 // Libcxx when built for z/OS doesn't contain the aligned allocation functions,
 // nor does the dynamic library shipped with z/OS.
@@ -48,22 +42,29 @@ void reset() {
     aligned_delete_called = 0;
 }
 
-void operator delete(void* p) TEST_NOEXCEPT
+alignas(OverAligned) char DummyData[OverAligned * 4];
+
+void* operator new [] (std::size_t s, std::align_val_t)
+{
+    assert(s <= sizeof(DummyData));
+    return DummyData;
+}
+
+void operator delete [] (void* p) noexcept
 {
     ++unsized_delete_called;
     std::free(p);
 }
 
-void operator delete(void* p, const std::nothrow_t&) TEST_NOEXCEPT
+void operator delete [] (void* p, const std::nothrow_t&) noexcept
 {
     ++unsized_delete_nothrow_called;
     std::free(p);
 }
 
-void operator delete [] (void* p, std::align_val_t) TEST_NOEXCEPT
+void operator delete [] (void*, std::align_val_t) noexcept
 {
     ++aligned_delete_called;
-    std::free(p);
 }
 
 struct alignas(OverAligned) A {};

diff  --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/delete_align_val_t_replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/delete_align_val_t_replace.pass.cpp
index 740ba21a430ff..3be61283094b7 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/delete_align_val_t_replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/delete_align_val_t_replace.pass.cpp
@@ -18,12 +18,6 @@
 // However, support for that was broken prior to Clang 8 and AppleClang 11.
 // UNSUPPORTED: apple-clang-9, apple-clang-10
 // UNSUPPORTED: clang-5, clang-6, clang-7
-// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13}}
-
-// On Windows libc++ doesn't provide its own definitions for new/delete
-// but instead depends on the ones in VCRuntime. However VCRuntime does not
-// yet provide aligned new/delete definitions so this test fails to compile/link.
-// XFAIL: LIBCXX-WINDOWS-FIXME
 
 // Libcxx when built for z/OS doesn't contain the aligned allocation functions,
 // nor does the dynamic library shipped with z/OS.
@@ -48,22 +42,29 @@ void reset() {
     aligned_delete_called = 0;
 }
 
-void operator delete(void* p) TEST_NOEXCEPT
+alignas(OverAligned) char DummyData[OverAligned * 4];
+
+void* operator new (std::size_t s, std::align_val_t)
+{
+    assert(s <= sizeof(DummyData));
+    return DummyData;
+}
+
+void operator delete(void* p) noexcept
 {
     ++unsized_delete_called;
     std::free(p);
 }
 
-void operator delete(void* p, const std::nothrow_t&) TEST_NOEXCEPT
+void operator delete(void* p, const std::nothrow_t&) noexcept
 {
     ++unsized_delete_nothrow_called;
     std::free(p);
 }
 
-void operator delete(void* p, std::align_val_t) TEST_NOEXCEPT
+void operator delete(void*, std::align_val_t) noexcept
 {
     ++aligned_delete_called;
-    std::free(p);
 }
 
 struct alignas(OverAligned) A {};


        


More information about the libcxx-commits mailing list