mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-12-02 18:52:28 +00:00
Review of #650: - extract_code.py treated every leading word with top bits 0xD580 as a 12-byte v_nop_xbox_data. A plain v_nop (8 bytes) there would have been skipped as 12 and misaligned the rest. It now requires the literal operand (third source operand 255) that makes the instruction 12 bytes. Two unit tests cover a plain v_nop at the start and after hash words. - The test's hash words are replaced by obviously synthetic values of the same shape. - README: the reproducibility limit is stated; build.sh writes SHA256SUMS of the compiled inputs for cross-checking between people with the compilers. Wording: the v_nop opcode is public, its literal form is what the public decoder rejects; the listing is compiler output, not Microsoft documentation. The contradiction about publishing is replaced by a plain statement that the layout facts are published and that this waits for a maintainer decision. Signed-off-by: ates <ahmetrizaates@hotmail.com>
107 lines
4.6 KiB
Python
107 lines
4.6 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Unit tests for extract_code.py on synthetic containers (no GDK or compiler needed)."""
|
|
|
|
import importlib.util
|
|
import struct
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
spec = importlib.util.spec_from_file_location("extract_code", Path(__file__).with_name("extract_code.py"))
|
|
ec = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(ec)
|
|
|
|
S_NOP = struct.pack("<I", 0xBF800000) # public gfx10 encoding of s_nop 0
|
|
S_ENDPGM = struct.pack("<I", 0xBF810000)
|
|
# Synthetic, not compiler output: the v_nop opcode with made-up low bits, a second word whose
|
|
# third source operand is 255 (literal) with made-up low bits, and a made-up literal.
|
|
XBOX_DATA = struct.pack("<III", 0xD5801111, 0x03FC2222, 0x33333333)
|
|
# The public VOP3 encoding of a plain `v_nop` (llvm-mc -mcpu=gfx1030): 8 bytes, no literal.
|
|
V_NOP_E64 = struct.pack("<II", 0xD5800000, 0x02010080)
|
|
|
|
|
|
def fspx(sections, total=None):
|
|
"""sections: list of (kind, bytes). Builds an FSPX payload with the parsed layout."""
|
|
count = len(sections)
|
|
offset = 16 + 16 * count
|
|
table, body = b"", b""
|
|
for kind, data in sections:
|
|
table += struct.pack("<IIII", kind, offset + len(body), len(data), 0)
|
|
body += data
|
|
size = 16 + len(table) + len(body)
|
|
return b"FSPX" + struct.pack("<III", 1, count, size if total is None else total) + table + body
|
|
|
|
|
|
def dxbc(chunks):
|
|
"""chunks: list of (fourcc, bytes)."""
|
|
header = 32 + 4 * len(chunks)
|
|
offsets, body = [], b""
|
|
for fourcc, data in chunks:
|
|
offsets.append(header + len(body))
|
|
body += fourcc + struct.pack("<I", len(data)) + data
|
|
total = header + len(body)
|
|
head = b"DXBC" + b"\0" * 16 + struct.pack("<III", 1, total, len(chunks))
|
|
return head + b"".join(struct.pack("<I", o) for o in offsets) + body
|
|
|
|
|
|
class ExtractTests(unittest.TestCase):
|
|
def test_extracts_code_after_xbox_data_nops(self):
|
|
code = XBOX_DATA * 3 + S_NOP + S_ENDPGM + b"\0" * 8
|
|
container = dxbc([(b"DXIL", b"x" * 12), (b"ACCC", fspx([(0, b"a" * 28), (6, code), (7, b"b" * 8)]))])
|
|
extracted, skipped = ec.extract(container)
|
|
self.assertEqual(skipped, 3)
|
|
self.assertEqual(extracted, S_NOP + S_ENDPGM)
|
|
|
|
def test_plain_v_nop_is_code_not_xbox_data(self):
|
|
# A plain v_nop at the start of the code must not be skipped as 12 bytes, which would
|
|
# misalign everything after it.
|
|
code = V_NOP_E64 + S_NOP + S_ENDPGM
|
|
container = dxbc([(b"ACCC", fspx([(6, code)]))])
|
|
self.assertEqual(ec.extract(container), (code, 0))
|
|
|
|
def test_plain_v_nop_after_xbox_data_is_kept(self):
|
|
code = XBOX_DATA * 2 + V_NOP_E64 + S_ENDPGM
|
|
container = dxbc([(b"ACCC", fspx([(6, code)]))])
|
|
self.assertEqual(ec.extract(container), (V_NOP_E64 + S_ENDPGM, 2))
|
|
|
|
def test_no_xbox_data_nops(self):
|
|
container = dxbc([(b"ACCC", fspx([(6, S_NOP + S_ENDPGM)]))])
|
|
self.assertEqual(ec.extract(container), (S_NOP + S_ENDPGM, 0))
|
|
|
|
def test_missing_accc(self):
|
|
with self.assertRaisesRegex(ec.LayoutError, "no ACCC chunk"):
|
|
ec.extract(dxbc([(b"DXIL", b"x" * 4)]))
|
|
|
|
def test_not_fspx(self):
|
|
with self.assertRaisesRegex(ec.LayoutError, "does not start with FSPX"):
|
|
ec.extract(dxbc([(b"ACCC", b"ABCD" + b"\0" * 12)]))
|
|
|
|
def test_total_size_mismatch(self):
|
|
with self.assertRaisesRegex(ec.LayoutError, "total size"):
|
|
ec.extract(dxbc([(b"ACCC", fspx([(6, S_ENDPGM)], total=999))]))
|
|
|
|
def test_section_inside_table_is_rejected(self):
|
|
payload = bytearray(fspx([(6, S_ENDPGM)]))
|
|
struct.pack_into("<I", payload, 16 + 4, 4) # section offset points into the header
|
|
with self.assertRaisesRegex(ec.LayoutError, "out of bounds"):
|
|
ec.extract(dxbc([(b"ACCC", bytes(payload))]))
|
|
|
|
def test_nonzero_reserved_word_is_rejected(self):
|
|
payload = bytearray(fspx([(6, S_ENDPGM)]))
|
|
struct.pack_into("<I", payload, 16 + 12, 1)
|
|
with self.assertRaisesRegex(ec.LayoutError, "reserved word"):
|
|
ec.extract(dxbc([(b"ACCC", bytes(payload))]))
|
|
|
|
def test_needs_exactly_one_code_section(self):
|
|
with self.assertRaisesRegex(ec.LayoutError, "found 0"):
|
|
ec.extract(dxbc([(b"ACCC", fspx([(0, S_ENDPGM)]))]))
|
|
with self.assertRaisesRegex(ec.LayoutError, "found 2"):
|
|
ec.extract(dxbc([(b"ACCC", fspx([(6, S_ENDPGM), (6, S_ENDPGM)]))]))
|
|
|
|
def test_code_without_endpgm(self):
|
|
with self.assertRaisesRegex(ec.LayoutError, "no s_endpgm"):
|
|
ec.extract(dxbc([(b"ACCC", fspx([(6, S_NOP * 4)]))]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|