fix(hid): nn::hid::detail::GetNpadStates for full key, handheld and joy dual #2

Open
rainy wants to merge 1 commit from rainy/Longshot-module-gaps:fix/hid-detail-get-npad-states into mods/module-gaps
First-time contributor

Type: fix · Risk: low · Tested on: Linux (Omarchy, kernel 7.2.15; Intel Core Ultra 7 270K Plus; RTX 4090, driver 615.78.08) · Needs: a run on Windows, any GPU, a controller

TL;DR

The console's input library has internal versions of its controller-state calls. They take one more argument and report how many states they wrote. Longshot answered the public versions but not the internal ones, so a mod that calls them runs library code that reads memory Longshot does not provide. This PR answers the three internal versions with the same states as the public ones. A new game-free test fails without the fix and passes here. The full check suite on this branch has the same 38 failures as mods/module-gaps, and one more pass. It also ran in the game on Linux, in a full patched build on an earlier base of the same module-gaps commits.

Breakdown

  • Who it helps: players of a code mod that reads the controller through nn::hid::detail::GetNpadStates, such as sloperinaGZ 0.0.8's menu.
  • What was wrong: only the public nn::hid::GetNpadStates were HLE'd. The detail versions run the SDK's own code, which reads hid shared memory the host does not provide.
  • What changes: the full key, handheld and joy dual detail versions give the public versions' states and write the count to their first argument.
  • What does not change: the public calls and the game's own input. The game does not import the detail versions.
  • How it was checked: the new tests/test_hid_detail_npad_states.py passes here and fails with the fix reverted. Existing hid tests still pass. python -I -B tests/run_all.py --jobs 8 has the same 38 failures as mods/module-gaps and one more pass. It also ran in the game on Linux, in a full patched build on an earlier base of the same module-gaps commits.
  • What is still open: a run on Windows.
  • Risk: low. Only code that imports these three symbols reaches the new functions.

Why

