Skip to content

docs(debt): record the _Mtx_current_owns results on firmware 11.60 - #22020

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libc-mtx-current-owns-console-1160
Nov 17, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libc-mtx-current-owns-console-1160

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

The _Mtx_current_owns entry in docs/dev/debt/libc.md says that this library returns 1 and ignores its argument, following a firmware 12.00 reading reported in PR8859, and that other firmware versions and the argument's console type remain unverified. A probe has now called the console's _Mtx_current_owns (libSceLibcInternal.sprx) from a payload process on firmware 11.60. The pull request adds one sentence to the entry and changes "The firmware observation" to "The 12.00 observation", through a debt fragment, as the contribution rules ask; no code changes.

On firmware 11.60 _Mtx_current_owns returned 1 in all six calls: on an initialized plain mutex before it was locked (case a.1, line 4), while this thread held it (a.2, line 5) and after unlocking (a.3, line 6); on another mutex while a different thread held it (case b, line 7); on a zeroed slot that was never initialized (c.1, line 8); and after _Mtx_destroy (c.2, line 9). So the console's answer does not depend on the state of the mutex or on which thread holds it in these cases, which is what ignoring the argument gives; this library's _Mtx_current_owns_nid_postfix in Threads.cpp returns 1 without reading its argument.

Not measured: other argument values (the probe passed pointers to mutexes and to a zeroed slot), the argument's console type, 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 and takes _Mtx_init, _Mtx_lock, _Mtx_unlock, _Mtx_current_owns and _Mtx_destroy with dlsym, on 2026-11-16 at 23:38 UTC. A title process may differ. The cases are 0 to c.2, lines 2 to 9 of the raw output in the first comment (line 2 shows that all five symbols were found).

Call PS5 payload process, firmware 11.60 AnyPS5 main (code read) in the entry
before locking / locked by the caller / after unlocking 1 / 1 / 1 (lines 4 to 6) 1 now stated
held by another thread 1 (line 7) 1 now stated
zeroed never initialized slot / after _Mtx_destroy 1 / 1 (lines 8, 9) 1 now stated

