David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [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 Rialto. |
| 16 | |
| 17 | use android_system_virtualizationservice::{ |
| 18 | aidl::android::system::virtualizationservice::{ |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 19 | CpuTopology::CpuTopology, DiskImage::DiskImage, Partition::Partition, |
| 20 | PartitionType::PartitionType, VirtualMachineConfig::VirtualMachineConfig, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 21 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 22 | }, |
| 23 | binder::{ParcelFileDescriptor, ProcessState}, |
| 24 | }; |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 25 | use anyhow::{anyhow, bail, Context, Result}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 26 | use log::info; |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 27 | use service_vm_comm::{Request, Response, VmType}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 28 | use std::fs::File; |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 29 | use std::io::{self, BufRead, BufReader, BufWriter, Write}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 30 | use std::os::unix::io::FromRawFd; |
| 31 | use std::panic; |
| 32 | use std::thread; |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 33 | use std::time::Duration; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 34 | use vmclient::{DeathReason, VmInstance}; |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 35 | use vsock::{VsockListener, VMADDR_CID_HOST}; |
| 36 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 37 | const SIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin"; |
| 38 | const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin"; |
| 39 | const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img"; |
| 40 | const INSTANCE_IMG_SIZE: i64 = 1024 * 1024; // 1MB |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 41 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 42 | #[test] |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 43 | fn boot_rialto_in_protected_vm_successfully() -> Result<()> { |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 44 | boot_rialto_successfully(SIGNED_RIALTO_PATH, VmType::ProtectedVm) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | #[test] |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 48 | fn boot_rialto_in_unprotected_vm_successfully() -> Result<()> { |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 49 | boot_rialto_successfully(UNSIGNED_RIALTO_PATH, VmType::NonProtectedVm) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 52 | fn boot_rialto_successfully(rialto_path: &str, vm_type: VmType) -> Result<()> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 53 | android_logger::init_once( |
| 54 | android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug), |
| 55 | ); |
| 56 | |
| 57 | // Redirect panic messages to logcat. |
| 58 | panic::set_hook(Box::new(|panic_info| { |
| 59 | log::error!("{}", panic_info); |
| 60 | })); |
| 61 | |
| 62 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 63 | ProcessState::start_thread_pool(); |
| 64 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 65 | let virtmgr = |
| 66 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 67 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
| 68 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 69 | let rialto = File::open(rialto_path).context("Failed to open Rialto kernel binary")?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 70 | let console = android_log_fd()?; |
| 71 | let log = android_log_fd()?; |
| 72 | |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 73 | let disks = match vm_type { |
| 74 | VmType::ProtectedVm => { |
| 75 | let instance_img = File::options() |
| 76 | .create(true) |
| 77 | .read(true) |
| 78 | .write(true) |
| 79 | .truncate(true) |
| 80 | .open(INSTANCE_IMG_PATH)?; |
| 81 | let instance_img = ParcelFileDescriptor::new(instance_img); |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 82 | |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 83 | service |
| 84 | .initializeWritablePartition( |
| 85 | &instance_img, |
| 86 | INSTANCE_IMG_SIZE, |
| 87 | PartitionType::ANDROID_VM_INSTANCE, |
| 88 | ) |
| 89 | .context("Failed to initialize instange.img")?; |
| 90 | let writable_partitions = vec![Partition { |
| 91 | label: "vm-instance".to_owned(), |
| 92 | image: Some(instance_img), |
| 93 | writable: true, |
| 94 | }]; |
| 95 | vec![DiskImage { image: None, partitions: writable_partitions, writable: true }] |
| 96 | } |
| 97 | VmType::NonProtectedVm => vec![], |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 100 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 101 | name: String::from("RialtoTest"), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 102 | kernel: None, |
| 103 | initrd: None, |
| 104 | params: None, |
| 105 | bootloader: Some(ParcelFileDescriptor::new(rialto)), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 106 | disks, |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 107 | protectedVm: vm_type.is_protected(), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 108 | memoryMib: 300, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 109 | cpuTopology: CpuTopology::ONE_CPU, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 110 | platformVersion: "~1.0".to_string(), |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 111 | gdbPort: 0, // No gdb |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 112 | ..Default::default() |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 113 | }); |
Jiyong Park | e6fb167 | 2023-06-26 16:45:55 +0900 | [diff] [blame] | 114 | let vm = VmInstance::create( |
| 115 | service.as_ref(), |
| 116 | &config, |
| 117 | Some(console), |
| 118 | /* consoleIn */ None, |
| 119 | Some(log), |
| 120 | None, |
| 121 | ) |
| 122 | .context("Failed to create VM")?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 123 | |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame^] | 124 | let check_socket_handle = |
| 125 | thread::spawn(move || try_check_socket_connection(vm_type.port()).unwrap()); |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 126 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 127 | vm.start().context("Failed to start VM")?; |
| 128 | |
| 129 | // Wait for VM to finish, and check that it shut down cleanly. |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 130 | let death_reason = vm |
| 131 | .wait_for_death_with_timeout(Duration::from_secs(10)) |
| 132 | .ok_or_else(|| anyhow!("Timed out waiting for VM exit"))?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 133 | assert_eq!(death_reason, DeathReason::Shutdown); |
| 134 | |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 135 | match check_socket_handle.join() { |
| 136 | Ok(_) => { |
| 137 | info!( |
| 138 | "Received the echoed message. \ |
| 139 | The socket connection between the host and the service VM works correctly." |
| 140 | ) |
| 141 | } |
| 142 | Err(_) => bail!("The socket connection check failed."), |
| 143 | } |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 144 | Ok(()) |
| 145 | } |
| 146 | |
| 147 | fn android_log_fd() -> io::Result<File> { |
| 148 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
| 149 | |
| 150 | // SAFETY: These are new FDs with no previous owner. |
| 151 | let reader = unsafe { File::from_raw_fd(reader_fd) }; |
Andrew Walbran | ae3350d | 2023-07-21 19:01:18 +0100 | [diff] [blame] | 152 | // SAFETY: These are new FDs with no previous owner. |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 153 | let writer = unsafe { File::from_raw_fd(writer_fd) }; |
| 154 | |
| 155 | thread::spawn(|| { |
| 156 | for line in BufReader::new(reader).lines() { |
| 157 | info!("{}", line.unwrap()); |
| 158 | } |
| 159 | }); |
| 160 | Ok(writer) |
| 161 | } |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 162 | |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 163 | fn try_check_socket_connection(port: u32) -> Result<()> { |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 164 | info!("Setting up the listening socket on port {port}..."); |
| 165 | let listener = VsockListener::bind_with_cid_port(VMADDR_CID_HOST, port)?; |
| 166 | info!("Listening on port {port}..."); |
| 167 | |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 168 | let mut vsock_stream = |
| 169 | listener.incoming().next().ok_or_else(|| anyhow!("Failed to get vsock_stream"))??; |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 170 | info!("Accepted connection {:?}", vsock_stream); |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 171 | vsock_stream.set_read_timeout(Some(Duration::from_millis(1_000)))?; |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 172 | |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 173 | const WRITE_BUFFER_CAPACITY: usize = 512; |
| 174 | let mut buffer = BufWriter::with_capacity(WRITE_BUFFER_CAPACITY, vsock_stream.clone()); |
| 175 | |
| 176 | // TODO(b/292080257): Test with message longer than the receiver's buffer capacity |
| 177 | // 1024 bytes once the guest virtio-vsock driver fixes the credit update in recv(). |
| 178 | let message = "abc".repeat(166); |
| 179 | let request = Request::Reverse(message.as_bytes().to_vec()); |
| 180 | ciborium::into_writer(&request, &mut buffer)?; |
| 181 | buffer.flush()?; |
| 182 | info!("Sent request: {request:?}."); |
| 183 | |
| 184 | let response: Response = ciborium::from_reader(&mut vsock_stream)?; |
| 185 | info!("Received response: {response:?}."); |
| 186 | |
| 187 | let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect(); |
| 188 | assert_eq!(Response::Reverse(expected_response), response); |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 189 | Ok(()) |
| 190 | } |