Browse zlib specs (27 of 62)
Proofs / zlib / The stream interface
api_deflate_any
Any accepted compression parameters, any data in any pieces, any flush and deflateParams between calls: deflate returns only the codes of zlib.h (Z_STREAM_ERROR exactly for an invalid flush), deflateParams returns Z_STREAM_ERROR exactly for an invalid level or strategy, and the output room not written is left as it was.
- spec
- 82 lines of C
- with its headers
- 174 lines
- status
- Proved
- 27 of 62
On this page
The spec
The spec is the C program harness/api_deflate_any.c, 82 lines, shown whole as it is in the repository at 37189f4. It includes harness/laws_common.h (40 lines) and harness/laws_session.h (52 lines). Its first comment states the property in full, with every bound.
// Law api_deflate_any: For any level, windowBits, memLevel and strategy in zlib.h's ranges for deflateInit2 and any
// data (any length the allocator provides), fed to any number of deflate() calls in any pieces below 2^32 bytes, each
// with any output space below 2^32 bytes, none included (input and output each an object of exactly avail_in and
// avail_out bytes), and any flush (any int; once a call had Z_FINISH, every later call has Z_FINISH and the input that
// call left, as zlib.h asks), each call after deflateParams with any level and strategy (any ints) or none (before the
// first Z_FINISH), until Z_STREAM_END: deflateInit2 returns Z_OK (Z_MEM_ERROR ends the run); deflateParams returns
// Z_STREAM_ERROR exactly when its level is not -1..9 or its strategy not Z_DEFAULT_STRATEGY..Z_FIXED, else Z_OK or
// Z_BUF_ERROR; deflate() returns Z_STREAM_ERROR exactly when its flush is not Z_NO_FLUSH..Z_BLOCK, else Z_OK,
// Z_STREAM_END (only with Z_FINISH) or Z_BUF_ERROR, and Z_BUF_ERROR, moving no pointer and no counter, when it is
// offered no output space; and the calls leave the part of the output space they did not write (the last avail_out
// bytes) as it was.
#include "laws_common.h"
#include "laws_session.h"
int main(void) {
int level = (int) nondet_u32();
int window_bits = (int) nondet_u32();
int mem_level = (int) nondet_u32();
int strategy = (int) nondet_u32();
gp_assume(deflate_params_ok(level, window_bits, mem_level, strategy));
size_t data_len = upto(PTRDIFF_MAX);
unsigned char *data = alloc_any_bytes(data_len);
if (data == NULL)
return 0;
z_stream strm;
new_stream(&strm);
int code = deflateInit2(&strm, level, Z_DEFLATED, window_bits, mem_level, strategy);
if (code == Z_MEM_ERROR)
return 0;
gp_assert(code == Z_OK);
size_t read = 0;
size_t left = 0;
int finishing = 0;
struct position at;
do {
size_t take = finishing ? left : upto(clip(data_len - read));
size_t out_size = nondet_u32();
int flush = finishing ? Z_FINISH : (int) nondet_u32();
unsigned char *piece = copy_of(data + read, take);
unsigned char *out = alloc_any_bytes(out_size);
if (piece == NULL || out == NULL)
return 0;
unsigned char *before = copy_of(out, out_size);
if (before == NULL)
return 0;
set_buffer(&strm, piece, take, out, out_size);
// zlib.h, deflateParams: "Z_STREAM_ERROR if the source stream state was inconsistent or if a parameter was
// invalid"; "The interpretation of level and strategy is as in deflateInit2()"
if (!finishing && nondet_u8() == 0) {
int new_level = (int) nondet_u32();
int new_strategy = (int) nondet_u32();
code = deflateParams(&strm, new_level, new_strategy);
int valid = new_level >= -1 && new_level <= 9 && new_strategy >= 0 && new_strategy <= Z_FIXED;
gp_assert(valid ? code == Z_OK || code == Z_BUF_ERROR : code == Z_STREAM_ERROR);
}
position_of(&strm, &at);
code = deflate(&strm, flush);
// zlib.h, deflate() "returns Z_OK", "Z_STREAM_END ... (only when flush is set to Z_FINISH), Z_STREAM_ERROR
// if the stream state was inconsistent", "or Z_BUF_ERROR if no progress is possible (for example avail_in or
// avail_out was zero)"; "Generate more output starting at next_out and update next_out and avail_out
// accordingly."
int bad_flush = flush < Z_NO_FLUSH || flush > Z_BLOCK;
gp_assert((code == Z_STREAM_ERROR) == bad_flush);
gp_assert(code == Z_OK || code == Z_BUF_ERROR || code == Z_STREAM_ERROR || code == Z_STREAM_END);
gp_assert(code != Z_STREAM_END || flush == Z_FINISH);
if (out_size == 0 && !bad_flush)
gp_assert(code == Z_BUF_ERROR && unmoved(&strm, &at));
size_t wrote = out_size - strm.avail_out;
gp_assert(strm.avail_out <= out_size && bytes_eq(out + wrote, before + wrote, strm.avail_out));
read += take - strm.avail_in;
left = strm.avail_in;
finishing = finishing || flush == Z_FINISH;
free(piece);
free(out);
free(before);
} while (code != Z_STREAM_END && nondet_u8() != 0);
return 0;
}harness/laws_common.h, 40 lines
// laws_common.h: trivial helpers shared by the zlib laws (each law includes it once). No models, no algorithms.
#include <limits.h>
#include <stdlib.h>
#include "quickproof.h"
#include "zlib.h"
static size_t upto(size_t max) {
size_t value = nondet_size();
return value < max ? value : max;
}
static uInt clip(size_t n) { return n < UINT_MAX ? (uInt) n : UINT_MAX; }
static unsigned char *alloc_any_bytes(size_t n) {
unsigned char *bytes = malloc(n);
if (bytes != NULL)
nondet_bytes(bytes, n);
return bytes;
}
static unsigned char *copy_of(const unsigned char *source, size_t n) {
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) {
for (size_t i = 0; i < n; i++)
if (a[i] != b[i])
return 0;
return 1;
}
static void set_buffer(z_stream *strm, unsigned char *in, size_t in_len, unsigned char *out, size_t out_len) {
strm->next_in = in;
strm->avail_in = (uInt) in_len;
strm->next_out = out;
strm->avail_out = (uInt) out_len;
}
static void new_stream(z_stream *strm) {
strm->zalloc = Z_NULL;
strm->zfree = Z_NULL;
strm->opaque = Z_NULL;
set_buffer(strm, Z_NULL, 0, Z_NULL, 0);
}harness/laws_session.h, 52 lines
// laws_session.h: helpers of the laws that drive a z_stream, included after laws_common.h. No models, no algorithms.
static int deflate_params_ok(int level, int window_bits, int mem_level, int strategy) {
// zlib.h, deflateInit2: level "Z_DEFAULT_COMPRESSION, or between 0 and 9"; memLevel 1..MAX_MEM_LEVEL (9,
// zconf.h); strategy Z_DEFAULT_STRATEGY (0) .. Z_FIXED (4); windowBits 8..15, "-8..-15 for raw deflate", "Add 16
// to windowBits to write a simple gzip header", but "For raw deflate or gzip encoding, a request for a 256-byte
// window is rejected as invalid"
int zlib_window = window_bits >= 8 && window_bits <= 15;
int raw_window = window_bits >= -15 && window_bits <= -9;
int gzip_window = window_bits >= 16 + 9 && window_bits <= 16 + 15;
int valid = level >= -1 && level <= 9 && mem_level >= 1 && mem_level <= 9 && strategy >= 0 && strategy <= Z_FIXED;
return valid && (zlib_window || raw_window || gzip_window);
}
static int inflate_window_ok(int window_bits) {
// zlib.h, inflateInit2: windowBits "should be in the range 8..15", "can also be zero to request that inflate
// use the window size in the zlib header", "can also be -8..-15 for raw inflate", "Add 32 to windowBits to
// enable zlib and gzip decoding with automatic header detection, or add 16 to decode only the gzip format"
int low = window_bits & 15;
int raw = window_bits >= -15 && window_bits <= -8;
int wrapped = window_bits >= 0 && window_bits < 48 && (low == 0 || low >= 8);
return raw || wrapped;
}
struct position {
unsigned char *next_in;
uInt avail_in;
unsigned char *next_out;
uInt avail_out;
uLong total_in;
uLong total_out;
};
static void position_of(z_stream *strm, struct position *at) {
at->next_in = strm->next_in;
at->avail_in = strm->avail_in;
at->next_out = strm->next_out;
at->avail_out = strm->avail_out;
at->total_in = strm->total_in;
at->total_out = strm->total_out;
}
static int unmoved(z_stream *strm, struct position *at) {
int in = strm->next_in == at->next_in && strm->avail_in == at->avail_in && strm->total_in == at->total_in;
int out = strm->next_out == at->next_out && strm->avail_out == at->avail_out && strm->total_out == at->total_out;
return in && out;
}
static int accounted(z_stream *strm, struct position *at) {
int in = strm->avail_in <= at->avail_in && strm->next_in + strm->avail_in == at->next_in + at->avail_in;
int out = strm->avail_out <= at->avail_out && strm->next_out + strm->avail_out == at->next_out + at->avail_out;
return in && out;
}
static int counted(z_stream *strm, struct position *at) {
int in = strm->total_in == at->total_in + (at->avail_in - strm->avail_in);
int out = strm->total_out == at->total_out + (at->avail_out - strm->avail_out);
return in && out;
}The theorem
QuickProof generated this statement from the spec, in src/contracts/api_deflate_any.v:
Theorem api_deflate_any : 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_deflate_any : harness_safe Zlib.contracts.api_deflate_any.whole_prog.
Proof. exact (proj1 (proj2 Zlib.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
adler32.ccrc32.cdeflate.ctrees.czutil.cquickproof/libc/libc.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), and binaries built by a compiler (the theorem is about the code as CompCert defines C for x86_64, LP64), one thread.
- Where zlib reports
Z_MEM_ERROR, most specs end the run and claim nothing about it; the first comment of the spec says so. - The edits to zlib's code, listed in the repository, are tested, 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.