d3d12_xs_surface: guard GUID negative bounds, SCARAB BOM/CRLF, and bad paths

This commit is contained in:
kirigayakazima
2026-11-26 19:39:01 +08:00
parent a73a57e133
commit 15d9ec9715
2 changed files with 148 additions and 45 deletions

View File

@ -127,6 +127,83 @@ def test_scarab_cross_check():
assert errors == []
def test_check_guid_values_rejects_negative_components():
first_name = next(iter(S.STANDARD_GUIDS))
bad = (-1, 0, 0x4000, 0x80, 0, 0, 0, 0, 0, 0, 0)
orig = S.STANDARD_GUIDS[first_name]
try:
S.STANDARD_GUIDS[first_name] = bad
errors = S.check_guid_values()
assert any(first_name in e for e in errors), errors
S.STANDARD_GUIDS[first_name] = (0, 0, 0x4000, 0x80, 0, -5, 0, 0, 0, 0, 0)
errors = S.check_guid_values()
assert any(first_name in e for e in errors), errors
finally:
S.STANDARD_GUIDS[first_name] = orig
def test_scarab_parsers_handle_bom_and_crlf():
import tempfile
with tempfile.TemporaryDirectory() as tmp:
cat = os.path.join(tmp, "catalogue.tsv")
with open(cat, "wb") as f:
f.write(
"\ufeffkind\tname\tsource\r\nmethod\tCreateCommandQueueX\tbridge\r\nenum\tD3D12_FOO\tbridge\r\n".encode(
"utf-8"
)
)
rows = S.parse_scarab_catalogue(cat)
assert rows == [("method", "CreateCommandQueueX", "bridge"), ("enum", "D3D12_FOO", "bridge")], rows
tbl = os.path.join(tmp, "iid_table.cpp")
with open(tbl, "wb") as f:
f.write(
"\ufeffstatic const GUID IID_ID3D12Foo = {0x1,0x2,0x3,{0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb}};\r\n".encode(
"utf-8"
)
)
parsed_tbl = S.parse_scarab_iid_table(tbl)
assert parsed_tbl == {"IID_ID3D12Foo": (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)}, parsed_tbl
inc = os.path.join(tmp, "iid_console.inc")
with open(inc, "wb") as f:
f.write("\ufeff// GUID IID_ID3D12Foo ordinal 28\r\n".encode("utf-8"))
con = S.parse_scarab_iid_console(inc)
assert con == {"IID_ID3D12Foo": 28}, con
def test_check_scarab_and_cli_guard_bad_paths_and_decoding():
import io
import tempfile
from contextlib import redirect_stdout
with tempfile.TemporaryDirectory() as tmp:
# Empty --scarab "" must not silently pass.
out = io.StringIO()
with redirect_stdout(out):
rc = S.main(["--scarab", ""])
assert rc == 1, rc
assert "FAIL:" in out.getvalue(), out.getvalue()
# Directory where a file is expected + invalid UTF-8 bytes must return clean errors.
bridge = os.path.join(tmp, "components", "runtime-bridge", "src")
ext_src = os.path.join(tmp, "components", "extension-wrapper", "src")
ext_doc = os.path.join(tmp, "components", "extension-wrapper", "docs")
os.makedirs(bridge)
os.makedirs(ext_src)
os.makedirs(ext_doc)
os.makedirs(os.path.join(bridge, "iid_table.cpp"))
with open(os.path.join(ext_src, "iid_console.inc"), "wb") as f:
f.write(b"\xff\xfe\x00\x00invalid-utf8\n")
with open(os.path.join(ext_doc, "console-extension-catalogue.tsv"), "wb") as f:
f.write(b"method\tFirstMethod\r\n")
errors, _notes = S.check_scarab(tmp)
assert any("iid_table.cpp" in e for e in errors), errors
assert any("iid_console.inc" in e for e in errors), errors
if __name__ == "__main__":
test_slots_are_116()
test_data_classes()
@ -136,6 +213,9 @@ if __name__ == "__main__":
test_sdk_include_override_accepts_a_trailing_separator()
test_sdk_discovery_sorts_versions_numerically()
test_sdk_discovery_skips_a_newer_driver_kit_folder()
test_check_guid_values_rejects_negative_components()
test_scarab_parsers_handle_bom_and_crlf()
test_check_scarab_and_cli_guard_bad_paths_and_decoding()
# Call the two optional checks through their real definitions so this runner cannot drift from them; both
# raise unittest.SkipTest when their inputs (SDK headers, SCARAB_REF) are absent.
for optional in (test_standard_iids_match_sdk, test_scarab_cross_check):

