Skip to content

fix(tools): stop expanding a helper's branches once one rules it out as a stub - #27805

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
videogamer10293:fix/tools-import-audit-prune-wrappers
Dec 6, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
videogamer10293:fix/tools-import-audit-prune-wrappers

Conversation

@videogamer10293

@videogamer10293 videogamer10293 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

What

On current main, tools/import_audit.py, and so tools/audit_dump.py, stop before they print a report:

FAIL: more than 256 preprocessor branch combinations in one function

The source scan does not depend on the title, so this happens for any title. The function is libkernel's PollDescriptors in Socket/src/Udp.cpp. Since #27260 it calls NotImplemented_nid_no_patch behind a condition. So always_stub_calls counts it among the functions that might reach a stub, and reads every combination of its preprocessor branches before checking any of them. It has 12,288 combinations, and the limit stops the read at 257. #13002 skips helpers that cannot reach a stub at all; this one can, but not in every combination.

A helper is an always-stub helper only if every combination calls, unconditionally, the stub or another such helper. All of those are among the functions that might reach a stub. So once one combination calls none of them, the helper can never qualify, whatever the other combinations are. always_stub_calls now stops reading there and leaves the helper out. Every body of the helper is still parsed first, so an unmatched #if in a later overload still fails the audit.

A helper whose combinations all call such a function is still read in full, and it still fails at the limit (test_relevant_helper_host_branches_keep_the_variant_limit).

Tested

Windows 10 (10.0.19045), Python 3.11. I tested on a078f08ea8 and then rebased onto fffb30ef5b. That main still fails with the error above, and none of its new commits change the audit's files. On it, test_import_audit.py passes, and the real-tree result is identical to the raised-limit result (21,086 stubs, 2,602 partial).

New tests.

  • test_guarded_stub_helper_host_branches_do_not_abort_source_classification: a helper with a guarded stub call, an unconditional call to a function that is not a stub, and 9 #ifdef blocks (512 combinations). The code on main fails it with the error above. With this change, the export that calls the helper is partial. The code on main gives the same answer for the same helper with 7 blocks (128 combinations).
  • test_unmatched_if_in_a_later_helper_overload_still_fails: the first overload stops the read, and the second has an unmatched #if. The audit still fails with #if without a matching #endif, as on main.

Breaking each part (each mutant run against the whole test_import_audit.py; the unchanged commit passes before and after):

Mutant First failing test
no early stop (the code on main) test_guarded_stub_helper_host_branches_do_not_abort_source_classification (the limit error)
stop only at a combination with no calls at all the same test
stop unless the combination calls the stub itself test_unrelated_helper_host_branches_do_not_abort_source_classification
keep the helper after a stop test_guarded_exports_are_partial_and_unconditional_ones_are_stubs
parse later bodies only when they are read test_unmatched_if_in_a_later_helper_overload_still_fails

Same answers on the real tree. source_stub_classes on core/libs/prx, with the code on main and with this change:

Tree Code on main This change
8e5fd06023, the last main before #27260 21,120 stubs, 2,598 partial identical
a078f08ea8 (current main) the error above; with the limit raised to 10^8, 21,115 stubs, 2,599 partial identical to the raised-limit result
ad768c99cc (#27260's head) the error above; with the limit raised, 21,289 stubs, 2,587 partial identical to the raised-limit result

A title. tools/audit_dump.py --windows on PPSA38500 exits 2 with the error above on main, and exits 0 with this change (650 implemented, 41 partial, 0 stub, 0 absent, 190 module imports).

Tools tests. I ran each tools/tests/test_*.py file as its own script, on this change and on a078f08ea8: 161 files. On both sides only test_forced_include_paths.py fails, with exit code 2, because it needs --cmake, --compiler, --generator and --make-program. tools/select_tests.py explain selects 1,214 tests: 870 registered from tools/, 342 entries whose executables are not built in my graph's tree, and 2 replay fixtures. I did not run those 344; this change touches only Python files.

Overlap. tools/pr_overlap.py could not merge #27628 into main. By hand, #27628 changes resolve in import_audit.py and one test near line 3095, not always_stub_calls. It found no overlap with the other open PRs that change these files.

Not covered: Linux and macOS. A helper whose first 256 combinations all call a function that might reach a stub still fails at the limit.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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. Claude Code found the function and the commit, wrote the change and the tests, ran the mutants, the comparisons and the tools tests on my machine, and drafted this description.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved stub-wrapper classification when conditional code branches contain different calls, so wrappers are retained only when every branch meets the classification criteria.
    • Preserved clear error reporting when unmatched conditional directives appear in later helper overloads, without preventing classification of other helpers.
  • Tests
    • Added coverage for helpers with multiple conditional branches, guarded not-implemented calls, unconditional stubs, and unmatched directives.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Stub wrapper audit

Layer / File(s) Summary
Filter variants and validate classification
tools/import_audit.py, tools/tests/test_import_audit.py
The audit checks every generated variant against possible stub calls before retaining a wrapper. Tests cover partial and stub caller classifications, plus an unmatched #ifdef in a later overload.

Suggested reviewers: alexander-src, marczx10

Change: Bug fix

Merge Risk: Minimal · up to b8817

The import audit no longer aborts on libkernel's conditionally compiled helper, and classifications on trees that already completed are reported as unchanged. The change touches only an audit tool and its tests, so merge risk is minimal.

Security architecture risk: Minimal · up to b8817

The import audit can stop checking a helper once a preprocessor variant rules it out as an always-stub wrapper. The change preserves the branch limit for helpers that remain candidates and does not establish a security or failure-containment regression.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Pylint (4.1.4)

Pylint used default settings because repository settings are unavailable in automated reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1b9186c9-2e5d-4c8c-bae8-ca52f0ba0a17
Commits

Reviewing files that changed from the base of the PR and between c916d39 and b8817a6.

Files selected for processing: 1

  • tools/tests/test_import_audit.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@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 — looks good

The change is sound based on the changed implementation and tests read; no checks were run during this review.

What I ran

Read the prepared diff and the pinned head c916d39fa7e9a7172901bade07bbda0e2491d298 using repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and implementation review passed Early stop matches helper classification
Tests not run Author-reported results were not rerun

Findings

Blocking

None.

Non-blocking

None.

Not verified

Did not inspect the rest of tools/import_audit.py or tools/tests/test_import_audit.py; did not run tests or verify the author-reported platform and full-suite results. Both changed files are included in the reviewed manifest and their relevant changed functions and test cases were read. No excluded changed files are listed.

To get this to a good state

Nothing is required.

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

Independent Linux verification at c916d39fa7e9a7172901bade07bbda0e2491d298 with Python 3.14. The import-audit suite completed with 287 passes and one Windows-only skip.

I also ran the classifiers on the same full source tree. The exact parent fffb30ef5b stopped at the 256-combination limit. The PR completed with the default limit and produced 21,086 stub entries, 2,602 marked partial. The complete sorted provider, NID, and name mapping and the partial set were byte-identical to the parent with only its limit raised to 32,768.

@muhmuhkuh

Copy link
Copy Markdown
Contributor

Test report: tools/audit_dump.py on PPSA38500, Windows

Setup

  • Windows 11 (10.0.26200), PPSA38500 01.002.001 (eboot and 2 modules)
  • main 1bc92752d23; then the same tree with this PR's two files (c916d39fa7e9)

Result

Tree Exit Output
main 2 FAIL: more than 256 preprocessor branch combinations in one function
main + this PR 0 952 references, 881 unique imports
Class Imports References
implemented 650 721
partial 41 41
stub 0 0
absent 0 0
module 190 190

The import totals match the ones in the PR description, from a second host. main fdf873a7d2a has the same tools/import_audit.py as 1bc92752d23.

Not checked: test_import_audit.py was not run; Linux and macOS.

AI-assisted test report.

@github-actions

Copy link
Copy Markdown

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 27628 tools/import_audit.py
tools/tests/test_import_audit.py

@videogamer10293

videogamer10293 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

The one failing check, relinker (macos-latest, clang++) (job), fails 1 of 1024 tests: frame_capture_minimize_replay. os.killpg(process.pid, 0) in drain_replay_group raised PermissionError: [Errno 1] Operation not permitted, so the test ended with cannot confirm replay process group cleanup. This PR changes only tools/import_audit.py and its test.

The same failure shows up on other PRs. Of the 20 completed macOS relinker jobs I found on other PRs between 2026-11-27 22:07 and 2026-11-28 01:50 UTC, 12 failed this test with the same error and 8 passed.

#27860 fixes it and is still open. Its body links a native macOS run where the replay tests pass, including the descendant-cleanup case that failed here (test_owned_descendant_is_drained_after_parent_exit). Its own macOS relinker job also passed, but one pass proves little when 8 of the 20 jobs above passed without the fix. I have not run it myself. Once it merges, I'll rebase this branch onto main and push to re-run CI. A maintainer could also re-run the job; I can't.

Not covered: the scan read only the most recent completed build.yml runs, by run creation time. It does not show why the other 8 jobs passed.

AI-assisted: yes. Claude Code read the job log, scanned the other PRs' macOS jobs and drafted this comment.

@akkadbakkad89

Copy link
Copy Markdown
Contributor

Validation datapoint on the real tree (current main 6100e07, linux x86_64): with this PRs tools/import_audit.py, the source scan of the shipped libkernel completes end-to-end — source_stubs finishes with 36 stub entries instead of dying at the 256-combination abort. PollDescriptors is the exploding function here, same as the report in #28157.

One coverage note while comparing approaches: pruning once a counterexample variant appears handles functions whose variants disagree, but a genuinely unconditional thrower wrapped in many independent host branches still needs exhaustive enumeration to prove the throw — that case can still hit the cap. Probably rare enough in practice not to matter; mentioning it only so the limitation is recorded.

Independent of this PR I had prototyped a complementary angle on akkadbakkad89/AnyPS5 branch import-audit-branch-collapse before discovering this one: collapse branch groups whose alternatives are inert for the analysis (no calls, control flow or braces) before enumerating — those cases then stay exact rather than merely surviving — plus a conservative per-function degrade with a loud stderr warning when genuinely distinct variants still overflow. Happy to re-propose it on top of this PR once yours lands, or drop it if you would rather fold the idea in here.

@ycbuzi

ycbuzi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Tested c916d39fa7 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows 11 on ARM64 (GitHub-hosted runner), relinker-only build, WinLibs GCC 15.2.0: 99% tests passed, 16 tests failed out of 1172; 100% tests passed, 0 tests failed out of 2. 16 of the failing tests fail on main too. run
  • Windows Server 2022 (GitHub-hosted runner), relinker-only build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 1172; 100% tests passed, 0 tests failed out of 1172. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@klosixgaming-arch

Copy link
Copy Markdown
Contributor

Tested PR #27805 (c916d39fa7) on Windows against Grand Theft Auto VI (PPSA38500, v01.002.001) relinker registries and the audit unit suite.

Results (c916d39fa7)

  • Static checks: tools/check_conventions.py --base origin/main and tools/debt_fold.py --check pass (0).
  • Unit tests: python -m unittest -v tools/tests/test_import_audit.py -> Ran 288 tests in 16.292s — OK (skipped=5) (exit 0).
  • Real PPSA38500 audit (app.registry.json + libc.prx.guest.prx + libSceNpCppWebApi.prx.guest.prx):
    • Before (origin/main @ 1f27e2abfa): Exited 2 before printing a report:
      FAIL: more than 256 preprocessor branch combinations in one function
      
    • With PR fix(tools): stop expanding a helper's branches once one rules it out as a stub #27805 (c916d39fa7): Completed cleanly (exit 0) and produced the full import report:
      952 references, 881 unique imports
        implemented    650 imports     721 references
        partial         41 imports      41 references
        stub             0 imports       0 references
        absent           0 imports       0 references
        module         190 imports     190 references
      
      with Current stop: none across all three registries (app, libc.prx, libSceNpCppWebApi.prx).

Tested-on: Windows 11, AMD Ryzen 9 9950X3D, Python 3.13 (tools unit suite + PPSA38500 import audit)
AI-assisted: yes. Written with AI assistance and reviewed by me.

…as a stub

always_stub_calls read every preprocessor combination of each function that
might reach NotImplemented_nid_no_patch before checking whether all of them
call a stub. A helper that calls it only behind a condition, with more than
256 combinations, aborted the whole audit, although its first combination
already shows it is not an always-stub helper. Since boykopovar#27260 libkernel's
PollDescriptors is such a helper, so import_audit.py and audit_dump.py stop
for every title.

A combination with no call to a function that might reach a stub can never
be satisfied, so reading stops there and the helper is left out, as the full
read would have decided. Every body is still parsed first, so an unmatched
#if in a later overload still fails the audit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@videogamer10293
videogamer10293 force-pushed the fix/tools-import-audit-prune-wrappers branch from c916d39 to b8817a6 Compare December 3, 2026 02:29

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The import-audit suite ran 288 tests, with 286 passes and two skips. The added cases verify that identifying a stub stops branch expansion for that helper while an unmatched conditional in a subsequent overload still fails the audit.

AI-assisted: yes.

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

I read the diff at b8817a6f73. Windows 11, Python 3.13.7, merged into main at c1593e4e1dd together with other open PRs (so not an isolated tree).

  • tools.tests.test_import_audit: 290 tests pass (3 skipped).
  • Control (only this PR's change to tools/import_audit.py taken out): test_guarded_stub_helper_host_branches_do_not_abort_source_classification fails.

Not checked: the speed over the whole source tree.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 8315b11 into boykopovar:main Dec 6, 2026
12 checks passed
superSniper4576154 pushed a commit to superSniper4576154/AnyPS5 that referenced this pull request Dec 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants