xref: /cloud-hypervisor/fuzz/fuzz_targets/linux_loader_cmdline.rs (revision 5e52729453cb62edbe4fb3a4aa24f8cca31e667e)
1 // Copyright 2018 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 // Copyright © 2022 Intel Corporation
6 //
7 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
8 
9 #![no_main]
10 
11 use libfuzzer_sys::fuzz_target;
12 use vm_memory::{bitmap::AtomicBitmap, GuestAddress};
13 
14 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
15 
16 const MEM_SIZE: usize = 256 * 1024 * 1024;
17 // From 'arch::x86_64::layout::CMDLINE_START'
18 const CMDLINE_START: GuestAddress = GuestAddress(0x20000);
19 
20 fuzz_target!(|bytes| {
21     let payload_config = vmm::config::PayloadConfig {
22         firmware: None,
23         kernel: None,
24         cmdline: Some(String::from_utf8_lossy(&bytes).to_string()),
25         initramfs: None,
26     };
27     let kernel_cmdline = match vmm::vm::Vm::generate_cmdline(&payload_config) {
28         Ok(cmdline) => cmdline,
29         _ => return,
30     };
31     let guest_memory = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), MEM_SIZE)]).unwrap();
32 
33     linux_loader::loader::load_cmdline(&guest_memory, CMDLINE_START, &kernel_cmdline).ok();
34 });
35