fix(libSceNgs2): refuse a destroy from inside its own system's render #8513
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8513"
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 milescoviello on 2026-10-20T14:25:56Z.
What
A voice callback runs during its system's render. If the callback destroys a rack of that system, or the system itself,
sceNgs2RackDestroyandsceNgs2SystemDestroyfree it at once, because the render belongs to the calling thread and nothing makes them wait. The render then goes on walking the freed rack.Both calls now throw when the calling thread is rendering the system concerned:
NGS2: destroying a rack of a system from that system's render (a voice callback) is not implementedNGS2: destroying a system from its own render (a voice callback) is not implementedEach system counts the renders it is in (
renderDepth), as @nikowolf123 suggested. A destroy first waits until no other thread holds the exclusive state, so a non-zero count can only come from the calling thread's own stack, including a render of another system nested in one of this system's callbacks (0ff3d32a3d). The first version used a thread-local pointer to the innermost system being rendered, which let such a nested destroy through. Destroying a rack of a system that is not being rendered works as before, and so do destroys from other threads, which wait as #8238 makes them. What the console does here is not measured. A new debt entry records that.This is a follow-up to #8238. @wachi35676 found the crash: it happens the same way on
main, so it is a separate fix.Why
On
main, and with #8238, a block-end callback that destroys its own rack, its own system, or another rack of its system crashes 3 runs out of 3:corrupted double-linked listorSIGSEGVon Linux, exit 127/139 on Windows (@wachi35676's probes). A clear error is easier to act on than heap corruption at a later, unrelated point.Tested
Ubuntu 24.04 (Linux 6.8), GCC 13.3, on #8238's head
741e706b19.ngs2_render/ngs2_core_rendergainTestDestroyDuringOwnRenderThrows. A block-end callback destroys its own rack in one pass and its own system in another. The render must end in the "is not implemented" error, and the system must still destroy cleanly afterwards. With #8238's sources, both tests fail at that check 3 of 3, because the destroy returns and the render carries on.0ff3d32a3d, after @nikowolf123's ASan report below): system A's block-end callback renders system B, and B's callback destroys A's rack in one pass and A in another. Both must be refused, and both systems must then destroy cleanly. With the thread-local off1cafaca07, both tests fail at that check 3 of 3. With0ff3d32a3d, all 13^ngs2tests pass 3 of 3.tools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pylists no conflicting lines.Not tested: Windows, a title that does this, the console itself.
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T14:38:01Z.
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.
core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/Rack.cppcore/libs/prx/libSceNgs2.native/src/System.cppcore/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppdocs/dev/debt/libSceNgs2.native.mddocs/dev/debt/libSceNgs2.native.mddocs/dev/debt/libSceNgs2.native.mdcore/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/Rack.cppdocs/dev/debt/libSceNgs2.native.mdImported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:04:40Z.
The check at
System.cpp#L107sees only the innermost render on the thread, because eachsceNgs2SystemRenderreplacesRenderedHere(). The rack lock entry from #8238 says a thread nests renders, also from its own callbacks (debt file, line 40). A destroy issued from such a nested render is not refused, even though the thread is still rendering the outer system.Input: system A has a sampler voice in rack RA, patched to a mastering voice. Its BLOCK_END callback calls
sceNgs2SystemRender(B). System B is set up the same way, and its BLOCK_END callback callssceNgs2RackDestroy(RA). A is rendered once. In B's callbackRenderedHere()is B, soRack.cpp#L206lets the destroy through and it returns 0.I built the PR-head
libSceNgs2.nativesources with-fsanitize=address(GCC 15.2, Ubuntu on WSL) and a small driver. Once B's render returns, A's render reportsheap-use-after-freeatRender.cpp#L85, on the voice thatNgs2DestroyRackfreed.sceNgs2SystemDestroy(A)from B's callback gives the same report, becauseSystem.cpp#L285has the same gap. The direct cases are refused as described.A render count on the system closes this gap and replaces the thread-local. After the wait loop in either destroy,
Ngs2ExclusiveElsewhere()is false, so no other thread is inside a render. A non-zero count can then only come from this thread's own stack:I applied this on top of
f1cafaca07and used the same ASan build:Ngs2Render.cpppasses.A nested pass in
TestDestroyDuringOwnRenderThrowswould cover this: A's callback renders B, and B's callback destroys RA or A. If nested renders should stay out of scope, the new debt entry could instead say that such a destroy is not refused.AI-assisted: written with Claude Code. I ran the ASan driver for both nested variants on the PR head and with the change above, and the
Ngs2Render.cppcases with the change.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T19:08:20Z.
Thanks @nikowolf123, you're right. The thread-local only saw the innermost render.
0ff3d32a3dreplaces it with your per-systemrenderDepth. Since a destroy first waits until no other thread holds the exclusive state, a non-zero count can only come from the calling thread's own stack.TestDestroyDuringOwnRenderThrowsgains your nested case: A's block-end callback renders B, and B's callback destroys A's rack in one pass and A in another. Both are refused, and both systems then destroy cleanly. With f1cafaca07's thread-local,ngs2_renderandngs2_core_renderfail at that check 3 of 3. With0ff3d32a3d, all 13^ngs2tests pass 3 of 3. The debt entry now names the nested case too.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-21T04:50:22Z.
Windows results for this head (
0ff3d32a3d), since the description only has Linux. Setup: Windows 11 Enterprise (build 22631), NVIDIA GeForce GT 1030 (driver 582.66), WinLibs GCC 15.2.0 r7 (UCRT), Release,BUILD_TESTING=ON.ngs2_renderandngs2_core_render, both with the newTestDestroyDuringOwnRenderThrows, pass 3 of 3 at this head.741e706b19, whose tests don't have the new case yet.AI-assisted: yes (Claude Code)