Repository navigation
fix(libc): store the x87 control word at 64 and MXCSR at 68 in the setjmp buffer as the console does - #21935
Conversation
…jmp buffer measured on the console Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…tjmp buffer as the console does Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…p buffer and what it left open Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Probe source (built with ps5-payload-sdk v0.45; 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)
/* setjmp, longjmp, sigsetjmp and siglongjmp of libSceLibcInternal.sprx (dlsym, function pointers) on firmware 11.60 ('save the signal mask at 72 in the jump buffer and restore it ... mark the buffer at 88 as sigsetjmp does ... unverified on a console'). The
functions are called through pointers from one helper frame that stays alive for the whole case: the call returns once normally, and
longjmp returns into the same call site while that frame is still live. State modified between the two returns is volatile. The signal mask is only
queried and set with sigprocmask (no signal is sent); the original mask is restored at the end. The jump buffer is a local 512 byte block. Cases:
b mask {SIGUSR1}, mask changed, longjmp(env, 1): bytes 64..96 of env, mask after; c no blocked signal, longjmp(env, 0); d mask changed to add SIGUSR2
between: restored?; e sigsetjmp(env, 1) and sigsetjmp(env, 0) layout bytes and masks. 9 cases, each guarded. */
typedef int (*sj_fn)(void*);
typedef int (*ssj_fn)(void*, int);
typedef void (*lj_fn)(void*, int);
static sj_fn p_setjmp;
static ssj_fn p_sigsetjmp;
static lj_fn p_longjmp, p_siglongjmp;
static unsigned char env[512] __attribute__((aligned(16)));
static uint32_t cur_mask(void) {
sigset_t s; sigemptyset(&s);
sigprocmask(SIG_SETMASK, 0, &s);
uint32_t m = 0;
for(int sg = 1; sg <= 31; sg++) if(sigismember(&s, sg) == 1) m |= 1u << (sg - 1);
return m;
}
static void set_mask(int a, int b) {
sigset_t s; sigemptyset(&s);
if(a) sigaddset(&s, a);
if(b) sigaddset(&s, b);
sigprocmask(SIG_SETMASK, &s, 0);
}
static void hex(char* out, const unsigned char* p, int n) { for(int i = 0; i < n; i++) snprintf(out + i * 2, 3, "%02x", p[i]); }
/* kind: 0 setjmp/longjmp, 1 sigsetjmp(,1)/siglongjmp, 2 sigsetjmp(,0)/siglongjmp. mask_at_save: first signal (0 none); extra: second signal set before the jump. */
static __attribute__((noinline)) void run(const char* id, const char* what, int kind, int save_sig, int extra_sig, int val) {
volatile int stage = 0;
volatile int ret = -99;
volatile uint32_t m_save = 0, m_after = 0;
char saved_hex[72], ex[240];
set_mask(save_sig, 0);
m_save = cur_mask();
int r;
if(kind == 0) r = p_setjmp(env);
else r = p_sigsetjmp(env, kind == 1 ? 1 : 0);
if(stage == 0) {
stage = 1;
set_mask(extra_sig ? extra_sig : SIGUSR2, 0);
if(kind == 0) p_longjmp(env, val); else p_siglongjmp(env, val);
}
ret = r;
m_after = cur_mask();
hex(saved_hex, env + 64, 32);
snprintf(ex, sizeof ex, "ret=%d mask_at_save=0x%x mask_after_jump=0x%x restored=%d env64_96=%s", (int)ret, (unsigned)m_save, (unsigned)m_after,
m_save == m_after, saved_hex);
probe_case(id, what, ret, 0, ex);
}
int main(void) {
signal(SIGALRM, probe_alarm);
alarm(PROBE_MAX_S);
probe_header();
guard_arm();
void* hl = dlopen("libSceLibcInternal.sprx", RTLD_LAZY);
p_setjmp = (sj_fn)(hl ? dlsym(hl, "setjmp") : 0);
p_longjmp = (lj_fn)(hl ? dlsym(hl, "longjmp") : 0);
p_sigsetjmp = (ssj_fn)(hl ? dlsym(hl, "sigsetjmp") : 0);
p_siglongjmp = (lj_fn)(hl ? dlsym(hl, "siglongjmp") : 0);
char ex[10];
snprintf(ex, sizeof ex, "%d%d%d%d%d", !!hl, !!p_setjmp, !!p_longjmp, !!p_sigsetjmp, !!p_siglongjmp);
probe_case("0", "dlopen libSceLibcInternal.sprx, dlsym setjmp longjmp sigsetjmp siglongjmp (1 yes, 0 no)", 0, 0, ex);
if(!p_setjmp || !p_longjmp) { probe_end(); return 0; }
sigset_t orig;
sigemptyset(&orig);
sigprocmask(SIG_SETMASK, 0, &orig);
memset(env, 0, sizeof env);
run("b", "setjmp with SIGUSR1 blocked, mask changed to SIGUSR2, longjmp(env, 1)", 0, SIGUSR1, 0, 1);
memset(env, 0, sizeof env);
run("c", "setjmp with nothing blocked, mask changed, longjmp(env, 0)", 0, 0, 0, 0);
memset(env, 0, sizeof env);
run("d", "setjmp with SIGHUP blocked, mask changed to SIGUSR2, longjmp(env, 7)", 0, SIGHUP, SIGUSR2, 7);
memset(env, 0, sizeof env);
if(p_sigsetjmp && p_siglongjmp) {
run("e.1", "sigsetjmp(env, 1) with SIGUSR1 blocked, mask changed, siglongjmp(env, 1)", 1, SIGUSR1, 0, 1);
memset(env, 0, sizeof env);
run("e.2", "sigsetjmp(env, 0) with SIGUSR1 blocked, mask changed, siglongjmp(env, 1)", 2, SIGUSR1, 0, 1);
} else {
probe_case("e", "sigsetjmp/siglongjmp not exported by libSceLibcInternal: cases e.1 and e.2 not run", 0, 0, "not run");
}
sigprocmask(SIG_SETMASK, &orig, 0);
probe_end();
return 0;
}stdoutAI-assisted (Claude Code). |
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against dbf5913dcf:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_underscore_jump_testsbuilt cleanly. - CTest (
guest_underscore_jumps):1/1 Test #5310: guest_underscore_jumps ........... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libc/src/JumpBuffer.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #5310: guest_underscore_jumps ...........Subprocess aborted***Exception: 0.22 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified that stmxcsr/ldmxcsr operate on a 32-bit memory location and are placed at offset 68(%rdi), fitting cleanly within bytes 68..71 of the 96-byte jmp_buf
- Verified that fnstcw/fldcw operate on a 16-bit memory location and are placed at offset 64(%rdi), occupying bytes 64..65 without overlapping MXCSR at 68
- Verified that longjmp, _setjmp, and _longjmp all use symmetric offsets (64 for x87 cw, 68 for MXCSR) matching setjmp, preserving caller and callee state correctly across both plain and underscore jump variants
- Verified GuestUnderscoreJumps.cpp test additions properly assert both plain and underscore jump buffers have the expected bit patterns at offset 64 and offset 68 without clobbering caller FPU control words
- Verified debt fragment docs/dev/debt/changes/fix-libc-setjmp-fpu-state-layout.md conforms to repository conventions and accurately documents the offset layout change
Looks Good To Me
|
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.
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
Head reviewed: dbf5913dcfc3408fcfabd5c0e11647cc6211d1ee against main. The diff moves the x87 control word to offset 64 and MXCSR to offset 68 in the setjmp/longjmp buffer, matching the description. I found no defect in the code I read.
What I ran
- Read the description, commit list, manifest and
diff.patchfor the head. - Read
core/libs/prx/libc/src/JumpBuffer.cppin full at the head. - Read
core/libs/tests/GuestUnderscoreJumps.cppat the head around the changed hunk (lines 140 to 220) and the helpers it uses. - Read the debt fragment
docs/dev/debt/changes/fix-libc-setjmp-fpu-state-layout.mdfrom the diff. - Searched the head for the x87 save and restore sites:
python3 repo_read.py search fnstcw
python3 repo_read.py cat core/libs/prx/libkernel/System/src/SignalSets.cpp 85 110
python3 repo_read.py cat core/libs/tests/GuestLibcInternalLongjmp.cpp 100 140Results
| Check | Result | Note |
|---|---|---|
| Changed code read | passed | Three of three changed files, with the caveats below |
| Offset use across the repo | passed | 22 fnstcw sites; libc and libkernel agree on 64 for the control word |
Build and ctest |
not run | No build in this pass |
| Hosted CI on this head | not run | Not checked |
Findings
Blocking
None.
Non-blocking
core/libs/prx/libc/src/JumpBuffer.cpp:98:sigsetjmpin libkernel (core/libs/prx/libkernel/System/src/SignalSets.cpp:98) writes only the control word at 64 and never MXCSR at 68. A buffer saved there and restored by thislongjmpnow loads MXCSR from bytes nobody wrote at 68. Before this change it loaded MXCSR from the control word at 64, so both outcomes are wrong. The fragment already sayssigsetjmpsaves only the control word, so this is documented rather than a regression. Consider naming it in the fragment as a known mismatch for buffers fromsigsetjmp.core/libs/prx/libc/src/JumpBuffer.cpp:128: the_longjmpmask merge now reads the saved MXCSR from 68, which matches the stores at lines 70 and 112. The change is consistent here.
Not verified
- Build,
ctest, andguest_underscore_jumpswere not run here. The results in the description are the author's and were not reproduced. - The firmware 11.60 console measurements (offsets, return values, signal mask) were not reproduced. The probe output is the author's.
- Hosted CI state for this head was not checked.
- Parts of
core/libs/tests/GuestUnderscoreJumps.cppoutside the changed hunk and its helpers were not read. - The rest of
SignalSets.cpp(thesiglongjmppath) was not read. - The claims about
libc.prxin PPSA21564 and the FreeBSD references were not checked. - The overlap with the other open pull requests and
tools/check_conventions.pywere not checked.
To get this to a good state
Nothing is required.
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
What
setjmpand_setjmpof this library saved MXCSR at offset 64 of the jump buffer and the x87 control word at 68, andlongjmpand_longjmpread them back from there. A PS5 payload process on firmware 11.60 called the console'ssetjmpandlongjmp(libSceLibcInternal.sprx) on a zeroed buffer and found the x87 control word at 64 (0x037f) and MXCSR at 68 (0x9fc0), and the signal mask at 72 (cases b, c and d, lines 3 to 5 of the raw output; the mask was0x20000000with SIGUSR1 blocked, 0 with none and0x1with SIGHUP blocked). This changes the 8 assembly operands inJumpBuffer.cpp(5 hunks) so thatsetjmp,_setjmp,longjmpand_longjmpuse the console's offsets; the saved values are the same, only the two offsets are exchanged. Save and restore were both changed, so a buffer this library fills is read back at the offsets it was written at. Thesigsetjmpoflibkernelalready writes the x87 word at 64.The signal mask at 72 was saved and restored on the console in all three cases, and
longjmp(env, 1),longjmp(env, 0)andlongjmp(env, 7)returned 1, 1 and 7; this library already does the same and the pull request does not touch that. Two things stay as they were, because the probe cannot tell: the flag at 88 (setjmpwrites 1 there,_setjmp0), since the probe zeroed the buffer before the call and the console may leave 80 and 88 alone, and whether the console restores the control word and MXCSR onlongjmp, since the probe did not change them between the calls.sigsetjmpandsiglongjmpare not exported bylibSceLibcInternal.sprxon firmware 11.60 (line 2, result11100: the library,setjmpandlongjmpfound, the other two not), so the console'ssigsetjmplayout is not measured either. No title is shown to read these fields; the entries indocs/dev/debt/libc.mdcalled the console behavior unverified, and this change closes that difference. The debt fragment changes two entries:setjmp, longjmpnow names the measured offsets, the signal mask, what is unmeasured (the restore of the control word and MXCSR, the flag at 88,sigsetjmpon the console) and says thatsigsetjmphere saves only the control word, not MXCSR;_setjmp, _longjmp, longjmpno longer says that the layout used by_setjmpdiffers from thelibc.prxbundled with PPSA21564 and states that it holds the control word at 64 and MXCSR at 68 like that module. The entrysetjmp, longjmpalso no longer says the console behavior is unverified.The test commit adds
CheckFloatingPointLayouttoGuestUnderscoreJumps.cpp: it sets the x87 precision control and the MXCSR rounding bits to known values, callssetjmpand_setjmpand checks that bits0x0300of the word at 64 and0x6000of the word at 68 hold them. It fails on the code before the fix.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 loadslibSceLibcInternal.sprxwithdlopenand takessetjmpandlongjmpwithdlsym, on 2026-11-16 at 09:34 UTC. A title process may differ. The cases are 0 to e, lines 2 to 6 of the raw output in the first comment.mainbefore (test commit only)setjmp0x037fat 64,0x9fc0at 68 (lines 3 to 5)guest_underscore_jumpsfails_setjmpthe samelibc.prx)longjmp; return values 1, 1, 7Windows 10 Pro 10.0.19045, one run per arm on the same base
upstream/maind9891959164, on 2026-11-16 (17:10 to 17:30 UTC): the build of the test commit alone finished withninjareturning 0 andctestreturned 8 (guest_underscore_jumpsfailed); the build of the fix, whose code is the same as the fix commit of this pull request (the two differ only in a debt file that this pull request now changes through a fragment), finished withninjareturning 0 andctestreturned 0. On Linux (WSL), on commits whose trees are those of the test commit and of the fix commit of this pull request (same base),ninjareturned 0 for both, the test commit alone ended withguest_underscore_jumpsaborted, and the fix commit passedguest_underscore_jumpsin three runs.tools/check_conventions.py --base upstream/mainpasses with no error and no warning (thesetjmp, longjmpentry is 972 characters, up from 523; the_setjmp, _longjmp, longjmpentry 827, up from 818). Not tested: macOS, the CI runner, a title process,sigsetjmpandsiglongjmpon the console (not exported there), the flag at 88, and the restore of the control word and MXCSR on the console.Overlap: the other open pull requests that change one of the three files of this branch (
core/libs/prx/libc/src/JumpBuffer.cpp,core/libs/tests/GuestUnderscoreJumps.cpp, the debt fragment; found by reading the changed files of all 1594 open pull requests at about 22:05 UTC, by reading what they change indocs/dev/debt/libc.mdand in the fragments underdocs/dev/debt/changes/forsetjmp,longjmp,JumpBufferandMXCSR, and by searching their titles and branch names for the same names) were test-merged on 2026-11-16 at about 22:05 UTC againstmainec9f1426283(the branch is based ond9891959164, an ancestor of it); the branch merges into it without a conflict,tools/check_conventions.py --base upstream/mainpasses on it, and the text the fragment changes occurs in exactly one entry each. Five pull requests share a file: #12965, #12700, #12688 and #12682 each carry the same change toJumpBuffer.cppandGuestUnderscoreJumps.cpp(74 added and 5 removed lines againstmain: a context identity at offset 92 of the buffer and a call before the jump in_longjmp), and #12315 lists the two files but changes nothing in them againstmain. All five already conflict withmainwithout this change (inFileStream.hpp,libkernelandlibSceLibreSsl3files, not in these three), and the set of conflicting files is the same with this change merged first; none conflicts only with this change, andJumpBuffer.cppandGuestUnderscoreJumps.cppmerge automatically in the four that carry the change. This change moves only the offsets 64 and 68 and leaves offset 92 and the code around it as it is; the merged result was not built or run. The four also append a sentence about offset 92 to thesetjmp, longjmpentry oflibc.md, which this change replaces through its fragment, so whichever is merged first, the other needs that sentence carried over in the entry. #21739 and #7544 change other entries oflibc.mdthat name MXCSR (nearbyintf,lgamma), and no open pull request changes the two entries this fragment changes in another way.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)