Skip to content

eBPF-based security platform

testing: prove the string, hex and line helpers with CBMC

Daniel Borkmann382fcb4b42ee
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.md
new file mode 100644
index 00000000..7843463e
--- /dev/null
+++ b/.agents/skills/proofs/SKILL.md
@@ -0,0 +1,76 @@
+---
+name: proofs
+description: Run, write and debug shirudo's CBMC proofs under testing/proofs, which check its parsers and emitters for every input within a bound. Covers make proofs, the harness and proofs.list conventions, choosing properties and bounds, keeping a proof fast and within memory, and telling a real bug from a harness error. Use when changing code a proof covers, when adding a parser or emitter, when a proof fails, or when asked to prove code correct.
+---
+
+# Proofs
+
+`Documentation/contribute/testing/proofs.rst` is the how-to, and
+`Documentation/contribute/testing/proof-coverage.rst` lists every proof
+and what no proof covers yet. A change to code a proof covers runs that
+proof, and a change to a proof or to what it covers updates the coverage
+page in the same commit.
+
+## Run
+
+```sh
+make proofs # every proof
+make proofs PROOFS="string-number hex" # some of them
+testing/proofs/run.sh -v <proof> # whole output and traces
+```
+
+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.
+
+## Write
+
+- `testing/proofs/<name>.c` includes `"proof.h"` and the `.c` it proves,
+ and defines `harness()`. Inputs come from `any_bytes(n)` and from
+ variables left uninitialized; `__CPROVER_assume()` bounds them and
+ `__CPROVER_assert(cond, "what holds")` states each property.
+- State the contract, not the implementation: which inputs are accepted,
+ that every offset or pointer returned lies inside the input, that a
+ refusal writes nothing, that output reads back. Proving the code equal
+ to a reference that computes the same arithmetic is hard for the SAT
+ solver and proves little more: an exact value check belongs on short
+ inputs only.
+- Prefer a harness that holds for inputs of every size: when the code
+ reads a fixed-size part, give it exactly that many bytes and leave the
+ size it is told symbolic, as `upatch-header.c` does.
+- Then run it on broken code: make a loop read one byte too many or drop
+ a check, see the proof fail, and restore the file.
+
+## Keep it fast
+
+- A length CBMC keeps symbolic makes every access to the buffer costly.
+ Run the proof once per length instead: `LEN=0-6` or `LEN=16,64` in
+ `proofs.list`, with the harness using `LEN`.
+- A global `--unwind` unrolls every loop that far, including those whose
+ count comes from the data. Give each loop its bound with
+ `--unwindset <function>.<n>:<bound>` (`goto-cc --function harness ... -o
+ x.goto; goto-instrument --show-loops x.goto` names them) and keep the
+ global one small; the unwinding assertions fail the proof if an input
+ needs more, so a tight bound is safe.
+- A loop the input cannot reach at the harness's sizes takes bound 0, as
+ the policy labels do in data too short to hold one.
+- `too many addressed objects` asks for `--object-bits 12`.
+- A proof over a few hundred symbolic bytes with long string scans can
+ take minutes: split it, proving the parts the caller composes, as the
+ `.upatch` header, sections and decoders are proven apart.
+
+## When it fails
+
+- `no body for function <f>`: CBMC lets that call return anything, so
+ `run.sh` fails the proof. Add a model of `<f>` to `proof.h` or keep the
+ proof away from it.
+- A failed `__CPROVER_assert` or bounds check comes with a trace under
+ `-v`. Rebuild the input it names as a unit test or a small C program
+ before calling it a bug: a harness that assumed too little or stated
+ the property wrongly fails too.
+- `run.sh` passes `--unsigned-char`, as the build passes
+ `-funsigned-char`; a conversion failure on `char` elsewhere means the
+ proof is not built as the code is.
+
+A real bug is fixed in a commit of its own, with the proof that found it
+or a unit test, and the proof commit follows.
diff --git a/AGENTS.md b/AGENTS.md
index a569a2ad..c98d85a6 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -46,7 +46,7 @@ Before changing security-relevant code, read
| `Documentation/upatches/` | Authoring and shipping upatches: attach kinds, maps and serializers, decoders, the Beefile, publishing, debugging a failed load. |
| `Documentation/reference/` | Every command (`reference/cli/`), the formats and settings (`reference/formats/`, including the `.upatch` container), failure categories and exit status, files, units, environment variables, the cheat sheet. |
| `Documentation/contribute/` | Developer guide, test tiers and the test VM (read `testing.rst` before adding or moving a test), test coverage, the kernel coding style, commits and sign-off (read `contributing.rst` before crafting a commit), and the documentation style guide `writing-docs.rst`. |
-| `.agents/skills/` | Skills for coding agents in the `SKILL.md` format of agentskills.io, which Codex reads there and Claude Code through the `.claude/skills` link: `coding` writes C in the tree's style and grows `util/`, `testing` runs the test tiers and the test VM, `pre-submit` the checks CI runs on a series, `upatch` writes for the target's preemption model and a low cost, builds, packages and publishes an upatch, `bpf-concurrency` checks upatch code whose runs share state, `bpf-coccinelle` runs and extends the Coccinelle rules for it, `docs` finds and checks the pages a change touches, `fuzz-target` adds a libFuzzer target for a parser. |
+| `.agents/skills/` | Skills for coding agents in the `SKILL.md` format of agentskills.io, which Codex reads there and Claude Code through the `.claude/skills` link: `coding` writes C in the tree's style and grows `util/`, `testing` runs the test tiers and the test VM, `pre-submit` the checks CI runs on a series, `upatch` writes for the target's preemption model and a low cost, builds, packages and publishes an upatch, `bpf-concurrency` checks upatch code whose runs share state, `bpf-coccinelle` runs and extends the Coccinelle rules for it, `docs` finds and checks the pages a change touches, `fuzz-target` adds a libFuzzer target for a parser, `proofs` runs and writes the CBMC proofs. |
The kernel side of the signing design is the kernel's own
`Documentation/bpf/signing.rst`; `bpf_prog_verify_signature()` runs
@@ -306,6 +306,10 @@ one usually still compiles and passes tests.
fuzz target, and a shell test `t<F><K><NN>` lives in its family's
directory (`Documentation/contribute/testing/testing.rst`). The
`testing` and `fuzz-target` skills say how to run and add them.
+- `make proofs` runs the CBMC proofs of `testing/proofs/`, which check
+ parsers and emitters for every input within a bound. A change to code a
+ proof covers runs it, and a new parser gets one where CBMC can carry
+ it; the `proofs` skill says how.
- CI runs `.github/scripts/build-commits.sh`, `check-style.sh`,
`check-dco.sh`, `check-coccinelle.sh`, `check-sparse.sh`,
`check-spdx.sh` and the documentation check on every pull request;
diff --git a/Documentation/contribute/testing/index.rst b/Documentation/contribute/testing/index.rst
index 01c10cc3..60f72b2a 100644
--- a/Documentation/contribute/testing/index.rst
+++ b/Documentation/contribute/testing/index.rst
@@ -5,11 +5,13 @@
Testing
=======
-Run the test tiers and the test VM, write a test, and see what the tests
-cover and what they do not.
+Run the test tiers and the test VM, write a test or a proof, and see what
+the tests and proofs cover and what they do not.
.. toctree::
:titlesonly:
testing
test-coverage
+ proofs
+ proof-coverage
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
new file mode 100644
index 00000000..7b72e10a
--- /dev/null
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -0,0 +1,72 @@
+..
+ SPDX-License-Identifier: BSD-2-Clause
+ Copyright (c) 2026 Authors of shirudo
+
+:page-type: Reference
+:audience: shirudo contributors
+
+Proof coverage
+==============
+
+This page lists the CBMC proofs under :src:`testing/proofs/`, the code
+each one proves, the inputs it covers and the properties it states, and
+the parsers no proof covers yet. :doc:`proofs` explains how to run and
+write them. Every proof also shows that the code reads and writes nothing
+outside its objects and has no undefined arithmetic for those inputs.
+
+"Every input" means every input of any length. A bound such as "up to 22
+bytes" means every input of that length or shorter, and "lengths 0 to 6"
+every input of each of those lengths.
+
+Proofs
+------
+
+.. list-table::
+ :header-rows: 1
+ :widths: 22 26 18 34
+
+ * - Proof
+ - Code
+ - Inputs
+ - Properties
+ * - ``string-number``
+ - ``parse_u64()``, ``parse_x64()``, ``parse_s64()``, ``digits()``
+ - Up to 22 bytes, past the 20 digits of the largest value
+ - Refuses exactly what is empty or holds a non-digit; a number
+ within its bounds and of the sign the string spells; nothing
+ written on refusal; ``digits()`` finds the whole run
+ * - ``string-utf8``
+ - ``utf8_width()``
+ - Every input
+ - Equals Table 3-7 of the Unicode Standard, the well-formed byte
+ sequences, row by row
+ * - ``string-sanitize``
+ - ``text_sanitize()``
+ - Lengths 0 to 6, with and without line breaks
+ - Leaves well-formed UTF-8 without a C0, DEL or C1 control but a
+ kept line break; changes a byte only to ``?``; keeps every
+ printable character
+ * - ``string-copy``
+ - ``strscpy()``
+ - Strings and buffers up to 8 bytes
+ - Terminates what it writes; the whole string and its length, or
+ what fits and ``-E2BIG``
+ * - ``hex``
+ - ``hex_encode()``, ``hex_decode()``
+ - Up to 4 bytes, strings up to 9 characters
+ - Lowercase hex that decodes back to the input; a decode takes
+ exactly ``2 * n`` hex digits of either case and yields their value
+ * - ``io-lines``
+ - ``lines_len()``
+ - Up to 8 bytes
+ - Ends at a newline or is 0, with no newline behind it
+
+Not covered yet
+---------------
+
+The fuzz targets in :doc:`test-coverage` cover these parsers; no proof
+does: the JSON parser and emitter, the ``.upatch`` signed sections,
+decoders and policy labels, the ``.rodata`` view, the package archive,
+index and manifest readers, the tree publication and log, the eventlog,
+the TPM structures of evidence, the configuration files and images, and
+the kernel notes and configuration.
diff --git a/Documentation/contribute/testing/proofs.rst b/Documentation/contribute/testing/proofs.rst
new file mode 100644
index 00000000..00f7b5a9
--- /dev/null
+++ b/Documentation/contribute/testing/proofs.rst
@@ -0,0 +1,117 @@
+..
+ SPDX-License-Identifier: BSD-2-Clause
+ Copyright (c) 2026 Authors of shirudo
+
+:page-type: How-to
+:audience: shirudo contributors
+
+Prove code with CBMC
+====================
+
+A test checks the inputs someone thought of, and a fuzz target the ones it
+happens to find. A proof under :src:`testing/proofs/` checks every input
+within its bounds: `CBMC <https://www.cprover.org/cbmc/>`_ turns shirudo's
+C and a harness into a formula and asks a SAT solver for an input that
+breaks a property. When none exists, the property holds for all of them.
+
+Each proof states two kinds of property. CBMC adds the first to every
+proof: no read or write outside an object, no null or dangling pointer,
+no signed overflow, no shift past the width of a type, no division by
+zero, no conversion that changes a value, and no loop that runs longer
+than the harness allows. The harness states the second: what the code
+must do, such as that a parser accepts exactly the well-formed inputs and
+that every offset it returns lies inside the input.
+
+A proof covers every input up to the bound its harness names. Where the
+format itself bounds what the code reads, as a fixed-size header does, the
+proof covers every input of any size; elsewhere it covers every input up
+to the stated length, and :doc:`proof-coverage` says which is which.
+
+Before you begin
+----------------
+
+- CBMC 5.95 or newer, packaged as ``cbmc`` by Debian and Ubuntu.
+- Memory: most proofs need little, but a large one can take several
+ gigabytes.
+
+Run the proofs
+--------------
+
+1. Run all of them from the checkout:
+
+ .. code-block:: console
+
+ $ make proofs
+
+ Each line names a proof, the number of checks CBMC made and the time
+ it took. ``proofs: <n> hold`` ends a run in which all of them held.
+
+2. Run some of them by name:
+
+ .. code-block:: console
+
+ $ make proofs PROOFS="string-number upatch-header"
+
+3. Read why one failed:
+
+ .. code-block:: console
+
+ $ testing/proofs/run.sh -v string-number
+
+ ``-v`` prints CBMC's whole output, with the trace of each failed
+ check: the input that breaks it and the steps that lead there.
+
+Write a proof
+-------------
+
+1. Write ``testing/proofs/<name>.c``. It includes ``proof.h`` and the
+ source file it proves, so that it reaches static functions too, and
+ defines ``harness()``, which CBMC runs with every input at once.
+ ``any_bytes(n)`` gives ``n`` bytes of any contents, and a variable
+ left uninitialized takes any value; ``__CPROVER_assume()`` narrows the
+ inputs to the bound, and ``__CPROVER_assert()`` states each property
+ with a message that says what holds.
+
+2. State what the code must do from its contract, not from its code: an
+ accepted string is a number of the right digits, an accepted file has
+ every section inside it. A check that restates the implementation
+ proves nothing about it.
+
+3. Add the proof to :src:`testing/proofs/proofs.list` with its CBMC
+ options:
+
+ - ``--unwind <n>`` for loops, ``--unwindset <loop>:<n>`` for one loop,
+ with ``goto-instrument --show-loops`` naming them. The unwinding
+ assertions ``run.sh`` adds fail when an input needs more iterations,
+ so a bound too low fails the proof rather than weakening it.
+ - ``LEN=<a>-<b>`` runs the proof once for each length, with ``LEN``
+ defined: CBMC takes far longer over an input whose length it has to
+ keep symbolic.
+ - ``--object-bits <n>`` when CBMC reports too many addressed objects.
+
+4. Check that the proof can fail: break the code it proves, for instance
+ a loop that reads one byte too many, run it and see it fail, then
+ restore the code.
+
+5. Add a row to :doc:`proof-coverage`.
+
+A function without a body in the proof, a libc one CBMC does not model or
+a system call, returns any value, and ``run.sh`` fails a proof that calls
+one. Give the function a model in ``proof.h``, or keep the proof away from
+the code that calls it.
+
+Limits
+------
+
+A proof is about the code CBMC sees: the C of the files the harness
+includes, with ``-funsigned-char`` as the build sets it, against a model
+of the libc functions it calls. It says nothing about the kernel, the
+compiler's code generation, time, or other threads, and it holds only
+within its bounds.
+
+Related
+-------
+
+- :doc:`proof-coverage`
+- :doc:`testing`
+- :doc:`test-coverage`
diff --git a/Makefile b/Makefile
index 4e717951..c7a7d1ca 100644
--- a/Makefile
+++ b/Makefile
@@ -50,7 +50,7 @@ export prefix exec_prefix bindir libexecdir sharedir libdir mandir completionsdi
systemdunitdir dracutmodulesdir SHIRUDO_TARGET_PKG_CONFIG_PATH \
SHIRUDO_TARGET_STATIC_LIBDIR
-.PHONY: all help target target-deps target-deps-clean shirudo-target target-config shirudo-build docs bpf-deps install install-target install-operator clean distclean config tests sparse
+.PHONY: all help target target-deps target-deps-clean shirudo-target target-config shirudo-build docs bpf-deps install install-target install-operator clean distclean config tests proofs sparse
all: config
+$(Q)$(MAKE) -f Makefile.shirudo PROFILE=operator CONFIG_MK=config.mk all
@@ -91,6 +91,7 @@ help:
' Install roles: make install-target, make install-operator' \
' Package: make -C packaging help' \
' Tests: make tests (all of them), make -C testing help' \
+ ' Proofs: make proofs [PROOFS=<proof>...] (CBMC, testing/proofs)' \
' Documentation: make docs (render and serve), make -C Documentation help' \
' Watch lists: make -C upatches/kernel-obs lists'
@@ -106,6 +107,9 @@ docs:
tests:
+$(Q)$(MAKE) -C testing
+proofs:
+ $(Q)testing/proofs/run.sh $(PROOFS)
+
# config.mk is regenerated by Makefile.config from the host's pkg-config
# state. Caches results until `make distclean`.
bpf-deps:
diff --git a/testing/proofs/hex.c b/testing/proofs/hex.c
new file mode 100644
index 00000000..de8c2049
--- /dev/null
+++ b/testing/proofs/hex.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * util/hex.c for every input of up to LEN_MAX bytes: an encoding is
+ * lowercase hex that decodes back to its input, and a decode takes
+ * exactly 2 * n hex digits of either case and yields their value.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../util/hex.c"
+
+#define LEN_MAX 4
+
+static int digit(char c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ return -1;
+}
+
+static void encoded(void)
+{
+ __u8 *in, *back;
+ size_t n, i;
+ char *out;
+
+ __CPROVER_assume(n <= LEN_MAX);
+ in = any_bytes(n);
+ out = any_bytes(2 * n + 1);
+ back = any_bytes(n);
+ hex_encode(in, n, out);
+ __CPROVER_assert(!out[2 * n], "terminated");
+ for (i = 0; i < 2 * n; i++)
+ __CPROVER_assert((out[i] >= '0' && out[i] <= '9') ||
+ (out[i] >= 'a' && out[i] <= 'f'),
+ "lowercase hex digits");
+ __CPROVER_assert(!hex_decode(out, back, n) && !memcmp(back, in, n),
+ "decodes back to its input");
+}
+
+static void decoded(void)
+{
+ size_t len, n, i;
+ bool hex = true;
+ char *s;
+ __u8 *out;
+ int ret;
+
+ __CPROVER_assume(len <= 2 * LEN_MAX + 1 && n <= LEN_MAX);
+ s = any_bytes(len + 1);
+ __CPROVER_assume(!s[len] && strnlen(s, len) == len);
+ out = any_bytes(n);
+ ret = hex_decode(s, out, n);
+ for (i = 0; i < len; i++)
+ if (digit(s[i]) < 0)
+ hex = false;
+ __CPROVER_assert(!ret || ret == -EINVAL, "0 or -EINVAL");
+ __CPROVER_assert(!ret == (len == 2 * n && hex),
+ "exactly 2 * n hex digits");
+ for (i = 0; !ret && i < n; i++)
+ __CPROVER_assert(out[i] == digit(s[2 * i]) * 16 +
+ digit(s[2 * i + 1]),
+ "the value of each pair");
+}
+
+void harness(void)
+{
+ encoded();
+ decoded();
+}
diff --git a/testing/proofs/io-lines.c b/testing/proofs/io-lines.c
new file mode 100644
index 00000000..b168f8d3
--- /dev/null
+++ b/testing/proofs/io-lines.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * lines_len() of util/io.c, which the eventlog and tree log readers trust
+ * to drop a torn last record, for every buffer of up to LEN_MAX bytes: it
+ * ends at a newline or is 0, and leaves no newline behind it.
+ */
+
+#include "proof.h"
+#include "../../util/io.c"
+
+#define LEN_MAX 8
+
+void harness(void)
+{
+ size_t len, n, i;
+ char *buf;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ buf = any_bytes(len);
+ n = lines_len(buf, len);
+ __CPROVER_assert(n <= len, "within the buffer");
+ __CPROVER_assert(!n || buf[n - 1] == '\n', "ends at a newline");
+ for (i = n; i < len; i++)
+ __CPROVER_assert(buf[i] != '\n', "no newline after it");
+}
diff --git a/testing/proofs/proof.h b/testing/proofs/proof.h
new file mode 100644
index 00000000..56e49782
--- /dev/null
+++ b/testing/proofs/proof.h
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * What the CBMC harnesses share: memory of any contents, which the
+ * allocators of util/malloc.h cannot give as they zero what they hand out.
+ */
+
+#ifndef SHIRUDO_PROOF_H
+#define SHIRUDO_PROOF_H
+
+#include <stddef.h>
+
+#include <shirudo/types.h>
+
+/*
+ * CBMC models neither strnlen() nor memchr(), and a function without a
+ * body returns anything.
+ */
+size_t strnlen(const char *s, size_t max)
+{
+ size_t n = 0;
+
+ while (n < max && s[n])
+ n++;
+ return n;
+}
+
+void *memchr(const void *s, int c, size_t n)
+{
+ const __u8 *p = s;
+ size_t i;
+
+ for (i = 0; i < n; i++)
+ if (p[i] == (__u8)c)
+ return (void *)(p + i);
+ return NULL;
+}
+
+static inline void *any_bytes(size_t size)
+{
+ void *p = __CPROVER_allocate(size, 0);
+
+ __CPROVER_assume(p);
+ return p;
+}
+
+#endif /* SHIRUDO_PROOF_H */
diff --git a/testing/proofs/proofs.list b/testing/proofs/proofs.list
new file mode 100644
index 00000000..f6852ad8
--- /dev/null
+++ b/testing/proofs/proofs.list
@@ -0,0 +1,8 @@
+# Each harness of this directory and the CBMC options it runs with.
+# --unwind must exceed the longest loop the harness's bound allows.
+string-number --unwind 24
+string-utf8 --unwind 10
+string-copy --unwind 10
+string-sanitize --unwind 8 LEN=0-6
+hex --unwind 12
+io-lines --unwind 10
diff --git a/testing/proofs/run.sh b/testing/proofs/run.sh
new file mode 100755
index 00000000..2ab60b33
--- /dev/null
+++ b/testing/proofs/run.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# testing/proofs/run.sh -- check with CBMC that shirudo's parsers keep the
+# properties the harnesses in this directory state, for every input within
+# the bounds each harness names, and fail when one does not hold. Each
+# harness defines harness() and includes the source it proves; proofs.list
+# gives its CBMC options, and LEN=<a>-<b> or LEN=<x>,<y> there runs it once
+# for each length, 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.
+#
+# testing/proofs/run.sh [-v] [<proof>...]
+
+set -eu
+
+verbose=
+if [ "${1:-}" = -v ]; then
+ verbose=--trace
+ shift
+fi
+
+cd "$(dirname "$0")"
+root=$(cd ../.. && pwd)
+command -v cbmc >/dev/null 2>&1 || {
+ printf 'proofs: cbmc not found; install cbmc\n' >&2
+ exit 2
+}
+
+# --unsigned-char as the build's -funsigned-char.
+checks='--unsigned-char --bounds-check --pointer-check
+ --pointer-primitive-check --div-by-zero-check --signed-overflow-check
+ --undefined-shift-check --pointer-overflow-check --conversion-check
+ --unwinding-assertions'
+
+work=$(mktemp -d)
+trap 'rm -rf "$work"' EXIT
+
+wanted() {
+ name=$1
+ shift 2
+ [ $# -eq 0 ] && return 0
+ for p in "$@"; do
+ [ "$p" = "$name" ] && return 0
+ done
+ return 1
+}
+
+# One CBMC run; a function CBMC has no body for returns anything, which
+# proves nothing about the code that calls it.
+prove() {
+ log=$1
+ shift
+ # shellcheck disable=SC2086
+ cbmc --function harness -I "$root/include" -I "$root/util" \
+ -D_GNU_SOURCE $checks "$@" $verbose >"$log" 2>&1 &&
+ ! grep -q 'no body for function' "$log"
+}
+
+failed=0
+ran=0
+grep -v '^[[:space:]]*\(#\|$\)' proofs.list >"$work/list"
+while read -r name opts; do
+ wanted "$name" -- "$@" || continue
+ ran=$((ran + 1))
+ lens=-
+ rest=
+ for o in $opts; do
+ case $o in
+ LEN=*) lens=${o#LEN=} ;;
+ *) rest="$rest $o" ;;
+ esac
+ done
+ case $lens in
+ -) ;;
+ *-*) lens=$(seq "${lens%-*}" "${lens#*-}") ;;
+ *) lens=$(printf '%s' "$lens" | tr ',' ' ') ;;
+ esac
+ start=$(date +%s)
+ checked=0
+ bad=
+ for len in $lens; do
+ log=$work/$name.$len.log
+ define=
+ [ "$len" = - ] || define="-DLEN=$len"
+ # shellcheck disable=SC2086
+ if prove "$log" $define $rest "$name.c"; then
+ n=$(grep -o 'of [0-9]* failed' "$log" | tail -1 | tr -dc '0-9')
+ checked=$((checked + ${n:-0}))
+ continue
+ fi
+ bad=${define:-all}
+ break
+ done
+ if [ -z "$bad" ]; then
+ printf 'ok %-28s %6d checks, %ss\n' "$name" "$checked" \
+ "$(($(date +%s) - start))"
+ continue
+ fi
+ failed=$((failed + 1))
+ printf 'FAILED %s (%s)\n' "$name" "$bad"
+ if [ -n "$verbose" ]; then
+ cat "$log"
+ else
+ grep -E 'FAILURE|ERROR|error:|no body for function|out of memory' \
+ "$log" | head -20 | sed 's/^/ /'
+ fi
+done <"$work/list"
+
+[ "$ran" -gt 0 ] || {
+ printf 'proofs: no proof named %s\n' "$*" >&2
+ exit 2
+}
+if [ "$failed" -gt 0 ]; then
+ printf 'proofs: %d of %d failed\n' "$failed" "$ran" >&2
+ exit 1
+fi
+printf 'proofs: %d hold\n' "$ran"
diff --git a/testing/proofs/string-copy.c b/testing/proofs/string-copy.c
new file mode 100644
index 00000000..a50b3618
--- /dev/null
+++ b/testing/proofs/string-copy.c
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * strscpy() of util/string.c for every string and buffer of up to
+ * LEN_MAX bytes: it terminates what it writes and copies the whole
+ * string or says that it cut it.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../util/string.c"
+
+#define LEN_MAX 8
+
+static void copied_string(void)
+{
+ size_t size, n;
+ char *dst, *src;
+ ssize_t ret;
+
+ __CPROVER_assume(size <= LEN_MAX && n <= LEN_MAX);
+ src = any_bytes(n + 1);
+ __CPROVER_assume(!src[n] && strnlen(src, n) == n);
+ dst = any_bytes(size);
+ ret = strscpy(dst, src, size);
+ if (size < 1) {
+ __CPROVER_assert(ret == -E2BIG, "no room is -E2BIG");
+ return;
+ }
+ __CPROVER_assert(memchr(dst, 0, size), "terminated");
+ if (n < size)
+ __CPROVER_assert(ret == (ssize_t)n && !memcmp(dst, src, n + 1),
+ "the whole string and its length");
+ else
+ __CPROVER_assert(ret == -E2BIG && !memcmp(dst, src, size - 1) &&
+ !dst[size - 1], "what fits, and -E2BIG");
+}
+
+void harness(void)
+{
+ copied_string();
+}
diff --git a/testing/proofs/string-number.c b/testing/proofs/string-number.c
new file mode 100644
index 00000000..5ca24d9f
--- /dev/null
+++ b/testing/proofs/string-number.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The number parsers of util/string.c on every string of up to LEN_MAX
+ * bytes: nothing past the string is read, what is no number is refused,
+ * a result keeps within its bounds and a refusal leaves it unwritten.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../util/string.c"
+
+#define LEN_MAX 22 /* past the 20 digits of the largest __u64 */
+#define UNSET 0x5a5a5a5a5a5a5a5aULL
+
+static bool is_digit(char c, bool hex)
+{
+ return (c >= '0' && c <= '9') ||
+ (hex && ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')));
+}
+
+static bool all_digits(const char *s, size_t len, bool hex)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++)
+ if (!is_digit(s[i], hex))
+ return false;
+ return true;
+}
+
+static void unsigned_number(bool hex)
+{
+ __u64 max, out = UNSET;
+ size_t len;
+ char *s;
+ int ret;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ s = any_bytes(len);
+ ret = hex ? parse_x64(s, len, max, &out) :
+ parse_u64(s, len, max, &out);
+ __CPROVER_assert(!ret || ret == -EINVAL || ret == -ERANGE,
+ "only 0, -EINVAL or -ERANGE");
+ __CPROVER_assert((ret == -EINVAL) == (!len || !all_digits(s, len, hex)),
+ "-EINVAL for what is empty or holds a non-digit");
+ __CPROVER_assert(ret || out <= max, "a number within its bound");
+ __CPROVER_assert(!ret || out == UNSET, "nothing written on refusal");
+}
+
+static void signed_number(void)
+{
+ __s64 min, max, out = (__s64)UNSET;
+ bool neg, number;
+ size_t len;
+ char *s;
+ int ret;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ s = any_bytes(len);
+ ret = parse_s64(s, len, min, max, &out);
+ neg = len && s[0] == '-';
+ number = len > neg && all_digits(s + neg, len - neg, false);
+ __CPROVER_assert(!ret || ret == -EINVAL || ret == -ERANGE,
+ "only 0, -EINVAL or -ERANGE");
+ __CPROVER_assert((ret == -EINVAL) == !number,
+ "-EINVAL for what is no optionally signed number");
+ __CPROVER_assert(ret || (out >= min && out <= max),
+ "a number within its bounds");
+ __CPROVER_assert(ret || (neg ? out <= 0 : out >= 0),
+ "the sign the string spells");
+ __CPROVER_assert(!ret || out == (__s64)UNSET,
+ "nothing written on refusal");
+}
+
+static void leading_digits(void)
+{
+ size_t len, n;
+ char *s;
+
+ __CPROVER_assume(len <= LEN_MAX);
+ s = any_bytes(len);
+ n = digits(s, len);
+ __CPROVER_assert(n <= len && all_digits(s, n, false),
+ "a run of digits within the string");
+ __CPROVER_assert(n == len || !is_digit(s[n], false),
+ "the whole run");
+}
+
+void harness(void)
+{
+ unsigned_number(false);
+ unsigned_number(true);
+ signed_number();
+ leading_digits();
+}
diff --git a/testing/proofs/string-sanitize.c b/testing/proofs/string-sanitize.c
new file mode 100644
index 00000000..5ac8565e
--- /dev/null
+++ b/testing/proofs/string-sanitize.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * text_sanitize() of util/string.c on every text of LEN bytes, each
+ * length proofs.list names: what it leaves is well-formed UTF-8 without a
+ * control character but a kept line break, and it changes only what it
+ * must, to '?'.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../util/string.c"
+
+static bool in(__u8 c, __u8 lo, __u8 hi)
+{
+ return c >= lo && c <= hi;
+}
+
+/* Table 3-7 of the Unicode Standard, row by row. */
+static size_t table_width(const __u8 *s, size_t n)
+{
+ if (n >= 1 && s[0] <= 0x7f)
+ return 1;
+ if (n >= 2 && in(s[0], 0xc2, 0xdf) && in(s[1], 0x80, 0xbf))
+ return 2;
+ if (n >= 3 && in(s[2], 0x80, 0xbf) &&
+ ((s[0] == 0xe0 && in(s[1], 0xa0, 0xbf)) ||
+ (in(s[0], 0xe1, 0xec) && in(s[1], 0x80, 0xbf)) ||
+ (s[0] == 0xed && in(s[1], 0x80, 0x9f)) ||
+ (in(s[0], 0xee, 0xef) && in(s[1], 0x80, 0xbf))))
+ return 3;
+ if (n >= 4 && in(s[2], 0x80, 0xbf) && in(s[3], 0x80, 0xbf) &&
+ ((s[0] == 0xf0 && in(s[1], 0x90, 0xbf)) ||
+ (in(s[0], 0xf1, 0xf3) && in(s[1], 0x80, 0xbf)) ||
+ (s[0] == 0xf4 && in(s[1], 0x80, 0x8f))))
+ return 4;
+ return 0;
+}
+
+static bool control(const __u8 *s, size_t i, size_t len)
+{
+ return s[i] < 0x20 || s[i] == 0x7f ||
+ (s[i] == 0xc2 && i + 1 < len && in(s[i + 1], 0x80, 0x9f));
+}
+
+static bool line_break(const __u8 *s, size_t i, size_t len)
+{
+ return s[i] == '\t' || s[i] == '\n' ||
+ (s[i] == '\r' && i + 1 < len && s[i + 1] == '\n');
+}
+
+static void sanitized_text(void)
+{
+ const __u8 *t, *before;
+ size_t len, i, j, w;
+ char *s, *was;
+ bool lines;
+
+ len = LEN;
+ s = any_bytes(len);
+ was = any_bytes(len);
+ memcpy(was, s, len);
+ text_sanitize(s, len, lines);
+ t = (const __u8 *)s;
+ before = (const __u8 *)was;
+ for (i = 0; i < len; i++)
+ __CPROVER_assert(s[i] == '?' || s[i] == was[i],
+ "a byte kept or replaced by '?'");
+ for (i = 0; i < len; i += w) {
+ w = table_width(t + i, len - i);
+ __CPROVER_assert(w, "well-formed UTF-8 throughout");
+ if (!w)
+ break;
+ __CPROVER_assert(!control(t, i, len) ||
+ (lines && line_break(t, i, len)),
+ "no control character but a kept line break");
+ }
+ for (i = 0; i < len; i += w) {
+ w = table_width(before + i, len - i);
+ if (!w) {
+ w = 1;
+ continue;
+ }
+ if (control(before, i, len) &&
+ !(lines && line_break(before, i, len)))
+ continue;
+ for (j = i; j < i + w; j++)
+ __CPROVER_assert(s[j] == was[j],
+ "a printable character kept as it was");
+ }
+}
+
+void harness(void)
+{
+ sanitized_text();
+}
diff --git a/testing/proofs/string-utf8.c b/testing/proofs/string-utf8.c
new file mode 100644
index 00000000..2cf175c9
--- /dev/null
+++ b/testing/proofs/string-utf8.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * utf8_width() of util/string.c against Unicode's table of well-formed
+ * byte sequences, for every input of any length.
+ */
+
+#include <errno.h>
+#include <stdbool.h>
+
+#include "proof.h"
+#include "../../util/string.c"
+
+static bool in(__u8 c, __u8 lo, __u8 hi)
+{
+ return c >= lo && c <= hi;
+}
+
+/* Table 3-7 of the Unicode Standard, row by row. */
+static size_t table_width(const __u8 *s, size_t n)
+{
+ if (n >= 1 && s[0] <= 0x7f)
+ return 1;
+ if (n >= 2 && in(s[0], 0xc2, 0xdf) && in(s[1], 0x80, 0xbf))
+ return 2;
+ if (n >= 3 && in(s[2], 0x80, 0xbf) &&
+ ((s[0] == 0xe0 && in(s[1], 0xa0, 0xbf)) ||
+ (in(s[0], 0xe1, 0xec) && in(s[1], 0x80, 0xbf)) ||
+ (s[0] == 0xed && in(s[1], 0x80, 0x9f)) ||
+ (in(s[0], 0xee, 0xef) && in(s[1], 0x80, 0xbf))))
+ return 3;
+ if (n >= 4 && in(s[2], 0x80, 0xbf) && in(s[3], 0x80, 0xbf) &&
+ ((s[0] == 0xf0 && in(s[1], 0x90, 0xbf)) ||
+ (in(s[0], 0xf1, 0xf3) && in(s[1], 0x80, 0xbf)) ||
+ (s[0] == 0xf4 && in(s[1], 0x80, 0x8f))))
+ return 4;
+ return 0;
+}
+
+static void utf8_character(void)
+{
+ size_t n;
+ char *s;
+
+ s = any_bytes(n);
+ __CPROVER_assert(utf8_width(s, n) == table_width((const __u8 *)s, n),
+ "a well-formed sequence of Table 3-7, or 0");
+}
+
+void harness(void)
+{
+ utf8_character();
+}