blob: 4d9dc5838d65e4caf27fc3a3092175d6a185ab35 [file] [log] [blame]
Alan Stokes3ef78d92021-09-08 11:51:06 +01001/*
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 Stokes9ca14ca2021-10-20 14:25:57 +010020use crate::compilation_task::CompilationTask;
Alan Stokesa2869d22021-09-22 09:06:41 +010021use crate::instance_manager::InstanceManager;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010022use crate::util::to_binder_result;
23use android_system_composd::aidl::android::system::composd::{
24 ICompilationTask::{BnCompilationTask, ICompilationTask},
25 ICompilationTaskCallback::ICompilationTaskCallback,
26 IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService},
Alan Stokes3ef78d92021-09-08 11:51:06 +010027};
Alan Stokescb732dc2021-11-16 15:18:13 +000028use android_system_composd::binder::{
29 self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
30};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010031use anyhow::{Context, Result};
Alan Stokes3189af02021-09-30 17:51:19 +010032use binder_common::new_binder_service_specific_error;
Alan Stokesa2869d22021-09-22 09:06:41 +010033use compos_aidl_interface::aidl::com::android::compos::{
34 CompilationResult::CompilationResult, FdAnnotation::FdAnnotation,
35};
Alan Stokescb732dc2021-11-16 15:18:13 +000036use rustutils::users::{AID_ROOT, AID_SYSTEM};
Alan Stokes3ef78d92021-09-08 11:51:06 +010037
Alan Stokesa2869d22021-09-22 09:06:41 +010038pub struct IsolatedCompilationService {
39 instance_manager: InstanceManager,
40}
Alan Stokes3ef78d92021-09-08 11:51:06 +010041
Alan Stokes69c610f2021-09-27 14:03:31 +010042pub fn new_binder(instance_manager: InstanceManager) -> Strong<dyn IIsolatedCompilationService> {
43 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010044 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
45}
46
Alan Stokes3ef78d92021-09-08 11:51:06 +010047impl Interface for IsolatedCompilationService {}
48
49impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010050 fn startTestCompile(
51 &self,
52 callback: &Strong<dyn ICompilationTaskCallback>,
53 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokescb732dc2021-11-16 15:18:13 +000054 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 Stokes9ca14ca2021-10-20 14:25:57 +010059 to_binder_result(self.do_start_test_compile(callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010060 }
Alan Stokesa2869d22021-09-22 09:06:41 +010061
Victor Hsieh3c044c42021-10-01 17:17:10 -070062 fn compile_cmd(
Alan Stokesa2869d22021-09-22 09:06:41 +010063 &self,
64 args: &[String],
65 fd_annotation: &FdAnnotation,
66 ) -> binder::Result<CompilationResult> {
Alan Stokescb732dc2021-11-16 15:18:13 +000067 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 Stokes9ca14ca2021-10-20 14:25:57 +010072 to_binder_result(self.do_compile_cmd(args, fd_annotation))
Alan Stokesa2869d22021-09-22 09:06:41 +010073 }
Victor Hsieh3c044c42021-10-01 17:17:10 -070074
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 Stokesb2cc79e2021-09-14 14:08:46 +010078}
79
Alan Stokesb2cc79e2021-09-14 14:08:46 +010080impl IsolatedCompilationService {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010081 fn do_start_test_compile(
82 &self,
83 callback: &Strong<dyn ICompilationTaskCallback>,
84 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010085 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010086
Alan Stokes9ca14ca2021-10-20 14:25:57 +010087 let task = CompilationTask::start_test_compile(comp_os, callback)?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010088
Alan Stokes9ca14ca2021-10-20 14:25:57 +010089 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
Alan Stokes3ef78d92021-09-08 11:51:06 +010090 }
Alan Stokesa2869d22021-09-22 09:06:41 +010091
Alan Stokes9ca14ca2021-10-20 14:25:57 +010092 fn do_compile_cmd(
Alan Stokesa2869d22021-09-22 09:06:41 +010093 &self,
94 args: &[String],
95 fd_annotation: &FdAnnotation,
96 ) -> Result<CompilationResult> {
97 let compos = self.instance_manager.get_running_service()?;
Victor Hsieh3c044c42021-10-01 17:17:10 -070098 compos.compile_cmd(args, fd_annotation).context("Compiling")
Alan Stokesa2869d22021-09-22 09:06:41 +010099 }
Alan Stokes3ef78d92021-09-08 11:51:06 +0100100}