blob: 1e38eee4277bf912b8fe2592f37ce1e09e32510c [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 Stokes9ca14ca2021-10-20 14:25:57 +010029use anyhow::{Context, Result};
Alan Stokes0e82b502022-08-08 14:44:48 +010030use binder::{self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState};
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>,
Inseob Kim74a4b052025-01-24 16:50:57 +090054 os: &str,
Alan Stokes6fc18372021-11-25 17:50:27 +000055 ) -> binder::Result<Strong<dyn ICompilationTask>> {
56 check_permissions()?;
Inseob Kim74a4b052025-01-24 16:50:57 +090057 to_binder_result(self.do_start_staged_apex_compile(callback, os))
Alan Stokes6fc18372021-11-25 17:50:27 +000058 }
59
Alan Stokes9ca14ca2021-10-20 14:25:57 +010060 fn startTestCompile(
61 &self,
Victor Hsiehbac923e2022-02-22 21:43:36 +000062 apex_source: ApexSource,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010063 callback: &Strong<dyn ICompilationTaskCallback>,
Inseob Kim4657d0e2024-11-28 13:34:10 +090064 os: &str,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010065 ) -> 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 };
Inseob Kim4657d0e2024-11-28 13:34:10 +090072 to_binder_result(self.do_start_test_compile(prefer_staged, callback, os))
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>,
Inseob Kim74a4b052025-01-24 16:50:57 +090080 os: &str,
Alan Stokes6fc18372021-11-25 17:50:27 +000081 ) -> Result<Strong<dyn ICompilationTask>> {
Inseob Kim74a4b052025-01-24 16:50:57 +090082 let comp_os =
83 self.instance_manager.start_current_instance(os).context("Starting CompOS")?;
Alan Stokes6fc18372021-11-25 17:50:27 +000084
Alan Stokes16fb8552022-02-10 15:07:27 +000085 let target_dir_name = PENDING_ARTIFACTS_SUBDIR.to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +000086 let task = OdrefreshTask::start(
87 comp_os,
88 CompilationMode::NORMAL_COMPILE,
89 target_dir_name,
90 callback,
91 )?;
Alan Stokes6fc18372021-11-25 17:50:27 +000092
93 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
94 }
95
Alan Stokes9ca14ca2021-10-20 14:25:57 +010096 fn do_start_test_compile(
97 &self,
Victor Hsiehbac923e2022-02-22 21:43:36 +000098 prefer_staged: bool,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010099 callback: &Strong<dyn ICompilationTaskCallback>,
Inseob Kim4657d0e2024-11-28 13:34:10 +0900100 os: &str,
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100101 ) -> Result<Strong<dyn ICompilationTask>> {
Inseob Kim4657d0e2024-11-28 13:34:10 +0900102 let comp_os = self
103 .instance_manager
104 .start_test_instance(prefer_staged, os)
105 .context("Starting CompOS")?;
Alan Stokesb2cc79e2021-09-14 14:08:46 +0100106
Alan Stokes16fb8552022-02-10 15:07:27 +0000107 let target_dir_name = TEST_ARTIFACTS_SUBDIR.to_owned();
Alan Stokes2d2e4db2022-01-28 16:41:52 +0000108 let task = OdrefreshTask::start(
109 comp_os,
110 CompilationMode::TEST_COMPILE,
111 target_dir_name,
112 callback,
113 )?;
Alan Stokes8c840442021-11-26 15:54:30 +0000114
115 Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
116 }
Victor Hsieh72c774c2021-11-18 15:52:28 -0800117}
118
Alan Stokes6fc18372021-11-25 17:50:27 +0000119fn check_permissions() -> binder::Result<()> {
Victor Hsieh72c774c2021-11-18 15:52:28 -0800120 let calling_uid = ThreadState::get_calling_uid();
121 // This should only be called by system server, or root while testing
122 if calling_uid != AID_SYSTEM && calling_uid != AID_ROOT {
123 Err(Status::new_exception(ExceptionCode::SECURITY, None))
124 } else {
125 Ok(())
126 }
127}