fix(libc): scan /proc/self/maps without a new descriptor or allocation #28045
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/28045"
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 Plyxen on 2026-11-28T07:40:09Z.
What
On a Linux kernel without
PROCMAP_QUERY(before 6.11, which includes Ubuntu 24.04's stock 6.8 kernel), three tests fail onmainc6331cc61b, because the/proc/self/mapsfallback thatHostMapping::FindandWalkuse there opens a new descriptor and allocates on every pointer check.guest_memoryaborts:This is the error in the Linux runs posted on #28006 and #28013, and
mainfails the same way.CheckFlexibleMemorySharedBacking(from #23613) setsRLIMIT_NOFILEto the lowest free descriptor and then callssceKernelMapFlexibleMemory, which checks its output pointer withGuestWritable. WithPROCMAP_QUERY, that check reuses the descriptorQueryFdopened earlier. The fallback needs a new one, so it throws.guest_json_copy_failureandguest_json_float_failurecrash with SIGSEGV:Both tests replace
operator newwith one that throwsstd::bad_allocafter a budget runs out. The fallback scan allocated for every line it read (std::ifstream,std::getline, and a path string for each parsed line), so the pointer checks themselves used up the budget.CI's runners have the ioctl, so CI never takes the fallback in these tests.
This change:
/proc/self/mapsonce (MapsFd) and shares the descriptor with thePROCMAP_QUERYpath;preadfrom offset 0 into a static 16 KiB buffer for each scan (ScanLines). A lock keeps concurrent scans on the shared descriptor and buffer sequential, so each one reads whole lines as a private descriptor does. A line longer than the buffer throws;Mapping, path included, only for the lines a caller keeps.Findtherefore allocates only for its result, as thePROCMAP_QUERYpath already does;Walk, collects the matching mappings under the lock and calls the visitor after releasing it, so a visitor that looks up a mapping itself cannot deadlock;mainhas since addedScanProtection(c55bfa776f), the fallbackGuestWritabletakes throughFindProtection, and it opened/proc/self/mapson every probe. After mergingmain,guest_memory_maps_scanaborted with the error above again, soScanProtectionnow parses lines fromScanLinesand keeps only the end address and the write bit;guest_memory_maps_scan,guest_json_copy_failure_maps_scanandguest_json_float_failure_maps_scan. They run the same executables withAPS5_NO_PROCMAP_QUERY=1, asguest_sysmodule_maps_scandoes forguest_sysmodule_tests.If the first open fails,
MapsFdtries again on the next call instead of keeping the failure.The cached descriptor is tied to the process that opened it.
/proc/self/mapsis bound to its opener: the kernel takes that task'smmatopen. So a forked child that reuses an inherited descriptor reads the parent's mappings. OnmainthePROCMAP_QUERYpath already does this, becauseQueryFdcaches its descriptor. The old fallback opened a fresh file in the child, and sharingMapsFdwould have carried the problem into it. Ducengarth found this in review.MapsFdkeeps the opener's pid next to the descriptor in one atomic and opens the file again whengetpid()differs.QueryFdcaches only whether the ioctl works and takes the descriptor fromMapsFdon each call. The child then closes the inherited descriptor, but only if/proc/self/fdshows that its number still names the opener's maps file, since the child may have reused the number by then. The previous head left it open, so each generation of forked processes kept its ancestors' descriptors; ligmaSec found this.guest_memorygains Ducengarth's check (CheckHostMappingInForkedChild): a forked child maps a page and unmaps an inherited one, and must see both throughGuestWritable. It also counts the open maps descriptors before and after a lookup in three generations of children, and checks that a child that put another file on the number keeps that file.The scan lock is new in this PR. If a thread forks while another thread is scanning, the child starts with the lock held and no thread to release it, so its first fallback lookup blocks. MarcZX10 and CodeRabbit found this.
ScanLocknow registers apthread_atforkprotocol the first time it is used:forktakes the lock before copying the process, so it waits for a running scan to finish, and releases it in both the parent and the child.guest_memorygainsCheckHostMappingForkDuringScan: a thread stops inside a scan, the main thread forks, and the child must finish aScanMapslookup within 10 s. The parent must still be able to scan afterwards.Still slow under the fallback, unchanged by this PR:
guest_libressl3_object_headermakes about 63,000 checkedASN1_get_objectcalls, and each pointer check scans the maps file from the start. It takes 24.4 s with this change and 26.4-26.8 s onmain, against its 20 s timeout and 0.22 s with the ioctl. I left that for a separate change.Tested
WSL 2 Ubuntu 24.04 (kernel 6.18), GCC 13.3, Ninja, Release, on
mainc6331cc61b. That kernel hasPROCMAP_QUERY, soAPS5_NO_PROCMAP_QUERY=1selects the fallback.With
APS5_NO_PROCMAP_QUERY=1, 3 runs each, after rebuilding the test executables:mainguest_memoryguest_json_copy_failureguest_json_float_failureguest_memory_maps_scan,guest_json_*_failure_maps_scanguest_libressl3_object_headerThe fork check, with
guest_memorybuilt on each tree, 3 runs each:guest_memoryguest_memorywithAPS5_NO_PROCMAP_QUERY=1mainc6331cc61b+ the checkwaitpidcheckfailed to open /proc/self/maps7747932a22(the previous head) + the checkwaitpidcheckwaitpidcheckThe descriptor checks, with
guest_memorybuilt on each tree, onmain35e34b1a7f0with this change (together with #28733), with and withoutAPS5_NO_PROCMAP_QUERY=1:bfe151217e(the previous head) + the checksguest_json_copy_failure,guest_json_float_failureand their_maps_scantests andguest_sysmodule,guest_sysmodule_maps_scanandguest_sysmodule-invalid-idspass there too.The fork-during-scan check, on
main35e34b1a7f0with this change, after rebuildingguest_memory_tests:guest_memoryguest_memory_maps_scanf272eb99db'sScanLock)guest_json_copy_failure,guest_json_float_failureand their_maps_scantests pass there too. I did not rerun the whole suite for this commit or the one before it.Before those two commits I ran the whole suite (7,274 tests,
-j 8) with this change, once normally and once withAPS5_NO_PROCMAP_QUERY=1:agc_driver_flat_aperture_lds_bounds,gcn_multisample_color_fill,image_d16_load,image_filter_mode_three,image_sample_lod_clamp_shader_laneandupload_cache_read_only_flush. They pass on an RTX 5080 and a Radeon iGPU on Windows, and this PR changes nothing in AGC.guest_libressl3_object_headertimes out, as described above.The runs on
7747932a22also had threeguest_savedata_backup_*timeouts while a Windows build loaded the machine; each passes alone. They also had oneguest_pthread_released_handleabort, which fails onmaintoo and is fixed by #28064.No other test failed. That includes the 92 tests that use
HostMappingor the maps fallback, among themguest_sysmodule_maps_scan,agc_driver_graphics_maps_fallbackand the frame capture and replay tests.The header and the test registrations are Linux only, so the Windows build is unchanged.
python3 tools/pr_overlap.py --names QueryFd ScanMaps Walkreports no open PR changing them. They are not exports, so I also searched the open PRs that mentionHostMapping,/proc/self/mapsorPROCMAP_QUERY(#14473, #26069, #28013 and others): none changesHostMapping.hpp.python3 tools/pr_overlap.py --branch fix/libc-linux-maps-scan-without-new-fdmerged the 59 open PRs that share a file into the base, including those that sharelibSceJson2/CMakeLists.txt. It could not compare 27 of them, because they conflict withmain. All 27 share onlycore/libs/prx/libkernel/CMakeLists.txt, so I read their hunks in that file. Only #16372 and #18293 touch theguest_memoryregistration: they add a Windowsguest_memory_armedtest in theif(WIN32)block below it. Neither changes the lines this PR adds afteradd_test(NAME guest_memory ...).After merging
main484b0368d3(d2bff93810), on WSL 2 Ubuntu 24.04 with the Release and ASan+UBSan builds, 3 runs each, withguest_memory,guest_memory_maps_scan,guest_json_copy_failure,guest_json_float_failure, their_maps_scantests,guest_mmap_fixed_anonymousandguest_mmap_executable:main484b0368d3,guest_memorywithAPS5_NO_PROCMAP_QUERY=1GuestWritable: failed to open /proc/self/mapsd2bff93810guest_memory_maps_scanaborts the same wayguest_memory_maps_scanaborts the same wayChecklist
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T08:08:02Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the described fallback scan and Linux test registration are consistent with the inspected code.
What I ran
Reviewed head
b214ffccdbcb12bed143a2ee981fc633a80d0a8e. I read the prepared description, commit list, manifest and diff, and used the pinned reader to inspect both changed files and the relevantGuestMemory.cppcallers. No tests or build commands were run; reported test results in the description are author supplied.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Runtime behavior, build results, and the author-reported test runs were not independently verified. No changed files were excluded;
files.jsonwas unavailable in the prepared review folder. Read files:core/libs/prx/libc/include/specifics/linux/HostMapping.hpp,core/libs/prx/libkernel/CMakeLists.txt,core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp,review/description.md,review/commits.md,review/manifest.json,review/diff.patch, andreview/README.md.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T09:45:13Z.
Review: COMMENTED
Reviewed — changes requested
Changes are required before merge: preserve the maps-scan fallback when
PROCMAP_QUERYis available but its ioctl stops working.What I ran
Reviewed head
7747932a222b4da004ee46dc867a0382b777d204usingrepo_read.pyaccess to the prepared diff and source. No build or tests were run.Results
Findings
Blocking
QueryFd()now returns the cached maps descriptor whenever its one-time probe succeeds. If a laterPROCMAP_QUERYioctl fails for a reason other thanENOENT,Find()andWalk()throw immediately rather than using the/proc/self/mapsscanner. The priorFind()andWalk()behavior also threw on ioctl failure, but this change introduces a shared descriptor and selects the ioctl path based on a static probe, while the fallback claim is specifically for kernels without the ioctl. This is a concrete robustness issue only if the ioctl becomes unavailable after probing; that condition is not established by the reviewed code, so it does not warrant a blocking finding.Non-blocking
Not verified
review/files.jsonwas unavailable through the reader, so excluded-file details and the complete reviewed-file inventory could not be confirmed. The manifest lists three changed files, all marked included; all three full files were read.PROCMAP_QUERY, and Windows behavior, were not run.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T17:06:24Z.
Review: APPROVED
Reviewed — looks good
Nothing in the inspected changes blocks merge; the fallback scanner, fork handling, and Linux test registrations are consistent with the stated goal.
What I ran
Read the prepared diff and pinned-head changed files. No commands or tests were run in this read-only review.
Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Tests and builds were not run. Author-reported test results were not independently verified. Read:
core/libs/prx/libSceJson2/CMakeLists.txt,core/libs/prx/libc/include/specifics/linux/HostMapping.hpp,core/libs/prx/libkernel/CMakeLists.txt, andcore/libs/tests/GuestMemory.cpp. No files were excluded from review; all four are listed as reviewed in the manifest.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T07:43:40Z.
Review: COMMENTED
The Linux
guest_memoryfixture passed. A standalone check of the patchedHostMappingfallback found an inherited file-descriptor leak across forks. The inline finding includes the measurements onmainand on this patch.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-03T18:37:49Z.
Review: COMMENTED
Actionable comments
36c816e0-ee09-4d8c-ac95-6df98bf34d62Reviewing files that changed from the base of the PR and between
bfe151217eandf272eb99db.core/libs/prx/libc/include/specifics/linux/HostMapping.hppcore/libs/tests/GuestMemory.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:58:09Z.
Review: COMMENTED
The exact
guest_memory_maps_scantest passed on Linux withAPS5_NO_PROCMAP_QUERY=1atf272eb99db. It now checks both cases from the fork issue I reported: the child closes the inherited parent maps descriptor, and a descriptor number reused by the child survives. This run exercised the forced fallback through the integrated guest-memory fixture.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-03T19:02:33Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: reset or otherwise make
ScanLocksafe in a forked child when another thread held it atfork().What I ran
Reviewed head
f272eb99db186f16c15edba20a1cc6936e2657be. Read the prepared description, commits, diff, manifest, and all four changed files listed as reviewed. Ran the reader search forHostMapping::Walk; a follow-up reader search could not run because the reader could not write its log in this read-only workspace. No tests or builds were run.Results
Negative control
Not run; no tests were executed.
Findings
Blocking
core/libs/prx/libc/include/specifics/linux/HostMapping.hpp:111acquires a process-localstd::mutexfor the duration of a scan. If another thread holds this mutex when the process forks, the child inherits the locked mutex without the owning thread; a child that calls the fallback scan can block forever onScanLock(). This is especially relevant to this change because it explicitly supports fallback lookups in forked children. Reinitialize or otherwise make the scan lock safe in the child, and add coverage for forking while a scan is in progress.Non-blocking
None.
Sources
Not verified
The callers returned by the reader search were not inspected. No tests, builds, or runtime behavior were verified. All four changed files listed under
reviewedinfiles.jsonwere read;files.jsonlists no excluded files.To get this to a good state
Make the scan lock usable by a forked child even when another thread held it at fork, and add a regression check for that case.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-04T04:36:55Z.
Review: COMMENTED
Actionable comments
b179e5c9-9f1a-4d93-b30a-4d6a1e636301Reviewing files that changed from the base of the PR and between
f272eb99dbandfa559833df.core/libs/prx/libc/include/specifics/linux/HostMapping.hppcore/libs/tests/GuestMemory.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T04:50:31Z.
Review: APPROVED
Reviewed — looks good
The reviewed changes address the fallback scan’s descriptor and allocation issues, and add Linux coverage for fallback and fork behavior.
What I ran
Reviewed head
fa559833df70a6a8a0da984a30fa1545d39c23e4using the prepared description, diff, manifest, andrepo_read.pyat the pinned head. No tests or builds were run.Results
Negative control
Not run; no tests were executed.
Findings
Blocking
None.
Non-blocking
None.
Sources
The scan, descriptor handling, and fork coordination are in HostMapping.hpp at the reviewed head. Linux fallback test registrations are in libkernel CMakeLists.txt and libSceJson2 CMakeLists.txt; fork checks are in GuestMemory.cpp.
Not verified
Runtime tests, builds, and behavior on other kernels or hardware were not independently verified. Read the changed ranges and relevant context in
core/libs/prx/libSceJson2/CMakeLists.txt,core/libs/prx/libc/include/specifics/linux/HostMapping.hpp,core/libs/prx/libkernel/CMakeLists.txt, andcore/libs/tests/GuestMemory.cpp.files.jsonlists no excluded files.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T10:33:22Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
4a90b99a7e4ed986f85704f16286f6dd15980026usingrepo_read.pyand the prepared diff. No tests or builds were run.Read all changed lines in the four reviewed files.
files.jsonlists no excluded files.Results
Negative control
Not run; no tests were executed.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
Runtime behavior and test results were not independently verified. The reported test runs in the PR description are author-provided.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T11:35:18Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the review points below are optional.
What I ran
Reviewed head
e9c28df1d7f7b28b0030c9b5c07050a1f3bbfc10. Usedrepo_read.pyto search pinned callers and readHostMapping.hpp, relevantGeneral.cppandFrameRecorder.cppcallers, and the changedGuestMemory.cpptest sections. Inspected the prepared diff and changed CMake sections. No tests or builds were run.Results
Findings
Blocking
None.
Non-blocking
None.
Sources
HostMapping.hppat the reviewed head — cached maps descriptor, serialized fallback scan, and line parsing.GuestMemory.cppat the reviewed head — fork descriptor and fork-during-scan coverage.Not verified
No runtime behavior or tests were verified. I read all of
HostMapping.hppand the relevant changed sections of the other three reviewed files; I did not read the fullGuestMemory.cppor full CMake files.files.jsonlists four reviewed files and no excluded files.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T13:33:51Z.
Review: APPROVED
Reviewed — looks good
The change at head
d2bff938107c420c47493e06f946c4dd13ee9e4dreuses and safely refreshes the maps descriptor, serializes fallback scans across forks, and adds Linux fallback coverage; I found no blocking defect in the reviewed code.What I ran
Reviewed the prepared diff and changed code at the exact head above. No tests or builds were run.
Also read the prepared diff, description, commit list, manifest, and changed CMake sections. Reviewed all 4 files listed under
reviewed;excludedis empty.Results
Negative control
Not run; no tests were run during this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
I did not run the test suite, build, or independently reproduce the author-reported results. No files were excluded by the supplied review manifest.
To get this to a good state
Nothing is required.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.