mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
buffer_swizzle_probe: upfront atomic JSON validation, CLI guards, and translator wave parity
This commit is contained in:
@ -137,6 +137,108 @@ class BufferSwizzleProbeTests(unittest.TestCase):
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertIn("WAVE_DIFF", out.getvalue())
|
||||
|
||||
def test_json_invalid_paths_fail_upfront_before_oracle(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
blocker = Path(tmp) / "blocker"
|
||||
blocker.write_bytes(b"keep")
|
||||
bad_paths = [
|
||||
"",
|
||||
" ",
|
||||
tmp,
|
||||
tmp + os.sep,
|
||||
os.path.join(tmp, "nested") + os.sep,
|
||||
str(blocker / "report.json"),
|
||||
]
|
||||
for bad in bad_paths:
|
||||
with self.subTest(json_path=bad):
|
||||
err = io.StringIO()
|
||||
with unittest.mock.patch.object(probe, "run_translator") as tr_mock, \
|
||||
unittest.mock.patch.object(probe, "run_hw") as hw_mock, \
|
||||
redirect_stderr(err):
|
||||
rc = probe.main(["--oracle", "both", "--group", "offset_wrap", "--json", bad])
|
||||
self.assertEqual(rc, 2)
|
||||
tr_mock.assert_not_called()
|
||||
hw_mock.assert_not_called()
|
||||
self.assertTrue(err.getvalue().strip())
|
||||
self.assertEqual(blocker.read_bytes(), b"keep")
|
||||
|
||||
def test_write_json_report_atomic_symlink_nan_and_devnull(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
nested = os.path.join(tmp, "a", "b", "out.json")
|
||||
self.assertIsNone(probe.write_json_report(nested, {"ok": True}))
|
||||
raw = Path(nested).read_bytes()
|
||||
self.assertTrue(raw.endswith(b"\n"))
|
||||
self.assertNotIn(b"\r\n", raw)
|
||||
self.assertEqual(json.loads(raw.decode("utf-8")), {"ok": True})
|
||||
|
||||
# NaN must fail serialization without overwriting existing file.
|
||||
err = probe.write_json_report(nested, {"bad": float("nan")})
|
||||
self.assertIsNotNone(err)
|
||||
self.assertEqual(json.loads(Path(nested).read_text(encoding="utf-8")), {"ok": True})
|
||||
|
||||
# Symlink target is updated in place without replacing the symlink itself.
|
||||
if hasattr(os, "symlink"):
|
||||
real_target = os.path.join(tmp, "real.json")
|
||||
link_path = os.path.join(tmp, "link.json")
|
||||
Path(real_target).write_text("old", encoding="utf-8")
|
||||
try:
|
||||
os.symlink(real_target, link_path)
|
||||
except OSError:
|
||||
link_path = None
|
||||
if link_path is not None:
|
||||
self.assertIsNone(probe.write_json_report(link_path, {"via": "link"}))
|
||||
self.assertTrue(os.path.islink(link_path))
|
||||
self.assertEqual(json.loads(Path(real_target).read_text(encoding="utf-8")), {"via": "link"})
|
||||
|
||||
if os.name == "posix" and os.path.exists("/dev/null"):
|
||||
self.assertIsNone(probe.write_json_report("/dev/null", {"discarded": 1}))
|
||||
|
||||
def test_empty_group_and_hw_arch_rejected_upfront(self):
|
||||
for argv in (["--group", ""], ["--group", " "], ["--hw-arch", ""], ["--hw-arch", " "]):
|
||||
with self.subTest(argv=argv):
|
||||
err = io.StringIO()
|
||||
with unittest.mock.patch.object(probe, "run_translator") as tr_mock, \
|
||||
redirect_stderr(err):
|
||||
rc = probe.main(argv)
|
||||
self.assertEqual(rc, 2)
|
||||
tr_mock.assert_not_called()
|
||||
self.assertTrue(err.getvalue().strip())
|
||||
|
||||
def test_translator_only_wave_parity_and_error_precedence(self):
|
||||
# Wave-invariant translator wave32 != wave64 must fail with WAVE_DIFF.
|
||||
bad_tr_diff = {
|
||||
32: {"voff_inst_wrap_and_soff_nowrap": [1, 2, 3, 4]},
|
||||
64: {"voff_inst_wrap_and_soff_nowrap": [1, 2, 3, 5]},
|
||||
}
|
||||
out = io.StringIO()
|
||||
with unittest.mock.patch.object(probe, "run_translator", return_value=bad_tr_diff), redirect_stdout(out):
|
||||
rc = probe.main(["--oracle", "translator", "--group", "offset_wrap"])
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertIn("WAVE_DIFF", out.getvalue())
|
||||
|
||||
# Wave-dependent translator wave32 == wave64 must fail with WAVE_SAME.
|
||||
bad_tr_same = {
|
||||
32: {"tid_wave32_vs_wave64": [0] * 64},
|
||||
64: {"tid_wave32_vs_wave64": [0] * 64},
|
||||
}
|
||||
out2 = io.StringIO()
|
||||
with unittest.mock.patch.object(probe, "run_translator", return_value=bad_tr_same), redirect_stdout(out2):
|
||||
rc2 = probe.main(["--oracle", "translator", "--group", "add_tid_wave_lane"])
|
||||
self.assertEqual(rc2, 1)
|
||||
self.assertIn("WAVE_SAME", out2.getvalue())
|
||||
|
||||
# A string harness error in one wave must report ERROR, not WAVE_DIFF.
|
||||
hw_err = ("device=mock", {
|
||||
32: {"voff_inst_wrap_and_soff_nowrap": "compile failed"},
|
||||
64: {"voff_inst_wrap_and_soff_nowrap": [1, 2, 3, 4]},
|
||||
})
|
||||
out3 = io.StringIO()
|
||||
with unittest.mock.patch.object(probe, "run_hw", return_value=hw_err), redirect_stdout(out3):
|
||||
rc3 = probe.main(["--oracle", "hw", "--group", "offset_wrap"])
|
||||
self.assertEqual(rc3, 1)
|
||||
self.assertIn("ERROR", out3.getvalue())
|
||||
self.assertNotIn("WAVE_DIFF", out3.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -30,6 +30,7 @@ Exit codes:
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
@ -42,7 +43,9 @@ for p in (HERE, os.path.join(ROOT, "tests", "semantic")):
|
||||
if p not in sys.path:
|
||||
sys.path.insert(0, p)
|
||||
|
||||
from file_mode import published_mode # noqa: E402
|
||||
import rdna2spirv # noqa: E402
|
||||
import replace_retry # noqa: E402
|
||||
import spirv_mini_interp # noqa: E402
|
||||
|
||||
# Buffer layout in the allocated buffer (s[0:3] RAW V# at offset 0):
|
||||
@ -539,6 +542,92 @@ def self_test():
|
||||
return 0
|
||||
|
||||
|
||||
def _has_trailing_sep(path: str) -> bool:
|
||||
seps = tuple(s for s in (os.sep, os.altsep) if s)
|
||||
return path.endswith(seps)
|
||||
|
||||
|
||||
def _resolve_output_target(path: str) -> tuple[str, str]:
|
||||
out_path = os.path.abspath(path)
|
||||
target = os.path.realpath(out_path) if os.path.islink(out_path) else out_path
|
||||
return out_path, target
|
||||
|
||||
|
||||
def validate_json_path(path: str) -> str | None:
|
||||
"""Validate --json destination before running translator or GPU work. Returns an error message or None."""
|
||||
if not isinstance(path, str) or not path.strip():
|
||||
return "--json path must not be empty"
|
||||
out_path, target = _resolve_output_target(path)
|
||||
if _has_trailing_sep(path) or os.path.isdir(out_path) or os.path.isdir(target):
|
||||
return f"--json path is a directory: {out_path}"
|
||||
parent = os.path.dirname(target)
|
||||
probe = parent
|
||||
while probe and not os.path.exists(probe):
|
||||
nxt = os.path.dirname(probe)
|
||||
if nxt == probe:
|
||||
break
|
||||
probe = nxt
|
||||
if probe and os.path.exists(probe) and not os.path.isdir(probe):
|
||||
return f"cannot write {out_path}: parent path is not a directory: {probe}"
|
||||
if os.path.exists(target):
|
||||
try:
|
||||
fd = os.open(target, os.O_WRONLY)
|
||||
os.close(fd)
|
||||
except OSError as exc:
|
||||
return f"cannot write {out_path}: {exc.strerror or exc}"
|
||||
return None
|
||||
|
||||
|
||||
def write_json_report(path: str, payload: dict) -> str | None:
|
||||
"""Write JSON report with LF endings atomically (direct write for non-regular targets like /dev/null)."""
|
||||
err = validate_json_path(path)
|
||||
if err:
|
||||
return err
|
||||
out_path, target = _resolve_output_target(path)
|
||||
try:
|
||||
text = json.dumps(payload, indent=2, allow_nan=False) + "\n"
|
||||
except (TypeError, ValueError) as exc:
|
||||
return f"cannot serialize JSON report for {out_path}: {exc}"
|
||||
try:
|
||||
st = os.stat(target) if os.path.exists(target) else None
|
||||
except OSError as exc:
|
||||
return f"cannot write {out_path}: {exc.strerror or exc}"
|
||||
if st is not None and not stat.S_ISREG(st.st_mode):
|
||||
try:
|
||||
with open(target, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write(text)
|
||||
return None
|
||||
except OSError as exc:
|
||||
return f"cannot write {out_path}: {exc.strerror or exc}"
|
||||
parent = os.path.dirname(target)
|
||||
tmp_path = None
|
||||
try:
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
fd, tmp_path = tempfile.mkstemp(prefix=".buffer_swizzle_probe.", suffix=".tmp", dir=parent)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as f:
|
||||
f.write(text)
|
||||
f.flush()
|
||||
except Exception:
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
os.chmod(tmp_path, published_mode(target))
|
||||
replace_retry.replace(tmp_path, target)
|
||||
tmp_path = None
|
||||
return None
|
||||
except OSError as exc:
|
||||
return f"cannot write {out_path}: {exc.strerror or exc}"
|
||||
finally:
|
||||
if tmp_path is not None:
|
||||
try:
|
||||
os.unlink(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
ap.add_argument("--oracle", choices=("translator", "hw", "both"), default="translator")
|
||||
@ -565,14 +654,28 @@ def main(argv=None):
|
||||
print(f"{g:<22} ({n} cases)")
|
||||
return 0
|
||||
|
||||
if args.hw_arch is not None and not args.hw_arch.strip():
|
||||
print("ERROR: --hw-arch must not be empty", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
if args.group:
|
||||
unknown = [g for g in args.group if g not in groups]
|
||||
if any(not g.strip() for g in args.group):
|
||||
print("ERROR: --group must not be empty", file=sys.stderr)
|
||||
return 2
|
||||
cleaned = [g.strip() for g in args.group]
|
||||
unknown = list(dict.fromkeys(g for g in cleaned if g not in groups))
|
||||
if unknown:
|
||||
print(f"unknown group(s): {', '.join(unknown)}", file=sys.stderr)
|
||||
return 2
|
||||
want = set(args.group)
|
||||
want = set(cleaned)
|
||||
cases = [c for c in cases if c.group in want]
|
||||
|
||||
if args.json_out is not None:
|
||||
err = validate_json_path(args.json_out)
|
||||
if err:
|
||||
print(f"ERROR: {err}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
waves = (32, 64) if args.wave == "both" else (int(args.wave),)
|
||||
tr_res = run_translator(cases, waves=waves) if args.oracle in ("translator", "both") else None
|
||||
hw_device, hw_res = None, None
|
||||
@ -596,20 +699,26 @@ def main(argv=None):
|
||||
h64 = hw_res[64][c.name] if hw_res and 64 in hw_res else None
|
||||
status = "OK"
|
||||
tag = None
|
||||
if not c.wave_dependent and h32 is not None and h64 is not None and h32 != h64:
|
||||
if any(v is not None and not isinstance(v, list) for v in (t32, t64, h32, h64)):
|
||||
status = "ERROR"
|
||||
mismatches += 1
|
||||
elif not c.wave_dependent and h32 is not None and h64 is not None and h32 != h64:
|
||||
status = "WAVE_DIFF"
|
||||
wave_diffs += 1
|
||||
elif c.wave_dependent and h32 is not None and h64 is not None and h32 == h64:
|
||||
status = "WAVE_SAME"
|
||||
mismatches += 1
|
||||
elif any(v is not None and not isinstance(v, list) for v in (t32, t64, h32, h64)):
|
||||
status = "ERROR"
|
||||
elif h32 is None and h64 is None and not c.wave_dependent and t32 is not None and t64 is not None and t32 != t64:
|
||||
status = "WAVE_DIFF"
|
||||
wave_diffs += 1
|
||||
elif h32 is None and h64 is None and c.wave_dependent and t32 is not None and t64 is not None and t32 == t64:
|
||||
status = "WAVE_SAME"
|
||||
mismatches += 1
|
||||
else:
|
||||
bad = ((t32 is not None and h32 is not None and t32 != h32)
|
||||
or (t64 is not None and h64 is not None and t64 != h64))
|
||||
if bad:
|
||||
hw_ref = h32 if h32 is not None else h64
|
||||
hw_ref = h32 if (t32 is not None and h32 is not None and t32 != h32) else h64
|
||||
known = KNOWN_DIVERGENCES.get(c.name)
|
||||
if known and hw_ref == known[1]:
|
||||
status = "KNOWN_DIFF"
|
||||
@ -640,19 +749,10 @@ def main(argv=None):
|
||||
print(f"summary: {total} cases: {matched} match, {known_diffs} known divergence, "
|
||||
f"{mismatches} unexpected mismatch, {wave_diffs} wave32/64 diff")
|
||||
|
||||
if args.json_out:
|
||||
out_path = os.path.abspath(args.json_out)
|
||||
if os.path.isdir(out_path):
|
||||
print(f"ERROR: --json path is a directory: {out_path}", file=sys.stderr)
|
||||
return 2
|
||||
parent = os.path.dirname(out_path)
|
||||
try:
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
with open(out_path, "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump({"device": hw_device, "waves": list(waves), "cases": records}, f, indent=2)
|
||||
f.write("\n")
|
||||
except OSError as exc:
|
||||
print(f"ERROR: cannot write {out_path}: {exc.strerror or exc}", file=sys.stderr)
|
||||
if args.json_out is not None:
|
||||
err = write_json_report(args.json_out, {"device": hw_device, "waves": list(waves), "cases": records})
|
||||
if err:
|
||||
print(f"ERROR: {err}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
if mismatches or wave_diffs or (args.strict and known_diffs):
|
||||
|
||||
Reference in New Issue
Block a user