Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 17 | //! compsvc is a service to run compilation tasks in a PVM upon request. It is able to set up |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 18 | //! file descriptors backed by authfs (via authfs_service) and pass the file descriptors to the |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 19 | //! actual compiler. |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 20 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 21 | use anyhow::Result; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame^] | 22 | use log::warn; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 23 | use std::ffi::CString; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 24 | use std::path::PathBuf; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 25 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 26 | use crate::compilation::compile; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame^] | 27 | use crate::compos_key_service::CompOsKeyService; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 28 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame^] | 29 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 30 | CompOsKeyData::CompOsKeyData, |
| 31 | ICompOsService::{BnCompOsService, ICompOsService}, |
| 32 | Metadata::Metadata, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 33 | }; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 34 | use compos_aidl_interface::binder::{ |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 35 | BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 38 | const AUTHFS_SERVICE_NAME: &str = "authfs_service"; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 39 | const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 40 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 41 | /// Constructs a binder object that implements ICompOsService. |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame^] | 42 | pub fn new_binder(rpc_binder: bool) -> Result<Strong<dyn ICompOsService>> { |
| 43 | let service = CompOsService { |
| 44 | dex2oat_path: PathBuf::from(DEX2OAT_PATH), |
| 45 | key_service: CompOsKeyService::new(rpc_binder)?, |
| 46 | }; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 47 | Ok(BnCompOsService::new_binder(service, BinderFeatures::default())) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 50 | struct CompOsService { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 51 | dex2oat_path: PathBuf, |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 52 | key_service: CompOsKeyService, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 55 | impl Interface for CompOsService {} |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 56 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 57 | impl ICompOsService for CompOsService { |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 58 | fn execute(&self, args: &[String], metadata: &Metadata) -> BinderResult<i8> { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 59 | let authfs_service = get_authfs_service()?; |
| 60 | compile(&self.dex2oat_path, args, authfs_service, metadata).map_err(|e| { |
| 61 | new_binder_exception( |
| 62 | ExceptionCode::SERVICE_SPECIFIC, |
| 63 | format!("Compilation failed: {}", e), |
| 64 | ) |
| 65 | }) |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 66 | } |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame^] | 67 | |
| 68 | fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> { |
| 69 | self.key_service |
| 70 | .do_generate() |
| 71 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 72 | } |
| 73 | |
| 74 | fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> { |
| 75 | Ok(if let Err(e) = self.key_service.do_verify(key_blob, public_key) { |
| 76 | warn!("Signing key verification failed: {}", e.to_string()); |
| 77 | false |
| 78 | } else { |
| 79 | true |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | fn sign(&self, key_blob: &[u8], data: &[u8]) -> BinderResult<Vec<u8>> { |
| 84 | self.key_service |
| 85 | .do_sign(key_blob, data) |
| 86 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 87 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 88 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 89 | |
| 90 | fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> { |
| 91 | Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?) |
| 92 | } |
| 93 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 94 | fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status { |
| 95 | Status::new_exception(exception, CString::new(message.as_ref()).as_deref().ok()) |
| 96 | } |