feat(libSceLibcInternal): implement the char16_t and char32_t codecvt conversions #21252
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21252"
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-16T02:00:33Z.
What
This implements
do_inanddo_outofcodecvt<char16_t,char,_Mbstatet>andcodecvt<char32_t,char,_Mbstatet>, the UTF-8 conversions of the two facets. Onmainall four aresceLibcInternalPending3_0xxstubs inErr.cppthat throw. Each function is a statement-by-statement transcription of microsoft/STL 219514876exlocale. The Dinkumware V6.00 code in crt.vc12 f1d47126bf is the same logic in an older layout. #19593 implemented the same facets' metadata anddo_unshift; this PR adds the conversions on the same layout.char16_tdo_in(+uPwCGfmJHs) decodes UTF-8 into UTF-16:error, as does a bad continuation byte._Maxcodeand U+10FFFF returnserror.do_inrolls back to the start of the sequence.partialeven after earlier sequences were converted, where thechar32_tdo_inreturnsok.char16_tdo_out(0FKwlv9iH1c) encodes UTF-16 as UTF-8:error._Maxcodehere, and neither does this code.char32_tdo_in(PJ2UDX9Tvwg) decodes forms of one to six bytes. It checks_Maxcodeafter the byte order mark check and keeps its state in the first byte. A sequence cut short stops the loop without a rollback.char32_tdo_out(eoW60zcLT8Q) checks_Maxcodebefore encoding and writes forms of one to six bytes._Consume_header(4), a U+FEFF as the first character is dropped.do_inthen converts the rest with a virtual call through the facet's vtable at slot 0x38. If that call returnspartial, the state and the input pointer are rolled back._Generate_header(2),do_outwrites EF BB BF before the first character when there is room for it and that character.char32_tdo_outmarks the state before it checks the room, as both source versions do. When the header does not fit it returnspartial, and a later call writes no header._Maxcodeis the 8-byteunsigned longafter the 0x10-byte facet base, at 0x10._Modeis the 4-byte enum at 0x18, the offset thatCodecvtUtfMetadata.cpponmaintakes from the SDK 10.00 disassembly.char16_tstate is the first two bytes of_Mbstatet, and thechar32_tstate is its first byte. The tests check that the other bytes stay unchanged.Reachability: neither
mainnor this branch exports the vtables of these facets (jlNI3SSF41o,9H2BStEAAMg) or theirids (xM5re58mxj8,u2MAta5SS84), all of which the console's libc exports. A title that imports one of them has an unresolved data import. On Windows the relinked executable then stops at startup withFAIL: unresolved ELF import(WindowsEntryStubBuilder.cppline 293). On Linux,LinuxElfPatcher.cpptraces only function imports (lines 253-256) and leaves data to the system loader, which I did not run. So I expect no title to reach these members through a locale yet. A facet derived in title code and used without alocalecould reach them, but I know of none. None of the ten NIDs is in the title import lists of #265 to #272, though those lists leave out the NIDs that were already known to be missing. This PR claims no unblocked title. #19593 was merged under the same condition.What still throws, and why:
do_lengthof both facets (lynApfiP6Lwandm6rjfL4aMcA). The two Dinkumware versions disagree:xlocalelines 1267-1295 and 1534-1564)._Codecvt_do_length, lines 850-891, called from lines 1162-1165 and 1404-1407).do_unshiftfollows V6.00 where the versions differ (it returnsok, while microsoft/STL'schar32_tdo_unshiftreturnsnoconv). That is a hint, not proof, sodo_lengthstays a throw until a disassembly settles it.Debt:
docs/dev/debt/changes/feat-codecvt-utf-conversions.mdadds a Functional entry for the four members (757 characters), removes the four Pending entries, and changes the twodo_lengthentries to give the reason they throw (564 characters each).Overlap:
tools/pr_overlap.py --namesfor the sixdo_in,do_outanddo_lengthnames found only #17300. That PR renameslibSceLibcInternalexport aliases and implements none of them.tools/pr_overlap.py --branchexits 2. It lists #17300 under same exports, and it cannot compare #13778, #17300, #18135, #19602 and #20059, which already conflict withmain.git merge-tree --write-tree, againstmain(cf058cf14b) and against this branch:Err.cpp(8 instead of 6). In both, #17300 renames thePending3_017,018and024stubs that this branch deletes. Whichever PR lands second drops the renamed stubs for those NIDs and keeps the rest of the rename.libSceLibcInternal.hpp: #13486, #13782, #19434, #19602, #19609 and #20059. Four of them append after the last line and #13782 inserts above it, so the new include goes afterMultiPrecision.hpp.git merge-treemerges this branch with all six without a conflict in that header.Not in this PR:
char16_tandchar32_tcodecvt vtables,ids and typeinfo, which the console's libc exports. Without them these members cannot be reached through a locale.do_lengthof both facets, pending a disassembly of the console'slibc.prx.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, onmainatcf058cf14b. I ran no title, homebrew or console, because I have none. Nothing was built or run on Windows or macOS.cmake --build buildbuilt the whole tree.tools/select_tests.py(BUILD.md, "Selective test builds") was 5602 tests, because libSceLibcInternal is linked into almost every test. 5600 passed and 2 failed:agc_driver_image_table_packed_guardsandguest_http. Both fail the same way onmainatcf058cf14b, which I rebuilt and ran them on, and this branch touches neither AGC nor libSceHttp. 59 tests skipped themselves.guest_codecvt_utf_conversion(new) has 22 checks. They run the exports on a facet whose vtable,_Maxcodeand_Modethe test lays out itself. They check the result, both next pointers, the output and, where it changes, the state. Every call also checks that nothing is written past the end of the output and that both next pointers stay in range:char16_tdo_in:_Maxcodebelow U+10FFFF, and a larger one limited to U+10FFFF;_Consume_header;char16_tdo_out:char32_tdo_in:_Maxcodechecked after the value is read, a consumed header dropped before the check, and a kept header rejected by it;char32_tdo_out:_Maxcode;partialwith the state set when the header does not fit, and no header on the next call.guest_codecvt_utf_metadata(the #19593 members of the same facets) andguest_libc_internal_codecvt_charpass.CodecvtUtfConversion.cpp, rebuilt the test, ran it and restored the file. Each change made it fail. They include no step over a lead continuation byte, no U+10FFFF limit, no rollback of a noncanonical sequence, the retry called directly instead of through the vtable, the retry through thedo_outslot, no header rollback, the consume flag read as the generate flag, thechar16_tstate written as 8 bytes and thechar32_tstate as 4, a four-byte write for a high surrogate, unsaved surrogate bits, no header room check, five-byte forms for every lead above 0xf7, no_Maxcodecheck, the top bits dropped from a six-byte lead, and the header given up without marking the state. With the file restored, the test passes.tools/check_conventions.py --base origin/mainreports "All conventions checks passed." With--titleand--bodyset to this title and body, it also passes.tools/debt_fold.py --checkpasses.tools/pr_overlap.py --branch feat/codecvt-utf-conversions: see Overlap above.Sources
char16_tdo_in: forms,_Maxcodelimited to U+10FFFF, surrogate split through the state, noncanonical rollback, header consumed and retried throughdo_in, rollback onpartialstl/inc/xlocalelines 965-1074; the same logic in crt.vc12 f1d47126bfsrc/xlocalelines 1081-1185char16_tdo_out: surrogate bits saved in the state, output counts, header generationxlocalelines 1077-1150; crt.vc12xlocalelines 1187-1255char32_tdo_in: one to six byte forms, header before_Maxcode, retry and rollbackxlocalelines 1264-1332; crt.vc12xlocalelines 1405-1470char32_tdo_out:_Maxcodefirst, one to six byte forms,partialafter marking the state when the header does not fitxlocalelines 1334-1394; crt.vc12xlocalelines 1472-1525_Consume_headeris 4 and_Generate_headeris 2;_Maxcodeisunsigned longfollowed by_Modexlocaleline 894, lines 1191-1192 and 1426-1427; crt.vc12xlocalelines 992-994, 1317-1318 and 1584-1585_Modeat 0x18ModeOffsetinCodecvtUtfMetadata.cppline 10, from the SDK 10.00libc.prxdisassembly recorded in the "UTF codecvt metadata and unshift" entry ofdocs/dev/debt/libSceLibcInternal.mdline 490do_inis vtable slot 0x38GuestLocale.hpplines 352 and 372; thecodecvt<char>members call slot 0x38 the same way (docs/dev/debt/libSceLibcInternal.mdline 488)docs/dev/REVERSE_ENGINEERING.mdline 43do_lengthdiffers between the two versionsxlocalelines 1267-1295 and 1534-1564; microsoft/STLxlocalelines 850-891, 1162-1165 and 1404-1407char32_tdo_unshiftreturnsok, as V6.00 does (lines 1527-1532); microsoft/STL returnsnoconv(lines 1396-1400)docs/dev/debt/libSceLibcInternal.mdline 490 (char32 unshift at0xb31b0in SDK 10.00libc.prx)libSceLibcInternal.clines 16396-16404;tools/nid_names.py --namegives the same NIDs for the mangled names9H2BStEAAMgandjlNI3SSF41o, and theidsu2MAta5SS84andxM5re58mxj8data/stubs/by_library/libc.txtlines 35, 91, 547, 1246, 1972, 2201, 2663 and 2804Checklist
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-16T02:05:57Z.
Review: CHANGES_REQUESTED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
60dc59c251:tools/check_conventions.pypassed;git diff --checkclean.guest_codecvt_utf_conversion_testsbuilt cleanly.guest_codecvt_utf_conversion):Requesting changes for the points above.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T02:25:54Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected conversion code and tests show no verified blocking defect at head
60dc59c25146904a593994fab25a4d7b64a0edbc; execution checks were not run in this review, so a formal approval is not justified.What I ran
Read the prepared description, commit list, manifest, and diff; inspected the full conversion implementation and the relevant test sections. No commands or tests were run. The author reports a Fedora build, tests, and negative controls; those results were not independently confirmed.
Results
Findings
Blocking
None identified in the code inspected.
Non-blocking
None.
Not verified
Did not run build or tests, inspect the full test file or every changed file, or verify behavior on console hardware. Inspected
CodecvtUtfConversion.cppin full, relevant test sections, and the prepared review documents and diff. The manifest lists seven changed files, all marked included; the other changed files were not read directly. Author-reported test and negative-control results remain unverified.To get this to a good state
Nothing is required.
Pull request closed