fix(json2): release partial entries on allocation failure #8530

Merged
Ondra97 merged 1 commit from upstream-pr/8530 into main 2026-10-25 01:11:58 +00:00
Ondra97 commented 2026-10-20 15:17:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-20T15:17:29Z.


What

Json2 array parsing constructs each value before linking it into its owning list. If list-node allocation fails, destination cleanup cannot reclaim that value. On the tested base, parsing [1] with allocation budget 2 retains one allocation after cleanup.

Construct each array entry directly in its owning list so existing partial-tree cleanup owns it before further allocation. The base already handles the object-entry failure paths from the original patch; retain its ordered object insertion, duplicate-key replacement and incremental parser behavior.

Extend allocation-failure coverage for valid and malformed documents, nested containers, long keys, callback reads and missing-key insertion. Preserve upstream iterator-copy and swap regressions. Check destination values, entry identity, container sizes and mismatch handlers after failures. Related to #8005, finding C9; no title, OOM or performance improvement is claimed.

The agreed order with #10378 is #8530 first, then #10378 rebased onto it; #10378 already declares Depends on: #8530. That branch replaces the same parser loop with allocator-backed storage and has added bounded [1]/[1, failure and callback-reader coverage. Its later rebase must preserve the cleanup invariant, allocator behavior and measured partial-parser states. The old list edit and host-allocation budget counts are not copied verbatim. No combined build of the two open PRs is claimed.

Tested

Rebased on upstream main 2e22b67032ea335ecad88b1acf777a29a70d851b; tested PR head 3e54ab2633b6b8ccc0c6b82d286dc3aa4e09948b. Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 31.7 GiB RAM, NVIDIA GeForce RTX 4070 Ti SUPER, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Ninja, Release, BUILD_TESTING=ON, pinned bundled dependencies. These Json2 tests use the CPU.

  • cmake --build <build> --target relinker libs guest_json_tests guest_json_copy_failure_tests patched_libraries_tests --parallel 8: production relinker, all patched libraries and selected Json2 fixtures built successfully.
  • ctest --test-dir <build> -R '^(guest_json|guest_json_numeric_locale|guest_json_copy_failure|patched_libraries)$' -V --output-on-failure --no-tests=error: 4/4 passed, zero skips; 368 patched libraries loaded, none failed. The allocation regression performed 37 operations and 549 forced allocation failures without a tracked leak, including [1], malformed/nested input, callback reads, long keys, object insertion, iterator copies and swap.
  • ctest --test-dir <build> -R '^public_headers($|_compile_)' -j 8 --output-on-failure --no-tests=error: 65/65 passed, zero skips, including the audit and all 64 header compilation batches. JsonCopyFailure.cpp now uses the public Api.hpp types and declarations introduced by #10706; its mismatch callback uses the declared ValueType ABI. No duplicate PS5 export prototypes remain in the test.
  • python tools/check_conventions.py --base origin/main, python tools/check_main_sync.py --base origin/main --strict, git diff origin/main...HEAD --check and python tools/check_test_registration.py <build>: passed. python tools/pr_overlap.py --branch codex/pr8530-rebase-20261025 --repo boykopovar/AnyPS5 --jobs 12 completed; #10378 is the agreed follow-up and changes the same parser loop, while #9826 covers separate placeholders.

The historical pre-fix control failed at parse array budget=2: operation leaked allocations. This rebase did not repeat that negative control on current main; its parser still constructs the item before list admission. The default all-target build, complete CTest suite and title run were not performed on this rebased head.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4937969084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8530). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-20T15:17:29Z. --- ### What Json2 array parsing constructs each value before linking it into its owning list. If list-node allocation fails, destination cleanup cannot reclaim that value. On the tested base, parsing `[1]` with allocation budget 2 retains one allocation after cleanup. Construct each array entry directly in its owning list so existing partial-tree cleanup owns it before further allocation. The base already handles the object-entry failure paths from the original patch; retain its ordered object insertion, duplicate-key replacement and incremental parser behavior. Extend allocation-failure coverage for valid and malformed documents, nested containers, long keys, callback reads and missing-key insertion. Preserve upstream iterator-copy and swap regressions. Check destination values, entry identity, container sizes and mismatch handlers after failures. Related to #8005, finding C9; no title, OOM or performance improvement is claimed. The [agreed order with #10378](https://github.com/boykopovar/AnyPS5/pull/10378#issuecomment-6385874580) is #8530 first, then #10378 rebased onto it; #10378 already declares `Depends on: #8530`. That branch replaces the same parser loop with allocator-backed storage and has added bounded `[1]`/`[1,` failure and callback-reader coverage. Its later rebase must preserve the cleanup invariant, allocator behavior and measured partial-parser states. The old list edit and host-allocation budget counts are not copied verbatim. No combined build of the two open PRs is claimed. ### Tested Rebased on upstream `main` `2e22b67032ea335ecad88b1acf777a29a70d851b`; tested PR head `3e54ab2633b6b8ccc0c6b82d286dc3aa4e09948b`. Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 31.7 GiB RAM, NVIDIA GeForce RTX 4070 Ti SUPER, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Ninja, Release, `BUILD_TESTING=ON`, pinned bundled dependencies. These Json2 tests use the CPU. - `cmake --build <build> --target relinker libs guest_json_tests guest_json_copy_failure_tests patched_libraries_tests --parallel 8`: production relinker, all patched libraries and selected Json2 fixtures built successfully. - `ctest --test-dir <build> -R '^(guest_json|guest_json_numeric_locale|guest_json_copy_failure|patched_libraries)$' -V --output-on-failure --no-tests=error`: 4/4 passed, zero skips; 368 patched libraries loaded, none failed. The allocation regression performed 37 operations and 549 forced allocation failures without a tracked leak, including `[1]`, malformed/nested input, callback reads, long keys, object insertion, iterator copies and swap. - `ctest --test-dir <build> -R '^public_headers($|_compile_)' -j 8 --output-on-failure --no-tests=error`: 65/65 passed, zero skips, including the audit and all 64 header compilation batches. `JsonCopyFailure.cpp` now uses the public `Api.hpp` types and declarations introduced by #10706; its mismatch callback uses the declared `ValueType` ABI. No duplicate PS5 export prototypes remain in the test. - `python tools/check_conventions.py --base origin/main`, `python tools/check_main_sync.py --base origin/main --strict`, `git diff origin/main...HEAD --check` and `python tools/check_test_registration.py <build>`: passed. `python tools/pr_overlap.py --branch codex/pr8530-rebase-20261025 --repo boykopovar/AnyPS5 --jobs 12` completed; #10378 is the agreed follow-up and changes the same parser loop, while #9826 covers separate placeholders. The historical pre-fix control failed at `parse array budget=2: operation leaked allocations`. This rebase did not repeat that negative control on current main; its parser still constructs the item before list admission. The default all-target build, complete CTest suite and title run were not performed on this rebased head. <!-- 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. --> ### 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
Ondra97 commented 2026-10-20 16:52:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-20T16:52:51Z.


