Skip to content

fix(libSceFiber): write the option block magic and reject a misaligned block as the console does - #23393

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/fiber-optparam-initialize-magic
Nov 19, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/fiber-optparam-initialize-magic

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

sceFiberOptParamInitialize in core/libs/prx/libSceFiber/Export.cpp wrote 128 zero bytes and had no alignment check; a pointer such as 0x1 reached the guest copy, which failed, and the call returned SCE_FIBER_ERROR_INVALID (0x80590004). A PS5 payload process on firmware 11.60 called the console's function and read the block back. The console writes 128 bytes: the bytes 0 to 3 are 4d e6 40 bb, the 32-bit value 0xBB40E64D in little-endian order, and the bytes 4 to 127 are zero (second probe, case 1.1, lines 4 to 12, with the call repeated in 1.2, lines 13 to 21; the first probe shows 128 bytes changed and the canary behind the block intact, lines 4 and 5). Buffers at +8, +16 and +32 returned 0, wrote 128 bytes from the offset and had the same magic there (2.1 to 2.3, lines 22 to 24). The offsets +1, +2 and +4 returned SCE_FIBER_ERROR_ALIGNMENT (0x80590002) and wrote nothing (first probe, 2.1 to 2.3, lines 6 to 8), the pointer 0x1 returned 0x80590002 as well (3.2, line 10), and NULL returned SCE_FIBER_ERROR_NULL (0x80590001, 3.1, line 9). The pointer 0x1000 faulted with SIGSEGV and wrote none of the first 120 bytes (3.3, line 11), and so did a block on a read-only page (second probe, 3.1, line 25).

The fix changes sceFiberOptParamInitialize to follow that: NULL still returns 0x80590001, a pointer that is not 8-byte aligned now returns 0x80590002 before anything is written, and the block written is the magic 0xBB40E64D at the bytes 0 to 3 and zero for the rest; a block the host cannot write still returns SCE_FIBER_ERROR_INVALID, where the console faults, which the debt entry of sceFiberOptParamInitialize now says. The 8-byte rule is the one Export.cpp already applies to the fiber object (object & 7); on the console +1, +2, +4 (fail) and +8, +16, +32 (succeed) were measured, +3 and +5 to +7 were not, so the rule for those offsets is an extension, and the test pins only the measured offsets.

The test commit changes core/libs/tests/Fiber.cpp: the old check that the block is all zero becomes a check of the magic and of the zero bytes 4 to 127 at the offsets 0, +8, +16 and +32, with the bytes around the block and a sentinel behind it intact; the offsets +1, +2, +4 and the pointer 0x1 must return SCE_FIBER_ERROR_ALIGNMENT and write nothing. The documentation commit adds a debt fragment (docs/dev/debt/changes/fix-fiber-optparam-initialize-magic.md) that replaces the one debt sentence of sceFiberOptParamInitialize to state the fault at 0x1000 and on a read-only page and the unmeasured offsets.

The effect on titles is small: _sceFiberInitializeImpl and _sceFiberInitializeWithInternalOptionImpl do not read the option block here ((void)opt_param), so the magic and the alignment error matter only to a title that reads the block back or checks the result; no such title is known. The block's meaning beyond the magic is not known.

Not measured: the offsets +3 and +5 to +7, a block in a title process, and other firmware.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from homebrew payload processes (ps5-payload-sdk v0.45, elfldr) that load libSceFiber.sprx and take sceFiberOptParamInitialize with dlsym, on 2026-11-18 at 16:37 UTC and 20:03 UTC. A title process may differ. The probe sources and the complete output of both runs are in the first comment.

Call PS5 payload process, firmware 11.60 AnyPS5 main before (code read; the test commit fails on it) here
aligned buffer rc 0, 128 bytes written, bytes 0 to 3 4d e6 40 bb, rest zero rc 0, 128 zero bytes as the console
+8, +16, +32 rc 0, magic at the offset rc 0, zeros as the console
+1, +2, +4, and 0x1 0x80590002, nothing written rc 0 (+1, +2, +4); 0x80590004 (0x1) 0x80590002, nothing written
NULL 0x80590001 0x80590001 same
0x1000, read-only page SIGSEGV, nothing written 0x80590004 0x80590004 (host fallback, stated in the entry)

