tools/int_hw_probe.py: validate realpath JSON targets, --hw-arch, and blank --group

This commit is contained in:
kirigayakazima
2026-11-26 20:53:00 +08:00
parent a73a57e133
commit 6a0a43449e
2 changed files with 57 additions and 12 deletions

View File

@ -207,6 +207,38 @@ class IntHwProbeTests(unittest.TestCase):
self.assertEqual(json.loads(raw)["waves"], [32, 64])
self.assertEqual([n for n in os.listdir(tmp) if n.startswith(".int-hw-probe-")], [])
def test_rejects_hw_arch_with_translator_empty_group_and_realpath_json_targets(self):
with tempfile.TemporaryDirectory() as tmp:
blocker = os.path.join(tmp, "not_a_dir")
existing_dir = os.path.join(tmp, "dir_target")
os.mkdir(existing_dir)
Path(blocker).write_text("x", encoding="utf-8", newline="\n")
with unittest.mock.patch.object(probe, "run_translator", side_effect=AssertionError("ran translator")):
for argv, expected in (
(["--oracle", "translator", "--hw-arch", "gfx1035"], "--hw-arch"),
(["--oracle", "hw", "--hw-arch", ""], "--hw-arch"),
(["--oracle", "translator", "--group", ""], "--group"),
(["--oracle", "translator", "--group", " "], "--group"),
):
with self.subTest(argv=argv):
err = io.StringIO()
with redirect_stderr(err):
rc = probe.main(argv)
self.assertEqual(rc, 2)
self.assertIn("ERROR:", err.getvalue())
self.assertIn(expected, err.getvalue())
for resolved in (existing_dir, os.path.join(blocker, "nested", "out.json")):
with self.subTest(resolved=resolved):
err = io.StringIO()
with unittest.mock.patch.object(probe.os.path, "realpath", return_value=resolved), \
redirect_stderr(err):
rc = probe.main(["--oracle", "translator", "--group", "salu_bfe_past31",
"--json", os.path.join(tmp, "alias.json")])
self.assertEqual(rc, 2)
self.assertIn("ERROR:", err.getvalue())
# v_bfe_u32 / v_bfe_i32 words measured natively on an AMD Radeon 610M (gfx1036, RDNA2), wave32 and wave64 identical:
# `python -I tools/int_hw_probe.py --oracle both --group valu_bfe_past31`. A field that would run past bit 31 stops

View File

@ -882,18 +882,20 @@ def _validate_json_out(raw_path):
if raw_path.endswith(os.sep) or (os.altsep and raw_path.endswith(os.altsep)):
return None, f"ERROR: --json path must name a file, not a directory: {raw_path}"
out_path = os.path.abspath(raw_path)
if os.path.isdir(out_path):
return None, f"ERROR: --json path is a directory: {out_path}"
cur = os.path.dirname(out_path)
while cur:
if os.path.exists(cur):
if not os.path.isdir(cur):
return None, f"ERROR: --json parent path is not a directory: {cur}"
break
parent = os.path.dirname(cur)
if parent == cur:
break
cur = parent
dest = os.path.realpath(out_path)
for candidate in (out_path, dest):
if os.path.isdir(candidate):
return None, f"ERROR: --json path is a directory: {out_path}"
cur = os.path.dirname(candidate)
while cur:
if os.path.exists(cur) or os.path.islink(cur):
if not os.path.isdir(cur):
return None, f"ERROR: --json parent path is not a directory: {cur}"
break
parent = os.path.dirname(cur)
if parent == cur:
break
cur = parent
return out_path, None
@ -951,7 +953,18 @@ def main(argv=None):
print(f"{g:<22} ({n} cases)")
return 0
if args.hw_arch is not None:
if not args.hw_arch.strip():
print("ERROR: --hw-arch must not be empty", file=sys.stderr)
return 2
if args.oracle == "translator":
print("ERROR: --hw-arch is only valid with --oracle hw or --oracle both", file=sys.stderr)
return 2
if args.group:
if any(not g.strip() for g in args.group):
print("ERROR: --group must not be empty", file=sys.stderr)
return 2
unknown = [g for g in args.group if g not in groups]
if unknown:
print(f"unknown group(s): {', '.join(unknown)}", file=sys.stderr)