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