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