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
24 changes: 24 additions & 0 deletions tests/test_d3d12_guid_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ def test_queue_desc_window(self):
self.assertEqual(len(r["queue_descs"]), 2)
self.assertEqual(r["guid_candidates"][0]["value"], str(UNKNOWN))

def test_queue_desc_in_the_last_16_bytes_of_a_section(self):
# The window that ends exactly at the end of .rdata is a whole window and is scanned too.
rdata = b"\0" * 16 + struct.pack("<IIII", 6, 0, 0, 0)
image, _ = pe32plus(lea(0x2010, 0x1000), rdata)
r = sites.scan(self.write("t7.dll", image), KNOWN)
self.assertEqual([(d["rva"], d["type"]) for d in r["queue_descs"]], [(0x2010, 6)])

def test_absolute_pointer_in_the_last_8_bytes_of_a_section(self):
# A GUID at +0 and, as the last 8 bytes of .rdata, an absolute pointer to it (ImageBase + 0x2000).
rdata = UNKNOWN.bytes_le + struct.pack("<Q", 0x180000000 + 0x2000)
image, _ = pe32plus(b"\x90" * 7, rdata)
r = sites.scan(self.write("t8.dll", image), KNOWN)
self.assertEqual([(c["value"], c["refs"], c["rva"]) for c in r["guid_candidates"]],
[(str(UNKNOWN), 1, 0x2000)])

def test_movabs_pair_that_ends_the_code_section(self):
# `mov rax, imm64` + `mov rdx, imm64` holding the two GUID halves, the second ending .text.
guid = UNKNOWN.bytes_le
text = b"\x48\xb8" + guid[:8] + b"\x48\xba" + guid[8:]
image, _ = pe32plus(text, b"\0" * 16)
r = sites.scan(self.write("t9.dll", image), KNOWN)
self.assertEqual([(c["value"], c["section"], c["rva"]) for c in r["guid_candidates"]],
[(str(UNKNOWN), "movabs", 0x1000)])

def test_guid_shaped_rejects_non_guids(self):
self.assertFalse(sites.guid_shaped(b"\0" * 16))
self.assertFalse(sites.guid_shaped(b"\x01" * 16)) # constant fill
Expand Down
6 changes: 3 additions & 3 deletions tools/d3d12_guid_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def movabs_pairs(img: Image) -> list[tuple[int, bytes]]:
continue
blob = img.data[s["raw"]:s["raw"] + s["rsize"]]
imm: list[tuple[int, bytes]] = []
for i in range(len(blob) - 10):
for i in range(len(blob) - 9): # a 10-byte movabs must fit entirely
if blob[i] == 0x48 and 0xB8 <= blob[i + 1] <= 0xBF:
imm.append((s["va"] + i, blob[i + 2:i + 10]))
for (rva1, first), (rva2, second) in zip(imm, imm[1:]):
Expand All @@ -245,7 +245,7 @@ def absolute_refs(img: Image) -> list[tuple[int, int]]:
lo = img.image_base
for s in img.initialized():
blob = img.data[s["raw"]:s["raw"] + s["rsize"]]
for i in range(0, len(blob) - 8, 8):
for i in range(0, len(blob) - 7, 8): # the last whole 8-byte slot too
value = struct.unpack_from("<Q", blob, i)[0]
if lo <= value < lo + 0x10000000:
target = value - lo
Expand Down Expand Up @@ -319,7 +319,7 @@ def constant_at(rva: int) -> tuple[bytes, str] | None:
if s["chars"] & 0x20000000: # code sections are not queue descriptions
continue
blob = img.data[s["raw"]:s["raw"] + s["rsize"]]
for i in range(0, len(blob) - 16, 4):
for i in range(0, len(blob) - 15, 4): # the last whole 16-byte window too
if s["va"] + i in lea_targets and queue_desc_shaped(blob[i:i + 16]):
t, p, flags, node = struct.unpack_from("<IIII", blob, i)
descs.append({"rva": s["va"] + i, "type": t, "priority": p, "flags": flags, "node": node})
Expand Down