260 lines
12 KiB
Python
260 lines
12 KiB
Python
"""Cross-platform contract tests for the reconstructed gr_strokepath module.
|
|
|
|
Mirrors tests/test_reconstructed_stb_truetype_wrapper_cross.py (the freshest
|
|
4-platform template whose imports GENUINELY differ per platform). RED until
|
|
gr_strokepath.c / gr_strokepath.h (and their CMake / modules.tsv rows) exist;
|
|
the retail-evidence assertions run now and prove the nm/strings parsing.
|
|
|
|
How EXPECTED_* were derived (all four .obj under
|
|
work/iggy-native-decomp/<platform>-objects/gr_strokepath.obj):
|
|
|
|
EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only <obj>` T-tagged
|
|
names, leading '.' stripped on ps3. IDENTICAL 3-name
|
|
set on durango/orbis/ps3/vita -- the exported ABI is
|
|
invariant. Retail *function* counts differ
|
|
(21/19/19/19) because durango inlines two extra
|
|
file-static helpers.
|
|
|
|
EXPECTED_GLOBALS = {iggy_aawidth} -- the single owned named data symbol
|
|
(tag 'D', 4 bytes == 0x3faccccd == 1.35f) present on
|
|
all four objects. On ps3 the .opd function
|
|
descriptors also surface as 'D' for every export;
|
|
those normalize to the exported function names and are
|
|
NOT separate globals (same handling as font_provider).
|
|
|
|
EXPECTED_IMPORTS = universal non-scaffolding core present on all four
|
|
retail objects: the 8 iggy_*/rr_isnan_f collaborators
|
|
plus memcpy and pow. pow is on every platform, so it
|
|
is core. libm sqrt / libc memset+abs GENUINELY differ
|
|
per platform -- see PLATFORM_IMPORTS.
|
|
|
|
PLATFORM_IMPORTS = per-platform extras on top of the core, from retail
|
|
`llvm-nm --undefined-only` (normalized: strip leading
|
|
'.', strip '__imp_'):
|
|
durango: sqrt
|
|
orbis: {} (-O2 inlines sqrt as sqrtsd on x86_64)
|
|
ps3: {} (PowerPC emits hardware fsqrt)
|
|
vita: abs, memset, sqrt
|
|
This same durango+vita-vs-orbis-vs-ps3 libm split is
|
|
the exact precedent already encoded for
|
|
stb_truetype_wrapper (ceil/floor/sqrt) and
|
|
swf_fontcache.
|
|
|
|
RUNTIME_ALLOWLISTS = pure toolchain scaffolding residual after subtracting
|
|
core + platform extras, verified exact against retail:
|
|
durango: __GSHandlerCheck, __security_check_cookie,
|
|
__security_cookie, _fltused
|
|
orbis: _GLOBAL_OFFSET_TABLE_
|
|
ps3: {} (empty)
|
|
vita: __aeabi_unwind_cpp_pr0,
|
|
__aeabi_unwind_cpp_pr1, __stack_chk_fail,
|
|
__stack_chk_guard
|
|
|
|
PROVENANCE = the canonical Windows source path
|
|
c:\\projects\\g\\src\\flash\\gr_strokepath.c is
|
|
embedded in every one of the four retail objects
|
|
(unlike stb_truetype_wrapper, whose durango object
|
|
strips it); assert it uniformly, like font_provider.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
|
SOURCE = RECONSTRUCTED / "gr_strokepath.c"
|
|
HEADER = RECONSTRUCTED / "gr_strokepath.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 = {
|
|
"iggy_stroke_path",
|
|
"iggy_stroke_path_for_antialiasing",
|
|
"iggy_stroke_path_free",
|
|
}
|
|
EXPECTED_GLOBALS = {
|
|
"iggy_aawidth",
|
|
}
|
|
|
|
# Universal non-scaffolding imports present on all four retail objects.
|
|
EXPECTED_IMPORTS = {
|
|
# 9-slice stroke transform.
|
|
"iggy_9slice_scale_forstroke",
|
|
"iggy_9slice_set_stroke_xform",
|
|
# allocation + temp arena.
|
|
"iggy_free_alloc",
|
|
"iggy_malloc_fileline",
|
|
"iggy_temp_alloc",
|
|
"iggy_temp_free",
|
|
# telemetry marker global.
|
|
"iggy_tm_normal",
|
|
# NaN guard.
|
|
"rr_isnan_f",
|
|
# libc / libm the reconstruction lowers on every target.
|
|
"memcpy",
|
|
"pow",
|
|
}
|
|
# Per-platform libm / libc extras (NOT forced into a single uniform set).
|
|
PLATFORM_IMPORTS = {
|
|
"durango": {"sqrt"},
|
|
"orbis": set(),
|
|
"ps3": set(),
|
|
"vita": {"abs", "memset", "sqrt"},
|
|
}
|
|
RUNTIME_ALLOWLISTS = {
|
|
"durango": {"__GSHandlerCheck", "__security_check_cookie",
|
|
"__security_cookie", "_fltused"},
|
|
"orbis": {"_GLOBAL_OFFSET_TABLE_"},
|
|
"ps3": set(),
|
|
"vita": {"__aeabi_unwind_cpp_pr0", "__aeabi_unwind_cpp_pr1",
|
|
"__stack_chk_fail", "__stack_chk_guard"},
|
|
}
|
|
|
|
|
|
def normalized_symbol(name):
|
|
name = name.removeprefix(".")
|
|
name = name.removeprefix("__imp_")
|
|
if name == "_setjmp":
|
|
return "setjmp"
|
|
return name
|
|
|
|
|
|
class ReconstructedGrStrokepathCrossTests(unittest.TestCase):
|
|
def test_native_provenance_counts_source_path_and_inventory(self):
|
|
# (function count, raw line count) per platform, from
|
|
# iggy_<platform>.modules.tsv column 3 and `wc -l` on the raw .c.
|
|
expected = {
|
|
"durango": (21, 7288), "orbis": (19, 7224),
|
|
"ps3": (19, 7640), "vita": (19, 7732),
|
|
}
|
|
for platform, (functions, lines) in expected.items():
|
|
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("gr_strokepath\t"))
|
|
self.assertEqual(int(row.split("\t")[2]), functions, platform)
|
|
raw = ROOT / f"iggy-decompiled/raw/{platform}/gr_strokepath.c"
|
|
self.assertEqual(
|
|
len(raw.read_text(encoding="utf-8").splitlines()),
|
|
lines, platform)
|
|
obj = ROOT / (
|
|
f"work/iggy-native-decomp/{platform}-objects/gr_strokepath.obj")
|
|
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_FUNCTIONS | EXPECTED_GLOBALS:
|
|
self.assertIn(symbol, normalized, (platform, symbol))
|
|
strings = subprocess.run(
|
|
["strings", str(obj)], capture_output=True, text=True)
|
|
self.assertIn(r"c:\projects\g\src\flash\gr_strokepath.c",
|
|
strings.stdout, platform)
|
|
|
|
if SOURCE.exists():
|
|
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(
|
|
encoding="utf-8")
|
|
self.assertIn(
|
|
"iggy_reconstructed_gr_strokepath_${platform}", cmake)
|
|
self.assertIn(
|
|
"add_library(${target} STATIC gr_strokepath.c)", cmake)
|
|
self.assertIn(
|
|
"IGGY_RECONSTRUCT_GR_STROKEPATH_${platform_upper}", cmake)
|
|
inventory = (RECONSTRUCTED / "modules.tsv").read_text(
|
|
encoding="utf-8")
|
|
# Exports are invariant (3) but retail source_functions vary
|
|
# (21/19/19/19), so the inventory uses per-platform rows exactly
|
|
# like stb_truetype_wrapper / iggy_font_provider.
|
|
for platform, functions in (
|
|
("durango", 21), ("orbis", 19),
|
|
("ps3", 19), ("vita", 19)):
|
|
self.assertIn(
|
|
f"gr_strokepath\t{functions}\t3\t{platform}\tcomplete",
|
|
inventory)
|
|
|
|
def test_exact_exports_globals_normalized_imports_and_c_cpp_abi(self):
|
|
self.assertTrue(SOURCE.exists(), f"missing {SOURCE}")
|
|
self.assertTrue(HEADER.exists(), f"missing {HEADER}")
|
|
self.assertIn(r"c:\\projects\\g\\src\\flash\\gr_strokepath.c",
|
|
SOURCE.read_text(encoding="utf-8"))
|
|
|
|
function_pointers = "".join(
|
|
f" (GenericFn)&{name},\n" for name in sorted(EXPECTED_FUNCTIONS))
|
|
probe_text = (
|
|
'#include <stdint.h>\n#include "gr_strokepath.h"\n'
|
|
'typedef void (*GenericFn)(void);\n'
|
|
'static GenericFn stroke_abis[] = {\n' + function_pointers + '};\n'
|
|
'int use_abis(void) {\n'
|
|
' (void)&iggy_aawidth;\n'
|
|
' for (unsigned i = 0; '
|
|
'i < sizeof(stroke_abis) / sizeof(stroke_abis[0]); ++i) {\n'
|
|
' if (!stroke_abis[i]) return 0;\n'
|
|
' }\n'
|
|
' return 1;\n'
|
|
'}\n')
|
|
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
temporary = Path(directory)
|
|
for platform, (compiler, target) in VARIANTS.items():
|
|
extra = ["-mthumb"] if platform == "vita" else []
|
|
output = temporary / f"gr_strokepath_{platform}.o"
|
|
build = subprocess.run([
|
|
compiler, "-target", target, "-ffreestanding",
|
|
"-fno-builtin",
|
|
"-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
|
*extra,
|
|
f"-DIGGY_RECONSTRUCT_GR_STROKEPATH_{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)
|
|
names = {normalized_symbol(parts[-1])
|
|
for line in defined.stdout.splitlines()
|
|
if len(parts := line.split()) >= 2 and
|
|
parts[-2].upper() in {"T", "D", "B", "C"}}
|
|
imports = {normalized_symbol(line.split()[-1])
|
|
for line in undefined.stdout.splitlines()
|
|
if line.split()}
|
|
self.assertEqual(
|
|
names, EXPECTED_FUNCTIONS | EXPECTED_GLOBALS, platform)
|
|
expected_imports = (
|
|
EXPECTED_IMPORTS | PLATFORM_IMPORTS[platform])
|
|
self.assertEqual(
|
|
imports - RUNTIME_ALLOWLISTS[platform],
|
|
expected_imports, (platform, imports))
|
|
|
|
for language, standard in (("c", "c11"), ("c++", "c++17")):
|
|
suffix = "c" if language == "c" else "cpp"
|
|
probe = temporary / f"gr_strokepath_abi_{platform}.{suffix}"
|
|
probe.write_text(probe_text, encoding="utf-8")
|
|
syntax = subprocess.run([
|
|
compiler, "-target", target, "-ffreestanding",
|
|
"-fsyntax-only", f"-std={standard}",
|
|
"-Wall", "-Wextra", "-Werror", *extra,
|
|
f"-DIGGY_RECONSTRUCT_GR_STROKEPATH_{platform.upper()}=1",
|
|
"-I", str(RECONSTRUCTED), str(probe),
|
|
], capture_output=True, text=True)
|
|
self.assertEqual(syntax.returncode, 0,
|
|
(platform, language, syntax.stderr))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|