fix(libkernel): check the module load and unload arguments as firmware 13.42 does #15556

Merged
BunnyFl merged 3 commits from upstream-pr/15556 into main 2026-11-07 20:46:22 +00:00
BunnyFl commented 2026-11-03 18:48:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-03T18:48:24Z.


What

sceKernelLoadStartModule and sceKernelStopUnloadModule ignored their flags and opt arguments, and sceKernelStopUnloadModule treated handle 0 like any unknown handle. Firmware 13.42 refuses these with 0x80020016 (EINVAL) before it does anything else, and this change does the same:

  • sceKernelLoadStartModule: nonzero flags or a non-null opt returns 0x80020016, and res is left alone.
  • sceKernelStopUnloadModule: nonzero flags, a non-null opt or handle 0 returns 0x80020016, and res is left alone.
  • sceKernelLoadStartModuleForSysmodule forwards flags as 0. The firmware export adds 0x10000 to flags and jumps past the public entry's flags check, so a sysmodule load with flags still loads. A non-null opt is refused on both paths.

Firmware 13.42 system/common/lib/libkernel_sys.sprx (sha256 3d361753f6c472cd5442bf92652b4a1dee04051297a0155fe197ede0c2002d4a), read from disassembly, not run on a console:

sceKernelLoadStartModule (wzvqT4UqKX8), 0x35c70
35c70: test %ecx,%ecx            ; flags
35c72: je   0x35c7a
35c74: mov  $0x80020016,%eax
35c7a: xor  %ecx,%ecx
35c7c: jmp  0x35c90              ; shared body

sceKernelLoadStartModuleForSysmodule (Gg3+yWL6DWU), 0x363e0
363e0: or   $0x10000,%ecx
363e6: jmp  0x35c90

shared body, 0x35c90
35ca4: mov  $0x80020016,%eax
35ca9: test %r8,%r8              ; opt
35cac: jne  0x35e0a              ; return

sceKernelStopUnloadModule (QKd0qM58Qes), 0x36400
36411: test %ecx,%ecx            ; flags
36415: mov  $0x80020016,%eax
3641d: test %r8,%r8              ; opt
36427: jne  0x367ea              ; return
3642d: test %ebx,%ebx            ; handle
3642f: je   0x364db              ; 0x80020016 at 0x36502

The debt file gains the gap this doesn't close. sceKernelStopUnloadModule never runs the module's module_stop and stores 0 in res. At 0x36657 firmware calls module_stop with args and argp and stores its result in res (0x36663). It skips that call while a count at +0x6c is above 1 (0x364b1–0x36557), which reads like a load count. It returns 0x80020010 on a path (0x3648d–0x36583) that appears to cover another thread still starting the module. The loader reads a start entry and initializers from the guest lifecycle metadata (core/libs/prx/libkernel/Module/src/DynamicLoader.cpp:221 and :318) but no stop entry, so a follow-up needs the relinker to record one. On Windows it also reads the metadata's dependency and sweep name lists (DeferredListNames, core/libs/prx/libkernel/Module/src/DynamicLoader.cpp:271).

