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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 12 23:01:35 PST 2025


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

>From e04029eacf02427d0bb11703a0565fdca74b0cf1 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/4] 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 c4ae87786cc0e9693c4bf9b5089b42a3a8d61e73 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/4] 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 0f3598a5c38010dfb96a67f26dfef49c98dde19b 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/4] 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                  | 30 ++++++++++++++++++---
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/libcxx/include/__new/global_new_delete.h b/libcxx/include/__new/global_new_delete.h
index 96510ab56b00b..4720a7d71848d 100644
--- a/libcxx/include/__new/global_new_delete.h
+++ b/libcxx/include/__new/global_new_delete.h
@@ -32,7 +32,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 a286bf7af628f..6c938ec03547b 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 _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
diff --git a/libcxx/include/__new/placement_new_delete.h b/libcxx/include/__new/placement_new_delete.h
index 42c9f34036775..9fefb17827a3c 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..b773e733dc089 100644
--- a/libcxx/src/new_handler.cpp
+++ b/libcxx/src/new_handler.cpp
@@ -24,13 +24,35 @@
 
 #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;
+    }
+}
+#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); }
+new_handler get_new_handler() noexcept {return __libcpp_atomic_load(&__new_handler);}
 
 } // namespace std
 

>From e9b8a77f0cfddc8e0a45153355209645231d5324 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Sat, 13 Dec 2025 07:59:57 +0100
Subject: [PATCH 4/4] run clang-format

---
 libcxx/include/__new/global_new_delete.h    |  6 ++---
 libcxx/include/__new/nothrow_t.h            |  2 +-
 libcxx/include/__new/placement_new_delete.h |  2 +-
 libcxx/src/new_handler.cpp                  | 30 ++++++++++-----------
 4 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/libcxx/include/__new/global_new_delete.h b/libcxx/include/__new/global_new_delete.h
index 4720a7d71848d..183249dffa458 100644
--- a/libcxx/include/__new/global_new_delete.h
+++ b/libcxx/include/__new/global_new_delete.h
@@ -32,7 +32,7 @@
 #endif
 
 #if defined(_LIBCPP_ABI_VCRUNTIME)
-#include <vcruntime_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
@@ -62,8 +62,8 @@ _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, c
 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
 #    endif
 
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
-operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
+[[__nodiscard__]]
+_LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
 [[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
 operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
diff --git a/libcxx/include/__new/nothrow_t.h b/libcxx/include/__new/nothrow_t.h
index 6c938ec03547b..7d13369728745 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 <vcruntime_new.h>
+#  include <vcruntime_new.h>
 #else
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
 struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
diff --git a/libcxx/include/__new/placement_new_delete.h b/libcxx/include/__new/placement_new_delete.h
index 9fefb17827a3c..f0bb2fd31dacf 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 <vcruntime_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 b773e733dc089..aeab6946a0ae3 100644
--- a/libcxx/src/new_handler.cpp
+++ b/libcxx/src/new_handler.cpp
@@ -26,33 +26,33 @@
 
 static constinit std::new_handler __new_handler = nullptr;
 
-#ifdef _LIBCPP_ABI_VCRUNTIME
+#  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;
-    }
+// adapter for _callnewh
+int __cdecl _new_handler_adapter(size_t) {
+  std::__libcpp_atomic_load (&__new_handler)();
+  return 1;
 }
-#endif
+} // 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
+#  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);}
+new_handler get_new_handler() noexcept { return __libcpp_atomic_load(&__new_handler); }
 
 } // namespace std
 



More information about the libcxx-commits mailing list