Skip to content

[libc++] Fix missing 'get_new_handler()' for Windows builds #150182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions libcxx/include/__new/new_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions libcxx/src/new_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right. The definitions provided __new_handler are dummy and non-conformant. Perhaps we should rely on VCRuntime's functionalities.

Copy link
Contributor

@frederick-vs-ja frederick-vs-ja Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we should include <vcruntime_new.h> instead of <new.h> (in other headers).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we should include <vcruntime_new.h> instead of <new.h> (in other headers).

Unfortunately that would increase the scope of this PR, which is why it exists in standalone form rather than with #14899

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right. The definitions provided __new_handler are dummy and non-conformant. Perhaps we should rely on VCRuntime's functionalities.

Care to elaborate? from what I've seen in MS-STL they both have similar implementations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we should include <vcruntime_new.h> instead of <new.h> (in other headers).

Unfortunately that would increase the scope of this PR, which is why it exists in standalone form rather than with #14899

I currently think that this is a simpler way to resolving declaration mismatch. If we don't do so, perhaps we need to define _CRTIMP2 properly in some configuration header.

This doesn't seem right. The definitions provided __new_handler are dummy and non-conformant. Perhaps we should rely on VCRuntime's functionalities.

Care to elaborate? from what I've seen in MS-STL they both have similar implementations.

The definitions provided in libcxx/src/new_handler.cpp don't interact with operator new/operator new[] (and thus non-conforming), and merely let the library compile under some conditions.

The proper implementations are expected to be provided by the underlying library (libsupc++, libc++abi, or VCRuntime, possibly along with some parts of MSVC STL).

#elif defined(LIBCXX_BUILDING_LIBCXXABI)
// nothing to do, we use the one from libc++abi
#elif defined(LIBCXXRT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Loading