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 | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 21 | use std::path::PathBuf; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 22 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 23 | use crate::compilation::compile; |
Alan Stokes | 7ec4e7f | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 24 | use crate::signer::Signer; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 25 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 26 | use compos_aidl_interface::aidl::com::android::compos::ICompService::{ |
| 27 | BnCompService, ICompService, |
| 28 | }; |
| 29 | use compos_aidl_interface::aidl::com::android::compos::Metadata::Metadata; |
| 30 | use compos_aidl_interface::binder::{ |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 31 | BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 32 | }; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 33 | use std::ffi::CString; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 34 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 35 | const AUTHFS_SERVICE_NAME: &str = "authfs_service"; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 36 | const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 37 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 38 | /// Constructs a binder object that implements ICompService. |
| 39 | pub fn new_binder(signer: Option<Box<dyn Signer>>) -> Strong<dyn ICompService> { |
| 40 | let service = CompService { dex2oat_path: PathBuf::from(DEX2OAT_PATH), signer }; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 41 | BnCompService::new_binder(service, BinderFeatures::default()) |
| 42 | } |
| 43 | |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 44 | struct CompService { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 45 | dex2oat_path: PathBuf, |
Alan Stokes | 7ec4e7f | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 46 | #[allow(dead_code)] // TODO: Make use of this |
| 47 | signer: Option<Box<dyn Signer>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 50 | impl Interface for CompService {} |
| 51 | |
| 52 | impl ICompService for CompService { |
| 53 | fn execute(&self, args: &[String], metadata: &Metadata) -> BinderResult<i8> { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame^] | 54 | let authfs_service = get_authfs_service()?; |
| 55 | compile(&self.dex2oat_path, args, authfs_service, metadata).map_err(|e| { |
| 56 | new_binder_exception( |
| 57 | ExceptionCode::SERVICE_SPECIFIC, |
| 58 | format!("Compilation failed: {}", e), |
| 59 | ) |
| 60 | }) |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 63 | |
| 64 | fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> { |
| 65 | Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?) |
| 66 | } |
| 67 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 68 | fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status { |
| 69 | Status::new_exception(exception, CString::new(message.as_ref()).as_deref().ok()) |
| 70 | } |