feat(libc): implement vsnprintf_s and vsscanf_s #1981

Closed
overlordxrz-source wants to merge 1 commit from upstream-pr/1981 into main
overlordxrz-source commented 2026-10-09 03:41:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-09T03:41:42Z.


What

Cyberpunk 2077 (PPSA04029) imports two libc functions that are still throwing stubs on main, libcCyberUnknown18 and libcCyberUnknown19. tools/nid_names.py resolves their NIDs:

NID Name
rWSuTWY2JN0 vsnprintf_s
tfNbpqL3D0M vsscanf_s

Both NIDs are in the PPSA04029 import list attached to #266. rWSuTWY2JN0 is also in the PPSA03396 list in #269.

The stub comments described a file-size idiom called as (handle, 0, 2) and (handle, 0, 0). That contradicts the hash-verified names, as the comment on strnlen_s did before b9c1c884. #492 left these two stubs alone because of it. This PR removes the stubs and their comments.

These are the C11 Annex K signatures, not MSVC's:

  • int vsnprintf_s(char*, rsize_t, const char*, va_list)
  • int vsscanf_s(const char*, const char*, va_list)

MSVC's vsnprintf_s has an extra count argument. The guest libc exports the Annex K-only set_constraint_handler_s, abort_handler_s and ignore_handler_s, plus snwprintf_s, vsnwprintf_s and wcsnlen_s. It exports none of MSVC's underscore forms such as _vsnprintf_s (see the export list on psdevwiki's LibSceLibcInternal page). The existing snprintf_s, vsprintf_s, snwprintf_s and sscanf_s exports here already use the C11 shapes. #1498 implements rWSuTWY2JN0 with MSVC's count argument, so a C11 caller's format pointer would be read as count there.

  • vsnprintf_s rejects these with a negative result: a null buffer or format, a size of 0 or above RSIZE_MAX, any %n (%%n is still literal text), and a null %s, %ls or %S argument. When the buffer is usable it writes an empty string. To find null strings it walks a copy of the guest va_list (* widths and precisions, integers, doubles, long double, pointers) and stops at the first conversion it doesn't know, leaving that to the formatter. Otherwise it formats through the existing vsnprintf export, which uses the Windows guest formatter on Windows. It returns the full length and truncates with a terminating null.
  • vsscanf_s returns EOF for a null input, format or target pointer. Otherwise it runs the scanner sscanf_s already uses, with sized %s, %c and %[. The size is read as unsigned int, as sscanf_s does.
  • Neither function calls a constraint handler, since none of the other _s exports here do.

Log

I don't own Cyberpunk 2077, so I can't paste a line where PPSA04029 stops on main. I found no stop line for these NIDs in any issue or PR. The evidence is the import lists in #266 and #269. If someone with either title can post the NotImplemented line for rWSuTWY2JN0 or tfNbpqL3D0M from main, I'll add it here.

Tested

aarch64 Debian 13 host. The full project can't be built here because it needs x86-64.

  • New core/libs/tests/GuestSecureFormatting.cpp (guest_secure_formatting), linked natively against Formatting.cpp through the non-Windows va_list path, passes and also passes with UBSan. The vsnprintf_s checks cover formatting, truncation with the full count, literal %%n, %n with and without flags, null %s and %ls (including after *, %f, %Lf, %c, %p and %lld arguments), null format and buffer, size 0 and a size above RSIZE_MAX leaving the buffer alone. The vsscanf_s checks cover sized %s, %c and %[, a too-small buffer, empty input, and null input, format and targets.
  • On x86_64-linux-gnu, Formatting.cpp, Export.cpp and the test compile with -Wall -Wextra (Zig 0.17, clang 22). The only warnings are the existing ones from General.hpp and FileStream.hpp. Zig's clang 22 lacks __builtin_sysv_va_list, so Formatting.cpp on main doesn't compile for x86_64-windows-gnu here either. With those builtins mapped to the plain va_list ones, the only errors on that target are the existing va_start lines; the new Windows path (LibcDetail::FormatArguments) compiles. It hasn't been run on Windows.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Not run with a title.

Checklist

  • Based on current main. The one other open PR that changes these functions is #1498, which also changes these stubs as part of a larger change; see What
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4795687797 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1981). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-09T03:41:42Z. --- ### What Cyberpunk 2077 (PPSA04029) imports two `libc` functions that are still throwing stubs on `main`, `libcCyberUnknown18` and `libcCyberUnknown19`. `tools/nid_names.py` resolves their NIDs: | NID | Name | | - | - | | `rWSuTWY2JN0` | `vsnprintf_s` | | `tfNbpqL3D0M` | `vsscanf_s` | Both NIDs are in the PPSA04029 import list attached to #266. `rWSuTWY2JN0` is also in the PPSA03396 list in #269. The stub comments described a file-size idiom called as `(handle, 0, 2)` and `(handle, 0, 0)`. That contradicts the hash-verified names, as the comment on `strnlen_s` did before b9c1c884. #492 left these two stubs alone because of it. This PR removes the stubs and their comments. These are the C11 Annex K signatures, not MSVC's: - `int vsnprintf_s(char*, rsize_t, const char*, va_list)` - `int vsscanf_s(const char*, const char*, va_list)` MSVC's `vsnprintf_s` has an extra `count` argument. The guest `libc` exports the Annex K-only `set_constraint_handler_s`, `abort_handler_s` and `ignore_handler_s`, plus `snwprintf_s`, `vsnwprintf_s` and `wcsnlen_s`. It exports none of MSVC's underscore forms such as `_vsnprintf_s` (see the export list on psdevwiki's LibSceLibcInternal page). The existing `snprintf_s`, `vsprintf_s`, `snwprintf_s` and `sscanf_s` exports here already use the C11 shapes. #1498 implements `rWSuTWY2JN0` with MSVC's `count` argument, so a C11 caller's format pointer would be read as `count` there. - `vsnprintf_s` rejects these with a negative result: a null buffer or format, a size of 0 or above `RSIZE_MAX`, any `%n` (`%%n` is still literal text), and a null `%s`, `%ls` or `%S` argument. When the buffer is usable it writes an empty string. To find null strings it walks a copy of the guest `va_list` (`*` widths and precisions, integers, doubles, `long double`, pointers) and stops at the first conversion it doesn't know, leaving that to the formatter. Otherwise it formats through the existing `vsnprintf` export, which uses the Windows guest formatter on Windows. It returns the full length and truncates with a terminating null. - `vsscanf_s` returns `EOF` for a null input, format or target pointer. Otherwise it runs the scanner `sscanf_s` already uses, with sized `%s`, `%c` and `%[`. The size is read as `unsigned int`, as `sscanf_s` does. - Neither function calls a constraint handler, since none of the other `_s` exports here do. ### Log I don't own Cyberpunk 2077, so I can't paste a line where PPSA04029 stops on `main`. I found no stop line for these NIDs in any issue or PR. The evidence is the import lists in #266 and #269. If someone with either title can post the `NotImplemented` line for `rWSuTWY2JN0` or `tfNbpqL3D0M` from `main`, I'll add it here. ### Tested aarch64 Debian 13 host. The full project can't be built here because it needs x86-64. - New `core/libs/tests/GuestSecureFormatting.cpp` (`guest_secure_formatting`), linked natively against `Formatting.cpp` through the non-Windows `va_list` path, passes and also passes with UBSan. The `vsnprintf_s` checks cover formatting, truncation with the full count, literal `%%n`, `%n` with and without flags, null `%s` and `%ls` (including after `*`, `%f`, `%Lf`, `%c`, `%p` and `%lld` arguments), null format and buffer, size 0 and a size above `RSIZE_MAX` leaving the buffer alone. The `vsscanf_s` checks cover sized `%s`, `%c` and `%[`, a too-small buffer, empty input, and null input, format and targets. - On x86_64-linux-gnu, `Formatting.cpp`, `Export.cpp` and the test compile with `-Wall -Wextra` (Zig 0.17, clang 22). The only warnings are the existing ones from `General.hpp` and `FileStream.hpp`. Zig's clang 22 lacks `__builtin_sysv_va_list`, so `Formatting.cpp` on `main` doesn't compile for x86_64-windows-gnu here either. With those builtins mapped to the plain `va_list` ones, the only errors on that target are the existing `va_start` lines; the new Windows path (`LibcDetail::FormatArguments`) compiles. It hasn't been run on Windows. - `python3 tools/check_conventions.py --base origin/main` passes. - Not run with a title. ### Checklist - [x] Based on current `main`. The one other open PR that changes these functions is #1498, which also changes these stubs as part of a larger change; see What - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-09 03:42:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T03:42:07Z.


Progress report (f145139 → 9a4e12e)

📈 System libraries: 84.83% (+0.07%, +2 implemented, -2 removed functions)

✅ 2 implemented
Library Name
libc vsnprintf_s_nid_postfix
libc vsscanf_s_nid_postfix
🗑️ 2 removed
Library Name
libc libcCyberUnknown18
libc libcCyberUnknown19
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6073807752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1981#issuecomment-6073807752). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T03:42:07Z. --- <!-- progress-report --> ### Progress report (f145139 → 9a4e12e) 📈 **System libraries**: 84.83% (+0.07%, +2 implemented, -2 removed functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libc | `vsnprintf_s_nid_postfix` | | libc | `vsscanf_s_nid_postfix` | </details> <details> <summary>🗑️ 2 removed</summary> | Library | Name | | - | - | | libc | `libcCyberUnknown18` | | libc | `libcCyberUnknown19` | </details>
MarcZX10 commented 2026-10-09 04:14:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T04:14:00Z.

Review: CHANGES_REQUESTED


The tests exercise the behavior this patch implements, and the updated build/conventions checks pass. The stated need is still only the PPSA import lists; the PR explicitly says no current-main NotImplemented call was found and no title was run. CONTRIBUTING.md says an import list alone does not show that a title reaches an export and asks for a reproduced failure or a concrete project need with its evidence limits. Since this adds two nontrivial secure-formatting implementations with assumed console behavior, please provide runtime evidence or a demonstrated project need before review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5465632296 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1981#pullrequestreview-5465632296). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T04:14:00Z. **Review:** CHANGES_REQUESTED --- The tests exercise the behavior this patch implements, and the updated build/conventions checks pass. The stated need is still only the PPSA import lists; the PR explicitly says no current-main `NotImplemented` call was found and no title was run. CONTRIBUTING.md says an import list alone does not show that a title reaches an export and asks for a reproduced failure or a concrete project need with its evidence limits. Since this adds two nontrivial secure-formatting implementations with assumed console behavior, please provide runtime evidence or a demonstrated project need before review.
overlordxrz-source commented 2026-10-09 04:54:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-09T04:54:58Z.


Thanks. I don't have a current-main runtime line. The only call-site evidence is main's own note from #358's static GOT-xref pass on PPSA04029 (// Live Cyberpunk 2077 import used in an fopen/fseek/ftell-like file-size idiom as (handle, 0, 2) above rWSuTWY2JN0, and (handle, 0, 0) above tfNbpqL3D0M). That shows a call site exists, not that boot reaches it. That observed argument shape also doesn't match the hash-verified vsnprintf_s/vsscanf_s signatures, which is why #492 left these stubs alone. Either the static attribution or the name is wrong, and I can't settle that without the binary. I'm moving this to draft until someone with PPSA04029 (or PPSA03396) posts the current-main rWSuTWY2JN0 not implemented / tfNbpqL3D0M not implemented line or a disassembly of the call site that fixes the signature.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074540623 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1981#issuecomment-6074540623). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-09T04:54:58Z. --- Thanks. I don't have a current-main runtime line. The only call-site evidence is main's own note from #358's static GOT-xref pass on PPSA04029 (`// Live Cyberpunk 2077 import used in an fopen/fseek/ftell-like file-size idiom as (handle, 0, 2)` above `rWSuTWY2JN0`, and `(handle, 0, 0)` above `tfNbpqL3D0M`). That shows a call site exists, not that boot reaches it. That observed argument shape also doesn't match the hash-verified `vsnprintf_s`/`vsscanf_s` signatures, which is why #492 left these stubs alone. Either the static attribution or the name is wrong, and I can't settle that without the binary. I'm moving this to draft until someone with PPSA04029 (or PPSA03396) posts the current-main `rWSuTWY2JN0 not implemented` / `tfNbpqL3D0M not implemented` line or a disassembly of the call site that fixes the signature.
oneandonlydean commented 2026-10-09 09:30:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:30:43Z.


Closing: only the import lists in #266 and #269 show these, and the call shape noted in Export.cpp doesn't match the names yet. Reopen with the not implemented line from main, or the disassembled call site, when a title stops here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078245282 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1981#issuecomment-6078245282). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:30:43Z. --- Closing: only the import lists in #266 and #269 show these, and the call shape noted in `Export.cpp` doesn't match the names yet. Reopen with the `not implemented` line from main, or the disassembled call site, when a title stops here.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!1981
No description provided.