feat(libkernel): implement the JIT shared memory calls #3787
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3787"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by YossiYad on 2026-10-11T16:42:35Z.
What
Carries screamm's
screamm/AnyPS5:feat/libkernel-jit-shared-memorycommit from #3736 unchanged onto currentmain, preserving its author, and adds one follow-up test commit:sceKernelJitCreateSharedMemory,sceKernelJitCreateAliasOfSharedMemoryandsceKernelJitMapSharedMemory. Create backs the object with a host anonymous file (memfdon Linux, a delete-on-close temporary file section on Windows) and returns a descriptor; Alias duplicates it with its own maximum protection; Map maps the whole object through the direct-memory placement path, so a write through one view is visible through every other.close,sceKernelClose,dupanddup2keep the JIT descriptor table in step. VLC (PPSA85300), XPSemu (PPSA97358) and RetroArch import these calls, as reported in #3736; their sources fall back on a non-zero return, so the errors are returned rather than thrown.guest_jit_shared_memorywithdup2: a JIT descriptor moved onto another descriptor maps the same object with the source's maximum protection,dup2onto itself keeps it, and an ordinary descriptor moved onto a JIT one is no longer treated as JIT. The original test does not calldup2: with the Linuxdup2no longer copying the entry, orDuplicateJitSharedMemoryno longer forgetting the target, the original test still passes and the extended one fails.The library progress moves from 2903/3383 (85.81%) to 2906/3386 (85.82%). The total rises by three because none of the calls was exported before. Choices no source settles are recorded in TechnicalDebt.
Closes #3736.
Tested
Linux (Bazzite 44, GCC 16.2.1, Ninja, Release,
-DBUILD_TESTING=ON), based onmaina11c8f7f:libs, and fullctest: 825 pass, 8 skip, no failures (833 registered tests). The GPUagc_driver_pixel_interlocktest failed on an earlier run and passed on the final full run; the changed library does not participate in that test.guest_jit_shared_memory,guest_shm_open,guest_memory,guest_dupandguest_kernel_fcntlpass 10 consecutive runs;guest_jit_shared_memoryruns every case here, including executing code through the RX and RWX views.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T16:44:25Z.
Progress report (
3e0e01f→3b7ac15)📈 System libraries: 86.38% (+0.02%, +3 implemented functions)
✅ 3 implemented
sceKernelJitCreateAliasOfSharedMemorysceKernelJitCreateSharedMemorysceKernelJitMapSharedMemoryImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:16:59Z.
Two retries after an error in
core/libs/prx/libkernel/DirectMemory/Jit.cpp, which aren't allowed here:CreateExecutableFilecallsmemfd_createwithMFD_EXECand calls it again without the flag onEINVAL. Pick one call up front.CreateBackingloops up to 64 times onERROR_FILE_EXISTS. Use a name that can't collide and throw ifCreateFileWfails.Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-12T06:56:44Z.
Thanks for the review. The two retries are removed in a fix-up commit on top of this PR's carried commit:
screamm:feat/libkernel-jit-shared-memory-v2, commiteb91497b7(fix(libkernel): create the JIT shared memory backing in one call, based on currentmain2ccbd376); @YossiYad, cherry-picking that one commit onto this branch should be enough (yourdup2test commit is untouched).memfd_createis called once withMFD_CLOEXEConly.MFD_EXECis dropped rather than retried: it only sets the file's execute permission bits (6.3+,EINVALon older kernels), while mapping the object executable depends on the mapping protection alone, the same as the direct memory memfd inDirectMemory.cpp; a compile-time#ifdef MFD_EXECwould reflect the headers, not the running kernel. Recorded in TechnicalDebt, including thatvm.memfd_noexec=2makes the call throw.CreateBackingno longer loops onERROR_FILE_EXISTS; the delete-on-close backing file is namedanyps5-jit-<pid>-<atomic counter>-<QueryPerformanceCounter>, soCREATE_NEWcannot meet an existing name, and anyCreateFileWfailure throwsstd::system_errorwith the Win32 error. The descriptor stays a CRT descriptor, so close/dup/dup2 behave as before.Tested on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2.0 r7, Release, full build including
libs:guest_jit_shared_memory,guest_shm_open,guest_memory,guest_dup,guest_kernel_fcntl3/3;ctest -j8 -E "^agc_driver_|^video_out_flip"594/594; conventions andgit diff --checkpass. The Linux side is not compiled here.Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-12T10:54:44Z.
A catalog title reaches these calls: PS5CEMU-HAR (PPSA99360). It is built with the ps5-payload-dev SDK, so it relinks only with
--trap-sdk-syscall-stubs(#3804, see #2136).mainit stops atavvJ3J0H0EY(sceKernelJitCreateSharedMemory). It also importsYKT49TOLQWs(sceKernelJitMapSharedMemory).maine7c70a2d(ea27cc46, clean), both resolve, and the next stop ispthread_setaffinity_npon Linux and Windows 11 (MinGW GCC 15.2):undefined symbol: 5KWrg7-ZqvE (pthread_setaffinity_np)FAIL: unresolved ELF import 5KWrg7-ZqvE (pthread_setaffinity_np) from libkernel.prxguest_jit_shared_memorypasses 5/5 (--repeat until-fail:5) on both, with no warnings in the PR's files.anyps5-jit-*temporary files remain in%TEMP%on Windows after the tests and the title run.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T18:30:10Z.
Review: COMMENTED
The two review points, and the Linux half of the fix-up commit
No PR of mine touches this path — this is a measurement, and nothing is requested.
Both of @oneandonlydean's points are addressed by @screamm's fix-up commit
eb91497b7(screamm:feat/libkernel-jit-shared-memory-v2). His comment on this PR says that commit's Linux side was not compiled there ("The Linux side is not compiled here"), and that is the half I built and ran below.The two asks, still open at the head
151626cc7The fix applies to this head unchanged
git cherry-pick -x eb91497b7on151626cc7gives81a9cae57with no conflicts (the only merge message isAuto-merging docs/dev/TechnicalDebt.md; its tree isc9eb40b9). Afterwards — a source reading, since Windows is not built on this box:MFD_EXECoccurs nowhere in that file afterwards (twice at the head), and the name cannot repeat within a process (pid plus a process-wide counter; the performance counter covers the cross-process case).<random>and<stdexcept>are dropped from the includes with norandom_deviceand noruntime_errorleft, so the Windows branch still has everything it uses (<system_error>,<atomic>,<filesystem>,<windows.h>).One inconsistency worth a look while this is open:
:115throws for anyCreateFileWfailure, while the section failure two lines below still routes the memory errors throughWindowsFailuretoSCE_KERNEL_ERROR_ENOMEM(:99-101, called at:121). That follows the review as written ("throw ifCreateFileWfails"), so it is an observation, not a request.Linux, both arms
CachyOS, GCC 16.2.1, CMake 4.4.3, Ninja, Release,
-DBUILD_TESTING=ON, kernel7.2.9-2-cachyos. Arm A is the head151626cc7, arm B is81a9cae57;Jit.cppis the only compiled difference (the fix also edits one TechnicalDebt sentence).The test maps an RX view (
core/libs/tests/GuestJitSharedMemory.cpp:67) and calls through it (:72), maps an RWX view (:90) and calls through it (:93), and repeats that through adupdescriptor (:97, called at:98) — so the Linux path the fix touches really ran code here. Both arms pass, so the test does not by itself separate the fix; the counts below do.The call counts, measured
An
LD_PRELOADinterposer onmemfd_create(logging the name and the flags), with the test binary rebuilt from each arm:Seven calls and four JIT objects in both arms. The object file says the same without running anything:
Jit.cpp.ocarries twomemfd_createrelocations at arm A (0x208c,0x20e1) and one at arm B (0x208c). So on Linux the fix removes a call site rather than a call — on this host there is no first error to retry, becausevm.memfd_noexecis 0 and the kernel acceptsMFD_EXEC, and the review point is about the retry the shape allows. On Windows the change is behavioural, as noted above; these counts say nothing about that platform.Is dropping
MFD_EXECsafe here? An independent probe, not the PR's codememfd_create+ftruncate(4096)+mmap(PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, fd, 0), then executingmov $42,%eax; retout of that mapping, on this kernel — the execute-through-mapping rows, verbatim:So on this kernel the flag is not what makes the object executable — the mapping protection is, which is what the commit message says — and it is not a new pattern either: the direct memory memfd beside it is created without
MFD_EXEC(DirectMemory.cpp:232,MFD_CLOEXEC | MFD_ALLOW_SEALING). What I could not check: there is no root here, sovm.memfd_noexeccannot be moved off 0, and the policy-2 refusal in the new debt sentence is screamm's statement rather than something I ran; and a pre-6.3 kernel, whereMFD_EXECis an unknown flag returningEINVAL— the case the removed retry existed for.Three smaller things
linuxandwindowsjobs are gated ongithub.event_name == 'push' || 'workflow_dispatch'withfull == 'true'(build.yml:193,:240), and the job a pull request does get configures-DANYPS5_RELINKER_ONLY=ON(build.yml:160), after whichCMakeLists.txt:90-93returns beforeadd_subdirectory(core/libs)at:153—guest_jit_shared_memoryis not even registered in that build. Every execution of it is a local one.maincleanly:git merge-tree --write-tree pr-3787 upstream/mainreports conflicts incore/libs/prx/libkernel/File/src/Open.cppandFile/src/Stdio.cpp(main was4a49cda98when I ran it, having moved twice earlier today), so a rebase is due — and the fix applies to this head with no conflicts.sceKernelClosekeeps the descriptor table in step, andOpen.cpp:227-229does hook it, but the test closes only throughclose_nid_postfix, so that path has no case.Not checked here: Windows compile and behaviour (screamm's and @babyhuey's Windows runs on the thread stand), and the full suite — I built the JIT target and the four neighbours, not all of the registered tests. The title half of the bar is already on the thread (@babyhuey's PS5CEMU-HAR run above).
AI-assisted: yes.
Hermes Agent 🤖 at your service
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-12T20:02:08Z.
Rebased onto current
main(the TechnicalDebt entry now lives indocs/dev/debt/libkernel.md) and added @screamm's fix-up for @oneandonlydean's two review points, cherry-picked with its authorship:memfd_createis called once withMFD_CLOEXEC(noMFD_EXECretry), and the Windows backing name is unique per process/counter/performance counter withCreateFileWfailure thrown instead of looping onERROR_FILE_EXISTS. TechnicalDebt records the droppedMFD_EXEC.Checked on Linux (GCC, Release):
guest_jit_shared_memory,guest_shm_open,guest_dupandguest_kernel_fcntlpass 5 consecutive runs, including the RX/RWX execution cases. The Windows part of the fix-up was reviewed but not compiled here. Merging once CI is green.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T20:06:46Z.
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.
main)core/libs/prx/libkernel/File/src/Stdio.cppL586docs/dev/debt/libkernel.mdL93core/libs/prx/libkernel/DirectMemory/DirectMemory.hppL40