tools/check_conventions.py --base upstream/main passes with no error and no warning (the entry is 653 characters, up from 453). This is a documentation change, so no test was added or run, and no GPU test applies. Not tested: the emulator against these calls, a title process, other firmware, 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-mtx-current-owns-console-1160.md, a new file) or the debt entry it changes, found by reading the changed files of all 1593 open pull requests at about 00:37 UTC, by reading what they change in docs/dev/debt/libc.md and in the fragments under docs/dev/debt/changes/ for _Mtx_current_owns, and by searching their titles and branch names for the same names. The branch was test-merged on 2026-11-16 at about 01:13 UTC against main c50f19c4b8e (the branch is based on c40f058298d, an ancestor of it) and merges into it without a conflict; tools/debt_fold.py --check applies the fragment to the debt file of that main without an error (checked without the fragment libressl3-20171-inventory.md, which already conflicts with feat-libressl3-evp-pkey-new-mac-key-v2.md on main and is not part of this change), the text it changes occurs in exactly one entry, and no other fragment changes that entry. Five other open pull requests change Threads.cpp (#18347, #18122, #12315, #9447, #1835); none of them changes a line that names _Mtx_current_owns or this debt entry (#18347 changes the _Mtx_trylock entry), and #12315 lists the file but changes nothing in it against main. #17555, #17556, #17557 and #17558 touch libc.md elsewhere. 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 17, 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, c10298635622) and its raw output from the run on 2026-11-16 at 23:38 UTC. The clock in the PROBE line is the console's own clock. The probe loads libSceLibcInternal.sprx, takes the _Mtx_ functions with dlsym and calls _Mtx_current_owns on a plain mutex before locking, locked, after unlocking, while another thread holds a second mutex, on a zeroed slot and after _Mtx_destroy; 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)

/* _Mtx_current_owns of libSceLibcInternal.sprx (dlsym) on firmware 11.60 ('returns 1 and ignores its argument ... PS5 12.00
   body reading reported in PR8859 ... other firmware versions and the argument's console type remain unverified'). Prototype int _Mtx_current_owns(void** m);
   helpers int _Mtx_init(void**, int), int _Mtx_lock(void**), int _Mtx_unlock(void**), void _Mtx_destroy(void**). Cases: a plain mutex locked by this thread and after
   unlock; b a mutex held by ANOTHER thread, queried by the main thread; c an initialised-then-destroyed handle and an empty slot with value 0 (single
   calls, guarded); the NULL pointer is not passed. Own objects only; the helper thread is joined. 10 calls, each guarded. */
#include <stdatomic.h>
typedef int (*mi_fn)(void**, int);
typedef int (*mo_fn)(void**);
typedef void (*md_fn)(void**);
static mi_fn minit; static mo_fn mlock, munlock, mowns; static md_fn mdestroy;
static void* mtx2;
static atomic_int held, release;
static void* holder(void* a) {
  (void)a;
  mlock(&mtx2);
  atomic_store(&held, 1);
  int spin = 0;
  while(!atomic_load(&release) && spin++ < 500000) usleep(100);
  munlock(&mtx2);
  return 0;
}
static void q(const char* id, const char* what, void** m) {
  int rc, e, r = -99;
  CALL(rc, e, (r = mowns(m)));
  char ex[60];
  snprintf(ex, sizeof ex, "value=%d signal=%d", r, (int)guard_sig);
  probe_case(id, what, guard_sig ? -1 : r, e, ex);
}
int main(void) {
  signal(SIGALRM, probe_alarm);
  alarm(PROBE_MAX_S);
  probe_header();
  guard_arm();
  void* hl = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
  minit = (mi_fn)(hl ? dlsym(hl, "_Mtx_init") : 0);
  mlock = (mo_fn)(hl ? dlsym(hl, "_Mtx_lock") : 0);
  munlock = (mo_fn)(hl ? dlsym(hl, "_Mtx_unlock") : 0);
  mowns = (mo_fn)(hl ? dlsym(hl, "_Mtx_current_owns") : 0);
  mdestroy = (md_fn)(hl ? dlsym(hl, "_Mtx_destroy") : 0);
  char ex[10];
  snprintf(ex, sizeof ex, "%d%d%d%d%d%d", !!hl, !!minit, !!mlock, !!munlock, !!mowns, !!mdestroy);
  probe_case("0", "dlopen LibcInternal; dlsym _Mtx_init _Mtx_lock _Mtx_unlock _Mtx_current_owns _Mtx_destroy (1 yes, 0 no)", 0, 0, ex);
  if(!minit || !mlock || !munlock || !mowns || !mdestroy) { probe_end(); return 0; }
  void* m1 = 0;
  int ir = minit(&m1, 1);
  char l[40]; snprintf(l, sizeof l, "init_rc=%d", ir);
  probe_case("a.0", "_Mtx_init(&m1, plain)", ir, 0, l);
  q("a.1", "_Mtx_current_owns(&m1) before locking", &m1);
  mlock(&m1);
  q("a.2", "_Mtx_current_owns(&m1) locked by this thread", &m1);
  munlock(&m1);
  q("a.3", "_Mtx_current_owns(&m1) after unlock", &m1);
  mtx2 = 0;
  minit(&mtx2, 1);
  atomic_store(&held, 0); atomic_store(&release, 0);
  pthread_t th;
  int cr = pthread_create(&th, 0, holder, 0);
  for(int i = 0; i < 2000 && !atomic_load(&held); i++) usleep(1000);
  q("b", "_Mtx_current_owns(&mtx2) while ANOTHER thread holds it", &mtx2);
  atomic_store(&release, 1);
  if(cr == 0) pthread_join(th, 0);
  void* empty = 0;
  q("c.1", "_Mtx_current_owns(&empty_slot) (value 0, never initialised)", &empty);
  mdestroy(&mtx2);
  q("c.2", "_Mtx_current_owns(&mtx2) after _Mtx_destroy", &mtx2);
  mdestroy(&m1);
  probe_end();
  return 0;
}
stdout
PROBE	libc_mtx_current_owns	fw=0x11600005	build=c10298635622	pid=197	utc=2026-11-17T01:20:03Z
CASE	0	dlopen LibcInternal; dlsym _Mtx_init _Mtx_lock _Mtx_unlock _Mtx_current_owns _Mtx_destroy (1 yes, 0 no)	0	0	111111
CASE	a.0	_Mtx_init(&m1, plain)	0	0	init_rc=0
CASE	a.1	_Mtx_current_owns(&m1) before locking	1	0	value=1 signal=0
CASE	a.2	_Mtx_current_owns(&m1) locked by this thread	1	0	value=1 signal=0
CASE	a.3	_Mtx_current_owns(&m1) after unlock	1	0	value=1 signal=0
CASE	b	_Mtx_current_owns(&mtx2) while ANOTHER thread holds it	1	0	value=1 signal=0
CASE	c.1	_Mtx_current_owns(&empty_slot) (value 0, never initialised)	1	0	value=1 signal=0
CASE	c.2	_Mtx_current_owns(&mtx2) after _Mtx_destroy	1	0	value=1 signal=0
END	8

AI-assisted (Claude Code).

@github-actions github-actions Bot added the docs label Nov 17, 2026
@oneandonlydean
oneandonlydean merged commit 05ea302 into boykopovar:main Nov 17, 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