[libcxx-commits] [libcxx] [libc++] Fix missing 'get_new_handler()' for Windows builds (PR #150182)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 23 19:04:24 PST 2025
https://github.com/siradam7th updated https://github.com/llvm/llvm-project/pull/150182
>From b4dfdfce0b040f6bf09ac6f315fb4c2ea6cbf617 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/6] 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 c8d52f58e4c35649a9744ca30810b5ef3df2cb62 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/6] 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 944821c4e3e3910cad23cd596a7b9af872a34781 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/6] implement 'set_new_handler' and 'get_new_handler' for
Windows
---
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 | 28 ++++++++++++++++++---
4 files changed, 30 insertions(+), 8 deletions(-)
diff --git a/libcxx/include/__new/global_new_delete.h b/libcxx/include/__new/global_new_delete.h
index 96510ab56b00b..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 <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 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 42c9f34036775..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 <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 56a2f75a7734ba05e938b3fc7a6bbca15f0b6275 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/6] 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 e2166aa1c8871157181fa891ad9bc360eef3b346 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Sat, 13 Dec 2025 16:29:03 +0100
Subject: [PATCH 5/6] add [[nodiscard]] to 'get_new_handler'
---
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 c0c6fe49596a4..4ba5e32ddbc19 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -18,7 +18,7 @@
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
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;
+[[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
#endif // _LIBCPP___NEW_NEW_HANDLER_H
>From b1d742954543cce07f0cac9a077f38cb586fe422 Mon Sep 17 00:00:00 2001
From: siradam7th <siradam7th at users.noreply.github.com>
Date: Sat, 13 Dec 2025 18:00:50 +0100
Subject: [PATCH 6/6] use [[__nodiscard__]] instead for backward compatibility
---
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 4ba5e32ddbc19..2ea4678bd7087 100644
--- a/libcxx/include/__new/new_handler.h
+++ b/libcxx/include/__new/new_handler.h
@@ -18,7 +18,7 @@
_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
using new_handler = void (*)();
_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
-[[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
+[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
#endif // _LIBCPP___NEW_NEW_HANDLER_H
More information about the libcxx-commits
mailing list