Skip to content

docs(debt): record the fstatat results on firmware 11.60 - #22523

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libkernel-sys-fstatat-console-1160
Nov 17, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
xXPBSXx:docs/libkernel-sys-fstatat-console-1160

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

The fstatat entry in docs/dev/debt/libkernel_sys.md describes how AnyPS5 passes AT_FDCWD and absolute paths to stat or lstat, joins any other relative path to the directory descriptor's host directory and returns EBADF and ENOTDIR for descriptors that are not open or not a directory, and ends with "Console behaviour is unverified". A probe has now called fstatat in libkernel.sprx from a payload process on firmware 11.60. The pull request replaces that last sentence through a debt fragment, as the contribution rules ask; no code changes.

On firmware 11.60 fstatat(AT_FDCWD, "/dev/null", ...) returned 0 with the same mode and inode as stat, with flags 0 and with AT_SYMLINK_NOFOLLOW (cases 2.1 and 2.2, lines 6 and 7), and so did an absolute path with descriptor -1, which is not open (2.3, line 8). Through a descriptor of the probe directory (opened read-only with O_DIRECTORY), . returned 0 with the mode and inode of that directory as stat gives them (3.1, line 10) and .. returned 0 with the mode and inode of its parent as fstatat gave them for an absolute path (3.2, line 11); a name that does not exist returned -1 with errno 2 (ENOENT, 3.3, line 12). A relative path with descriptor -1 or 999 returned -1 with errno 9 (EBADF, 4.1 and 4.2, lines 13 and 14), and a relative path with the descriptor of /dev/null returned -1 with errno 20 (ENOTDIR, 5.1, line 15).

When the code is read, AnyPS5 answers the same way: fstatat_nid_postfix (libkernel_sys/Export.cpp, lines 1135 to 1143) sends AT_FDCWD and absolute paths to stat or lstat (line 1139), also with descriptor -1, and other relative paths to StatInDirectory_nid_no_patch (line 1140), where GetDirectoryDescriptorPath_nid_no_patch returns EBADF for a descriptor that is not open and ENOTDIR for an open one without a directory path (libkernel/File/src/DirectoryDescriptor.cpp, lines 489 to 500). The test GuestKernelSys pins the successes with a directory descriptor and with AT_FDCWD, ENOENT, the absolute path with descriptor -1 and EBADF for -1 (lines 2242 to 2258). The emulator itself was not run for this pull request; descriptor 999 and a /dev/null descriptor take the same code paths but were not run, and .. is compared only in that both answer with the parent directory, not for the console's directory tree.

Not measured: flags other than AT_SYMLINK_NOFOLLOW (the entry says EINVAL), symbolic links, .. above the guest root, a relative path with AT_FDCWD, guest path aliases and case-insensitive name matching, 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 libkernel.sprx and takes fstatat and stat with dlsym, on 2026-11-17 at 16:04 UTC. A title process may differ. The cases are 0 to 5.1, lines 2 to 15 of the raw output in the first comment.

Call PS5 payload process, firmware 11.60 AnyPS5 main (code read, not run) in the entry
fstatat(AT_FDCWD, "/dev/null", 0), with AT_SYMLINK_NOFOLLOW, and with descriptor -1 0, same mode and inode as stat (lines 6 to 8) stat or lstat now stated
fstatat(dirfd, "."), fstatat(dirfd, "..") 0, directory; mode and inode of that directory (stat) and of its parent (fstatat, absolute path) (lines 10, 11) joined to the directory path; not compared for the console's tree now stated
fstatat(dirfd, "zzz_not_here") -1, ENOENT (line 12) ENOENT now stated
relative path with descriptor -1, 999 -1, EBADF (lines 13, 14) EBADF now stated
relative path with the descriptor of /dev/null -1, ENOTDIR (line 15) ENOTDIR for an open descriptor without a directory path now stated

tools/check_conventions.py --base upstream/main passes with no error and no warning (the entry is 998 characters, up from 632). 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-libkernel-sys-fstatat-console-1160.md, a new file), core/libs/prx/libkernel_sys/Export.cpp or core/libs/prx/libkernel/File/src/DirectoryDescriptor.cpp, which the entry links, or the debt entry it changes, found by reading the changed files of all 1751 open pull requests at about 17:38 UTC, by reading what they change in docs/dev/debt/libkernel_sys.md and in the fragments under docs/dev/debt/changes/ for fstatat, and by searching their titles and branch names for the same name (no hit), were test-merged on 2026-11-17 at about 18:35 UTC against main 89b0eef7ab4 together with this head; the branch is based on 4b83e9d2234, an ancestor of it, merges into main without a conflict, and tools/debt_fold.py --check applies the fragment to the libkernel_sys debt file of 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 the change names occurs in exactly one entry, and no other fragment changes that entry. Ten pull requests list Export.cpp of libkernel_sys (#22219, #21984, #18190, #17908, #17897, #17885, #15037, #12315, #9389 and #6918) and none lists DirectoryDescriptor.cpp; as this branch adds only a fragment, none of them conflicts only with this change, and none changes a line that names fstatat or the debt entry of this branch. 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, 03d32efa9fef) and its raw output from the run on 2026-11-17 at 16:04 UTC. The clock in the PROBE line is the console's own clock. The probe loads libkernel.sprx, takes fstatat and stat with dlsym and reads only the metadata of existing paths: /dev/null through AT_FDCWD and through descriptor -1, . and .. through a descriptor of the probe directory opened read-only, a name that does not exist, a relative path with the descriptors -1 and 999, and a relative path with the descriptor of /dev/null; it prints the return value, errno and whether mode and inode equal those of stat. Every call is guarded against signals and by a self limit. An internal probe number in the header comment was removed; the code is unchanged, and the build ID is the hash of the original file.

probe.c
/* fstatat, payload process, firmware 11.60 (debt libkernel_sys.md fstatat entry: AnyPS5 passes AT_FDCWD (-100) and
   absolute paths to stat, or to lstat with AT_SYMLINK_NOFOLLOW (0x200); any other relative path is joined to the descriptor's
   host directory, found the same way as for fchdir, so guest path aliases and case-insensitive name matching do not apply to it,
   and a ".." component is not kept inside the guest root; it returns EBADF for a descriptor that is not open [...]; the
   console's answers are not measured). The probe looks fstatat and stat up with dlsym in libkernel.sprx and reads only metadata
   of existing paths: fstatat(AT_FDCWD, "/dev/null", ...) against stat("/dev/null") (mode and inode equal), with and without
   AT_SYMLINK_NOFOLLOW; a descriptor of the probe directory opened read-only with O_DIRECTORY (no listing): fstatat(dirfd, "."),
   fstatat(dirfd, "..") against the same path reached through an absolute path (inode equal), a name that does not exist, an absolute path with a descriptor that is not open (-1: does it succeed); an invalid descriptor (-1 and 999) with a
   relative path; a descriptor of /dev/null (not a directory) with a relative path. Only the return code, errno and the
   equality of mode and inode are printed. No file is created, nothing is listed, no descriptor of the payload is closed.
   Nothing is written, 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 <fcntl.h>
#include <sys/stat.h>
typedef int (*fn_fstatat)(int, const char*, struct stat*, int);

static void fault(int sig) {
  static const char msg[] = "FAULT\tsignal in the call\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(125);
}

static fn_fstatat g_fs;

static void fs_case(const char* id, const char* label, int fd, const char* path, int flag, const struct stat* ref) {
  struct stat st;
  memset(&st, 0xaa, sizeof st);
  char extra[140];
  errno = 0;
  int r = g_fs(fd, path, &st, flag);
  int e = errno;
  if (r == 0 && ref)
    snprintf(extra, sizeof extra, "rc=0 is_dir=%d same_mode_as_ref=%d same_ino_as_ref=%d", S_ISDIR(st.st_mode) != 0,
             st.st_mode == ref->st_mode, st.st_ino == ref->st_ino);
  else if (r == 0)
    snprintf(extra, sizeof extra, "rc=0 is_dir=%d", S_ISDIR(st.st_mode) != 0);
  else
    snprintf(extra, sizeof extra, "rc=%d errno=%d", r, e);
  probe_case(id, label, r, e, extra);
}

int main(void) {
  signal(SIGALRM, probe_alarm);
  signal(SIGSEGV, fault);
  signal(SIGBUS, fault);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[160];

  errno = 0;
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
  if (!lk) { probe_end(); return 0; }
  g_fs = (fn_fstatat)dlsym(lk, "fstatat");
  snprintf(extra, sizeof extra, "fstatat=%d", g_fs != 0);
  probe_case("0b", "dlsym of fstatat", g_fs ? 0 : -1, 0, extra);
  if (!g_fs) { probe_end(); return 0; }

  struct stat sn, sd, sr;
  int a = stat("/dev/null", &sn), b = stat("/data/anyps5_probe", &sd);
  snprintf(extra, sizeof extra, "stat(/dev/null)=%d stat(probe dir)=%d", a, b);
  probe_case("1", "reference stats", 0, 0, extra);
  if (a != 0 || b != 0) { probe_end(); return 0; }
  memset(&sr, 0, sizeof sr);
  int pr = g_fs(-100, "/data/anyps5_probe/..", &sr, 0);
  snprintf(extra, sizeof extra, "fstatat(AT_FDCWD, probe dir/..)=%d", pr);
  probe_case("1b", "reference stat of the parent through an absolute path", pr, errno, extra);

  fs_case("2.1", "fstatat(AT_FDCWD, \"/dev/null\", 0)", -100, "/dev/null", 0, &sn);
  fs_case("2.2", "fstatat(AT_FDCWD, \"/dev/null\", AT_SYMLINK_NOFOLLOW)", -100, "/dev/null", 0x200, &sn);
  fs_case("2.3", "fstatat(-1, \"/dev/null\", 0) absolute path with no descriptor", -1, "/dev/null", 0, &sn);

  int dirfd = open("/data/anyps5_probe", O_RDONLY | O_DIRECTORY);
  snprintf(extra, sizeof extra, "opened=%d", dirfd >= 0);
  probe_case("3a", "open the probe directory read-only with O_DIRECTORY", dirfd, errno, extra);
  if (dirfd >= 0) {
    fs_case("3.1", "fstatat(dirfd, \".\", 0)", dirfd, ".", 0, &sd);
    fs_case("3.2", "fstatat(dirfd, \"..\", 0)", dirfd, "..", 0, pr == 0 ? &sr : 0);
    fs_case("3.3", "fstatat(dirfd, \"zzz_not_here\", 0)", dirfd, "zzz_not_here", 0, 0);
    close(dirfd);
  }
  fs_case("4.1", "fstatat(-1, \"null\", 0) relative path", -1, "null", 0, 0);
  fs_case("4.2", "fstatat(999, \"null\", 0) relative path", 999, "null", 0, 0);
  int fileFd = open("/dev/null", O_RDONLY);
  if (fileFd >= 0) {
    fs_case("5.1", "fstatat(fd of /dev/null, \"x\", 0) not a directory", fileFd, "x", 0, 0);
    close(fileFd);
  }

  probe_end();
  return 0;
}
stdout
PROBE	libkernel_sys_fstatat	fw=0x11600005	build=03d32efa9fef	pid=224	utc=2026-11-17T17:46:51Z
CASE	0	load libkernel.sprx	0	2	loaded
CASE	0b	dlsym of fstatat	0	0	fstatat=1
CASE	1	reference stats	0	0	stat(/dev/null)=0 stat(probe dir)=0
CASE	1b	reference stat of the parent through an absolute path	0	2	fstatat(AT_FDCWD, probe dir/..)=0
CASE	2.1	fstatat(AT_FDCWD, "/dev/null", 0)	0	0	rc=0 is_dir=0 same_mode_as_ref=1 same_ino_as_ref=1
CASE	2.2	fstatat(AT_FDCWD, "/dev/null", AT_SYMLINK_NOFOLLOW)	0	0	rc=0 is_dir=0 same_mode_as_ref=1 same_ino_as_ref=1
CASE	2.3	fstatat(-1, "/dev/null", 0) absolute path with no descriptor	0	0	rc=0 is_dir=0 same_mode_as_ref=1 same_ino_as_ref=1
CASE	3a	open the probe directory read-only with O_DIRECTORY	7	0	opened=1
CASE	3.1	fstatat(dirfd, ".", 0)	0	0	rc=0 is_dir=1 same_mode_as_ref=1 same_ino_as_ref=1
CASE	3.2	fstatat(dirfd, "..", 0)	0	0	rc=0 is_dir=1 same_mode_as_ref=1 same_ino_as_ref=1
CASE	3.3	fstatat(dirfd, "zzz_not_here", 0)	-1	2	rc=-1 errno=2
CASE	4.1	fstatat(-1, "null", 0) relative path	-1	9	rc=-1 errno=9
CASE	4.2	fstatat(999, "null", 0) relative path	-1	9	rc=-1 errno=9
CASE	5.1	fstatat(fd of /dev/null, "x", 0) not a directory	-1	20	rc=-1 errno=20
END	14

AI-assisted (Claude Code).

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