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