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