Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [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 | |
| 17 | //! Implementation of IIsolatedCompilationService, called from system server when compilation is |
| 18 | //! desired. |
| 19 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 20 | use crate::instance_manager::InstanceManager; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 21 | use crate::odrefresh; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 22 | use android_system_composd::aidl::android::system::composd::IIsolatedCompilationService::{ |
| 23 | BnIsolatedCompilationService, IIsolatedCompilationService, |
| 24 | }; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 25 | use android_system_composd::binder::{self, BinderFeatures, Interface, Strong}; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 26 | use anyhow::{bail, Context, Result}; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 27 | use binder_common::new_binder_service_specific_error; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 28 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 29 | CompilationResult::CompilationResult, FdAnnotation::FdAnnotation, |
| 30 | }; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 31 | use log::{error, info}; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 32 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 33 | pub struct IsolatedCompilationService { |
| 34 | instance_manager: InstanceManager, |
| 35 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 36 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 37 | pub fn new_binder(instance_manager: InstanceManager) -> Strong<dyn IIsolatedCompilationService> { |
| 38 | let service = IsolatedCompilationService { instance_manager }; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 39 | BnIsolatedCompilationService::new_binder(service, BinderFeatures::default()) |
| 40 | } |
| 41 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 42 | impl Interface for IsolatedCompilationService {} |
| 43 | |
| 44 | impl IIsolatedCompilationService for IsolatedCompilationService { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 45 | fn runForcedCompileForTest(&self) -> binder::Result<()> { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 46 | // TODO - check caller is system or shell/root? |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 47 | to_binder_result(self.do_run_forced_compile_for_test()) |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 48 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 49 | |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 50 | fn compile_cmd( |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 51 | &self, |
| 52 | args: &[String], |
| 53 | fd_annotation: &FdAnnotation, |
| 54 | ) -> binder::Result<CompilationResult> { |
| 55 | // TODO - check caller is odrefresh |
| 56 | to_binder_result(self.do_compile(args, fd_annotation)) |
| 57 | } |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 58 | |
| 59 | fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> binder::Result<i8> { |
| 60 | Err(new_binder_service_specific_error(-1, "Not yet implemented")) |
| 61 | } |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | fn to_binder_result<T>(result: Result<T>) -> binder::Result<T> { |
| 65 | result.map_err(|e| { |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 66 | let message = format!("{:?}", e); |
| 67 | error!("Returning binder error: {}", &message); |
| 68 | new_binder_service_specific_error(-1, message) |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 69 | }) |
| 70 | } |
| 71 | |
| 72 | impl IsolatedCompilationService { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 73 | fn do_run_forced_compile_for_test(&self) -> Result<()> { |
| 74 | info!("runForcedCompileForTest"); |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 75 | |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 76 | let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 77 | |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 78 | let exit_code = odrefresh::run_forced_compile("test-artifacts")?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 79 | |
| 80 | if exit_code != odrefresh::ExitCode::CompilationSuccess { |
| 81 | bail!("Unexpected odrefresh result: {:?}", exit_code); |
| 82 | } |
| 83 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 84 | // The instance is needed until odrefresh is finished |
| 85 | drop(comp_os); |
| 86 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 87 | Ok(()) |
| 88 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 89 | |
| 90 | fn do_compile( |
| 91 | &self, |
| 92 | args: &[String], |
| 93 | fd_annotation: &FdAnnotation, |
| 94 | ) -> Result<CompilationResult> { |
| 95 | let compos = self.instance_manager.get_running_service()?; |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 96 | compos.compile_cmd(args, fd_annotation).context("Compiling") |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 97 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 98 | } |