Skip to content

docs(debt): record the hardware_concurrency result on firmware 11.60 - #22023

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

oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libc-hardware-concurrency-console-1160

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

The std::thread::hardware_concurrency entry in docs/dev/debt/libc.md says that this library returns 13, the number of CPUs in libkernel's default thread mask, and that in payload processes whose threads started with mask 0xea00 the console's libSceLibcInternal returned 5 on firmware 13.60 and 12.70. A probe has now called it from a payload process on firmware 11.60. The pull request adds one sentence to the entry through a debt fragment, as the contribution rules ask; no code changes.

On firmware 11.60 hardware_concurrency returned 5 (case a, line 3). The probe could not read the thread's affinity mask there (cpuset_getaffinity failed, so no mask is shown), so it cannot say whether that mask is 0xea00 as on the other firmware versions; the entry therefore says "On 11.60 it also returned 5, but the mask could not be read there." The part of the probe that sets a process-wide mask depends on reading the original mask and did not run. This library still returns 13 and the entry still says that what a title sees is not measured.

Not measured: the starting mask on 11.60, the effect of a process-wide cpuset_setaffinity there, and a title process (PPSA34015, named in the entry, imports the function).

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 the function with dlsym under its mangled name _ZNSt6thread20hardware_concurrencyEv, on 2026-11-16 at 23:38 UTC. A title process may differ. The cases used are 0 and a, lines 2 and 3 of the raw output in the first comment (the probe also read srandomdev, random and srandom; those results are in a separate pull request).

Call PS5 payload process, firmware 11.60 AnyPS5 main in the entry
hardware_concurrency() 5 (line 3); the affinity mask could not be read 13 (default thread mask 0x1FFF) now stated for 11.60

tools/check_conventions.py --base upstream/main passes with no error and no warning (the entry is 908 characters, up from 841). This is a documentation change, so no test was added or run, and no GPU test applies. Not tested: the emulator against this call, 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-hardware-concurrency-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 std::thread::hardware_concurrency, 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 3b7ffd7a05c, 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). #9447 adds std::__sce_v2::_Thrd_hardware_concurrency, a different function with its own debt entry that returns the population count of a thread's affinity mask, and it already conflicts with main in Threads.cpp without this change; none of them changes this debt entry or a line that names std::thread::hardware_concurrency, and #12315 lists the file but changes nothing in it against main. The pull request for srandom and srandomdev (the same probe, a different file) changes neither this entry nor this file. 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, 1f6691d286e9) 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 hardware_concurrency, srandomdev, random and srandom with dlsym, and calls hardware_concurrency while trying to read the thread's affinity mask with cpuset_getaffinity; the srandomdev part (case c) is the subject of another pull request. 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)

/* srandomdev and std::thread::hardware_concurrency of libSceLibcInternal.sprx (dlsym) on firmware 11.60 ('srandomdev
   ... not verified against the console' and 'the starting mask ... and whether the console's count follows a process-wide
   cpuset_setaffinity is not measured; this returns 13'). Part c: srandomdev(); random() x4; srandomdev() again; random() x4; srandom(1) and random()
   x4 as a counter-test (only 'different yes or no' and the export). Part a: hardware_concurrency() against the number of set bits of this process's
   affinity mask from cpuset_getaffinity. Part b: the process-wide mask (CPU_WHICH_PID, id -1) is set to 0x3 and hardware_concurrency is read again, then the
   ORIGINAL mask is restored at once and verified; only the probe process is affected, a 5 s alarm limit covers a stuck call. KERN_ARND is not touched.
   About 14 calls, each guarded. */
