Files
totk-pc-recomp/tests/exefs_plugins_test.py
T
Valiantsin DzerakhandClaude Opus 5.5 3fe8d7a1d9 Mod plugins that hook the middle of game functions, swap pointers in the game's data, use the SD card or switch their hooks during play build in, and may call what the game itself calls; Zonai Fieldworks starts (#1388)
Zonai Fieldworks was refused at several steps; each is now handled:
1. The start-up asked GetInfo 6, 7, 16 and 17 and SetHeapSize; runtime/plugin_init.c serves them.
2. Its start-up calls main where the lift has no function start. The tool names those addresses (--entries-out) and
   the installer lifts again with them as `entry` lines, up to four rounds (installer/steps.py PLUGIN_ENTRY_ROUNDS).
3. Branch hooks into the middle of game functions (far-jump veneers) and pointer swaps in main's data are accepted
   hook kinds; the veneer targets seed the plugin's lift, and an Orig trampoline counts as a seed.
4. Code the start-up writes into the plugin's own .text (hook pads it switches to during play) is found by comparing
   the image before and after the init, and seeds the lift (init_code). Without it the game stopped with "no function
   at guest pc" when a feature switched on.
5. recomp/gen.py: a branch-out alternative on a flag-writing site leaves with the flags from before the site; an inline
   hook may sit on a blr.
6. nn::fs::MountSdCard mounts <profile>/sdcard (runtime/fs_host.c), and nn::settings language codes are answered
   (runtime/ov_system.c).

The import check now also allows what the game's main imports (--exefs, game_imports): the game runs on those paths,
so the runtime serves them. Left out: nn::ro, the C runtime start, the thread-local allocator hook and the shader
compiler. Break in code the hooks reach is allowed (an abort ends the game with the reason, as on the Switch), and
RenameFile, DeleteDirectoryRecursively, Commit and Unmount join the SD card calls. Checked: Zonai Fieldworks, The Second
Quest, Challenge Mode and Material Columns on 1.4.3, Full Inventory Expansion, Quick Weapon Durability and Magic of the
Kingdom on 1.2.1 accepted; UltraCam's init still crashes its service run. Plain lift unchanged; probes 4/4 on both
versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 10:35:59 +03:00

910 lines
48 KiB
Python

