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