Red and green, on one base: Windows 10 Pro (MinGW GCC, Release, ninja -j4), base upstream/main a1dc368acc8, one build per phase (checkout, cmake, ninja, then ctest -R ^fiber, which runs fiber and fiber_contextless): on the base both tests pass; with the test commit 78145dc01e8 (tree ade7e9dddead) fiber fails (exit code 0xc0000409, "Fiber check failed: misaligned opt param") and fiber_contextless passes; with the fix commit a0cf746f1be (tree 15300a111f58) both pass; with the documentation commit b1637fec884 (tree 497bc243f301) both pass. tools/debt_fold.py --check and the convention check pass on the head. A merge into upstream/main 9a50e1014ee was checked with git merge-tree and is clean. Linux was not run here; CI will.

tools/check_conventions.py --base upstream/main passes with no error. The change is 3 files, +37/-12, with no new comments in code.

Overlap: the other open pull requests that change the files of this branch (core/libs/prx/libSceFiber/Export.cpp, core/libs/tests/Fiber.cpp and the fragment docs/dev/debt/changes/fix-fiber-optparam-initialize-magic.md, a new file, for the entry sceFiberOptParamInitialize in docs/dev/debt/libSceFiber.md), found by reading the changed files of all 2014 open pull requests at about 22:59 to 23:16 UTC, by reading what they change in those files and in the debt files for sceFiberOptParamInitialize and FiberOptParam, and by searching their titles and branch names for the same names (no hit), were checked on 2026-11-18 at about 23:02 UTC against main 9a50e1014ee together with this head: the branch is based on a1dc368acc8, an ancestor of it, merges into main without a conflict, and tools/debt_fold.py --check applies the fragment together with every other fragment on main without an error (main itself reports an error for the fragment feat-tools-windows-response-files.md, "no debt file for TechnicalDebt", with or without this branch; with that one fragment left out, the check passes with this fragment). Four pull requests list one of these files, and one of them conflicts with this change: #18918 (test(libSceFiber): audit fiber optional parameter buffer zeroing and invalid pointer rejection, approved) changes the same lines of core/libs/tests/Fiber.cpp. It keeps the check that the block is all zero after sceFiberOptParamInitialize and adds the same check after a second call (opt param cleared, opt param cleared again), which the console measurement here contradicts, because the bytes 0 to 3 are the magic. It merges into main without a conflict and conflicts with this branch in Fiber.cpp. Its checks that the pointers 0x1000 and 0x7FFFFFFFFFFF0000 return SCE_FIBER_ERROR_INVALID and that the sentinels stay intact hold with this change; its two zero checks do not. Whichever of the two lands second has to bring the test of the other along, and in #18918 the zero checks have to become checks of the magic and the zero bytes 4 to 127. #20050 (fix(build): spell the ASan opt-out so Clang honours it) changes Fiber.cpp in lines that do not name sceFiberOptParamInitialize, and #22414 (fix(libSceFiber): reject unreadable fiber objects instead of faulting) changes other functions of Export.cpp; both merge with this branch without a conflict. #7149 (fix(threads): run Windows pthread and small fiber stacks in guest VA range) lists Export.cpp and libSceFiber.md and none of its changed lines names sceFiberOptParamInitialize; it conflicts with main in six other files already. #21056 (test(libSceFiber): audit sceFiberGetInfo parameter validation, structure size, and field bounds) changes only libSceFiber/CMakeLists.txt and a new test file, and conflicts with main in the CMakeLists.txt already. Unpublished local branches that touch these files were compared as well, and none changes these functions. The merged result was not built or run beyond the red and green runs above.

Added after a wider search: four more open pull requests test this call with the same all-zero expectation, each in a new test file of its own, and require that all 0x80 bytes are zero after sceFiberOptParamInitialize: #18568 (test(libSceFiber): audit fiber optional parameter size validation and limits, GuestFiberOptParamSize.cpp, "opt param bytes must be cleared"), #20851 (test(libSceFiber): audit optional parameter zeroing, guard page size validation, and run error bounds, GuestFiberOptParamGuardBounds.cpp), #21031 (test(libSceFiber): audit optional parameter zeroing, stack range bounds, context alignment, and guard page margins, GuestFiberOptStackGuardBounds.cpp) and #21126 (test(libSceFiber): audit internal options, attached stack boundaries, and guard page margins, GuestFiberInternalOptStackBounds.cpp). They register their test files in core/libs/prx/libSceFiber/CMakeLists.txt, which this branch does not change, so there is no textual conflict with it, but with this change their zero checks fail, like the two zero checks of #18918; #20851, #21031 and #21126 already conflict with main. #20947 (test(libSceFiber): audit green threading, context parameters, stack guard pages, and run error bounds) only checks that the call returns 0 on an 8-byte aligned buffer and is not affected. This search read the changed files of all 2005 open pull requests at about 00:27 to 00:29 UTC on 2026-11-19, for paths containing Fiber and for OptParam in their patches. On main the call is made only in core/libs/tests/Fiber.cpp, which this branch updates.

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 (no GPU code)
  • AI-assisted: yes