"""recomp/layout.py with and without mod plugins, and tools/exefs_plugins.py's check of a plugin service run record:
a synthetic exlaunch-shaped record with one inline hook that passes, and one bad record per rule; the check of the
code the hooks reach; the patch lines of the three hook kinds; and what recomp/patches.py and recomp/gen.py make of
those lines (the hook call, the stub's return, the unpatched copy a replace hook's trampoline reaches, the start-up
record in patches.h). The start-up comparison itself runs in the game (runtime/plugin_init.c plugins_start); its
mods.c side is in totk.exe --probe-mods. No game files.
Run with .venv (lz4 and numpy, as recomp/): python tests/exefs_plugins_test.py
"""
import hashlib
import json
import struct
import subprocess
import sys
import tempfile
import unittest
from types import SimpleNamespace
from unittest import mock
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / 'tools'))
sys.path.insert(0, str(ROOT / 'recomp'))
import exefs_plugins as xp # noqa: E402
import gen # noqa: E402
import layout # noqa: E402
import patches # noqa: E402
from nso import Nso # noqa: E402
def nso(size_text, bss=0x1000):
"""An uncompressed NSO: .text of size_text bytes (b over MOD0's offset word), .rodata with MOD0 at 0x1000 past
.text, .data with an empty .dynamic."""
text = bytearray(size_text)
struct.pack_into('<II', text, 0, 0x14000002, size_text) # b +8; the MOD0 offset
ro = b'MOD0' + struct.pack('<6i', 0x1000, 0, 0, 0, 0, 0) # .dynamic at .rodata + 0x1000 = .data
data = bytes(16) # DT_NULL
segs = [(0, bytes(text)), (size_text, ro), (size_text + 0x1000, data)]
hdr = bytearray(0x100)
hdr[0:4] = b'NSO0'
off = 0x100
for i, (mem, d) in enumerate(segs):
struct.pack_into('<IIII', hdr, 0x10 + 0x10 * i, off, mem, len(d), bss if i == 2 else 0)
struct.pack_into('<I', hdr, 0x60 + 4 * i, len(d))
off += len(d)
return bytes(hdr) + b''.join(d for _, d in segs)
def dev_layout_files(exefs, out):
"""layout.json and layout.h as recomp/layout.py wrote them before plugins (dev f8505807), the reference for the
no-plugin case."""
out.mkdir(parents=True, exist_ok=True)
mods, base = [], layout.FIRST_BASE
for name in layout.ORDER:
if (exefs / name).exists():
size = Nso(str(exefs / name)).size
mods.append((name, base, size))
base = layout.align_up(base + size)
(out / 'layout.json').write_text(json.dumps(
[{'name': n, 'base': f'{b:#x}', 'size': f'{s:#x}'} for n, b, s in mods], indent=1) + '\n')
lines = ['// Module layout chosen by recomp/layout.py. Generated; do not edit.', '#pragma once', '']
for n, b, s in mods:
lines.append(f'#define MOD_{n.upper()}_BASE {b:#x}ull')
lines.append(f'#define MOD_{n.upper()}_SIZE {s:#x}ull')
lines += ['', f'#define MOD_COUNT {len(mods)}',
'#define MOD_LIST(X) ' + ' '.join(f'X({n}, {n.upper()})' for n, _, _ in mods), '']
(out / 'layout.h').write_text('\n'.join(lines))
class Layout(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tmp = tempfile.TemporaryDirectory()
cls.dir = Path(cls.tmp.name)
cls.exefs = cls.dir / 'exefs'
cls.exefs.mkdir()
for name, size in (('main', 0x30000), ('subsdk0', 0x5000), ('sdk', 0x21000)):
(cls.exefs / name).write_bytes(nso(size))
cls.plugins = [cls.dir / 'mod a' / 'subsdk9', cls.dir / 'mod b' / 'subsdk8']
for p, size in zip(cls.plugins, (0x9000, 0x3000)):
p.parent.mkdir()
p.write_bytes(nso(size))
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def test_no_plugins_identical(self):
dev, ours = self.dir / 'dev', self.dir / 'ours'
dev_layout_files(self.exefs, dev)
layout.write(layout.layout(self.exefs), ours, layout.plugin_files([]))
for f in ('layout.json', 'layout.h'):
self.assertEqual((dev / f).read_bytes(), (ours / f).read_bytes(), f)
def test_plugins_after_sdk(self):
plain = layout.layout(self.exefs)
mods = layout.layout(self.exefs, self.plugins)
self.assertEqual(mods[:3], plain) # the game modules keep their bases
self.assertEqual([n for n, _, _ in mods], ['main', 'subsdk0', 'sdk', 'plugin0', 'plugin1'])
for (_, b0, s0), (_, b1, _) in zip(mods, mods[1:]):
self.assertEqual(b1, layout.align_up(b0 + s0))
self.assertEqual(mods[3][2], Nso(str(self.plugins[0])).size)
out = self.dir / 'with'
files = layout.plugin_files(self.plugins)
layout.write(mods, out, files)
data = json.loads((out / 'layout.json').read_text())
self.assertNotIn('file', data[2])
self.assertEqual(data[3]['file'], self.plugins[0].resolve().as_posix())
self.assertEqual(len(data[3]['sha256']), 64)
self.assertEqual(layout.read(out), mods)
self.assertIn('#define MOD_PLUGIN1_BASE', (out / 'layout.h').read_text())
self.assertIn('#define MOD_COUNT 5', (out / 'layout.h').read_text())
def test_address_limit(self):
old = layout.FIRST_BASE
layout.FIRST_BASE = layout.ADDRESS_LIMIT - 0x400000 # the plugins end above 2^39
try:
with self.assertRaises(ValueError):
layout.layout(self.exefs, self.plugins)
finally:
layout.FIRST_BASE = old
# ---- records
MAIN, PLUGIN = 0x7000000000, 0x7005200000
SITE_OFF, STUB_OFF, TRAMP_OFF, CB_OFF, IMPL_OFF, THUNK_OFF = 0x1000, 0x4000, 0x2000, 0x66f0, 0x73ec, 0xf00
MAIN_TEXT, PLUGIN_TEXT = 0x100000, 0x9000
def b(src, dst, link=False):
return (0x94000000 if link else 0x14000000) | ((dst - src) >> 2) & 0x3FFFFFF
def quad(v):
"""The two words of a .quad v."""
return [v & 0xFFFFFFFF, v >> 32]
def record(original=0x94000010, trampoline_first=None, stub=None, writes=None, **extra):
"""A record of one inline hook at main+SITE_OFF whose original is `original` (default bl main+SITE_OFF+0x40)."""
site, stub_at, tramp = MAIN + SITE_OFF, PLUGIN + STUB_OFF, PLUGIN + TRAMP_OFF
img = bytearray(0x14000)
words = stub or [xp.STUR_X30, b(stub_at + 4, PLUGIN + IMPL_OFF, True), xp.LDUR_X30, b(stub_at + 12, tramp)]
struct.pack_into('<4IQ', img, STUB_OFF, *words, PLUGIN + CB_OFF)
if trampoline_first is None:
rel = xp.pc_relative(original, site)
trampoline_first = (b(tramp, rel[1], True) if rel and rel[0][0] == 'b' else original)
struct.pack_into('<II', img, TRAMP_OFF, trampoline_first, b(tramp + 4, site + 4))
rec = {
'refused': None, 'svcs': {'0x6': 52, '0x1e': 10, '0x27': 18, '0x29': 17, '0x74': 11, '0x75': 7},
'get_info': {'2': 1, '3': 1, '12': 1, '13': 1, '65001': 1},
'imports': {'_ZN2nn4util8SNPrintfEPcmPKcz': 20, '_ZN2nn2oe17GetDisplayVersionEPNS0_14DisplayVersionE': 1},
'modules': [
{'name': 'main', 'base': MAIN, 'size': 0x200000, 'text_end': MAIN_TEXT, 'data_off': 0x180000, 'init': 0,
'plugin': False},
{'name': 'sdk', 'base': 0x7004400000, 'size': 0x100000, 'text_end': 0x80000, 'data_off': 0xc0000,
'init': 0, 'plugin': False},
{'name': 'plugin0', 'base': PLUGIN, 'size': 0x14000, 'text_end': PLUGIN_TEXT, 'data_off': 0xd000,
'init': PLUGIN + 0x50a0, 'plugin': True, 'functions': [hex(CB_OFF), hex(IMPL_OFF), hex(THUNK_OFF)]}],
'writes': writes if writes is not None else [
{'module': 'main', 'offset': SITE_OFF, 'old': original, 'new': b(site, stub_at)}],
'aliases': [], 'images': {'plugin0': bytes(img)}}
rec.update(extra)
return rec
IMPLS = {PLUGIN + IMPL_OFF: 'exl_inline_hook_impl'}
class Check(unittest.TestCase):
def reasons(self, rec):
return xp.check_record(rec, IMPLS)[1]
def refused(self, rec, text):
r = self.reasons(rec)
self.assertTrue(r, 'accepted')
self.assertTrue(any(text in x for x in r), r)
def test_good(self):
hooks, reasons = xp.check_record(record(), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(len(hooks), 1)
self.assertEqual(hooks[0]['callback'], f'plugin0+{CB_OFF:#x}')
self.assertEqual(hooks[0]['trampoline'], f'plugin0+{TRAMP_OFF:#x}')
def test_good_with_fp_impl(self): # Magic of the Kingdom: the impl that saves d0..d15 as well
self.assertIn('exl_inline_hook_with_fp_impl', xp.HOOK_IMPLS)
hooks, reasons = xp.check_record(record(), {PLUGIN + IMPL_OFF: 'exl_inline_hook_with_fp_impl'})
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['impl'], 'exl_inline_hook_with_fp_impl')
def test_good_plain_original(self):
self.assertEqual(self.reasons(record(original=0xAA0003F3)), []) # mov x19, x0, copied as it is
def test_call_site_rewrite(self): # Quick Weapon Durability: bl straight to a thunk of the plugin
site = MAIN + SITE_OFF
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': b(site, PLUGIN + THUNK_OFF, True)}]
hooks, reasons = xp.check_record(record(writes=w), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['kind'], 'call')
self.assertEqual(hooks[0]['target'], f'plugin0+{THUNK_OFF:#x}')
def test_replace_hook(self): # Full Inventory Expansion: b from a main function start, Orig trampoline in the pool
site, prologue = MAIN + SITE_OFF, 0xA9BA7BFD # stp x29, x30, [sp, #-0x60]!
w = [{'module': 'main', 'offset': SITE_OFF, 'old': prologue, 'new': b(site, PLUGIN + THUNK_OFF),
'fn_start': True}]
hooks, reasons = xp.check_record(record(original=prologue, writes=w), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['kind'], 'replace')
self.assertEqual(hooks[0]['trampoline'], f'plugin0+{TRAMP_OFF:#x}')
def test_branch_inside_function(self): # a far hook: the plugin's code comes back through an entry
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': b(MAIN + SITE_OFF, PLUGIN + THUNK_OFF),
'fn_start': False}]
hooks, reasons = xp.check_record(record(writes=w), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['kind'], 'branch')
def test_call_into_function_middle(self): # the target becomes a seed of the plugin's lift
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': b(MAIN + SITE_OFF, PLUGIN + 0xf08, True)}]
hooks, reasons = xp.check_record(record(writes=w), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual((hooks[0]['kind'], hooks[0]['target']), ('call', 'plugin0+0xf08'))
def test_bl_to_stub(self): # an inline stub is entered by b; a bl there would skip the saved x30
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': b(MAIN + SITE_OFF, PLUGIN + STUB_OFF, True)}]
self.refused(record(writes=w), 'into an inline-hook stub')
def test_refused_run(self):
self.refused(record(refused='svc 0x40 at plugin0+0x76a0'), 'the run was refused')
def test_svc(self):
self.refused(record(svcs={'0x6': 1, '0x40': 1}), 'svc 0x40')
def test_get_info(self):
self.refused(record(get_info={'2': 1, '18': 1}), 'GetInfo id 18')
def test_import(self):
self.refused(record(imports={'_ZN2nn2fs8MountRomEPKcPvm': 1}), 'the import _ZN2nn2fs8MountRom')
def test_import_the_game_imports_is_allowed(self):
rec = record(imports={'_ZN2nn3hid13GetNpadStatesEPNS0_16NpadFullKeyStateEiRKj': 1})
self.assertTrue(xp.check_record(rec, IMPLS)[1])
self.assertEqual(xp.check_record(rec, IMPLS, frozenset(rec['imports']))[1], [])
def test_game_imports_leave_out_the_loader_and_the_boot_hooks(self):
names = ['_ZN2nn2ro10LoadModuleEPNS0_6ModuleEPKvPvmi', '__nnmusl_init_dso', 'glslcCompile',
'_ZN2nn2os32SetMemoryAllocatorForThreadLocalEPFPvmmEPFvS1_mE']
self.assertEqual([n for n in names if not xp.GAME_IMPORTS_LEFT_OUT.search(n)], [])
kept = ['abort', '_ZN2nn2os12CreateThreadEPNS0_10ThreadTypeEPFvPvES3_S3_mi', '_ZN2nn2fs6CommitEPKc']
self.assertEqual([n for n in kept if xp.GAME_IMPORTS_LEFT_OUT.search(n)], [])
def test_game_imports_of_a_missing_exefs_are_none(self):
with tempfile.TemporaryDirectory() as d:
self.assertEqual(xp.game_imports(Path(d)), frozenset())
self.assertEqual(xp.game_imports(None), frozenset())
def test_write_main_data(self): # kept as written, compared at every start
w = [{'module': 'main', 'offset': 0x180008, 'old': 0, 'new': 1}]
hooks, reasons = xp.check_record(record(writes=w), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['kind'], 'data')
self.assertNotIn('target', hooks[0])
def test_pointer_swap(self): # a table entry of main's .data now points at a plugin function
w = [{'module': 'main', 'offset': 0x180010, 'old': 0x00001000, 'new': (PLUGIN + THUNK_OFF) & 0xFFFFFFFF}]
hooks, reasons = xp.check_record(record(writes=w), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['target'], f'plugin0+{THUNK_OFF:#x}')
def test_write_main_rodata(self): # the lift read .rodata as constants
w = [{'module': 'main', 'offset': 0x120000, 'old': 0, 'new': 1}]
self.refused(record(writes=w), "a write into main's .rodata: main+0x120000")
def test_write_other_module(self):
w = [{'module': 'sdk', 'offset': 0x100, 'old': 0, 'new': 1}]
self.refused(record(writes=w), 'a write outside main: sdk+0x100')
def test_not_a_branch(self):
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': 0xD503201F}]
self.refused(record(writes=w), 'is no b')
def test_branch_elsewhere(self):
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': b(MAIN + SITE_OFF, MAIN + 0x8000)}]
self.refused(record(writes=w), "does not reach into a plugin's .text")
def test_stub_shape(self): # not exlaunch's stub: plugin code the site branches to, a far hook
stub_at = PLUGIN + STUB_OFF
words = [0xD503201F, b(stub_at + 4, PLUGIN + IMPL_OFF, True), xp.LDUR_X30, b(stub_at + 12, PLUGIN + TRAMP_OFF)]
hooks, reasons = xp.check_record(record(stub=words), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['kind'], 'branch')
def test_stub_impl(self):
stub_at = PLUGIN + STUB_OFF
words = [xp.STUR_X30, b(stub_at + 4, PLUGIN + 0x100, True), xp.LDUR_X30, b(stub_at + 12, PLUGIN + TRAMP_OFF)]
self.refused(record(stub=words), 'not exl_inline_hook_impl')
def test_callback_outside(self):
rec = record()
img = bytearray(rec['images']['plugin0'])
struct.pack_into('<Q', img, STUB_OFF + 16, MAIN + 0x500)
rec['images']['plugin0'] = bytes(img)
self.refused(rec, "callback 0x7000000500 is outside plugin0's .text")
def test_trampoline_not_original(self):
self.refused(record(original=0xAA0003F3, trampoline_first=0xAA0003F4), 'not the original')
def test_trampoline_bl_elsewhere(self):
tramp = PLUGIN + TRAMP_OFF
self.refused(record(trampoline_first=b(tramp, MAIN + SITE_OFF + 0x44, True)), 'not the original')
def test_trampoline_return(self):
rec = record()
img = bytearray(rec['images']['plugin0'])
struct.pack_into('<I', img, TRAMP_OFF + 4, b(PLUGIN + TRAMP_OFF + 4, MAIN + SITE_OFF + 8))
rec['images']['plugin0'] = bytes(img)
self.refused(rec, 'no b to the site + 4')
def test_indirect_original(self):
self.refused(record(original=0xD65F03C0), 'is a br or ret')
self.refused(record(original=0xD61F0100), 'is a br or ret')
def test_blr_original(self): # Zonai Fieldworks hooks blr x8 sites: the call returns, the site goes on
self.assertEqual(self.reasons(record(original=0xD63F0100)), [])
def test_veneer_entries(self): # Zonai Fieldworks: site -> veneer -> slot -> trampoline -> b site + 4
site, ven, slot, tramp = MAIN + SITE_OFF, PLUGIN + 0x5000, PLUGIN + 0xd000, PLUGIN + 0x5100
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0xD5033BBF, 'new': b(site, ven), 'fn_start': False}]
rec = record(writes=w)
img = bytearray(rec['images']['plugin0'])
imm = ((slot & ~0xFFF) - (ven & ~0xFFF)) >> 12
adrp = 0x90000011 | (imm & 3) << 29 | (imm >> 2 & 0x7FFFF) << 5
struct.pack_into('<3I', img, 0x5000, adrp, 0xF9400231 | ((slot & 0xFFF) // 8) << 10, 0xD61F0220)
struct.pack_into('<Q', img, 0xd000, tramp)
struct.pack_into('<2I', img, 0x5100, 0xD5033BBF, b(tramp + 4, site + 4))
rec['images']['plugin0'] = bytes(img)
hooks, reasons = xp.check_record(rec, IMPLS)
self.assertEqual(reasons, [])
back, ahead = xp.entries(rec, hooks, 'plugin0')
self.assertEqual(back, [SITE_OFF + 4])
self.assertEqual(ahead, [0x5100])
# exlaunch's long forms (tools/exefs_plugins.py relocation_end, returns_to): the trampoline at plugin0+off
def long_form(self, original, off, words):
rec = record(original=original)
img = bytearray(rec['images']['plugin0'])
struct.pack_into('<I', img, STUB_OFF + 12, b(PLUGIN + STUB_OFF + 12, PLUGIN + off))
struct.pack_into(f'<{len(words)}I', img, off, *words)
rec['images']['plugin0'] = bytes(img)
return rec
def test_trampoline_tbnz_long(self): # Challenge: tbnz w0, #0 out of the plugin's reach
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF
words = [0x37000040, 0x14000005, xp.LDR_X17_8, xp.BR_X17, *quad(site + 0x14), b(t + 24, site + 4)]
hooks, reasons = xp.check_record(self.long_form(0x370000A0, TRAMP_OFF, words), IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['relocated'], '0x37000040 +5 words')
def test_trampoline_backward_unsigned(self): # Challenge: b.eq back 0x2ec; exlaunch's .quad lacks the sign
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF
form = [0x54000040, 0x14000005, xp.LDR_X17_8, xp.BR_X17]
for target in (site + 0x1FFD14, site - 0x2EC):
words = [*form, *quad(target), b(t + 24, site + 4)]
self.assertEqual(self.reasons(self.long_form(0x54FFE8A0, TRAMP_OFF, words)), [], hex(target))
self.refused(self.long_form(0x54FFE8A0, TRAMP_OFF, [*form, *quad(site + 0x1FFD18), b(t + 24, site + 4)]),
'not the original')
def test_trampoline_long_nop(self): # the .quad 8-byte aligned by one nop before the form
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF + 4
form = [0x37000040, 0x14000005, xp.LDR_X17_8, xp.BR_X17, *quad(site + 0x14)]
self.assertEqual(self.reasons(self.long_form(0x370000A0, TRAMP_OFF + 4,
[xp.NOP, *form, b(t + 28, site + 4)])), [])
self.refused(self.long_form(0x370000A0, TRAMP_OFF + 4, [*form, b(t + 24, site + 4)]), 'not the original')
def test_trampoline_long_wrong_target(self):
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF
words = [0x37000040, 0x14000005, xp.LDR_X17_8, xp.BR_X17, *quad(site + 0x18), b(t + 24, site + 4)]
self.refused(self.long_form(0x370000A0, TRAMP_OFF, words), 'not the original')
def test_trampoline_bl_long(self): # ldr x17, #12; adr x30, #16; br x17; .quad, behind a nop here
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF
words = [xp.NOP, xp.LDR_X17_12, xp.ADR_X30_16, xp.BR_X17, *quad(site + 0x40), b(t + 24, site + 4)]
self.assertEqual(self.reasons(self.long_form(0x94000010, TRAMP_OFF, words)), [])
def test_trampoline_adrp_long(self): # adrp x8: ldr x8, #8; b #12; .quad <page>
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF
words = [0x58000048, 0x14000003, *quad(site & ~0xFFF), b(t + 16, site + 4)]
self.assertEqual(self.reasons(self.long_form(0x90000008, TRAMP_OFF, words)), [])
def test_trampoline_ldr_literal_long(self): # ldr x1, <literal>: ldr x1, #8; b over the 8 bytes; the bytes
site, t = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF
words = [0x58000041, 0x14000003, 0x11223344, 0x55667788, b(t + 16, site + 4)]
self.assertEqual(self.reasons(self.long_form(0x58000801, TRAMP_OFF, words)), [])
def test_trampoline_absolute_return(self): # back to the site + 4 by ldr x17, #8; br x17; .quad, nop-aligned
site = MAIN + SITE_OFF
words = [0xAA0003F3, xp.NOP, xp.LDR_X17_8, xp.BR_X17, *quad(site + 4)]
self.assertEqual(self.reasons(self.long_form(0xAA0003F3, TRAMP_OFF, words)), [])
words[-2:] = quad(site + 8)
self.refused(self.long_form(0xAA0003F3, TRAMP_OFF, words), 'no b to the site + 4')
def test_replace_hook_adrp_orig(self): # a function starting with adrp: its Orig trampoline is the long form
site, t, adrp = MAIN + SITE_OFF, PLUGIN + TRAMP_OFF, 0x90000008
w = [{'module': 'main', 'offset': SITE_OFF, 'old': adrp, 'new': b(site, PLUGIN + THUNK_OFF), 'fn_start': True}]
rec = self.long_form(adrp, TRAMP_OFF, [0x58000048, 0x14000003, *quad(site & ~0xFFF), b(t + 16, site + 4)])
rec['writes'] = w
hooks, reasons = xp.check_record(rec, IMPLS)
self.assertEqual(reasons, [])
self.assertEqual(hooks[0]['trampoline'], f'plugin0+{TRAMP_OFF:#x}')
class Diff(unittest.TestCase):
def test_lists_differing_words(self):
a, c = record(), record()
img = bytearray(c['images']['plugin0'])
struct.pack_into('<Q', img, 0x13450, 0x2345000000)
c['images']['plugin0'] = bytes(img)
a['pool'] = c['pool'] = {'base': '0x2000000000', 'size': '0x1000000000'}
lines = xp.diff_records(a, c)
self.assertEqual(len(lines), 1)
self.assertIn('plugin0+0x13450', lines[0])
self.assertIn('alias pool', lines[0])
self.assertEqual(xp.diff_records(a, record()), [])
# ---- the code the hooks reach
SVC_40, RET_W, NOP = 0xD4000801, 0xD65F03C0, 0xD503201F
def code(words, at=0):
img = bytearray(0x200)
for i, w in enumerate(words):
struct.pack_into('<I', img, at + 4 * i, w)
return bytes(img)
class Reach(unittest.TestCase):
BASE = 0x7005200000
def test_follows_calls_and_finds_svc(self):
# root: bl +0x40; ret. callee at 0x40: svc 0x40; ret
img = bytearray(code([b(self.BASE, self.BASE + 0x40, True), RET_W]))
struct.pack_into('<II', img, 0x40, SVC_40, RET_W)
svcs, imports, indirect, outside, _ = xp.reach(bytes(img), self.BASE, 0x200, [self.BASE], {})
self.assertEqual(svcs, {0x40})
def test_imports_through_plt(self):
img = code([b(self.BASE, self.BASE + 0x80, True), RET_W])
svcs, imports, _, _, _ = xp.reach(img, self.BASE, 0x200, [self.BASE], {self.BASE + 0x80: 'malloc'})
self.assertEqual((svcs, imports), (set(), {'malloc': 1}))
def test_stub_returns_at_its_trampoline_branch(self):
# stur; bl impl; ldur; b trampoline (where an svc sits): the b is the stub's return
stub = [xp.STUR_X30, b(self.BASE + 4, self.BASE + 0x40, True), xp.LDUR_X30, b(self.BASE + 12, self.BASE + 0x100)]
img = bytearray(code(stub))
struct.pack_into('<I', img, 0x40, RET_W)
struct.pack_into('<I', img, 0x100, SVC_40)
self.assertEqual(xp.reach(bytes(img), self.BASE, 0x200, [self.BASE], {}, stubs={self.BASE})[0], set())
self.assertEqual(xp.reach(bytes(img), self.BASE, 0x200, [self.BASE], {})[0], {0x40})
def test_indirect_calls_counted(self):
img = code([0xD63F0100, RET_W]) # blr x8
self.assertEqual(xp.reach(img, self.BASE, 0x200, [self.BASE], {})[2], 1)
def test_check_refuses_svc_in_callback(self):
rec = record()
img = bytearray(rec['images']['plugin0'])
struct.pack_into('<II', img, CB_OFF, SVC_40, RET_W)
struct.pack_into('<I', img, IMPL_OFF, RET_W)
rec['images']['plugin0'] = bytes(img)
hooks, _ = xp.check_record(rec, IMPLS)
reasons, _ = xp.reach_check(rec, hooks, IMPLS)
self.assertTrue(any('svc 0x40' in r for r in reasons), reasons)
def test_check_allows_the_game_imports_in_callbacks(self):
# callback: bl <PLT stub of a hid import>; ret
rec = record()
img = bytearray(rec['images']['plugin0'])
plt_at = PLUGIN + 0x7000
struct.pack_into('<II', img, CB_OFF, b(PLUGIN + CB_OFF, plt_at, True), RET_W)
struct.pack_into('<I', img, IMPL_OFF, RET_W)
rec['images']['plugin0'] = bytes(img)
hid = '_ZN2nn3hid13GetNpadStatesEPNS0_16NpadFullKeyStateEiRKj'
hooks, _ = xp.check_record(rec, IMPLS)
with tempfile.NamedTemporaryFile(delete=False) as f:
rec['modules'][2]['file'] = f.name
try:
with mock.patch.object(xp, 'Nso'), mock.patch.object(xp, 'plt_imports', return_value={plt_at: hid}):
refused, _ = xp.reach_check(rec, hooks, IMPLS)
allowed, _ = xp.reach_check(rec, hooks, IMPLS, frozenset({hid}))
finally:
Path(f.name).unlink()
self.assertEqual(refused, [f'the code its hooks reach calls the import {hid}'])
self.assertEqual(allowed, [])
def test_check_allows_break(self): # an abort or a failed assert ends the game, as on the Switch
rec = record()
img = bytearray(rec['images']['plugin0'])
struct.pack_into('<II', img, CB_OFF, 0xD40004C1, RET_W) # svc 0x26
struct.pack_into('<I', img, IMPL_OFF, RET_W)
rec['images']['plugin0'] = bytes(img)
hooks, _ = xp.check_record(rec, IMPLS)
self.assertEqual(xp.reach_check(rec, hooks, IMPLS)[0], [])
def test_check_allows_output_debug_string(self):
rec = record()
img = bytearray(rec['images']['plugin0'])
struct.pack_into('<II', img, CB_OFF, 0xD40004E1, RET_W) # svc 0x27
struct.pack_into('<I', img, IMPL_OFF, RET_W)
rec['images']['plugin0'] = bytes(img)
hooks, _ = xp.check_record(rec, IMPLS)
self.assertEqual(xp.reach_check(rec, hooks, IMPLS)[0], [])
# ---- patch lines of the three hook kinds
class Lines(unittest.TestCase):
def lines(self, rec):
hooks, reasons = xp.check_record(rec, IMPLS)
self.assertEqual(reasons, [])
with tempfile.TemporaryDirectory() as d:
(Path(d) / 'plugin0.img').write_bytes(rec['images']['plugin0'])
rec['modules'][2]['image'] = 'plugin0.img'
return xp.plugin_lines(rec, xp.hooks_of(hooks, 'plugin0'), 'mod_x', d, 'plugin0')
def test_image_path_roundtrips_through_the_patch_table(self):
rec = record()
mods = [(m['name'], m['base'], m['size']) for m in rec['modules']]
with tempfile.TemporaryDirectory() as d:
for folder in ('plain', 'with spaces', 'with#hash', 'caf\u00e9 #1'):
with self.subTest(folder=folder):
dest = Path(d) / folder
dest.mkdir()
image = dest / 'plugin0.img'
image.write_bytes(rec['images']['plugin0'])
rec['modules'][2]['image'] = image.name
lines = xp.plugin_lines(rec, [], 'mod_x', dest, 'plugin0')
patch = dest / 'patches.txt'
patch.write_text('set mod_x TOTK_MODPATCH_X 0 ordered\n' + '\n'.join(lines), encoding='utf-8')
actual = patches.load(patch, mods).plugins['plugin0']['image']
self.assertEqual(actual, image.resolve().as_posix())
self.assertEqual(Path(actual).read_bytes(), rec['images']['plugin0'])
def test_quoted_image_and_legacy_unquoted_paths(self):
mods = [('main', MAIN, 0x200000), ('plugin0', PLUGIN, 0x14000)]
with tempfile.TemporaryDirectory() as d:
patch = Path(d) / 'patches.txt'
for image, field in (('C:/folder #1/plugin.img', '"C:/folder #1/plugin.img" # comment'),
('C:/plain/plugin.img', 'C:/plain/plugin.img # comment'),
(r'C:\with space\plugin.img', json.dumps(r'C:\with space\plugin.img')),
(r'C:\plain\plugin.img', r'C:\plain\plugin.img')):
with self.subTest(field=field):
patch.write_text(f'set x ENV 0\nplugin x plugin0 {"0" * 64} {field}\n')
self.assertEqual(patches.load(patch, mods).plugins['plugin0']['image'], image)
def test_malformed_quoted_image_names_the_patch_line(self):
mods = [('main', MAIN, 0x200000), ('plugin0', PLUGIN, 0x14000)]
with tempfile.TemporaryDirectory() as d:
patch = Path(d) / 'patches.txt'
for field in ('"unfinished', '""', '"image.img" extra', '"image.img" "#not a comment"'):
with self.subTest(field=field):
patch.write_text(f'set x ENV 0\nplugin x plugin0 {"0" * 64} {field}\n')
with self.assertRaises(patches.PatchError) as error:
patches.load(patch, mods)
self.assertIn(f'{patch}:2:', str(error.exception))
def test_inline(self):
out = self.lines(record(original=0xAA0003F3))
self.assertIn(f'site mod_x main {SITE_OFF:x} aa0003f3 @call:plugin0+{STUB_OFF:x} aa0003f3', out)
self.assertIn(f'stub plugin0 {STUB_OFF:x}', out)
self.assertIn(f'seed plugin0 {CB_OFF:x} {IMPL_OFF:x}', out)
self.assertIn(f'expect mod_x main {SITE_OFF:x} aa0003f3 {b(MAIN + SITE_OFF, PLUGIN + STUB_OFF):08x}', out)
self.assertTrue(out[0].startswith('plugin mod_x plugin0 '))
def test_replace(self):
site, prologue = MAIN + SITE_OFF, 0xA9BA7BFD
new = b(site, PLUGIN + THUNK_OFF)
w = [{'module': 'main', 'offset': SITE_OFF, 'old': prologue, 'new': new, 'fn_start': True}]
out = self.lines(record(original=prologue, writes=w))
self.assertIn(f'site mod_x main {SITE_OFF:x} a9ba7bfd {new:08x}', out)
self.assertIn(f'orig mod_x plugin0 {TRAMP_OFF:x} main {SITE_OFF:x}', out)
self.assertIn(f'seed plugin0 {THUNK_OFF:x}', out)
def test_call(self):
new = b(MAIN + SITE_OFF, PLUGIN + THUNK_OFF, True)
w = [{'module': 'main', 'offset': SITE_OFF, 'old': 0x94000010, 'new': new}]
out = self.lines(record(writes=w))
self.assertIn(f'site mod_x main {SITE_OFF:x} 94000010 {new:08x}', out)
self.assertFalse(any(x.startswith(('orig', 'stub')) for x in out))
# ---- one service run per plugin
class PerPlugin(unittest.TestCase):
"""main() with three plugins in three mods and a stand-in for the service run: plugin0 hooks its site, plugin1's
init ends the run without a record (a crash), plugin2's run is refused (an svc). Each gets its own status line;
only plugin0's set goes into the patch file."""
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.d = Path(tmp.name)
self.files = []
for name in ('A', 'B', 'C'):
f = self.d / 'mods' / name / 'exefs' / 'subsdk9'
f.parent.mkdir(parents=True)
f.write_bytes(nso(PLUGIN_TEXT) + name.encode())
self.files.append(f)
(self.d / 'patches.txt').write_text('set fps60 TOTK_FPS60 1\n')
self.runs = []
saved = xp.run, xp.hook_impls
self.real_run = xp.run
self.addCleanup(lambda: (setattr(xp, 'run', saved[0]), setattr(xp, 'hook_impls', saved[1])))
xp.run, xp.hook_impls = self.fake_run, lambda rec: IMPLS
def fake_run(self, exe, out, tick_offset, timeout, only):
self.runs.append(only)
out.mkdir(parents=True, exist_ok=True)
if only == 'plugin1':
return 0xC0000005, 'its init ended the service run without a record (exit 0xc0000005)'
rec = record(writes=[] if only != 'plugin0' else None)
rec['modules'][2]['file'] = str(self.files[0])
for i in (1, 2):
rec['modules'].append(dict(rec['modules'][2], name=f'plugin{i}', base=PLUGIN + i * 0x100000,
file=str(self.files[i])))
for m in rec['modules'][2:]:
m['ran'] = m['name'] == only
if only == 'plugin2':
rec['refused'] = 'svc 0x1 at plugin2+0x50a0'
(out / 'plugin0.img').write_bytes(rec.pop('images')['plugin0'])
rec['modules'][2]['image'] = 'plugin0.img'
(out / 'record.json').write_text(json.dumps(rec))
return 3 if rec['refused'] else 0, None
def test_each_plugin_alone(self):
out, status, patched = self.d / 'rec', self.d / 'plugins.status', self.d / 'patches-out.txt'
argv = sys.argv
sys.argv = ['exefs_plugins.py', '--exe', 'totk.exe', '--out', str(out), '--mods', str(self.d / 'mods'),
'--patches', str(self.d / 'patches.txt'), '--patches-out', str(patched), '--status', str(status)]
try:
code = xp.main()
finally:
sys.argv = argv
self.assertEqual(code, 1)
self.assertEqual(self.runs, ['-', 'plugin0', 'plugin0', 'plugin1', 'plugin2'])
lines = {line.split('\t')[0]: line.split('\t') for line in status.read_text().splitlines()}
key = {n: xp.fnv(f'{n}/exefs/subsdk9'.encode()) for n in 'ABC'}
self.assertEqual(lines[key['A']][2:], ['plugin', '-', 'plugin0, 1 hooks'])
self.assertEqual(lines[key['B']][2], 'unsupported')
self.assertIn('ended the service run', lines[key['B']][4])
self.assertEqual(lines[key['C']][2], 'unsupported')
self.assertIn('svc 0x1', lines[key['C']][4])
text = patched.read_text()
self.assertIn(f'set mod_{key["A"].lower()} ', text)
self.assertNotIn(f'mod_{key["B"].lower()}', text)
self.assertNotIn(f'mod_{key["C"].lower()}', text)
self.assertIn((out / 'plugin0' / 'run1' / 'plugin0.img').resolve().as_posix(), text)
def test_run_reasons(self):
# the real run(): a hang becomes its own reason, a crash names the FATAL line
saved = xp.subprocess.run
self.addCleanup(setattr, xp.subprocess, 'run', saved)
def hang(cmd, **kw):
raise xp.subprocess.TimeoutExpired(cmd, kw['timeout'], output=b'started', stderr=None)
xp.subprocess.run = hang
self.assertEqual(self.real_run(None, self.d / 'h', 0, 7, 'plugin0'), (None, 'its init did not return within 7 s'))
xp.subprocess.run = lambda cmd, **kw: SimpleNamespace(returncode=0xC0000005, stdout='x\nFATAL: access violation '
'(0xc0000005) at host 1\n', stderr='')
code, why = self.real_run(None, self.d / 'c', 0, 7, 'plugin0')
self.assertEqual(why, 'its init crashed the service run: access violation (0xc0000005) at host 1')
def test_ran_view_leaves_out_plugins_that_did_not_run(self):
rec = record()
rec['modules'].append(dict(rec['modules'][2], name='plugin1', base=PLUGIN + 0x100000, ran=False))
rec['modules'][2]['ran'] = True
rec['images']['plugin1'] = rec['images']['plugin0']
view = xp.ran_view(rec)
self.assertEqual([m['name'] for m in view['modules'] if m['plugin']], ['plugin0'])
self.assertEqual(set(view['images']), {'plugin0'})
whole = record() # no "ran": every init ran, the record is its own view
self.assertIs(xp.ran_view(whole), whole)
# ---- the generator on those lines
def code_nso(words_at, size_text=0x5000):
"""An NSO as nso() whose .text holds words_at {offset: word}."""
raw = bytearray(nso(size_text))
for off, w in words_at.items():
struct.pack_into('<I', raw, 0x100 + off, w)
return bytes(raw)
FN = 0x100 # the hooked main function: mov x19, x0; mov x0, x19; ret
class Generator(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.d = Path(tmp.name)
exefs = self.d / 'exefs'
exefs.mkdir()
(exefs / 'main').write_bytes(code_nso({FN: 0xAA0003F3, FN + 4: 0xAA1303E0, FN + 8: RET_W}))
(exefs / 'sdk').write_bytes(nso(0x1000))
stub = PLUGIN_TEXT_STUB = 0x4000
self.plugin_file = self.d / 'subsdk9'
self.plugin_file.write_bytes(nso(0x5000))
self.mods = layout.layout(exefs, [self.plugin_file])
self.bases = {n: b_ for n, b_, _ in self.mods}
pb = self.bases['plugin0']
# the image after the init: a stub at 0x4000 (b to a trampoline at 0x2000), an impl at 0x4100, a callback at
# 0x4200
img = bytearray(Nso(str(self.plugin_file)).image)
struct.pack_into('<4IQ', img, stub, xp.STUR_X30, b(pb + stub + 4, pb + 0x4100, True), xp.LDUR_X30,
b(pb + stub + 12, pb + 0x2000), pb + 0x4200)
struct.pack_into('<I', img, 0x4100, RET_W)
struct.pack_into('<I', img, 0x4200, RET_W)
struct.pack_into('<II', img, 0x2000, 0xAA0003F3, b(pb + 0x2004, self.bases['main'] + FN + 4))
(self.d / 'plugin0.img').write_bytes(bytes(img))
n = Nso(str(self.plugin_file))
self.sha = hashlib.sha256(bytes(img[n.text[0]:n.text[0] + n.text[1]])).hexdigest()
def table(self, site_line, extra=(), image=None):
image = image or self.d / 'plugin0.img'
text = '\n'.join(['set mod_x TOTK_MODPATCH_X 0 ordered',
f'plugin mod_x plugin0 {self.sha} {json.dumps(image.as_posix())}',
f'expect mod_x main {FN:x} aa0003f3 14000000', site_line, *extra]) + '\n'
(self.d / 'p.txt').write_text(text)
return patches.load(self.d / 'p.txt', self.mods)
def test_generator_cli_reads_image_in_a_folder_with_spaces_and_hash(self):
# The direct-emitter fixture needs a discoverable caller for a full CLI lift.
main = self.d / 'exefs' / 'main'
raw = bytearray(main.read_bytes())
struct.pack_into('<II', raw, 0x108, b(8, FN, True), RET_W)
main.write_bytes(raw)
image = self.d / 'record files #1' / 'plugin0.img'
image.parent.mkdir()
image.write_bytes((self.d / 'plugin0.img').read_bytes())
self.table(f'site mod_x main {FN:x} aa0003f3 @call:plugin0+4000 aa0003f3',
['stub plugin0 4000', 'seed plugin0 4100 4200'], image=image)
out = self.d / 'generated'
run = subprocess.run([sys.executable, str(ROOT / 'recomp/gen.py'), '--exefs', str(self.d / 'exefs'),
'--out', str(out), '--modules', 'main', '--plugins', str(self.plugin_file),
'--patches', str(self.d / 'p.txt'), '--jobs', '1'],
capture_output=True, text=True, timeout=30)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
self.assertEqual(layout.read(out), self.mods)
self.assertTrue(list((out / 'plugin0').glob('*.c')))
def emit(self, table, name, entry, end, origs=()):
mod = gen.open_module(name, self.d / ('exefs/main' if name == 'main' else 'subsdk9'), self.bases[name], table,
plugin=name != 'main')
e = gen.Emitter(mod, {entry}, table.for_module(name, self.bases[name]), origs=set(origs))
return gen.chunk_text(name, 0, e, [(entry, end)])
def test_inline_hook_calls_the_stub_then_the_original(self):
t = self.table(f'site mod_x main {FN:x} aa0003f3 @call:plugin0+4000 aa0003f3', ['stub plugin0 4000',
'seed plugin0 4100 4200'])
m = self.bases['main'] + FN
c = self.emit(t, 'main', m, m + 12)
stub = self.bases['plugin0'] + 0x4000
on = c[c.index('if (rt_patch_on[0])'):]
self.assertLess(on.index(f'f_{stub:x}(c);'), on.index('} else {')) # the hook, then the original, when on
head = on[:on.index(f'f_{stub:x}(c);')]
self.assertNotIn('c->x[30] =', head) # no x30 written for the call
self.assertIn(f'void f_{stub:x}(Cpu *c);', c)
h = patches.header_text(t, 'MARK')
self.assertIn('#define RT_PLUGIN_COUNT 1', h)
self.assertIn(f'X(0, "main", {FN:#x}, 0xaa0003f3u, 0x14000000u)', h)
self.assertIn('X("plugin0", 0x4100) X("plugin0", 0x4200)', h)
# exlaunch's hook impl saves no NZCV: flags pass from the site to the callback and back (recomp/flags.py)
def code(self, main_words, plugin_words=None):
"""Main's hooked function and the plugin image's words replaced; the plugin hash follows."""
(self.d / 'exefs' / 'main').write_bytes(code_nso(main_words))
img = bytearray((self.d / 'plugin0.img').read_bytes())
for off, w in (plugin_words or {}).items():
struct.pack_into('<I', img, off, w)
(self.d / 'plugin0.img').write_bytes(bytes(img))
n = Nso(str(self.plugin_file))
self.sha = hashlib.sha256(bytes(img[n.text[0]:n.text[0] + n.text[1]])).hexdigest()
def test_hook_site_hands_the_flags_to_the_callback_and_back(self):
# cmp x0, x1; b.eq +8 (hooked); mov x0, x19; ret
self.code({FN: 0xEB01001F, FN + 4: 0x54000040, FN + 8: 0xAA1303E0, FN + 12: RET_W})
t = self.table(f'site mod_x main {FN + 4:x} 54000040 @call:plugin0+4000 54000040', ['stub plugin0 4000'])
m = self.bases['main'] + FN
c = self.emit(t, 'main', m, m + 16)
stub = self.bases['plugin0'] + 0x4000
on = c[c.index('if (rt_patch_on[0])'):]
on, off = on[:on.index('} else {')], on[on.index('} else {'):]
call = on.index(f'f_{stub:x}(c);')
self.assertIn('c->nzcv = fnz_', on[:call]) # the callback sees the cmp's flags
self.assertIn('F.nz = c->nzcv;', on[call:]) # the b.eq reads what the callback left
self.assertIn('fc_nz(&F, CC_EQ)', on[call:])
self.assertIn('fc_nz(&F, CC_EQ)', off[:off.index('}', 8)]) # off: the cmp's flags, packed into F.nz
self.assertIn('F.nz = fnz_sub64(&F);', c)
def test_plugin_return_leaves_its_flags(self):
pb = self.bases['plugin0']
# the callback: cmp w1, #5; ret
self.code({FN: 0xAA0003F3, FN + 4: 0xAA1303E0, FN + 8: RET_W}, {0x4200: 0x7100143F, 0x4204: RET_W})
t = self.table(f'site mod_x main {FN:x} aa0003f3 @call:plugin0+4000 aa0003f3',
['stub plugin0 4000', 'seed plugin0 4100 4200'])
c = self.emit(t, 'plugin0', pb + 0x4200, pb + 0x4208)
self.assertIn('c->nzcv = fnz_sub32(&F); return;', c)
def test_plugin_call_takes_the_callee_flags(self):
pb = self.bases['plugin0']
# bl 0x4200; b.eq +8; ret; ret
self.code({FN: 0xAA0003F3, FN + 4: 0xAA1303E0, FN + 8: RET_W},
{0x4100: b(pb + 0x4100, pb + 0x4200, True), 0x4104: 0x54000040, 0x4108: RET_W, 0x410c: RET_W})
t = self.table(f'site mod_x main {FN:x} aa0003f3 @call:plugin0+4000 aa0003f3',
['stub plugin0 4000', 'seed plugin0 4100 4200'])
c = self.emit(t, 'plugin0', pb + 0x4100, pb + 0x4110)
body = c[c.index(f'void f_{pb + 0x4100:x}(Cpu *c) {{'):]
self.assertLess(body.index(f'c->pc = {pb + 0x4200:#x}ull; rt_dispatch(c);'), body.index('F.nz = c->nzcv;'))
self.assertIn('fc_nz(&F, CC_EQ)', body)
self.assertEqual(body.count('c->nzcv = fnz_nz(&F); return;'), 2) # both returns leave the flags
def test_stub_returns_instead_of_branching_to_its_trampoline(self):
t = self.table(f'site mod_x main {FN:x} aa0003f3 @call:plugin0+4000 aa0003f3', ['stub plugin0 4000'])
stub = self.bases['plugin0'] + 0x4000
c = self.emit(t, 'plugin0', stub, stub + 16)
body = c[c.index(f'void f_{stub:x}(Cpu *c) {{'):]
self.assertIn('return;', body)
self.assertNotIn(f'{self.bases["plugin0"] + 0x2000:#x}', body) # the trampoline is not reached
def test_replace_hook_keeps_an_unpatched_copy(self):
pb = self.bases['plugin0']
m = self.bases['main'] + FN
new = b(m, pb + 0x4200)
t = self.table(f'site mod_x main {FN:x} aa0003f3 {new:08x}', [f'orig mod_x plugin0 2000 main {FN:x}',
'seed plugin0 4200'])
self.assertEqual(t.origs_for('main', self.bases['main']), {m})
self.assertEqual(t.orig_entries('plugin0', self.bases), {pb + 0x2000: m})
c = self.emit(t, 'main', m, m + 12, origs=[m])
copy = c[c.index(f'void fo_{m:x}(Cpu *c) {{'):]
self.assertNotIn('rt_patch_on', copy)
self.assertIn('rt_patch_on[0]', c[:c.index(f'void fo_{m:x}')])
tab = gen.table_text('plugin0', [pb + 0x4200, pb + 0x2000], {pb + 0x2000: gen.orig_name(m)})
self.assertIn(f'void fo_{m:x}(Cpu *c);', tab)
self.assertIn(f'fo_{m:x}', tab[tab.index('_fn_ptrs'):])
def test_branch_hook_on_a_flag_write_leaves_with_the_flags_before_it(self):
# cmp x0, x1; cmp x2, x3 (b to the plugin's trampoline, which runs the cmp); b.eq +8; ret; ret
pb = self.bases['plugin0']
m = self.bases['main'] + FN
self.code({FN: 0xEB01001F, FN + 4: 0xEB03005F, FN + 8: 0x54000040, FN + 12: RET_W, FN + 16: RET_W})
t = self.table(f'site mod_x main {FN + 4:x} eb03005f {b(m + 4, pb + 0x4200):08x}', ['seed plugin0 4200'])
c = self.emit(t, 'main', m, m + 20)
on = c[c.index('if (rt_patch_on[0])'):]
on = on[:on.index('} else {')]
self.assertIn('c->nzcv = fnz_sub64(&F);', on) # the first cmp's flags go to the trampoline
self.assertIn(f'{pb + 0x4200:#x}', on)
def test_inline_hook_on_a_register_call(self):
# blr x8 (inline-hooked); ret
self.code({FN: 0xD63F0100, FN + 4: RET_W})
t = self.table(f'site mod_x main {FN:x} d63f0100 @call:plugin0+4000 d63f0100', ['stub plugin0 4000'])
m = self.bases['main'] + FN
c = self.emit(t, 'main', m, m + 8)
stub = self.bases['plugin0'] + 0x4000
on = c[c.index('if (rt_patch_on[0])'):]
on = on[:on.index('} else {')]
self.assertLess(on.index(f'f_{stub:x}(c);'), on.index('rt_dispatch(c);')) # the hook, then the blr
self.assertIn(f'x30 = {m + 4:#x}ull; c->x[30] = x30; rt_dispatch(c);', on)
def test_orig_trampoline_counts_as_a_seed(self):
# a veneer slot names a replace hook's trampoline: dispatch serves it with the function's unpatched copy
main = self.d / 'exefs' / 'main'
raw = bytearray(main.read_bytes())
struct.pack_into('<II', raw, 0x108, b(8, FN, True), RET_W)
main.write_bytes(raw)
m = self.bases['main'] + FN
self.table(f'site mod_x main {FN:x} aa0003f3 {b(m, self.bases["plugin0"] + 0x4200):08x}',
[f'orig mod_x plugin0 2000 main {FN:x}', 'seed plugin0 4200 2000'])
run = subprocess.run([sys.executable, str(ROOT / 'recomp/gen.py'), '--exefs', str(self.d / 'exefs'),
'--out', str(self.d / 'generated'), '--modules', 'main,plugin0', '--plugins',
str(self.plugin_file), '--patches', str(self.d / 'p.txt'), '--jobs', '1'],
capture_output=True, text=True, timeout=30)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
def test_image_must_match_its_hash(self):
t = self.table(f'site mod_x main {FN:x} aa0003f3 @call:plugin0+4000 aa0003f3', ['stub plugin0 4000'])
t.plugins['plugin0']['sha256'] = '0' * 64
with self.assertRaises(patches.PatchError):
gen.open_module('plugin0', self.plugin_file, self.bases['plugin0'], t)
def test_chunk_key_takes_the_unpatched_copy(self):
# the lift cache (recomp/gen.py chunk_key): a chunk whose function gets an unpatched copy is keyed apart, and a
# chunk without one keeps the key it had before plugins
fns = [(0x100, 0x10c)]
plain = gen.chunk_key('m', 0, fns, {}, [], {}, [], None, None)
self.assertEqual(gen.chunk_key('m', 0, fns, {}, [], {}, [], None, None, {0x200}), plain)
self.assertNotEqual(gen.chunk_key('m', 0, fns, {}, [], {}, [], None, None, {0x100}), plain)
site = {0x100: [(0, [('call', 0x7005204000), 0xAA0003F3])]}
self.assertNotEqual(gen.chunk_key('m', 0, fns, site, [0x100], {}, [], None, None),
gen.chunk_key('m', 0, fns, {0x100: [(0, [('call', 0x7005204010), 0xAA0003F3])]}, [0x100],
{}, [], None, None))
def test_no_plugin_lines_no_plugin_header(self):
(self.d / 'q.txt').write_text('set fps60 TOTK_FPS60 0\n')
self.assertNotIn('PLUGIN', patches.header_text(patches.load(self.d / 'q.txt', self.mods), 'MARK'))
if __name__ == '__main__':
unittest.main()