Browse zstd specs (14 of 44)
api_reset
A context used for one-shot and streaming decoding of any inputs, with dictionaries loaded by copy and any ZSTD_d_windowLogMax, then reset with ZSTD_reset_session_and_parameters, behaves like a new one on any input (same results, positions and bytes; both fail or neither).
- spec
- 77 lines of C
- with its headers
- 110 lines
- status
- Proved
- 14 of 44
On this page
The spec
The spec is the C program harness/api_reset.c, 77 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 api_reset: A decompression context used in any way (one-shot and streaming decoding of any inputs, dictionaries
// of any bytes, any ZSTD_d_windowLogMax), then reset with ZSTD_DCtx_reset(ZSTD_reset_session_and_parameters), behaves
// like a new one: given the same calls as a new context (one-shot, or any input in any pieces with any output room),
// each call fails in both, or returns the same result, moves the same positions and writes the same bytes in both
// (ZSTD_error_memory_allocation ends the run; which error is reported is not compared: zstd.h documents none).
#include "laws_common.h"
int main(void) {
ZSTD_DCtx *used = ZSTD_createDCtx(), *fresh = ZSTD_createDCtx();
ZSTD_inBuffer in, in2;
ZSTD_outBuffer out, out2;
size_t n, cap, result, expected;
unsigned char *src, *dst, *other;
if (used == NULL || fresh == NULL)
return 0;
do { // Any use: any input, one-shot or in any pieces with any room; any dictionary, any windowLogMax
n = upto(PTRDIFF_MAX);
src = any_bytes(n);
cap = upto(PTRDIFF_MAX);
dst = malloc(cap);
if (src == NULL || dst == NULL)
return 0;
in.src = src;
in.size = in.pos = 0;
out.dst = dst;
out.pos = 0;
if (nondet_u8() & 1)
ZSTD_decompressDCtx(used, dst, cap, src, n);
else
do { // the input so far and any more of it, any room after the output so far
in.size += upto(n - in.size);
out.size = out.pos + upto(cap - out.pos);
} while (!ZSTD_isError(ZSTD_decompressStream(used, &out, &in)) && nondet_u8() != 0);
ZSTD_DCtx_reset(used, ZSTD_reset_session_only);
if (nondet_u8() & 1)
ZSTD_DCtx_loadDictionary(used, src, n);
ZSTD_DCtx_setParameter(used, ZSTD_d_windowLogMax, (int) nondet_u32());
free(src);
free(dst);
} while (nondet_u8() != 0);
if (ZSTD_isError(ZSTD_DCtx_reset(used, ZSTD_reset_session_and_parameters)))
return 0;
// The same calls on both: any input, any capacity, one-shot or in any pieces with any output room
n = upto(PTRDIFF_MAX);
src = any_bytes(n);
cap = upto(PTRDIFF_MAX);
dst = malloc(cap);
other = malloc(cap);
if (src == NULL || dst == NULL || other == NULL)
return 0;
if (nondet_u8() & 1) {
result = ZSTD_decompressDCtx(used, dst, cap, src, n);
expected = ZSTD_decompressDCtx(fresh, other, cap, src, n);
if (out_of_memory(result) || out_of_memory(expected))
return 0;
gp_assert(ZSTD_isError(result) ? ZSTD_isError(expected)
: result == expected && bytes_eq(dst, other, result));
return 0;
}
in.src = in2.src = src;
in.pos = in2.pos = 0;
out.dst = dst;
out2.dst = other;
out.pos = out2.pos = 0;
do { // any number of calls, until an error
in.size = in2.size = in.pos + upto(n - in.pos); // each call: any more input, any more room
out.size = out2.size = out.pos + upto(cap - out.pos);
result = ZSTD_decompressStream(used, &out, &in);
expected = ZSTD_decompressStream(fresh, &out2, &in2);
if (out_of_memory(result) || out_of_memory(expected))
return 0;
// Check. zstd.h, ZSTD_DCtx_reset: "Return a DCtx to clean state."
gp_assert(ZSTD_isError(result) ? ZSTD_isError(expected) : result == expected && in.pos == in2.pos &&
out.pos == out2.pos && bytes_eq(dst, other, out.pos));
} while (!ZSTD_isError(result) && nondet_u8() != 0);
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/api_reset.v:
Theorem api_reset : 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 api_reset : harness_safe Zstd.contracts.api_reset.whole_prog.
Proof. exact (proj1 (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.