Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef DEXOPT_H_ |
| 18 | #define DEXOPT_H_ |
| 19 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 20 | #include "installd_constants.h" |
| 21 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 24 | #include <optional> |
| 25 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 26 | #include <cutils/multiuser.h> |
| 27 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace installd { |
| 30 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 31 | /* dexopt needed flags matching those in dalvik.system.DexFile */ |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 32 | static constexpr int NO_DEXOPT_NEEDED = 0; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 33 | static constexpr int DEX2OAT_FROM_SCRATCH = 1; |
| 34 | static constexpr int DEX2OAT_FOR_BOOT_IMAGE = 2; |
| 35 | static constexpr int DEX2OAT_FOR_FILTER = 3; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 36 | |
Martin Stjernholm | f4caaa0 | 2019-07-17 22:14:14 +0100 | [diff] [blame] | 37 | #define ANDROID_ART_APEX_BIN "/apex/com.android.art/bin" |
Roland Levillain | 67a14f6 | 2019-01-23 15:59:50 +0000 | [diff] [blame] | 38 | // Location of binaries in the Android Runtime APEX. |
David Sehr | 6df89a6 | 2020-04-15 20:43:48 -0700 | [diff] [blame] | 39 | static constexpr const char* kDex2oat32Path = ANDROID_ART_APEX_BIN "/dex2oat32"; |
| 40 | static constexpr const char* kDex2oat64Path = ANDROID_ART_APEX_BIN "/dex2oat64"; |
| 41 | static constexpr const char* kDex2oatDebug32Path = ANDROID_ART_APEX_BIN "/dex2oatd32"; |
| 42 | static constexpr const char* kDex2oatDebug64Path = ANDROID_ART_APEX_BIN "/dex2oatd64"; |
Martin Stjernholm | f4caaa0 | 2019-07-17 22:14:14 +0100 | [diff] [blame] | 43 | static constexpr const char* kProfmanPath = ANDROID_ART_APEX_BIN "/profman"; |
| 44 | static constexpr const char* kProfmanDebugPath = ANDROID_ART_APEX_BIN "/profmand"; |
| 45 | static constexpr const char* kDexoptanalyzerPath = ANDROID_ART_APEX_BIN "/dexoptanalyzer"; |
| 46 | static constexpr const char* kDexoptanalyzerDebugPath = ANDROID_ART_APEX_BIN "/dexoptanalyzerd"; |
| 47 | #undef ANDROID_ART_APEX_BIN |
Roland Levillain | 67a14f6 | 2019-01-23 15:59:50 +0000 | [diff] [blame] | 48 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 49 | // Clear the reference profile identified by the given profile name. |
| 50 | bool clear_primary_reference_profile(const std::string& pkgname, const std::string& profile_name); |
| 51 | // Clear the current profile identified by the given profile name (for single user). |
| 52 | bool clear_primary_current_profile(const std::string& pkgname, const std::string& profile_name, |
| 53 | userid_t user); |
| 54 | // Clear all current profiles identified by the given profile name (all users). |
| 55 | bool clear_primary_current_profiles(const std::string& pkgname, const std::string& profile_name); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 56 | |
Calin Juravle | e90de86 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 57 | // Decides if profile guided compilation is needed or not based on existing profiles. |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 58 | // The analysis is done for a single profile name (which corresponds to a single code path). |
Calin Juravle | e90de86 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 59 | // |
| 60 | // Returns PROFILES_ANALYSIS_OPTIMIZE if there is enough information in the current profiles |
| 61 | // that makes it worth to recompile the package. |
| 62 | // If the return value is PROFILES_ANALYSIS_OPTIMIZE all the current profiles would have been |
| 63 | // merged into the reference profiles accessible with open_reference_profile(). |
| 64 | // |
| 65 | // Return PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA if the package should not optimize. |
| 66 | // As a special case returns PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES if all profiles are |
| 67 | // empty. |
| 68 | int analyze_primary_profiles(uid_t uid, |
| 69 | const std::string& pkgname, |
| 70 | const std::string& profile_name); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 71 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 72 | // Create a snapshot of the profile information for the given package profile. |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 73 | // If appId is -1, the method creates the profile snapshot for the boot image. |
| 74 | // |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 75 | // The profile snapshot is the aggregation of all existing profiles (all current user |
| 76 | // profiles & the reference profile) and is meant to capture the all the profile information |
| 77 | // without performing a merge into the reference profile which might impact future dex2oat |
| 78 | // compilations. |
| 79 | // The snapshot is created next to the reference profile of the package and the |
| 80 | // ownership is assigned to AID_SYSTEM. |
| 81 | // The snapshot location is reference_profile_location.snapshot. If a snapshot is already |
| 82 | // there, it will be truncated and overwritten. |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 83 | // |
| 84 | // The classpath acts as filter: only profiling data belonging to elements of the classpath |
| 85 | // will end up in the snapshot. |
| 86 | bool create_profile_snapshot(int32_t app_id, |
| 87 | const std::string& package, |
| 88 | const std::string& profile_name, |
| 89 | const std::string& classpath); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 90 | |
Martin Stjernholm | fd00040 | 2022-04-11 18:04:59 +0100 | [diff] [blame] | 91 | bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name, |
| 92 | const std::string& code_path, bool dump_classes_and_methods); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 93 | |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 94 | bool copy_system_profile(const std::string& system_profile, |
| 95 | uid_t packageUid, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 96 | const std::string& pkgname, |
| 97 | const std::string& profile_name); |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 98 | |
Jiakai Zhang | f515a1b | 2022-02-23 18:33:26 +0000 | [diff] [blame] | 99 | // Prepares the app profile for the package at the given path: |
| 100 | // - Creates the current profile for the given user ID, unless the user ID is `USER_NULL`. |
| 101 | // - Merges the profile from the dex metadata file (if present) into the reference profile. |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 102 | bool prepare_app_profile(const std::string& package_name, |
| 103 | userid_t user_id, |
| 104 | appid_t app_id, |
| 105 | const std::string& profile_name, |
| 106 | const std::string& code_path, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 107 | const std::optional<std::string>& dex_metadata); |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 108 | |
Calin Juravle | 0f3beba | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 109 | // Returns the total bytes that were freed, or -1 in case of errors. |
| 110 | int64_t delete_odex(const char* apk_path, const char* instruction_set, const char* output_path); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 111 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 112 | bool reconcile_secondary_dex_file(const std::string& dex_path, |
| 113 | const std::string& pkgname, int uid, const std::vector<std::string>& isas, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 114 | const std::optional<std::string>& volumeUuid, int storage_flag, |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 115 | /*out*/bool* out_secondary_dex_exists); |
| 116 | |
Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 117 | bool hash_secondary_dex_file(const std::string& dex_path, |
Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 118 | const std::string& pkgname, int uid, const std::optional<std::string>& volume_uuid, |
Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 119 | int storage_flag, std::vector<uint8_t>* out_secondary_dex_hash); |
| 120 | |
Keun young Park | 65578d6 | 2021-06-28 17:52:05 -0700 | [diff] [blame] | 121 | // completed pass false if it is canceled. Otherwise it will be true even if there is other |
| 122 | // error. |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 123 | int dexopt(const char *apk_path, uid_t uid, const char *pkgName, const char *instruction_set, |
| 124 | int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter, |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 125 | const char* volume_uuid, const char* class_loader_context, const char* se_info, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 126 | bool downgrade, int target_sdk_version, const char* profile_name, |
Keun young Park | 65578d6 | 2021-06-28 17:52:05 -0700 | [diff] [blame] | 127 | const char* dexMetadataPath, const char* compilation_reason, std::string* error_msg, |
| 128 | /* out */ bool* completed = nullptr); |
| 129 | |
| 130 | bool is_dexopt_blocked(); |
| 131 | |
| 132 | void control_dexopt_blocking(bool block); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 133 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 134 | bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir, |
| 135 | const char *apk_path, const char *instruction_set); |
| 136 | |
| 137 | bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path, |
| 138 | const char *instruction_set); |
| 139 | |
| 140 | bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src, |
| 141 | const char *instruction_set); |
| 142 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 143 | bool move_ab(const char* apk_path, const char* instruction_set, const char* output_path); |
| 144 | |
Calin Juravle | f74a737 | 2019-02-28 20:29:41 -0800 | [diff] [blame] | 145 | const char* select_execution_binary( |
| 146 | const char* binary, |
| 147 | const char* debug_binary, |
| 148 | bool background_job_compile, |
| 149 | bool is_debug_runtime, |
| 150 | bool is_release, |
| 151 | bool is_debuggable_build); |
| 152 | |
Jiakai Zhang | f515a1b | 2022-02-23 18:33:26 +0000 | [diff] [blame] | 153 | // Returns `ODEX_NOT_FOUND` if the optimized artifacts are not found, or `ODEX_IS_PUBLIC` if the |
| 154 | // optimized artifacts are accessible by all apps, or `ODEX_IS_PRIVATE` if the optimized artifacts |
| 155 | // are only accessible by this app, or -1 if failed to get the visibility of the optimized |
| 156 | // artifacts. |
| 157 | int get_odex_visibility(const char* apk_path, const char* instruction_set, const char* oat_dir); |
| 158 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 159 | } // namespace installd |
| 160 | } // namespace android |
| 161 | |
| 162 | #endif // DEXOPT_H_ |