Merge task/abilityguard: a stale Recall / Ultrahand target is dropped only where the game would fault on it, replacing the 4 GiB target check, and the model scene report loop gets a bound (#1005, #1137, #1145)

The ability guard (runtime/ov_ability.c) checks the holder, the actor, its component and the reference word the game
writes, so it catches every bad target seen in the reports: -1, 0x80, an empty bounding box (FLT_MAX) and a vtable in
main's data. The optref site at main+0x1bd01c8 (1.2.1: 0x1622bf4) is dropped: set ability patches the same address and
must win there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-12 19:57:07 +03:00
commit e6839387fc
8 files changed
+161 -19

No files matched your search

+6 -3
View File
@@ -32,7 +32,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o
// logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line, and gets a
// description in totk.ini.example or docs/settings.md (tests/settings_doc_check.py checks both).
static const char *const known[] = {
"AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"AB", "ABILITY_GUARD", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT",
"BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS",
"BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP",
@@ -55,11 +55,11 @@ static const char *const known[] = {
"MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE",
"MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK",
"MV_SURVEY",
"ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POINTER_IMAGE",
"ONE_PER_CORE", "OPERATION_MODE", "OPTREF_GUARD", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POINTER_IMAGE",
"PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES",
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_ASYNC", "REC_END", "REC_SPIN_US", "REEXEC", "REFLEX",
"RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHUNKS", "REPLAY_CHUNK_DRAWS", "REPLAY_SKEW_US", "REPLAY_STATE_CHECK", "REPLAY_WORKERS",
"REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"REPLAY_CHECK", "REPORT_GUARD", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE",
"SHADER_MEMO", "SHADOW_DEFER", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHADOW_SNAP", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR",
"SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS",
@@ -109,6 +109,7 @@ static const struct { const char *key, *section; } sections[] = {
{"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"VB_USED", "performance"}, {"DESC_TEMPLATE", "performance"},
{"SYNC_AHEAD", "performance"}, {"SHADOW_DEFER", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"},
{"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"},
{"OPTREF_GUARD", "performance"}, {"ABILITY_GUARD", "performance"}, {"REPORT_GUARD", "performance"},
{"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"},
{"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"},
{"MV_SURVEY", "diagnostics"},
@@ -204,6 +205,8 @@ static const CfgMeta metas[] = {
{"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART},
{"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "90", "0..100", RESTART},
{"TEX_RELEASED_MIB", "256", "0..65536", RESTART}, {"TEX_BLOCK_MIB", "64", "0..1024", RESTART}, {"VMA_BLOCK_MIB", "64", "0..1024", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART},
// crash guards: code patch sets (recomp/patches-v*.txt) over game reads that crash on stale data
{"OPTREF_GUARD", "1", "0|1", RESTART}, {"ABILITY_GUARD", "1", "0|1", RESTART}, {"REPORT_GUARD", "1", "0|1", RESTART},
// render fixes
{"LOD_GUARD", "1", "0|1", RESTART}, {"FIX_C0", "1", "0|1", RESTART}, {"FIX_ZERO", "1", "0|1", RESTART},
{"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"PIPE_MEMO", "1", "0|1", RESTART}, {"TEX_MEMO", "1", "0|1", RESTART},
+104
View File
@@ -0,0 +1,104 @@
// 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 the actor's +0x10, +0x24 and
// +0x1a8), reads [[x21+0x228]+0xb0] (main+0x9a89d4) and takes a reference on that (main+0x2b8464: an atomic
// read-modify-write of the word at +0x90). The holder can be reused memory by then, 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; this code is ours.
//
// A target is dropped only where the game's own code would fault on it: the holder or the actor unreadable, the
// component pointer unreadable, or the reference word unreadable or in a module's code or read-only data. Every
// other target goes on exactly as the game reads it, so no target the game can handle is ever lost. UltraTears' test
// of a live actor (its link record [actor+0x1a8] points back to it at +0x40 and carries its id at +0x48) is not in the
// game's code: the game links an actor whose link record is null without complaint. It runs here as a log line only,
// to show in players' logs whether it would ever reject a live target.
//
// 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, or 0 for a dropped target, which the cbz skips 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)
#define ACTOR_SPAN 0x230 // the actor fields the function and its callees read: +0x10 .. +0x22f
static int readable(uint64_t a, uint64_t n) { return a && !(a & 3) && mem_guest_readable(a, n); }
static uint64_t u64_at(uint64_t a) { return *(volatile uint64_t *)(uintptr_t)a; }
static uint32_t u32_at(uint64_t a) { return *(volatile uint32_t *)(uintptr_t)a; }
// [a, a+4) inside a module's code or read-only data: a write there faults.
static int read_only_module(uint64_t a) {
for (int i = 0; i < ldr_count(); i++) {
const RtLoaded *m = ldr_module(i);
if (a - m->base < m->size) return a - m->base < m->data_off;
}
return 0;
}
// NULL when the game's code can follow the actor without a fault, else what would fault.
static const char *faults(uint64_t actor) {
if (!readable(actor, ACTOR_SPAN)) return "actor not readable";
uint64_t comp = u64_at(actor + 0x228);
if (!comp) return NULL; // main+0x9a89d4 returns 0 for it and the game stops there
if (!readable(comp + 0xb0, 8)) return "component not readable";
uint64_t ref = u64_at(comp + 0xb0);
if (!ref) return NULL; // the game's cbz x0 skips the reference
if (!readable(ref + 0x90, 4) || read_only_module(ref + 0x90)) return "reference word not writable";
return NULL;
}
// UltraTears' live-actor test, for the log: NULL when it passes.
static const char *not_live(uint64_t actor) {
uint64_t link = u64_at(actor + 0x1a8);
if (!readable(link + 0x40, 0x10)) return "link record not readable";
if (u64_at(link + 0x40) != actor) return "link record points elsewhere";
if (u32_at(link + 0x48) != u32_at(actor + 0x10)) return "link record has another id";
return NULL;
}
static void host_ability_target(Cpu *c) {
static uint64_t checked, dropped, mismatched;
uint64_t holder = c->x[8], actor = 0;
const char *why = NULL;
if (!readable(holder, 8)) why = "holder not readable";
else if ((actor = u64_at(holder)) && !(actor & 1)) why = faults(actor);
if (why) {
c->x[9] = 0;
uint64_t n = __atomic_add_fetch(&dropped, 1, __ATOMIC_RELAXED);
if (n <= 20 || !(n & (n - 1)))
rt_log("ability: target dropped, the game would fault on it (%s): holder %#llx actor %#llx; %llu dropped",
why, (unsigned long long)holder, (unsigned long long)actor, (unsigned long long)n);
return;
}
c->x[9] = actor;
if (!actor || (actor & 1)) return;
uint64_t k = __atomic_add_fetch(&checked, 1, __ATOMIC_RELAXED);
const char *odd = not_live(actor);
if (!odd) {
if (k == 1) rt_log("ability: first target checked (TOTK_ABILITY_GUARD=0: no check)");
return;
}
uint64_t n = __atomic_add_fetch(&mismatched, 1, __ATOMIC_RELAXED);
if (n <= 20 || !(n & (n - 1)))
rt_log("ability: target kept although the live-actor test fails (%s): actor %#llx; %llu of %llu targets", odd,
(unsigned long long)actor, (unsigned long long)n, (unsigned long long)k);
}
__attribute__((constructor)) static void ability_register(void) {
rt_register_host(HOST_ABILITY_TARGET, host_ability_target);
}
#endif
+3 -2
View File
@@ -1019,9 +1019,10 @@ static bool validation_layer_present(void) {
// Every setting the menu shows back to its default (the environment's keys stay), written to totk.ini.
static void reset_all(void) {
const char *keys[128];
const char *keys[256]; // above the number of rows in cfg.c metas (128 on 2026-11-11)
int n = 0;
for (const CfgMeta *m; n < 128 && (m = cfg_meta_at(n)); n++) keys[n] = m->key;
for (const CfgMeta *m; n < 256 && (m = cfg_meta_at(n)); n++) keys[n] = m->key;
if (cfg_meta_at(n)) rt_log("menu: reset covers the first %d settings only; raise keys[] in reset_all", n);
int done = cfg_reset(keys, n);
opt_save_soon();
rt_log("menu: reset %d settings to their defaults", done);