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