Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Integration test for VM bootloader. |
| 16 | |
| 17 | use android_system_virtualizationservice::{ |
| 18 | aidl::android::system::virtualizationservice::{ |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 19 | CpuTopology::CpuTopology, DiskImage::DiskImage, VirtualMachineConfig::VirtualMachineConfig, |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 20 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 21 | }, |
| 22 | binder::{ParcelFileDescriptor, ProcessState}, |
| 23 | }; |
| 24 | use anyhow::{Context, Error}; |
| 25 | use log::info; |
| 26 | use std::{ |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 27 | collections::{HashSet, VecDeque}, |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 28 | fs::File, |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 29 | io::{self, BufRead, BufReader, Read, Write}, |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 30 | panic, thread, |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 31 | }; |
| 32 | use vmclient::{DeathReason, VmInstance}; |
| 33 | |
Pierre-Clément Tosi | e0d6840 | 2024-08-14 00:27:47 +0100 | [diff] [blame] | 34 | const VMBASE_EXAMPLE_BIOS_PATH: &str = "vmbase_example_bios.bin"; |
Nikita Putikhin | cf9c24e | 2024-07-16 12:42:14 +0200 | [diff] [blame] | 35 | const TEST_DISK_IMAGE_PATH: &str = "test_disk.img"; |
| 36 | const EMPTY_DISK_IMAGE_PATH: &str = "empty_disk.img"; |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 37 | |
Pierre-Clément Tosi | e0d6840 | 2024-08-14 00:27:47 +0100 | [diff] [blame] | 38 | /// Runs the vmbase_example VM as an unprotected VM BIOS via VirtualizationService. |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 39 | #[test] |
Pierre-Clément Tosi | e0d6840 | 2024-08-14 00:27:47 +0100 | [diff] [blame] | 40 | fn test_run_example_bios_vm() -> Result<(), Error> { |
| 41 | run_test(Some(open_payload(VMBASE_EXAMPLE_BIOS_PATH)?)) |
| 42 | } |
| 43 | |
| 44 | fn run_test(bootloader: Option<ParcelFileDescriptor>) -> Result<(), Error> { |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 45 | android_logger::init_once( |
Jeff Vander Stoep | d9dda0c | 2024-02-07 14:27:06 +0100 | [diff] [blame] | 46 | android_logger::Config::default() |
| 47 | .with_tag("vmbase") |
| 48 | .with_max_level(log::LevelFilter::Debug), |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 49 | ); |
| 50 | |
| 51 | // Redirect panic messages to logcat. |
| 52 | panic::set_hook(Box::new(|panic_info| { |
| 53 | log::error!("{}", panic_info); |
| 54 | })); |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 55 | |
| 56 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 57 | ProcessState::start_thread_pool(); |
| 58 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 59 | let virtmgr = |
| 60 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 61 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 62 | |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 63 | // Make file for test disk image. |
| 64 | let mut test_image = File::options() |
| 65 | .create(true) |
| 66 | .read(true) |
| 67 | .write(true) |
| 68 | .truncate(true) |
| 69 | .open(TEST_DISK_IMAGE_PATH) |
| 70 | .with_context(|| format!("Failed to open test disk image {}", TEST_DISK_IMAGE_PATH))?; |
| 71 | // Write 4 sectors worth of 4-byte numbers counting up. |
| 72 | for i in 0u32..512 { |
| 73 | test_image.write_all(&i.to_le_bytes())?; |
| 74 | } |
| 75 | let test_image = ParcelFileDescriptor::new(test_image); |
| 76 | let disk_image = DiskImage { image: Some(test_image), writable: false, partitions: vec![] }; |
| 77 | |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 78 | // Make file for empty test disk image. |
| 79 | let empty_image = File::options() |
| 80 | .create(true) |
| 81 | .read(true) |
| 82 | .write(true) |
| 83 | .truncate(true) |
| 84 | .open(EMPTY_DISK_IMAGE_PATH) |
| 85 | .with_context(|| format!("Failed to open empty disk image {}", EMPTY_DISK_IMAGE_PATH))?; |
| 86 | let empty_image = ParcelFileDescriptor::new(empty_image); |
| 87 | let empty_disk_image = |
| 88 | DiskImage { image: Some(empty_image), writable: false, partitions: vec![] }; |
| 89 | |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 90 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 91 | name: String::from("VmBaseTest"), |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 92 | kernel: None, |
| 93 | initrd: None, |
| 94 | params: None, |
Pierre-Clément Tosi | e0d6840 | 2024-08-14 00:27:47 +0100 | [diff] [blame] | 95 | bootloader, |
Andrew Walbran | 6ac174e | 2023-06-23 14:58:51 +0000 | [diff] [blame] | 96 | disks: vec![disk_image, empty_disk_image], |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 97 | protectedVm: false, |
| 98 | memoryMib: 300, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 99 | cpuTopology: CpuTopology::ONE_CPU, |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 100 | platformVersion: "~1.0".to_string(), |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 101 | gdbPort: 0, // no gdb |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 102 | ..Default::default() |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 103 | }); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 104 | let (handle, console) = android_log_fd()?; |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 105 | let (mut log_reader, log_writer) = pipe()?; |
Jiyong Park | e6fb167 | 2023-06-26 16:45:55 +0900 | [diff] [blame] | 106 | let vm = VmInstance::create( |
| 107 | service.as_ref(), |
| 108 | &config, |
| 109 | Some(console), |
| 110 | /* consoleIn */ None, |
| 111 | Some(log_writer), |
| 112 | None, |
| 113 | ) |
| 114 | .context("Failed to create VM")?; |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 115 | vm.start().context("Failed to start VM")?; |
| 116 | info!("Started example VM."); |
| 117 | |
| 118 | // Wait for VM to finish, and check that it shut down cleanly. |
| 119 | let death_reason = vm.wait_for_death(); |
| 120 | assert_eq!(death_reason, DeathReason::Shutdown); |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 121 | handle.join().unwrap(); |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 122 | |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 123 | // Check that the expected string was written to the log VirtIO console device. |
| 124 | let expected = "Hello VirtIO console\n"; |
| 125 | let mut log_output = String::new(); |
| 126 | assert_eq!(log_reader.read_to_string(&mut log_output)?, expected.len()); |
| 127 | assert_eq!(log_output, expected); |
| 128 | |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 129 | Ok(()) |
| 130 | } |
| 131 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 132 | fn android_log_fd() -> Result<(thread::JoinHandle<()>, File), io::Error> { |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 133 | let (reader, writer) = pipe()?; |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 134 | let handle = thread::spawn(|| VmLogProcessor::new(reader).run().unwrap()); |
| 135 | Ok((handle, writer)) |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 136 | } |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 137 | |
| 138 | fn pipe() -> io::Result<(File, File)> { |
| 139 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
Frederick Mayle | fbbcfcd | 2024-04-08 16:31:54 -0700 | [diff] [blame] | 140 | Ok((reader_fd.into(), writer_fd.into())) |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame] | 141 | } |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 142 | |
Pierre-Clément Tosi | e0d6840 | 2024-08-14 00:27:47 +0100 | [diff] [blame] | 143 | fn open_payload(path: &str) -> Result<ParcelFileDescriptor, Error> { |
| 144 | let file = File::open(path).with_context(|| format!("Failed to open VM image {path}"))?; |
| 145 | Ok(ParcelFileDescriptor::new(file)) |
| 146 | } |
| 147 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 148 | struct VmLogProcessor { |
| 149 | reader: Option<File>, |
| 150 | expected: VecDeque<String>, |
| 151 | unexpected: HashSet<String>, |
| 152 | had_unexpected: bool, |
| 153 | } |
| 154 | |
| 155 | impl VmLogProcessor { |
| 156 | fn messages() -> (VecDeque<String>, HashSet<String>) { |
| 157 | let mut expected = VecDeque::new(); |
| 158 | let mut unexpected = HashSet::new(); |
| 159 | for log_lvl in ["[ERROR]", "[WARN]", "[INFO]", "[DEBUG]"] { |
| 160 | expected.push_back(format!("{log_lvl} Unsuppressed message")); |
| 161 | unexpected.insert(format!("{log_lvl} Suppressed message")); |
| 162 | } |
| 163 | (expected, unexpected) |
| 164 | } |
| 165 | |
| 166 | fn new(reader: File) -> Self { |
| 167 | let (expected, unexpected) = Self::messages(); |
| 168 | Self { reader: Some(reader), expected, unexpected, had_unexpected: false } |
| 169 | } |
| 170 | |
| 171 | fn verify(&mut self, msg: &str) { |
| 172 | if self.expected.front() == Some(&msg.to_owned()) { |
| 173 | self.expected.pop_front(); |
| 174 | } |
| 175 | if !self.had_unexpected && self.unexpected.contains(msg) { |
| 176 | self.had_unexpected = true; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn run(mut self) -> Result<(), &'static str> { |
| 181 | for line in BufReader::new(self.reader.take().unwrap()).lines() { |
| 182 | let msg = line.unwrap(); |
| 183 | info!("{msg}"); |
| 184 | self.verify(&msg); |
| 185 | } |
| 186 | if !self.expected.is_empty() { |
| 187 | Err("missing expected log message") |
| 188 | } else if self.had_unexpected { |
| 189 | Err("unexpected log message") |
| 190 | } else { |
| 191 | Ok(()) |
| 192 | } |
| 193 | } |
| 194 | } |