blob: b69b053160fc314690e5beb9968d41970529c823 [file] [log] [blame]
Victor Hsieh272aa242021-02-01 14:19:20 -08001/*
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
Victor Hsieh51789de2021-08-06 16:50:49 -070017//! compsvc is a service to run compilation tasks in a PVM upon request. It is able to set up
Victor Hsiehebb1d902021-08-06 13:00:18 -070018//! file descriptors backed by authfs (via authfs_service) and pass the file descriptors to the
Victor Hsieh51789de2021-08-06 16:50:49 -070019//! actual compiler.
Victor Hsieh272aa242021-02-01 14:19:20 -080020
Victor Hsieha64194b2021-08-06 17:43:36 -070021use anyhow::Result;
22use std::ffi::CString;
Victor Hsieh51789de2021-08-06 16:50:49 -070023use std::path::PathBuf;
Victor Hsieh272aa242021-02-01 14:19:20 -080024
Victor Hsieh51789de2021-08-06 16:50:49 -070025use crate::compilation::compile;
Victor Hsieha64194b2021-08-06 17:43:36 -070026use crate::compos_key_service::{CompOsKeyService, KeystoreNamespace};
Alan Stokes7ec4e7f2021-07-21 11:29:10 +010027use crate::signer::Signer;
Victor Hsieh51789de2021-08-06 16:50:49 -070028use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService;
Victor Hsieha64194b2021-08-06 17:43:36 -070029use compos_aidl_interface::aidl::com::android::compos::ICompOsService::{
30 BnCompOsService, ICompOsService,
Victor Hsieh272aa242021-02-01 14:19:20 -080031};
32use compos_aidl_interface::aidl::com::android::compos::Metadata::Metadata;
33use compos_aidl_interface::binder::{
Victor Hsieh51789de2021-08-06 16:50:49 -070034 BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong,
Victor Hsieh272aa242021-02-01 14:19:20 -080035};
36
Victor Hsiehebb1d902021-08-06 13:00:18 -070037const AUTHFS_SERVICE_NAME: &str = "authfs_service";
Victor Hsieh51789de2021-08-06 16:50:49 -070038const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64";
Alan Stokes9e2c5d52021-07-21 11:29:10 +010039
Victor Hsieha64194b2021-08-06 17:43:36 -070040/// Constructs a binder object that implements ICompOsService.
41pub fn new_binder(
42 rpc_binder: bool,
43 signer: Option<Box<dyn Signer>>,
44) -> Result<Strong<dyn ICompOsService>> {
45 let namespace =
46 if rpc_binder { KeystoreNamespace::VmPayload } else { KeystoreNamespace::Odsign };
47 let key_service = CompOsKeyService::new(namespace)?;
48
49 let service = CompOsService { dex2oat_path: PathBuf::from(DEX2OAT_PATH), signer, key_service };
50 Ok(BnCompOsService::new_binder(service, BinderFeatures::default()))
Alan Stokes9e2c5d52021-07-21 11:29:10 +010051}
52
Victor Hsieha64194b2021-08-06 17:43:36 -070053struct CompOsService {
Victor Hsieh51789de2021-08-06 16:50:49 -070054 dex2oat_path: PathBuf,
Alan Stokes7ec4e7f2021-07-21 11:29:10 +010055 #[allow(dead_code)] // TODO: Make use of this
56 signer: Option<Box<dyn Signer>>,
Victor Hsieha64194b2021-08-06 17:43:36 -070057 #[allow(dead_code)] // TODO: Make use of this
58 key_service: CompOsKeyService,
Victor Hsieh272aa242021-02-01 14:19:20 -080059}
60
Victor Hsieha64194b2021-08-06 17:43:36 -070061impl Interface for CompOsService {}
Victor Hsieh272aa242021-02-01 14:19:20 -080062
Victor Hsieha64194b2021-08-06 17:43:36 -070063impl ICompOsService for CompOsService {
Victor Hsieh272aa242021-02-01 14:19:20 -080064 fn execute(&self, args: &[String], metadata: &Metadata) -> BinderResult<i8> {
Victor Hsieh51789de2021-08-06 16:50:49 -070065 let authfs_service = get_authfs_service()?;
66 compile(&self.dex2oat_path, args, authfs_service, metadata).map_err(|e| {
67 new_binder_exception(
68 ExceptionCode::SERVICE_SPECIFIC,
69 format!("Compilation failed: {}", e),
70 )
71 })
Victor Hsieh272aa242021-02-01 14:19:20 -080072 }
73}
Victor Hsiehebb1d902021-08-06 13:00:18 -070074
75fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> {
76 Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?)
77}
78
Victor Hsiehebb1d902021-08-06 13:00:18 -070079fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status {
80 Status::new_exception(exception, CString::new(message.as_ref()).as_deref().ok())
81}