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, |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 20 | 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 | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 25 | use anyhow::{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}; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 28 | use service_vm_manager::ServiceVm; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 29 | use std::fs::File; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 30 | use std::io::{self, BufRead, BufReader}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 31 | use std::os::unix::io::FromRawFd; |
| 32 | use std::panic; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 33 | use std::path::PathBuf; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 34 | use std::thread; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 35 | use vmclient::VmInstance; |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 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"; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 40 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame^] | 41 | fn rialto_path(vm_type: VmType) -> &'static str { |
| 42 | match vm_type { |
| 43 | VmType::ProtectedVm => SIGNED_RIALTO_PATH, |
| 44 | VmType::NonProtectedVm => UNSIGNED_RIALTO_PATH, |
| 45 | } |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | #[test] |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame^] | 49 | fn process_requests_in_protected_vm() -> Result<()> { |
| 50 | let mut vm = start_service_vm(VmType::ProtectedVm)?; |
| 51 | |
| 52 | check_processing_reverse_request(&mut vm)?; |
| 53 | Ok(()) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame^] | 56 | #[test] |
| 57 | fn process_requests_in_non_protected_vm() -> Result<()> { |
| 58 | let mut vm = start_service_vm(VmType::NonProtectedVm)?; |
| 59 | |
| 60 | check_processing_reverse_request(&mut vm)?; |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | fn check_processing_reverse_request(vm: &mut ServiceVm) -> Result<()> { |
| 65 | // TODO(b/292080257): Test with message longer than the receiver's buffer capacity |
| 66 | // 1024 bytes once the guest virtio-vsock driver fixes the credit update in recv(). |
| 67 | let message = "abc".repeat(166); |
| 68 | let request = Request::Reverse(message.as_bytes().to_vec()); |
| 69 | |
| 70 | let response = vm.process_request(&request)?; |
| 71 | info!("Received response '{response:?}' for the request '{request:?}'."); |
| 72 | |
| 73 | let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect(); |
| 74 | assert_eq!(Response::Reverse(expected_response), response); |
| 75 | Ok(()) |
| 76 | } |
| 77 | |
| 78 | fn start_service_vm(vm_type: VmType) -> Result<ServiceVm> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 79 | android_logger::init_once( |
| 80 | android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug), |
| 81 | ); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 82 | // Redirect panic messages to logcat. |
| 83 | panic::set_hook(Box::new(|panic_info| { |
| 84 | log::error!("{}", panic_info); |
| 85 | })); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 86 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 87 | ProcessState::start_thread_pool(); |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame^] | 88 | ServiceVm::start_vm(vm_instance(vm_type)?, vm_type) |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame^] | 91 | fn vm_instance(vm_type: VmType) -> Result<VmInstance> { |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 92 | let virtmgr = |
| 93 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 94 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
| 95 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame^] | 96 | let rialto = File::open(rialto_path(vm_type)).context("Failed to open Rialto kernel binary")?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 97 | let console = android_log_fd()?; |
| 98 | let log = android_log_fd()?; |
| 99 | |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame] | 100 | let disks = match vm_type { |
| 101 | VmType::ProtectedVm => { |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 102 | let instance_img = service_vm_manager::instance_img( |
| 103 | service.as_ref(), |
| 104 | PathBuf::from(INSTANCE_IMG_PATH), |
| 105 | )?; |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame] | 106 | let writable_partitions = vec![Partition { |
| 107 | label: "vm-instance".to_owned(), |
| 108 | image: Some(instance_img), |
| 109 | writable: true, |
| 110 | }]; |
| 111 | vec![DiskImage { image: None, partitions: writable_partitions, writable: true }] |
| 112 | } |
| 113 | VmType::NonProtectedVm => vec![], |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 114 | }; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 115 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 116 | name: String::from("RialtoTest"), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 117 | kernel: None, |
| 118 | initrd: None, |
| 119 | params: None, |
| 120 | bootloader: Some(ParcelFileDescriptor::new(rialto)), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 121 | disks, |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame] | 122 | protectedVm: vm_type.is_protected(), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 123 | memoryMib: 300, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 124 | cpuTopology: CpuTopology::ONE_CPU, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 125 | platformVersion: "~1.0".to_string(), |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 126 | gdbPort: 0, // No gdb |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 127 | ..Default::default() |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 128 | }); |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 129 | VmInstance::create( |
Jiyong Park | e6fb167 | 2023-06-26 16:45:55 +0900 | [diff] [blame] | 130 | service.as_ref(), |
| 131 | &config, |
| 132 | Some(console), |
| 133 | /* consoleIn */ None, |
| 134 | Some(log), |
| 135 | None, |
| 136 | ) |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 137 | .context("Failed to create VM") |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | fn android_log_fd() -> io::Result<File> { |
| 141 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
| 142 | |
| 143 | // SAFETY: These are new FDs with no previous owner. |
| 144 | let reader = unsafe { File::from_raw_fd(reader_fd) }; |
Andrew Walbran | ae3350d | 2023-07-21 19:01:18 +0100 | [diff] [blame] | 145 | // SAFETY: These are new FDs with no previous owner. |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 146 | let writer = unsafe { File::from_raw_fd(writer_fd) }; |
| 147 | |
| 148 | thread::spawn(|| { |
| 149 | for line in BufReader::new(reader).lines() { |
| 150 | info!("{}", line.unwrap()); |
| 151 | } |
| 152 | }); |
| 153 | Ok(writer) |
| 154 | } |