fix(mod-api): a write of Link's magic holds #3123
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!3123
Loading…
Reference in a new issue
No description provided.
Delete branch "glinesbdev/Longshot:fix/mod-api-link-magic"
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?
PlayerStatus.MPis a mirror: the game keeps Link's magic in a record among his components, keyed by thePlayerStatus.MPandPlayerStatus.MPMaxhashes, and sets the flag back to it whenever they differ. A mod'sgame.flags.setof the flag alone was undone on the next frame, and the meter never moved.A write of
PlayerStatus.MPnow also writes that record, found by its key in each of Link's components (its place among them is not fixed), and the flag follows it. An undo writes the record too. With Link not there, only the flag is written, and the log says so.WIP: fix(mod-api): a write of Link's magic holdsto fix(mod-api): a write of Link's magic holds@review
Recommendation: Request changes. The search can read uncommitted guest memory on Windows, and an undo can be reported complete even when the magic record was not restored.
The change attempts to make mod writes to Link’s magic persist by finding and updating the game’s magic record, including during undo. It adds a broad live-memory search on each such write; its behavior in the game has not been reported.
Blocking (1): must change before merging.
game/crates/api/src/services/writes.rs:529: Undo ignores failure to write the magic record and reports success based only on the flag write. (more on the line) Do: Treat the flag and record restoration as one undo operation: retain or report the undo as incomplete when the record cannot be restored, and retry when appropriate.Should fix (1): not blocking, but worth doing before merging.
game/crates/api/src/services/writes.rs:584: The search reads an entire 0x2000-byte window for every aligned word that looks like a pointer, but read_bytes checks only the reserved guest window, not whether the pages are committed. (more on the line) Do: Follow validated component pointers and bounded record extents, and use a non-faulting committed-memory read such as copy_out for memory whose lifetime is uncertain.Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
game/crates/api/src/services/writes.rs:475: There is no reported in-game check that the located record is the one the game uses or that the write and undo hold. (more on the line) Do: Run the write and undo in game on Windows and Linux, including a transition where Link is recreated, and report the observed values and meter behavior.@fixcan do L1 and L2.Questions (2) (not confirmed from the code; they don't hold the recommendation)
game/crates/api/src/services/writes.rs:600: Can the game read this magic record on another thread while the frame's mod writes are applied?game/crates/api/src/services/writes.rs:608: Is there a concrete write or undo case in the current implementation that fails and would be caught by the proposed behavioral test?Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven: the pull request and comments contain no game run showing a magic write persisting or an undo restoring it.
Other systems: The search reads live guest memory and runs on both Windows and Linux. Windows can commit untouched guest pages during the search. Undo uses the same search; no reported run covers either path.
Switches: None.
Reviewed:
aa4b75ff16against6318f4056e: Read the changed writes.rs file and diff, commit, repository rules, standards, pull request template, guest-memory access implementations, player lookup and journal undo paths. The review checklist and adjacent Tooling checkout were absent. Other related actor and layout references were searched, not read in full; no generated files or recorded runs were changed.; nothing run (a code review).Checked and fine:
Testing: No commands from the pull request or tests were run, as requested. CI was still running in the supplied status.
Overlaps and merge order: No overlapping pull requests were available in the supplied information.
Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.
@ -457,6 +472,23 @@ impl Services {after: p.attempt.value,},);if p.hash == magic_hash {L3 (should fix): There is no reported in-game check that the located record is the one the game uses or that the write and undo hold.
When: A mod writes MP during play and is then stopped; the submitted evidence does not show the meter retaining the new value or returning to the original value.
Do: Run the write and undo in game on Windows and Linux, including a transition where Link is recreated, and report the observed values and meter behavior.
@ -495,0 +526,4 @@let ok = flag_write(hash, kind, value);if ok && hash == flag_hash(MAGIC_FLAG) {// (Link's magic: his record too, or the game puts the undone value back)magic_home_write(value);L2 (blocking): Undo ignores failure to write the magic record and reports success based only on the flag write.
When: If the flag is still the mod’s value but the record cannot be found when the mod stops, the flag is restored and the journal removes the entry as undone. The unchanged record can then copy the mod’s value back into the flag on the next update, with no undo left to retry.
Do: Treat the flag and record restoration as one undo operation: retain or report the undo as incomplete when the record cannot be restored, and retry when appropriate.
@ -524,0 +581,4 @@for word in list.as_chunks::<8>().0 {let at = u64::from_le_bytes(*word);if at == 0 || at % 8 != 0 || !seen.insert(at) || !game_core::mem::read_bytes(at, 0, &mut buf) {continue;L1 (should fix): The search reads an entire 0x2000-byte window for every aligned word that looks like a pointer, but read_bytes checks only the reserved guest window, not whether the pages are committed.
When: On Windows, a list word can point inside the guest window near uncommitted or decommitted heap pages. The byte-by-byte read faults those pages in while searching, even though they are not part of a live component; repeated magic writes can commit unrelated guest memory.
Do: Follow validated component pointers and bounded record extents, and use a non-faulting committed-memory read such as copy_out for memory whose lifetime is uncertain.
@ask what is this, in a more human way. explain simply what it fixes
In reply to this comment
This fixes mods trying to change Link’s magic. Previously, the game would immediately restore the old amount, so the magic meter wouldn’t change. Now the change is also saved where the game keeps the magic amount, so it can stick; undoing the change updates it there too (
game/crates/api/src/services/writes.rs:475,game/crates/api/src/services/writes.rs:525).If Link’s magic record can’t be found, only the flag is changed and the game may restore the old value.
Automated answer by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
aa4b75ff1643b0eea251Rebased on main (
b77c2f4b3); the review's findings are addressed in43b0eea25.L2 (undo reported done without the record): the journal now asks the game whether a flag can be put back with what it keeps beside it (UndoGame::flag_ready, true by default). For PlayerStatus.MP that is whether Link's record is found; until it is, the undo waits (Undone::Later, as items wait for play: kept, counted as waiting in the Mods tab, tried again each frame) instead of being dropped. The record is written before the flag, and a miss leaves the flag as it is. Test: journal::tests::a_flag_not_ready_waits_and_is_put_back_once_it_is (three frames waiting with the flag untouched and the entry kept, then put back); it fails without the change.
L1 (the search could commit memory): every read of the search is now host_sys::mem::copy_out, the host's read that never faults, the one memory.read uses. A component is read a page at a time and its search stops at the first page with no memory behind it, so a list word that is no live component, or a page the game gave back, is never touched.
Test: links_magic_record_is_searched_for_without_touching_memory_the_game_has_not (a list with a word pointing at unmapped memory, one off the 8-byte grid, a component with no record, one whose second page is not mapped, and his: the record is found on his second page, and on the half component's first page when it has one there).
Question 1 (another thread reading the record during the write): the value is now written in one aligned store (mem::write::), so a reader on any thread sees the old value or the new one, never half of each.
Question 2: yes, the L2 case: Link not there when the mod stops (a load, the title screen). The test above is that case.
L3: see the game run below.
Game run (Windows 10 Home 22H2, 19045; this branch's own build,
43b0eea25onb77c2f4b3; a script-only test mod that sets PlayerStatus.MP to half the meter 60 frames into each map and logs it every 2 s):One more thing seen: the first write of the run, on the map the game shows at its start (before the save was loaded), read 480 again once the save's own values were loaded into Hyrule Field.
Not seen: the undo waiting while Link is not there (the new Later path); the unit test covers it.
Not run on Linux: no Linux machine here.
Checks on
43b0eea25: cargo test -p api -p game-core --lib (api 583 pass; the one failure, watch::tests::a_walked_reshade_folder_counts_a_change_once_two_walks_agree, also fails on main; game-core 409 pass), cargo clippy -p api -p game-core --lib --tests --no-deps -- -D warnings, cargo fmt --all --check, schema-gen --check.@review
Recommendation: Approve. The search avoids touching uncommitted component pages, and undo no longer reports success when the magic record cannot be written. The reported Windows game run shows the write and undo holding. Recommend approval; not approved on the forge.
Mods can set Link’s magic without the game immediately restoring the old value. Undo also restores the component record, and waits when that record is unavailable; this adds a bounded search and journal handling.
Since the last review (on
aa4b75ff16)Resolved: L1 (The search reads an entire 0x2000-byte window for every aligned word that looks like a poi…); L2 (Undo ignores failure to write the magic record and reports success based only on the flag…); L3 (There is no reported in-game check that the located record is the one the game uses or tha…)
Checks: Still running: 11 checks.
Also noticed (1; not asked for, not blocking)
More: does it help, other systems, switches, what was read
Does it help: Yes. In the reported Windows run, a write changed magic from 480 to 240 and held across samples and a map change; undo restored 480. When the game changed magic to 180 afterward, undo left that value alone.
Other systems: The search and undo affect mod flag writes on Windows and Linux. The reported game run covers Windows; Linux and the in-game waiting path have not been run.
Switches: None.
Reviewed:
43b0eea251againstb77c2f4b3e: Read AGENTS.md, CONTRIBUTING.md, the pull request template, the full diff and relevant memory and journal code. Skimmed the changed files and project standards; the separate Tooling rules were unavailable.; nothing run (a code review).Checked and fine:
Testing: Author reports a Windows game run and game-free tests; CI is still running. No tests were run during this review.
Overlaps and merge order: None known.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.