fix(npcppwebapi): preserve aliased inputs during growth #9612
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9612"
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 Ondra97 on 2026-10-22T04:30:22Z.
What
A
Common::Vectorcan receive a reference to one of its own elements. Growing it withreallocmay free that element beforepushBackreads it.Common::String::appendalso read an aliased source after freeing its buffer during growth, and usedmemcpyfor 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
memmovefor in-place appends.The main update preserves the complete current
GuestNpCppWebApiIntrusivePtr.cpptest coverage, includingTestUserProfileVectorAssignandTestUserProfileModelAndTransaction, alongside the unchangedTestVectorAliasedGrowthregression and itsalias-vectordispatch. #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 head7f046f0baa7f86c5b2cfbdba8f9c683b47718917. 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.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_testsandguest_np_cpp_web_api_offline_testswithcmake --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 useBUILD_TESTING=ONand 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 runsctest --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 onlyExport.cppwith base8787ae1bb3, 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.guest_np_cpp_web_api_alias_appendmemcpy-param-overlapguest_np_cpp_web_api_alias_string_vectorheap-use-after-freeguest_np_cpp_web_api_alias_intrusive_vectorheap-use-after-freeIn 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 --checkandtools/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.pycompleted. #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 addsMissingExports.cppto 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
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)Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T04:49:37Z.
Review: COMMENTED
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
51ac98e7emerged--no-commitontomain17f533556(clean). Release build, plus a separate-DANYPS5_ENABLE_ASAN=ONRelWithDebInfo 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.cppunder ASan: all three new tests abort, each at the line the PR changes:alias_intrusive_vector: heap-use-after-free, READ of size 24 inVectorPushBackIntrusivePtr(Export.cpp:869,elements[body->size] = *element).alias_string_vector: heap-use-after-free, READ of size 32 inVectorPushBackString(Export.cpp:879, the copy afterVectorGrow).alias_append:memcpy-param-overlapatExport.cpp:1068(the in-place path). With only that line switched tomemmove, the next report is heap-use-after-free on the growth path: the freedself->bufferis read at line 1068 after thefreeat line 1064.Base
Export.cpp, Release, no ASan (glibc):alias_appendfails: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_vectoraborts, as on Windows.alias_intrusive_vectorpasses under ctest (30/30 with--repeat until-fail:30). It aborts atGuestNpCppWebApiIntrusivePtr.cpp:300every time it is run frombuild/testsor/tmp. The only difference is the ctestWORKING_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.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T09:29:54Z.
Review: APPROVED
The aliasing fixes are well targeted:
VectorPushBackIntrusivePtrcaptures the element before growth,VectorPushBackStringdeep-copies before growth and frees the copy on allocation failure, andString::appendcopies into the new allocation before freeing the old one while usingmemmovefor overlapping in-place sources. The test additions cover all vector source indices over repeated growth and append offsets/boundaries.Exact-head
git diff --checkandpython -I tools/check_conventions.py --base upstream/main --head HEADpass. 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 because3rdparty/vgmstreamis uninitialized (atrac9_dec.cis missing), so I did not rerun runtime tests.Please retain the current
Depends on: #8959, #9015coordination 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: noneandUnblocks: noneare appropriate because this is not tied to a title stop.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T13:15:58Z.
Review: CHANGES_REQUESTED
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
71138a2642380b6068e4a2a6699c93cf0c1501a3against current main3ca07d086d5f44fbe3c00370e7f03953ab46ca1b: 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.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T03:14:25Z.
Review: COMMENTED
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'sTestVectorAliasedGrowthand the test registration are unchanged since51ac98e7e.Machine: Linux x86_64, i7-8700, Ubuntu 26.04, glibc 2.43, GCC 15.2, Release,
BUILD_TESTING=ON, full build. Head0f998c157merged--no-commitontomainabed03546. The one conflict, inGuestNpCppWebApiIntrusivePtr.cpp, is two independent additions: I keptmain'sTestUserProfile*functions and the PR'sTestVectorAliasedGrowthwithmain(argc, argv).guest_np_cpp_web_api*tests pass, 3/3.Export.cppfrommainwith the tests kept:guest_np_cpp_web_api_alias_appendfails, andguest_np_cpp_web_api_alias_string_vectoraborts.guest_np_cpp_web_api_alias_intrusive_vectorpasses under ctest, 30/30 with--repeat until-fail:30. The same binary run as./guest_np_cpp_web_api_intrusive_ptr_tests alias-vectorfrombuild/testsaborts 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
VectorPushBackIntrusivePtrfix 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.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T22:24:21Z.
Review: CHANGES_REQUESTED
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.cppat the same test insertion point. This PR's checklist still saysDepends 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.