blob: a9b8202c5d6c45568db8791f03a6202a51b030d7 [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,
Victor Hsiehbac923e2022-02-22 21:43:36 +000025 IIsolatedCompilationService::{
26 ApexSource::ApexSource, BnIsolatedCompilationService, IIsolatedCompilationService,
27 },
Alan Stokes3ef78d92021-09-08 11:51:06 +010028};
Alan Stokescb732dc2021-11-16 15:18:13 +000029use android_system_composd::binder::{
30 self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
31};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010032use anyhow::{Context, Result};
Alan Stokes2d2e4db2022-01-28 16:41:52 +000033use compos_aidl_interface::aidl::com::android::compos::ICompOsService::CompilationMode::CompilationMode;
Alan Stokes126fd512021-12-16 15:00:01 +000034use compos_common::binder::to_binder_result;
Alan Stokes16fb8552022-02-10 15:07:27 +000035use compos_common::odrefresh::{PENDING_ARTIFACTS_SUBDIR, TEST_ARTIFACTS_SUBDIR};
Alan Stokesac9aa1a2021-12-14 11:32:13 +000036use rustutils::{users::AID_ROOT, users::AID_SYSTEM};
Alan Stokese5e1d8d2021-11-19 16:31:14 +000037use std::sync::Arc;
Alan Stokes3ef78d92021-09-08 11:51:06 +010038
Alan Stokesa2869d22021-09-22 09:06:41 +010039pub struct IsolatedCompilationService {
Alan Stokese5e1d8d2021-11-19 16:31:14 +000040 instance_manager: Arc<InstanceManager>,
Alan Stokesa2869d22021-09-22 09:06:41 +010041}
Alan Stokes3ef78d92021-09-08 11:51:06 +010042
Alan Stokese5e1d8d2021-11-19 16:31:14 +000043pub fn new_binder(
44 instance_manager: Arc<InstanceManager>,
45) -> Strong<dyn IIsolatedCompilationService> {
Alan Stokes69c610f2021-09-27 14:03:31 +010046 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010047 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
48}
49
Alan Stokes3ef78d92021-09-08 11:51:06 +010050impl Interface for IsolatedCompilationService {}
51
52impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000053 fn startStagedApexCompile(
54 &self,
55 callback: &Strong<dyn ICompilationTaskCallback>,
56 ) -> binder::Result<Strong<dyn ICompilationTask>> {
57 check_permissions()?;
58 to_binder_result(self.do_start_staged_apex_compile(callback))
59 }
60
Alan Stokes9ca14ca2021-10-20 14:25:57 +010061 fn startTestCompile(
62 &self,
Victor Hsiehbac923e2022-02-22 21:43:36 +000063 apex_source: ApexSource,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010064 callback: &Strong<dyn ICompilationTaskCallback>,
65 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokes6fc18372021-11-25 17:50:27 +000066 check_permissions()?;
Victor Hsiehbac923e2022-02-22 21:43:36 +000067 let prefer_staged = match apex_source {
68 ApexSource::NoStaged => false,
69 ApexSource::PreferStaged => true,
70 _ => unreachable!("Invalid ApexSource {:?}", apex_source),
71 };
72 to_binder_result(self.do_start_test_compile(prefer_staged, callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010073 }
74}
75
Alan Stokesb2cc79e2021-09-14 14:08:46 +010076impl IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000077 fn do_start_staged_apex_compile(
78 &self,
79 callback: &Strong<dyn ICompilationTaskCallback>,
80 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes6542fdd2022-02-17 15:21:46 +000081 let comp_os = self.instance_manager.start_current_instance().context("Starting CompOS")?;
Alan Stokes6fc18372021-11-25 17:50:27 +000082
Alan Stokes16fb8552022-02-10 15:07:27 +000083 let target_dir_name = PENDING_ARTIFACTS_SUBDIR.to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +000084 let task = OdrefreshTask::start(
85 comp_os,
86 CompilationMode::NORMAL_COMPILE,
87 target_dir_name,
88 callback,
89 )?;
Alan Stokes6fc18372021-11-25 17:50:27 +000090
91 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
92 }
93
Alan Stokes9ca14ca2021-10-20 14:25:57 +010094 fn do_start_test_compile(
95 &self,
Victor Hsiehbac923e2022-02-22 21:43:36 +000096 prefer_staged: bool,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010097 callback: &Strong<dyn ICompilationTaskCallback>,
98 ) -> Result<Strong<dyn ICompilationTask>> {
Victor Hsiehbac923e2022-02-22 21:43:36 +000099 let comp_os =
100 self.instance_manager.start_test_instance(prefer_staged).context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +0100101
Alan Stokes16fb8552022-02-10 15:07:27 +0000102 let target_dir_name = TEST_ARTIFACTS_SUBDIR.to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +0000103 let task = OdrefreshTask::start(
104 comp_os,
105 CompilationMode::TEST_COMPILE,
106 target_dir_name,
107 callback,
108 )?;
Alan Stokes8c840442021-11-26 15:54:30 +0000109
110 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
111 }
Victor Hsieh72c774c2021-11-18 15:52:28 -0800112}
113
Alan Stokes6fc18372021-11-25 17:50:27 +0000114fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800115 let calling_uid = ThreadState::get_calling_uid();
116 // This should only be called by system server, or root while testing
117 if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT {
118 Err(Status::new_exception(ExceptionCode::SECURITY, None))
119 } else {
120 Ok(())
121 }
122}