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