1 /*
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 //! Helpers for running odrefresh
18 
19 use anyhow::{anyhow, Result};
20 use num_derive::FromPrimitive;
21 use num_traits::FromPrimitive;
22 
23 /// The path to the odrefresh binary
24 pub const ODREFRESH_PATH: &str = "/apex/com.android.art/bin/odrefresh";
25 
26 /// The path under which odrefresh writes compiled artifacts
27 pub const ODREFRESH_OUTPUT_ROOT_DIR: &str = "/data/misc/apexdata/com.android.art";
28 
29 /// The directory under ODREFRESH_OUTPUT_ROOT_DIR where pending artifacts are written
30 pub const PENDING_ARTIFACTS_SUBDIR: &str = "compos-pending";
31 
32 /// The directory under ODREFRESH_OUTPUT_ROOT_DIR where test artifacts are written
33 pub const TEST_ARTIFACTS_SUBDIR: &str = "test-artifacts";
34 
35 /// The directory under ODREFRESH_OUTPUT_ROOT_DIR where the current (active) artifacts are stored
36 pub const CURRENT_ARTIFACTS_SUBDIR: &str = "dalvik-cache";
37 
38 /// Prefixes of system properties that are interested to odrefresh and dex2oat.
39 const ALLOWLIST_SYSTEM_PROPERTY_PREFIXES: &[&str] =
40     &["dalvik.vm.", "ro.dalvik.vm.", "persist.device_config.runtime_native_boot."];
41 
42 // The highest "standard" exit code defined in sysexits.h (as EX__MAX); odrefresh error codes
43 // start above here to avoid clashing.
44 // TODO: What if this changes?
45 const EX_MAX: i8 = 78;
46 
47 /// The defined odrefresh exit codes - see art/odrefresh/include/odrefresh/odrefresh.h
48 #[derive(Debug, PartialEq, Eq, FromPrimitive)]
49 #[repr(i8)]
50 pub enum ExitCode {
51     /// No compilation required, all artifacts look good
52     Okay = 0,
53     /// Compilation required
54     CompilationRequired = EX_MAX + 1,
55     /// New artifacts successfully generated
56     CompilationSuccess = EX_MAX + 2,
57     /// Compilation failed
58     CompilationFailed = EX_MAX + 3,
59     /// Removal of existing invalid artifacts failed
60     CleanupFailed = EX_MAX + 4,
61 }
62 
63 impl ExitCode {
64     /// Map an integer to the corresponding ExitCode enum, if there is one
from_i32(exit_code: i32) -> Result<Self>65     pub fn from_i32(exit_code: i32) -> Result<Self> {
66         FromPrimitive::from_i32(exit_code)
67             .ok_or_else(|| anyhow!("Unexpected odrefresh exit code: {}", exit_code))
68     }
69 }
70 
71 /// Returns whether the system property name is interesting to odrefresh and dex2oat.
is_system_property_interesting(name: &str) -> bool72 pub fn is_system_property_interesting(name: &str) -> bool {
73     for prefix in ALLOWLIST_SYSTEM_PROPERTY_PREFIXES {
74         if name.starts_with(prefix) {
75             return true;
76         }
77     }
78     false
79 }
80