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