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 | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame] | 32 | use rustutils::users::{AID_ROOT, AID_SYSTEM}; |
Alan Stokes | e5e1d8d | 2021-11-19 16:31:14 +0000 | [diff] [blame^] | 33 | use std::sync::Arc; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 34 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 35 | pub struct IsolatedCompilationService { |
Alan Stokes | e5e1d8d | 2021-11-19 16:31:14 +0000 | [diff] [blame^] | 36 | instance_manager: Arc<InstanceManager>, |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 37 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 38 | |
Alan Stokes | e5e1d8d | 2021-11-19 16:31:14 +0000 | [diff] [blame^] | 39 | pub fn new_binder( |
| 40 | instance_manager: Arc<InstanceManager>, |
| 41 | ) -> Strong<dyn IIsolatedCompilationService> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 42 | let service = IsolatedCompilationService { instance_manager }; |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 43 | BnIsolatedCompilationService::new_binder(service, BinderFeatures::default()) |
| 44 | } |
| 45 | |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 46 | impl Interface for IsolatedCompilationService {} |
| 47 | |
| 48 | impl IIsolatedCompilationService for IsolatedCompilationService { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 49 | fn startTestCompile( |
| 50 | &self, |
| 51 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 52 | ) -> binder::Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | cb732dc | 2021-11-16 15:18:13 +0000 | [diff] [blame] | 53 | let calling_uid = ThreadState::get_calling_uid(); |
| 54 | // This should only be called by system server, or root while testing |
| 55 | if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT { |
| 56 | return Err(Status::new_exception(ExceptionCode::SECURITY, None)); |
| 57 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 58 | to_binder_result(self.do_start_test_compile(callback)) |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 62 | impl IsolatedCompilationService { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 63 | fn do_start_test_compile( |
| 64 | &self, |
| 65 | callback: &Strong<dyn ICompilationTaskCallback>, |
| 66 | ) -> Result<Strong<dyn ICompilationTask>> { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 67 | let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 68 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 69 | let task = CompilationTask::start_test_compile(comp_os, callback)?; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 70 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 71 | Ok(BnCompilationTask::new_binder(task, BinderFeatures::default())) |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 72 | } |
Alan Stokes | 3ef78d9 | 2021-09-08 11:51:06 +0100 | [diff] [blame] | 73 | } |