Skip to content

fix(npcppwebapi): preserve aliased inputs during growth - #9612

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
Ondra97:codex/fix-npcppwebapi-aliased-growth
Oct 29, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
Ondra97:codex/fix-npcppwebapi-aliased-growth

Conversation

@Ondra97

@Ondra97 Ondra97 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

A Common::Vector can receive a reference to one of its own elements. Growing it with realloc may free that element before pushBack reads it. Common::String::append also read an aliased source after freeing its buffer during growth, and used memcpy for overlapping in-place appends.

Capture an intrusive pointer and deep-copy a string before vector growth, releasing the temporary string if growth fails. Append into the new allocation before freeing the old buffer, and use memmove for in-place appends.

The main update preserves the complete current GuestNpCppWebApiIntrusivePtr.cpp test coverage, including TestUserProfileVectorAssign and TestUserProfileModelAndTransaction, alongside the unchanged TestVectorAliasedGrowth regression and its alias-vector dispatch. #8959 and #9015 are both merged into the base.

For the shared test insertion point, #11631 lands first. After it lands, #9612 will incorporate current main, preserve both PRs' tests, and rerun the focused validation. That integration is pending; the results below describe the existing tested head.

The existing host-owned layout is attributed in repository debt to the module bundled with PPSA21564. Console alias handling remains unmeasured.

Tested

Upstream base 8787ae1bb390b97ada6b4304fdb7c2f43318bb47; tested PR head 7f046f0baa7f86c5b2cfbdba8f9c683b47718917. The branch incorporates main through ordinary merge commits.

Windows 11 Pro 10.0.26300, Ryzen 9 7900X, 31.7 GiB RAM, RTX 4070 Ti SUPER; WinLibs MinGW-w64 GCC 15.2.0 r7 UCRT, CMake 4.3.1, Ninja 1.13.2, Release, BUILD_TESTING=ON, pinned bundled dependencies. These tests use the CPU.

  • Built relinker, libs, patched_libraries_tests, guest_np_cpp_web_api_intrusive_ptr_tests, guest_np_cpp_web_api_string_tests, guest_np_cpp_web_api_signed_out_tests and guest_np_cpp_web_api_offline_tests with cmake --build <build> --target <targets> --parallel 8.
  • ctest --test-dir <build> -R '^(guest_np_cpp_web_api.*|patched_libraries)$' -V --output-on-failure --no-tests=error --repeat until-fail:3: 8/8 passed in all three repetitions, zero skips. Each library-load check loaded 368 patched libraries, with zero failures. Append checked 762 cases and 256 growths with zero corrupt results; the vector regressions each checked 500 insertions, with 17 string-vector moves and 12 intrusive-vector moves per run.
  • ctest --test-dir <build> -R '^public_headers($|_compile_)' -j 4 --output-on-failure --no-tests=error: 65/65 passed, zero skips.

Linux evidence and the paired controls are recorded in the exact-head verification run. The workflow checks out 7f046f0baa7f86c5b2cfbdba8f9c683b47718917; its definition is kept on a separate verification branch.

Both GitHub-hosted Linux runners: Ubuntu 24.04.5 LTS x86_64, AMD EPYC 7763, approximately 15 GiB visible RAM, GCC 14.2.0, CMake 3.31.6, Ninja, CPU fixtures. Release uses -DCMAKE_BUILD_TYPE=Release -DANYPS5_ENABLE_ASAN=OFF; ASan uses -DCMAKE_BUILD_TYPE=RelWithDebInfo -DANYPS5_ENABLE_ASAN=ON. Both configurations use BUILD_TESTING=ON and the pinned bundled dependencies. Each passed 7/7 tests in all three repetitions, zero skips. The fixed and restored ASan runs reported no sanitizer errors.

All seven guest_np_cpp_web_api* tests, including the main UserProfile cases and all three aliasing modes, are built explicitly. Each configuration runs ctest --test-dir build -R '^guest_np_cpp_web_api' -V --output-on-failure --no-tests=error --repeat until-fail:3. The paired control keeps the identical tests and replaces only Export.cpp with base 8787ae1bb3, rebuilds the library, and invokes each aliasing test through its registered CTest entry. The fixed source is restored, rebuilt and all seven tests run again.

