blob: d9963d185154fbd1ccf82e71699f6e4a096a581b [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 Stokescb732dc2021-11-16 15:18:13 +000032use rustutils::users::{AID_ROOT, AID_SYSTEM};
Alan Stokese5e1d8d2021-11-19 16:31:14 +000033use std::sync::Arc;
Alan Stokes3ef78d92021-09-08 11:51:06 +010034
Alan Stokesa2869d22021-09-22 09:06:41 +010035pub struct IsolatedCompilationService {
Alan Stokese5e1d8d2021-11-19 16:31:14 +000036 instance_manager: Arc<InstanceManager>,
Alan Stokesa2869d22021-09-22 09:06:41 +010037}
Alan Stokes3ef78d92021-09-08 11:51:06 +010038
Alan Stokese5e1d8d2021-11-19 16:31:14 +000039pub fn new_binder(
40 instance_manager: Arc<InstanceManager>,
41) -> Strong<dyn IIsolatedCompilationService> {
Alan Stokes69c610f2021-09-27 14:03:31 +010042 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010043 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
44}
45
Alan Stokes3ef78d92021-09-08 11:51:06 +010046impl Interface for IsolatedCompilationService {}
47
48impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010049 fn startTestCompile(
50 &self,
51 callback: &Strong<dyn ICompilationTaskCallback>,
52 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokescb732dc2021-11-16 15:18:13 +000053 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 Stokes9ca14ca2021-10-20 14:25:57 +010058 to_binder_result(self.do_start_test_compile(callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010059 }
60}
61
Alan Stokesb2cc79e2021-09-14 14:08:46 +010062impl IsolatedCompilationService {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010063 fn do_start_test_compile(
64 &self,
65 callback: &Strong<dyn ICompilationTaskCallback>,
66 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010067 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010068
Alan Stokes9ca14ca2021-10-20 14:25:57 +010069 let task = CompilationTask::start_test_compile(comp_os, callback)?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010070
Alan Stokes9ca14ca2021-10-20 14:25:57 +010071 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
Alan Stokes3ef78d92021-09-08 11:51:06 +010072 }
Alan Stokes3ef78d92021-09-08 11:51:06 +010073}