Repository navigation
fix(libkernel): sceKernelMapNamedDirectMemory returns ENAMETOOLONG for a 32 character name and EFAULT for a NULL name - #16945
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: sceKernelClockGettime, sceKernelGettimeofday, sceKernelUsleep, sceKernelNanosleep (own sleeps, total well under 1 s)
and sceKernelAllocateDirectMemory / sceKernelMapNamedDirectMemory (one own reservation each time, given back at once) on
firmware 11.60 (case sheets sceKernelClockGettime_I, Gettimeofday_I, Usleep_I, Nanosleep_I, AllocateDirectMemory_I,
MapNamedDirectMemory_I). No absolute time and no address is printed: only return codes, differences and resolutions.
Prototypes are assumptions from the case sheets. */
#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 struct {
long sec;
long nsec;
} Ts;
typedef struct {
long sec;
long usec;
} Tv;
typedef int (*cg_fn)(int, Ts*);
typedef int (*gtod_fn)(Tv*);
typedef int (*usleep_fn)(unsigned);
typedef int (*nanosleep_fn)(const Ts*, Ts*);
typedef long (*dmsize_fn)(void);
typedef int (*alloc_fn)(long, long, size_t, size_t, int, long*);
typedef int (*mapn_fn)(void**, size_t, int, int, long, size_t, const char*);
typedef int (*unmap_fn)(void*, size_t);
typedef int (*rel_fn)(long, size_t);
typedef int (*vq_fn)(const void*, int, void*, size_t);
#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)
static cg_fn f_cg;
static long now_ns(void) {
Ts t = {0, 0};
f_cg(4, &t);
return t.sec * 1000000000L + t.nsec;
}
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_cg = (cg_fn)dlsym(RTLD_DEFAULT, "sceKernelClockGettime");
gtod_fn f_gtod = (gtod_fn)dlsym(RTLD_DEFAULT, "sceKernelGettimeofday");
usleep_fn f_usleep = (usleep_fn)dlsym(RTLD_DEFAULT, "sceKernelUsleep");
nanosleep_fn f_nano = (nanosleep_fn)dlsym(RTLD_DEFAULT, "sceKernelNanosleep");
dmsize_fn f_size = (dmsize_fn)dlsym(RTLD_DEFAULT, "sceKernelGetDirectMemorySize");
alloc_fn f_alloc = (alloc_fn)dlsym(RTLD_DEFAULT, "sceKernelAllocateDirectMemory");
mapn_fn f_mapn = (mapn_fn)dlsym(RTLD_DEFAULT, "sceKernelMapNamedDirectMemory");
unmap_fn f_unmap = (unmap_fn)dlsym(RTLD_DEFAULT, "sceKernelMunmap");
rel_fn f_rel = (rel_fn)dlsym(RTLD_DEFAULT, "sceKernelReleaseDirectMemory");
vq_fn f_vq = (vq_fn)dlsym(RTLD_DEFAULT, "sceKernelVirtualQuery");
char line[160], extra[240], id[16], call[120];
snprintf(line, sizeof line, "clock=%d gtod=%d usleep=%d nanosleep=%d dmem=%d%d%d%d%d%d", !!f_cg, !!f_gtod, !!f_usleep, !!f_nano, !!f_size, !!f_alloc, !!f_mapn, !!f_unmap, !!f_rel, !!f_vq);
probe_case("0", "dlsym exported (1 yes, 0 no)", 0, 0, line);
int r, e;
if (f_cg) {
for (int ck = 0; ck <= 20; ck++) {
Ts t = {-1, -1};
RUN(r = f_cg(ck, &t));
snprintf(extra, sizeof extra, "sec_nonneg=%d nsec_below_1e9=%d signal=%d", t.sec >= 0, t.nsec >= 0 && t.nsec < 1000000000L, (int)guard_sig);
snprintf(id, sizeof id, "1.%d", ck + 1);
snprintf(call, sizeof call, "ClockGettime(clockid %d)", ck);
probe_case(id, call, r, e, extra);
}
{
Ts t;
RUN(r = f_cg(4, 0));
snprintf(extra, sizeof extra, "signal=%d", (int)guard_sig);
probe_case("1.30", "ClockGettime(4, NULL)", r, e, extra);
RUN(r = f_cg(-1, &t));
probe_case("1.31", "ClockGettime(-1, &t)", r, e, "-");
}
long best = 1000000000L, d0 = 0;
for (int i = 0; i < 200; i++) {
long a = now_ns(), b2 = now_ns();
long d = b2 - a;
if (d > 0 && d < best) best = d;
d0 = d;
}
snprintf(extra, sizeof extra, "min_nonzero_delta_ns=%ld last_delta_ns=%ld", best, d0);
probe_case("1.32", "ClockGettime(4) resolution over 200 pairs", 0, 0, extra);
}
if (f_gtod) {
Tv a = {0, 0}, b2 = {0, 0};
RUN(r = f_gtod(&a));
f_gtod(&b2);
snprintf(extra, sizeof extra, "usec_below_1e6=%d delta_us=%ld", a.usec >= 0 && a.usec < 1000000, (b2.sec - a.sec) * 1000000L + (b2.usec - a.usec));
probe_case("2.1", "Gettimeofday(&tv) twice", r, e, extra);
RUN(r = f_gtod(0));
snprintf(extra, sizeof extra, "signal=%d", (int)guard_sig);
probe_case("2.2", "Gettimeofday(NULL)", r, e, extra);
}
if (f_usleep && f_cg) {
static const unsigned us[] = {0, 1, 9, 10, 1000, 10000};
for (int i = 0; i < 6; i++) {
long t0 = now_ns();
RUN(r = f_usleep(us[i]));
long dt = now_ns() - t0;
snprintf(extra, sizeof extra, "elapsed_us=%ld signal=%d", dt / 1000, (int)guard_sig);
snprintf(id, sizeof id, "3.%d", i + 1);
snprintf(call, sizeof call, "Usleep(%u)", us[i]);
probe_case(id, call, r, e, extra);
}
}
if (f_nano && f_cg) {
static const Ts rq[] = {{0, 0}, {0, 1000000}, {0, 10000000}, {0, 1000000000L}, {0, -1}, {-1, 0}};
for (int i = 0; i < 6; i++) {
long t0 = now_ns();
RUN(r = f_nano(&rq[i], 0));
long dt = now_ns() - t0;
snprintf(extra, sizeof extra, "elapsed_us=%ld signal=%d", dt / 1000, (int)guard_sig);
snprintf(id, sizeof id, "4.%d", i + 1);
snprintf(call, sizeof call, "Nanosleep({%ld s, %ld ns}, NULL)", rq[i].sec, rq[i].nsec);
probe_case(id, call, r, e, extra);
}
Ts rem = {7, 7};
Ts rq2 = {0, 1000000};
RUN(r = f_nano(&rq2, &rem));
snprintf(extra, sizeof extra, "rem_sec=%ld rem_nsec=%ld", rem.sec, rem.nsec);
probe_case("4.7", "Nanosleep({0 s, 1 ms}, &rem)", r, e, extra);
}
if (f_alloc && f_rel) {
long total = f_size ? f_size() : 0x100000000L;
static const struct { int type; size_t len, align; } al[] = {
{11, 1 * MB, 0}, {12, 1 * MB, 0}, {11, 4 * MB, 16 * KB}, {12, 4 * MB, 64 * KB}, {12, 1 * MB, 1 * MB}, {12, 4 * MB, 2 * MB}, {12, 4 * MB, 8 * MB}, {12, 100000, 0}, {12, 0, 0}};
for (int i = 0; i < 9; i++) {
long ph = -1;
RUN(r = f_alloc(0, total, al[i].len, al[i].align, al[i].type, &ph));
snprintf(extra, sizeof extra, "phys_set=%d phys_mod_align=%ld signal=%d", ph != -1, (ph == -1 || !al[i].align) ? -1L : ph % (long)al[i].align, (int)guard_sig);
snprintf(id, sizeof id, "5.%d", i + 1);
snprintf(call, sizeof call, "AllocateDirectMemory(0, total, len %zu, align 0x%zx, type %d)", al[i].len, al[i].align, al[i].type);
probe_case(id, call, r, e, extra);
if (r == 0 && ph != -1 && al[i].len) f_rel(ph, al[i].len); /* a zero-length reservation has nothing of its own to give back */
}
long ph = -1;
RUN(r = f_alloc(1 * (long)MB, 0, 1 * MB, 0, 12, &ph));
snprintf(extra, sizeof extra, "phys_set=%d", ph != -1);
probe_case("5.10", "AllocateDirectMemory(start 1 MiB, end 0 (before start), 1 MiB)", r, e, extra);
if (r == 0 && ph != -1) f_rel(ph, 1 * MB);
}
if (f_alloc && f_mapn && f_unmap && f_rel) {
long total = f_size ? f_size() : 0x100000000L;
long ph = -1;
RUN(r = f_alloc(0, total, 4 * MB, 2 * MB, 12, &ph));
probe_case("6.1", "AllocateDirectMemory(4 MiB, 2 MiB align, type 12) for the named maps", r, e, "-");
if (r == 0 && ph != -1) {
static const struct { int prot, flags; size_t align; const char* name; } mp[] = {
{0x33, 0, 64 * KB, "f_probe_named"}, {0x32, 0, 64 * KB, "f_probe_named"}, {0x33, 0x400000, 64 * KB, "f_probe_named"}, {0x33, 0, 2 * MB, "f_probe_named"},
{0x33, 0, 64 * KB, "0123456789012345678901234567890"}, {0x33, 0, 64 * KB, "01234567890123456789012345678901"}, {0x33, 0, 64 * KB, 0}, {0x3, 0, 64 * KB, "f_probe_rw"}};
for (int i = 0; i < 8; i++) {
void* a = 0;
RUN(r = f_mapn(&a, 64 * KB, mp[i].prot, mp[i].flags, ph, mp[i].align, mp[i].name));
char nm[40] = "-";
int rq = -9;
if (a && f_vq) {
unsigned char info[160];
memset(info, 0, sizeof info);
rq = f_vq(a, 0, info, 72);
memcpy(nm, info + 33, 32);
nm[31] = 0;
for (int k = 0; nm[k]; k++)
if ((unsigned char)nm[k] < 32 || (unsigned char)nm[k] > 126) nm[k] = '?';
}
snprintf(extra, sizeof extra, "mapped=%d align_ok=%d vq_rc=%d name=[%s] signal=%d", a != 0, a ? ((uintptr_t)a % mp[i].align) == 0 : -1, rq, nm, (int)guard_sig);
snprintf(id, sizeof id, "6.%d", i + 2);
snprintf(call, sizeof call, "MapNamedDirectMemory(64 KiB, prot 0x%x, flags 0x%x, align 0x%zx, name of %zu chars)", mp[i].prot, mp[i].flags, mp[i].align, mp[i].name ? strlen(mp[i].name) : 0);
probe_case(id, call, r, e, extra);
if (a) f_unmap(a, 64 * KB);
}
RUN(r = f_rel(ph, 4 * MB));
probe_case("6.10", "ReleaseDirectMemory(phys, 4 MiB) own block", 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. |
|
Merged AI-assisted (Claude Code). |
|
Windows check of head Setup: Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (r7), Ninja, Release,
AI-assisted: yes. |
923988c to
344e54d
Compare
|
@xXPBSXx I reviewed 344e54d. The 31-character result (0, name stored) and the 32-character result (0x8002003f, errno 63, mapped, unnamed) match probe cases 6.6 and 6.7, and the check runs after a successful map so the other errors keep their order. One claim does not match the probe. Probe case 6.8 is labelled "name of 0 chars", but its table entry is Please do one of these: re-measure with a real "" argument and keep EFAULT if the console returns 0x8002000e for it, or make NULL return 0x8002000e as measured and drop the "" claim. Either way, please say NULL in the body, the test and the debt entry where that is what was measured. A 33-character case would also help, or say that it is inferred from the 32-character one. |
adriadam10
left a comment
There was a problem hiding this comment.
Thanks, the 31 and 32 character results match probe cases 6.6 and 6.7. One measured claim does not match the probe, as @oneandonlydean noted:
- Probe case 6.8 is labelled "name of 0 chars", but its table entry is
{0x33, 0, 64 * KB, 0}: the name argument is NULL, not"". What the console measured is NULL name →0x8002000ewith the range mapped and unnamed. - This PR keeps NULL returning 0 (
Export.cpp:360,if (result == 0 && name)) and returnsEFAULTfor"", which no probe case covers. The PR body, the test (GuestMemory.cpp:254) and the debt entry (libkernel.md:209) all present that as measured.
To fix, either:
- re-measure with a real
""argument and keepEFAULTif the console returns it, or - make a NULL name return
EFAULTafter the mapping as measured, drop the""claim, and say NULL in the body, test and debt entry (check that no internal caller ofsceKernelMapNamedDirectMemorypasses NULL).
Please also say that names longer than 32 characters are inferred from the 32 character case, or add a 33 character probe.
|
Tested
AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports. |
… a 32 character name and a NULL name Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…irectMemory for a 32 character name or a NULL name Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
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) |
|
The points of the review of
The review of AI-assisted (Claude Code). |
|
The change request of 12 November is addressed in the current head |
ligmaSec
left a comment
There was a problem hiding this comment.
The Linux integration build passed the registered GuestMemory CTest, including the added named-memory mapping cases.
AI-assisted: yes.
What
sceKernelMapNamedDirectMemoryreturned 0 for a name of 32 or more characters and named the range with the name cut to 31, and returned 0 for a NULL name. On a PS5 the mapping succeeds in both cases and the call returnsSCE_KERNEL_ERROR_ENAMETOOLONG(0x8002003f, errno 63) for a name of 32 characters andSCE_KERNEL_ERROR_EFAULT(0x8002000e, errno 14) for a NULL name; the range stays mapped and has no name of its own (it readsanon:and hex digits), and a name of 31 characters is stored (lines 57 to 59 of the output in the first comment). This does the same: after a successful mapping, a name of 32 or more bytes returnsENAMETOOLONGand a NULL name returnsEFAULT, and neither names the range. A test pins the three results, a non-null mapping in each case, and the name read back; it also pins thatsceKernelMapNamedFlexibleMemoryis unchanged. The debt entry indocs/dev/debt/libkernel.mdstates the measurement through a debt fragment.The NULL is what the probe passed. The probe's case 6.8 is labelled "name of 0 chars", but its table entry is
{0x33, 0, 64 * KB, 0}, so the name argument is a NULL pointer, and the label comes from printing 0 for a NULL name (mp[i].name ? strlen(mp[i].name) : 0in the probe source in the first comment). The first version of this pull request returnedEFAULTfor an empty string and called that measured; the review pointed out that no probe case passes an empty string, so that is removed. An empty string is not measured and is unchanged here, and the entry says so.Not measured: a name of 33 or more characters (it follows from the 32-character case and from
sceKernelSetVirtualRangeName, whose limit is the same 31 characters), an empty string,sceKernelMapNamedFlexibleMemory(unchanged, and it still cuts a longer name to 31 characters here), a title process, and other firmware. The range without a name reads as empty here where the console reportsanon:and hex digits; that is not changed.git grepfinds no call ofsceKernelMapNamedDirectMemoryin this repository outside its definition, its declaration and tests, and no test other than the new one passes NULL. #16869 is merged, so this branch is rebuilt on currentmainand no longer depends on it.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 19:25 UTC (theutcin thePROBEline is the console's own clock, about 1 h 42 min ahead). A title process may differ. The calls mapped 64 KiB of an own 4 MiB direct memory block with protection 0x33 and alignment 0x10000, and the name was read back withsceKernelVirtualQuery. The probe source and its raw output are in the first comment.mainbeforeanon:..., line 58)anon:..., line 59)Red and green on one base: Windows 10 Pro (MinGW GCC, Release,
cmake -DBUILD_TESTING=ON,ninja -j4 guest_memory_tests,ctest -R ^guest_memory$, one worktree, a checkout, cmake, ninja and ctest per phase), baseupstream/mainfd6d6f4a8498: on the baseguest_memorypasses (21:13 UTC on 2026-11-19); with the test commitee6651cb08cfit fails (21:15 UTC; exit code 0xc0000409 and "Guest memory check failed at core/libs/tests/GuestMemory.cpp:286", the check of the call's result in the loop over the cases; the message does not name the case, and that it is the 32-character one follows from the order of the cases, where the first returns 0 onmain); with the fix commit528371df05c6it passes (21:18 UTC); with the documentation commitccde1e977bd0it passes (21:21 UTC).tools/check_conventions.py --base fd6d6f4a8498andtools/debt_fold.py --checkpass on the head, and a merge intoupstream/maina410f24ddadwas checked withgit merge-treeand is clean. The change is 3 files, +46/-2, with no new comments in code. Linux and macOS were not run here; CI will. Not tested: a title process and any other firmware.Overlap: the other open pull requests that change the files of this branch (
core/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/tests/GuestMemory.cppand the fragmentdocs/dev/debt/changes/fix-kernel-map-named-direct-memory-null-name.md, a new file, for the entry ofsceKernelMapNamedDirectMemoryindocs/dev/debt/libkernel.md), found by reading the changed files of all 2028 open pull requests at about 22:31 to 22:50 UTC on 2026-11-19, by reading what they change in every file whose path containsDirectMemory,GuestMemory,VirtualRangeorNamedDirectand in all debt files forsceKernelMapNamedDirectMemory,NameMappedRange,CopyRangeNameandMapNamedFlexibleMemory, and by searching their titles and branch names for the same names, were checked on 2026-11-19 at about 23:06 UTC againstmaina410f24ddadtogether with this head: the branch is based onfd6d6f4a849, an ancestor of it, merges intomainwithout a conflict, andtools/debt_fold.py --checkapplies the fragment together with every other fragment onmainwithout an error. This branch replaces the earlier head344e54d0f5bof this pull request, which is not an ancestor of the new head. Two other pull requests carry changes to the same function: #17718 (fix(libkernel): sceKernelMapNamedSystemFlexibleMemory maps and then rejects a name of 32 characters or a null name) contains an earlier copy of the commits of this pull request (ab942cf2991,03ccc499427,de6c7c5e3dc, the variant with the empty-name result) in front of its own three commits, and so conflicts with this head inExport.cppandGuestMemory.cpp; it has to be rebased onto the new head once this one is pushed. #21569 (fix(libkernel): bound virtual range names as the console does) adds a test function toGuestMemory.cppand changesExport.cpp, and does not change a line that names these functions; it already conflicts withmaininExport.cpp, and with this branch applied it also conflicts inGuestMemory.cpp(both add a check function in the same place), so whichever of the two is merged second needs a rebase of that test file. Four further pull requests show these names only as unchanged context or as other calls: #19183 and #23770 callsceKernelMapNamedDirectMemoryandsceKernelMapNamedFlexibleMemorywith short names inGuestMemory.cpp, #18585 callssceKernelMapNamedDirectMemoryinGuestKernelDirectMemoryMapping.cppwith the names "bad" and "test_dmem_named" (3 and 15 characters, so none is affected), and #19486 shows the definitions in its patch, but its head is identical tomaininExport.cppandGuestMemory.cpp. With this branch applied tomain, all 71 pull requests that list one of these files or matched a name were merged on top of it: 31 of them already conflict withmainalone, and apart from #17718 and #21569 (and the old head of this pull request) none has more conflicting files with this branch than without it. The merged result was not built or run beyond the red and green runs above.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)