forked from MagicBeanDev/Longshot
fix(hid): nn::hid::detail::GetNpadStates for full key, handheld and joy dual #2
Loading…
Reference in a new issue
No description provided.
Delete branch "rainy/Longshot-module-gaps:fix/hid-detail-get-npad-states"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
nn::hid::detail::GetNpadStates, such as sloperinaGZ 0.0.8's menu.nn::hid::GetNpadStateswere HLE'd. The detail versions run the SDK's own code, which reads hid shared memory the host does not provide.tests/test_hid_detail_npad_states.pypasses here and fails with the fix reverted. Existing hid tests still pass.python -I -B tests/run_all.py --jobs 8has the same 38 failures asmods/module-gapsand 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.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. Itsinput.csays why: the detail versions are what the public ones call, so they keep working when the mod hooks the public ones. The symbols demangle tonn::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 thathost/nn_hid.cdoes not provide. I wrote this fix from reading the mod's source, before its first run, so I have not watched the failure onmods/module-gapsitself.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-gapsat2756bd26and 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.npad_states_detailinhost/nn_hid.cmoves the arguments to the public call's order, callsnpad_states, and writes the returned count to*outCountwhen it is not null. It is the HLE for the full key, handheld and joy dual detail symbols.tests/test_hid_detail_npad_states.pychecks that the three symbols are HLE'd, demangles them with c++filt where it exists, and compilesnpad_states_detailfrom the source over a stand-innpad_states. It checks the arguments passed on, the count written, and a nulloutCount.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.pypasses.host/nn_hid.creverted tomods/module-gaps, it fails withhost/nn_hid.c has no HLE for _ZN2nn3hid6detail13GetNpadStatesEPiPNS0_16NpadFullKeyStateEiRKj.tests/test_input_background.py,tests/test_rumble_mapping.pyandtests/test_stick_rest.pypass.python -I -B tests/run_all.py --jobs 8on 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 onmods/module-gapsat2756bd26. They are:Tests that also fail on mods/module-gaps @
2756bd26in this environmenttests/test_bug_report_defaults.pytests/test_capture_read_once.pytests/test_cpu_report.pytests/test_crash_record.pytests/test_crt_options.pytests/test_damage_debug_menu.pytests/test_dev_packs.pytests/test_display_startup_failure.pytests/test_dlss_feature_size.pytests/test_failed_compile_keeps_objects.pytests/test_guest_imported.pytests/test_high_fps_fixes.pytests/test_host_guest_writes.pytests/test_hud_layout_row.pytests/test_invalidate_quick.pytests/test_invalidate_vcore.pytests/test_libc_hooks.pytests/test_link_path_comma.pytests/test_linux_preflight.pytests/test_linux_pressure.pytests/test_linux_update.pytests/test_log_open_failure.pytests/test_missing_nup.pytests/test_mods_size_table.pytests/test_pending_guest_import.pytests/test_pipeline_play_cache.pytests/test_prepare_cache_save.pytests/test_present_thread.pytests/test_profiling_mode.pytests/test_psychov35_fsr3_fg.pytests/test_readback_timeout_source.pytests/test_run_all_changed.pytests/test_slow_frames.pytests/test_song_note_cue.pytests/test_switch_table.pytests/test_upscaler_rows.pytests/test_walk_key.pytests/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:
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:
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_statesthe 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
type(scope): description) of the status line's type.can't be run by hand is in Testing's Not testable by hand, with why.
in Risks.
main. This PR targets snaildev99'smods/module-gaps, not main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). The failures named below also fail onmods/module-gapsat2756bd26, which is this PR's base.third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.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.pypasses at3ed935e5and 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 doescbnz w0to 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 functionsvoidand ignores x0, which is why it works there. Fix: return 0 after writing the count, andtests/test_hid_detail_npad_states.py:67(CHECK(c->x[0] == 3)) should ask for 0.Small:
npad_stateschecks guest ranges; the*outCountwrite (G32(n)) should be guarded the same way (hos_guest_range(n, 4)).c++filtonly;llvm-cxxfiltas 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.
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*outCountand 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 asrainy, with anAssisted-by: Grok Bottrailer.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.