| /* | |
| * Copyright (C) 2021 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| //! Common items used by CompOS server and/or clients | |
| pub mod binder; | |
| pub mod compos_client; | |
| pub mod odrefresh; | |
| pub mod timeouts; | |
| use anyhow::{Context, Result, anyhow, bail}; | |
| use compos_client::{CompOsType, VmParameters}; | |
| #[cfg(not(test))] | |
| use compos_wrappers::system_properties; | |
| #[cfg(test)] | |
| use compos_wrappers_with_mocks::mock_system_properties as system_properties; | |
| use log::info; | |
| use std::str::FromStr; | |
| /// The magic word that is prepended to the sha256 of the secure compilation manifest | |
| /// before signing and signature verification. | |
| pub const COMPOS_MANIFEST_MAGIC_PREFIX: &str = "compos_secure_compilation"; | |
| /// VSock port that the CompOS server listens on for RPC binder connections. This should be out of | |
| /// future port range (if happens) that microdroid may reserve for system components. | |
| pub const COMPOS_VSOCK_PORT: u32 = 6432; | |
| /// The root directory where the CompOS APEX is mounted (read only). | |
| pub const COMPOS_APEX_ROOT: &str = "/apex/com.android.compos"; | |
| /// The root of the data directory available for private use by the CompOS APEX. | |
| pub const COMPOS_DATA_ROOT: &str = "/data/misc/apexdata/com.android.compos"; | |
| /// The path to the CompOS dex2oat key cache file. | |
| pub const DEX2OAT_KEY_CACHE_PATH: &str = "/data/misc/odsign/key_cache/compos_dex2oat_key_cache"; | |
| /// The path to the temporary CompOS dex2oat key cache file used during validation. | |
| pub const DEX2OAT_KEY_CACHE_TMP_PATH: &str = | |
| "/data/misc/odsign/key_cache/compos_dex2oat_key_cache.tmp"; | |
| /// The sub-directory where we store information relating to the instance of CompOS used for | |
| /// real compilation. | |
| pub const CURRENT_INSTANCE_DIR: &str = "current"; | |
| /// The sub-directory where we store information relating to the instance of CompOS used for | |
| /// verified dex2oat compilation. | |
| pub const DEX2OAT_INSTANCE_DIR: &str = "dex2oat"; | |
| /// The sub-directory where we store information relating to the instance of CompOS used for | |
| /// tests. | |
| pub const TEST_INSTANCE_DIR: &str = "test"; | |
| /// The file that holds the instance_id of CompOS instance. | |
| pub const INSTANCE_ID_FILE: &str = "instance_id"; | |
| /// The file that holds the instance image for a CompOS instance. | |
| pub const INSTANCE_IMAGE_FILE: &str = "instance.img"; | |
| /// The file that holds the idsig for the CompOS Payload APK. | |
| pub const IDSIG_FILE: &str = "idsig"; | |
| /// The file that holds the idsig for the build manifest APK that makes enumerated files from | |
| /// /system available in CompOS. | |
| pub const IDSIG_MANIFEST_APK_FILE: &str = "idsig_manifest_apk"; | |
| /// The file that holds the idsig for the build manifest APK that makes enumerated files from | |
| /// /system_ext available in CompOS. | |
| pub const IDSIG_MANIFEST_EXT_APK_FILE: &str = "idsig_manifest_ext_apk"; | |
| /// The Android path of fs-verity build manifest APK for /system. | |
| pub const BUILD_MANIFEST_APK_PATH: &str = "/system/etc/security/fsverity/BuildManifest.apk"; | |
| /// The Android path of fs-verity build manifest APK for /system_ext. | |
| pub const BUILD_MANIFEST_SYSTEM_EXT_APK_PATH: &str = | |
| "/system_ext/etc/security/fsverity/BuildManifestSystemExt.apk"; | |
| /// Returns the path of proper VM config for the current device. | |
| pub fn get_vm_config_path(vm_parameters: &VmParameters, has_system_ext: bool) -> Result<String> { | |
| match vm_parameters.compos_type { | |
| CompOsType::Dex2Oat => { | |
| if vm_parameters.prefer_staged { | |
| bail!("Staged configurations for dex2oat are not supported."); | |
| } | |
| Ok(match has_system_ext { | |
| false => "assets/dex2oat_vm_config.json", | |
| true => "assets/dex2oat_vm_config_system_ext.json", | |
| } | |
| .to_owned()) | |
| } | |
| CompOsType::OdRefresh => Ok(match (has_system_ext, vm_parameters.prefer_staged) { | |
| (false, false) => "assets/vm_config.json", | |
| (false, true) => "assets/vm_config_staged.json", | |
| (true, false) => "assets/vm_config_system_ext.json", | |
| (true, true) => "assets/vm_config_system_ext_staged.json", | |
| } | |
| .to_owned()), | |
| } | |
| } | |
| /// Returns the memory size (in MiB) configured for the CompOS VM. | |
| /// | |
| /// This reads the configuration from system properties (with a default of 800 MiB) | |
| /// and applies any vendor adjustments. | |
| pub fn compos_memory_mib() -> Result<i32> { | |
| // Enough memory to complete compilation in the VM, for older versions of ART that don't set the | |
| // property explicitly. | |
| const DEFAULT_MEMORY_MIB: u32 = 800; | |
| let art_requested_mib = | |
| read_property("composd.vm.art.memory_mib.config")?.unwrap_or(DEFAULT_MEMORY_MIB); | |
| let vm_adjustment_mib = read_property("composd.vm.vendor.memory_mib.config")?.unwrap_or(0); | |
| info!( | |
| "Compilation VM memory: ART requests {art_requested_mib} MiB, \ | |
| VM adjust is {vm_adjustment_mib}" | |
| ); | |
| art_requested_mib | |
| .checked_add_signed(vm_adjustment_mib) | |
| .and_then(|x| x.try_into().ok()) | |
| .context("Invalid vm memory adjustment") | |
| } | |
| fn read_property<T: FromStr>(name: &str) -> Result<Option<T>> { | |
| let str = system_properties::read(name).context("Failed to read {name}")?; | |
| str.map(|s| s.parse().map_err(|_| anyhow!("Invalid {name}: {s}"))).transpose() | |
| } |