Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 17 | //! Handle running odrefresh in the VM, with an async interface to allow cancellation |
| 18 | |
| 19 | use crate::fd_server_helper::FdServerConfig; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 20 | use crate::instance_starter::CompOsInstance; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 21 | use android_system_composd::aidl::android::system::composd::{ |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 22 | ICompilationTask::ICompilationTask, |
| 23 | ICompilationTaskCallback::{FailureReason::FailureReason, ICompilationTaskCallback}, |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 24 | }; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 25 | use anyhow::{Context, Result}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 26 | use binder::{Interface, Result as BinderResult, Strong}; |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 27 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{ |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 28 | CompilationMode::CompilationMode, ICompOsService, OdrefreshArgs::OdrefreshArgs, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 29 | }; |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 30 | use compos_common::odrefresh::{ |
Victor Hsieh | 9807dcd | 2023-03-14 09:58:53 -0700 | [diff] [blame] | 31 | is_system_property_interesting, ExitCode, CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, |
| 32 | PENDING_ARTIFACTS_SUBDIR, |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 33 | }; |
Victor Hsieh | de76d90 | 2023-03-16 11:37:52 -0700 | [diff] [blame] | 34 | use compos_common::BUILD_MANIFEST_SYSTEM_EXT_APK_PATH; |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 35 | use log::{error, info, warn}; |
Victor Hsieh | 9807dcd | 2023-03-14 09:58:53 -0700 | [diff] [blame] | 36 | use odsign_proto::odsign_info::OdsignInfo; |
| 37 | use protobuf::Message; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 38 | use rustutils::system_properties; |
Victor Hsieh | 9807dcd | 2023-03-14 09:58:53 -0700 | [diff] [blame] | 39 | use std::fs::{remove_dir_all, File, OpenOptions}; |
| 40 | use std::os::fd::AsFd; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 41 | use std::os::unix::fs::OpenOptionsExt; |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 42 | use std::os::unix::io::{AsRawFd, OwnedFd}; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 43 | use std::path::Path; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 44 | use std::sync::{Arc, Mutex}; |
| 45 | use std::thread; |
| 46 | |
| 47 | #[derive(Clone)] |
| 48 | pub struct OdrefreshTask { |
| 49 | running_task: Arc<Mutex<Option<RunningTask>>>, |
| 50 | } |
| 51 | |
| 52 | impl Interface for OdrefreshTask {} |
| 53 | |
| 54 | impl ICompilationTask for OdrefreshTask { |
| 55 | fn cancel(&self) -> BinderResult<()> { |
| 56 | let task = self.take(); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 57 | // Drop the VM, which should end compilation - and cause our thread to exit. |
| 58 | // Note that we don't do a graceful shutdown here; we've been asked to give up our resources |
| 59 | // ASAP, and the VM has not failed so we don't need to ensure VM logs are written. |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 60 | drop(task); |
| 61 | Ok(()) |
| 62 | } |
| 63 | } |
| 64 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 65 | struct RunningTask { |
| 66 | callback: Strong<dyn ICompilationTaskCallback>, |
| 67 | #[allow(dead_code)] // Keeps the CompOS VM alive |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 68 | comp_os: CompOsInstance, |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 71 | impl OdrefreshTask { |
| 72 | /// Return the current running task, if any, removing it from this CompilationTask. |
| 73 | /// Once removed, meaning the task has ended or been canceled, further calls will always return |
| 74 | /// None. |
| 75 | fn take(&self) -> Option<RunningTask> { |
| 76 | self.running_task.lock().unwrap().take() |
| 77 | } |
| 78 | |
| 79 | pub fn start( |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 80 | comp_os: CompOsInstance, |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 81 | compilation_mode: CompilationMode, |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 82 | target_dir_name: String, |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 83 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 84 | ) -> Result<OdrefreshTask> { |
| 85 | let service = comp_os.get_service(); |
| 86 | let task = RunningTask { comp_os, callback: callback.clone() }; |
| 87 | let task = OdrefreshTask { running_task: Arc::new(Mutex::new(Some(task))) }; |
| 88 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 89 | task.clone().start_thread(service, compilation_mode, target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 90 | |
| 91 | Ok(task) |
| 92 | } |
| 93 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 94 | fn start_thread( |
| 95 | self, |
| 96 | service: Strong<dyn ICompOsService>, |
| 97 | compilation_mode: CompilationMode, |
| 98 | target_dir_name: String, |
| 99 | ) { |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 100 | thread::spawn(move || { |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 101 | let exit_code = run_in_vm(service, compilation_mode, &target_dir_name); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 102 | |
| 103 | let task = self.take(); |
| 104 | // We don't do the callback if cancel has already happened. |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 105 | if let Some(RunningTask { callback, comp_os }) = task { |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 106 | // Make sure we keep our service alive until we have called the callback. |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 107 | let lazy_service_guard = comp_os.shutdown(); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 108 | |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 109 | let result = match exit_code { |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 110 | Ok(ExitCode::CompilationSuccess) => { |
Victor Hsieh | 9807dcd | 2023-03-14 09:58:53 -0700 | [diff] [blame] | 111 | if compilation_mode == CompilationMode::TEST_COMPILE { |
| 112 | info!("Compilation success"); |
| 113 | callback.onSuccess() |
| 114 | } else { |
| 115 | // compos.info is generated only during NORMAL_COMPILE |
| 116 | if let Err(e) = enable_fsverity_to_all() { |
| 117 | let message = |
| 118 | format!("Unexpected failure when enabling fs-verity: {:?}", e); |
| 119 | error!("{}", message); |
| 120 | callback.onFailure(FailureReason::FailedToEnableFsverity, &message) |
| 121 | } else { |
| 122 | info!("Compilation success, fs-verity enabled"); |
| 123 | callback.onSuccess() |
| 124 | } |
| 125 | } |
Alan Stokes | 454069c | 2022-02-03 11:21:19 +0000 | [diff] [blame] | 126 | } |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 127 | Ok(exit_code) => { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 128 | let message = format!("Unexpected odrefresh result: {:?}", exit_code); |
| 129 | error!("{}", message); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 130 | callback.onFailure(FailureReason::UnexpectedCompilationResult, &message) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 131 | } |
| 132 | Err(e) => { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 133 | let message = format!("Running odrefresh failed: {:?}", e); |
| 134 | error!("{}", message); |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 135 | callback.onFailure(FailureReason::CompilationFailed, &message) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 136 | } |
| 137 | }; |
| 138 | if let Err(e) = result { |
| 139 | warn!("Failed to deliver callback: {:?}", e); |
| 140 | } |
Alan Stokes | 0fc6ce5 | 2022-08-02 17:01:48 +0100 | [diff] [blame] | 141 | drop(lazy_service_guard); |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 142 | } |
| 143 | }); |
| 144 | } |
| 145 | } |
| 146 | |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 147 | fn run_in_vm( |
| 148 | service: Strong<dyn ICompOsService>, |
| 149 | compilation_mode: CompilationMode, |
| 150 | target_dir_name: &str, |
| 151 | ) -> Result<ExitCode> { |
Victor Hsieh | cb6d66b | 2022-05-10 16:12:06 -0700 | [diff] [blame] | 152 | let mut names = Vec::new(); |
| 153 | let mut values = Vec::new(); |
| 154 | system_properties::foreach(|name, value| { |
| 155 | if is_system_property_interesting(name) { |
| 156 | names.push(name.to_owned()); |
| 157 | values.push(value.to_owned()); |
| 158 | } |
| 159 | })?; |
| 160 | service.initializeSystemProperties(&names, &values).context("initialize system properties")?; |
| 161 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 162 | let output_root = Path::new(ODREFRESH_OUTPUT_ROOT_DIR); |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 163 | |
| 164 | // We need to remove the target directory because odrefresh running in compos will create it |
| 165 | // (and can't see the existing one, since authfs doesn't show it existing files in an output |
| 166 | // directory). |
| 167 | let target_path = output_root.join(target_dir_name); |
Alan Stokes | a4542ec | 2021-12-20 09:39:33 +0000 | [diff] [blame] | 168 | if target_path.exists() { |
| 169 | remove_dir_all(&target_path) |
| 170 | .with_context(|| format!("Failed to delete {}", target_path.display()))?; |
| 171 | } |
Alan Stokes | 35bac3c | 2021-12-16 14:37:24 +0000 | [diff] [blame] | 172 | |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 173 | let staging_dir_fd = open_dir(composd_native::palette_create_odrefresh_staging_directory()?)?; |
| 174 | let system_dir_fd = open_dir(Path::new("/system"))?; |
| 175 | let output_dir_fd = open_dir(output_root)?; |
| 176 | |
| 177 | // Get the raw FD before passing the ownership, since borrowing will violate the borrow check. |
| 178 | let system_dir_raw_fd = system_dir_fd.as_raw_fd(); |
| 179 | let output_dir_raw_fd = output_dir_fd.as_raw_fd(); |
| 180 | let staging_dir_raw_fd = staging_dir_fd.as_raw_fd(); |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 181 | |
Victor Hsieh | de76d90 | 2023-03-16 11:37:52 -0700 | [diff] [blame] | 182 | // When the VM starts, it starts with or without mouting the extra build manifest APK from |
| 183 | // /system_ext. Later on request (here), we need to pass the directory FD of /system_ext, but |
| 184 | // only if the VM is configured to need it. |
| 185 | // |
| 186 | // It is possible to plumb the information from ComposClient to here, but it's extra complexity |
| 187 | // and feel slightly weird to encode the VM's state to the task itself, as it is a request to |
| 188 | // the VM. |
| 189 | let need_system_ext = Path::new(BUILD_MANIFEST_SYSTEM_EXT_APK_PATH).exists(); |
| 190 | let (system_ext_dir_raw_fd, ro_dir_fds) = if need_system_ext { |
| 191 | let system_ext_dir_fd = open_dir(Path::new("/system_ext"))?; |
| 192 | (system_ext_dir_fd.as_raw_fd(), vec![system_dir_fd, system_ext_dir_fd]) |
| 193 | } else { |
| 194 | (-1, vec![system_dir_fd]) |
| 195 | }; |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 196 | |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 197 | // Spawn a fd_server to serve the FDs. |
| 198 | let fd_server_config = FdServerConfig { |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 199 | ro_dir_fds, |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 200 | rw_dir_fds: vec![staging_dir_fd, output_dir_fd], |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 201 | ..Default::default() |
| 202 | }; |
| 203 | let fd_server_raii = fd_server_config.into_fd_server()?; |
| 204 | |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 205 | let zygote_arch = system_properties::read("ro.zygote")?.context("ro.zygote not set")?; |
Victor Hsieh | 9bfbc5f | 2021-12-16 11:45:10 -0800 | [diff] [blame] | 206 | let system_server_compiler_filter = |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 207 | system_properties::read("dalvik.vm.systemservercompilerfilter")?.unwrap_or_default(); |
Victor Hsieh | e769867 | 2022-09-23 16:22:28 -0700 | [diff] [blame] | 208 | |
| 209 | let args = OdrefreshArgs { |
| 210 | compilationMode: compilation_mode, |
| 211 | systemDirFd: system_dir_raw_fd, |
| 212 | systemExtDirFd: system_ext_dir_raw_fd, |
| 213 | outputDirFd: output_dir_raw_fd, |
| 214 | stagingDirFd: staging_dir_raw_fd, |
| 215 | targetDirName: target_dir_name.to_string(), |
| 216 | zygoteArch: zygote_arch, |
| 217 | systemServerCompilerFilter: system_server_compiler_filter, |
| 218 | }; |
| 219 | let exit_code = service.odrefresh(&args)?; |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 220 | |
| 221 | drop(fd_server_raii); |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 222 | ExitCode::from_i32(exit_code.into()) |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Victor Hsieh | 9807dcd | 2023-03-14 09:58:53 -0700 | [diff] [blame] | 225 | /// Enable fs-verity to output artifacts according to compos.info in the pending directory. Any |
| 226 | /// error before the completion will just abort, leaving the previous files enabled. |
| 227 | fn enable_fsverity_to_all() -> Result<()> { |
| 228 | let odrefresh_current_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(CURRENT_ARTIFACTS_SUBDIR); |
| 229 | let pending_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(PENDING_ARTIFACTS_SUBDIR); |
| 230 | let mut reader = |
Chris Wailes | 9d09f57 | 2024-01-16 13:31:02 -0800 | [diff] [blame] | 231 | File::open(pending_dir.join("compos.info")).context("Failed to open compos.info")?; |
Victor Hsieh | 9807dcd | 2023-03-14 09:58:53 -0700 | [diff] [blame] | 232 | let compos_info = OdsignInfo::parse_from_reader(&mut reader).context("Failed to parse")?; |
| 233 | |
| 234 | for path_str in compos_info.file_hashes.keys() { |
| 235 | // Need to rebase the directory on to compos-pending first |
| 236 | if let Ok(relpath) = Path::new(path_str).strip_prefix(&odrefresh_current_dir) { |
| 237 | let path = pending_dir.join(relpath); |
| 238 | let file = File::open(&path).with_context(|| format!("Failed to open {:?}", path))?; |
| 239 | // We don't expect error. But when it happens, don't bother handle it here. For |
| 240 | // simplicity, just let odsign do the regular check. |
| 241 | fsverity::enable(file.as_fd()) |
| 242 | .with_context(|| format!("Failed to enable fs-verity to {:?}", path))?; |
| 243 | } else { |
| 244 | warn!("Skip due to unexpected path: {}", path_str); |
| 245 | } |
| 246 | } |
| 247 | Ok(()) |
| 248 | } |
| 249 | |
Victor Hsieh | bebcb87 | 2022-08-26 11:23:16 -0700 | [diff] [blame] | 250 | /// Returns an `OwnedFD` of the directory. |
| 251 | fn open_dir(path: &Path) -> Result<OwnedFd> { |
| 252 | Ok(OwnedFd::from( |
| 253 | OpenOptions::new() |
| 254 | .custom_flags(libc::O_DIRECTORY) |
| 255 | .read(true) // O_DIRECTORY can only be opened with read |
| 256 | .open(path) |
| 257 | .with_context(|| format!("Failed to open {:?} directory as path fd", path))?, |
| 258 | )) |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 259 | } |