Skip to content

buffer_bounds_probe: validate and write --json through the shared helpers - #5406

Open
liang-kai-shui wants to merge 8 commits into
ItsNotRexed:mainfrom
liang-kai-shui:build/buffer-bounds-json-path
Open

liang-kai-shui wants to merge 8 commits into
ItsNotRexed:mainfrom
liang-kai-shui:build/buffer-bounds-json-path

Conversation

@liang-kai-shui

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: claim in #5256

What and why

tools/buffer_bounds_probe.py --json wrote its report with a bare open(), and the path was only looked at after the hardware oracle had been opened. So an empty path, a NUL byte, a directory, or a file used as a parent component either cost a GPU session first or raised a traceback, and a missing parent directory was not created.

It now uses the same two helpers as tools/f16_trans_sweep.py (validate_json_path and write_json_report, copied in the shape the other probes use):

  • commit 1: --json is validated before open_oracle(). A bad path prints exactly one ERROR: <message> line and returns 2. The report goes through write_json_report, which creates missing parents and writes LF JSON; an OSError there is one ERROR: line and exit 2, not a traceback. --self-test, --waves, the kernels and the stdout output are unchanged.
  • commit 2: CPU-only tests. A directory is refused before hardware (the subprocess uses a deliberately broken LEONIDA_AMD_CLANG, so reaching the oracle would have exited 77), open_oracle is asserted not called for every bad path, missing parents are created and the JSON round-trips, a file parent gives a clean error, and a write failure is exactly one ERROR: line.
  • commit 3: the module docstring/usage line documents the path rules.

Overlaps #4785 on the write-failure path; this also validates before hardware is opened. First to land wins, as usual.

Tests

CPU only, Windows 11, Python 3.13.15:

  • python -I tests/test_buffer_bounds_probe.py: 8 tests, OK.
  • python -m compileall -q .: clean.
  • git diff --check: clean.
  • python -I tools/buffer_bounds_probe.py --self-test: self-test OK.
  • Manual: --json <directory> and --json <file>/out.json with a broken clang print one ERROR: line and exit 2 (77 if the oracle had been reached).

No game data: every kernel, buffer and pattern is built in the tool, and the new tests use synthetic probe results and temporary directories only.

AI-assisted (DeepSeek coding agent); I reviewed the diff and ran the commands above.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

An existing read-only report still costs a complete probe before the tool detects the output error. I tested head deff92f6 on Windows 11 with CPython 3.14.7. I used the test file's synthetic oracle and counted calls. Both runs used the same file and path.

Windows file attribute Oracle opens Probe calls Exit
Read-only 1 1 2 (permission denied)
Writable 1 1 0 (report written)

The read-only attribute was set before the call and stayed set throughout. The old report survived. To avoid wasting the hardware measurement, check that attribute for an existing output file before opening the oracle. Keep the write-time check for failures that occur later.

To repeat, run this script from the repository root with py -3.14 -I:

import contextlib,io,stat,sys,tempfile
from pathlib import Path
from unittest.mock import patch
sys.path.insert(0, "tests")
import test_buffer_bounds_probe as t
with tempfile.TemporaryDirectory() as d:
    target=Path(d)/"report.json"
    target.write_text("previous report")
    try:
        for mode in (stat.S_IREAD,stat.S_IREAD|stat.S_IWRITE):
            target.chmod(mode)
            err=io.StringIO()
            with patch.object(t.hw_runner,"open_oracle",return_value=t.fake_oracle()) as opened, patch.object(t.bbp,"probe",return_value=t.FAKE_REPORT) as measured, contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err):
                rc=t.bbp.main(["--json",str(target)])
            print(rc,opened.call_count,measured.call_count,err.getvalue().strip(),target.read_text()[:15])
    finally:
        target.chmod(stat.S_IREAD|stat.S_IWRITE)

All eight existing tests and the tool's self-test pass. This is a remaining early-validation case. AI-assisted.

@liang-kai-shui

Copy link
Copy Markdown
Contributor Author

