blob: f4121e764e745e499be61204a65aeaa5407b289f [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 Stokesa2869d22021-09-22 09:06:41 +010020use crate::instance_manager::InstanceManager;
Alan Stokes8c840442021-11-26 15:54:30 +000021use crate::odrefresh_task::OdrefreshTask;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010022use android_system_composd::aidl::android::system::composd::{
23 ICompilationTask::{BnCompilationTask, ICompilationTask},
24 ICompilationTaskCallback::ICompilationTaskCallback,
25 IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService},
Alan Stokes3ef78d92021-09-08 11:51:06 +010026};
Alan Stokescb732dc2021-11-16 15:18:13 +000027use android_system_composd::binder::{
28 self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
29};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010030use anyhow::{Context, Result};
Alan Stokes2d2e4db2022-01-28 16:41:52 +000031use compos_aidl_interface::aidl::com::android::compos::ICompOsService::CompilationMode::CompilationMode;
Alan Stokes126fd512021-12-16 15:00:01 +000032use compos_common::binder::to_binder_result;
Alan Stokesac9aa1a2021-12-14 11:32:13 +000033use rustutils::{users::AID_ROOT, users::AID_SYSTEM};
Alan Stokese5e1d8d2021-11-19 16:31:14 +000034use std::sync::Arc;
Alan Stokes3ef78d92021-09-08 11:51:06 +010035
Alan Stokesa2869d22021-09-22 09:06:41 +010036pub struct IsolatedCompilationService {
Alan Stokese5e1d8d2021-11-19 16:31:14 +000037 instance_manager: Arc<InstanceManager>,
Alan Stokesa2869d22021-09-22 09:06:41 +010038}
Alan Stokes3ef78d92021-09-08 11:51:06 +010039
Alan Stokese5e1d8d2021-11-19 16:31:14 +000040pub fn new_binder(
41 instance_manager: Arc<InstanceManager>,
42) -> Strong<dyn IIsolatedCompilationService> {
Alan Stokes69c610f2021-09-27 14:03:31 +010043 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 Stokes6fc18372021-11-25 17:50:27 +000050 fn startStagedApexCompile(
51 &self,
52 callback: &Strong<dyn ICompilationTaskCallback>,
53 ) -> binder::Result<Strong<dyn ICompilationTask>> {
54 check_permissions()?;
55 to_binder_result(self.do_start_staged_apex_compile(callback))
56 }
57
Alan Stokes9ca14ca2021-10-20 14:25:57 +010058 fn startTestCompile(
59 &self,
60 callback: &Strong<dyn ICompilationTaskCallback>,
61 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokes6fc18372021-11-25 17:50:27 +000062 check_permissions()?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010063 to_binder_result(self.do_start_test_compile(callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010064 }
65}
66
Alan Stokesb2cc79e2021-09-14 14:08:46 +010067impl IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000068 fn do_start_staged_apex_compile(
69 &self,
70 callback: &Strong<dyn ICompilationTaskCallback>,
71 ) -> Result<Strong<dyn ICompilationTask>> {
72 // TODO: Try to start the current instance with staged APEXes to see if it works?
73 let comp_os = self.instance_manager.start_pending_instance().context("Starting CompOS")?;
74
Alan Stokese8d7d002022-01-11 18:08:08 +000075 let target_dir_name = "compos-pending".to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +000076 let task = OdrefreshTask::start(
77 comp_os,
78 CompilationMode::NORMAL_COMPILE,
79 target_dir_name,
80 callback,
81 )?;
Alan Stokes6fc18372021-11-25 17:50:27 +000082
83 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
84 }
85
Alan Stokes9ca14ca2021-10-20 14:25:57 +010086 fn do_start_test_compile(
87 &self,
88 callback: &Strong<dyn ICompilationTaskCallback>,
89 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010090 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010091
Alan Stokes9646db92021-12-14 13:22:33 +000092 let target_dir_name = "test-artifacts".to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +000093 let task = OdrefreshTask::start(
94 comp_os,
95 CompilationMode::TEST_COMPILE,
96 target_dir_name,
97 callback,
98 )?;
Alan Stokes8c840442021-11-26 15:54:30 +000099
100 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
101 }
Victor Hsieh72c774c2021-11-18 15:52:28 -0800102}
103
Alan Stokes6fc18372021-11-25 17:50:27 +0000104fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800105 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}