Tested

  • Windows 11 Pro 10.0.26300, WinLibs GCC, Release, head d164e44244 (main 5f2e6336ed merged in): guest_dynamic_loader, guest_kernel_sys_module_internal and the eight guest_deferred_module_* tests pass (10 of 10, none Not Run), with their three test executables rebuilt at this head.
  • New rows in core/libs/tests/GuestDynamicLoader.cpp load the test system module with flags 1 and with an opt block (refused, res untouched), then load it, refuse to unload it with flags 1, with an opt block and as handle 0, call into it, and unload it. New rows in core/libs/tests/GuestKernelSysModuleInternal.cpp check that sceKernelLoadStartModuleForSysmodule with flags 0x10000 still reaches the missing-file error, and that an opt block is refused.
  • Negative control at this head: both tests built against main's core/libs/prx/libkernel/Module/src/Module.cpp and core/libs/prx/libkernel_sys/Export.cpp abort (0xc0000409, 2 of 2 failed; guest_dynamic_loader stops at GuestDynamicLoader.cpp:209, the flags row).
  • guest_runtime_lifecycle and guest_dynamic_loader_fixed_base_tests are registered on Linux only, and I didn't run them. Both pass flags 0 and a null opt.
  • tools/check_conventions.py --base upstream/main on the committed head d164e44244: all checks pass.
  • The merge of main 5f2e6336ed conflicted once, in docs/dev/debt/libkernel_sys.md: main reworded the scePthreadMutexIsowned, utrace and tcsetsid entry below this branch's new sysmodule flags entry. That entry takes main's wording and the flags entry is unchanged.
    • Correction: the first head, 5ee657294b, had a debt-length warning (the libkernel_sys.md module info entry reached 1186 characters). I had run the check before committing, and it reads committed Git objects (CONTRIBUTING.md, "reads pull request Git objects"), so it didn't see the debt change. The second commit moves the sysmodule flags note into its own entry, which fixes it.
  • tools/pr_overlap.py --branch, run at the earlier head caca1e4031: the code-file overlaps are #13486, #15037, #15115, #5021, #6149, #9097 and #9389. In their diffs only #6149 names one of these functions, in a debt line about Linux DT_INIT arguments, and none changes them. The many debt-file overlaps are other entries.
  • No title was 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5142744288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15556). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-03T18:48:24Z. --- ### What `sceKernelLoadStartModule` and `sceKernelStopUnloadModule` ignored their `flags` and `opt` arguments, and `sceKernelStopUnloadModule` treated handle 0 like any unknown handle. Firmware 13.42 refuses these with `0x80020016` (EINVAL) before it does anything else, and this change does the same: - `sceKernelLoadStartModule`: nonzero `flags` or a non-null `opt` returns `0x80020016`, and `res` is left alone. - `sceKernelStopUnloadModule`: nonzero `flags`, a non-null `opt` or handle 0 returns `0x80020016`, and `res` is left alone. - `sceKernelLoadStartModuleForSysmodule` forwards `flags` as 0. The firmware export adds `0x10000` to `flags` and jumps past the public entry's `flags` check, so a sysmodule load with flags still loads. A non-null `opt` is refused on both paths. Firmware 13.42 `system/common/lib/libkernel_sys.sprx` (sha256 `3d361753f6c472cd5442bf92652b4a1dee04051297a0155fe197ede0c2002d4a`), read from disassembly, not run on a console: ``` sceKernelLoadStartModule (wzvqT4UqKX8), 0x35c70 35c70: test %ecx,%ecx ; flags 35c72: je 0x35c7a 35c74: mov $0x80020016,%eax 35c7a: xor %ecx,%ecx 35c7c: jmp 0x35c90 ; shared body sceKernelLoadStartModuleForSysmodule (Gg3+yWL6DWU), 0x363e0 363e0: or $0x10000,%ecx 363e6: jmp 0x35c90 shared body, 0x35c90 35ca4: mov $0x80020016,%eax 35ca9: test %r8,%r8 ; opt 35cac: jne 0x35e0a ; return sceKernelStopUnloadModule (QKd0qM58Qes), 0x36400 36411: test %ecx,%ecx ; flags 36415: mov $0x80020016,%eax 3641d: test %r8,%r8 ; opt 36427: jne 0x367ea ; return 3642d: test %ebx,%ebx ; handle 3642f: je 0x364db ; 0x80020016 at 0x36502 ``` The debt file gains the gap this doesn't close. `sceKernelStopUnloadModule` never runs the module's `module_stop` and stores 0 in `res`. At `0x36657` firmware calls `module_stop` with `args` and `argp` and stores its result in `res` (`0x36663`). It skips that call while a count at `+0x6c` is above 1 (`0x364b1`–`0x36557`), which reads like a load count. It returns `0x80020010` on a path (`0x3648d`–`0x36583`) that appears to cover another thread still starting the module. The loader reads a start entry and initializers from the guest lifecycle metadata (`core/libs/prx/libkernel/Module/src/DynamicLoader.cpp:221` and `:318`) but no stop entry, so a follow-up needs the relinker to record one. On Windows it also reads the metadata's dependency and sweep name lists (`DeferredListNames`, `core/libs/prx/libkernel/Module/src/DynamicLoader.cpp:271`). ### 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 11 Pro 10.0.26300, WinLibs GCC, Release, head `d164e44244` (`main` `5f2e6336ed` merged in): `guest_dynamic_loader`, `guest_kernel_sys_module_internal` and the eight `guest_deferred_module_*` tests pass (10 of 10, none Not Run), with their three test executables rebuilt at this head. - New rows in `core/libs/tests/GuestDynamicLoader.cpp` load the test system module with `flags` 1 and with an `opt` block (refused, `res` untouched), then load it, refuse to unload it with `flags` 1, with an `opt` block and as handle 0, call into it, and unload it. New rows in `core/libs/tests/GuestKernelSysModuleInternal.cpp` check that `sceKernelLoadStartModuleForSysmodule` with `flags` `0x10000` still reaches the missing-file error, and that an `opt` block is refused. - **Negative control** at this head: both tests built against `main`'s `core/libs/prx/libkernel/Module/src/Module.cpp` and `core/libs/prx/libkernel_sys/Export.cpp` abort (`0xc0000409`, 2 of 2 failed; `guest_dynamic_loader` stops at `GuestDynamicLoader.cpp:209`, the `flags` row). - `guest_runtime_lifecycle` and `guest_dynamic_loader_fixed_base_tests` are registered on Linux only, and I didn't run them. Both pass `flags` 0 and a null `opt`. - `tools/check_conventions.py --base upstream/main` on the committed head `d164e44244`: all checks pass. - The merge of `main` `5f2e6336ed` conflicted once, in `docs/dev/debt/libkernel_sys.md`: `main` reworded the `scePthreadMutexIsowned, utrace and tcsetsid` entry below this branch's new sysmodule `flags` entry. That entry takes `main`'s wording and the `flags` entry is unchanged. - Correction: the first head, `5ee657294b`, had a `debt-length` warning (the `libkernel_sys.md` module info entry reached 1186 characters). I had run the check before committing, and it reads committed Git objects (`CONTRIBUTING.md`, "reads pull request Git objects"), so it didn't see the debt change. The second commit moves the sysmodule `flags` note into its own entry, which fixes it. - `tools/pr_overlap.py --branch`, run at the earlier head `caca1e4031`: the code-file overlaps are #13486, #15037, #15115, #5021, #6149, #9097 and #9389. In their diffs only #6149 names one of these functions, in a debt line about Linux `DT_INIT` arguments, and none changes them. The many debt-file overlaps are other entries. - No title was run. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: 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!15556
No description provided.