Test Before (base, ASan) After (fix, ASan)
guest_np_cpp_web_api_alias_append fails: memcpy-param-overlap passes
guest_np_cpp_web_api_alias_string_vector fails: heap-use-after-free passes
guest_np_cpp_web_api_alias_intrusive_vector fails: heap-use-after-free passes

In the matching Linux Release control, append failed, the string-vector test aborted, and the intrusive-vector test passed. All seven tests passed again after restoring the fix in each configuration. These are correctness checks, with no speed claim.

The intrusive-vector Release negative control depends on the allocator and heap layout. A passing uninstrumented control does not prove the original read from freed storage was safe; ASan supplies the memory-error check. This update preserves the original regression without claiming to make its Release failure deterministic.

tools/check_conventions.py --base origin/main, tools/check_main_sync.py --base origin/main --strict, git diff origin/main HEAD --check and tools/check_test_registration.py <build> pass. The Windows registration audit counts 1,447 test executables; the executed runtime checks are listed above.

tools/pr_overlap.py completed. #11631 shares the test-entry insertion context while adding separate UserProfile functionality; it does not implement these aliasing fixes. #11475's uncomparable CMake edit only adds MissingExports.cpp to the library source list, separately from this PR's test registrations. Both were inspected manually. The final main refresh leaves these shared files unchanged.

No full CTest suite, title or console execution is claimed.

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: feat(libSceNpCppWebApi): answer the singular public profile request offline #11631
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux run with and without ASan. The fix passes both ways. Without ASan, the base results on Linux differ from the Windows table in both directions, and one of the two vector regressions passes on base under ctest.

Machine: Linux x86_64, i7-8700, Ubuntu 26.04 (kernel 7.0.0-38), GCC 15.2 / glibc. Head 51ac98e7e merged --no-commit onto main 17f533556 (clean). Release build, plus a separate -DANYPS5_ENABLE_ASAN=ON RelWithDebInfo build of the two test targets.

  • With the PR: the five guest_np_cpp_web_api* tests pass, both in Release and under ASan.

  • Base Export.cpp under ASan: all three new tests abort, each at the line the PR changes:

    • alias_intrusive_vector: heap-use-after-free, READ of size 24 in VectorPushBackIntrusivePtr (Export.cpp:869, elements[body->size] = *element).
    • alias_string_vector: heap-use-after-free, READ of size 32 in VectorPushBackString (Export.cpp:879, the copy after VectorGrow).
    • alias_append: memcpy-param-overlap at Export.cpp:1068 (the in-place path). With only that line switched to memmove, the next report is heap-use-after-free on the growth path: the freed self->buffer is read at line 1068 after the free at line 1064.
  • Base Export.cpp, Release, no ASan (glibc):

    • alias_append fails: 762 cases, 256 growths, 83 corrupt results. On Linux the freed buffer is reused, so the append test does catch it here. On Windows it passed 762/762.
    • alias_string_vector aborts, as on Windows.
    • alias_intrusive_vector passes under ctest (30/30 with --repeat until-fail:30). It aborts at GuestNpCppWebApiIntrusivePtr.cpp:300 every time it is run from build/tests or /tmp. The only difference is the ctest WORKING_DIRECTORY (build/core/libs/prx/libSceNpCppWebApi). The cwd length changes the heap layout, so the stale old elements are still intact when they are read.

    So on Linux, the intrusive-vector regression's red depends on where the binary runs. Under ctest it doesn't detect a revert, and only ASan makes it deterministic.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aliasing fixes are well targeted: VectorPushBackIntrusivePtr captures the element before growth, VectorPushBackString deep-copies before growth and frees the copy on allocation failure, and String::append copies into the new allocation before freeing the old one while using memmove for overlapping in-place sources. The test additions cover all vector source indices over repeated growth and append offsets/boundaries.

Exact-head git diff --check and python -I tools/check_conventions.py --base upstream/main --head HEAD pass. Hosted conventions, eligibility, labels, and progress report pass; product Linux/Windows jobs are skipped. The author reports Windows current-main red/green tests and a full selected suite, and an independent Linux reviewer reports passing release and ASan tests plus useful negative-control analysis. I attempted local exact-head configuration, but CMake generation stops because 3rdparty/vgmstream is uninitialized (atrac9_dec.c is missing), so I did not rerun runtime tests.

