182 lines
6.7 KiB
Python
182 lines
6.7 KiB
Python
import ctypes
|
|
import hashlib
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
SOURCE = ROOT / "iggy-decompiled/src/reconstructed/avm2_microcode.c"
|
|
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
|
DATA_SIZE = 0x2D0
|
|
RECORD_SIZE = 5
|
|
RECORD_COUNT = DATA_SIZE // RECORD_SIZE
|
|
DATA_SHA256 = "cdf55985fcad0b6b58aa053d83feb93d69772d211e3fa02550961da2de689ea2"
|
|
|
|
|
|
class ReconstructedAvm2MicrocodeTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.temporary_directory = tempfile.TemporaryDirectory()
|
|
library_path = Path(cls.temporary_directory.name) / "libavm2_microcode.dylib"
|
|
result = subprocess.run(
|
|
[
|
|
"clang",
|
|
"-dynamiclib",
|
|
"-std=c11",
|
|
"-Wall",
|
|
"-Wextra",
|
|
"-Werror",
|
|
"-I",
|
|
str(RECONSTRUCTED),
|
|
str(SOURCE),
|
|
"-o",
|
|
str(library_path),
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
if result.returncode != 0:
|
|
raise AssertionError(result.stdout + result.stderr)
|
|
cls.library = ctypes.CDLL(str(library_path))
|
|
cls.library.as3vm_microcode_init.argtypes = []
|
|
cls.library.as3vm_microcode_init.restype = None
|
|
cls.table_type = ctypes.c_void_p * 256
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.temporary_directory.cleanup()
|
|
|
|
def setUp(self):
|
|
table = self.table_type.in_dll(self.library, "avm2_pseudo_microcode")
|
|
for opcode in range(256):
|
|
table[opcode] = None
|
|
|
|
def test_init_maps_every_recovered_record_and_leaves_other_opcodes_null(self):
|
|
table = self.table_type.in_dll(self.library, "avm2_pseudo_microcode")
|
|
self.assertTrue(all(pointer is None for pointer in table))
|
|
|
|
self.library.as3vm_microcode_init()
|
|
populated = [pointer for pointer in table if pointer is not None]
|
|
self.assertEqual(len(populated), RECORD_COUNT)
|
|
data_address = min(populated) - 1
|
|
data = ctypes.string_at(data_address, DATA_SIZE)
|
|
self.assertEqual(hashlib.sha256(data).hexdigest(), DATA_SHA256)
|
|
data_before_repeat = bytes(data)
|
|
|
|
records = [data[offset : offset + RECORD_SIZE] for offset in range(0, DATA_SIZE, RECORD_SIZE)]
|
|
opcodes = {record[0] for record in records}
|
|
self.assertEqual(len(opcodes), RECORD_COUNT)
|
|
for index, record in enumerate(records):
|
|
self.assertEqual(table[record[0]], data_address + index * RECORD_SIZE + 1)
|
|
self.assertEqual(ctypes.string_at(table[record[0]], 4), record[1:])
|
|
for opcode in set(range(256)) - opcodes:
|
|
self.assertIsNone(table[opcode])
|
|
self.assertNotIn(0x00, opcodes)
|
|
self.assertNotIn(0x01, opcodes)
|
|
self.assertNotIn(0xFF, opcodes)
|
|
self.assertEqual(records[0], bytes.fromhex("a0 02 02 09 00"))
|
|
self.assertEqual(records[-1], bytes.fromhex("a7 03 03 0b 00"))
|
|
self.assertEqual(ctypes.string_at(table[0x2B], 4), bytes.fromhex("02 02 09 09"))
|
|
|
|
first_mapping = tuple(table)
|
|
self.library.as3vm_microcode_init()
|
|
self.assertEqual(tuple(table), first_mapping)
|
|
self.assertEqual(ctypes.string_at(data_address, DATA_SIZE), data_before_repeat)
|
|
|
|
def test_init_preserves_missing_slots_and_duplicate_records_are_last_wins(self):
|
|
table = self.table_type.in_dll(self.library, "avm2_pseudo_microcode")
|
|
self.library.as3vm_microcode_init()
|
|
data_address = table[0xA0] - 1
|
|
canonical_data = ctypes.string_at(data_address, DATA_SIZE)
|
|
records = [
|
|
canonical_data[offset : offset + RECORD_SIZE]
|
|
for offset in range(0, DATA_SIZE, RECORD_SIZE)
|
|
]
|
|
opcodes = {record[0] for record in records}
|
|
sentinel_storage = ctypes.c_uint32(0x12345678)
|
|
sentinel = ctypes.addressof(sentinel_storage)
|
|
|
|
for opcode in range(256):
|
|
table[opcode] = sentinel
|
|
self.library.as3vm_microcode_init()
|
|
for opcode in set(range(256)) - opcodes:
|
|
self.assertEqual(table[opcode], sentinel)
|
|
for opcode in opcodes:
|
|
self.assertNotEqual(table[opcode], sentinel)
|
|
|
|
final_opcode = ctypes.c_uint8.from_address(data_address + DATA_SIZE - RECORD_SIZE)
|
|
self.assertEqual(final_opcode.value, 0xA7)
|
|
final_opcode.value = 0xA0
|
|
try:
|
|
for opcode in range(256):
|
|
table[opcode] = sentinel
|
|
self.library.as3vm_microcode_init()
|
|
self.assertEqual(table[0xA0], data_address + DATA_SIZE - RECORD_SIZE + 1)
|
|
self.assertEqual(table[0xA7], sentinel)
|
|
finally:
|
|
final_opcode.value = 0xA7
|
|
self.assertEqual(ctypes.string_at(data_address, DATA_SIZE), canonical_data)
|
|
|
|
def test_object_exports_only_the_lookup_table_and_initializer(self):
|
|
temporary_path = Path(self.temporary_directory.name)
|
|
object_path = temporary_path / "avm2_microcode.o"
|
|
result = subprocess.run(
|
|
[
|
|
"clang",
|
|
"-std=c11",
|
|
"-Wall",
|
|
"-Wextra",
|
|
"-Werror",
|
|
"-I",
|
|
str(RECONSTRUCTED),
|
|
"-c",
|
|
str(SOURCE),
|
|
"-o",
|
|
str(object_path),
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
self.assertEqual(
|
|
self.object_symbols(object_path, "--defined-only"),
|
|
{"as3vm_microcode_init", "avm2_pseudo_microcode"},
|
|
)
|
|
self.assertEqual(self.object_symbols(object_path, "--undefined-only"), set())
|
|
|
|
ir = subprocess.run(
|
|
[
|
|
"clang",
|
|
"-m32",
|
|
"-O2",
|
|
"-std=c11",
|
|
"-S",
|
|
"-emit-llvm",
|
|
"-I",
|
|
str(RECONSTRUCTED),
|
|
str(SOURCE),
|
|
"-o",
|
|
"-",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(ir.returncode, 0, ir.stdout + ir.stderr)
|
|
self.assertIn("[256 x ptr] zeroinitializer", ir.stdout)
|
|
self.assertIn("internal global [720 x i8]", ir.stdout)
|
|
|
|
def object_symbols(self, object_path, mode):
|
|
result = subprocess.run(
|
|
["xcrun", "llvm-nm", mode, "--extern-only", str(object_path)],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
return {line.split()[-1].removeprefix("_") for line in result.stdout.splitlines()}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|