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