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