Skip to content

feat(libc): implement vsnprintf_s and vsscanf_s - #1981

Closed
overlordxrz-source wants to merge 1 commit into
boykopovar:mainfrom
overlordxrz-source:feat/libc-annexk-vsnprintf-vsscanf
Closed

overlordxrz-source wants to merge 1 commit into
boykopovar:mainfrom
overlordxrz-source:feat/libc-annexk-vsnprintf-vsscanf

Conversation

@overlordxrz-source

@overlordxrz-source overlordxrz-source commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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 b9c1c88. #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 feat(libs): implement font, libc, libkernel and misc prx stubs #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

rWSuTWY2JN0 and tfNbpqL3D0M were throwing libcCyberUnknown18/19 stubs.
They are the NIDs of vsnprintf_s and vsscanf_s, the C11 Annex K forms
that take a guest va_list. The stub comments described a file-size
idiom, which contradicts the names, like the comment strnlen_s had.

vsnprintf_s rejects a null buffer or format, a zero size, a size above
RSIZE_MAX, %n and a null %s argument, writing an empty string when the
buffer is usable, and otherwise formats through vsnprintf. vsscanf_s
rejects a null input, format or target with EOF and otherwise scans
with the sized %s, %c and %[ rules of sscanf_s.
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

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

@MarcZX10 MarcZX10 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.

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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Collaborator

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.

@oneandonlydean oneandonlydean mentioned this pull request Oct 9, 2026
9 tasks done
ycbuzi pushed a commit to ycbuzi/AnyPS5 that referenced this pull request Oct 10, 2026
…nf_s

Harvested from upstream. Hand merge as for 1952: kept our libc/Export.cpp and deleted the stubs the PR implements elsewhere.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011krDVCnnMQPaEFEqCDnKmA
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.

3 participants