fix(libc): validate guest heap block headers before freeing #11468
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11468"
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 valenciana-nc on 2026-10-26T15:15:22Z.
What
Authenticate guest-writable heap headers against trusted host-owned records of the exact user pointer, raw block start and allocator-selected size. Free validates before removing registration, changing usage or mutating free lists; realloc/realign validate before replacement allocation, including zero-size requests. This rejects the reviewed in-arena interior-pointer forgery and registered non-heap memory.
Addressed the shutdown review:
blocks()now has process lifetime, matchingGuestAllocations::registry(). It is deliberately not destroyed while guest threads may still allocate/free. Addedguest_heap_lifetimeunder UNIX: a child registers an exit callback before first allocation; the callback starts a worker during static finalization, checks a retained small allocation, performs 256 allocation/free pairs, then frees the retained block. The parent verifies callback success and normal child exit. This targets the new map's lifetime, not the pre-existing large-block cache lifetime gap.Tested
Pinned main:
3ae8209b643be054ef09542f9e9e5e951b1ed73b. Tested head:69667d35df0329efbbab3bc9e99c9d4e2d2519b1. Windows 11 x86-64 build 26300; Intel i7-13650HX; NVIDIA GeForce RTX 4060 Laptop GPU / NVIDIA 610.62; WinLibs GCC 15.2.0 r7; Release,BUILD_TESTING=ON.agc_driver_graphics.-std=c++20 -pthread: the exact committed lifetime fixture and GuestHeap/GuestAllocations/GuestArena/GuestWriteWatch sources pass optimized and AddressSanitizer builds; the optimized fixed case also passes 20/20 repeats.ASAN_OPTIONS=detect_leaks=0excludes intentionally process-lived state; no leak-clean claim.heap-negative-tests.log, not a freshly repeated negative control on this base.Requested allocation-heavy microbenchmark
Same Linux machine/compiler,
-std=c++20 -O2 -pthread, main versus the final fixed allocator; no overlapping source build during timed execution. Warmed single-thread workloads: 32-byte allocate/write/free churn and batches of 1,024 simultaneous 32/96/256-byte allocations. Each timed sample is 524,288 allocation/free pairs; 21 samples per variant/workload, ordered main/fixed/fixed/main/main/fixed across processes. Wall time per pair, pooled sample medians:Instrumentation of warmed ordinary C++
operator newcalls sees 8 to 9 calls per guest allocation/free pair, and 384 to 416 requested host bytes: one extra 32-byte map node per live allocation on this libstdc++ implementation. This is requested allocation traffic, not RSS or a cross-platform node-size guarantee; startup/bucket growth is excluded. Freed nodes are reclaimed, but the intentionally process-lived map retains its bucket capacity until process exit.Clocks/power/host load were uncontrolled and samples vary substantially; the table is a small synthetic overhead observation, not a statistically established game/frame/throughput result or a speedup claim. No title run, thread scaling, cold start or trace-digest performance comparison was made. Benchmark source, exact build/run script and raw samples are attached in the gist, not added to the repository.
Exact GPU environment on baseline and candidates:
The 12 common controls pass on main and every candidate: request serializer, shader memory, driver graphics, six draw-shader-failure modes, allocator failures and two application-heap modes. Actual
libsand affected test binaries rebuilt at each pinned head. Every GPU-bearing log confirms actual Khronos layer insertion, with no skips,VUID-, synchronization hazards, validation errors/warnings or missing-requested-layer diagnostics. This is not inferred from CTest's exit status alone.git diff --check, current-main conventions and the requiredpython tools/check_conventions.py --base origin/mainpass. The latter retains inherited debt-length warnings from the stale fork-main comparison. Hosted path-skipped product jobs are not runtime evidence. Exact commands, raw logs and source/binary hashes are in the evidence gist (resume-*files andresume-provenance.json); older prefixes describe older heads. AI-assisted.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)Local validation logs and scope.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T15:17:16Z.
Review: CHANGES_REQUESTED
I found a remaining allocator-integrity gap in the new validation on head
c7bfe92aa55b5ba3e3548d8b412f5d5b8b8c328c.The check proves that the guest-writable
rawandblockBytescover the registered user range and lie inside the reserved arena, but it does not prove thatrawis the actual block start returned byArenaHeapor that the block size matches that allocation.GuestArenaContainsonly checks the arena's overall address bounds. For a 32-byte allocation, the allocator requests 80 bytes and uses the 96-byte size class. If the header is changed toraw = pointer - 16andblockBytes = 96, the current preceding-header, coverage and arena checks all pass for an ordinary in-arena allocation.ArenaHeap::Freethen inserts that interior pointer into the 96-byte free list; the next 32-byte allocation can reuse an overlapping block and extend 16 bytes past the original class block into its neighbor.Please validate
raw/blockBytesagainst trusted host-side allocation metadata (or an equivalent allocator-owned block-boundary check) before removing the registered allocation or mutating the free list. Add a regression that forges an in-arena interiorrawwith a plausible valid class size, verifies free is rejected and the allocation remains registered, then proves a same-class allocation cannot receive the forged overlapping block. The existing zero-size corruption test does not exercise this case.The current diff passes
git diff --check, current-main conventions and a clean merge-tree preview. I did not independently run the AGC test suite; hosted relinker/registration jobs are still running, and AGC product jobs are path-skipped.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T07:57:48Z.
Review: CHANGES_REQUESTED
Re-review — changes requested
The two earlier code findings appear addressed on exact head
69667d35df0329efbbab3bc9e99c9d4e2d2519b1: the free/realloc validation now checks the guest header against host-owned block metadata, and the block map has process lifetime. The author reports an exit-time ASan regression/control, Windows controls, and a microbenchmark with its limitations documented. I found no new allocator-integrity defect in this re-review.Findings
[P2] Agree on the overlapping heap changes
This PR and open #12239 both change
core/libs/prx/libc/src/GuestHeap.cpp; a pairwise merge preview reports a content conflict. CONTRIBUTING requires the authors to agree on landing order and record the prerequisite on the later PR. Please coordinate the order with #12239's author and update the relevantDepends onchecklist line. The currentnoneentry does not record that conflict.[P2] Integrate current main and rerun the heap tests
Current
mainis4c7d275bef89d309313d16bc2bc05d37a1b50be2; this head is based on3ae8209b643be054ef09542f9e9e5e951b1ed73b. The current merge preview is clean, butmainhas changed the libc CMake file andGuestAllocations.cpptest path, both touched by this PR. Please keep the branch current and rerun the heap lifetime/integrity tests and relevant allocator suite on the integrated tree.Checks
git diff --check: clean.tools/check_conventions.py: passed against currentmain.main; hosted product jobs are not current-main validation.