Files
neoLegacy-mac/tests/test_reconstructed_rrCore_cross.py
2026-07-17 21:37:39 -05:00

272 lines
12 KiB
Python

r"""Cross-platform contract tests for the reconstructed rrCore module.
rrCore is a 4-platform, DATA-ONLY translation unit. Unlike win32_rrAtomics
(functions, zero globals) this module inverts the shape: ZERO exported
functions and exactly ONE owned data global. Structure copied from
tests/test_reconstructed_win32_rrAtomics_cross.py and adapted so the module's
"exports" are a data symbol, not functions.
How EXPECTED_* were derived (all four .obj under
work/iggy-native-decomp/<platform>-objects/rrCore.obj; the perfmon/expr/patched
variants are ignored):
EXPECTED_FUNCTIONS = empty. `llvm-nm --extern-only --defined-only <obj>`
emits ZERO T-tagged names on every platform: rrCore
contains no code at all.
EXPECTED_GLOBALS = {g_fp_rrDisplayAssertion}. The single owned data symbol,
tagged `B` on durango/orbis/ps3 (.bss, zero-init) and `D`
on vita (.data, still zero). It is the display-assertion
callback default-initialized to NULL.
EXPECTED_IMPORTS = empty. `llvm-nm --undefined-only <obj>` emits zero lines
on every platform: a data-only unit imports nothing.
RUNTIME_ALLOWLISTS = empty on all four platforms because the retail objects,
and a clean freestanding recompile of rrCore.c, have zero
undefined symbols.
Provenance (embedded paths recovered with `strings`):
durango: c:\projects\g\build\release\temps\libdu\iggy_durango\rrCore.obj
orbis: c:\projects\shared\radrtl\rrCore.c (real RAD RTL source path)
ps3/vita: bare "rrCore.c"
Because the durango object is a build-temp .obj path (no code -> no src\flash
string) while the orbis object retains the true radrtl source path, the
reconstruction anchors to the radrtl path and the test checks each object's own
provenance independently.
modules.tsv convention: source_functions=0 (per-platform inventory column 3 is
0 on all four) and external_functions=0 (zero T exports). The owned data
global is tracked via EXPECTED_GLOBALS, not the function-count columns. Counts
are identical on every platform, so a single combined row is used (like
iggy_allocation), not per-platform rows.
Signature (authoritative, from iggy-decompiled/include/rrCore.h:1702, matching
the already-reconstructed triangulate_ears/as3vm_class importers):
rrbool (RADLINK *)(int *Ignored, const char *fileName, const int line,
const char *function, const char *message)
so the first argument is `int *` (not an unsigned flags word) and the callback
is fixed five-argument, NOT variadic.
Header note: reconstructed/rrCore.h forwards to the authoritative RAD Core
header (include/rrCore.h) so dependents such as rrMath.h keep resolving F32/U32
and the assertion prototype. That full header only compiles under a native /
platform toolchain (it needs MSVC/SDK platform macros and intrin.h), so the
C11+C++17 header ABI probe runs on the HOST only. The four-target freestanding
cross-compile exercises the self-contained rrCore.c, whose symbol table is the
retail contract being verified.
"""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
SOURCE = RECONSTRUCTED / "rrCore.c"
HEADER = RECONSTRUCTED / "rrCore.h"
LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm")
VARIANTS = {
"durango": ("clang", "x86_64-pc-windows-msvc"),
"orbis": ("clang", "x86_64-unknown-freebsd"),
"vita": ("clang", "armv7a-unknown-linux-gnueabihf"),
}
PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang")
if PS3_CLANG.exists():
VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu")
EXPECTED_FUNCTIONS = set()
EXPECTED_GLOBALS = {"g_fp_rrDisplayAssertion"}
EXPECTED_IMPORTS = set()
RUNTIME_ALLOWLISTS = {
"durango": set(), "orbis": set(), "ps3": set(), "vita": set(),
}
# Per-platform embedded provenance strings, verified with `strings` on each
# retail object.
PROVENANCE = {
"durango": r"c:\projects\g\build\release\temps\libdu\iggy_durango\rrCore.obj",
"orbis": r"c:\projects\shared\radrtl\rrCore.c",
"ps3": "rrCore.c",
"vita": "rrCore.c",
}
# The source path the reconstruction embeds (the real RAD RTL location kept by
# the orbis object).
SOURCE_PROVENANCE = r"c:\projects\shared\radrtl\rrCore.c"
def normalized_symbol(name):
name = name.removeprefix(".")
name = name.removeprefix("__imp_")
if name == "_setjmp":
return "setjmp"
return name
def defined_symbols_by_kind(output):
functions = set()
globals_ = set()
for line in output.splitlines():
parts = line.split()
if len(parts) < 2:
continue
kind = parts[-2].upper()
name = normalized_symbol(parts[-1])
if kind == "T":
functions.add(name)
elif kind in {"D", "B", "C"}:
globals_.add(name)
return functions, globals_
class ReconstructedRrCoreCrossTests(unittest.TestCase):
def test_native_provenance_counts_source_path_and_inventory(self):
# Per-platform inventory: category=platform, source_functions=0,
# lines=3 (the raw .c is an empty Ghidra stub -- verified with wc -l).
for platform in ("durango", "orbis", "ps3", "vita"):
modules = ROOT / f"iggy-decompiled/symbols/iggy_{platform}.modules.tsv"
row = next(
line for line in modules.read_text(encoding="utf-8").splitlines()
if line.startswith("rrCore\t"))
cols = row.split("\t")
self.assertEqual(cols[1], "platform", platform)
self.assertEqual(int(cols[2]), 0, platform)
self.assertEqual(int(cols[3]), 3, platform)
raw = ROOT / f"iggy-decompiled/raw/{platform}/rrCore.c"
self.assertEqual(
len(raw.read_text(encoding="utf-8").splitlines()), 3, platform)
obj = ROOT / (
f"work/iggy-native-decomp/{platform}-objects/rrCore.obj")
self.assertTrue(obj.exists(), f"missing {obj}")
# The one owned data global is present; nm -g surfaces it.
symbols = subprocess.run(
["nm", "-g", str(obj)], capture_output=True, text=True)
self.assertEqual(symbols.returncode, 0, (platform, symbols.stderr))
normalized = symbols.stdout.replace(" .", " ")
for symbol in EXPECTED_GLOBALS:
self.assertIn(symbol, normalized, (platform, symbol))
# Each object carries its own provenance string.
strings = subprocess.run(
["strings", "-a", str(obj)], capture_output=True, text=True)
self.assertEqual(strings.returncode, 0, (platform, strings.stderr))
self.assertIn(PROVENANCE[platform], strings.stdout, platform)
if LLVM_NM.exists():
undefined = subprocess.run(
[str(LLVM_NM), "--undefined-only", str(obj)],
capture_output=True, text=True)
self.assertEqual(undefined.returncode, 0, undefined.stderr)
imports = {
normalized_symbol(line.split()[-1])
for line in undefined.stdout.splitlines()
if line.split()
}
self.assertEqual(imports, EXPECTED_IMPORTS, platform)
defined = subprocess.run(
[str(LLVM_NM), "--extern-only", "--defined-only", str(obj)],
capture_output=True, text=True)
self.assertEqual(defined.returncode, 0, defined.stderr)
functions, globals_ = defined_symbols_by_kind(defined.stdout)
self.assertEqual(functions, EXPECTED_FUNCTIONS, platform)
self.assertEqual(globals_, EXPECTED_GLOBALS, platform)
if SOURCE.exists():
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(
encoding="utf-8")
self.assertIn("iggy_reconstructed_rrCore_${platform}", cmake)
self.assertIn(
"add_library(${target} STATIC rrCore.c)", cmake)
self.assertIn("IGGY_RECONSTRUCT_RRCORE_${platform_upper}", cmake)
inventory = (RECONSTRUCTED / "modules.tsv").read_text(
encoding="utf-8")
self.assertIn(
"rrCore\t0\t0\tdurango,orbis,ps3,vita\tcomplete", inventory)
def test_four_target_cross_compile_symbol_equality(self):
# The self-contained rrCore.c must cross-compile under all four
# freestanding platform triples and, on each, define exactly the owned
# data global with zero functions and zero imports -- the retail
# contract from work/iggy-native-decomp/<platform>-objects/rrCore.obj.
self.assertTrue(SOURCE.exists(), f"missing {SOURCE}")
# The reconstruction embeds the real RAD RTL source path.
self.assertIn(SOURCE_PROVENANCE, SOURCE.read_text(encoding="utf-8"))
with tempfile.TemporaryDirectory() as directory:
temporary = Path(directory)
for platform, (compiler, target) in VARIANTS.items():
extra = ["-mthumb"] if platform == "vita" else []
output = temporary / f"rrCore_{platform}.o"
build = subprocess.run([
compiler, "-target", target, "-ffreestanding",
"-fno-builtin", "-c", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", *extra,
f"-DIGGY_RECONSTRUCT_RRCORE_{platform.upper()}=1",
"-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output),
], capture_output=True, text=True)
self.assertEqual(build.returncode, 0, (platform, build.stderr))
if LLVM_NM.exists():
defined = subprocess.run([
str(LLVM_NM), "--extern-only", "--defined-only",
str(output),
], capture_output=True, text=True)
undefined = subprocess.run([
str(LLVM_NM), "--undefined-only", str(output),
], capture_output=True, text=True)
self.assertEqual(defined.returncode, 0, defined.stderr)
self.assertEqual(undefined.returncode, 0, undefined.stderr)
functions, globals_ = defined_symbols_by_kind(
defined.stdout)
imports = {
normalized_symbol(line.split()[-1])
for line in undefined.stdout.splitlines()
if line.split()
}
self.assertEqual(functions, EXPECTED_FUNCTIONS, platform)
self.assertEqual(globals_, EXPECTED_GLOBALS, platform)
self.assertEqual(
imports - RUNTIME_ALLOWLISTS[platform],
EXPECTED_IMPORTS, (platform, imports))
self.assertEqual(imports, set(), platform)
def test_header_forwards_and_is_c11_cpp17_includable(self):
# reconstructed/rrCore.h forwards to the authoritative RAD Core header,
# so it must (a) actually forward and (b) be includable as both C11 and
# C++17 on the host, where the full RAD header resolves the platform.
self.assertTrue(HEADER.exists(), f"missing {HEADER}")
self.assertIn(
'#include "../../include/rrCore.h"',
HEADER.read_text(encoding="utf-8"))
probe_text = (
'#include "rrCore.h"\n'
'int rrcore_abi_probe(void) {\n'
' return g_fp_rrDisplayAssertion == 0 ? 1 : 0;\n'
'}\n')
with tempfile.TemporaryDirectory() as directory:
temporary = Path(directory)
for language, standard in (("c", "c11"), ("c++", "c++17")):
suffix = "c" if language == "c" else "cpp"
probe = temporary / f"rrCore_abi.{suffix}"
probe.write_text(probe_text, encoding="utf-8")
syntax = subprocess.run([
"clang", "-fsyntax-only", f"-std={standard}",
"-Wall", "-Wextra", "-Werror",
"-I", str(RECONSTRUCTED), str(probe),
], capture_output=True, text=True)
self.assertEqual(
syntax.returncode, 0, (language, syntax.stderr))
if __name__ == "__main__":
unittest.main()