Repository navigation
docs(debt): record the dup, dup2 and _dup2 results on firmware 11.60 - #22734
oneandonlydean merged 1 commit into
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe source (built with ps5-payload-sdk v0.45; probe.c/* dup, dup2 and _dup2 edge cases and descriptor exhaustion with plain files, payload process, firmware 11.60 (debt libkernel.md
entry: dup and dup2 throw for the combinations they do not model, dup2 of a guest socket onto descriptors 0 to 2; the numbers come from
the host descriptor table like those of files; the errno values for descriptor exhaustion are not verified against the console; debt
libkernel_sys.md _dup2 entry: follows FreeBSD 11.4's _dup2 and POSIX.1-2008 dup2, forwards to libkernel's dup2, returning the target
descriptor or setting errno and returning -1; console behaviour unverified). Prototypes: int dup(int), int dup2(int, int), _dup2 like
dup2. The three are looked up with dlsym (libkernel.sprx first, then the global scope); a missing export is the result for its cases.
Only descriptors of the regular file f1 in the probe's own subdirectory /data/anyps5_probe/n2_14 are used: no socket, no kqueue, no
semaphore descriptor and never a dup2 onto descriptor 0, 1 or 2. Cases: dup of the file descriptor (the number as small or not), dup2
to 50, dup2 onto itself, dup2 to 50 again, dup2 with the source -1 and 999 and the target -1, 1000000 and 0x7fffffff, dup(-1), the
values of getdtablesize() and sysconf(_SC_OPEN_MAX); the same cases through _dup2; exhaustion: open f1 in a loop (at most 4096 descriptors,
at most 5 seconds, stopping at the first failure): the number opened and errno at the stop, the number against getdtablesize(), then a dup
at the full limit (errno), then every descriptor opened is closed; shared state: after a write on f, dup g shares the file offset, close
f then a write through g works, the close-on-exec flag of the duplicate of a descriptor that has it set (dup, dup2 and fcntl
F_DUPFD_CLOEXEC). Cleanup is mandatory: every descriptor opened is closed and f1 and the directory are removed. 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 <limits.h>
#include <sys/stat.h>
#include <time.h>
typedef int (*fn_dup)(int);
typedef int (*fn_dup2)(int, int);
static int fds[4200];
static fn_dup f_dup;
static fn_dup2 f_dup2, f_dup2u;
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 void res(const char* id, const char* label, int r, int e) {
char extra[100];
snprintf(extra, sizeof extra, "rc=%d errno=%d", r, e);
probe_case(id, label, r, e, extra);
}
static void dup2_cases(const char* tag, fn_dup2 d2, int f, int base) {
char id[16], label[100];
int r;
struct { int from, to; const char* what; } c[] = {
{0, 50, "dup2(f, 50)"}, {0, 0, "dup2(f, f)"}, {0, 50, "dup2(f, 50) again"}, {-1, 5, "dup2(-1, 5)"}, {999, 5, "dup2(999, 5)"},
{0, -1, "dup2(f, -1)"}, {0, 1000000, "dup2(f, 1000000)"}, {0, 0x7fffffff, "dup2(f, 0x7fffffff)"}};
for (int i = 0; i < 8; ++i) {
int from = c[i].from == 0 && i != 1 ? f : (i == 1 ? f : c[i].from);
int to = i == 1 ? f : c[i].to;
errno = 0;
r = d2(from, to);
int e = errno;
snprintf(id, sizeof id, "%s.%d", tag, i + 1);
snprintf(label, sizeof label, "%s %s", base ? "_dup2" : "dup2", c[i].what);
char extra[120];
snprintf(extra, sizeof extra, "rc=%d errno=%d equals_target=%d", r, e, r == to);
probe_case(id, label, r, e, extra);
if (r >= 0 && r != f && r > 2) close(r);
}
}
int main(void) {
signal(SIGALRM, probe_alarm);
signal(SIGSEGV, fault);
signal(SIGBUS, fault);
alarm(PROBE_MAX_S);
probe_header();
char extra[220], id[16], label[100];
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; }
f_dup = (fn_dup)dlsym(lk, "dup");
f_dup2 = (fn_dup2)dlsym(lk, "dup2");
f_dup2u = (fn_dup2)dlsym(lk, "_dup2");
const char* where_u = "libkernel";
if (!f_dup2u) { f_dup2u = (fn_dup2)dlsym(RTLD_DEFAULT, "_dup2"); where_u = "global"; }
snprintf(extra, sizeof extra, "dup=%d dup2=%d _dup2=%d (_dup2 found in %s)", f_dup != 0, f_dup2 != 0, f_dup2u != 0, f_dup2u ? where_u : "none");
probe_case("0b", "dlsym of the functions", 0, 0, extra);
if (!f_dup && !f_dup2 && !f_dup2u) { probe_end(); return 0; }
errno = 0;
int mk = mkdir("/data/anyps5_probe/n2_14", 0700);
int mke = errno;
int f = open("/data/anyps5_probe/n2_14/f1", O_CREAT | O_RDWR | O_EXCL, 0600);
int fe = errno;
if (f < 0) {
snprintf(extra, sizeof extra, "mkdir_rc=%d (errno %d) open_f1 errno %d", mk, mke, fe);
probe_case("1", "set up f1", -1, 0, extra);
goto cleanup;
}
snprintf(extra, sizeof extra, "f1_fd_ge_3=%d getdtablesize=%d sysconf_open_max=%ld", f > 2, getdtablesize(), (long)sysconf(_SC_OPEN_MAX));
probe_case("1", "set up f1; descriptor table limits", 0, 0, extra);
/* b) dup, dup2 */
if (f_dup) {
errno = 0;
int g = f_dup(f);
int e = errno;
snprintf(extra, sizeof extra, "rc_ge_0=%d is_f_plus_1=%d errno=%d", g >= 0, g == f + 1, e);
probe_case("2.1", "dup(f)", g, e, extra);
if (g >= 0) close(g);
errno = 0;
g = f_dup(-1);
res("2.2", "dup(-1)", g, errno);
errno = 0;
g = f_dup(999);
res("2.3", "dup(999)", g, errno);
}
if (f_dup2) dup2_cases("3", f_dup2, f, 0);
if (f_dup2u) dup2_cases("4", f_dup2u, f, 1);
/* d) shared state */
if (f_dup) {
int g = -1;
(void)!write(f, "a", 1);
g = f_dup(f);
if (g >= 0) {
off_t of = lseek(f, 0, SEEK_CUR), og = lseek(g, 0, SEEK_CUR);
snprintf(extra, sizeof extra, "offset_f_equals_g=%d offset_is_1=%d", of == og, of == 1);
probe_case("5.1", "dup(f) shares the file offset", 0, 0, extra);
fcntl(f, F_SETFD, FD_CLOEXEC);
int h = f_dup(f);
int hf = h >= 0 ? fcntl(h, F_GETFD) : -1;
snprintf(extra, sizeof extra, "f_flag=%d dup_flag=%d", fcntl(f, F_GETFD), hf);
probe_case("5.2", "close-on-exec flag of f and of dup(f)", 0, 0, extra);
if (h >= 0) close(h);
if (f_dup2) {
int k = f_dup2(f, 51);
snprintf(extra, sizeof extra, "rc=%d flag_of_target=%d", k, k >= 0 ? fcntl(k, F_GETFD) : -1);
probe_case("5.3", "dup2(f, 51) with the flag set on f", k, 0, extra);
if (k >= 0 && k != f) close(k);
}
int m = fcntl(f, F_DUPFD_CLOEXEC, 52);
snprintf(extra, sizeof extra, "rc_ge_52=%d flag_of_result=%d errno=%d", m >= 52, m >= 0 ? fcntl(m, F_GETFD) : -1, m < 0 ? errno : 0);
probe_case("5.4", "fcntl(f, F_DUPFD_CLOEXEC, 52)", m, 0, extra);
if (m >= 0) close(m);
fcntl(f, F_SETFD, 0);
/* close f's twin: write through g after closing the original f is checked on a fresh pair */
int f2 = f_dup(f);
if (f2 >= 0) {
close(f);
f = -1;
ssize_t w = write(f2, "b", 1);
snprintf(extra, sizeof extra, "write_rc=%zd", w);
probe_case("5.5", "write through the duplicate after the original is closed", (int)w, 0, extra);
f = f2;
}
close(g);
}
}
/* c) exhaustion, last */
if (f >= 0) {
int n = 0, e = 0;
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
int timed_out = 0;
for (; n < 4096; ++n) {
errno = 0;
int x = open("/data/anyps5_probe/n2_14/f1", O_RDONLY);
if (x < 0) { e = errno; break; }
fds[n] = x;
clock_gettime(CLOCK_MONOTONIC, &t1);
if ((t1.tv_sec - t0.tv_sec) >= 5) { timed_out = 1; n++; break; }
}
snprintf(extra, sizeof extra, "opened=%d errno_at_stop=%d stopped_by_failure=%d stopped_by_limit_4096=%d stopped_by_time=%d getdtablesize=%d", n, e,
n < 4096 && !timed_out, n >= 4096, timed_out, getdtablesize());
probe_case("6.1", "open(f1) in a loop up to 4096 descriptors or 5 seconds", n, e, extra);
if (f_dup && n < 4096 && !timed_out) {
errno = 0;
int g = f_dup(f);
int ge = errno;
snprintf(extra, sizeof extra, "rc=%d errno=%d", g, ge);
probe_case("6.2", "dup(f) at the full limit", g, ge, extra);
if (g >= 0) close(g);
}
int closed = 0;
for (int i = 0; i < n; ++i) if (close(fds[i]) == 0) closed++;
snprintf(extra, sizeof extra, "closed=%d of %d", closed, n);
probe_case("6.3", "close every descriptor opened by the loop", closed == n ? 0 : -1, 0, extra);
}
cleanup:
{
if (f >= 0) close(f);
int c1 = unlink("/data/anyps5_probe/n2_14/f1");
int c2 = rmdir("/data/anyps5_probe/n2_14");
snprintf(extra, sizeof extra, "unlink_f1=%d rmdir_dir=%d", c1, c2);
probe_case("9", "cleanup: close and remove f1 and the directory", c2, 0, extra);
}
probe_end();
return 0;
}stdoutAI-assisted (Claude Code). |
What
The
dup and dup2entry indocs/dev/debt/libkernel.mdsays that the descriptor numbers come from the host table and that "the errno values for descriptor exhaustion are not verified against the console"; the_dup2entry indocs/dev/debt/libkernel_sys.mdends with "console behaviour is unverified". A probe has now calleddup,dup2and_dup2inlibkernel.sprxfrom a payload process on firmware 11.60, on a plain file it created. The pull request replaces those sentences through one debt fragment with a section for each file, as the contribution rules ask; no code changes.dupof the file descriptor returned the next number (case 2.1, line 5), anddup(-1)anddup(999)returned -1 witherrno9 (EBADF, 2.2 and 2.3, lines 6 and 7).dup2returned its target for 50, for the descriptor itself and again for 50 (3.1 to 3.3, lines 8 to 10), andEBADFfor the sources -1 and 999 and for the targets -1, 1000000 and 0x7fffffff (3.4 to 3.8, lines 11 to 15)._dup2gave the same eight results (4.1 to 4.8, lines 16 to 23). A duplicate shared the file offset with the original (5.1, line 24), did not inherit the close-on-exec flag that the original had (5.2, line 25), and neither did the target ofdup2(5.3, line 26);fcntlwithF_DUPFD_CLOEXECreturned -1 witherrno45 (EOPNOTSUPP, 5.4, line 27), as theF_DUPFD_CLOEXECentry already says for files, and a write through the duplicate worked after the original was closed (5.5, line 28). Opening the file in a loop failed witherrno24 (EMFILE) after 632 descriptors, whilegetdtablesize()returned 12555 (6.1, line 29); adupat that point still succeeded and returned 640 (6.2, line 30), and all 632 descriptors were closed again (6.3, line 31).When the code is read, AnyPS5 answers the first cases the same way:
dup_nid_postfix(libkernel/File/src/Stdio.cpp, lines 1021 to 1038) returnsEBADFfor a negative descriptor (line 1023) and otherwise asks the host, anddup2_nid_postfix(lines 1040 and following) returnsEBADFfor a negative source or target (line 1041);_dup2_nid_postfix(libkernel_sys/AdditionalExports.cpp, lines 224 to 226) callsGuestDup2_nid_no_patch(lines 220 to 222), which callsdup2_nid_postfix. The other cases (999, the large targets, the offset, the close-on-exec flag) depend on the host calls and were not compared, and the emulator was not run for this pull request. The limit differs: AnyPS5 fails withEMFILEat the host's table (thegetrlimitentry describes it), not after 632 descriptors, so a program that opens plain files untilEMFILEreaches a different count here. This pull request records that and changes no behaviour, because the console's limit was measured only for plain files in one payload process.Not measured: descriptors of sockets, pipes, kqueues and shared memory,
dup2onto descriptors 0 to 2 (the probe never does it), the limit of other descriptor types, and a title process.Tested
Measured on a PS5 with firmware 11.60 (
kernel_get_fw_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loadslibkernel.sprxand takesdup,dup2and_dup2withdlsym(line 3 shows all three were found), on 2026-11-18 at 02:36 UTC. A title process may differ. The cases are 0 to 9, lines 2 to 32 of the raw output in the first comment.main(code read, not run)dup(-1),dup(999)EBADF(lines 6, 7)EBADFfor -1 (line 1023); 999 by the host calldup2source -1 or 999, target -1, 1000000, 0x7fffffffEBADF(lines 11 to 15)EBADFfor a negative one (line 1041); the rest by the host call_dup2, same eight casesdup2(lines 16 to 23)dup2throughGuestDup2_nid_no_patchopenof a plain file until it failsEMFILEafter 632;getdtablesize12555 (line 29)EMFILEat the host tabletools/check_conventions.py --base upstream/mainpasses with no error and no warning (the two entries are now 952 and 644 characters, up from 646 and 497). 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-dup-family-console-1160.md, a new file),core/libs/prx/libkernel/File/src/Stdio.cpp,core/libs/prx/libkernel_sys/AdditionalExports.cpporcore/libs/tests/GuestDup.cpp, which the entries and the text name, or the two debt entries it changes, found by reading the changed files of all 1775 open pull requests at about 05:02 to 05:16 UTC, by reading what they change indocs/dev/debt/libkernel.md,docs/dev/debt/libkernel_sys.mdand the fragments underdocs/dev/debt/changes/fordup,dup2and_dup2, and by searching their titles and branch names for the same names (see below), were test-merged on 2026-11-18 at about 05:19 UTC againstmaina940e04517ftogether with this head; the branch is based on092a1df132b, an ancestor of it, merges intomainwithout a conflict, andtools/debt_fold.py --checkapplies the fragment to thelibkernelandlibkernel_sysdebt files ofmainwithout an error (checked without the fragmentdocs-nkwebkit-drop-readded-count-lines.md, which already fails onmainagainst thelibSceNKWebKitdebt file and is not part of this change), each of the two texts the change names occurs in exactly one entry, and no other fragment changes those entries. Eighty-two pull requests list one of the code files or a debt file that names these functions, most of themdocs/dev/debt/libkernel.mdfor other entries; as this branch adds only a fragment, none of them conflicts only with this change. Three touch the functions in code: #22191 (share a ksem semaphore across dup, dup2 and ...) changesdupinStdio.cppand carries its own fragment for theksementry, not for thedup and dup2entry, and may change what the entry describes when it lands; #18190 changes the call inGuestDup2_nid_no_patch, which_dup2uses, fromdup2_nid_postfixtodup2_nid_no_patch, which does not change the answers recorded here; and #7121 carries thedup2code lines as unchanged context. The other title hits (#19044, #19024 and #17621 forEC_POINT_dupandRSAPublicKey_dup, #18740 for a test of the web library) are about other functions. None of them changes a line of the two entries of this branch. The merged result was not built or run; this is a documentation change.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)