1 // Copyright 2019 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #![cfg(not(test))] 6 #![no_main] 7 8 use std::io::Cursor; 9 use std::io::Read; 10 use std::io::Seek; 11 use std::io::SeekFrom; 12 use std::io::Write; 13 use std::mem::size_of; 14 15 use base::FileReadWriteAtVolatile; 16 use cros_fuzz::fuzz_target; 17 use data_model::VolatileSlice; 18 use disk::QcowFile; 19 20 // Take the first 64 bits of data as an address and the next 64 bits as data to 21 // store there. The rest of the data is used as a qcow image. 22 fuzz_target!(|bytes| { 23 if bytes.len() < 16 { 24 // Need an address and data, each are 8 bytes. 25 return; 26 } 27 let mut disk_image = Cursor::new(bytes); 28 let addr = read_u64(&mut disk_image); 29 let value = read_u64(&mut disk_image); 30 let max_nesting_depth = 10; 31 let mut disk_file = tempfile::tempfile().unwrap(); 32 disk_file.write_all(&bytes[16..]).unwrap(); 33 disk_file.seek(SeekFrom::Start(0)).unwrap(); 34 if let Ok(mut qcow) = QcowFile::from(disk_file, max_nesting_depth) { 35 let mut mem = value.to_le_bytes().to_owned(); 36 let vslice = VolatileSlice::new(&mut mem); 37 let _ = qcow.write_all_at_volatile(vslice, addr); 38 } 39 }); 40 read_u64<T: Read>(readable: &mut T) -> u6441fn read_u64<T: Read>(readable: &mut T) -> u64 { 42 let mut buf = [0u8; size_of::<u64>()]; 43 readable.read_exact(&mut buf[..]).unwrap(); 44 u64::from_le_bytes(buf) 45 } 46