[libcxx-commits] [libcxx] [libc++] Fix missing 'get_new_handler()' for Windows builds (PR #150182)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jul 12 16:02:05 PDT 2026


https://github.com/siradam7th updated https://github.com/llvm/llvm-project/pull/150182

>From c1e976f6466c76c7aae8f158cf343c4873779b8a Mon Sep 17 00:00:00 2001
From: siradam7th <oss at afterback.gq>
Date: Mon, 13 Jul 2026 00:00:29 +0100
Subject: [PATCH 1/6] [libc++] 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 fc6aabec61b64..c4473c13fab21 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -15,9 +15,6 @@
 #  pragma GCC system_header
 #endif
 
-#if defined(_LIBCPP_ABI_VCRUNTIME)
-#  include <new.h>
-#else
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
 _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
@@ -27,6 +24,5 @@ _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
 
 _LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
 _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 2fcdec32f07061e2f96c858641693ca841bc8d39 Mon Sep 17 00:00:00 2001
From: siradam7th <oss at afterback.gq>
Date: Mon, 13 Jul 2026 00:00:30 +0100
Subject: [PATCH 2/6] [libc++] 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 e3ac94f62e8d23ad5d7210ab1094e665c4d576da Mon Sep 17 00:00:00 2001
From: siradam7th <oss at afterback.gq>
Date: Mon, 13 Jul 2026 00:00:31 +0100
Subject: [PATCH 3/6] [libc++] 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 49a671ee4f4a3e6f3a390f8317f3b3bc9c40c776 Mon Sep 17 00:00:00 2001
From: siradam7th <oss at afterback.gq>
Date: Mon, 13 Jul 2026 00:00:32 +0100
Subject: [PATCH 4/6] [libc++] 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 c4473c13fab21..fa5f29146e1b2 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -18,7 +18,7 @@
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
 _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
-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;
 

>From 49422e8680152e5c2e46aaec3fd0f7514f29c14b Mon Sep 17 00:00:00 2001
From: siradam7th <oss at afterback.gq>
Date: Mon, 13 Jul 2026 00:00:33 +0100
Subject: [PATCH 5/6] [libc++] 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;
 }

>From e9086142be6a1a395a6a74d52206446c43ac8fed Mon Sep 17 00:00:00 2001
From: siradam7th <oss at afterback.gq>
Date: Mon, 13 Jul 2026 00:00:34 +0100
Subject: [PATCH 6/6] [libc++] add release note for LLVM 23

---
 libcxx/docs/ReleaseNotes/23.rst | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index f7a8e5b04f1ea..a7a5421d52653 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -131,5 +131,9 @@ ABI Affecting Changes
 - ``std::exception_ptr`` now uses the same layout in MSVC mode as on other platforms, changing the size from two to one
   pointer.
 
+- ``std::set_new_handler`` and ``std::get_new_handler`` are now implemented by libc++ on Windows using ``VCRuntime``
+  directly instead of implicitly relying on ``msvcprt`` to implement them, therefore the global state of the handler
+  is no longer shared with ``msvcprt`` since libc++ now maintains its own internal state of the handler.
+
 Build System Changes
 --------------------



More information about the libcxx-commits mailing list