View File

@ -369,7 +369,15 @@ def check_guid_values():
if len(g) != 11:
errors.append("%s: expected 11 components, got %d" % (name, len(g)))
continue
if g[0] > 0xFFFFFFFF or g[1] > 0xFFFF or g[2] > 0xFFFF or any(x > 0xFF for x in g[3:]):
if (
g[0] < 0
or g[0] > 0xFFFFFFFF
or g[1] < 0
or g[1] > 0xFFFF
or g[2] < 0
or g[2] > 0xFFFF
or any(x < 0 or x > 0xFF for x in g[3:])
):
errors.append("%s: component out of range" % name)
if guid_version(g) != 4:
errors.append("%s: not an RFC 4122 version 4 UUID" % name)
@ -483,7 +491,7 @@ def check_sdk():
def parse_scarab_iid_table(path):
with open(path, encoding="utf-8") as f:
with open(path, encoding="utf-8-sig") as f:
text = f.read()
out = {}
for name, body in re.findall(r"GUID\s+((?:IID|DXGI_DEBUG)_[A-Za-z0-9_]+)\s*=\s*\{([^;]*)\};", text):
@ -494,7 +502,7 @@ def parse_scarab_iid_table(path):
def parse_scarab_iid_console(path):
with open(path, encoding="utf-8") as f:
with open(path, encoding="utf-8-sig") as f:
text = f.read()
out = {}
for m in re.finditer(r"GUID\s+(IID_[A-Za-z0-9_]+).*?ordinal\s+(\d+)", text):
@ -504,10 +512,10 @@ def parse_scarab_iid_console(path):
def parse_scarab_catalogue(path):
rows = []
with open(path, encoding="utf-8") as fh:
with open(path, encoding="utf-8-sig") as fh:
header = fh.readline()
for line in fh:
parts = line.rstrip("\n").split("\t")
parts = line.rstrip("\r\n").split("\t")
if len(parts) >= 3:
rows.append(tuple(parts[:3]))
return rows
@ -517,51 +525,66 @@ def check_scarab(root):
"""Cross-check the table against a SCARAB checkout. Returns (errors, notes)."""
errors = []
notes = []
if not root or not os.path.isdir(root):
errors.append("SCARAB checkout is not a directory: %r" % (root,))
return errors, notes
iid_table = os.path.join(root, "components", "runtime-bridge", "src", "iid_table.cpp")
iid_console = os.path.join(root, "components", "extension-wrapper", "src", "iid_console.inc")
catalogue = os.path.join(root, "components", "extension-wrapper", "docs",
"console-extension-catalogue.tsv")
if os.path.exists(iid_table):
theirs = parse_scarab_iid_table(iid_table)
mine = dict(STANDARD_GUIDS)
mine.update(CONSOLE_IIDS)
if set(theirs) != set(mine):
errors.append("SCARAB iid_table names differ from this table")
for name in sorted(set(theirs) & set(mine)):
if theirs[name] != mine[name]:
errors.append("%s: SCARAB %s != table %s"
% (name, guid_text(theirs[name]), guid_text(mine[name])))
notes.append("iid_table.cpp: %d GUID values compared" % len(theirs))
if os.path.isfile(iid_table):
try:
theirs = parse_scarab_iid_table(iid_table)
except (OSError, UnicodeDecodeError, ValueError) as exc:
errors.append("unreadable %s: %s" % (iid_table, exc))
else:
mine = dict(STANDARD_GUIDS)
mine.update(CONSOLE_IIDS)
if set(theirs) != set(mine):
errors.append("SCARAB iid_table names differ from this table")
for name in sorted(set(theirs) & set(mine)):
if theirs[name] != mine[name]:
errors.append("%s: SCARAB %s != table %s"
% (name, guid_text(theirs[name]), guid_text(mine[name])))
notes.append("iid_table.cpp: %d GUID values compared" % len(theirs))
else:
errors.append("missing %s" % iid_table)
if os.path.exists(iid_console):
theirs = parse_scarab_iid_console(iid_console)
known = {name for _n, name, _kind, _ev in DATA_EXPORTS}
unexpected = set(theirs) - known
if unexpected:
errors.append("iid_console.inc has names not in the table: %s" % sorted(unexpected))
mine = {name: n for n, name, _kind, _ev in DATA_EXPORTS
if name in theirs}
for name in sorted(set(theirs) & set(mine)):
if theirs[name] != mine[name]:
errors.append("%s: SCARAB ordinal %d != table %d"
% (name, theirs[name], mine[name]))
notes.append("iid_console.inc: %d documented ordinals compared" % len(set(theirs) & set(mine)))
errors.append("missing %s" % iid_table if not os.path.exists(iid_table) else "not a file: %s" % iid_table)
if os.path.isfile(iid_console):
try:
theirs = parse_scarab_iid_console(iid_console)
except (OSError, UnicodeDecodeError, ValueError) as exc:
errors.append("unreadable %s: %s" % (iid_console, exc))
else:
known = {name for _n, name, _kind, _ev in DATA_EXPORTS}
unexpected = set(theirs) - known
if unexpected:
errors.append("iid_console.inc has names not in the table: %s" % sorted(unexpected))
mine = {name: n for n, name, _kind, _ev in DATA_EXPORTS
if name in theirs}
for name in sorted(set(theirs) & set(mine)):
if theirs[name] != mine[name]:
errors.append("%s: SCARAB ordinal %d != table %d"
% (name, theirs[name], mine[name]))
notes.append("iid_console.inc: %d documented ordinals compared" % len(set(theirs) & set(mine)))
else:
errors.append("missing %s" % iid_console)
if os.path.exists(catalogue):
rows = parse_scarab_catalogue(catalogue)
methods = [r for r in rows if r[0] == "method"]
enums = [r for r in rows if r[0] == "enum"]
notes.append("catalogue: %d method rows, %d enum rows" % (len(methods), len(enums)))
if (len(methods), len(enums)) != (105, 106):
notes.append("catalogue counts changed since the pin (expected 105/106)")
present = {r[1] for r in methods}
missing = sorted(CATALOGUE_NON_METHODS - present)
if missing:
errors.append("catalogue non-method rows missing: %s" % missing)
errors.append("missing %s" % iid_console if not os.path.exists(iid_console) else "not a file: %s" % iid_console)
if os.path.isfile(catalogue):
try:
rows = parse_scarab_catalogue(catalogue)
except (OSError, UnicodeDecodeError, ValueError) as exc:
errors.append("unreadable %s: %s" % (catalogue, exc))
else:
methods = [r for r in rows if r[0] == "method"]
enums = [r for r in rows if r[0] == "enum"]
notes.append("catalogue: %d method rows, %d enum rows" % (len(methods), len(enums)))
if (len(methods), len(enums)) != (105, 106):
notes.append("catalogue counts changed since the pin (expected 105/106)")
present = {r[1] for r in methods}
missing = sorted(CATALOGUE_NON_METHODS - present)
if missing:
errors.append("catalogue non-method rows missing: %s" % missing)
else:
errors.append("missing %s" % catalogue)
errors.append("missing %s" % catalogue if not os.path.exists(catalogue) else "not a file: %s" % catalogue)
return errors, notes
@ -606,7 +629,7 @@ def main(argv=None):
else:
print("sdk: SKIP (Windows SDK headers not found)")
if args.scarab:
if args.scarab is not None:
scarab_errors, notes = check_scarab(args.scarab)
errors += scarab_errors
for note in notes:
@ -616,7 +639,7 @@ def main(argv=None):
for e in errors:
print("FAIL: " + e)
return 1
if args.check or args.sdk or args.scarab:
if args.check or args.sdk or args.scarab is not None:
print("PASS: 116 slots (23 functions + 93 GUID data), %d standard + %d console GUIDs, "
"%d unresolved command-list IIDs absent from local tables"
% (len(STANDARD_GUIDS), len(CONSOLE_IIDS), len(UNRECOVERED_CONSOLE_IIDS)))