Please retain the current Depends on: #8959, #9015 coordination and land/rebase in that order if their shared test setup requires it. The independent Linux report found the intrusive-pointer negative test passes under CTest on base due to working-directory-dependent heap layout, though it fails when run directly and is caught under ASan. Consider making the regression deterministic under the registered CTest invocation before relying on it as a future regression guard. Also keep the final test summary scoped: the stated 1,173 registered executables is a registration count, while the seven selected runtime tests and 367-library load check are the executed checks.

The current-main issue and paired negative controls justify this generic correctness fix despite the general intake hold. Fixes: none and Unblocks: none are appropriate because this is not tied to a title stop.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation and aliasing regressions remain well supported by the Windows red/green report and the independent Linux Release/ASan results; my prior code approval still stands. I refreshed exact head 71138a2642380b6068e4a2a6699c93cf0c1501a3 against current main 3ca07d086d5f44fbe3c00370e7f03953ab46ca1b: whitespace and conventions pass, and the current-main merge tree is clean.

The branch currently includes main only through d6eafa54d0464258574fd2f71889754d05a5bc93; the repository requires keeping branches up to date. Please update it to current main and refresh the focused aliasing/ASan checks on the resulting head. I did not rerun runtime tests locally; hosted full product jobs are skipped. The earlier intake objection is withdrawn; this review request is only for the current-main update and refreshed test record.

@Ondra97
Ondra97 requested a review from MarcZX10 October 22, 2026 14:42
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@Ondra97
Ondra97 force-pushed the codex/fix-npcppwebapi-aliased-growth branch from 1d41d30 to a37880b Compare October 23, 2026 14:56
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 23, 2026
@blawesom

Copy link
Copy Markdown
Contributor

Independent verification of the review ask ("update the branch to current main and refresh the focused aliasing/ASan checks on the resulting head") at a head merged onto current main. I did not modify this branch.

Heads

  • main (local mirror of origin/main) b82c33ef65
  • PR head 0f998c1574 (1391 commits behind that main at fetch)
  • Tested head 2cd8c9ab8e, a local no-conflict merge of PR head 0f998c1574 into main b82c33ef65. git diff --stat main 2cd8c9ab8e:
core/libs/prx/libSceNpCppWebApi/CMakeLists.txt  | 10 ++-
core/libs/prx/libSceNpCppWebApi/Export.cpp      | 14 ++--
core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp| 40 ++++++++++-
core/libs/tests/GuestNpCppWebApiString.cpp      | 90 +++++++++++++++++++++++-
4 files changed, 145 insertions(+), 9 deletions(-)

Still exactly the claimed scope.

Confirmation the bug is live on current main

I checked current main's core/libs/prx/libSceNpCppWebApi/Export.cpp: VectorPushBackString and VectorPushBackIntrusivePtr still assign from *element after VectorGrow, and String::append still memcpys after growth. So the parent-vs-change comparison is a real red/green against the refreshed base, not against the 1391-commit-old one.

Results (same machine, same build, parent vs change)

Arch Linux (kernel 7.2.10-1-cachyos), AMD Ryzen 7 9800X3D, GCC, Ninja, BUILD_TESTING=ON. Two builds: Release, and Debug -DANYPS5_ENABLE_ASAN=ON. The parent binaries were rebuilt from the same tree with only core/libs/prx/libSceNpCppWebApi/Export.cpp restored from main; everything else identical.

