crosvm/crosvm-fuzz/zimage_fuzzer.rs
Zihan Chen 646b6ea8fa crosvm-fuzz: Migrate crosvm-fuzz to cargo fuzz
Fuzzer targets can be ran with
cargo +nightly fuzz run --fuzz-dir crosvm-fuzz --features
upstream-fuzz <target>

This should enable us to move fuzzing to anywhere including
ClusterFuzz while maintain compatibility with cros infra.

TEST=`cargo fuzz` won't crash in first 30s,
`USE="asan fuzzer" emerge-hatch crosvm` builds,
`/build/hatch/usr/libexec/fuzzers/crosvm_qcow_fuzzer` won't
crash in first 30s
FIXED=b:245007212
BUG=b:244631591

Change-Id: I4b262ee1a6a90247dea96347c55a3849af793bec
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3905095
Auto-Submit: Zihan Chen <zihanchen@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
2022-10-03 16:12:15 +00:00

28 lines
851 B
Rust

// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#![no_main]
use cros_fuzz::fuzz_target;
use std::fs::File;
use std::io::Write;
use vm_memory::GuestAddress;
use vm_memory::GuestMemory;
const MEM_SIZE: u64 = 256 * 1024 * 1024;
fn make_elf_bin(elf_bytes: &[u8]) -> File {
let mut elf_bin = tempfile::tempfile().expect("failed to create tempfile");
elf_bin
.write_all(elf_bytes)
.expect("failed to write elf to tempfile");
elf_bin
}
fuzz_target!(|bytes| {
let mut kimage = make_elf_bin(bytes);
let mem = GuestMemory::new(&[(GuestAddress(0), MEM_SIZE)]).unwrap();
let _ = kernel_loader::load_elf32(&mem, GuestAddress(0), &mut kimage);
let _ = kernel_loader::load_elf64(&mem, GuestAddress(0), &mut kimage);
});