I found this while getting a code mod, sloperinaGZ 0.0.8 (https://github.com/PracticeROM/sloperina/releases/tag/v0.0.8), to run on this branch. Its menu reads its buttons through nn::hid::detail::GetNpadStates. Its input.c says why: the detail versions are what the public ones call, so they keep working when the mod hooks the public ones. The symbols demangle to nn::hid::detail::GetNpadStates(int*, nn::hid::NpadFullKeyState*, int, unsigned int const&) and the same for the handheld and joy dual states. Longshot has no HLE for them, so they run as the SDK's lifted code, and that code reads hid shared memory that host/nn_hid.c does not provide. I wrote this fix from reading the mod's source, before its first run, so I have not watched the failure on mods/module-gaps itself.

Any code module built against the SDK can import these symbols. The game itself does not, so this matters only with code modules, which is why it targets snaildev99's mods/module-gaps at 2756bd26 and not main. I searched the tracker for npad and GetNpadStates: no issue or PR covers it.

How to reproduce

Seen on: not watched in a run. Found in the mod's source. It does not depend on the OS or GPU.
Settings and switches: defaults, with sloperinaGZ 0.0.8 in the mods folder
Steps: build the game with the mod on mods/module-gaps, start it, and press the mod's menu buttons.
Expected: the mod's menu reads the buttons.
Actual: not watched. The detail calls run the SDK's code, which reads hid shared memory the host does not provide, so the mod has no working input path. The game-free test below shows the missing HLE.

What changed

Three HLE functions in host/nn_hid.c, next to the public ones.

  • hid: new npad_states_detail in host/nn_hid.c moves the arguments to the public call's order, calls npad_states, and writes the returned count to *outCount when it is not null. It is the HLE for the full key, handheld and joy dual detail symbols.
  • tests: new tests/test_hid_detail_npad_states.py checks that the three symbols are HLE'd, demangles them with c++filt where it exists, and compiles npad_states_detail from the source over a stand-in npad_states. It checks the arguments passed on, the count written, and a null outCount.

Measurements

Nothing to measure. Each call moves three registers and writes one word, around the existing public call.

Flags and settings

None

Testing

Tested: game-free tests on Linux with gcc, on this branch at 3ed935e5:

  • python -I -B tests/test_hid_detail_npad_states.py passes.
  • With host/nn_hid.c reverted to mods/module-gaps, it fails with host/nn_hid.c has no HLE for _ZN2nn3hid6detail13GetNpadStatesEPiPNS0_16NpadFullKeyStateEiRKj.
  • tests/test_input_background.py, tests/test_rumble_mapping.py and tests/test_stick_rest.py pass.

python -I -B tests/run_all.py --jobs 8 on Linux (Omarchy, kernel 7.2.15; Intel Core Ultra 7 270K Plus; RTX 4090, driver 615.78.08). mods/module-gaps @ 2756bd26: 686 passed, 38 failed, 97 skipped. this branch @ 3ed935e5: 687 passed, 38 failed, 97 skipped, 0 new failures compared by test name. The 38 failures also fail on mods/module-gaps at 2756bd26. They are:

Tests that also fail on mods/module-gaps @ 2756bd26 in this environment
  • tests/test_bug_report_defaults.py
  • tests/test_capture_read_once.py
  • tests/test_cpu_report.py
  • tests/test_crash_record.py
  • tests/test_crt_options.py
  • tests/test_damage_debug_menu.py
  • tests/test_dev_packs.py
  • tests/test_display_startup_failure.py
  • tests/test_dlss_feature_size.py
  • tests/test_failed_compile_keeps_objects.py
  • tests/test_guest_imported.py
  • tests/test_high_fps_fixes.py
  • tests/test_host_guest_writes.py
  • tests/test_hud_layout_row.py
  • tests/test_invalidate_quick.py
  • tests/test_invalidate_vcore.py
  • tests/test_libc_hooks.py
  • tests/test_link_path_comma.py
  • tests/test_linux_preflight.py
  • tests/test_linux_pressure.py
  • tests/test_linux_update.py
  • tests/test_log_open_failure.py
  • tests/test_missing_nup.py
  • tests/test_mods_size_table.py
  • tests/test_pending_guest_import.py
  • tests/test_pipeline_play_cache.py
  • tests/test_prepare_cache_save.py
  • tests/test_present_thread.py
  • tests/test_profiling_mode.py
  • tests/test_psychov35_fsr3_fg.py
  • tests/test_readback_timeout_source.py
  • tests/test_run_all_changed.py
  • tests/test_slow_frames.py
  • tests/test_song_note_cue.py
  • tests/test_switch_table.py
  • tests/test_upscaler_rows.py
  • tests/test_walk_key.py
  • tests/test_vbytes.py (timed out at 600 s)

In the game, on Linux with an RTX 4090, this fix ran as part of a full patched build: all the fixes together, on an earlier base of the same module-gaps commits (ed1a8513). The mod's menu opened, and everything tried in the mod worked. It was not run alone in the game on this exact branch.

Not testable by hand: the game never calls these functions, so a run without a code mod cannot show the fix. The game-free test stands in for it, and the in-game run with the mod shows it.

Not tested: Windows (no build or run there) and other GPUs.

Must be tested:

  • Windows, any GPU, a controller: the same steps, expecting the same result (Test Farm: @Beanstalk windows, a controller, a code mod's menu)

Testing with sloperinaGZ

This branch alone isn't enough to use sloperinaGZ in the game. The in-game run above also had these fixes applied:

  • #1683 fix(svc): svcQueryMemory free-range answer
  • #1684 fix(hid): stick direction pseudo-buttons. Without it, the mod's menu combo (R + right stick down) never fires.
  • #1685 fix(nvn): window origin and depth mode getters
  • #1686 fix(lift): SCVTF / UCVTF fixed-point
  • #1 and #3 here

A branch with all of them applied together: https://git.magicbean.dev/rainy/Longshot/src/branch/longslop-rc8

Risks

What could break: a mod's detail calls go through the same npad_states the game's calls use, so it runs more than once a frame and its sampling counter moves faster. It looks stateless apart from that counter and the hold-toggle edges, but that is not checked in play yet.

Turning it off: revert the commit

Compatibility: Nothing changes

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what
    can't be run by hand is in Testing's Not testable by hand, with why.
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and
    in Risks.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main. This PR targets snaildev99's mods/module-gaps, not main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named). The failures named below also fail on mods/module-gaps at 2756bd26, which is this PR's base.
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
**Type:** fix · **Risk:** low · **Tested on:** Linux (Omarchy, kernel 7.2.15; Intel Core Ultra 7 270K Plus; RTX 4090, driver 615.78.08) · **Needs:** a run on Windows, any GPU, a controller ## TL;DR The console's input library has internal versions of its controller-state calls. They take one more argument and report how many states they wrote. Longshot answered the public versions but not the internal ones, so a mod that calls them runs library code that reads memory Longshot does not provide. This PR answers the three internal versions with the same states as the public ones. A new game-free test fails without the fix and passes here. The full check suite on this branch has the same 38 failures as `mods/module-gaps`, and one more pass. It also ran in the game on Linux, in a full patched build on an earlier base of the same module-gaps commits. ## Breakdown - **Who it helps:** players of a code mod that reads the controller through `nn::hid::detail::GetNpadStates`, such as sloperinaGZ 0.0.8's menu. - **What was wrong:** only the public `nn::hid::GetNpadStates` were HLE'd. The detail versions run the SDK's own code, which reads hid shared memory the host does not provide. - **What changes:** the full key, handheld and joy dual detail versions give the public versions' states and write the count to their first argument. - **What does not change:** the public calls and the game's own input. The game does not import the detail versions. - **How it was checked:** the new `tests/test_hid_detail_npad_states.py` passes here and fails with the fix reverted. Existing hid tests still pass. `python -I -B tests/run_all.py --jobs 8` has the same 38 failures as `mods/module-gaps` and one more pass. It also ran in the game on Linux, in a full patched build on an earlier base of the same module-gaps commits. - **What is still open:** a run on Windows. - **Risk:** low. Only code that imports these three symbols reaches the new functions. ## Why I found this while getting a code mod, sloperinaGZ 0.0.8 (https://github.com/PracticeROM/sloperina/releases/tag/v0.0.8), to run on this branch. Its menu reads its buttons through `nn::hid::detail::GetNpadStates`. Its `input.c` says why: the detail versions are what the public ones call, so they keep working when the mod hooks the public ones. The symbols demangle to `nn::hid::detail::GetNpadStates(int*, nn::hid::NpadFullKeyState*, int, unsigned int const&)` and the same for the handheld and joy dual states. Longshot has no HLE for them, so they run as the SDK's lifted code, and that code reads hid shared memory that `host/nn_hid.c` does not provide. I wrote this fix from reading the mod's source, before its first run, so I have not watched the failure on `mods/module-gaps` itself. Any code module built against the SDK can import these symbols. The game itself does not, so this matters only with code modules, which is why it targets snaildev99's `mods/module-gaps` at 2756bd26 and not main. I searched the tracker for npad and GetNpadStates: no issue or PR covers it. ## How to reproduce **Seen on:** not watched in a run. Found in the mod's source. It does not depend on the OS or GPU. **Settings and switches:** defaults, with sloperinaGZ 0.0.8 in the mods folder **Steps:** build the game with the mod on `mods/module-gaps`, start it, and press the mod's menu buttons. **Expected:** the mod's menu reads the buttons. **Actual:** not watched. The detail calls run the SDK's code, which reads hid shared memory the host does not provide, so the mod has no working input path. The game-free test below shows the missing HLE. ## What changed Three HLE functions in `host/nn_hid.c`, next to the public ones. - hid: new `npad_states_detail` in `host/nn_hid.c` moves the arguments to the public call's order, calls `npad_states`, and writes the returned count to `*outCount` when it is not null. It is the HLE for the full key, handheld and joy dual detail symbols. - tests: new `tests/test_hid_detail_npad_states.py` checks that the three symbols are HLE'd, demangles them with c++filt where it exists, and compiles `npad_states_detail` from the source over a stand-in `npad_states`. It checks the arguments passed on, the count written, and a null `outCount`. ## Measurements Nothing to measure. Each call moves three registers and writes one word, around the existing public call. ### Flags and settings None ## Testing **Tested:** game-free tests on Linux with gcc, on this branch at 3ed935e5: - `python -I -B tests/test_hid_detail_npad_states.py` passes. - With `host/nn_hid.c` reverted to `mods/module-gaps`, it fails with `host/nn_hid.c has no HLE for _ZN2nn3hid6detail13GetNpadStatesEPiPNS0_16NpadFullKeyStateEiRKj`. - `tests/test_input_background.py`, `tests/test_rumble_mapping.py` and `tests/test_stick_rest.py` pass. `python -I -B tests/run_all.py --jobs 8` on Linux (Omarchy, kernel 7.2.15; Intel Core Ultra 7 270K Plus; RTX 4090, driver 615.78.08). `mods/module-gaps` @ 2756bd26: 686 passed, 38 failed, 97 skipped. this branch @ 3ed935e5: 687 passed, 38 failed, 97 skipped, 0 new failures compared by test name. The 38 failures also fail on `mods/module-gaps` at 2756bd26. They are: <details> <summary>Tests that also fail on mods/module-gaps @ 2756bd26 in this environment</summary> - `tests/test_bug_report_defaults.py` - `tests/test_capture_read_once.py` - `tests/test_cpu_report.py` - `tests/test_crash_record.py` - `tests/test_crt_options.py` - `tests/test_damage_debug_menu.py` - `tests/test_dev_packs.py` - `tests/test_display_startup_failure.py` - `tests/test_dlss_feature_size.py` - `tests/test_failed_compile_keeps_objects.py` - `tests/test_guest_imported.py` - `tests/test_high_fps_fixes.py` - `tests/test_host_guest_writes.py` - `tests/test_hud_layout_row.py` - `tests/test_invalidate_quick.py` - `tests/test_invalidate_vcore.py` - `tests/test_libc_hooks.py` - `tests/test_link_path_comma.py` - `tests/test_linux_preflight.py` - `tests/test_linux_pressure.py` - `tests/test_linux_update.py` - `tests/test_log_open_failure.py` - `tests/test_missing_nup.py` - `tests/test_mods_size_table.py` - `tests/test_pending_guest_import.py` - `tests/test_pipeline_play_cache.py` - `tests/test_prepare_cache_save.py` - `tests/test_present_thread.py` - `tests/test_profiling_mode.py` - `tests/test_psychov35_fsr3_fg.py` - `tests/test_readback_timeout_source.py` - `tests/test_run_all_changed.py` - `tests/test_slow_frames.py` - `tests/test_song_note_cue.py` - `tests/test_switch_table.py` - `tests/test_upscaler_rows.py` - `tests/test_walk_key.py` - `tests/test_vbytes.py` (timed out at 600 s) </details> In the game, on Linux with an RTX 4090, this fix ran as part of a full patched build: all the fixes together, on an earlier base of the same module-gaps commits (ed1a8513). The mod's menu opened, and everything tried in the mod worked. It was not run alone in the game on this exact branch. **Not testable by hand:** the game never calls these functions, so a run without a code mod cannot show the fix. The game-free test stands in for it, and the in-game run with the mod shows it. **Not tested:** Windows (no build or run there) and other GPUs. **Must be tested:** - Windows, any GPU, a controller: the same steps, expecting the same result (Test Farm: @Beanstalk windows, a controller, a code mod's menu) **Testing with sloperinaGZ** This branch alone isn't enough to use sloperinaGZ in the game. The in-game run above also had these fixes applied: - [#1683](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1683) fix(svc): svcQueryMemory free-range answer - [#1684](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1684) fix(hid): stick direction pseudo-buttons. Without it, the mod's menu combo (R + right stick down) never fires. - [#1685](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1685) fix(nvn): window origin and depth mode getters - [#1686](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1686) fix(lift): SCVTF / UCVTF fixed-point - [#1](https://git.magicbean.dev/snaildev99/Longshot/pulls/1) and [#3](https://git.magicbean.dev/snaildev99/Longshot/pulls/3) here A branch with all of them applied together: https://git.magicbean.dev/rainy/Longshot/src/branch/longslop-rc8 ## Risks **What could break:** a mod's detail calls go through the same `npad_states` the game's calls use, so it runs more than once a frame and its sampling counter moves faster. It looks stateless apart from that counter and the hold-toggle edges, but that is not checked in play yet. **Turning it off:** revert the commit **Compatibility:** Nothing changes ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Tested on my own PC where it runs (in the game, for a change the game runs), as Testing's Tested says; what can't be run by hand is in Testing's Not testable by hand, with why. - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Nothing gets worse that I know of: any cost (speed, latency, memory, a setup that worked before) is measured and in Risks. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [ ] Rebased on current `main`. This PR targets snaildev99's `mods/module-gaps`, not main. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). The failures named below also fail on `mods/module-gaps` at 2756bd26, which is this PR's base. - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here.
The public GetNpadStates are HLE'd; the SDK's detail versions, which take
an int *outCount first, were not. The game does not import them, but a
mod's code module may: sloperinaGZ 0.0.8's menu reads its buttons through
them, so they would run the SDK's own code, which reads hid shared memory
the host does not provide. They now give the public ones' states and write
the count out.

tests/test_hid_detail_npad_states.py checks the three symbols are HLE'd
and compiles npad_states_detail over a stand-in npad_states.

Assisted-by: Grok Bot
Owner

Thank you. I'd like to take this with one fix to the return value.

Windows run (Windows 11, the builder's clang): tests/test_hid_detail_npad_states.py passes at 3ed935e5 and fails with the fix reverted; the existing hid tests still pass.

The return value (medium): the detail functions return a Result, not the count. The game's own public nn::hid::GetNpadStates(NpadFullKeyState*, int, const u32&) (sdk 0x8566ac30) passes &count, calls the detail function through its PLT stub, and then does cbnz w0 to an error routine; on zero it returns the count from its own variable. This change leaves the count in x0, so a caller that checks the Result, as the SDK's wrapper does, would take success for failure. sloperina declares the functions void and ignores x0, which is why it works there. Fix: return 0 after writing the count, and tests/test_hid_detail_npad_states.py:67 (CHECK(c->x[0] == 3)) should ask for 0.

Small:

  • On current main npad_states checks guest ranges; the *outCount write (G32(n)) should be guarded the same way (hos_guest_range(n, 4)).
  • For symmetry with the five public HLEs, the JoyLeft and JoyRight detail versions.
  • The test's demangle check looks for c++filt only; llvm-cxxfilt as a fallback lets it run on Windows.

Checked and fine: the three names demangle as you say, the argument order matches, the state size is 0x28 as the game's own routine uses, and the game itself does not import these, so nothing of its input changes.

Same offer as on the first one: if you'd rather, I will put the follow-up on top of your commit.

Thank you. I'd like to take this with one fix to the return value. **Windows run (Windows 11, the builder's clang):** `tests/test_hid_detail_npad_states.py` passes at 3ed935e5 and fails with the fix reverted; the existing hid tests still pass. **The return value (medium):** the detail functions return a Result, not the count. The game's own public `nn::hid::GetNpadStates(NpadFullKeyState*, int, const u32&)` (sdk 0x8566ac30) passes `&count`, calls the detail function through its PLT stub, and then does `cbnz w0` to an error routine; on zero it returns the count from its own variable. This change leaves the count in x0, so a caller that checks the Result, as the SDK's wrapper does, would take success for failure. sloperina declares the functions `void` and ignores x0, which is why it works there. Fix: return 0 after writing the count, and `tests/test_hid_detail_npad_states.py:67` (`CHECK(c->x[0] == 3)`) should ask for 0. **Small:** - On current main `npad_states` checks guest ranges; the `*outCount` write (`G32(n)`) should be guarded the same way (`hos_guest_range(n, 4)`). - For symmetry with the five public HLEs, the JoyLeft and JoyRight detail versions. - The test's demangle check looks for `c++filt` only; `llvm-cxxfilt` as a fallback lets it run on Windows. **Checked and fine:** the three names demangle as you say, the argument order matches, the state size is 0x28 as the game's own routine uses, and the game itself does not import these, so nothing of its input changes. Same offer as on the first one: if you'd rather, I will put the follow-up on top of your commit.
Author
First-time contributor

Thanks for the careful review. The return value was my mistake. I matched sloperina, which declares these void, and never checked the SDK's own wrapper. Returning 0 and asking for 0 in the test is the right fix. The guest-range check on *outCount and adding JoyLeft/JoyRight both make sense for consistency with the public HLEs. Please go ahead and add your follow-ups as your own commits on top of ours, your way. For credit, my commits should be authored as rainy, with an Assisted-by: Grok Bot trailer.

Thanks for the careful review. The return value was my mistake. I matched sloperina, which declares these `void`, and never checked the SDK's own wrapper. Returning 0 and asking for 0 in the test is the right fix. The guest-range check on `*outCount` and adding JoyLeft/JoyRight both make sense for consistency with the public HLEs. Please go ahead and add your follow-ups as your own commits on top of ours, your way. For credit, my commits should be authored as `rainy`, with an `Assisted-by: Grok Bot` trailer.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/hid-detail-get-npad-states:rainy-fix/hid-detail-get-npad-states'
git switch 'rainy-fix/hid-detail-get-npad-states'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'mods/module-gaps'
git merge --no-ff 'rainy-fix/hid-detail-get-npad-states'
git switch 'rainy-fix/hid-detail-get-npad-states'
git rebase 'mods/module-gaps'
git switch 'mods/module-gaps'
git merge --ff-only 'rainy-fix/hid-detail-get-npad-states'
git switch 'rainy-fix/hid-detail-get-npad-states'
git rebase 'mods/module-gaps'
git switch 'mods/module-gaps'
git merge --no-ff 'rainy-fix/hid-detail-get-npad-states'
git switch 'mods/module-gaps'
git merge --squash 'rainy-fix/hid-detail-get-npad-states'
git switch 'mods/module-gaps'
git merge --ff-only 'rainy-fix/hid-detail-get-npad-states'
git switch 'mods/module-gaps'
git merge 'rainy-fix/hid-detail-get-npad-states'
git push origin 'mods/module-gaps'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
snaildev99/Longshot!2
No description provided.