Skip to content

fix(libkernel): provide default arguments block for DT_INIT on Linux - #6149

Open
Nubi-Prey wants to merge 1 commit into
boykopovar:mainfrom
Nubi-Prey:fix/libkernel-default-module-args
Open

Nubi-Prey wants to merge 1 commit into
boykopovar:mainfrom
Nubi-Prey:fix/libkernel-default-module-args

Conversation

@Nubi-Prey

@Nubi-Prey Nubi-Prey commented Oct 14, 2026 •

Copy link
Copy Markdown

What

  • Extract pending guest module argument state into core/libs/prx/libkernel/Module/src/ModuleArgs.cpp and ModuleArgs.hpp (SetPendingModuleArgs_nid_no_patch, GetPendingModuleInitResult_nid_no_patch, SetModuleInitResultObserverForTest_nid_no_patch) and link it into both libkernel and guest_module_args_host so the relinker test exercises the production implementation directly.
  • Track whether an explicit sceKernelLoadStartModule call is active via thread_local bool explicitLoadInProgress (with exception-safe reset around dlopen_nid_postfix in Module.cpp), preserving explicit (args, nullptr) pairs when explicitLoadInProgress is true and returning a 16-byte default block {size: 0x10, version: 0x200, context: nullptr} (args = 16) only when explicitLoadInProgress is false.
  • Document the unmeasured console semantics in docs/dev/debt/libkernel.md: what the console passes outside sceKernelLoadStartModule or to DT_NEEDED dependencies pulled in during sceKernelLoadStartModule, that {0x10, 0x200} is a guess from Linux: Cult of the Lamb (PPSA06464) faults at process start in UnifyPS5Plugin.prx, a needed library started with a null arguments block #5621 while context is unknown (nullptr faults if called during init), and that explicitLoadInProgress remains true across dlopen_nid_postfix.

Tested

  • Linux x86_64 (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), rebased on main (474030e584): built relinker and guest_module_args_host and ran ctest --test-dir build-relinker -R "guest_module_args|guest_deferred_dependencies" --output-on-failure -V (2/2 passed: load_and_record, load_and_record_default, load_and_record_explicit_null, and guest_deferred_dependencies).
  • Negative controls verified and reverted:
    1. Mutating SceKernelDefaultModuleArgs::version to 0x201 in ModuleArgs.hpp fails guest_module_args with Guest module argument fixture: Default arguments corruption.
    2. Falling back to kEmptyPendingArgs when pendingModuleArgs.argp == nullptr in ModuleArgs.cpp fails load_and_record_explicit_null in guest_module_args.
  • python3 tools/check_conventions.py --base upstream/main passes with 0 warnings and 0 errors.
  • Not tested with a commercial title in this environment.

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 (Gemini 4 Argon)

@simonbbby

Copy link
Copy Markdown
Contributor

Independent Linux check at exact head 950bc7c323a44bac2f83781e57d968cf016e932f (Ubuntu 24.04 / Ryzen 9 9950X / GCC 13.3 / Ninja Release).

The relinker-only suite passes 165/165 (28.88 s), and the real production getter returns the proposed default block in a separate native probe. There is a gap in the new regression, though: it remains green with the runtime fix removed, and also with a null default pointer in its own provider.

Controls, rebuilt and run with ctest -R '^guest_module_args$' -V:

Change from the stated head Result
None pass
Restore only production Module.cpp from this head's parent pass
Provider kEmptyPending{sizeof(kDefaultArgs), nullptr} pass
Provider kEmptyPending{8, &kDefaultArgs} fail: the Python size assertion
Null-pointer provider plus the assertion change below fail: Missing default argument block
Correct provider plus the assertion change below pass

The size mutation confirms the fixture ran and rebuilt. The two false greens have separate causes: guest_module_args_host.prx supplies its own __aps5_get_pending_module_args_nid_no_patch, so it never calls the changed production helper; and RecordArgs checks the payload only if argp == &kDefaultArgs. A null pointer skips that check, while Python asserts the size and initializer result but not the pointer. The fixture's initializer only calls RecordArgs, so it does not dereference the null pointer as the reported Unity initializer does.

