Browse zstd specs (30 of 44)

Proofs / zstd / Earlier, longer properties

x_any_info

The frame-inspection functions on any bytes return what zstd.h documents (sizes within the input, "wanted" sizes, magic tests).

spec
67 lines of C
status
Proved
30 of 44
On this page

The spec

The spec is the C program harness/x_any_info.c, 67 lines, shown whole as it is in the repository at 2e46017. Its first comment states the property in full, with every bound.

C
// Law any_info (PROMISES.md: S1, I1-I6): the frame-inspection functions on ANY bytes.
//
// Claim (QuickProof.Contract.harness_safe of this program): no execution has undefined behaviour, and every gp_assert
// holds, as zstd.h documents:
//   ZSTD_findFrameCompressedSize: an error, or the size of a frame within src (<= srcSize);
//   ZSTD_getFrameHeader: 0 (the header is complete: headerSize <= srcSize), or "> 0 => srcSize is too small, @return
//     value is the wanted srcSize amount" (> srcSize), or an error;
//   ZSTD_isFrame, ZSTD_isSkippableFrame: "If `size < 4`, @return will always be 0" (isFrame);
//   ZSTD_readSkippableFrame: an error or "number of bytes written" (<= dstCapacity);
//   ZSTD_getFrameContentSize, ZSTD_findDecompressedSize, ZSTD_decompressBound, ZSTD_frameHeaderSize,
//   ZSTD_getDictID_fromFrame: any value (only safety).
// Draw order n, bytes, cap (env.c).
#include <stdlib.h>
#include "quickproof.h"
#define ZSTD_STATIC_LINKING_ONLY   /* the experimental inspection functions */
#include "zstd.h"

#define SRC_MAX 4096
#define DST_MAX 65536

int main(void)
{
	size_t n = nondet_size(), cap, r;
	unsigned char *src, *dst;
	unsigned variant = 0;
	ZSTD_FrameHeader zfh;
	gp_assume(n <= SRC_MAX);
	src = malloc(n);
	if (src == NULL)
		return 0;                       /* harness out of memory: end the run */
	nondet_bytes(src, n);
	cap = nondet_size();
	gp_assume(cap <= DST_MAX);
	dst = malloc(cap);
	if (dst == NULL) {
		free(src);
		return 0;
	}
	r = ZSTD_findFrameCompressedSize(src, n);
	if (!ZSTD_isError(r))
		gp_assert(r <= n);
	r = ZSTD_getFrameHeader(&zfh, src, n);
	if (!ZSTD_isError(r)) {
		if (r == 0)
			gp_assert(zfh.headerSize <= n);
		else
			gp_assert(r > n);
	}
	if (n < 4) {
		gp_assert(ZSTD_isFrame(src, n) == 0);
		gp_assert(ZSTD_isSkippableFrame(src, n) == 0);
	} else {
		(void) ZSTD_isFrame(src, n);
		(void) ZSTD_isSkippableFrame(src, n);
	}
	r = ZSTD_readSkippableFrame(dst, cap, &variant, src, n);
	if (!ZSTD_isError(r))
		gp_assert(r <= cap);
	(void) ZSTD_getFrameContentSize(src, n);
	(void) ZSTD_findDecompressedSize(src, n);
	(void) ZSTD_decompressBound(src, n);
	(void) ZSTD_frameHeaderSize(src, n);
	(void) ZSTD_getDictID_fromFrame(src, n);
	free(src);
	free(dst);
	return 0;
}

The theorem

QuickProof generated this statement from the spec, in src/contracts/x_any_info.v:

Rocq
Theorem x_any_info : 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:

Rocq
Theorem x_any_info : harness_safe Zstd.contracts.x_any_info.whole_prog.
Proof. exact (proj1 (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.c
  • quickproof/libc/libc.c
  • quickproof/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.