[libcxx-commits] [libcxx] [libc++] Fix missing 'get_new_handler()' for Windows builds (PR #150182)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Apr 18 04:00:46 PDT 2026
https://github.com/siradam7th updated https://github.com/llvm/llvm-project/pull/150182
>From b65138980625df070b536793daea36080c2b058f Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Wed, 23 Jul 2025 08:28:44 +0100
Subject: [PATCH 1/5] fix missing 'get_new_handler()' for Windows builds
---
libcxx/include/__new/new_handler.h | 4 ----
libcxx/src/new_handler.cpp | 4 +---
2 files changed, 1 insertion(+), 7 deletions(-)
diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
index 05f4e846c3ef9..8f93578da9f9c 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -15,14 +15,10 @@
# pragma GCC system_header
#endif
-#if defined(_LIBCPP_ABI_VCRUNTIME)
-# include <new.h>
-#else
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
typedef void (*new_handler)();
_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
-#endif // _LIBCPP_ABI_VCRUNTIME
#endif // _LIBCPP___NEW_NEW_HANDLER_H
diff --git a/libcxx/src/new_handler.cpp b/libcxx/src/new_handler.cpp
index 49c21d85f5908..5124cb501c938 100644
--- a/libcxx/src/new_handler.cpp
+++ b/libcxx/src/new_handler.cpp
@@ -11,9 +11,7 @@
#include "include/atomic_support.h"
#if defined(_LIBCPP_ABI_MICROSOFT)
-# if !defined(_LIBCPP_ABI_VCRUNTIME)
-# define _LIBPCPP_DEFINE_NEW_HANDLER
-# endif
+# define _LIBPCPP_DEFINE_NEW_HANDLER
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
// nothing to do, we use the one from libc++abi
#elif defined(LIBCXXRT)
>From b4cad9c7fadaa8e2cb082b8b224fa6480009c8b7 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Wed, 23 Jul 2025 08:56:05 +0100
Subject: [PATCH 2/5] remove 'FIXME' and 'XFAIL' for 'get_new_handler()' test
---
.../alloc.errors/set.new.handler/get_new_handler.pass.cpp | 7 -------
1 file changed, 7 deletions(-)
diff --git a/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp b/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
index c5725bde6e6c3..d20a65d8b6b48 100644
--- a/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
@@ -8,13 +8,6 @@
// test get_new_handler
-// FIXME: When libc++ is linked against vcruntime (i.e. the default config in
-// MSVC mode), the declarations of std::set_new_handler and std::get_new_handler
-// are provided by vcruntime/UCRT's new.h. However, that header actually only
-// declares set_new_handler - it's missing a declaration of get_new_handler.
-
-// XFAIL: msvc && stdlib=libc++
-
#include <new>
#include <cassert>
>From 7fa18fc793d08ee6a407b0ec75c1158f331c0bd1 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Sat, 13 Dec 2025 07:44:36 +0100
Subject: [PATCH 3/5] implement 'set_new_handler' and 'get_new_handler' for
Windows
---
libcxx/include/__new/global_new_delete.h | 2 +-
libcxx/include/__new/nothrow_t.h | 2 +-
libcxx/include/__new/placement_new_delete.h | 2 +-
libcxx/src/new_handler.cpp | 28 ++++++++++++++++++---
4 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__new/global_new_delete.h b/libcxx/include/__new/global_new_delete.h
index f31bac3730489..4e25295aa20fa 100644
--- a/libcxx/include/__new/global_new_delete.h
+++ b/libcxx/include/__new/global_new_delete.h
@@ -25,7 +25,7 @@
#endif
#if defined(_LIBCPP_ABI_VCRUNTIME)
-# include <new.h>
+# include <vcruntime_new.h>
#else
[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
diff --git a/libcxx/include/__new/nothrow_t.h b/libcxx/include/__new/nothrow_t.h
index a099772123934..7d9e73648fad2 100644
--- a/libcxx/include/__new/nothrow_t.h
+++ b/libcxx/include/__new/nothrow_t.h
@@ -16,7 +16,7 @@
#endif
#if defined(_LIBCPP_ABI_VCRUNTIME)
-# include <new.h>
+# include <vcruntime_new.h>
#else
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
struct nothrow_t {
diff --git a/libcxx/include/__new/placement_new_delete.h b/libcxx/include/__new/placement_new_delete.h
index 74692f36fd04c..041651a2a6ef9 100644
--- a/libcxx/include/__new/placement_new_delete.h
+++ b/libcxx/include/__new/placement_new_delete.h
@@ -17,7 +17,7 @@
#endif
#if defined(_LIBCPP_ABI_VCRUNTIME)
-# include <new.h>
+# include <vcruntime_new.h>
#else
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void*
operator new(std::size_t, void* __p) _NOEXCEPT {
diff --git a/libcxx/src/new_handler.cpp b/libcxx/src/new_handler.cpp
index 5124cb501c938..aeab6946a0ae3 100644
--- a/libcxx/src/new_handler.cpp
+++ b/libcxx/src/new_handler.cpp
@@ -24,11 +24,33 @@
#if defined(_LIBPCPP_DEFINE_NEW_HANDLER)
-namespace std { // purposefully not versioned
-
static constinit std::new_handler __new_handler = nullptr;
-new_handler set_new_handler(new_handler handler) noexcept { return __libcpp_atomic_exchange(&__new_handler, handler); }
+# ifdef _LIBCPP_ABI_VCRUNTIME
+// to avoid including <new.h>
+using _new_h = int(__cdecl*)(size_t);
+extern "C" _new_h __cdecl _set_new_handler(_new_h);
+
+namespace {
+// adapter for _callnewh
+int __cdecl _new_handler_adapter(size_t) {
+ std::__libcpp_atomic_load (&__new_handler)();
+ return 1;
+}
+} // namespace
+# endif
+
+namespace std { // purposefully not versioned
+
+new_handler set_new_handler(new_handler handler) noexcept {
+# ifdef _LIBCPP_ABI_VCRUNTIME
+ auto old = __libcpp_atomic_exchange(&__new_handler, handler);
+ _set_new_handler(handler ? _new_handler_adapter : nullptr);
+ return old;
+# else
+ return __libcpp_atomic_exchange(&__new_handler, handler);
+# endif
+}
new_handler get_new_handler() noexcept { return __libcpp_atomic_load(&__new_handler); }
>From 033018721296e35ba5b46fa8778cfe1b26103df4 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Sat, 13 Dec 2025 16:21:42 +0100
Subject: [PATCH 4/5] replace 'typedef' with a type alias
---
libcxx/include/__new/new_handler.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
index 8f93578da9f9c..c0c6fe49596a4 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -16,7 +16,7 @@
#endif
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
-typedef void (*new_handler)();
+using new_handler = void (*)();
_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
>From 1c613c28c7cbe14c8d633fb5f6774469151e1c06 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Wed, 14 Jan 2026 08:43:56 +0100
Subject: [PATCH 5/5] add test for 'set_new_handler' when an allocation fails
---
.../set.new.handler/set_new_handler.pass.cpp | 25 ++++++++++++++++---
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/set_new_handler.pass.cpp b/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/set_new_handler.pass.cpp
index b0becc9efe8c4..deefbf7460afb 100644
--- a/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/set_new_handler.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/set_new_handler.pass.cpp
@@ -6,20 +6,37 @@
//
//===----------------------------------------------------------------------===//
+// a test will allocate an impossible amount of memory on purpose
+// and it will trigger an expected failure
+// UNSUPPORTED: asan, msan, tsan
+
// test set_new_handler
#include <new>
#include <cassert>
+#include <cstdlib>
+#include <limits>
+#include <climits>
#include "test_macros.h"
void f1() {}
void f2() {}
+void f3() { std::exit(0); }
+
+int main(int, char**) {
+ assert(std::set_new_handler(f1) == 0);
+ assert(std::set_new_handler(f2) == f1);
-int main(int, char**)
-{
- assert(std::set_new_handler(f1) == 0);
- assert(std::set_new_handler(f2) == f1);
+ // note: this test must be the last test since it will trigger
+ // the new handler and exit on the first call to it to signal success
+ // else we would be stuck in the handler being called in a loop
+ std::set_new_handler(f3);
+ size_t max_size = std::numeric_limits<size_t>::max();
+ // cap size to (~4GB) on 32-bit, (~1Exabytes) on 64-bit
+ size_t alloc_size = max_size > UINT_MAX ? max_size / 16 : max_size - 1;
+ auto ptr = new char[alloc_size]; // huge allocation
+ (void)ptr;
return 0;
}
More information about the libcxx-commits
mailing list