This small test-only change catches the null pointer and also checks context:

--- a/core/relinker/elfpatcher/tests/GuestModuleArgsHost.cpp
+++ b/core/relinker/elfpatcher/tests/GuestModuleArgsHost.cpp
@@ -48,9 +48,10 @@
     std::snprintf(line, sizeof(line), "args=%016zx argp=%016zx\n", args,
                   static_cast<std::size_t>(reinterpret_cast<std::uintptr_t>(argp)));
     Write(line);
-    if (args == sizeof(DefaultModuleArgs) && argp == &kDefaultArgs) {
+    if (args == sizeof(DefaultModuleArgs)) {
+        if (argp == nullptr) Fail("Missing default argument block");
         const auto* defaultArgs = static_cast<const DefaultModuleArgs*>(argp);
-        if (defaultArgs->size != 0x10 || defaultArgs->version != 0x200) Fail("Default arguments corruption");
+        if (defaultArgs->size != 0x10 || defaultArgs->version != 0x200 || defaultArgs->context != nullptr) Fail("Default arguments corruption");
     }
 }
 

It still needs a regression that calls the real runtime getter. I checked that independently without the full SDL build: compile the actual core/libs/prx/libkernel/Module/src/Module.cpp with -std=c++20 -O2 -ffunction-sections -fdata-sections -Icore/libs, then link a small getter probe with -Wl,--gc-sections to discard unrelated unused runtime functions. The probe casts the getter result to PendingModuleArgs, requires args == 16 and non-null argp, then checks the payload's size 0x10, version 0x200 and null context. The PR production object passes; its parent's production object exits 1 with a missing/null default block. This links the actual compiled helper, rather than a replacement getter.

All temporary source edits were restored, the stock provider rebuilt, and guest_module_args passes again with a clean checkout. No title run, full-runtime load, or claim that this default block matches console behavior.

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

macOS run of the new --load-default path, which has its own darwin branch (RTLD_GLOBAL) that the Linux runs don't cover.

Machine: Apple M2 Max, macOS 27.2, Xcode 27 toolchain, relinker-only Release.
Head: 950bc7c32, merged onto main @ ab9a47684 (clean).

The test only executes natively on macOS when the interpreter is x86_64 (platform.machine() == 'x86_64'). Under ctest on Apple silicon, both the arm64 build and the x86_64 build print native execution skipped, because /usr/bin/python3 starts as arm64. So I ran it by hand from the x86_64 build:

arch -x86_64 /usr/bin/python3 core/relinker/relinker/tests/test_guest_module_args.py <build>/core/relinker/relinker <build>/core/relinker/guest_module_args_host.prx
Change Result (x86_64 under Rosetta, Python 3.9.6)
None pass
Fixture provider GuestModuleArgsHost.cpp restored from main fail: args=0000000000000000 argp=0000000000000000
--load-default with DEFAULT_MODE instead of RTLD_GLOBAL fail: dlopen(...): symbol not found in flat namespace '_RecordArgs'

So the darwin branch works and the RTLD_GLOBAL is required there. As @simonbbby showed on Linux, what the test checks is the fixture's own provider, not the production __aps5_get_pending_module_args_nid_no_patch in Module.cpp. That is the same on macOS: Module.cpp is not part of the relinker-only build.

Full relinker-only suite with the PR: arm64 163/163; x86_64 + Python 3.9.6 159/163, with the same 4 failures as main.

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

@Nubi-Prey

Nubi-Prey commented Oct 15, 2026 •

Copy link
Copy Markdown
Author

Thanks @jotitadev and @simonbbby for the thorough independent validation on macOS and Linux!

Carried the suggested mutation control in commit 20e36060f:

  • In GuestModuleArgsHost.cpp: fail with "Missing default argument block" if argp == nullptr when args == sizeof(DefaultModuleArgs), and check context == nullptr.
  • In test_guest_module_args.py: assert that the recorded argp is not null.

Verified locally with a negative mutation control (setting nullptr fails CTest immediately with the expected error) and all 165 CTests and conventions pass on a clean checkout.

