fix(json2): release partial copies on allocation failure #6593

Merged
Ondra97 merged 4 commits from upstream-pr/6593 into main 2026-10-19 14:15:27 +00:00
Ondra97 commented 2026-10-15 21:49:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-15T21:49:31Z.


What

Release partially constructed libSceJson2 values and deep copies when an allocation fails. Construction keeps a temporary value until initialization succeeds; partial arrays, objects, nodes and append copies use the existing cleanup helpers before rethrowing.

guest_json_copy_failure sweeps every allocation failure point across 17 construction, copy, assignment, setter and append cases. It checks allocation balance, unchanged sources and preservation of existing destinations. It compiles the production Export.cpp directly into the executable so its replacement operator new can inject failures.

The review fix isolates this test from AnyPS5 libc on Linux. The injected failure previously also reached the guest unwinder: FindFrameFast allocates a cache-map node while holding its mutex, and a second injected exception reenters that mutex. A debugger backtrace reproduced this deadlock with GCC 13.3 and the CI linker, Wild 0.10.0. The isolated test uses the host C++ exception runtime; its unused unimplemented-export hook fails immediately if reached. Windows retains its existing libc dependency and DWARF unwind setup for the APS5_VABI frames. The shared-library guest_json test continues to exercise the production library.

Tested

Base: 7a36de66b91217fb00fe6751343506ab01e677b3. Submitted revision: 59dbdd615a3aa967e010e5d1c86f0ac7fef7de10. Review-fix commit: 9ee4af350e0e928440745c11dcfb8c3bb17b7802.

  • Windows host: Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 32 GiB RAM, GeForce RTX 4070 Ti SUPER; supported WinLibs MinGW-w64 GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPU execution was not exercised.
  • Linux VM: Ubuntu 24.04.5 LTS, kernel 7.0.0-38-generic, VirtualBox 7.2.20, four vCPUs on the same Ryzen host, 8 GiB RAM, VirtualBox SVGA graphics not exercised. GCC 13.3.0 and Wild 0.10.0, matching the CI compiler/linker versions. A separate build used the signed Ubuntu Toolchain PPA GCC 16.0.1 20260315 experimental and its matching C++ runtime, without replacing the VM's system compiler or libraries.
Check Result
Original test wiring with current production libc, Linux GCC 13.3/Wild Hangs; bounded probe exits 124 and GDB shows recursive FindFrameFast mutex acquisition
Isolated test with the base JSON implementation, Linux Fails at value object copy budget=1, one retained allocation
Fixed sweep, Windows GCC 15.2 PASS: 17 operations, 309 injected failures, zero retained allocations
Fixed sweep, Linux GCC 13.3/Wild and GCC 16.0.1/Wild PASS in CTest: 17 operations, 309 injected failures, zero retained allocations on each
Existing shared-library JSON test PASS on Windows and Linux GCC 13.3
Patched-library loading, Windows PASS: 351 loaded, zero failures
Complete Windows Release build, including libraries, relinker and default test targets PASS, including the final incremental build after updating main

Final focused runs: Windows 3/3, Linux GCC 13.3 2/2, and Linux GCC 16.0.1 1/1, with no failures or skips. The GCC 16 test also passed in a separate -O0 build. The original unwinder deadlock also reproduced with the available GCC 16.0.1 runtime. The 30-second CTest timeout and allocation sweep remain unchanged. Conventions, whitespace, main-sync and test-registration checks passed.

Windows commands, after configuring with the supported toolchain:

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON
cmake --build build --parallel 8
ctest --test-dir build -R '^(guest_json(_copy_failure)?|patched_libraries)$' -V --output-on-failure --no-tests=error --parallel 1

Linux GCC 13.3 built guest_json_tests and guest_json_copy_failure_tests, including their production-library dependencies, then ran ctest -R '^guest_json(_copy_failure)?$' -V --output-on-failure --no-tests=error. The separate GCC 16 build compiled and ran guest_json_copy_failure_tests through CMake/CTest.