#include <sys/param.h>
#include <sys/cpuset.h>
typedef unsigned (*hc_fn)(void);
typedef void (*sd_fn)(void);
typedef long (*rd_fn)(void);
typedef void (*sr_fn)(unsigned);
static int count_bits(const cpuset_t* m) { int n = 0; for(int i = 0; i < CPU_SETSIZE; i++) if(CPU_ISSET(i, m)) n++; return n; }
static unsigned long first_word(const cpuset_t* m) { unsigned long w = 0; memcpy(&w, m, sizeof w); return w; }
int main(void) {
  signal(SIGALRM, probe_alarm);
  alarm(PROBE_MAX_S);
  probe_header();
  guard_arm();
  void* hl = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
  hc_fn hc = (hc_fn)(hl ? dlsym(hl, "_ZNSt6thread20hardware_concurrencyEv") : 0);
  sd_fn sdev = (sd_fn)(hl ? dlsym(hl, "srandomdev") : 0);
  rd_fn rnd = (rd_fn)(hl ? dlsym(hl, "random") : 0);
  sr_fn sr = (sr_fn)(hl ? dlsym(hl, "srandom") : 0);
  char ex[16];
  snprintf(ex, sizeof ex, "%d%d%d%d%d", !!hl, !!hc, !!sdev, !!rnd, !!sr);
  probe_case("0", "dlopen LibcInternal; dlsym hardware_concurrency srandomdev random srandom (1 yes, 0 no)", 0, 0, ex);
  int rc, e;
  char line[200];
  cpuset_t orig;
  int have_orig = 0;
  CPU_ZERO(&orig);
  CALL(rc, e, (have_orig = (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof orig, &orig) == 0)));
  if(hc) {
    unsigned v = 0xdead;
    CALL(rc, e, (v = hc(), 0));
    snprintf(line, sizeof line, "value=%u mask_bits=%d mask_word0=0x%lx getaffinity_ok=%d signal=%d", v, have_orig ? count_bits(&orig) : -1,
             have_orig ? first_word(&orig) : 0UL, have_orig, (int)guard_sig);
    probe_case("a", "hardware_concurrency() against the thread's affinity mask", guard_sig ? -1 : (int)v, e, line);
  }
  if(hc && have_orig) {
    cpuset_t two, back;
    CPU_ZERO(&two); CPU_SET(0, &two); CPU_SET(1, &two);
    int sr0 = -9;
    CALL(rc, e, (sr0 = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof two, &two)));
    int e0 = e;
    unsigned v2 = 0xdead;
    CALL(rc, e, (v2 = hc(), 0));
    int rs = -9;
    CALL(rc, e, (rs = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof orig, &orig)));
    CPU_ZERO(&back);
    int gb = -9;
    CALL(rc, e, (gb = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof back, &back)));
    unsigned v3 = 0xdead;
    CALL(rc, e, (v3 = hc(), 0));
    snprintf(line, sizeof line, "set_rc=%d errno=%d value_after_set=%u restore_rc=%d restored_equal=%d value_after_restore=%u", sr0, e0, v2, rs,
             gb == 0 && memcmp(&back, &orig, sizeof back) == 0, v3);
    probe_case("b", "process-wide affinity {0,1}, hardware_concurrency(), then restore", sr0, e0, line);
  }
  if(sdev && rnd && sr) {
    long a[4] = {0, 0, 0, 0}, b[4] = {0, 0, 0, 0}, c[4] = {0, 0, 0, 0};
    CALL(rc, e, (sdev(), 0));
    for(int i = 0; i < 4; i++) { CALL(rc, e, (a[i] = rnd(), 0)); }
    CALL(rc, e, (sdev(), 0));
    for(int i = 0; i < 4; i++) { CALL(rc, e, (b[i] = rnd(), 0)); }
    CALL(rc, e, (sr(1), 0));
    for(int i = 0; i < 4; i++) { CALL(rc, e, (c[i] = rnd(), 0)); }
    int diff = memcmp(a, b, sizeof a) != 0;
    snprintf(line, sizeof line, "sequences_differ=%d first_after_srandom1=%lx,%lx,%lx,%lx signal=%d", diff, c[0], c[1], c[2], c[3], (int)guard_sig);
    probe_case("c", "srandomdev() x2 with 4 random() each: sequences differ? and srandom(1) known sequence", diff, 0, line);
  }
  probe_end();
  return 0;
}
stdout
PROBE	libc_srandomdev_hwconc	fw=0x11600005	build=1f6691d286e9	pid=198	utc=2026-11-17T01:20:04Z
CASE	0	dlopen LibcInternal; dlsym hardware_concurrency srandomdev random srandom (1 yes, 0 no)	0	0	11111
CASE	a	hardware_concurrency() against the thread's affinity mask	5	0	value=5 mask_bits=-1 mask_word0=0x0 getaffinity_ok=0 signal=0
CASE	c	srandomdev() x2 with 4 random() each: sequences differ? and srandom(1) known sequence	1	0	sequences_differ=1 first_after_srandom1=6b8b4567,327b23c6,643c9869,66334873 signal=0
END	3

AI-assisted (Claude Code).

@github-actions github-actions Bot added the docs label Nov 17, 2026
@oneandonlydean
oneandonlydean merged commit f907484 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