Browse zstd specs (6 of 44)
Proofs / zstd / Rejecting bad input
rj_any_dict
On any dictionary bytes and any input, ZSTD_decompress_usingDict returns an error or a size at most the capacity. The dictionary and the output are one object (the spec explains why).
- spec
- 30 lines of C
- with its headers
- 63 lines
- status
- Proved
- 6 of 44
On this page
The spec
The spec is the C program harness/rj_any_dict.c, 30 lines, shown whole as it is in the repository at 2e46017. It includes harness/laws_common.h (33 lines). Its first comment states the property in full, with every bound.
// Law rj_any_dict: For any dictionary and any input (any bytes, any lengths the allocator provides) and any output
// capacity, ZSTD_decompress_usingDict, called any number of times on one context, returns an error code or a size at
// most the capacity (RFC 8878 5: "If a dictionary is provided by an external source, it should be loaded with great
// care, its content considered untrusted"). The dictionary and the output are one object, with any gap between them:
// zstd compares the output's start with the dictionary's end, which CompCert's C defines only within one object.
#include "laws_common.h"
int main(void) {
ZSTD_DCtx *dctx = ZSTD_createDCtx();
if (dctx == NULL)
return 0;
do {
// Each call: any input; any dictionary, gap and capacity, laid out as dictionary, gap, output
size_t n = upto(PTRDIFF_MAX), dict_len = upto(PTRDIFF_MAX), gap = upto(PTRDIFF_MAX);
size_t cap = upto(PTRDIFF_MAX);
unsigned char *src = any_bytes(n), *both = NULL;
if (dict_len <= PTRDIFF_MAX - gap && dict_len + gap <= PTRDIFF_MAX - cap)
both = any_bytes(dict_len + gap + cap);
if (src == NULL || both == NULL)
return 0;
size_t result = ZSTD_decompress_usingDict(dctx, both + dict_len + gap, cap, src, n, both, dict_len);
// Check. zstd.h, ZSTD_decompress: "@return : the number of bytes decompressed into `dst` (<=
// `dstCapacity`), or an errorCode if it fails"
gp_assert(ZSTD_isError(result) || result <= cap);
free(src);
free(both);
} while (nondet_u8() != 0);
ZSTD_freeDCtx(dctx);
return 0;
}harness/laws_common.h, 33 lines
// laws_common.h: trivial helpers shared by the zstd laws (each law includes it once). No models, no algorithms.
#define ZSTD_STATIC_LINKING_ONLY // zstd.h's experimental part (ZSTD_WINDOWLOG_MAX, ...)
#include <stdint.h>
#include <stdlib.h>
#include "quickproof.h"
#include "zstd.h"
#include "zstd_errors.h"
uint64_t ZSTD_XXH64(const void *input, size_t length, uint64_t seed); // zstd's XXH64 (law ck_xxh64 checks it)
static size_t upto(size_t max) { // any value from 0 to max
size_t value = nondet_size();
return value < max ? value : max;
}
static unsigned char *any_bytes(size_t n) { // a new object of n arbitrary bytes (NULL if malloc fails)
unsigned char *bytes = malloc(n);
if (bytes != NULL && n != 0)
nondet_bytes(bytes, n);
return bytes;
}
static unsigned char *copy_of(const unsigned char *source, size_t n) { // a new object holding source[0..n-1]
unsigned char *copy = malloc(n);
for (size_t i = 0; copy != NULL && i < n; i++)
copy[i] = source[i];
return copy;
}
static int bytes_eq(const unsigned char *a, const unsigned char *b, size_t n) { // a[0..n-1] == b[0..n-1]
for (size_t i = 0; i < n; i++)
if (a[i] != b[i])
return 0;
return 1;
}
static int out_of_memory(size_t result) { // zstd could not allocate (this ends a run: no claim)
return ZSTD_getErrorCode(result) == ZSTD_error_memory_allocation;
}The theorem
QuickProof generated this statement from the spec, in src/contracts/rj_any_dict.v:
Theorem rj_any_dict : harness_safe whole_prog.src/contracts/laws.v states it on the spec's own program, whole_prog (the spec linked with the code under test), with its proof:
Theorem rj_any_dict : harness_safe Zstd.contracts.rj_any_dict.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 Zstd.contracts.suite.suite)))))))))))))))))). Qed.Read as a statement about runs, it says that no run of the program has undefined behaviour or a failing check, for every value the spec can draw. That reading rests on VST's adequacy theorem, which is not yet instantiated in Rocq (formerly Coq) for QuickProof's environment.
The C files (units) its program links
patch/decoder.cquickproof/libc/libc.cquickproof/libc/malloc_unit.c
What it leaves out
- What the first comment of the spec excludes, and its bounds.
- Like every spec of this proof: termination (a run that never returns is not a counterexample), one thread, and binaries built by a compiler (the theorem is about the code as CompCert defines C for x86_64, LP64).
- Other configurations of zstd (flags, legacy formats, assembly) and the compressor.
- The edits to zstd's code, listed in the repository, are checked against the original by testing, not proved.
The summary page says what the proof as a whole covers.
How it was proved
AI agents wrote the proof, and Rocq checks it. qp verify passes on this repository: this statement is the one generated from the spec at the contract commit, and its proof uses only Rocq's standard axioms and VST's abstract malloc library.