Fixed in 33de5f9a (tool) + ab22cd13 (test): validate_json_path now also refuses an existing file that is not writable (os.access(path, os.W_OK)), and that check runs before open_oracle. The new test sets an existing report read-only, asserts exit 2, open_oracle/probe are not called, the message says "not writable", and the old report text is unchanged. The write-time check stays for failures that appear later.

python -m pytest tests/test_buffer_bounds_probe.py -q -> 9 passed, 7 subtests passed.

AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted Windows CPU review at ab22cd1. All nine focused tests passed. A read-only existing report survived and the oracle was never opened. A valid nested destination produced the expected JSON. Two limits reproduced with a recording harness through main: bad?.json reached both open_oracle and probe before the write failed, and injecting OSError after the writer emitted its first byte replaced an existing report with just {. Both returned 2. The existing open-failure test cannot catch data loss after truncation. A temporary sibling followed by replacement after successful close would preserve an existing report on a late failure.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head deff92f60b48804e7501ca5e6860f6eff880935c (base main 3faac557b).

  • python -m pytest -q tests/test_buffer_bounds_probe.py -> 8 passed, 7 subtests.
  • python -I tools/buffer_bounds_probe.py --self-test -> self-test OK.
  • Negative control: git checkout main -- tools/buffer_bounds_probe.py, same pytest command -> 11 failed, 4 passed.
  • git diff --check main clean

validate_json_path runs before from hw_oracle import ...; runner.open_oracle(), and the tests assert open_oracle was not called for every bad path. The broken-LEONIDA_AMD_CLANG subprocess expects 2, where reaching the oracle would exit 77, so ordering is proven, not assumed. write_json_report creates parents and returns one error line.

Overlap: the PR itself notes #4785 for the write-failure path (outside this range); this one also validates before hardware.

Verdict: pass.

@cynbun

cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed 591879b (code read + CPU tests on Linux, Python 3.14): pass, with one small mismatch.

  • python -I tests/test_buffer_bounds_probe.py passes, and pytest tests/test_buffer_bounds_probe.py gives 9 passed with 7 subtests. With main's tools/buffer_bounds_probe.py, 12 fail. The read-only check (tools/buffer_bounds_probe.py:205) runs before open_oracle, which covers shdann's case.
  • Mismatch: the write_json_report docstring (:221) promises "LF-terminated JSON", but :230 writes only json.dump(report, f, indent=2) with no trailing "\n". write_json_report(path, {"a": [1]}) leaves a file ending in b' ]\n}'. Main behaved the same, so this is not a regression. It is the same issue shdann found in cvt_f16_omod_probe: validate and write --json through the shared helpers #5414, which you fixed there with f.write("\n") and a test asserting raw.endswith(b"\n"). The test here (tests/test_buffer_bounds_probe.py:110) only checks for no CRLF. The same fix and assertion would apply.

AI-assisted (Claude Code).

@liang-kai-shui

Copy link
Copy Markdown
Contributor Author

Fixed in f1356919 (writer) + c8aa33aa (test): write_json_report writes the trailing "\n" again, and the new test asserts the raw bytes end with b"\n" while still parsing. pytest tests/test_buffer_bounds_probe.py -q -> 10 passed, 7 subtests passed. Same fix as #5414, as you noted.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested c8aa33a merged with main 561b65a on Linux (WSL2) / Python 3.12.16 and Windows 11 / Python 3.13.1: pass

tests/test_buffer_bounds_probe.py: 10 run, 10 passed, 0 failed on both (Linux and a native Windows worktree of the merged tree). Negative control: with tools/buffer_bounds_probe.py restored from upstream/main the new tests fail (10 run, 1 failure and 12 errors, since main has no main(argv), validate_json_path or write_json_report); fix restored afterwards. --self-test still prints "self-test OK". Two notes, neither a failure: (1) --json now creates missing parent folders, whereas the other probe tools I have seen in this round (#5821 d16_image_probe, #5550 sandbox_scene) refuse a missing folder, so the PR sets a different convention; (2) the module docstring gets a stray line "Usage: python -I tools/buffer_bounds_probe.py --help lists the options." inside the "Exit codes" block, after the 77 entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants