fix(libSceNgs2): refuse a destroy from inside its own system's render #8513

Merged
milescoviello merged 6 commits from upstream-pr/8513 into main 2026-10-21 04:44:57 +00:00
milescoviello commented 2026-10-20 14:25:56 +00:00 (Migrated from github.com)

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, sceNgs2RackDestroy and sceNgs2SystemDestroy free 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 implemented
  • NGS2: destroying a system from its own render (a voice callback) is not implemented

Each 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 list or SIGSEGV on 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_render gain TestDestroyDuringOwnRenderThrows. 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.
  • A nested pass (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 of f1cafaca07, both tests fail at that check 3 of 3. With 0ff3d32a3d, all 13 ^ngs2 tests pass 3 of 3.
  • tools/check_conventions.py --base upstream/main passes. tools/pr_overlap.py lists no conflicting lines.

Not tested: Windows, a title that does this, the console itself.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (#8238 has merged; the branch now carries only this change's two commits)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (audio library)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4937235849 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8513). Originally posted by [milescoviello](https://github.com/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, `sceNgs2RackDestroy` and `sceNgs2SystemDestroy` free 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 implemented` - `NGS2: destroying a system from its own render (a voice callback) is not implemented` Each 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](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6292795752): 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 list` or `SIGSEGV` on 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_render` gain `TestDestroyDuringOwnRenderThrows`. 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. - A nested pass (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 of f1cafaca07, both tests fail at that check 3 of 3. With 0ff3d32a3d, all 13 `^ngs2` tests pass 3 of 3. - `tools/check_conventions.py --base upstream/main` passes. `tools/pr_overlap.py` lists no conflicting lines. Not tested: Windows, a title that does this, the console itself. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#8238 has merged; the branch now carries only this change's two commits) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a (audio library) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 14:38:01 +00:00 (Migrated from github.com)

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.

Pull request Shared files not compared
PR 1384 core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
core/libs/prx/libSceNgs2.native/src/Rack.cpp
core/libs/prx/libSceNgs2.native/src/System.cpp
PR 5797 core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
docs/dev/debt/libSceNgs2.native.md
PR 8327 docs/dev/debt/libSceNgs2.native.md
PR 8528 docs/dev/debt/libSceNgs2.native.md
PR 8609 core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
core/libs/prx/libSceNgs2.native/src/Rack.cpp
docs/dev/debt/libSceNgs2.native.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293438376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8513#issuecomment-6293438376). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T14:38:01Z. --- <!-- pr-overlap --> 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. | Pull request | Shared files not compared | |---|---| | [PR 1384](https://www.github.com/boykopovar/AnyPS5/pull/1384) | `core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp`<br>`core/libs/prx/libSceNgs2.native/src/Rack.cpp`<br>`core/libs/prx/libSceNgs2.native/src/System.cpp` | | [PR 5797](https://www.github.com/boykopovar/AnyPS5/pull/5797) | `core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp`<br>`docs/dev/debt/libSceNgs2.native.md` | | [PR 8327](https://www.github.com/boykopovar/AnyPS5/pull/8327) | `docs/dev/debt/libSceNgs2.native.md` | | [PR 8528](https://www.github.com/boykopovar/AnyPS5/pull/8528) | `docs/dev/debt/libSceNgs2.native.md` | | [PR 8609](https://www.github.com/boykopovar/AnyPS5/pull/8609) | `core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp`<br>`core/libs/prx/libSceNgs2.native/src/Rack.cpp`<br>`docs/dev/debt/libSceNgs2.native.md` |
nikowolf123 commented 2026-10-20 19:04:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T19:04:40Z.


The check at System.cpp#L107 sees only the innermost render on the thread, because each sceNgs2SystemRender replaces RenderedHere(). 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 calls sceNgs2RackDestroy(RA). A is rendered once. In B's callback RenderedHere() is B, so Rack.cpp#L206 lets the destroy through and it returns 0.

I built the PR-head libSceNgs2.native sources with -fsanitize=address (GCC 15.2, Ubuntu on WSL) and a small driver. Once B's render returns, A's render reports heap-use-after-free at Render.cpp#L85, on the voice that Ngs2DestroyRack freed. sceNgs2SystemDestroy(A) from B's callback gives the same report, because System.cpp#L285 has 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:

// Ngs2System
std::uint32_t renderDepth = 0;

bool Ngs2RenderingHere(const Ngs2System& system) {
    return system.renderDepth != 0;
}

// sceNgs2SystemRender
Ngs2EnterExclusive();
system->renderDepth++;
struct Release {
    SystemLockState* state;
    Ngs2System* system;
    ~Release() {
        system->renderDepth--;
        Ngs2LeaveExclusive();
        Ngs2NotifyWaiters();
        std::lock_guard locks(SystemLockMutex());
        ReleaseSystemLock(*state);
    }
} release{state, system};

I applied this on top of f1cafaca07 and used the same ASan build:

  • Both nested cases end in the "is not implemented" error, and both systems then destroy cleanly.
  • The direct cases still throw.
  • A callback that destroys a rack of a system that is not being rendered still gets 0.
  • Ngs2Render.cpp passes.

A nested pass in TestDestroyDuringOwnRenderThrows would 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.cpp cases with the change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298905943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8513#issuecomment-6298905943). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T19:04:40Z. --- The check at [`System.cpp#L107`](https://github.com/boykopovar/AnyPS5/blob/f1cafaca07b217c281db9bb23b58814ec35e9972/core/libs/prx/libSceNgs2.native/src/System.cpp#L107) sees only the innermost render on the thread, because each `sceNgs2SystemRender` replaces `RenderedHere()`. The rack lock entry from #8238 says a thread nests renders, also from its own callbacks ([debt file, line 40](https://github.com/boykopovar/AnyPS5/blob/f1cafaca07b217c281db9bb23b58814ec35e9972/docs/dev/debt/libSceNgs2.native.md#L40)). 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 calls `sceNgs2RackDestroy(RA)`. A is rendered once. In B's callback `RenderedHere()` is B, so [`Rack.cpp#L206`](https://github.com/boykopovar/AnyPS5/blob/f1cafaca07b217c281db9bb23b58814ec35e9972/core/libs/prx/libSceNgs2.native/src/Rack.cpp#L206) lets the destroy through and it returns 0. I built the PR-head `libSceNgs2.native` sources with `-fsanitize=address` (GCC 15.2, Ubuntu on WSL) and a small driver. Once B's render returns, A's render reports `heap-use-after-free` at [`Render.cpp#L85`](https://github.com/boykopovar/AnyPS5/blob/f1cafaca07b217c281db9bb23b58814ec35e9972/core/libs/prx/libSceNgs2.native/src/Render.cpp#L85), on the voice that `Ngs2DestroyRack` freed. `sceNgs2SystemDestroy(A)` from B's callback gives the same report, because [`System.cpp#L285`](https://github.com/boykopovar/AnyPS5/blob/f1cafaca07b217c281db9bb23b58814ec35e9972/core/libs/prx/libSceNgs2.native/src/System.cpp#L285) has 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: ```cpp // Ngs2System std::uint32_t renderDepth = 0; bool Ngs2RenderingHere(const Ngs2System& system) { return system.renderDepth != 0; } // sceNgs2SystemRender Ngs2EnterExclusive(); system->renderDepth++; struct Release { SystemLockState* state; Ngs2System* system; ~Release() { system->renderDepth--; Ngs2LeaveExclusive(); Ngs2NotifyWaiters(); std::lock_guard locks(SystemLockMutex()); ReleaseSystemLock(*state); } } release{state, system}; ``` I applied this on top of f1cafaca07 and used the same ASan build: - Both nested cases end in the "is not implemented" error, and both systems then destroy cleanly. - The direct cases still throw. - A callback that destroys a rack of a system that is not being rendered still gets 0. - `Ngs2Render.cpp` passes. A nested pass in `TestDestroyDuringOwnRenderThrows` would 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.cpp` cases with the change.
milescoviello commented 2026-10-20 19:08:20 +00:00 (Migrated from github.com)

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. 0ff3d32a3d replaces it with your per-system renderDepth. 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.

TestDestroyDuringOwnRenderThrows gains 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_render and ngs2_core_render fail at that check 3 of 3. With 0ff3d32a3d, all 13 ^ngs2 tests pass 3 of 3. The debt entry now names the nested case too.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6298987099 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8513#issuecomment-6298987099). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T19:08:20Z. --- Thanks @nikowolf123, you're right. The thread-local only saw the innermost render. 0ff3d32a3d replaces it with your per-system `renderDepth`. 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. `TestDestroyDuringOwnRenderThrows` gains 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_render` and `ngs2_core_render` fail at that check 3 of 3. With 0ff3d32a3d, all 13 `^ngs2` tests pass 3 of 3. The debt entry now names the nested case too. AI-assisted: yes (Claude Code)
milescoviello commented 2026-10-21 04:50:22 +00:00 (Migrated from github.com)

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_render and ngs2_core_render, both with the new TestDestroyDuringOwnRenderThrows, pass 3 of 3 at this head.
  • They also pass 3 of 3 at the merge base 741e706b19, whose tests don't have the new case yet.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6308322769 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8513#issuecomment-6308322769). Originally posted by [milescoviello](https://github.com/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_render` and `ngs2_core_render`, both with the new `TestDestroyDuringOwnRenderThrows`, pass 3 of 3 at this head. - They also pass 3 of 3 at the merge base 741e706b19, whose tests don't have the new case yet. AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8513
No description provided.