Files
neoLegacy-mac/tests/test_internal_layout.py

108 lines
3.9 KiB
Python

import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
INCLUDE_DIRECTORY = ROOT / "iggy-decompiled" / "src" / "internal"
GHIDRA_STUB_DIRECTORY = ROOT / "tools" / "ghidra" / "stubs"
class InternalLayoutTests(unittest.TestCase):
def test_durango_layout_static_assertions_compile_as_c(self):
self.compile_header("iggy_internal_durango.h")
def test_orbis_layout_static_assertions_compile_as_c(self):
self.compile_header("iggy_internal_orbis.h")
def test_ps3_layout_static_assertions_compile_as_c(self):
self.compile_header("iggy_internal_ps3.h")
def test_vita_layout_static_assertions_compile_as_c(self):
self.compile_header("iggy_internal_vita.h")
def test_ghidra_durango_overlay_matches_layout(self):
self.compile_overlay("_DURANGO", 0x3750, 0x0098, 0x28F0, 0x3534)
def test_ghidra_orbis_overlay_matches_layout(self):
self.compile_overlay("__ORBIS__", 0x3610, 0x0098, 0x28F0, 0x3534)
def test_ghidra_ps3_overlay_matches_layout(self):
self.compile_overlay("__CELLOS_LV2__", 0x3018, 0x0078, 0x2208, 0x2BC0, ["-m32"])
def test_ghidra_vita_overlay_matches_layout(self):
self.compile_overlay("__psp2__", 0x2D58, 0x0078, 0x2208, 0x2BC0, ["-m32"])
def compile_header(self, header_name):
compiler = os.environ.get("CC") or shutil.which("clang") or shutil.which("cc")
if compiler is None:
self.skipTest("no C compiler available")
source = f'#include "{header_name}"\nint main(void) {{ return 0; }}\n'
with tempfile.TemporaryDirectory() as temporary_directory:
source_path = Path(temporary_directory) / "layout_test.c"
source_path.write_text(source, encoding="utf-8")
result = subprocess.run(
[
compiler,
"-std=c11",
"-Wall",
"-Wextra",
"-Werror",
"-fsyntax-only",
"-I",
str(INCLUDE_DIRECTORY),
str(source_path),
],
capture_output=True,
text=True,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def compile_overlay(
self,
platform_define,
expected_size,
resource_entries_offset,
as3_vm_offset,
api_lock_offset,
extra_flags=None,
):
compiler = os.environ.get("CC") or shutil.which("clang") or shutil.which("cc")
if compiler is None:
self.skipTest("no C compiler available")
source = f'''\
#include "iggy_internal_overlay.h"
_Static_assert(sizeof(struct Iggy) == {expected_size}, "Iggy size");
_Static_assert(sizeof(struct IggyGeneric) == {expected_size}, "IggyGeneric size");
_Static_assert(__builtin_offsetof(struct Iggy, resource_entries) == {resource_entries_offset}, "resource entries");
_Static_assert(__builtin_offsetof(struct Iggy, as3_vm) == {as3_vm_offset}, "AS3 VM");
_Static_assert(__builtin_offsetof(struct Iggy, api_lock) == {api_lock_offset}, "API lock");
int main(void) {{ return 0; }}
'''
with tempfile.TemporaryDirectory() as temporary_directory:
source_path = Path(temporary_directory) / "overlay_test.c"
source_path.write_text(source, encoding="utf-8")
command = [compiler, "-std=c11", "-Werror", "-fsyntax-only"]
command.extend(extra_flags or [])
command.extend(
[
f"-D{platform_define}=1",
"-I",
str(GHIDRA_STUB_DIRECTORY),
str(source_path),
]
)
result = subprocess.run(command, capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
if __name__ == "__main__":
unittest.main()