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