Skip to content

fix(libSceNpCppWebApi): count Vector<String> and Vector<unsigned long> references where the bundled module does - #10925

Merged
oneandonlydean merged 8 commits into
boykopovar:mainfrom
AlexFigas:fix/npcppwebapi-vector-reference-counts
Nov 2, 2026
Merged

oneandonlydean merged 8 commits into
boykopovar:mainfrom
AlexFigas:fix/npcppwebapi-vector-reference-counts

Conversation

@AlexFigas

@AlexFigas AlexFigas commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What

libSceNpCppWebApi counted every IntrusivePtr<Vector<T>> reference at offset 0x30 in the pointee, and its pointer/deleter constructor did not count the reference it adopts. The module bundled with PPSA21564 (sce_module/libSceNpCppWebApi.prx, the library's own code) puts the count 0x18 bytes past a first member the size of one element. That is 0x20 for 8-byte elements, 0x30 for IntrusivePtr elements and 0x38 for String elements. Every IntrusivePtr<Vector<T>> destructor, copy constructor and pointer/deleter constructor the module exports agrees.

For Vector<String>, the module's code does the following (addresses in the relinked module, read from the disassembly):

  • Vector<String>::Vector(LibContext*) (0xabc0) zeroes the 32-bit count at 0x38.
  • IntrusivePtr<Vector<String>>::IntrusivePtr(Vector<String>*, deleter, LibContext*) (0x10fc0) stores the three fields and increments the count at 0x38 when the pointer is not null. The copy constructor (0x11010) increments it too.
  • The destructor (0x11140, which jumps to 0x110b0) decrements the count with lock xadd. When the old value was 1, it destroys the object through the deleter.
  • pushBack (0xacf0) keeps begin, the capacity end and end at 0x20, 0x28 and 0x30.

For IntrusivePtr<Vector<unsigned long>>, the destructor (0xe890, which jumps to 0xe7d0) and the assignment (0xe8a0) use 0x20.

PPSA21564's own code creates a Vector<String> with operator new(0x48) and the exported constructor (0x2dbece to 0x2dbeec), so the count lives in title memory that only the constructor initializes. Here, the constructor left those bytes alone and the pointer/deleter constructor did not count the adoption. The first release therefore decremented whatever value was there: it either leaked the vector or called the deleter while references remained.

Changes:

  • The Vector<String> constructor zeroes the count at 0x38.
  • The IntrusivePtr<Vector<String>> pointer/deleter constructor increments the count, and its copy constructor and destructor use 0x38.
  • The IntrusivePtr<Vector<unsigned long>> destructor and assignment use 0x20. The assignment goes through AssignIntrusivePtr, which skips it when both pointers hold the same object, as the module does (cmp qword ptr [rdi], r14; je). Without that, a sole owner assigned to itself ran the deleter and then copied from the cleared pointer, losing the object.
  • Tests:
    • The adopted Vector<String> test starts from the constructor's 0 and checks adoption, copy, release and a null adoption.
    • Destructor cases cover both pointers.
    • A new test covers the Vector<unsigned long> assignment, and the shared assignment test also assigns a sole-owner pointer to itself: the object, count, deleter and context must be unchanged and the deleter must not run.
    • The string test checks that the constructor zeroes the count in a 0x48-byte vector.
  • The IntrusivePtr and Vector debt entries give the offset for each element type.

The IntrusivePtr<Vector<IntrusivePtr<T>>> functions keep 0x30, which the module confirms. The host-side body that pushBack creates is unchanged.

Tested

Ubuntu 24.04 (WSL2), GCC 13.3, Release build with BUILD_TESTING=ON, head b21d67c, which adds the self-assignment guard to b7ec1d9, which merges main cc26ab4 into the previous head 743b6e0 (main f1e340b), and 3fdaafe before it (main da026c7). main now defines the five IntrusivePtr<Vector<String>> and IntrusivePtr<Vector<unsigned long>> exports this PR changes in a new block near the end of Export.cpp, so the merge resolves the conflict by applying this PR's offsets and the adoption count there:

No title run.

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

@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.

Reviewed head f4296894ac390b15c1ff7c75f2ec785a167a5b86. The String-vector constructor initializes the count before pointer adoption increments it; copies/destruction use 0x38, and the unsigned-long vector assignment/destructor use 0x20, matching the offsets and module evidence in the PR. The tests check the affected count slots and deleter behavior. Independently ran strict C++20 syntax checks for the changed implementation and test files, plus conventions and git diff --check; all passed. I did not rerun the runtime suite locally; the author reports the four focused CTests pass. The disclosed same-object assignment behavior remains a separate follow-up.

@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.

Reviewed exact head f429689. The String-vector and unsigned-long-vector reference offsets are separated and applied consistently to construction, copies, assignment, and destruction; the tests check reference changes and deleter behavior. Local strict syntax, conventions, and diff checks passed. The author reports four Linux runtime tests; I did not rerun them locally. The documented self-assignment limitation remains outside this change.

@wachi35676

Copy link
Copy Markdown
Contributor

Windows run of head f4296894ac39 merged onto main 90f918fa7. WinLibs GCC 15.2.0, Release.

  • guest_np_cpp_web_api_intrusive_ptr and guest_np_cpp_web_api_string pass, 2 runs.
  • With main's Export.cpp, both fail (0xc0000409).

AI-assisted: yes (Claude Code); results above were run on this machine.

@maskym

maskym commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 309192e27a, merged into main f759e165d9 (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. Build clean.

The tree also carried the open PRs #11724, #11644, #11639, #11636, #11635, #11630, #11353, #11347, #11221, #11706, #11693, #11686, #11682, all merged cleanly, so the run is a joint one; the tests of this PR are guest_np_cpp_web_api_intrusive_ptr and guest_np_cpp_web_api_string (0.04 s), all passed (the batch's mapped tests: 16/16, plus 43/43 for the Ngs2 and host-import suites). Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Merged main 98ddd9c, which is still current main: head 5c847d9. On this head the 7 np_cpp_web_api tests pass 3 runs each, the intrusive_ptr and string tests abort with main's Export.cpp, and the header audit passes.

The order is unchanged: #11631 lands first, and #12801 follows this PR. Two new PRs from edubox, #12808 and #13255, add debt entries next to the Vector entry this PR rewrites. I asked on both for them to follow this PR.

@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.

Re-checked — new head, changes requested

The reference-count fixes remain consistent with the earlier review, but the current head is behind main and does not include the current head of declared dependency #11631.

What I ran

Reviewed head 5c847d9387cb636e36a2edafe6059f1d782122e3 against PR base 98ddd9c1792bbab016ceed5db267a93d1d61f00a. Current main is 1c6b316cf762c7a8d34623907d62bc1cba2217b6. Environment: Debian 12 x86_64, Python 3.11.2, GCC 12.2.0. The current-main merge preview is clean; no local build or CTest run was performed.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 10925
python3 tools/pr_stack_check.py 10925 --repo boykopovar/AnyPS5 --output-dir /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-10925-5c847d9387cb/stack
git diff --name-status 98ddd9c1792bbab016ceed5db267a93d1d61f00a..1c6b316cf762c7a8d34623907d62bc1cba2217b6 -- core/libs/prx/libSceNpCppWebApi/Export.cpp core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp core/libs/tests/GuestNpCppWebApiString.cpp docs/dev/debt/libSceNpCppWebApi.md
gh pr checks 10925 --repo boykopovar/AnyPS5 --watch=false

Results

Check Result
Diff and conventions Passed
Current-main merge preview Clean, but current main is not an ancestor of this head
Dependency chain #11631 → #10925; #10925 omits current #11631 head, which conflicts with current main
Focused tests Author reports the four WebApi tests plus three alias tests passed three runs each on this head; not rerun locally
Hosted CI Windows, conventions, eligibility, labels and report passed; Linux and specialized jobs skipped
Observed stack and CI output
Target branch tip: `1c6b316cf762c7a8d34623907d62bc1cba2217b6`
Dependency order: #11631 → #10925
PR #11631: conflict against current base; head `04b3dd67f8ea9a03ca205dff4ebc4f088e154f7b`
PR #10925: clean against current base; head `5c847d9387cb636e36a2edafe6059f1d782122e3`
#11631 → #10925: current_dependency_head_not_included
conventions pass
intake pass
labels pass
windows pass
linux skipping
register-context skipping
relinker skipping
relinker-asan skipping
write-watch skipping
eligibility pass
report pass

Negative control

Not applicable to this recheck: no new behavior assertion was added since the earlier exact-head runtime test report.

Findings

Blocking

The branch includes main only through 98ddd9c1792bbab016ceed5db267a93d1d61f00a, while current main is 1c6b316cf762c7a8d34623907d62bc1cba2217b6. CONTRIBUTING.md requires keeping branches up to date. The PR also declares dependency #11631, but its current head is absent and that PR currently conflicts with main. The minimal next step is to resolve/rebase #11631, then refresh this branch on current main with the updated dependency and rerun the focused tests on the resulting head.

Non-blocking

The intervening main commits do not touch this PR's implementation or test paths, and the current-main merge preview is clean. The reported tests are useful evidence for the unchanged code but do not verify the refreshed dependency chain.

Sources

Not verified

No local build or CTest run; author-reported focused tests only. No PS5 console/title behavior. The refreshed dependency stack has not been tested.

To get this to a good state

After #11631 is resolved on current main, rebase this PR on its updated head and current main, then rerun the four focused tests plus the three alias tests three times. I will rerun the same no-build checks and review that exact head.

@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 #11631 still conflicts with main. Its only overlap with this PR was the end of the destructor table in GuestNpCppWebApiIntrusivePtr.cpp, where both appended rows. 65a8e8e moves this PR's two rows up, next to the String destructor, so the two no longer overlap:

So the PR no longer depends on #11631; the checklist says Depends on: none.

Head 3fdaafe also merges main da026c7. On this head the 7 tests pass 3 runs each, the intrusive_ptr and string tests abort with main's Export.cpp, and the header audit passes. #12801, #12808 and #13255 declare Depends on: #10925.

@AlexFigas
AlexFigas requested a review from MarcZX10 October 30, 2026 13:54
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 13206 core/libs/prx/libSceNpCppWebApi/Export.cpp L1148-1150

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 14212 docs/dev/debt/libSceNpCppWebApi.md

@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.

Re-checked — new head, changes requested

The implementation and its reported focused tests remain consistent with the earlier review, but this branch still does not include current main, as required by the repository rules.

What I ran

Reviewed head 3fdaafe5c346ba6640cb2afd0045e8139ac50c9e against PR base da026c70655a132031d45e2008c24f993870d801 on Debian 12 x86_64, Python 3.11.2, GCC 12.2.0. Current main is ed1e0ed08fc2452192df91793a33dd0cc7ad5212; the merge preview is clean.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 10925
git diff --check da026c70655a132031d45e2008c24f993870d801...3fdaafe5c346ba6640cb2afd0045e8139ac50c9e
git merge-tree --write-tree refs/remotes/upstream/main 3fdaafe5c346ba6640cb2afd0045e8139ac50c9e
gh pr checks 10925 --repo boykopovar/AnyPS5

Results

Check Result
Diff check Passed.
Conventions and PR template Passed.
Current-main merge preview Clean; preview tree 9b0c08ba14c5101ae02d5e3417b92a547999c852.
Dependency status Checklist says Depends on: none; #11631 no longer overlaps the moved destructor rows. #11221 is stacked on this PR, and #12801/#12808/#13255 declare this PR as a dependency.
Hosted CI Windows, conventions, labels, eligibility, library portability and report passed. Linux and specialized build jobs were skipped by workflow filters.
Focused C++ tests and header audit The author reports all seven np_cpp_web_api tests pass three runs on this head, with the two directly affected tests aborting against main's original Export.cpp; the 374-library/23,571-export header audit passes. I did not run these locally.
ABI audit and test-quality heuristic Not executable in the isolated checkout because the helper scripts are local-only and absent from this PR's tree.
Stack checker Confirms a clean current-main merge preview, but emits a false-positive self-cycle for #10925.
Selected unedited output
All conventions checks passed.
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-10925-3fdaafe5c346/head-3fdaafe5c346-91a89562/tools/pr_abi_audit.py': [Errno 2] No such file or directory
- PR #10925: Declared dependency cycle involving PRs [10925]
Target branch tip: `ed1e0ed08fc2452192df91793a33dd0cc7ad5212`
Dependency order: #10925
PR #10925: clean against current base; head `3fdaafe5c346ba6640cb2afd0045e8139ac50c9e`
9b0c08ba14c5101ae02d5e3417b92a547999c852

Negative control

The author reports that the two directly affected tests abort when run with the baseline Export.cpp from main. I did not independently run that control or build an exact-head binary.

Findings

Blocking

The PR head contains main only through da026c70655a132031d45e2008c24f993870d801, while current main is ed1e0ed08fc2452192df91793a33dd0cc7ad5212. CONTRIBUTING.md requires keeping the branch up to date. The merge preview is clean and intervening main changes do not touch this PR's files, but the branch remains behind. Please merge current main into this branch and rerun the focused tests on the resulting exact head.

Non-blocking

The stack checker reports a self-cycle despite confirming the PR's checklist has no dependency and the merge preview is clean. Its ABI and test-quality helpers are also unavailable in the isolated checkout.

Sources

Not verified

No local build or CTest run; the focused tests and header audit are author-reported. No PS5 title run.

To get this to a good state

Merge current main into the branch and rerun the seven np_cpp_web_api tests plus the header audit on the resulting exact head. I will repeat the no-build checks and review that head before responding again.

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

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Head b7ec1d9 merges main cc26ab4 and resolves the one conflict that was keeping this PR from merging. Since the previous head, main added the five IntrusivePtr<Vector<String>> and IntrusivePtr<Vector<unsigned long>> exports this PR changes in a new block near the end of Export.cpp, still with the 0x30 offset and without counting the adopted reference. The merge applies this PR's offsets (0x38 and 0x20) and the adoption count there, and leaves the rest of the diff as reviewed. On this head all 9 np_cpp_web_api tests pass 3 runs each, and the intrusive_ptr and string tests abort with main's Export.cpp. The header audit passes (394 libraries, 25128 exports), as do git diff --check and check_conventions.py. #11221 is refreshed on this head.

#14122 and #14136 were opened after this PR and change the same lines (the StringVectorReferenceCountOffset constant and the IntrusivePtr<Vector<String>> release, plus test and debt rows), so neither merges with this head without a conflict. I have not added a dependency here; the order is for their author to agree.

@AlexFigas
AlexFigas requested a review from MarcZX10 November 1, 2026 00:59
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 1, 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.

Reviewed — changes requested

The implementation and reported tests remain consistent with the earlier review, but the open overlap with #14122 and #14136 has no agreed landing order and causes real merge conflicts.

What I ran

Reviewed head b7ec1d9006b704cc84f98391ea566ee92844f68f against base cc26ab44380db3cd7b53715b84f13945227ce28b on Debian 12 x86_64, Python 3.11.2 and GCC 12.2.0. Its preview onto current main (7c4dcf0ad84de7a473ce97bb21644c614f1c3bd3) is clean. The #11221 dependent head 1c163f759c3854e6c8b5c433d8bbbfcc566efd1b includes this head.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 10925
git diff --check cc26ab44380db3cd7b53715b84f13945227ce28b...b7ec1d9006b704cc84f98391ea566ee92844f68f
git merge-tree --write-tree upstream/main refs/anyps5/review/10925
git merge-base --is-ancestor refs/anyps5/review/10925 refs/anyps5/review/11221
git merge-tree --write-tree --name-only refs/anyps5/review/10925 refs/anyps5/review/14122
git merge-tree --write-tree --name-only refs/anyps5/review/10925 refs/anyps5/review/14136
gh pr checks 10925 --repo boykopovar/AnyPS5

Results

Check Result
Diff and current-main preview Passed; preview tree fea8af4f74c18d3141995da878164d239c1b0533.
Stack Passed; #10925 is included in #11221. The local stack helper reports a false-positive self-cycle.
Conventions Hosted check passed.
Pairwise overlap previews Failed with content conflicts against both #14122 and #14136 in Export.cpp, GuestNpCppWebApiIntrusivePtr.cpp, and the debt file.
Hosted CI Windows and labels remain pending; conventions, eligibility, library-portability, and report passed. Linux and specialized jobs were skipped.
Focused tests The author reports all 9 np_cpp_web_api tests pass three runs on this head, with intrusive_ptr and string tests failing against baseline main; I did not run them locally.
Selected unedited merge-preview output
CONFLICT (content): Merge conflict in core/libs/prx/libSceNpCppWebApi/Export.cpp
CONFLICT (content): Merge conflict in core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp
CONFLICT (content): Merge conflict in docs/dev/debt/libSceNpCppWebApi.md
CONFLICT (content): Merge conflict in core/libs/prx/libSceNpCppWebApi/Export.cpp
CONFLICT (content): Merge conflict in core/libs/tests/GuestNpCppWebApiIntrusivePtr.cpp
CONFLICT (content): Merge conflict in docs/dev/debt/libSceNpCppWebApi.md

Findings

Blocking

The live overlap previews conflict with #14122 and #14136 in the implementation, intrusive-pointer tests, and debt file. The PR discussion says the landing order is still for the authors to agree, and the checklist remains Depends on: none. The repository requires overlapping PR authors to agree on order and declare dependencies. Please coordinate the order with the authors of #14122 and #14136 and update the relevant Depends on lines before this stack is ready.

Non-blocking

The head is behind current main, but its current-main preview is clean and main has not changed these PR paths since the base; I am not requesting a rebase for age alone. Windows CI was still pending at this snapshot.

Sources

Not verified

No local build or CTest run. Windows checks are pending; no Linux product run or console run. The reported test results are author-provided.

To get this to a good state

Agree with the owners of #14122 and #14136 which PRs land first, record the order in the affected checklists, then rerun the pairwise merge previews and current main preview. I will repeat these checks on the updated heads.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AlexFigas At b7ec1d9006, the IntrusivePtr<Vector<unsigned long>>::operator= body at Export.cpp:6038-6040 (from commit 022ff47ee1) calls ReleaseIntrusivePtr and then CopyIntrusivePtr unconditionally. When an object is assigned to itself and holds the only reference, the release runs the deleter and clears the pointer, and the copy then reads the cleared source, so the object is lost. We reproduced this under ASan/UBSan: the deleter is called once and the object is not kept.

Could you guard same-object assignment before releasing? The nearby AssignIntrusivePtr helper already checks self->object == source->object. A regression test that assigns a sole-owner scalar-vector pointer to itself and checks that the object, count, deleter and context are unchanged (and the deleter isn't called) would cover it. #12801 builds on this PR and inherits the same body, so fixing it here fixes both.

@AlexFigas

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Thanks, you are right: IntrusivePtr<Vector<unsigned long>>::operator= released before it copied, so a sole owner assigned to itself ran the deleter and then copied from the cleared pointer. Head b21d67c routes it through AssignIntrusivePtr, which returns early when both pointers hold the same object, as the module does. The shared assignment test now also covers Vector<unsigned long>: it assigns a sole-owner pointer to itself and requires the object, count, deleter and context to be unchanged and the deleter not to run. With the unguarded body from b7ec1d9 that case aborts; with the guard all 9 np_cpp_web_api tests pass 3 runs each. #11221 is refreshed on this head (3760ab7) and passes the same 9 tests, and #12801 inherits the fix.

@MarcZX10 On the order with #14122 and #14136: I asked their author to declare Depends on: #10925 (and #11221 for #14136) on both PRs, and offered to go the other way if they prefer. I will record whichever order is agreed here and rerun the pairwise merge previews.

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.

5 participants