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