Fanbase

bz2/bz2-boundary

version 1.0

bzip2 files at the edges: every block size, a stream of no blocks, long runs, all 256 byte values, a stream of two blocks, two streams of different block sizes

Format
bzip2 (bz2)
Authors
The Fandango Fuzzer Team
License
Apache-2.0
Fandango
>=1.3
Extensions
bz2
Media type
application/x-bzip2
Specification
https://sourceware.org/bzip2/
Its files should decode
always
In the registry
specs/bz2/bz2-boundary/bz2-boundary.fan

Use it

fanbase install bz2-boundary
fandango fuzz -F bz2-boundary -n 10

How it does

accepts 100%, covers 65.0%, 9.5/s. 958 inputs, seed 1.

ParserAcceptsVersion
7zip100%25.01
7zip-bz2-cov100%7-Zip (bzip2) 26.05
busybox100%1.37.0
bzip2100%1.0.8
bzip2-cov100%libbzip2 1.0.8
python-bz2100%3.12.16

The spec says its files decode always: as expected.

7zip-bz2-cov

lines reached
65.0% of 992
real files reach
64.7%
only the generated files
3 lines
only the real files
0 lines
56.5% after 1 inputs, 65.0% after 958real files: 64.7%

bzip2-cov

lines reached
38.0% of 1186
real files reach
38.5%
only the generated files
1 lines
only the real files
6 lines
37.0% after 1 inputs, 38.0% after 958real files: 38.5%

Cite it

@software{fanbase_bz2_bz2_boundary_1_0,
  author  = {{The Fandango Fuzzer Team}},
  title   = {bzip2 (bz2/bz2-boundary), a Fandango input specification},
  version = {1.0},
  url     = {https://github.com/fandango-fuzzer/fanbase},
  note    = {Fanbase registry, specs/bz2/bz2-boundary/bz2-boundary.fan, sha256 9ca90df394cf},
}

Source

Source (109 lines)
# bzip2 files at the edges of what the format allows: every block size from
# 1 to 9, a stream with no blocks at all (empty data), long runs of one byte,
# all 256 byte values (the largest symbol map), random data that does not
# compress, a stream of more than one block, and two streams one after the
# other with different block sizes. The blocks come from Python's bz2 module
# (libbzip2), and where constraints decompress every stream.
#
# TODO:
# * The data is drawn with `random`, not by Fandango.
# * Each block size goes with one kind of data, always the same.

import bz2
import random


def _rand_bytes(lo, hi):
    n = random.randint(lo, hi)
    if n <= 0:
        return b''
    return bytes(random.choices(range(256), k=n))


# The blocks and end-of-stream marker of a stream at this block size, without
# the four-byte header: data of the given shape, compressed.
def gen_tail(level, shape):
    if shape == 'small':
        data = _rand_bytes(1, 60)
    elif shape == 'runs':
        data = (bytes([random.randint(0, 255)]) * random.randint(8, 500)) + _rand_bytes(0, 40)
    elif shape == 'alpha':
        data = bytes(range(256)) * random.randint(1, 4)
    elif shape == 'inc':
        data = _rand_bytes(200, 900)
    elif shape == 'empty':
        data = b''
    elif shape == 'multiblock':
        # 102,400 bytes without runs: more than one 100 kB block (runs would
        # shrink in bzip2's first run-length stage and fit in one)
        data = (bytes(range(256)) * 400) + _rand_bytes(0, 60)
    else:
        data = _rand_bytes(1, 100)
    return bz2.compress(data, level)[4:]


# Whether libbzip2 decompresses the whole stream: every block CRC and the
# combined CRC match, and the blocks fit the block size in the header.
def dec_ok(full):
    try:
        bz2.decompress(bytes(full))
        return True
    except Exception:
        return False


<start> ::= <bz2_file>

<bz2_file> ::= <stream> | <multi_stream>

<stream> ::= <s_l1> | <s_l2> | <s_l3> | <s_l4> | <s_l5> \
           | <s_l6> | <s_l7> | <s_l8> | <s_l9> | <s_empty>

# One stream per block size, 100 kB to 900 kB
<s_l1> ::= b'BZh1' <t_l1>
<t_l1> ::= <byte>* := gen_tail(1, 'multiblock')

<s_l2> ::= b'BZh2' <t_l2>
<t_l2> ::= <byte>* := gen_tail(2, 'runs')

<s_l3> ::= b'BZh3' <t_l3>
<t_l3> ::= <byte>* := gen_tail(3, 'alpha')

<s_l4> ::= b'BZh4' <t_l4>
<t_l4> ::= <byte>* := gen_tail(4, 'inc')

<s_l5> ::= b'BZh5' <t_l5>
<t_l5> ::= <byte>* := gen_tail(5, 'small')

<s_l6> ::= b'BZh6' <t_l6>
<t_l6> ::= <byte>* := gen_tail(6, 'alpha')

<s_l7> ::= b'BZh7' <t_l7>
<t_l7> ::= <byte>* := gen_tail(7, 'runs')

<s_l8> ::= b'BZh8' <t_l8>
<t_l8> ::= <byte>* := gen_tail(8, 'inc')

<s_l9> ::= b'BZh9' <t_l9>
<t_l9> ::= <byte>* := gen_tail(9, 'small')

# No blocks: the header, the end-of-stream marker and a combined CRC of 0
<s_empty> ::= b'BZh9' <t_empty>
<t_empty> ::= <byte>* := gen_tail(9, 'empty')

# Two streams, the second with another block size
<multi_stream> ::= b'BZh9' <t_ma> b'BZh1' <t_mb>
<t_ma> ::= <byte>* := gen_tail(9, 'alpha')
<t_mb> ::= <byte>* := gen_tail(1, 'runs')

where dec_ok(b'BZh1' + bytes(<t_l1>))
where dec_ok(b'BZh2' + bytes(<t_l2>))
where dec_ok(b'BZh3' + bytes(<t_l3>))
where dec_ok(b'BZh4' + bytes(<t_l4>))
where dec_ok(b'BZh5' + bytes(<t_l5>))
where dec_ok(b'BZh6' + bytes(<t_l6>))
where dec_ok(b'BZh7' + bytes(<t_l7>))
where dec_ok(b'BZh8' + bytes(<t_l8>))
where dec_ok(b'BZh9' + bytes(<t_l9>))
where dec_ok(b'BZh9' + bytes(<t_empty>))
where dec_ok(b'BZh9' + bytes(<t_ma>) + b'BZh1' + bytes(<t_mb>))