fix(libkernel): validate and roll back DirectMemory pool operations #3469
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3469"
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 yash27git on 2026-10-11T11:04:22Z.
What
Hardens
PhysicalMemoryPoolincore/libs/prx/libkernel/DirectMemory/MemoryPool.cpp:start/lencan no longer reach_used[].Alloceffectively did before.Alloc,Free/CheckedFreeandRetyperoll back the pool's own metadata (_blocksand_used[]) when node allocation or backing creation throws. Blocks are split in place (_splitAt/_unsplitAtplus a rangeerase) and split first, so every step that can throw runs before any state is committed.DirectMemoryLargestFree, now validated.Free;CheckedFreestill requires every page in range to be allocated;Retypechanges allocated pages and skips gaps.DirectMemoryAlloccatchesstd::bad_alloc(toSCE_KERNEL_ERROR_ENOMEM). Every other entry point lets exceptions propagate. A mismatch between_blocksand_used[]throwsstd::logic_errorinstead of returning a plausible error code.MemoryPoolTest, registered in ctest, and updates theTechnicalDebt.mdentry for MemoryPool.Why
Free,Retypeand the page-marking helpers did no range checks, andcur + len,start + align - 1andstart + lencould overflow, so guest input could write past_used[]or create overlapping allocations.Alloc,FreeandRetypecould throw part-way (bad_alloc, backing cleanup) and leave_used[]and_blocksout of sync.false,0orEFAULT, which hides emulator invariant failures.Behaviour changes
EINVAL/EFAULTstartinFree/CheckedFree/RetypeFreeon a range with nothing allocatedForgetDirectMemoryFree/Retypepropagated, others swallowedbad_allocinAlloc(toENOMEM)LargestFreewith negativesearchEndKnown limitation
The rollback covers the pool's own metadata only.
ForgetDirectMemoryerasesg_physPagesbeforeErasePhysMappings, andErasePhysMappingscan throw while inserting the surviving pieces of a partially overlapped mapping. In that case the pool restores its splits, but the already-erased physical pages and mapping nodes are not restored, so a retry may failValidatePhysicalRange. Making those updates transactional is tracked inTechnicalDebt.mdand is not part of this PR.Tested
New tests (
MemoryPoolTest): 14 cases covering argument validation, rounding/alignment/clamping, partial and spanningFree,Retypesplitting, and rollback of_blocks/_used[]after injectedbad_allocand backing/forget failures. Run per case against the previousMemoryPool.cpp, 9 fail or crash; all 14 pass after the change (ASan + UBSan, C++11 and C++17, mockForgetDirectMemoryandCreateDirectMemoryBacking).Environment: Windows 11 Home (26100), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release.
Full suite: 550 of 554 tests passed, 1 skipped.
agc_driver_prepared_shaders_debug_failurefails on cleanmainon this machine.guest_module_identityfailed once but passed when rerun alone.CTestTestfile.cmakecommands because local antivirus blocksctest.exe.tools/check_conventions.py: Passed with 0 warnings.Checklist
main(rebased, no merge commits); no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T11:39:14Z.
Review: CHANGES_REQUESTED
Thanks for the work. Blocking points:
core/libs/tests/MemoryPoolTest.cppis not registered anywhere: noadd_test_executable/add_testincore/libs/CMakeLists.txtorcore/libs/prx/libkernel/CMakeLists.txtmentions it, so it never runs locally or in CI (the PR body says "registered in ctest"). Please register it with an explicitadd_testand make sure it fails against the oldMemoryPool.cppthrough ctest. Note it definesCreateDirectMemoryBacking,ForgetDirectMemoryand the globaloperator new, so it must compileMemoryPool.cppdirectly rather than linklibkernel.### Testedsection still has template placeholders (<passed / total, skipped; ...>,<result>fortools/check_conventions.py). Fill them with the real results on the final head.docs/dev/TechnicalDebt.md:591: the new line is the last of the "Functional" section, where every PR appends and conflicts. Put it a few lines away from the end.sys/kern/man pages, KytyPS5, shadPS4):DirectMemoryFree/DirectMemoryRetypenow silently ignore invalid ranges (MemoryPool.cpp:306,321) instead of failing visibly, and a negativesearchEndnow means unbounded inLargestFree. Say which reference each follows, or record it under "Unknown function info" in TechnicalDebt.Happy to re-review once these are addressed.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T11:52:20Z.
Review: CHANGES_REQUESTED
Thanks for the update. The points from the previous review are still open on the current head (the new commits are merges from
mainplus a docs change):core/libs/tests/MemoryPoolTest.cppis still not registered: noadd_test_executable/add_testmentions it incore/libs/CMakeLists.txtorcore/libs/prx/libkernel/CMakeLists.txt, so it never runs locally or in CI. It definesCreateDirectMemoryBacking,ForgetDirectMemoryand the globaloperator new, so compileMemoryPool.cppdirectly into its own target instead of linkinglibkernel, and check through ctest that it fails against the oldMemoryPool.cpp.### Testedstill contains the template placeholders (<passed / total, skipped; ...>,<list any other failures...>,<result>fortools/check_conventions.py). Fill them with the real results on the final head.docs/dev/TechnicalDebt.md:597-598: the entry is still the last line of "Functional" (everyone appends there, so it conflicts), and the last commit added a second line starting with- - [MemoryPool]that is malformed and repeats the first. Keep one line, a few lines away from the end of the section.DirectMemoryFree/DirectMemoryRetypenow silently ignore invalid ranges, and a negativesearchEndinLargestFreemeans unbounded. Say which reference each follows (FreeBSDsys/vm, KytyPS5, shadPS4) or record it under "Unknown function info" indocs/dev/TechnicalDebt.md.Happy to re-review once these are addressed.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T12:01:02Z.
Review: CHANGES_REQUESTED
The current head breaks CMake configure for the whole tree, so nothing can be built or tested:
core/libs/CMakeLists.txt:332-339: the block you added ends with an extraendif(), and cmake stops withFlow control statements are not properly nested(line 339). Drop the extraendif()and put the block inside the existingif(BUILD_TESTING), e.g. next to the otheradd_test_executablecalls (indented like them, no blank line beforeendif()). Then check withctest -NthatMemoryPoolTestis listed and withctest -R MemoryPoolTestthat it fails against the oldMemoryPool.cpp.### Testedstill has the template placeholders (<passed / total, skipped; ...>,<list any other failures ...>,<result>). Fill them with the real results on the final head.docs/dev/TechnicalDebt.md:600-601: there are two lines, the second starting with- - [MemoryPool](malformed). Keep one, and not as the last line of "Functional" (everyone appends there and it conflicts).DirectMemoryFree/DirectMemoryRetypenow ignore invalid ranges, and a negativesearchEndinLargestFreemeans unbounded. Say which reference each follows (FreeBSDsys/vm, KytyPS5, shadPS4) or record it under "Unknown function info".conventionsjob is red; runpython3 tools/check_conventions.pyand fix what belongs to your commits.Happy to re-review once the build configures again.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T12:49:25Z.
Review: CHANGES_REQUESTED
Configure still fails on the current head (checked on the PR merged onto
main):core/libs/CMakeLists.txt:323-330and:334-341: theguest_posix_random_tests/guest_posix_randomblock is duplicated, so CMake stops withadd_executable cannot create target "guest_posix_random_tests" because another target with the same name already existsandadd_test given test NAME "guest_posix_random" which already exists. Remove the second copy and keep only theMemoryPoolTestaddition (the blank line with trailing spaces before it too)..gitmodules: the PR adds a3rdparty/imguientry thatmaindoesn't have. Drop it, it is unrelated to this change.docs/dev/TechnicalDebt.md:595: the line starts with- -. The entry at line 100 ("silently ignore invalid ranges") is no longer true, since invalid ranges now returnEINVAL/EFAULT; keep one accurate entry.Once it configures, please check with
ctest -NthatMemoryPoolTestis listed, and withctest -R MemoryPoolTestagainst the oldMemoryPool.cppthat it fails. Rebase onmaininstead of merging it into the branch (no merge commits).Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T14:28:36Z.
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)docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599core/libs/CMakeLists.txtL331docs/dev/TechnicalDebt.mdL593docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL593docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL593docs/dev/TechnicalDebt.mdL593docs/dev/TechnicalDebt.mdL599docs/dev/TechnicalDebt.mdL599Pull request closed