fix(libc): write the constraint message and return from the default handler #9935

Closed
orezraey wants to merge 8 commits from upstream-pr/9935 into main
orezraey commented 2026-10-22 22:37:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-22T22:37:10Z.


What

The default runtime-constraint handler of libc called std::abort() without writing anything. It now writes the message and a newline to the guest stderr and returns, so an _s function called with no handler installed returns its error to the title instead of ending it.

This follows the default handler of the libc.prx titles ship in sce_module/, which this module stands for. JJenkx read it from the disassembly of the copies in PPSA13934, PPSA07923 and PPSA05916 (#9087 comment): set_constraint_handler_s stores handler ? handler : default, and the default calls fputs(message, stderr), fputc('\n', stderr) and returns, while abort_handler_s makes the same two calls and then calls abort. The wcsrtombs_s debt entry already describes that handler the same way. The console's libSceLibcInternal.sprx (firmware 13.60, measured for #9087) has a different default that ends the process after writing; that module is not this one, and the debt entry says so.

libSceAmpr's re-export shares the header, so its default handler changes the same way.

Tested

  • Head after merging main (f80ce6cec1, with #11121): 7aa063d91c. The only conflict was docs/dev/debt/libc.md: kept main's localeconv entry (and its removal of the setlocale entry) and this PR's set_constraint_handler_s wording. guest_safe_errors passes three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); check_conventions passes.
  • Head after merging main (f26434afa3, with #10706): 0bae438695. The test now takes its declarations from the libc headers; SonyCrt.hpp includes Api.hpp instead of declaring fputs/fputc again. guest_safe_errors, guest_crt, guest_bounds_checking, guest_qsort and guest_wide_strings pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); public_headers audit and check_conventions pass.
  • Head after merging main (a2c36d4ed6, with #12589): 9260172a4c. guest_safe_errors now expects the default handler to write strerror_s: bad argument\n twice, the message #12589 passes. guest_safe_errors, guest_crt and guest_bounds_checking pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); check_conventions passes.
    Windows 11, MinGW (winlibs).
  • guest_safe_errors now installs the default handler with set_constraint_handler_s(NULL), redirects the guest stderr to a file and checks that two strerror_s violations return EINVAL (22) and write strerror_s: bad argument\n twice. On main the test ends with 0xc0000409 at the first violation; with the change it passes.
  • guest_safe_errors, guest_crt, guest_bounds_checking, guest_qsort and guest_wide_strings pass three runs in a row.
  • tools/check_conventions.py passes.
  • Head after merging main (ba8dcc6ae8, with #13220, which removes the duplicate write-watch test names that failed the macOS jobs): d7b7f9ad59. guest_safe_errors passes three runs in a row on Windows (MinGW, GCC 15.2). tools/check_conventions.py passes.
  • Head after merging main (98346b748c, with #9688's qsort_s entry): 91d71873be. The only conflict was docs/dev/debt/libc.md: kept main's qsort_s entry and this PR's set_constraint_handler_s wording. guest_safe_errors passes three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL, GCC 16.2). tools/check_conventions.py passes.

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: #7081 (it rewrites the set_constraint_handler_s debt entry and edits the same lines; merge order only; #9688 has merged); #9087 lands after this PR and will be refreshed then; #10706 removes the test declarations this PR adds to GuestSafeErrors.cpp (merge order only); #11621 (B-Howell) adds libc.md entries next to the one this PR edits (merge order only); #12598 (JJenkx) rewrites the qsort_s entry next to the set_constraint_handler_s entry this PR edits (merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4975051878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9935). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-22T22:37:10Z. --- ### What The default runtime-constraint handler of `libc` called `std::abort()` without writing anything. It now writes the message and a newline to the guest `stderr` and returns, so an `_s` function called with no handler installed returns its error to the title instead of ending it. This follows the default handler of the `libc.prx` titles ship in `sce_module/`, which this module stands for. JJenkx read it from the disassembly of the copies in PPSA13934, PPSA07923 and PPSA05916 ([#9087 comment](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6332707345)): `set_constraint_handler_s` stores `handler ? handler : default`, and the default calls `fputs(message, stderr)`, `fputc('\n', stderr)` and returns, while `abort_handler_s` makes the same two calls and then calls `abort`. The `wcsrtombs_s` debt entry already describes that handler the same way. The console's `libSceLibcInternal.sprx` (firmware 13.60, measured for #9087) has a different default that ends the process after writing; that module is not this one, and the debt entry says so. `libSceAmpr`'s re-export shares the header, so its default handler changes the same way. ### Tested - Head after merging `main` (`f80ce6cec1`, with #11121): `7aa063d91c`. The only conflict was `docs/dev/debt/libc.md`: kept main's `localeconv` entry (and its removal of the `setlocale` entry) and this PR's `set_constraint_handler_s` wording. `guest_safe_errors` passes three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `check_conventions` passes. - Head after merging `main` (`f26434afa3`, with #10706): `0bae438695`. The test now takes its declarations from the libc headers; `SonyCrt.hpp` includes `Api.hpp` instead of declaring `fputs`/`fputc` again. `guest_safe_errors`, `guest_crt`, `guest_bounds_checking`, `guest_qsort` and `guest_wide_strings` pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `public_headers` audit and `check_conventions` pass. - Head after merging `main` (`a2c36d4ed6`, with #12589): `9260172a4c`. `guest_safe_errors` now expects the default handler to write `strerror_s: bad argument\n` twice, the message #12589 passes. `guest_safe_errors`, `guest_crt` and `guest_bounds_checking` pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL Ubuntu, GCC); `check_conventions` passes. Windows 11, MinGW (winlibs). - `guest_safe_errors` now installs the default handler with `set_constraint_handler_s(NULL)`, redirects the guest `stderr` to a file and checks that two `strerror_s` violations return `EINVAL` (22) and write `strerror_s: bad argument\n` twice. On `main` the test ends with `0xc0000409` at the first violation; with the change it passes. - `guest_safe_errors`, `guest_crt`, `guest_bounds_checking`, `guest_qsort` and `guest_wide_strings` pass three runs in a row. - `tools/check_conventions.py` passes. - Head after merging `main` (`ba8dcc6ae8`, with #13220, which removes the duplicate write-watch test names that failed the macOS jobs): `d7b7f9ad59`. `guest_safe_errors` passes three runs in a row on Windows (MinGW, GCC 15.2). `tools/check_conventions.py` passes. - Head after merging `main` (`98346b748c`, with #9688's `qsort_s` entry): `91d71873be`. The only conflict was `docs/dev/debt/libc.md`: kept main's `qsort_s` entry and this PR's `set_constraint_handler_s` wording. `guest_safe_errors` passes three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL, GCC 16.2). `tools/check_conventions.py` passes. ### 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 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: #7081 (it rewrites the `set_constraint_handler_s` debt entry and edits the same lines; merge order only; #9688 has merged); #9087 lands after this PR and will be refreshed then; #10706 removes the test declarations this PR adds to `GuestSafeErrors.cpp` (merge order only); #11621 (B-Howell) adds `libc.md` entries next to the one this PR edits (merge order only); #12598 (JJenkx) rewrites the `qsort_s` entry next to the `set_constraint_handler_s` entry this PR edits (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-22 23:58:12 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed exact head f96b6d2fb8e39db1f0fb8d9c5f3777aee65833bc against current main 895f47f4eeaf56048b9a7080b7c01a9a47cf0ecf. The default handler writes its supplied message and newline to guest stderr and returns; this is the behavior attributed to the three shipped libc.prx disassemblies, and the debt entry separately retains the measured libSceLibcInternal.sprx process-termination difference. The regression redirects guest stderr, checks two exact messages, and verifies both violating calls return EINVAL. The author reports five focused/neighboring Windows tests passing three repetitions after merging main; I did not rerun the C++ suite locally. I independently ran current-main git diff --check and conventions; both pass. Hosted eligibility, conventions, intake, labels, relinker, ASan, register-context and write-watch checks pass; full Linux/Windows product builds are skipped by repository policy. The declared #9087 debt-text integration is appropriate and no intervening current-main commit changes the reviewed source, test or debt paths. I found no remaining issue in the reviewed change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5622353646 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9935#pullrequestreview-5622353646). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T23:58:12Z. **Review:** APPROVED --- Reviewed exact head `f96b6d2fb8e39db1f0fb8d9c5f3777aee65833bc` against current `main` `895f47f4eeaf56048b9a7080b7c01a9a47cf0ecf`. The default handler writes its supplied message and newline to guest `stderr` and returns; this is the behavior attributed to the three shipped `libc.prx` disassemblies, and the debt entry separately retains the measured `libSceLibcInternal.sprx` process-termination difference. The regression redirects guest `stderr`, checks two exact messages, and verifies both violating calls return `EINVAL`. The author reports five focused/neighboring Windows tests passing three repetitions after merging main; I did not rerun the C++ suite locally. I independently ran current-main `git diff --check` and conventions; both pass. Hosted eligibility, conventions, intake, labels, relinker, ASan, register-context and write-watch checks pass; full Linux/Windows product builds are skipped by repository policy. The declared #9087 debt-text integration is appropriate and no intervening current-main commit changes the reviewed source, test or debt paths. I found no remaining issue in the reviewed change.
MarcZX10 commented 2026-10-29 23:22:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T23:22:55Z.

Review: APPROVED


Re-checked — new head, looks good

The current head retains the reviewed default-handler behavior and test, and the rebase passes the available static and hosted checks.

What I ran

Reviewed PR head 7aa063d91cb079e5d82cdb64aff5731288b0406f against base f80ce6cec1cad29aa45bd0bf6d8062a714c18e03; refreshed current main is f46f453a56f91d77e29ce0cbd7fe5bfb2fb69834. The merge preview with current main is clean (tree a678797797647eee21884965d95daac02477dea6). Environment: Linux 6.1.0-54-amd64 x86_64; no local compiler, build, or tests run.

git diff --check upstream/main...refs/review/pr-9935-current
python3 tools/check_conventions.py --base upstream/main
git merge-tree --write-tree upstream/main refs/review/pr-9935-current
gh pr checks 9935 --repo boykopovar/AnyPS5

Results

Check Result
Diff whitespace check Pass; exit 0, no output.
Repository conventions Pass.
Merge preview with current main Clean.
Hosted relinker, ASan, register-context, write-watch, conventions, eligibility, labels, and report checks Pass on this exact head.
Hosted Linux and Windows product jobs Skipped by the workflow.
Focused guest_safe_errors and neighboring tests The author reports three passing runs on Windows and WSL2 on this exact head; not rerun locally.
Unedited output from local and hosted checks
$ git diff --check upstream/main...refs/review/pr-9935-current
(exit 0; no output)
$ python3 tools/check_conventions.py --base upstream/main
All conventions checks passed.
$ git merge-tree --write-tree upstream/main refs/review/pr-9935-current
a678797797647eee21884965d95daac02477dea6
$ gh pr checks 9935 --repo boykopovar/AnyPS5
conventions	pass	17s	https://github.com/boykopovar/AnyPS5/actions/runs/40971070959/job/123787653774
eligibility	pass	5s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122005990939
labels	pass	10s	https://github.com/boykopovar/AnyPS5/actions/runs/40971071060/job/123787654343
register-context (macos-15-intel, Debug)	pass	53s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026989
register-context (macos-15-intel, Release)	pass	1m7s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027030
register-context (ubuntu-latest, Debug)	pass	31s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026976
register-context (ubuntu-latest, Release)	pass	28s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027024
relinker (macos-latest, clang++)	pass	2m54s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027082
relinker (macos-latest, clang++, x86_64)	pass	5m20s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027040
relinker (ubuntu-latest, g++)	pass	3m41s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027013
relinker (windows-latest, g++)	pass	7m57s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026952
relinker-asan	pass	2m34s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026978
write-watch (macos-15-intel)	pass	49s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026977
linux	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006028604
windows	skipping	0	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006028418
report	pass	38s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684047/job/122005990560
write-watch (macos-15)	pass	37s	https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026999

The focused test results are author-reported in the PR description; they are not local runs.

Negative control

Assertion Change to code under test Did the new test fail?
The default handler returns instead of aborting and writes its message and newline The author reports running the added test against main without this PR. Yes; it ended with exit code 0xc0000409 at the first violation.

Findings

Blocking

None found in the current head. It writes the supplied message and newline to guest stderr, then returns to the caller; the test exercises two violations and confirms both return EINVAL and produce the expected output.

Non-blocking

The PR records merge-order dependencies for adjacent libc debt and test changes. Preserve that order if one of those dependent changes lands first.

Sources

Not verified

I did not build or run tests locally, independently reproduce the author's Windows/WSL2 tests, or verify behavior on a physical console. The hosted Linux and Windows product jobs were skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5706233539 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9935#pullrequestreview-5706233539). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T23:22:55Z. **Review:** APPROVED --- # Re-checked — new head, looks good The current head retains the reviewed default-handler behavior and test, and the rebase passes the available static and hosted checks. ## What I ran Reviewed PR head `7aa063d91cb079e5d82cdb64aff5731288b0406f` against base `f80ce6cec1cad29aa45bd0bf6d8062a714c18e03`; refreshed current `main` is `f46f453a56f91d77e29ce0cbd7fe5bfb2fb69834`. The merge preview with current `main` is clean (tree `a678797797647eee21884965d95daac02477dea6`). Environment: Linux 6.1.0-54-amd64 x86_64; no local compiler, build, or tests run. ```sh git diff --check upstream/main...refs/review/pr-9935-current python3 tools/check_conventions.py --base upstream/main git merge-tree --write-tree upstream/main refs/review/pr-9935-current gh pr checks 9935 --repo boykopovar/AnyPS5 ``` ## Results | Check | Result | |---|---| | Diff whitespace check | Pass; exit 0, no output. | | Repository conventions | Pass. | | Merge preview with current `main` | Clean. | | Hosted relinker, ASan, register-context, write-watch, conventions, eligibility, labels, and report checks | Pass on this exact head. | | Hosted Linux and Windows product jobs | Skipped by the workflow. | | Focused `guest_safe_errors` and neighboring tests | The author reports three passing runs on Windows and WSL2 on this exact head; not rerun locally. | <details> <summary>Unedited output from local and hosted checks</summary> ```text $ git diff --check upstream/main...refs/review/pr-9935-current (exit 0; no output) $ python3 tools/check_conventions.py --base upstream/main All conventions checks passed. $ git merge-tree --write-tree upstream/main refs/review/pr-9935-current a678797797647eee21884965d95daac02477dea6 $ gh pr checks 9935 --repo boykopovar/AnyPS5 conventions pass 17s https://github.com/boykopovar/AnyPS5/actions/runs/40971070959/job/123787653774 eligibility pass 5s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122005990939 labels pass 10s https://github.com/boykopovar/AnyPS5/actions/runs/40971071060/job/123787654343 register-context (macos-15-intel, Debug) pass 53s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026989 register-context (macos-15-intel, Release) pass 1m7s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027030 register-context (ubuntu-latest, Debug) pass 31s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026976 register-context (ubuntu-latest, Release) pass 28s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027024 relinker (macos-latest, clang++) pass 2m54s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027082 relinker (macos-latest, clang++, x86_64) pass 5m20s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027040 relinker (ubuntu-latest, g++) pass 3m41s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006027013 relinker (windows-latest, g++) pass 7m57s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026952 relinker-asan pass 2m34s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026978 write-watch (macos-15-intel) pass 49s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026977 linux skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006028604 windows skipping 0 https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006028418 report pass 38s https://github.com/boykopovar/AnyPS5/actions/runs/40448684047/job/122005990560 write-watch (macos-15) pass 37s https://github.com/boykopovar/AnyPS5/actions/runs/40448684045/job/122006026999 ``` The focused test results are author-reported in the PR description; they are not local runs. </details> ## Negative control | Assertion | Change to code under test | Did the new test fail? | |---|---|---| | The default handler returns instead of aborting and writes its message and newline | The author reports running the added test against `main` without this PR. | Yes; it ended with exit code `0xc0000409` at the first violation. | ## Findings ### Blocking None found in the current head. It writes the supplied message and newline to guest `stderr`, then returns to the caller; the test exercises two violations and confirms both return `EINVAL` and produce the expected output. ### Non-blocking The PR records merge-order dependencies for adjacent libc debt and test changes. Preserve that order if one of those dependent changes lands first. ## Sources - [Default handler and test at this head](https://github.com/boykopovar/AnyPS5/tree/7aa063d91cb079e5d82cdb64aff5731288b0406f/core/libs) - [Firmware behavior evidence cited by the author](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6332707345) ## Not verified I did not build or run tests locally, independently reproduce the author's Windows/WSL2 tests, or verify behavior on a physical console. The hosted Linux and Windows product jobs were skipped.

Pull request closed

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!9935
No description provided.