Elie Kheirallah | ea5dd52 | 2024-11-26 22:16:01 +0000 | [diff] [blame] | 1 | // Copyright 2024, 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 VMs on device. |
| 16 | |
| 17 | use android_system_virtualizationservice::{ |
| 18 | aidl::android::system::virtualizationservice::{ |
| 19 | CpuTopology::CpuTopology, DiskImage::DiskImage, VirtualMachineConfig::VirtualMachineConfig, |
| 20 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 21 | }, |
| 22 | binder::{ParcelFileDescriptor, ProcessState}, |
| 23 | }; |
| 24 | use anyhow::anyhow; |
| 25 | use anyhow::Context; |
| 26 | use anyhow::Error; |
| 27 | use log::error; |
| 28 | use log::info; |
| 29 | use std::fs::read_to_string; |
| 30 | use std::fs::File; |
| 31 | use std::io::Write; |
| 32 | use std::process::Command; |
| 33 | use vmclient::VmInstance; |
| 34 | |
| 35 | const VMBASE_EXAMPLE_KERNEL_PATH: &str = "vmbase_example_kernel.bin"; |
| 36 | const TEST_DISK_IMAGE_PATH: &str = "test_disk.img"; |
| 37 | const EMPTY_DISK_IMAGE_PATH: &str = "empty_disk.img"; |
| 38 | const GOLDEN_DEVICE_TREE: &str = "./goldens/dt_dump_golden.dts"; |
| 39 | const GOLDEN_DEVICE_TREE_PROTECTED: &str = "./goldens/dt_dump_protected_golden.dts"; |
| 40 | |
| 41 | /// Runs an unprotected VM and validates it against a golden device tree. |
| 42 | #[test] |
| 43 | fn test_device_tree_compat() -> Result<(), Error> { |
| 44 | run_test(false, GOLDEN_DEVICE_TREE) |
| 45 | } |
| 46 | |
| 47 | /// Runs a protected VM and validates it against a golden device tree. |
| 48 | #[test] |
| 49 | fn test_device_tree_protected_compat() -> Result<(), Error> { |
| 50 | run_test(true, GOLDEN_DEVICE_TREE_PROTECTED) |
| 51 | } |
| 52 | |
| 53 | fn run_test(protected: bool, golden_dt: &str) -> Result<(), Error> { |
| 54 | let kernel = Some(open_payload(VMBASE_EXAMPLE_KERNEL_PATH)?); |
| 55 | android_logger::init_once( |
| 56 | android_logger::Config::default() |
| 57 | .with_tag("backcompat") |
| 58 | .with_max_level(log::LevelFilter::Debug), |
| 59 | ); |
| 60 | |
| 61 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 62 | ProcessState::start_thread_pool(); |
| 63 | |
| 64 | let virtmgr = |
| 65 | vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?; |
| 66 | let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?; |
| 67 | |
| 68 | // Make file for test disk image. |
| 69 | let mut test_image = File::options() |
| 70 | .create(true) |
| 71 | .read(true) |
| 72 | .write(true) |
| 73 | .truncate(true) |
| 74 | .open(TEST_DISK_IMAGE_PATH) |
| 75 | .with_context(|| format!("Failed to open test disk image {}", TEST_DISK_IMAGE_PATH))?; |
| 76 | // Write 4 sectors worth of 4-byte numbers counting up. |
| 77 | for i in 0u32..512 { |
| 78 | test_image.write_all(&i.to_le_bytes())?; |
| 79 | } |
| 80 | let test_image = ParcelFileDescriptor::new(test_image); |
| 81 | let disk_image = DiskImage { image: Some(test_image), writable: false, partitions: vec![] }; |
| 82 | |
| 83 | // Make file for empty test disk image. |
| 84 | let empty_image = File::options() |
| 85 | .create(true) |
| 86 | .read(true) |
| 87 | .write(true) |
| 88 | .truncate(true) |
| 89 | .open(EMPTY_DISK_IMAGE_PATH) |
| 90 | .with_context(|| format!("Failed to open empty disk image {}", EMPTY_DISK_IMAGE_PATH))?; |
| 91 | let empty_image = ParcelFileDescriptor::new(empty_image); |
| 92 | let empty_disk_image = |
| 93 | DiskImage { image: Some(empty_image), writable: false, partitions: vec![] }; |
| 94 | |
| 95 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
| 96 | name: String::from("VmBaseTest"), |
| 97 | kernel, |
| 98 | disks: vec![disk_image, empty_disk_image], |
| 99 | protectedVm: protected, |
| 100 | memoryMib: 300, |
| 101 | cpuTopology: CpuTopology::ONE_CPU, |
| 102 | platformVersion: "~1.0".to_string(), |
| 103 | ..Default::default() |
| 104 | }); |
| 105 | |
| 106 | let dump_dt = File::options() |
| 107 | .create(true) |
| 108 | .read(true) |
| 109 | .write(true) |
| 110 | .truncate(true) |
| 111 | .open("dump_dt.dtb") |
| 112 | .with_context(|| "Failed to open device tree dump file dump_dt.dtb")?; |
| 113 | let vm = VmInstance::create( |
| 114 | service.as_ref(), |
| 115 | &config, |
| 116 | None, |
| 117 | /* consoleIn */ None, |
| 118 | None, |
| 119 | Some(dump_dt), |
| 120 | None, |
| 121 | ) |
| 122 | .context("Failed to create VM")?; |
| 123 | vm.start().context("Failed to start VM")?; |
| 124 | info!("Started example VM."); |
| 125 | |
| 126 | // Wait for VM to finish |
| 127 | let _ = vm.wait_for_death(); |
| 128 | |
| 129 | if !Command::new("./dtc_static") |
| 130 | .arg("-I") |
| 131 | .arg("dts") |
| 132 | .arg("-O") |
| 133 | .arg("dtb") |
| 134 | .arg("-qqq") |
| 135 | .arg("-f") |
| 136 | .arg("-s") |
| 137 | .arg("-o") |
| 138 | .arg("dump_dt_golden.dtb") |
| 139 | .arg(golden_dt) |
| 140 | .output()? |
| 141 | .status |
| 142 | .success() |
| 143 | { |
| 144 | return Err(anyhow!("failed to execute dtc")); |
| 145 | } |
| 146 | let dtcompare_res = Command::new("./dtcompare") |
| 147 | .arg("--dt1") |
| 148 | .arg("dump_dt_golden.dtb") |
| 149 | .arg("--dt2") |
| 150 | .arg("dump_dt.dtb") |
| 151 | .arg("--ignore-path-value") |
| 152 | .arg("/chosen/kaslr-seed") |
| 153 | .arg("--ignore-path-value") |
| 154 | .arg("/chosen/rng-seed") |
| 155 | .arg("--ignore-path-value") |
| 156 | .arg("/avf/untrusted/instance-id") |
| 157 | .arg("--ignore-path-value") |
| 158 | .arg("/chosen/linuxinitrd-start") |
| 159 | .arg("--ignore-path-value") |
| 160 | .arg("/chosen/linuxinitrd-end") |
| 161 | .arg("--ignore-path-value") |
| 162 | .arg("/avf/secretkeeper_public_key") |
| 163 | .arg("--ignore-path") |
| 164 | .arg("/avf/name") |
| 165 | .output() |
| 166 | .context("failed to execute dtcompare")?; |
| 167 | if !dtcompare_res.status.success() { |
| 168 | if !Command::new("./dtc_static") |
| 169 | .arg("-I") |
| 170 | .arg("dtb") |
| 171 | .arg("-O") |
| 172 | .arg("dts") |
| 173 | .arg("-qqq") |
| 174 | .arg("-f") |
| 175 | .arg("-s") |
| 176 | .arg("-o") |
| 177 | .arg("dump_dt_failed.dts") |
| 178 | .arg("dump_dt.dtb") |
| 179 | .output()? |
| 180 | .status |
| 181 | .success() |
| 182 | { |
| 183 | return Err(anyhow!("failed to execute dtc")); |
| 184 | } |
| 185 | let dt2 = read_to_string("dump_dt_failed.dts")?; |
| 186 | error!( |
| 187 | "Device tree 2 does not match golden DT.\n |
| 188 | Device Tree 2: {}", |
| 189 | dt2 |
| 190 | ); |
| 191 | return Err(anyhow!( |
| 192 | "stdout: {:?}\n stderr: {:?}", |
| 193 | dtcompare_res.stdout, |
| 194 | dtcompare_res.stderr |
| 195 | )); |
| 196 | } |
| 197 | |
| 198 | Ok(()) |
| 199 | } |
| 200 | |
| 201 | fn open_payload(path: &str) -> Result<ParcelFileDescriptor, Error> { |
| 202 | let file = File::open(path).with_context(|| format!("Failed to open VM image {path}"))?; |
| 203 | Ok(ParcelFileDescriptor::new(file)) |
| 204 | } |