Written with AI assistance (Gemini).

This was referenced Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

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 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7173 core/relinker/CMakeLists.txt
PR 7447 docs/dev/debt/libkernel.md
PR 7531 core/relinker/CMakeLists.txt
PR 8041 core/libs/prx/libkernel/CMakeLists.txt
PR 8605 docs/dev/debt/libkernel.md
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 10558 docs/dev/debt/libkernel.md
PR 10645 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 10792 core/relinker/CMakeLists.txt
PR 10861 core/relinker/CMakeLists.txt
PR 11215 docs/dev/debt/libkernel.md
PR 11628 core/relinker/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11661 core/relinker/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11689 core/relinker/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11698 core/relinker/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 12544 core/libs/prx/libkernel/CMakeLists.txt
PR 12678 core/relinker/CMakeLists.txt
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
PR 13486 core/relinker/CMakeLists.txt
PR 13716 core/relinker/CMakeLists.txt
PR 14103 docs/dev/debt/libkernel.md
PR 14108 docs/dev/debt/libkernel.md
PR 15037 docs/dev/debt/libkernel.md
PR 15040 docs/dev/debt/libkernel.md
PR 15245 core/relinker/CMakeLists.txt
PR 15642 core/relinker/CMakeLists.txt
PR 16372 core/libs/prx/libkernel/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 16826 docs/dev/debt/libkernel.md
PR 17438 core/libs/prx/libkernel/CMakeLists.txt
PR 17439 core/libs/prx/libkernel/CMakeLists.txt
PR 17463 core/libs/prx/libkernel/CMakeLists.txt
PR 17477 core/libs/prx/libkernel/CMakeLists.txt
PR 17484 core/libs/prx/libkernel/CMakeLists.txt
PR 17489 core/libs/prx/libkernel/CMakeLists.txt
PR 17493 core/libs/prx/libkernel/CMakeLists.txt
PR 17513 core/libs/prx/libkernel/CMakeLists.txt
PR 17521 core/libs/prx/libkernel/CMakeLists.txt
PR 17557 core/relinker/CMakeLists.txt
PR 17558 core/relinker/CMakeLists.txt
PR 17638 docs/dev/debt/libkernel.md
PR 18112 core/libs/prx/libkernel/CMakeLists.txt
PR 18190 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Module/src/Module.cpp
PR 18197 docs/dev/debt/libkernel.md
PR 18293 core/libs/prx/libkernel/CMakeLists.txt
PR 18324 docs/dev/debt/libkernel.md
PR 18373 core/relinker/CMakeLists.txt
PR 18449 core/relinker/CMakeLists.txt
PR 18462 docs/dev/debt/libkernel.md
PR 19181 core/libs/prx/libkernel/CMakeLists.txt
PR 19433 docs/dev/debt/libkernel.md
PR 19567 core/relinker/CMakeLists.txt
PR 20021 docs/dev/debt/libkernel.md
PR 20161 docs/dev/debt/libkernel.md
PR 20349 docs/dev/debt/libkernel.md
PR 20865 core/libs/prx/libkernel/CMakeLists.txt
PR 20899 core/libs/prx/libkernel/CMakeLists.txt
PR 20900 core/relinker/CMakeLists.txt
PR 20917 core/relinker/CMakeLists.txt
PR 20957 core/libs/prx/libkernel/CMakeLists.txt
PR 21020 core/libs/prx/libkernel/CMakeLists.txt
PR 21022 core/libs/prx/libkernel/CMakeLists.txt
PR 21024 core/libs/prx/libkernel/CMakeLists.txt
PR 21026 core/libs/prx/libkernel/CMakeLists.txt
PR 21041 core/libs/prx/libkernel/CMakeLists.txt
PR 21051 core/libs/prx/libkernel/CMakeLists.txt
PR 21058 core/libs/prx/libkernel/CMakeLists.txt
PR 21119 core/libs/prx/libkernel/CMakeLists.txt
PR 21132 core/libs/prx/libkernel/CMakeLists.txt
PR 21135 core/libs/prx/libkernel/CMakeLists.txt
PR 21604 core/libs/prx/libkernel/CMakeLists.txt
PR 21619 core/libs/prx/libkernel/CMakeLists.txt
PR 21625 core/libs/prx/libkernel/CMakeLists.txt
PR 21626 core/libs/prx/libkernel/CMakeLists.txt
PR 21647 core/libs/prx/libkernel/CMakeLists.txt
PR 21655 core/libs/prx/libkernel/CMakeLists.txt
PR 21663 core/libs/prx/libkernel/CMakeLists.txt
PR 21672 core/libs/prx/libkernel/CMakeLists.txt
PR 21728 core/relinker/CMakeLists.txt
PR 21877 core/relinker/CMakeLists.txt
PR 22391 core/libs/prx/libkernel/CMakeLists.txt
PR 22392 core/libs/prx/libkernel/Module/src/Module.cpp
PR 22466 core/relinker/CMakeLists.txt
PR 22479 core/libs/prx/libkernel/CMakeLists.txt
PR 22483 core/libs/prx/libkernel/CMakeLists.txt
PR 22839 core/libs/prx/libkernel/Module/src/Module.cpp
PR 23465 core/relinker/CMakeLists.txt
PR 23555 core/libs/prx/libkernel/CMakeLists.txt
PR 23636 core/relinker/CMakeLists.txt
PR 23663 core/relinker/CMakeLists.txt
PR 23747 core/relinker/CMakeLists.txt
PR 23781 core/relinker/CMakeLists.txt
PR 23836 core/relinker/CMakeLists.txt
PR 23842 core/relinker/CMakeLists.txt
PR 24062 core/relinker/CMakeLists.txt
PR 24166 core/libs/prx/libkernel/CMakeLists.txt
PR 24322 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 24343 core/libs/prx/libkernel/CMakeLists.txt
PR 24472 core/relinker/CMakeLists.txt
PR 24819 core/libs/prx/libkernel/Module/src/Module.cpp
PR 24826 core/libs/prx/libkernel/CMakeLists.txt
PR 24910 core/libs/prx/libkernel/CMakeLists.txt
PR 24915 core/relinker/CMakeLists.txt
PR 24917 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 24924 core/libs/prx/libkernel/CMakeLists.txt
PR 25483 core/relinker/CMakeLists.txt
PR 25491 core/libs/prx/libkernel/CMakeLists.txt
PR 25672 core/relinker/CMakeLists.txt
PR 25949 core/libs/prx/libkernel/CMakeLists.txt
PR 25960 core/libs/prx/libkernel/Module/src/Module.cpp
PR 26174 core/libs/prx/libkernel/CMakeLists.txt
PR 26206 core/libs/prx/libkernel/CMakeLists.txt
PR 26232 core/relinker/CMakeLists.txt
PR 26633 core/relinker/CMakeLists.txt
PR 26695 core/libs/prx/libkernel/CMakeLists.txt
PR 26952 core/libs/prx/libkernel/Module/src/Module.cpp