Test parent (main's Export.cpp) tested head 2cd8c9ab8e
guest_np_cpp_web_api_string pass pass
guest_np_cpp_web_api_alias_append fail (exit 1) pass
guest_np_cpp_web_api_alias_string_vector fail (exit 134 Release; ASan abort heap-use-after-free) pass
guest_np_cpp_web_api_alias_intrusive_vector fail (exit 134 Release; ASan abort heap-use-after-free) pass

ASan on the parent names two distinct reports, matching the PR's failure class: memcpy-param-overlap in alias-append, and heap-use-after-free in both alias-string-vector and alias-intrusive-vector. I recorded the error signatures, not the full stacks. The parent guest_np_cpp_web_api_string test passes, matching the earlier reports that the base needs an aliasing source to fail.

Change head, 3 consecutive runs each — Release: 3/3 with 4/4 tests passing. ASan: 3/3 with 4/4 tests passing and no ASan report.

Conventions: python3 tools/check_conventions.py --base origin/main passes on the tested head; git diff --check is clean.

Not run: Windows, macOS, a title, or the console side of the alias behaviour.

Note on the ctest inventory

ctest -R 'guest_np_cpp_web_api' on these builds also lists guest_np_cpp_web_api_signed_out and guest_np_cpp_web_api_offline as Not Run (their executables do not exist in my Release/ASan configurations). That is unrelated to this change and the same before and after.

@blawesom

Copy link
Copy Markdown
Contributor

The tested merge head is also published: branch pr-9612-refresh on blawesom/AnyPS5 (head 2cd8c9a), for a rerun without recomputing the merge.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@blawesom Thanks, a red/green against current main with ASan is useful, and the branch saves recomputing the merge. This PR lands after #9015; we'll run CI on the updated head then.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-ran the Release control from my earlier review on the current head 0f998c157. The intrusive-vector gap I reported is still there on Linux. GuestNpCppWebApiIntrusivePtr.cpp's TestVectorAliasedGrowth and the test registration are unchanged since 51ac98e7e.

Machine: Linux x86_64, i7-8700, Ubuntu 26.04, glibc 2.43, GCC 15.2, Release, BUILD_TESTING=ON, full build. Head 0f998c157 merged --no-commit onto main abed03546. The one conflict, in GuestNpCppWebApiIntrusivePtr.cpp, is two independent additions: I kept main's TestUserProfile* functions and the PR's TestVectorAliasedGrowth with main(argc, argv).

  • With the PR: the 7 guest_np_cpp_web_api* tests pass, 3/3.
  • Control, Export.cpp from main with the tests kept:
    • guest_np_cpp_web_api_alias_append fails, and guest_np_cpp_web_api_alias_string_vector aborts.
    • guest_np_cpp_web_api_alias_intrusive_vector passes under ctest, 30/30 with --repeat until-fail:30. The same binary run as ./guest_np_cpp_web_api_intrusive_ptr_tests alias-vector from build/tests aborts 3/3.

@blawesom's Release run on Arch/Ryzen does show this test failing on the parent. Here, under ctest, it doesn't. So in a Release run without ASan, whether reverting the VectorPushBackIntrusivePtr fix gets caught still depends on the host's heap layout. With ASan it is caught every time, in both of our runs.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 27, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aliasing fixes remain well supported: intrusive pointers are captured before growth, strings are copied before growth with cleanup on failure, and overlapping in-place appends use memmove. The author reports repeated Windows tests and an independent Linux Release/ASan red/green run on the tested merge tree. I did not rerun the suite locally; hosted product jobs are skipped.

The current overlap report adds #11631 in GuestNpCppWebApiIntrusivePtr.cpp at the same test insertion point. This PR's checklist still says Depends on: none, and I found no agreement in the thread on which test change lands first. Please coordinate with #11631's author, record the agreed landing order in both checklists, and refresh the affected test file on the resulting base. This follows the repository's overlap rule; the aliasing implementation itself has no new blocker.

@nicolasabosch

Copy link
Copy Markdown
Contributor

@Ondra97 #11631 adds one new test block in GuestNpCppWebApiIntrusivePtr.cpp at the same spot this PR keeps its TestUserProfileVectorAssign / TestUserProfileModelAndTransaction coverage; the library code does not overlap. #11631 is approved by code review with passing hosted checks, and #10925 and #11639 already declare Depends on: #11631, so I propose it lands first and this PR rebases onto it keeping both test blocks (only the test file conflicts). If you would rather go first, say so here and I will switch to Depends on: #9612 and keep both blocks when I merge main.

🤖 Generated with Claude Code

@Ondra97

Ondra97 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

@nicolasabosch Agreed: #11631 can land first. I have recorded Depends on: #11631 in this PR. After it lands, I will update #9612 to main, retain the existing UserProfile coverage, your new tests, and the TestVectorAliasedGrowth regression with its alias-vector dispatch, then rerun the focused tests and conventions on the updated head.

@oneandonlydean
oneandonlydean merged commit daf4eb3 into boykopovar:main Oct 29, 2026
14 checks passed
@Ondra97
Ondra97 deleted the codex/fix-npcppwebapi-aliased-growth branch November 3, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants