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