@github-actions github-actions Bot removed needs rebase Conflicts with main, rebase needed fix error Closes an issue (Fixes #N) labels Oct 31, 2026
@fogydes

fogydes commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Correction: In the previous review (at head d8be19d), the checklist was missing the mandatory Fixes: line. The updated head a407a5e cleanly merges with current origin/main (git merge-tree exits 0, clean tree 2b03619e7d8f5d3dd8d0679c9c051214d668beda), adds - [x] Fixes: none to the checklist, passes check_conventions.py with 0 findings, and maintains a clean single-commit history.

OK

Findings

  1. [verified] Branch freshness & clean merge: git merge-tree --write-tree origin/main a407a5ef836405798b7e248b876acd66aa3f39e7 exits with code 0 (clean tree 2b03619e7d8f5d3dd8d0679c9c051214d668beda). The branch is 1,048 commits behind origin/main and merges without conflict.
  2. [verified] Conventions: python tools/check_conventions.py --base 474030e58424fefdb65e5df9f3820810fbb4553c executed in an isolated scratch worktree and passed cleanly with 0 errors and 0 warnings (All conventions checks passed.).
  3. [verified] Commit hygiene & zero comments: History consists of a single linear Conventional Commit (a407a5ef836). Zero code comments added across all source and test files.
  4. [read-only] PR template checklist: The checklist is fully filled out and compliant, including - [x] Fixes: none, resolving the previous review finding.
  5. [read-only] Technical debt: Unmeasured console semantics and known gaps ({0x10, 0x200} guess and unknown context pointer) are cleanly documented in docs/dev/debt/libkernel.md.
  6. [author-reported] Test claim: Author reports passing Linux x86_64 CTest suite (guest_module_args and guest_deferred_dependencies, 2/2 passed) and verified negative controls; test execution not independently run in this review session.

Freshness Envelope

  • Head SHA: a407a5ef836405798b7e248b876acd66aa3f39e7
  • Base SHA: 474030e58424fefdb65e5df9f3820810fbb4553c
  • Commits behind origin/main: 1,048
  • git merge-tree with origin/main: exit 0 (clean tree 2b03619e7d8f5d3dd8d0679c9c051214d668beda)

Cross-PR Ledger

  • No other open PRs currently modify core/libs/prx/libkernel/Module/src/Module.cpp or core/libs/prx/libkernel/Module/src/ModuleArgs.cpp.

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

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

This pull request does not merge cleanly into main (4f41f4c): head a407a5e conflicts in

  • core/relinker/elfpatcher/tests/GuestModuleArgsHost.cpp

Please rebase it onto current main. I will review the change once it merges cleanly.

@Aryansh1520

Copy link
Copy Markdown
Contributor

We hit the same problem while running Sonic Superstars (PPSA06888) on Linux, and this PR's default argument block fixes it.

What we saw without it: a guest module started through dlopen (SaveData.prx, a Unity plugin) receives a null argp, and its init reads the block before checking the size, so the process faults inside the module's init code. With a zeroed 16-byte argument block in place of the null pointer, the module gets past that point and the title keeps going (save-data files open and the game reaches its in-game level).

The code we tested was a smaller version of the same idea (a zeroed block returned from __aps5_get_pending_module_args_nid_no_patch when argp is null). We are not opening a second PR for it, since this PR covers it and goes further. Your fix is the one to land.

Two things we observed that may matter for this PR:

  • Unity modules are loaded by path from app0/Media/Modules and app0/Media/Plugins. They need to be relinked with --module-dir for those paths, otherwise the load fails with 0x80020002.
  • The explicitLoadInProgress gate means an implicit dlopen gets the default block, which matches what we needed.

If it helps, we can test a rebased version of this PR against the title on our side.

@github-actions github-actions Bot added the build label Nov 11, 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 — no blocking findings

The reviewed changes look consistent with the stated behavior, but I could not run the reported checks in this session, so a formal approval is not justified.

What I ran

Reviewed head a407a5ef836405798b7e248b876acd66aa3f39e7. I read every changed line in all 8 changed files using the review reader; no checks were run in this session.

Results

Check Result Note
Diff review passed All changed lines read
Reported Linux tests not run Reported as passing by the author
Convention check not run Reported as passing by the author
Console behavior not run Commercial title not tested

Negative control

Not run in this session. The description reports two negative controls that were verified and reverted.

Findings

Blocking

None.

Non-blocking

None.

Sources

Reviewed core/libs/prx/libkernel/CMakeLists.txt, core/libs/prx/libkernel/Module/ModuleArgs.hpp, core/libs/prx/libkernel/Module/src/Module.cpp, core/libs/prx/libkernel/Module/src/ModuleArgs.cpp, core/relinker/CMakeLists.txt, core/relinker/elfpatcher/tests/GuestModuleArgsHost.cpp, core/relinker/relinker/tests/test_guest_module_args.py, and docs/dev/debt/libkernel.md at the pinned head. All were included in the review folder and their changed lines were read.

Not verified

The author-reported test and convention command results were not independently confirmed. Commercial title and console behavior remain unverified.

To get this to a good state

Nothing is required. Optional: independently run the reported Linux tests and convention check before merge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants