Skip to content

docs(debt): record the console results of snwprintf_s - #21896

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libc-snwprintf-s-console
Nov 16, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libc-snwprintf-s-console

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

What

The snwprintf_s and vsnwprintf_s entry in docs/dev/debt/libc.md follows the bundled libc.prx modules of two titles and has no console measurement. A probe on a PS5 has now called snwprintf_s from a payload process, in libSceLibcInternal, with a counting constraint handler, on 16-bit char16_t buffers. The pull request adds one sentence and one clause to the entry through a debt fragment, as the contribution rules ask (the clause, "as does a title process", extends the existing note on what stays unverified); no code changes.

On firmware 11.60 the console's snwprintf_s returned 6 for ab%dcd with 42 into 16 elements (line 3); into 4 elements it returned the full length 8 and left abc and a terminator (line 4), and into 9 elements it returned 8 with the whole text (line 5). A size of 0 and a size of (SIZE_MAX >> 3) + 1 each returned -1 after one handler call with "snwprintf_s: bad argument" and wrote nothing (lines 6 and 7), while a size of SIZE_MAX >> 3 returned 3 (line 8). %n, a null %s and a null %ls each returned -23 after one handler call with "wprintf_s: %n disallowed", "wprintf_s: bad %s argument" and "wprintf_s: bad %ls argument", with the text before the conversion in the buffer and its first element cleared (lines 9 to 11). A null buffer returned -1 after the "bad argument" handler call (line 12), and a null format returned -1 after a handler call with "snwprintf_s: bad format argument", clearing the first element (line 13).

AnyPS5 gives the same for all of these when its code is read: FormatWideSecure in FormattingWide.cpp (lines 292 to 336) with snwprintf_s_nid_postfix (lines 371 to 376) returns the full length when the output is cut, -1 with the messages above for the bad size, buffer and format, and -23 after the handler call for the three violations. The emulator itself was not run.

Not measured on the console: vsnwprintf_s, the pointer and error number the handler receives (the probe's handler did not record them), an invalid multibyte %s (the entry's remaining gap, unchanged), a width or *, and a title process.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loads libSceLibcInternal.sprx with dlopen and takes snwprintf_s and set_constraint_handler_s with dlsym, on 2026-11-15 at 20:35 UTC. A title process may differ. The cases are 0 to d.2, lines 2 to 13 of the raw output in the first comment (line 2 shows that both symbols were found); the output buffer is a 16-element area inside a 32-element area filled with 0x7e7e.

Call PS5 payload process, firmware 11.60 AnyPS5 main (code read, not run) in the entry
ab%dcd with 42, size 16 / 4 / 9 6 / 8 with abc / 8 (lines 3 to 5) same stated (truncated, full length returned)
size 0 / (SIZE_MAX >> 3) + 1 -1, handler "bad argument", nothing written (lines 6, 7) same stated
size SIZE_MAX >> 3 3 (line 8) 3 stated (limit)
%n / null %s / null %ls -23, handler messages above, first element cleared (lines 9 to 11) same stated
null buffer / null format -1, "bad argument" / "bad format argument", first element cleared for the format (lines 12, 13) same stated

tools/check_conventions.py --base upstream/main passes with no error and no warning (the entry is 992 characters, up from 795). This is a documentation change, so no test was added or run, and no GPU test applies. Not tested: the emulator against these cases, Linux, macOS, the CI runner, a title process, and the cases listed above as not measured.

Overlap: the other open pull requests that change the file of this branch (the fragment docs/dev/debt/changes/docs-libc-snwprintf-s-console.md, a new file) or the debt entry it changes, found by reading the changed files of all 1762 open pull requests at about 19:40 UTC, by reading what they change in docs/dev/debt/libc.md and in the fragments under docs/dev/debt/changes/ for snwprintf_s and vsnwprintf_s, and by searching their titles and branch names for snwprintf_s and vsnwprintf_s. The branch was test-merged on 2026-11-16 at about 19:49 UTC against main e2e77aa74a5 (the branch is based on f4381f7ade3, an ancestor of it) and merges into it without a conflict; tools/debt_fold.py --check applies the fragment to the libc debt file of that main without an error, the text it changes occurs in exactly one entry, and no other fragment changes that entry. One open pull request changes this entry: #9087 (fix(libc): follow snprintf_s and sscanf_s constraints as measured on a PS5) adds a sentence to the snwprintf_s entry of libc.md about RSIZE_MAX and the narrow snprintf_s. It already conflicts with main without this change (it is based on an older version of the entry), and as this fragment replaces the whole entry, whichever is merged second has to carry the other's text over. #12315 lists FormattingWide.cpp but changes nothing in it against main, and #8965 changes the entries around it without naming these functions. The merged result was not built or run; this is a documentation change.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (documentation only)
  • AI-assisted: yes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Probe source (built with ps5-payload-sdk v0.45; PROBE_BUILD is the first 12 hex digits of the source's SHA-256, ff5c4bce7af1) and its raw output from the run on 2026-11-15 at 20:35 UTC. The clock in the PROBE line is the console's own clock. The probe opens libSceLibcInternal.sprx, installs a counting constraint handler and calls snwprintf_s with the arguments named in its output on char16_t buffers; every call is guarded against signals. An internal probe number and case reference in the header comment were removed; the code is unchanged, and the build ID is the hash of the original file.

probe.c
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h>   /* only for kernel_get_fw_version() */

#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: on timeout the runner can only close the connection, not end the process on the console.
   The probe ends itself after at most PROBE_MAX_S seconds (the build script sets timeout - 10 s). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static void probe_alarm(int sig) {
  static const char msg[] = "TIMEOUT\tprobe self limit\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(124);
}

static int cases = 0;

static void probe_header(void) {
  char utc[32];
  time_t now = time(0);
  struct tm tmv;
  gmtime_r(&now, &tmv);
  strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
  printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
         getpid(), utc);
  fflush(stdout);
}

/* CASE: id, description of the call, return value as long, errno (0 if not set), extra (hex/bytes). */
static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
  printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
  fflush(stdout);
  cases++;
}

static void probe_end(void) {
  printf("END\t%d\n", cases);
  fflush(stdout);
}

#include <dlfcn.h>
#include <setjmp.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>

static sigjmp_buf guard_env;
static volatile int guard_sig = 0;
static volatile int guard_active = 0;
static volatile int guard_hits = 0;
static void guard_handler(int s) {
  static const char msg[] = "FAULT\tprobe guard: fault outside a guarded call or too many faults\n";
  if(!guard_active || ++guard_hits > 8) {
    (void)!write(1, msg, sizeof msg - 1);
    _exit(125);
  }
  guard_sig = s;
  guard_active = 0;
  siglongjmp(guard_env, 1);
}
static void guard_arm(void) {
  struct sigaction sa;
  memset(&sa, 0, sizeof sa);
  sa.sa_handler = guard_handler;
  sigaction(SIGSEGV, &sa, 0);
  sigaction(SIGBUS, &sa, 0);
  sigaction(SIGABRT, &sa, 0);
}
#define CALL(rvar, evar, expr) do { errno = 0; guard_sig = 0; rvar = -1; if(sigsetjmp(guard_env, 1) == 0) { guard_active = 1; rvar = (expr); } guard_active = 0; evar = errno; } while(0)

/* snwprintf_s and vsnwprintf_s of libSceLibcInternal.sprx (dlsym) on firmware 11.60 ('A null buffer, a size of 0
   or above SIZE_MAX >> 3 elements reports "<name>: bad argument" and returns -1 without writing ... %n or a null %s or %ls argument clears
   buffer[0] ... returns -23'). The console's wchar_t has 16 bits: buffers and formats are char16_t. Prototype int snwprintf_s(char16_t*, size_t,
   const char16_t*, ...); a counting constraint handler is set when set_constraint_handler_s exists (the handler text is kept). Local 16 element
   buffer in a 32 element area filled with 0x7e7e. No %*d and no widths. At most 20 calls, each guarded. NULL buffer and NULL format are single
   calls. */
