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 | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 21 | use crate::odrefresh_task::OdrefreshTask; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 22 | use android_system_composd::aidl::android::system::composd::{ |
| 23 | ICompilationTask::{BnCompilationTask, ICompilationTask}, |
| 24 | ICompilationTaskCallback::ICompilationTaskCallback, |
| 25 | IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService}, |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 26 | }; |
Alan Stokes | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame] | 27 | use android_system_composd::binder::{ |
| 28 | self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState, |
| 29 | }; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 30 | use anyhow::{Context, Result}; |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 31 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::CompilationMode::CompilationMode; |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame] | 32 | use compos_common::binder::to_binder_result; |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 33 | use compos_common::odrefresh::{PENDING_ARTIFACTS_SUBDIR, TEST_ARTIFACTS_SUBDIR}; |
Alan Stokes | ac9aa1a | 2021-12-14 11:32:13 +0000 | [diff] [blame] | 34 | use rustutils::{users::AID_ROOT, users::AID_SYSTEM}; |
Alan Stokes | e5e1d8d | 2021-11-19 16:31:14 +0000 | [diff] [blame] | 35 | use std::sync::Arc; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 36 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 37 | pub struct IsolatedCompilationService { |
Alan Stokes | e5e1d8d | 2021-11-19 16:31:14 +0000 | [diff] [blame] | 38 | instance_manager: Arc<InstanceManager>, |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 39 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 40 | |
Alan Stokes | e5e1d8d | 2021-11-19 16:31:14 +0000 | [diff] [blame] | 41 | pub fn new_binder( |
| 42 | instance_manager: Arc<InstanceManager>, |
| 43 | ) -> Strong<dyn IIsolatedCompilationService> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 44 | let service = IsolatedCompilationService { instance_manager }; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 45 | BnIsolatedCompilationService::new_binder(service, BinderFeatures::default()) |
| 46 | } |
| 47 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 48 | impl Interface for IsolatedCompilationService {} |
| 49 | |
| 50 | impl IIsolatedCompilationService for IsolatedCompilationService { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 51 | fn startStagedApexCompile( |
| 52 | &self, |
| 53 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 54 | ) -> binder::Result<Strong<dyn ICompilationTask>> { |
| 55 | check_permissions()?; |
| 56 | to_binder_result(self.do_start_staged_apex_compile(callback)) |
| 57 | } |
| 58 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 59 | fn startTestCompile( |
| 60 | &self, |
| 61 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 62 | ) -> binder::Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 63 | check_permissions()?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 64 | to_binder_result(self.do_start_test_compile(callback)) |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 68 | impl IsolatedCompilationService { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 69 | fn do_start_staged_apex_compile( |
| 70 | &self, |
| 71 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 72 | ) -> Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame^] | 73 | let comp_os = self.instance_manager.start_current_instance().context("Starting CompOS")?; |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 74 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 75 | let target_dir_name = PENDING_ARTIFACTS_SUBDIR.to_owned(); |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 76 | let task = OdrefreshTask::start( |
| 77 | comp_os, |
| 78 | CompilationMode::NORMAL_COMPILE, |
| 79 | target_dir_name, |
| 80 | callback, |
| 81 | )?; |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 82 | |
| 83 | Ok(BnCompilationTask::new_binder(task, BinderFeatures::default())) |
| 84 | } |
| 85 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 86 | fn do_start_test_compile( |
| 87 | &self, |
| 88 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 89 | ) -> Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 90 | let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 91 | |
Alan Stokes | 16fb855 | 2022-02-10 15:07:27 +0000 | [diff] [blame] | 92 | let target_dir_name = TEST_ARTIFACTS_SUBDIR.to_owned(); |
Alan Stokes | 2d2e4db | 2022-01-28 16:41:52 +0000 | [diff] [blame] | 93 | let task = OdrefreshTask::start( |
| 94 | comp_os, |
| 95 | CompilationMode::TEST_COMPILE, |
| 96 | target_dir_name, |
| 97 | callback, |
| 98 | )?; |
Alan Stokes | 8c84044 | 2021-11-26 15:54:30 +0000 | [diff] [blame] | 99 | |
| 100 | Ok(BnCompilationTask::new_binder(task, BinderFeatures::default())) |
| 101 | } |
Victor Hsieh | 72c774c | 2021-11-18 15:52:28 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 104 | fn check_permissions() -> binder::Result<()> { |
Victor Hsieh | 72c774c | 2021-11-18 15:52:28 -0800 | [diff] [blame] | 105 | let calling_uid = ThreadState::get_calling_uid(); |
| 106 | // This should only be called by system server, or root while testing |
| 107 | if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT { |
| 108 | Err(Status::new_exception(ExceptionCode::SECURITY, None)) |
| 109 | } else { |
| 110 | Ok(()) |
| 111 | } |
| 112 | } |