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 | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 20 | use crate::compilation_task::CompilationTask; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 21 | use crate::instance_manager::InstanceManager; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 22 | use crate::util::to_binder_result; |
| 23 | use android_system_composd::aidl::android::system::composd::{ |
| 24 | ICompilationTask::{BnCompilationTask, ICompilationTask}, |
| 25 | ICompilationTaskCallback::ICompilationTaskCallback, |
| 26 | IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService}, |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 27 | }; |
Alan Stokes | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame^] | 28 | use android_system_composd::binder::{ |
| 29 | self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState, |
| 30 | }; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 31 | use anyhow::{Context, Result}; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 32 | use binder_common::new_binder_service_specific_error; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 33 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 34 | CompilationResult::CompilationResult, FdAnnotation::FdAnnotation, |
| 35 | }; |
Alan Stokes | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame^] | 36 | use rustutils::users::{AID_ROOT, AID_SYSTEM}; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 37 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 38 | pub struct IsolatedCompilationService { |
| 39 | instance_manager: InstanceManager, |
| 40 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 41 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 42 | pub fn new_binder(instance_manager: InstanceManager) -> Strong<dyn IIsolatedCompilationService> { |
| 43 | let service = IsolatedCompilationService { instance_manager }; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 44 | BnIsolatedCompilationService::new_binder(service, BinderFeatures::default()) |
| 45 | } |
| 46 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 47 | impl Interface for IsolatedCompilationService {} |
| 48 | |
| 49 | impl IIsolatedCompilationService for IsolatedCompilationService { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 50 | fn startTestCompile( |
| 51 | &self, |
| 52 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 53 | ) -> binder::Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame^] | 54 | let calling_uid = ThreadState::get_calling_uid(); |
| 55 | // This should only be called by system server, or root while testing |
| 56 | if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT { |
| 57 | return Err(Status::new_exception(ExceptionCode::SECURITY, None)); |
| 58 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 59 | to_binder_result(self.do_start_test_compile(callback)) |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 60 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 61 | |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 62 | fn compile_cmd( |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 63 | &self, |
| 64 | args: &[String], |
| 65 | fd_annotation: &FdAnnotation, |
| 66 | ) -> binder::Result<CompilationResult> { |
Alan Stokes | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame^] | 67 | let calling_uid = ThreadState::get_calling_uid(); |
| 68 | // This should only be called by odrefresh, which runs as root |
| 69 | if calling_uid != AID_ROOT { |
| 70 | return Err(Status::new_exception(ExceptionCode::SECURITY, None)); |
| 71 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 72 | to_binder_result(self.do_compile_cmd(args, fd_annotation)) |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 73 | } |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 74 | |
| 75 | fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> binder::Result<i8> { |
| 76 | Err(new_binder_service_specific_error(-1, "Not yet implemented")) |
| 77 | } |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 80 | impl IsolatedCompilationService { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 81 | fn do_start_test_compile( |
| 82 | &self, |
| 83 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 84 | ) -> Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 85 | let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 86 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 87 | let task = CompilationTask::start_test_compile(comp_os, callback)?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 88 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 89 | Ok(BnCompilationTask::new_binder(task, BinderFeatures::default())) |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 90 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 91 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 92 | fn do_compile_cmd( |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 93 | &self, |
| 94 | args: &[String], |
| 95 | fd_annotation: &FdAnnotation, |
| 96 | ) -> Result<CompilationResult> { |
| 97 | let compos = self.instance_manager.get_running_service()?; |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 98 | compos.compile_cmd(args, fd_annotation).context("Compiling") |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 99 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 100 | } |