blob: 8e5586e443ffccc1d24a17ac9ccfc572122c9de3 [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 Stokes16fb8552022-02-10 15:07:27 +000033use compos_common::odrefresh::{PENDING_ARTIFACTS_SUBDIR, TEST_ARTIFACTS_SUBDIR};
Alan Stokesac9aa1a2021-12-14 11:32:13 +000034use rustutils::{users::AID_ROOT, users::AID_SYSTEM};
Alan Stokese5e1d8d2021-11-19 16:31:14 +000035use std::sync::Arc;
Alan Stokes3ef78d92021-09-08 11:51:06 +010036
Alan Stokesa2869d22021-09-22 09:06:41 +010037pub struct IsolatedCompilationService {
Alan Stokese5e1d8d2021-11-19 16:31:14 +000038 instance_manager: Arc<InstanceManager>,
Alan Stokesa2869d22021-09-22 09:06:41 +010039}
Alan Stokes3ef78d92021-09-08 11:51:06 +010040
Alan Stokese5e1d8d2021-11-19 16:31:14 +000041pub fn new_binder(
42 instance_manager: Arc<InstanceManager>,
43) -> Strong<dyn IIsolatedCompilationService> {
Alan Stokes69c610f2021-09-27 14:03:31 +010044 let service = IsolatedCompilationService { instance_manager };
Alan Stokes3ef78d92021-09-08 11:51:06 +010045 BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
46}
47
Alan Stokes3ef78d92021-09-08 11:51:06 +010048impl Interface for IsolatedCompilationService {}
49
50impl IIsolatedCompilationService for IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000051 fn startStagedApexCompile(
52 &self,
53 callback: &Strong<dyn ICompilationTaskCallback>,
54 ) -> binder::Result<Strong<dyn ICompilationTask>> {
55 check_permissions()?;
56 to_binder_result(self.do_start_staged_apex_compile(callback))
57 }
58
Alan Stokes9ca14ca2021-10-20 14:25:57 +010059 fn startTestCompile(
60 &self,
61 callback: &Strong<dyn ICompilationTaskCallback>,
62 ) -> binder::Result<Strong<dyn ICompilationTask>> {
Alan Stokes6fc18372021-11-25 17:50:27 +000063 check_permissions()?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010064 to_binder_result(self.do_start_test_compile(callback))
Alan Stokesb2cc79e2021-09-14 14:08:46 +010065 }
66}
67
Alan Stokesb2cc79e2021-09-14 14:08:46 +010068impl IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000069 fn do_start_staged_apex_compile(
70 &self,
71 callback: &Strong<dyn ICompilationTaskCallback>,
72 ) -> Result<Strong<dyn ICompilationTask>> {
73 // TODO: Try to start the current instance with staged APEXes to see if it works?
74 let comp_os = self.instance_manager.start_pending_instance().context("Starting CompOS")?;
75
Alan Stokes16fb8552022-02-10 15:07:27 +000076 let target_dir_name = PENDING_ARTIFACTS_SUBDIR.to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +000077 let task = OdrefreshTask::start(
78 comp_os,
79 CompilationMode::NORMAL_COMPILE,
80 target_dir_name,
81 callback,
82 )?;
Alan Stokes6fc18372021-11-25 17:50:27 +000083
84 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
85 }
86
Alan Stokes9ca14ca2021-10-20 14:25:57 +010087 fn do_start_test_compile(
88 &self,
89 callback: &Strong<dyn ICompilationTaskCallback>,
90 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010091 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010092
Alan Stokes16fb8552022-02-10 15:07:27 +000093 let target_dir_name = TEST_ARTIFACTS_SUBDIR.to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +000094 let task = OdrefreshTask::start(
95 comp_os,
96 CompilationMode::TEST_COMPILE,
97 target_dir_name,
98 callback,
99 )?;
Alan Stokes8c840442021-11-26 15:54:30 +0000100
101 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
102 }
Victor Hsieh72c774c2021-11-18 15:52:28 -0800103}
104
Alan Stokes6fc18372021-11-25 17:50:27 +0000105fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800106 let calling_uid = ThreadState::get_calling_uid();
107 // This should only be called by system server, or root while testing
108 if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT {
109 Err(Status::new_exception(ExceptionCode::SECURITY, None))
110 } else {
111 Ok(())
112 }
113}