Could someone help establish the PS5 Json2 allocator/initialization contract for the separate C2 follow-up in #8005? This is a contract question for that follow-up; the implementation in this PR covers allocation-failure ownership.

On tested revision c9f680fb, a synthetic parse/serialize probe performs 21 tracked host C++ allocations and makes zero calls to the supplied allocator's allocate/free/notify callbacks. Both the normal and null-returning callback settings still give successful initialize/parse results. Since no callback runs, this confirms ignored configuration but does not validate the candidate callback ABI.

I need to establish:

  • The allocator virtual-method signatures and alignment requirements, allocation-failure reporting, and how the RTTI allocator variant differs.
  • Whether initialization state is global or per Initializer, and the required allocator/object lifetime across repeated initialize/terminate calls and reinitialization with live Values.
  • Which parser overloads consume fileBufferSize, including what a zero size means.
  • The allowed SpecialFloatFormatType values and their behavior, especially value 2 in the supplied initialization configuration.

An API reference or results from a small PS5 test covering these cases would help me implement the general behavior and add a regression that fails before the fix and passes afterward. The synthetic probe uses no title assets.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6296297316 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8530#issuecomment-6296297316). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-20T16:52:51Z. --- Could someone help establish the PS5 Json2 allocator/initialization contract for the separate C2 follow-up in #8005? This is a contract question for that follow-up; the implementation in this PR covers allocation-failure ownership. On [tested revision c9f680fb](https://github.com/boykopovar/AnyPS5/blob/c9f680fb36611522509c0a94462340d834479186/core/libs/prx/libSceJson2/Export.cpp), a synthetic parse/serialize probe performs 21 tracked host C++ allocations and makes zero calls to the supplied allocator's allocate/free/notify callbacks. Both the normal and null-returning callback settings still give successful initialize/parse results. Since no callback runs, this confirms ignored configuration but does not validate the candidate callback ABI. I need to establish: - The allocator virtual-method signatures and alignment requirements, allocation-failure reporting, and how the RTTI allocator variant differs. - Whether initialization state is global or per Initializer, and the required allocator/object lifetime across repeated initialize/terminate calls and reinitialization with live Values. - Which parser overloads consume `fileBufferSize`, including what a zero size means. - The allowed `SpecialFloatFormatType` values and their behavior, especially value `2` in the supplied initialization configuration. An API reference or results from a small PS5 test covering these cases would help me implement the general behavior and add a regression that fails before the fix and passes afterward. The synthetic probe uses no title assets.
harisibrar1 commented 2026-10-24 13:50:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:50:11Z.

Review: APPROVED


Checked against main 522e32e3b6 and libSceJson2 Export.cpp on main:

  • It still merges cleanly. Main's array parser loop (Export.cpp:553-556) still builds the Node before push_back, so a failure on the list-node allocation leaks it; [1] at budget 2 is a concrete case.
  • emplace_back() leaves node == nullptr until Construct succeeds, and Destroy(Value) skips null nodes, so ParseValue's Clear now reclaims every partial entry. I found no double-free or dangling reference.
  • The other insertion paths (ObjectEntry, Array::push_back, the object iterator insert) already roll back on failure, so this was the remaining gap. The test's expected codes (-1 from the reader, 0x80848101 otherwise) match main's Parser::Error.
  • As the bot noted, #10378 rewrites this same loop, so whichever lands second needs to redo it.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638476991 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8530#pullrequestreview-5638476991). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:50:11Z. **Review:** APPROVED --- Checked against main 522e32e3b6 and libSceJson2 Export.cpp on main: - It still merges cleanly. Main's array parser loop (Export.cpp:553-556) still builds the `Node` before `push_back`, so a failure on the list-node allocation leaks it; `[1]` at budget 2 is a concrete case. - `emplace_back()` leaves `node == nullptr` until `Construct` succeeds, and `Destroy(Value)` skips null nodes, so `ParseValue`'s `Clear` now reclaims every partial entry. I found no double-free or dangling reference. - The other insertion paths (`ObjectEntry`, `Array::push_back`, the object iterator insert) already roll back on failure, so this was the remaining gap. The test's expected codes (-1 from the reader, 0x80848101 otherwise) match main's `Parser::Error`. - As the bot noted, #10378 rewrites this same loop, so whichever lands second needs to redo it. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!8530
No description provided.