blob: 3f31adbc3c86db72688366012ae132c21b12339d [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 Stokesac9aa1a2021-12-14 11:32:13 +000022use crate::odrefresh;
Alan Stokes8c840442021-11-26 15:54:30 +000023use crate::odrefresh_task::OdrefreshTask;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010024use crate::util::to_binder_result;
25use android_system_composd::aidl::android::system::composd::{
26 ICompilationTask::{BnCompilationTask, ICompilationTask},
27 ICompilationTaskCallback::ICompilationTaskCallback,
28 IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService},
Alan Stokes3ef78d92021-09-08 11:51:06 +010029};
Alan Stokescb732dc2021-11-16 15:18:13 +000030use android_system_composd::binder::{
31 self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
32};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010033use anyhow::{Context, Result};
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 }
Victor Hsieh72c774c2021-11-18 15:52:28 -080066
Alan Stokes8c840442021-11-26 15:54:30 +000067 fn startAsyncOdrefresh(
68 &self,
69 callback: &Strong<dyn ICompilationTaskCallback>,
70 ) -> binder::Result<Strong<dyn ICompilationTask>> {
71 check_permissions()?;
72 to_binder_result(self.do_start_async_odrefresh(callback))
73 }
74
Victor Hsieh72c774c2021-11-18 15:52:28 -080075 fn startTestOdrefresh(&self) -> binder::Result<i8> {
Alan Stokes6fc18372021-11-25 17:50:27 +000076 check_permissions()?;
Victor Hsieh72c774c2021-11-18 15:52:28 -080077 to_binder_result(self.do_odrefresh_for_test())
78 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010079}
80
Alan Stokesb2cc79e2021-09-14 14:08:46 +010081impl IsolatedCompilationService {
Alan Stokes6fc18372021-11-25 17:50:27 +000082 fn do_start_staged_apex_compile(
83 &self,
84 callback: &Strong<dyn ICompilationTaskCallback>,
85 ) -> Result<Strong<dyn ICompilationTask>> {
86 // TODO: Try to start the current instance with staged APEXes to see if it works?
87 let comp_os = self.instance_manager.start_pending_instance().context("Starting CompOS")?;
88
89 let task = CompilationTask::start_staged_apex_compile(comp_os, callback)?;
90
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,
96 callback: &Strong<dyn ICompilationTaskCallback>,
97 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes388b88a2021-10-13 16:03:17 +010098 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010099
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100100 let task = CompilationTask::start_test_compile(comp_os, callback)?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +0100101
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100102 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
Alan Stokes3ef78d92021-09-08 11:51:06 +0100103 }
Victor Hsieh72c774c2021-11-18 15:52:28 -0800104
Alan Stokes8c840442021-11-26 15:54:30 +0000105 fn do_start_async_odrefresh(
106 &self,
107 callback: &Strong<dyn ICompilationTaskCallback>,
108 ) -> Result<Strong<dyn ICompilationTask>> {
Alan Stokes8c840442021-11-26 15:54:30 +0000109 let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
110
Alan Stokesac9aa1a2021-12-14 11:32:13 +0000111 let target_dir_name = "test-instance".to_owned();
112 let task = OdrefreshTask::start(comp_os, target_dir_name, callback)?;
Alan Stokes8c840442021-11-26 15:54:30 +0000113
114 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
115 }
116
Victor Hsieh72c774c2021-11-18 15:52:28 -0800117 fn do_odrefresh_for_test(&self) -> Result<i8> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800118 let compos = self
119 .instance_manager
120 .start_test_instance()
121 .context("Starting CompOS for odrefresh test")?;
Alan Stokesac9aa1a2021-12-14 11:32:13 +0000122 let service = compos.get_service();
Victor Hsieh72c774c2021-11-18 15:52:28 -0800123
Alan Stokesac9aa1a2021-12-14 11:32:13 +0000124 let exit_code = odrefresh::run_in_vm(service, "test-artifacts")?;
125 Ok(exit_code as i8)
Victor Hsieh72c774c2021-11-18 15:52:28 -0800126 }
127}
128
Alan Stokes6fc18372021-11-25 17:50:27 +0000129fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800130 let calling_uid = ThreadState::get_calling_uid();
131 // This should only be called by system server, or root while testing
132 if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT {
133 Err(Status::new_exception(ExceptionCode::SECURITY, None))
134 } else {
135 Ok(())
136 }
137}