eBPF-based security platform
testing: prove the JSON reader and writer and the OTLP headers
Daniel Borkmann6d7b1a9501a7
diff --git a/.agents/skills/proofs/SKILL.md b/.agents/skills/proofs/SKILL.mdindex 39c7da2d..7dfb597d 100644--- a/.agents/skills/proofs/SKILL.md+++ b/.agents/skills/proofs/SKILL.md@@ -75,6 +75,15 @@ to wait for.`--conversion-check`. Include the file between `#pragma CPROVER checkpush`, `#pragma CPROVER check disable "conversion"` and `#pragma CPROVERcheck pop`, and keep the check on everywhere else.+- A write at a symbolic index makes every later read of that array+ symbolic, and a heap object of symbolic size blows up memory: give an+ allocation a fixed size the harness bounds. A write through a pointer+ into a big struct becomes an update of all of it; index the member in+ place, as `sep()` of `util/json.c` does.+- `proof.h` models `vsnprintf()` for `%s` and `%d`, `%u`, `%x` with a zero+ flag, a width and `ll`, and fails a proof on any other conversion;+ CBMC does not promote variadic arguments, so the model reads each by+ its size.- A struct of tens of kilobytes, such as a tree operation, makes even afew symbolic copies slow: prove the logic that reads its fields on asmaller harness, or leave it to the unit cases.diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 17cff98c..e11f6fa3 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -188,6 +188,66 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Every encoding and every value of up to 8 bytes- The bits stay inside the integer's bytes; the value is zero- orsign-extended from them+ * - ``json-string``+ - ``parse_string()`` of :src:`util/json.c`+ - Lengths 0 to 6, every room up to one byte more+ - Accepts exactly the ECMA-404 strings without ``\u0000``, a lone+ surrogate or bytes that are not UTF-8, decoded as a reference+ decoder does; ``-ENOSPC`` exactly when the value does not fit;+ nothing written past its room+ * - ``json-escape``+ - ``\u`` escapes and surrogate pairs+ - Every pair of code units+ - A pair is its 4-byte UTF-8, a code unit its own; ``\u0000`` and+ a lone surrogate refused+ * - ``json-field``+ - ``parse_value()`` into an integer, string or boolean field+ - Values of 0 to 6 bytes+ - An integer field takes exactly an ECMA-404 integer at its value; a+ string fits its room; a boolean takes ``true`` or ``false``;+ ``null`` gives no value+ * - ``json-walk``+ - ``json_walk()``+ - Every document of up to 2 bytes, depth 2+ - Agrees with an independent ECMA-404 recognizer; containers in+ pairs, numbers whole with their integer flag+ * - ``json-limits``+ - The bounds of ``json_walk()`` and ``json_parse()``+ - Fixed documents at small bounds+ - Past the size or depth limit ``-E2BIG`` before any read; a+ callback's error ends the walk; a declared key named twice,+ null or not, refused+ * - ``json-emit-string``+ - ``put_string_n()`` of the JSON writer, strict and lossy+ - Strings of 0 to 4 bytes+ - Refuses exactly a NUL or bytes that are not UTF-8; writes one+ ECMA-404 string that decodes back to the input, or with U+FFFD in+ place of each NUL and stray byte+ * - ``json-roundtrip``+ - The writer and ``parse_string()``+ - Strings of 0 to 4 bytes+ - Every string the strict writer takes reads back whole+ * - ``json-emit-buffer``+ - ``put()``, ``grow()``+ - Texts of 0 to 4 bytes, every room and limit, allocations allowed+ to fail+ - Grows only up to its limit; fails exactly when the text does not+ fit, keeping the longest prefix that does, terminated+ * - ``json-emit-nest``+ - Every container and key writer+ - Every sequence of up to 4 calls, every depth+ - Valid JSON by a byte-level recognizer; a comma only after a+ member; ``-E2BIG`` past the deepest level+ * - ``otel-header-fields``+ - The OTLP header checks and ``percent_decode()``+ - Strings up to 32 bytes+ - Names exactly RFC 9110 tokens, values without controls but tab;+ each ``%XX`` decoded, a stray ``%`` and ``%00`` refused+ * - ``otel-retry-after``+ - ``otel_retry_after()``, ``response_header()``+ - Headers up to 32 bytes, dates modelled+ - A delay is at most 30 seconds, the value itself when shorter; a+ header line read only within itself* - ``tpm-reader``- The TPM reader primitives of :src:`util/tpm_wire.c`: ``get8()``to ``get64()``, ``get2b()``, ``get2b_fixed()``, ``get_name()``@@ -322,8 +382,9 @@ Not covered yet---------------The fuzz targets in :doc:`test-coverage` cover these parsers; no proof-does: the JSON parser and emitter, the package archive, index and-manifest readers, the tree publication and log, the eventlog's lines,+does: how ``json_parse()`` dispatches objects, arrays and maps to their+schemas, the OTLP headers a configuration adds, the package archive,+index and manifest readers, the tree publication and log, the eventlog's lines,the evidence document beyond its PCRs, the config update record, thesite configuration file, the config image's settings and tree, and thekernel configuration. The ``.upatch`` reader and the TPM structures arediff --git a/testing/proofs/cbmc/json-emit-buffer.c b/testing/proofs/cbmc/json-emit-buffer.cnew file mode 100644index 00000000..97385a95--- /dev/null+++ b/testing/proofs/cbmc/json-emit-buffer.c@@ -0,0 +1,62 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The output buffer of util/json.c for every text of LEN bytes written in+ * two calls: the caller's buffer and the emitter's growing one, which may+ * fail to grow, hold the text when it fits and its longest prefix when it+ * does not, with -ENOSPC, -E2BIG past max or -ENOMEM, and stay so.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++#define CAP_MAX (LEN + 3)++void harness(void)+{+ struct json_emit e = {};+ size_t cut = LEN / 2, cap, max, i;+ bool grows;+ char *t;++ t = any_bytes(LEN);+ __CPROVER_assume(cap <= CAP_MAX && max <= CAP_MAX);+ if (grows) {+ __CPROVER_assume(max && cap <= max);+ e.buf = cap ? xmalloc(cap) : NULL;+ __CPROVER_assume(!cap || e.buf);+ e.max = max;+ } else {+ e.buf = cap ? any_bytes(cap) : NULL;+ }+ e.cap = cap;+ e.value_pending = true;+ json_raw_value(&e, t, cut);+ e.value_pending = true;+ json_raw_value(&e, t + cut, LEN - cut);+ __CPROVER_assert(e.cap >= cap && e.cap <= (grows ? max : cap),+ "a buffer grown up to max alone");+ __CPROVER_assert(!e.err || e.err == (grows ? -E2BIG : -ENOSPC) ||+ (grows && e.err == -ENOMEM),+ "-ENOSPC, -E2BIG or -ENOMEM");+ __CPROVER_assert(e.err == -ENOMEM ||+ !e.err == (!LEN || LEN + 1 <= (grows ? max : cap)),+ "the text fails only when it does not fit");+ __CPROVER_assert(e.len == (e.err ? (e.cap ? e.cap - 1 : 0) : LEN),+ "the whole text or a buffer filled to the end");+ __CPROVER_assert(!e.len || e.len < e.cap, "inside the buffer");+ for (i = 0; i < e.len; i++)+ __CPROVER_assert(e.buf[i] == t[i], "a prefix of the text");+ __CPROVER_assert(!e.len || !e.buf[e.len], "terminated");+ cap = e.cap;+ cut = e.len;+ if (!e.err)+ return;+ e.value_pending = true;+ json_raw_value(&e, t, LEN);+ __CPROVER_assert(e.len == cut && e.cap == cap, "an error stays");+}diff --git a/testing/proofs/cbmc/json-emit-nest.c b/testing/proofs/cbmc/json-emit-nest.cnew file mode 100644index 00000000..ee4c75db--- /dev/null+++ b/testing/proofs/cbmc/json-emit-nest.c@@ -0,0 +1,315 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The containers of util/json.c: every sequence of up to CALLS calls that+ * json.h allows, closed at the end, writes one JSON text of ECMA-404, and+ * at every depth the emitter has, a container takes its commas and leaves+ * those of the others as they were.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++#define CALLS 4+#define OUT_MAX 56++enum state { VALUE, FIRST_VALUE, KEY, FIRST_KEY, COLON, NEXT, STRING,+ ESCAPE, HEX, SIGN, ZERO, DIGITS, WORD };++static bool hex_digit(__u8 c)+{+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||+ (c >= 'A' && c <= 'F');+}++static bool close_ok(char c, bool *array, size_t *depth, enum state *s)+{+ if (!*depth || c != (array[*depth - 1] ? ']' : '}'))+ return false;+ --*depth;+ *s = NEXT;+ return true;+}++/*+ * ECMA-404 as a pushdown automaton over bytes, for text without white+ * space, numbers with no fraction or exponent and ASCII strings: a subset+ * of the grammar, so what it takes is JSON.+ */+static bool json_text(const char *t, size_t n)+{+ bool array[CALLS + 1], key = false;+ size_t i, depth = 0, hex = 0;+ const char *word = "";+ enum state s = VALUE;+ __u8 c;++ for (i = 0; i < n; i++) {+ c = t[i];+ switch (s) {+ case STRING:+ if (c == '"')+ s = key ? COLON : NEXT;+ else if (c == '\\')+ s = ESCAPE;+ else if (c < 0x20 || c >= 0x80)+ return false;+ continue;+ case ESCAPE:+ if (c == 'u') {+ s = HEX;+ hex = 0;+ } else if (memchr("\"\\/bfnrt", c, 8)) {+ s = STRING;+ } else {+ return false;+ }+ continue;+ case HEX:+ if (!hex_digit(c))+ return false;+ if (++hex == 4)+ s = STRING;+ continue;+ case SIGN:+ if (c < '0' || c > '9')+ return false;+ s = c == '0' ? ZERO : DIGITS;+ continue;+ case DIGITS:+ if (c >= '0' && c <= '9')+ continue;+ s = NEXT;+ break;+ case ZERO:+ s = NEXT;+ break;+ case WORD:+ if (c != *word++)+ return false;+ if (!*word)+ s = NEXT;+ continue;+ case COLON:+ if (c != ':')+ return false;+ s = VALUE;+ continue;+ default:+ break;+ }+ if (s == NEXT) {+ if (c == ',' && depth) {+ s = array[depth - 1] ? VALUE : KEY;+ continue;+ }+ if (!close_ok(c, array, &depth, &s))+ return false;+ continue;+ }+ if ((s == FIRST_VALUE && c == ']') || (s == FIRST_KEY && c == '}')) {+ close_ok(c, array, &depth, &s);+ continue;+ }+ key = s == KEY || s == FIRST_KEY;+ if (key && c != '"')+ return false;+ if (c == '{' || c == '[') {+ if (depth == CALLS + 1)+ return false;+ array[depth++] = c == '[';+ s = c == '[' ? FIRST_VALUE : FIRST_KEY;+ } else if (c == '"') {+ s = STRING;+ } else if (c == '-') {+ s = SIGN;+ } else if (c >= '0' && c <= '9') {+ s = c == '0' ? ZERO : DIGITS;+ } else {+ word = c == 't' ? "rue" : c == 'f' ? "alse" :+ c == 'n' ? "ull" : NULL;+ if (!word)+ return false;+ s = WORD;+ }+ }+ return !depth && s == NEXT;+}++static void push(bool *array, size_t *depth, bool is_array)+{+ array[(*depth)++] = is_array;+}++static void value(struct json_emit *e, __u8 op, bool *array, size_t *depth)+{+ switch (op % 5) {+ case 0:+ json_obj_value_open(e);+ push(array, depth, false);+ return;+ case 1:+ json_arr_open_value(e);+ push(array, depth, true);+ return;+ case 2:+ json_str_value(e, "v");+ return;+ case 3:+ json_raw_value(e, "-1", 2);+ return;+ }+ json_str_lossy(e, "\xff", 1);+}++static void element(struct json_emit *e, __u8 op, bool *array,+ size_t *depth)+{+ switch (op % 4) {+ case 0:+ json_str(e, "w");+ return;+ case 1:+ json_raw(e, "true", 4);+ return;+ case 2:+ json_obj_open(e);+ push(array, depth, false);+ return;+ }+ value(e, op / 4, array, depth);+}++static void member(struct json_emit *e, __u8 op, bool *array, size_t *depth,+ bool *pending)+{+ switch (op % 9) {+ case 0:+ json_obj_open_key(e, "o");+ push(array, depth, false);+ return;+ case 1:+ json_arr_open(e, "a");+ push(array, depth, true);+ return;+ case 2:+ json_key(e, "k");+ *pending = true;+ return;+ case 3:+ json_key_lossy(e, "\x01", 1);+ *pending = true;+ return;+ case 4:+ json_kv_string(e, "s", "\"");+ return;+ case 5:+ json_kv_int(e, "i", -7);+ return;+ case 6:+ json_kv_bool(e, "b", op & 16);+ return;+ case 7:+ json_kv_null(e, "n");+ return;+ }+ json_kv_raw(e, "r", "[]", 2);+}++static void close_container(struct json_emit *e, bool *array,+ size_t *depth)+{+ if (array[--*depth])+ json_arr_close(e);+ else+ json_obj_close(e);+}++/* One call json.h allows where the emitter stands, as op picks it. */+static void step(struct json_emit *e, __u8 op, bool *array, size_t *depth,+ bool *pending)+{+ if (*pending) {+ *pending = false;+ value(e, op, array, depth);+ } else if (op % 8 == 7) {+ close_container(e, array, depth);+ } else if (array[*depth - 1]) {+ element(e, op / 8, array, depth);+ } else {+ member(e, op / 8, array, depth, pending);+ }+}++static void sequences(void)+{+ bool array[CALLS + 1], pending = false, root;+ struct json_emit e = {};+ size_t depth = 0, i;++ e.buf = any_bytes(OUT_MAX);+ e.cap = OUT_MAX;+ if (root)+ json_arr_open_value(&e);+ else+ json_obj_open(&e);+ push(array, &depth, root);+ for (i = 0; i < CALLS - 1 && depth; i++) {+ bool more;+ __u8 op;++ if (!more)+ break;+ step(&e, op, array, &depth, &pending);+ }+ if (pending)+ json_str_value(&e, "x");+ while (depth)+ close_container(&e, array, &depth);+ __CPROVER_assert(!e.err, "room enough");+ __CPROVER_assert(json_text(e.buf, e.len), "one JSON text");+}++/* A container at depth d, which a member before it may precede. */+static void any_depth(void)+{+ __u64 was[JSON_EMIT_MAX_DEPTH / 64];+ struct json_emit e = {};+ char out[16];+ __u32 d, w;+ bool had;++ __CPROVER_assume(d < JSON_EMIT_MAX_DEPTH);+ for (w = 0; w < ARRAY_SIZE(was); w++)+ e.nonempty[w] = was[w];+ e.buf = out;+ e.cap = sizeof(out);+ e.depth = d;+ had = was[d / 64] >> (d % 64) & 1;+ json_arr_open_value(&e);+ if (d + 1 == JSON_EMIT_MAX_DEPTH) {+ __CPROVER_assert(e.err == -E2BIG, "no deeper than the bitmap");+ return;+ }+ json_str_value(&e, "a");+ json_str_value(&e, "b");+ json_arr_close(&e);+ __CPROVER_assert(!e.err && e.depth == d, "back at depth d");+ __CPROVER_assert(!strcmp(out, ",[\"a\",\"b\"]" + !had),+ "a comma after a member alone");+ for (w = 0; w < ARRAY_SIZE(was); w++)+ __CPROVER_assert(e.nonempty[w] == (was[w] |+ (w == d / 64 ? 1ull << d % 64 : 0) |+ (w == (d + 1) / 64 ? 1ull << (d + 1) % 64 : 0)),+ "the bits of the other depths kept");+}++void harness(void)+{+ sequences();+ any_depth();+}diff --git a/testing/proofs/cbmc/json-emit-string.c b/testing/proofs/cbmc/json-emit-string.cnew file mode 100644index 00000000..61aa7769--- /dev/null+++ b/testing/proofs/cbmc/json-emit-string.c@@ -0,0 +1,178 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The string writer of util/json.c on every string of LEN bytes: it writes+ * one JSON string of ECMA-404 in well-formed UTF-8 that decodes to the+ * input, or lossy to it with U+FFFD for each NUL and ill-formed byte, and+ * strict refuses exactly the input that is no NUL-free well-formed UTF-8.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++#define OUT_MAX (6 * LEN + 3)++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 well_formed(const __u8 *s, size_t n)+{+ size_t i, w;++ for (i = 0; i < n; i += w) {+ w = table_width(s + i, n - i);+ if (!w)+ return false;+ }+ return true;+}++static int hex_value(__u8 c)+{+ if (in(c, '0', '9'))+ return c - '0';+ if (in(c, 'a', 'f'))+ return c - 'a' + 10;+ if (in(c, 'A', 'F'))+ return c - 'A' + 10;+ return -1;+}++/* A code point of the Basic Multilingual Plane as UTF-8, Table 3-6. */+static size_t bmp_utf8(__u32 cp, __u8 *out)+{+ if (cp < 0x80) {+ out[0] = cp;+ return 1;+ }+ if (cp < 0x800) {+ out[0] = 0xc0 + cp / 64;+ out[1] = 0x80 + cp % 64;+ return 2;+ }+ out[0] = 0xe0 + cp / 4096;+ out[1] = 0x80 + cp / 64 % 64;+ out[2] = 0x80 + cp % 64;+ return 3;+}++/*+ * The n bytes at t as one string of ECMA-404, section 9, with no+ * surrogate escape, decoded into out.+ */+static bool json_string(const __u8 *t, size_t n, __u8 *out, size_t *len)+{+ static const char plain[] = "\"\\/bfnrt", value[] = "\"\\/\b\f\n\r\t";+ size_t i = 1, k = 0, j;+ const char *q;+ __u32 cp;+ __u8 c;+ int h;++ if (n < 2 || t[0] != '"' || t[n - 1] != '"')+ return false;+ while (i < n - 1) {+ c = t[i++];+ if (c == '"' || c < 0x20)+ return false;+ if (c != '\\') {+ out[k++] = c;+ continue;+ }+ if (i == n - 1)+ return false;+ c = t[i++];+ q = memchr(plain, c, sizeof(plain) - 1);+ if (q) {+ out[k++] = value[q - plain];+ continue;+ }+ if (c != 'u' || n - 1 - i < 4)+ return false;+ for (cp = 0, j = 0; j < 4; j++) {+ h = hex_value(t[i++]);+ if (h < 0)+ return false;+ cp = cp * 16 + h;+ }+ if (cp >= 0xd800 && cp <= 0xdfff)+ return false;+ k += bmp_utf8(cp, out + k);+ }+ *len = k;+ return true;+}++/* The input with U+FFFD for each NUL and each byte no character starts. */+static size_t replaced(const __u8 *s, size_t n, __u8 *out)+{+ size_t i, k = 0, w;++ for (i = 0; i < n; i += w) {+ w = s[i] ? table_width(s + i, n - i) : 0;+ if (!w) {+ k += bmp_utf8(0xfffd, out + k);+ w = 1;+ continue;+ }+ memcpy(out + k, s + i, w);+ k += w;+ }+ return k;+}++void harness(void)+{+ __u8 want[3 * LEN + 1], got[OUT_MAX];+ struct json_emit e = {};+ size_t want_len, got_len;+ bool lossy, ok;+ const __u8 *s;++ s = any_bytes(LEN);+ e.buf = any_bytes(OUT_MAX);+ e.cap = OUT_MAX;+ put_string_n(&e, (const char *)s, LEN, lossy);+ ok = well_formed(s, LEN) && !memchr(s, '\0', LEN);+ __CPROVER_assert(e.err == (ok || lossy ? 0 : -EILSEQ),+ "strict refuses exactly NUL and ill-formed UTF-8");+ if (e.err)+ return;+ __CPROVER_assert(e.len < e.cap && !e.buf[e.len], "terminated output");+ __CPROVER_assert(well_formed((const __u8 *)e.buf, e.len),+ "well-formed UTF-8 output");+ __CPROVER_assert(json_string((const __u8 *)e.buf, e.len, got,+ &got_len),+ "one JSON string");+ want_len = lossy ? replaced(s, LEN, want) : LEN;+ __CPROVER_assert(got_len == want_len &&+ !memcmp(got, lossy ? want : s, want_len),+ "which decodes to the input");+}diff --git a/testing/proofs/cbmc/json-escape.c b/testing/proofs/cbmc/json-escape.cnew file mode 100644index 00000000..73664b10--- /dev/null+++ b/testing/proofs/cbmc/json-escape.c@@ -0,0 +1,98 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The \u escapes of util/json.c for every pair of code units, spelled in+ * either case: two characters, or one from a surrogate pair, become their+ * UTF-8, and \u0000 and a lone surrogate are refused.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++static bool in(__u32 c, __u32 lo, __u32 hi)+{+ return c >= lo && c <= hi;+}++static void spell(char *out, __u16 cu)+{+ int i;++ out[0] = '\\';+ out[1] = 'u';+ for (i = 0; i < 4; i++) {+ __u8 d = cu >> (12 - 4 * i) & 15;+ bool upper;++ out[2 + i] = d < 10 ? '0' + d : (upper ? 'A' : 'a') + d - 10;+ }+}++/* A scalar value as UTF-8, Table 3-6. */+static size_t utf8(__u32 cp, __u8 *out)+{+ if (cp < 0x80) {+ out[0] = cp;+ return 1;+ }+ if (cp < 0x800) {+ out[0] = 0xc0 + cp / 64;+ out[1] = 0x80 + cp % 64;+ return 2;+ }+ if (cp < 0x10000) {+ out[0] = 0xe0 + cp / 4096;+ out[1] = 0x80 + cp / 64 % 64;+ out[2] = 0x80 + cp % 64;+ return 3;+ }+ out[0] = 0xf0 + cp / 262144;+ out[1] = 0x80 + cp / 4096 % 64;+ out[2] = 0x80 + cp / 64 % 64;+ out[3] = 0x80 + cp % 64;+ return 4;+}++static bool character(__u16 cu)+{+ return cu && !in(cu, 0xd800, 0xdfff);+}++void harness(void)+{+ __u8 want[8], dst[8];+ struct pctx c = {};+ size_t want_len;+ char text[14];+ __u16 hi, lo;+ bool pair;+ int ret;++ text[0] = '"';+ spell(text + 1, hi);+ spell(text + 7, lo);+ text[13] = '"';+ c.p = text;+ c.end = text + sizeof(text);+ ret = parse_string(&c, (char *)dst, sizeof(dst));+ pair = in(hi, 0xd800, 0xdbff) && in(lo, 0xdc00, 0xdfff);+ if (!pair && !(character(hi) && character(lo))) {+ __CPROVER_assert(ret == -EINVAL,+ "\\u0000 and a lone surrogate refused");+ return;+ }+ if (pair) {+ want_len = utf8(0x10000 + (hi - 0xd800) * 1024 + lo - 0xdc00,+ want);+ } else {+ want_len = utf8(hi, want);+ want_len += utf8(lo, want + want_len);+ }+ __CPROVER_assert(!ret && c.p == c.end, "the string taken");+ __CPROVER_assert(!memcmp(dst, want, want_len) && !dst[want_len],+ "as the UTF-8 of its characters");+}diff --git a/testing/proofs/cbmc/json-field.c b/testing/proofs/cbmc/json-field.cnew file mode 100644index 00000000..09171e4a--- /dev/null+++ b/testing/proofs/cbmc/json-field.c@@ -0,0 +1,138 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The typed fields of util/json.c on every value of LEN bytes: an integer+ * field takes an integer of ECMA-404, a string field a string that fits,+ * a boolean field true or false, null leaves a field unseen, and anything+ * else is refused.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++#define STR_CAP 3++static bool in(__s64 c, __s64 lo, __s64 hi)+{+ return c >= lo && c <= hi;+}++static size_t digits_end(const __u8 *t, size_t n, size_t i)+{+ while (i < n && in(t[i], '0', '9'))+ i++;+ return i;+}++/* Past the integer part of ECMA-404, section 8, at t[i]; 0 if none. */+static size_t integer_end(const __u8 *t, size_t n, size_t i)+{+ if (i < n && t[i] == '-')+ i++;+ if (i < n && t[i] == '0')+ return i + 1;+ if (i < n && in(t[i], '1', '9'))+ return digits_end(t, n, i);+ return 0;+}++static bool word(const __u8 *t, size_t n, size_t i, const char *w)+{+ size_t k = strlen(w);++ return n - i >= k && !memcmp(t + i, w, k);+}++static __s64 decimal(const __u8 *t, size_t i, size_t end)+{+ bool neg = t[i] == '-';+ __s64 v = 0;++ for (i += neg; i < end; i++)+ v = v * 10 + t[i] - '0';+ return neg ? -v : v;+}++static void integer(const __u8 *t, size_t i)+{+ struct pctx c = { .p = (const char *)t, .end = (const char *)t + LEN };+ struct json_field f = { "k", JSON_T_INT, NULL, 0, 0, NULL, 0 };+ size_t end = integer_end(t, LEN, i);+ __s64 num = 0;+ int ret;++ f.dst = #+ ret = parse_value(&c, &f);+ if (word(t, LEN, i, "null")) {+ __CPROVER_assert(!ret && !f.seen, "null leaves it unseen");+ return;+ }+ if (!end || (end < LEN && (t[end] == '.' || t[end] == 'e' ||+ t[end] == 'E'))) {+ __CPROVER_assert(ret == -EINVAL, "no integer refused");+ return;+ }+ __CPROVER_assert(!ret && f.seen && num == decimal(t, i, end) &&+ c.p == (const char *)t + end, "an integer taken");+}++static void string(const __u8 *t, size_t i)+{+ struct pctx c = { .p = (const char *)t, .end = (const char *)t + LEN };+ struct json_field f = { "k", JSON_T_STRING, NULL, STR_CAP, 0, NULL,+ 0 };+ char str[STR_CAP];+ int ret;++ f.dst = str;+ ret = parse_value(&c, &f);+ if (word(t, LEN, i, "null")) {+ __CPROVER_assert(!ret && !f.seen, "null leaves it unseen");+ return;+ }+ if (i == LEN || t[i] != '"') {+ __CPROVER_assert(ret == -EINVAL, "no string refused");+ return;+ }+ __CPROVER_assert(!ret == f.seen && (!ret || ret == -EINVAL ||+ ret == -ENOSPC),+ "a string taken, or refused");+ __CPROVER_assert(ret || strnlen(str, STR_CAP) < STR_CAP,+ "a string that fits");+}++static void boolean(const __u8 *t, size_t i)+{+ struct pctx c = { .p = (const char *)t, .end = (const char *)t + LEN };+ struct json_field f = { "k", JSON_T_BOOL, NULL, 0, 0, NULL, 0 };+ bool yes = word(t, LEN, i, "true"), no = word(t, LEN, i, "false");+ int flag = -1, ret;++ f.dst = &flag;+ ret = parse_value(&c, &f);+ if (word(t, LEN, i, "null")) {+ __CPROVER_assert(!ret && !f.seen, "null leaves it unseen");+ return;+ }+ __CPROVER_assert(!ret == (yes || no) && (!ret || ret == -EINVAL),+ "true and false taken alone");+ __CPROVER_assert(ret || (f.seen && flag == yes), "as 1 and 0");+}++void harness(void)+{+ const __u8 *t;+ size_t i = 0;++ t = any_bytes(LEN);+ while (i < LEN && (t[i] == ' ' || t[i] == '\t' || t[i] == '\n' ||+ t[i] == '\r'))+ i++;+ integer(t, i);+ string(t, i);+ boolean(t, i);+}diff --git a/testing/proofs/cbmc/json-limits.c b/testing/proofs/cbmc/json-limits.cnew file mode 100644index 00000000..3e4c4030--- /dev/null+++ b/testing/proofs/cbmc/json-limits.c@@ -0,0 +1,132 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The limits of json_walk() and json_parse() in util/json.c, at the+ * JSON_MAX_BYTES and JSON_MAX_DEPTH proofs.list sets: a longer input and+ * deeper nesting are refused, a callback's error ends the walk at whichever+ * callback, and a declared key takes one value.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++#define DEEP (JSON_MAX_DEPTH + 1)++struct walk {+ size_t calls;+ size_t fail_at;+};++static int called(void *ctx)+{+ struct walk *w = ctx;++ return w->calls++ == w->fail_at ? -EIO : 0;+}++static int on_open(void *ctx, bool array)+{+ return called(ctx);+}++static int on_close(void *ctx, bool array)+{+ return called(ctx);+}++static int on_string(void *ctx, const char *s)+{+ return called(ctx);+}++static int on_number(void *ctx, const char *text, size_t len, bool integer)+{+ return called(ctx);+}++static int on_literal(void *ctx, int value)+{+ return called(ctx);+}++static const struct json_visitor visitor = {+ .open = on_open,+ .close = on_close,+ .key = on_string,+ .string = on_string,+ .number = on_number,+ .literal = on_literal,+};++static void stopped(const char *doc, size_t calls)+{+ struct walk w = {};+ size_t fail_at;+ int ret;++ __CPROVER_assume(fail_at <= calls);+ w.fail_at = fail_at;+ ret = json_walk(doc, strlen(doc), &visitor, &w);+ __CPROVER_assert(ret == (fail_at < calls ? -EIO : 0) &&+ w.calls == min(fail_at + 1, calls),+ "a callback's error ends the walk");+}++static void nested(size_t depth)+{+ struct walk w = { .fail_at = SIZE_MAX };+ char doc[2 * DEEP];+ size_t i;++ for (i = 0; i < depth; i++) {+ doc[i] = '[';+ doc[2 * depth - 1 - i] = ']';+ }+ __CPROVER_assert(json_walk(doc, 2 * depth, &visitor, &w) ==+ (depth > JSON_MAX_DEPTH ? -E2BIG : 0),+ "JSON_MAX_DEPTH levels at most");+}++/* A declared key named twice, null or not, as json.h says. */+static void twice(void)+{+ struct json_field f = { "k", JSON_T_INT, NULL, 0, 0, NULL, 0 };+ __s64 num = 0;++ f.dst = #+ __CPROVER_assert(json_parse("{\"k\":1,\"k\":2}", 13, &f, 1) == -EINVAL,+ "a second value refused");+ __CPROVER_assert(json_parse("{\"k\":1,\"k\":null}", 16, &f, 1) ==+ -EINVAL, "a null after a value refused");+ __CPROVER_assert(json_parse("{\"k\":null,\"k\":2}", 16, &f, 1) ==+ -EINVAL, "a value after null refused");+ __CPROVER_assert(!json_parse("{\"k\":null}", 10, &f, 1) && !f.seen,+ "null alone gives no value");+ __CPROVER_assert(!json_parse("{}", 2, &f, 1) && !f.seen,+ "seen reset per document");+}++void harness(void)+{+ struct walk w = { .fail_at = SIZE_MAX };+ struct json_field f = {};+ char *data;++ data = any_bytes(JSON_MAX_BYTES + 1);+ __CPROVER_assert(json_walk(data, JSON_MAX_BYTES + 1, &visitor, &w) ==+ -E2BIG && !w.calls, "past JSON_MAX_BYTES unread");+ __CPROVER_assert(json_parse(data, JSON_MAX_BYTES + 1, &f, 1) ==+ -E2BIG, "a document too");+ __CPROVER_assert(json_walk(data, 1, NULL, NULL) == -EINVAL,+ "a visitor needed");+ stopped("{\"\":1}", 4);+ stopped("[\"\"]", 3);+ stopped("[true]", 3);+ nested(JSON_MAX_DEPTH);+ nested(DEEP);+ twice();+}diff --git a/testing/proofs/cbmc/json-roundtrip.c b/testing/proofs/cbmc/json-roundtrip.cnew file mode 100644index 00000000..21c772b3--- /dev/null+++ b/testing/proofs/cbmc/json-roundtrip.c@@ -0,0 +1,34 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * util/json.c reads back what it writes: every string of LEN bytes the+ * writer takes, the reader takes whole and decodes to the same bytes.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++void harness(void)+{+ char out[6 * LEN + 3], back[LEN + 1];+ struct json_emit e = {};+ struct pctx c = {};+ const char *s;++ s = any_bytes(LEN);+ e.buf = out;+ e.cap = sizeof(out);+ put_string_n(&e, s, LEN, false);+ if (e.err)+ return;+ c.p = out;+ c.end = out + e.len;+ __CPROVER_assert(!parse_string(&c, back, sizeof(back)) && c.p == c.end,+ "read back whole");+ __CPROVER_assert(!memcmp(back, s, LEN) && !back[LEN],+ "to the same bytes");+}diff --git a/testing/proofs/cbmc/json-string.c b/testing/proofs/cbmc/json-string.cnew file mode 100644index 00000000..f728d4ef--- /dev/null+++ b/testing/proofs/cbmc/json-string.c@@ -0,0 +1,181 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The string reader of util/json.c on every input of LEN bytes and every+ * buffer of up to LEN bytes: it takes exactly a string of ECMA-404 under+ * json.h's restrictions, decodes it into the buffer when it fits and+ * reports -ENOSPC when not, and writes nothing past the buffer.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++static bool in(__s64 c, __s64 lo, __s64 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 int hex_value(__u8 c)+{+ if (in(c, '0', '9'))+ return c - '0';+ if (in(c, 'a', 'f'))+ return c - 'a' + 10;+ if (in(c, 'A', 'F'))+ return c - 'A' + 10;+ return -1;+}++/* The code unit of \uXXXX at t[i], or -1. */+static int code_unit(const __u8 *t, size_t n, size_t i)+{+ int cu = 0, h, j;++ if (i + 6 > n || t[i] != '\\' || t[i + 1] != 'u')+ return -1;+ for (j = 2; j < 6; j++) {+ h = hex_value(t[i + j]);+ if (h < 0)+ return -1;+ cu = cu * 16 + h;+ }+ return cu;+}++/* A scalar value as UTF-8, Table 3-6. */+static size_t utf8(__u32 cp, __u8 *out)+{+ if (cp < 0x80) {+ out[0] = cp;+ return 1;+ }+ if (cp < 0x800) {+ out[0] = 0xc0 + cp / 64;+ out[1] = 0x80 + cp % 64;+ return 2;+ }+ if (cp < 0x10000) {+ out[0] = 0xe0 + cp / 4096;+ out[1] = 0x80 + cp / 64 % 64;+ out[2] = 0x80 + cp % 64;+ return 3;+ }+ out[0] = 0xf0 + cp / 262144;+ out[1] = 0x80 + cp / 4096 % 64;+ out[2] = 0x80 + cp / 64 % 64;+ out[3] = 0x80 + cp % 64;+ return 4;+}++/*+ * The string of ECMA-404, section 9, that t starts with, decoded into out,+ * and its length in *end: no \u0000, no lone surrogate escape and nothing+ * but well-formed UTF-8 between the quotes.+ */+static bool json_string(const __u8 *t, size_t n, __u8 *out, size_t *len,+ size_t *end)+{+ static const char plain[] = "\"\\/bfnrt", value[] = "\"\\/\b\f\n\r\t";+ size_t i = 1, k = 0, w;+ const char *q;+ int cu, lo;++ if (!n || t[0] != '"')+ return false;+ while (i < n && t[i] != '"') {+ if (t[i] < 0x20)+ return false;+ if (t[i] != '\\') {+ w = table_width(t + i, n - i);+ if (!w)+ return false;+ memcpy(out + k, t + i, w);+ k += w;+ i += w;+ continue;+ }+ q = i + 1 < n ? memchr(plain, t[i + 1], sizeof(plain) - 1) : NULL;+ if (q) {+ out[k++] = value[q - plain];+ i += 2;+ continue;+ }+ cu = code_unit(t, n, i);+ if (cu <= 0 || in(cu, 0xdc00, 0xdfff))+ return false;+ i += 6;+ if (in(cu, 0xd800, 0xdbff)) {+ lo = code_unit(t, n, i);+ if (!in(lo, 0xdc00, 0xdfff))+ return false;+ cu = 0x10000 + (cu - 0xd800) * 1024 + lo - 0xdc00;+ i += 6;+ }+ k += utf8(cu, out + k);+ }+ if (i == n)+ return false;+ *len = k;+ *end = i + 1;+ return true;+}++void harness(void)+{+ char buf[LEN + 1], was[LEN + 1], *dst;+ size_t cap, want_len, end, i;+ __u8 want[LEN + 1];+ struct pctx c = {};+ const char *s;+ bool discard;+ int ret;++ s = any_bytes(LEN);+ c.p = s;+ c.end = s + LEN;+ __CPROVER_assume(cap >= 1 && cap <= sizeof(buf));+ memcpy(was, buf, sizeof(buf));+ dst = discard ? NULL : buf;+ ret = parse_string(&c, dst, cap);+ for (i = cap; i < sizeof(buf); i++)+ __CPROVER_assert(buf[i] == was[i], "nothing written past cap");+ if (!json_string((const __u8 *)s, LEN, want, &want_len, &end)) {+ __CPROVER_assert(ret == -EINVAL || (!discard && ret == -ENOSPC),+ "no string refused");+ return;+ }+ __CPROVER_assert(!ret == (discard || want_len < cap) &&+ (!ret || ret == -ENOSPC),+ "a string taken when it fits");+ if (ret)+ return;+ __CPROVER_assert(c.p == s + end, "and nothing past it");+ if (!discard)+ __CPROVER_assert(!memcmp(dst, want, want_len) &&+ !dst[want_len], "decoded and terminated");+}diff --git a/testing/proofs/cbmc/json-walk.c b/testing/proofs/cbmc/json-walk.cnew file mode 100644index 00000000..4502996d--- /dev/null+++ b/testing/proofs/cbmc/json-walk.c@@ -0,0 +1,305 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * json_walk() of util/json.c on every input of LEN bytes: it takes exactly+ * one value of ECMA-404 under json.h's restrictions, as a pushdown+ * automaton of the grammar decides, reports containers in pairs, strings+ * terminated and numbers as their whole text.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"++enum want { VALUE, FIRST_KEY, KEY, COLON, FIRST_VALUE, NEXT, END };++static bool in(__s64 c, __s64 lo, __s64 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 hex_digit(__u8 c)+{+ return in(c, '0', '9') || in(c, 'a', 'f') || in(c, 'A', 'F');+}++/* The code unit of \uXXXX at t[i], or -1. */+static int code_unit(const __u8 *t, size_t n, size_t i)+{+ int cu = 0, j;++ if (i + 6 > n || t[i] != '\\' || t[i + 1] != 'u')+ return -1;+ for (j = 2; j < 6; j++) {+ if (!hex_digit(t[i + j]))+ return -1;+ cu = cu * 16 + (t[i + j] <= '9' ? t[i + j] - '0' :+ (t[i + j] | 0x20) - 'a' + 10);+ }+ return cu;+}++/*+ * Past the string of ECMA-404, section 9, at t[i], with no \u0000, no lone+ * surrogate escape and nothing but well-formed UTF-8; 0 if none.+ */+static size_t string_end(const __u8 *t, size_t n, size_t i)+{+ size_t w;+ int cu;++ if (t[i++] != '"')+ return 0;+ while (i < n && t[i] != '"') {+ if (t[i] < 0x20)+ return 0;+ if (t[i] != '\\') {+ w = table_width(t + i, n - i);+ if (!w)+ return 0;+ i += w;+ continue;+ }+ if (i + 1 < n && memchr("\"\\/bfnrt", t[i + 1], 8)) {+ i += 2;+ continue;+ }+ cu = code_unit(t, n, i);+ if (cu <= 0 || in(cu, 0xdc00, 0xdfff))+ return 0;+ i += 6;+ if (!in(cu, 0xd800, 0xdbff))+ continue;+ if (!in(code_unit(t, n, i), 0xdc00, 0xdfff))+ return 0;+ i += 6;+ }+ return i < n ? i + 1 : 0;+}++static size_t digits_end(const __u8 *t, size_t n, size_t i)+{+ while (i < n && in(t[i], '0', '9'))+ i++;+ return i;+}++/* Past the number of ECMA-404, section 8, at t[i]; 0 if none. */+static size_t number_end(const __u8 *t, size_t n, size_t i)+{+ size_t j;++ if (i < n && t[i] == '-')+ i++;+ if (i < n && t[i] == '0')+ i++;+ else if (i < n && in(t[i], '1', '9'))+ i = digits_end(t, n, i);+ else+ return 0;+ if (i < n && t[i] == '.') {+ j = digits_end(t, n, i + 1);+ if (j == i + 1)+ return 0;+ i = j;+ }+ if (i < n && (t[i] == 'e' || t[i] == 'E')) {+ i += i + 1 < n && (t[i + 1] == '+' || t[i + 1] == '-');+ j = digits_end(t, n, i + 1);+ if (j == i + 1)+ return 0;+ i = j;+ }+ return i;+}++static size_t literal_end(const __u8 *t, size_t n, size_t i)+{+ static const char *const words[] = { "true", "false", "null" };+ size_t w, k;++ for (w = 0; w < ARRAY_SIZE(words); w++) {+ k = strlen(words[w]);+ if (n - i >= k && !memcmp(t + i, words[w], k))+ return i + k;+ }+ return 0;+}++static bool space(__u8 c)+{+ return c == ' ' || c == '\t' || c == '\n' || c == '\r';+}++/*+ * The grammar of ECMA-404 as a pushdown automaton: 0 for one value with+ * white space around it, -E2BIG for a container past JSON_MAX_DEPTH and+ * -EINVAL for anything else.+ */+static int json_text(const __u8 *t, size_t n)+{+ bool array[JSON_MAX_DEPTH];+ size_t i = 0, depth = 0;+ enum want want = VALUE;+ __u8 c;++ for (;;) {+ while (i < n && space(t[i]))+ i++;+ if (i == n)+ return want == END ? 0 : -EINVAL;+ c = t[i];+ if (want == END || (want == COLON && c != ':'))+ return -EINVAL;+ if (want == COLON) {+ want = VALUE;+ i++;+ continue;+ }+ if ((want == NEXT && c == ',') || (want == FIRST_KEY && c == '}') ||+ (want == FIRST_VALUE && c == ']') ||+ (want == NEXT && c == (array[depth - 1] ? ']' : '}'))) {+ if (c == ',')+ want = array[depth - 1] ? VALUE : KEY;+ else+ want = --depth ? NEXT : END;+ i++;+ continue;+ }+ if (want == NEXT)+ return -EINVAL;+ if (want == FIRST_KEY || want == KEY) {+ i = string_end(t, n, i);+ if (!i)+ return -EINVAL;+ want = COLON;+ continue;+ }+ if (c == '{' || c == '[') {+ if (depth == JSON_MAX_DEPTH)+ return -E2BIG;+ array[depth++] = c == '[';+ want = c == '[' ? FIRST_VALUE : FIRST_KEY;+ i++;+ continue;+ }+ if (c == '"')+ i = string_end(t, n, i);+ else if (c == '-' || in(c, '0', '9'))+ i = number_end(t, n, i);+ else+ i = literal_end(t, n, i);+ if (!i)+ return -EINVAL;+ want = depth ? NEXT : END;+ }+}++struct walk {+ const char *data;+ bool array[LEN + 1];+ size_t depth;+ size_t calls;+};++static int called(struct walk *w)+{+ w->calls++;+ return 0;+}++static int on_open(void *ctx, bool array)+{+ struct walk *w = ctx;++ __CPROVER_assert(w->depth < JSON_MAX_DEPTH, "within the depth");+ w->array[w->depth++] = array;+ return called(w);+}++static int on_close(void *ctx, bool array)+{+ struct walk *w = ctx;++ __CPROVER_assert(w->depth && w->array[w->depth - 1] == array,+ "the container last opened closed");+ w->depth--;+ return called(w);+}++static int on_string(void *ctx, const char *s)+{+ __CPROVER_assert(strnlen(s, LEN + 1) <= LEN, "a terminated string");+ return called(ctx);+}++static int on_number(void *ctx, const char *text, size_t len, bool integer)+{+ struct walk *w = ctx;+ size_t at = text - w->data;++ __CPROVER_assert(text >= w->data && at + len <= LEN,+ "a number inside the input");+ __CPROVER_assert(len && number_end((const __u8 *)w->data, LEN, at) ==+ at + len, "its whole text");+ __CPROVER_assert(integer == !(memchr(text, '.', len) ||+ memchr(text, 'e', len) ||+ memchr(text, 'E', len)),+ "an integer without fraction and exponent");+ return called(w);+}++static int on_literal(void *ctx, int value)+{+ __CPROVER_assert(value >= -1 && value <= 1, "true, false or null");+ return called(ctx);+}++static const struct json_visitor visitor = {+ .open = on_open,+ .close = on_close,+ .key = on_string,+ .string = on_string,+ .number = on_number,+ .literal = on_literal,+};++void harness(void)+{+ struct walk w = {};+ char *data;+ int ret;++ data = any_bytes(LEN);+ w.data = data;+ ret = json_walk(data, LEN, &visitor, &w);+ __CPROVER_assert(ret == json_text((const __u8 *)data, LEN),+ "exactly one value of ECMA-404 taken");+ __CPROVER_assert(ret || !w.depth, "every container closed");+ __CPROVER_assert(json_walk(data, LEN, NULL, NULL) == -EINVAL,+ "a visitor needed");+}diff --git a/testing/proofs/cbmc/otel-header-fields.c b/testing/proofs/cbmc/otel-header-fields.cnew file mode 100644index 00000000..276ed0c2--- /dev/null+++ b/testing/proofs/cbmc/otel-header-fields.c@@ -0,0 +1,96 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The header field checks of util/upatch_maps_otel.c on every string of up+ * to LEN bytes: a name is a token of RFC 9110, a value holds no control+ * character but tab, and percent-decoding turns each %XX but %00 into its+ * byte in place, refusing any other '%'.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"+#include "../../../util/upatch_maps_otel.c"++/* RFC 9110, 5.6.2. */+static bool tchar(__u8 c)+{+ switch (c) {+ case '!': case '#': case '$': case '%': case '&': case '\'': case '*':+ case '+': case '-': case '.': case '^': case '_': case '`': case '|':+ case '~':+ return true;+ }+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||+ (c >= 'A' && c <= 'Z');+}++static int hex_value(__u8 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 name_and_value(const char *s, size_t len)+{+ bool token = len, field = true;+ size_t i;++ for (i = 0; i < len; i++) {+ token = token && tchar(s[i]);+ field = field && (s[i] == '\t' ||+ ((__u8)s[i] >= 0x20 && s[i] != 0x7f));+ }+ __CPROVER_assert(header_name_ok(s) == token, "a token");+ __CPROVER_assert(header_value_ok(s) == field,+ "no control character but tab");+}++static void percent(char *s, size_t len)+{+ size_t i = 0, k = 0;+ char want[LEN + 1];+ bool ok = true;+ int hi, lo;++ while (i < len) {+ if (s[i] != '%') {+ want[k++] = s[i++];+ continue;+ }+ hi = i + 1 < len ? hex_value(s[i + 1]) : -1;+ lo = i + 2 < len ? hex_value(s[i + 2]) : -1;+ if (hi < 0 || lo < 0 || !(hi * 16 + lo)) {+ ok = false;+ break;+ }+ want[k++] = hi * 16 + lo;+ i += 3;+ }+ if (percent_decode(s)) {+ __CPROVER_assert(!ok, "a stray '%' or %00 refused");+ return;+ }+ __CPROVER_assert(ok && !memcmp(s, want, k) && !s[k],+ "each %XX decoded in place");+}++void harness(void)+{+ size_t len;+ char *s;++ s = any_bytes(LEN + 1);+ s[LEN] = '\0';+ len = strlen(s);+ name_and_value(s, len);+ percent(s, len);+}diff --git a/testing/proofs/cbmc/otel-retry-after.c b/testing/proofs/cbmc/otel-retry-after.cnew file mode 100644index 00000000..ce6acbb4--- /dev/null+++ b/testing/proofs/cbmc/otel-retry-after.c@@ -0,0 +1,102 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The Retry-After parser of util/upatch_maps_otel.c on every header line+ * of up to LEN bytes and every value it holds: a delay of digits is read+ * exactly, up to 30 seconds, and whatever date strptime() makes of the+ * rest, the wait is at most 30 seconds and nothing past the line is read.+ */++#include "proof.h"+#include "../../../util/malloc.c"+#include "../../../util/string.c"+#include "../../../util/hex.c"+#include "../../../util/json.c"+#include "../../../util/upatch_maps_otel.c"++#define YEAR_10000 253402300800LL+#define YEAR_0 (-62167219200LL)++/* Any prefix of the date taken, or none, into any broken-down time. */+char *strptime(const char *s, const char *format, struct tm *tm)+{+ bool fails;+ size_t n;++ __CPROVER_assume(n <= strlen(s));+ return fails ? NULL : (char *)s + n;+}++/* Any time the four-digit years of strptime() reach, or -1. */+time_t timegm(struct tm *tm)+{+ time_t t;++ __CPROVER_assume(t >= YEAR_0 && t < YEAR_10000);+ return t;+}++/* Now, before the year 10000, or -1 for a clock it cannot read. */+time_t time(time_t *out)+{+ time_t t;++ __CPROVER_assume(t >= -1 && t < YEAR_10000);+ if (out)+ *out = t;+ return t;+}++static void delay(void)+{+ __u64 seconds = 31, want = 0;+ size_t len, i;+ bool digits;+ time_t now;+ char *s;+ int ret;++ s = any_bytes(LEN + 1);+ s[LEN] = '\0';+ len = strlen(s);+ __CPROVER_assume(now >= -1 && now < YEAR_10000);+ digits = len;+ for (i = 0; i < len; i++) {+ digits = digits && s[i] >= '0' && s[i] <= '9';+ if (digits)+ want = min(want * 10 + s[i] - '0', 31U);+ }+ ret = otel_retry_after(s, now, &seconds);+ __CPROVER_assert(!ret || ret == -EINVAL,+ "a delay, a date or -EINVAL");+ __CPROVER_assert(len || ret == -EINVAL, "no empty value");+ __CPROVER_assert(ret || seconds <= 30, "at most 30 seconds");+ if (digits)+ __CPROVER_assert(!ret && seconds == min(want, 30U),+ "the delay given");+ __CPROVER_assert(otel_retry_after(NULL, now, &seconds) == -EINVAL,+ "a value needed");+}++static void header_line(void)+{+ struct otel_response *r;+ char *line;+ size_t n;++ r = any_bytes(sizeof(*r));+ r->retry_after_valid = false;+ line = any_bytes(LEN);+ __CPROVER_assume(n <= LEN);+ __CPROVER_assert(response_header(line, 1, n, r) == n,+ "the whole line taken");+ __CPROVER_assert(!r->retry_after_valid || r->retry_after <= 30,+ "a wait of at most 30 seconds");+}++void harness(void)+{+ delay();+ header_line();+}diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.hindex 851ff15e..ecda1d4d 100644--- a/testing/proofs/cbmc/proof.h+++ b/testing/proofs/cbmc/proof.h@@ -9,14 +9,17 @@#ifndef SHIRUDO_PROOF_H#define SHIRUDO_PROOF_H+#include <stdarg.h>+#include <stdbool.h>#include <stddef.h>+#include <stdio.h>#include <string.h>#include <shirudo/types.h>/*- * CBMC models neither strnlen(), memchr() nor strstr(), and a function- * without a body returns anything.+ * CBMC models neither strnlen(), memchr(), strstr() nor vsnprintf(), and a+ * function without a body returns anything.*/size_t strnlen(const char *s, size_t max){@@ -71,6 +74,106 @@ char *strstr(const char *s, const char *needle)}}+static size_t format_char(char *buf, size_t size, size_t n, char c)+{+ if (n + 1 < size)+ buf[n] = c;+ return n + 1;+}++static size_t format_number(char *buf, size_t size, size_t n, __u64 v,+ bool neg, __u32 base, size_t width, bool zero)+{+ size_t k = 0;+ char d[20];++ do {+ d[k++] = "0123456789abcdef"[base == 16 ? v & 15 : v % 10];+ v = base == 16 ? v >> 4 : v / 10;+ } while (v);+ for (; !zero && width > k + neg; width--)+ n = format_char(buf, size, n, ' ');+ if (neg)+ n = format_char(buf, size, n, '-');+ for (; zero && width > k + neg; width--)+ n = format_char(buf, size, n, '0');+ while (k)+ n = format_char(buf, size, n, d[--k]);+ return n;+}++/*+ * CBMC passes each variadic argument as an object of its own type, a+ * char not promoted to int, so an integer is read by its size, a char or+ * short as unsigned.+ */+static __u64 integer_arg(va_list *ap, bool is_signed, bool *neg)+{+ __s64 v;++ switch (__CPROVER_OBJECT_SIZE(*(void **)*ap)) {+ case 1:+ return va_arg(*ap, __u8);+ case 2:+ return va_arg(*ap, __u16);+ case 4:+ v = is_signed ? va_arg(*ap, __s32) : va_arg(*ap, __u32);+ break;+ default:+ if (!is_signed)+ return va_arg(*ap, __u64);+ v = va_arg(*ap, __s64);+ }+ *neg = v < 0;+ return *neg ? (__u64)-(v + 1) + 1 : (__u64)v;+}++/*+ * CBMC has no vsnprintf() either. This one formats what shirudo's proofs+ * reach, %s and the d, u and x conversions with a zero flag, a width and+ * ll, and fails the proof on any other.+ */+int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)+{+ size_t n = 0;++ for (; *fmt; fmt++) {+ bool zero = false, neg = false;+ size_t width = 0;+ const char *s;+ __u64 v;++ if (*fmt != '%' || fmt[1] == '%') {+ n = format_char(buf, size, n, *fmt);+ fmt += *fmt == '%';+ continue;+ }+ if (*++fmt == '0') {+ zero = true;+ fmt++;+ }+ while (*fmt >= '1' && *fmt <= '9' && width < 10)+ width = width * 10 + (size_t)(*fmt++ - '0');+ if (fmt[0] == 'l' && fmt[1] == 'l')+ fmt += 2;+ if (*fmt == 's') {+ __CPROVER_assert(!zero && !width,+ "a plain %s in the vsnprintf() model");+ for (s = va_arg(ap, const char *); *s; s++)+ n = format_char(buf, size, n, *s);+ continue;+ }+ __CPROVER_assert(*fmt == 'd' || *fmt == 'u' || *fmt == 'x',+ "a conversion the vsnprintf() model has");+ v = integer_arg(&ap, *fmt == 'd', &neg);+ n = format_number(buf, size, n, v, neg, *fmt == 'x' ? 16 : 10,+ width, zero);+ }+ if (size)+ buf[n < size ? n : size - 1] = '\0';+ return (int)n;+}+/** CBMC 5.95's memcpy() reads wrong bytes from a string literal when the* source pointer is one of several literals picked by symbolic data, anddiff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex d6df3578..550e69fd 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -44,3 +44,14 @@ dm-names --unwind 10kernel-notes --unwind 1 --unwindset kernel_build_id_from_notes.0:5,hex_encode.0:29,strnlen.0:58,harness.0:58,first_note.0:29,memcmp.0:5 LEN=0-24,28,32,36,40kernel-release --unwind 10log-escape --unwind 7 LEN=0-5+json-emit-string --unwind 5 --unwindset json_string.0:27,put_string_n.0:5,put_raw.0:7,well_formed.0:27,replaced.0:5,memchr.0:9,memcmp.0:13 LEN=0-4+json-emit-buffer --unwind 1 --unwindset json_raw_value.0:5,harness.0:5 --malloc-may-fail --malloc-fail-null LEN=0-4+json-emit-nest --object-bits 12 --unwind 1 --unwindset json_text.0:57,sequences.0:4,sequences.1:5,any_depth.0:4,any_depth.1:4,strcmp.0:12,memchr.0:9,put_string_n.0:2,put_string_n.1:2,put_raw.0:8,json_kv_raw.0:3,json_raw_value.0:3,json_raw.0:5,strlen.0:3,vsnprintf.2:5,vsnprintf.0:2,format_number.0:3,format_number.2:5,format_number.3:3+json-string --unwind 5 --unwindset json_string.0:7,parse_string.0:5,parse_string.1:7,memchr.0:9,memcmp.0:7,harness.0:8 LEN=0-6+json-escape --unwind 5 --unwindset parse_string.0:4,memcmp.0:9+json-roundtrip --unwind 1 --unwindset put_string_n.0:5,put_string_n.1:5,put_raw.0:7,vsnprintf.2:4,vsnprintf.0:2,format_number.0:3,format_number.2:5,format_number.3:3,parse_string.1:6,parse_string.0:4,hex4.0:5,utf8_width.0:4,memcmp.0:5 LEN=0-4+otel-header-fields -I ../../../vendor/libbpf -I ../../../vendor/libbpf/include/uapi -D__NO_CTYPE --unwind 34 --unwindset strchr.0:17 LEN=32+otel-retry-after -I ../../../vendor/libbpf -I ../../../vendor/libbpf/include/uapi -D__NO_CTYPE --unwind 1 --unwindset response_header.0:33,response_header.1:33,otel_retry_after.0:33,otel_retry_after.1:33,delay.0:33,strlen.0:34,strncasecmp.0:13 LEN=32+json-walk -DJSON_MAX_DEPTH=2 --unwind 3 --unwindset string_end.0:2,code_unit.0:0,hex4.0:0,parse_string.0:1,parse_string.1:2,literal_end.0:4,json_text.0:3,json_text.1:3,utf8_width.0:1,parse_array.0:1,parse_object.0:1,parse_object.1:1,digits_end.0:3,digits.0:3,skip_ws.0:3,strlen.0:6,memcmp.0:1,memchr.0:3,strnlen.0:4 LEN=0-2+json-limits -DJSON_MAX_DEPTH=2 -DJSON_MAX_BYTES=24 --unwind 8+json-field --unwind 1 --unwindset digits_end.0:7,decimal.0:7,harness.0:7,skip_ws.0:7,parse_string.1:7,parse_string.0:4,hex4.0:0,utf8_width.0:4,parse_base.0:7,parse_base.1:7,digits.0:7,strnlen.0:4,strlen.0:6,memcmp.0:6 LEN=0-6