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 | }; |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 25 | use anyhow::{anyhow, Context, Error}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 26 | use log::info; |
| 27 | use std::fs::File; |
| 28 | use std::io::{self, BufRead, BufReader}; |
| 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}; |
| 34 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 35 | const SIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin"; |
| 36 | const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin"; |
| 37 | const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img"; |
| 38 | const INSTANCE_IMG_SIZE: i64 = 1024 * 1024; // 1MB |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 39 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 40 | #[test] |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 41 | fn boot_rialto_in_protected_vm_successfully() -> Result<(), Error> { |
| 42 | boot_rialto_successfully( |
| 43 | SIGNED_RIALTO_PATH, |
| 44 | true, // protected_vm |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn boot_rialto_in_unprotected_vm_successfully() -> Result<(), Error> { |
| 50 | boot_rialto_successfully( |
| 51 | UNSIGNED_RIALTO_PATH, |
| 52 | false, // protected_vm |
| 53 | ) |
| 54 | } |
| 55 | |
| 56 | fn boot_rialto_successfully(rialto_path: &str, protected_vm: bool) -> Result<(), Error> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 57 | android_logger::init_once( |
| 58 | android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug), |
| 59 | ); |
| 60 | |
| 61 | // Redirect panic messages to logcat. |
| 62 | panic::set_hook(Box::new(|panic_info| { |
| 63 | log::error!("{}", panic_info); |
| 64 | })); |
| 65 | |
| 66 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 67 | ProcessState::start_thread_pool(); |
| 68 | |
David Brazdil | 4b4c510 | 2022-12-19 22:56:20 +0000 | [diff] [blame] | 69 | let virtmgr = |
| 70 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 71 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
| 72 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 73 | 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] | 74 | let console = android_log_fd()?; |
| 75 | let log = android_log_fd()?; |
| 76 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 77 | let disks = if protected_vm { |
| 78 | let instance_img = File::options() |
| 79 | .create(true) |
| 80 | .read(true) |
| 81 | .write(true) |
| 82 | .truncate(true) |
| 83 | .open(INSTANCE_IMG_PATH)?; |
| 84 | let instance_img = ParcelFileDescriptor::new(instance_img); |
| 85 | |
| 86 | service |
| 87 | .initializeWritablePartition( |
| 88 | &instance_img, |
| 89 | INSTANCE_IMG_SIZE, |
| 90 | PartitionType::ANDROID_VM_INSTANCE, |
| 91 | ) |
| 92 | .context("Failed to initialize instange.img")?; |
| 93 | let writable_partitions = vec![Partition { |
| 94 | label: "vm-instance".to_owned(), |
| 95 | image: Some(instance_img), |
| 96 | writable: true, |
| 97 | }]; |
| 98 | vec![DiskImage { image: None, partitions: writable_partitions, writable: true }] |
| 99 | } else { |
| 100 | vec![] |
| 101 | }; |
| 102 | |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 103 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 104 | name: String::from("RialtoTest"), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 105 | kernel: None, |
| 106 | initrd: None, |
| 107 | params: None, |
| 108 | bootloader: Some(ParcelFileDescriptor::new(rialto)), |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame^] | 109 | disks, |
| 110 | protectedVm: protected_vm, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 111 | memoryMib: 300, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 112 | cpuTopology: CpuTopology::ONE_CPU, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 113 | platformVersion: "~1.0".to_string(), |
| 114 | taskProfiles: vec![], |
Nikita Ioffe | 5776f08 | 2023-02-10 21:38:26 +0000 | [diff] [blame] | 115 | gdbPort: 0, // No gdb |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 116 | }); |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 117 | let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log), None) |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 118 | .context("Failed to create VM")?; |
| 119 | |
| 120 | vm.start().context("Failed to start VM")?; |
| 121 | |
| 122 | // Wait for VM to finish, and check that it shut down cleanly. |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 123 | let death_reason = vm |
| 124 | .wait_for_death_with_timeout(Duration::from_secs(10)) |
| 125 | .ok_or_else(|| anyhow!("Timed out waiting for VM exit"))?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 126 | assert_eq!(death_reason, DeathReason::Shutdown); |
| 127 | |
| 128 | Ok(()) |
| 129 | } |
| 130 | |
| 131 | fn android_log_fd() -> io::Result<File> { |
| 132 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
| 133 | |
| 134 | // SAFETY: These are new FDs with no previous owner. |
| 135 | let reader = unsafe { File::from_raw_fd(reader_fd) }; |
| 136 | let writer = unsafe { File::from_raw_fd(writer_fd) }; |
| 137 | |
| 138 | thread::spawn(|| { |
| 139 | for line in BufReader::new(reader).lines() { |
| 140 | info!("{}", line.unwrap()); |
| 141 | } |
| 142 | }); |
| 143 | Ok(writer) |
| 144 | } |