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