xXPBSXx and others added 3 commits November 18, 2026 22:02
…nitialize and its alignment error

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…d block as the console does


Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… and alignment

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

xXPBSXx commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Probe sources (built with ps5-payload-sdk v0.45; PROBE_BUILD is the first 12 hex digits of the source's SHA-256: 6791671682e7 for the first probe and b6ab01f816ac for the second) and the complete raw output of both runs on 2026-11-18, at 16:37 UTC and at 20:03 UTC. The utc in each PROBE line is the console's own clock, about 1 h 42 min ahead of real UTC. Each probe loads libSceFiber.sprx with dlopen, takes sceFiberOptParamInitialize with dlsym and calls nothing else of the library; every call is guarded against signals and by a self limit, and nothing is sent. Internal probe numbers in the header comments were removed; the code is unchanged, and each build ID is the hash of the original file.

1st probe (16:37 UTC): probe.c (build 6791671682e7)
/* Probe: sceFiberOptParamInitialize, payload process, firmware 11.60 (debt libSceFiber.md:6: it returns SCE_FIBER_ERROR_INVALID when the destination pointer is unwritable; this is a
   defensive host-protection fallback, whether the console raises a signal or faults on unmapped or read-only memory is not measured). Prototype from the repo: int32_t
   sceFiberOptParamInitialize(SceFiberOptParam* optParam), the structure being 0x80 bytes in the repo (FIBER_OPT_PARAM_SIZE); the repo's error values are SCE_FIBER_ERROR_NULL 0x80590001,
   ALIGNMENT 0x80590002 and INVALID 0x80590004. The module is opened with dlopen("libSceFiber.sprx") and sceFiberOptParamInitialize is looked up with dlsym first (sceFiberInitialize and
   sceFiberGetInfo only as existence flags, they are not called). Cases: a 0xAA-filled 128-byte buffer that is 8-byte aligned, followed by a 64-byte canary, in a static array: the return value, how many
   bytes changed from 0xAA, the offsets of the first and the last changed byte, whether the canary is intact, a second call giving the same result; the same buffer at the offsets +1, +2 and +4 (the
   return value; single calls under a guard); then the pointer cases, last, each one single call and only while the previous one did not fault: NULL, 0x1, 0x1000, a pointer to a read-only page, a pointer
   to a page without access, and a pointer 120 bytes before the end of a writable page that is followed by a page without access (two 16 KiB aligned static arrays, set with mprotect and back at once, no mmap):
   the return value in hex, the fault signal and, for the last case, how many of the first 120 bytes were written. A fault is caught by a guard (one hit per case, more than eight end the probe
   with _exit(125)); a fault is a result. Nothing is written to a file, nothing is sent. */
#include <dlfcn.h>
#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: the runner can only close the connection, it cannot end the process on the console, so the probe ends
   itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
  if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}

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

/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
   thread 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);
  reset_rate();
  _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);
}

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 <setjmp.h>
#include <sys/mman.h>

typedef int (*fn_opt)(void*);
typedef int (*fn_mprot)(void*, size_t, int);

static sigjmp_buf guard_env;
static volatile int guard_sig = 0, guard_active = 0, guard_hits = 0;
static void guard_handler(int s) {
  static const char msg[] = "FAULT" "\t" "guard" "\n";
  guard_sig = s;
  if (!guard_active || ++guard_hits > 8) { (void)!write(1, msg, sizeof msg - 1); _exit(125); }
  guard_active = 0;
  siglongjmp(guard_env, 1);
}

static unsigned char pages[32768] __attribute__((aligned(16384)));
static unsigned char buf[256] __attribute__((aligned(16)));

