blob: fb6a1ad5e6464cbe2e57a993aa58add8872f4ebc [file] [log] [blame]
David Brazdil66fc1202022-07-04 21:48:45 +01001// 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
17use android_system_virtualizationservice::{
18 aidl::android::system::virtualizationservice::{
19 VirtualMachineConfig::VirtualMachineConfig,
20 VirtualMachineRawConfig::VirtualMachineRawConfig,
21 },
22 binder::{ParcelFileDescriptor, ProcessState},
23};
Alan Stokesdfca76c2022-08-03 13:31:47 +010024use anyhow::{anyhow, Context, Error};
David Brazdil66fc1202022-07-04 21:48:45 +010025use log::info;
26use std::fs::File;
27use std::io::{self, BufRead, BufReader};
28use std::os::unix::io::FromRawFd;
29use std::panic;
30use std::thread;
Alan Stokesdfca76c2022-08-03 13:31:47 +010031use std::time::Duration;
David Brazdil66fc1202022-07-04 21:48:45 +010032use vmclient::{DeathReason, VmInstance};
33
34const 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]
38fn 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 Stokes0e82b502022-08-08 14:44:48 +010069 let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log), None)
David Brazdil66fc1202022-07-04 21:48:45 +010070 .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 Stokesdfca76c2022-08-03 13:31:47 +010075 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 Brazdil66fc1202022-07-04 21:48:45 +010078 assert_eq!(death_reason, DeathReason::Shutdown);
79
80 Ok(())
81}
82
83fn 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}