|
| 1 | +#![no_main] |
| 2 | +use libbz2_rs_sys::bz_stream; |
| 3 | +use libbz2_rs_sys::BZ2_bzDecompress; |
| 4 | +use libbz2_rs_sys::BZ2_bzDecompressEnd; |
| 5 | +use libbz2_rs_sys::BZ2_bzDecompressInit; |
| 6 | +use libbz2_rs_sys::{ |
| 7 | + BZ_CONFIG_ERROR, BZ_DATA_ERROR, BZ_DATA_ERROR_MAGIC, BZ_FINISH, BZ_FINISH_OK, BZ_FLUSH_OK, |
| 8 | + BZ_IO_ERROR, BZ_MEM_ERROR, BZ_OK, BZ_OUTBUFF_FULL, BZ_PARAM_ERROR, BZ_RUN_OK, |
| 9 | + BZ_SEQUENCE_ERROR, BZ_STREAM_END, BZ_UNEXPECTED_EOF, |
| 10 | +}; |
| 11 | + |
| 12 | +use libfuzzer_sys::fuzz_target; |
| 13 | + |
| 14 | +fn compress_c(data: &[u8], compression_level: u8, work_factor: u8) -> Vec<u8> { |
| 15 | + // compress the data with the stock C bzip2 |
| 16 | + |
| 17 | + // output buffer for compression, will get resized later if needed |
| 18 | + let mut output = vec![0u8; 1024]; |
| 19 | + |
| 20 | + let mut stream = libbz2_rs_sys::bz_stream { |
| 21 | + next_in: data.as_ptr() as *mut _, |
| 22 | + avail_in: data.len() as _, |
| 23 | + total_in_lo32: 0, |
| 24 | + total_in_hi32: 0, |
| 25 | + next_out: output.as_mut_ptr() as *mut _, |
| 26 | + avail_out: output.len() as _, |
| 27 | + total_out_lo32: 0, |
| 28 | + total_out_hi32: 0, |
| 29 | + state: std::ptr::null_mut(), |
| 30 | + bzalloc: None, |
| 31 | + bzfree: None, |
| 32 | + opaque: std::ptr::null_mut(), |
| 33 | + }; |
| 34 | + |
| 35 | + unsafe { |
| 36 | + let err = libbz2_rs_sys::BZ2_bzCompressInit( |
| 37 | + &mut stream, |
| 38 | + compression_level.into(), |
| 39 | + 0, |
| 40 | + work_factor.into(), |
| 41 | + ); |
| 42 | + // init should always succeed |
| 43 | + assert_eq!(err, BZ_OK); |
| 44 | + }; |
| 45 | + |
| 46 | + let error = loop { |
| 47 | + match unsafe { libbz2_rs_sys::BZ2_bzCompress(&mut stream, BZ_FINISH) } { |
| 48 | + BZ_FINISH_OK => { |
| 49 | + let used = output.len() - stream.avail_out as usize; |
| 50 | + // The output buffer is full, resize it |
| 51 | + let add_space: u32 = Ord::max(1024, output.len().try_into().unwrap()); |
| 52 | + output.resize(output.len() + add_space as usize, 0); |
| 53 | + |
| 54 | + // If resize() reallocates, it may have moved in memory |
| 55 | + stream.next_out = output.as_mut_ptr().cast::<i8>().wrapping_add(used); |
| 56 | + stream.avail_out += add_space; |
| 57 | + |
| 58 | + continue; |
| 59 | + } |
| 60 | + BZ_STREAM_END => { |
| 61 | + break BZ_OK; |
| 62 | + } |
| 63 | + ret => { |
| 64 | + break ret; |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // compression should always succeed |
| 70 | + assert_eq!(error, BZ_OK); |
| 71 | + |
| 72 | + // truncate the output buffer down to the actual number of compressed bytes |
| 73 | + output.truncate( |
| 74 | + ((u64::from(stream.total_out_hi32) << 32) + u64::from(stream.total_out_lo32)) |
| 75 | + .try_into() |
| 76 | + .unwrap(), |
| 77 | + ); |
| 78 | + |
| 79 | + unsafe { |
| 80 | + // cleanup, should always succeed |
| 81 | + let err = libbz2_rs_sys::BZ2_bzCompressEnd(&mut stream); |
| 82 | + assert_eq!(err, BZ_OK); |
| 83 | + } |
| 84 | + |
| 85 | + output |
| 86 | +} |
| 87 | + |
| 88 | +fuzz_target!(|input: (&[u8], usize, u8, u8)| { |
| 89 | + let (fuzzer_data, chunk_size, compression_decider, work_factor_decider) = input; |
| 90 | + |
| 91 | + // let the fuzzer pick a value from 1 to 9 (inclusive) |
| 92 | + // use modulo to ensure this always maps to a valid number |
| 93 | + let compression_level: u8 = (compression_decider % 9) + 1; |
| 94 | + |
| 95 | + // valid values 0 to 250 (inclusive) |
| 96 | + // use modulo to ensure this always maps to a valid number |
| 97 | + let work_factor = work_factor_decider % 251; |
| 98 | + |
| 99 | + if chunk_size == 0 { |
| 100 | + // we can't iterate over chunks of size 0 byte, exit early |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + let compressed_data = compress_c(fuzzer_data, compression_level, work_factor); |
| 105 | + |
| 106 | + let mut stream = bz_stream::zeroed(); |
| 107 | + |
| 108 | + unsafe { |
| 109 | + let err = BZ2_bzDecompressInit(&mut stream, 0, 0); |
| 110 | + assert_eq!(err, BZ_OK); |
| 111 | + }; |
| 112 | + |
| 113 | + // output buffer for decompression, will get resized later if needed |
| 114 | + let mut output = vec![0u8; 1 << 10]; |
| 115 | + stream.next_out = output.as_mut_ptr() as *mut _; |
| 116 | + stream.avail_out = output.len() as _; |
| 117 | + |
| 118 | + // iterate over chunks of the compressed data |
| 119 | + 'decompression: for chunk in compressed_data.as_slice().chunks(chunk_size) { |
| 120 | + // designate the current chunk as input |
| 121 | + stream.next_in = chunk.as_ptr() as *mut _; |
| 122 | + stream.avail_in = chunk.len() as _; |
| 123 | + |
| 124 | + loop { |
| 125 | + // perform one round of decompression |
| 126 | + let err = unsafe { BZ2_bzDecompress(&mut stream) }; |
| 127 | + match err { |
| 128 | + BZ_OK => { |
| 129 | + // the decompression mechanism still has data in the input buffer, |
| 130 | + // but no more space in the output buffer |
| 131 | + if stream.avail_in > 0 && stream.avail_out == 0 { |
| 132 | + let used = output.len() - stream.avail_out as usize; |
| 133 | + // The dest buffer is full, increase its size |
| 134 | + let add_space: u32 = Ord::max(1024, output.len().try_into().unwrap()); |
| 135 | + output.resize(output.len() + add_space as usize, 0); |
| 136 | + |
| 137 | + // If resize() reallocates, it may have moved in memory |
| 138 | + stream.next_out = output.as_mut_ptr().cast::<i8>().wrapping_add(used); |
| 139 | + stream.avail_out += add_space; |
| 140 | + continue; |
| 141 | + } else { |
| 142 | + // the decompression of this chunk step was successful, move on to the next |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + BZ_STREAM_END => { |
| 147 | + // the decompression has completed, exit loops |
| 148 | + break 'decompression; |
| 149 | + } |
| 150 | + BZ_RUN_OK => panic!("BZ_RUN_OK"), |
| 151 | + BZ_FLUSH_OK => panic!("BZ_FLUSH_OK"), |
| 152 | + BZ_FINISH_OK => panic!("BZ_FINISH_OK"), |
| 153 | + BZ_SEQUENCE_ERROR => panic!("BZ_SEQUENCE_ERROR"), |
| 154 | + BZ_PARAM_ERROR => panic!("BZ_PARAM_ERROR"), |
| 155 | + BZ_MEM_ERROR => panic!("BZ_MEM_ERROR"), |
| 156 | + BZ_DATA_ERROR => { |
| 157 | + panic!("BZ_DATA_ERROR") |
| 158 | + } |
| 159 | + BZ_DATA_ERROR_MAGIC => panic!("BZ_DATA_ERROR_MAGIC"), |
| 160 | + BZ_IO_ERROR => panic!("BZ_IO_ERROR"), |
| 161 | + BZ_UNEXPECTED_EOF => panic!("BZ_UNEXPECTED_EOF"), |
| 162 | + BZ_OUTBUFF_FULL => panic!("BZ_OUTBUFF_FULL"), |
| 163 | + BZ_CONFIG_ERROR => panic!("BZ_CONFIG_ERROR"), |
| 164 | + _ => panic!("{err}"), |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // truncate the output buffer down to the actual number of decompressed bytes |
| 170 | + output.truncate( |
| 171 | + ((u64::from(stream.total_out_hi32) << 32) + u64::from(stream.total_out_lo32)) |
| 172 | + .try_into() |
| 173 | + .unwrap(), |
| 174 | + ); |
| 175 | + |
| 176 | + unsafe { |
| 177 | + // cleanup, should always succeed |
| 178 | + let err = BZ2_bzDecompressEnd(&mut stream); |
| 179 | + assert_eq!(err, BZ_OK); |
| 180 | + } |
| 181 | + |
| 182 | + // round-trip check |
| 183 | + // compressing and then decompressing should lead back to the input data |
| 184 | + assert_eq!(output, fuzzer_data); |
| 185 | +}); |
0 commit comments