Skip to content

fix(libc): map _Towctrans descriptors 1 and 2 to lower and upper case - #4095

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
EagleBearerYS:fix/libc-towctrans-descriptors
Oct 12, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
EagleBearerYS:fix/libc-towctrans-descriptors

Conversation

@EagleBearerYS

Copy link
Copy Markdown
Contributor

What

_Towctrans passed the guest's descriptor straight to the host towctrans, but the descriptor is Dinkumware's, not the host's:

  • Dinkumware's headers turn towlower(wc) into _Towctrans(wc, 1) and towupper(wc) into _Towctrans(wc, 2): TI's C6000 library xwcc.h (V4.02), the C++ inlines at lines 73-81 and the C macros at lines 129-131, next to towctrans(wc, off) as _Towctrans(wc, off). The descriptor is a size_t there (typedef _Sizet wctrans_t;, wctype.h line 21 of another Dinkumware copy).
  • Sony's libc exports these Dinkumware internals: _Getpctype, _Iswctype, _Stoul and _Towctrans are in the PS4 name list (names.txt lines 456, 480, 691, 733) and the Vita one (vita_names.txt line 803), and the review of feat(libc): implement the C11 mtx and cnd functions #492 (point 6) found Sony's _WStoul calling _Iswctype(c, 9) for white space, the index xwcc.h gives iswspace.
  • FreeBSD uses the same values: wctrans.c has _WCT_TOLOWER = 1 and _WCT_TOUPPER = 2, and towctrans_l returns wc unchanged with errno = EINVAL for any other descriptor.

The hosts read the value differently. On Windows, UCRT's towctrans reads 1 as toupper and other nonzero values as tolower, and its wint_t is 16 bits. A probe calling the export as a guest does:

Call Before After
towlower(L'A') = _Towctrans(L'A', 1) L'A' L'a'
towlower(L'a') = _Towctrans(L'a', 1) L'A' L'a'
towupper(L'a') = _Towctrans(L'a', 2) L'a' L'A'
_Towctrans(L'A', 3) L'a', errno 0 L'A', errno EINVAL
_Towctrans(L'A', 0) L'A', errno 0 L'A', errno EINVAL
_Towctrans(WEOF, 1) 0xffff 0xffffffff

On Linux, glibc's wctrans_t is const __int32_t * (wctype.h line 48) and towctrans passes any nonzero descriptor to wctrans_table_lookup, which reads the table at that address (towctrans.c lines 24-33, wchar-lookup.h lines 117-119), so descriptor 1 or 2 is dereferenced as an address.

_Towctrans now takes the guest's 32-bit wint_t and 64-bit descriptor. 1 and 2 map 0..255 through the C locale tables behind _Getptolower and _Getptoupper (ASCII letters only, as in the rune locale's __maplower and __mapupper) and return other values unchanged, without the host's towlower or towupper. Any other descriptor sets errno to EINVAL and returns wc unchanged, as FreeBSD does. towlower, towupper and the other wide classification functions are left to #1783.

Tested

Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, -DBUILD_TESTING=ON, on main at 325e0a8 with #4021 applied locally, since main's Windows build stops in Socket.cpp without it:

  • New CheckWideCaseTranslation in guest_locale: _Towctrans(L'A', 1) == L'a' and _Towctrans(L'a', 2) == L'A'; descriptors 1 and 2 against _Getptolower/_Getptoupper for 0..255 (digits unchanged) with errno untouched; 0x100, 0x391, 0x3B1, 0xFFFF and WEOF unchanged; descriptors 0 and 3 return L'A' and L'7' unchanged and set errno to 22. Without the library change the test fails at the first check (terminate called after throwing an instance of 'std::runtime_error', what(): Guest locale check failed); with it, it passes 20 runs in a row.
  • The probe in the table, before and after the change.
  • All 128 tests registered in core/libs/prx/libc/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt that build on Windows pass.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux (no machine here; the glibc crash is from reading its source, not from a run), macOS, any title.

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

_Towctrans passed the guest's descriptor to the host towctrans.
Dinkumware's <wctype.h> (xwcc.h) defines towlower(wc) as
_Towctrans(wc, 1) and towupper(wc) as _Towctrans(wc, 2), the values
FreeBSD's wctrans.c also uses for "tolower" and "toupper". UCRT reads
1 as toupper and other nonzero values as tolower, so on Windows
towlower(L'A') returned L'A' and towupper(L'a') returned L'a', while
towlower(L'a') returned L'A'. The host wint_t is 16 bits there, which
also turned WEOF into 0xffff. glibc's wctrans_t is a pointer to a
table that towctrans reads, so on Linux descriptor 1 or 2 is
dereferenced as an address.

_Towctrans now takes the guest's 32-bit wint_t and size_t descriptor.
1 and 2 map 0..255 through the C locale tables behind _Getptolower and
_Getptoupper and return other values unchanged. Any other descriptor
sets errno to EINVAL and returns wc unchanged, as FreeBSD's
towctrans_l does.
@github-actions github-actions Bot added the libc label Oct 11, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@oneandonlydean
oneandonlydean merged commit 375e99e into boykopovar:main Oct 12, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants