Skip to content

fix(libc): convert wcsrtombs using guest characters - #1507

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
ArjenS-LA:fix/libc-wcsrtombs
Oct 10, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
ArjenS-LA:fix/libc-wcsrtombs

Conversation

@ArjenS-LA

@ArjenS-LA ArjenS-LA commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What

wcsrtombs passed guest strings and conversion state directly to the host CRT. On Windows, an invalid character such as U+0100 returned host EILSEQ=42 instead of guest 86. On Linux, the host's 32-bit wchar_t also disagrees with the guest's 16-bit characters.

The export now lives beside the other guest multibyte conversions and uses wcrtomb with 16-bit guest characters. Partial conversions leave the source at the next unconverted character; a stored terminator sets it to null. Length queries ignore capacity and preserve the source pointer. Invalid characters report guest EILSEQ and preserve the converted prefix.

Adds the registered guest_wcsrtombs regression test and updates the existing technical-debt entry. Conversion follows the existing guest single-byte C locale; other encodings and shift states remain unsupported. The restart and length-query behavior follows the C working draft, section 7.31.6.4.2.

Tested

Windows validation of PR head 74e1a6f5c4ba8c1f4cf805ba23965199bd473407, rebased onto main 6b56175a4290a03ab28ebc6697beeffe6fe9be8d.

The rebase resolves the TechnicalDebt.md conflict by preserving upstream's SPIRV-Tools and Windows host-import entries alongside the corrected wide-character note. The note's leading link now points to Multibyte.cpp as requested in review. The implementation and regression-test patch are unchanged.

Windows 11 Enterprise 10.0.26200; WinLibs MinGW-w64 GCC 15.2.0 r2 UCRT/posix/SEH, Ninja Release, BUILD_TESTING=ON, SPIRV-Tools disabled. This uses the installed r2 toolchain, not CI's r7 package.

The libc library and all four selected test targets rebuilt successfully after the rebase:

CTest test Result
guest_wcsrtombs Passed
guest_wcsrtombs_s Passed
guest_wide_strings Passed
guest_locale Passed

4/4 passed, 0 failed, 0 skipped. The new test covers full, empty, partial and resumed conversions; exact and zero capacity; high-byte characters; explicit and null state pointers; length queries; source-pointer positions; output boundaries; and guest error numbers.

Before the fix, the new regression test was compiled directly against the original Strings.cpp from 8763483811950f6e150201e664e771ec0dac0794, with a test-only errno accessor. It failed at invalid character uses guest EILSEQ. The same standalone test passed after the replacement. This is a focused reproduction, not a full baseline-versus-PR suite comparison.

The committed-head conventions check, diff whitespace check, and test-registration check passed. The registration check found no newly unregistered test executables.

These local results cover the pinned PR revision. Linux, the full registered CTest suite, the complete default/libs build, games, and the eventual merged tree were not rerun locally for this rebase. No performance or game-compatibility conclusion is claimed.

Independent validation of the earlier head: Dextroid17 reported 462/462 Linux tests passing after integrating it with main fd5342b6 (CachyOS, GCC 16.2.1, Release, SPIRV-Tools enabled). Reverting only the implementation made the new test fail at full conversion length. Those are the reviewer's results for that earlier integration, not a Linux run of the rebased head above.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes — OpenAI Codex assisted with implementation, regression tests, execution, analysis, and this report.

@klosixgaming-arch klosixgaming-arch 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.

Solid fix for wcsrtombs with excellent test coverage. The comprehensive edge case testing (partial conversions, exact capacity, invalid characters, length queries) is thorough and follows C11 standards properly.

Note: This overlaps with #1511 and #1512 on the TechnicalDebt.md line - all three address wide-character conversion issues. Coordination between these PRs would be helpful to avoid merge conflicts.

@klosixgaming-arch klosixgaming-arch 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.

Good implementation of wcsrtombs using the guest's single-byte C locale through wcrtomb. This fixes the issue where on Linux (where host wchar_t is 32 bits), guest strings were misread and miswritten.

Strengths:

  • Clean implementation using existing wcrtomb infrastructure
  • Comprehensive test coverage including edge cases (partial conversion, invalid characters, zero capacity, length queries)
  • Proper error handling with guest EILSEQ errno value (86)
  • Good documentation update in TechnicalDebt.md noting remaining limitations

Technical notes:

  • The approach of iterating through wide characters and converting each individually is correct for the single-byte C locale
  • Proper handling of source pointer updates on both success and failure
  • Test cases properly verify all conversion scenarios including resume after partial conversion

This will improve compatibility with titles that use wcsrtombs. Ready to merge.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 8, 2026
klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
Remove items that have been completed in recent PRs:
- sce::Json::Value::clear (implemented in boykopovar#1204)
- libSceNpPartner001 functions (implemented in boykopovar#1517)
- swprintf formatting (fixed in boykopovar#1512/boykopovar#1511)
- wcsrtombs implementation (completed in boykopovar#1507)
- sceNetResolverAbort (implemented in boykopovar#1502)

Update remaining entries to reflect current state.
@Dextroid17

Copy link
Copy Markdown
Contributor

Ran this on Linux because it has no independent run yet, and it holds up. Details, so the next reader does not have to redo them.

Merged onto current main (fd5342b6): one conflict, in docs/dev/TechnicalDebt.md, and only because main added an unrelated SPIRV-Tools entry next to the one you edited — keep both.

Result (CachyOS, GCC 16.2.1, Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON):

ctest -R 'guest_wcsrtombs|guest_wcsrtombs_s|guest_strings'   →  3/3 passed
full suite                                                   →  462/462 passed

The test bites — reverted only the implementation (Strings.cpp and Multibyte.cpp back to main, test and registration kept):

Test #33: guest_wcsrtombs ...***Failed
full conversion length

That failure is worth quoting in the description, because it is stronger than "misreads non-ASCII": on main the very first assertion fails, so every guest string of two or more characters fails to convert, not only the ones with a character above 0x7f. A change that fixes every caller rather than one title's case is the shape that gets merged here.

Semantics cross-checked against the platform's own wcsrtombs (same cases, C locale, host wchar_t, so the reference is the same function implemented correctly):

capacity 8   → 3, source NULL, terminator stored
capacity 4   → 3, source NULL
capacity 3   → 3, source at the terminator      (terminator pending)
capacity 2   → 2, source at the third character
capacity 0   → 0, source unchanged
dst NULL     → the length, source unchanged
invalid char → (size_t)-1, source at the offending character, errno set

Every one matches yours, including the two that are easy to get wrong (the terminator-not-stored case and the length query leaving source alone). The errno half is complete through wcrtomb, which sets 86 itself — I checked, because the plain function has to set it and it is not obvious from the call site.

One nit: the debt entry's first link still points at Strings.cpp although wcsrtombs now lives in Multibyte.cpp; pointing that link at Multibyte.cpp (and leaving the swprintf one) would match the tree after this change.

Written with AI assistance.

klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
This PR updates TechnicalDebt.md to remove items that have been completed in recent PRs:

**Removed (completed):**
- `sce::Json::Value::clear` - implemented in boykopovar#1204
- libSceNpPartner001 functions (`sceNpEAAccessInitialize`, `sceNpEAAccessTerminate`) - implemented in boykopovar#1517
- swprintf formatting issues - fixed in boykopovar#1512/boykopovar#1511 (now routes through 16-bit guest formatter)
- wcsrtombs implementation - completed in boykopovar#1507 (uses guest's single-byte C locale through wcrtomb)
- sceNetResolverAbort - implemented in boykopovar#1502

**Updated:**
- libSceNpPartner001 entry now reflects the actual implementation status
- Wide-character conversions entry updated to reflect wcsrtombs completion
- PPSA23566 unknown signatures list updated to remove completed items

This keeps the technical debt documentation accurate and up-to-date with recent development.
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 8, 2026
@hashaam1217

Copy link
Copy Markdown
Contributor

Linux run, since the bug is Linux-specific (32-bit host wchar_t): Ubuntu 24.04.5, glibc 2.39, GCC 13.3.0, Release. Head 74e1a6f merged onto main 4b6ab6b.

  • guest_wcsrtombs, guest_wcsrtombs_s, guest_wide_strings and guest_locale pass.
  • I reverted Strings.cpp and Multibyte.cpp to main and kept the new test. On Linux, guest_wcsrtombs then fails at its first check, full conversion length, not at the EILSEQ check where it failed on Windows. So on Linux the old host-wchar_t path already gets the plain ASCII conversion wrong.

@github-actions

github-actions Bot commented Oct 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
#545 core/libs/prx/libc/src/Strings.cpp L455-458
#1511 docs/dev/TechnicalDebt.md L259

klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
This PR updates TechnicalDebt.md to remove items that have been completed in recent PRs:

**Removed (completed):**
- `sce::Json::Value::clear` - implemented in boykopovar#1204
- libSceNpPartner001 functions (`sceNpEAAccessInitialize`, `sceNpEAAccessTerminate`) - implemented in boykopovar#1517
- swprintf formatting issues - fixed in boykopovar#1512/boykopovar#1511 (now routes through 16-bit guest formatter)
- wcsrtombs implementation - completed in boykopovar#1507 (uses guest's single-byte C locale through wcrtomb)
- sceNetResolverAbort - implemented in boykopovar#1502

**Updated:**
- libSceNpPartner001 entry now reflects the actual implementation status
- Wide-character conversions entry updated to reflect wcsrtombs completion
- PPSA23566 unknown signatures list updated to remove completed items

This keeps the technical debt documentation accurate and up-to-date with recent development.
klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
Remove entries for functions that have been implemented:
- libSceNpPartner001 unknown functions (implemented in boykopovar#1517)
- sce::Json::Value::clear (implemented in boykopovar#1204)
- sceNetResolverAbort (implemented in boykopovar#1502)
- Wide-character conversions: swprintf (fixed in boykopovar#1512/boykopovar#1511), wcsrtombs (completed in boykopovar#1507)

Addresses feedback from @Dextroid17 on PR boykopovar#1833 to remove only completed
entries rather than replacing the entire file.

Written with AI assistance.
klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
Remove only the debt entries that have since been implemented, leaving
every other entry in TechnicalDebt.md unchanged:

- libSceNpPartner001 EA access functions (implemented in boykopovar#1517)
- sce::Json::Value::clear (implemented in boykopovar#1204)
- sceNetResolverAbort (implemented in boykopovar#1502)
- Wide-character conversions: swprintf (fixed in boykopovar#1511/boykopovar#1512) and
  wcsrtombs (completed in boykopovar#1507)

This is the targeted, reviewable change requested by @Dextroid17 on boykopovar#1539
and boykopovar#1833, instead of replacing the whole file.

Written with AI assistance.
klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
Remove only the debt entries that have since been implemented, leaving every
other entry in TechnicalDebt.md byte-for-byte unchanged:

- libSceNpPartner001 EA access entry (7CxI50-xlCk, pMxXhNozUX8) - boykopovar#1517
- sce::Json::Value::clear - boykopovar#1204
- sceNetResolverAbort - boykopovar#1502
- Wide-character conversions (swprintf / wcsrtombs) - boykopovar#1511/boykopovar#1512 and boykopovar#1507

Content is aligned to this branch's merge base so the PR diff shows only these
three removals, per the review request on boykopovar#1539.

Written with AI assistance.
klosixgaming-arch added a commit to klosixgaming-arch/AnyPS5 that referenced this pull request Oct 8, 2026
…te-exact)

Remove only the debt entries that have since been implemented, leaving every
other entry byte-for-byte identical to this branch's merge base:

- libSceNpPartner001 EA access entry (7CxI50-xlCk, pMxXhNozUX8) - boykopovar#1517
- sce::Json::Value::clear - boykopovar#1204
- sceNetResolverAbort - boykopovar#1502
- Wide-character conversions (swprintf / wcsrtombs) - boykopovar#1511/boykopovar#1512 and boykopovar#1507

Content is aligned to the merge base so the PR diff shows only these three
removals, per the review request on boykopovar#1539.

Written with AI assistance.
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the build and the four libc wide-string tests (guest_wcsrtombs, guest_wcsrtombs_s, guest_wide_strings, guest_locale) on Linux; guest_wcsrtombs fails without the implementation. Restart, length-query and EILSEQ paths match the description; TechnicalDebt entry is accurate. CI is green; merging.

@adriadam10
adriadam10 merged commit 7f1b6a0 into boykopovar:main Oct 10, 2026
6 of 7 checks passed
SleglJan pushed a commit to SleglJan/AnyPS5 that referenced this pull request Oct 10, 2026
fix(libc): convert wcsrtombs using guest characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants