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