Skip to content

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

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
sherySJ:agent/anyps5-sonycrt-redo
Nov 11, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
sherySJ:agent/anyps5-sonycrt-redo

Conversation

@sherySJ

@sherySJ sherySJ commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What

Redo of #7081 by @Pouare514 on current main, carrying their commit with authorship kept (rebased onto main with the conflicts resolved) plus a second commit with the tests and the debt cut, which has a Co-authored-by trailer. #7081 was closed as stale; this follows adriadam10's note there and my offer to carry it.

A handler set through libc is not seen by libSceAmpr, and lconv is stored once per module. On main, SonyConstraintMutex, SonyConstraintHandlerSlot, SonyDefaultConstraintHandler, SonySetConstraintHandlerS, SonyIgnoreHandlerS, SonyInvokeConstraintHandler and SonyLocaleconv are inline functions in core/libs/prx/libc/include/SonyCrt.hpp, so every module that includes it gets its own slot and mutex where the image format does not merge inline statics (PE), and SonyLocaleconv sits in the anonymous namespace, so even on Linux sceAmprLocaleconv and localeconv return two different tables.

  • core/libs/prx/libc/src/SonyCrt.cpp (new, in libc) holds the one definition of the handler slot, its mutex, the default handler, set_constraint_handler_s, ignore_handler_s, the invoke path and the lconv table.
  • The four entry points libSceAmpr needs are published as extern "C" SonyInvokeConstraintHandler_nid_no_patch, SonySetConstraintHandlerS_nid_no_patch, SonyIgnoreHandlerS_nid_no_patch and SonyLocaleconv_nid_no_patch, the mechanism fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081 settled on (plain C++ names are NID-hashed in libc.prx's exports, _nid_no_patch names are left verbatim). SonyInvokeConstraintHandler stays an inline wrapper, so the _s helpers in the header and every libc caller are unchanged.
  • libSceAmpr (sceAmprLocaleconv, sceAmprSetConstraintHandlerS, sceAmprIgnoreHandlerS) and libc (localeconv, set_constraint_handler_s, ignore_handler_s) call the shared definitions. libSceAmpr already links libc.
  • docs/dev/debt/libc.md: the set_constraint_handler_s entry loses "Each module keeps its own handler state, so a handler set through one library is not seen by the other", the gap this closes.

Changed relative to #7081 because of what main did since:

  • The default handler keeps main's body: it writes the message and a newline to the guest stderr and returns. fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081's out-of-line copy called std::abort(). The abort-check test (a child process started through std::system, which the review flagged as shell dependent on Windows) and the GuestFiopen change that installed the ignore handler because the default aborted are dropped; nothing aborts any more.
  • fopen_s is not touched: main already calls the handler with the module's messages and a NULL pointer, so fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081's SonyInvokeConstraintHandler("fopen_s", result, ...) edits are gone.
  • SonyLconvVariant::Compact0 and SonyLconvCompact are not part of this PR (the review asked to drop them: no export uses them and the layout is unverified), so SceTypes.hpp is untouched.
  • The __stderrp_nid_postfix declaration stays in SonyCrt.hpp because other tests rely on it through that header.
  • The anonymous namespace in SonyCrt.hpp stays for the pure helpers; only the stateful pieces moved out.
  • The guest_crt cross-module checks are rewritten on main's GuestCrt.cpp (below), and the counting handler is declared APS5_VABI, the convention it is installed with.

Tested

Linux (Arch, GCC, Ninja, Release, BUILD_TESTING=ON), this branch on top of 0f147e329f:

  • guest_crt, guest_ampr_markers, guest_ampr_div_and_buffer_size, guest_ampr_threads, guest_ampr_cpp_error, guest_fiopen, guest_locale, guest_safe_errors, guest_wide_strings and guest_wide_conversion pass (10/10, ctest --test-dir build -R '^(...)$').
  • nid_patcher run by hand on this branch's libc.prx and libSceAmpr.prx as core/libs/CMakeLists.txt does (--write-export-list, then --preserve-export-list): libc.prx exports the four Sony*_nid_no_patch symbols and libSceAmpr.prx imports the same four verbatim, no mangled Sony* import left. The full patched_libraries target was not run.
  • python3 tools/check_conventions.py --base origin/main: all checks passed.

New checks in guest_crt (no function-pointer identity across a module boundary, no paths, no errno values beyond EINVAL/ERANGE): sceAmprLocaleconv() equals localeconv(); a handler installed with set_constraint_handler_s is called by sceAmprWcscpyS, one installed with sceAmprSetConstraintHandlerS by wcscpy_s and fopen_s(NULL, ...); the ignore handler installed from libc silences sceAmprWcscpyS; a reset to the default from either side is seen by the other (the previous-handler values returned by each side are compared with each other, not with &export); and with the default installed a violation raised through either module returns instead of aborting.

Negative control, the same new guest_crt against main's sources (SonyCrt.hpp, Export.cpp, LocaleSupport.cpp, WideConversion.cpp, libc/CMakeLists.txt, no SonyCrt.cpp):

  • lconv, plain configuration: fails.
    guest_crt: line 47: locale != nullptr && locale == localeconv_nid_postfix()
    
    With this PR the same line passes.
  • Handler: on Linux the inline slot is a unique ELF symbol, so both modules happen to share it and main passes this part. To reproduce what a PE build does (each DLL keeps its own copy of the inline statics) I built libSceAmpr with -fno-gnu-unique, linked it with -Bsymbolic and a version script keeping its _Z*Sony* symbols local, on both main and this branch. On main with that setup:
    wcscpy_s: buffer too small
    guest_crt: line 162: g_handlerCalls == 1
    
    (wcscpy_s called through libc does not see the counting handler installed through libSceAmpr.) With this PR the handler is a function in libc that libSceAmpr calls, so that setup does not duplicate anything, and guest_crt passes. The flags were local to the control and are not in the diff.

Windows: @JJenkx ran head 6c827bf98f merged onto main 8eb293447e on Windows 11 (WinLibs GCC 15.2 UCRT, Release): guest_crt passes 3 of 3, none skipped (comment). @gabichuuuuu ran the same head merged onto main c2ea254d0d (Windows 11, WinLibs GCC 15.2 x86_64-ucrt-posix-seh, Release): the 10 tests above and guest_mktime, guest_time_conversion, guest_time_extras, guest_time_return pass 3 of 3; with main's SonyCrt.hpp, Ampr export, locale, wide conversion and CMake files and this PR's GuestCrt.cpp, guest_crt fails 3 of 3 at the locale check (line 47) and, past it, at the shared handler check (line 162), so both parts of the fix are needed on PE as well (review). macOS: not tested.

Overlap

Base 0f147e329f. The claim check on this head lists #12574 (SonyCrt.hpp, docs/dev/debt/libc.md; git merge-tree against its head conflicts in both files, its base is older than main) and #12592 (SonyCrt.hpp, merges cleanly). #12574 also makes SonyConstraintMutex and SonyConstraintHandlerSlot hidden on ELF; after this PR they are no longer inline, so that hunk goes away. #12315 is a draft that also conflicts with main and is not an overlap in intent.

Checklist

Pouare514 and others added 2 commits November 8, 2026 04:45
…libSceAmpr

A handler set through libc's set_constraint_handler_s is now checked from libSceAmpr's wcscpy_s and the other way round, a reset to the default from either side is seen by both, and localeconv returns the same table from both. The default handler is also checked to return from a violation raised across the module boundary. The counting handler gets the guest calling convention it is installed with.

Co-authored-by: Pouare514 <julienaulycee@gmail.com>
@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

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
PR 12574 core/libs/prx/libc/include/SonyCrt.hpp L30-45
docs/dev/debt/libc.md L124

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7447 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7544 docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8150 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8965 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 9087 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9466 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 9526 core/libs/prx/libc/src/LocaleSupport.cpp
core/libs/tests/GuestCrt.cpp
docs/dev/debt/libc.md
PR 9584 core/libs/prx/libc/CMakeLists.txt
PR 10409 docs/dev/debt/libc.md
PR 10780 docs/dev/debt/libc.md
PR 11109 docs/dev/debt/libc.md
PR 11172 core/libs/prx/libc/src/LocaleSupport.cpp
docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11634 docs/dev/debt/libc.md
PR 11661 docs/dev/debt/libc.md
PR 11689 docs/dev/debt/libc.md
PR 11698 docs/dev/debt/libc.md
PR 11726 docs/dev/debt/libc.md
PR 12682 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12688 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12700 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12717 core/libs/prx/libc/CMakeLists.txt
PR 12849 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12965 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 13449 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 13794 docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md
PR 17259 docs/dev/debt/libc.md

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

Progress report (0f147e3 → 6c827bf)

📈 System libraries: 32.02% (+0.0018%, +1 implemented functions)

✅ 1 implemented
Library Name
libc SonyDefaultConstraintHandler

Changed libraries (implemented exports)

Library Before After Change
libc 1674/1694 (98.82%) 1675/1695 (98.82%) +0.0007%

@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 6c827bf98f, merged onto main 8eb293447e (4d6b2863a0, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_crt passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@gabichuuuuu gabichuuuuu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Windows run of head 6c827bf98f, merged onto main c2ea254d0d (clean merge). On Windows each DLL really does keep its own copy of the inline state, so no -fno-gnu-unique setup is needed here.

Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, full build with BUILD_TESTING=ON.

  • Your 10 tests (guest_crt, guest_ampr_markers, guest_ampr_div_and_buffer_size, guest_ampr_threads, guest_ampr_cpp_error, guest_fiopen, guest_locale, guest_safe_errors, guest_wide_strings, guest_wide_conversion): 10 / 10, 3 runs.
  • The other tests that include SonyCrt.hpp also pass: guest_mktime, guest_time_conversion, guest_time_extras, guest_time_return.

Negative control, with main's SonyCrt.hpp, libSceAmpr/Export.cpp, LocaleSupport.cpp, WideConversion.cpp and libc/CMakeLists.txt, without SonyCrt.cpp, and with this PR's GuestCrt.cpp:

  • guest_crt fails 3 / 3 at line 47: locale != nullptr && locale == localeconv_nid_postfix(). The other 9 pass.
  • With line 47 reduced to locale != nullptr to get past it (local only), it fails 3 / 3 at line 162: g_handlerCalls == 1, after wcscpy_s: buffer too small. On Windows, main keeps one handler slot per DLL: a handler installed through libSceAmpr is not seen by libc's wcscpy_s.

So both parts of the fix work on PE without any setup. From reading the code, SonyConstraintMutex, SonyConstraintHandlerSlot and SonyDefaultConstraintHandler are now declared in the shared header but defined only in libc, and only libc uses them today, so nothing breaks.

@github-actions github-actions Bot added review queue Next in the maintainer review queue: checks pass, no open change request, no conflict and removed review queue Next in the maintainer review queue: checks pass, no open change request, no conflict labels Nov 8, 2026
@DonRyuzaki DonRyuzaki mentioned this pull request Nov 11, 2026
12 tasks done
@adriadam10 adriadam10 self-assigned this Nov 11, 2026
@oneandonlydean
oneandonlydean merged commit b390b11 into boykopovar:main Nov 11, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs libc prx unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants