fix(tools): audit unscoped Windows imports against searched libraries #17564

Merged
adam25136 merged 2 commits from upstream-pr/17564 into main 2026-11-12 07:12:22 +00:00
adam25136 commented 2026-11-08 22:19:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adam25136 on 2026-11-08T22:19:37Z.


What

On Windows the executable's entry stub searches an import that names no library only in the libraries it loads: the title's converted modules, then the libraries the converted executable lists (the title's host dependencies and its modules'). WindowsImportBuilder::ReadLibraries adds the three kernel libraries when the title needs one of them, and libc.prx when it needs a kernel library or libSceLibcInternal.prx (WindowsEntryStubBuilder.cpp puts every library in the search order when the import has no library). tools/import_audit.py counted every built library that exports the NID instead. A title can therefore pass the audit and stop at load: inFAMOUS Second Son (CUSA00223, a PS4 title) passes on main with no absent import and no current stop, while 47 of its imports are exported only by libSceGnmDriverForNeoMode.prx, which it does not list. The registry gives every import of a PS4 executable an empty library, because the relinker reads an executable's import modules only from tag 0x61000045 (RelinkerPipeline.cpp:321), while a PS4 executable lists them under 0x6100000f, which GuestImageReader.cpp reads for modules. So the library mismatch note does not catch them either. PS5 executables have unscoped imports too: 29 of the 2,079 entries in Planet Zoo's executable registry.

  • With .needed.json companions and PE libraries, an import without a library now counts only the built libraries that stub searches. The title's modules are all searched, as the stub puts every converted module in the order, so the needed-list filter on modules no longer applies on that path.
  • An import left with no provider is absent: it fails the audit and can be the current stop. Its JSON record lists the built libraries that do export it in unsearched_providers; the summary counts them per library under unsearched_providers, and the report names them.
  • Runs where no companion lists a library, --modules ELF imports and Linux builds are classified as before. Their records and summary get an empty unsearched_providers, and the report's absent heading now reads "no built library the loader searches exports these".
  • Imports that name a library are unchanged: one exported only by another library stays a library_mismatch note (a failure with --strict-providers), although for a built library the stub searches that library and libc.prx, plus the kernel family for a kernel library and a title module that provides libc.prx for libSceLibcInternal.prx. If the relinker scoped PS4 imports, the 47 would become such notes.
  • docs/user/USAGE.md: the absent row and a paragraph on the Windows search. It also says that the companions of all registries passed are pooled, so one executable's registries go together, and that without a module's folder in --modules, imports only that module provides are absent.

Tested

Windows 10 Pro 22H2 (19045), Python 3.13.3, on main 0bae53facd; rebased onto 22ae9a397d before opening, whose one new merge changes only core/libs/prx/libSceFios2/src/Diagnostics.cpp, and the tests pass there too:

  • tools/tests/test_import_audit.py: 190 tests run, 2 skipped; main runs 187, 2 skipped. The three new tests fail against main's audit:
    • the first exits 0 instead of 1;
    • the second fails in both libSceLibcInternal cases, where main counts libkernel_sys.prx and libkernel_web.prx as providers;
    • the third errors on the missing unsearched_providers field. There main binds the name to libSceOther.prx, which exports it literally but is not searched, where the stub falls back to the NID in libc.prx.
  • ctest -R '^import_audit$' passes, and so do test_audit_dump.py, test_audit_strict_partial.py and test_job_reference_errors.py.
  • Mutations of import_audit.py, each reverted afterwards, are each caught by the new tests: no kernel family, no libc.prx, no .sprx mapping of the needed names, hosts not filtered, the Windows path turned off, modules still filtered by the needed list on the Windows path, and unsearched_providers kept on a resolved import.
  • inFAMOUS Second Son (CUSA00223, 1.07), registries from relinker --registry --windows on 0bae53facd. The eboot was unwrapped with a local change to tools/unwrap_self.py (it fails on this PS4 eboot with "segment 5 does not fit the file"); --modules was the converted .guest.prx modules, because the unwrapped PS4 modules have no DT_STRTAB or DT_SYMTAB.
    • main: exit 0; 550 implemented, 10 partial, 34 stub, 0 absent; no current stop.
    • This branch: exit 1; 520 implemented, 10 partial, 17 stub, 47 absent, all 47 exported only by libSceGnmDriverForNeoMode.prx; current stop 0BzLGljcwBo (sceGnmDispatchDirect). The 41 libSceFios2 imports now resolve to the title's own module instead of the built libSceFios2.prx, which the converted app.exe does not load because the title's module supplies that library; 28 others lose a .native.prx or libSceAmpr.prx provider and keep a searched one.
    • Started on main 22ae9a397d (this branch rebased, which changes no library) with the libraries built there; the conversion is byte-identical to the 0bae53facd one. It exits with 0xC0000139 after 0.2 s, printing unresolved ELF import for the same 47 NIDs, the first being 0BzLGljcwBo. Its Searched libraries list is the set this change computes from the companions: 33 entries, the title's two modules first, without libSceGnmDriverForNeoMode.prx.
  • Planet Zoo (PPSA18026), which loads on Windows (with #16974), registries from relinker --registry --windows --to-intel on 0bae53facd: main and this branch both exit 0 with the same counts (649 implemented, 7 partial, 1 stub, 0 absent, 268 module) and no class change. 26 imports lose libSceAjm.prx, libSceMsgDialog.prx or libSceSaveDataDialog.prx as a provider and keep the .native.prx library the title lists.
  • python tools/check_conventions.py --base origin/main with this title and description passed.
  • python tools/pr_overlap.py --branch over all 1,979 open pull requests: 45 share a file with this branch, and none of the 32 it compared conflicts with it. It could not compare 13 that conflict with main, all through docs/user/USAGE.md (#5021, #5925, #6924, #7642, #9345, #10193, #12678, #12863, #13928, #13929, #14298, #15169, #15456). On each one's merge base, every hunk they make in that file ends before the "Import audit" section, and none of their changed lines mentions the audit.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5215674520 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17564). Originally posted by [adam25136](https://github.com/adam25136) on 2026-11-08T22:19:37Z. --- ### What On Windows the executable's entry stub searches an import that names no library only in the libraries it loads: the title's converted modules, then the libraries the converted executable lists (the title's host dependencies and its modules'). `WindowsImportBuilder::ReadLibraries` adds the three kernel libraries when the title needs one of them, and `libc.prx` when it needs a kernel library or `libSceLibcInternal.prx` (`WindowsEntryStubBuilder.cpp` puts every library in the search order when the import has no library). `tools/import_audit.py` counted every built library that exports the NID instead. A title can therefore pass the audit and stop at load: inFAMOUS Second Son (CUSA00223, a PS4 title) passes on `main` with no absent import and no current stop, while 47 of its imports are exported only by `libSceGnmDriverForNeoMode.prx`, which it does not list. The registry gives every import of a PS4 executable an empty `library`, because the relinker reads an executable's import modules only from tag `0x61000045` (`RelinkerPipeline.cpp:321`), while a PS4 executable lists them under `0x6100000f`, which `GuestImageReader.cpp` reads for modules. So the library mismatch note does not catch them either. PS5 executables have unscoped imports too: 29 of the 2,079 entries in Planet Zoo's executable registry. - With `.needed.json` companions and PE libraries, an import without a library now counts only the built libraries that stub searches. The title's modules are all searched, as the stub puts every converted module in the order, so the needed-list filter on modules no longer applies on that path. - An import left with no provider is `absent`: it fails the audit and can be the current stop. Its JSON record lists the built libraries that do export it in `unsearched_providers`; the summary counts them per library under `unsearched_providers`, and the report names them. - Runs where no companion lists a library, `--modules` ELF imports and Linux builds are classified as before. Their records and summary get an empty `unsearched_providers`, and the report's absent heading now reads "no built library the loader searches exports these". - Imports that name a library are unchanged: one exported only by another library stays a `library_mismatch` note (a failure with `--strict-providers`), although for a built library the stub searches that library and `libc.prx`, plus the kernel family for a kernel library and a title module that provides `libc.prx` for `libSceLibcInternal.prx`. If the relinker scoped PS4 imports, the 47 would become such notes. - `docs/user/USAGE.md`: the `absent` row and a paragraph on the Windows search. It also says that the companions of all registries passed are pooled, so one executable's registries go together, and that without a module's folder in `--modules`, imports only that module provides are `absent`. ### Tested <!-- 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. --> Windows 10 Pro 22H2 (19045), Python 3.13.3, on `main` `0bae53facd`; rebased onto `22ae9a397d` before opening, whose one new merge changes only `core/libs/prx/libSceFios2/src/Diagnostics.cpp`, and the tests pass there too: - `tools/tests/test_import_audit.py`: 190 tests run, 2 skipped; `main` runs 187, 2 skipped. The three new tests fail against `main`'s audit: - the first exits 0 instead of 1; - the second fails in both `libSceLibcInternal` cases, where `main` counts `libkernel_sys.prx` and `libkernel_web.prx` as providers; - the third errors on the missing `unsearched_providers` field. There `main` binds the name to `libSceOther.prx`, which exports it literally but is not searched, where the stub falls back to the NID in `libc.prx`. - `ctest -R '^import_audit$'` passes, and so do `test_audit_dump.py`, `test_audit_strict_partial.py` and `test_job_reference_errors.py`. - Mutations of `import_audit.py`, each reverted afterwards, are each caught by the new tests: no kernel family, no `libc.prx`, no `.sprx` mapping of the needed names, hosts not filtered, the Windows path turned off, modules still filtered by the needed list on the Windows path, and `unsearched_providers` kept on a resolved import. - inFAMOUS Second Son (CUSA00223, 1.07), registries from `relinker --registry --windows` on `0bae53facd`. The eboot was unwrapped with a local change to `tools/unwrap_self.py` (it fails on this PS4 eboot with "segment 5 does not fit the file"); `--modules` was the converted `.guest.prx` modules, because the unwrapped PS4 modules have no `DT_STRTAB` or `DT_SYMTAB`. - `main`: exit 0; 550 implemented, 10 partial, 34 stub, 0 absent; no current stop. - This branch: exit 1; 520 implemented, 10 partial, 17 stub, 47 absent, all 47 exported only by `libSceGnmDriverForNeoMode.prx`; current stop `0BzLGljcwBo` (`sceGnmDispatchDirect`). The 41 `libSceFios2` imports now resolve to the title's own module instead of the built `libSceFios2.prx`, which the converted `app.exe` does not load because the title's module supplies that library; 28 others lose a `.native.prx` or `libSceAmpr.prx` provider and keep a searched one. - Started on `main` `22ae9a397d` (this branch rebased, which changes no library) with the libraries built there; the conversion is byte-identical to the `0bae53facd` one. It exits with `0xC0000139` after 0.2 s, printing `unresolved ELF import` for the same 47 NIDs, the first being `0BzLGljcwBo`. Its `Searched libraries` list is the set this change computes from the companions: 33 entries, the title's two modules first, without `libSceGnmDriverForNeoMode.prx`. - Planet Zoo (PPSA18026), which loads on Windows (with #16974), registries from `relinker --registry --windows --to-intel` on `0bae53facd`: `main` and this branch both exit 0 with the same counts (649 implemented, 7 partial, 1 stub, 0 absent, 268 module) and no class change. 26 imports lose `libSceAjm.prx`, `libSceMsgDialog.prx` or `libSceSaveDataDialog.prx` as a provider and keep the `.native.prx` library the title lists. - `python tools/check_conventions.py --base origin/main` with this title and description passed. - `python tools/pr_overlap.py --branch` over all 1,979 open pull requests: 45 share a file with this branch, and none of the 32 it compared conflicts with it. It could not compare 13 that conflict with `main`, all through `docs/user/USAGE.md` (#5021, #5925, #6924, #7642, #9345, #10193, #12678, #12863, #13928, #13929, #14298, #15169, #15456). On each one's merge base, every hunk they make in that file ends before the "Import audit" section, and none of their changed lines mentions the audit. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!17564
No description provided.