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 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 18 | #include <fcntl.h> |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 21 | #include <sys/capability.h> |
| 22 | #include <sys/file.h> |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 24 | #include <sys/time.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/resource.h> |
| 27 | #include <sys/wait.h> |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <android-base/logging.h> |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 31 | #include <android-base/properties.h> |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 33 | #include <android-base/strings.h> |
| 34 | #include <android-base/unique_fd.h> |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 35 | #include <cutils/fs.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
| 37 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 38 | #include <log/log.h> // TODO: Move everything to base/logging. |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> |
Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 40 | #include <selinux/android.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 41 | #include <system/thread_defs.h> |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 42 | |
| 43 | #include "dexopt.h" |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 44 | #include "installd_deps.h" |
| 45 | #include "otapreopt_utils.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 46 | #include "utils.h" |
| 47 | |
| 48 | using android::base::StringPrintf; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 49 | using android::base::EndsWith; |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 50 | using android::base::unique_fd; |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 51 | |
| 52 | namespace android { |
| 53 | namespace installd { |
| 54 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 55 | // Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below. |
| 56 | struct FreeDelete { |
| 57 | // NOTE: Deleting a const object is valid but free() takes a non-const pointer. |
| 58 | void operator()(const void* ptr) const { |
| 59 | free(const_cast<void*>(ptr)); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | // Alias for std::unique_ptr<> that uses the C function free() to delete objects. |
| 64 | template <typename T> |
| 65 | using UniqueCPtr = std::unique_ptr<T, FreeDelete>; |
| 66 | |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 67 | static unique_fd invalid_unique_fd() { |
| 68 | return unique_fd(-1); |
| 69 | } |
| 70 | |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 71 | static bool is_debug_runtime() { |
| 72 | return android::base::GetProperty("persist.sys.dalvik.vm.lib.2", "") == "libartd.so"; |
| 73 | } |
| 74 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 75 | static bool clear_profile(const std::string& profile) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 76 | 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] | 77 | if (ufd.get() < 0) { |
| 78 | if (errno != ENOENT) { |
| 79 | PLOG(WARNING) << "Could not open profile " << profile; |
| 80 | return false; |
| 81 | } else { |
| 82 | // Nothing to clear. That's ok. |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) { |
| 88 | if (errno != EWOULDBLOCK) { |
| 89 | PLOG(WARNING) << "Error locking profile " << profile; |
| 90 | } |
| 91 | // This implies that the app owning this profile is running |
| 92 | // (and has acquired the lock). |
| 93 | // |
| 94 | // If we can't acquire the lock bail out since clearing is useless anyway |
| 95 | // (the app will write again to the profile). |
| 96 | // |
| 97 | // Note: |
| 98 | // This does not impact the this is not an issue for the profiling correctness. |
| 99 | // In case this is needed because of an app upgrade, profiles will still be |
| 100 | // eventually cleared by the app itself due to checksum mismatch. |
| 101 | // If this is needed because profman advised, then keeping the data around |
| 102 | // until the next run is again not an issue. |
| 103 | // |
| 104 | // If the app attempts to acquire a lock while we've held one here, |
| 105 | // it will simply skip the current write cycle. |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | bool truncated = ftruncate(ufd.get(), 0) == 0; |
| 110 | if (!truncated) { |
| 111 | PLOG(WARNING) << "Could not truncate " << profile; |
| 112 | } |
| 113 | if (flock(ufd.get(), LOCK_UN) != 0) { |
| 114 | PLOG(WARNING) << "Error unlocking profile " << profile; |
| 115 | } |
| 116 | return truncated; |
| 117 | } |
| 118 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 119 | // Clear the reference profile for the given location. |
| 120 | // The location is the package name for primary apks or the dex path for secondary dex files. |
| 121 | static bool clear_reference_profile(const std::string& location, bool is_secondary_dex) { |
| 122 | return clear_profile(create_reference_profile_path(location, is_secondary_dex)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 125 | // Clear the reference profile for the given location. |
| 126 | // The location is the package name for primary apks or the dex path for secondary dex files. |
| 127 | static bool clear_current_profile(const std::string& pkgname, userid_t user, |
| 128 | bool is_secondary_dex) { |
| 129 | return clear_profile(create_current_profile_path(user, pkgname, is_secondary_dex)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 132 | // Clear the reference profile for the primary apk of the given package. |
| 133 | bool clear_primary_reference_profile(const std::string& pkgname) { |
| 134 | return clear_reference_profile(pkgname, /*is_secondary_dex*/false); |
| 135 | } |
| 136 | |
| 137 | // Clear all current profile for the primary apk of the given package. |
| 138 | bool clear_primary_current_profiles(const std::string& pkgname) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 139 | bool success = true; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 140 | // 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] | 141 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); |
| 142 | for (auto user : users) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 143 | success &= clear_current_profile(pkgname, user, /*is_secondary_dex*/false); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 144 | } |
| 145 | return success; |
| 146 | } |
| 147 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 148 | // Clear the current profile for the primary apk of the given package and user. |
| 149 | bool clear_primary_current_profile(const std::string& pkgname, userid_t user) { |
| 150 | return clear_current_profile(pkgname, user, /*is_secondary_dex*/false); |
| 151 | } |
| 152 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 153 | static int split_count(const char *str) |
| 154 | { |
| 155 | char *ctx; |
| 156 | int count = 0; |
| 157 | char buf[kPropertyValueMax]; |
| 158 | |
| 159 | strncpy(buf, str, sizeof(buf)); |
| 160 | char *pBuf = buf; |
| 161 | |
| 162 | while(strtok_r(pBuf, " ", &ctx) != NULL) { |
| 163 | count++; |
| 164 | pBuf = NULL; |
| 165 | } |
| 166 | |
| 167 | return count; |
| 168 | } |
| 169 | |
| 170 | static int split(char *buf, const char **argv) |
| 171 | { |
| 172 | char *ctx; |
| 173 | int count = 0; |
| 174 | char *tok; |
| 175 | char *pBuf = buf; |
| 176 | |
| 177 | while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) { |
| 178 | argv[count++] = tok; |
| 179 | pBuf = NULL; |
| 180 | } |
| 181 | |
| 182 | return count; |
| 183 | } |
| 184 | |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 185 | static const char* get_location_from_path(const char* path) { |
| 186 | static constexpr char kLocationSeparator = '/'; |
| 187 | const char *location = strrchr(path, kLocationSeparator); |
| 188 | if (location == NULL) { |
| 189 | return path; |
| 190 | } else { |
| 191 | // Skip the separator character. |
| 192 | return location + 1; |
| 193 | } |
| 194 | } |
| 195 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 196 | static void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vdex_fd, int image_fd, |
| 197 | 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] | 198 | const char* instruction_set, const char* compiler_filter, |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 199 | bool debuggable, bool post_bootcomplete, int profile_fd, const char* class_loader_context) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 200 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; |
| 201 | |
| 202 | if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { |
| 203 | ALOGE("Instruction set %s longer than max length of %d", |
| 204 | instruction_set, MAX_INSTRUCTION_SET_LEN); |
| 205 | return; |
| 206 | } |
| 207 | |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 208 | // Get the relative path to the input file. |
| 209 | const char* relative_input_file_name = get_location_from_path(input_file_name); |
| 210 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 211 | char dex2oat_Xms_flag[kPropertyValueMax]; |
| 212 | bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0; |
| 213 | |
| 214 | char dex2oat_Xmx_flag[kPropertyValueMax]; |
| 215 | bool have_dex2oat_Xmx_flag = get_property("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0; |
| 216 | |
| 217 | char dex2oat_threads_buf[kPropertyValueMax]; |
| 218 | bool have_dex2oat_threads_flag = get_property(post_bootcomplete |
| 219 | ? "dalvik.vm.dex2oat-threads" |
| 220 | : "dalvik.vm.boot-dex2oat-threads", |
| 221 | dex2oat_threads_buf, |
| 222 | NULL) > 0; |
| 223 | char dex2oat_threads_arg[kPropertyValueMax + 2]; |
| 224 | if (have_dex2oat_threads_flag) { |
| 225 | sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf); |
| 226 | } |
| 227 | |
| 228 | char dex2oat_isa_features_key[kPropertyKeyMax]; |
| 229 | sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set); |
| 230 | char dex2oat_isa_features[kPropertyValueMax]; |
| 231 | bool have_dex2oat_isa_features = get_property(dex2oat_isa_features_key, |
| 232 | dex2oat_isa_features, NULL) > 0; |
| 233 | |
| 234 | char dex2oat_isa_variant_key[kPropertyKeyMax]; |
| 235 | sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set); |
| 236 | char dex2oat_isa_variant[kPropertyValueMax]; |
| 237 | bool have_dex2oat_isa_variant = get_property(dex2oat_isa_variant_key, |
| 238 | dex2oat_isa_variant, NULL) > 0; |
| 239 | |
| 240 | const char *dex2oat_norelocation = "-Xnorelocate"; |
| 241 | bool have_dex2oat_relocation_skip_flag = false; |
| 242 | |
| 243 | char dex2oat_flags[kPropertyValueMax]; |
| 244 | int dex2oat_flags_count = get_property("dalvik.vm.dex2oat-flags", |
| 245 | dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags); |
| 246 | ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags); |
| 247 | |
Nicolas Geoffray | be6ecd6 | 2017-05-03 13:21:37 +0100 | [diff] [blame] | 248 | // 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] | 249 | char vold_decrypt[kPropertyValueMax]; |
| 250 | bool have_vold_decrypt = get_property("vold.decrypt", vold_decrypt, "") > 0; |
| 251 | bool skip_compilation = (have_vold_decrypt && |
| 252 | (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 || |
| 253 | (strcmp(vold_decrypt, "1") == 0))); |
| 254 | |
| 255 | bool generate_debug_info = property_get_bool("debug.generate-debug-info", false); |
| 256 | |
| 257 | char app_image_format[kPropertyValueMax]; |
| 258 | char image_format_arg[strlen("--image-format=") + kPropertyValueMax]; |
| 259 | bool have_app_image_format = |
| 260 | image_fd >= 0 && get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0; |
| 261 | if (have_app_image_format) { |
| 262 | sprintf(image_format_arg, "--image-format=%s", app_image_format); |
| 263 | } |
| 264 | |
| 265 | char dex2oat_large_app_threshold[kPropertyValueMax]; |
| 266 | bool have_dex2oat_large_app_threshold = |
| 267 | get_property("dalvik.vm.dex2oat-very-large", dex2oat_large_app_threshold, NULL) > 0; |
| 268 | char dex2oat_large_app_threshold_arg[strlen("--very-large-app-threshold=") + kPropertyValueMax]; |
| 269 | if (have_dex2oat_large_app_threshold) { |
| 270 | sprintf(dex2oat_large_app_threshold_arg, |
| 271 | "--very-large-app-threshold=%s", |
| 272 | dex2oat_large_app_threshold); |
| 273 | } |
| 274 | |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 275 | // If the runtime was requested to use libartd.so, we'll run dex2oatd, otherwise dex2oat. |
| 276 | const char* dex2oat_bin = is_debug_runtime() ? "/system/bin/dex2oatd" : "/system/bin/dex2oat"; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 277 | |
| 278 | static const char* RUNTIME_ARG = "--runtime-arg"; |
| 279 | |
| 280 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
| 281 | |
George Burgess IV | 36cebe77 | 2017-01-25 11:52:01 -0800 | [diff] [blame] | 282 | // clang FORTIFY doesn't let us use strlen in constant array bounds, so we |
| 283 | // use arraysize instead. |
| 284 | char zip_fd_arg[arraysize("--zip-fd=") + MAX_INT_LEN]; |
| 285 | char zip_location_arg[arraysize("--zip-location=") + PKG_PATH_MAX]; |
| 286 | char input_vdex_fd_arg[arraysize("--input-vdex-fd=") + MAX_INT_LEN]; |
| 287 | char output_vdex_fd_arg[arraysize("--output-vdex-fd=") + MAX_INT_LEN]; |
| 288 | char oat_fd_arg[arraysize("--oat-fd=") + MAX_INT_LEN]; |
| 289 | char oat_location_arg[arraysize("--oat-location=") + PKG_PATH_MAX]; |
| 290 | char instruction_set_arg[arraysize("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; |
| 291 | char instruction_set_variant_arg[arraysize("--instruction-set-variant=") + kPropertyValueMax]; |
| 292 | char instruction_set_features_arg[arraysize("--instruction-set-features=") + kPropertyValueMax]; |
| 293 | char dex2oat_Xms_arg[arraysize("-Xms") + kPropertyValueMax]; |
| 294 | char dex2oat_Xmx_arg[arraysize("-Xmx") + kPropertyValueMax]; |
| 295 | char dex2oat_compiler_filter_arg[arraysize("--compiler-filter=") + kPropertyValueMax]; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 296 | bool have_dex2oat_swap_fd = false; |
George Burgess IV | 36cebe77 | 2017-01-25 11:52:01 -0800 | [diff] [blame] | 297 | char dex2oat_swap_fd[arraysize("--swap-fd=") + MAX_INT_LEN]; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 298 | bool have_dex2oat_image_fd = false; |
George Burgess IV | 36cebe77 | 2017-01-25 11:52:01 -0800 | [diff] [blame] | 299 | char dex2oat_image_fd[arraysize("--app-image-fd=") + MAX_INT_LEN]; |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 300 | size_t class_loader_context_size = arraysize("--class-loader-context=") + PKG_PATH_MAX; |
| 301 | char class_loader_context_arg[class_loader_context_size]; |
| 302 | if (class_loader_context != nullptr) { |
| 303 | snprintf(class_loader_context_arg, class_loader_context_size, "--class-loader-context=%s", |
| 304 | class_loader_context); |
| 305 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 306 | |
| 307 | sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd); |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 308 | sprintf(zip_location_arg, "--zip-location=%s", relative_input_file_name); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 309 | sprintf(input_vdex_fd_arg, "--input-vdex-fd=%d", input_vdex_fd); |
| 310 | sprintf(output_vdex_fd_arg, "--output-vdex-fd=%d", output_vdex_fd); |
| 311 | sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd); |
| 312 | sprintf(oat_location_arg, "--oat-location=%s", output_file_name); |
| 313 | sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); |
| 314 | sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant); |
| 315 | sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features); |
| 316 | if (swap_fd >= 0) { |
| 317 | have_dex2oat_swap_fd = true; |
| 318 | sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd); |
| 319 | } |
| 320 | if (image_fd >= 0) { |
| 321 | have_dex2oat_image_fd = true; |
| 322 | sprintf(dex2oat_image_fd, "--app-image-fd=%d", image_fd); |
| 323 | } |
| 324 | |
| 325 | if (have_dex2oat_Xms_flag) { |
| 326 | sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag); |
| 327 | } |
| 328 | if (have_dex2oat_Xmx_flag) { |
| 329 | sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag); |
| 330 | } |
| 331 | |
| 332 | // Compute compiler filter. |
| 333 | |
Nicolas Geoffray | be6ecd6 | 2017-05-03 13:21:37 +0100 | [diff] [blame] | 334 | bool have_dex2oat_compiler_filter_flag = false; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 335 | if (skip_compilation) { |
Nicolas Geoffray | 4b64ed9 | 2017-04-25 12:28:28 +0100 | [diff] [blame] | 336 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=extract"); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 337 | have_dex2oat_compiler_filter_flag = true; |
| 338 | have_dex2oat_relocation_skip_flag = true; |
Nicolas Geoffray | be6ecd6 | 2017-05-03 13:21:37 +0100 | [diff] [blame] | 339 | } else if (compiler_filter != nullptr) { |
| 340 | if (strlen(compiler_filter) + strlen("--compiler-filter=") < |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 341 | arraysize(dex2oat_compiler_filter_arg)) { |
Nicolas Geoffray | be6ecd6 | 2017-05-03 13:21:37 +0100 | [diff] [blame] | 342 | sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", compiler_filter); |
| 343 | have_dex2oat_compiler_filter_flag = true; |
| 344 | } else { |
| 345 | ALOGW("Compiler filter name '%s' is too large (max characters is %zu)", |
| 346 | compiler_filter, |
| 347 | kPropertyValueMax); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (!have_dex2oat_compiler_filter_flag) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 352 | char dex2oat_compiler_filter_flag[kPropertyValueMax]; |
| 353 | have_dex2oat_compiler_filter_flag = get_property("dalvik.vm.dex2oat-filter", |
| 354 | dex2oat_compiler_filter_flag, NULL) > 0; |
| 355 | if (have_dex2oat_compiler_filter_flag) { |
| 356 | sprintf(dex2oat_compiler_filter_arg, |
| 357 | "--compiler-filter=%s", |
| 358 | dex2oat_compiler_filter_flag); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Check whether all apps should be compiled debuggable. |
| 363 | if (!debuggable) { |
| 364 | char prop_buf[kPropertyValueMax]; |
| 365 | debuggable = |
| 366 | (get_property("dalvik.vm.always_debuggable", prop_buf, "0") > 0) && |
| 367 | (prop_buf[0] == '1'); |
| 368 | } |
| 369 | char profile_arg[strlen("--profile-file-fd=") + MAX_INT_LEN]; |
| 370 | if (profile_fd != -1) { |
| 371 | sprintf(profile_arg, "--profile-file-fd=%d", profile_fd); |
| 372 | } |
| 373 | |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 374 | // Get the directory of the apk to pass as a base classpath directory. |
| 375 | char base_dir[arraysize("--classpath-dir=") + PKG_PATH_MAX]; |
| 376 | std::string apk_dir(input_file_name); |
| 377 | unsigned long dir_index = apk_dir.rfind('/'); |
| 378 | bool has_base_dir = dir_index != std::string::npos; |
| 379 | if (has_base_dir) { |
| 380 | apk_dir = apk_dir.substr(0, dir_index); |
| 381 | sprintf(base_dir, "--classpath-dir=%s", apk_dir.c_str()); |
| 382 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 383 | |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 384 | |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 385 | 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] | 386 | |
| 387 | const char* argv[9 // program name, mandatory arguments and the final NULL |
| 388 | + (have_dex2oat_isa_variant ? 1 : 0) |
| 389 | + (have_dex2oat_isa_features ? 1 : 0) |
| 390 | + (have_dex2oat_Xms_flag ? 2 : 0) |
| 391 | + (have_dex2oat_Xmx_flag ? 2 : 0) |
| 392 | + (have_dex2oat_compiler_filter_flag ? 1 : 0) |
| 393 | + (have_dex2oat_threads_flag ? 1 : 0) |
| 394 | + (have_dex2oat_swap_fd ? 1 : 0) |
| 395 | + (have_dex2oat_image_fd ? 1 : 0) |
| 396 | + (have_dex2oat_relocation_skip_flag ? 2 : 0) |
| 397 | + (generate_debug_info ? 1 : 0) |
| 398 | + (debuggable ? 1 : 0) |
| 399 | + (have_app_image_format ? 1 : 0) |
| 400 | + dex2oat_flags_count |
| 401 | + (profile_fd == -1 ? 0 : 1) |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 402 | + (class_loader_context != nullptr ? 1 : 0) |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 403 | + (has_base_dir ? 1 : 0) |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 404 | + (have_dex2oat_large_app_threshold ? 1 : 0)]; |
| 405 | int i = 0; |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 406 | argv[i++] = dex2oat_bin; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 407 | argv[i++] = zip_fd_arg; |
| 408 | argv[i++] = zip_location_arg; |
| 409 | argv[i++] = input_vdex_fd_arg; |
| 410 | argv[i++] = output_vdex_fd_arg; |
| 411 | argv[i++] = oat_fd_arg; |
| 412 | argv[i++] = oat_location_arg; |
| 413 | argv[i++] = instruction_set_arg; |
| 414 | if (have_dex2oat_isa_variant) { |
| 415 | argv[i++] = instruction_set_variant_arg; |
| 416 | } |
| 417 | if (have_dex2oat_isa_features) { |
| 418 | argv[i++] = instruction_set_features_arg; |
| 419 | } |
| 420 | if (have_dex2oat_Xms_flag) { |
| 421 | argv[i++] = RUNTIME_ARG; |
| 422 | argv[i++] = dex2oat_Xms_arg; |
| 423 | } |
| 424 | if (have_dex2oat_Xmx_flag) { |
| 425 | argv[i++] = RUNTIME_ARG; |
| 426 | argv[i++] = dex2oat_Xmx_arg; |
| 427 | } |
| 428 | if (have_dex2oat_compiler_filter_flag) { |
| 429 | argv[i++] = dex2oat_compiler_filter_arg; |
| 430 | } |
| 431 | if (have_dex2oat_threads_flag) { |
| 432 | argv[i++] = dex2oat_threads_arg; |
| 433 | } |
| 434 | if (have_dex2oat_swap_fd) { |
| 435 | argv[i++] = dex2oat_swap_fd; |
| 436 | } |
| 437 | if (have_dex2oat_image_fd) { |
| 438 | argv[i++] = dex2oat_image_fd; |
| 439 | } |
| 440 | if (generate_debug_info) { |
| 441 | argv[i++] = "--generate-debug-info"; |
| 442 | } |
| 443 | if (debuggable) { |
| 444 | argv[i++] = "--debuggable"; |
| 445 | } |
| 446 | if (have_app_image_format) { |
| 447 | argv[i++] = image_format_arg; |
| 448 | } |
| 449 | if (have_dex2oat_large_app_threshold) { |
| 450 | argv[i++] = dex2oat_large_app_threshold_arg; |
| 451 | } |
| 452 | if (dex2oat_flags_count) { |
| 453 | i += split(dex2oat_flags, argv + i); |
| 454 | } |
| 455 | if (have_dex2oat_relocation_skip_flag) { |
| 456 | argv[i++] = RUNTIME_ARG; |
| 457 | argv[i++] = dex2oat_norelocation; |
| 458 | } |
| 459 | if (profile_fd != -1) { |
| 460 | argv[i++] = profile_arg; |
| 461 | } |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 462 | if (has_base_dir) { |
| 463 | argv[i++] = base_dir; |
| 464 | } |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 465 | if (class_loader_context != nullptr) { |
| 466 | argv[i++] = class_loader_context_arg; |
| 467 | } |
| 468 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 469 | // Do not add after dex2oat_flags, they should override others for debugging. |
| 470 | argv[i] = NULL; |
| 471 | |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 472 | execv(dex2oat_bin, (char * const *)argv); |
| 473 | ALOGE("execv(%s) failed: %s\n", dex2oat_bin, strerror(errno)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Whether dexopt should use a swap file when compiling an APK. |
| 478 | * |
| 479 | * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision |
| 480 | * itself, anyways). |
| 481 | * |
| 482 | * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true". |
| 483 | * |
| 484 | * Otherwise, return true if this is a low-mem device. |
| 485 | * |
| 486 | * Otherwise, return default value. |
| 487 | */ |
| 488 | static bool kAlwaysProvideSwapFile = false; |
| 489 | static bool kDefaultProvideSwapFile = true; |
| 490 | |
| 491 | static bool ShouldUseSwapFileForDexopt() { |
| 492 | if (kAlwaysProvideSwapFile) { |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | // Check the "override" property. If it exists, return value == "true". |
| 497 | char dex2oat_prop_buf[kPropertyValueMax]; |
| 498 | if (get_property("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) { |
| 499 | if (strcmp(dex2oat_prop_buf, "true") == 0) { |
| 500 | return true; |
| 501 | } else { |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Shortcut for default value. This is an implementation optimization for the process sketched |
| 507 | // above. If the default value is true, we can avoid to check whether this is a low-mem device, |
| 508 | // as low-mem is never returning false. The compiler will optimize this away if it can. |
| 509 | if (kDefaultProvideSwapFile) { |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | bool is_low_mem = property_get_bool("ro.config.low_ram", false); |
| 514 | if (is_low_mem) { |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | // Default value must be false here. |
| 519 | return kDefaultProvideSwapFile; |
| 520 | } |
| 521 | |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 522 | static void SetDex2OatScheduling(bool set_to_bg) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 523 | if (set_to_bg) { |
| 524 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 525 | ALOGE("set_sched_policy failed: %s\n", strerror(errno)); |
| 526 | exit(70); |
| 527 | } |
| 528 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 529 | ALOGE("setpriority failed: %s\n", strerror(errno)); |
| 530 | exit(71); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 535 | static bool create_profile(int uid, const std::string& profile) { |
| 536 | unique_fd fd(TEMP_FAILURE_RETRY(open(profile.c_str(), O_CREAT | O_NOFOLLOW, 0600))); |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 537 | if (fd.get() < 0) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 538 | if (errno == EEXIST) { |
| 539 | return true; |
| 540 | } else { |
| 541 | PLOG(ERROR) << "Failed to create profile " << profile; |
| 542 | return false; |
| 543 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 544 | } |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 545 | // Profiles should belong to the app; make sure of that by giving ownership to |
| 546 | // the app uid. If we cannot do that, there's no point in returning the fd |
| 547 | // since dex2oat/profman will fail with SElinux denials. |
| 548 | if (fchown(fd.get(), uid, uid) < 0) { |
| 549 | PLOG(ERROR) << "Could not chwon profile " << profile; |
| 550 | return false; |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | static unique_fd open_profile(int uid, const std::string& profile, bool read_write) { |
| 556 | // Check if we need to open the profile for a read-write operation. If so, we |
| 557 | // might need to create the profile since the file might not be there. Reference |
| 558 | // profiles are created on the fly so they might not exist beforehand. |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 559 | if (read_write) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 560 | if (!create_profile(uid, profile)) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 561 | return invalid_unique_fd(); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 564 | int flags = read_write ? O_RDWR : O_RDONLY; |
| 565 | // Do not follow symlinks when opening a profile: |
| 566 | // - primary profiles should not contain symlinks in their paths |
| 567 | // - secondary dex paths should have been already resolved and validated |
| 568 | flags |= O_NOFOLLOW; |
| 569 | |
| 570 | unique_fd fd(TEMP_FAILURE_RETRY(open(profile.c_str(), flags))); |
| 571 | if (fd.get() < 0) { |
| 572 | if (errno != ENOENT) { |
| 573 | // Profiles might be missing for various reasons. For example, in a |
| 574 | // multi-user environment, the profile directory for one user can be created |
| 575 | // after we start a merge. In this case the current profile for that user |
| 576 | // will not be found. |
| 577 | // Also, the secondary dex profiles might be deleted by the app at any time, |
| 578 | // so we can't we need to prepare if they are missing. |
| 579 | PLOG(ERROR) << "Failed to open profile " << profile; |
| 580 | } |
| 581 | return invalid_unique_fd(); |
| 582 | } |
| 583 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 584 | return fd; |
| 585 | } |
| 586 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 587 | static unique_fd open_current_profile(uid_t uid, userid_t user, const std::string& location, |
| 588 | bool is_secondary_dex) { |
| 589 | std::string profile = create_current_profile_path(user, location, is_secondary_dex); |
| 590 | return open_profile(uid, profile, /*read_write*/false); |
| 591 | } |
| 592 | |
| 593 | static unique_fd open_reference_profile(uid_t uid, const std::string& location, bool read_write, |
| 594 | bool is_secondary_dex) { |
| 595 | std::string profile = create_reference_profile_path(location, is_secondary_dex); |
| 596 | return open_profile(uid, profile, read_write); |
| 597 | } |
| 598 | |
| 599 | static void open_profile_files(uid_t uid, const std::string& location, bool is_secondary_dex, |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 600 | /*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] | 601 | // Open the reference profile in read-write mode as profman might need to save the merge. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 602 | *reference_profile_fd = open_reference_profile(uid, location, /*read_write*/ true, |
| 603 | is_secondary_dex); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 604 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 605 | // For secondary dex files, we don't really need the user but we use it for sanity checks. |
| 606 | // Note: the user owning the dex file should be the current user. |
| 607 | std::vector<userid_t> users; |
| 608 | if (is_secondary_dex){ |
| 609 | users.push_back(multiuser_get_user_id(uid)); |
| 610 | } else { |
| 611 | users = get_known_users(/*volume_uuid*/ nullptr); |
| 612 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 613 | for (auto user : users) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 614 | unique_fd profile_fd = open_current_profile(uid, user, location, is_secondary_dex); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 615 | // Add to the lists only if both fds are valid. |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 616 | if (profile_fd.get() >= 0) { |
| 617 | profiles_fd->push_back(std::move(profile_fd)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static void drop_capabilities(uid_t uid) { |
| 623 | if (setgid(uid) != 0) { |
| 624 | ALOGE("setgid(%d) failed in installd during dexopt\n", uid); |
| 625 | exit(64); |
| 626 | } |
| 627 | if (setuid(uid) != 0) { |
| 628 | ALOGE("setuid(%d) failed in installd during dexopt\n", uid); |
| 629 | exit(65); |
| 630 | } |
| 631 | // drop capabilities |
| 632 | struct __user_cap_header_struct capheader; |
| 633 | struct __user_cap_data_struct capdata[2]; |
| 634 | memset(&capheader, 0, sizeof(capheader)); |
| 635 | memset(&capdata, 0, sizeof(capdata)); |
| 636 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 637 | if (capset(&capheader, &capdata[0]) < 0) { |
| 638 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 639 | exit(66); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | static constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 0; |
| 644 | static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION = 1; |
| 645 | static constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 2; |
| 646 | static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 3; |
| 647 | static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 4; |
| 648 | |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 649 | static void run_profman_merge(const std::vector<unique_fd>& profiles_fd, |
| 650 | const unique_fd& reference_profile_fd) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 651 | static const size_t MAX_INT_LEN = 32; |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 652 | 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] | 653 | |
| 654 | std::vector<std::string> profile_args(profiles_fd.size()); |
| 655 | char profile_buf[strlen("--profile-file-fd=") + MAX_INT_LEN]; |
| 656 | for (size_t k = 0; k < profiles_fd.size(); k++) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 657 | sprintf(profile_buf, "--profile-file-fd=%d", profiles_fd[k].get()); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 658 | profile_args[k].assign(profile_buf); |
| 659 | } |
| 660 | char reference_profile_arg[strlen("--reference-profile-file-fd=") + MAX_INT_LEN]; |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 661 | sprintf(reference_profile_arg, "--reference-profile-file-fd=%d", reference_profile_fd.get()); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 662 | |
| 663 | // program name, reference profile fd, the final NULL and the profile fds |
| 664 | const char* argv[3 + profiles_fd.size()]; |
| 665 | int i = 0; |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 666 | argv[i++] = profman_bin; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 667 | argv[i++] = reference_profile_arg; |
| 668 | for (size_t k = 0; k < profile_args.size(); k++) { |
| 669 | argv[i++] = profile_args[k].c_str(); |
| 670 | } |
| 671 | // Do not add after dex2oat_flags, they should override others for debugging. |
| 672 | argv[i] = NULL; |
| 673 | |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 674 | execv(profman_bin, (char * const *)argv); |
| 675 | ALOGE("execv(%s) failed: %s\n", profman_bin, strerror(errno)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 676 | exit(68); /* only get here on exec failure */ |
| 677 | } |
| 678 | |
| 679 | // 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] | 680 | // The location is the package name for primary apks or the dex path for secondary dex files. |
| 681 | // Returns true if there is enough information in the current profiles that makes it |
| 682 | // worth to recompile the given location. |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 683 | // If the return value is true all the current profiles would have been merged into |
| 684 | // the reference profiles accessible with open_reference_profile(). |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 685 | static bool analyze_profiles(uid_t uid, const std::string& location, bool is_secondary_dex) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 686 | std::vector<unique_fd> profiles_fd; |
| 687 | unique_fd reference_profile_fd; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 688 | open_profile_files(uid, location, is_secondary_dex, &profiles_fd, &reference_profile_fd); |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 689 | if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 690 | // Skip profile guided compilation because no profiles were found. |
| 691 | // Or if the reference profile info couldn't be opened. |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 692 | return false; |
| 693 | } |
| 694 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 695 | pid_t pid = fork(); |
| 696 | if (pid == 0) { |
| 697 | /* child -- drop privileges before continuing */ |
| 698 | drop_capabilities(uid); |
| 699 | run_profman_merge(profiles_fd, reference_profile_fd); |
| 700 | exit(68); /* only get here on exec failure */ |
| 701 | } |
| 702 | /* parent */ |
| 703 | int return_code = wait_child(pid); |
| 704 | bool need_to_compile = false; |
| 705 | bool should_clear_current_profiles = false; |
| 706 | bool should_clear_reference_profile = false; |
| 707 | if (!WIFEXITED(return_code)) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 708 | LOG(WARNING) << "profman failed for location " << location << ": " << return_code; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 709 | } else { |
| 710 | return_code = WEXITSTATUS(return_code); |
| 711 | switch (return_code) { |
| 712 | case PROFMAN_BIN_RETURN_CODE_COMPILE: |
| 713 | need_to_compile = true; |
| 714 | should_clear_current_profiles = true; |
| 715 | should_clear_reference_profile = false; |
| 716 | break; |
| 717 | case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION: |
| 718 | need_to_compile = false; |
| 719 | should_clear_current_profiles = false; |
| 720 | should_clear_reference_profile = false; |
| 721 | break; |
| 722 | case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES: |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 723 | LOG(WARNING) << "Bad profiles for location " << location; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 724 | need_to_compile = false; |
| 725 | should_clear_current_profiles = true; |
| 726 | should_clear_reference_profile = true; |
| 727 | break; |
| 728 | case PROFMAN_BIN_RETURN_CODE_ERROR_IO: // fall-through |
| 729 | case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING: |
| 730 | // Temporary IO problem (e.g. locking). Ignore but log a warning. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 731 | LOG(WARNING) << "IO error while reading profiles for location " << location; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 732 | need_to_compile = false; |
| 733 | should_clear_current_profiles = false; |
| 734 | should_clear_reference_profile = false; |
| 735 | break; |
| 736 | default: |
| 737 | // Unknown return code or error. Unlink profiles. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 738 | LOG(WARNING) << "Unknown error code while processing profiles for location " |
| 739 | << location << ": " << return_code; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 740 | need_to_compile = false; |
| 741 | should_clear_current_profiles = true; |
| 742 | should_clear_reference_profile = true; |
| 743 | break; |
| 744 | } |
| 745 | } |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 746 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 747 | if (should_clear_current_profiles) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 748 | if (is_secondary_dex) { |
| 749 | // For secondary dex files, the owning user is the current user. |
| 750 | clear_current_profile(location, multiuser_get_user_id(uid), is_secondary_dex); |
| 751 | } else { |
| 752 | clear_primary_current_profiles(location); |
| 753 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 754 | } |
| 755 | if (should_clear_reference_profile) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 756 | clear_reference_profile(location, is_secondary_dex); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 757 | } |
| 758 | return need_to_compile; |
| 759 | } |
| 760 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 761 | // Decides if profile guided compilation is needed or not based on existing profiles. |
| 762 | // The analysis is done for the primary apks of the given package. |
| 763 | // Returns true if there is enough information in the current profiles that makes it |
| 764 | // worth to recompile the package. |
| 765 | // If the return value is true all the current profiles would have been merged into |
| 766 | // the reference profiles accessible with open_reference_profile(). |
| 767 | bool analyze_primary_profiles(uid_t uid, const std::string& pkgname) { |
| 768 | return analyze_profiles(uid, pkgname, /*is_secondary_dex*/false); |
| 769 | } |
| 770 | |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 771 | static void run_profman_dump(const std::vector<unique_fd>& profile_fds, |
| 772 | const unique_fd& reference_profile_fd, |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 773 | const std::vector<std::string>& dex_locations, |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 774 | const std::vector<unique_fd>& apk_fds, |
| 775 | const unique_fd& output_fd) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 776 | std::vector<std::string> profman_args; |
| 777 | static const char* PROFMAN_BIN = "/system/bin/profman"; |
| 778 | profman_args.push_back(PROFMAN_BIN); |
| 779 | profman_args.push_back("--dump-only"); |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 780 | 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] | 781 | if (reference_profile_fd != -1) { |
| 782 | profman_args.push_back(StringPrintf("--reference-profile-file-fd=%d", |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 783 | reference_profile_fd.get())); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 784 | } |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 785 | for (size_t i = 0; i < profile_fds.size(); i++) { |
| 786 | 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] | 787 | } |
| 788 | for (const std::string& dex_location : dex_locations) { |
| 789 | profman_args.push_back(StringPrintf("--dex-location=%s", dex_location.c_str())); |
| 790 | } |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 791 | for (size_t i = 0; i < apk_fds.size(); i++) { |
| 792 | profman_args.push_back(StringPrintf("--apk-fd=%d", apk_fds[i].get())); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 793 | } |
| 794 | const char **argv = new const char*[profman_args.size() + 1]; |
| 795 | size_t i = 0; |
| 796 | for (const std::string& profman_arg : profman_args) { |
| 797 | argv[i++] = profman_arg.c_str(); |
| 798 | } |
| 799 | argv[i] = NULL; |
| 800 | |
| 801 | execv(PROFMAN_BIN, (char * const *)argv); |
| 802 | ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno)); |
| 803 | exit(68); /* only get here on exec failure */ |
| 804 | } |
| 805 | |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 806 | bool dump_profiles(int32_t uid, const std::string& pkgname, const char* code_paths) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 807 | std::vector<unique_fd> profile_fds; |
| 808 | unique_fd reference_profile_fd; |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 809 | std::string out_file_name = StringPrintf("/data/misc/profman/%s.txt", pkgname.c_str()); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 810 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 811 | open_profile_files(uid, pkgname, /*is_secondary_dex*/false, |
| 812 | &profile_fds, &reference_profile_fd); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 813 | |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 814 | const bool has_reference_profile = (reference_profile_fd.get() != -1); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 815 | const bool has_profiles = !profile_fds.empty(); |
| 816 | |
| 817 | if (!has_reference_profile && !has_profiles) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 818 | LOG(ERROR) << "profman dump: no profiles to dump for " << pkgname; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 819 | return false; |
| 820 | } |
| 821 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 822 | unique_fd output_fd(open(out_file_name.c_str(), |
| 823 | O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 824 | if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) { |
| 825 | ALOGE("installd cannot chmod '%s' dump_profile\n", out_file_name.c_str()); |
| 826 | return false; |
| 827 | } |
| 828 | std::vector<std::string> code_full_paths = base::Split(code_paths, ";"); |
| 829 | std::vector<std::string> dex_locations; |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 830 | std::vector<unique_fd> apk_fds; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 831 | for (const std::string& code_full_path : code_full_paths) { |
| 832 | const char* full_path = code_full_path.c_str(); |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 833 | unique_fd apk_fd(open(full_path, O_RDONLY | O_NOFOLLOW)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 834 | if (apk_fd == -1) { |
| 835 | ALOGE("installd cannot open '%s'\n", full_path); |
| 836 | return false; |
| 837 | } |
| 838 | dex_locations.push_back(get_location_from_path(full_path)); |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 839 | apk_fds.push_back(std::move(apk_fd)); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | pid_t pid = fork(); |
| 843 | if (pid == 0) { |
| 844 | /* child -- drop privileges before continuing */ |
| 845 | drop_capabilities(uid); |
| 846 | run_profman_dump(profile_fds, reference_profile_fd, dex_locations, |
| 847 | apk_fds, output_fd); |
| 848 | exit(68); /* only get here on exec failure */ |
| 849 | } |
| 850 | /* parent */ |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 851 | int return_code = wait_child(pid); |
| 852 | if (!WIFEXITED(return_code)) { |
| 853 | LOG(WARNING) << "profman failed for package " << pkgname << ": " |
| 854 | << return_code; |
| 855 | return false; |
| 856 | } |
| 857 | return true; |
| 858 | } |
| 859 | |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 860 | bool copy_system_profile(const std::string& system_profile, |
| 861 | uid_t packageUid, const std::string& data_profile_location) { |
| 862 | unique_fd in_fd(open(system_profile.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); |
| 863 | unique_fd out_fd(open_reference_profile(packageUid, |
| 864 | data_profile_location, |
| 865 | /*read_write*/ true, |
| 866 | /*secondary*/ false)); |
| 867 | if (in_fd.get() < 0) { |
| 868 | PLOG(WARNING) << "Could not open profile " << system_profile; |
| 869 | return false; |
| 870 | } |
| 871 | if (out_fd.get() < 0) { |
| 872 | PLOG(WARNING) << "Could not open profile " << data_profile_location; |
| 873 | return false; |
| 874 | } |
| 875 | |
Mathieu Chartier | 78f71fe | 2017-06-14 13:02:26 -0700 | [diff] [blame] | 876 | // As a security measure we want to write the profile information with the reduced capabilities |
| 877 | // 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] | 878 | pid_t pid = fork(); |
| 879 | if (pid == 0) { |
| 880 | /* child -- drop privileges before continuing */ |
| 881 | drop_capabilities(packageUid); |
| 882 | |
| 883 | if (flock(out_fd.get(), LOCK_EX | LOCK_NB) != 0) { |
| 884 | if (errno != EWOULDBLOCK) { |
| 885 | PLOG(WARNING) << "Error locking profile " << data_profile_location; |
| 886 | } |
| 887 | // This implies that the app owning this profile is running |
| 888 | // (and has acquired the lock). |
| 889 | // |
| 890 | // The app never acquires the lock for the reference profiles of primary apks. |
| 891 | // Only dex2oat from installd will do that. Since installd is single threaded |
| 892 | // we should not see this case. Nevertheless be prepared for it. |
| 893 | PLOG(WARNING) << "Failed to flock " << data_profile_location; |
| 894 | return false; |
| 895 | } |
| 896 | |
| 897 | bool truncated = ftruncate(out_fd.get(), 0) == 0; |
| 898 | if (!truncated) { |
| 899 | PLOG(WARNING) << "Could not truncate " << data_profile_location; |
| 900 | } |
| 901 | |
| 902 | // Copy over data. |
| 903 | static constexpr size_t kBufferSize = 4 * 1024; |
| 904 | char buffer[kBufferSize]; |
| 905 | while (true) { |
| 906 | ssize_t bytes = read(in_fd.get(), buffer, kBufferSize); |
| 907 | if (bytes == 0) { |
| 908 | break; |
| 909 | } |
| 910 | write(out_fd.get(), buffer, bytes); |
| 911 | } |
| 912 | if (flock(out_fd.get(), LOCK_UN) != 0) { |
| 913 | PLOG(WARNING) << "Error unlocking profile " << data_profile_location; |
| 914 | } |
Mathieu Chartier | 78f71fe | 2017-06-14 13:02:26 -0700 | [diff] [blame] | 915 | // Use _exit since we don't want to run the global destructors in the child. |
| 916 | // b/62597429 |
| 917 | _exit(0); |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 918 | } |
| 919 | /* parent */ |
| 920 | int return_code = wait_child(pid); |
| 921 | return return_code == 0; |
| 922 | } |
| 923 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 924 | static std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) { |
| 925 | // A standard dalvik-cache entry. Replace ".dex" with `new_ext`. |
| 926 | if (EndsWith(oat_path, ".dex")) { |
| 927 | std::string new_path = oat_path; |
| 928 | new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext); |
| 929 | CHECK(EndsWith(new_path, new_ext.c_str())); |
| 930 | return new_path; |
| 931 | } |
| 932 | |
| 933 | // An odex entry. Not that this may not be an extension, e.g., in the OTA |
| 934 | // case (where the base name will have an extension for the B artifact). |
| 935 | size_t odex_pos = oat_path.rfind(".odex"); |
| 936 | if (odex_pos != std::string::npos) { |
| 937 | std::string new_path = oat_path; |
| 938 | new_path.replace(odex_pos, strlen(".odex"), new_ext); |
| 939 | CHECK_NE(new_path.find(new_ext), std::string::npos); |
| 940 | return new_path; |
| 941 | } |
| 942 | |
| 943 | // Don't know how to handle this. |
| 944 | return ""; |
| 945 | } |
| 946 | |
| 947 | // Translate the given oat path to an art (app image) path. An empty string |
| 948 | // denotes an error. |
| 949 | static std::string create_image_filename(const std::string& oat_path) { |
| 950 | return replace_file_extension(oat_path, ".art"); |
| 951 | } |
| 952 | |
| 953 | // Translate the given oat path to a vdex path. An empty string denotes an error. |
| 954 | static std::string create_vdex_filename(const std::string& oat_path) { |
| 955 | return replace_file_extension(oat_path, ".vdex"); |
| 956 | } |
| 957 | |
| 958 | static bool add_extension_to_file_name(char* file_name, const char* extension) { |
| 959 | if (strlen(file_name) + strlen(extension) + 1 > PKG_PATH_MAX) { |
| 960 | return false; |
| 961 | } |
| 962 | strcat(file_name, extension); |
| 963 | return true; |
| 964 | } |
| 965 | |
| 966 | static int open_output_file(const char* file_name, bool recreate, int permissions) { |
| 967 | int flags = O_RDWR | O_CREAT; |
| 968 | if (recreate) { |
| 969 | if (unlink(file_name) < 0) { |
| 970 | if (errno != ENOENT) { |
| 971 | PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name; |
| 972 | } |
| 973 | } |
| 974 | flags |= O_EXCL; |
| 975 | } |
| 976 | return open(file_name, flags, permissions); |
| 977 | } |
| 978 | |
Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 979 | static bool set_permissions_and_ownership( |
| 980 | int fd, bool is_public, int uid, const char* path, bool is_secondary_dex) { |
| 981 | // Primary apks are owned by the system. Secondary dex files are owned by the app. |
| 982 | int owning_uid = is_secondary_dex ? uid : AID_SYSTEM; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 983 | if (fchmod(fd, |
| 984 | S_IRUSR|S_IWUSR|S_IRGRP | |
| 985 | (is_public ? S_IROTH : 0)) < 0) { |
| 986 | ALOGE("installd cannot chmod '%s' during dexopt\n", path); |
| 987 | return false; |
Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 988 | } else if (fchown(fd, owning_uid, uid) < 0) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 989 | ALOGE("installd cannot chown '%s' during dexopt\n", path); |
| 990 | return false; |
| 991 | } |
| 992 | return true; |
| 993 | } |
| 994 | |
| 995 | static bool IsOutputDalvikCache(const char* oat_dir) { |
| 996 | // InstallerConnection.java (which invokes installd) transforms Java null arguments |
| 997 | // into '!'. Play it safe by handling it both. |
| 998 | // TODO: ensure we never get null. |
| 999 | // TODO: pass a flag instead of inferring if the output is dalvik cache. |
| 1000 | return oat_dir == nullptr || oat_dir[0] == '!'; |
| 1001 | } |
| 1002 | |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1003 | // Best-effort check whether we can fit the the path into our buffers. |
| 1004 | // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run |
| 1005 | // without a swap file, if necessary. Reference profiles file also add an extra ".prof" |
| 1006 | // extension to the cache path (5 bytes). |
| 1007 | // TODO(calin): move away from char* buffers and PKG_PATH_MAX. |
| 1008 | static bool validate_dex_path_size(const std::string& dex_path) { |
| 1009 | if (dex_path.size() >= (PKG_PATH_MAX - 8)) { |
| 1010 | LOG(ERROR) << "dex_path too long: " << dex_path; |
| 1011 | return false; |
| 1012 | } |
| 1013 | return true; |
| 1014 | } |
| 1015 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1016 | 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] | 1017 | 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] | 1018 | if (!validate_dex_path_size(apk_path)) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
| 1021 | |
| 1022 | if (!IsOutputDalvikCache(oat_dir)) { |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1023 | // Oat dirs for secondary dex files are already validated. |
| 1024 | if (!is_secondary_dex && validate_apk_path(oat_dir)) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1025 | ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir); |
| 1026 | return false; |
| 1027 | } |
| 1028 | if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) { |
| 1029 | return false; |
| 1030 | } |
| 1031 | } else { |
| 1032 | if (!create_cache_path(out_oat_path, apk_path, instruction_set)) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | } |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
| 1039 | // Helper for fd management. This is similar to a unique_fd in that it closes the file descriptor |
| 1040 | // on destruction. It will also run the given cleanup (unless told not to) after closing. |
| 1041 | // |
| 1042 | // Usage example: |
| 1043 | // |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1044 | // Dex2oatFileWrapper file(open(...), |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1045 | // [name]() { |
| 1046 | // unlink(name.c_str()); |
| 1047 | // }); |
| 1048 | // // Note: care needs to be taken about name, as it needs to have a lifetime longer than the |
| 1049 | // wrapper if captured as a reference. |
| 1050 | // |
| 1051 | // if (file.get() == -1) { |
| 1052 | // // Error opening... |
| 1053 | // } |
| 1054 | // |
| 1055 | // ... |
| 1056 | // if (error) { |
| 1057 | // // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will run |
| 1058 | // // and delete the file (after the fd is closed). |
| 1059 | // return -1; |
| 1060 | // } |
| 1061 | // |
| 1062 | // (Success case) |
| 1063 | // file.SetCleanup(false); |
| 1064 | // // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will not run |
| 1065 | // // (leaving the file around; after the fd is closed). |
| 1066 | // |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1067 | class Dex2oatFileWrapper { |
| 1068 | public: |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1069 | Dex2oatFileWrapper() : value_(-1), cleanup_(), do_cleanup_(true), auto_close_(true) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1072 | Dex2oatFileWrapper(int value, std::function<void ()> cleanup) |
| 1073 | : value_(value), cleanup_(cleanup), do_cleanup_(true), auto_close_(true) {} |
| 1074 | |
| 1075 | Dex2oatFileWrapper(Dex2oatFileWrapper&& other) { |
| 1076 | value_ = other.value_; |
| 1077 | cleanup_ = other.cleanup_; |
| 1078 | do_cleanup_ = other.do_cleanup_; |
| 1079 | auto_close_ = other.auto_close_; |
| 1080 | other.release(); |
| 1081 | } |
| 1082 | |
| 1083 | Dex2oatFileWrapper& operator=(Dex2oatFileWrapper&& other) { |
| 1084 | value_ = other.value_; |
| 1085 | cleanup_ = other.cleanup_; |
| 1086 | do_cleanup_ = other.do_cleanup_; |
| 1087 | auto_close_ = other.auto_close_; |
| 1088 | other.release(); |
| 1089 | return *this; |
| 1090 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1091 | |
| 1092 | ~Dex2oatFileWrapper() { |
| 1093 | reset(-1); |
| 1094 | } |
| 1095 | |
| 1096 | int get() { |
| 1097 | return value_; |
| 1098 | } |
| 1099 | |
| 1100 | void SetCleanup(bool cleanup) { |
| 1101 | do_cleanup_ = cleanup; |
| 1102 | } |
| 1103 | |
| 1104 | void reset(int new_value) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1105 | if (auto_close_ && value_ >= 0) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1106 | close(value_); |
| 1107 | } |
| 1108 | if (do_cleanup_ && cleanup_ != nullptr) { |
| 1109 | cleanup_(); |
| 1110 | } |
| 1111 | |
| 1112 | value_ = new_value; |
| 1113 | } |
| 1114 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1115 | void reset(int new_value, std::function<void ()> new_cleanup) { |
| 1116 | if (auto_close_ && value_ >= 0) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1117 | close(value_); |
| 1118 | } |
| 1119 | if (do_cleanup_ && cleanup_ != nullptr) { |
| 1120 | cleanup_(); |
| 1121 | } |
| 1122 | |
| 1123 | value_ = new_value; |
| 1124 | cleanup_ = new_cleanup; |
| 1125 | } |
| 1126 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1127 | void DisableAutoClose() { |
| 1128 | auto_close_ = false; |
| 1129 | } |
| 1130 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1131 | private: |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1132 | void release() { |
| 1133 | value_ = -1; |
| 1134 | do_cleanup_ = false; |
| 1135 | cleanup_ = nullptr; |
| 1136 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1137 | int value_; |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1138 | std::function<void ()> cleanup_; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1139 | bool do_cleanup_; |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1140 | bool auto_close_; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1141 | }; |
| 1142 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1143 | // (re)Creates the app image if needed. |
| 1144 | 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] | 1145 | bool is_public, int uid, bool is_secondary_dex) { |
Nicolas Geoffray | aa17ab4 | 2017-08-15 14:51:05 +0100 | [diff] [blame] | 1146 | |
| 1147 | // We don't create an image for secondary dex files. |
| 1148 | if (is_secondary_dex) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1149 | return Dex2oatFileWrapper(); |
| 1150 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1151 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1152 | const std::string image_path = create_image_filename(out_oat_path); |
| 1153 | if (image_path.empty()) { |
| 1154 | // Happens when the out_oat_path has an unknown extension. |
| 1155 | return Dex2oatFileWrapper(); |
| 1156 | } |
Nicolas Geoffray | aa17ab4 | 2017-08-15 14:51:05 +0100 | [diff] [blame] | 1157 | |
| 1158 | // Use app images only if it is enabled (by a set image format) and we are compiling |
| 1159 | // profile-guided (so the app image doesn't conservatively contain all classes). |
| 1160 | if (!profile_guided) { |
| 1161 | // In case there is a stale image, remove it now. Ignore any error. |
| 1162 | unlink(image_path.c_str()); |
| 1163 | return Dex2oatFileWrapper(); |
| 1164 | } |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1165 | char app_image_format[kPropertyValueMax]; |
| 1166 | bool have_app_image_format = |
| 1167 | get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0; |
| 1168 | if (!have_app_image_format) { |
| 1169 | return Dex2oatFileWrapper(); |
| 1170 | } |
| 1171 | // Recreate is true since we do not want to modify a mapped image. If the app is |
| 1172 | // already running and we modify the image file, it can cause crashes (b/27493510). |
| 1173 | Dex2oatFileWrapper wrapper_fd( |
| 1174 | open_output_file(image_path.c_str(), true /*recreate*/, 0600 /*permissions*/), |
| 1175 | [image_path]() { unlink(image_path.c_str()); }); |
| 1176 | if (wrapper_fd.get() < 0) { |
| 1177 | // Could not create application image file. Go on since we can compile without it. |
| 1178 | LOG(ERROR) << "installd could not create '" << image_path |
| 1179 | << "' for image file during dexopt"; |
| 1180 | // If we have a valid image file path but no image fd, explicitly erase the image file. |
| 1181 | if (unlink(image_path.c_str()) < 0) { |
| 1182 | if (errno != ENOENT) { |
| 1183 | PLOG(ERROR) << "Couldn't unlink image file " << image_path; |
| 1184 | } |
| 1185 | } |
| 1186 | } else if (!set_permissions_and_ownership( |
Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 1187 | 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] | 1188 | ALOGE("installd cannot set owner '%s' for image during dexopt\n", image_path.c_str()); |
| 1189 | wrapper_fd.reset(-1); |
| 1190 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1191 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1192 | return wrapper_fd; |
| 1193 | } |
| 1194 | |
| 1195 | // Creates the dexopt swap file if necessary and return its fd. |
| 1196 | // 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] | 1197 | unique_fd maybe_open_dexopt_swap_file(const char* out_oat_path) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1198 | if (!ShouldUseSwapFileForDexopt()) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 1199 | return invalid_unique_fd(); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1200 | } |
| 1201 | // Make sure there really is enough space. |
| 1202 | char swap_file_name[PKG_PATH_MAX]; |
| 1203 | strcpy(swap_file_name, out_oat_path); |
| 1204 | if (!add_extension_to_file_name(swap_file_name, ".swap")) { |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 1205 | return invalid_unique_fd(); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1206 | } |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 1207 | unique_fd swap_fd(open_output_file( |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1208 | swap_file_name, /*recreate*/true, /*permissions*/0600)); |
| 1209 | if (swap_fd.get() < 0) { |
| 1210 | // Could not create swap file. Optimistically go on and hope that we can compile |
| 1211 | // without it. |
| 1212 | ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name); |
| 1213 | } else { |
| 1214 | // Immediately unlink. We don't really want to hit flash. |
| 1215 | if (unlink(swap_file_name) < 0) { |
| 1216 | PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name; |
| 1217 | } |
| 1218 | } |
| 1219 | return swap_fd; |
| 1220 | } |
| 1221 | |
| 1222 | // Opens the reference profiles if needed. |
| 1223 | // 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] | 1224 | Dex2oatFileWrapper maybe_open_reference_profile(const std::string& pkgname, |
| 1225 | const std::string& dex_path, bool profile_guided, bool is_public, int uid, |
| 1226 | bool is_secondary_dex) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1227 | // Public apps should not be compiled with profile information ever. Same goes for the special |
| 1228 | // package '*' used for the system server. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1229 | if (!profile_guided || is_public || (pkgname[0] == '*')) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1230 | return Dex2oatFileWrapper(); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1231 | } |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1232 | |
| 1233 | // Open reference profile in read only mode as dex2oat does not get write permissions. |
| 1234 | const std::string location = is_secondary_dex ? dex_path : pkgname; |
| 1235 | unique_fd ufd = open_reference_profile(uid, location, /*read_write*/false, is_secondary_dex); |
| 1236 | const auto& cleanup = [location, is_secondary_dex]() { |
| 1237 | clear_reference_profile(location.c_str(), is_secondary_dex); |
| 1238 | }; |
| 1239 | return Dex2oatFileWrapper(ufd.release(), cleanup); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1240 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1241 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1242 | // Opens the vdex files and assigns the input fd to in_vdex_wrapper_fd and the output fd to |
| 1243 | // out_vdex_wrapper_fd. Returns true for success or false in case of errors. |
| 1244 | bool open_vdex_files(const char* apk_path, const char* out_oat_path, int dexopt_needed, |
Nicolas Geoffray | 3c95f2d | 2017-04-24 13:34:59 +0000 | [diff] [blame] | 1245 | 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] | 1246 | bool profile_guided, Dex2oatFileWrapper* in_vdex_wrapper_fd, |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1247 | Dex2oatFileWrapper* out_vdex_wrapper_fd) { |
| 1248 | CHECK(in_vdex_wrapper_fd != nullptr); |
| 1249 | CHECK(out_vdex_wrapper_fd != nullptr); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1250 | // Open the existing VDEX. We do this before creating the new output VDEX, which will |
| 1251 | // unlink the old one. |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1252 | char in_odex_path[PKG_PATH_MAX]; |
| 1253 | int dexopt_action = abs(dexopt_needed); |
| 1254 | bool is_odex_location = dexopt_needed < 0; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1255 | std::string in_vdex_path_str; |
Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1256 | |
| 1257 | // Infer the name of the output VDEX. |
| 1258 | const std::string out_vdex_path_str = create_vdex_filename(out_oat_path); |
| 1259 | if (out_vdex_path_str.empty()) { |
| 1260 | return false; |
| 1261 | } |
| 1262 | |
| 1263 | bool update_vdex_in_place = false; |
Nicolas Geoffray | 3c95f2d | 2017-04-24 13:34:59 +0000 | [diff] [blame] | 1264 | if (dexopt_action != DEX2OAT_FROM_SCRATCH) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1265 | // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd. |
| 1266 | const char* path = nullptr; |
| 1267 | if (is_odex_location) { |
| 1268 | if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { |
| 1269 | path = in_odex_path; |
| 1270 | } else { |
| 1271 | ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1272 | return false; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } else { |
| 1275 | path = out_oat_path; |
| 1276 | } |
| 1277 | in_vdex_path_str = create_vdex_filename(path); |
| 1278 | if (in_vdex_path_str.empty()) { |
| 1279 | ALOGE("installd cannot compute input vdex location for '%s'\n", path); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1280 | return false; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1281 | } |
Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1282 | // We can update in place when all these conditions are met: |
| 1283 | // 1) The vdex location to write to is the same as the vdex location to read (vdex files |
| 1284 | // on /system typically cannot be updated in place). |
| 1285 | // 2) We dex2oat due to boot image change, because we then know the existing vdex file |
| 1286 | // cannot be currently used by a running process. |
| 1287 | // 3) We are not doing a profile guided compilation, because dexlayout requires two |
| 1288 | // different vdex files to operate. |
| 1289 | update_vdex_in_place = |
| 1290 | (in_vdex_path_str == out_vdex_path_str) && |
| 1291 | (dexopt_action == DEX2OAT_FOR_BOOT_IMAGE) && |
| 1292 | !profile_guided; |
| 1293 | if (update_vdex_in_place) { |
| 1294 | // Open the file read-write to be able to update it. |
| 1295 | in_vdex_wrapper_fd->reset(open(in_vdex_path_str.c_str(), O_RDWR, 0)); |
| 1296 | if (in_vdex_wrapper_fd->get() == -1) { |
| 1297 | // If we failed to open the file, we cannot update it in place. |
| 1298 | update_vdex_in_place = false; |
| 1299 | } |
| 1300 | } else { |
| 1301 | in_vdex_wrapper_fd->reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0)); |
| 1302 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1305 | // If we are updating the vdex in place, we do not need to recreate a vdex, |
| 1306 | // and can use the same existing one. |
| 1307 | if (update_vdex_in_place) { |
| 1308 | // We unlink the file in case the invocation of dex2oat fails, to ensure we don't |
| 1309 | // have bogus stale vdex files. |
| 1310 | out_vdex_wrapper_fd->reset( |
| 1311 | in_vdex_wrapper_fd->get(), |
| 1312 | [out_vdex_path_str]() { unlink(out_vdex_path_str.c_str()); }); |
| 1313 | // Disable auto close for the in wrapper fd (it will be done when destructing the out |
| 1314 | // wrapper). |
| 1315 | in_vdex_wrapper_fd->DisableAutoClose(); |
| 1316 | } else { |
| 1317 | out_vdex_wrapper_fd->reset( |
| 1318 | open_output_file(out_vdex_path_str.c_str(), /*recreate*/true, /*permissions*/0644), |
| 1319 | [out_vdex_path_str]() { unlink(out_vdex_path_str.c_str()); }); |
| 1320 | if (out_vdex_wrapper_fd->get() < 0) { |
| 1321 | ALOGE("installd cannot open vdex'%s' during dexopt\n", out_vdex_path_str.c_str()); |
| 1322 | return false; |
| 1323 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1324 | } |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1325 | 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] | 1326 | out_vdex_path_str.c_str(), is_secondary_dex)) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1327 | ALOGE("installd cannot set owner '%s' for vdex during dexopt\n", out_vdex_path_str.c_str()); |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | // If we got here we successfully opened the vdex files. |
| 1332 | return true; |
| 1333 | } |
| 1334 | |
| 1335 | // Opens the output oat file for the given apk. |
| 1336 | // If successful it stores the output path into out_oat_path and returns true. |
| 1337 | 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] | 1338 | bool is_public, int uid, const char* instruction_set, bool is_secondary_dex, |
| 1339 | char* out_oat_path) { |
| 1340 | 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] | 1341 | return Dex2oatFileWrapper(); |
| 1342 | } |
| 1343 | const std::string out_oat_path_str(out_oat_path); |
| 1344 | Dex2oatFileWrapper wrapper_fd( |
| 1345 | open_output_file(out_oat_path, /*recreate*/true, /*permissions*/0644), |
| 1346 | [out_oat_path_str]() { unlink(out_oat_path_str.c_str()); }); |
| 1347 | if (wrapper_fd.get() < 0) { |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1348 | PLOG(ERROR) << "installd cannot open output during dexopt" << out_oat_path; |
Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 1349 | } else if (!set_permissions_and_ownership( |
| 1350 | wrapper_fd.get(), is_public, uid, out_oat_path, is_secondary_dex)) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1351 | ALOGE("installd cannot set owner '%s' for output during dexopt\n", out_oat_path); |
| 1352 | wrapper_fd.reset(-1); |
| 1353 | } |
| 1354 | return wrapper_fd; |
| 1355 | } |
| 1356 | |
| 1357 | // Updates the access times of out_oat_path based on those from apk_path. |
| 1358 | void update_out_oat_access_times(const char* apk_path, const char* out_oat_path) { |
| 1359 | struct stat input_stat; |
| 1360 | memset(&input_stat, 0, sizeof(input_stat)); |
| 1361 | if (stat(apk_path, &input_stat) != 0) { |
| 1362 | PLOG(ERROR) << "Could not stat " << apk_path << " during dexopt"; |
| 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | struct utimbuf ut; |
| 1367 | ut.actime = input_stat.st_atime; |
| 1368 | ut.modtime = input_stat.st_mtime; |
| 1369 | if (utime(out_oat_path, &ut) != 0) { |
| 1370 | PLOG(WARNING) << "Could not update access times for " << apk_path << " during dexopt"; |
| 1371 | } |
| 1372 | } |
| 1373 | |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1374 | // Runs (execv) dexoptanalyzer on the given arguments. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1375 | // The analyzer will check if the dex_file needs to be (re)compiled to match the compiler_filter. |
| 1376 | // If this is for a profile guided compilation, profile_was_updated will tell whether or not |
| 1377 | // the profile has changed. |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1378 | static void exec_dexoptanalyzer(const std::string& dex_file, const std::string& instruction_set, |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1379 | const std::string& compiler_filter, bool profile_was_updated, bool downgrade) { |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 1380 | const char* dexoptanalyzer_bin = |
| 1381 | is_debug_runtime() |
| 1382 | ? "/system/bin/dexoptanalyzerd" |
| 1383 | : "/system/bin/dexoptanalyzer"; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1384 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; |
| 1385 | |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1386 | if (instruction_set.size() >= MAX_INSTRUCTION_SET_LEN) { |
| 1387 | LOG(ERROR) << "Instruction set " << instruction_set |
| 1388 | << " longer than max length of " << MAX_INSTRUCTION_SET_LEN; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1389 | return; |
| 1390 | } |
| 1391 | |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1392 | std::string dex_file_arg = "--dex-file=" + dex_file; |
| 1393 | std::string isa_arg = "--isa=" + instruction_set; |
| 1394 | std::string compiler_filter_arg = "--compiler-filter=" + compiler_filter; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1395 | const char* assume_profile_changed = "--assume-profile-changed"; |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1396 | const char* downgrade_flag = "--downgrade"; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1397 | |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1398 | // program name, dex file, isa, filter, the final NULL |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1399 | const int argc = 5 + |
| 1400 | (profile_was_updated ? 1 : 0) + |
| 1401 | (downgrade ? 1 : 0); |
| 1402 | const char* argv[argc]; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1403 | int i = 0; |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 1404 | argv[i++] = dexoptanalyzer_bin; |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1405 | argv[i++] = dex_file_arg.c_str(); |
| 1406 | argv[i++] = isa_arg.c_str(); |
| 1407 | argv[i++] = compiler_filter_arg.c_str(); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1408 | if (profile_was_updated) { |
| 1409 | argv[i++] = assume_profile_changed; |
| 1410 | } |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1411 | if (downgrade) { |
| 1412 | argv[i++] = downgrade_flag; |
| 1413 | } |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1414 | argv[i] = NULL; |
| 1415 | |
Andreas Gampe | 6a9cf72 | 2017-07-24 16:49:10 -0700 | [diff] [blame] | 1416 | execv(dexoptanalyzer_bin, (char * const *)argv); |
| 1417 | ALOGE("execv(%s) failed: %s\n", dexoptanalyzer_bin, strerror(errno)); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | // Prepares the oat dir for the secondary dex files. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1421 | static bool prepare_secondary_dex_oat_dir(const std::string& dex_path, int uid, |
| 1422 | const char* instruction_set, std::string* oat_dir_out) { |
| 1423 | unsigned long dirIndex = dex_path.rfind('/'); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1424 | if (dirIndex == std::string::npos) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1425 | LOG(ERROR ) << "Unexpected dir structure for secondary dex " << dex_path; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1426 | return false; |
| 1427 | } |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1428 | std::string dex_dir = dex_path.substr(0, dirIndex); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1429 | |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1430 | // Create oat file output directory. |
Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1431 | mode_t oat_dir_mode = S_IRWXU | S_IRWXG | S_IXOTH; |
| 1432 | 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] | 1433 | LOG(ERROR) << "Could not prepare oat dir for secondary dex: " << dex_path; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1434 | return false; |
| 1435 | } |
| 1436 | |
| 1437 | char oat_dir[PKG_PATH_MAX]; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1438 | snprintf(oat_dir, PKG_PATH_MAX, "%s/oat", dex_dir.c_str()); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1439 | oat_dir_out->assign(oat_dir); |
| 1440 | |
| 1441 | // Create oat/isa output directory. |
Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1442 | if (prepare_app_cache_dir(*oat_dir_out, instruction_set, oat_dir_mode, uid, uid) != 0) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1443 | 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] | 1444 | return false; |
| 1445 | } |
| 1446 | |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
| 1450 | static int constexpr DEXOPTANALYZER_BIN_EXEC_ERROR = 200; |
| 1451 | |
| 1452 | // Verifies the result of dexoptanalyzer executed for the apk_path. |
| 1453 | // If the result is valid returns true and sets dexopt_needed_out to a valid value. |
| 1454 | // Returns false for errors or unexpected result values. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1455 | static bool process_dexoptanalyzer_result(const std::string& dex_path, int result, |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1456 | int* dexopt_needed_out) { |
| 1457 | // The result values are defined in dexoptanalyzer. |
| 1458 | switch (result) { |
| 1459 | case 0: // no_dexopt_needed |
| 1460 | *dexopt_needed_out = NO_DEXOPT_NEEDED; return true; |
| 1461 | case 1: // dex2oat_from_scratch |
| 1462 | *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; return true; |
| 1463 | case 5: // dex2oat_for_bootimage_odex |
| 1464 | *dexopt_needed_out = -DEX2OAT_FOR_BOOT_IMAGE; return true; |
| 1465 | case 6: // dex2oat_for_filter_odex |
| 1466 | *dexopt_needed_out = -DEX2OAT_FOR_FILTER; return true; |
| 1467 | case 7: // dex2oat_for_relocation_odex |
| 1468 | *dexopt_needed_out = -DEX2OAT_FOR_RELOCATION; return true; |
| 1469 | case 2: // dex2oat_for_bootimage_oat |
| 1470 | case 3: // dex2oat_for_filter_oat |
| 1471 | case 4: // dex2oat_for_relocation_oat |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1472 | LOG(ERROR) << "Dexoptnalyzer return the status of an oat file." |
| 1473 | << " Expected odex file status for secondary dex " << dex_path |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1474 | << " : dexoptanalyzer result=" << result; |
| 1475 | return false; |
| 1476 | default: |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1477 | LOG(ERROR) << "Unexpected result for dexoptanalyzer " << dex_path |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1478 | << " exec_dexoptanalyzer result=" << result; |
| 1479 | return false; |
| 1480 | } |
| 1481 | } |
| 1482 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1483 | // 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] | 1484 | // be compiled. Returns false for errors (logged) or true if the secondary dex path was process |
| 1485 | // successfully. |
Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1486 | // When returning true, the output parameters will be: |
| 1487 | // - is_public_out: whether or not the oat file should not be made public |
| 1488 | // - dexopt_needed_out: valid OatFileAsssitant::DexOptNeeded |
| 1489 | // - oat_dir_out: the oat dir path where the oat file should be stored |
| 1490 | // - dex_path_out: the real path of the dex file |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1491 | static bool process_secondary_dex_dexopt(const char* original_dex_path, const char* pkgname, |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1492 | 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] | 1493 | const char* compiler_filter, bool* is_public_out, int* dexopt_needed_out, |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1494 | std::string* oat_dir_out, std::string* dex_path_out, bool downgrade) { |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1495 | int storage_flag; |
| 1496 | |
| 1497 | if ((dexopt_flags & DEXOPT_STORAGE_CE) != 0) { |
| 1498 | storage_flag = FLAG_STORAGE_CE; |
| 1499 | if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) { |
| 1500 | LOG(ERROR) << "Ambiguous secondary dex storage flag. Both, CE and DE, flags are set"; |
| 1501 | return false; |
| 1502 | } |
| 1503 | } else if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) { |
| 1504 | storage_flag = FLAG_STORAGE_DE; |
| 1505 | } else { |
| 1506 | LOG(ERROR) << "Secondary dex storage flag must be set"; |
| 1507 | return false; |
| 1508 | } |
| 1509 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1510 | { |
| 1511 | // As opposed to the primary apk, secondary dex files might contain symlinks. |
| 1512 | // Resolve the path before passing it to the validate method to |
| 1513 | // make sure the verification is done on the real location. |
| 1514 | UniqueCPtr<char> dex_real_path_cstr(realpath(original_dex_path, nullptr)); |
| 1515 | if (dex_real_path_cstr == nullptr) { |
| 1516 | PLOG(ERROR) << "Could not get the real path of the secondary dex file " |
| 1517 | << original_dex_path; |
| 1518 | return false; |
| 1519 | } else { |
| 1520 | dex_path_out->assign(dex_real_path_cstr.get()); |
| 1521 | } |
| 1522 | } |
| 1523 | const std::string& dex_path = *dex_path_out; |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1524 | if (!validate_dex_path_size(dex_path)) { |
| 1525 | return false; |
| 1526 | } |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1527 | if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid, uid, storage_flag)) { |
| 1528 | LOG(ERROR) << "Could not validate secondary dex path " << dex_path; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | // Check if the path exist. If not, there's nothing to do. |
Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1533 | struct stat dex_path_stat; |
| 1534 | if (stat(dex_path.c_str(), &dex_path_stat) != 0) { |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1535 | if (errno == ENOENT) { |
| 1536 | // Secondary dex files might be deleted any time by the app. |
| 1537 | // Nothing to do if that's the case |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1538 | ALOGV("Secondary dex does not exist %s", dex_path.c_str()); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1539 | return NO_DEXOPT_NEEDED; |
| 1540 | } else { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1541 | PLOG(ERROR) << "Could not access secondary dex " << dex_path; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1545 | // Check if we should make the oat file public. |
| 1546 | // Note that if the dex file is not public the compiled code cannot be made public. |
| 1547 | *is_public_out = ((dexopt_flags & DEXOPT_PUBLIC) != 0) && |
| 1548 | ((dex_path_stat.st_mode & S_IROTH) != 0); |
| 1549 | |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1550 | // Prepare the oat directories. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1551 | if (!prepare_secondary_dex_oat_dir(dex_path, uid, instruction_set, oat_dir_out)) { |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1552 | return false; |
| 1553 | } |
| 1554 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1555 | // Analyze profiles. |
| 1556 | bool profile_was_updated = analyze_profiles(uid, dex_path, /*is_secondary_dex*/true); |
| 1557 | |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1558 | pid_t pid = fork(); |
| 1559 | if (pid == 0) { |
| 1560 | // child -- drop privileges before continuing. |
| 1561 | drop_capabilities(uid); |
| 1562 | // Run dexoptanalyzer to get dexopt_needed code. |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1563 | exec_dexoptanalyzer(dex_path, instruction_set, compiler_filter, profile_was_updated, |
| 1564 | downgrade); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1565 | exit(DEXOPTANALYZER_BIN_EXEC_ERROR); |
| 1566 | } |
| 1567 | |
| 1568 | /* parent */ |
| 1569 | |
| 1570 | int result = wait_child(pid); |
| 1571 | if (!WIFEXITED(result)) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1572 | LOG(ERROR) << "dexoptanalyzer failed for path " << dex_path << ": " << result; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1573 | return false; |
| 1574 | } |
| 1575 | result = WEXITSTATUS(result); |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1576 | bool success = process_dexoptanalyzer_result(dex_path, result, dexopt_needed_out); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1577 | // Run dexopt only if needed or forced. |
| 1578 | // Note that dexoptanalyzer is executed even if force compilation is enabled. |
| 1579 | // We ignore its valid dexopNeeded result, but still check (in process_dexoptanalyzer_result) |
| 1580 | // that we only get results for odex files (apk_dir/oat/isa/code.odex) and not |
| 1581 | // for oat files from dalvik-cache. |
| 1582 | if (success && ((dexopt_flags & DEXOPT_FORCE) != 0)) { |
| 1583 | *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; |
| 1584 | } |
| 1585 | |
| 1586 | return success; |
| 1587 | } |
| 1588 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1589 | 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] | 1590 | 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] | 1591 | const char* volume_uuid, const char* class_loader_context, const char* se_info, |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1592 | bool downgrade) { |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1593 | CHECK(pkgname != nullptr); |
| 1594 | CHECK(pkgname[0] != 0); |
| 1595 | if ((dexopt_flags & ~DEXOPT_MASK) != 0) { |
| 1596 | LOG_FATAL("dexopt flags contains unknown fields\n"); |
| 1597 | } |
| 1598 | |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1599 | if (!validate_dex_path_size(dex_path)) { |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 1600 | return -1; |
| 1601 | } |
| 1602 | |
| 1603 | if (class_loader_context != nullptr && strlen(class_loader_context) > PKG_PATH_MAX) { |
| 1604 | LOG(ERROR) << "Class loader context exceeds the allowed size: " << class_loader_context; |
| 1605 | return -1; |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Calin Juravle | ebc8a79 | 2017-04-04 20:21:05 -0700 | [diff] [blame] | 1608 | bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0; |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1609 | bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0; |
| 1610 | bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0; |
| 1611 | bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1612 | bool is_secondary_dex = (dexopt_flags & DEXOPT_SECONDARY_DEX) != 0; |
| 1613 | |
| 1614 | // Check if we're dealing with a secondary dex file and if we need to compile it. |
| 1615 | std::string oat_dir_str; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1616 | std::string dex_real_path; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1617 | if (is_secondary_dex) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1618 | 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] | 1619 | instruction_set, compiler_filter, &is_public, &dexopt_needed, &oat_dir_str, |
Shubham Ajmera | 54ef862 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 1620 | &dex_real_path, |
| 1621 | downgrade)) { |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1622 | oat_dir = oat_dir_str.c_str(); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1623 | dex_path = dex_real_path.c_str(); |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1624 | if (dexopt_needed == NO_DEXOPT_NEEDED) { |
| 1625 | return 0; // Nothing to do, report success. |
| 1626 | } |
| 1627 | } else { |
| 1628 | return -1; // We had an error, logged in the process method. |
| 1629 | } |
| 1630 | } else { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1631 | // Currently these flags are only use for secondary dex files. |
| 1632 | // Verify that they are not set for primary apks. |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1633 | CHECK((dexopt_flags & DEXOPT_STORAGE_CE) == 0); |
| 1634 | CHECK((dexopt_flags & DEXOPT_STORAGE_DE) == 0); |
| 1635 | } |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1636 | |
| 1637 | // Open the input file. |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 1638 | unique_fd input_fd(open(dex_path, O_RDONLY, 0)); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1639 | if (input_fd.get() < 0) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1640 | ALOGE("installd cannot open '%s' for input during dexopt\n", dex_path); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1641 | return -1; |
| 1642 | } |
| 1643 | |
| 1644 | // Create the output OAT file. |
| 1645 | char out_oat_path[PKG_PATH_MAX]; |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1646 | 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] | 1647 | instruction_set, is_secondary_dex, out_oat_path); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1648 | if (out_oat_fd.get() < 0) { |
| 1649 | return -1; |
| 1650 | } |
| 1651 | |
| 1652 | // Open vdex files. |
| 1653 | Dex2oatFileWrapper in_vdex_fd; |
| 1654 | Dex2oatFileWrapper out_vdex_fd; |
Nicolas Geoffray | 3c95f2d | 2017-04-24 13:34:59 +0000 | [diff] [blame] | 1655 | if (!open_vdex_files(dex_path, out_oat_path, dexopt_needed, instruction_set, is_public, uid, |
Nicolas Geoffray | b03814f | 2017-06-05 12:38:10 +0000 | [diff] [blame] | 1656 | is_secondary_dex, profile_guided, &in_vdex_fd, &out_vdex_fd)) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1657 | return -1; |
| 1658 | } |
| 1659 | |
Calin Juravle | cb556e3 | 2017-04-04 20:22:50 -0700 | [diff] [blame] | 1660 | // Ensure that the oat dir and the compiler artifacts of secondary dex files have the correct |
| 1661 | // selinux context (we generate them on the fly during the dexopt invocation and they don't |
| 1662 | // fully inherit their parent context). |
| 1663 | // Note that for primary apk the oat files are created before, in a separate installd |
| 1664 | // call which also does the restorecon. TODO(calin): unify the paths. |
| 1665 | if (is_secondary_dex) { |
| 1666 | if (selinux_android_restorecon_pkgdir(oat_dir, se_info, uid, |
| 1667 | SELINUX_ANDROID_RESTORECON_RECURSE)) { |
| 1668 | LOG(ERROR) << "Failed to restorecon " << oat_dir; |
| 1669 | return -1; |
| 1670 | } |
| 1671 | } |
| 1672 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1673 | // Create a swap file if necessary. |
Calin Juravle | 1a0af3b | 2017-03-09 14:33:33 -0800 | [diff] [blame] | 1674 | unique_fd swap_fd = maybe_open_dexopt_swap_file(out_oat_path); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1675 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1676 | // Create the app image file if needed. |
| 1677 | Dex2oatFileWrapper image_fd = |
Calin Juravle | 2289c0a | 2017-02-15 12:44:14 -0800 | [diff] [blame] | 1678 | 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] | 1679 | |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1680 | // Open the reference profile if needed. |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1681 | Dex2oatFileWrapper reference_profile_fd = maybe_open_reference_profile( |
| 1682 | pkgname, dex_path, profile_guided, is_public, uid, is_secondary_dex); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1683 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1684 | ALOGV("DexInv: --- BEGIN '%s' ---\n", dex_path); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1685 | |
| 1686 | pid_t pid = fork(); |
| 1687 | if (pid == 0) { |
| 1688 | /* child -- drop privileges before continuing */ |
| 1689 | drop_capabilities(uid); |
| 1690 | |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1691 | SetDex2OatScheduling(boot_complete); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1692 | if (flock(out_oat_fd.get(), LOCK_EX | LOCK_NB) != 0) { |
| 1693 | ALOGE("flock(%s) failed: %s\n", out_oat_path, strerror(errno)); |
| 1694 | _exit(67); |
| 1695 | } |
| 1696 | |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1697 | run_dex2oat(input_fd.get(), |
| 1698 | out_oat_fd.get(), |
| 1699 | in_vdex_fd.get(), |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1700 | out_vdex_fd.get(), |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1701 | image_fd.get(), |
Jeff Hao | 10b8a6e | 2017-04-05 17:11:39 -0700 | [diff] [blame] | 1702 | dex_path, |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1703 | out_oat_path, |
| 1704 | swap_fd.get(), |
| 1705 | instruction_set, |
| 1706 | compiler_filter, |
Richard Uhler | 76cc027 | 2016-12-08 10:46:35 +0000 | [diff] [blame] | 1707 | debuggable, |
| 1708 | boot_complete, |
| 1709 | reference_profile_fd.get(), |
Calin Juravle | 52c4582 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 1710 | class_loader_context); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1711 | _exit(68); /* only get here on exec failure */ |
| 1712 | } else { |
| 1713 | int res = wait_child(pid); |
| 1714 | if (res == 0) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1715 | ALOGV("DexInv: --- END '%s' (success) ---\n", dex_path); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1716 | } else { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1717 | ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", dex_path, res); |
Andreas Gampe | 013f02e | 2017-03-20 18:36:54 -0700 | [diff] [blame] | 1718 | return res; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1719 | } |
| 1720 | } |
| 1721 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1722 | update_out_oat_access_times(dex_path, out_oat_path); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1723 | |
| 1724 | // We've been successful, don't delete output. |
| 1725 | out_oat_fd.SetCleanup(false); |
Calin Juravle | 7a570e8 | 2017-01-14 16:23:30 -0800 | [diff] [blame] | 1726 | out_vdex_fd.SetCleanup(false); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1727 | image_fd.SetCleanup(false); |
| 1728 | reference_profile_fd.SetCleanup(false); |
| 1729 | |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1733 | // Try to remove the given directory. Log an error if the directory exists |
| 1734 | // and is empty but could not be removed. |
| 1735 | static bool rmdir_if_empty(const char* dir) { |
| 1736 | if (rmdir(dir) == 0) { |
| 1737 | return true; |
| 1738 | } |
| 1739 | if (errno == ENOENT || errno == ENOTEMPTY) { |
| 1740 | return true; |
| 1741 | } |
| 1742 | PLOG(ERROR) << "Failed to remove dir: " << dir; |
| 1743 | return false; |
| 1744 | } |
| 1745 | |
| 1746 | // Try to unlink the given file. Log an error if the file exists and could not |
| 1747 | // be unlinked. |
| 1748 | static bool unlink_if_exists(const std::string& file) { |
| 1749 | if (unlink(file.c_str()) == 0) { |
| 1750 | return true; |
| 1751 | } |
| 1752 | if (errno == ENOENT) { |
| 1753 | return true; |
| 1754 | |
| 1755 | } |
| 1756 | PLOG(ERROR) << "Could not unlink: " << file; |
| 1757 | return false; |
| 1758 | } |
| 1759 | |
| 1760 | // Create the oat file structure for the secondary dex 'dex_path' and assign |
| 1761 | // the individual path component to the 'out_' parameters. |
| 1762 | static bool create_secondary_dex_oat_layout(const std::string& dex_path, const std::string& isa, |
| 1763 | /*out*/char* out_oat_dir, /*out*/char* out_oat_isa_dir, /*out*/char* out_oat_path) { |
| 1764 | size_t dirIndex = dex_path.rfind('/'); |
| 1765 | if (dirIndex == std::string::npos) { |
| 1766 | LOG(ERROR) << "Unexpected dir structure for dex file " << dex_path; |
| 1767 | return false; |
| 1768 | } |
| 1769 | // TODO(calin): we have similar computations in at lest 3 other places |
| 1770 | // (InstalldNativeService, otapropt and dexopt). Unify them and get rid of snprintf by |
| 1771 | // use string append. |
| 1772 | std::string apk_dir = dex_path.substr(0, dirIndex); |
| 1773 | snprintf(out_oat_dir, PKG_PATH_MAX, "%s/oat", apk_dir.c_str()); |
| 1774 | snprintf(out_oat_isa_dir, PKG_PATH_MAX, "%s/%s", out_oat_dir, isa.c_str()); |
| 1775 | |
| 1776 | if (!create_oat_out_path(dex_path.c_str(), isa.c_str(), out_oat_dir, |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1777 | /*is_secondary_dex*/true, out_oat_path)) { |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1778 | LOG(ERROR) << "Could not create oat path for secondary dex " << dex_path; |
| 1779 | return false; |
| 1780 | } |
| 1781 | return true; |
| 1782 | } |
| 1783 | |
| 1784 | // Reconcile the secondary dex 'dex_path' and its generated oat files. |
| 1785 | // Return true if all the parameters are valid and the secondary dex file was |
| 1786 | // processed successfully (i.e. the dex_path either exists, or if not, its corresponding |
| 1787 | // oat/vdex/art files where deleted successfully). In this case, out_secondary_dex_exists |
| 1788 | // will be true if the secondary dex file still exists. If the secondary dex file does not exist, |
| 1789 | // the method cleans up any previously generated compiler artifacts (oat, vdex, art). |
| 1790 | // Return false if there were errors during processing. In this case |
| 1791 | // out_secondary_dex_exists will be set to false. |
| 1792 | bool reconcile_secondary_dex_file(const std::string& dex_path, |
| 1793 | const std::string& pkgname, int uid, const std::vector<std::string>& isas, |
| 1794 | const std::unique_ptr<std::string>& volume_uuid, int storage_flag, |
| 1795 | /*out*/bool* out_secondary_dex_exists) { |
| 1796 | // Set out to false to start with, just in case we have validation errors. |
| 1797 | *out_secondary_dex_exists = false; |
Calin Juravle | d23dee7 | 2017-07-06 16:29:11 -0700 | [diff] [blame] | 1798 | if (!validate_dex_path_size(dex_path)) { |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1802 | if (isas.size() == 0) { |
| 1803 | LOG(ERROR) << "reconcile_secondary_dex_file called with empty isas vector"; |
| 1804 | return false; |
| 1805 | } |
| 1806 | |
| 1807 | const char* volume_uuid_cstr = volume_uuid == nullptr ? nullptr : volume_uuid->c_str(); |
| 1808 | if (!validate_secondary_dex_path(pkgname.c_str(), dex_path.c_str(), volume_uuid_cstr, |
| 1809 | uid, storage_flag)) { |
| 1810 | LOG(ERROR) << "Could not validate secondary dex path " << dex_path; |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | if (access(dex_path.c_str(), F_OK) == 0) { |
| 1815 | // The path exists, nothing to do. The odex files (if any) will be left untouched. |
| 1816 | *out_secondary_dex_exists = true; |
| 1817 | return true; |
| 1818 | } else if (errno != ENOENT) { |
| 1819 | PLOG(ERROR) << "Failed to check access to secondary dex " << dex_path; |
| 1820 | return false; |
| 1821 | } |
| 1822 | |
| 1823 | // The secondary dex does not exist anymore. Clear any generated files. |
| 1824 | char oat_path[PKG_PATH_MAX]; |
| 1825 | char oat_dir[PKG_PATH_MAX]; |
| 1826 | char oat_isa_dir[PKG_PATH_MAX]; |
| 1827 | bool result = true; |
| 1828 | for (size_t i = 0; i < isas.size(); i++) { |
| 1829 | if (!create_secondary_dex_oat_layout(dex_path, isas[i], oat_dir, oat_isa_dir, oat_path)) { |
| 1830 | LOG(ERROR) << "Could not create secondary odex layout: " << dex_path; |
| 1831 | result = false; |
| 1832 | continue; |
| 1833 | } |
Calin Juravle | 5131409 | 2017-05-18 15:33:05 -0700 | [diff] [blame] | 1834 | |
| 1835 | // Delete oat/vdex/art files. |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1836 | result = unlink_if_exists(oat_path) && result; |
| 1837 | result = unlink_if_exists(create_vdex_filename(oat_path)) && result; |
| 1838 | result = unlink_if_exists(create_image_filename(oat_path)) && result; |
| 1839 | |
Calin Juravle | 5131409 | 2017-05-18 15:33:05 -0700 | [diff] [blame] | 1840 | // Delete profiles. |
| 1841 | std::string current_profile = create_current_profile_path( |
| 1842 | multiuser_get_user_id(uid), dex_path, /*is_secondary*/true); |
| 1843 | std::string reference_profile = create_reference_profile_path( |
| 1844 | dex_path, /*is_secondary*/true); |
| 1845 | result = unlink_if_exists(current_profile) && result; |
| 1846 | result = unlink_if_exists(reference_profile) && result; |
| 1847 | |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame^] | 1848 | // We upgraded once the location of current profile for secondary dex files. |
| 1849 | // Check for any previous left-overs and remove them as well. |
| 1850 | std::string old_current_profile = dex_path + ".prof"; |
| 1851 | result = unlink_if_exists(old_current_profile); |
| 1852 | |
Calin Juravle | c9eab38 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 1853 | // Try removing the directories as well, they might be empty. |
| 1854 | result = rmdir_if_empty(oat_isa_dir) && result; |
| 1855 | result = rmdir_if_empty(oat_dir) && result; |
| 1856 | } |
| 1857 | |
| 1858 | return result; |
| 1859 | } |
| 1860 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1861 | // Helper for move_ab, so that we can have common failure-case cleanup. |
| 1862 | static bool unlink_and_rename(const char* from, const char* to) { |
| 1863 | // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise, |
| 1864 | // return a failure. |
| 1865 | struct stat s; |
| 1866 | if (stat(to, &s) == 0) { |
| 1867 | if (!S_ISREG(s.st_mode)) { |
| 1868 | LOG(ERROR) << from << " is not a regular file to replace for A/B."; |
| 1869 | return false; |
| 1870 | } |
| 1871 | if (unlink(to) != 0) { |
| 1872 | LOG(ERROR) << "Could not unlink " << to << " to move A/B."; |
| 1873 | return false; |
| 1874 | } |
| 1875 | } else { |
| 1876 | // This may be a permission problem. We could investigate the error code, but we'll just |
| 1877 | // let the rename failure do the work for us. |
| 1878 | } |
| 1879 | |
| 1880 | // Try to rename "to" to "from." |
| 1881 | if (rename(from, to) != 0) { |
| 1882 | PLOG(ERROR) << "Could not rename " << from << " to " << to; |
| 1883 | return false; |
| 1884 | } |
| 1885 | return true; |
| 1886 | } |
| 1887 | |
| 1888 | // Move/rename a B artifact (from) to an A artifact (to). |
| 1889 | static bool move_ab_path(const std::string& b_path, const std::string& a_path) { |
| 1890 | // Check whether B exists. |
| 1891 | { |
| 1892 | struct stat s; |
| 1893 | if (stat(b_path.c_str(), &s) != 0) { |
| 1894 | // Silently ignore for now. The service calling this isn't smart enough to understand |
| 1895 | // lack of artifacts at the moment. |
| 1896 | return false; |
| 1897 | } |
| 1898 | if (!S_ISREG(s.st_mode)) { |
| 1899 | LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file."; |
| 1900 | // Try to unlink, but swallow errors. |
| 1901 | unlink(b_path.c_str()); |
| 1902 | return false; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | // Rename B to A. |
| 1907 | if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) { |
| 1908 | // Delete the b_path so we don't try again (or fail earlier). |
| 1909 | if (unlink(b_path.c_str()) != 0) { |
| 1910 | PLOG(ERROR) << "Could not unlink " << b_path; |
| 1911 | } |
| 1912 | |
| 1913 | return false; |
| 1914 | } |
| 1915 | |
| 1916 | return true; |
| 1917 | } |
| 1918 | |
| 1919 | bool move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) { |
| 1920 | // Get the current slot suffix. No suffix, no A/B. |
| 1921 | std::string slot_suffix; |
| 1922 | { |
| 1923 | char buf[kPropertyValueMax]; |
| 1924 | if (get_property("ro.boot.slot_suffix", buf, nullptr) <= 0) { |
| 1925 | return false; |
| 1926 | } |
| 1927 | slot_suffix = buf; |
| 1928 | |
| 1929 | if (!ValidateTargetSlotSuffix(slot_suffix)) { |
| 1930 | LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix; |
| 1931 | return false; |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | // Validate other inputs. |
| 1936 | if (validate_apk_path(apk_path) != 0) { |
| 1937 | LOG(ERROR) << "Invalid apk_path: " << apk_path; |
| 1938 | return false; |
| 1939 | } |
| 1940 | if (validate_apk_path(oat_dir) != 0) { |
| 1941 | LOG(ERROR) << "Invalid oat_dir: " << oat_dir; |
| 1942 | return false; |
| 1943 | } |
| 1944 | |
| 1945 | char a_path[PKG_PATH_MAX]; |
| 1946 | if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) { |
| 1947 | return false; |
| 1948 | } |
| 1949 | const std::string a_vdex_path = create_vdex_filename(a_path); |
| 1950 | const std::string a_image_path = create_image_filename(a_path); |
| 1951 | |
| 1952 | // B path = A path + slot suffix. |
| 1953 | const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str()); |
| 1954 | const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str()); |
| 1955 | const std::string b_image_path = StringPrintf("%s.%s", |
| 1956 | a_image_path.c_str(), |
| 1957 | slot_suffix.c_str()); |
| 1958 | |
| 1959 | bool success = true; |
| 1960 | if (move_ab_path(b_path, a_path)) { |
| 1961 | if (move_ab_path(b_vdex_path, a_vdex_path)) { |
| 1962 | // Note: we can live without an app image. As such, ignore failure to move the image file. |
| 1963 | // If we decide to require the app image, or the app image being moved correctly, |
| 1964 | // then change accordingly. |
| 1965 | constexpr bool kIgnoreAppImageFailure = true; |
| 1966 | |
| 1967 | if (!a_image_path.empty()) { |
| 1968 | if (!move_ab_path(b_image_path, a_image_path)) { |
| 1969 | unlink(a_image_path.c_str()); |
| 1970 | if (!kIgnoreAppImageFailure) { |
| 1971 | success = false; |
| 1972 | } |
| 1973 | } |
| 1974 | } |
| 1975 | } else { |
| 1976 | // Cleanup: delete B image, ignore errors. |
| 1977 | unlink(b_image_path.c_str()); |
| 1978 | success = false; |
| 1979 | } |
| 1980 | } else { |
| 1981 | // Cleanup: delete B image, ignore errors. |
| 1982 | unlink(b_vdex_path.c_str()); |
| 1983 | unlink(b_image_path.c_str()); |
| 1984 | success = false; |
| 1985 | } |
| 1986 | return success; |
| 1987 | } |
| 1988 | |
| 1989 | bool delete_odex(const char* apk_path, const char* instruction_set, const char* oat_dir) { |
| 1990 | // Delete the oat/odex file. |
| 1991 | char out_path[PKG_PATH_MAX]; |
Calin Juravle | 80a2125 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1992 | if (!create_oat_out_path(apk_path, instruction_set, oat_dir, |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1993 | /*is_secondary_dex*/false, out_path)) { |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1994 | return false; |
| 1995 | } |
| 1996 | |
| 1997 | // In case of a permission failure report the issue. Otherwise just print a warning. |
| 1998 | auto unlink_and_check = [](const char* path) -> bool { |
| 1999 | int result = unlink(path); |
| 2000 | if (result != 0) { |
| 2001 | if (errno == EACCES || errno == EPERM) { |
| 2002 | PLOG(ERROR) << "Could not unlink " << path; |
| 2003 | return false; |
| 2004 | } |
| 2005 | PLOG(WARNING) << "Could not unlink " << path; |
| 2006 | } |
| 2007 | return true; |
| 2008 | }; |
| 2009 | |
| 2010 | // Delete the oat/odex file. |
| 2011 | bool return_value_oat = unlink_and_check(out_path); |
| 2012 | |
| 2013 | // Derive and delete the app image. |
| 2014 | bool return_value_art = unlink_and_check(create_image_filename(out_path).c_str()); |
| 2015 | |
Nicolas Geoffray | 192fb96 | 2017-05-25 13:58:06 +0100 | [diff] [blame] | 2016 | // Derive and delete the vdex file. |
| 2017 | bool return_value_vdex = unlink_and_check(create_vdex_filename(out_path).c_str()); |
| 2018 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2019 | // Report success. |
Nicolas Geoffray | 192fb96 | 2017-05-25 13:58:06 +0100 | [diff] [blame] | 2020 | return return_value_oat && return_value_art && return_value_vdex; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2023 | } // namespace installd |
| 2024 | } // namespace android |