Browse zstd specs (42 of 44)
Proofs / zstd / Earlier, longer properties
x_valid_bufferless
Buffer-less streaming of compliant frames, each call given exactly what it asks for: exactly the content, 0 through skippable frames.
- spec
- 106 lines of C
- with its headers
- 1,400 lines
- status
- Proved
- 42 of 44
On this page
The spec
The spec is the C program harness/x_valid_bufferless.c, 106 lines, shown whole as it is in the repository at 2e46017. It includes harness/writer.h (1,231 lines) and harness/zlimits.h (63 lines). Its first comment states the property in full, with every bound.
// Law valid_bufferless (PROMISES.md: V22; zstd's experimental "Buffer-less streaming decompression (synchronous
// mode)"): ANY series of compliant frames (harness/writer.h), with no dictionary, ANY dictionary through
// ZSTD_decompressBegin_usingDict, or the same through a ZSTD_DDict and ZSTD_decompressBegin_usingDDict, decoded frame
// by frame: ZSTD_decompressBegin*, then "ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively",
// each call given exactly the requested number of bytes, the output of every frame right after the previous one in one
// buffer that holds the series' content ("decompresses each block next to previous one").
//
// Claim (QuickProof.Contract.harness_safe of this program): no execution has undefined behaviour, and every gp_assert
// holds: ZSTD_nextSrcSizeToDecompress asks for no more than what remains of the frame; ZSTD_decompressContinue returns
// "the number of bytes regenerated within 'dst' (necessarily <= dstCapacity)", those bytes being the series' content
// at that position, and 0 throughout a skippable frame ("For skippable frames ZSTD_decompressContinue() always
// returns 0"); "A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero", exactly at its end with
// all its content regenerated; ZSTD_nextInputType returns one of its enumerators. Decoding fails only for a frame
// beyond zstd's window range (zlimits.h). A ZSTD_decompressBegin* that fails (e.g. out of memory) ends the run.
// The output and a dictionary used by reference are two regions of one allocation [dst | 1 byte | dict] (any_dict
// explains why).
#include <stdlib.h>
#include "quickproof.h"
#include "writer.h"
#include "zlimits.h"
static wdict_t vb_wd;
static wseries_t sr;
int main(void)
{
unsigned mode = choose(3), k;
size_t cap, dn = 0, ipos = 0, pos = 0, r, j;
unsigned char *src, *buf, *dst, *dict;
ZSTD_DCtx *dctx;
ZSTD_DDict *ddict = NULL;
int stop = 0;
if (mode != 0) {
w_dict(&vb_wd);
dn = vb_wd.n;
w_series(&sr, &vb_wd);
} else {
w_series(&sr, 0);
}
cap = sr.content_n;
src = exact_copy(sr.bytes, sr.n);
buf = malloc(cap + 1 + dn);
dctx = ZSTD_createDCtx();
dst = buf;
dict = NULL;
if (buf != NULL)
dict = buf + cap + 1; /* not on NULL: NULL + n is undefined (C11 6.5.6p8) */
if (src == NULL || buf == NULL || dctx == NULL) {
free(src);
free(buf);
ZSTD_freeDCtx(dctx);
return 0; /* out of memory: end the run */
}
for (j = 0; j < dn; j++)
dict[j] = vb_wd.bytes[j];
if (mode == 2) {
ddict = ZSTD_createDDict(dict, dn);
if (ddict == NULL)
stop = 1;
}
for (k = 0; k < sr.nframes && !stop; k++) {
const winfo_t *f = &sr.f[k];
size_t end = f->start + f->size, cend = f->cstart + f->csize;
r = mode == 0 ? ZSTD_decompressBegin(dctx) : mode == 1 ? ZSTD_decompressBegin_usingDict(dctx, dict, dn)
: ZSTD_decompressBegin_usingDDict(dctx, ddict);
if (ZSTD_isError(r))
break; /* no claim */
gp_assert(ipos == f->start && pos == f->cstart);
for (;;) {
size_t want = ZSTD_nextSrcSizeToDecompress(dctx);
ZSTD_nextInputType_e t = ZSTD_nextInputType(dctx);
unsigned char *ib;
gp_assert(t == ZSTDnit_frameHeader || t == ZSTDnit_blockHeader || t == ZSTDnit_block
|| t == ZSTDnit_lastBlock || t == ZSTDnit_checksum || t == ZSTDnit_skippableFrame);
if (want == 0) {
gp_assert(ipos == end && pos == cend);
break; /* the frame is fully decoded */
}
gp_assert(want <= end - ipos);
ib = exact_copy(src + ipos, want);
if (ib == NULL) {
stop = 1; /* harness out of memory: end the run */
break;
}
r = ZSTD_decompressContinue(dctx, dst + pos, cap - pos, ib, want);
free(ib);
if (ZSTD_isError(r)) {
gp_assert(!within_oneshot(&sr));
stop = 1;
break;
}
gp_assert(r <= cap - pos);
if (f->skippable)
gp_assert(r == 0);
for (j = 0; j < r; j++)
gp_assert(dst[pos + j] == sr.content[pos + j]);
pos += r;
ipos += want;
}
}
ZSTD_freeDDict(ddict);
ZSTD_freeDCtx(dctx);
free(src);
free(buf);
return 0;
}harness/writer.h (1,231 lines) is too long to show here; it is in the repository.
harness/zlimits.h, 63 lines
// The limits under which zstd may refuse a compliant frame, as its documents state them (PROMISES.md V4): "a decoder
// is allowed to reject a compressed frame which requests a memory size beyond decoder's authorized range"
// (format), zstd's range being ZSTD_WINDOWLOG_MAX (31 on 64-bit: zstd.h) for any decoding and, for streaming,
// "By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT)" (zstd.h);
// the memory allocation of its contexts; and, for streaming, zstd's deliberate busy-loop guard (stall_error).
#ifndef ZSTD_HARNESS_ZLIMITS_H
#define ZSTD_HARNESS_ZLIMITS_H
#include "writer.h"
#ifndef ZSTD_STATIC_LINKING_ONLY
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_WINDOWLOG_MAX, ZSTD_WINDOWLOG_LIMIT_DEFAULT */
#endif
#include "zstd.h"
#include "zstd_errors.h"
/* every Zstandard frame of the series is within zstd's window range (single-segment: Window_Size = content size) */
static int within_oneshot(const wseries_t *sr)
{
unsigned k;
for (k = 0; k < sr->nframes; k++)
if (!sr->f[k].skippable && !sr->f[k].single && sr->f[k].window_log > ZSTD_WINDOWLOG_MAX)
return 0;
return 1;
}
/* ... and within the streaming default */
static int within_stream(const wseries_t *sr)
{
unsigned k;
for (k = 0; k < sr->nframes; k++)
if (!sr->f[k].skippable && sr->f[k].window > (1ULL << ZSTD_WINDOWLOG_LIMIT_DEFAULT))
return 0;
return within_oneshot(sr);
}
/* zstd's busy-loop guard, deliberate (commit 31769ce70 "error on no forward progress": a caller "may be caught in an
infinite loop, calling the streaming decompression function again and again, without making any progress";
db18a62f8 gave it its error codes): ZSTD_decompressStream fails on the ZSTD_NO_FORWARD_PROGRESS_MAX-th (16 by
default) call in a row that reads no input and writes no output, with noForwardProgress_destFull if the output is
full, else noForwardProgress_inputEmpty (the input is then empty). zstd.h does not document it. True iff error
code e is that error, legitimately: this call (fresh input, output position opos0 before it) read and wrote
nothing, and so did the `stalled` calls in a row before it. */
#define NO_PROGRESS_MAX 16
static int stall_error(ZSTD_ErrorCode e, size_t stalled, const ZSTD_inBuffer *in, const ZSTD_outBuffer *out,
size_t opos0)
{
if (stalled + 1 < NO_PROGRESS_MAX || in->pos != 0 || out->pos != opos0)
return 0;
if (e == ZSTD_error_noForwardProgress_destFull)
return out->pos == out->size;
return e == ZSTD_error_noForwardProgress_inputEmpty && out->pos < out->size && in->pos == in->size;
}
/* a copy of b[0..n) in a buffer of exactly n bytes (NULL: out of memory) */
static unsigned char *exact_copy(const unsigned char *b, size_t n)
{
unsigned char *p = malloc(n > 0 ? n : 1);
size_t i;
if (p != NULL)
for (i = 0; i < n; i++)
p[i] = b[i];
return p;
}
#endifThe theorem
QuickProof generated this statement from the spec, in src/contracts/x_valid_bufferless.v:
Theorem x_valid_bufferless : 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 x_valid_bufferless : harness_safe Zstd.contracts.x_valid_bufferless.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 Zstd.contracts.suite.suite_x)))))))))))). 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.