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