fix(mods): a code module the image cannot place is left out with why, instead of stopping the build #3168
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3168
Loading…
Reference in a new issue
No description provided.
Delete branch "snaildev99/Longshot:mods/module-parity"
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*) in after the game's own, and leaves out one that writes codememory at its start-up. A module with other faults stopped the whole build instead (an unknown relocation, a
relocation pointing outside the module or naming no symbol), or was built in and failed later in the game (a system
call the host does not answer, a
.textthat isn't whole instructions, segments out of order or overlapping, morebytes than its header's slot, a relative relocation that overflows at its base, an import nothing provides).
With this change such a mod module is left out, with why in the build log and the Mods tab, and the build goes on. The
game's own modules still stop the build on the same faults, as before. The checks are the ones #984 had on the old
builder:
game-files::modules::refusal: the segment layout, relocation kinds and targets, and system calls againstHOST_SVCS(a test keeps it equal to the caseshost/hos_svc.c'sa64_svcanswers), in the code the module runs:each straight run of
.textfrom a place code starts (its start, unwind starts, symbols,DT_INIT/DT_FINI,relative relocation targets, branch and call targets), so an
svc's bits in a literal after a return are not one;image::build::take_modules, once the bases are known: a relative relocation that overflows at the module'sbase, or a name one of its symbol relocations binds (strongly, by the image relocation's own rule) that nothing
placed before the module exports (the game, an earlier mod's module, or the host). Such a module takes no base,
exports nothing and takes none of the modules' 128 MB (counted here now, after these checks), and
exefs_modules.txtis written after these checks so the Mods tab agrees.The discovery also takes an address in
mainthat a mod's module builds withMOVZ/MOVK, adds to another registerand then calls (
BLR) or jumps to (BR), as an entry: a hook that calls into the game that way lands on a function,and one that jumps back into a game function after its patch's stub lands on an entry. A call strictly inside a
function
main's unwind table covers is not taken; a sum only added to a pointer is not an address.How to see it on
main:a_mod_module_the_image_cannot_place_is_left_out_and_the_build_finishes(image) builds amodule with an unknown relocation kind; on
mainit is built in with an empty note.This ports the rest of #984's module checks and #940's notes. Read first:
builder/crates/game-files/src/modules.rs(
refusal),builder/crates/image/src/build.rs(take_modules), thenbuilder/crates/cfg/src/entries.rs.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, with four code mods: the three valid
modules (HUD of Time, Rupee Counter, Edge-to-Edge) pass every new check, are built in, and work in the game; Ragdoll
Button is left out by
main's existing rule (it writes code memory at start-up), with that reason. No mod on this PChas a module the new checks leave out: those cases are the game-free tests below, which build each fault into a
synthetic module.
After the rebase onto
main@3a0f7d8f7and the review's fixes, the same profile again, built with this PR @2335ed09eand the other ports merged (929bf025a): HUD of Time (subsdk9), Rupee Counter (subsdk7) andEdge-to-Edge (
subsdk6) built in, 7 modules with no unresolved imports, Ragdoll Button left out for itsMapProcessMemorycall as before, and the three mods at work in Kakariko.mise run test, run locally while CI's run waits for approval (Windows 11,tests/run_all.py --strict, 12 checks atonce), here @
2335ed09eand onmain@3a0f7d8f7: 847 passed, 12 failed, 52 skipped here; 845 passed, 14 failed, 52skipped on
main. All 12 also fail onmain: test_dlssg, test_fsr4_tune, test_lifter, test_no_game_title,test_no_map_reload, test_present_acquire_ahead, test_present_geometry_trace, test_present_held_image,
test_present_image_free, test_sync_watch_event, test_turf_budget, test_ui_renderer.
After the review (rebased on
main@3a0f7d8f7),cargo test -p cfg -p game-files -p imageon Windows 11 @2335ed09e:cfg 5 + 9, game-files 63 + 1 + 3, image 14 + 1 passed, none failed;
cargo clippy -p cfg -p game-files -p image --all-targets --no-deps -- -D warningsandcargo fmt --checkof the three are clean. The review's fixes each have atest that failed on this pull request's code before the fix and fails again with the fix's key line undone:
a_mod_module_s_added_movz_movk_constant_in_main_is_an_entry(cfg: a sum only added to a pointer, read throughbefore the call, called too late, or called inside an unwound function is no entry; a call of that function's start
and a jump back into it are),
a_literal_shaped_like_a_system_call_is_not_one(game-files),only_a_name_a_relocation_binds_is_an_importanda_module_left_out_at_placement_leaves_its_room_to_later_ones(image).
Still to test:
mainon the same PC(measurement, the review's L5)
Measurements
Verdict: Fixed: each test that fails on
mainpasses, and valid modules are still built in. Nothing changesper frame; the checks run in the build, once per mod module.
Setup:
cargo testofcfg,game-filesandimageon Windows 11; the before side is the test commit (8dfec6ef0),which is
main's code with the tests.3a0f7d8f72335ed09ecargo test -p game-files modules::(system call, segment layout, a literal shaped like a system call)cargo test -p image a_mod_module_the_image_cannot_placecargo test -p cfg a_mod_module_s_added_movz_movkThe review's two
take_modulestests (only_a_name_a_relocation_binds_is_an_import,a_module_left_out_at_placement_leaves_its_room_to_later_ones) are in the fix commit:mainhas notake_modules.Both failed on this pull request's code before the review's fix (21403aad3) and pass here.
Risks
build now lets it finish without the module.
main).main: an address ofmaina module calls or jumps to some other way (a sumstored and called later, a call more than eight instructions after its
ADD) gets no entry, and the game stops atthat call; a system call the host lacks in code the module reaches only through a jump table, or from the game's
patched code, does not leave the module out, and the game stops there if it runs.
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
tests/that reads changed source is updated, not weakened. (tests/test_dlss_nr_forwarder.pyreadsbuild-driver/src/build.rs: it passes unchanged)mainand passes here.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, Rupee Counter and Edge-to-Edge's modules passed the new checks, were built in and worked in the game; Ragdoll Button was left out by main's existing rule (code memory written at start-up), with its reason. No mod on this PC trips the new checks: those cases are the game-free tests.
Possibly related, for context:
mise run test, run locally while CI's run waits for approval (Windows 11, run_all --strict, 12 checks at once), here @
4a22e212dand on main @f577a1aea: 833 passed, 12 failed, 51 skipped here; 820 passed, 25 failed, 51 skipped on main. All 12 also fail on main (named in Testing). Box ticked; the check that reads build-driver's build.rs (test_dlss_nr_forwarder) passes unchanged, box added.WIP: fix(mods): a code module the image cannot place is left out with why, instead of stopping the buildto fix(mods): a code module the image cannot place is left out with why, instead of stopping the buildRecommendation: Request changes. The new checks can leave out usable modules, and the entry heuristic can split a game function at an unrelated address. These regressions need fixes before merge.
The change leaves out several kinds of faulty mod module with a reason, allowing the build to continue, and adds discovery of some game addresses used by mods. It also adds rejection and discovery heuristics that can affect otherwise usable mods and game code.
Blocking (3): must change before merging.
builder/crates/cfg/src/entries.rs:109: An arbitrary arithmetic constant is treated as a game function entry without establishing that the other operand is main's base or that the result is called. (more on the line) Do: Establish that the value forms a game code address used as a call target before adding it, and exclude addresses inside known function spans unless they are known function starts.builder/crates/image/src/build.rs:168: The missing-import check rejects every undefined strong symbol, including symbols that no relocation actually uses. (more on the line) Do: Check the symbols referenced by applicable relocations, using the same binding rules as image relocation, rather than all undefined symbol-table entries.builder/crates/game-files/src/modules.rs:152: Scanning every word of text as an instruction can mistake embedded data for an unsupported system call. (more on the line) Do: Use code discovery or another reliable indication of executable instructions before rejecting a module for an unsupported SVC; add a test with an SVC-shaped literal.Should fix (1): not blocking, but worth doing before merging.
builder/crates/image/src/build.rs:125: A module rejected during placement has already consumed the collection limit, so its space is not made available to later modules. (more on the line) Do: Apply the aggregate limit after placement checks, counting only modules actually taken, and reconsider later modules in list order.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/cfg/src/entries.rs:94: The added discovery scan and any additional lifted entries have no build-time or build-size comparison. (more on the line) Do: Compare clean build time and size and a one-function incremental build against the base with representative code mods on the same machine. (@fixdoes its part first: Avoid allocating a complete word vector for each mod's text and avoid adding candidates that are not needed.)@fixcan do L1, L2, L3, L4 and L5.Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the synthetic cases: the description reports four previously failing tests passing, and three valid modules working in a Windows game run. No real module triggering a new rejection was tested in game, and build-time cost was not measured.
Other systems: The builder and discovery changes apply on Windows and Linux and can affect every GPU because they alter the generated game image and code. The reported game run covers Windows with NVIDIA; no Linux build-time comparison was provided.
Switches: None.
Reviewed:
4a22e212d8againstf577a1aeab: Read the repository rules and PR template, commit messages, diff, changed module and discovery logic, image builder, and relevant callers and discovery walk. Skimmed the remaining changed driver and test code and documentation; did not read every changed code file in full. No recorded runs, translations or generated files changed.; nothing run (a code review).Checked and fine:
Testing: No commands from the project or tests were run, as requested. The author reports game-free test results against the base and a Windows in-game run; CI was still running.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, as snaildev99 took it out of WIP. Nothing was run; a maintainer decides.
@ -74,0 +91,4 @@return BTreeSet::new();};let mut found = BTreeSet::new();for m in modules.iter().filter(|m| is_mod_module(&m.name)) {L5 (should fix): The added discovery scan and any additional lifted entries have no build-time or build-size comparison.
When: A build with code mods scans their text and can generate additional game functions; the submitted results do not show the effect on a clean build or an incremental build.
Do: Compare clean build time and size and a one-function incremental build against the base with representative code mods on the same machine.
@ -74,0 +106,4 @@if w3 & 0xFF20_FC00 == 0x8B00_0000 && (reg == rm || reg == rn) && rm != rn {let v = u64::from(((w >> 5) & 0xFFFF) | (((next >> 5) & 0xFFFF) << 16));if v % 4 == 0 && (lo..hi).contains(&v) {found.insert(main.base + v);L1 (blocking): An arbitrary arithmetic constant is treated as a game function entry without establishing that the other operand is main's base or that the result is called.
When: A mod constructs a four-aligned size or structure offset that happens to fall inside main's text and adds it to a data pointer. The resulting candidate can land inside a game function; discovery stops walking at entries, splitting that function and potentially changing its generated control flow.
Do: Establish that the value forms a game code address used as a call target before adding it, and exclude addresses inside known function spans unless they are known function starts.
@ -87,0 +149,4 @@let code = nso.image.get(text.offset as usize..(text.offset + text.size) as usize).unwrap_or_default();let mut writers = Vec::new();let mut missing = std::collections::BTreeSet::new();for w in code.as_chunks::<4>().0.iter().map(|b| u32::from_le_bytes(*b)) {L3 (blocking): Scanning every word of text as an instruction can mistake embedded data for an unsupported system call.
When: A working module has a literal or other non-executed word in its text segment whose bits encode SVC 0x7f. The module is left out even though execution never invokes that system call.
Do: Use code discovery or another reliable indication of executable instructions before rejecting a module for an unsupported SVC; add a test with an SVC-shaped literal.
@ -93,0 +122,4 @@if let Some(why) = cannot_place(&nso, base, &exports, &opts.hle) {file.built = false;file.note = why.clone();log(format!("exefs: left out {}/exefs/{} ({why})", file.mod_name, file.file));L4 (should fix): A module rejected during placement has already consumed the collection limit, so its space is not made available to later modules.
When: Earlier modules fill the 128 MiB collection limit, but one is subsequently rejected for a missing import. A later valid module was already marked left out for exceeding the limit and is skipped here, even though the rejected module takes no image space.
Do: Apply the aggregate limit after placement checks, counting only modules actually taken, and reconsider later modules in list order.
@ -93,0 +165,4 @@let own: BTreeSet<&str> =nso.symbols.iter().filter(|s| s.defined && !s.name.is_empty()).map(|s| s.name.as_str()).collect();let mut lost = BTreeSet::new();for s in &nso.symbols {L2 (blocking): The missing-import check rejects every undefined strong symbol, including symbols that no relocation actually uses.
When: A module contains an unused undefined strong symbol in its dynamic symbol table but has no relocation referencing it. Image relocation never resolves that symbol, so the module could run on the base; this check instead leaves the whole module out.
Do: Check the symbols referenced by applicable relocations, using the same binding rules as image relocation, rather than all undefined symbol-table entries.
4a22e212d82335ed09edRebased onto main @
3a0f7d8f7for the base conflict; here @2335ed09e. Tested (the game again on this head),mise run test(against main @3a0f7d8f7: every failure also fails on main) and Measurements are updated. Answers to the review below.L1 Fixed in
2335ed09e(cfg/src/entries.rs,mod_address_constants). A mod's MOVZ+MOVK constant added to anotherregister is now an entry only when the sum is then called (BLR) or jumped to (BR) within eight instructions, with
nothing writing that register or branching in between: a size or field offset added to a data pointer, or a sum read
through before a call, no longer counts. A called address strictly inside a function main's unwind table covers is
dropped (a span's own start is kept; it is the filter
code_address_refsalready had, now shared asdrop_inside).A jump (BR) into such a function is kept on purpose, so that part of the "Do" is not taken: the scan is there for a
module that jumps back into a game function after its patch's stub (#984's reason for it in the old
nx2/exefs_modules.py: "a module jumps back into the middle of a game function after its patch's stub, and anindirect jump needs an entry"), and without that entry the jump stops the game at the lookup. Whether the other
operand is main's base can't be shown from the code alone; being branched to is what the check can see. Cases in
a_mod_module_s_added_movz_movk_constant_in_main_is_an_entry(8dfec6ef0).L2 Fixed in
2335ed09e(image/src/build.rs,missing_imports). The check now walks the module's ABS64, GLOB_DAT andJUMP_SLOT relocations and asks only about the names they bind, with the image relocation's rule for weak (the module's
last symbol of that name: one helper,
weak_bindings, thatrelocatenow uses too). An undefined symbol norelocation names is never bound and no longer leaves the module out. Test (
2335ed09e: it callstake_modules, whichmainlacks):only_a_name_a_relocation_binds_is_an_import, an unbound undefined symbol and a name bound weak, thenthe image built from them with nothing unresolved.
L3 Fixed in
2335ed09e(game-files/src/modules.rs,runs). A system call the host lacks now counts only in code themodule runs: each straight run of
.textfrom a place code starts (its start, unwind starts, symbols, DT_INIT andDT_FINI, relative relocation targets, the target of any branch or call) up to a B, BR, RET, BRK, HLT or UDF. A literal
after a function's return is in no run. No decoder dependency: the few branch encodings are read by mask, as the SVC
already was. The code-memory system calls are still looked for in every word, as on
main. Test (8dfec6ef0):a_literal_shaped_like_a_system_call_is_not_one, ansvc #0x7fword after aretthat anldrreads is taken; thesame word as a branch target leaves the module out.
L4 It held:
collectcounted a module against the 128 MB beforetake_modulescould leave it out. Fixed in2335ed09e:the limit moved from
collectintotake_modules, after the placement checks, counting only the modules taken, inthe list's order. Test:
a_module_left_out_at_placement_leaves_its_room_to_later_ones(64 MB files: the room of oneleft out for its import goes to a later module, and one past the limit is still left out).
L5 The code part is in
2335ed09e: the scan reads each mod module's.textin place (no word vector per module) andtakes only sums that are called or jumped to, so it adds fewer candidates; a call that the unwind table already starts
adds nothing new. The clean and one-function build time and size with code mods, against
main, is still to run onthe same PC.
Still to test now lists the measurement the review asked for, which the answers above say is still to run.
@rebase
I rebased this pull request onto
main(5d7e0668c8): the new head isc38103b145.Conflicts resolved
builder/crates/build-driver/tests/driver.rsNothing was built or run: read the resolution before merging.
Automated rebase by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
2335ed09edc38103b145Recommendation: Approve. The code is ready: only testing is left. The fixes address the reported code defects, and the reported synthetic tests show the intended behavior. The build-time and build-size comparison remains outstanding. Recommend approval; not approved on the forge.
Mod modules the builder cannot place are left out with a reason, allowing the build to continue. The additional scans and placement checks add build work whose cost has not yet been measured.
Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
builder/crates/cfg/src/entries.rs:94: The added discovery scan and any additional lifted entries have no build-time or build-size comparison. When: A build with code mods scans their text and can generate additional game functions; the submitted results do not show the effect on a clean build or an incremental build. Do: Compare clean build time and size and a one-function incremental build against the base with representative code mods on the same machine. (@fixdoes its part first: Avoid allocating a complete word vector for each mod's text and avoid adding candidates that are not needed.)@fixcan do L5.Since the last review (on
4a22e212d8)Resolved: L1 (An arbitrary arithmetic constant is treated as a game function entry without establishing…); L2 (The missing-import check rejects every undefined strong symbol, including symbols that no…); L3 (Scanning every word of text as an instruction can mistake embedded data for an unsupported…); L4 (A module rejected during placement has already consumed the collection limit, so its space…)
Still open: L5 (The added discovery scan and any additional lifted entries have no build-time or build-siz…)
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 for the reported synthetic cases: the description reports three game-files tests, one image test and one cfg test failing on main and passing with the change. Three valid code mods also reportedly worked in game; no real mod tested triggered the new refusal checks.
Other systems: The checks affect code-mod collection, image placement and entry discovery on Windows and Linux; the reported game run is on Windows. GPU rendering, audio, input and saves are not directly changed.
Switches: None.
Reviewed:
c38103b145against5d7e0668c8: Read the repository rules and pull request template, the supplied comments, ledger and range-diff. Read cfg/src/entries.rs; the combined output for game-files/src/modules.rs and image/src/build.rs was truncated, and the remaining changed files and dependent code were not read in full.; nothing run (a code review).Checked and fine:
Testing: No commands that build or run tests were executed. The author reports Windows game and synthetic test results; CI has not run on this head.
Overlaps and merge order: Not established.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, after its rebase. Nothing was run; a maintainer decides.