Skip to content

eBPF-based security platform

testing: run the proofs that read what a range of commits changed

Daniel Borkmannf523c4b0f116
diff --git a/.agents/skills/pre-submit/SKILL.md b/.agents/skills/pre-submit/SKILL.md
index 553ad894..38674750 100644
--- a/.agents/skills/pre-submit/SKILL.md
+++ b/.agents/skills/pre-submit/SKILL.md
@@ -1,6 +1,6 @@
---
name: pre-submit
-description: Run the checks CI runs on a series of shirudo commits before handing it over. Covers each commit built on its own, checkpatch, sign-offs, Coccinelle, sparse, SPDX and the documentation check, both compilers, the sanitizer and fuzz runs, and the VM families the change reaches. Use before saying a change is ready, after rebasing or reordering a series, and to reproduce a CI failure.
+description: Run the checks CI runs on a series of shirudo commits before handing it over, and the proofs CI does not run. Covers each commit built on its own, checkpatch, sign-offs, Coccinelle, sparse, SPDX and the documentation check, both compilers, the sanitizer and fuzz runs, the proofs that read the code the series changed, and the VM families the change reaches. Use before saying a change is ready, after rebasing or reordering a series, and to reproduce a CI failure.
---
# Pre-submit checks
@@ -60,6 +60,8 @@ make -B CC=clang && make -B CC=clang shirudo-build &&
make tests VM=0 SANITIZE=address,undefined # CC=gcc and CC=clang,
# one worktree each
make -C testing fuzz FUZZ_SECONDS=60
+make proofs-cbmc CHANGED=$range # the proofs that read
+make proofs-lean4 # what the series changed
make -C testing functional/<family> # each family it touches
```
@@ -69,6 +71,18 @@ fixed. `check-sparse.sh` refuses a little- or big-endian field read or
written without `le32_to_cpu()`, `cpu_to_le32()` or their kin, and
`check-spdx.sh` the wrong license tag for a file's kind.
+CI does not run the proofs, so this run is the only one a series gets
+before it lands. `CHANGED` takes what `git diff` takes and runs the CBMC
+proofs whose harness reads a file changed there, all of them after a
+change to `run.sh` or `proofs.list`. A proof that fails, or that passes
+and no longer states what the changed code does, is brought back in
+step in the series, with its row in
+`Documentation/contribute/testing/proof-coverage.rst`; so is a Lean 4
+model that the page links to the changed C by review, which no run
+checks: read it against the change. A proof out of step with the code
+is a security bug, as the `security-audit` skill has it; the `proofs`
+skill says how to fix one.
+
## Fixing a commit of the series
Fold a fix into the commit it belongs to rather than adding one on top:
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.md
index 745b230f..20dccd9e 100644
--- a/.agents/skills/proofs/SKILL.md
+++ b/.agents/skills/proofs/SKILL.md
@@ -18,13 +18,15 @@ a proof or to what it covers updates the coverage page too.
```sh
make proofs # CBMC and Lean 4
make proofs-cbmc PROOFS="string-number hex" # some CBMC proofs
+make proofs-cbmc CHANGED=origin/main..HEAD # those reading a changed file
testing/proofs/cbmc/run.sh -v <proof> # whole output and traces
make proofs-lean4 # the Lean 4 proofs
```
CI does not run the proofs, as all of them take about an hour: run the
-ones covering the code you changed by name, and all of them after a
-change to `proof.h` or `run.sh`. Cap the memory of a long run, `ulimit
+ones covering the code you changed, which `CHANGED` picks from what
+each harness includes, and all of them after a change to `proof.h` or
+`run.sh`, which `CHANGED` does too. Cap the memory of a long run, `ulimit
-v 24000000` in the shell that starts it: a proof that grows past a few
gigabytes is one to reshape, not to wait for.
diff --git a/.agents/skills/security-audit/SKILL.md b/.agents/skills/security-audit/SKILL.md
index c84366c1..3bcd344f 100644
--- a/.agents/skills/security-audit/SKILL.md
+++ b/.agents/skills/security-audit/SKILL.md
@@ -63,9 +63,10 @@ before code that did not.
For a change, the diff names the rows it touches. A check it removes
or moves is read first: `git log -S'<the check>'` finds the commit
that added it and why. Then the threats whose fact boxes name the
-hooks and functions touched, the tests and proofs behind them, and
-every caller of a changed `util/` function, since each relied on what
-it did before.
+hooks and functions touched, the tests and proofs behind them (`make
+proofs-cbmc CHANGED=<range>` runs the proofs that read a changed
+file), and every caller of a changed `util/` function, since each
+relied on what it did before.
For a reported issue, restate it as the five things before reading
code, then take it through "Refute it".
diff --git a/AGENTS.md b/AGENTS.md
index afdd5769..f6cf555d 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -75,8 +75,10 @@ grep -n '<file>.c\|<function>' testing/proofs/cbmc`), or to an algorithm
a Lean 4 model of `testing/proofs/lean4/` follows, adapts the harness or
the model in the same commit, with its line in `proofs.list` and its row
in `Documentation/contribute/testing/proof-coverage.rst`, and runs the
-proofs it touches. A proof that no longer matches the code, or no
-longer holds, is a bug like a page.
+proofs it touches (`make proofs-cbmc CHANGED=<range>` picks them). A
+proof that no longer matches the code, or no longer holds, is a security
+bug: a threat's fact box or a coverage row then claims an assurance the
+tree does not have.
New documentation extends an existing page or adds one to its section's
toctree, and follows `Documentation/contribute/documentation/writing-docs.rst`. Only
diff --git a/Documentation/contribute/testing/proofs.rst b/Documentation/contribute/testing/proofs.rst
index aab77984..a114f905 100644
--- a/Documentation/contribute/testing/proofs.rst
+++ b/Documentation/contribute/testing/proofs.rst
@@ -63,15 +63,25 @@ Run the proofs
held. ``Build completed successfully`` ends the Lean 4 run. CI does
not run the proofs: all of them take about an hour, a few of them
minutes and gigabytes each. Run the ones that cover the code you
- change by name, as the next step shows, and all of them after a
- change to ``proof.h`` or ``run.sh``.
+ change, as the next step shows, and all of them after a change to
+ ``proof.h`` or ``run.sh``.
-2. Run one kind, or some CBMC proofs by name:
+2. Run one kind, some CBMC proofs by name, or the CBMC proofs that read
+ a file you changed:
.. code-block:: console
$ make proofs-lean4
$ make proofs-cbmc PROOFS="string-number upatch-header"
+ $ make proofs-cbmc CHANGED=origin/main..HEAD
+
+ ``CHANGED`` takes what ``git diff`` takes, a range of commits or one
+ commit to compare the working tree with. The compiler lists the files
+ each harness includes, and a proof runs when one of them changed; a
+ change to ``run.sh`` or ``proofs.list`` runs every proof. A run that
+ finds none prints ``proofs: none reads a file changed in <range>``.
+ No run checks a Lean 4 model against the C it follows by review: read
+ the model against the change.
3. Read why a CBMC proof failed:
diff --git a/Makefile b/Makefile
index ad315e77..d8e98d5f 100644
--- a/Makefile
+++ b/Makefile
@@ -130,7 +130,7 @@ cscope:
proofs: proofs-cbmc proofs-lean4
proofs-cbmc:
- $(Q)testing/proofs/cbmc/run.sh $(PROOFS)
+ $(Q)testing/proofs/cbmc/run.sh $(if $(CHANGED),-c '$(CHANGED)') $(PROOFS)
proofs-lean4:
$(Q)testing/proofs/lean4/run.sh
diff --git a/testing/proofs/cbmc/run.sh b/testing/proofs/cbmc/run.sh
index 873c8fc3..490b33f7 100755
--- a/testing/proofs/cbmc/run.sh
+++ b/testing/proofs/cbmc/run.sh
@@ -8,18 +8,27 @@
# harness defines harness() and includes the source it proves; proofs.list
# gives its CBMC options, and LEN=<a>-<b>,<c> there runs it once for each
# length the list of lengths and ranges names, as CBMC handles a fixed
-# length far faster than one it has to keep symbolic. -v prints the whole CBMC output and the trace of a
-# failed check.
+# length far faster than one it has to keep symbolic. -v prints the whole
+# CBMC output and the trace of a failed check. -c takes what git diff
+# takes, a range of commits or one to compare the working tree with, and
+# runs only the proofs whose harness reads a file changed there, as the
+# compiler lists what each harness includes; a change to this script or
+# to proofs.list runs them all.
#
-# testing/proofs/cbmc/run.sh [-v] [<proof>...]
+# testing/proofs/cbmc/run.sh [-v] [-c <rev-range>] [<proof>...]
set -eu
verbose=
-if [ "${1:-}" = -v ]; then
- verbose=--trace
- shift
-fi
+range=
+while getopts vc: opt; do
+ case $opt in
+ v) verbose=--trace ;;
+ c) range=$OPTARG ;;
+ *) exit 2 ;;
+ esac
+done
+shift $((OPTIND - 1))
cd "$(dirname "$0")"
root=$(cd ../../.. && pwd)
@@ -37,6 +46,44 @@ checks='--unsigned-char --bounds-check --pointer-check
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
+if [ -n "$range" ]; then
+ git diff --name-only "$range" >"$work/changed"
+ if grep -qxE 'testing/proofs/cbmc/(run\.sh|proofs\.list)' "$work/changed"; then
+ range=
+ fi
+fi
+
+# Whether a harness, built with the -I and -D options and the first
+# length of its line, reads a changed file. One the compiler cannot list
+# counts as changed.
+touched() {
+ name=$1
+ shift
+ flags=
+ dir=
+ for o in "$@"; do
+ if [ -n "$dir" ]; then
+ flags="$flags -I $o"
+ dir=
+ continue
+ fi
+ case $o in
+ -I) dir=1 ;;
+ -D*) flags="$flags $o" ;;
+ LEN=*)
+ o=${o#LEN=}
+ flags="$flags -DLEN=${o%%[-,]*}"
+ ;;
+ esac
+ done
+ # shellcheck disable=SC2086
+ "${CC:-cc}" -MM -I "$root/include" -I "$root/util" -D_GNU_SOURCE \
+ $flags "$name.c" >"$work/deps" 2>/dev/null || return 0
+ sed 's/\\$//' "$work/deps" | tr -s ' ' '\n' | grep -v ':$' | grep . |
+ xargs realpath --relative-to="$root" |
+ grep -qxF -f "$work/changed"
+}
+
wanted() {
name=$1
shift 2
@@ -63,6 +110,8 @@ ran=0
grep -v '^[[:space:]]*\(#\|$\)' proofs.list >"$work/list"
while read -r name opts; do
wanted "$name" -- "$@" || continue
+ # shellcheck disable=SC2086
+ [ -z "$range" ] || touched "$name" $opts || continue
ran=$((ran + 1))
lens=-
rest=
@@ -109,6 +158,10 @@ while read -r name opts; do
done <"$work/list"
[ "$ran" -gt 0 ] || {
+ [ -z "$range" ] || {
+ printf 'proofs: none reads a file changed in %s\n' "$range"
+ exit 0
+ }
printf 'proofs: no proof named %s\n' "$*" >&2
exit 2
}