#include <uchar.h>
typedef int (*sw_fn)(char16_t*, size_t, const char16_t*, ...);
typedef void (*handler_t)(const char*, void*, int);
typedef handler_t (*sch_fn)(handler_t);
static volatile int handler_calls;
static char last_msg[48];
static void hnd(const char* msg, void* p, int e) { (void)p; (void)e; handler_calls++; snprintf(last_msg, sizeof last_msg, "%.46s", msg ? msg : "(null)"); }
static char16_t area[32];
static void show(const char* id, const char* what, int r, int e, int sig) {
  char ex[200];
  char txt[20];
  size_t n = 0;
  for(; n < 16 && area[n] && area[n] != 0x7e7e; n++) txt[n] = area[n] < 0x80 && area[n] >= 32 ? (char)area[n] : '?';
  txt[n] = 0;
  snprintf(ex, sizeof ex, "rc=%d text='%s' a0=%04x a1=%04x a4=%04x a15=%04x a16=%04x handler=%d msg='%s' signal=%d", r, txt, area[0], area[1], area[4], area[15],
           area[16], (int)handler_calls, last_msg, sig);
  probe_case(id, what, sig ? -1 : r, e, ex);
}
#define RESET() do { for(int k_ = 0; k_ < 32; k_++) area[k_] = 0x7e7e; handler_calls = 0; last_msg[0] = 0; } while(0)
int main(void) {
  signal(SIGALRM, probe_alarm);
  alarm(PROBE_MAX_S);
  probe_header();
  guard_arm();
  void* hl = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
  sw_fn f = (sw_fn)(hl ? dlsym(hl, "snwprintf_s") : 0);
  sch_fn sch = (sch_fn)(hl ? dlsym(hl, "set_constraint_handler_s") : 0);
  char ex[8];
  snprintf(ex, sizeof ex, "%d%d%d", !!hl, !!f, !!sch);
  probe_case("0", "dlopen libSceLibcInternal.sprx, dlsym snwprintf_s set_constraint_handler_s (1 yes, 0 no)", 0, 0, ex);
  if(!f || !sch) { probe_end(); return 0; }
  sch(hnd);
  int r, e;
  RESET(); CALL(r, e, f(area, 16, u"ab%dcd", 42));
  show("a.1", "snwprintf_s(buf, 16, \"ab%d\" \"cd\", 42)", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, 4, u"abcdefgh"));
  show("a.2", "snwprintf_s(buf, 4, \"abcdefgh\") truncation", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, 9, u"abcdefgh"));
  show("a.3", "snwprintf_s(buf, 9, \"abcdefgh\") exact fit", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, 0, u"abc"));
  show("b.1", "snwprintf_s(buf, 0, \"abc\")", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, (((size_t)-1) >> 3) + 1, u"abc"));
  show("b.2", "snwprintf_s(buf, (SIZE_MAX>>3)+1, \"abc\")", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, (((size_t)-1) >> 3), u"abc"));
  show("b.3", "snwprintf_s(buf, SIZE_MAX>>3, \"abc\")", r, e, guard_sig);
  int cnt = 0;
  RESET(); CALL(r, e, f(area, 16, u"ab%ncd", &cnt));
  show("c.1", "snwprintf_s(buf, 16, \"ab%ncd\", &cnt)", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, 16, u"ab%scd", (const char*)0));
  show("c.2", "snwprintf_s(buf, 16, \"ab%scd\", NULL)", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, 16, u"ab%lscd", (const char16_t*)0));
  show("c.3", "snwprintf_s(buf, 16, \"ab%lscd\", NULL)", r, e, guard_sig);
  RESET(); CALL(r, e, f((char16_t*)0, 16, u"abc"));
  show("d.1", "snwprintf_s(NULL buffer, 16, \"abc\") (single call)", r, e, guard_sig);
  RESET(); CALL(r, e, f(area, 16, (const char16_t*)0));
  show("d.2", "snwprintf_s(buf, 16, NULL format) (single call)", r, e, guard_sig);
  probe_end();
  return 0;
}
stdout
PROBE	libc_snwprintf_s	fw=0x11600005	build=ff5c4bce7af1	pid=173	utc=2026-11-15T22:17:08Z
CASE	0	dlopen libSceLibcInternal.sprx, dlsym snwprintf_s set_constraint_handler_s (1 yes, 0 no)	0	0	111
CASE	a.1	snwprintf_s(buf, 16, "ab%d" "cd", 42)	6	0	rc=6 text='ab42cd' a0=0061 a1=0062 a4=0063 a15=7e7e a16=7e7e handler=0 msg='' signal=0
CASE	a.2	snwprintf_s(buf, 4, "abcdefgh") truncation	8	0	rc=8 text='abc' a0=0061 a1=0062 a4=7e7e a15=7e7e a16=7e7e handler=0 msg='' signal=0
CASE	a.3	snwprintf_s(buf, 9, "abcdefgh") exact fit	8	0	rc=8 text='abcdefgh' a0=0061 a1=0062 a4=0065 a15=7e7e a16=7e7e handler=0 msg='' signal=0
CASE	b.1	snwprintf_s(buf, 0, "abc")	-1	0	rc=-1 text='' a0=7e7e a1=7e7e a4=7e7e a15=7e7e a16=7e7e handler=1 msg='snwprintf_s: bad argument' signal=0
CASE	b.2	snwprintf_s(buf, (SIZE_MAX>>3)+1, "abc")	-1	0	rc=-1 text='' a0=7e7e a1=7e7e a4=7e7e a15=7e7e a16=7e7e handler=1 msg='snwprintf_s: bad argument' signal=0
CASE	b.3	snwprintf_s(buf, SIZE_MAX>>3, "abc")	3	0	rc=3 text='abc' a0=0061 a1=0062 a4=7e7e a15=7e7e a16=7e7e handler=0 msg='' signal=0
CASE	c.1	snwprintf_s(buf, 16, "ab%ncd", &cnt)	-23	0	rc=-23 text='' a0=0000 a1=0062 a4=7e7e a15=7e7e a16=7e7e handler=1 msg='wprintf_s: %n disallowed' signal=0
CASE	c.2	snwprintf_s(buf, 16, "ab%scd", NULL)	-23	0	rc=-23 text='' a0=0000 a1=0062 a4=7e7e a15=7e7e a16=7e7e handler=1 msg='wprintf_s: bad %s argument' signal=0
CASE	c.3	snwprintf_s(buf, 16, "ab%lscd", NULL)	-23	0	rc=-23 text='' a0=0000 a1=0062 a4=7e7e a15=7e7e a16=7e7e handler=1 msg='wprintf_s: bad %ls argument' signal=0
CASE	d.1	snwprintf_s(NULL buffer, 16, "abc") (single call)	-1	0	rc=-1 text='' a0=7e7e a1=7e7e a4=7e7e a15=7e7e a16=7e7e handler=1 msg='snwprintf_s: bad argument' signal=0
CASE	d.2	snwprintf_s(buf, 16, NULL format) (single call)	-1	0	rc=-1 text='' a0=0000 a1=7e7e a4=7e7e a15=7e7e a16=7e7e handler=1 msg='snwprintf_s: bad format argument' signal=0
END	12

AI-assisted (Claude Code).

@github-actions github-actions Bot added the docs label Nov 16, 2026
@oneandonlydean
oneandonlydean merged commit 4e63035 into boykopovar:main Nov 16, 2026
12 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