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