forge
Explore
Log in

Don't you dare go Hollow

census: comment-stripped matching plus a self-test in the shape that cost a gate

Browse files at this commit

3972653317a8b46fd71abe8364040b5343d141c0Ahmet Kaan GümüşParent: 7e097fe
The strip was real but landed in 2b4ecf71, not in the commit I told Agent01-5 it
was in, so their measurement against 12cae635 was accurate for that tree: a doc
comment belonging to the NEXT gate lands inside the previous gate's body (fn_bodies
spans fn-line to next-fn-line), ROOT_RE matched the comment, the gate was
reclassified into the root-literal branch, found no names and hit continue. The
census count dropped and NOTHING went red -- unproven read as clean.

Two things close that door:
  - every search (ROOT_RE, JOIN_RE, the crate_pin/pin_dir singles, the asserts
    check) matches on a comment-stripped body, /* */ and // and /// alike;
  - scripts/pin-gate-audit.py --self-test is a POSITIVE CONTROL in the exact
    shape: a comment reproducing the forbidden literal INSIDE a gate that reads
    crate_pin, plus a second gate after it. It runs the real pipeline over a
    synthetic tree in a temp dir, so it works on a read-only staged tree, and it
    asserts BOTH gates are still counted as crate_pin. The verifier runs it and
    fails if it does not pass.
    MUTATION-PROVED: with the stripping removed from the self-test path it fails
    with reclassified=[('selftest_a_reads_a_pin', 'root-literal')]; restored, it
    passes. A control that only proved 'the scanner finds a forbidden literal'
    would NOT have caught this, which is why it is a separate control.

Re-measured with the verbatim prose restored (the state that ships): census
total=8 committed=21 uncommitted=0 ignored=0 assertless=0, code-shape violations
0, both positive controls firing, verify GREEN (303 rows).
scripts/pin-gate-audit.pymodified+84 -1
@@ -45,7 +45,8 @@ import re
45 import subprocess45 import subprocess
46 import sys46 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" >&2303 echo "FAIL [invariant 4]: python3 is required for the pin-gate audit and is absent" >&2
304 fail=1304 fail=1
305 else305 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" ]; then308 if [ $? -ne 0 ] && [ -z "$audit" ]; then
308 echo "FAIL [invariant 4]: scripts/pin-gate-audit.py did not run" >&2309 echo "FAIL [invariant 4]: scripts/pin-gate-audit.py did not run" >&2
309 fail=1310 fail=1
310 fi311 fi
311 echo "$audit" | awk -F'\t' '/^COUNT/{print " pin gates: "$2" "$3" "$4" "$5" "$6}' >&2312 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"' EXIT394+trap 'rm -f "$regen" "$patch" ${selftest_out:-} ' EXIT
380 if ! "$ROOT/scripts/gen-fixture-library.sh" "$regen" >/dev/null 2>&1; then395 if ! "$ROOT/scripts/gen-fixture-library.sh" "$regen" >/dev/null 2>&1; then
381 echo "FAIL: scripts/gen-fixture-library.sh did not run" >&2396 echo "FAIL: scripts/gen-fixture-library.sh did not run" >&2
382 exit 1397 exit 1