blob: ef0106323d5477a730263af355383e99877896f4 [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::{
Alice Wang9a8b39f2023-04-12 15:31:48 +000019 CpuTopology::CpuTopology, DiskImage::DiskImage, Partition::Partition,
Alice Wang17dc76e2023-09-06 09:43:52 +000020 VirtualMachineConfig::VirtualMachineConfig,
David Brazdil66fc1202022-07-04 21:48:45 +010021 VirtualMachineRawConfig::VirtualMachineRawConfig,
22 },
23 binder::{ParcelFileDescriptor, ProcessState},
24};
Alice Wang17dc76e2023-09-06 09:43:52 +000025use anyhow::{Context, Result};
David Brazdil66fc1202022-07-04 21:48:45 +010026use log::info;
Alice Wang1d9a5872023-09-06 14:32:36 +000027use service_vm_comm::{Request, Response, VmType};
Alice Wang17dc76e2023-09-06 09:43:52 +000028use service_vm_manager::ServiceVm;
David Brazdil66fc1202022-07-04 21:48:45 +010029use std::fs::File;
Alice Wang17dc76e2023-09-06 09:43:52 +000030use std::io::{self, BufRead, BufReader};
David Brazdil66fc1202022-07-04 21:48:45 +010031use std::os::unix::io::FromRawFd;
32use std::panic;
Alice Wang17dc76e2023-09-06 09:43:52 +000033use std::path::PathBuf;
David Brazdil66fc1202022-07-04 21:48:45 +010034use std::thread;
Alice Wang17dc76e2023-09-06 09:43:52 +000035use vmclient::VmInstance;
Alice Wang4e082c32023-07-11 07:41:50 +000036
Alice Wang9a8b39f2023-04-12 15:31:48 +000037const SIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin";
38const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin";
39const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img";
David Brazdil66fc1202022-07-04 21:48:45 +010040
David Brazdil66fc1202022-07-04 21:48:45 +010041#[test]
Alice Wang748b0322023-07-24 12:51:18 +000042fn boot_rialto_in_protected_vm_successfully() -> Result<()> {
Alice Wang1d9a5872023-09-06 14:32:36 +000043 boot_rialto_successfully(SIGNED_RIALTO_PATH, VmType::ProtectedVm)
Alice Wang9a8b39f2023-04-12 15:31:48 +000044}
45
46#[test]
Alice Wang748b0322023-07-24 12:51:18 +000047fn boot_rialto_in_unprotected_vm_successfully() -> Result<()> {
Alice Wang1d9a5872023-09-06 14:32:36 +000048 boot_rialto_successfully(UNSIGNED_RIALTO_PATH, VmType::NonProtectedVm)
Alice Wang9a8b39f2023-04-12 15:31:48 +000049}
50
Alice Wang1d9a5872023-09-06 14:32:36 +000051fn boot_rialto_successfully(rialto_path: &str, vm_type: VmType) -> Result<()> {
David Brazdil66fc1202022-07-04 21:48:45 +010052 android_logger::init_once(
53 android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug),
54 );
David Brazdil66fc1202022-07-04 21:48:45 +010055 // Redirect panic messages to logcat.
56 panic::set_hook(Box::new(|panic_info| {
57 log::error!("{}", panic_info);
58 }));
David Brazdil66fc1202022-07-04 21:48:45 +010059 // We need to start the thread pool for Binder to work properly, especially link_to_death.
60 ProcessState::start_thread_pool();
61
Alice Wang17dc76e2023-09-06 09:43:52 +000062 let mut vm = ServiceVm::start_vm(vm_instance(rialto_path, vm_type)?, vm_type)?;
63
64 // TODO(b/292080257): Test with message longer than the receiver's buffer capacity
65 // 1024 bytes once the guest virtio-vsock driver fixes the credit update in recv().
66 let message = "abc".repeat(166);
67 let request = Request::Reverse(message.as_bytes().to_vec());
68 let response = vm.process_request(&request)?;
69 info!("Received response: {response:?}.");
70
71 let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect();
72 assert_eq!(Response::Reverse(expected_response), response);
73 Ok(())
74}
75
76fn vm_instance(rialto_path: &str, vm_type: VmType) -> Result<VmInstance> {
David Brazdil4b4c5102022-12-19 22:56:20 +000077 let virtmgr =
78 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
79 let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?;
80
Alice Wang9a8b39f2023-04-12 15:31:48 +000081 let rialto = File::open(rialto_path).context("Failed to open Rialto kernel binary")?;
David Brazdil66fc1202022-07-04 21:48:45 +010082 let console = android_log_fd()?;
83 let log = android_log_fd()?;
84
Alice Wang1d9a5872023-09-06 14:32:36 +000085 let disks = match vm_type {
86 VmType::ProtectedVm => {
Alice Wang17dc76e2023-09-06 09:43:52 +000087 let instance_img = service_vm_manager::instance_img(
88 service.as_ref(),
89 PathBuf::from(INSTANCE_IMG_PATH),
90 )?;
Alice Wang1d9a5872023-09-06 14:32:36 +000091 let writable_partitions = vec![Partition {
92 label: "vm-instance".to_owned(),
93 image: Some(instance_img),
94 writable: true,
95 }];
96 vec![DiskImage { image: None, partitions: writable_partitions, writable: true }]
97 }
98 VmType::NonProtectedVm => vec![],
Alice Wang9a8b39f2023-04-12 15:31:48 +000099 };
David Brazdil66fc1202022-07-04 21:48:45 +0100100 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000101 name: String::from("RialtoTest"),
David Brazdil66fc1202022-07-04 21:48:45 +0100102 kernel: None,
103 initrd: None,
104 params: None,
105 bootloader: Some(ParcelFileDescriptor::new(rialto)),
Alice Wang9a8b39f2023-04-12 15:31:48 +0000106 disks,
Alice Wang1d9a5872023-09-06 14:32:36 +0000107 protectedVm: vm_type.is_protected(),
David Brazdil66fc1202022-07-04 21:48:45 +0100108 memoryMib: 300,
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000109 cpuTopology: CpuTopology::ONE_CPU,
David Brazdil66fc1202022-07-04 21:48:45 +0100110 platformVersion: "~1.0".to_string(),
Nikita Ioffe5776f082023-02-10 21:38:26 +0000111 gdbPort: 0, // No gdb
Inseob Kim6ef80972023-07-20 17:23:36 +0900112 ..Default::default()
David Brazdil66fc1202022-07-04 21:48:45 +0100113 });
Alice Wang17dc76e2023-09-06 09:43:52 +0000114 VmInstance::create(
Jiyong Parke6fb1672023-06-26 16:45:55 +0900115 service.as_ref(),
116 &config,
117 Some(console),
118 /* consoleIn */ None,
119 Some(log),
120 None,
121 )
Alice Wang17dc76e2023-09-06 09:43:52 +0000122 .context("Failed to create VM")
David Brazdil66fc1202022-07-04 21:48:45 +0100123}
124
125fn android_log_fd() -> io::Result<File> {
126 let (reader_fd, writer_fd) = nix::unistd::pipe()?;
127
128 // SAFETY: These are new FDs with no previous owner.
129 let reader = unsafe { File::from_raw_fd(reader_fd) };
Andrew Walbranae3350d2023-07-21 19:01:18 +0100130 // SAFETY: These are new FDs with no previous owner.
David Brazdil66fc1202022-07-04 21:48:45 +0100131 let writer = unsafe { File::from_raw_fd(writer_fd) };
132
133 thread::spawn(|| {
134 for line in BufReader::new(reader).lines() {
135 info!("{}", line.unwrap());
136 }
137 });
138 Ok(writer)
139}