From 8235ea8d3405d0cd5503816de43b574056dd3d87 Mon Sep 17 00:00:00 2001 From: Tempus3214 <128173221+Tempus3214@users.noreply.github.com> Date: Thu, 5 Nov 2026 05:43:25 -0300 Subject: [PATCH] Fix the crash when the camera turns vertically while climbing a creature The ClimbObj pitch hook replaced the pre-indexed load ldr s1, [x22, #0x88]! and wrote the write-back to x22 from the host call. The lifter keeps X19-X29 in locals across a call and does not reload them, so the write-back was lost and the following str s0, [x22] wrote the pitch over the camera object's vtable pointer; the next virtual call crashed (seen climbing a Stone Talus). The mov x22, x19 before the load is now patched to add x22, x19, #0x88, the hook performs a plain ldr s1, [x22], and load hooks no longer support write-back loads at all. Co-Authored-By: Claude Opus 5.5 --- recomp/patches-v143.txt | 6 +++++- runtime/mouse_direct.c | 17 +++++++++-------- tests/mouse_direct_check.py | 4 ++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/recomp/patches-v143.txt b/recomp/patches-v143.txt index ede32ae6..8f7ebf35 100644 --- a/recomp/patches-v143.txt +++ b/recomp/patches-v143.txt @@ -146,7 +146,11 @@ site mouse_direct main 23c2964 bd404be1 945c7587 # CameraFinder: ldr s1, [sp, site mouse_direct main 23c3dd0 bd442301 945c706c # ClimbObj: ldr s1, [x24, #0x420] site mouse_direct main 23c3dd8 bd409261 945c706a # ClimbObj: ldr s1, [x19, #0x90] (yaw) site mouse_direct main 23c3d64 bd442701 945c7087 # ClimbObj: ldr s1, [x24, #0x424] -site mouse_direct main 23c3d70 bc488ec1 945c7084 # ClimbObj: ldr s1, [x22, #0x88]! (pitch) +# ClimbObj's pitch is a pre-indexed load, ldr s1, [x22, #0x88]!, whose write-back the code after it needs (ldp and +# str through x22). A host call cannot write x22: the lifter keeps X19-X29 in locals across a call. So the mov before +# it adds the 0x88 instead (x22 = x19 + 0x88) and the hook loads [x22] with no write-back. +site mouse_direct main 23c3d5c aa1303f6 91022276 # ClimbObj: mov x22, x19 -> add x22, x19, #0x88 +site mouse_direct main 23c3d70 bc488ec1 945c7084 # ClimbObj: ldr s1, [x22, #0x88]! (pitch), as ldr s1, [x22] site mouse_direct main 23da694 bd4422c1 945c163b # Wakeboard: ldr s1, [x22, #0x420] site mouse_direct main 23da698 bd4426c0 945c163a # Wakeboard: ldr s0, [x22, #0x424] (stick y, kept) site mouse_direct main 23da840 bd40da60 945c15d0 # Wakeboard: ldr s0, [x19, #0xd8] (pitch) diff --git a/runtime/mouse_direct.c b/runtime/mouse_direct.c index 8d9e7c07..71631b8b 100644 --- a/runtime/mouse_direct.c +++ b/runtime/mouse_direct.c @@ -586,9 +586,9 @@ static void hand_input(Cpu *c) { // other bow cameras (Bow, Bow_Crouch, Bow_Zoom, Bow_Horse, Bow_Zoom_Horse) are Aiming. // Tail (wall climbing, parameter set Climb; main+0x23d4950): the stick as angle and length; in its manual state // (+0x16c == 4) yaw +0xc0 and pitch +0xb4 -= x / y * speed * frame ratio. ClimbObj (main+0x23c3a98): pitch +0x88 -// (a pre-indexed load) and yaw +0x90 likewise. WaterfallClimb (main+0x23dce00): yaw = the camera's yaw - x * speed -// * frame ratio, kept at [x29-0x34] and loaded into w8 (the base); its vertical axis moves the camera's height -// (ManualHeightMin..Max), not an angle, and stays stick input (watched). +// (through x22, set by a patched mov) and yaw +0x90 likewise. WaterfallClimb (main+0x23dce00): yaw = the camera's +// yaw - x * speed * frame ratio, kept at [x29-0x34] and loaded into w8 (the base); its vertical axis moves the +// camera's height (ManualHeightMin..Max), not an angle, and stays stick input (watched). // EventMultiTalk (main+0x23c531c) and EventTalkManual (main+0x23c8ffc): pitch / yaw fields -= stick * speed * // frame ratio; MultiTalk's x load is also its input test (EPSILON). // ShieldSurfing (main+0x23d0088) and Wakeboard (main+0x23da61c), one template: the stick as angle and length; yaw @@ -656,7 +656,9 @@ static const LoadSite load_sites[] = { {0x23c3dd0, 0xbd442301, MODE_CLIMB, 0, ROLE_STICK, 24, 0}, // ldr s1, [x24, #0x420] {0x23c3dd8, 0xbd409261, MODE_CLIMB, 0, ROLE_BASE, 0, 0}, // ldr s1, [x19, #0x90] (yaw) {0x23c3d64, 0xbd442701, MODE_CLIMB, 1, ROLE_STICK, 24, 0}, // ldr s1, [x24, #0x424] - {0x23c3d70, 0xbc488ec1, MODE_CLIMB, 1, ROLE_BASE, 0, 0}, // ldr s1, [x22, #0x88]! (pitch; x22 = x19) + // The game's pitch load is ldr s1, [x22, #0x88]! (bc488ec1). recomp/patches-v143.txt makes the mov before it + // set x22 = x19 + 0x88, so the hook performs it as the plain load below and writes no register but s1. + {0x23c3d70, 0xbd4002c1, MODE_CLIMB, 1, ROLE_BASE, 0, 0}, // ldr s1, [x22] (pitch +0x88) {0x23da694, 0xbd4422c1, MODE_WAKEBOARD, 0, ROLE_STICK, 22, EPSILON}, // ldr s1, [x22, #0x420] {0x23da698, 0xbd4426c0, MODE_WAKEBOARD, 1, ROLE_STICK, 22, KEEP_STICK}, // ldr s0, [x22, #0x424] {0x23da840, 0xbd40da60, MODE_WAKEBOARD, 1, ROLE_BASE, 0, 0}, // ldr s0, [x19, #0xd8] (pitch) @@ -697,8 +699,9 @@ static const LoadSite load_sites[] = { }; #define LOAD_SITES (sizeof load_sites / sizeof load_sites[0]) -// The original single-register 32-bit loads: LDR S (unsigned offset), LDUR S, LDR S pre-indexed (the base register -// becomes the address) and LDUR W (a float's bits in a general register). Their address: +// The original single-register 32-bit loads: LDR S (unsigned offset), LDUR S and LDUR W (a float's bits in a general +// register). Never a load with write-back: the hook is a call, and the lifted caller keeps X19-X29 in locals that a +// call does not reload (recomp/regs.py), so a base register written here would be lost. Their address: static uint64_t load_address(const Cpu *c, uint32_t insn) { unsigned n = (insn >> 5) & 31; uint64_t base = n == 31 ? c->sp : c->x[n]; @@ -707,7 +710,6 @@ static uint64_t load_address(const Cpu *c, uint32_t insn) { if (imm9 & 0x100) imm9 -= 0x200; return base + (int64_t)imm9; } -static int load_pre_index(uint32_t insn) { return (insn & 0xffe00c00u) == 0xbc400c00u; } static int load_to_general(uint32_t insn) { return (insn & 0xffe00c00u) == 0xb8400000u; } // Degrees a stick load left for its base load, on this thread: per mode and axis, for one camera object @@ -866,7 +868,6 @@ static void camera_load(Cpu *c) { } uint64_t at = load_address(c, s->insn); float f = guest_float(at); - if (load_pre_index(s->insn)) c->x[(s->insn >> 5) & 31] = at; // the pre-indexed load's write-back if (s->role == ROLE_SCALE) { // the ability camera's offset scale: P.y is this load, P.x the float before it plat_mutex_lock(&sample_lock); offset_scale.object = c->x[19]; diff --git a/tests/mouse_direct_check.py b/tests/mouse_direct_check.py index 389bd094..d4bc7f82 100644 --- a/tests/mouse_direct_check.py +++ b/tests/mouse_direct_check.py @@ -366,8 +366,8 @@ int main(void) { guest_float_store((uintptr_t)cam+0xc0,7); guest_float_store((uintptr_t)cam+0xb4,8); assert(LOAD(0x23d4b14,12)==pad_x && LOAD(0x23d4b1c,13)==pad_y); assert(fabsf(LOAD(0x23d4f34,0)-(7-100*DEG_PER_COUNT))<1e-5f && fabsf(LOAD(0x23d5028,1)-(8-50*DEG_PER_COUNT))<1e-5f); - // Climbing an object: the pitch's pre-indexed load writes its address back to x22 - SAMPLE(100,-50); c.x[24]=(uintptr_t)input; c.x[22]=(uintptr_t)cam; + // Climbing an object: the patched mov has set x22 = x19 + 0x88; the pitch load writes no register but s1 + SAMPLE(100,-50); c.x[24]=(uintptr_t)input; c.x[22]=(uintptr_t)cam+0x88; guest_float_store((uintptr_t)cam+0x88,4); guest_float_store((uintptr_t)cam+0x90,5); assert(LOAD(0x23c3d64,1)==pad_y && fabsf(LOAD(0x23c3d70,1)-(4-50*DEG_PER_COUNT))<1e-5f); assert(c.x[22]==(uintptr_t)cam+0x88);