feat(host): svcQueryMemory answers for the image module by module, as the kernel does #3167
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3167
Loading…
Reference in a new issue
No description provided.
Delete branch "snaildev99/Longshot:mods/query-memory-modules"
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?
Summary
mainbuilds a mod's code module (exefs/subsdk*) into the game, but answeredsvcQueryMemoryfor the whole gameimage as one executable region. A mod's code module finds the game's
mainthe way it does on a console: by walkingthe memory regions down from its own. With one region the walk meets the loader and gives up, so the module never
finds the game. The SDK's own readers (
nn::ro's module header search,nn::diag's backtrace) find a module's startthe same way.
With this change
svcQueryMemoryanswers module by module, as the kernel does: each module's.textread-execute,.rodataread-only, and.datawith the bss read-write, back to back, together spanning the image. The imagebuilder lists the regions (
image::Image::memory_regions), the lift writes them intogen/image.c,A64Imagecarries the table (
A64_MODULE_ABI2: the runtime and the game module must agree on its layout), andhost/game_module.ccopies it at the load. An address in the image that no region holds gets the whole image, asbefore; the stack, heap, system region, output region and free gaps are unchanged.
HOS_QUERY_MEMORY_OLD=1(a killswitch) answers the one region as before.
This is the part of #984 that
maindidn't have yet (mainalready builds the modules in,modules.rs).Read first:
host/hos_svc.c(image_region,query_memory),builder/crates/image/src/lib.rs(
memory_regions), thenbuilder/crates/lift/src/whole/files.rs(image_c).Testing
Tested: Windows 11 Pro, RTX 3090, NVIDIA 610.88, in the game, built by
longshot-builder play --profilewiththis PR and the other mod compatibility ports merged, adult Link in Kakariko: HUD of Time and Rupee Counter, two mods
with a code module each, find the game and draw their HUD. The switch both ways, at the same spot after loading the
same save: with
HOS_QUERY_MEMORY_OLD=1(the start's switches line saysHOS_QUERY_MEMORY_OLD=1 (kill)) HUD ofTime's clock is gone; unset, it is drawn (
13:12, bottom left). Onmain, with the module built in (Code module subsdk9: built in.), the clock is gone too (screenshots in the comments).Game-free:
tests/test_query_memory.py(the realquery_memoryover a made-up image: each segment, a mod module'swalk down to
main, a hole, the switch both ways, four broken variants) andtests/test_query_memory_free.py, withllvm-mingw's clang; the lift's
image_c_writes_each_modules_regionsand the image builder's region tests.mise run test, run locally while CI's run waits for approval (Windows 11,tests/run_all.py --strict, 12 checks atonce), here @
f4b45e70aand onmain@f577a1aea: 832 passed, 14 failed, 51 skipped here; 820 passed, 25 failed, 51skipped on
main. 13 of the 14 also fail onmain: test_cursor_hidden_linux, test_dev_update, test_dlssg_folders,test_fsr4_tune, test_fsr_uniform_ownership, test_input_background, test_lifter, test_linux_pressure,
test_loaded_modules_drop, test_program_cache_cleanup, test_program_cache_one_variant, test_program_cache_store_record,
test_state_list_give_back. The other, test_fallback_buffer_cleanup, hit its program's 10-second timeout under that
load and passes here run alone (
--exact); this PR touches none of the renderer sources it cuts.Still to test:
on
main(host code changed) (@Beanstalk)Measurements
Verdict: Fixed: a code module that walks memory down to the game finds it, and HUD of Time draws its clock, which
it does not on
mainor with the switch set. Nothing changes per frame:svcQueryMemorylooks the address up in atable of a few regions.
Setup: Windows 11 Pro, RTX 3090, NVIDIA 610.88; the test profile above in Kakariko; the switch set in the launcher's
environment, one run each way, the same save loaded and nothing pressed after.
f577a1aeaf4b45e70apython -I tests/test_query_memory.pyimage_regionis not inhost/hos_svc.c)cargo test -p lift image_c_writes_each_modules_regionsimage.chas no region table)HOS_QUERY_MEMORY_OLD=1HOS_QUERY_MEMORY_OLDships unset (the module-by-module answer): set, the mod's HUD is gone as onmain; unset, it isdrawn. The game's After column is the build with the other mod compatibility ports merged (72d64e92f).
Risks
A64_MODULE_ABI2: a game module built before this change is refused until the game is built again. A player'sbuild links the runtime and the game module together, so an update builds both.
modules back to back, so a real image has no such hole.
see
mainas its own region (the module checks of the module PR leave such a module out).HOS_QUERY_MEMORY_OLD=1. Players: code modules that walk memory find the game. No save, setting orcache changes.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (run locally, Testing names them; CI's run waits for approval)main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened. (tests/test_query_memory_free.pystubs the newimage_region, so it still sees one region)mainand here, for a change that shows on screen. (in the comments)Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: Windows 11 Pro, RTX 3090, NVIDIA 610.88, adult Link in Kakariko, built by longshot-builder play --profile with the mod compatibility ports merged: HUD of Time and Rupee Counter, a code module each, found the game and drew their HUD. The run with HOS_QUERY_MEMORY_OLD=1 is still to do.
Possibly related, for context:
Screenshots: Kakariko after loading the same save, nothing pressed after, the same test profile (Windows 11, RTX 3090). HUD of Time draws a clock at the bottom left from its code module.
Before, main @

f577a1aea(the module built in): no clock.After, this PR (the build with the other mod compatibility ports merged, 72d64e92f): the clock is drawn.

After, with

HOS_QUERY_MEMORY_OLD=1: no clock, as on main.Description updated: the switch's A/B in the game is in Testing and Measurements (HUD of Time's clock is drawn with this PR, and not on main or with HOS_QUERY_MEMORY_OLD=1; screenshots in the comment above).
@Beanstalk Build the game from this PR on Linux (any GPU) and start it to the title screen, no mods. It changes host C (
host/hos_svc.c,host/game_module.c) and the game module's ABI (A64_MODULE_ABI2): expect it to build and start asmaindoes.@snaildev99 only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was forf4b45e7, and the PR has changed since (3d17857), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.mise run test, run locally while CI's run waits for approval (Windows 11, run_all --strict, 12 checks at once), here @
f4b45e70aand on main @f577a1aea: 832 passed, 14 failed, 51 skipped here; 820 passed, 25 failed, 51 skipped on main. 13 of the 14 also fail on main (named in Testing); the other, test_fallback_buffer_cleanup, timed out under that load and passes here run alone (--exact). Box ticked. The Linux build (Host code) still waits for the Test Farm request's approval.@review
Recommendation: Approve. The new first-use switch cache has a data race when guest threads query memory concurrently. The new check also hard-codes GCC, so it can be skipped or fail on supported setups that provide another compiler. Nothing blocking is left: what remains is for the author before merging (below). Recommend approval; not approved on the forge.
The change reports image memory by module segment, allowing code modules that walk memory downward to find the game. A Windows test with additional mod compatibility ports showed two mod HUDs working; the change adds a small region lookup and a kill switch.
Should fix (2): not blocking, but worth doing before merging.
host/hos_svc.c:357: The lazy initialization of the shared switch cache uses unsynchronized reads and writes. (more on the line) Do: Initialize the switch before guest threads start, or use a thread-safe once mechanism or atomic cache.tests/test_query_memory.py:28: The new check hard-codes GCC instead of using the compiler selected by the test runner. (more on the line) Do: Use the runner's {cc} placeholder and honor CC for direct invocation, including compiler arguments as appropriate.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
@fixcan do L1 and L2.Checks: Still running: 10 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the tested configuration: HUD of Time's clock was absent on main and with the kill switch set, and present with this change and the other mod compatibility ports merged. Linux game validation is still pending.
Other systems: The query can run on any guest thread and on both Windows and Linux. It changes answers for game and SDK callers as well as mods; the reported game run covers Windows on NVIDIA, while the requested Linux build and startup remain pending. No renderer or GPU-specific code changes.
Switches: HOS_QUERY_MEMORY_OLD is added, unset by default, selecting the new module-by-module answers; setting it to 1 restores the old whole-image answer. It has a switches.def row. The default fixes the demonstrated mod behavior, subject to the outstanding Linux run.
Reviewed:
f4b45e70a3againstf577a1aeab: Read AGENTS.md, CONTRIBUTING.md, the PR template, standards, the diff and commit message; read the changed image and query tests and host game-module loader, and traced image construction, module collection, ABI loading and test compiler selection. Skimmed the remaining changed documentation, switch table, generated-C emission and unchanged surrounding code. docs/review-checklist.md and ../Tooling/AGENTS.md were absent. The supplied comments and CI status were reviewed; screenshots themselves were not accessible.; nothing run (a code review).Checked and fine:
Testing: Read-only review; no builds, tests, or project scripts were run. CI is still running.
Overlaps and merge order: Not determined from the offline checkout.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -337,1 +354,4 @@static void query_memory(uint64_t out, uint64_t addr) {static int whole = -1;if (whole < 0) whole = env_on("HOS_QUERY_MEMORY_OLD");L1 (should fix): The lazy initialization of the shared switch cache uses unsynchronized reads and writes.
When: If two guest threads enter svcQueryMemory while whole is still -1, both can read and write it concurrently. That is a C data race and has undefined behavior, even when both read the same environment value.
Do: Initialize the switch before guest threads start, or use a thread-safe once mechanism or atomic cache.
@ -0,0 +25,4 @@import tempfilefrom pathlib import PathRUN_ALL_ARGS = ["--cc", "gcc"]L2 (should fix): The new check hard-codes GCC instead of using the compiler selected by the test runner.
When: On a supported Windows or Linux setup with CC pointing to clang but no gcc on PATH, run_all skips this check, or fails it in strict mode, even though a suitable compiler is available. Direct invocation also ignores CC.
Do: Use the runner's {cc} placeholder and honor CC for direct invocation, including compiler arguments as appropriate.
@fix
WIP: feat(host): svcQueryMemory answers for the image module by module, as the kernel doesto feat(host): svcQueryMemory answers for the image module by module, as the kernel doesf4b45e70a33d17857165Recommendation: Approve. The code is ready: only testing is left. The switch cache is synchronized and the test now uses the selected compiler. The host and module ABI still need a Linux build and game run. Recommend approval; not approved on the forge.
What
@fixchangedI changed this pull request: the new head is
3d17857165. I rebased it ontomain(3145d9d48f).Changed
Nothing was built or run: the checks run again on the new head.
The change lets code modules find the game by reporting image memory one module segment at a time. It adds a region table and an ABI change, with a switch to restore the previous answer.
Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
Checks: they run again on this head; a failure shows on the pull request.
More: does it help, other systems, switches, what was read
Does it help: Yes: the reported Windows run showed HUD of Time's clock with the change, but not on main or with the old-answer switch enabled. The tested build also included other mod compatibility ports.
Other systems: The host and game module both consume the new ABI and region table. Windows was exercised in game; Linux remains untested. The change does not alter rendering code directly.
Switches: HOS_QUERY_MEMORY_OLD defaults to unset, selecting module-by-module answers; setting it restores the old single-region answer. The reported Windows comparison supports the default.
Reviewed:
3d17857165against3145d9d48f: Read AGENTS.md, CONTRIBUTING.md, the pull request template, the fix diff, tests/test_query_memory.py, tests/compiler_command.py and gpu/vk/compat/longshot_env.h in full; inspected the runner's compiler selection and the original pull request diff. Other changed files were reviewed through their diffs, not read in full. Tooling's AGENTS.md was unavailable; no review checklist was present.; nothing run (a code review).Checked and fine:
Testing: No commands from the project or tests were run. The author reports Windows game testing and local test-suite results; CI has not run on this head, and the Linux Test Farm request awaits approval.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, after its fix. Nothing was run; a maintainer decides.