Browse zlib specs (17 of 62)
Proofs / zlib / Rejecting bad input
rj_deflate_params
deflateInit2 accepts its parameters exactly when all are in the ranges of zlib.h.
- spec
- 34 lines of C
- with its headers
- 74 lines
- status
- Proved
- 17 of 62
On this page
The spec
The spec is the C program harness/rj_deflate_params.c, 34 lines, shown whole as it is in the repository at 37189f4. It includes harness/laws_common.h (40 lines). Its first comment states the property in full, with every bound.
// Law rj_deflate_params: For any level, method, windowBits, memLevel and strategy (any ints), deflateInit2 returns Z_OK
// when all of them are in zlib.h's ranges (below), else Z_STREAM_ERROR (Z_MEM_ERROR ends the run).
#include "laws_common.h"
int main(void) {
int level = (int) nondet_u32();
int method = (int) nondet_u32();
int window_bits = (int) nondet_u32();
int mem_level = (int) nondet_u32();
int strategy = (int) nondet_u32();
// zlib.h, deflateInit2: level "Z_DEFAULT_COMPRESSION, or between 0 and 9"; method "must be Z_DEFLATED";
// 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 valid = level >= -1 && level <= 9 && method == Z_DEFLATED;
valid = valid && mem_level >= 1 && mem_level <= 9 && strategy >= 0 && strategy <= Z_FIXED;
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;
valid = valid && (zlib_window || raw_window || gzip_window);
z_stream strm;
new_stream(&strm);
int code = deflateInit2(&strm, level, method, window_bits, mem_level, strategy);
if (code == Z_MEM_ERROR)
return 0;
// zlib.h: "Z_STREAM_ERROR if any parameter is invalid"
if (valid)
gp_assert(code == Z_OK);
else
gp_assert(code == Z_STREAM_ERROR);
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);
}The theorem
QuickProof generated this statement from the spec, in src/contracts/rj_deflate_params.v:
Theorem rj_deflate_params : 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_deflate_params : harness_safe Zlib.contracts.rj_deflate_params.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (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.