Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

fix: discover address-taken functions with external tail calls

Sennett Laue20fcd231b51
diff --git a/recomp/analysis.cpp b/recomp/analysis.cpp
index 2d964ba..e8b5fdf 100644
--- a/recomp/analysis.cpp
+++ b/recomp/analysis.cpp
@@ -52,7 +52,13 @@ public:
if (has_delay_slot(decode(*image_.word(pc + 4), pc + 4).op)) return false;
seen.insert(pc + 4);
if (f == Flow::Branch || f == Flow::BranchLikely || (f == Flow::Jump && !p_.dynamic.count(pc))) {
- if (!valid_target(insn.branch_target_of())) return false;
+ // Fixed-address modules can tail-call a routine in the main
+ // executable. Like an external jal, that does not make the
+ // caller invalid; dispatch resolves the target at run time.
+ if (!valid_target(insn.branch_target_of())) {
+ if (f == Flow::Jump) break;
+ return false;
+ }
pending.push_back(insn.branch_target_of());
}
if (f == Flow::Jump || f == Flow::JumpRegister) break;
diff --git a/tests/analysis_tailcall.py b/tests/analysis_tailcall.py
new file mode 100644
index 0000000..98826f9
--- /dev/null
+++ b/tests/analysis_tailcall.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+"""Discover an address-taken function that tail-calls outside its image."""
+from pathlib import Path
+import argparse
+import struct
+import subprocess
+import tempfile
+
+root = Path(__file__).resolve().parents[1]
+parser = argparse.ArgumentParser(description=__doc__)
+parser.add_argument("--recompiler", type=Path, default=root / "build/tools/eerecomp")
+args = parser.parse_args()
+
+with tempfile.TemporaryDirectory(prefix='ps2-tailcall-test-') as temp:
+ work = Path(temp)
+ data = bytearray(0x150)
+ data[:7] = b'\x7fELF\x01\x01\x01'
+ struct.pack_into('<HHIIIIIHHHHHH', data, 16, 2, 8, 1, 0x100000, 52, 0, 0, 52, 32, 1, 40, 0, 0)
+ struct.pack_into('<8I', data, 52, 1, 0x100, 0x100000, 0, 0x50, 0x50, 5, 4)
+ # jal 0x100040; nop; take &0x100020; jr ra; nop
+ struct.pack_into('<6I', data, 0x100, 0x0c040010, 0, 0x3c080010, 0x35080020, 0x03e00008, 0)
+ # The address-taken function ends with a tail call outside this image.
+ struct.pack_into('<3I', data, 0x120, 0x24020001, 0x08080000, 0)
+ struct.pack_into('<2I', data, 0x140, 0x03e00008, 0)
+ (work / 'input.elf').write_bytes(data)
+ def check(words, count):
+ struct.pack_into('<3I', data, 0x120, *words)
+ (work / 'input.elf').write_bytes(data)
+ result = subprocess.run([str(args.recompiler.resolve()), str(work / 'input.elf'),
+ '--out', str(work / 'generated')], check=True, capture_output=True, text=True)
+ assert f'code {count} instructions' in result.stdout, result.stdout
+
+ check([0x24020001, 0x08080000, 0], 11)
+ # An invalid delay slot still rejects the candidate.
+ check([0x24020001, 0x08080000, 1], 8)
+ # beq zero,zero,0x100100: external conditional branches remain rejected.
+ check([0x24020001, 0x10000036, 0], 8)
+ # j 0x100030: a local destination containing an invalid opcode is rejected.
+ struct.pack_into('<I', data, 0x130, 1)
+ check([0x24020001, 0x0804000c, 0], 8)
+print('External tail-call discovery regression passed')