fix(libSceNpCommon): refuse to terminate a callout context from its own callout #11601

Merged
x-labb merged 1 commit from upstream-pr/11601 into main 2026-11-02 12:25:59 +00:00
x-labb commented 2026-10-26 22:24:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by x-labb on 2026-10-26T22:24:11Z.


What

sceNpCalloutTermCtx called from a callout of the same context could not join the worker thread (the self-join returns EDEADLK), destroyed the context's mutex and condition anyway, and returned. If the caller then released the context, the worker locked the destroyed mutex after the callout returned and the host crashed with 0xC0000005.

The call now throws when the calling thread is the context's worker. What the console does here is unknown, so the gap is recorded in the library's technical debt. Terminating a context from any other thread is unchanged.

Tested

Windows 11 x64, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release.

  • guest_np_common_runtime fails without the change (the new TestCalloutSelfTermination aborts) and passes with it.
  • ctest -R guest_np_common: 4/4 passed on each of 5 consecutive runs.
  • python tools/check_conventions.py --base origin/main: passed.
    No retail title or homebrew was run. Linux and macOS were not tested.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: Fixes #11567
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5025565805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11601). Originally posted by [x-labb](https://github.com/x-labb) on 2026-10-26T22:24:11Z. --- ### What `sceNpCalloutTermCtx` called from a callout of the same context could not join the worker thread (the self-join returns `EDEADLK`), destroyed the context's mutex and condition anyway, and returned. If the caller then released the context, the worker locked the destroyed mutex after the callout returned and the host crashed with `0xC0000005`. The call now throws when the calling thread is the context's worker. What the console does here is unknown, so the gap is recorded in the library's technical debt. Terminating a context from any other thread is unchanged. ### Tested Windows 11 x64, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release. - `guest_np_common_runtime` fails without the change (the new `TestCalloutSelfTermination` aborts) and passes with it. - `ctest -R guest_np_common`: 4/4 passed on each of 5 consecutive runs. - `python tools/check_conventions.py --base origin/main`: passed. No retail title or homebrew was run. Linux and macOS were not tested. ### Checklist - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: Fixes #11567 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-26 23:12:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T23:12:47Z.

Review: CHANGES_REQUESTED


Reviewed head c6705fd7636aff25d97840144e35e1a2f3d1f68f and issue #11567.

[P1] Do not let the new exception escape the callout worker. CalloutThread invokes head->handler(head->arg) without a catch boundary. If a real callback calls sceNpCalloutTermCtx and does not itself catch std::runtime_error, this guard throws out of the worker entry point and invokes std::terminate; the host still terminates instead of reaching a safe state. TestCalloutSelfTermination catches the exception inside its callback, so it does not exercise the uncaught case that ordinary callers will hit.

Please make this path safe for an uncaught call from a callback (for example, refuse teardown without throwing and leave the context initialized for later external termination, or add an explicit worker exception policy that preserves process safety). Add a test where the callback calls sceNpCalloutTermCtx without catching and confirm the process remains alive, the worker returns to its loop, and external teardown succeeds. The existing external-thread termination path should remain covered.

The issue demonstrates that returning from self-termination and freeing the context causes a later use-after-free; this guard correctly detects that call site, but the exception currently converts it into a process-wide termination. Hosted conventions, intake and labels pass; no build/test run is reported in hosted status. The author reports Windows CTest passes with the callback catching the exception; I did not rerun locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5660325380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11601#pullrequestreview-5660325380). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T23:12:47Z. **Review:** CHANGES_REQUESTED --- Reviewed head `c6705fd7636aff25d97840144e35e1a2f3d1f68f` and issue #11567. **[P1] Do not let the new exception escape the callout worker.** `CalloutThread` invokes `head->handler(head->arg)` without a catch boundary. If a real callback calls `sceNpCalloutTermCtx` and does not itself catch `std::runtime_error`, this guard throws out of the worker entry point and invokes `std::terminate`; the host still terminates instead of reaching a safe state. `TestCalloutSelfTermination` catches the exception inside its callback, so it does not exercise the uncaught case that ordinary callers will hit. Please make this path safe for an uncaught call from a callback (for example, refuse teardown without throwing and leave the context initialized for later external termination, or add an explicit worker exception policy that preserves process safety). Add a test where the callback calls `sceNpCalloutTermCtx` without catching and confirm the process remains alive, the worker returns to its loop, and external teardown succeeds. The existing external-thread termination path should remain covered. The issue demonstrates that returning from self-termination and freeing the context causes a later use-after-free; this guard correctly detects that call site, but the exception currently converts it into a process-wide termination. Hosted conventions, intake and labels pass; no build/test run is reported in hosted status. The author reports Windows CTest passes with the callback catching the exception; I did not rerun locally.
jotitadev commented 2026-10-27 01:07:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T01:07:01Z.

Review: COMMENTED


Linux run of the current head, since the description says Linux and macOS were not tested.

Machine: Linux x86_64, i7-8700 (6 cores / 12 threads, AVX2), Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON. Head c6705fd76 merged onto main cb45ae2dd.

  • Build ok. The 4 guest_np_common* tests pass; guest_np_common_runtime 100 of 100 runs.
  • With Runtime.cpp from main and the test kept, guest_np_common_runtime aborts with terminating a context from its own callout is refused. On Linux, main's self-join does not hang: it returns and the context is torn down from inside its own callout, as #11567 describes.
  • On the P1 point: with the PR's Runtime.cpp and the callback's try/catch removed, Linux ends the same way. [libc] unhandled exception 'St13runtime_error', then terminate with the new message, exit 134. So an uncaught call terminates the process on Linux too.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5661087303 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11601#pullrequestreview-5661087303). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T01:07:01Z. **Review:** COMMENTED --- Linux run of the current head, since the description says Linux and macOS were not tested. Machine: Linux x86_64, i7-8700 (6 cores / 12 threads, AVX2), Ubuntu 26.04, GCC 15.2, Release, `BUILD_TESTING=ON`. Head `c6705fd76` merged onto `main` `cb45ae2dd`. - Build ok. The 4 `guest_np_common*` tests pass; `guest_np_common_runtime` 100 of 100 runs. - With `Runtime.cpp` from `main` and the test kept, `guest_np_common_runtime` aborts with `terminating a context from its own callout is refused`. On Linux, `main`'s self-join does not hang: it returns and the context is torn down from inside its own callout, as #11567 describes. - On the P1 point: with the PR's `Runtime.cpp` and the callback's `try`/`catch` removed, Linux ends the same way. `[libc] unhandled exception 'St13runtime_error'`, then `terminate` with the new message, exit 134. So an uncaught call terminates the process on Linux too. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-29 07:51:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T07:51:01Z.

Review: APPROVED


Review — approved

I withdraw my earlier P1 finding. The repository README explicitly requires unsupported or unexpected states to throw std::runtime_error, print the reason, and terminate the process. This guard follows that policy and records the console behavior as unknown; requiring the callback to survive an uncaught exception would contradict the documented fail-fast rule.

What I checked

  • Reviewed exact head c6705fd7636aff25d97840144e35e1a2f3d1f68f.
  • Confirmed the self-call check runs before locking, joining, or destroying the context, preventing the reported self-join/use-after-destroy sequence.
  • Reviewed the regression and the technical-debt entry.
  • Compared against current main 10bec5c322eb581b3e959e6c7e18bcf09e068e96: the merge preview is clean. Its only production-file delta in this PR's touched paths removes an unrelated include; the other overlapping changes are test additions and debt-document reformatting.
  • git diff --check and tools/check_conventions.py pass against current main.

Test evidence

  • The author reports five Windows runs of the focused CTest set, 4/4 each.
  • Independent reports describe 412 selected Windows tests passing (406 pass, 6 skip), plus Linux build and 100/100 focused runtime runs. These are contributor-reported; I did not rerun them.

Not verified

No console/title behavior was measured. The current-main Linux product CI is not reported as having run for this head.

The implementation is consistent with the repository's explicit fail-fast policy, so I approve this change at the reviewed head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5694412882 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11601#pullrequestreview-5694412882). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T07:51:01Z. **Review:** APPROVED --- # Review — approved I withdraw my earlier P1 finding. The repository README explicitly requires unsupported or unexpected states to throw `std::runtime_error`, print the reason, and terminate the process. This guard follows that policy and records the console behavior as unknown; requiring the callback to survive an uncaught exception would contradict the documented fail-fast rule. ## What I checked - Reviewed exact head `c6705fd7636aff25d97840144e35e1a2f3d1f68f`. - Confirmed the self-call check runs before locking, joining, or destroying the context, preventing the reported self-join/use-after-destroy sequence. - Reviewed the regression and the technical-debt entry. - Compared against current `main` `10bec5c322eb581b3e959e6c7e18bcf09e068e96`: the merge preview is clean. Its only production-file delta in this PR's touched paths removes an unrelated include; the other overlapping changes are test additions and debt-document reformatting. - `git diff --check` and `tools/check_conventions.py` pass against current `main`. ## Test evidence - The author reports five Windows runs of the focused CTest set, 4/4 each. - Independent reports describe 412 selected Windows tests passing (406 pass, 6 skip), plus Linux build and 100/100 focused runtime runs. These are contributor-reported; I did not rerun them. ## Not verified No console/title behavior was measured. The current-main Linux product CI is not reported as having run for this head. The implementation is consistent with the repository's explicit fail-fast policy, so I approve this change at the reviewed head.
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!11601
No description provided.