eBPF-based security platform
testing: prove the path builders, chunk check and command matching
Daniel Borkmannc0a90858d482
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 1740846e..f227b2c7 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -63,9 +63,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- True exactly when the name's last bytes are the suffix and atleast one byte comes before them* - ``hex``- - ``hex_encode()``, ``hex_decode()``, ``hex_decode_max()``+ - ``hex_encode()``, ``hex_alloc()``, ``hex_decode()``,+ ``hex_decode_max()``- Up to 4 bytes, strings up to 9 characters- - Lowercase hex that decodes back to the input; a decode takes+ - Lowercase hex that decodes back to the input, in the caller's+ buffer or one ``hex_alloc()`` allocates; a decode takesexactly ``2 * n`` hex digits of either case and yields theirvalue, and ``hex_decode_max()`` any even count up to ``2 * cap``,reading no further than the digit after them@@ -74,6 +76,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Up to 8 bytes, strings up to 17 characters, counts and capacitiesup to 8 and past ``SIZE_MAX / 2``- The result and the bytes of the Lean model ``Shirudo.Hex``+ * - ``hex-digest``+ - ``sha256_digest_decode()``+ - Strings of 0, 6 to 8 and 70 to 72 bytes+ - Accepted exactly as ``sha256:`` and 64 hex digits of either case,+ into their value; a null pointer refused* - ``io-lines``- ``lines_len()``- Up to 8 bytes@@ -96,6 +103,20 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- A name without ``~`` maps to a pin name without a dot and back toitself; a pin name without a dot that decodes is the encoding ofwhat it decodes to+ * - ``paths-join``+ - ``path_join()``, ``format_key_path()``+ - Parts up to 2 bytes, every buffer size up to 7+ - The parts with one slash between them, terminated, exactly when+ they fit, else ``-ENAMETOOLONG``, with an empty string from+ ``path_join()``; no second slash after a key directory that ends+ in one, and ``-EINVAL`` without a buffer, a directory or a file+ * - ``paths-pin``+ - ``upatch_pin_path()``+ - A name and up to two parts of a byte each or none, buffers of 0,+ 1 and 28 to 35 bytes+ - The pin directory, the pin name and each part after one slash,+ terminated, exactly when they fit, else ``-ENAMETOOLONG`` with an+ empty string* - ``bpm-tar-octal``- ``bpm_tar_octal()``- Every field of 8 and 12 bytes@@ -538,6 +559,29 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Terminated printable ASCII, every backslash a ``\\`` or three octaldigits, decoding to the message or to a prefix cut betweencharacters+ * - ``serializer-chunk``+ - ``serial_chunk_validate()``+ - Every chunk+ - Accepted exactly as the chunk asked for, by index, count, page and+ cursors, of this invocation and format, naming no more bytes than+ a chunk carries; ``-EINVAL`` without a chunk or for invocation 0+ * - ``similar-distance``+ - ``edit_distance()``+ - Two strings of 0 to 3 bytes each, caps 1 to 3+ - The Levenshtein distance by its definition when it is within the+ cap, and more than the cap when it is not+ * - ``similar-suggest``+ - ``suggest()``+ - A word of 0 to 2 bytes, up to two commands of 0 to 2 bytes,+ capacities 0 to 2, with ``strlen()`` given the lengths+ - Commands of the table within the cap, each with its distance,+ nearest first, never more than the capacity, none nearer left out+ * - ``similar-resolve``+ - ``resolve()``+ - A word and up to three commands of 0 to 2 bytes, capacities 0 to 2+ - The first command that is the word, else the commands the word+ begins, else those that begin it, in table order, as many as the+ capacity takes and none written past itLean 4 proofs-------------@@ -755,13 +799,11 @@ a proof covers.- Why no proof reads them* - :src:`util/bpm_state.c`, :src:`util/bpm_tree_overlap.c`,:src:`util/bpm_tree_state.c`, :src:`util/conditions.c`,- :src:`util/config_attest.c`, :src:`util/paths.c`,- :src:`util/serializer.c`, :src:`util/similar.c`+ :src:`util/config_attest.c`- No proof is written yet. A harness could carry their logic, inwhole or in part: the package state and tree state readers, which- selectors of a tree can meet on one node, the status verdict, the- config history of an evidence document, the pin and key paths,- ``serial_chunk_validate()`` and the edit distance+ selectors of a tree can meet on one node, the status verdict and+ the config history of an evidence document* - :src:`tools/shirudo-build/bpm-build.c`,:src:`tools/shirudo-build/bpm-publish.c`,:src:`tools/shirudo-build/package.c`,@@ -827,6 +869,17 @@ The ``.upatch`` reader, the TPM structures and the package, index andtree readers are covered part by part, as the table shows; what joinsthose parts has no proof.+Of :src:`util/paths.c` and :src:`util/serializer.c` the proofs cover the+functions the table names. ``key_paths()``, which calls+``format_key_path()`` twice, ``keys_dir_resolve()`` and+``path_exists()``, and the serializer's lock and invocation number, which+go to the environment or the kernel, have none, and neither has the loop+of :src:`util/upatch_maps.c` that puts a page together from the chunks+``serial_chunk_validate()`` accepted. ``edit_distance()`` of+:src:`util/similar.c` is proven on strings of up to 3 bytes: at its+limit of 64 CBMC ends a run on the code as it is and none on the code+broken, so no proof of the limit is kept.+Within the ``.upatch`` reader, the proofs cover its parts but not theircomposition: ``upatch_parse()`` and ``upatch_open_memory()`` join theheader, the signed sections, the decoders and the policy labels thediff --git a/testing/proofs/cbmc/hex-digest.c b/testing/proofs/cbmc/hex-digest.cnew file mode 100644index 00000000..9a9a3549--- /dev/null+++ b/testing/proofs/cbmc/hex-digest.c@@ -0,0 +1,54 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * sha256_digest_decode() of util/hex.c for every string of LEN bytes: it+ * takes "sha256:" and exactly 64 hex digits of either case, yields their+ * value, and refuses a null pointer.+ */++#include <errno.h>+#include <stdbool.h>++#include "proof.h"+#include "../../../util/hex.c"++#define PREFIX 7+#define DIGITS 64++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;+}++void harness(void)+{+ bool tagged, hex = true;+ size_t i, len = LEN;+ __u8 out[32];+ char *s;+ int ret;++ __CPROVER_assert(sha256_digest_decode(NULL, out) == -EINVAL,+ "no string, no digest");+ s = any_bytes(len + 1);+ __CPROVER_assume(!s[len] && strnlen(s, len) == len);+ ret = sha256_digest_decode(s, out);+ tagged = len >= PREFIX && !memcmp(s, "sha256:", PREFIX);+ for (i = PREFIX; i < len; i++)+ if (digit(s[i]) < 0)+ hex = false;+ __CPROVER_assert(!ret || ret == -EINVAL, "0 or -EINVAL");+ __CPROVER_assert(!ret == (tagged && len == PREFIX + DIGITS && hex),+ "the tag and exactly 64 hex digits");+ for (i = 0; !ret && i < sizeof(out); i++)+ __CPROVER_assert(out[i] == digit(s[PREFIX + 2 * i]) * 16 ++ digit(s[PREFIX + 2 * i + 1]),+ "the value of each pair");+}diff --git a/testing/proofs/cbmc/hex.c b/testing/proofs/cbmc/hex.cindex bbe2226d..7268c986 100644--- a/testing/proofs/cbmc/hex.c+++ b/testing/proofs/cbmc/hex.c@@ -3,16 +3,17 @@* 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, a decode takes exactly- * 2 * n hex digits of either case and yields their value, and one of at- * most cap bytes any even count of them up to 2 * cap, reading no- * further than the digit after those.+ * lowercase hex that decodes back to its input, allocated or not, a+ * decode takes exactly 2 * n hex digits of either case and yields their+ * value, and one of at most cap bytes any even count of them up to+ * 2 * cap, reading no further than the digit after those.*/#include <errno.h>#include <stdbool.h>#include "proof.h"+#include "../../../util/malloc.c"#include "../../../util/hex.c"#define LEN_MAX 4@@ -48,6 +49,24 @@ static void encoded(void)"decodes back to its input");}+static void allocated(void)+{+ __u8 *in, *back;+ char *out;+ size_t n;++ __CPROVER_assume(n <= LEN_MAX);+ in = any_bytes(n);+ back = any_bytes(n);+ out = hex_alloc(in, n);+ if (!out)+ return;+ __CPROVER_assert(strlen(out) == 2 * n, "2 * n digits, terminated");+ __CPROVER_assert(!hex_decode(out, back, n) && !memcmp(back, in, n),+ "decodes back to its input");+ xfree(out, 2 * n + 1);+}+static void decoded(void){size_t len, n, i;@@ -116,6 +135,7 @@ static void decoded_max_unterminated(void)void harness(void){encoded();+ allocated();decoded();decoded_max();decoded_max_unterminated();diff --git a/testing/proofs/cbmc/paths-join.c b/testing/proofs/cbmc/paths-join.cnew file mode 100644index 00000000..dba232d3--- /dev/null+++ b/testing/proofs/cbmc/paths-join.c@@ -0,0 +1,98 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * path_join() and format_key_path() of util/paths.c on parts of up to+ * LEN_MAX bytes and every buffer size: a path is its parts with one slash+ * between them, terminated, exactly when it fits, and -ENAMETOOLONG+ * otherwise, with an empty string from path_join(); a key path takes no+ * second slash after a directory that ends in one.+ */++#include <errno.h>+#include <stdbool.h>++#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/paths.c"++#define LEN_MAX 2++static char *any_string(size_t *len)+{+ char *s;++ __CPROVER_assume(*len <= LEN_MAX);+ s = any_bytes(*len + 1);+ __CPROVER_assume(!s[*len] && strnlen(s, *len) == *len);+ return s;+}++/* Whether out holds part at off, and then what follows it. */+static size_t holds(const char *out, size_t off, const char *part,+ size_t len)+{+ __CPROVER_assert(!memcmp(out + off, part, len), "the part, as given");+ return off + len;+}++static void joined(void)+{+ size_t ld, ln, cap, off;+ char *dir, *name, *out;+ int ret;++ dir = any_string(&ld);+ name = any_string(&ln);+ __CPROVER_assume(cap <= 2 * LEN_MAX + 3);+ out = any_bytes(cap);+ ret = path_join(out, cap, dir, name);+ __CPROVER_assert(!ret || ret == -ENAMETOOLONG, "0 or -ENAMETOOLONG");+ __CPROVER_assert(!ret == (ld + 1 + ln < cap), "exactly when it fits");+ if (ret) {+ __CPROVER_assert(!cap || !out[0], "empty on refusal");+ return;+ }+ off = holds(out, 0, dir, ld);+ __CPROVER_assert(out[off++] == '/', "one slash");+ off = holds(out, off, name, ln);+ __CPROVER_assert(!out[off], "terminated");+}++static void key_path(void)+{+ size_t ld, lf, cap, off, sep;+ char *dir, *file, *out;+ bool valid;+ int ret;++ dir = any_string(&ld);+ file = any_string(&lf);+ __CPROVER_assume(cap <= 2 * LEN_MAX + 3);+ out = any_bytes(cap);+ ret = format_key_path(out, cap, dir, file);+ valid = cap && ld && lf;+ sep = ld && dir[ld - 1] != '/';+ __CPROVER_assert((ret == -EINVAL) == !valid,+ "a buffer, a directory and a file, or -EINVAL");+ __CPROVER_assert(!ret == (valid && ld + sep + lf < cap),+ "exactly when it fits");+ __CPROVER_assert(!ret || ret == -EINVAL || ret == -ENAMETOOLONG,+ "0, -EINVAL or -ENAMETOOLONG");+ if (ret)+ return;+ off = holds(out, 0, dir, ld);+ if (sep)+ __CPROVER_assert(out[off++] == '/', "one slash, not two");+ off = holds(out, off, file, lf);+ __CPROVER_assert(!out[off], "terminated");+ __CPROVER_assert(format_key_path(NULL, cap, dir, file) == -EINVAL &&+ format_key_path(out, cap, dir, NULL) == -EINVAL,+ "no buffer or no file");+}++void harness(void)+{+ joined();+ key_path();+}diff --git a/testing/proofs/cbmc/paths-pin.c b/testing/proofs/cbmc/paths-pin.cnew file mode 100644index 00000000..c0f4cdcf--- /dev/null+++ b/testing/proofs/cbmc/paths-pin.c@@ -0,0 +1,84 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * upatch_pin_path() of util/paths.c into a buffer of LEN bytes, for an+ * upatch name and up to two parts of a byte each or none: the pin+ * directory, the pin name and the parts with one slash before each,+ * terminated, exactly when they fit, and -ENAMETOOLONG with an empty+ * string otherwise.+ */++#include <errno.h>+#include <stdbool.h>++#include "proof.h"+#include "../../../util/string.c"+#include "../../../util/paths.c"++#define LEN_MAX 1++static char *any_string(size_t *len)+{+ char *s;++ __CPROVER_assume(*len <= LEN_MAX);+ s = any_bytes(*len + 1);+ __CPROVER_assume(!s[*len] && strnlen(s, *len) == *len);+ return s;+}++static size_t holds(const char *out, size_t off, const char *part,+ size_t len)+{+ __CPROVER_assert(!memcmp(out + off, part, len), "the part, as given");+ return off + len;+}++void harness(void)+{+ size_t ln, la, lb, lp, off, need, cap = LEN;+ char bpffs[2 * LEN_MAX + 1];+ char *name, *a, *b, *out;+ __u32 parts;+ int ret;++ name = any_string(&ln);+ a = any_string(&la);+ b = any_string(&lb);+ __CPROVER_assume(parts <= 2);+ out = any_bytes(cap);+ if (parts == 0)+ ret = upatch_pin_path(out, cap, name, NULL);+ else if (parts == 1)+ ret = upatch_pin_path(out, cap, name, a, NULL);+ else+ ret = upatch_pin_path(out, cap, name, a, b, NULL);+ __CPROVER_assert(!upatch_bpffs_name(bpffs, sizeof(bpffs), name),+ "a short name has a pin name");+ lp = strlen(bpffs);+ need = sizeof(BPFFS_UPATCHES_DIR) + lp;+ if (parts >= 1)+ need += 1 + la;+ if (parts == 2)+ need += 1 + lb;+ __CPROVER_assert(!ret || ret == -ENAMETOOLONG, "0 or -ENAMETOOLONG");+ __CPROVER_assert(!ret == (need < cap), "exactly when it fits");+ if (ret) {+ __CPROVER_assert(!cap || !out[0], "empty on refusal");+ return;+ }+ off = holds(out, 0, BPFFS_UPATCHES_DIR,+ sizeof(BPFFS_UPATCHES_DIR) - 1);+ __CPROVER_assert(out[off++] == '/', "a slash before the pin name");+ off = holds(out, off, bpffs, lp);+ if (parts >= 1) {+ __CPROVER_assert(out[off++] == '/', "a slash before a part");+ off = holds(out, off, a, la);+ }+ if (parts == 2) {+ __CPROVER_assert(out[off++] == '/', "a slash before a part");+ off = holds(out, off, b, lb);+ }+ __CPROVER_assert(!out[off], "terminated");+}diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex a4c092b5..2d8f5858 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -87,3 +87,10 @@ json-emit-string-model --unwind 5 --unwindset put_string_n.0:5,put_string_n.1:5,json-string-model --unwind 5 --unwindset parse_string.0:7,parse_string.1:7,model_read.0:7,memcmp.0:7 LEN=0-6json-escape-model --unwind 5 --unwindset parse_string.0:4,parse_string.1:4,model_read.0:4,memcmp.0:9bpm-archive-path-model --unwind 12 LEN=0-10+hex-digest --unwind 74 LEN=0,6-8,70-72+serializer-chunk -I ../../.. --unwind 1+paths-join --unwind 12+paths-pin --unwind 32 LEN=0,1,28-35+similar-distance --max-field-sensitivity-array-size 65 --unwind 8 --unwindset memcpy.0:18 LEN=0-15+similar-suggest --object-bits 10 --max-field-sensitivity-array-size 65 --unwind 8 --unwindset memcpy.0:14 LEN=0-8,18-26+similar-resolve --object-bits 10 --unwind 8diff --git a/testing/proofs/cbmc/serializer-chunk.c b/testing/proofs/cbmc/serializer-chunk.cnew file mode 100644index 00000000..ef572840--- /dev/null+++ b/testing/proofs/cbmc/serializer-chunk.c@@ -0,0 +1,46 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * serial_chunk_validate() of util/serializer.c for every chunk a map can+ * hold: one it accepts is the chunk asked for, of this invocation, and+ * names no more bytes than a chunk carries, so that a caller's copy of+ * bytes_in_chunk bytes stays inside it.+ */++#include <errno.h>++#include "proof.h"+#include "../../../util/serializer.c"++void harness(void)+{+ __u64 cursor, next_cursor, invocation_id;+ __u32 index, total, page;+ struct serial_chunk *c;+ int ret;++ __CPROVER_assert(serial_chunk_validate(NULL, index, total, page, cursor,+ next_cursor, invocation_id) ==+ -EINVAL, "no chunk");+ c = any_bytes(sizeof(*c));+ ret = serial_chunk_validate(c, index, total, page, cursor, next_cursor,+ invocation_id);+ __CPROVER_assert(!ret || ret == -EINVAL || ret == -EBADMSG,+ "0, -EINVAL or -EBADMSG");+ __CPROVER_assert((ret == -EINVAL) == !invocation_id,+ "an invocation is never 0");+ __CPROVER_assert(!ret == (invocation_id &&+ c->magic == SERIAL_MAGIC &&+ c->version == SERIAL_VERSION &&+ c->chunk_index == index &&+ c->total_chunks == total &&+ c->page_index == page &&+ c->cursor == cursor &&+ c->next_cursor == next_cursor &&+ c->invocation_id == invocation_id &&+ c->bytes_in_chunk <= SERIAL_CHUNK_PAYLOAD),+ "exactly the chunk asked for, of this invocation");+ __CPROVER_assert(ret || c->bytes_in_chunk <= sizeof(c->data),+ "its bytes lie inside the chunk");+}diff --git a/testing/proofs/cbmc/similar-distance.c b/testing/proofs/cbmc/similar-distance.cnew file mode 100644index 00000000..a602e7c2--- /dev/null+++ b/testing/proofs/cbmc/similar-distance.c@@ -0,0 +1,54 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * edit_distance() of util/similar.c on every two strings of LEN / 4 and+ * LEN % 4 bytes and every cap a caller passes: the Levenshtein distance+ * when it is within the cap, and more than the cap when it is not.+ */++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/similar.c"++#define M (LEN / 4)+#define N (LEN % 4)++static char *any_string(size_t len)+{+ char *s = any_bytes(len + 1);++ __CPROVER_assume(!s[len] && strnlen(s, len) == len);+ return s;+}++/* The distance by its definition: the cheapest of the three last edits. */+static int distance(const char *s, size_t m, const char *t, size_t n)+{+ int best, d;++ if (!m)+ return (int)n;+ if (!n)+ return (int)m;+ best = distance(s + 1, m - 1, t + 1, n - 1) + (s[0] != t[0]);+ d = distance(s + 1, m - 1, t, n) + 1;+ if (d < best)+ best = d;+ d = distance(s, m, t + 1, n - 1) + 1;+ if (d < best)+ best = d;+ return best;+}++void harness(void)+{+ char *s = any_string(M), *t = any_string(N);+ int max, got, want;++ __CPROVER_assume(max >= 1 && max <= 3);+ got = edit_distance(s, t, max);+ want = distance(s, M, t, N);+ __CPROVER_assert(want <= max ? got == want : got > max,+ "the distance, or more than the cap");+}diff --git a/testing/proofs/cbmc/similar-resolve.c b/testing/proofs/cbmc/similar-resolve.cnew file mode 100644index 00000000..823a8dcf--- /dev/null+++ b/testing/proofs/cbmc/similar-resolve.c@@ -0,0 +1,91 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * resolve() of util/similar.c on every word of up to LEN_MAX bytes+ * against every table of up to N_MAX such words: a word resolves to the+ * first command that is the word, else to the commands it begins, else+ * to those that begin it, in table order and never past the capacity.+ */++#include <stdbool.h>++#include "proof.h"+#include "../../../util/similar.c"++#define N_MAX 3+#define LEN_MAX 2+#define CAP_MAX 2++static const char *table[N_MAX];+static size_t lens[N_MAX];++static char *any_string(size_t *len)+{+ char *s;++ __CPROVER_assume(*len <= LEN_MAX);+ s = any_bytes(*len + 1);+ __CPROVER_assume(!s[*len] && strnlen(s, *len) == *len);+ return s;+}++static size_t any_table(void)+{+ size_t n, i;++ __CPROVER_assume(n <= N_MAX);+ for (i = 0; i < n; i++) {+ size_t len;++ table[i] = any_string(&len);+ lens[i] = len;+ }+ return n;+}++static bool begins(const char *word, size_t len, const char *with, size_t n)+{+ return n < len && !memcmp(word, with, n);+}++static void resolved(void)+{+ size_t n = any_table(), len, cap, k, i, tier;+ size_t exact = N_MAX, longer = 0, shorter = 0;+ char *input = any_string(&len);+ size_t *matches;++ __CPROVER_assume(cap <= CAP_MAX);+ matches = any_bytes(cap * sizeof(*matches));+ k = resolve(input, table, n, matches, cap);+ for (i = n; i-- > 0; ) {+ if (lens[i] == len && !memcmp(input, table[i], len))+ exact = i;+ longer += begins(table[i], lens[i], input, len);+ shorter += begins(input, len, table[i], lens[i]);+ }+ if (exact < N_MAX) {+ __CPROVER_assert(k == 1 && (!cap || matches[0] == exact),+ "the first command that is the word");+ return;+ }+ tier = longer ? longer : shorter;+ __CPROVER_assert(k == min(tier, cap),+ "the first tier that hits, up to the capacity");+ for (i = 0; i < k; i++) {+ size_t j = matches[i];++ __CPROVER_assert(j < n, "an index of the table");+ __CPROVER_assert(!i || matches[i - 1] < j, "in table order");+ __CPROVER_assert(longer ?+ begins(table[j], lens[j], input, len) :+ begins(input, len, table[j], lens[j]),+ "of that tier");+ }+}++void harness(void)+{+ resolved();+}diff --git a/testing/proofs/cbmc/similar-suggest.c b/testing/proofs/cbmc/similar-suggest.cnew file mode 100644index 00000000..e1461d09--- /dev/null+++ b/testing/proofs/cbmc/similar-suggest.c@@ -0,0 +1,72 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * suggest() of util/similar.c on a word and up to two commands whose+ * lengths LEN encodes: the suggestions are commands within the cap, each+ * with its distance, nearest first, within the capacity, none nearer left+ * out. strlen() is given the lengths to keep the loops' bounds concrete.+ */++#include <stdbool.h>++#define PROOF_MEMCPY_BYTEWISE+#include "proof.h"+#include "../../../util/similar.c"++#define N_MAX 2+#define CAP_MAX 2++static const size_t lens[N_MAX] = { LEN / 3 % 3, LEN / 9 };+static const char *table[N_MAX], *word;++static char *any_string(size_t len)+{+ char *s = any_bytes(len + 1);++ __CPROVER_assume(!s[len] && strnlen(s, len) == len);+ return s;+}++size_t strlen(const char *s)+{+ if (s == word)+ return LEN % 3;+ __CPROVER_assert(s == table[0] || s == table[1], "a string of ours");+ return lens[s == table[1]];+}++void harness(void)+{+ struct suggestion *out;+ size_t n, cap, k, i, j;+ int dist[N_MAX], max;++ word = any_string(LEN % 3);+ table[0] = any_string(lens[0]);+ table[1] = any_string(lens[1]);+ __CPROVER_assume(n <= N_MAX && cap <= CAP_MAX && max >= 1 && max <= 3);+ out = cap == 2 ? any_bytes(2 * sizeof(*out)) :+ cap == 1 ? any_bytes(sizeof(*out)) : any_bytes(0);+ k = suggest(word, table, n, max, out, cap);+ for (j = 0; j < n; j++)+ dist[j] = edit_distance(word, table[j], max);+ __CPROVER_assert(k <= cap && k <= n, "no more than fit, or exist");+ for (i = 0; i < k; i++) {+ for (j = 0; j < n && table[j] != out[i].name; j++)+ ;+ __CPROVER_assert(j < n, "a command of the table");+ __CPROVER_assert(out[i].dist == dist[j] && dist[j] <= max,+ "its distance, within the cap");+ __CPROVER_assert(!i || out[i - 1].dist <= out[i].dist,+ "nearest first");+ }+ for (j = 0; j < n; j++) {+ for (i = 0; i < k && out[i].name != table[j]; i++)+ ;+ __CPROVER_assert(dist[j] > max || i < k ||+ (k == cap &&+ (!cap || out[cap - 1].dist <= dist[j])),+ "none nearer is left out");+ }+}