Repository navigation
docs(debt): record that a PS5 does not check the key pointer of pthread_key_create - #27934
Conversation
…ad_key_create Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe source ( Run: probe.c (build d4c9e3099d50)#include <errno.h>
#include <pthread.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);
}
/* Watchdog thread: alarm()/SIGALRM does not help when the main thread cannot be scheduled (a CPU mask without a usable
core) or hangs in a blocking call. The thread is created before any mask is changed, sleeps PROBE_MAX_S seconds, writes
TIMEOUT with write() and ends the process with _exit(124). alarm() stays as a second safeguard. */
static void* probe_watchdog(void* arg) {
static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
unsigned left = PROBE_MAX_S;
(void)arg;
while (left) left = sleep(left);
(void)!write(1, msg, sizeof msg - 1);
_exit(124);
return 0;
}
static void probe_watchdog_start(void) {
pthread_t t;
if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}
static int cases = 0;
static void probe_header(void) {
char utc[32];
time_t now = time(0);
probe_watchdog_start();
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 <stdarg.h>
#include <stdlib.h>
static sigjmp_buf guard_env;
static volatile int guard_sig = 0;
static void guard_handler(int s) {
guard_sig = s;
siglongjmp(guard_env, 1);
}
static void setup_guard(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 RUN(stmt) do { errno = 0; guard_sig = 0; if (sigsetjmp(guard_env, 1) == 0) { stmt; } e = errno; } while (0)
#define KB 1024UL
#define MB (1024UL * 1024UL)
/* One CASE line: the extra text is formatted here and always ends with the signal caught by the guard (0 = none). */
static void C(const char* id, const char* call, long r, int e, const char* fmt, ...) {
char x[300];
va_list ap;
va_start(ap, fmt);
vsnprintf(x, sizeof x - 24, fmt, ap);
va_end(ap);
size_t n = strlen(x);
snprintf(x + n, sizeof x - n, " signal=%d", (int)guard_sig);
probe_case(id, call, r, e, x);
}
/* what the console does when a call is given a pointer it cannot write or read, for the entries
of libkernel.md that name host-safety EFAULT fallbacks with unmeasured console behaviour: sceKernelMmap (null or unwritable
res), pthread_getcpuclockid (unwritable clock id), pthread_key_create (unwritable key), pthread_mutex_init and
pthread_mutex_destroy (unreadable mutex or attribute pointer) and scePthreadMutexDestroy; plus __progname / getprogname.
Unusable pointers are an own page set to PROT_NONE and an own page that was mapped and unmapped again. Reference calls with
good pointers run first; the faulting cases run last (a fault inside a pthread call may leave an internal lock taken, so
nothing that needs a pthread lock runs after them). Every call is under the fault guard (a fault is reported as signal=);
the watchdog thread was started before any case. Prototypes: int sceKernelMmap(void*, size_t, int, int, int, long,
void** res), int pthread_getcpuclockid(pthread_t, clockid_t*), int pthread_key_create(pthread_key_t*, void(*)(void*)),
int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*), int pthread_mutex_destroy(pthread_mutex_t*). */
typedef int (*mmap_fn)(void*, size_t, int, int, int, long, void**);
typedef int (*unmap_fn)(void*, size_t);
typedef int (*cpuclk_fn)(void*, int*);
typedef int (*keyc_fn)(unsigned*, void (*)(void*));
typedef int (*keyd_fn)(unsigned);
typedef int (*mxinit_fn)(void*, const void*);
typedef int (*mxdest_fn)(void*);
typedef int (*mxattr_fn)(void*);
typedef int (*sce_mxinit_fn)(void*, const void*, const char*);
typedef void* (*self_fn)(void);
typedef const char* (*getprogname_fn)(void);
typedef long (*dmsize_fn)(void);
typedef int (*alloc_fn)(long, long, size_t, size_t, int, long*);
typedef int (*map_fn)(void**, size_t, int, int, long, size_t);
typedef int (*prot_fn)(void*, size_t, int);
typedef int (*rel_fn)(long, size_t);
int main(void) {
signal(SIGALRM, probe_alarm);
alarm(PROBE_MAX_S);
probe_header();
setup_guard();
mmap_fn f_mmap = (mmap_fn)dlsym(RTLD_DEFAULT, "sceKernelMmap");
unmap_fn f_unmap = (unmap_fn)dlsym(RTLD_DEFAULT, "sceKernelMunmap");
cpuclk_fn f_cpuclk = (cpuclk_fn)dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
keyc_fn f_keyc = (keyc_fn)dlsym(RTLD_DEFAULT, "pthread_key_create");
keyd_fn f_keyd = (keyd_fn)dlsym(RTLD_DEFAULT, "pthread_key_delete");
mxinit_fn f_mxi = (mxinit_fn)dlsym(RTLD_DEFAULT, "pthread_mutex_init");
mxdest_fn f_mxd = (mxdest_fn)dlsym(RTLD_DEFAULT, "pthread_mutex_destroy");
mxattr_fn f_mai = (mxattr_fn)dlsym(RTLD_DEFAULT, "pthread_mutexattr_init");
mxdest_fn f_sce_mxd = (mxdest_fn)dlsym(RTLD_DEFAULT, "scePthreadMutexDestroy");
self_fn f_self = (self_fn)dlsym(RTLD_DEFAULT, "scePthreadSelf");
getprogname_fn f_gpn = (getprogname_fn)dlsym(RTLD_DEFAULT, "getprogname");
void** pn = (void**)dlsym(RTLD_DEFAULT, "__progname");
dmsize_fn f_size = (dmsize_fn)dlsym(RTLD_DEFAULT, "sceKernelGetDirectMemorySize");
alloc_fn f_alloc = (alloc_fn)dlsym(RTLD_DEFAULT, "sceKernelAllocateDirectMemory");
map_fn f_map = (map_fn)dlsym(RTLD_DEFAULT, "sceKernelMapDirectMemory");
prot_fn f_prot = (prot_fn)dlsym(RTLD_DEFAULT, "sceKernelMprotect");
rel_fn f_rel = (rel_fn)dlsym(RTLD_DEFAULT, "sceKernelReleaseDirectMemory");
char line[240];
snprintf(line, sizeof line, "mmap=%d munmap=%d cpuclockid=%d keycreate=%d keydelete=%d mutexinit=%d mutexdestroy=%d mutexattrinit=%d sce_mutexdestroy=%d self=%d getprogname=%d __progname=%d dmem=%d%d%d%d%d", !!f_mmap, !!f_unmap, !!f_cpuclk, !!f_keyc, !!f_keyd, !!f_mxi, !!f_mxd, !!f_mai, !!f_sce_mxd, !!f_self, !!f_gpn, !!pn, !!f_size, !!f_alloc, !!f_map, !!f_prot, !!f_rel);
probe_case("0", "dlsym exported (1 yes, 0 no)", 0, 0, line);
int r, e;
char nm[64];
if (pn || f_gpn) {
const char* a = (pn && *pn) ? (const char*)*pn : "(null)";
const char* g = f_gpn ? f_gpn() : "(none)";
snprintf(nm, sizeof nm, "%.40s", a);
char x[160];
snprintf(x, sizeof x, "__progname=%s getprogname=%.40s equal=%d", nm, g, strcmp(a, g) == 0);
probe_case("1.1", "__progname and getprogname() of this process", 0, 0, x);
}
/* own bad pointers: a PROT_NONE page and a hole, from one own 64 KiB direct-memory mapping */
void* none = 0;
void* hole = 0;
long ph = -1;
if (f_alloc && f_map && f_prot && f_unmap && f_rel) {
long total = f_size ? f_size() : 0x100000000L;
RUN(r = f_alloc(0, total, 1 * MB, 16 * KB, 3, &ph));
if (r == 0 && ph != -1) {
void* a = 0;
RUN(r = f_map(&a, 64 * KB, 3, 0, ph, 0));
if (a) {
none = a;
hole = (char*)a + 32 * KB;
f_prot(none, 16 * KB, 0);
f_unmap(hole, 16 * KB);
probe_case("1.2", "own PROT_NONE page and own hole prepared", 0, 0, "ok");
}
}
}
/* references with good pointers */
if (f_mmap && f_unmap) {
void* res = 0;
RUN(r = f_mmap(0, 64 * KB, 3, 0x1002, -1, 0, &res));
char x[80];
snprintf(x, sizeof x, "res_set=%d", res != 0);
C("2.1", "sceKernelMmap(anon 64 KiB, prot 3, res = good pointer)", r, e, "%s", x);
if (res) f_unmap(res, 64 * KB);
}
void* me = f_self ? f_self() : 0;
if (f_cpuclk && me) {
int clk = -77;
RUN(r = f_cpuclk(me, &clk));
C("3.1", "pthread_getcpuclockid(self, good pointer)", r, e, "clockid=%d", clk);
}
unsigned char mx[128];
memset(mx, 0, sizeof mx);
if (f_mxi && f_mxd) {
RUN(r = f_mxi(mx, 0));
C("4.1", "pthread_mutex_init(good mutex, NULL attr)", r, e, "-");
RUN(r = f_mxd(mx));
C("4.2", "pthread_mutex_destroy(good mutex)", r, e, "-");
}
unsigned key = 0;
if (f_keyc) {
RUN(r = f_keyc(&key, 0));
C("5.1", "pthread_key_create(good pointer)", r, e, "key_set=%d", key != 0);
if (r == 0 && f_keyd) f_keyd(key);
}
/* faulting cases last */
if (f_mmap && f_unmap) {
RUN(r = f_mmap(0, 64 * KB, 3, 0x1002, -1, 0, 0));
C("2.2", "sceKernelMmap(anon 64 KiB, res = NULL)", r, e, "-");
if (none) {
RUN(r = f_mmap(0, 64 * KB, 3, 0x1002, -1, 0, (void**)none));
C("2.3", "sceKernelMmap(anon 64 KiB, res = PROT_NONE page)", r, e, "-");
}
if (hole) {
RUN(r = f_mmap(0, 64 * KB, 3, 0x1002, -1, 0, (void**)hole));
C("2.4", "sceKernelMmap(anon 64 KiB, res = unmapped page)", r, e, "-");
}
}
if (f_cpuclk && me) {
RUN(r = f_cpuclk(me, 0));
C("3.2", "pthread_getcpuclockid(self, NULL clock id)", r, e, "-");
if (none) {
RUN(r = f_cpuclk(me, (int*)none));
C("3.3", "pthread_getcpuclockid(self, PROT_NONE page)", r, e, "-");
}
if (hole) {
RUN(r = f_cpuclk(me, (int*)hole));
C("3.4", "pthread_getcpuclockid(self, unmapped page)", r, e, "-");
}
}
if (f_mxi && f_mxd && f_mai) {
unsigned char at[64];
memset(at, 0, sizeof at);
f_mai(at);
if (none) {
RUN(r = f_mxi(mx, none));
C("4.3", "pthread_mutex_init(good mutex, attribute = PROT_NONE page)", r, e, "-");
RUN(r = f_mxi(none, 0));
C("4.4", "pthread_mutex_init(mutex = PROT_NONE page, NULL attr)", r, e, "-");
RUN(r = f_mxd(none));
C("4.5", "pthread_mutex_destroy(PROT_NONE page)", r, e, "-");
}
if (hole) {
RUN(r = f_mxi(mx, hole));
C("4.6", "pthread_mutex_init(good mutex, attribute = unmapped page)", r, e, "-");
RUN(r = f_mxi(hole, 0));
C("4.7", "pthread_mutex_init(mutex = unmapped page, NULL attr)", r, e, "-");
RUN(r = f_mxd(hole));
C("4.8", "pthread_mutex_destroy(unmapped page)", r, e, "-");
}
if (f_sce_mxd && none) {
RUN(r = f_sce_mxd(none));
C("4.9", "scePthreadMutexDestroy(PROT_NONE page)", r, e, "-");
}
}
if (f_keyc) {
RUN(r = f_keyc(0, 0));
C("5.2", "pthread_key_create(NULL pointer)", r, e, "-");
if (none) {
RUN(r = f_keyc((unsigned*)none, 0));
C("5.3", "pthread_key_create(PROT_NONE page)", r, e, "-");
}
if (hole) {
RUN(r = f_keyc((unsigned*)hole, 0));
C("5.4", "pthread_key_create(unmapped page)", r, e, "-");
}
}
if (ph != -1 && none) {
if (f_prot) f_prot(none, 16 * KB, 3);
f_unmap(none, 16 * KB);
f_unmap((char*)none + 16 * KB, 16 * KB);
f_rel(ph, 1 * MB);
}
probe_end();
return 0;
}Run: raw output (26 lines)AI-assisted (Claude Code). |
|
No actionable comments were generated in the recent review. Walkthrough
Change: Other Merge Risk:
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this documentation change; the points below are optional.
What I ran
Reviewed head 48413678fba28e403bdeaaa5bd49dbb46f45e09a. No commands or tests were run. I read description.md, commits.md, manifest.json, diff.patch, and the full changed file; I also searched the debt documentation for pthread_key_create.
Results
| Check | Result | Note |
|---|---|---|
| Change review | passed | Wording matches the stated probe scope |
| Tests | not run | Documentation-only change |
Findings
Blocking
None.
Non-blocking
None.
Not verified
The PS5 probe itself and the underlying implementations were not independently verified. The changed file was read; no files were excluded by manifest.json.
To get this to a good state
Nothing is required.
ligmaSec
left a comment
There was a problem hiding this comment.
debt_fold.py --check passes. The updated entry preserves the host EFAULT and key cleanup behavior while recording the console pointer cases and the firmware and process used for the measurements.
AI-assisted: yes.
What
The debt entry for
pthread_key_create(docs/dev/debt/libkernel.md:435onmainea49f32e8982), as the pending fragmentdocs/dev/debt/changes/fix-libkernel-pthread-key-create-guest-copy.md(added by #21577, merged on 2026-11-16) changes it, calls theEFAULTresult for an unwritable key pointer a host-safety fallback with unmeasured console behaviour. A probe on a PS5 (firmware 11.60, payload process; a title process may differ) calledpthread_key_createwith a pointer to its own variable and a NULL destructor: it returned 0 (case 5.1, line 9 of the raw output in the first comment). The calls with a NULL key pointer (case 5.2, line 23), a pointer to a PROT_NONE page (case 5.3, line 24) and a pointer to an unmapped page (case 5.4, line 25) each ended in a fault: the probe's handler, installed for SIGSEGV, SIGBUS and SIGABRT (probe.c:89to:91), stored the signal number (probe.c:81) and eachCASEline showssignal=11, a segmentation fault. The run was made once (END 24, line 26); the pointer cases ran last. So the console checked none of the three pointers.scePthreadKeyCreatewas not in that run (it is not among the functions the probe looked up, line 2).On
mainpthread_key_createreturnsEINVALfor a NULL key pointer (core/libs/prx/libkernel/Pthread/Posix/Tsd.cpp:19) and, when it cannot write the key it created, frees that key and returnsEFAULT(:22to:25);scePthreadKeyCreatethrows for a NULL key (core/libs/prx/libkernel/Pthread/src/Tsd.cpp:92). Both results are host-side checks and not what the console does.This pull request changes the one entry in the existing fragment (1 line added and 1 removed, no new file): the entry grows from 610 to 946 characters with its list marker. It keeps the account of the
EFAULTand the freed key, replacesThis is a host-safety fallback with unmeasured console behaviour.by the measurement (a writable pointer returned 0; a NULL pointer, a pointer to a PROT_NONE page and a pointer to an unmapped page each took SIGSEGV, in one run), says that theEFAULThere is a host-safety result and not the console's, and that theEINVALfor a NULL key pointer is a host-safety result too, and adds thatscePthreadKeyCreatewas not run there. No code changes.Not measured:
scePthreadKeyCreate, a read-only key pointer, a title process, other firmware.Tested
Measured on a PS5 with firmware 11.60 from a homebrew payload process, one run at about 04:24 UTC on 2026-11-09 (probe build d4c9e3099d50,
END 24). This is a documentation change; no test was added or run.On the head merged into
mainat ea49f32 (2026-11-27 20:14 UTC) I rantools/check_conventions.py --base upstream/mainwith this title and description (All conventions checks passed, exit code 0) andtools/debt_fold.py --check(exit code 0, no message). The commit has one author,xXPBSXxwith the noreply address, and changes one line of an existing fragment (mode 100644).Overlap: counted on 2026-11-27 from the 2088 open pull requests (file lists from the GitHub API, the pull requests with 100 or more files read in full through the files API and, for those and for the pull requests with an old base, the real diff against the merge base; the patches of the 1268 pull requests that change a file under
docs/dev/debt/,Tsd.cpp,Strings.cpporMath.cpp, or have pthread, key, div or tsd in the title or branch name, were searched forpthread_key_create,scePthreadKeyCreate,PthreadKeyCreate,ldiv,lldiv,imaxdiv,INT_MIN,INT64_MINand the two fragment names, from 18:16 to 19:57 UTC). No open pull request changes the fragmentdocs/dev/debt/changes/fix-libkernel-pthread-key-create-guest-copy.mdor the entry forpthread_key_createatdocs/dev/debt/libkernel.md:435, and no commit onmainsince13bf980d9659(the base of this branch; 124 commits, up toea49f32e8982) changed the fragment, the two entries atlibkernel.md:435andlibc.md:431,core/libs/prx/libkernel/Pthread/Posix/Tsd.cpp,core/libs/prx/libc/src/Strings.cpporcore/libs/prx/libc/src/Math.cpp; the fragment is still pending (the entry inlibkernel.mdhas the text from before #21577). A search of the open pull requests forpthread_key_createfinds #18190 and #18112 (feat(libs): mirror libkernel exports through libkernel_sysand... libkernel_web), and forscePthreadKeyCreatenone: #18190 movespthread_key_create_nid_postfixout ofTsd.cppinto provider files and changes onlydocs/dev/debt/libkernel_sys.mdamong the debt files, #18112 changeslibkernel_web.md; neither changes the entry:435or the fragment (the entry's link toTsd.cppstays as it is until that pull request merges). One more patch namesscePthreadKeyCreate: #9938 (fix(libkernel): honor caller-provided pthread stacks, a test call, and other entries oflibkernel.md). The merges of this head with each of the 96 pull requests that changelibc.md,libkernel.md,Math.cpp,Strings.cpporTsd.cppor have a searched name, or have 100 or more files or an old base, were made withgit merge-treefrom 18:16 to 19:57 UTC (mainat9f12ec0e3840at the start): 57 are clean; the other 39 (among them #18190 in 2 files and #9938 in 3 files) already conflict withmainalone, in at least as many files as with this head, so none of them conflicts because of this change. The count was repeated for the 613 pull requests that were opened or updated since 18:16 UTC (of the 2083 open pull requests, listed from 19:58 to 20:12 UTC; 394 of them were patch-scanned for the same names): no new hit beyond the ones named, and the 8 that change a searched file merge cleanly with this head. A merge of this head intomainat ea49f32 was checked the same way at 19:59 UTC and is clean, and it merges with the second head of this series (docs-libc-div-int-min-console-1160.md, a new file of its own) without a conflict. This pull request does not depend on any of them (Depends on: none).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)Summary by CodeRabbit
pthread_key_create, including results for writable and invalid key pointers. Clarified that host-safety error results are not confirmed console behavior and that results may vary by process.