| 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 | */ | 
| Alan Stokes | cb27c34 | 2018-04-20 17:09:25 +0100 | [diff] [blame] | 16 | #define LOG_TAG "installd" | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 17 |  | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 18 | #include <array> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 20 | #include <stdlib.h> | 
|  | 21 | #include <string.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 22 | #include <sys/capability.h> | 
|  | 23 | #include <sys/file.h> | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 25 | #include <sys/time.h> | 
|  | 26 | #include <sys/types.h> | 
|  | 27 | #include <sys/resource.h> | 
|  | 28 | #include <sys/wait.h> | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 29 | #include <unistd.h> | 
|  | 30 |  | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 31 | #include <iomanip> | 
|  | 32 |  | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 33 | #include <android-base/file.h> | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> | 
| Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 35 | #include <android-base/properties.h> | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 36 | #include <android-base/stringprintf.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 37 | #include <android-base/strings.h> | 
|  | 38 | #include <android-base/unique_fd.h> | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 39 | #include <async_safe/log.h> | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 40 | #include <cutils/fs.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 41 | #include <cutils/properties.h> | 
|  | 42 | #include <cutils/sched_policy.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 43 | #include <log/log.h>               // TODO: Move everything to base/logging. | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 44 | #include <openssl/sha.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 45 | #include <private/android_filesystem_config.h> | 
| Suren Baghdasaryan | 1cc5de6 | 2019-01-25 05:29:23 +0000 | [diff] [blame] | 46 | #include <processgroup/sched_policy.h> | 
| Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 47 | #include <selinux/android.h> | 
| Nicolas Geoffray | aaad21e | 2019-02-25 13:31:10 +0000 | [diff] [blame] | 48 | #include <server_configurable_flags/get_flags.h> | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 49 | #include <system/thread_defs.h> | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 50 |  | 
|  | 51 | #include "dexopt.h" | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 52 | #include "dexopt_return_codes.h" | 
| Victor Hsieh | c9821f1 | 2020-08-07 11:32:29 -0700 | [diff] [blame] | 53 | #include "execv_helper.h" | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 54 | #include "globals.h" | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 55 | #include "installd_deps.h" | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 56 | #include "installd_constants.h" | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 57 | #include "otapreopt_utils.h" | 
| Victor Hsieh | c9821f1 | 2020-08-07 11:32:29 -0700 | [diff] [blame] | 58 | #include "run_dex2oat.h" | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 59 | #include "unique_file.h" | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 60 | #include "utils.h" | 
|  | 61 |  | 
| Victor Hsieh | 76f19ba | 2020-08-10 14:37:26 -0700 | [diff] [blame] | 62 | using android::base::Basename; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 63 | using android::base::EndsWith; | 
| Mathieu Chartier | 9b2da08 | 2018-10-26 13:23:11 -0700 | [diff] [blame] | 64 | using android::base::GetBoolProperty; | 
|  | 65 | using android::base::GetProperty; | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 66 | using android::base::ReadFdToString; | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 67 | using android::base::ReadFully; | 
|  | 68 | using android::base::StringPrintf; | 
|  | 69 | using android::base::WriteFully; | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 70 | using android::base::unique_fd; | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | namespace android { | 
|  | 73 | namespace installd { | 
|  | 74 |  | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 75 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 76 | // Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below. | 
|  | 77 | struct FreeDelete { | 
|  | 78 | // NOTE: Deleting a const object is valid but free() takes a non-const pointer. | 
|  | 79 | void operator()(const void* ptr) const { | 
|  | 80 | free(const_cast<void*>(ptr)); | 
|  | 81 | } | 
|  | 82 | }; | 
|  | 83 |  | 
|  | 84 | // Alias for std::unique_ptr<> that uses the C function free() to delete objects. | 
|  | 85 | template <typename T> | 
|  | 86 | using UniqueCPtr = std::unique_ptr<T, FreeDelete>; | 
|  | 87 |  | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 88 | static unique_fd invalid_unique_fd() { | 
|  | 89 | return unique_fd(-1); | 
|  | 90 | } | 
|  | 91 |  | 
| Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 92 | static bool is_debug_runtime() { | 
|  | 93 | return android::base::GetProperty("persist.sys.dalvik.vm.lib.2", "") == "libartd.so"; | 
|  | 94 | } | 
|  | 95 |  | 
| David Sehr | a3b5ab6 | 2017-10-25 14:27:29 -0700 | [diff] [blame] | 96 | static bool is_debuggable_build() { | 
|  | 97 | return android::base::GetBoolProperty("ro.debuggable", false); | 
|  | 98 | } | 
|  | 99 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 100 | static bool clear_profile(const std::string& profile) { | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 101 | unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC)); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 102 | if (ufd.get() < 0) { | 
|  | 103 | if (errno != ENOENT) { | 
|  | 104 | PLOG(WARNING) << "Could not open profile " << profile; | 
|  | 105 | return false; | 
|  | 106 | } else { | 
|  | 107 | // Nothing to clear. That's ok. | 
|  | 108 | return true; | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) { | 
|  | 113 | if (errno != EWOULDBLOCK) { | 
|  | 114 | PLOG(WARNING) << "Error locking profile " << profile; | 
|  | 115 | } | 
|  | 116 | // This implies that the app owning this profile is running | 
|  | 117 | // (and has acquired the lock). | 
|  | 118 | // | 
|  | 119 | // If we can't acquire the lock bail out since clearing is useless anyway | 
|  | 120 | // (the app will write again to the profile). | 
|  | 121 | // | 
|  | 122 | // Note: | 
|  | 123 | // This does not impact the this is not an issue for the profiling correctness. | 
|  | 124 | // In case this is needed because of an app upgrade, profiles will still be | 
|  | 125 | // eventually cleared by the app itself due to checksum mismatch. | 
|  | 126 | // If this is needed because profman advised, then keeping the data around | 
|  | 127 | // until the next run is again not an issue. | 
|  | 128 | // | 
|  | 129 | // If the app attempts to acquire a lock while we've held one here, | 
|  | 130 | // it will simply skip the current write cycle. | 
|  | 131 | return false; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | bool truncated = ftruncate(ufd.get(), 0) == 0; | 
|  | 135 | if (!truncated) { | 
|  | 136 | PLOG(WARNING) << "Could not truncate " << profile; | 
|  | 137 | } | 
|  | 138 | if (flock(ufd.get(), LOCK_UN) != 0) { | 
|  | 139 | PLOG(WARNING) << "Error unlocking profile " << profile; | 
|  | 140 | } | 
|  | 141 | return truncated; | 
|  | 142 | } | 
|  | 143 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 144 | // Clear the reference profile for the given location. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 145 | // The location is the profile name for primary apks or the dex path for secondary dex files. | 
|  | 146 | static bool clear_reference_profile(const std::string& package_name, const std::string& location, | 
|  | 147 | bool is_secondary_dex) { | 
|  | 148 | return clear_profile(create_reference_profile_path(package_name, location, is_secondary_dex)); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 151 | // Clear the reference profile for the given location. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 152 | // The location is the profile name for primary apks or the dex path for secondary dex files. | 
|  | 153 | static bool clear_current_profile(const std::string& package_name, const std::string& location, | 
|  | 154 | userid_t user, bool is_secondary_dex) { | 
|  | 155 | return clear_profile(create_current_profile_path(user, package_name, location, | 
|  | 156 | is_secondary_dex)); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 159 | // Clear the reference profile for the primary apk of the given package. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 160 | // The location is the profile name for primary apks or the dex path for secondary dex files. | 
|  | 161 | bool clear_primary_reference_profile(const std::string& package_name, | 
|  | 162 | const std::string& location) { | 
|  | 163 | return clear_reference_profile(package_name, location, /*is_secondary_dex*/false); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
|  | 166 | // Clear all current profile for the primary apk of the given package. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 167 | // The location is the profile name for primary apks or the dex path for secondary dex files. | 
|  | 168 | bool clear_primary_current_profiles(const std::string& package_name, const std::string& location) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 169 | bool success = true; | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 170 | // For secondary dex files, we don't really need the user but we use it for sanity checks. | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 171 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); | 
|  | 172 | for (auto user : users) { | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 173 | success &= clear_current_profile(package_name, location, user, /*is_secondary_dex*/false); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 174 | } | 
|  | 175 | return success; | 
|  | 176 | } | 
|  | 177 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 178 | // Clear the current profile for the primary apk of the given package and user. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 179 | bool clear_primary_current_profile(const std::string& package_name, const std::string& location, | 
|  | 180 | userid_t user) { | 
|  | 181 | return clear_current_profile(package_name, location, user, /*is_secondary_dex*/false); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
| Calin Juravle | f74a737 | 2019-02-28 20:29:41 -0800 | [diff] [blame] | 184 | // Determines which binary we should use for execution (the debug or non-debug version). | 
|  | 185 | // e.g. dex2oatd vs dex2oat | 
|  | 186 | static const char* select_execution_binary(const char* binary, const char* debug_binary, | 
|  | 187 | bool background_job_compile) { | 
|  | 188 | return select_execution_binary( | 
|  | 189 | binary, | 
|  | 190 | debug_binary, | 
|  | 191 | background_job_compile, | 
|  | 192 | is_debug_runtime(), | 
|  | 193 | (android::base::GetProperty("ro.build.version.codename", "") == "REL"), | 
|  | 194 | is_debuggable_build()); | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | // Determines which binary we should use for execution (the debug or non-debug version). | 
|  | 198 | // e.g. dex2oatd vs dex2oat | 
|  | 199 | // This is convenient method which is much easier to test because it doesn't read | 
|  | 200 | // system properties. | 
|  | 201 | const char* select_execution_binary( | 
|  | 202 | const char* binary, | 
|  | 203 | const char* debug_binary, | 
|  | 204 | bool background_job_compile, | 
|  | 205 | bool is_debug_runtime, | 
|  | 206 | bool is_release, | 
|  | 207 | bool is_debuggable_build) { | 
|  | 208 | // Do not use debug binaries for release candidates (to give more soak time). | 
|  | 209 | bool is_debug_bg_job = background_job_compile && is_debuggable_build && !is_release; | 
|  | 210 |  | 
|  | 211 | // If the runtime was requested to use libartd.so, we'll run the debug version - assuming | 
|  | 212 | // the file is present (it may not be on images with very little space available). | 
|  | 213 | bool useDebug = (is_debug_runtime || is_debug_bg_job) && (access(debug_binary, X_OK) == 0); | 
|  | 214 |  | 
|  | 215 | return useDebug ? debug_binary : binary; | 
|  | 216 | } | 
|  | 217 |  | 
| Nicolas Geoffray | aaad21e | 2019-02-25 13:31:10 +0000 | [diff] [blame] | 218 | // Namespace for Android Runtime flags applied during boot time. | 
|  | 219 | static const char* RUNTIME_NATIVE_BOOT_NAMESPACE = "runtime_native_boot"; | 
|  | 220 | // Feature flag name for running the JIT in Zygote experiment, b/119800099. | 
| Nicolas Geoffray | 5a4c4e9 | 2020-02-07 11:37:34 +0000 | [diff] [blame] | 221 | static const char* ENABLE_JITZYGOTE_IMAGE = "enable_apex_image"; | 
| Nicolas Geoffray | aaad21e | 2019-02-25 13:31:10 +0000 | [diff] [blame] | 222 |  | 
| Mathieu Chartier | e97261e | 2019-10-01 15:36:01 -0700 | [diff] [blame] | 223 | // Phenotype property name for enabling profiling the boot class path. | 
|  | 224 | static const char* PROFILE_BOOT_CLASS_PATH = "profilebootclasspath"; | 
|  | 225 |  | 
| Calin Juravle | f85ddb9 | 2020-05-01 14:05:40 -0700 | [diff] [blame] | 226 | static bool IsBootClassPathProfilingEnable() { | 
|  | 227 | std::string profile_boot_class_path = GetProperty("dalvik.vm.profilebootclasspath", ""); | 
|  | 228 | profile_boot_class_path = | 
|  | 229 | server_configurable_flags::GetServerConfigurableFlag( | 
|  | 230 | RUNTIME_NATIVE_BOOT_NAMESPACE, | 
|  | 231 | PROFILE_BOOT_CLASS_PATH, | 
|  | 232 | /*default_value=*/ profile_boot_class_path); | 
|  | 233 | return profile_boot_class_path == "true"; | 
|  | 234 | } | 
|  | 235 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 236 | static void UnlinkIgnoreResult(const std::string& path) { | 
|  | 237 | if (unlink(path.c_str()) < 0) { | 
|  | 238 | PLOG(ERROR) << "Failed to unlink " << path; | 
|  | 239 | } | 
|  | 240 | } | 
|  | 241 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 242 | /* | 
|  | 243 | * Whether dexopt should use a swap file when compiling an APK. | 
|  | 244 | * | 
|  | 245 | * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision | 
|  | 246 | * itself, anyways). | 
|  | 247 | * | 
|  | 248 | * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true". | 
|  | 249 | * | 
|  | 250 | * Otherwise, return true if this is a low-mem device. | 
|  | 251 | * | 
|  | 252 | * Otherwise, return default value. | 
|  | 253 | */ | 
|  | 254 | static bool kAlwaysProvideSwapFile = false; | 
|  | 255 | static bool kDefaultProvideSwapFile = true; | 
|  | 256 |  | 
|  | 257 | static bool ShouldUseSwapFileForDexopt() { | 
|  | 258 | if (kAlwaysProvideSwapFile) { | 
|  | 259 | return true; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | // Check the "override" property. If it exists, return value == "true". | 
| Mathieu Chartier | 9b2da08 | 2018-10-26 13:23:11 -0700 | [diff] [blame] | 263 | std::string dex2oat_prop_buf = GetProperty("dalvik.vm.dex2oat-swap", ""); | 
|  | 264 | if (!dex2oat_prop_buf.empty()) { | 
|  | 265 | return dex2oat_prop_buf == "true"; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
|  | 268 | // Shortcut for default value. This is an implementation optimization for the process sketched | 
|  | 269 | // above. If the default value is true, we can avoid to check whether this is a low-mem device, | 
|  | 270 | // as low-mem is never returning false. The compiler will optimize this away if it can. | 
|  | 271 | if (kDefaultProvideSwapFile) { | 
|  | 272 | return true; | 
|  | 273 | } | 
|  | 274 |  | 
| Mathieu Chartier | 9b2da08 | 2018-10-26 13:23:11 -0700 | [diff] [blame] | 275 | if (GetBoolProperty("ro.config.low_ram", false)) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 276 | return true; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | // Default value must be false here. | 
|  | 280 | return kDefaultProvideSwapFile; | 
|  | 281 | } | 
|  | 282 |  | 
| Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 283 | static void SetDex2OatScheduling(bool set_to_bg) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 284 | if (set_to_bg) { | 
|  | 285 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 286 | PLOG(ERROR) << "set_sched_policy failed"; | 
|  | 287 | exit(DexoptReturnCodes::kSetSchedPolicy); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 288 | } | 
|  | 289 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 290 | PLOG(ERROR) << "setpriority failed"; | 
|  | 291 | exit(DexoptReturnCodes::kSetPriority); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
|  | 294 | } | 
|  | 295 |  | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 296 | static unique_fd create_profile(uid_t uid, const std::string& profile, int32_t flags, mode_t mode) { | 
|  | 297 | unique_fd fd(TEMP_FAILURE_RETRY(open(profile.c_str(), flags, mode))); | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 298 | if (fd.get() < 0) { | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 299 | if (errno != EEXIST) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 300 | PLOG(ERROR) << "Failed to create profile " << profile; | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 301 | return invalid_unique_fd(); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 302 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 303 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 304 | // Profiles should belong to the app; make sure of that by giving ownership to | 
|  | 305 | // the app uid. If we cannot do that, there's no point in returning the fd | 
|  | 306 | // since dex2oat/profman will fail with SElinux denials. | 
|  | 307 | if (fchown(fd.get(), uid, uid) < 0) { | 
| Roland Levillain | 019db5b | 2019-03-14 14:31:59 +0000 | [diff] [blame] | 308 | PLOG(ERROR) << "Could not chown profile " << profile; | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 309 | return invalid_unique_fd(); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 310 | } | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 311 | return fd; | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 314 | static unique_fd open_profile(uid_t uid, const std::string& profile, int32_t flags, mode_t mode) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 315 | // Do not follow symlinks when opening a profile: | 
|  | 316 | //   - primary profiles should not contain symlinks in their paths | 
|  | 317 | //   - secondary dex paths should have been already resolved and validated | 
|  | 318 | flags |= O_NOFOLLOW; | 
|  | 319 |  | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 320 | // Check if we need to create the profile | 
|  | 321 | // Reference profiles and snapshots are created on the fly; so they might not exist beforehand. | 
|  | 322 | unique_fd fd; | 
|  | 323 | if ((flags & O_CREAT) != 0) { | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 324 | fd = create_profile(uid, profile, flags, mode); | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 325 | } else { | 
|  | 326 | fd.reset(TEMP_FAILURE_RETRY(open(profile.c_str(), flags))); | 
|  | 327 | } | 
|  | 328 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 329 | if (fd.get() < 0) { | 
|  | 330 | if (errno != ENOENT) { | 
|  | 331 | // Profiles might be missing for various reasons. For example, in a | 
|  | 332 | // multi-user environment, the profile directory for one user can be created | 
|  | 333 | // after we start a merge. In this case the current profile for that user | 
|  | 334 | // will not be found. | 
|  | 335 | // Also, the secondary dex profiles might be deleted by the app at any time, | 
|  | 336 | // so we can't we need to prepare if they are missing. | 
|  | 337 | PLOG(ERROR) << "Failed to open profile " << profile; | 
|  | 338 | } | 
|  | 339 | return invalid_unique_fd(); | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 340 | } else { | 
|  | 341 | // If we just create the file we need to set its mode because on Android | 
|  | 342 | // open has a mask that only allows owner access. | 
|  | 343 | if ((flags & O_CREAT) != 0) { | 
|  | 344 | if (fchmod(fd.get(), mode) != 0) { | 
|  | 345 | PLOG(ERROR) << "Could not set mode " << std::hex << mode << std::dec | 
|  | 346 | << " on profile" << profile; | 
|  | 347 | // Not a terminal failure. | 
|  | 348 | } | 
|  | 349 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 350 | } | 
|  | 351 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 352 | return fd; | 
|  | 353 | } | 
|  | 354 |  | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 355 | static unique_fd open_current_profile(uid_t uid, userid_t user, const std::string& package_name, | 
|  | 356 | const std::string& location, bool is_secondary_dex) { | 
|  | 357 | std::string profile = create_current_profile_path(user, package_name, location, | 
|  | 358 | is_secondary_dex); | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 359 | return open_profile(uid, profile, O_RDONLY, /*mode=*/ 0); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 360 | } | 
|  | 361 |  | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 362 | static unique_fd open_reference_profile(uid_t uid, const std::string& package_name, | 
|  | 363 | const std::string& location, bool read_write, bool is_secondary_dex) { | 
|  | 364 | std::string profile = create_reference_profile_path(package_name, location, is_secondary_dex); | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 365 | return open_profile( | 
|  | 366 | uid, | 
|  | 367 | profile, | 
|  | 368 | read_write ? (O_CREAT | O_RDWR) : O_RDONLY, | 
|  | 369 | S_IRUSR | S_IWUSR | S_IRGRP);  // so that ART can also read it when apps run. | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 372 | static UniqueFile open_reference_profile_as_unique_file(uid_t uid, const std::string& package_name, | 
|  | 373 | const std::string& location, bool read_write, bool is_secondary_dex) { | 
|  | 374 | std::string profile_path = create_reference_profile_path(package_name, location, | 
|  | 375 | is_secondary_dex); | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 376 | unique_fd ufd = open_profile( | 
|  | 377 | uid, | 
|  | 378 | profile_path, | 
|  | 379 | read_write ? (O_CREAT | O_RDWR) : O_RDONLY, | 
|  | 380 | S_IRUSR | S_IWUSR | S_IRGRP);  // so that ART can also read it when apps run. | 
|  | 381 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 382 | return UniqueFile(ufd.release(), profile_path, [](const std::string& path) { | 
|  | 383 | clear_profile(path); | 
|  | 384 | }); | 
|  | 385 | } | 
|  | 386 |  | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 387 | static unique_fd open_spnashot_profile(uid_t uid, const std::string& package_name, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 388 | const std::string& location) { | 
|  | 389 | std::string profile = create_snapshot_profile_path(package_name, location); | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 390 | return open_profile(uid, profile, O_CREAT | O_RDWR | O_TRUNC,  S_IRUSR | S_IWUSR); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 391 | } | 
|  | 392 |  | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 393 | static void open_profile_files(uid_t uid, const std::string& package_name, | 
|  | 394 | const std::string& location, bool is_secondary_dex, | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 395 | /*out*/ std::vector<unique_fd>* profiles_fd, /*out*/ unique_fd* reference_profile_fd) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 396 | // Open the reference profile in read-write mode as profman might need to save the merge. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 397 | *reference_profile_fd = open_reference_profile(uid, package_name, location, | 
|  | 398 | /*read_write*/ true, is_secondary_dex); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 399 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 400 | // For secondary dex files, we don't really need the user but we use it for sanity checks. | 
|  | 401 | // Note: the user owning the dex file should be the current user. | 
|  | 402 | std::vector<userid_t> users; | 
|  | 403 | if (is_secondary_dex){ | 
|  | 404 | users.push_back(multiuser_get_user_id(uid)); | 
|  | 405 | } else { | 
|  | 406 | users = get_known_users(/*volume_uuid*/ nullptr); | 
|  | 407 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 408 | for (auto user : users) { | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 409 | unique_fd profile_fd = open_current_profile(uid, user, package_name, location, | 
|  | 410 | is_secondary_dex); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 411 | // Add to the lists only if both fds are valid. | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 412 | if (profile_fd.get() >= 0) { | 
|  | 413 | profiles_fd->push_back(std::move(profile_fd)); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 414 | } | 
|  | 415 | } | 
|  | 416 | } | 
|  | 417 |  | 
| Calin Juravle | 7656132 | 2019-11-14 09:08:52 -0800 | [diff] [blame] | 418 | static constexpr int PROFMAN_BIN_RETURN_CODE_SUCCESS = 0; | 
|  | 419 | static constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 1; | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 420 | static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_NOT_ENOUGH_DELTA = 2; | 
| Calin Juravle | 7656132 | 2019-11-14 09:08:52 -0800 | [diff] [blame] | 421 | static constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 3; | 
|  | 422 | static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 4; | 
|  | 423 | static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 5; | 
|  | 424 | static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS = 6; | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 425 | static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_EMPTY_PROFILES = 7; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 426 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 427 | class RunProfman : public ExecVHelper { | 
|  | 428 | public: | 
|  | 429 | void SetupArgs(const std::vector<unique_fd>& profile_fds, | 
|  | 430 | const unique_fd& reference_profile_fd, | 
|  | 431 | const std::vector<unique_fd>& apk_fds, | 
|  | 432 | const std::vector<std::string>& dex_locations, | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 433 | bool copy_and_update, | 
|  | 434 | bool for_snapshot, | 
|  | 435 | bool for_boot_image) { | 
| Calin Juravle | f74a737 | 2019-02-28 20:29:41 -0800 | [diff] [blame] | 436 |  | 
|  | 437 | // TODO(calin): Assume for now we run in the bg compile job (which is in | 
|  | 438 | // most of the invocation). With the current data flow, is not very easy or | 
|  | 439 | // clean to discover this in RunProfman (it will require quite a messy refactoring). | 
|  | 440 | const char* profman_bin = select_execution_binary( | 
|  | 441 | kProfmanPath, kProfmanDebugPath, /*background_job_compile=*/ true); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 442 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 443 | if (copy_and_update) { | 
|  | 444 | CHECK_EQ(1u, profile_fds.size()); | 
|  | 445 | CHECK_EQ(1u, apk_fds.size()); | 
| Mathieu Chartier | 3163652 | 2018-11-09 23:53:07 +0000 | [diff] [blame] | 446 | } | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 447 | if (reference_profile_fd != -1) { | 
|  | 448 | AddArg("--reference-profile-file-fd=" + std::to_string(reference_profile_fd.get())); | 
| Mathieu Chartier | 3163652 | 2018-11-09 23:53:07 +0000 | [diff] [blame] | 449 | } | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 450 |  | 
|  | 451 | for (const unique_fd& fd : profile_fds) { | 
|  | 452 | AddArg("--profile-file-fd=" + std::to_string(fd.get())); | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | for (const unique_fd& fd : apk_fds) { | 
|  | 456 | AddArg("--apk-fd=" + std::to_string(fd.get())); | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | for (const std::string& dex_location : dex_locations) { | 
|  | 460 | AddArg("--dex-location=" + dex_location); | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | if (copy_and_update) { | 
|  | 464 | AddArg("--copy-and-update-profile-key"); | 
|  | 465 | } | 
|  | 466 |  | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 467 | if (for_snapshot) { | 
|  | 468 | AddArg("--force-merge"); | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | if (for_boot_image) { | 
|  | 472 | AddArg("--boot-image-merge"); | 
|  | 473 | } | 
|  | 474 |  | 
| yawanng | a27559e | 2020-11-13 19:17:44 +0000 | [diff] [blame] | 475 | // The percent won't exceed 100, otherwise, don't set it and use the | 
|  | 476 | // default one set in profman. | 
| yawanng | 6fe34a5 | 2020-11-06 05:13:53 +0000 | [diff] [blame] | 477 | uint32_t min_new_classes_percent_change = ::android::base::GetUintProperty<uint32_t>( | 
| yawanng | a27559e | 2020-11-13 19:17:44 +0000 | [diff] [blame] | 478 | "dalvik.vm.bgdexopt.new-classes-percent", | 
|  | 479 | /*default*/std::numeric_limits<uint32_t>::max()); | 
|  | 480 | if (min_new_classes_percent_change <= 100) { | 
| yawanng | 6fe34a5 | 2020-11-06 05:13:53 +0000 | [diff] [blame] | 481 | AddArg("--min-new-classes-percent-change=" + | 
|  | 482 | std::to_string(min_new_classes_percent_change)); | 
|  | 483 | } | 
|  | 484 |  | 
| yawanng | a27559e | 2020-11-13 19:17:44 +0000 | [diff] [blame] | 485 | // The percent won't exceed 100, otherwise, don't set it and use the | 
|  | 486 | // default one set in profman. | 
| yawanng | 6fe34a5 | 2020-11-06 05:13:53 +0000 | [diff] [blame] | 487 | uint32_t min_new_methods_percent_change = ::android::base::GetUintProperty<uint32_t>( | 
| yawanng | a27559e | 2020-11-13 19:17:44 +0000 | [diff] [blame] | 488 | "dalvik.vm.bgdexopt.new-methods-percent", | 
|  | 489 | /*default*/std::numeric_limits<uint32_t>::max()); | 
|  | 490 | if (min_new_methods_percent_change <= 100) { | 
| yawanng | 6fe34a5 | 2020-11-06 05:13:53 +0000 | [diff] [blame] | 491 | AddArg("--min-new-methods-percent-change=" + | 
|  | 492 | std::to_string(min_new_methods_percent_change)); | 
|  | 493 | } | 
|  | 494 |  | 
| Orion Hodson | 5f6b3ad | 2021-07-01 12:12:40 +0100 | [diff] [blame] | 495 | // On-device signing related. odsign sets the system property odsign.verification.success if | 
|  | 496 | // AOT artifacts have the expected signatures. | 
|  | 497 | const bool trust_art_apex_data_files = | 
|  | 498 | ::android::base::GetBoolProperty("odsign.verification.success", false); | 
|  | 499 | if (!trust_art_apex_data_files) { | 
|  | 500 | AddRuntimeArg("-Xdeny-art-apex-data-files"); | 
|  | 501 | } | 
|  | 502 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 503 | // Do not add after dex2oat_flags, they should override others for debugging. | 
|  | 504 | PrepareArgs(profman_bin); | 
| Mathieu Chartier | 62d218d | 2018-11-05 09:34:24 -0800 | [diff] [blame] | 505 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 506 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 507 | void SetupMerge(const std::vector<unique_fd>& profiles_fd, | 
|  | 508 | const unique_fd& reference_profile_fd, | 
|  | 509 | const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>(), | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 510 | const std::vector<std::string>& dex_locations = std::vector<std::string>(), | 
|  | 511 | bool for_snapshot = false, | 
|  | 512 | bool for_boot_image = false) { | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 513 | SetupArgs(profiles_fd, | 
| Calin Juravle | b3a929d | 2018-12-11 14:40:00 -0800 | [diff] [blame] | 514 | reference_profile_fd, | 
|  | 515 | apk_fds, | 
|  | 516 | dex_locations, | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 517 | /*copy_and_update=*/ false, | 
|  | 518 | for_snapshot, | 
|  | 519 | for_boot_image); | 
| Mathieu Chartier | 62d218d | 2018-11-05 09:34:24 -0800 | [diff] [blame] | 520 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 521 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 522 | void SetupCopyAndUpdate(unique_fd&& profile_fd, | 
|  | 523 | unique_fd&& reference_profile_fd, | 
|  | 524 | unique_fd&& apk_fd, | 
|  | 525 | const std::string& dex_location) { | 
|  | 526 | // The fds need to stay open longer than the scope of the function, so put them into a local | 
|  | 527 | // variable vector. | 
|  | 528 | profiles_fd_.push_back(std::move(profile_fd)); | 
|  | 529 | apk_fds_.push_back(std::move(apk_fd)); | 
|  | 530 | reference_profile_fd_ = std::move(reference_profile_fd); | 
|  | 531 | std::vector<std::string> dex_locations = {dex_location}; | 
| Calin Juravle | b3a929d | 2018-12-11 14:40:00 -0800 | [diff] [blame] | 532 | SetupArgs(profiles_fd_, | 
|  | 533 | reference_profile_fd_, | 
|  | 534 | apk_fds_, | 
|  | 535 | dex_locations, | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 536 | /*copy_and_update=*/true, | 
|  | 537 | /*for_snapshot*/false, | 
|  | 538 | /*for_boot_image*/false); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 539 | } | 
| Calin Juravle | f63d479 | 2018-01-30 17:43:34 +0000 | [diff] [blame] | 540 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 541 | void SetupDump(const std::vector<unique_fd>& profiles_fd, | 
|  | 542 | const unique_fd& reference_profile_fd, | 
|  | 543 | const std::vector<std::string>& dex_locations, | 
|  | 544 | const std::vector<unique_fd>& apk_fds, | 
|  | 545 | const unique_fd& output_fd) { | 
|  | 546 | AddArg("--dump-only"); | 
|  | 547 | AddArg(StringPrintf("--dump-output-to-fd=%d", output_fd.get())); | 
| Calin Juravle | b3a929d | 2018-12-11 14:40:00 -0800 | [diff] [blame] | 548 | SetupArgs(profiles_fd, | 
|  | 549 | reference_profile_fd, | 
|  | 550 | apk_fds, | 
|  | 551 | dex_locations, | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 552 | /*copy_and_update=*/false, | 
|  | 553 | /*for_snapshot*/false, | 
|  | 554 | /*for_boot_image*/false); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 555 | } | 
| Calin Juravle | f63d479 | 2018-01-30 17:43:34 +0000 | [diff] [blame] | 556 |  | 
| Victor Hsieh | c9821f1 | 2020-08-07 11:32:29 -0700 | [diff] [blame] | 557 | using ExecVHelper::Exec;  // To suppress -Wno-overloaded-virtual | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 558 | void Exec() { | 
|  | 559 | ExecVHelper::Exec(DexoptReturnCodes::kProfmanExec); | 
|  | 560 | } | 
| Mathieu Chartier | 3163652 | 2018-11-09 23:53:07 +0000 | [diff] [blame] | 561 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 562 | private: | 
|  | 563 | unique_fd reference_profile_fd_; | 
|  | 564 | std::vector<unique_fd> profiles_fd_; | 
|  | 565 | std::vector<unique_fd> apk_fds_; | 
|  | 566 | }; | 
| Mathieu Chartier | 3163652 | 2018-11-09 23:53:07 +0000 | [diff] [blame] | 567 |  | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 568 | static int analyze_profiles(uid_t uid, const std::string& package_name, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 569 | const std::string& location, bool is_secondary_dex) { | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 570 | std::vector<unique_fd> profiles_fd; | 
|  | 571 | unique_fd reference_profile_fd; | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 572 | open_profile_files(uid, package_name, location, is_secondary_dex, | 
|  | 573 | &profiles_fd, &reference_profile_fd); | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 574 | if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 575 | // Skip profile guided compilation because no profiles were found. | 
|  | 576 | // Or if the reference profile info couldn't be opened. | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 577 | return PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 578 | } | 
|  | 579 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 580 | RunProfman profman_merge; | 
| Calin Juravle | f85ddb9 | 2020-05-01 14:05:40 -0700 | [diff] [blame] | 581 | const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>(); | 
|  | 582 | const std::vector<std::string>& dex_locations = std::vector<std::string>(); | 
|  | 583 | profman_merge.SetupMerge( | 
|  | 584 | profiles_fd, | 
|  | 585 | reference_profile_fd, | 
|  | 586 | apk_fds, | 
|  | 587 | dex_locations, | 
|  | 588 | /* for_snapshot= */ false, | 
|  | 589 | IsBootClassPathProfilingEnable()); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 590 | pid_t pid = fork(); | 
|  | 591 | if (pid == 0) { | 
|  | 592 | /* child -- drop privileges before continuing */ | 
|  | 593 | drop_capabilities(uid); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 594 | profman_merge.Exec(); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 595 | } | 
|  | 596 | /* parent */ | 
|  | 597 | int return_code = wait_child(pid); | 
|  | 598 | bool need_to_compile = false; | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 599 | bool empty_profiles = false; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 600 | bool should_clear_current_profiles = false; | 
|  | 601 | bool should_clear_reference_profile = false; | 
|  | 602 | if (!WIFEXITED(return_code)) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 603 | LOG(WARNING) << "profman failed for location " << location << ": " << return_code; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 604 | } else { | 
|  | 605 | return_code = WEXITSTATUS(return_code); | 
|  | 606 | switch (return_code) { | 
|  | 607 | case PROFMAN_BIN_RETURN_CODE_COMPILE: | 
|  | 608 | need_to_compile = true; | 
|  | 609 | should_clear_current_profiles = true; | 
|  | 610 | should_clear_reference_profile = false; | 
|  | 611 | break; | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 612 | case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_NOT_ENOUGH_DELTA: | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 613 | need_to_compile = false; | 
|  | 614 | should_clear_current_profiles = false; | 
|  | 615 | should_clear_reference_profile = false; | 
|  | 616 | break; | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 617 | case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_EMPTY_PROFILES: | 
|  | 618 | need_to_compile = false; | 
|  | 619 | empty_profiles = true; | 
|  | 620 | should_clear_current_profiles = false; | 
|  | 621 | should_clear_reference_profile = false; | 
|  | 622 | break; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 623 | case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES: | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 624 | LOG(WARNING) << "Bad profiles for location " << location; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 625 | need_to_compile = false; | 
|  | 626 | should_clear_current_profiles = true; | 
|  | 627 | should_clear_reference_profile = true; | 
|  | 628 | break; | 
|  | 629 | case PROFMAN_BIN_RETURN_CODE_ERROR_IO:  // fall-through | 
|  | 630 | case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING: | 
|  | 631 | // Temporary IO problem (e.g. locking). Ignore but log a warning. | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 632 | LOG(WARNING) << "IO error while reading profiles for location " << location; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 633 | need_to_compile = false; | 
|  | 634 | should_clear_current_profiles = false; | 
|  | 635 | should_clear_reference_profile = false; | 
|  | 636 | break; | 
| Calin Juravle | 7656132 | 2019-11-14 09:08:52 -0800 | [diff] [blame] | 637 | case PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS: | 
|  | 638 | need_to_compile = false; | 
|  | 639 | should_clear_current_profiles = true; | 
|  | 640 | should_clear_reference_profile = true; | 
|  | 641 | break; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 642 | default: | 
|  | 643 | // Unknown return code or error. Unlink profiles. | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 644 | LOG(WARNING) << "Unexpected error code while processing profiles for location " | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 645 | << location << ": " << return_code; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 646 | need_to_compile = false; | 
|  | 647 | should_clear_current_profiles = true; | 
|  | 648 | should_clear_reference_profile = true; | 
|  | 649 | break; | 
|  | 650 | } | 
|  | 651 | } | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 652 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 653 | if (should_clear_current_profiles) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 654 | if (is_secondary_dex) { | 
|  | 655 | // For secondary dex files, the owning user is the current user. | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 656 | clear_current_profile(package_name, location, multiuser_get_user_id(uid), | 
|  | 657 | is_secondary_dex); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 658 | } else  { | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 659 | clear_primary_current_profiles(package_name, location); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 660 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 661 | } | 
|  | 662 | if (should_clear_reference_profile) { | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 663 | clear_reference_profile(package_name, location, is_secondary_dex); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 664 | } | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 665 | int result = 0; | 
|  | 666 | if (need_to_compile) { | 
|  | 667 | result = PROFILES_ANALYSIS_OPTIMIZE; | 
|  | 668 | } else if (empty_profiles) { | 
|  | 669 | result = PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES; | 
|  | 670 | } else { | 
|  | 671 | result = PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA; | 
|  | 672 | } | 
|  | 673 | return result; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 674 | } | 
|  | 675 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 676 | // Decides if profile guided compilation is needed or not based on existing profiles. | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 677 | // The analysis is done for a single profile name (which corresponds to a single code path). | 
|  | 678 | // | 
|  | 679 | // Returns PROFILES_ANALYSIS_OPTIMIZE if there is enough information in the current profiles | 
|  | 680 | // that makes it worth to recompile the package. | 
|  | 681 | // If the return value is PROFILES_ANALYSIS_OPTIMIZE all the current profiles would have been | 
|  | 682 | // merged into the reference profiles accessible with open_reference_profile(). | 
|  | 683 | // | 
|  | 684 | // Return PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA if the package should not optimize. | 
|  | 685 | // As a special case returns PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES if all profiles are | 
|  | 686 | // empty. | 
|  | 687 | int analyze_primary_profiles(uid_t uid, const std::string& package_name, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 688 | const std::string& profile_name) { | 
|  | 689 | return analyze_profiles(uid, package_name, profile_name, /*is_secondary_dex*/false); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 690 | } | 
|  | 691 |  | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 692 | bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name, | 
|  | 693 | const std::string& code_path) { | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 694 | std::vector<unique_fd> profile_fds; | 
|  | 695 | unique_fd reference_profile_fd; | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 696 | std::string out_file_name = StringPrintf("/data/misc/profman/%s-%s.txt", | 
|  | 697 | pkgname.c_str(), profile_name.c_str()); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 698 |  | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 699 | open_profile_files(uid, pkgname, profile_name, /*is_secondary_dex*/false, | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 700 | &profile_fds, &reference_profile_fd); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 701 |  | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 702 | const bool has_reference_profile = (reference_profile_fd.get() != -1); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 703 | const bool has_profiles = !profile_fds.empty(); | 
|  | 704 |  | 
|  | 705 | if (!has_reference_profile && !has_profiles) { | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 706 | LOG(ERROR)  << "profman dump: no profiles to dump for " << pkgname; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 707 | return false; | 
|  | 708 | } | 
|  | 709 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 710 | unique_fd output_fd(open(out_file_name.c_str(), | 
|  | 711 | O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644)); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 712 | if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) { | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 713 | LOG(ERROR) << "installd cannot chmod file for dump_profile" << out_file_name; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 714 | return false; | 
|  | 715 | } | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 716 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 717 | std::vector<std::string> dex_locations; | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 718 | std::vector<unique_fd> apk_fds; | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 719 | unique_fd apk_fd(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW)); | 
|  | 720 | if (apk_fd == -1) { | 
|  | 721 | PLOG(ERROR) << "installd cannot open " << code_path.c_str(); | 
|  | 722 | return false; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 723 | } | 
| Victor Hsieh | 76f19ba | 2020-08-10 14:37:26 -0700 | [diff] [blame] | 724 | dex_locations.push_back(Basename(code_path)); | 
| Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 725 | apk_fds.push_back(std::move(apk_fd)); | 
|  | 726 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 727 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 728 | RunProfman profman_dump; | 
|  | 729 | profman_dump.SetupDump(profile_fds, reference_profile_fd, dex_locations, apk_fds, output_fd); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 730 | pid_t pid = fork(); | 
|  | 731 | if (pid == 0) { | 
|  | 732 | /* child -- drop privileges before continuing */ | 
|  | 733 | drop_capabilities(uid); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 734 | profman_dump.Exec(); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 735 | } | 
|  | 736 | /* parent */ | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 737 | int return_code = wait_child(pid); | 
|  | 738 | if (!WIFEXITED(return_code)) { | 
|  | 739 | LOG(WARNING) << "profman failed for package " << pkgname << ": " | 
|  | 740 | << return_code; | 
|  | 741 | return false; | 
|  | 742 | } | 
|  | 743 | return true; | 
|  | 744 | } | 
|  | 745 |  | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 746 | bool copy_system_profile(const std::string& system_profile, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 747 | uid_t packageUid, const std::string& package_name, const std::string& profile_name) { | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 748 | unique_fd in_fd(open(system_profile.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); | 
|  | 749 | unique_fd out_fd(open_reference_profile(packageUid, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 750 | package_name, | 
|  | 751 | profile_name, | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 752 | /*read_write*/ true, | 
|  | 753 | /*secondary*/ false)); | 
|  | 754 | if (in_fd.get() < 0) { | 
|  | 755 | PLOG(WARNING) << "Could not open profile " << system_profile; | 
|  | 756 | return false; | 
|  | 757 | } | 
|  | 758 | if (out_fd.get() < 0) { | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 759 | PLOG(WARNING) << "Could not open profile " << package_name; | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 760 | return false; | 
|  | 761 | } | 
|  | 762 |  | 
| Mathieu Chartier | 78f71fe | 2017-06-14 13:02:26 -0700 | [diff] [blame] | 763 | // As a security measure we want to write the profile information with the reduced capabilities | 
|  | 764 | // of the package user id. So we fork and drop capabilities in the child. | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 765 | pid_t pid = fork(); | 
|  | 766 | if (pid == 0) { | 
|  | 767 | /* child -- drop privileges before continuing */ | 
|  | 768 | drop_capabilities(packageUid); | 
|  | 769 |  | 
|  | 770 | if (flock(out_fd.get(), LOCK_EX | LOCK_NB) != 0) { | 
|  | 771 | if (errno != EWOULDBLOCK) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 772 | async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Error locking profile %s: %d", | 
|  | 773 | package_name.c_str(), errno); | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 774 | } | 
|  | 775 | // This implies that the app owning this profile is running | 
|  | 776 | // (and has acquired the lock). | 
|  | 777 | // | 
|  | 778 | // The app never acquires the lock for the reference profiles of primary apks. | 
|  | 779 | // Only dex2oat from installd will do that. Since installd is single threaded | 
|  | 780 | // we should not see this case. Nevertheless be prepared for it. | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 781 | async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Failed to flock %s: %d", | 
|  | 782 | package_name.c_str(), errno); | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 783 | return false; | 
|  | 784 | } | 
|  | 785 |  | 
|  | 786 | bool truncated = ftruncate(out_fd.get(), 0) == 0; | 
|  | 787 | if (!truncated) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 788 | async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Could not truncate %s: %d", | 
|  | 789 | package_name.c_str(), errno); | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 790 | } | 
|  | 791 |  | 
|  | 792 | // Copy over data. | 
|  | 793 | static constexpr size_t kBufferSize = 4 * 1024; | 
|  | 794 | char buffer[kBufferSize]; | 
|  | 795 | while (true) { | 
|  | 796 | ssize_t bytes = read(in_fd.get(), buffer, kBufferSize); | 
|  | 797 | if (bytes == 0) { | 
|  | 798 | break; | 
|  | 799 | } | 
|  | 800 | write(out_fd.get(), buffer, bytes); | 
|  | 801 | } | 
|  | 802 | if (flock(out_fd.get(), LOCK_UN) != 0) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 803 | async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Error unlocking profile %s: %d", | 
|  | 804 | package_name.c_str(), errno); | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 805 | } | 
| Mathieu Chartier | 78f71fe | 2017-06-14 13:02:26 -0700 | [diff] [blame] | 806 | // Use _exit since we don't want to run the global destructors in the child. | 
|  | 807 | // b/62597429 | 
|  | 808 | _exit(0); | 
| Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 809 | } | 
|  | 810 | /* parent */ | 
|  | 811 | int return_code = wait_child(pid); | 
|  | 812 | return return_code == 0; | 
|  | 813 | } | 
|  | 814 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 815 | static std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) { | 
|  | 816 | // A standard dalvik-cache entry. Replace ".dex" with `new_ext`. | 
|  | 817 | if (EndsWith(oat_path, ".dex")) { | 
|  | 818 | std::string new_path = oat_path; | 
|  | 819 | new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext); | 
| Elliott Hughes | 969e4f8 | 2017-12-20 12:34:09 -0800 | [diff] [blame] | 820 | CHECK(EndsWith(new_path, new_ext)); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 821 | return new_path; | 
|  | 822 | } | 
|  | 823 |  | 
|  | 824 | // An odex entry. Not that this may not be an extension, e.g., in the OTA | 
|  | 825 | // case (where the base name will have an extension for the B artifact). | 
|  | 826 | size_t odex_pos = oat_path.rfind(".odex"); | 
|  | 827 | if (odex_pos != std::string::npos) { | 
|  | 828 | std::string new_path = oat_path; | 
|  | 829 | new_path.replace(odex_pos, strlen(".odex"), new_ext); | 
|  | 830 | CHECK_NE(new_path.find(new_ext), std::string::npos); | 
|  | 831 | return new_path; | 
|  | 832 | } | 
|  | 833 |  | 
|  | 834 | // Don't know how to handle this. | 
|  | 835 | return ""; | 
|  | 836 | } | 
|  | 837 |  | 
|  | 838 | // Translate the given oat path to an art (app image) path. An empty string | 
|  | 839 | // denotes an error. | 
|  | 840 | static std::string create_image_filename(const std::string& oat_path) { | 
|  | 841 | return replace_file_extension(oat_path, ".art"); | 
|  | 842 | } | 
|  | 843 |  | 
|  | 844 | // Translate the given oat path to a vdex path. An empty string denotes an error. | 
|  | 845 | static std::string create_vdex_filename(const std::string& oat_path) { | 
|  | 846 | return replace_file_extension(oat_path, ".vdex"); | 
|  | 847 | } | 
|  | 848 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 849 | static int open_output_file(const char* file_name, bool recreate, int permissions) { | 
|  | 850 | int flags = O_RDWR | O_CREAT; | 
|  | 851 | if (recreate) { | 
|  | 852 | if (unlink(file_name) < 0) { | 
|  | 853 | if (errno != ENOENT) { | 
|  | 854 | PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name; | 
|  | 855 | } | 
|  | 856 | } | 
|  | 857 | flags |= O_EXCL; | 
|  | 858 | } | 
|  | 859 | return open(file_name, flags, permissions); | 
|  | 860 | } | 
|  | 861 |  | 
| Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 862 | static bool set_permissions_and_ownership( | 
|  | 863 | int fd, bool is_public, int uid, const char* path, bool is_secondary_dex) { | 
|  | 864 | // Primary apks are owned by the system. Secondary dex files are owned by the app. | 
|  | 865 | int owning_uid = is_secondary_dex ? uid : AID_SYSTEM; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 866 | if (fchmod(fd, | 
|  | 867 | S_IRUSR|S_IWUSR|S_IRGRP | | 
|  | 868 | (is_public ? S_IROTH : 0)) < 0) { | 
|  | 869 | ALOGE("installd cannot chmod '%s' during dexopt\n", path); | 
|  | 870 | return false; | 
| Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 871 | } else if (fchown(fd, owning_uid, uid) < 0) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 872 | ALOGE("installd cannot chown '%s' during dexopt\n", path); | 
|  | 873 | return false; | 
|  | 874 | } | 
|  | 875 | return true; | 
|  | 876 | } | 
|  | 877 |  | 
|  | 878 | static bool IsOutputDalvikCache(const char* oat_dir) { | 
|  | 879 | // InstallerConnection.java (which invokes installd) transforms Java null arguments | 
|  | 880 | // into '!'. Play it safe by handling it both. | 
|  | 881 | // TODO: ensure we never get null. | 
|  | 882 | // TODO: pass a flag instead of inferring if the output is dalvik cache. | 
|  | 883 | return oat_dir == nullptr || oat_dir[0] == '!'; | 
|  | 884 | } | 
|  | 885 |  | 
| Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 886 | // Best-effort check whether we can fit the the path into our buffers. | 
|  | 887 | // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run | 
|  | 888 | // without a swap file, if necessary. Reference profiles file also add an extra ".prof" | 
|  | 889 | // extension to the cache path (5 bytes). | 
|  | 890 | // TODO(calin): move away from char* buffers and PKG_PATH_MAX. | 
|  | 891 | static bool validate_dex_path_size(const std::string& dex_path) { | 
|  | 892 | if (dex_path.size() >= (PKG_PATH_MAX - 8)) { | 
|  | 893 | LOG(ERROR) << "dex_path too long: " << dex_path; | 
|  | 894 | return false; | 
|  | 895 | } | 
|  | 896 | return true; | 
|  | 897 | } | 
|  | 898 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 899 | static bool create_oat_out_path(const char* apk_path, const char* instruction_set, | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 900 | const char* oat_dir, bool is_secondary_dex, /*out*/ char* out_oat_path) { | 
| Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 901 | if (!validate_dex_path_size(apk_path)) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 902 | return false; | 
|  | 903 | } | 
|  | 904 |  | 
|  | 905 | if (!IsOutputDalvikCache(oat_dir)) { | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 906 | // Oat dirs for secondary dex files are already validated. | 
|  | 907 | if (!is_secondary_dex && validate_apk_path(oat_dir)) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 908 | ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir); | 
|  | 909 | return false; | 
|  | 910 | } | 
|  | 911 | if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) { | 
|  | 912 | return false; | 
|  | 913 | } | 
|  | 914 | } else { | 
|  | 915 | if (!create_cache_path(out_oat_path, apk_path, instruction_set)) { | 
|  | 916 | return false; | 
|  | 917 | } | 
|  | 918 | } | 
|  | 919 | return true; | 
|  | 920 | } | 
|  | 921 |  | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 922 | // (re)Creates the app image if needed. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 923 | UniqueFile maybe_open_app_image(const std::string& out_oat_path, | 
| Mathieu Chartier | 1dc3dfa | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 924 | bool generate_app_image, bool is_public, int uid, bool is_secondary_dex) { | 
| Nicolas Geoffray | aa17ab4 | 2017-08-15 14:51:05 +0100 | [diff] [blame] | 925 |  | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 926 | const std::string image_path = create_image_filename(out_oat_path); | 
|  | 927 | if (image_path.empty()) { | 
|  | 928 | // Happens when the out_oat_path has an unknown extension. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 929 | return UniqueFile(); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 930 | } | 
| Nicolas Geoffray | aa17ab4 | 2017-08-15 14:51:05 +0100 | [diff] [blame] | 931 |  | 
| Mathieu Chartier | 1dc3dfa | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 932 | // In case there is a stale image, remove it now. Ignore any error. | 
|  | 933 | unlink(image_path.c_str()); | 
|  | 934 |  | 
|  | 935 | // Not enabled, exit. | 
|  | 936 | if (!generate_app_image) { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 937 | return UniqueFile(); | 
| Nicolas Geoffray | aa17ab4 | 2017-08-15 14:51:05 +0100 | [diff] [blame] | 938 | } | 
| Mathieu Chartier | 9b2da08 | 2018-10-26 13:23:11 -0700 | [diff] [blame] | 939 | std::string app_image_format = GetProperty("dalvik.vm.appimageformat", ""); | 
|  | 940 | if (app_image_format.empty()) { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 941 | return UniqueFile(); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 942 | } | 
|  | 943 | // Recreate is true since we do not want to modify a mapped image. If the app is | 
|  | 944 | // already running and we modify the image file, it can cause crashes (b/27493510). | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 945 | UniqueFile image_file( | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 946 | open_output_file(image_path.c_str(), true /*recreate*/, 0600 /*permissions*/), | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 947 | image_path, | 
|  | 948 | UnlinkIgnoreResult); | 
|  | 949 | if (image_file.fd() < 0) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 950 | // Could not create application image file. Go on since we can compile without it. | 
|  | 951 | LOG(ERROR) << "installd could not create '" << image_path | 
|  | 952 | << "' for image file during dexopt"; | 
|  | 953 | // If we have a valid image file path but no image fd, explicitly erase the image file. | 
|  | 954 | if (unlink(image_path.c_str()) < 0) { | 
|  | 955 | if (errno != ENOENT) { | 
|  | 956 | PLOG(ERROR) << "Couldn't unlink image file " << image_path; | 
|  | 957 | } | 
|  | 958 | } | 
|  | 959 | } else if (!set_permissions_and_ownership( | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 960 | image_file.fd(), is_public, uid, image_path.c_str(), is_secondary_dex)) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 961 | ALOGE("installd cannot set owner '%s' for image during dexopt\n", image_path.c_str()); | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 962 | image_file.reset(); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 963 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 964 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 965 | return image_file; | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 966 | } | 
|  | 967 |  | 
|  | 968 | // Creates the dexopt swap file if necessary and return its fd. | 
|  | 969 | // Returns -1 if there's no need for a swap or in case of errors. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 970 | unique_fd maybe_open_dexopt_swap_file(const std::string& out_oat_path) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 971 | if (!ShouldUseSwapFileForDexopt()) { | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 972 | return invalid_unique_fd(); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 973 | } | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 974 | auto swap_file_name = out_oat_path + ".swap"; | 
| Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 975 | unique_fd swap_fd(open_output_file( | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 976 | swap_file_name.c_str(), /*recreate*/true, /*permissions*/0600)); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 977 | if (swap_fd.get() < 0) { | 
|  | 978 | // Could not create swap file. Optimistically go on and hope that we can compile | 
|  | 979 | // without it. | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 980 | ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name.c_str()); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 981 | } else { | 
|  | 982 | // Immediately unlink. We don't really want to hit flash. | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 983 | if (unlink(swap_file_name.c_str()) < 0) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 984 | PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name; | 
|  | 985 | } | 
|  | 986 | } | 
|  | 987 | return swap_fd; | 
|  | 988 | } | 
|  | 989 |  | 
|  | 990 | // Opens the reference profiles if needed. | 
|  | 991 | // Note that the reference profile might not exist so it's OK if the fd will be -1. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 992 | UniqueFile maybe_open_reference_profile(const std::string& pkgname, | 
| Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 993 | const std::string& dex_path, const char* profile_name, bool profile_guided, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 994 | bool is_public, int uid, bool is_secondary_dex) { | 
| Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 995 | // If we are not profile guided compilation, or we are compiling system server | 
|  | 996 | // do not bother to open the profiles; we won't be using them. | 
|  | 997 | if (!profile_guided || (pkgname[0] == '*')) { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 998 | return UniqueFile(); | 
| Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 999 | } | 
|  | 1000 |  | 
|  | 1001 | // If this is a secondary dex path which is public do not open the profile. | 
|  | 1002 | // We cannot compile public secondary dex paths with profiles. That's because | 
|  | 1003 | // it will expose how the dex files are used by their owner. | 
|  | 1004 | // | 
|  | 1005 | // Note that the PackageManager is responsible to set the is_public flag for | 
|  | 1006 | // primary apks and we do not check it here. In some cases, e.g. when | 
|  | 1007 | // compiling with a public profile from the .dm file the PackageManager will | 
|  | 1008 | // set is_public toghether with the profile guided compilation. | 
|  | 1009 | if (is_secondary_dex && is_public) { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1010 | return UniqueFile(); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1011 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1012 |  | 
|  | 1013 | // Open reference profile in read only mode as dex2oat does not get write permissions. | 
| Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 1014 | std::string location; | 
|  | 1015 | if (is_secondary_dex) { | 
|  | 1016 | location = dex_path; | 
|  | 1017 | } else { | 
|  | 1018 | if (profile_name == nullptr) { | 
|  | 1019 | // This path is taken for system server re-compilation lunched from ZygoteInit. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1020 | return UniqueFile(); | 
| Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 1021 | } else { | 
|  | 1022 | location = profile_name; | 
|  | 1023 | } | 
|  | 1024 | } | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1025 | return open_reference_profile_as_unique_file(uid, pkgname, location, /*read_write*/false, | 
|  | 1026 | is_secondary_dex); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1027 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1028 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1029 | // Opens the vdex files and assigns the input fd to in_vdex_wrapper and the output fd to | 
|  | 1030 | // out_vdex_wrapper. Returns true for success or false in case of errors. | 
| Shubham Ajmera | b6bcd22 | 2017-10-19 10:08:03 -0700 | [diff] [blame] | 1031 | bool open_vdex_files_for_dex2oat(const char* apk_path, const char* out_oat_path, int dexopt_needed, | 
| Nicolas Geoffray | 3c95f2d | 2017-04-24 13:34:59 +0000 | [diff] [blame] | 1032 | const char* instruction_set, bool is_public, int uid, bool is_secondary_dex, | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1033 | bool profile_guided, UniqueFile* in_vdex_wrapper, | 
|  | 1034 | UniqueFile* out_vdex_wrapper) { | 
|  | 1035 | CHECK(in_vdex_wrapper != nullptr); | 
|  | 1036 | CHECK(out_vdex_wrapper != nullptr); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1037 | // Open the existing VDEX. We do this before creating the new output VDEX, which will | 
|  | 1038 | // unlink the old one. | 
| Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1039 | char in_odex_path[PKG_PATH_MAX]; | 
|  | 1040 | int dexopt_action = abs(dexopt_needed); | 
|  | 1041 | bool is_odex_location = dexopt_needed < 0; | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1042 |  | 
|  | 1043 | // Infer the name of the output VDEX. | 
|  | 1044 | const std::string out_vdex_path_str = create_vdex_filename(out_oat_path); | 
|  | 1045 | if (out_vdex_path_str.empty()) { | 
|  | 1046 | return false; | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | bool update_vdex_in_place = false; | 
| Nicolas Geoffray | 3c95f2d | 2017-04-24 13:34:59 +0000 | [diff] [blame] | 1050 | if (dexopt_action != DEX2OAT_FROM_SCRATCH) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1051 | // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd. | 
|  | 1052 | const char* path = nullptr; | 
|  | 1053 | if (is_odex_location) { | 
|  | 1054 | if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { | 
|  | 1055 | path = in_odex_path; | 
|  | 1056 | } else { | 
|  | 1057 | ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1058 | return false; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1059 | } | 
|  | 1060 | } else { | 
|  | 1061 | path = out_oat_path; | 
|  | 1062 | } | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1063 | std::string in_vdex_path_str = create_vdex_filename(path); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1064 | if (in_vdex_path_str.empty()) { | 
|  | 1065 | ALOGE("installd cannot compute input vdex location for '%s'\n", path); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1066 | return false; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1067 | } | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1068 | // We can update in place when all these conditions are met: | 
|  | 1069 | // 1) The vdex location to write to is the same as the vdex location to read (vdex files | 
|  | 1070 | //    on /system typically cannot be updated in place). | 
|  | 1071 | // 2) We dex2oat due to boot image change, because we then know the existing vdex file | 
|  | 1072 | //    cannot be currently used by a running process. | 
|  | 1073 | // 3) We are not doing a profile guided compilation, because dexlayout requires two | 
|  | 1074 | //    different vdex files to operate. | 
|  | 1075 | update_vdex_in_place = | 
|  | 1076 | (in_vdex_path_str == out_vdex_path_str) && | 
|  | 1077 | (dexopt_action == DEX2OAT_FOR_BOOT_IMAGE) && | 
|  | 1078 | !profile_guided; | 
|  | 1079 | if (update_vdex_in_place) { | 
|  | 1080 | // Open the file read-write to be able to update it. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1081 | in_vdex_wrapper->reset(open(in_vdex_path_str.c_str(), O_RDWR, 0), | 
|  | 1082 | in_vdex_path_str); | 
|  | 1083 | if (in_vdex_wrapper->fd() == -1) { | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1084 | // If we failed to open the file, we cannot update it in place. | 
|  | 1085 | update_vdex_in_place = false; | 
|  | 1086 | } | 
|  | 1087 | } else { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1088 | in_vdex_wrapper->reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0), | 
|  | 1089 | in_vdex_path_str); | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1090 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1091 | } | 
|  | 1092 |  | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1093 | // If we are updating the vdex in place, we do not need to recreate a vdex, | 
|  | 1094 | // and can use the same existing one. | 
|  | 1095 | if (update_vdex_in_place) { | 
|  | 1096 | // We unlink the file in case the invocation of dex2oat fails, to ensure we don't | 
|  | 1097 | // have bogus stale vdex files. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1098 | out_vdex_wrapper->reset( | 
|  | 1099 | in_vdex_wrapper->fd(), | 
|  | 1100 | out_vdex_path_str, | 
|  | 1101 | UnlinkIgnoreResult); | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1102 | // Disable auto close for the in wrapper fd (it will be done when destructing the out | 
|  | 1103 | // wrapper). | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1104 | in_vdex_wrapper->DisableAutoClose(); | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1105 | } else { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1106 | out_vdex_wrapper->reset( | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1107 | open_output_file(out_vdex_path_str.c_str(), /*recreate*/true, /*permissions*/0644), | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1108 | out_vdex_path_str, | 
|  | 1109 | UnlinkIgnoreResult); | 
|  | 1110 | if (out_vdex_wrapper->fd() < 0) { | 
| Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1111 | ALOGE("installd cannot open vdex'%s' during dexopt\n", out_vdex_path_str.c_str()); | 
|  | 1112 | return false; | 
|  | 1113 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1114 | } | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1115 | if (!set_permissions_and_ownership(out_vdex_wrapper->fd(), is_public, uid, | 
| Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 1116 | out_vdex_path_str.c_str(), is_secondary_dex)) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1117 | ALOGE("installd cannot set owner '%s' for vdex during dexopt\n", out_vdex_path_str.c_str()); | 
|  | 1118 | return false; | 
|  | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | // If we got here we successfully opened the vdex files. | 
|  | 1122 | return true; | 
|  | 1123 | } | 
|  | 1124 |  | 
|  | 1125 | // Opens the output oat file for the given apk. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1126 | UniqueFile open_oat_out_file(const char* apk_path, const char* oat_dir, | 
|  | 1127 | bool is_public, int uid, const char* instruction_set, bool is_secondary_dex) { | 
|  | 1128 | char out_oat_path[PKG_PATH_MAX]; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1129 | if (!create_oat_out_path(apk_path, instruction_set, oat_dir, is_secondary_dex, out_oat_path)) { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1130 | return UniqueFile(); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1131 | } | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1132 | UniqueFile oat( | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1133 | open_output_file(out_oat_path, /*recreate*/true, /*permissions*/0644), | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1134 | out_oat_path, | 
|  | 1135 | UnlinkIgnoreResult); | 
|  | 1136 | if (oat.fd() < 0) { | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1137 | PLOG(ERROR) << "installd cannot open output during dexopt" <<  out_oat_path; | 
| Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 1138 | } else if (!set_permissions_and_ownership( | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1139 | oat.fd(), is_public, uid, out_oat_path, is_secondary_dex)) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1140 | ALOGE("installd cannot set owner '%s' for output during dexopt\n", out_oat_path); | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1141 | oat.reset(); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1142 | } | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1143 | return oat; | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1144 | } | 
|  | 1145 |  | 
| Shubham Ajmera | b6bcd22 | 2017-10-19 10:08:03 -0700 | [diff] [blame] | 1146 | // Creates RDONLY fds for oat and vdex files, if exist. | 
|  | 1147 | // Returns false if it fails to create oat out path for the given apk path. | 
|  | 1148 | // Note that the method returns true even if the files could not be opened. | 
|  | 1149 | bool maybe_open_oat_and_vdex_file(const std::string& apk_path, | 
|  | 1150 | const std::string& oat_dir, | 
|  | 1151 | const std::string& instruction_set, | 
|  | 1152 | bool is_secondary_dex, | 
|  | 1153 | unique_fd* oat_file_fd, | 
|  | 1154 | unique_fd* vdex_file_fd) { | 
|  | 1155 | char oat_path[PKG_PATH_MAX]; | 
|  | 1156 | if (!create_oat_out_path(apk_path.c_str(), | 
|  | 1157 | instruction_set.c_str(), | 
|  | 1158 | oat_dir.c_str(), | 
|  | 1159 | is_secondary_dex, | 
|  | 1160 | oat_path)) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1161 | LOG(ERROR) << "Could not create oat out path for " | 
|  | 1162 | << apk_path << " with oat dir " << oat_dir; | 
| Shubham Ajmera | b6bcd22 | 2017-10-19 10:08:03 -0700 | [diff] [blame] | 1163 | return false; | 
|  | 1164 | } | 
|  | 1165 | oat_file_fd->reset(open(oat_path, O_RDONLY)); | 
|  | 1166 | if (oat_file_fd->get() < 0) { | 
|  | 1167 | PLOG(INFO) << "installd cannot open oat file during dexopt" <<  oat_path; | 
|  | 1168 | } | 
|  | 1169 |  | 
|  | 1170 | std::string vdex_filename = create_vdex_filename(oat_path); | 
|  | 1171 | vdex_file_fd->reset(open(vdex_filename.c_str(), O_RDONLY)); | 
|  | 1172 | if (vdex_file_fd->get() < 0) { | 
|  | 1173 | PLOG(INFO) << "installd cannot open vdex file during dexopt" <<  vdex_filename; | 
|  | 1174 | } | 
|  | 1175 |  | 
|  | 1176 | return true; | 
|  | 1177 | } | 
|  | 1178 |  | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1179 | // Runs (execv) dexoptanalyzer on the given arguments. | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1180 | // The analyzer will check if the dex_file needs to be (re)compiled to match the compiler_filter. | 
|  | 1181 | // If this is for a profile guided compilation, profile_was_updated will tell whether or not | 
|  | 1182 | // the profile has changed. | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1183 | class RunDexoptAnalyzer : public ExecVHelper { | 
|  | 1184 | public: | 
|  | 1185 | RunDexoptAnalyzer(const std::string& dex_file, | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1186 | int vdex_fd, | 
|  | 1187 | int oat_fd, | 
|  | 1188 | int zip_fd, | 
|  | 1189 | const std::string& instruction_set, | 
|  | 1190 | const std::string& compiler_filter, | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 1191 | int profile_analysis_result, | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1192 | bool downgrade, | 
|  | 1193 | const char* class_loader_context, | 
|  | 1194 | const std::string& class_loader_context_fds) { | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1195 | CHECK_GE(zip_fd, 0); | 
| Calin Juravle | f74a737 | 2019-02-28 20:29:41 -0800 | [diff] [blame] | 1196 |  | 
|  | 1197 | // We always run the analyzer in the background job. | 
|  | 1198 | const char* dexoptanalyzer_bin = select_execution_binary( | 
|  | 1199 | kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true); | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1200 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1201 | std::string dex_file_arg = "--dex-file=" + dex_file; | 
|  | 1202 | std::string oat_fd_arg = "--oat-fd=" + std::to_string(oat_fd); | 
|  | 1203 | std::string vdex_fd_arg = "--vdex-fd=" + std::to_string(vdex_fd); | 
|  | 1204 | std::string zip_fd_arg = "--zip-fd=" + std::to_string(zip_fd); | 
|  | 1205 | std::string isa_arg = "--isa=" + instruction_set; | 
|  | 1206 | std::string compiler_filter_arg = "--compiler-filter=" + compiler_filter; | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 1207 | std::string profile_analysis_arg = "--profile-analysis-result=" | 
|  | 1208 | + std::to_string(profile_analysis_result); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1209 | const char* downgrade_flag = "--downgrade"; | 
|  | 1210 | std::string class_loader_context_arg = "--class-loader-context="; | 
|  | 1211 | if (class_loader_context != nullptr) { | 
|  | 1212 | class_loader_context_arg += class_loader_context; | 
|  | 1213 | } | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1214 | std::string class_loader_context_fds_arg = "--class-loader-context-fds="; | 
|  | 1215 | if (!class_loader_context_fds.empty()) { | 
|  | 1216 | class_loader_context_fds_arg += class_loader_context_fds; | 
|  | 1217 | } | 
| Mathieu Chartier | 3163652 | 2018-11-09 23:53:07 +0000 | [diff] [blame] | 1218 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1219 | // program name, dex file, isa, filter | 
|  | 1220 | AddArg(dex_file_arg); | 
|  | 1221 | AddArg(isa_arg); | 
|  | 1222 | AddArg(compiler_filter_arg); | 
|  | 1223 | if (oat_fd >= 0) { | 
|  | 1224 | AddArg(oat_fd_arg); | 
|  | 1225 | } | 
|  | 1226 | if (vdex_fd >= 0) { | 
|  | 1227 | AddArg(vdex_fd_arg); | 
|  | 1228 | } | 
| Greg Kaiser | 8042c37 | 2019-03-26 06:23:19 -0700 | [diff] [blame] | 1229 | AddArg(zip_fd_arg); | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 1230 | AddArg(profile_analysis_arg); | 
|  | 1231 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1232 | if (downgrade) { | 
|  | 1233 | AddArg(downgrade_flag); | 
|  | 1234 | } | 
|  | 1235 | if (class_loader_context != nullptr) { | 
| Greg Kaiser | 8042c37 | 2019-03-26 06:23:19 -0700 | [diff] [blame] | 1236 | AddArg(class_loader_context_arg); | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1237 | if (!class_loader_context_fds.empty()) { | 
| Greg Kaiser | 8042c37 | 2019-03-26 06:23:19 -0700 | [diff] [blame] | 1238 | AddArg(class_loader_context_fds_arg); | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1239 | } | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1240 | } | 
| Mathieu Chartier | 3163652 | 2018-11-09 23:53:07 +0000 | [diff] [blame] | 1241 |  | 
| Orion Hodson | 5f6b3ad | 2021-07-01 12:12:40 +0100 | [diff] [blame] | 1242 | // On-device signing related. odsign sets the system property odsign.verification.success if | 
|  | 1243 | // AOT artifacts have the expected signatures. | 
|  | 1244 | const bool trust_art_apex_data_files = | 
|  | 1245 | ::android::base::GetBoolProperty("odsign.verification.success", false); | 
|  | 1246 | if (!trust_art_apex_data_files) { | 
|  | 1247 | AddRuntimeArg("-Xdeny-art-apex-data-files"); | 
|  | 1248 | } | 
|  | 1249 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1250 | PrepareArgs(dexoptanalyzer_bin); | 
|  | 1251 | } | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1252 |  | 
|  | 1253 | // Dexoptanalyzer mode which flattens the given class loader context and | 
|  | 1254 | // prints a list of its dex files in that flattened order. | 
|  | 1255 | RunDexoptAnalyzer(const char* class_loader_context) { | 
|  | 1256 | CHECK(class_loader_context != nullptr); | 
|  | 1257 |  | 
|  | 1258 | // We always run the analyzer in the background job. | 
|  | 1259 | const char* dexoptanalyzer_bin = select_execution_binary( | 
|  | 1260 | kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true); | 
|  | 1261 |  | 
|  | 1262 | AddArg("--flatten-class-loader-context"); | 
|  | 1263 | AddArg(std::string("--class-loader-context=") + class_loader_context); | 
|  | 1264 | PrepareArgs(dexoptanalyzer_bin); | 
|  | 1265 | } | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1266 | }; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1267 |  | 
|  | 1268 | // Prepares the oat dir for the secondary dex files. | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1269 | static bool prepare_secondary_dex_oat_dir(const std::string& dex_path, int uid, | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1270 | const char* instruction_set) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1271 | unsigned long dirIndex = dex_path.rfind('/'); | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1272 | if (dirIndex == std::string::npos) { | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1273 | LOG(ERROR ) << "Unexpected dir structure for secondary dex " << dex_path; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1274 | return false; | 
|  | 1275 | } | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1276 | std::string dex_dir = dex_path.substr(0, dirIndex); | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1277 |  | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1278 | // Create oat file output directory. | 
| Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1279 | mode_t oat_dir_mode = S_IRWXU | S_IRWXG | S_IXOTH; | 
|  | 1280 | if (prepare_app_cache_dir(dex_dir, "oat", oat_dir_mode, uid, uid) != 0) { | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1281 | LOG(ERROR) << "Could not prepare oat dir for secondary dex: " << dex_path; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1282 | return false; | 
|  | 1283 | } | 
|  | 1284 |  | 
|  | 1285 | char oat_dir[PKG_PATH_MAX]; | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1286 | snprintf(oat_dir, PKG_PATH_MAX, "%s/oat", dex_dir.c_str()); | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1287 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1288 | if (prepare_app_cache_dir(oat_dir, instruction_set, oat_dir_mode, uid, uid) != 0) { | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1289 | LOG(ERROR) << "Could not prepare oat/isa dir for secondary dex: " << dex_path; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1290 | return false; | 
|  | 1291 | } | 
|  | 1292 |  | 
|  | 1293 | return true; | 
|  | 1294 | } | 
|  | 1295 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1296 | // Return codes for identifying the reason why dexoptanalyzer was not invoked when processing | 
|  | 1297 | // secondary dex files. This return codes are returned by the child process created for | 
|  | 1298 | // analyzing secondary dex files in process_secondary_dex_dexopt. | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1299 |  | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1300 | enum DexoptAnalyzerSkipCodes { | 
|  | 1301 | // The dexoptanalyzer was not invoked because of validation or IO errors. | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1302 | // Specific errors are encoded in the name. | 
|  | 1303 | kSecondaryDexDexoptAnalyzerSkippedValidatePath = 200, | 
|  | 1304 | kSecondaryDexDexoptAnalyzerSkippedOpenZip = 201, | 
|  | 1305 | kSecondaryDexDexoptAnalyzerSkippedPrepareDir = 202, | 
|  | 1306 | kSecondaryDexDexoptAnalyzerSkippedOpenOutput = 203, | 
|  | 1307 | kSecondaryDexDexoptAnalyzerSkippedFailExec = 204, | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1308 | // The dexoptanalyzer was not invoked because the dex file does not exist anymore. | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1309 | kSecondaryDexDexoptAnalyzerSkippedNoFile = 205, | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1310 | }; | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1311 |  | 
|  | 1312 | // Verifies the result of analyzing secondary dex files from process_secondary_dex_dexopt. | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1313 | // If the result is valid returns true and sets dexopt_needed_out to a valid value. | 
|  | 1314 | // Returns false for errors or unexpected result values. | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1315 | // The result is expected to be either one of SECONDARY_DEX_* codes or a valid exit code | 
|  | 1316 | // of dexoptanalyzer. | 
|  | 1317 | static bool process_secondary_dexoptanalyzer_result(const std::string& dex_path, int result, | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1318 | int* dexopt_needed_out, std::string* error_msg) { | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1319 | // The result values are defined in dexoptanalyzer. | 
|  | 1320 | switch (result) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1321 | case 0:  // dexoptanalyzer: no_dexopt_needed | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1322 | *dexopt_needed_out = NO_DEXOPT_NEEDED; return true; | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1323 | case 1:  // dexoptanalyzer: dex2oat_from_scratch | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1324 | *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; return true; | 
| Vladimir Marko | 1752a11 | 2018-09-03 18:15:16 +0100 | [diff] [blame] | 1325 | case 4:  // dexoptanalyzer: dex2oat_for_bootimage_odex | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1326 | *dexopt_needed_out = -DEX2OAT_FOR_BOOT_IMAGE; return true; | 
| Vladimir Marko | 1752a11 | 2018-09-03 18:15:16 +0100 | [diff] [blame] | 1327 | case 5:  // dexoptanalyzer: dex2oat_for_filter_odex | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1328 | *dexopt_needed_out = -DEX2OAT_FOR_FILTER; return true; | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1329 | case 2:  // dexoptanalyzer: dex2oat_for_bootimage_oat | 
|  | 1330 | case 3:  // dexoptanalyzer: dex2oat_for_filter_oat | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1331 | *error_msg = StringPrintf("Dexoptanalyzer return the status of an oat file." | 
|  | 1332 | " Expected odex file status for secondary dex %s" | 
|  | 1333 | " : dexoptanalyzer result=%d", | 
|  | 1334 | dex_path.c_str(), | 
|  | 1335 | result); | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1336 | return false; | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1337 | } | 
|  | 1338 |  | 
|  | 1339 | // Use a second switch for enum switch-case analysis. | 
|  | 1340 | switch (static_cast<DexoptAnalyzerSkipCodes>(result)) { | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1341 | case kSecondaryDexDexoptAnalyzerSkippedNoFile: | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1342 | // If the file does not exist there's no need for dexopt. | 
|  | 1343 | *dexopt_needed_out = NO_DEXOPT_NEEDED; | 
|  | 1344 | return true; | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1345 |  | 
|  | 1346 | case kSecondaryDexDexoptAnalyzerSkippedValidatePath: | 
|  | 1347 | *error_msg = "Dexoptanalyzer path validation failed"; | 
|  | 1348 | return false; | 
|  | 1349 | case kSecondaryDexDexoptAnalyzerSkippedOpenZip: | 
|  | 1350 | *error_msg = "Dexoptanalyzer open zip failed"; | 
|  | 1351 | return false; | 
|  | 1352 | case kSecondaryDexDexoptAnalyzerSkippedPrepareDir: | 
|  | 1353 | *error_msg = "Dexoptanalyzer dir preparation failed"; | 
|  | 1354 | return false; | 
|  | 1355 | case kSecondaryDexDexoptAnalyzerSkippedOpenOutput: | 
|  | 1356 | *error_msg = "Dexoptanalyzer open output failed"; | 
|  | 1357 | return false; | 
|  | 1358 | case kSecondaryDexDexoptAnalyzerSkippedFailExec: | 
|  | 1359 | *error_msg = "Dexoptanalyzer failed to execute"; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1360 | return false; | 
|  | 1361 | } | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1362 |  | 
|  | 1363 | *error_msg = StringPrintf("Unexpected result from analyzing secondary dex %s result=%d", | 
|  | 1364 | dex_path.c_str(), | 
|  | 1365 | result); | 
|  | 1366 | return false; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1367 | } | 
|  | 1368 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1369 | enum SecondaryDexAccess { | 
|  | 1370 | kSecondaryDexAccessReadOk = 0, | 
|  | 1371 | kSecondaryDexAccessDoesNotExist = 1, | 
|  | 1372 | kSecondaryDexAccessPermissionError = 2, | 
|  | 1373 | kSecondaryDexAccessIOError = 3 | 
|  | 1374 | }; | 
|  | 1375 |  | 
|  | 1376 | static SecondaryDexAccess check_secondary_dex_access(const std::string& dex_path) { | 
|  | 1377 | // Check if the path exists and can be read. If not, there's nothing to do. | 
|  | 1378 | if (access(dex_path.c_str(), R_OK) == 0) { | 
|  | 1379 | return kSecondaryDexAccessReadOk; | 
|  | 1380 | } else { | 
|  | 1381 | if (errno == ENOENT) { | 
|  | 1382 | LOG(INFO) << "Secondary dex does not exist: " <<  dex_path; | 
|  | 1383 | return kSecondaryDexAccessDoesNotExist; | 
|  | 1384 | } else { | 
|  | 1385 | PLOG(ERROR) << "Could not access secondary dex " << dex_path; | 
|  | 1386 | return errno == EACCES | 
|  | 1387 | ? kSecondaryDexAccessPermissionError | 
|  | 1388 | : kSecondaryDexAccessIOError; | 
|  | 1389 | } | 
|  | 1390 | } | 
|  | 1391 | } | 
|  | 1392 |  | 
|  | 1393 | static bool is_file_public(const std::string& filename) { | 
|  | 1394 | struct stat file_stat; | 
|  | 1395 | if (stat(filename.c_str(), &file_stat) == 0) { | 
|  | 1396 | return (file_stat.st_mode & S_IROTH) != 0; | 
|  | 1397 | } | 
|  | 1398 | return false; | 
|  | 1399 | } | 
|  | 1400 |  | 
|  | 1401 | // Create the oat file structure for the secondary dex 'dex_path' and assign | 
|  | 1402 | // the individual path component to the 'out_' parameters. | 
|  | 1403 | static bool create_secondary_dex_oat_layout(const std::string& dex_path, const std::string& isa, | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1404 | char* out_oat_dir, char* out_oat_isa_dir, char* out_oat_path, std::string* error_msg) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1405 | size_t dirIndex = dex_path.rfind('/'); | 
|  | 1406 | if (dirIndex == std::string::npos) { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1407 | *error_msg = std::string("Unexpected dir structure for dex file ").append(dex_path); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1408 | return false; | 
|  | 1409 | } | 
|  | 1410 | // TODO(calin): we have similar computations in at lest 3 other places | 
|  | 1411 | // (InstalldNativeService, otapropt and dexopt). Unify them and get rid of snprintf by | 
|  | 1412 | // using string append. | 
|  | 1413 | std::string apk_dir = dex_path.substr(0, dirIndex); | 
|  | 1414 | snprintf(out_oat_dir, PKG_PATH_MAX, "%s/oat", apk_dir.c_str()); | 
|  | 1415 | snprintf(out_oat_isa_dir, PKG_PATH_MAX, "%s/%s", out_oat_dir, isa.c_str()); | 
|  | 1416 |  | 
|  | 1417 | if (!create_oat_out_path(dex_path.c_str(), isa.c_str(), out_oat_dir, | 
|  | 1418 | /*is_secondary_dex*/true, out_oat_path)) { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1419 | *error_msg = std::string("Could not create oat path for secondary dex ").append(dex_path); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1420 | return false; | 
|  | 1421 | } | 
|  | 1422 | return true; | 
|  | 1423 | } | 
|  | 1424 |  | 
|  | 1425 | // Validate that the dexopt_flags contain a valid storage flag and convert that to an installd | 
|  | 1426 | // recognized storage flags (FLAG_STORAGE_CE or FLAG_STORAGE_DE). | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1427 | static bool validate_dexopt_storage_flags(int dexopt_flags, | 
|  | 1428 | int* out_storage_flag, | 
|  | 1429 | std::string* error_msg) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1430 | if ((dexopt_flags & DEXOPT_STORAGE_CE) != 0) { | 
|  | 1431 | *out_storage_flag = FLAG_STORAGE_CE; | 
|  | 1432 | if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1433 | *error_msg = "Ambiguous secondary dex storage flag. Both, CE and DE, flags are set"; | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1434 | return false; | 
|  | 1435 | } | 
|  | 1436 | } else if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) { | 
|  | 1437 | *out_storage_flag = FLAG_STORAGE_DE; | 
|  | 1438 | } else { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1439 | *error_msg = "Secondary dex storage flag must be set"; | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1440 | return false; | 
|  | 1441 | } | 
|  | 1442 | return true; | 
|  | 1443 | } | 
|  | 1444 |  | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1445 | static bool get_class_loader_context_dex_paths(const char* class_loader_context, int uid, | 
|  | 1446 | /* out */ std::vector<std::string>* context_dex_paths) { | 
|  | 1447 | if (class_loader_context == nullptr) { | 
|  | 1448 | return true; | 
|  | 1449 | } | 
|  | 1450 |  | 
|  | 1451 | LOG(DEBUG) << "Getting dex paths for context " << class_loader_context; | 
|  | 1452 |  | 
|  | 1453 | // Pipe to get the hash result back from our child process. | 
|  | 1454 | unique_fd pipe_read, pipe_write; | 
|  | 1455 | if (!Pipe(&pipe_read, &pipe_write)) { | 
|  | 1456 | PLOG(ERROR) << "Failed to create pipe"; | 
|  | 1457 | return false; | 
|  | 1458 | } | 
|  | 1459 |  | 
|  | 1460 | pid_t pid = fork(); | 
|  | 1461 | if (pid == 0) { | 
|  | 1462 | // child -- drop privileges before continuing. | 
|  | 1463 | drop_capabilities(uid); | 
|  | 1464 |  | 
|  | 1465 | // Route stdout to `pipe_write` | 
|  | 1466 | while ((dup2(pipe_write, STDOUT_FILENO) == -1) && (errno == EINTR)) {} | 
|  | 1467 | pipe_write.reset(); | 
|  | 1468 | pipe_read.reset(); | 
|  | 1469 |  | 
|  | 1470 | RunDexoptAnalyzer run_dexopt_analyzer(class_loader_context); | 
|  | 1471 | run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec); | 
|  | 1472 | } | 
|  | 1473 |  | 
|  | 1474 | /* parent */ | 
|  | 1475 | pipe_write.reset(); | 
|  | 1476 |  | 
|  | 1477 | std::string str_dex_paths; | 
|  | 1478 | if (!ReadFdToString(pipe_read, &str_dex_paths)) { | 
|  | 1479 | PLOG(ERROR) << "Failed to read from pipe"; | 
|  | 1480 | return false; | 
|  | 1481 | } | 
|  | 1482 | pipe_read.reset(); | 
|  | 1483 |  | 
|  | 1484 | int return_code = wait_child(pid); | 
|  | 1485 | if (!WIFEXITED(return_code)) { | 
|  | 1486 | PLOG(ERROR) << "Error waiting for child dexoptanalyzer process"; | 
|  | 1487 | return false; | 
|  | 1488 | } | 
|  | 1489 |  | 
|  | 1490 | constexpr int kFlattenClassLoaderContextSuccess = 50; | 
|  | 1491 | return_code = WEXITSTATUS(return_code); | 
|  | 1492 | if (return_code != kFlattenClassLoaderContextSuccess) { | 
|  | 1493 | LOG(ERROR) << "Dexoptanalyzer could not flatten class loader context, code=" << return_code; | 
|  | 1494 | return false; | 
|  | 1495 | } | 
|  | 1496 |  | 
|  | 1497 | if (!str_dex_paths.empty()) { | 
|  | 1498 | *context_dex_paths = android::base::Split(str_dex_paths, ":"); | 
|  | 1499 | } | 
|  | 1500 | return true; | 
|  | 1501 | } | 
|  | 1502 |  | 
|  | 1503 | static int open_dex_paths(const std::vector<std::string>& dex_paths, | 
|  | 1504 | /* out */ std::vector<unique_fd>* zip_fds, /* out */ std::string* error_msg) { | 
|  | 1505 | for (const std::string& dex_path : dex_paths) { | 
|  | 1506 | zip_fds->emplace_back(open(dex_path.c_str(), O_RDONLY)); | 
|  | 1507 | if (zip_fds->back().get() < 0) { | 
|  | 1508 | *error_msg = StringPrintf( | 
|  | 1509 | "installd cannot open '%s' for input during dexopt", dex_path.c_str()); | 
|  | 1510 | if (errno == ENOENT) { | 
|  | 1511 | return kSecondaryDexDexoptAnalyzerSkippedNoFile; | 
|  | 1512 | } else { | 
|  | 1513 | return kSecondaryDexDexoptAnalyzerSkippedOpenZip; | 
|  | 1514 | } | 
|  | 1515 | } | 
|  | 1516 | } | 
|  | 1517 | return 0; | 
|  | 1518 | } | 
|  | 1519 |  | 
|  | 1520 | static std::string join_fds(const std::vector<unique_fd>& fds) { | 
|  | 1521 | std::stringstream ss; | 
|  | 1522 | bool is_first = true; | 
|  | 1523 | for (const unique_fd& fd : fds) { | 
|  | 1524 | if (is_first) { | 
|  | 1525 | is_first = false; | 
|  | 1526 | } else { | 
|  | 1527 | ss << ":"; | 
|  | 1528 | } | 
|  | 1529 | ss << fd.get(); | 
|  | 1530 | } | 
|  | 1531 | return ss.str(); | 
|  | 1532 | } | 
|  | 1533 |  | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1534 | // Processes the dex_path as a secondary dex files and return true if the path dex file should | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1535 | // be compiled. Returns false for errors (logged) or true if the secondary dex path was process | 
|  | 1536 | // successfully. | 
| Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1537 | // When returning true, the output parameters will be: | 
|  | 1538 | //   - is_public_out: whether or not the oat file should not be made public | 
|  | 1539 | //   - dexopt_needed_out: valid OatFileAsssitant::DexOptNeeded | 
|  | 1540 | //   - oat_dir_out: the oat dir path where the oat file should be stored | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1541 | static bool process_secondary_dex_dexopt(const std::string& dex_path, const char* pkgname, | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1542 | int dexopt_flags, const char* volume_uuid, int uid, const char* instruction_set, | 
| Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1543 | const char* compiler_filter, bool* is_public_out, int* dexopt_needed_out, | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1544 | std::string* oat_dir_out, bool downgrade, const char* class_loader_context, | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1545 | const std::vector<std::string>& context_dex_paths, /* out */ std::string* error_msg) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1546 | LOG(DEBUG) << "Processing secondary dex path " << dex_path; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1547 | int storage_flag; | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1548 | if (!validate_dexopt_storage_flags(dexopt_flags, &storage_flag, error_msg)) { | 
|  | 1549 | LOG(ERROR) << *error_msg; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1550 | return false; | 
|  | 1551 | } | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1552 | // Compute the oat dir as it's not easy to extract it from the child computation. | 
|  | 1553 | char oat_path[PKG_PATH_MAX]; | 
|  | 1554 | char oat_dir[PKG_PATH_MAX]; | 
|  | 1555 | char oat_isa_dir[PKG_PATH_MAX]; | 
|  | 1556 | if (!create_secondary_dex_oat_layout( | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1557 | dex_path, instruction_set, oat_dir, oat_isa_dir, oat_path, error_msg)) { | 
|  | 1558 | LOG(ERROR) << "Could not create secondary odex layout: " << *error_msg; | 
| Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1559 | return false; | 
|  | 1560 | } | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1561 | oat_dir_out->assign(oat_dir); | 
| Shubham Ajmera | b6bcd22 | 2017-10-19 10:08:03 -0700 | [diff] [blame] | 1562 |  | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1563 | pid_t pid = fork(); | 
|  | 1564 | if (pid == 0) { | 
|  | 1565 | // child -- drop privileges before continuing. | 
|  | 1566 | drop_capabilities(uid); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1567 |  | 
|  | 1568 | // Validate the path structure. | 
|  | 1569 | if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid, uid, storage_flag)) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 1570 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 1571 | "Could not validate secondary dex path %s", dex_path.c_str()); | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1572 | _exit(kSecondaryDexDexoptAnalyzerSkippedValidatePath); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1573 | } | 
|  | 1574 |  | 
|  | 1575 | // Open the dex file. | 
|  | 1576 | unique_fd zip_fd; | 
|  | 1577 | zip_fd.reset(open(dex_path.c_str(), O_RDONLY)); | 
|  | 1578 | if (zip_fd.get() < 0) { | 
|  | 1579 | if (errno == ENOENT) { | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1580 | _exit(kSecondaryDexDexoptAnalyzerSkippedNoFile); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1581 | } else { | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1582 | _exit(kSecondaryDexDexoptAnalyzerSkippedOpenZip); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1583 | } | 
|  | 1584 | } | 
|  | 1585 |  | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1586 | // Open class loader context dex files. | 
|  | 1587 | std::vector<unique_fd> context_zip_fds; | 
|  | 1588 | int open_dex_paths_rc = open_dex_paths(context_dex_paths, &context_zip_fds, error_msg); | 
|  | 1589 | if (open_dex_paths_rc != 0) { | 
|  | 1590 | _exit(open_dex_paths_rc); | 
|  | 1591 | } | 
|  | 1592 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1593 | // Prepare the oat directories. | 
|  | 1594 | if (!prepare_secondary_dex_oat_dir(dex_path, uid, instruction_set)) { | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1595 | _exit(kSecondaryDexDexoptAnalyzerSkippedPrepareDir); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1596 | } | 
|  | 1597 |  | 
|  | 1598 | // Open the vdex/oat files if any. | 
|  | 1599 | unique_fd oat_file_fd; | 
|  | 1600 | unique_fd vdex_file_fd; | 
|  | 1601 | if (!maybe_open_oat_and_vdex_file(dex_path, | 
|  | 1602 | *oat_dir_out, | 
|  | 1603 | instruction_set, | 
|  | 1604 | true /* is_secondary_dex */, | 
|  | 1605 | &oat_file_fd, | 
|  | 1606 | &vdex_file_fd)) { | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1607 | _exit(kSecondaryDexDexoptAnalyzerSkippedOpenOutput); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1608 | } | 
|  | 1609 |  | 
|  | 1610 | // Analyze profiles. | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 1611 | int profile_analysis_result = analyze_profiles(uid, pkgname, dex_path, | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1612 | /*is_secondary_dex*/true); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1613 |  | 
|  | 1614 | // Run dexoptanalyzer to get dexopt_needed code. This is not expected to return. | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1615 | // Note that we do not do it before the fork since opening the files is required to happen | 
|  | 1616 | // after forking. | 
|  | 1617 | RunDexoptAnalyzer run_dexopt_analyzer(dex_path, | 
|  | 1618 | vdex_file_fd.get(), | 
|  | 1619 | oat_file_fd.get(), | 
|  | 1620 | zip_fd.get(), | 
|  | 1621 | instruction_set, | 
| Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 1622 | compiler_filter, | 
|  | 1623 | profile_analysis_result, | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1624 | downgrade, | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1625 | class_loader_context, | 
|  | 1626 | join_fds(context_zip_fds)); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1627 | run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec); | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1628 | } | 
|  | 1629 |  | 
|  | 1630 | /* parent */ | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1631 | int result = wait_child(pid); | 
|  | 1632 | if (!WIFEXITED(result)) { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1633 | *error_msg = StringPrintf("dexoptanalyzer failed for path %s: 0x%04x", | 
|  | 1634 | dex_path.c_str(), | 
|  | 1635 | result); | 
|  | 1636 | LOG(ERROR) << *error_msg; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1637 | return false; | 
|  | 1638 | } | 
|  | 1639 | result = WEXITSTATUS(result); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1640 | // Check that we successfully executed dexoptanalyzer. | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1641 | bool success = process_secondary_dexoptanalyzer_result(dex_path, | 
|  | 1642 | result, | 
|  | 1643 | dexopt_needed_out, | 
|  | 1644 | error_msg); | 
|  | 1645 | if (!success) { | 
|  | 1646 | LOG(ERROR) << *error_msg; | 
|  | 1647 | } | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1648 |  | 
|  | 1649 | LOG(DEBUG) << "Processed secondary dex file " << dex_path << " result=" << result; | 
|  | 1650 |  | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1651 | // Run dexopt only if needed or forced. | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1652 | // Note that dexoptanalyzer is executed even if force compilation is enabled (because it | 
|  | 1653 | // makes the code simpler; force compilation is only needed during tests). | 
|  | 1654 | if (success && | 
| Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 1655 | (result != kSecondaryDexDexoptAnalyzerSkippedNoFile) && | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1656 | ((dexopt_flags & DEXOPT_FORCE) != 0)) { | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1657 | *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; | 
|  | 1658 | } | 
|  | 1659 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1660 | // Check if we should make the oat file public. | 
|  | 1661 | // Note that if the dex file is not public the compiled code cannot be made public. | 
|  | 1662 | // It is ok to check this flag outside in the parent process. | 
|  | 1663 | *is_public_out = ((dexopt_flags & DEXOPT_PUBLIC) != 0) && is_file_public(dex_path); | 
|  | 1664 |  | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1665 | return success; | 
|  | 1666 | } | 
|  | 1667 |  | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 1668 | static std::string format_dexopt_error(int status, const char* dex_path) { | 
|  | 1669 | if (WIFEXITED(status)) { | 
|  | 1670 | int int_code = WEXITSTATUS(status); | 
|  | 1671 | const char* code_name = get_return_code_name(static_cast<DexoptReturnCodes>(int_code)); | 
|  | 1672 | if (code_name != nullptr) { | 
|  | 1673 | return StringPrintf("Dex2oat invocation for %s failed: %s", dex_path, code_name); | 
|  | 1674 | } | 
|  | 1675 | } | 
|  | 1676 | return StringPrintf("Dex2oat invocation for %s failed with 0x%04x", dex_path, status); | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1677 | } | 
|  | 1678 |  | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1679 | int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* instruction_set, | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1680 | 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] | 1681 | 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] | 1682 | bool downgrade, int target_sdk_version, const char* profile_name, | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1683 | const char* dex_metadata_path, const char* compilation_reason, std::string* error_msg) { | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1684 | CHECK(pkgname != nullptr); | 
|  | 1685 | CHECK(pkgname[0] != 0); | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1686 | CHECK(error_msg != nullptr); | 
| Andreas Gampe | d32eec2 | 2018-02-28 16:02:51 -0800 | [diff] [blame] | 1687 | CHECK_EQ(dexopt_flags & ~DEXOPT_MASK, 0) | 
|  | 1688 | << "dexopt flags contains unknown fields: " << dexopt_flags; | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1689 |  | 
| Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1690 | if (!validate_dex_path_size(dex_path)) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1691 | *error_msg = StringPrintf("Failed to validate %s", dex_path); | 
| Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 1692 | return -1; | 
|  | 1693 | } | 
|  | 1694 |  | 
|  | 1695 | if (class_loader_context != nullptr && strlen(class_loader_context) > PKG_PATH_MAX) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1696 | *error_msg = StringPrintf("Class loader context exceeds the allowed size: %s", | 
|  | 1697 | class_loader_context); | 
|  | 1698 | LOG(ERROR) << *error_msg; | 
| Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 1699 | return -1; | 
| Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1700 | } | 
|  | 1701 |  | 
| Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1702 | bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0; | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1703 | bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0; | 
|  | 1704 | bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0; | 
|  | 1705 | bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1706 | bool is_secondary_dex = (dexopt_flags & DEXOPT_SECONDARY_DEX) != 0; | 
| Andreas Gampe | a73a0cb | 2017-11-02 18:14:42 -0700 | [diff] [blame] | 1707 | bool background_job_compile = (dexopt_flags & DEXOPT_IDLE_BACKGROUND_JOB) != 0; | 
| David Brazdil | 5224916 | 2018-02-12 18:04:59 -0800 | [diff] [blame] | 1708 | bool enable_hidden_api_checks = (dexopt_flags & DEXOPT_ENABLE_HIDDEN_API_CHECKS) != 0; | 
| Mathieu Chartier | f69c2f7 | 2018-03-06 13:55:58 -0800 | [diff] [blame] | 1709 | bool generate_compact_dex = (dexopt_flags & DEXOPT_GENERATE_COMPACT_DEX) != 0; | 
| Mathieu Chartier | 1dc3dfa | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 1710 | bool generate_app_image = (dexopt_flags & DEXOPT_GENERATE_APP_IMAGE) != 0; | 
| Patrick Baumann | 2271b3e | 2020-04-14 17:03:00 -0700 | [diff] [blame] | 1711 | bool for_restore = (dexopt_flags & DEXOPT_FOR_RESTORE) != 0; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1712 |  | 
|  | 1713 | // Check if we're dealing with a secondary dex file and if we need to compile it. | 
|  | 1714 | std::string oat_dir_str; | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1715 | std::vector<std::string> context_dex_paths; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1716 | if (is_secondary_dex) { | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1717 | if (!get_class_loader_context_dex_paths(class_loader_context, uid, &context_dex_paths)) { | 
|  | 1718 | *error_msg = "Failed acquiring context dex paths"; | 
|  | 1719 | return -1;  // We had an error, logged in the process method. | 
|  | 1720 | } | 
|  | 1721 |  | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1722 | if (process_secondary_dex_dexopt(dex_path, pkgname, dexopt_flags, volume_uuid, uid, | 
| Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1723 | instruction_set, compiler_filter, &is_public, &dexopt_needed, &oat_dir_str, | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1724 | downgrade, class_loader_context, context_dex_paths, error_msg)) { | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1725 | oat_dir = oat_dir_str.c_str(); | 
|  | 1726 | if (dexopt_needed == NO_DEXOPT_NEEDED) { | 
|  | 1727 | return 0;  // Nothing to do, report success. | 
|  | 1728 | } | 
|  | 1729 | } else { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1730 | if (error_msg->empty()) {  // TODO: Make this a CHECK. | 
|  | 1731 | *error_msg = "Failed processing secondary."; | 
|  | 1732 | } | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1733 | return -1;  // We had an error, logged in the process method. | 
|  | 1734 | } | 
|  | 1735 | } else { | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1736 | // Currently these flags are only used for secondary dex files. | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1737 | // Verify that they are not set for primary apks. | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1738 | CHECK((dexopt_flags & DEXOPT_STORAGE_CE) == 0); | 
|  | 1739 | CHECK((dexopt_flags & DEXOPT_STORAGE_DE) == 0); | 
|  | 1740 | } | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1741 |  | 
|  | 1742 | // Open the input file. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1743 | UniqueFile in_dex(open(dex_path, O_RDONLY, 0), dex_path); | 
|  | 1744 | if (in_dex.fd() < 0) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1745 | *error_msg = StringPrintf("installd cannot open '%s' for input during dexopt", dex_path); | 
|  | 1746 | LOG(ERROR) << *error_msg; | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1747 | return -1; | 
|  | 1748 | } | 
|  | 1749 |  | 
| David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 1750 | // Open class loader context dex files. | 
|  | 1751 | std::vector<unique_fd> context_input_fds; | 
|  | 1752 | if (open_dex_paths(context_dex_paths, &context_input_fds, error_msg) != 0) { | 
|  | 1753 | LOG(ERROR) << *error_msg; | 
|  | 1754 | return -1; | 
|  | 1755 | } | 
|  | 1756 |  | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1757 | // Create the output OAT file. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1758 | UniqueFile out_oat = open_oat_out_file(dex_path, oat_dir, is_public, uid, | 
|  | 1759 | instruction_set, is_secondary_dex); | 
|  | 1760 | if (out_oat.fd() < 0) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1761 | *error_msg = "Could not open out oat file."; | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1762 | return -1; | 
|  | 1763 | } | 
|  | 1764 |  | 
|  | 1765 | // Open vdex files. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1766 | UniqueFile in_vdex; | 
|  | 1767 | UniqueFile out_vdex; | 
|  | 1768 | if (!open_vdex_files_for_dex2oat(dex_path, out_oat.path().c_str(), dexopt_needed, | 
|  | 1769 | instruction_set, is_public, uid, is_secondary_dex, profile_guided, &in_vdex, | 
|  | 1770 | &out_vdex)) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1771 | *error_msg = "Could not open vdex files."; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1772 | return -1; | 
|  | 1773 | } | 
|  | 1774 |  | 
| Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 1775 | // Ensure that the oat dir and the compiler artifacts of secondary dex files have the correct | 
|  | 1776 | // selinux context (we generate them on the fly during the dexopt invocation and they don't | 
|  | 1777 | // fully inherit their parent context). | 
|  | 1778 | // Note that for primary apk the oat files are created before, in a separate installd | 
|  | 1779 | // call which also does the restorecon. TODO(calin): unify the paths. | 
|  | 1780 | if (is_secondary_dex) { | 
|  | 1781 | if (selinux_android_restorecon_pkgdir(oat_dir, se_info, uid, | 
|  | 1782 | SELINUX_ANDROID_RESTORECON_RECURSE)) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1783 | *error_msg = std::string("Failed to restorecon ").append(oat_dir); | 
|  | 1784 | LOG(ERROR) << *error_msg; | 
| Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 1785 | return -1; | 
|  | 1786 | } | 
|  | 1787 | } | 
|  | 1788 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1789 | // Create a swap file if necessary. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1790 | unique_fd swap_fd = maybe_open_dexopt_swap_file(out_oat.path()); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1791 |  | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1792 | // Open the reference profile if needed. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1793 | UniqueFile reference_profile = maybe_open_reference_profile( | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1794 | pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex); | 
| Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1795 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1796 | if (reference_profile.fd() == -1) { | 
| liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 1797 | // We don't create an app image without reference profile since there is no speedup from | 
|  | 1798 | // loading it in that case and instead will be a small overhead. | 
|  | 1799 | generate_app_image = false; | 
|  | 1800 | } | 
|  | 1801 |  | 
|  | 1802 | // Create the app image file if needed. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1803 | UniqueFile out_image = maybe_open_app_image( | 
|  | 1804 | out_oat.path(), generate_app_image, is_public, uid, is_secondary_dex); | 
| liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame] | 1805 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1806 | UniqueFile dex_metadata; | 
| Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 1807 | if (dex_metadata_path != nullptr) { | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1808 | dex_metadata.reset(TEMP_FAILURE_RETRY(open(dex_metadata_path, O_RDONLY | O_NOFOLLOW)), | 
|  | 1809 | dex_metadata_path); | 
|  | 1810 | if (dex_metadata.fd() < 0) { | 
| Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 1811 | PLOG(ERROR) << "Failed to open dex metadata file " << dex_metadata_path; | 
|  | 1812 | } | 
|  | 1813 | } | 
|  | 1814 |  | 
| Victor Hsieh | 8948a86 | 2020-08-07 11:30:55 -0700 | [diff] [blame] | 1815 | std::string jitzygote_flag = server_configurable_flags::GetServerConfigurableFlag( | 
|  | 1816 | RUNTIME_NATIVE_BOOT_NAMESPACE, | 
|  | 1817 | ENABLE_JITZYGOTE_IMAGE, | 
|  | 1818 | /*default_value=*/ ""); | 
|  | 1819 | bool use_jitzygote_image = jitzygote_flag == "true" || IsBootClassPathProfilingEnable(); | 
|  | 1820 |  | 
| Victor Hsieh | e98e651 | 2020-08-10 15:39:22 -0700 | [diff] [blame] | 1821 | // Decide whether to use dex2oat64. | 
|  | 1822 | bool use_dex2oat64 = false; | 
|  | 1823 | // Check whether the device even supports 64-bit ABIs. | 
|  | 1824 | if (!GetProperty("ro.product.cpu.abilist64", "").empty()) { | 
|  | 1825 | use_dex2oat64 = GetBoolProperty("dalvik.vm.dex2oat64.enabled", false); | 
|  | 1826 | } | 
|  | 1827 | const char* dex2oat_bin = select_execution_binary( | 
|  | 1828 | (use_dex2oat64 ? kDex2oat64Path : kDex2oat32Path), | 
|  | 1829 | (use_dex2oat64 ? kDex2oatDebug64Path : kDex2oatDebug32Path), | 
|  | 1830 | background_job_compile); | 
|  | 1831 |  | 
| Victor Hsieh | c9821f1 | 2020-08-07 11:32:29 -0700 | [diff] [blame] | 1832 | auto execv_helper = std::make_unique<ExecVHelper>(); | 
|  | 1833 |  | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1834 | LOG(VERBOSE) << "DexInv: --- BEGIN '" << dex_path << "' ---"; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1835 |  | 
| Victor Hsieh | c9821f1 | 2020-08-07 11:32:29 -0700 | [diff] [blame] | 1836 | RunDex2Oat runner(dex2oat_bin, execv_helper.get()); | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1837 | runner.Initialize(out_oat, | 
|  | 1838 | out_vdex, | 
|  | 1839 | out_image, | 
|  | 1840 | in_dex, | 
|  | 1841 | in_vdex, | 
|  | 1842 | dex_metadata, | 
|  | 1843 | reference_profile, | 
|  | 1844 | class_loader_context, | 
|  | 1845 | join_fds(context_input_fds), | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1846 | swap_fd.get(), | 
|  | 1847 | instruction_set, | 
|  | 1848 | compiler_filter, | 
|  | 1849 | debuggable, | 
|  | 1850 | boot_complete, | 
| Patrick Baumann | 2271b3e | 2020-04-14 17:03:00 -0700 | [diff] [blame] | 1851 | for_restore, | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1852 | target_sdk_version, | 
|  | 1853 | enable_hidden_api_checks, | 
|  | 1854 | generate_compact_dex, | 
| Victor Hsieh | 8948a86 | 2020-08-07 11:30:55 -0700 | [diff] [blame] | 1855 | use_jitzygote_image, | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1856 | compilation_reason); | 
|  | 1857 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1858 | pid_t pid = fork(); | 
|  | 1859 | if (pid == 0) { | 
| Wei Wang | e0dbd9b | 2020-12-11 17:24:19 +0000 | [diff] [blame] | 1860 | // Need to set schedpolicy before dropping privileges | 
|  | 1861 | // for cgroup migration. See details at b/175178520. | 
|  | 1862 | SetDex2OatScheduling(boot_complete); | 
|  | 1863 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1864 | /* child -- drop privileges before continuing */ | 
|  | 1865 | drop_capabilities(uid); | 
|  | 1866 |  | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1867 | if (flock(out_oat.fd(), LOCK_EX | LOCK_NB) != 0) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 1868 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "flock(%s) failed", | 
|  | 1869 | out_oat.path().c_str()); | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 1870 | _exit(DexoptReturnCodes::kFlock); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1871 | } | 
|  | 1872 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 1873 | runner.Exec(DexoptReturnCodes::kDex2oatExec); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1874 | } else { | 
|  | 1875 | int res = wait_child(pid); | 
|  | 1876 | if (res == 0) { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1877 | LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' (success) ---"; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1878 | } else { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 1879 | LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' --- status=0x" | 
|  | 1880 | << std::hex << std::setw(4) << res << ", process failed"; | 
|  | 1881 | *error_msg = format_dexopt_error(res, dex_path); | 
| Andreas Gampe | 013f02e | 2017-03-20 18:36:54 -0700 | [diff] [blame] | 1882 | return res; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1883 | } | 
|  | 1884 | } | 
|  | 1885 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1886 | // We've been successful, don't delete output. | 
| Victor Hsieh | cb35a06 | 2020-08-13 16:11:13 -0700 | [diff] [blame] | 1887 | out_oat.DisableCleanup(); | 
|  | 1888 | out_vdex.DisableCleanup(); | 
|  | 1889 | out_image.DisableCleanup(); | 
|  | 1890 | reference_profile.DisableCleanup(); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1891 |  | 
|  | 1892 | return 0; | 
|  | 1893 | } | 
|  | 1894 |  | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1895 | // Try to remove the given directory. Log an error if the directory exists | 
|  | 1896 | // and is empty but could not be removed. | 
|  | 1897 | static bool rmdir_if_empty(const char* dir) { | 
|  | 1898 | if (rmdir(dir) == 0) { | 
|  | 1899 | return true; | 
|  | 1900 | } | 
|  | 1901 | if (errno == ENOENT || errno == ENOTEMPTY) { | 
|  | 1902 | return true; | 
|  | 1903 | } | 
|  | 1904 | PLOG(ERROR) << "Failed to remove dir: " << dir; | 
|  | 1905 | return false; | 
|  | 1906 | } | 
|  | 1907 |  | 
|  | 1908 | // Try to unlink the given file. Log an error if the file exists and could not | 
|  | 1909 | // be unlinked. | 
|  | 1910 | static bool unlink_if_exists(const std::string& file) { | 
|  | 1911 | if (unlink(file.c_str()) == 0) { | 
|  | 1912 | return true; | 
|  | 1913 | } | 
|  | 1914 | if (errno == ENOENT) { | 
|  | 1915 | return true; | 
|  | 1916 |  | 
|  | 1917 | } | 
|  | 1918 | PLOG(ERROR) << "Could not unlink: " << file; | 
|  | 1919 | return false; | 
|  | 1920 | } | 
|  | 1921 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1922 | enum ReconcileSecondaryDexResult { | 
|  | 1923 | kReconcileSecondaryDexExists = 0, | 
|  | 1924 | kReconcileSecondaryDexCleanedUp = 1, | 
|  | 1925 | kReconcileSecondaryDexValidationError = 2, | 
|  | 1926 | kReconcileSecondaryDexCleanUpError = 3, | 
|  | 1927 | kReconcileSecondaryDexAccessIOError = 4, | 
|  | 1928 | }; | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1929 |  | 
|  | 1930 | // Reconcile the secondary dex 'dex_path' and its generated oat files. | 
|  | 1931 | // Return true if all the parameters are valid and the secondary dex file was | 
|  | 1932 | //   processed successfully (i.e. the dex_path either exists, or if not, its corresponding | 
|  | 1933 | //   oat/vdex/art files where deleted successfully). In this case, out_secondary_dex_exists | 
|  | 1934 | //   will be true if the secondary dex file still exists. If the secondary dex file does not exist, | 
|  | 1935 | //   the method cleans up any previously generated compiler artifacts (oat, vdex, art). | 
|  | 1936 | // Return false if there were errors during processing. In this case | 
|  | 1937 | //   out_secondary_dex_exists will be set to false. | 
|  | 1938 | bool reconcile_secondary_dex_file(const std::string& dex_path, | 
|  | 1939 | const std::string& pkgname, int uid, const std::vector<std::string>& isas, | 
| Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1940 | const std::optional<std::string>& volume_uuid, int storage_flag, | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1941 | /*out*/bool* out_secondary_dex_exists) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1942 | *out_secondary_dex_exists = false;  // start by assuming the file does not exist. | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1943 | if (isas.size() == 0) { | 
|  | 1944 | LOG(ERROR) << "reconcile_secondary_dex_file called with empty isas vector"; | 
|  | 1945 | return false; | 
|  | 1946 | } | 
|  | 1947 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1948 | if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) { | 
|  | 1949 | LOG(ERROR) << "reconcile_secondary_dex_file called with invalid storage_flag: " | 
|  | 1950 | << storage_flag; | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1951 | return false; | 
|  | 1952 | } | 
|  | 1953 |  | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 1954 | // As a security measure we want to unlink art artifacts with the reduced capabilities | 
|  | 1955 | // of the package user id. So we fork and drop capabilities in the child. | 
|  | 1956 | pid_t pid = fork(); | 
|  | 1957 | if (pid == 0) { | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1958 | /* child -- drop privileges before continuing */ | 
|  | 1959 | drop_capabilities(uid); | 
|  | 1960 |  | 
| Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1961 | const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr; | 
| Greg Kaiser | 8042c37 | 2019-03-26 06:23:19 -0700 | [diff] [blame] | 1962 | if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr, | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1963 | uid, storage_flag)) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 1964 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 1965 | "Could not validate secondary dex path %s", dex_path.c_str()); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1966 | _exit(kReconcileSecondaryDexValidationError); | 
|  | 1967 | } | 
|  | 1968 |  | 
|  | 1969 | SecondaryDexAccess access_check = check_secondary_dex_access(dex_path); | 
|  | 1970 | switch (access_check) { | 
|  | 1971 | case kSecondaryDexAccessDoesNotExist: | 
|  | 1972 | // File does not exist. Proceed with cleaning. | 
|  | 1973 | break; | 
|  | 1974 | case kSecondaryDexAccessReadOk: _exit(kReconcileSecondaryDexExists); | 
|  | 1975 | case kSecondaryDexAccessIOError: _exit(kReconcileSecondaryDexAccessIOError); | 
|  | 1976 | case kSecondaryDexAccessPermissionError: _exit(kReconcileSecondaryDexValidationError); | 
|  | 1977 | default: | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 1978 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 1979 | "Unexpected result from check_secondary_dex_access: %d", access_check); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1980 | _exit(kReconcileSecondaryDexValidationError); | 
|  | 1981 | } | 
|  | 1982 |  | 
|  | 1983 | // The secondary dex does not exist anymore or it's. Clear any generated files. | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 1984 | char oat_path[PKG_PATH_MAX]; | 
|  | 1985 | char oat_dir[PKG_PATH_MAX]; | 
|  | 1986 | char oat_isa_dir[PKG_PATH_MAX]; | 
|  | 1987 | bool result = true; | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 1988 | for (size_t i = 0; i < isas.size(); i++) { | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1989 | std::string error_msg; | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1990 | if (!create_secondary_dex_oat_layout( | 
| Andreas Gampe | 194fe42 | 2018-02-28 20:16:19 -0800 | [diff] [blame] | 1991 | dex_path,isas[i], oat_dir, oat_isa_dir, oat_path, &error_msg)) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 1992 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "%s", error_msg.c_str()); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1993 | _exit(kReconcileSecondaryDexValidationError); | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 1994 | } | 
| Calin Juravle | 5131409 | 2017-05-18 15:33:05 -0700 | [diff] [blame] | 1995 |  | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 1996 | // Delete oat/vdex/art files. | 
|  | 1997 | result = unlink_if_exists(oat_path) && result; | 
|  | 1998 | result = unlink_if_exists(create_vdex_filename(oat_path)) && result; | 
|  | 1999 | result = unlink_if_exists(create_image_filename(oat_path)) && result; | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2000 |  | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 2001 | // Delete profiles. | 
|  | 2002 | std::string current_profile = create_current_profile_path( | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2003 | multiuser_get_user_id(uid), pkgname, dex_path, /*is_secondary*/true); | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 2004 | std::string reference_profile = create_reference_profile_path( | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2005 | pkgname, dex_path, /*is_secondary*/true); | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 2006 | result = unlink_if_exists(current_profile) && result; | 
|  | 2007 | result = unlink_if_exists(reference_profile) && result; | 
| Calin Juravle | 5131409 | 2017-05-18 15:33:05 -0700 | [diff] [blame] | 2008 |  | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 2009 | // We upgraded once the location of current profile for secondary dex files. | 
|  | 2010 | // Check for any previous left-overs and remove them as well. | 
|  | 2011 | std::string old_current_profile = dex_path + ".prof"; | 
|  | 2012 | result = unlink_if_exists(old_current_profile); | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 2013 |  | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 2014 | // Try removing the directories as well, they might be empty. | 
|  | 2015 | result = rmdir_if_empty(oat_isa_dir) && result; | 
|  | 2016 | result = rmdir_if_empty(oat_dir) && result; | 
|  | 2017 | } | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 2018 | if (!result) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 2019 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 2020 | "Could not validate secondary dex path %s", dex_path.c_str()); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 2021 | } | 
|  | 2022 | _exit(result ? kReconcileSecondaryDexCleanedUp : kReconcileSecondaryDexAccessIOError); | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2023 | } | 
|  | 2024 |  | 
| Shubham Ajmera | e6d7ad5 | 2017-08-25 13:07:44 -0700 | [diff] [blame] | 2025 | int return_code = wait_child(pid); | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 2026 | if (!WIFEXITED(return_code)) { | 
|  | 2027 | LOG(WARNING) << "reconcile dex failed for location " << dex_path << ": " << return_code; | 
|  | 2028 | } else { | 
|  | 2029 | return_code = WEXITSTATUS(return_code); | 
|  | 2030 | } | 
|  | 2031 |  | 
|  | 2032 | LOG(DEBUG) << "Reconcile secondary dex path " << dex_path << " result=" << return_code; | 
|  | 2033 |  | 
|  | 2034 | switch (return_code) { | 
|  | 2035 | case kReconcileSecondaryDexCleanedUp: | 
|  | 2036 | case kReconcileSecondaryDexValidationError: | 
|  | 2037 | // If we couldn't validate assume the dex file does not exist. | 
|  | 2038 | // This will purge the entry from the PM records. | 
|  | 2039 | *out_secondary_dex_exists = false; | 
|  | 2040 | return true; | 
|  | 2041 | case kReconcileSecondaryDexExists: | 
|  | 2042 | *out_secondary_dex_exists = true; | 
|  | 2043 | return true; | 
|  | 2044 | case kReconcileSecondaryDexAccessIOError: | 
|  | 2045 | // We had an access IO error. | 
|  | 2046 | // Return false so that we can try again. | 
|  | 2047 | // The value of out_secondary_dex_exists does not matter in this case and by convention | 
|  | 2048 | // is set to false. | 
|  | 2049 | *out_secondary_dex_exists = false; | 
|  | 2050 | return false; | 
|  | 2051 | default: | 
|  | 2052 | LOG(ERROR) << "Unexpected code from reconcile_secondary_dex_file: " << return_code; | 
|  | 2053 | *out_secondary_dex_exists = false; | 
|  | 2054 | return false; | 
|  | 2055 | } | 
| Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2056 | } | 
|  | 2057 |  | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2058 | // Compute and return the hash (SHA-256) of the secondary dex file at dex_path. | 
|  | 2059 | // Returns true if all parameters are valid and the hash successfully computed and stored in | 
|  | 2060 | // out_secondary_dex_hash. | 
|  | 2061 | // Also returns true with an empty hash if the file does not currently exist or is not accessible to | 
|  | 2062 | // the app. | 
|  | 2063 | // For any other errors (e.g. if any of the parameters are invalid) returns false. | 
|  | 2064 | bool hash_secondary_dex_file(const std::string& dex_path, const std::string& pkgname, int uid, | 
| Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2065 | const std::optional<std::string>& volume_uuid, int storage_flag, | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2066 | std::vector<uint8_t>* out_secondary_dex_hash) { | 
|  | 2067 | out_secondary_dex_hash->clear(); | 
|  | 2068 |  | 
| Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2069 | const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr; | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2070 |  | 
|  | 2071 | if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) { | 
|  | 2072 | LOG(ERROR) << "hash_secondary_dex_file called with invalid storage_flag: " | 
|  | 2073 | << storage_flag; | 
|  | 2074 | return false; | 
|  | 2075 | } | 
|  | 2076 |  | 
|  | 2077 | // Pipe to get the hash result back from our child process. | 
|  | 2078 | unique_fd pipe_read, pipe_write; | 
|  | 2079 | if (!Pipe(&pipe_read, &pipe_write)) { | 
|  | 2080 | PLOG(ERROR) << "Failed to create pipe"; | 
|  | 2081 | return false; | 
|  | 2082 | } | 
|  | 2083 |  | 
|  | 2084 | // Fork so that actual access to the files is done in the app's own UID, to ensure we only | 
|  | 2085 | // access data the app itself can access. | 
|  | 2086 | pid_t pid = fork(); | 
|  | 2087 | if (pid == 0) { | 
|  | 2088 | // child -- drop privileges before continuing | 
|  | 2089 | drop_capabilities(uid); | 
|  | 2090 | pipe_read.reset(); | 
|  | 2091 |  | 
|  | 2092 | if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr, uid, storage_flag)) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 2093 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 2094 | "Could not validate secondary dex path %s", dex_path.c_str()); | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 2095 | _exit(DexoptReturnCodes::kHashValidatePath); | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2096 | } | 
|  | 2097 |  | 
|  | 2098 | unique_fd fd(TEMP_FAILURE_RETRY(open(dex_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW))); | 
|  | 2099 | if (fd == -1) { | 
|  | 2100 | if (errno == EACCES || errno == ENOENT) { | 
|  | 2101 | // Not treated as an error. | 
|  | 2102 | _exit(0); | 
|  | 2103 | } | 
|  | 2104 | PLOG(ERROR) << "Failed to open secondary dex " << dex_path; | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 2105 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 2106 | "Failed to open secondary dex %s: %d", dex_path.c_str(), errno); | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 2107 | _exit(DexoptReturnCodes::kHashOpenPath); | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2108 | } | 
|  | 2109 |  | 
|  | 2110 | SHA256_CTX ctx; | 
|  | 2111 | SHA256_Init(&ctx); | 
|  | 2112 |  | 
|  | 2113 | std::vector<uint8_t> buffer(65536); | 
|  | 2114 | while (true) { | 
|  | 2115 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size())); | 
|  | 2116 | if (bytes_read == 0) { | 
|  | 2117 | break; | 
|  | 2118 | } else if (bytes_read == -1) { | 
| Martijn Coenen | 6de402a | 2021-04-26 16:23:40 +0200 | [diff] [blame] | 2119 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, | 
|  | 2120 | "Failed to read secondary dex %s: %d", dex_path.c_str(), errno); | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 2121 | _exit(DexoptReturnCodes::kHashReadDex); | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2122 | } | 
|  | 2123 |  | 
|  | 2124 | SHA256_Update(&ctx, buffer.data(), bytes_read); | 
|  | 2125 | } | 
|  | 2126 |  | 
|  | 2127 | std::array<uint8_t, SHA256_DIGEST_LENGTH> hash; | 
|  | 2128 | SHA256_Final(hash.data(), &ctx); | 
|  | 2129 | if (!WriteFully(pipe_write, hash.data(), hash.size())) { | 
| Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 2130 | _exit(DexoptReturnCodes::kHashWrite); | 
| Alan Stokes | a25d90c | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2131 | } | 
|  | 2132 |  | 
|  | 2133 | _exit(0); | 
|  | 2134 | } | 
|  | 2135 |  | 
|  | 2136 | // parent | 
|  | 2137 | pipe_write.reset(); | 
|  | 2138 |  | 
|  | 2139 | out_secondary_dex_hash->resize(SHA256_DIGEST_LENGTH); | 
|  | 2140 | if (!ReadFully(pipe_read, out_secondary_dex_hash->data(), out_secondary_dex_hash->size())) { | 
|  | 2141 | out_secondary_dex_hash->clear(); | 
|  | 2142 | } | 
|  | 2143 | return wait_child(pid) == 0; | 
|  | 2144 | } | 
|  | 2145 |  | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2146 | // Helper for move_ab, so that we can have common failure-case cleanup. | 
|  | 2147 | static bool unlink_and_rename(const char* from, const char* to) { | 
|  | 2148 | // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise, | 
|  | 2149 | // return a failure. | 
|  | 2150 | struct stat s; | 
|  | 2151 | if (stat(to, &s) == 0) { | 
|  | 2152 | if (!S_ISREG(s.st_mode)) { | 
|  | 2153 | LOG(ERROR) << from << " is not a regular file to replace for A/B."; | 
|  | 2154 | return false; | 
|  | 2155 | } | 
|  | 2156 | if (unlink(to) != 0) { | 
|  | 2157 | LOG(ERROR) << "Could not unlink " << to << " to move A/B."; | 
|  | 2158 | return false; | 
|  | 2159 | } | 
|  | 2160 | } else { | 
|  | 2161 | // This may be a permission problem. We could investigate the error code, but we'll just | 
|  | 2162 | // let the rename failure do the work for us. | 
|  | 2163 | } | 
|  | 2164 |  | 
|  | 2165 | // Try to rename "to" to "from." | 
|  | 2166 | if (rename(from, to) != 0) { | 
|  | 2167 | PLOG(ERROR) << "Could not rename " << from << " to " << to; | 
|  | 2168 | return false; | 
|  | 2169 | } | 
|  | 2170 | return true; | 
|  | 2171 | } | 
|  | 2172 |  | 
|  | 2173 | // Move/rename a B artifact (from) to an A artifact (to). | 
|  | 2174 | static bool move_ab_path(const std::string& b_path, const std::string& a_path) { | 
|  | 2175 | // Check whether B exists. | 
|  | 2176 | { | 
|  | 2177 | struct stat s; | 
|  | 2178 | if (stat(b_path.c_str(), &s) != 0) { | 
| Alex Light | 9f29566 | 2021-02-25 11:50:33 -0800 | [diff] [blame] | 2179 | // Ignore for now. The service calling this isn't smart enough to | 
|  | 2180 | // understand lack of artifacts at the moment. | 
|  | 2181 | LOG(VERBOSE) << "A/B artifact " << b_path << " does not exist!"; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2182 | return false; | 
|  | 2183 | } | 
|  | 2184 | if (!S_ISREG(s.st_mode)) { | 
|  | 2185 | LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file."; | 
|  | 2186 | // Try to unlink, but swallow errors. | 
|  | 2187 | unlink(b_path.c_str()); | 
|  | 2188 | return false; | 
|  | 2189 | } | 
|  | 2190 | } | 
|  | 2191 |  | 
|  | 2192 | // Rename B to A. | 
|  | 2193 | if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) { | 
|  | 2194 | // Delete the b_path so we don't try again (or fail earlier). | 
|  | 2195 | if (unlink(b_path.c_str()) != 0) { | 
|  | 2196 | PLOG(ERROR) << "Could not unlink " << b_path; | 
|  | 2197 | } | 
|  | 2198 |  | 
|  | 2199 | return false; | 
|  | 2200 | } | 
|  | 2201 |  | 
|  | 2202 | return true; | 
|  | 2203 | } | 
|  | 2204 |  | 
|  | 2205 | bool move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) { | 
|  | 2206 | // Get the current slot suffix. No suffix, no A/B. | 
| Mathieu Chartier | 9b2da08 | 2018-10-26 13:23:11 -0700 | [diff] [blame] | 2207 | const std::string slot_suffix = GetProperty("ro.boot.slot_suffix", ""); | 
|  | 2208 | if (slot_suffix.empty()) { | 
|  | 2209 | return false; | 
|  | 2210 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2211 |  | 
| Mathieu Chartier | 9b2da08 | 2018-10-26 13:23:11 -0700 | [diff] [blame] | 2212 | if (!ValidateTargetSlotSuffix(slot_suffix)) { | 
|  | 2213 | LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix; | 
|  | 2214 | return false; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2215 | } | 
|  | 2216 |  | 
|  | 2217 | // Validate other inputs. | 
|  | 2218 | if (validate_apk_path(apk_path) != 0) { | 
|  | 2219 | LOG(ERROR) << "Invalid apk_path: " << apk_path; | 
|  | 2220 | return false; | 
|  | 2221 | } | 
|  | 2222 | if (validate_apk_path(oat_dir) != 0) { | 
|  | 2223 | LOG(ERROR) << "Invalid oat_dir: " << oat_dir; | 
|  | 2224 | return false; | 
|  | 2225 | } | 
|  | 2226 |  | 
|  | 2227 | char a_path[PKG_PATH_MAX]; | 
|  | 2228 | if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) { | 
|  | 2229 | return false; | 
|  | 2230 | } | 
|  | 2231 | const std::string a_vdex_path = create_vdex_filename(a_path); | 
|  | 2232 | const std::string a_image_path = create_image_filename(a_path); | 
|  | 2233 |  | 
|  | 2234 | // B path = A path + slot suffix. | 
|  | 2235 | const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str()); | 
|  | 2236 | const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str()); | 
|  | 2237 | const std::string b_image_path = StringPrintf("%s.%s", | 
|  | 2238 | a_image_path.c_str(), | 
|  | 2239 | slot_suffix.c_str()); | 
|  | 2240 |  | 
|  | 2241 | bool success = true; | 
|  | 2242 | if (move_ab_path(b_path, a_path)) { | 
|  | 2243 | if (move_ab_path(b_vdex_path, a_vdex_path)) { | 
|  | 2244 | // Note: we can live without an app image. As such, ignore failure to move the image file. | 
|  | 2245 | //       If we decide to require the app image, or the app image being moved correctly, | 
|  | 2246 | //       then change accordingly. | 
|  | 2247 | constexpr bool kIgnoreAppImageFailure = true; | 
|  | 2248 |  | 
|  | 2249 | if (!a_image_path.empty()) { | 
|  | 2250 | if (!move_ab_path(b_image_path, a_image_path)) { | 
|  | 2251 | unlink(a_image_path.c_str()); | 
|  | 2252 | if (!kIgnoreAppImageFailure) { | 
|  | 2253 | success = false; | 
|  | 2254 | } | 
|  | 2255 | } | 
|  | 2256 | } | 
|  | 2257 | } else { | 
|  | 2258 | // Cleanup: delete B image, ignore errors. | 
|  | 2259 | unlink(b_image_path.c_str()); | 
|  | 2260 | success = false; | 
|  | 2261 | } | 
|  | 2262 | } else { | 
|  | 2263 | // Cleanup: delete B image, ignore errors. | 
|  | 2264 | unlink(b_vdex_path.c_str()); | 
|  | 2265 | unlink(b_image_path.c_str()); | 
|  | 2266 | success = false; | 
|  | 2267 | } | 
|  | 2268 | return success; | 
|  | 2269 | } | 
|  | 2270 |  | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2271 | int64_t delete_odex(const char* apk_path, const char* instruction_set, const char* oat_dir) { | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2272 | // Delete the oat/odex file. | 
|  | 2273 | char out_path[PKG_PATH_MAX]; | 
| Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 2274 | if (!create_oat_out_path(apk_path, instruction_set, oat_dir, | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 2275 | /*is_secondary_dex*/false, out_path)) { | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2276 | LOG(ERROR) << "Cannot create apk path for " << apk_path; | 
|  | 2277 | return -1; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2278 | } | 
|  | 2279 |  | 
|  | 2280 | // In case of a permission failure report the issue. Otherwise just print a warning. | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2281 | auto unlink_and_check = [](const char* path) -> int64_t { | 
|  | 2282 | struct stat file_stat; | 
|  | 2283 | if (stat(path, &file_stat) != 0) { | 
|  | 2284 | if (errno != ENOENT) { | 
|  | 2285 | PLOG(ERROR) << "Could not stat " << path; | 
|  | 2286 | return -1; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2287 | } | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2288 | return 0; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2289 | } | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2290 |  | 
|  | 2291 | if (unlink(path) != 0) { | 
|  | 2292 | if (errno != ENOENT) { | 
|  | 2293 | PLOG(ERROR) << "Could not unlink " << path; | 
|  | 2294 | return -1; | 
|  | 2295 | } | 
|  | 2296 | } | 
|  | 2297 | return static_cast<int64_t>(file_stat.st_size); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2298 | }; | 
|  | 2299 |  | 
|  | 2300 | // Delete the oat/odex file. | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2301 | int64_t return_value_oat = unlink_and_check(out_path); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2302 |  | 
|  | 2303 | // Derive and delete the app image. | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2304 | int64_t return_value_art = unlink_and_check(create_image_filename(out_path).c_str()); | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2305 |  | 
| Nicolas Geoffray | 192fb96 | 2017-05-25 13:58:06 +0100 | [diff] [blame] | 2306 | // Derive and delete the vdex file. | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2307 | int64_t return_value_vdex = unlink_and_check(create_vdex_filename(out_path).c_str()); | 
| Nicolas Geoffray | 192fb96 | 2017-05-25 13:58:06 +0100 | [diff] [blame] | 2308 |  | 
| Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 2309 | // Report result | 
|  | 2310 | if (return_value_oat == -1 | 
|  | 2311 | || return_value_art == -1 | 
|  | 2312 | || return_value_vdex == -1) { | 
|  | 2313 | return -1; | 
|  | 2314 | } | 
|  | 2315 |  | 
|  | 2316 | return return_value_oat + return_value_art + return_value_vdex; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2317 | } | 
|  | 2318 |  | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 2319 | static bool is_absolute_path(const std::string& path) { | 
|  | 2320 | if (path.find('/') != 0 || path.find("..") != std::string::npos) { | 
|  | 2321 | LOG(ERROR) << "Invalid absolute path " << path; | 
|  | 2322 | return false; | 
|  | 2323 | } else { | 
|  | 2324 | return true; | 
|  | 2325 | } | 
|  | 2326 | } | 
|  | 2327 |  | 
|  | 2328 | static bool is_valid_instruction_set(const std::string& instruction_set) { | 
|  | 2329 | // TODO: add explicit whitelisting of instruction sets | 
|  | 2330 | if (instruction_set.find('/') != std::string::npos) { | 
|  | 2331 | LOG(ERROR) << "Invalid instruction set " << instruction_set; | 
|  | 2332 | return false; | 
|  | 2333 | } else { | 
|  | 2334 | return true; | 
|  | 2335 | } | 
|  | 2336 | } | 
|  | 2337 |  | 
|  | 2338 | bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir, | 
|  | 2339 | const char *apk_path, const char *instruction_set) { | 
|  | 2340 | std::string oat_dir_ = oat_dir; | 
|  | 2341 | std::string apk_path_ = apk_path; | 
|  | 2342 | std::string instruction_set_ = instruction_set; | 
|  | 2343 |  | 
|  | 2344 | if (!is_absolute_path(oat_dir_)) return false; | 
|  | 2345 | if (!is_absolute_path(apk_path_)) return false; | 
|  | 2346 | if (!is_valid_instruction_set(instruction_set_)) return false; | 
|  | 2347 |  | 
|  | 2348 | std::string::size_type end = apk_path_.rfind('.'); | 
|  | 2349 | std::string::size_type start = apk_path_.rfind('/', end); | 
|  | 2350 | if (end == std::string::npos || start == std::string::npos) { | 
|  | 2351 | LOG(ERROR) << "Invalid apk_path " << apk_path_; | 
|  | 2352 | return false; | 
|  | 2353 | } | 
|  | 2354 |  | 
|  | 2355 | std::string res_ = oat_dir_ + '/' + instruction_set + '/' | 
|  | 2356 | + apk_path_.substr(start + 1, end - start - 1) + ".odex"; | 
|  | 2357 | const char* res = res_.c_str(); | 
|  | 2358 | if (strlen(res) >= PKG_PATH_MAX) { | 
|  | 2359 | LOG(ERROR) << "Result too large"; | 
|  | 2360 | return false; | 
|  | 2361 | } else { | 
|  | 2362 | strlcpy(path, res, PKG_PATH_MAX); | 
|  | 2363 | return true; | 
|  | 2364 | } | 
|  | 2365 | } | 
|  | 2366 |  | 
|  | 2367 | bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path, | 
|  | 2368 | const char *instruction_set) { | 
|  | 2369 | std::string apk_path_ = apk_path; | 
|  | 2370 | std::string instruction_set_ = instruction_set; | 
|  | 2371 |  | 
|  | 2372 | if (!is_absolute_path(apk_path_)) return false; | 
|  | 2373 | if (!is_valid_instruction_set(instruction_set_)) return false; | 
|  | 2374 |  | 
|  | 2375 | std::string::size_type end = apk_path_.rfind('.'); | 
|  | 2376 | std::string::size_type start = apk_path_.rfind('/', end); | 
|  | 2377 | if (end == std::string::npos || start == std::string::npos) { | 
|  | 2378 | LOG(ERROR) << "Invalid apk_path " << apk_path_; | 
|  | 2379 | return false; | 
|  | 2380 | } | 
|  | 2381 |  | 
|  | 2382 | std::string oat_dir = apk_path_.substr(0, start + 1) + "oat"; | 
|  | 2383 | return calculate_oat_file_path_default(path, oat_dir.c_str(), apk_path, instruction_set); | 
|  | 2384 | } | 
|  | 2385 |  | 
|  | 2386 | bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src, | 
|  | 2387 | const char *instruction_set) { | 
|  | 2388 | std::string src_ = src; | 
|  | 2389 | std::string instruction_set_ = instruction_set; | 
|  | 2390 |  | 
|  | 2391 | if (!is_absolute_path(src_)) return false; | 
|  | 2392 | if (!is_valid_instruction_set(instruction_set_)) return false; | 
|  | 2393 |  | 
|  | 2394 | for (auto it = src_.begin() + 1; it < src_.end(); ++it) { | 
|  | 2395 | if (*it == '/') { | 
|  | 2396 | *it = '@'; | 
|  | 2397 | } | 
|  | 2398 | } | 
|  | 2399 |  | 
|  | 2400 | std::string res_ = android_data_dir + DALVIK_CACHE + '/' + instruction_set_ + src_ | 
|  | 2401 | + DALVIK_CACHE_POSTFIX; | 
|  | 2402 | const char* res = res_.c_str(); | 
|  | 2403 | if (strlen(res) >= PKG_PATH_MAX) { | 
|  | 2404 | LOG(ERROR) << "Result too large"; | 
|  | 2405 | return false; | 
|  | 2406 | } else { | 
|  | 2407 | strlcpy(path, res, PKG_PATH_MAX); | 
|  | 2408 | return true; | 
|  | 2409 | } | 
|  | 2410 | } | 
|  | 2411 |  | 
| Calin Juravle | 59f7ab8 | 2018-04-27 17:50:23 -0700 | [diff] [blame] | 2412 | bool open_classpath_files(const std::string& classpath, std::vector<unique_fd>* apk_fds, | 
|  | 2413 | std::vector<std::string>* dex_locations) { | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2414 | std::vector<std::string> classpaths_elems = base::Split(classpath, ":"); | 
|  | 2415 | for (const std::string& elem : classpaths_elems) { | 
|  | 2416 | unique_fd fd(TEMP_FAILURE_RETRY(open(elem.c_str(), O_RDONLY))); | 
|  | 2417 | if (fd < 0) { | 
|  | 2418 | PLOG(ERROR) << "Could not open classpath elem " << elem; | 
|  | 2419 | return false; | 
|  | 2420 | } else { | 
|  | 2421 | apk_fds->push_back(std::move(fd)); | 
| Calin Juravle | 59f7ab8 | 2018-04-27 17:50:23 -0700 | [diff] [blame] | 2422 | dex_locations->push_back(elem); | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2423 | } | 
|  | 2424 | } | 
|  | 2425 | return true; | 
|  | 2426 | } | 
|  | 2427 |  | 
|  | 2428 | static bool create_app_profile_snapshot(int32_t app_id, | 
|  | 2429 | const std::string& package_name, | 
|  | 2430 | const std::string& profile_name, | 
|  | 2431 | const std::string& classpath) { | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2432 | int app_shared_gid = multiuser_get_shared_gid(/*user_id*/ 0, app_id); | 
|  | 2433 |  | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2434 | unique_fd snapshot_fd = open_spnashot_profile(AID_SYSTEM, package_name, profile_name); | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2435 | if (snapshot_fd < 0) { | 
|  | 2436 | return false; | 
|  | 2437 | } | 
|  | 2438 |  | 
|  | 2439 | std::vector<unique_fd> profiles_fd; | 
|  | 2440 | unique_fd reference_profile_fd; | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2441 | open_profile_files(app_shared_gid, package_name, profile_name, /*is_secondary_dex*/ false, | 
|  | 2442 | &profiles_fd, &reference_profile_fd); | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2443 | if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) { | 
|  | 2444 | return false; | 
|  | 2445 | } | 
|  | 2446 |  | 
|  | 2447 | profiles_fd.push_back(std::move(reference_profile_fd)); | 
|  | 2448 |  | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2449 | // Open the class paths elements. These will be used to filter out profile data that does | 
|  | 2450 | // not belong to the classpath during merge. | 
|  | 2451 | std::vector<unique_fd> apk_fds; | 
| Calin Juravle | 59f7ab8 | 2018-04-27 17:50:23 -0700 | [diff] [blame] | 2452 | std::vector<std::string> dex_locations; | 
|  | 2453 | if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) { | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2454 | return false; | 
|  | 2455 | } | 
|  | 2456 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 2457 | RunProfman args; | 
| Calin Juravle | f85ddb9 | 2020-05-01 14:05:40 -0700 | [diff] [blame] | 2458 | // This is specifically a snapshot for an app, so don't use boot image profiles. | 
|  | 2459 | args.SetupMerge(profiles_fd, | 
|  | 2460 | snapshot_fd, | 
|  | 2461 | apk_fds, | 
|  | 2462 | dex_locations, | 
|  | 2463 | /* for_snapshot= */ true, | 
|  | 2464 | /* for_boot_image= */ false); | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2465 | pid_t pid = fork(); | 
|  | 2466 | if (pid == 0) { | 
|  | 2467 | /* child -- drop privileges before continuing */ | 
|  | 2468 | drop_capabilities(app_shared_gid); | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 2469 | args.Exec(); | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2470 | } | 
|  | 2471 |  | 
|  | 2472 | /* parent */ | 
|  | 2473 | int return_code = wait_child(pid); | 
|  | 2474 | if (!WIFEXITED(return_code)) { | 
| Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2475 | LOG(WARNING) << "profman failed for " << package_name << ":" << profile_name; | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2476 | return false; | 
|  | 2477 | } | 
|  | 2478 |  | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 2479 | // Verify that profman finished successfully. | 
|  | 2480 | int profman_code = WEXITSTATUS(return_code); | 
|  | 2481 | if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) { | 
|  | 2482 | LOG(WARNING) << "profman error for " << package_name << ":" << profile_name | 
|  | 2483 | << ":" << profman_code; | 
|  | 2484 | return false; | 
|  | 2485 | } | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2486 | return true; | 
|  | 2487 | } | 
|  | 2488 |  | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2489 | static bool create_boot_image_profile_snapshot(const std::string& package_name, | 
|  | 2490 | const std::string& profile_name, | 
|  | 2491 | const std::string& classpath) { | 
|  | 2492 | // The reference profile directory for the android package might not be prepared. Do it now. | 
|  | 2493 | const std::string ref_profile_dir = | 
|  | 2494 | create_primary_reference_profile_package_dir_path(package_name); | 
|  | 2495 | if (fs_prepare_dir(ref_profile_dir.c_str(), 0770, AID_SYSTEM, AID_SYSTEM) != 0) { | 
|  | 2496 | PLOG(ERROR) << "Failed to prepare " << ref_profile_dir; | 
|  | 2497 | return false; | 
|  | 2498 | } | 
|  | 2499 |  | 
| Mathieu Chartier | e0d64a1 | 2018-11-01 12:07:26 -0700 | [diff] [blame] | 2500 | // Return false for empty class path since it may otherwise return true below if profiles is | 
|  | 2501 | // empty. | 
|  | 2502 | if (classpath.empty()) { | 
|  | 2503 | PLOG(ERROR) << "Class path is empty"; | 
|  | 2504 | return false; | 
|  | 2505 | } | 
|  | 2506 |  | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2507 | // Open and create the snapshot profile. | 
|  | 2508 | unique_fd snapshot_fd = open_spnashot_profile(AID_SYSTEM, package_name, profile_name); | 
|  | 2509 |  | 
|  | 2510 | // Collect all non empty profiles. | 
|  | 2511 | // The collection will traverse all applications profiles and find the non empty files. | 
|  | 2512 | // This has the potential of inspecting a large number of files and directories (depending | 
|  | 2513 | // on the number of applications and users). So there is a slight increase in the chance | 
|  | 2514 | // to get get occasionally I/O errors (e.g. for opening the file). When that happens do not | 
|  | 2515 | // fail the snapshot and aggregate whatever profile we could open. | 
|  | 2516 | // | 
|  | 2517 | // The profile snapshot is a best effort based on available data it's ok if some data | 
|  | 2518 | // from some apps is missing. It will be counter productive for the snapshot to fail | 
|  | 2519 | // because we could not open or read some of the files. | 
|  | 2520 | std::vector<std::string> profiles; | 
|  | 2521 | if (!collect_profiles(&profiles)) { | 
|  | 2522 | LOG(WARNING) << "There were errors while collecting the profiles for the boot image."; | 
|  | 2523 | } | 
|  | 2524 |  | 
|  | 2525 | // If we have no profiles return early. | 
|  | 2526 | if (profiles.empty()) { | 
|  | 2527 | return true; | 
|  | 2528 | } | 
|  | 2529 |  | 
|  | 2530 | // Open the classpath elements. These will be used to filter out profile data that does | 
|  | 2531 | // not belong to the classpath during merge. | 
|  | 2532 | std::vector<unique_fd> apk_fds; | 
| Calin Juravle | 59f7ab8 | 2018-04-27 17:50:23 -0700 | [diff] [blame] | 2533 | std::vector<std::string> dex_locations; | 
|  | 2534 | if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) { | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2535 | return false; | 
|  | 2536 | } | 
|  | 2537 |  | 
|  | 2538 | // If we could not open any files from the classpath return an error. | 
|  | 2539 | if (apk_fds.empty()) { | 
|  | 2540 | LOG(ERROR) << "Could not open any of the classpath elements."; | 
|  | 2541 | return false; | 
|  | 2542 | } | 
|  | 2543 |  | 
|  | 2544 | // Aggregate the profiles in batches of kAggregationBatchSize. | 
|  | 2545 | // We do this to avoid opening a huge a amount of files. | 
|  | 2546 | static constexpr size_t kAggregationBatchSize = 10; | 
|  | 2547 |  | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2548 | for (size_t i = 0; i < profiles.size(); )  { | 
| Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 2549 | std::vector<unique_fd> profiles_fd; | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2550 | for (size_t k = 0; k < kAggregationBatchSize && i < profiles.size(); k++, i++) { | 
| Calin Juravle | e01f871 | 2021-06-03 18:16:54 -0700 | [diff] [blame] | 2551 | unique_fd fd = open_profile(AID_SYSTEM, profiles[i], O_RDONLY, /*mode=*/ 0); | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2552 | if (fd.get() >= 0) { | 
|  | 2553 | profiles_fd.push_back(std::move(fd)); | 
|  | 2554 | } | 
|  | 2555 | } | 
| Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 2556 |  | 
|  | 2557 | // We aggregate (read & write) into the same fd multiple times in a row. | 
|  | 2558 | // We need to reset the cursor every time to ensure we read the whole file every time. | 
|  | 2559 | if (TEMP_FAILURE_RETRY(lseek(snapshot_fd, 0, SEEK_SET)) == static_cast<off_t>(-1)) { | 
|  | 2560 | PLOG(ERROR) << "Cannot reset position for snapshot profile"; | 
|  | 2561 | return false; | 
|  | 2562 | } | 
|  | 2563 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 2564 | RunProfman args; | 
| Calin Juravle | b3a929d | 2018-12-11 14:40:00 -0800 | [diff] [blame] | 2565 | args.SetupMerge(profiles_fd, | 
|  | 2566 | snapshot_fd, | 
|  | 2567 | apk_fds, | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 2568 | dex_locations, | 
|  | 2569 | /*for_snapshot=*/true, | 
|  | 2570 | /*for_boot_image=*/true); | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2571 | pid_t pid = fork(); | 
|  | 2572 | if (pid == 0) { | 
|  | 2573 | /* child -- drop privileges before continuing */ | 
|  | 2574 | drop_capabilities(AID_SYSTEM); | 
|  | 2575 |  | 
| Calin Juravle | 59f7ab8 | 2018-04-27 17:50:23 -0700 | [diff] [blame] | 2576 | // The introduction of new access flags into boot jars causes them to | 
|  | 2577 | // fail dex file verification. | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 2578 | args.Exec(); | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2579 | } | 
|  | 2580 |  | 
|  | 2581 | /* parent */ | 
|  | 2582 | int return_code = wait_child(pid); | 
| Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 2583 |  | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2584 | if (!WIFEXITED(return_code)) { | 
|  | 2585 | PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name; | 
|  | 2586 | return false; | 
|  | 2587 | } | 
| Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 2588 |  | 
|  | 2589 | // Verify that profman finished successfully. | 
|  | 2590 | int profman_code = WEXITSTATUS(return_code); | 
| Calin Juravle | 78728f3 | 2019-11-08 17:55:46 -0800 | [diff] [blame] | 2591 | if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) { | 
|  | 2592 | LOG(WARNING) << "profman error for " << package_name << ":" << profile_name | 
|  | 2593 | << ":" << profman_code; | 
|  | 2594 | return false; | 
| Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 2595 | } | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2596 | } | 
| Calin Juravle | a64fb51 | 2019-11-06 16:11:14 -0800 | [diff] [blame] | 2597 |  | 
| Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2598 | return true; | 
|  | 2599 | } | 
|  | 2600 |  | 
|  | 2601 | bool create_profile_snapshot(int32_t app_id, const std::string& package_name, | 
|  | 2602 | const std::string& profile_name, const std::string& classpath) { | 
|  | 2603 | if (app_id == -1) { | 
|  | 2604 | return create_boot_image_profile_snapshot(package_name, profile_name, classpath); | 
|  | 2605 | } else { | 
|  | 2606 | return create_app_profile_snapshot(app_id, package_name, profile_name, classpath); | 
|  | 2607 | } | 
|  | 2608 | } | 
|  | 2609 |  | 
| Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2610 | bool prepare_app_profile(const std::string& package_name, | 
|  | 2611 | userid_t user_id, | 
|  | 2612 | appid_t app_id, | 
|  | 2613 | const std::string& profile_name, | 
| Calin Juravle | f63d479 | 2018-01-30 17:43:34 +0000 | [diff] [blame] | 2614 | const std::string& code_path, | 
| Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2615 | const std::optional<std::string>& dex_metadata) { | 
| Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2616 | // Prepare the current profile. | 
|  | 2617 | std::string cur_profile  = create_current_profile_path(user_id, package_name, profile_name, | 
|  | 2618 | /*is_secondary_dex*/ false); | 
|  | 2619 | uid_t uid = multiuser_get_uid(user_id, app_id); | 
|  | 2620 | if (fs_prepare_file_strict(cur_profile.c_str(), 0600, uid, uid) != 0) { | 
|  | 2621 | PLOG(ERROR) << "Failed to prepare " << cur_profile; | 
|  | 2622 | return false; | 
|  | 2623 | } | 
|  | 2624 |  | 
|  | 2625 | // Check if we need to install the profile from the dex metadata. | 
| Jooyung Han | 9fcc4ef | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2626 | if (!dex_metadata) { | 
| Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2627 | return true; | 
|  | 2628 | } | 
|  | 2629 |  | 
|  | 2630 | // We have a dex metdata. Merge the profile into the reference profile. | 
|  | 2631 | unique_fd ref_profile_fd = open_reference_profile(uid, package_name, profile_name, | 
|  | 2632 | /*read_write*/ true, /*is_secondary_dex*/ false); | 
|  | 2633 | unique_fd dex_metadata_fd(TEMP_FAILURE_RETRY( | 
|  | 2634 | open(dex_metadata->c_str(), O_RDONLY | O_NOFOLLOW))); | 
| Calin Juravle | f63d479 | 2018-01-30 17:43:34 +0000 | [diff] [blame] | 2635 | unique_fd apk_fd(TEMP_FAILURE_RETRY(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW))); | 
|  | 2636 | if (apk_fd < 0) { | 
|  | 2637 | PLOG(ERROR) << "Could not open code path " << code_path; | 
|  | 2638 | return false; | 
|  | 2639 | } | 
| Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2640 |  | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 2641 | RunProfman args; | 
|  | 2642 | args.SetupCopyAndUpdate(std::move(dex_metadata_fd), | 
|  | 2643 | std::move(ref_profile_fd), | 
|  | 2644 | std::move(apk_fd), | 
|  | 2645 | code_path); | 
| Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2646 | pid_t pid = fork(); | 
|  | 2647 | if (pid == 0) { | 
|  | 2648 | /* child -- drop privileges before continuing */ | 
|  | 2649 | gid_t app_shared_gid = multiuser_get_shared_gid(user_id, app_id); | 
|  | 2650 | drop_capabilities(app_shared_gid); | 
|  | 2651 |  | 
| Calin Juravle | f63d479 | 2018-01-30 17:43:34 +0000 | [diff] [blame] | 2652 | // The copy and update takes ownership over the fds. | 
| Mathieu Chartier | cc66c44 | 2018-11-09 15:57:21 -0800 | [diff] [blame] | 2653 | args.Exec(); | 
| Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2654 | } | 
|  | 2655 |  | 
|  | 2656 | /* parent */ | 
|  | 2657 | int return_code = wait_child(pid); | 
|  | 2658 | if (!WIFEXITED(return_code)) { | 
|  | 2659 | PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name; | 
|  | 2660 | return false; | 
|  | 2661 | } | 
|  | 2662 | return true; | 
|  | 2663 | } | 
|  | 2664 |  | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2665 | }  // namespace installd | 
|  | 2666 | }  // namespace android |