Repository navigation
fix(libkernel): posix mlock and munlock lock mapped pages like the console - #24706
Conversation
Console evidenceMeasurement context: PS5 Pro,
The same session measured the pthread attribute family (type 1..4 with default 1, protocol 0..2, An earlier run ( Probe sources: tw5b/main.c#include <ps5/kernel.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef int (*AttrFn)(void*);
typedef int (*SetTypeFn)(void*, int);
typedef int (*GetTypeFn)(void*, int*);
typedef int (*GetkindAFn)(void*);
typedef int (*GetkindBFn)(void*, int*);
typedef int (*MLockFn)(const void*, uint64_t);
typedef int (*ClockSetFn)(int, const struct timespec*);
static void step(const char* what) {
printf("T5B|STEP|%s\n", what);
fflush(stdout);
}
int main(void) {
printf("T5B|FW|0x%x\n", kernel_get_fw_version());
fflush(stdout);
void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
printf("T5B|LK|%p|%s\n", lk, lk ? "ok" : dlerror());
fflush(stdout);
if (!lk) return 1;
MLockFn MLOCK = (MLockFn)dlsym(lk, "mlock");
MLockFn MUNLOCK = (MLockFn)dlsym(lk, "munlock");
ClockSetFn CLOCKSET = (ClockSetFn)dlsym(lk, "clock_settime");
AttrFn MA_INIT = (AttrFn)dlsym(lk, "scePthreadMutexattrInit");
AttrFn MA_DESTROY = (AttrFn)dlsym(lk, "scePthreadMutexattrDestroy");
SetTypeFn MA_SETTYPE = (SetTypeFn)dlsym(lk, "scePthreadMutexattrSettype");
GetTypeFn MA_GETTYPE = (GetTypeFn)dlsym(lk, "scePthreadMutexattrGettype");
SetTypeFn MA_SETPROTO = (SetTypeFn)dlsym(lk, "scePthreadMutexattrSetprotocol");
GetTypeFn MA_GETPROTO = (GetTypeFn)dlsym(lk, "scePthreadMutexattrGetprotocol");
SetTypeFn MA_SETKIND = (SetTypeFn)dlsym(lk, "scePthreadMutexattrSetkind");
GetkindAFn GETKIND_A = (GetkindAFn)dlsym(lk, "scePthreadMutexattrGetkind");
GetkindBFn GETKIND_B = (GetkindBFn)dlsym(lk, "scePthreadMutexattrGetkind");
SetTypeFn MA_SETPRIO = (SetTypeFn)dlsym(lk, "scePthreadMutexattrSetprioceiling");
GetTypeFn MA_GETPRIO = (GetTypeFn)dlsym(lk, "scePthreadMutexattrGetprioceiling");
AttrFn RW_INIT = (AttrFn)dlsym(lk, "scePthreadRwlockattrInit");
SetTypeFn RW_SETPSH = (SetTypeFn)dlsym(lk, "scePthreadRwlockattrSetpshared");
GetTypeFn RW_GETPSH = (GetTypeFn)dlsym(lk, "scePthreadRwlockattrGetpshared");
static uint8_t page[4096] __attribute__((aligned(4096)));
if (MLOCK) {
step("mlock-valid");
errno = 0;
int rc = MLOCK(page, sizeof(page));
printf("T5B|mlock|rc=%d|errno=%d\n", rc, errno);
fflush(stdout);
if (MUNLOCK) {
step("munlock");
errno = 0;
rc = MUNLOCK(page, sizeof(page));
printf("T5B|munlock|rc=%d|errno=%d\n", rc, errno);
fflush(stdout);
}
}
if (CLOCKSET) {
struct timespec now, after;
clock_gettime(0, &now);
step("clock-set-now");
errno = 0;
int rc = CLOCKSET(0, &now);
clock_gettime(0, &after);
printf("T5B|clock-set-now|rc=%d|errno=%d|drift=%lld\n", rc, errno, (long long)(after.tv_sec - now.tv_sec));
fflush(stdout);
step("clock-set-null");
errno = 0;
rc = CLOCKSET(0, 0);
printf("T5B|clock-set-null|rc=%d|errno=%d\n", rc, errno);
fflush(stdout);
}
static uint64_t mattr[8];
if (MA_INIT) {
step("ma-init");
memset(mattr, 0xAA, sizeof(mattr));
errno = 0;
printf("T5B|ma-init|rc=%d\n", MA_INIT(mattr));
fflush(stdout);
}
if (MA_SETTYPE && MA_GETTYPE) {
static const int types[] = {0, 1, 2, 3, 4, 5, 999};
for (unsigned i = 0; i < sizeof(types) / sizeof(types[0]); ++i) {
char id[32];
snprintf(id, sizeof(id), "type-%d", types[i]);
step(id);
errno = 0;
int rc = MA_SETTYPE(mattr, types[i]);
int val = -777;
int grc = MA_GETTYPE(mattr, &val);
printf("T5B|%s|set=%d|get=%d|val=%d\n", id, rc, grc, val);
fflush(stdout);
}
}
if (MA_SETPROTO && MA_GETPROTO) {
static const int protos[] = {0, 1, 2, 999};
for (unsigned i = 0; i < sizeof(protos) / sizeof(protos[0]); ++i) {
char id[32];
snprintf(id, sizeof(id), "proto-%d", protos[i]);
step(id);
errno = 0;
int rc = MA_SETPROTO(mattr, protos[i]);
int val = -777;
int grc = MA_GETPROTO(mattr, &val);
printf("T5B|%s|set=%d|get=%d|val=%d\n", id, rc, grc, val);
fflush(stdout);
}
}
if (MA_SETKIND) {
static const int kinds[] = {-1, 0, 1, 2, 3, 999};
for (unsigned i = 0; i < sizeof(kinds) / sizeof(kinds[0]); ++i) {
char id[40];
snprintf(id, sizeof(id), "kind-%d", kinds[i]);
step(id);
errno = 0;
int rc = MA_SETKIND(mattr, kinds[i]);
int val = -777;
int ga = GETKIND_A ? GETKIND_A(mattr) : -1;
int gb = GETKIND_B ? GETKIND_B(mattr, &val) : -1;
printf("T5B|%s|set=%d|getA=0x%x|getB=0x%x|valB=%d\n", id, rc, (unsigned)ga, (unsigned)gb, val);
fflush(stdout);
}
}
if (MA_SETPRIO && MA_GETPRIO) {
static const int prios[] = {0, 100, -1};
for (unsigned i = 0; i < sizeof(prios) / sizeof(prios[0]); ++i) {
char id[32];
snprintf(id, sizeof(id), "prio-%d", prios[i]);
step(id);
errno = 0;
int rc = MA_SETPRIO(mattr, prios[i]);
int val = -777;
int grc = MA_GETPRIO(mattr, &val);
printf("T5B|%s|set=%d|get=%d|val=%d\n", id, rc, grc, val);
fflush(stdout);
}
}
static uint64_t rwattr[8];
if (RW_INIT && RW_GETPSH) {
step("rw-init");
memset(rwattr, 0xAA, sizeof(rwattr));
errno = 0;
int rc = RW_INIT(rwattr);
printf("T5B|rw-init|rc=%d\n", rc);
fflush(stdout);
}
if (RW_SETPSH && RW_GETPSH) {
static const int psh[] = {0, 1, 2, -1};
for (unsigned i = 0; i < sizeof(psh) / sizeof(psh[0]); ++i) {
char id[32];
snprintf(id, sizeof(id), "rw-psh-%d", psh[i]);
step(id);
errno = 0;
int rc = RW_SETPSH(rwattr, psh[i]);
int val = -777;
int grc = RW_GETPSH(rwattr, &val);
printf("T5B|%s|set=%d|get=%d|val=%d\n", id, rc, grc, val);
fflush(stdout);
}
}
if (MA_SETTYPE) {
step("null-settype");
errno = 0;
printf("T5B|null-settype|rc=0x%x|errno=%d\n", (unsigned)MA_SETTYPE(0, 0), errno);
fflush(stdout);
}
if (MA_DESTROY) {
step("ma-destroy");
errno = 0;
printf("T5B|ma-destroy|rc=%d\n", MA_DESTROY(mattr));
fflush(stdout);
}
printf("T5B|DONE\n");
fflush(stdout);
return 0;
} |
8caf46d to
36096e4
Compare
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
Nothing blocks this change; the inspected forwarding and error mapping are consistent with the existing kernel locking APIs.
What I ran
Reviewed head 36096e40a7515c98d039c5a3f5120c98b491adcf. No commands or tests were run. Read core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp, core/libs/prx/libkernel/DirectMemory/Export.cpp, core/libs/prx/libkernel/include/Api.hpp, core/libs/tests/GuestMlock.cpp, and the prepared review/description.md, review/commits.md, review/manifest.json, and review/diff.patch. core/libs/tests/GuestMlock.cpp and debt documentation are listed as excluded by review rule; I read the test file only for context, not as a changed file.
Results
| Check | Result | Note |
|---|---|---|
| Changed code review | passed | Single changed file inspected |
| Tests | not run | Author reports server and console checks |
| Error mapping | passed | Kernel invalid maps to EINVAL; other returned errors map to ENOMEM |
Findings
Blocking
None.
Non-blocking
None.
Not verified
No checks were run in this review. The author's console measurements and server test results were not independently verified. core/libs/tests/GuestMlock.cpp and the change debt note were excluded from this review by rule, although the test source was read for context. No other files were marked as reviewed in the prepared manifest.
To get this to a good state
Nothing is required.
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 36096e40a7:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_mlock_testsbuilt cleanly. - CTest (
guest_mlock):1/1 Test #5933: guest_mlock ...................... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 - Negative control: with
core/libs/prx/libkernel/DirectMemory/PosixMemory.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #5933: guest_mlock ......................Subprocess aborted***Exception: 0.91 sec 0% tests passed, 1 tests failed out of 1 - Code reading:
- Verified mlock_nid_postfix and munlock_nid_postfix correctly forward to sceKernelMlock_nid_postfix and sceKernelMunlock_nid_postfix with const_cast<void*>(address) and length
- Verified ForwardPosixLock correctly checks result == 0 returning 0, and maps KernelInvalid (0x80020016 / SCE_KERNEL_ERROR_EINVAL) to GuestInvalid (EINVAL = 22) and all other errors to GuestNoMemory (ENOMEM = 12) per POSIX standards
- Verified set error errno propagation matches FreeBSD/guest errno calling *__error_nid_postfix() = error
- Verified docs/dev/debt/changes/fix-libkernel-posix-mlock-locks-mapped-pages.md accurately documents the change and debt fragment without rule violations
- Verified core/libs/tests/GuestMlock.cpp exercises mlock and munlock on mapped memory, 0-length ranges, unmapped memory (ENOMEM), and overflow/invalid addresses (EINVAL)
Looks Good To Me
|
Follow-up measurement for this PR's debt entry, same console (PS5 Pro, The console validates the flags ( |
|
@olegon85 Windows check on head
I did not run the rest of the libkernel suite. AI-assisted: yes |
|
Thank you - Windows verification with a negative control on top of console + Linux is exactly the coverage these posix-forwarding changes needed. Recorded; nothing to change on our side. |
|
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) |
|
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.
|
What
mlockandmunlockfailed withEPERMbefore looking at their arguments, on a FreeBSD 11.4 jail reading agreed in #11216 and explicitly recorded as never measured on a console. A payload process on a PS5 Pro (firmware 13.60) locked a mapped page throughmlockwith return 0 and unlocked it the same way, so the console does not deny it; the jail model does not hold. Both now forward tosceKernelMlock/sceKernelMunlock, which already lock and unlock host pages, mapping the kernel error codes toEINVALorENOMEMas the posix layer expects (length 0 succeeds, unmapped ranges giveENOMEM, an overflowing rangeEINVAL).mlockallandmunlockallkeep theEPERMstub - their console behavior was not measured.The same session measured the pthread attribute family (type table 1..4 with default 1 confirmed against our implementation;
Getkind, prioceiling and rwlock pshared answers diverge from our wrappers) - those findings feed follow-up work and are quoted in full in the first comment, together with thescePthreadMutexattrInit(NULL)console fault that ended an earlier run.Probe sources and raw session output are in the first comment. Measurement context: PS5 Pro,
kernel_get_fw_version=0x13600007, payload process over elfldr (port 9021), sessions 2026-11-20T23:35Z and 23:39Z UTC; a title process was not measured.Tested
tw5/tw5b;mlockandmunlockon a mapped page answered rc 0 both times.guest_mlock_testspassed with the change on01ac7c81be, including the new success pins on a fresh mapping and theENOMEM/EINVALfailure pins; the pre-existingsceKernelMlockfamily pins are untouched.python3 tools/check_conventions.py --base origin/main: passed.git diff --check: clean.python3 tools/pr_overlap.py --branch fix/libkernel-posix-mlock-locks-mapped-pages: failed closed on 23 base-conflicting PRs sharing these files; each patch was grepped and only feat(libs): mirror libkernel exports through libkernel_sys #18190 touches the two functions - a mechanical_nid_no_patchrename keeping the stub body, so whichever merges later adapts it to the other. No open PR changes this behavior.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)