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::{ |
| 19 | VirtualMachineConfig::VirtualMachineConfig, |
| 20 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 21 | }, |
| 22 | binder::{ParcelFileDescriptor, ProcessState}, |
| 23 | }; |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 24 | use anyhow::{anyhow, Context, Error}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 25 | use log::info; |
| 26 | use std::fs::File; |
| 27 | use std::io::{self, BufRead, BufReader}; |
| 28 | use std::os::unix::io::FromRawFd; |
| 29 | use std::panic; |
| 30 | use std::thread; |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 31 | use std::time::Duration; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 32 | use vmclient::{DeathReason, VmInstance}; |
| 33 | |
| 34 | const RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin"; |
| 35 | |
| 36 | /// Runs the Rialto VM as an unprotected VM via VirtualizationService. |
| 37 | #[test] |
| 38 | fn test_boots() -> Result<(), Error> { |
| 39 | android_logger::init_once( |
| 40 | android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug), |
| 41 | ); |
| 42 | |
| 43 | // Redirect panic messages to logcat. |
| 44 | panic::set_hook(Box::new(|panic_info| { |
| 45 | log::error!("{}", panic_info); |
| 46 | })); |
| 47 | |
| 48 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 49 | ProcessState::start_thread_pool(); |
| 50 | |
| 51 | let service = vmclient::connect().context("Failed to find VirtualizationService")?; |
| 52 | let rialto = File::open(RIALTO_PATH).context("Failed to open Rialto kernel binary")?; |
| 53 | let console = android_log_fd()?; |
| 54 | let log = android_log_fd()?; |
| 55 | |
| 56 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
| 57 | kernel: None, |
| 58 | initrd: None, |
| 59 | params: None, |
| 60 | bootloader: Some(ParcelFileDescriptor::new(rialto)), |
| 61 | disks: vec![], |
| 62 | protectedVm: false, |
| 63 | memoryMib: 300, |
| 64 | numCpus: 1, |
| 65 | cpuAffinity: None, |
| 66 | platformVersion: "~1.0".to_string(), |
| 67 | taskProfiles: vec![], |
| 68 | }); |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame^] | 69 | 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] | 70 | .context("Failed to create VM")?; |
| 71 | |
| 72 | vm.start().context("Failed to start VM")?; |
| 73 | |
| 74 | // Wait for VM to finish, and check that it shut down cleanly. |
Alan Stokes | dfca76c | 2022-08-03 13:31:47 +0100 | [diff] [blame] | 75 | let death_reason = vm |
| 76 | .wait_for_death_with_timeout(Duration::from_secs(10)) |
| 77 | .ok_or_else(|| anyhow!("Timed out waiting for VM exit"))?; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 78 | assert_eq!(death_reason, DeathReason::Shutdown); |
| 79 | |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | fn android_log_fd() -> io::Result<File> { |
| 84 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
| 85 | |
| 86 | // SAFETY: These are new FDs with no previous owner. |
| 87 | let reader = unsafe { File::from_raw_fd(reader_fd) }; |
| 88 | let writer = unsafe { File::from_raw_fd(writer_fd) }; |
| 89 | |
| 90 | thread::spawn(|| { |
| 91 | for line in BufReader::new(reader).lines() { |
| 92 | info!("{}", line.unwrap()); |
| 93 | } |
| 94 | }); |
| 95 | Ok(writer) |
| 96 | } |