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