These are host C++ allocation-failure regressions, not retail-title or system-wide OOM measurements. A new hosted CI run and the exact reported Arch GCC 16.2.1 toolchain have not been tested. Full Linux build, native Linux and GPU execution: not tested. The former build dependency #6452 is merged and no longer required as an open dependency.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4880309107 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-15T21:49:31Z. --- ### What Release partially constructed `libSceJson2` values and deep copies when an allocation fails. Construction keeps a temporary value until initialization succeeds; partial arrays, objects, nodes and append copies use the existing cleanup helpers before rethrowing. `guest_json_copy_failure` sweeps every allocation failure point across 17 construction, copy, assignment, setter and append cases. It checks allocation balance, unchanged sources and preservation of existing destinations. It compiles the production `Export.cpp` directly into the executable so its replacement `operator new` can inject failures. The review fix isolates this test from AnyPS5 `libc` on Linux. The injected failure previously also reached the guest unwinder: `FindFrameFast` allocates a cache-map node while holding its mutex, and a second injected exception reenters that mutex. A debugger backtrace reproduced this deadlock with GCC 13.3 and the CI linker, Wild 0.10.0. The isolated test uses the host C++ exception runtime; its unused unimplemented-export hook fails immediately if reached. Windows retains its existing `libc` dependency and DWARF unwind setup for the `APS5_VABI` frames. The shared-library `guest_json` test continues to exercise the production library. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Base: `7a36de66b91217fb00fe6751343506ab01e677b3`. Submitted revision: `59dbdd615a3aa967e010e5d1c86f0ac7fef7de10`. Review-fix commit: `9ee4af350e0e928440745c11dcfb8c3bb17b7802`. - **Windows host:** Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 32 GiB RAM, GeForce RTX 4070 Ti SUPER; supported WinLibs MinGW-w64 GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. GPU execution was not exercised. - **Linux VM:** Ubuntu 24.04.5 LTS, kernel 7.0.0-38-generic, VirtualBox 7.2.20, four vCPUs on the same Ryzen host, 8 GiB RAM, VirtualBox SVGA graphics not exercised. GCC 13.3.0 and Wild 0.10.0, matching the CI compiler/linker versions. A separate build used the signed Ubuntu Toolchain PPA GCC **16.0.1 20260315 experimental** and its matching C++ runtime, without replacing the VM's system compiler or libraries. | Check | Result | |---|---| | Original test wiring with current production `libc`, Linux GCC 13.3/Wild | Hangs; bounded probe exits 124 and GDB shows recursive `FindFrameFast` mutex acquisition | | Isolated test with the base JSON implementation, Linux | Fails at `value object copy budget=1`, one retained allocation | | Fixed sweep, Windows GCC 15.2 | PASS: 17 operations, 309 injected failures, zero retained allocations | | Fixed sweep, Linux GCC 13.3/Wild and GCC 16.0.1/Wild | PASS in CTest: 17 operations, 309 injected failures, zero retained allocations on each | | Existing shared-library JSON test | PASS on Windows and Linux GCC 13.3 | | Patched-library loading, Windows | PASS: 351 loaded, zero failures | | Complete Windows Release build, including libraries, relinker and default test targets | PASS, including the final incremental build after updating main | Final focused runs: Windows **3/3**, Linux GCC 13.3 **2/2**, and Linux GCC 16.0.1 **1/1**, with no failures or skips. The GCC 16 test also passed in a separate `-O0` build. The original unwinder deadlock also reproduced with the available GCC 16.0.1 runtime. The 30-second CTest timeout and allocation sweep remain unchanged. Conventions, whitespace, main-sync and test-registration checks passed. Windows commands, after configuring with the supported toolchain: ```sh cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON cmake --build build --parallel 8 ctest --test-dir build -R '^(guest_json(_copy_failure)?|patched_libraries)$' -V --output-on-failure --no-tests=error --parallel 1 ``` Linux GCC 13.3 built `guest_json_tests` and `guest_json_copy_failure_tests`, including their production-library dependencies, then ran `ctest -R '^guest_json(_copy_failure)?$' -V --output-on-failure --no-tests=error`. The separate GCC 16 build compiled and ran `guest_json_copy_failure_tests` through CMake/CTest. These are host C++ allocation-failure regressions, not retail-title or system-wide OOM measurements. A new hosted CI run and the exact reported Arch GCC **16.2.1** toolchain have not been tested. Full Linux build, native Linux and GPU execution: not tested. The former build dependency #6452 is merged and no longer required as an open dependency. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Codex)
github-actions[bot] commented 2026-10-15 22:07:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T22:07:17Z.


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 6044 core/libs/prx/libSceJson2/CMakeLists.txt
PR 7215 core/libs/prx/libSceJson2/CMakeLists.txt
core/libs/prx/libSceJson2/Export.cpp
PR 7477 core/libs/prx/libSceJson2/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6203272881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593#issuecomment-6203272881). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T22:07:17Z. --- <!-- 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 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libSceJson2/CMakeLists.txt` | | [PR 7215](https://www.github.com/boykopovar/AnyPS5/pull/7215) | `core/libs/prx/libSceJson2/CMakeLists.txt`<br>`core/libs/prx/libSceJson2/Export.cpp` | | [PR 7477](https://www.github.com/boykopovar/AnyPS5/pull/7477) | `core/libs/prx/libSceJson2/Export.cpp` |
nicolasfalesy commented 2026-10-16 08:52:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-16T08:52:56Z.


