fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081

Closed
Pouare514 wants to merge 4 commits from upstream-pr/7081 into main
Pouare514 commented 2026-10-17 10:37:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T10:37:31Z.


What

  • Share Sony CRT constraint-handler state and lconv storage between libc and libSceAmpr through a single definition in libc (core/libs/prx/libc/include/SonyCrt.hpp + src/SonyCrt.cpp). Each module previously owned its own handler/mutex/lconv static (internal linkage per TU), so a handler set via one module never fired in the other.
  • Add SonyLconvVariant { Ampr48, Compact0 }: SonyLocaleconv() keeps the verified Ampr layout (decimal_point@0x48); Compact0 (decimal_point@0x00, no prefix) covers catalog titles reusing the Dinkumware layout with a different offset (e.g. PPSA99902, per review on #545). Layouts pinned with static_assert(offsetof(...)).
  • libSceAmpr re-exports the shared state (0hlfW1O4Aa4 localeconv, 6f5f-qx4ucA wcscpy_s, ENLfKJEZTjE/ODGONXcSmz4 handlers) and links libc. wcscpy_s stays 16-bit (guest wchar); the default handler still aborts. Both assumptions debt-noted in TechnicalDebt.md.

Tested

  • New guest_crt test: lconv offset asserts (0x48 incl. sceAmprLocaleconv == localeconv, 0x00 compact), cross-TU handler visibility both directions, default-abort via child process. Passes; verified it fails without the fix (slot disconnected → abort 0xc0000409) and passes with it.
  • Neighbors guest_fiopen, guest_locale, guest_ampr_markers, guest_wide_strings, file_position: 5/5 pass (WinLibs GCC 15.2.0, Release).
  • python tools/check_conventions.py --base origin/main --head HEAD clean, git diff --check clean.
  • Note: the full libs target has a pre-existing, unrelated failure (libSceVideoOut.prx NID patch: duplicate patched export name: LibwuIonIBw), reproduced on pristine origin/main.

Checklist

  • Based on current main; no other open PR implements the same change
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • No new unimplemented paths or silent stubs
  • No notes, investigation files or images added to the repository
  • New third-party code: none
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: catalog localeconv reuse (SymphonyStation5/Cloud Play same NID, PPSA99902 compact variant), #6707 qsort_s rebase
  • GPUs tested: n/a for this libc accounting change
  • AI-assisted: yes; I am responsible for this change
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899365640 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T10:37:31Z. --- ### What - Share Sony CRT constraint-handler state and `lconv` storage between `libc` and `libSceAmpr` through a single definition in `libc` (`core/libs/prx/libc/include/SonyCrt.hpp` + `src/SonyCrt.cpp`). Each module previously owned its own handler/mutex/lconv static (internal linkage per TU), so a handler set via one module never fired in the other. - Add `SonyLconvVariant { Ampr48, Compact0 }`: `SonyLocaleconv()` keeps the verified Ampr layout (`decimal_point@0x48`); `Compact0` (`decimal_point@0x00`, no prefix) covers catalog titles reusing the Dinkumware layout with a different offset (e.g. PPSA99902, per review on #545). Layouts pinned with `static_assert(offsetof(...))`. - `libSceAmpr` re-exports the shared state (`0hlfW1O4Aa4` localeconv, `6f5f-qx4ucA` wcscpy_s, `ENLfKJEZTjE`/`ODGONXcSmz4` handlers) and links `libc`. `wcscpy_s` stays 16-bit (guest `wchar`); the default handler still aborts. Both assumptions debt-noted in `TechnicalDebt.md`. ### Tested - New `guest_crt` test: `lconv` offset asserts (0x48 incl. `sceAmprLocaleconv == localeconv`, 0x00 compact), cross-TU handler visibility both directions, default-abort via child process. **Passes**; verified it **fails without** the fix (slot disconnected → abort `0xc0000409`) and passes with it. - Neighbors `guest_fiopen`, `guest_locale`, `guest_ampr_markers`, `guest_wide_strings`, `file_position`: 5/5 pass (WinLibs GCC 15.2.0, Release). - `python tools/check_conventions.py --base origin/main --head HEAD` clean, `git diff --check` clean. - Note: the full `libs` target has a pre-existing, unrelated failure (`libSceVideoOut.prx` NID patch: `duplicate patched export name: LibwuIonIBw`), reproduced on pristine `origin/main`. ### Checklist - [x] Based on current `main`; no other open PR implements the same change - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] No new unimplemented paths or silent stubs - [x] No notes, investigation files or images added to the repository - [x] New third-party code: none - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: catalog `localeconv` reuse (SymphonyStation5/Cloud Play same NID, PPSA99902 compact variant), #6707 `qsort_s` rebase - [x] GPUs tested: n/a for this libc accounting change - [x] AI-assisted: yes; I am responsible for this change
Pouare514 commented 2026-10-17 11:46:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T11:46:09Z.


Rebased onto 677354d9d (was e51d4e1f) and reconciled with main without deleting main content. New HEAD fe0120852, MERGEABLE, conventions pass.

Per file:

  • SonyCrt.hpp: kept main full content (*ti3 family, strtok/ctime/asctime/mbstowcs/fpclassify/sinh/fltrounds, fixed Dinkumware localeconv table); removed namespace{} and externalized only stateful pieces (mutex/slot/default+ignore+set+invoke handlers, SonyLocaleconv) with pure helpers staying inline. Added SonyLconvVariant/SonyLconvFor/SonyLocaleconvEx decls.
  • SonyCrt.cpp: kept; lconv section rewritten for main fixed table (shared Ampr storage with main init values + Compact storage, decimal_point .), no host sync.
  • SceTypes.hpp: kept main SonyLconv untouched; added SonyLconvCompact + 0x00 assert.
  • GuestCrt.cpp: merged (main fpclassify/fltrounds/sinh/divti/umodti/asctime/ctime/strtok/mbstowcs/random + branch offsets/cross-TU/abort-child). Tests live at core/libs/tests/GuestCrt.cpp (#6044 open, no move).
  • Ampr Export/CMakeLists, libc test stanza: main already had all 4 exports + links, kept as-is (incl. _Thrd_id etc.).
  • libc/CMakeLists: adds SonyCrt.cpp. FileAndHeap fopen_s: handler invoke + main nullptr hardening (Annex K). GuestFiopen: installs ignore handler before violation checks (default aborts by design).
  • Debt: TechnicalDebt.md untouched (now index); updated the two debt/libc.md lines to shared-state wording + compact note.

Verified: Release/Ninja build OK; guest_crt/fiopen/locale/ampr_markers/wide_strings + qsort/wide_conversion/math/mktime/strftime/time_conversion/allocating_strings all pass; git diff --check clean; check_conventions --base origin/main clean. Fail-without/pass-with holds: disconnecting Ampr slot fails guest_crt (abort), restoring passes.

Overlap audit: true coordination needed with #6133 (adds SonyModti3 inside same namespace{} - must rebase onto this) and #6707 (touches SonyCrt.hpp handler typedef/namespace + qsort_s per-TU handler docs, will need shared update). #7068 (fopen_s host errno) still open, reconciled here as handler+errno; everything else rebase-only disjoint.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233505307 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081#issuecomment-6233505307). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T11:46:09Z. --- Rebased onto 677354d9d (was e51d4e1f) and reconciled with main without deleting main content. New HEAD fe0120852, MERGEABLE, conventions pass. Per file: - SonyCrt.hpp: kept main full content (*ti3 family, strtok/ctime/asctime/mbstowcs/fpclassify/sinh/fltrounds, fixed Dinkumware localeconv table); removed namespace{} and externalized only stateful pieces (mutex/slot/default+ignore+set+invoke handlers, SonyLocaleconv) with pure helpers staying inline. Added SonyLconvVariant/SonyLconvFor/SonyLocaleconvEx decls. - SonyCrt.cpp: kept; lconv section rewritten for main fixed table (shared Ampr storage with main init values + Compact storage, decimal_point .), no host sync. - SceTypes.hpp: kept main SonyLconv untouched; added SonyLconvCompact + 0x00 assert. - GuestCrt.cpp: merged (main fpclassify/fltrounds/sinh/divti/umodti/asctime/ctime/strtok/mbstowcs/random + branch offsets/cross-TU/abort-child). Tests live at core/libs/tests/GuestCrt.cpp (#6044 open, no move). - Ampr Export/CMakeLists, libc test stanza: main already had all 4 exports + links, kept as-is (incl. _Thrd_id etc.). - libc/CMakeLists: adds SonyCrt.cpp. FileAndHeap fopen_s: handler invoke + main nullptr hardening (Annex K). GuestFiopen: installs ignore handler before violation checks (default aborts by design). - Debt: TechnicalDebt.md untouched (now index); updated the two debt/libc.md lines to shared-state wording + compact note. Verified: Release/Ninja build OK; guest_crt/fiopen/locale/ampr_markers/wide_strings + qsort/wide_conversion/math/mktime/strftime/time_conversion/allocating_strings all pass; git diff --check clean; check_conventions --base origin/main clean. Fail-without/pass-with holds: disconnecting Ampr slot fails guest_crt (abort), restoring passes. Overlap audit: true coordination needed with #6133 (adds SonyModti3 inside same namespace{} - must rebase onto this) and #6707 (touches SonyCrt.hpp handler typedef/namespace + qsort_s per-TU handler docs, will need shared update). #7068 (fopen_s host errno) still open, reconciled here as handler+errno; everything else rebase-only disjoint.
Pouare514 commented 2026-10-17 12:43:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-17T12:43:31Z.


CI note: everything runnable passes (conventions, register-context x4, relinker on Linux/Windows/macOS, write-watch x2). The full Linux + Windows builds need collaborator dispatch (automatic full builds only run on main) - requesting dispatch when review allows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6234019651 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081#issuecomment-6234019651). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-17T12:43:31Z. --- CI note: everything runnable passes (conventions, register-context x4, relinker on Linux/Windows/macOS, write-watch x2). The full Linux + Windows builds need collaborator dispatch (automatic full builds only run on main) - requesting dispatch when review allows.
blawesom commented 2026-10-17 18:57:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T18:57:10Z.


Rebase handoff — rebased onto current main (authorship unchanged). One conflict in FileAndHeap.cpp: your constraint-handler-invoking fopen_s failure path taken (main's bare GuestEinval return superseded by your handler call with GuestEnoent).

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; full suite green apart from the known main regression (agc_driver_tessellation segfault — Mesa, #5444), not this PR.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7081
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is ca9407394.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6239040742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081#issuecomment-6239040742). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T18:57:10Z. --- **Rebase handoff** — rebased onto current `main` (authorship unchanged). One conflict in `FileAndHeap.cpp`: your constraint-handler-invoking `fopen_s` failure path taken (`main`'s bare `GuestEinval` return superseded by your handler call with `GuestEnoent`). Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): builds; conventions pass; **full suite green apart from the known `main` regression** (`agc_driver_tessellation` segfault — Mesa, #5444), not this PR. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7081 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `ca9407394`.
blawesom commented 2026-10-17 18:57:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T18:57:26Z.


Pre-review verification found a link failure — the handoff branch builds, but patched_libraries fails on it: libSceAmpr.prx references SonyIgnoreHandlerS (mangled _Z18SonyIgnoreHandlerSPKcPvi, defined in libc's SonyCrt.cpp) but cannot resolve it — the handler is defined in libc's build (core/libs/prx/libc/CMakeLists.txt compiles SonyCrt.cpp) and SceAmpr/Export.cpp calls it at line 1044, but nothing exports it across the prx boundary. Everything else passes (full suite green apart from the known main regression agc_driver_tessellation, Mesa #5444); the handoff branch is still pushed for the rebase value, with this caveat: the shared CRT handler needs an export (or an inlined copy) in libSceAmpr before it links. Details in the rebase comment above.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6239045447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081#issuecomment-6239045447). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T18:57:26Z. --- **Pre-review verification found a link failure** — the handoff branch builds, but `patched_libraries` fails on it: `libSceAmpr.prx` references `SonyIgnoreHandlerS` (mangled `_Z18SonyIgnoreHandlerSPKcPvi`, defined in libc's `SonyCrt.cpp`) but cannot resolve it — the handler is defined in libc's build (`core/libs/prx/libc/CMakeLists.txt` compiles `SonyCrt.cpp`) and `SceAmpr/Export.cpp` calls it at line 1044, but nothing exports it across the prx boundary. Everything else passes (full suite green apart from the known `main` regression `agc_driver_tessellation`, Mesa #5444); the handoff branch is still pushed for the rebase value, with this caveat: the shared CRT handler needs an export (or an inlined copy) in libSceAmpr before it links. Details in the rebase comment above.
Pouare514 commented 2026-10-18 00:48:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-18T00:48:44Z.


Handoff picked up, thanks @blawesom - branch reset to ca9407394 (authorship intact) plus one fix commit on top, and thanks for the Linux verification.

fopen_s errno verdict, checked hunk by hunk: the constraint-handler invocations are kept as intended (null result/filename/mode invoke the handler and return EINVAL 22, per Annex K which returns the passed error - matches GuestFiopen/GuestCrt). One correction to the handoff: the open-failure path returns the guest-translated errno, not a hardcoded ENOENT - that is main's newer behavior (GuestErrnoFromHost in fopen_nid_postfix, debt note included) and GuestFopenErrors requires it for the ELOOP/ENAMETOOLONG/EISDIR cases, where a hardcoded 2 would fail. I also removed the now-unused GuestEnoent constexpr the merge left behind.

Link failure fixed via extern C _nid_no_patch, the same mechanism as AddPathProvider_nid_no_patch: the four Sony CRT entry points Ampr calls (SonyLocaleconv, SonySetConstraintHandlerS, SonyIgnoreHandlerS, and SonyInvokeConstraintHandler via the inlined SonyWcscpyS) are now published under literal _nid_no_patch names, so the NID patcher leaves them verbatim and the patched libSceAmpr.prx resolves them from libc.prx. Root cause was that plain C++ names get NID-hashed in libc's exports while Ampr still imported the mangled names. Handler slot and lconv storage stay single-definition in libc SonyCrt.cpp - no inlined copy, shared state intact.

Verified (Release, Ninja, BUILD_TESTING=ON, Windows): full patched-libs deploy green - patched_libraries passes (previously libSceAmpr.prx failed with error 127); unit gate green: guest_crt, guest_fiopen, guest_locale, guest_ampr_markers, guest_wide_strings, plus guest_safe_errors, guest_wide_conversion, guest_amm_placement, guest_ampr_threads, guest_ampr_cpp_error, guest_rune_locale, guest_collate, guest_errors, guest_posix_scalar_io. git diff --check and tools/check_conventions.py --base origin/main --head HEAD clean.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243895829 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081#issuecomment-6243895829). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-18T00:48:44Z. --- Handoff picked up, thanks @blawesom - branch reset to ca9407394 (authorship intact) plus one fix commit on top, and thanks for the Linux verification. fopen_s errno verdict, checked hunk by hunk: the constraint-handler invocations are kept as intended (null result/filename/mode invoke the handler and return EINVAL 22, per Annex K which returns the passed error - matches GuestFiopen/GuestCrt). One correction to the handoff: the open-failure path returns the guest-translated errno, not a hardcoded ENOENT - that is main's newer behavior (GuestErrnoFromHost in fopen_nid_postfix, debt note included) and GuestFopenErrors requires it for the ELOOP/ENAMETOOLONG/EISDIR cases, where a hardcoded 2 would fail. I also removed the now-unused GuestEnoent constexpr the merge left behind. Link failure fixed via extern C _nid_no_patch, the same mechanism as AddPathProvider_nid_no_patch: the four Sony CRT entry points Ampr calls (SonyLocaleconv, SonySetConstraintHandlerS, SonyIgnoreHandlerS, and SonyInvokeConstraintHandler via the inlined SonyWcscpyS) are now published under literal _nid_no_patch names, so the NID patcher leaves them verbatim and the patched libSceAmpr.prx resolves them from libc.prx. Root cause was that plain C++ names get NID-hashed in libc's exports while Ampr still imported the mangled names. Handler slot and lconv storage stay single-definition in libc SonyCrt.cpp - no inlined copy, shared state intact. Verified (Release, Ninja, BUILD_TESTING=ON, Windows): full patched-libs deploy green - patched_libraries passes (previously libSceAmpr.prx failed with error 127); unit gate green: guest_crt, guest_fiopen, guest_locale, guest_ampr_markers, guest_wide_strings, plus guest_safe_errors, guest_wide_conversion, guest_amm_placement, guest_ampr_threads, guest_ampr_cpp_error, guest_rune_locale, guest_collate, guest_errors, guest_posix_scalar_io. git diff --check and tools/check_conventions.py --base origin/main --head HEAD clean.
adriadam10 commented 2026-10-19 13:00:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T13:00:05Z.

Review: CHANGES_REQUESTED


Thanks, the shared handler/lconv state across the libc/libSceAmpr boundary is a real gap and the _nid_no_patch exports are the right mechanism. Before this can merge:

  • Conflicts with main (SonyCrt.hpp, FileAndHeap.cpp, docs/dev/debt/libc.md). main changed SonyConstraintHandler to APS5_VABI, moved the slot/mutex/default handler out of the anonymous namespace, and qsort_s (#6707) and strerror_s now call SonyInvokeConstraintHandler / compare against SonyDefaultConstraintHandler. The out-of-line definitions in SonyCrt.cpp (lines 7-44) need the APS5_VABI signature, and strerror_s must keep seeing the same default-handler pointer. Please rebase and re-run guest_crt, guest_qsort, guest_fiopen.
  • Drop SonyLconvVariant::Compact0 / SonyLconvCompact (SceTypes.hpp:36, SonyCrt.cpp:76-106, GuestCrt.cpp:94-105). No export uses it; only the test does, and the debt note itself says the layout is unverified. An unreferenced, unverified layout is dead code. Re-add it in the PR that wires a title-backed export to it, with the evidence.
  • GuestCrt.cpp:275: the abort check re-launches argv[0] through std::system with a hand-quoted path; that is shell-dependent on Windows. Prefer CreateProcess/fork-free alternatives already used by other guest tests, or assert the default handler pointer instead of spawning.

The CI on this head only covers Conventions/relinker; the full Linux + Windows Build will run in the merge queue after the rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5571898734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7081#pullrequestreview-5571898734). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T13:00:05Z. **Review:** CHANGES_REQUESTED --- Thanks, the shared handler/`lconv` state across the libc/libSceAmpr boundary is a real gap and the `_nid_no_patch` exports are the right mechanism. Before this can merge: - **Conflicts with `main`** (`SonyCrt.hpp`, `FileAndHeap.cpp`, `docs/dev/debt/libc.md`). `main` changed `SonyConstraintHandler` to `APS5_VABI`, moved the slot/mutex/default handler out of the anonymous namespace, and `qsort_s` (#6707) and `strerror_s` now call `SonyInvokeConstraintHandler` / compare against `SonyDefaultConstraintHandler`. The out-of-line definitions in `SonyCrt.cpp` (lines 7-44) need the `APS5_VABI` signature, and `strerror_s` must keep seeing the same default-handler pointer. Please rebase and re-run `guest_crt`, `guest_qsort`, `guest_fiopen`. - **Drop `SonyLconvVariant::Compact0` / `SonyLconvCompact`** (`SceTypes.hpp:36`, `SonyCrt.cpp:76-106`, `GuestCrt.cpp:94-105`). No export uses it; only the test does, and the debt note itself says the layout is unverified. An unreferenced, unverified layout is dead code. Re-add it in the PR that wires a title-backed export to it, with the evidence. - **`GuestCrt.cpp:275`**: the abort check re-launches `argv[0]` through `std::system` with a hand-quoted path; that is shell-dependent on Windows. Prefer `CreateProcess`/`fork`-free alternatives already used by other guest tests, or assert the default handler pointer instead of spawning. The CI on this head only covers Conventions/relinker; the full Linux + Windows Build will run in the merge queue after the rebase.

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