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::{ |
| 27 | fs::File, |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame^] | 28 | io::{self, BufRead, BufReader, Read, Write}, |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 29 | os::unix::io::FromRawFd, |
| 30 | panic, thread, |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 31 | }; |
| 32 | use vmclient::{DeathReason, VmInstance}; |
| 33 | |
| 34 | const VMBASE_EXAMPLE_PATH: &str = |
| 35 | "/data/local/tmp/vmbase_example.integration_test/arm64/vmbase_example.bin"; |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 36 | const TEST_DISK_IMAGE_PATH: &str = "/data/local/tmp/vmbase_example.integration_test/test_disk.img"; |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 37 | |
| 38 | /// Runs the vmbase_example VM as an unprotected VM via VirtualizationService. |
| 39 | #[test] |
| 40 | fn test_run_example_vm() -> Result<(), Error> { |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 41 | android_logger::init_once( |
| 42 | android_logger::Config::default().with_tag("vmbase").with_min_level(log::Level::Debug), |
| 43 | ); |
| 44 | |
| 45 | // Redirect panic messages to logcat. |
| 46 | panic::set_hook(Box::new(|panic_info| { |
| 47 | log::error!("{}", panic_info); |
| 48 | })); |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 49 | |
| 50 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 51 | ProcessState::start_thread_pool(); |
| 52 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 53 | let virtmgr = |
| 54 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 55 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 56 | |
| 57 | // Start example VM. |
| 58 | let bootloader = ParcelFileDescriptor::new( |
| 59 | File::open(VMBASE_EXAMPLE_PATH) |
| 60 | .with_context(|| format!("Failed to open VM image {}", VMBASE_EXAMPLE_PATH))?, |
| 61 | ); |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +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 | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 78 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 79 | name: String::from("VmBaseTest"), |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 80 | kernel: None, |
| 81 | initrd: None, |
| 82 | params: None, |
| 83 | bootloader: Some(bootloader), |
Andrew Walbran | b713baa | 2022-12-07 14:34:49 +0000 | [diff] [blame] | 84 | disks: vec![disk_image], |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 85 | protectedVm: false, |
| 86 | memoryMib: 300, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 87 | cpuTopology: CpuTopology::ONE_CPU, |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 88 | platformVersion: "~1.0".to_string(), |
| 89 | taskProfiles: vec![], |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 90 | gdbPort: 0, // no gdb |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 91 | }); |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 92 | let console = android_log_fd()?; |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame^] | 93 | let (mut log_reader, log_writer) = pipe()?; |
| 94 | let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log_writer), None) |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 95 | .context("Failed to create VM")?; |
| 96 | vm.start().context("Failed to start VM")?; |
| 97 | info!("Started example VM."); |
| 98 | |
| 99 | // Wait for VM to finish, and check that it shut down cleanly. |
| 100 | let death_reason = vm.wait_for_death(); |
| 101 | assert_eq!(death_reason, DeathReason::Shutdown); |
| 102 | |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame^] | 103 | // Check that the expected string was written to the log VirtIO console device. |
| 104 | let expected = "Hello VirtIO console\n"; |
| 105 | let mut log_output = String::new(); |
| 106 | assert_eq!(log_reader.read_to_string(&mut log_output)?, expected.len()); |
| 107 | assert_eq!(log_output, expected); |
| 108 | |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 109 | Ok(()) |
| 110 | } |
| 111 | |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 112 | fn android_log_fd() -> io::Result<File> { |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame^] | 113 | let (reader, writer) = pipe()?; |
Pierre-Clément Tosi | 0d1aed0 | 2022-11-17 17:06:28 +0000 | [diff] [blame] | 114 | |
| 115 | thread::spawn(|| { |
| 116 | for line in BufReader::new(reader).lines() { |
| 117 | info!("{}", line.unwrap()); |
| 118 | } |
| 119 | }); |
| 120 | Ok(writer) |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 121 | } |
Andrew Walbran | 8d05dae | 2023-03-22 16:42:55 +0000 | [diff] [blame^] | 122 | |
| 123 | fn pipe() -> io::Result<(File, File)> { |
| 124 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
| 125 | |
| 126 | // SAFETY: These are new FDs with no previous owner. |
| 127 | let reader = unsafe { File::from_raw_fd(reader_fd) }; |
| 128 | let writer = unsafe { File::from_raw_fd(writer_fd) }; |
| 129 | |
| 130 | Ok((reader, writer)) |
| 131 | } |