Tested on Linux (Arch), c++ (GCC) 16.2.1 20260810, Release, BUILD_TESTING=ON, main at 5d9dcc42d2ec5401aebd089028b8d4d7410a77b4.

guest_json_copy_failure (this PR's test) aborts on main plus this PR (2 of 2), and also on the PR head as submitted. It prints nothing:

The following tests FAILED:
	guest_json_copy_failure (Subprocess aborted)

Under gdb the abort is raised inside the unwinder, not in a Require:

#2  abort ()
#3  _Unwind_RaiseException_Phase2.cold ()
#4  _Unwind_Resume ()
#5  sce::Json::Value::Value(sce::Json::Value const&) [clone .cold] ()
#6  main ()

So the injected std::bad_alloc thrown by the replacement operator new (JsonCopyFailure.cpp:55-62) seems not to reach the catch (const std::bad_alloc&) at JsonCopyFailure.cpp:125 when it passes through the cleanup path of Value::Value(const Value&) (Export.cpp:683). The description reports Ubuntu 24.04 with GCC 13.3 passing, so this may depend on the toolchain (GCC 16.2.1 here). It also aborts when I build the two sources by hand at -O0. I did not find the root cause.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6211826749 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593#issuecomment-6211826749). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-16T08:52:56Z. --- Tested on Linux (Arch), `c++ (GCC) 16.2.1 20260810`, Release, `BUILD_TESTING=ON`, `main` at `5d9dcc42d2ec5401aebd089028b8d4d7410a77b4`. `guest_json_copy_failure` (this PR's test) aborts on `main` plus this PR (2 of 2), and also on the PR head as submitted. It prints nothing: ``` The following tests FAILED: guest_json_copy_failure (Subprocess aborted) ``` Under gdb the abort is raised inside the unwinder, not in a `Require`: ``` #2 abort () #3 _Unwind_RaiseException_Phase2.cold () #4 _Unwind_Resume () #5 sce::Json::Value::Value(sce::Json::Value const&) [clone .cold] () #6 main () ``` So the injected `std::bad_alloc` thrown by the replacement `operator new` (`JsonCopyFailure.cpp:55-62`) seems not to reach the `catch (const std::bad_alloc&)` at `JsonCopyFailure.cpp:125` when it passes through the cleanup path of `Value::Value(const Value&)` (`Export.cpp:683`). The description reports Ubuntu 24.04 with GCC 13.3 passing, so this may depend on the toolchain (GCC 16.2.1 here). It also aborts when I build the two sources by hand at `-O0`. I did not find the root cause. AI-assisted: yes
adriadam10 commented 2026-10-19 07:01:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T07:01:00Z.

Review: CHANGES_REQUESTED


The full Build's Linux job fails on this PR's new test, and main does not have the failure:

  • guest_json_copy_failure hits the 30 s TIMEOUT set in core/libs/prx/libSceJson2/CMakeLists.txt:23 (run 39211061883, "293/729 Test #1076 ... Timeout 30.05 sec").
  • @nicolasfalesy reported the same test aborting inside _Unwind_Resume (from Value::Value(const Value&)) on GCC 16.2.1, so it fails on more than one toolchain: the injected std::bad_alloc from the replacement operator new (JsonCopyFailure.cpp:55-62) does not seem to reach the catch at JsonCopyFailure.cpp:125 when it unwinds through the APS5_VABI export frames.

Please make the sweep terminate and pass on the CI Linux runner (and GCC 16), e.g. check that unwinding through the APS5_VABI frames works there, or inject the failure at a point where it is catchable. The Export.cpp changes themselves look right; the test is what needs to hold on Linux CI.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5567965344 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593#pullrequestreview-5567965344). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T07:01:00Z. **Review:** CHANGES_REQUESTED --- The full Build's Linux job fails on this PR's new test, and `main` does not have the failure: - `guest_json_copy_failure` hits the 30 s `TIMEOUT` set in `core/libs/prx/libSceJson2/CMakeLists.txt:23` (run 39211061883, "293/729 Test #1076 ... Timeout 30.05 sec"). - @nicolasfalesy reported the same test aborting inside `_Unwind_Resume` (from `Value::Value(const Value&)`) on GCC 16.2.1, so it fails on more than one toolchain: the injected `std::bad_alloc` from the replacement `operator new` (`JsonCopyFailure.cpp:55-62`) does not seem to reach the `catch` at `JsonCopyFailure.cpp:125` when it unwinds through the `APS5_VABI` export frames. Please make the sweep terminate and pass on the CI Linux runner (and GCC 16), e.g. check that unwinding through the `APS5_VABI` frames works there, or inject the failure at a point where it is catchable. The `Export.cpp` changes themselves look right; the test is what needs to hold on Linux CI.
Ondra97 commented 2026-10-19 11:37:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-19T11:37:47Z.


@adriadam10 I pushed the test fix in 9ee4af350e, included in the current head 59dbdd615a.

I reproduced the timeout with GCC 13.3 and Wild 0.10.0. The replacement operator new also injected failures into AnyPS5 libc: FindFrameFast allocates a cache-map entry while holding its mutex, and the resulting nested exception tries to acquire the same mutex. GDB confirmed that recursive lock acquisition.

On Linux, the failure-injection test now uses the host C++ exception runtime without linking AnyPS5 libc. It still compiles the production Export.cpp and checks all 17 operations and 309 injected failures. Windows retains its existing libc dependency and DWARF unwind setup. The 30-second timeout and sweep are unchanged. The base JSON implementation still fails the regression with one retained allocation.

Validation at the current head:

  • Windows GCC 15.2: complete Release build passed; 3/3 focused tests passed, including loading 351 patched libraries.
  • Linux GCC 13.3 / Wild 0.10.0: both JSON tests passed, 2/2.
  • Linux GCC 16.0.1 experimental (20260315) / Wild 0.10.0: the allocation-failure CTest passed; a separate -O0 build passed too.

The full hosted Linux/Windows jobs were skipped on this update. Could you rerun the full Linux CI job? @nicolasfalesy, could you retest with your Arch GCC 16.2.1? I have not verified that exact toolchain or independently reproduced its reported abort. The PR description contains the detailed results and commands.

AI-assisted: yes (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6266306834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593#issuecomment-6266306834). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-19T11:37:47Z. --- @adriadam10 I pushed the test fix in 9ee4af350e, included in the current head 59dbdd615a. I reproduced the timeout with GCC 13.3 and Wild 0.10.0. The replacement `operator new` also injected failures into AnyPS5 `libc`: `FindFrameFast` allocates a cache-map entry while holding its mutex, and the resulting nested exception tries to acquire the same mutex. GDB confirmed that recursive lock acquisition. On Linux, the failure-injection test now uses the host C++ exception runtime without linking AnyPS5 `libc`. It still compiles the production `Export.cpp` and checks all 17 operations and 309 injected failures. Windows retains its existing `libc` dependency and DWARF unwind setup. The 30-second timeout and sweep are unchanged. The base JSON implementation still fails the regression with one retained allocation. Validation at the current head: - Windows GCC 15.2: complete Release build passed; 3/3 focused tests passed, including loading 351 patched libraries. - Linux GCC 13.3 / Wild 0.10.0: both JSON tests passed, 2/2. - Linux GCC 16.0.1 experimental (20260315) / Wild 0.10.0: the allocation-failure CTest passed; a separate `-O0` build passed too. The full hosted Linux/Windows jobs were skipped on this update. Could you rerun the full Linux CI job? @nicolasfalesy, could you retest with your Arch GCC 16.2.1? I have not verified that exact toolchain or independently reproduced its reported abort. The PR description contains the detailed results and commands. AI-assisted: yes (Codex).
nicolasfalesy commented 2026-10-19 13:15:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-19T13:15:06Z.


Retested on Linux (Arch), c++ (GCC) 16.2.1 20260810, Release, BUILD_TESTING=ON, head 59dbdd615a3aa967e010e5d1c86f0ac7fef7de10.

  • guest_json_copy_failure: passes, 2 of 2 (0.01 s each). The binary prints JSON copy cleanup: 17 operations, 309 allocation failures, no leaks.
  • guest_json: passes, 2 of 2.
  • The two sources built by hand at -O0 (JsonCopyFailure.cpp + Export.cpp, as in my earlier report): passes, 2 of 2, same output.

The _Unwind_Resume abort from my earlier report is gone on this toolchain.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6268103215 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593#issuecomment-6268103215). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-19T13:15:06Z. --- Retested on Linux (Arch), `c++ (GCC) 16.2.1 20260810`, Release, `BUILD_TESTING=ON`, head `59dbdd615a3aa967e010e5d1c86f0ac7fef7de10`. - `guest_json_copy_failure`: passes, 2 of 2 (0.01 s each). The binary prints `JSON copy cleanup: 17 operations, 309 allocation failures, no leaks`. - `guest_json`: passes, 2 of 2. - The two sources built by hand at `-O0` (`JsonCopyFailure.cpp` + `Export.cpp`, as in my earlier report): passes, 2 of 2, same output. The `_Unwind_Resume` abort from my earlier report is gone on this toolchain. AI-assisted: yes
adriadam10 commented 2026-10-19 14:16:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T14:16:37Z.


Re-checked after the test isolation (9ee4af350): on Linux guest_json_copy_failure no longer links AnyPS5 libc, so the injected bad_alloc goes through the host C++ unwinder instead of the guest FindFrameFast mutex that caused the 30 s timeout; Windows keeps libc and the DWARF unwind setup. Export.cpp cleanup paths are unchanged from the earlier review and still correct. Merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6269377635 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6593#issuecomment-6269377635). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T14:16:37Z. --- Re-checked after the test isolation (`9ee4af350`): on Linux `guest_json_copy_failure` no longer links AnyPS5 `libc`, so the injected `bad_alloc` goes through the host C++ unwinder instead of the guest `FindFrameFast` mutex that caused the 30 s timeout; Windows keeps `libc` and the DWARF unwind setup. `Export.cpp` cleanup paths are unchanged from the earlier review and still correct. Merged.
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!6593
No description provided.