int main(void) {
  struct sigaction sa;
  memset(&sa, 0, sizeof sa);
  sa.sa_handler = guard_handler;
  sigaction(SIGSEGV, &sa, 0);
  sigaction(SIGBUS, &sa, 0);
  signal(SIGALRM, probe_alarm);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[300], id[16], label[100];

  errno = 0;
  void* fb = dlopen("libSceFiber.sprx", RTLD_LAZY);
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  snprintf(extra, sizeof extra, "libSceFiber=%d libkernel=%d", fb != 0, lk != 0);
  probe_case("0", "load libSceFiber.sprx and libkernel.sprx", fb && lk ? 0 : -1, errno, extra);
  if (!fb) { probe_end(); return 0; }
  fn_opt f_opt = (fn_opt)dlsym(fb, "sceFiberOptParamInitialize");
  void* f_init = dlsym(fb, "sceFiberInitialize");
  void* f_info = dlsym(fb, "sceFiberGetInfo");
  fn_mprot prot = lk ? (fn_mprot)dlsym(lk, "mprotect") : 0;
  snprintf(extra, sizeof extra, "sceFiberOptParamInitialize=%d sceFiberInitialize=%d sceFiberGetInfo=%d mprotect=%d", f_opt != 0, f_init != 0, f_info != 0, prot != 0);
  probe_case("0b", "dlsym of the functions (nothing but the first is called)", f_opt ? 0 : -1, 0, extra);
  if (!f_opt) { probe_end(); return 0; }

  /* b) normal call */
  for (int round = 0; round < 2; ++round) {
    memset(buf, 0xAA, sizeof buf);
    errno = 0;
    int r = f_opt(buf);
    int e = errno;
    int changed = 0, first = -1, last = -1;
    for (int i = 0; i < 128; ++i) if (buf[i] != 0xAA) { changed++; if (first < 0) first = i; last = i; }
    int canary = 1;
    for (int i = 128; i < 192; ++i) if (buf[i] != 0xAA) canary = 0;
    snprintf(id, sizeof id, "1.%d", round + 1);
    snprintf(label, sizeof label, "sceFiberOptParamInitialize(aligned 0xAA buffer), call %d", round + 1);
    snprintf(extra, sizeof extra, "rc=0x%08x errno=%d bytes_changed=%d first_changed=%d last_changed=%d canary_intact=%d first_16_bytes=%02x%02x%02x%02x%02x%02x%02x%02x", (unsigned)r, e, changed, first, last, canary,
             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
    probe_case(id, label, r, e, extra);
  }

  /* c) alignment */
  int offs[3] = {1, 2, 4};
  for (int i = 0; i < 3; ++i) {
    memset(buf, 0xAA, sizeof buf);
    guard_sig = 0;
    int r = -1;
    if (sigsetjmp(guard_env, 1) == 0) {
      guard_active = 1;
      errno = 0;
      r = f_opt(buf + offs[i]);
      guard_active = 0;
    }
    int changed = 0;
    for (int k = 0; k < 160; ++k) if (buf[k] != 0xAA) changed++;
    snprintf(id, sizeof id, "2.%d", i + 1);
    snprintf(label, sizeof label, "sceFiberOptParamInitialize(buffer + %d)", offs[i]);
    snprintf(extra, sizeof extra, "rc=0x%08x fault_signal=%d bytes_changed_in_first_160=%d", (unsigned)r, (int)guard_sig, changed);
    probe_case(id, label, r, 0, extra);
  }

  /* d) pointer cases */
  struct { const char* tag; int kind; } pc[6] = {{"NULL", 0}, {"0x1", 1}, {"0x1000", 2}, {"read-only page", 3}, {"page without access", 4}, {"120 bytes before the end of a writable page", 5}};
  int stopped = 0;
  for (int i = 0; i < 6 && !stopped; ++i) {
    void* target = 0;
    unsigned char* tail = pages + 16384 - 120;
    switch (pc[i].kind) {
      case 0: target = 0; break;
      case 1: target = (void*)0x1; break;
      case 2: target = (void*)0x1000; break;
      case 3:
        if (!prot) continue;
        prot(pages, 16384, PROT_READ);
        target = pages;
        break;
      case 4:
        if (!prot) continue;
        prot(pages, 16384, PROT_NONE);
        target = pages;
        break;
      default:
        if (!prot) continue;
        memset(pages, 0xAA, sizeof pages);
        prot(pages + 16384, 16384, PROT_NONE);
        target = tail;
        break;
    }
    guard_sig = 0;
    int r = -1;
    if (sigsetjmp(guard_env, 1) == 0) {
      guard_active = 1;
      errno = 0;
      r = f_opt(target);
      guard_active = 0;
    } else {
      stopped = 1;
    }
    if (pc[i].kind == 3 || pc[i].kind == 4) prot(pages, 16384, PROT_READ | PROT_WRITE);
    int written = 0;
    if (pc[i].kind == 5) {
      prot(pages + 16384, 16384, PROT_READ | PROT_WRITE);
      for (int k = 0; k < 120; ++k) if (tail[k] != 0xAA) written++;
    }
    snprintf(id, sizeof id, "3.%d", i + 1);
    snprintf(label, sizeof label, "sceFiberOptParamInitialize(%s)", pc[i].tag);
    snprintf(extra, sizeof extra, "rc=0x%08x fault_signal=%d bytes_written_of_the_first_120=%d", (unsigned)r, (int)guard_sig, written);
    probe_case(id, label, r, 0, extra);
  }
  if (stopped) probe_case("4", "pointer cases stopped after the first fault", 0, 0, "remaining pointer cases skipped by design");

  probe_end();
  return 0;
}
1st probe (16:37 UTC): stdout
PROBE	libSceFiber_optparam_init	fw=0x11600005	build=6791671682e7	pid=306	utc=2026-11-18T18:19:13Z
CASE	0	load libSceFiber.sprx and libkernel.sprx	0	2	libSceFiber=1 libkernel=1
CASE	0b	dlsym of the functions (nothing but the first is called)	0	0	sceFiberOptParamInitialize=1 sceFiberInitialize=0 sceFiberGetInfo=1 mprotect=1
CASE	1.1	sceFiberOptParamInitialize(aligned 0xAA buffer), call 1	0	0	rc=0x00000000 errno=0 bytes_changed=128 first_changed=0 last_changed=127 canary_intact=1 first_16_bytes=4de640bb00000000
CASE	1.2	sceFiberOptParamInitialize(aligned 0xAA buffer), call 2	0	0	rc=0x00000000 errno=0 bytes_changed=128 first_changed=0 last_changed=127 canary_intact=1 first_16_bytes=4de640bb00000000
CASE	2.1	sceFiberOptParamInitialize(buffer + 1)	-2141650942	0	rc=0x80590002 fault_signal=0 bytes_changed_in_first_160=0
CASE	2.2	sceFiberOptParamInitialize(buffer + 2)	-2141650942	0	rc=0x80590002 fault_signal=0 bytes_changed_in_first_160=0
CASE	2.3	sceFiberOptParamInitialize(buffer + 4)	-2141650942	0	rc=0x80590002 fault_signal=0 bytes_changed_in_first_160=0
CASE	3.1	sceFiberOptParamInitialize(NULL)	-2141650943	0	rc=0x80590001 fault_signal=0 bytes_written_of_the_first_120=0
CASE	3.2	sceFiberOptParamInitialize(0x1)	-2141650942	0	rc=0x80590002 fault_signal=0 bytes_written_of_the_first_120=0
CASE	3.3	sceFiberOptParamInitialize(0x1000)	-1	0	rc=0xffffffff fault_signal=11 bytes_written_of_the_first_120=0
CASE	4	pointer cases stopped after the first fault	0	0	remaining pointer cases skipped by design
END	11
2nd probe (20:03 UTC): probe.c (build b6ab01f816ac)
/* Probe: sceFiberOptParamInitialize, follow-up to libSceFiber_optparam_init, payload process, firmware 11.60 (debt libSceFiber.md:6: it returns SCE_FIBER_ERROR_INVALID when the destination pointer is
   unwritable, a defensive host-protection fallback, whether the console raises a signal or faults on unmapped or read-only memory is not measured). The first run measured: all 128 bytes written, the first
   8 bytes 4d e6 40 bb 00 00 00 00, the alignment error 0x80590002 for the offsets +1, +2 and +4, NULL 0x80590001, 0x1 0x80590002 and the aligned unmapped address 0x1000 SIGSEGV, which ended the
   pointer cases. This run (dlopen("libSceFiber.sprx"), sceFiberOptParamInitialize looked up with dlsym first and the only function called) prints the 128 bytes written into a 0xAA-filled aligned buffer as eight
   lines of 16 bytes in hex (the content of the structure is a constant of the library, no identifier), twice (are both calls equal), and the buffer at the offsets +8, +16 and +32 (return value, how many bytes changed, where the first one is), and then the pointer cases the first run did not reach, each one single call and only while
   the previous one did not fault: a pointer to a read-only page, a pointer to a page without access, and a pointer 120 bytes before the end of a writable page followed by a page without access (two 16 KiB aligned
   static arrays, mprotect and back at once, no mmap; the return value in hex, the fault signal and how many of the first 120 bytes were written). A fault is caught by a guard (one hit per case, more than eight
   end the probe with _exit(125)); a fault is a result. Nothing is written to a file, nothing is sent. */
#include <dlfcn.h>
#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: the runner can only close the connection, it cannot end the process on the console, so the probe ends
   itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
  if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}

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

