mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-18 12:08:38 +00:00
Skip a stale Recall / Ultrahand target and an unbounded model scene report row instead of crashing (#1005)
Set ability (1.4.3 main+0x1bd01c4, 1.2.1 main+0x1622bf0): the target actor load becomes a host call that reads the holder safely and drops an actor failing the live-actor test (link record points back and carries its id), as UltraTears found; the game's own cbz skips it for that frame. The answer comes back in x9, since the lifted code keeps x21 in a C local across calls. Set report (1.4.3 only): the model scene report loop sends a negative handle or a row index from 0x8000 to the game's handle reset, the class Weeping of the Empire met (#52, #73). Both on by default, TOTK_ABILITY_GUARD=0 / TOTK_REPORT_GUARD=0 turn them off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d8a1d14104
commit
dde624e603
4 files changed
+121
-1
No files matched your search
@@ -49,7 +49,8 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method
|
||||
| a host core at 100% while the game waits | game spin loops without system calls | profile: which guest function spins |
|
||||
| crash on world load | sizes the game reads from the driver (command memory used) differ from the console's | the sizes the game asks for against the console's |
|
||||
| crash with one save only | the save itself; some crash on a real Switch | the same save on another build and, if possible, on hardware |
|
||||
| random crash after minutes to hours, access violation reading `0xffffffffffffffff` in a game function | an invalid value (-1, `0xffff0000ffffffff`) in an optional reference the game checks only for null; set `optref` in `recomp/patches-v143.txt` guards main+0x1bd0138 | the crash dump's faulting host instruction, the register it loads through and the field that register came from |
|
||||
| random crash after minutes to hours, access violation reading `0xffffffffffffffff` in a game function (Windows reports any non-canonical address so) | an invalid value (-1, `0xffff0000ffffffff`) in an optional reference the game checks only for null; in main+0x1bd0138 (Recall / Ultrahand picking a target) also a reused holder whose actor points into other memory. Sets `optref` and `ability` in `recomp/patches-v143.txt` guard both there | the crash dump's faulting host instruction, the register it loads through and the field that register came from; `ability: stale target skipped` lines in the log |
|
||||
| crash while using Ultrahand or aiming, in the model scene's report loop (1.4.3 main+0xa22bc..0xa234c) | a report row index read from the report buffer with no bound (Weeping of the Empire #52, #73); set `report` sends a negative handle or an index from 0x8000 to the game's own reset | the faulting address against the report rows base (x12) |
|
||||
|
||||
## Device lost, hangs, black window
|
||||
|
||||
|
||||
@@ -1133,3 +1133,10 @@ site grass main 1a30a98 52a83f48 @page:x8 b9409108 # mov w8, #31.25 -> ldr
|
||||
# x8 and x9 are dead at the branch target 0x1622c1c, which loads x8 again.
|
||||
set optref TOTK_OPTREF_GUARD 1
|
||||
site optref main 1622bec b4000188 937ffd09 8a290108 b4000188 # cbz x8 -> asr x9, x8, #63; bic x8, x8, x9; cbz x8
|
||||
|
||||
# Recall and Ultrahand target that is stale memory: set ability in patches-v143.txt, here for 1.2.1's main+0x1622bf0
|
||||
# (function main+0x1622b60; the callees main+0x809ecc, 0x842aa4 and 0x842d10 read the same actor offsets). The model
|
||||
# scene report-row guard (set report there) has no 1.2.1 site: this version does not have that loop's code shape.
|
||||
set ability TOTK_ABILITY_GUARD 1
|
||||
site ability main 1622bf0 f9400115 94c424dc # ldr x21, [x8] -> bl page+0xf60
|
||||
site ability main 1622bf4 b4000155 aa0903f5 b4000155 # cbz x21 -> mov x21, x9; cbz x21 (x9 dead after it)
|
||||
@@ -1128,3 +1128,30 @@ site grass main 2063460 52a83f48 @page:x8 b9409108 # mov w8, #31.25 -> ldr
|
||||
# any read; on the fall-through path x8 is unchanged.
|
||||
set optref TOTK_OPTREF_GUARD 1
|
||||
site optref main 1bd01c0 b4000188 937ffd09 8a290108 b4000188 # cbz x8 -> asr x9, x8, #63; bic x8, x8, x9; cbz x8
|
||||
|
||||
# Recall and Ultrahand target that is stale memory. The same function reads the target actor from the holder
|
||||
# (ldr x21, [x8]) and follows it into main+0x155ec, main+0x9a89d4 and main+0x2b8464 (an atomic write at +0x90). The
|
||||
# holder can be reused memory by then, so the actor points into .rodata, outside the address space or at other heap
|
||||
# data, and the game crashes reading 0xffffffffffffffff. UltraTears (kuraeow/ultratears, ability target guard) found
|
||||
# the class. The load becomes a call to runtime/ov_ability.c host_ability_target (page+0xf60): x21 is the holder's
|
||||
# first word when the holder is readable and the actor passes the live-actor test (its link record [actor+0x1a8]
|
||||
# points back to it at +0x40 with its id at +0x48), else 0, which the game's cbz x21 skips for this frame. The answer
|
||||
# comes back in x9 and the cbz site moves it to x21: the lifted code keeps x21 (callee-saved) in a C local across the
|
||||
# call. x9 is dead after 0x1bd01c8 on every path (written or clobbered by a bl before any read); x30 is saved by the
|
||||
# prologue and clobbered by the next bl anyway.
|
||||
set ability TOTK_ABILITY_GUARD 1
|
||||
site ability main 1bd01c4 f9400115 947c3f67 # ldr x21, [x8] -> bl page+0xf60
|
||||
site ability main 1bd01c8 b4000155 aa0903f5 b4000155 # cbz x21 -> mov x21, x9; cbz x21
|
||||
|
||||
# Model scene report rows (WoE, daniel-madrid-07/weeping-of-the-empire, MIT, games/totk/patches/sites_v143.py
|
||||
# reportguard; their #52 and #73: crashes while using Ultrahand and aiming). gsys::ModelScene::calcGPU_ is inlined in
|
||||
# main+0xa1f84; its per-object loop (0xa22bc..0xa234c) takes the object's report handle h (int16 at obj+0x98 + 2 * side),
|
||||
# exits for -1 only, reads row 2h+1 and then u32 n from row 2h, and follows n to row 2n with no bound (WoE's crash had
|
||||
# n = the float 0.05, a read 32 GB past the rows). n indexes a row pair as h does, so a valid one is below 0x8000.
|
||||
# A negative h (other than -1) or n >= 0x8000 goes to 0xa2340, where the game resets the handle to -1 after every use:
|
||||
# the object has coverage 0 this frame (stored at 0xa22cc) and the game asks for a new report, as after a normal read.
|
||||
# WoE's veneers exit at 0xa2344 instead, which keeps the bad handle for the next frames. No flags change; w4 is never
|
||||
# used in main+0xa1f84; x1, x16, x15 and w13, which 0xa2340 reads, hold what they hold on the game's own path there.
|
||||
set report TOTK_REPORT_GUARD 1
|
||||
site report main a22d8 2a010571 2a010571 37f80341 # orr w17, w11, w1, lsl #1; tbnz w1, #31, 0xa2340
|
||||
site report main a2310 531f7821 530f7c24 531f7821 35000184 # lsr w4, w1, #15; lsl w1, w1, #1; cbnz w4, 0xa2340
|
||||
@@ -0,0 +1,85 @@
|
||||
// Recall and Ultrahand target check (patch set "ability", recomp/patches-v143.txt and patches-v121.txt).
|
||||
//
|
||||
// main+0x1bd0138 on 1.4.3 (main+0x1622b60 on 1.2.1, the same code and actor offsets) runs while an ability picks its
|
||||
// target. Once the owner's request says ready, it loads the result holder from the owner (x8 = [x19+0x6b0]), the
|
||||
// target actor from the holder (x21 = [x8]), links the actor (main+0x155ec), reads [[x21+0x228]+0xb0]
|
||||
// (main+0x9a89d4) and takes a reference on that (main+0x2b8464, an atomic write at +0x90).
|
||||
// The holder can be reused memory by then: the actor pointer lands in .rodata, past the address space or on heap
|
||||
// memory that holds other data, and the game crashes reading 0xffffffffffffffff (Windows' report of a non-canonical
|
||||
// address) minutes to hours into play. UltraTears (kuraeow/ultratears, "ability target guard") found the class and
|
||||
// the test below; this code is ours.
|
||||
//
|
||||
// The first site replaces `ldr x21, [x8]` with a call here; the second, at the game's `cbz x21` right after it, becomes
|
||||
// `mov x21, x9; cbz x21`. The answer travels in x9 because the lifted caller keeps x21 (callee-saved) in a C local
|
||||
// across the call and reloads only caller-saved registers; x9 is dead there on every path. x9 gets the holder's first
|
||||
// word when the holder is readable, and 0 when the holder is unreadable or the actor fails the live-actor test: the
|
||||
// actor's link record [actor+0x1a8] points back to it (+0x40) and carries its id (+0x48 == [actor+0x10]), and every
|
||||
// pointer the game then follows is readable. The cbz skips a 0 target for this frame, as it skips an empty one. A null
|
||||
// or tagged (bit 0) word goes back unchanged: the game skips those itself. No other register changes.
|
||||
// Only an ability's target pick reaches this (hundreds of calls an hour), so the memory queries cost nothing.
|
||||
#include <stdint.h>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#if __has_include("patches.h")
|
||||
#include "patches.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_PATCH_PAGE
|
||||
|
||||
#define HOST_ABILITY_TARGET (RT_PATCH_PAGE + 0xf60)
|
||||
|
||||
// 0 when [a, a+8) cannot be read.
|
||||
static int rd64(uint64_t a, uint64_t *v) {
|
||||
*v = 0;
|
||||
if (!a || (a & 7) || !mem_guest_readable(a, 8)) return 0;
|
||||
*v = *(volatile uint64_t *)(uintptr_t)a;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int rd32(uint64_t a, uint32_t *v) {
|
||||
*v = 0;
|
||||
if (!a || (a & 3) || !mem_guest_readable(a, 4)) return 0;
|
||||
*v = *(volatile uint32_t *)(uintptr_t)a;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// NULL when the actor passes, else why it does not.
|
||||
static const char *stale(uint64_t actor) {
|
||||
uint64_t link, back, comp, sub, ref;
|
||||
uint32_t link_id, actor_id;
|
||||
if (!rd64(actor + 0x1a8, &link) || !rd32(actor + 0x10, &actor_id)) return "actor not readable";
|
||||
if (!rd64(link + 0x40, &back)) return "link record not readable";
|
||||
if (back != actor) return "link record points elsewhere";
|
||||
if (!rd32(link + 0x48, &link_id) || link_id != actor_id) return "link record has another id";
|
||||
if (!rd64(actor + 0x228, &comp)) return "actor not readable";
|
||||
if (!comp) return NULL; // main+0x9a89d4 returns 0 for it and the game stops there
|
||||
if (!rd64(comp + 0xb0, &sub)) return "component not readable";
|
||||
if (sub && !rd64(sub + 0x90, &ref)) return "component target not readable";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void host_ability_target(Cpu *c) {
|
||||
static uint64_t checked, skipped;
|
||||
uint64_t holder = c->x[8], actor = 0;
|
||||
const char *why = NULL;
|
||||
if (!rd64(holder, &actor)) why = "holder not readable";
|
||||
else if (actor && !(actor & 1)) why = stale(actor);
|
||||
if (!why) {
|
||||
c->x[9] = actor;
|
||||
if (actor && !(actor & 1) && __atomic_add_fetch(&checked, 1, __ATOMIC_RELAXED) == 1)
|
||||
rt_log("ability: first target checked, live (TOTK_ABILITY_GUARD=0: no check)");
|
||||
return;
|
||||
}
|
||||
c->x[9] = 0;
|
||||
uint64_t n = __atomic_add_fetch(&skipped, 1, __ATOMIC_RELAXED);
|
||||
if (n <= 20 || !(n & (n - 1)))
|
||||
rt_log("ability: stale target skipped (%s): holder %#llx actor %#llx; %llu skipped", why,
|
||||
(unsigned long long)holder, (unsigned long long)actor, (unsigned long long)n);
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void ability_register(void) {
|
||||
rt_register_host(HOST_ABILITY_TARGET, host_ability_target);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user