Repository navigation
fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr - #16933
Conversation
…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>
|
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.
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.
|
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
gabichuuuuu
left a comment
There was a problem hiding this comment.
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.hppalso 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_crtfails 3 / 3 atline 47: locale != nullptr && locale == localeconv_nid_postfix(). The other 9 pass.- With line 47 reduced to
locale != nullptrto get past it (local only), it fails 3 / 3 atline 162: g_handlerCalls == 1, afterwcscpy_s: buffer too small. On Windows,mainkeeps one handler slot per DLL: a handler installed throughlibSceAmpris not seen bylibc'swcscpy_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.
What
Redo of #7081 by @Pouare514 on current
main, carrying their commit with authorship kept (rebased ontomainwith the conflicts resolved) plus a second commit with the tests and the debt cut, which has aCo-authored-bytrailer. #7081 was closed as stale; this follows adriadam10's note there and my offer to carry it.A handler set through
libcis not seen bylibSceAmpr, andlconvis stored once per module. Onmain,SonyConstraintMutex,SonyConstraintHandlerSlot,SonyDefaultConstraintHandler,SonySetConstraintHandlerS,SonyIgnoreHandlerS,SonyInvokeConstraintHandlerandSonyLocaleconvare inline functions incore/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), andSonyLocaleconvsits in the anonymous namespace, so even on LinuxsceAmprLocaleconvandlocaleconvreturn two different tables.core/libs/prx/libc/src/SonyCrt.cpp(new, inlibc) holds the one definition of the handler slot, its mutex, the default handler,set_constraint_handler_s,ignore_handler_s, the invoke path and thelconvtable.libSceAmprneeds are published asextern "C"SonyInvokeConstraintHandler_nid_no_patch,SonySetConstraintHandlerS_nid_no_patch,SonyIgnoreHandlerS_nid_no_patchandSonyLocaleconv_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 inlibc.prx's exports,_nid_no_patchnames are left verbatim).SonyInvokeConstraintHandlerstays an inline wrapper, so the_shelpers in the header and everylibccaller are unchanged.libSceAmpr(sceAmprLocaleconv,sceAmprSetConstraintHandlerS,sceAmprIgnoreHandlerS) andlibc(localeconv,set_constraint_handler_s,ignore_handler_s) call the shared definitions.libSceAmpralready linkslibc.docs/dev/debt/libc.md: theset_constraint_handler_sentry 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
maindid since:main's body: it writes the message and a newline to the gueststderrand returns. fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081's out-of-line copy calledstd::abort(). The abort-check test (a child process started throughstd::system, which the review flagged as shell dependent on Windows) and theGuestFiopenchange that installed the ignore handler because the default aborted are dropped; nothing aborts any more.fopen_sis not touched:mainalready 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'sSonyInvokeConstraintHandler("fopen_s", result, ...)edits are gone.SonyLconvVariant::Compact0andSonyLconvCompactare not part of this PR (the review asked to drop them: no export uses them and the layout is unverified), soSceTypes.hppis untouched.__stderrp_nid_postfixdeclaration stays inSonyCrt.hppbecause other tests rely on it through that header.SonyCrt.hppstays for the pure helpers; only the stateful pieces moved out.guest_crtcross-module checks are rewritten onmain'sGuestCrt.cpp(below), and the counting handler is declaredAPS5_VABI, the convention it is installed with.Tested
Linux (Arch, GCC, Ninja, Release,
BUILD_TESTING=ON), this branch on top of0f147e329f: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_stringsandguest_wide_conversionpass (10/10,ctest --test-dir build -R '^(...)$').nid_patcherrun by hand on this branch'slibc.prxandlibSceAmpr.prxascore/libs/CMakeLists.txtdoes (--write-export-list, then--preserve-export-list):libc.prxexports the fourSony*_nid_no_patchsymbols andlibSceAmpr.prximports the same four verbatim, no mangledSony*import left. The fullpatched_librariestarget 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, noerrnovalues beyondEINVAL/ERANGE):sceAmprLocaleconv()equalslocaleconv(); a handler installed withset_constraint_handler_sis called bysceAmprWcscpyS, one installed withsceAmprSetConstraintHandlerSbywcscpy_sandfopen_s(NULL, ...); the ignore handler installed fromlibcsilencessceAmprWcscpyS; 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_crtagainstmain's sources (SonyCrt.hpp,Export.cpp,LocaleSupport.cpp,WideConversion.cpp,libc/CMakeLists.txt, noSonyCrt.cpp):lconv, plain configuration: fails.mainpasses this part. To reproduce what a PE build does (each DLL keeps its own copy of the inline statics) I builtlibSceAmprwith-fno-gnu-unique, linked it with-Bsymbolicand a version script keeping its_Z*Sony*symbols local, on bothmainand this branch. Onmainwith that setup:wcscpy_scalled throughlibcdoes not see the counting handler installed throughlibSceAmpr.) With this PR the handler is a function inlibcthatlibSceAmprcalls, so that setup does not duplicate anything, andguest_crtpasses. The flags were local to the control and are not in the diff.Windows: @JJenkx ran head
6c827bf98fmerged ontomain8eb293447eon Windows 11 (WinLibs GCC 15.2 UCRT, Release):guest_crtpasses 3 of 3, none skipped (comment). @gabichuuuuu ran the same head merged ontomainc2ea254d0d(Windows 11, WinLibs GCC 15.2x86_64-ucrt-posix-seh, Release): the 10 tests above andguest_mktime,guest_time_conversion,guest_time_extras,guest_time_returnpass 3 of 3; withmain'sSonyCrt.hpp, Ampr export, locale, wide conversion and CMake files and this PR'sGuestCrt.cpp,guest_crtfails 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-treeagainst its head conflicts in both files, its base is older thanmain) and #12592 (SonyCrt.hpp, merges cleanly). #12574 also makesSonyConstraintMutexandSonyConstraintHandlerSlothidden on ELF; after this PR they are no longer inline, so that hunk goes away. #12315 is a draft that also conflicts withmainand is not an overlap in intent.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)localeconvreuse (SymphonyStation5/Cloud Play same NID; the PPSA99902 compact variant of fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081 is not in this PR) and the feat(libc): implement qsort_s #6707qsort_srebase, as named in fix(libs): share Sony CRT handler and lconv state between libc and libSceAmpr #7081