blob: 8a78861eab6cf7d6d6a537d20e530bca6f480776 [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 {
Seungjae Yoo62085c02022-08-12 04:44:52 +000057 name: String::from("RialtoTest"),
David Brazdil66fc1202022-07-04 21:48:45 +010058 kernel: None,
59 initrd: None,
60 params: None,
61 bootloader: Some(ParcelFileDescriptor::new(rialto)),
62 disks: vec![],
63 protectedVm: false,
64 memoryMib: 300,
65 numCpus: 1,
66 cpuAffinity: None,
67 platformVersion: "~1.0".to_string(),
68 taskProfiles: vec![],
69 });
Alan Stokes0e82b502022-08-08 14:44:48 +010070 let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log), None)
David Brazdil66fc1202022-07-04 21:48:45 +010071 .context("Failed to create VM")?;
72
73 vm.start().context("Failed to start VM")?;
74
75 // Wait for VM to finish, and check that it shut down cleanly.
Alan Stokesdfca76c2022-08-03 13:31:47 +010076 let death_reason = vm
77 .wait_for_death_with_timeout(Duration::from_secs(10))
78 .ok_or_else(|| anyhow!("Timed out waiting for VM exit"))?;
David Brazdil66fc1202022-07-04 21:48:45 +010079 assert_eq!(death_reason, DeathReason::Shutdown);
80
81 Ok(())
82}
83
84fn android_log_fd() -> io::Result<File> {
85 let (reader_fd, writer_fd) = nix::unistd::pipe()?;
86
87 // SAFETY: These are new FDs with no previous owner.
88 let reader = unsafe { File::from_raw_fd(reader_fd) };
89 let writer = unsafe { File::from_raw_fd(writer_fd) };
90
91 thread::spawn(|| {
92 for line in BufReader::new(reader).lines() {
93 info!("{}", line.unwrap());
94 }
95 });
96 Ok(writer)
97}