Browse zlib specs (52 of 62)
Proofs / zlib / Earlier, longer properties
x_inflate_reset
After any use, inflateReset, or inflateReset2 with any windowBits, gives a stream that behaves like a new one from inflateInit2 on the same input (up to 16 MiB).
- spec
- 110 lines of C
- with its headers
- 265 lines
- status
- Proved
- 52 of 62
On this page
The spec
The spec is the C program harness/x_inflate_reset.c, 110 lines, shown whole as it is in the repository at 37189f4. It includes harness/zlib_common.h (155 lines). Its first comment states the property in full, with every bound.
// Law inflate_reset: inflateReset and inflateReset2 give a stream that behaves exactly like a new one. inflateInit2
// with ANY windowBits; ANY input of n <= 2^24 bytes; ANY number of inflate() calls (ANY flush, ANY split, ANY output
// space <= 2^20 bytes; objects of exactly avail_in and avail_out bytes); then inflateReset, or inflateReset2 with ANY
// windowBits; then the same input from its start, fed by ANY number of inflate() calls (ANY flush, ANY split, ANY
// output space), in lockstep to the reset stream and to a stream freshly made by inflateInit2 with the same
// windowBits, each call offering both streams the same bytes and the same amount of output space. Properties:
// - inflateReset returns Z_OK; inflateReset2 returns Z_OK exactly when inflateInit2 accepts its windowBits;
// - after the reset total_in and total_out are 0, msg is Z_NULL, and every call returns the same code on both
// streams, consumes and produces the same number of bytes, produces the same bytes, and leaves the same total_in,
// total_out, data_type, msg, and (with a wrapper) adler;
// unless one of them runs out of memory (Z_MEM_ERROR: the reset stream keeps its window, the new one allocates its
// own), which ends the run.
// Bounds: n <= 2^24 input bytes, at most 2^20 bytes of output space per call, any number of calls.
#include "zlib_common.h"
#define GP_IN_MAX (1u << 24)
#define GP_OUT_MAX (1u << 20)
int main(void)
{
const int wbits = (int) nondet_u32();
const unsigned n = nondet_u32();
gp_assume(n <= GP_IN_MAX);
unsigned char *src = malloc(n);
if (src == NULL)
return 0;
nondet_bytes(src, n);
z_stream a, b;
stream_defaults(&a);
stream_defaults(&b);
int r = inflateInit2(&a, wbits);
if (r != Z_OK)
return 0;
int phase = 1, w2 = wbits;
unsigned pos = 0;
for (;;) {
const unsigned nlim = nondet_u32();
const unsigned aout = nondet_u32();
const int flush = (int) nondet_u32();
const int wsel = (int) nondet_u32();
const unsigned char stop = nondet_u8();
const unsigned char how = nondet_u8();
gp_assume(pos <= nlim && nlim <= n);
gp_assume(aout <= GP_OUT_MAX);
const unsigned ain = nlim - pos;
unsigned char *ia = exact_copy(src + pos, ain);
unsigned char *ib = exact_copy(src + pos, ain);
unsigned char *sa, *sb;
unsigned char *oa = exact_space(aout, &sa);
unsigned char *ob = exact_space(aout, &sb);
if (ia == NULL || ib == NULL || oa == NULL || ob == NULL)
return 0;
a.next_in = ia;
a.avail_in = ain;
a.next_out = oa;
a.avail_out = aout;
const int ra = inflate(&a, flush);
if (ra == Z_MEM_ERROR)
return 0;
if (phase == 2) {
b.next_in = ib;
b.avail_in = ain;
b.next_out = ob;
b.avail_out = aout;
const int rb = inflate(&b, flush);
if (rb == Z_MEM_ERROR)
return 0;
gp_assert(ra == rb);
gp_assert(a.avail_in == b.avail_in && a.avail_out == b.avail_out);
gp_assert(bytes_same(oa, ob, aout - a.avail_out));
gp_assert(a.total_in == b.total_in && a.total_out == b.total_out);
gp_assert(a.data_type == b.data_type && a.msg == b.msg);
if (w2 >= 0)
gp_assert(a.adler == b.adler);
}
pos += ain - a.avail_in;
free(ia);
free(ib);
free(oa);
free(ob);
free(sa);
free(sb);
if (stop == 0) {
if (phase == 2)
break;
// the reset, and the new stream
if (how & 1) {
r = inflateReset(&a);
gp_assert(r == Z_OK);
} else {
w2 = wsel;
r = inflateReset2(&a, w2);
gp_assert((r == Z_OK) == inflate_wbits_ok(w2));
if (r != Z_OK)
return 0;
}
gp_assert(a.total_in == 0 && a.total_out == 0 && a.msg == Z_NULL);
r = inflateInit2(&b, w2);
if (r != Z_OK)
return 0;
phase = 2;
pos = 0;
if (w2 < 0)
b.adler = a.adler; // a raw stream never sets adler: start both alike
}
}
inflateEnd(&a);
inflateEnd(&b);
return 0;
}harness/zlib_common.h, 155 lines
// Shared by the zlib harnesses (zlib 1.3.2 core: adler32.c, crc32.c, deflate.c, trees.c, inflate.c, inffast.c,
// inftrees.c, compress.c, uncompr.c, zutil.c).
//
// A harness draws every input from quickproof.h (nondet_*, pick), restricts it with gp_assume, and states each property
// with gp_assert (undefined behaviour when the property fails). Buffers have exactly the size the claim allows. Every
// call of inflate() or deflate() gets its input and its output space as objects of their own, allocated with exactly
// avail_in and avail_out bytes (exact_copy, exact_space): an access before next_in, after next_in + avail_in, before
// next_out or after next_out + avail_out is then outside an object, i.e. undefined behaviour in the semantics of the
// theorem, so "inflate reads only the input it is offered and writes only the space it is offered" is part of every
// law's "no undefined behaviour".
//
// The reference facts, written from the definitions, not from zlib:
// - Adler-32 (RFC 1950 8.2): s1 = 1 + the sum of the bytes, s2 = the sum of the successive values of s1, both mod
// 65521; the checksum is s2 * 65536 + s1. ref_adler32 updates a checksum byte by byte;
// - CRC-32 (RFC 1952 8, ISO 3309): the reflected polynomial 0xedb88320, register preset to all ones and inverted at
// the end. ref_crc32 updates a checksum bit by bit;
// - the zlib wrapper (RFC 1950): 2 header bytes, an optional 4-byte dictionary id (big-endian Adler-32 of the
// dictionary), the deflate data, then the big-endian Adler-32 of the uncompressed data;
// - the gzip wrapper (RFC 1952): a header of at least 10 bytes (1f 8b 08 FLG MTIME(4) XFL OS, then the optional
// fields), the deflate data, then the little-endian CRC-32 and the little-endian length mod 2^32 of the data;
// - the inflate windowBits of zlib.h: 8..15 (zlib wrapper, 2^windowBits window), 0 (zlib wrapper, the window size of
// the header), -8..-15 (raw deflate), + 16 (gzip wrapper only; 16 itself: the window size of the header), + 32
// (zlib or gzip, detected); everything else is rejected with Z_STREAM_ERROR.
#ifndef ZLIB_HARNESS_COMMON_H
#define ZLIB_HARNESS_COMMON_H
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "quickproof.h"
#include "zlib.h"
#define GP_BASE 65521ul // the Adler-32 modulus (RFC 1950)
#define GP_POLY 0xedb88320ul // the CRC-32 polynomial, reflected (RFC 1952)
// Adler-32 of buf[0..n-1] continued from the checksum a (both halves < 65521 for a checksum).
static unsigned long ref_adler32(unsigned long a, const unsigned char *buf, unsigned long n)
{
unsigned long s1 = a & 0xffff;
unsigned long s2 = (a >> 16) & 0xffff;
for (unsigned long i = 0; i < n; ++i) {
s1 = (s1 + buf[i]) % GP_BASE;
s2 = (s2 + s1) % GP_BASE;
}
return (s2 << 16) | s1;
}
// The CRC-32 register update by one byte, without pre- or post-conditioning (the register is the 32 low bits).
static unsigned long ref_crc_byte(unsigned long c, unsigned char b)
{
c ^= b;
for (int k = 0; k < 8; ++k) {
if (c & 1)
c = (c >> 1) ^ GP_POLY;
else
c = c >> 1;
}
return c;
}
// CRC-32 of buf[0..n-1] continued from the checksum c (its 32 low bits).
static unsigned long ref_crc32(unsigned long c, const unsigned char *buf, unsigned long n)
{
c = (~c) & 0xffffffff;
for (unsigned long i = 0; i < n; ++i)
c = ref_crc_byte(c, buf[i]);
return (~c) & 0xffffffff;
}
static unsigned long be32(const unsigned char *p)
{
return ((unsigned long) p[0] << 24) | ((unsigned long) p[1] << 16) | ((unsigned long) p[2] << 8) | p[3];
}
static unsigned long le32(const unsigned char *p)
{
return ((unsigned long) p[3] << 24) | ((unsigned long) p[2] << 16) | ((unsigned long) p[1] << 8) | p[0];
}
static int bytes_same(const unsigned char *a, const unsigned char *b, unsigned long n)
{
for (unsigned long i = 0; i < n; ++i)
if (a[i] != b[i])
return 0;
return 1;
}
static void bytes_copy(unsigned char *d, const unsigned char *s, unsigned long n)
{
for (unsigned long i = 0; i < n; ++i)
d[i] = s[i];
}
// A new object of exactly n bytes holding src[0..n-1] (NULL when the allocation fails).
static unsigned char *exact_copy(const unsigned char *src, unsigned n)
{
unsigned char *p = malloc(n);
if (p != NULL)
bytes_copy(p, src, n);
return p;
}
// A new object of exactly n bytes with any contents, and a snapshot of them in *snap (NULL when an allocation fails).
static unsigned char *exact_space(unsigned n, unsigned char **snap)
{
unsigned char *p = malloc(n);
unsigned char *q = malloc(n);
if (p == NULL || q == NULL) {
free(p);
free(q);
*snap = NULL;
return NULL;
}
nondet_bytes(p, n);
bytes_copy(q, p, n);
*snap = q;
return p;
}
// inflateInit2's windowBits check (zlib.h): accepted values.
static int inflate_wbits_ok(int w)
{
if (w < 0)
return w >= -15 && w <= -8;
if (w >= 48)
return 0;
w &= 15;
return w == 0 || (w >= 8 && w <= 15);
}
// Is a value one of inflate()'s documented return codes (zlib.h)?
static int inflate_code_ok(int r)
{
return r == Z_OK || r == Z_STREAM_END || r == Z_NEED_DICT || r == Z_DATA_ERROR || r == Z_STREAM_ERROR ||
r == Z_MEM_ERROR || r == Z_BUF_ERROR;
}
// A z_stream with the default allocator (zalloc, zfree, opaque all Z_NULL: zlib uses zcalloc/zcfree, i.e. malloc and
// free) and any values in the fields the caller does not have to set.
static void stream_defaults(z_stream *s)
{
s->zalloc = Z_NULL;
s->zfree = Z_NULL;
s->opaque = Z_NULL;
s->next_in = Z_NULL;
s->avail_in = 0;
s->next_out = Z_NULL;
s->avail_out = 0;
pick(s->total_in);
pick(s->total_out);
pick(s->adler);
pick(s->data_type);
pick(s->reserved);
}
#endifThe theorem
QuickProof generated this statement from the spec, in src/contracts/x_inflate_reset.v:
Theorem x_inflate_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 x_inflate_reset : harness_safe Zlib.contracts.x_inflate_reset.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 Zlib.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
adler32.ccrc32.cinflate.cinffast.cinftrees.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.