294 lines
13 KiB
Python
294 lines
13 KiB
Python
"""Durango-only contract tests for the reconstructed win32_rrThreads2 module.
|
|
|
|
Mirrors tests/test_reconstructed_iggy_font_provider_cross.py but SINGLE-
|
|
PLATFORM (durango only — win32_rrThreads2 appears solely in
|
|
iggy_durango.modules.tsv; no orbis/ps3/vita raw source or object exists).
|
|
|
|
RED until win32_rrThreads2.c / win32_rrThreads2.h (and their CMake /
|
|
modules.tsv rows) exist; the retail-evidence assertions run now and prove
|
|
the nm/strings parsing is correct.
|
|
|
|
How EXPECTED_* were derived (retail object
|
|
work/iggy-native-decomp/durango-objects/win32_rrThreads2.obj; ignore the
|
|
empty durango_rrThreads2.obj which is only @comp.id/@feat.00):
|
|
|
|
EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only <obj>` T-tagged
|
|
names — exactly 21 uppercase-T exports. The three
|
|
file-static helpers rad_to_win_pri / rr_set_thread_name
|
|
/ set_cpu_core are lowercase-t and NOT listed here.
|
|
(win32_shim / win_to_rad_pri are also local-t with no
|
|
Ghidra body — internal only.)
|
|
|
|
EXPECTED_GLOBALS = empty. active_rrthreads / number_cores / the nested
|
|
static in rr_set_thread_name are lowercase-d locals,
|
|
not exported D/B/C. Same shape as
|
|
stb_truetype_wrapper's EXPECTED_GLOBALS = set().
|
|
|
|
EXPECTED_IMPORTS = the three genuine cross-module Iggy symbols from
|
|
`llvm-nm --undefined-only` after normalized_symbol
|
|
strips __imp_: rrAtomicAddExchange32,
|
|
rrAtomicMemoryBarrierFull (win32_rrAtomics.obj),
|
|
rrGetCPUCoreByPrecedence (durango_rrCpu.obj).
|
|
rrGetTicks is NOT imported.
|
|
|
|
RUNTIME_ALLOWLISTS = residual undefined symbols after subtracting the 3
|
|
imports — 2 toolchain (__C_specific_handler SEH,
|
|
__ImageBase) + 21 genuine Win32 API names = 23.
|
|
Total undefined = 26. No __security_cookie /
|
|
__GSHandlerCheck / _fltused / __chkstk on this object.
|
|
|
|
PROVENANCE = the object's own build-temp path embedded in strings
|
|
(NOT a c:\\projects\\g\\src\\flash\\... source path):
|
|
c:\\projects\\g\\build\\release\\temps\\libdu\\
|
|
iggy_durango\\win32_rrThreads2.obj
|
|
(same shape as the original_object column in
|
|
iggy_durango.modules.tsv).
|
|
|
|
Macro convention for the future .c (not authored here):
|
|
IGGY_RECONSTRUCT_WIN32_RRTHREADS2_DURANGO — single-platform #error idiom
|
|
as in swf_draw.c.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
|
SOURCE = RECONSTRUCTED / "win32_rrThreads2.c"
|
|
HEADER = RECONSTRUCTED / "win32_rrThreads2.h"
|
|
LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm")
|
|
RETAIL_OBJ = (
|
|
ROOT / "work/iggy-native-decomp/durango-objects/win32_rrThreads2.obj")
|
|
|
|
EXPECTED_FUNCTIONS = {
|
|
"rrMutexCreate20",
|
|
"rrMutexDestroy20",
|
|
"rrMutexLock20",
|
|
"rrMutexLockTimeout20",
|
|
"rrMutexUnlock20",
|
|
"rrPlatformHandleGetPriority20",
|
|
"rrSemaphoreCreate20",
|
|
"rrSemaphoreDecrementOrWait20",
|
|
"rrSemaphoreDestroy20",
|
|
"rrSemaphoreIncrement20",
|
|
"rrThreadCreate20",
|
|
"rrThreadDestroy20",
|
|
"rrThreadGetCore20",
|
|
"rrThreadGetCurrentThreadID20",
|
|
"rrThreadGetName20",
|
|
"rrThreadGetOSThreadID20",
|
|
"rrThreadNativePriorityHelper",
|
|
"rrThreadPriorityAdd",
|
|
"rrThreadSetPriorityThreadID",
|
|
"rrThreadSleep20",
|
|
"rrThreadSpinHyperYield20",
|
|
}
|
|
# No owned external data globals (only lowercase-d locals in the retail obj).
|
|
EXPECTED_GLOBALS = set()
|
|
|
|
EXPECTED_IMPORTS = {
|
|
"rrAtomicAddExchange32",
|
|
"rrAtomicMemoryBarrierFull",
|
|
"rrGetCPUCoreByPrecedence",
|
|
}
|
|
|
|
RUNTIME_ALLOWLISTS = {
|
|
"durango": {
|
|
"__C_specific_handler", "__ImageBase",
|
|
"CloseHandle", "CreateMutexA", "CreateSemaphoreExW", "CreateThread",
|
|
"DeleteCriticalSection", "EnterCriticalSection", "GetCurrentThread",
|
|
"GetCurrentThreadId", "GetThreadPriority",
|
|
"InitializeCriticalSectionAndSpinCount", "LeaveCriticalSection",
|
|
"OpenThread", "RaiseException", "ReleaseMutex", "ReleaseSemaphore",
|
|
"ResumeThread", "SetThreadIdealProcessorEx", "SetThreadPriority",
|
|
"Sleep", "TryEnterCriticalSection", "WaitForSingleObject",
|
|
},
|
|
}
|
|
|
|
# Object's own embedded build-temp path (strings on retail .obj).
|
|
PROVENANCE = (
|
|
r"c:\projects\g\build\release\temps\libdu\iggy_durango\win32_rrThreads2.obj")
|
|
|
|
|
|
def normalized_symbol(name):
|
|
name = name.removeprefix(".")
|
|
name = name.removeprefix("__imp_")
|
|
if name == "_setjmp":
|
|
return "setjmp"
|
|
return name
|
|
|
|
|
|
class ReconstructedWin32RrThreads2CrossTests(unittest.TestCase):
|
|
def test_native_provenance_counts_source_path_and_inventory(self):
|
|
# source_functions=24, lines=552 from iggy_durango.modules.tsv col 3
|
|
# and `wc -l` on the raw .c. category column is "platform".
|
|
modules = ROOT / "iggy-decompiled/symbols/iggy_durango.modules.tsv"
|
|
row = next(
|
|
line for line in modules.read_text(encoding="utf-8").splitlines()
|
|
if line.startswith("win32_rrThreads2\t"))
|
|
cols = row.split("\t")
|
|
self.assertEqual(cols[1], "platform")
|
|
self.assertEqual(int(cols[2]), 24)
|
|
self.assertEqual(int(cols[3]), 552)
|
|
self.assertEqual(cols[4], PROVENANCE)
|
|
|
|
raw = ROOT / "iggy-decompiled/raw/durango/win32_rrThreads2.c"
|
|
self.assertEqual(
|
|
len(raw.read_text(encoding="utf-8").splitlines()), 552)
|
|
|
|
self.assertTrue(RETAIL_OBJ.exists(), f"missing {RETAIL_OBJ}")
|
|
symbols = subprocess.run(
|
|
["nm", "-g", str(RETAIL_OBJ)], capture_output=True, text=True)
|
|
self.assertEqual(symbols.returncode, 0, symbols.stderr)
|
|
normalized = symbols.stdout.replace(" .", " ")
|
|
for symbol in EXPECTED_FUNCTIONS | EXPECTED_GLOBALS:
|
|
self.assertIn(symbol, normalized, symbol)
|
|
|
|
# Retail undefined set must be exactly imports + allowlist (26).
|
|
if LLVM_NM.exists():
|
|
undefined = subprocess.run(
|
|
[str(LLVM_NM), "--undefined-only", str(RETAIL_OBJ)],
|
|
capture_output=True, text=True)
|
|
self.assertEqual(undefined.returncode, 0, undefined.stderr)
|
|
undef = {
|
|
normalized_symbol(line.split()[-1])
|
|
for line in undefined.stdout.splitlines()
|
|
if line.split()
|
|
}
|
|
self.assertEqual(
|
|
undef - RUNTIME_ALLOWLISTS["durango"], EXPECTED_IMPORTS, undef)
|
|
self.assertEqual(
|
|
undef,
|
|
EXPECTED_IMPORTS | RUNTIME_ALLOWLISTS["durango"])
|
|
self.assertEqual(len(undef), 26)
|
|
|
|
defined = subprocess.run(
|
|
[str(LLVM_NM), "--extern-only", "--defined-only",
|
|
str(RETAIL_OBJ)],
|
|
capture_output=True, text=True)
|
|
exports = {
|
|
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"}
|
|
}
|
|
self.assertEqual(exports, EXPECTED_FUNCTIONS | EXPECTED_GLOBALS)
|
|
|
|
strings = subprocess.run(
|
|
["strings", str(RETAIL_OBJ)], capture_output=True, text=True)
|
|
self.assertIn(PROVENANCE, strings.stdout)
|
|
# No src\\flash source path in this object's strings (verified).
|
|
self.assertNotIn(r"src\flash\win32_rrThreads2", strings.stdout)
|
|
|
|
# Absent on other platforms' module inventories.
|
|
for platform in ("orbis", "ps3", "vita"):
|
|
tsv = ROOT / f"iggy-decompiled/symbols/iggy_{platform}.modules.tsv"
|
|
text = tsv.read_text(encoding="utf-8")
|
|
self.assertNotIn("win32_rrThreads2\t", text, platform)
|
|
|
|
if SOURCE.exists():
|
|
# Single-platform CMake stanza (no foreach loop) — fully-resolved
|
|
# literal target / macro names.
|
|
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(
|
|
encoding="utf-8")
|
|
self.assertIn(
|
|
"iggy_reconstructed_win32_rrThreads2_durango", cmake)
|
|
self.assertIn(
|
|
"add_library(iggy_reconstructed_win32_rrThreads2_durango "
|
|
"STATIC win32_rrThreads2.c)",
|
|
cmake)
|
|
self.assertIn(
|
|
"IGGY_RECONSTRUCT_WIN32_RRTHREADS2_DURANGO", cmake)
|
|
inventory = (RECONSTRUCTED / "modules.tsv").read_text(
|
|
encoding="utf-8")
|
|
# source_functions=24, external_functions=21, durango only.
|
|
self.assertIn(
|
|
"win32_rrThreads2\t24\t21\tdurango\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}")
|
|
# Reconstructed source embeds the retail provenance string that
|
|
# actually appears in the object (build-temp path, not src\\flash).
|
|
self.assertIn(
|
|
r"c:\\projects\\g\\build\\release\\temps\\libdu\\iggy_durango\\"
|
|
r"win32_rrThreads2.obj",
|
|
SOURCE.read_text(encoding="utf-8"))
|
|
|
|
function_pointers = "".join(
|
|
f" (GenericFn)&{name},\n"
|
|
for name in sorted(EXPECTED_FUNCTIONS))
|
|
# Zero-globals probe shape (stb_truetype_wrapper precedent): function
|
|
# pointer table only, no (void)&global lines.
|
|
probe_text = (
|
|
'#include <stdint.h>\n#include "win32_rrThreads2.h"\n'
|
|
'typedef void (*GenericFn)(void);\n'
|
|
'static GenericFn rr_abis[] = {\n' + function_pointers + '};\n'
|
|
'int use_abis(void) {\n'
|
|
' for (unsigned i = 0; '
|
|
'i < sizeof(rr_abis) / sizeof(rr_abis[0]); ++i) {\n'
|
|
' if (!rr_abis[i]) return 0;\n'
|
|
' }\n'
|
|
' return 1;\n'
|
|
'}\n')
|
|
|
|
compiler, target = "clang", "x86_64-pc-windows-msvc"
|
|
platform = "durango"
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
temporary = Path(directory)
|
|
output = temporary / f"win32_rrThreads2_{platform}.o"
|
|
build = subprocess.run([
|
|
compiler, "-target", target, "-ffreestanding", "-fno-builtin",
|
|
"-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
|
"-DIGGY_RECONSTRUCT_WIN32_RRTHREADS2_DURANGO=1",
|
|
"-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output),
|
|
], capture_output=True, text=True)
|
|
self.assertEqual(build.returncode, 0, 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)
|
|
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"win32_rrThreads2_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",
|
|
"-DIGGY_RECONSTRUCT_WIN32_RRTHREADS2_DURANGO=1",
|
|
"-I", str(RECONSTRUCTED), str(probe),
|
|
], capture_output=True, text=True)
|
|
self.assertEqual(
|
|
syntax.returncode, 0, (language, syntax.stderr))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|