Strengthen fuzz_write: use longer vectors and multiple writes

This commit is contained in:
Chris Hennick 2023-05-01 11:56:46 -07:00
parent c3d0fd145b
commit a23e3889e2
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74
2 changed files with 17 additions and 6 deletions

View file

@ -109,4 +109,4 @@ jobs:
cargo fuzz build fuzz_write cargo fuzz build fuzz_write
- name: run fuzz - name: run fuzz
run: | run: |
cargo fuzz run fuzz_write -- -timeout=1s -runs=1000000 cargo fuzz run fuzz_write -- -timeout=1s -runs=1000000 -max_len=5000000000

View file

@ -1,12 +1,21 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use arbitrary::Arbitrary; use arbitrary::Arbitrary;
use std::io::{Cursor, Read, Seek, Write}; use std::io::{Cursor, Read, Seek, Write};
#[derive(Arbitrary,Debug)]
pub struct ExtraData {
pub header_id: u16,
pub data: Vec<u8>
}
#[derive(Arbitrary,Debug)] #[derive(Arbitrary,Debug)]
pub struct File { pub struct File {
pub name: String, pub name: String,
pub contents: Vec<u8> pub contents: Vec<Vec<u8>>,
pub local_extra_data: Vec<ExtraData>,
pub central_extra_data: Vec<ExtraData>
} }
#[derive(Arbitrary,Debug)] #[derive(Arbitrary,Debug)]
@ -22,7 +31,7 @@ pub enum FileOperation {
DeepCopy { DeepCopy {
base: Box<FileOperation>, base: Box<FileOperation>,
new_name: String new_name: String
}, }
} }
impl FileOperation { impl FileOperation {
@ -40,11 +49,13 @@ fn do_operation<T>(writer: &mut zip_next::ZipWriter<T>,
where T: Read + Write + Seek { where T: Read + Write + Seek {
match operation { match operation {
FileOperation::Write {file, mut options} => { FileOperation::Write {file, mut options} => {
if (*file).contents.len() >= u32::MAX as usize { if file.contents.iter().map(Vec::len).sum::<usize>() >= u32::MAX as usize {
options = options.large_file(true); options = options.large_file(true);
} }
writer.start_file(file.name.to_owned(), options)?; writer.start_file(file.name.to_owned(), options)?;
writer.write_all(file.contents.as_slice())?; for chunk in &file.contents {
writer.write_all(chunk.as_slice())?;
}
} }
FileOperation::ShallowCopy {base, new_name} => { FileOperation::ShallowCopy {base, new_name} => {
do_operation(writer, base)?; do_operation(writer, base)?;
@ -63,5 +74,5 @@ fuzz_target!(|data: Vec<FileOperation>| {
for operation in data { for operation in data {
let _ = do_operation(&mut writer, &operation); let _ = do_operation(&mut writer, &operation);
} }
writer.finish().unwrap(); let _ = zip_next::ZipArchive::new(writer.finish().unwrap());
}); });