[libcxx-commits] [libcxx] 759327d - [libc++] Fix missing 'get_new_handler()' for MSVC mode builds (#150182)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Aug 18 05:59:32 PDT 2026
Author: Adam
Date: 2026-08-18T15:59:27+03:00
New Revision: 759327d9a39e6bbcd31383f16396dde04fa424a6
URL: https://github.com/llvm/llvm-project/commit/759327d9a39e6bbcd31383f16396dde04fa424a6
DIFF: https://github.com/llvm/llvm-project/commit/759327d9a39e6bbcd31383f16396dde04fa424a6.diff
LOG: [libc++] Fix missing 'get_new_handler()' for MSVC mode builds (#150182)
This PR adds the missing `get_new_handler()` for MSVC mode since `<new.h>`
in the UCRT is missing this function and only defines
`set_new_handler()` so it is best if all of them are defined and
implemented by libc++.
Fixes #64813, and also unblocks building for libc++'s C++23 standard
library modules on Windows using clang-cl.
Added:
Modified:
libcxx/docs/ReleaseNotes/24.rst
libcxx/include/__new/global_new_delete.h
libcxx/include/__new/new_handler.h
libcxx/include/__new/nothrow_t.h
libcxx/include/__new/placement_new_delete.h
libcxx/src/new_handler.cpp
libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
libcxx/test/std/language.support/support.dynamic/alloc.errors/set.new.handler/set_new_handler.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/ReleaseNotes/24.rst b/libcxx/docs/ReleaseNotes/24.rst
index e9a36ae94392f..fc2b95de8a676 100644
--- a/libcxx/docs/ReleaseNotes/24.rst
+++ b/libcxx/docs/ReleaseNotes/24.rst
@@ -81,5 +81,9 @@ Announcements About Future Releases
ABI Affecting Changes
---------------------
+- ``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
--------------------
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/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/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 49c21d85f5908..aeab6946a0ae3 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)
@@ -26,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); }
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>
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..9dbd4e664457a 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,35 @@
//
//===----------------------------------------------------------------------===//
+// 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);
+ volatile size_t max_size = std::numeric_limits<size_t>::max();
+ auto ptr = new char[max_size]; // huge allocation
+ (void)ptr;
return 0;
}
More information about the libcxx-commits
mailing list