/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
   thread 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);
  reset_rate();
  _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);
}

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 <setjmp.h>
#include <sys/mman.h>

typedef int (*fn_opt)(void*);
typedef int (*fn_mprot)(void*, size_t, int);

static sigjmp_buf guard_env;
static volatile int guard_sig = 0, guard_active = 0, guard_hits = 0;
static void guard_handler(int s) {
  static const char msg[] = "FAULT" "\t" "guard" "\n";
  guard_sig = s;
  if (!guard_active || ++guard_hits > 8) { (void)!write(1, msg, sizeof msg - 1); _exit(125); }
  guard_active = 0;
  siglongjmp(guard_env, 1);
}

static unsigned char pages[32768] __attribute__((aligned(16384)));
static unsigned char buf[256] __attribute__((aligned(16)));

int main(void) {
  struct sigaction sa;
  memset(&sa, 0, sizeof sa);
  sa.sa_handler = guard_handler;
  sigaction(SIGSEGV, &sa, 0);
  sigaction(SIGBUS, &sa, 0);
  signal(SIGALRM, probe_alarm);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[300], id[16], label[100];

  errno = 0;
  void* fb = dlopen("libSceFiber.sprx", RTLD_LAZY);
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  snprintf(extra, sizeof extra, "libSceFiber=%d libkernel=%d", fb != 0, lk != 0);
  probe_case("0", "load libSceFiber.sprx and libkernel.sprx", fb && lk ? 0 : -1, errno, extra);
  if (!fb) { probe_end(); return 0; }
  fn_opt f_opt = (fn_opt)dlsym(fb, "sceFiberOptParamInitialize");
  void* f_init = dlsym(fb, "sceFiberInitialize");
  void* f_info = dlsym(fb, "sceFiberGetInfo");
  fn_mprot prot = lk ? (fn_mprot)dlsym(lk, "mprotect") : 0;
  snprintf(extra, sizeof extra, "sceFiberOptParamInitialize=%d sceFiberInitialize=%d sceFiberGetInfo=%d mprotect=%d", f_opt != 0, f_init != 0, f_info != 0, prot != 0);
  probe_case("0b", "dlsym of the functions (nothing but the first is called)", f_opt ? 0 : -1, 0, extra);
  if (!f_opt) { probe_end(); return 0; }

  /* b) normal call */
  for (int round = 0; round < 2; ++round) {
    memset(buf, 0xAA, sizeof buf);
    errno = 0;
    int r = f_opt(buf);
    int e = errno;
    int changed = 0, first = -1, last = -1;
    for (int i = 0; i < 128; ++i) if (buf[i] != 0xAA) { changed++; if (first < 0) first = i; last = i; }
    int canary = 1;
    for (int i = 128; i < 192; ++i) if (buf[i] != 0xAA) canary = 0;
    snprintf(id, sizeof id, "1.%d", round + 1);
    snprintf(label, sizeof label, "sceFiberOptParamInitialize(aligned 0xAA buffer), call %d", round + 1);
    snprintf(extra, sizeof extra, "rc=0x%08x errno=%d bytes_changed=%d first_changed=%d last_changed=%d canary_intact=%d first_16_bytes=%02x%02x%02x%02x%02x%02x%02x%02x", (unsigned)r, e, changed, first, last, canary,
             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
    probe_case(id, label, r, e, extra);
    for (int row = 0; row < 8; ++row) {
      char hx[48] = "";
      for (int k = 0; k < 16; ++k) { char b[4]; snprintf(b, sizeof b, "%02x", buf[row * 16 + k]); strncat(hx, b, sizeof hx - strlen(hx) - 1); }
      snprintf(id, sizeof id, "1.%d.r%d", round + 1, row);
      snprintf(label, sizeof label, "bytes %d..%d of call %d", row * 16, row * 16 + 15, round + 1);
      probe_case(id, label, 0, 0, hx);
    }
  }

  /* c) alignment, offsets 8, 16 and 32 inside the own buffer (the offsets 1, 2 and 4 were measured, ALIGNMENT) */
  int offs[3] = {8, 16, 32};
  for (int i = 0; i < 3; ++i) {
    memset(buf, 0xAA, sizeof buf);
    guard_sig = 0;
    int r = -1;
    if (sigsetjmp(guard_env, 1) == 0) {
      guard_active = 1;
      errno = 0;
      r = f_opt(buf + offs[i]);
      guard_active = 0;
    }
    int changed = 0, first = -1;
    for (int k = 0; k < 256; ++k) if (buf[k] != 0xAA) { changed++; if (first < 0) first = k; }
    snprintf(id, sizeof id, "2.%d", i + 1);
    snprintf(label, sizeof label, "sceFiberOptParamInitialize(buffer + %d)", offs[i]);
    snprintf(extra, sizeof extra, "rc=0x%08x fault_signal=%d bytes_changed_in_256=%d first_changed=%d magic_at_offset=%02x%02x%02x%02x", (unsigned)r, (int)guard_sig, changed, first, buf[offs[i]], buf[offs[i]+1], buf[offs[i]+2], buf[offs[i]+3]);
    probe_case(id, label, r, 0, extra);
  }

  /* d) pointer cases */
  struct { const char* tag; int kind; } pc[3] = {{"read-only page", 3}, {"page without access", 4}, {"120 bytes before the end of a writable page", 5}};
  int stopped = 0;
  for (int i = 0; i < 3 && !stopped; ++i) {
    void* target = 0;
    unsigned char* tail = pages + 16384 - 120;
    switch (pc[i].kind) {
      case 0: target = 0; break;
      case 1: target = (void*)0x1; break;
      case 2: target = (void*)0x1000; break;
      case 3:
        if (!prot) continue;
        prot(pages, 16384, PROT_READ);
        target = pages;
        break;
      case 4:
        if (!prot) continue;
        prot(pages, 16384, PROT_NONE);
        target = pages;
        break;
      default:
        if (!prot) continue;
        memset(pages, 0xAA, sizeof pages);
        prot(pages + 16384, 16384, PROT_NONE);
        target = tail;
        break;
    }
    guard_sig = 0;
    int r = -1;
    if (sigsetjmp(guard_env, 1) == 0) {
      guard_active = 1;
      errno = 0;
      r = f_opt(target);
      guard_active = 0;
    } else {
      stopped = 1;
    }
    if (pc[i].kind == 3 || pc[i].kind == 4) prot(pages, 16384, PROT_READ | PROT_WRITE);
    int written = 0;
    if (pc[i].kind == 5) {
      prot(pages + 16384, 16384, PROT_READ | PROT_WRITE);
      for (int k = 0; k < 120; ++k) if (tail[k] != 0xAA) written++;
    }
    snprintf(id, sizeof id, "3.%d", i + 1);
    snprintf(label, sizeof label, "sceFiberOptParamInitialize(%s)", pc[i].tag);
    snprintf(extra, sizeof extra, "rc=0x%08x fault_signal=%d bytes_written_of_the_first_120=%d", (unsigned)r, (int)guard_sig, written);
    probe_case(id, label, r, 0, extra);
  }
  if (stopped) probe_case("4", "pointer cases stopped after the first fault", 0, 0, "remaining pointer cases skipped by design");

  probe_end();
  return 0;
}
2nd probe (20:03 UTC): stdout
PROBE	libSceFiber_optparam_dump	fw=0x11600005	build=b6ab01f816ac	pid=316	utc=2026-11-18T21:45:53Z
CASE	0	load libSceFiber.sprx and libkernel.sprx	0	2	libSceFiber=1 libkernel=1
CASE	0b	dlsym of the functions (nothing but the first is called)	0	0	sceFiberOptParamInitialize=1 sceFiberInitialize=0 sceFiberGetInfo=1 mprotect=1
CASE	1.1	sceFiberOptParamInitialize(aligned 0xAA buffer), call 1	0	0	rc=0x00000000 errno=0 bytes_changed=128 first_changed=0 last_changed=127 canary_intact=1 first_16_bytes=4de640bb00000000
CASE	1.1.r0	bytes 0..15 of call 1	0	0	4de640bb000000000000000000000000
CASE	1.1.r1	bytes 16..31 of call 1	0	0	00000000000000000000000000000000
CASE	1.1.r2	bytes 32..47 of call 1	0	0	00000000000000000000000000000000
CASE	1.1.r3	bytes 48..63 of call 1	0	0	00000000000000000000000000000000
CASE	1.1.r4	bytes 64..79 of call 1	0	0	00000000000000000000000000000000
CASE	1.1.r5	bytes 80..95 of call 1	0	0	00000000000000000000000000000000
CASE	1.1.r6	bytes 96..111 of call 1	0	0	00000000000000000000000000000000
CASE	1.1.r7	bytes 112..127 of call 1	0	0	00000000000000000000000000000000
CASE	1.2	sceFiberOptParamInitialize(aligned 0xAA buffer), call 2	0	0	rc=0x00000000 errno=0 bytes_changed=128 first_changed=0 last_changed=127 canary_intact=1 first_16_bytes=4de640bb00000000
CASE	1.2.r0	bytes 0..15 of call 2	0	0	4de640bb000000000000000000000000
CASE	1.2.r1	bytes 16..31 of call 2	0	0	00000000000000000000000000000000
CASE	1.2.r2	bytes 32..47 of call 2	0	0	00000000000000000000000000000000
CASE	1.2.r3	bytes 48..63 of call 2	0	0	00000000000000000000000000000000
CASE	1.2.r4	bytes 64..79 of call 2	0	0	00000000000000000000000000000000
CASE	1.2.r5	bytes 80..95 of call 2	0	0	00000000000000000000000000000000
CASE	1.2.r6	bytes 96..111 of call 2	0	0	00000000000000000000000000000000
CASE	1.2.r7	bytes 112..127 of call 2	0	0	00000000000000000000000000000000
CASE	2.1	sceFiberOptParamInitialize(buffer + 8)	0	0	rc=0x00000000 fault_signal=0 bytes_changed_in_256=128 first_changed=8 magic_at_offset=4de640bb
CASE	2.2	sceFiberOptParamInitialize(buffer + 16)	0	0	rc=0x00000000 fault_signal=0 bytes_changed_in_256=128 first_changed=16 magic_at_offset=4de640bb
CASE	2.3	sceFiberOptParamInitialize(buffer + 32)	0	0	rc=0x00000000 fault_signal=0 bytes_changed_in_256=128 first_changed=32 magic_at_offset=4de640bb
CASE	3.1	sceFiberOptParamInitialize(read-only page)	-1	0	rc=0xffffffff fault_signal=11 bytes_written_of_the_first_120=0
CASE	4	pointer cases stopped after the first fault	0	0	remaining pointer cases skipped by design
END	25

AI-assisted (Claude Code).

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against b1637fec88:

  • Conventions: tools/check_conventions.py passed.
  • Build: fiber_tests built cleanly.
  • CTest (fiber, fiber_contextless):
    1/2 Test #3634: fiber ............................   Passed    0.02 sec
    2/2 Test #3635: fiber_contextless ................   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceFiber/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #3635: fiber_contextless ................   Passed    0.03 sec
    2/2 Test #3634: fiber ............................Subprocess aborted***Exception:   0.36 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libSceFiber/Export.cpp: sceFiberOptParamInitialize performs null pointer validation returning SCE_FIBER_ERROR_NULL (0x80590001) before alignment check.
    • Verified 8-byte alignment check ((reinterpret_caststd::uintptr_t(opt_param) & 7) != 0) returns SCE_FIBER_ERROR_ALIGNMENT (0x80590002) matching probe measurements on FW 11.60.
    • Verified buffer zeroing and little-endian 32-bit magic 0xBB40E64D placement via GuestCopyOut_nid_no_patch to prevent host faults and retain preserved errno on failure returning SCE_FIBER_ERROR_INVALID.
    • Verified technical debt fragment in docs/dev/debt/changes/fix-fiber-optparam-initialize-magic.md correctly documents defensive unwritable host handling and alignment behavior according to AGENTS.md.
    • Verified unit test core/libs/tests/Fiber.cpp rigorously covers alignment offsets (0, 8, 16, 32 vs 1, 2, 4), sentinel integrity, magic match, and zeroes in trailing bytes.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit d27be81 into boykopovar:main Nov 19, 2026
15 of 16 checks passed
@github-actions

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 18918 core/libs/tests/Fiber.cpp L628, L630, L642-644

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 7149 core/libs/prx/libSceFiber/Export.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants