Repository navigation
Conversation
|
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);
}
/* Probe F: sceKernelAllocateDirectMemory, sceKernelMapDirectMemory, sceKernelMprotect, sceKernelMunmap and
sceKernelReleaseDirectMemory on firmware 11.60 (docs/dev/debt/libkernel.md: direct memory entries). Only memory this probe
reserves itself is touched: one 4 MiB direct-memory block, mapped in small pieces and given back at the end. Addresses
are never printed; each case reports the return value, errno, and whether the first byte of the range can be read and
written afterwards. Prototypes are assumptions from the case sheets:
sceKernelAllocateDirectMemory(off_t start, off_t end, size_t len, size_t align, int type, off_t* phys),
sceKernelMapDirectMemory(void** addr, size_t len, int prot, int flags, off_t phys, size_t align),
sceKernelMprotect(void* addr, size_t len, int prot), sceKernelMunmap(void* addr, size_t len),
sceKernelReleaseDirectMemory(off_t start, size_t len). */
#include <dlfcn.h>
#include <setjmp.h>
#include <stdint.h>
static sigjmp_buf guard_env;
static volatile int guard_sig = 0;
static void guard_handler(int s) {
guard_sig = s;
siglongjmp(guard_env, 1);
}
typedef long (*dmsize_fn)(void);
typedef int (*alloc_fn)(long, long, size_t, size_t, int, long*);
typedef int (*map_fn)(void**, size_t, int, int, long, size_t);
typedef int (*prot_fn)(void*, size_t, int);
typedef int (*unmap_fn)(void*, size_t);
typedef int (*rel_fn)(long, size_t);
static dmsize_fn f_size;
static alloc_fn f_alloc;
static map_fn f_map;
static prot_fn f_prot;
static unmap_fn f_unmap;
static rel_fn f_rel;
#define RUN(stmt) \
do { \
errno = 0; \
guard_sig = 0; \
if (sigsetjmp(guard_env, 1) == 0) { \
stmt; \
} \
e = errno; \
} while (0)
#define KB 1024UL
#define MB (1024UL * 1024UL)
/* 'R' readable, 'W' writable (a write of the byte already there), '-' a fault; recorded without printing the address */
static void access_state(volatile unsigned char* p, char* out) {
out[0] = out[1] = '-';
out[2] = 0;
if (!p) return;
guard_sig = 0;
if (sigsetjmp(guard_env, 1) == 0) {
unsigned char v = *p;
out[0] = 'R';
*p = v;
out[1] = 'W';
}
}
static int id_n = 0;
static void emit(const char* grp, const char* call, long r, int e, const char* extra) {
char id[16];
snprintf(id, sizeof id, "%s.%d", grp, ++id_n);
probe_case(id, call, r, e, extra);
}
/* one map of 'len' bytes at phys 'ph'; on success the range is returned in *out and stays mapped */
static int do_map(void** out, size_t len, int prot, int flags, long ph, size_t align, int* err) {
int r = -99, e;
void* a = 0;
RUN(r = f_map(&a, len, prot, flags, ph, align));
*err = e;
*out = (r == 0 && !guard_sig) ? a : 0;
return guard_sig ? -98 : r;
}
int main(void) {
signal(SIGALRM, probe_alarm);
alarm(PROBE_MAX_S);
probe_header();
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);
f_size = (dmsize_fn)dlsym(RTLD_DEFAULT, "sceKernelGetDirectMemorySize");
f_alloc = (alloc_fn)dlsym(RTLD_DEFAULT, "sceKernelAllocateDirectMemory");
f_map = (map_fn)dlsym(RTLD_DEFAULT, "sceKernelMapDirectMemory");
f_prot = (prot_fn)dlsym(RTLD_DEFAULT, "sceKernelMprotect");
f_unmap = (unmap_fn)dlsym(RTLD_DEFAULT, "sceKernelMunmap");
f_rel = (rel_fn)dlsym(RTLD_DEFAULT, "sceKernelReleaseDirectMemory");
char line[160], extra[220], call[120], st[4];
snprintf(line, sizeof line, "size=%d alloc=%d map=%d mprotect=%d munmap=%d release=%d", !!f_size, !!f_alloc, !!f_map, !!f_prot, !!f_unmap, !!f_rel);
probe_case("0", "dlsym exported (1 yes, 0 no)", 0, 0, line);
if (!f_alloc || !f_map || !f_unmap || !f_rel || !f_prot) {
probe_end();
return 0;
}
int e, r;
long total = f_size ? f_size() : 0;
snprintf(extra, sizeof extra, "total_mib=%ld", total / (long)MB);
probe_case("1", "sceKernelGetDirectMemorySize()", total, 0, extra);
long ph = -1;
RUN(r = f_alloc(0, total ? total : 0x100000000L, 4 * MB, 2 * MB, 3, &ph));
snprintf(extra, sizeof extra, "phys_set=%d phys_mod_2mib=%ld signal=%d", ph != -1, ph == -1 ? -1L : (long)(ph % (long)(2 * MB)), (int)guard_sig);
probe_case("2", "sceKernelAllocateDirectMemory(0, total, 4 MiB, 2 MiB, 3, &phys)", r, e, extra);
if (r != 0 || ph == -1) {
probe_end();
return 0;
}
void* a;
/* piece 0..8: protection values of the titles, 16 KiB each, alignment 0 */
static const int prots[] = {3, 0x33, 0xc2, 0xf2, 0x3f2, 0x32, 2, 1, 0};
for (int i = 0; i < 9; i++) {
int er;
r = do_map(&a, 16 * KB, prots[i], 0, ph + (long)i * (long)(16 * KB), 0, &er);
access_state((volatile unsigned char*)a, st);
snprintf(extra, sizeof extra, "access=%s signal=%d", st, (int)guard_sig);
snprintf(call, sizeof call, "MapDirectMemory(16 KiB, prot 0x%x, flags 0, align 0)", prots[i]);
emit("3", call, r, er, extra);
if (a) f_unmap(a, 16 * KB);
}
/* flags of the titles with prot 3 */
static const int flg[] = {0x10, 0x400010, 0x90, 0x400000, 0x80};
for (int i = 0; i < 5; i++) {
int er;
r = do_map(&a, 16 * KB, 3, flg[i], ph + 128 * (long)KB, 0, &er);
access_state((volatile unsigned char*)a, st);
snprintf(extra, sizeof extra, "access=%s signal=%d", st, (int)guard_sig);
snprintf(call, sizeof call, "MapDirectMemory(16 KiB, prot 3, flags 0x%x, align 0)", flg[i]);
emit("4", call, r, er, extra);
if (a) f_unmap(a, 16 * KB);
}
/* alignments of the titles (phys is 2 MiB aligned, so phys + 0 satisfies all of them) */
static const size_t als[] = {16 * KB, 64 * KB, 1 * MB, 2 * MB, 4 * MB};
for (int i = 0; i < 5; i++) {
int er;
r = do_map(&a, 16 * KB, 3, 0, ph, als[i], &er);
access_state((volatile unsigned char*)a, st);
snprintf(extra, sizeof extra, "addr_aligned_to_request=%d access=%s signal=%d", a ? ((uintptr_t)a % als[i]) == 0 : -1, st, (int)guard_sig);
snprintf(call, sizeof call, "MapDirectMemory(16 KiB, prot 3, flags 0, align 0x%zx)", als[i]);
emit("5", call, r, er, extra);
if (a) f_unmap(a, 16 * KB);
}
/* Mprotect on a 64 KiB mapping of our own */
void* m;
int er;
r = do_map(&m, 64 * KB, 3, 0, ph + 1 * (long)MB, 0, &er);
emit("6", "MapDirectMemory(64 KiB, prot 3) for the Mprotect cases", r, er, "-");
if (m) {
static const int mp[] = {0x11, 0x12, 0x3f2, 0xc2, 0xc3, 0x42, 0x82, 0x13, 0x3, 0x1, 0x7};
for (int i = 0; i < 11; i++) {
RUN(r = f_prot(m, 64 * KB, mp[i]));
access_state((volatile unsigned char*)m, st);
snprintf(extra, sizeof extra, "access_after=%s signal=%d", st, (int)guard_sig);
snprintf(call, sizeof call, "Mprotect(mapped 64 KiB, prot 0x%x)", mp[i]);
emit("7", call, r, e, extra);
}
RUN(r = f_prot(m, 64 * KB, 3));
emit("7", "Mprotect(mapped 64 KiB, prot 3) restore", r, e, "-");
RUN(r = f_prot(m, 0, 3));
emit("7", "Mprotect(mapped, len 0, prot 3)", r, e, "-");
RUN(r = f_prot(m, 100, 3));
emit("7", "Mprotect(mapped, len 100, prot 3)", r, e, "-");
RUN(r = f_prot((char*)m + 1, 16 * KB, 3));
emit("7", "Mprotect(mapped + 1, 16 KiB, prot 3)", r, e, "-");
RUN(r = f_prot(m, 128 * KB, 3));
emit("7", "Mprotect(mapped, 128 KiB over a 64 KiB mapping, prot 3)", r, e, "-");
/* Munmap on the same mapping */
RUN(r = f_unmap(m, 0));
emit("8", "Munmap(mapped, len 0)", r, e, "-");
RUN(r = f_unmap(m, 100));
emit("8", "Munmap(mapped, len 100)", r, e, "-");
RUN(r = f_unmap(m, 16 * KB));
emit("8", "Munmap(mapped, 16 KiB of 64 KiB)", r, e, "-");
access_state((volatile unsigned char*)m, st);
emit("8", "access after partial Munmap (first 16 KiB)", 0, 0, st);
RUN(r = f_unmap((char*)m + 16 * KB, 48 * KB));
emit("8", "Munmap(rest 48 KiB)", r, e, "-");
RUN(r = f_unmap(m, 64 * KB));
emit("8", "Munmap(already unmapped 64 KiB)", r, e, "-");
RUN(r = f_prot(m, 64 * KB, 3));
emit("8", "Mprotect(already unmapped 64 KiB, prot 3)", r, e, "-");
}
/* ReleaseDirectMemory: only inside the 4 MiB this probe allocated */
RUN(r = f_rel(ph, 1));
emit("9", "ReleaseDirectMemory(phys, 1)", r, e, "-");
RUN(r = f_rel(ph, 0));
emit("9", "ReleaseDirectMemory(phys, 0)", r, e, "-");
RUN(r = f_rel(ph + 1, 16 * KB));
emit("9", "ReleaseDirectMemory(phys + 1, 16 KiB)", r, e, "-");
RUN(r = f_rel(ph, 16 * KB));
emit("9", "ReleaseDirectMemory(phys, 16 KiB)", r, e, "-");
RUN(r = f_rel(ph, 16 * KB));
emit("9", "ReleaseDirectMemory(phys, 16 KiB) again", r, e, "-");
RUN(r = f_rel(ph, 4 * MB));
emit("9", "ReleaseDirectMemory(phys, 4 MiB) rest of the block", r, e, "-");
RUN(r = f_rel(ph, 4 * MB));
emit("9", "ReleaseDirectMemory(phys, 4 MiB) again", r, e, "-");
probe_end();
return 0;
}stdoutThe process ended with exit value 0. |
|
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.
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.
|
|
Windows check of head Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.
AI-assisted: yes. |
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
|
Windows check of head Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release,
AI-assisted: yes. |
|
@xXPBSXx the code and test look right to me: the zero-length shortcut sits after the alignment and range checks, which matches your fw 11.60 probe cases, and CheckReleaseDirectMemoryZeroLength exercises the real API. The docs need work before this can pass:
Please also rebase onto main; the libkernel.md bullets overlap with #16869. |
…ength of 0 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…r a length of 0 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
6c33cf2 to
15a8b0b
Compare
|
Thanks for the review. The branch is rebuilt on
Rebase: since the branch no longer edits Red and green on the new commits, 17 November 2026 (UTC): with only the test commit, I will update the description to the new head and these numbers. AI-assisted (Claude Code). |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
The change matches the documented PS5 payload observation, validates the range before returning success for zero length, and adds regression coverage for the changed behavior.
What I ran
Reviewed head 15a8b0b46f84bb8dfc86c39a63ce89e8fb1108a0 against the prepared diff. No tests or builds were run. Read core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestMemory.cpp, and docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md; the complete text of each changed file was available. The excluded-file list was not available as a separate artifact.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed lines | passed | All added lines reviewed |
| Targeted code review | passed | Range validation precedes the zero-length return |
| Tests and build | not run | Author-reported runs only |
| Console behavior | not run | PS5 observation is author-reported |
Findings
Blocking
None.
Non-blocking
None.
Not verified
The author reports PS5 firmware 11.60 behavior and host test runs; these were not independently reproduced. No tests, builds, CI, title-process behavior, or other firmware were checked. No claims are made about excluded files.
To get this to a good state
Nothing is required.
|
Tested head This PR makes Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, AI-assisted: yes (Claude Code) |
|
@xXPBSXx this head (15a8b0b) fails guest_memory when run alone on main 8b72a1e: "flexible memory exhausted: request 0x1bfbc000 with 0x64000 of 0x1c000000 in use", then "Guest memory check failed at core/libs/tests/GuestMemory.cpp:741" and the test aborts. The same train without this PR passed, so the change in this PR is what trips it. I have held it until that test passes; please rebase on main and run guest_memory locally. |
…ory in the physical pages test Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Thank you for running this. The failing assertion is line 741 of I merged Could you re-run it on the new head? AI-assisted (Claude Code). |
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the targeted implementation and tests support the reported zero-length behavior.
What I ran
Reviewed head b561ad37603674c3f66d52781cacbe48b8ef2ea0 using the prepared diff and repo_read.py. No commands or tests were run in this review.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed source | passed | Three changed files read |
| Tests | not run | Author-reported runs only |
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests were run independently. The author reports PS5 measurements on firmware 11.60 in a payload process and host test runs; these were not independently verified. The changed files listed in the manifest were read: core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/tests/GuestMemory.cpp, and docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md. No changed files were excluded.
To get this to a good state
Nothing is required.
ligmaSec
left a comment
There was a problem hiding this comment.
The Linux integration build passed the registered GuestMemory CTest, including zero-length release and neighboring argument checks.
AI-assisted: yes.
Plyxen
left a comment
There was a problem hiding this comment.
I read the diff at b561ad3760. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at 6c6b29320fe together with other open PRs (so not an isolated build).
guest_memory: pass on both.- Control (the same tree with only this PR's source change taken out): on Windows it fails at line 742. On Linux, with the changed sources of the tested CPU PRs put back to
main's, the same test fails too.
Not checked: a console.
AI-assisted: yes (Claude Code)
What
sceKernelReleaseDirectMemoryreturnedEINVALfor a length of 0, andguest_memorypinnedRelease(0, 0)asEINVALwithout a console result. On a PS5, releasing a length of 0 at an aligned start returns 0, while a length of 1 and an unaligned start (phys + 1, 16 KiB) returnEINVAL, and releasing a block and releasing it again return 0 (cases 9.44 to 9.48, lines 48 to 52 of the raw output). This returns 0 for a length of 0 at an aligned start before the range is claimed; the twoEINVALcases and a start below 0 or unaligned are unchanged. The test now expectsRelease(0, 0)to return 0 and pins the five measured results. A debt fragment,docs/dev/debt/changes/fix-kernel-release-direct-memory-zero-length.md, changes thesceKernelReleaseDirectMemoryentry ofdocs/dev/debt/libkernel.mdto record the measurement; an unaligned start with a length of 0, a start at the end of direct memory and a title process were not measured.Tested
Measured on a PS5 with firmware 11.60 (
kernel_get_fw_version0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-07 at 08:55 UTC. A title process may differ. The calls released a block of the probe's own 4 MiB direct memory; the cases are 9.44 to 9.48, lines 48 to 52 of the raw output in the first comment.mainbeforesceKernelReleaseDirectMemory(phys, 0)EINVALsceKernelReleaseDirectMemory(phys, 1)EINVALsceKernelReleaseDirectMemory(phys + 1, 16 KiB)EINVALsceKernelReleaseDirectMemory(phys, 16 KiB), then againOn this branch,
maineb2d16b170b, on 2026-11-17: with only the test commitb220bf30d12,guest_memoryfails on Windows (MinGW, Release, 23:01 to 23:05 UTC, one run) withGuest memory check failed at core/libs/tests/GuestMemory.cpp:1052, which isRelease(0, 0) == 0, and on Linux (WSL, Release, from 22:29 UTC, one run) withSubprocess aborted; with the fix commit81531bed9b8it passes in 3 of 3 runs on Linux, and with the docs commit15a8b0b46f8it passes in 1 run on Windows (23:04 to 23:07 UTC) and in 3 of 3 runs on Linux. The Linux commits were applied withgit amto a clean checkout ofmain.tools/check_conventions.py --base upstream/mainpasses on each of the three commits on Linux. The probe source and its raw output are in the first comment. Not tested: macOS, the CI runner, a title process and any other firmware.Overlap (2026-11-18, list of open pull requests read from 23:59 UTC on 2026-11-17 to 00:16 UTC, test-merged at 00:18 UTC against
main4568142448e, head15a8b0b46f84): of the 1786 open pull requests, 12 changecore/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/tests/GuestMemory.cpporcore/libs/prx/libkernel/DirectMemory/MemoryPool.cpp(#22051, #22040, #22013, #21569, #20349, #19183, #18293, #17936, #17718, #16945, #16372 and #12315), and each was merged with and without this branch. None conflicts only with this branch: five conflict withmainby themselves (#21569, #17718 and #16945 inDirectMemory/Export.cpp, #18293 inWindowsMappings.hpp, #12315 inlibSceLibreSsl3) and the other seven merge cleanly with and without it. None of them changessceKernelReleaseDirectMemory; #22013, #20349, #17718, #16945 and #12315 only add or keep calls of it in tests, and the hunk of #19183 sits insceKernelCheckedReleaseDirectMemory. The branch no longer editsdocs/dev/debt/libkernel.md, so the debt lines of #16869 (merged on 2026-11-12) and #16945 no longer meet this change, and no open pull request changes a debt line that namessceKernelReleaseDirectMemory.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)