feat(libc,libSceLibcInternal): wire the ctype<char> facet vtable #21537
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21537"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Herolias on 2026-11-16T11:15:55Z.
What
libc exports the
ctype<char>vtable (_ZTVSt5ctypeIcE, NIDGKWcAz6-G7k) as fourteenuintptr_tslots, and onmainall fourteen are zero. This change fills slots 2 to 13. To do that, libc gains the fourctype<char>case maps and the two destructors that the console'slibc.prxexports, and libSceLibcInternal's exports of the same NIDs forward to them.How code reaches the vtable: Dinkumware defines
ctype<char>::_Getcatinside the class (crt.vc12xlocaleline 2468), and no module exports it (its NIDSlxGzFV6+04is not in ps5rslibc.txt). Souse_facet<ctype<char>>constructs the facet in the caller's own code, on the exported vtable. libc's classic locale holds no facets (g_classicFacets[1]{}), so that path is taken.use_facetthen calls_Increfon the new facet (microsoft/STLxlocaleline 453), which is slot 4, a zero onmain.Which titles this serves: the executables of Minecraft Legends (PPSA05510, #268), Deathloop (PPSA01670, #272), Dreaming Sarah (PPSA02929, #265) and Astro Bot (PPSA21564, #271) import
ctype<char>::id(Cv+zC4EjGMA), which onlyuse_facet<ctype<char>>andhas_facetread. However, bundled modules come before AnyPS5's libraries when an import is looked up (ARCHITECTURE.md, Overview). Deathloop (#11484), Dreaming Sarah (#3018) and Astro Bot (#357) ship their ownlibc.prx, so theirctype<char>imports go to that module, not to this code. This change matters where actype<char>import reaches AnyPS5's libc: a title without a bundledlibc.prx, or one whose copy does not export the symbol. It also matters for imports of the libSceLibcInternal NIDs, which try the host modules first. I know of no title in either situation that callsuse_facet<ctype<char>>, so this PR claims no unblocked title. It completes the exported vtable from sourced code, as #21170 did formessages<char>.RetainFacetandReleaseFacet(the_Incref/_Decrefof libc's other facets), and thendo_tolower,do_tolower(range),do_toupper,do_toupper(range),do_widen,do_widen(range),do_narrowanddo_narrow(range). That is the declaration order in Dinkumware'sctype<char>(microsoft/STLxlocalelines 2777-2843, crt.vc12xlocalelines 2492-2566), and the orderGuestLocale::CtypeVtablealready records forwidenandnarrow. Offset-to-top and typeinfo stay zero, as in thecollate,messagesandnum_putvtables. The destructor and case-map slots point at libc'sCtype*_nid_no_patchbodies (below), as themessagesvtable points at libc's own functions rather than at exports.do_toloweranddo_toupper(WXOoCK+kqwY,2w+4Mo2DPro,mnq3tbhCs34,7glioH0t9HM): the console'slibc.prxexports them (ps5rslibc.txt), but onmainonly libSceLibcInternal had them (#19547). Among AnyPS5's libraries, alibc.prximport is looked up only in libc (WindowsEntryStubBuilder.cpplines 180-215: other modules fall back to libc, not the reverse). I moved the code fromlibSceLibcInternal/CtypeCase.cppinto libc'sLocaleSupport.cppwithout changing its behaviour. Only the helpers were renamed, toCtypeFieldAddress,MapCtypeCharacterandMapCtypeRange, andstd::bit_cast<char>becamestatic_cast<char>. The bodies live inCtypeDoTolower_nid_no_patch,CtypeDoTolowerRange_nid_no_patch,CtypeDoToupper_nid_no_patchandCtypeDoToupperRange_nid_no_patch(declared inGuestLocale.hpp), and the_nid_postfixexports call them.tPsGA6EzNKAcomplete,p8-44cVeO84deleting): these follow~ctypeand_Tidyin both sources (microsoft/STL lines 2777-2793, crt.vc12 lines 2492-2510). The classification table goes tofreewhendeleteTable(_Delfl) is positive and todelete[]when it is negative; a zero leaves it alone. The deleting destructor then frees the facet withoperator delete. The bodies areCtypeDestroy_nid_no_patchandCtypeDelete_nid_no_patch, which the two exports call. The table and flag are theclassificationanddeleteTablefields ofGuestLocale::CtypeFacet, from #19547. Both sources also free_Ctype._LocaleName, a Windows field that this layout has no room for, so that step is left out. The lower and upper tables are never freed, because neither source frees them.CtypeCase.cppnow holds six forwarders exported by NID, the patternNumpunct.cppuses. They call libc's six_nid_no_patchbodies, not its exports, as6c5bee0282(#13730) madeNumpunct.cppdo: after NID patching, a forwarder that imports libc's export imports the NID it also exports, and can bind to itself.patched_self_imports(core/libs/tests/PatchedSelfImports.cpp, also from #13730) failed for all six NIDs when this branch's first two commits were merged withmain, and passes with the third. The two destructor NIDs were whole-bodyNotImplemented_nid_no_patchinventory stubs (libSceLibcInternalInventory_064and_065). They are removed fromInventoryExports.cpp/.hpp, as8e6b7e1524did for the entries it implemented.What still throws, and why:
std::invalid_argument, where the console would fault. This is the same host-safety policy as the facet base'sDeleteCollate.std::invalid_argumentthrows for unusable memory, null tables and inverted ranges.NotImplemented_nid_no_patch.Debt:
docs/dev/debt/changes/feat-ctype-char-vtable.md.libc.mdit changes thectype<char> vtableentry and thectype<char> do_widen and do_narrowentry, which said the vtable stays zeroed.libc.mdas "ctype do_tolower and do_toupper", text unchanged apart from the link and the facet-construction caveat this PR resolves.libc.md.libSceLibcInternal.mdit removes the_ZNSt5ctypeIcED0Ev/D1Evrows from "Additional callable exports" and adds a Functional entry for the forwarders.Overlap:
tools/pr_overlap.py --namesfor the 17ctype<char>andnumpunct<char>names found that:ctype<char>::table_sizeand_ZTSSt5ctypeIcE, so both stay out of this PR;do_toupperstub (mnq3tbhCs34) inInventoryExports2.cpp, which #19547 already replaced onmain. This branch does not change that file, so #17300's conflict there is the same againstmainand against this branch;ctype<char>members.git log -Gover the fetched open-PR heads found no other change to the destructor or case-map names.libSceLibcInternalInventory_064/_065to_ZNSt5ctypeIcED0Ev/D1Evstubs.pr_overlap --branchlists it under "Same exports" because those stubs carry the NIDs the new forwarders export. #17300 already conflicts withmainin 8 places inInventoryExports.cpp. With this branch it has one more there, plus a wider one inInventoryExports.hpp. On a rebase it would drop those two renamed stubs.tools/pr_overlap.py --branchexits 2 because 11 PRs that share files already conflict withmain. On10d0f25e12I rangit merge-treeof each againstmain(a7b44b7125) and against this branch and compared the conflict hunks in every file both change:LocaleSupport.cpp,GuestLocale.cpp), #11172 and #18135 (LocaleSupport.cpp), #13778 and #14834 (InventoryExports.cpp/.hpp) have identical hunks.Api.hpp) and #18135 (Api.hpp,GuestLocale.hpp) merge cleanly in these files both ways.numpunct<char>bodies that libSceLibcInternal forwards to into_nid_no_patchfunctions. This branch follows it forctype<char>and adds its six declarations after #13730's inGuestLocale.hpp.num_put<char>andcodecvt<char>vtables further down the same block ofLocaleSupport.cpp. Unchanged lines separate the hunks.Not in this PR:
ctype<char>'s typeinfo_ZTISt5ctypeIcE, whichmaindoes not export; slot 1 stays zero.ctype<char>::table_sizeand_ZTSSt5ctypeIcE(#13449).ctype<wchar_t>vtable (qdHsu+gIxRo, 18 zeroed slots). PPSA02929 importsctype<wchar_t>::id. Its virtuals are split: libc has the classification anddo_toupper(#17836), and libSceLibcInternal hasdo_tolower,do_widenanddo_narrow. Its destructor would need the wide facet's layout, which no source here gives.numpunct<char>vtable. libc's_Getcatinstalls its own vtable, and the exportednumpunct<char>constructors are absent, so nothing builds a facet on the exported object yet.Tested
Fedora 44 x86-64, GCC 16.2.1, GNU ld 2.46.1, CMake 4.3.0, Ninja 1.13.2, Release with
-DBUILD_TESTING=ON. The branch mergesmainata7b44b7125(10d0f25e12), and the results below are for that merge, unless they name another commit.10d0f25e12only mergesmain: no Build run started forf5531db33d, and a push is the only way to start one. I ran no title, homebrew or console, because I have none. Nothing was run on Windows or macOS; the MinGW compile check is below.cmake --build buildbuilt the whole tree.The selection from
tools/select_tests.py(BUILD.md, "Selective test builds") was 5732 tests. 5668 passed, 62 skipped themselves and 2 failed:agc_driver_image_table_packed_guards("invalid format" write-backs) andguest_http. Both also fail withmain's (a7b44b7125) copies of this branch's nine source files in place, which I checked out, rebuilt the tests against, ran and then restored. This branch touches neither AGC nor libSceHttp.patched_self_importsfailed on the merge beforef5531db33d, once for each of the six NIDs ("libSceLibcInternal.prx imports WXOoCK+kqwY, a name it also exports, so the import can bind to its own export"), and passes onf5531db33dand on10d0f25e12.guest_localechecks that the exportedctype<char>vtable is 112 bytes, that slots 0 and 1 are zero and that slots 2 to 13 are set. It then calls every slot on a facet built from the classic tables:tolower,toupperand their range forms on "Mixed Case 42!";widen,narrowand their range forms;operator new, and a nullthis, which throws.Like the
collate,messagesandnum_putvtable checks in the same file, it does not compare slots with&functiontaken in the test. On Windows,libc.prxis a DLL, and there the test's&functionis the executable's import thunk, not the address stored in the vtable. A MinGW DLL and executable run under wine showed the two differ. The calls tell every slot apart except the single-characterdo_widenanddo_narrow(slots 10 and 12). Both return the character unchanged, so their order is not tested.guest_ctype_caseruns all of its case-conversion checks through libc's exports and through libSceLibcInternal's. It registers a recording heap and checks the destructors through both libraries' exports and through slots 2 and 3 of the exported vtable:INT32_MAX, then -1 andINT32_MIN, release the classification table and nothing else;loweranduppertables are never released;std::invalid_argument.libc's
free,operator deleteandoperator delete[]all release through the application heap, so the test sees which pointers are released but cannot tell whetherfreeordelete[]released them.guest_libc_internal_ctype(unchanged) passes.Negative controls, on
f5531db33d: I made 18 one-line changes, rebuilt the three tests, ran them and restored the files. Each change made at least one test fail:tolowerandtoupperslots swapped, destructor slots swapped, the retain slot empty, thenarrowrange slot empty, and the whole vtable left zeroed as onmain;lowertable freed, the deleting destructor keeping the facet, and the deleting destructor skipping the table;tolowerreading theuppertable;tolowerforwarding totoupper, thetoupperrange to thetolowerrange, the complete destructor deleting, and the deleting destructor only destroying;do_tolowermapping throughtoupper, the complete destructor deleting, and the deleting destructor only destroying.With the files restored, the three tests pass.
Windows:
70ba5d5e95, failed to compileGuestCtypeCase.cpp. That file includeswindows.hon Windows, and its recording heap'sHeapFreecollided with the Win32 function, so it is nowHeapRelease.34bdcbb151merged withmainate4caa07950, passedguest_locale,guest_ctype_caseandguest_libc_internal_ctype. It failed onlyguest_multibyte_locale("wcstombs fills the capacity mid-character"), which #21244 broke onmainand #21567 reverted (cc1c20e0dd) after that run had started.x86_64-w64-mingw32-g++16.2.1 with UCRT headers (-D_UCRT -D__MSVCRT_VERSION__=0xE00 -fsyntax-only) reproduces the first check's error on70ba5d5e95, and compiles the five changed sources on10d0f25e12. That is not the WinLibs toolchain CI uses, and nothing was linked or run on Windows.tools/check_conventions.py --base origin/mainreports "All conventions checks passed."tools/debt_fold.py --checkfails onmainata7b44b7125, onmain's owndocs-nkwebkit-drop-readded-count-lines.mdline 5. In agit archivecopy of this branch without that one fragment, it passes with this branch's fragment.tools/pr_overlap.py --branch feat/ctype-char-vtable: see Overlap above.Sources
_Incref/_Decreffirstst_size112 measured on PPSA16833'slibc.prx, recorded in thelibc.md"ctype vtable" entry (#9735)stl/inc/xlocalelines 2777-2843; ganboing/crt.vc12 f1d47126bfsrc/xlocalelines 2492-2566freefor a positive flag anddelete[]for a negative one; the deleting destructor then frees the facet~ctypeand_Tidy: microsoft/STL lines 2777-2793, crt.vc12 lines 2492-2510classification,lower,upperanddeleteTableGuestLocale::CtypeFacetincore/libs/prx/libc/include/GuestLocale.hpp, from #19547 (SDK 10.00 headers)do_tolower/do_toupperbehaviour (moved unchanged)libc.prxdisassembly)_Getcatis defined in the class, so the guest builds the facetxlocalelines 2468-2474; NIDSlxGzFV6+04is absent from ps5rslibc.txtuse_facetregisters and_Increfs a lazily built facetxlocalelines 427-461do_tolower,do_toupper, both destructors, the vtable andiddata/stubs/by_library/libc.txtlines 242, 465, 703, 860, 1573, 2346, 2443, 2630libSceLibcInternal.clines 3119-3140, 6521, 6528_nid_no_patchbodies, because a patched forwarder that imports the NID it exports can bind to itself6c5bee0282(#13730), "fix(prx): keep forwarding exports from binding to themselves on Linux", and itscore/libs/tests/PatchedSelfImports.cpplibc.prximport resolves only in libc; bundled modules are tried first, except forlibSceLibcInternal.prximportscore/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpplines 180-215;docs/dev/ARCHITECTURE.mdline 47ctype<char>::id; PPSA01670, PPSA02929 and PPSA21564 shiplibc.prxChecklist
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)Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T11:22:40Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
70ba5d5e95:tools/check_conventions.pypassed;git diff --checkclean.guest_ctype_case_tests,guest_locale_testsbuilt cleanly.guest_locale,guest_ctype_case):core/libs/prx/libSceLibcInternal/CtypeCase.cpp,core/libs/prx/libSceLibcInternal/InventoryExports.cpp,core/libs/prx/libSceLibcInternal/include/Ctype.hpp,core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp,core/libs/prx/libc/include/Api.hpp,core/libs/prx/libc/src/LocaleSupport.cppreverted tomain, the new tests do not build/link without the change; restoring it makes them build and pass.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T12:22:24Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
34bdcbb151:tools/check_conventions.pypassed;git diff --checkclean.guest_ctype_case_tests,guest_locale_testsbuilt cleanly.guest_locale,guest_ctype_case):core/libs/prx/libSceLibcInternal/CtypeCase.cpp,core/libs/prx/libSceLibcInternal/InventoryExports.cpp,core/libs/prx/libSceLibcInternal/include/Ctype.hpp,core/libs/prx/libSceLibcInternal/include/InventoryExports.hpp,core/libs/prx/libc/include/Api.hpp,core/libs/prx/libc/src/LocaleSupport.cppreverted tomain, the new tests do not build/link without the change; restoring it makes them build and pass.Looks Good To Me