blob: 27802e350000c910e8b7ec8cfa71ed3c50a71c1e [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 Hsieh9ed27182021-08-25 15:52:42 -070017//! pvm_exec is a proxy/wrapper command to run compilation task remotely. The most important task
Victor Hsieh272aa242021-02-01 14:19:20 -080018//! for this program is to run a `fd_server` that serves remote file read/write requests.
19//!
Victor Hsieh9ed27182021-08-25 15:52:42 -070020//! It currently works as a command line wrapper to make it easy to schedule an existing dex2oat
21//! task to run in the VM.
Victor Hsieh272aa242021-02-01 14:19:20 -080022//!
Victor Hsieh9ed27182021-08-25 15:52:42 -070023//! Example:
24//! $ adb shell exec 3</input/dex 4<>/output/oat ... pvm_exec --in-fd 3 --out-fd 4 -- dex2oat64 ...
25//!
26//! Note the immediate argument "dex2oat64" right after "--" is not really used. It is only for
27//! ergonomics.
Victor Hsieh272aa242021-02-01 14:19:20 -080028
29use anyhow::{bail, Context, Result};
Victor Hsieha7d37862021-06-04 17:14:20 -070030use binder::unstable_api::{new_spibinder, AIBinder};
31use binder::FromIBinder;
Victor Hsieh9ed27182021-08-25 15:52:42 -070032use log::{debug, error, warn};
Victor Hsieh272aa242021-02-01 14:19:20 -080033use minijail::Minijail;
34use nix::fcntl::{fcntl, FcntlArg::F_GETFD};
Victor Hsieh272aa242021-02-01 14:19:20 -080035use std::os::unix::io::RawFd;
36use std::path::Path;
37use std::process::exit;
38
39use compos_aidl_interface::aidl::com::android::compos::{
Victor Hsieh13333e82021-09-03 15:17:32 -070040 FdAnnotation::FdAnnotation, ICompOsService::ICompOsService,
Victor Hsieh272aa242021-02-01 14:19:20 -080041};
42use compos_aidl_interface::binder::Strong;
43
Victor Hsieha7d37862021-06-04 17:14:20 -070044mod common;
45use common::{SERVICE_NAME, VSOCK_PORT};
Victor Hsieh272aa242021-02-01 14:19:20 -080046
Victor Hsieha7d37862021-06-04 17:14:20 -070047const FD_SERVER_BIN: &str = "/apex/com.android.virt/bin/fd_server";
48
Victor Hsieha64194b2021-08-06 17:43:36 -070049fn get_local_service() -> Result<Strong<dyn ICompOsService>> {
Victor Hsieha7d37862021-06-04 17:14:20 -070050 compos_aidl_interface::binder::get_interface(SERVICE_NAME).context("get local binder")
51}
52
Victor Hsieha64194b2021-08-06 17:43:36 -070053fn get_rpc_binder(cid: u32) -> Result<Strong<dyn ICompOsService>> {
Victor Hsieha7d37862021-06-04 17:14:20 -070054 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
55 // safely taken by new_spibinder.
56 let ibinder = unsafe {
57 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, VSOCK_PORT) as *mut AIBinder)
58 };
59 if let Some(ibinder) = ibinder {
Victor Hsieha64194b2021-08-06 17:43:36 -070060 <dyn ICompOsService>::try_from(ibinder).context("Cannot connect to RPC service")
Victor Hsieha7d37862021-06-04 17:14:20 -070061 } else {
62 bail!("Invalid raw AIBinder")
63 }
Victor Hsieh272aa242021-02-01 14:19:20 -080064}
65
Victor Hsieh13333e82021-09-03 15:17:32 -070066fn spawn_fd_server(fd_annotation: &FdAnnotation, debuggable: bool) -> Result<Minijail> {
Victor Hsieh272aa242021-02-01 14:19:20 -080067 let mut inheritable_fds = if debuggable {
68 vec![1, 2] // inherit/redirect stdout/stderr for debugging
69 } else {
70 vec![]
71 };
72
Victor Hsieha7d37862021-06-04 17:14:20 -070073 let mut args = vec![FD_SERVER_BIN.to_string(), "--rpc-binder".to_string()];
Victor Hsieh13333e82021-09-03 15:17:32 -070074 for fd in &fd_annotation.input_fds {
Victor Hsieh272aa242021-02-01 14:19:20 -080075 args.push("--ro-fds".to_string());
Victor Hsieh13333e82021-09-03 15:17:32 -070076 args.push(fd.to_string());
77 inheritable_fds.push(*fd);
Victor Hsieh272aa242021-02-01 14:19:20 -080078 }
Victor Hsieh13333e82021-09-03 15:17:32 -070079 for fd in &fd_annotation.output_fds {
Victor Hsieh272aa242021-02-01 14:19:20 -080080 args.push("--rw-fds".to_string());
Victor Hsieh13333e82021-09-03 15:17:32 -070081 args.push(fd.to_string());
82 inheritable_fds.push(*fd);
Victor Hsieh272aa242021-02-01 14:19:20 -080083 }
84
85 let jail = Minijail::new()?;
86 let _pid = jail.run(Path::new(FD_SERVER_BIN), &inheritable_fds, &args)?;
87 Ok(jail)
88}
89
90fn is_fd_valid(fd: RawFd) -> Result<bool> {
91 let retval = fcntl(fd, F_GETFD)?;
92 Ok(retval >= 0)
93}
94
95fn parse_arg_fd(arg: &str) -> Result<RawFd> {
96 let fd = arg.parse::<RawFd>()?;
97 if !is_fd_valid(fd)? {
98 bail!("Bad FD: {}", fd);
99 }
100 Ok(fd)
101}
102
103struct Config {
104 args: Vec<String>,
Victor Hsieh13333e82021-09-03 15:17:32 -0700105 fd_annotation: FdAnnotation,
Victor Hsieha7d37862021-06-04 17:14:20 -0700106 cid: Option<u32>,
Victor Hsieh272aa242021-02-01 14:19:20 -0800107 debuggable: bool,
108}
109
110fn parse_args() -> Result<Config> {
111 #[rustfmt::skip]
112 let matches = clap::App::new("pvm_exec")
113 .arg(clap::Arg::with_name("in-fd")
114 .long("in-fd")
115 .takes_value(true)
116 .multiple(true)
117 .use_delimiter(true))
118 .arg(clap::Arg::with_name("out-fd")
119 .long("out-fd")
120 .takes_value(true)
121 .multiple(true)
122 .use_delimiter(true))
Victor Hsieha7d37862021-06-04 17:14:20 -0700123 .arg(clap::Arg::with_name("cid")
124 .takes_value(true)
125 .long("cid"))
Victor Hsieh272aa242021-02-01 14:19:20 -0800126 .arg(clap::Arg::with_name("debug")
127 .long("debug"))
128 .arg(clap::Arg::with_name("args")
129 .last(true)
130 .required(true)
131 .multiple(true))
132 .get_matches();
133
Victor Hsieh13333e82021-09-03 15:17:32 -0700134 let results: Result<Vec<_>> =
135 matches.values_of("in-fd").unwrap_or_default().map(parse_arg_fd).collect();
136 let input_fds = results?;
Victor Hsieh272aa242021-02-01 14:19:20 -0800137
Victor Hsieh13333e82021-09-03 15:17:32 -0700138 let results: Result<Vec<_>> =
139 matches.values_of("out-fd").unwrap_or_default().map(parse_arg_fd).collect();
140 let output_fds = results?;
Victor Hsieh272aa242021-02-01 14:19:20 -0800141
142 let args: Vec<_> = matches.values_of("args").unwrap().map(|s| s.to_string()).collect();
Victor Hsieha7d37862021-06-04 17:14:20 -0700143 let cid =
144 if let Some(arg) = matches.value_of("cid") { Some(arg.parse::<u32>()?) } else { None };
Victor Hsieh272aa242021-02-01 14:19:20 -0800145 let debuggable = matches.is_present("debug");
146
Victor Hsieh13333e82021-09-03 15:17:32 -0700147 Ok(Config { args, fd_annotation: FdAnnotation { input_fds, output_fds }, cid, debuggable })
Victor Hsieh272aa242021-02-01 14:19:20 -0800148}
149
150fn main() -> Result<()> {
Victor Hsieh4a654592021-08-19 09:08:19 -0700151 let debuggable = env!("TARGET_BUILD_VARIANT") != "user";
152 let log_level = if debuggable { log::Level::Trace } else { log::Level::Info };
153 android_logger::init_once(
154 android_logger::Config::default().with_tag("pvm_exec").with_min_level(log_level),
155 );
156
Victor Hsieh272aa242021-02-01 14:19:20 -0800157 // 1. Parse the command line arguments for collect execution data.
Victor Hsieh13333e82021-09-03 15:17:32 -0700158 let Config { args, fd_annotation, cid, debuggable } = parse_args()?;
Victor Hsieh272aa242021-02-01 14:19:20 -0800159
160 // 2. Spawn and configure a fd_server to serve remote read/write requests.
Victor Hsieh13333e82021-09-03 15:17:32 -0700161 let fd_server_jail = spawn_fd_server(&fd_annotation, debuggable)?;
Victor Hsieh272aa242021-02-01 14:19:20 -0800162 let fd_server_lifetime = scopeguard::guard(fd_server_jail, |fd_server_jail| {
163 if let Err(e) = fd_server_jail.kill() {
164 if !matches!(e, minijail::Error::Killed(_)) {
165 warn!("Failed to kill fd_server: {}", e);
166 }
167 }
168 });
169
170 // 3. Send the command line args to the remote to execute.
Victor Hsieha7d37862021-06-04 17:14:20 -0700171 let service = if let Some(cid) = cid { get_rpc_binder(cid) } else { get_local_service() }?;
Victor Hsieh13333e82021-09-03 15:17:32 -0700172 let result = service.compile(&args, &fd_annotation).context("Binder call failed")?;
Victor Hsieh9ed27182021-08-25 15:52:42 -0700173
174 // TODO: store/use the signature
175 debug!(
176 "Signature length: oat {}, vdex {}, image {}",
177 result.oatSignature.len(),
178 result.vdexSignature.len(),
179 result.imageSignature.len()
180 );
Victor Hsieh272aa242021-02-01 14:19:20 -0800181
182 // Be explicit about the lifetime, which should last at least until the task is finished.
183 drop(fd_server_lifetime);
184
Victor Hsieh9ed27182021-08-25 15:52:42 -0700185 if result.exitCode > 0 {
186 error!("remote execution failed with exit code {}", result.exitCode);
187 exit(result.exitCode as i32);
Victor Hsieh272aa242021-02-01 14:19:20 -0800188 }
189 Ok(())
190}