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