Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions scripts/re/check_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ def layout(r):
consts, descs, size = lay
lst = subprocess.run([XCL, 'disasm', os.path.join(D, name)], capture_output=True, text=True, encoding='utf-8', errors='replace').stdout
# the pair the table pointer was copied to: s_mov sA, s0 ; s_and sB, s1, 0xff
m = re.search(r's_and_b32 s(\d+), s1, 0xff\s', lst)
if not m or not re.search(rf's_mov_b32 s{int(m.group(1)) - 1}, s0\s', lst):
low_regs = set(map(int, re.findall(r's_mov_b32 s(\d+), s0\s', lst)))
high_regs = map(int, re.findall(r's_and_b32 s(\d+), s1, 0xff\s', lst))
high = next((reg for reg in high_regs if reg - 1 in low_regs), None)
if high is None:
print(name, 'no table pointer pattern')
continue
pair = f's[{int(m.group(1)) - 1}:{m.group(1)}]'
pair = f's[{high - 1}:{high}]'
loads = re.findall(r's_load_dword(x\d+)? s\[?(\d+)(?::(\d+))?\]?, ' + re.escape(pair) + r', (0x[0-9a-f]+)', lst)
for w, lo, hi, off in loads:
off = int(off, 16)
Expand Down
37 changes: 37 additions & 0 deletions scripts/test_check_layout_pair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
import runpy
import struct
import subprocess
import tempfile
import unittest
from unittest.mock import patch


SCRIPT = Path(__file__).resolve().parent / "re" / "check_layout.py"


class LayoutPointerTests(unittest.TestCase):
def test_unrelated_mask_does_not_hide_the_root_argument_pair(self):
root = struct.pack("<11I", 1, 1, 24, 0, 0, 0, 2, 0, 36, 0, 0)
chunk = b"RTS0" + struct.pack("<I", len(root)) + root
blob = struct.pack("<4s16sIIII", b"DXBC", bytes(16), 1, 36 + len(chunk), 1, 36) + chunk
listing = (
"s_and_b32 s13, s1, 0xff\n"
"s_mov_b32 s4, s0\n"
"s_and_b32 s5, s1, 0xff\n"
"s_load_dword s6, s[4:5], 0x0\n"
"s_load_dword s7, s[4:5], 0x8\n"
)
result = subprocess.CompletedProcess(args=[], returncode=0, stdout=listing, stderr="")
with tempfile.TemporaryDirectory() as tmp:
(Path(tmp) / "00.dxbc").write_bytes(blob)
output = StringIO()
with patch("sys.argv", [str(SCRIPT), tmp]), patch("subprocess.run", return_value=result), redirect_stdout(output):
runpy.run_path(str(SCRIPT), run_name="__main__")
self.assertIn("loads on parameter boundaries: 1, off: 1", output.getvalue())


if __name__ == "__main__":
unittest.main()
Loading