fix(libc): call the new handler when operator new fails to allocate #1846

Merged
NahuelGenchi merged 1 commit from upstream-pr/1846 into main 2026-10-10 04:42:05 +00:00
NahuelGenchi commented 2026-10-08 21:36:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NahuelGenchi on 2026-10-08T21:36:52Z.


What

std::set_new_handler was implemented in libc but operator new never called the handler. When the allocator returned null, operator new threw bad_alloc straight away. The C++ standard ([new.delete.single]) says operator new loops on failure: it calls the current new handler and tries again, and it only throws bad_alloc once there is no handler. A title that frees a cache or a reserve inside its handler, which is the usual reason to install one, would crash on the first out of memory condition instead of recovering.

Allocate in StdioExtras.cpp now follows that loop for operator new, operator new[] and the two nothrow forms. The handler runs outside the allocator callback, so it may allocate, free or reinstall itself. A handler that throws bad_alloc propagates from the throwing forms and gives a null pointer from the nothrow ones. The handler slot is atomic now, since set_new_handler and get_new_handler are required to be thread safe.

Tested

Ubuntu 26.04, GCC 15.2. The new guest_new_handler test installs a replacement allocator that fails a chosen number of times and checks each form: no handler (throws, or null for nothrow), a handler that returns (retried exactly as often as it fails), a handler that uninstalls itself after three calls, a handler that throws bad_alloc, a handler that allocates, the previous handler returned by set_new_handler, and a zero byte request. It passes with the change, and without the change it dies with an uncaught bad_alloc. The non-GPU suite passes (241 tests) and the convention checks pass. Not run on Windows locally, so the Windows job is the first run there.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793084883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1846). Originally posted by [NahuelGenchi](https://github.com/NahuelGenchi) on 2026-10-08T21:36:52Z. --- ### What `std::set_new_handler` was implemented in libc but `operator new` never called the handler. When the allocator returned null, `operator new` threw `bad_alloc` straight away. The C++ standard ([new.delete.single]) says `operator new` loops on failure: it calls the current new handler and tries again, and it only throws `bad_alloc` once there is no handler. A title that frees a cache or a reserve inside its handler, which is the usual reason to install one, would crash on the first out of memory condition instead of recovering. `Allocate` in `StdioExtras.cpp` now follows that loop for `operator new`, `operator new[]` and the two nothrow forms. The handler runs outside the allocator callback, so it may allocate, free or reinstall itself. A handler that throws `bad_alloc` propagates from the throwing forms and gives a null pointer from the nothrow ones. The handler slot is atomic now, since `set_new_handler` and `get_new_handler` are required to be thread safe. ### Tested Ubuntu 26.04, GCC 15.2. The new `guest_new_handler` test installs a replacement allocator that fails a chosen number of times and checks each form: no handler (throws, or null for nothrow), a handler that returns (retried exactly as often as it fails), a handler that uninstalls itself after three calls, a handler that throws `bad_alloc`, a handler that allocates, the previous handler returned by `set_new_handler`, and a zero byte request. It passes with the change, and without the change it dies with an uncaught `bad_alloc`. The non-GPU suite passes (241 tests) and the convention checks pass. Not run on Windows locally, so the Windows job is the first run there. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
rmz-24 commented 2026-10-09 12:00:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rmz-24 on 2026-10-09T12:00:17Z.


Windows run, since this was tested on Ubuntu only: Windows 11 Pro 25H2 (10.0.26200.9457), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. 81d86aa5 cherry-picked onto current main d70b8998 (applies cleanly), full build plus libs.

  • guest_new_handler passes.
  • Full ctest: 511/511.
  • With StdioExtras.cpp reset to main and the new test kept, guest_new_handler fails as expected: std::bad_alloc escapes on the first failed allocation, because no handler is called.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080438205 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1846#issuecomment-6080438205). Originally posted by [rmz-24](https://github.com/rmz-24) on 2026-10-09T12:00:17Z. --- Windows run, since this was tested on Ubuntu only: Windows 11 Pro 25H2 (10.0.26200.9457), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. 81d86aa5 cherry-picked onto current `main` d70b8998 (applies cleanly), full build plus `libs`. - `guest_new_handler` passes. - Full ctest: 511/511. - With `StdioExtras.cpp` reset to `main` and the new test kept, `guest_new_handler` fails as expected: `std::bad_alloc` escapes on the first failed allocation, because no handler is called. AI-assisted: yes
github-actions[bot] commented 2026-10-10 04:18:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T04:18:39Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2545 core/libs/tests/GuestNewHandler.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6093689082 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1846#issuecomment-6093689082). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T04:18:39Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2545 | | | `core/libs/tests/GuestNewHandler.cpp` |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!1846
No description provided.