Repository navigation
docs(debt): record the console results of snwprintf_s - #21896
Merged
oneandonlydean merged 1 commit intoNov 16, 2026
Merged
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
Author
|
Probe source (built with ps5-payload-sdk v0.45; 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;
}stdoutAI-assisted (Claude Code). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The
snwprintf_s and vsnwprintf_sentry indocs/dev/debt/libc.mdfollows the bundledlibc.prxmodules of two titles and has no console measurement. A probe on a PS5 has now calledsnwprintf_sfrom a payload process, inlibSceLibcInternal, with a counting constraint handler, on 16-bitchar16_tbuffers. 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_sreturned 6 forab%dcdwith 42 into 16 elements (line 3); into 4 elements it returned the full length 8 and leftabcand 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) + 1each returned -1 after one handler call with "snwprintf_s: bad argument" and wrote nothing (lines 6 and 7), while a size ofSIZE_MAX >> 3returned 3 (line 8).%n, a null%sand a null%lseach 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:
FormatWideSecureinFormattingWide.cpp(lines 292 to 336) withsnwprintf_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_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loadslibSceLibcInternal.sprxwithdlopenand takessnwprintf_sandset_constraint_handler_swithdlsym, 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 with0x7e7e.main(code read, not run)ab%dcdwith 42, size 16 / 4 / 9abc/ 8 (lines 3 to 5)(SIZE_MAX >> 3) + 1SIZE_MAX >> 3%n/ null%s/ null%lstools/check_conventions.py --base upstream/mainpasses 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 indocs/dev/debt/libc.mdand in the fragments underdocs/dev/debt/changes/forsnwprintf_sandvsnwprintf_s, and by searching their titles and branch names forsnwprintf_sandvsnwprintf_s. The branch was test-merged on 2026-11-16 at about 19:49 UTC againstmaine2e77aa74a5(the branch is based onf4381f7ade3, an ancestor of it) and merges into it without a conflict;tools/debt_fold.py --checkapplies the fragment to thelibcdebt file of thatmainwithout 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 thesnwprintf_sentry oflibc.mdaboutRSIZE_MAXand the narrowsnprintf_s. It already conflicts withmainwithout 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 listsFormattingWide.cppbut changes nothing in it againstmain, 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)