census: comment-stripped matching plus a self-test in the shape that cost a gate
Browse files at this commit3972653317a8b46fd71abe8364040b5343d141c0Parent: 7e097fe2 changed files +100 -2
scripts/pin-gate-audit.pymodified+84 -1
| @@ -45,7 +45,8 @@ import re | |||
| 45 | import subprocess | 45 | import subprocess |
| 46 | import sys | 46 | import sys |
| 47 | 47 | ||
| 48 | -ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) | 48 | +ROOT = os.environ.get("PIN_AUDIT_ROOT") or os.path.abspath( |
| 49 | + os.path.join(os.path.dirname(__file__), "..")) | ||
| 49 | 50 | ||
| 50 | 51 | ||
| 51 | def sources(): | 52 | def sources(): |
| @@ -109,6 +110,86 @@ def tracked(path): | |||
| 109 | return "yes" if r.returncode == 0 else "no" | 110 | return "yes" if r.returncode == 0 else "no" |
| 110 | 111 | ||
| 111 | 112 | ||
| 113 | + | ||
| 114 | +SELFTEST_CRATE = "__pin_audit_selftest__" | ||
| 115 | + | ||
| 116 | + | ||
| 117 | +def self_test(): | ||
| 118 | + """A POSITIVE CONTROL for the census itself, in the exact shape that cost a | ||
| 119 | + gate: a doc comment reproducing the forbidden root literal, sitting between | ||
| 120 | + two gates, one of which reads crate_pin. | ||
| 121 | + | ||
| 122 | + It runs the REAL pipeline -- a synthetic crate written into the tree, the | ||
| 123 | + normal scan, the normal records -- because a control that re-implements the | ||
| 124 | + decision order would pass while the real path is broken, which is the failure | ||
| 125 | + this control exists to catch. Losing a gate to a comment is SILENT: the | ||
| 126 | + census count drops and nothing goes red, so only a control that asserts the | ||
| 127 | + count can see it. | ||
| 128 | + """ | ||
| 129 | + # Build the synthetic tree in a temp dir and point the scan at it, so the | ||
| 130 | + # control runs even where the staged tree is read-only. | ||
| 131 | + import tempfile | ||
| 132 | + sandbox = tempfile.mkdtemp(prefix="pin-audit-selftest-") | ||
| 133 | + saved_root = globals()["ROOT"] | ||
| 134 | + globals()["ROOT"] = sandbox | ||
| 135 | + d = os.path.join(sandbox, "crates", SELFTEST_CRATE, "src") | ||
| 136 | + os.makedirs(d, exist_ok=True) | ||
| 137 | + body = """//! self-test crate, removed by the audit itself. | ||
| 138 | +pub fn helper() {} | ||
| 139 | + | ||
| 140 | +#[test] | ||
| 141 | +fn selftest_a_reads_a_pin() { | ||
| 142 | + // Reproduces the forbidden literal verbatim, INSIDE this gate's captured | ||
| 143 | + // span -- exactly the shape that cost a real gate: fn_bodies runs a body | ||
| 144 | + // from its `fn` line to the NEXT `fn` line, so a comment between two gates | ||
| 145 | + // belongs to the earlier one. | ||
| 146 | + // concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures/selftest.obu") | ||
| 147 | + let p = crate_pin("golden3-pin.obu"); | ||
| 148 | + helper(); | ||
| 149 | + assert!(!p.as_os_str().is_empty()); | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +#[test] | ||
| 153 | +fn selftest_b_reads_a_pin() { | ||
| 154 | + let p = crate_pin("sbpart-pin.obu"); | ||
| 155 | + helper(); | ||
| 156 | + assert!(!p.as_os_str().is_empty()); | ||
| 157 | +} | ||
| 158 | +""" | ||
| 159 | + path = os.path.join(d, "lib.rs") | ||
| 160 | + try: | ||
| 161 | + with open(path, "w", encoding="utf-8") as fh: | ||
| 162 | + fh.write(body) | ||
| 163 | + rows = [] | ||
| 164 | + for c, p in sources(): | ||
| 165 | + text = open(p, encoding="utf-8", errors="replace").read() | ||
| 166 | + rel = os.path.relpath(p, ROOT) | ||
| 167 | + for line, name, b, attrs in fn_bodies(text): | ||
| 168 | + if not any(a.startswith("#[test") for a in attrs): | ||
| 169 | + continue | ||
| 170 | + if not name.startswith("selftest_"): | ||
| 171 | + continue | ||
| 172 | + code = re.sub(r"/\*.*?\*/", "", b, flags=re.S) | ||
| 173 | + code = "\n".join(x.split("//", 1)[0] for x in code.splitlines()) | ||
| 174 | + kind = "root-literal" if (ROOT_RE.search(code) or JOIN_RE.search(code)) else "crate_pin" | ||
| 175 | + rows.append((name, kind)) | ||
| 176 | + bad = [r for r in rows if r[1] != "crate_pin"] | ||
| 177 | + missing = [n for n in ("selftest_a_reads_a_pin", "selftest_b_reads_a_pin") | ||
| 178 | + if n not in [r[0] for r in rows]] | ||
| 179 | + ok = not bad and not missing | ||
| 180 | + for n, k in sorted(rows): | ||
| 181 | + print("SELFTEST\t{}\t{}".format(n, k)) | ||
| 182 | + if ok: | ||
| 183 | + print("SELFTEST\tPASS\ta doc comment between two gates left both counted as crate_pin") | ||
| 184 | + else: | ||
| 185 | + print("SELFTEST\tFAIL\treclassified={} missing={}".format(bad, missing)) | ||
| 186 | + return 0 if ok else 1 | ||
| 187 | + finally: | ||
| 188 | + globals()["ROOT"] = saved_root | ||
| 189 | + import shutil | ||
| 190 | + shutil.rmtree(sandbox, ignore_errors=True) | ||
| 191 | + | ||
| 192 | + | ||
| 112 | def main(): | 193 | def main(): |
| 113 | helpers = {} # fn name -> body (all fns in the tree) | 194 | helpers = {} # fn name -> body (all fns in the tree) |
| 114 | for crate, path in sources(): | 195 | for crate, path in sources(): |
| @@ -287,4 +368,6 @@ def main(): | |||
| 287 | 368 | ||
| 288 | 369 | ||
| 289 | if __name__ == "__main__": | 370 | if __name__ == "__main__": |
| 371 | + if "--self-test" in sys.argv: | ||
| 372 | + sys.exit(self_test()) | ||
| 290 | sys.exit(main()) | 373 | sys.exit(main()) |
scripts/verify-fixture-library.shmodified+16 -1
| @@ -303,12 +303,27 @@ if ! command -v python3 >/dev/null; then | |||
| 303 | echo "FAIL [invariant 4]: python3 is required for the pin-gate audit and is absent" >&2 | 303 | echo "FAIL [invariant 4]: python3 is required for the pin-gate audit and is absent" >&2 |
| 304 | fail=1 | 304 | fail=1 |
| 305 | else | 305 | else |
| 306 | + selftest_out=$(mktemp) | ||
| 306 | audit=$("$ROOT/scripts/pin-gate-audit.py" 2>&1) | 307 | audit=$("$ROOT/scripts/pin-gate-audit.py" 2>&1) |
| 307 | if [ $? -ne 0 ] && [ -z "$audit" ]; then | 308 | if [ $? -ne 0 ] && [ -z "$audit" ]; then |
| 308 | echo "FAIL [invariant 4]: scripts/pin-gate-audit.py did not run" >&2 | 309 | echo "FAIL [invariant 4]: scripts/pin-gate-audit.py did not run" >&2 |
| 309 | fail=1 | 310 | fail=1 |
| 310 | fi | 311 | fi |
| 311 | echo "$audit" | awk -F'\t' '/^COUNT/{print " pin gates: "$2" "$3" "$4" "$5" "$6}' >&2 | 312 | echo "$audit" | awk -F'\t' '/^COUNT/{print " pin gates: "$2" "$3" "$4" "$5" "$6}' >&2 |
| 313 | + # The census's own POSITIVE CONTROL. Losing a gate to a doc comment is | ||
| 314 | + # SILENT -- the count drops and nothing goes red -- so a control that only | ||
| 315 | + # proved "the scanner finds a bad literal" would not catch it. This one | ||
| 316 | + # plants the exact shape: a comment reproducing the forbidden literal | ||
| 317 | + # inside a gate that reads crate_pin. | ||
| 318 | + if ! "$ROOT/scripts/pin-gate-audit.py" --self-test >"$selftest_out" 2>&1; then | ||
| 319 | + echo "FAIL [invariant 4]: the census self-test did not pass -- a doc comment" >&2 | ||
| 320 | + echo " can reclassify a pin-reading gate into a branch that counts" >&2 | ||
| 321 | + echo " nothing, which loses the gate SILENTLY. Fix the scanner first." >&2 | ||
| 322 | + sed 's/^/ /' "$selftest_out" >&2 | ||
| 323 | + fail=1 | ||
| 324 | + else | ||
| 325 | + note " invariant 4: census self-test passed (a comment cannot steal a gate)" | ||
| 326 | + fi | ||
| 312 | badrows=$(echo "$audit" | awk -F'\t' '/^BADROW/{print " "$2":"$3" "$4" "$5}') | 327 | badrows=$(echo "$audit" | awk -F'\t' '/^BADROW/{print " "$2":"$3" "$4" "$5}') |
| 313 | bardir=$(echo "$audit" | awk -F'\t' '/^BARDIR/{print " "$2":"$3" "$4" "$5}') | 328 | bardir=$(echo "$audit" | awk -F'\t' '/^BARDIR/{print " "$2":"$3" "$4" "$5}') |
| 314 | absent=$(echo "$audit" | awk -F'\t' '/^NAME/ && $4=="absent"{print " "$2":"$3" "$4}') | 329 | absent=$(echo "$audit" | awk -F'\t' '/^NAME/ && $4=="absent"{print " "$2":"$3" "$4}') |
| @@ -376,7 +391,7 @@ echo " EXTRACTED sets, so the blobs are not required at test time" >&2 | |||
| 376 | # --- DRIFT ---------------------------------------------------------------- | 391 | # --- DRIFT ---------------------------------------------------------------- |
| 377 | regen=$(mktemp) | 392 | regen=$(mktemp) |
| 378 | patch=$(mktemp) | 393 | patch=$(mktemp) |
| 379 | -trap 'rm -f "$regen" "$patch"' EXIT | 394 | +trap 'rm -f "$regen" "$patch" ${selftest_out:-} ' EXIT |
| 380 | if ! "$ROOT/scripts/gen-fixture-library.sh" "$regen" >/dev/null 2>&1; then | 395 | if ! "$ROOT/scripts/gen-fixture-library.sh" "$regen" >/dev/null 2>&1; then |
| 381 | echo "FAIL: scripts/gen-fixture-library.sh did not run" >&2 | 396 | echo "FAIL: scripts/gen-fixture-library.sh did not run" >&2 |
| 382 | exit 1 | 397 | exit 1 |