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