| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | ** Copyright 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 | */ | 
|  | 16 |  | 
|  | 17 | #include <algorithm> | 
|  | 18 | #include <inttypes.h> | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 19 | #include <limits> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 20 | #include <random> | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 21 | #include <regex> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 22 | #include <selinux/android.h> | 
|  | 23 | #include <selinux/avc.h> | 
|  | 24 | #include <stdlib.h> | 
|  | 25 | #include <string.h> | 
|  | 26 | #include <sys/capability.h> | 
|  | 27 | #include <sys/prctl.h> | 
|  | 28 | #include <sys/stat.h> | 
|  | 29 | #include <sys/wait.h> | 
|  | 30 |  | 
|  | 31 | #include <android-base/logging.h> | 
|  | 32 | #include <android-base/macros.h> | 
|  | 33 | #include <android-base/stringprintf.h> | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 34 | #include <android-base/strings.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 35 | #include <cutils/fs.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 36 | #include <cutils/properties.h> | 
| Andreas Gampe | 54e1a40 | 2017-03-20 18:42:49 -0700 | [diff] [blame] | 37 | #include <dex2oat_return_codes.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 38 | #include <log/log.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> | 
|  | 40 |  | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 41 | #include "dexopt.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 42 | #include "file_parsing.h" | 
|  | 43 | #include "globals.h" | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 44 | #include "installd_constants.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 45 | #include "installd_deps.h"  // Need to fill in requirements of commands. | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 46 | #include "otapreopt_parameters.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 47 | #include "otapreopt_utils.h" | 
|  | 48 | #include "system_properties.h" | 
|  | 49 | #include "utils.h" | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 50 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 51 | #ifndef LOG_TAG | 
|  | 52 | #define LOG_TAG "otapreopt" | 
|  | 53 | #endif | 
|  | 54 |  | 
|  | 55 | #define BUFFER_MAX    1024  /* input buffer for commands */ | 
|  | 56 | #define TOKEN_MAX     16    /* max number of arguments in buffer */ | 
|  | 57 | #define REPLY_MAX     256   /* largest reply allowed */ | 
|  | 58 |  | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 59 | using android::base::EndsWith; | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 60 | using android::base::Join; | 
|  | 61 | using android::base::Split; | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 62 | using android::base::StartsWith; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 63 | using android::base::StringPrintf; | 
|  | 64 |  | 
|  | 65 | namespace android { | 
|  | 66 | namespace installd { | 
|  | 67 |  | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 68 | // Check expected values for dexopt flags. If you need to change this: | 
|  | 69 | // | 
|  | 70 | //   RUN AN A/B OTA TO MAKE SURE THINGS STILL WORK! | 
|  | 71 | // | 
|  | 72 | // You most likely need to increase the protocol version and all that entails! | 
|  | 73 |  | 
|  | 74 | static_assert(DEXOPT_PUBLIC         == 1 << 1, "DEXOPT_PUBLIC unexpected."); | 
|  | 75 | static_assert(DEXOPT_DEBUGGABLE     == 1 << 2, "DEXOPT_DEBUGGABLE unexpected."); | 
|  | 76 | static_assert(DEXOPT_BOOTCOMPLETE   == 1 << 3, "DEXOPT_BOOTCOMPLETE unexpected."); | 
|  | 77 | static_assert(DEXOPT_PROFILE_GUIDED == 1 << 4, "DEXOPT_PROFILE_GUIDED unexpected."); | 
|  | 78 | static_assert(DEXOPT_SECONDARY_DEX  == 1 << 5, "DEXOPT_SECONDARY_DEX unexpected."); | 
|  | 79 | static_assert(DEXOPT_FORCE          == 1 << 6, "DEXOPT_FORCE unexpected."); | 
|  | 80 | static_assert(DEXOPT_STORAGE_CE     == 1 << 7, "DEXOPT_STORAGE_CE unexpected."); | 
|  | 81 | static_assert(DEXOPT_STORAGE_DE     == 1 << 8, "DEXOPT_STORAGE_DE unexpected."); | 
| David Brazdil | 22cce5a | 2018-02-12 18:04:59 -0800 | [diff] [blame] | 82 | static_assert(DEXOPT_ENABLE_HIDDEN_API_CHECKS == 1 << 10, | 
|  | 83 | "DEXOPT_ENABLE_HIDDEN_API_CHECKS unexpected"); | 
| Mathieu Chartier | 351bc94 | 2018-03-06 13:55:58 -0800 | [diff] [blame] | 84 | static_assert(DEXOPT_GENERATE_COMPACT_DEX == 1 << 11, "DEXOPT_GENERATE_COMPACT_DEX unexpected"); | 
| Mathieu Chartier | ad45a1b | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 85 | static_assert(DEXOPT_GENERATE_APP_IMAGE == 1 << 12, "DEXOPT_GENERATE_APP_IMAGE unexpected"); | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 86 |  | 
| Mathieu Chartier | ad45a1b | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 87 | static_assert(DEXOPT_MASK           == (0x1dfe | DEXOPT_IDLE_BACKGROUND_JOB), | 
| Andreas Gampe | d32eec2 | 2018-02-28 16:02:51 -0800 | [diff] [blame] | 88 | "DEXOPT_MASK unexpected."); | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 89 |  | 
|  | 90 |  | 
| Andreas Gampe | a64a627 | 2018-07-10 10:43:47 -0700 | [diff] [blame] | 91 | template<typename T> | 
|  | 92 | static constexpr bool IsPowerOfTwo(T x) { | 
|  | 93 | static_assert(std::is_integral<T>::value, "T must be integral"); | 
|  | 94 | // TODO: assert unsigned. There is currently many uses with signed values. | 
|  | 95 | return (x & (x - 1)) == 0; | 
|  | 96 | } | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 97 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 98 | template<typename T> | 
|  | 99 | static constexpr T RoundDown(T x, typename std::decay<T>::type n) { | 
|  | 100 | return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | template<typename T> | 
|  | 104 | static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { | 
|  | 105 | return RoundDown(x + n - 1, n); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | class OTAPreoptService { | 
|  | 109 | public: | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 110 | // Main driver. Performs the following steps. | 
|  | 111 | // | 
|  | 112 | // 1) Parse options (read system properties etc from B partition). | 
|  | 113 | // | 
|  | 114 | // 2) Read in package data. | 
|  | 115 | // | 
|  | 116 | // 3) Prepare environment variables. | 
|  | 117 | // | 
|  | 118 | // 4) Prepare(compile) boot image, if necessary. | 
|  | 119 | // | 
|  | 120 | // 5) Run update. | 
|  | 121 | int Main(int argc, char** argv) { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 122 | if (!ReadArguments(argc, argv)) { | 
|  | 123 | LOG(ERROR) << "Failed reading command line."; | 
|  | 124 | return 1; | 
|  | 125 | } | 
|  | 126 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 127 | if (!ReadSystemProperties()) { | 
|  | 128 | LOG(ERROR)<< "Failed reading system properties."; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 129 | return 2; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | if (!ReadEnvironment()) { | 
|  | 133 | LOG(ERROR) << "Failed reading environment properties."; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 134 | return 3; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 137 | if (!CheckAndInitializeInstalldGlobals()) { | 
|  | 138 | LOG(ERROR) << "Failed initializing globals."; | 
|  | 139 | return 4; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | PrepareEnvironment(); | 
|  | 143 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 144 | if (!PrepareBootImage(/* force */ false)) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 145 | LOG(ERROR) << "Failed preparing boot image."; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 146 | return 5; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
|  | 149 | int dexopt_retcode = RunPreopt(); | 
|  | 150 |  | 
|  | 151 | return dexopt_retcode; | 
|  | 152 | } | 
|  | 153 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 154 | int GetProperty(const char* key, char* value, const char* default_value) const { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 155 | const std::string* prop_value = system_properties_.GetProperty(key); | 
|  | 156 | if (prop_value == nullptr) { | 
|  | 157 | if (default_value == nullptr) { | 
|  | 158 | return 0; | 
|  | 159 | } | 
|  | 160 | // Copy in the default value. | 
| Jeff Sharkey | 1b9d9a6 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 161 | strlcpy(value, default_value, kPropertyValueMax - 1); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 162 | value[kPropertyValueMax - 1] = 0; | 
|  | 163 | return strlen(default_value);// TODO: Need to truncate? | 
|  | 164 | } | 
| Andreas Gampe | 5696e63 | 2017-09-26 20:41:48 -0700 | [diff] [blame] | 165 | size_t size = std::min(kPropertyValueMax - 1, prop_value->length()) + 1; | 
| Jeff Sharkey | 1b9d9a6 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 166 | strlcpy(value, prop_value->data(), size); | 
| Andreas Gampe | 5696e63 | 2017-09-26 20:41:48 -0700 | [diff] [blame] | 167 | return static_cast<int>(size - 1); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 170 | std::string GetOTADataDirectory() const { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 171 | return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), GetTargetSlot().c_str()); | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | const std::string& GetTargetSlot() const { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 175 | return parameters_.target_slot; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 178 | private: | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 179 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 180 | bool ReadSystemProperties() { | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 181 | static constexpr const char* kPropertyFiles[] = { | 
|  | 182 | "/default.prop", "/system/build.prop" | 
|  | 183 | }; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 184 |  | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 185 | for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) { | 
|  | 186 | if (!system_properties_.Load(kPropertyFiles[i])) { | 
|  | 187 | return false; | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | return true; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
|  | 194 | bool ReadEnvironment() { | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 195 | // Parse the environment variables from init.environ.rc, which have the form | 
|  | 196 | //   export NAME VALUE | 
|  | 197 | // For simplicity, don't respect string quotation. The values we are interested in can be | 
|  | 198 | // encoded without them. | 
|  | 199 | std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)"); | 
|  | 200 | bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) { | 
|  | 201 | std::smatch export_match; | 
|  | 202 | if (!std::regex_match(line, export_match, export_regex)) { | 
|  | 203 | return true; | 
|  | 204 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 205 |  | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 206 | if (export_match.size() != 3) { | 
|  | 207 | return true; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | std::string name = export_match[1].str(); | 
|  | 211 | std::string value = export_match[2].str(); | 
|  | 212 |  | 
|  | 213 | system_properties_.SetProperty(name, value); | 
|  | 214 |  | 
|  | 215 | return true; | 
|  | 216 | }); | 
|  | 217 | if (!parse_result) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 218 | return false; | 
|  | 219 | } | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 220 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 221 | if (system_properties_.GetProperty(kAndroidDataPathPropertyName) == nullptr) { | 
|  | 222 | return false; | 
|  | 223 | } | 
|  | 224 | android_data_ = *system_properties_.GetProperty(kAndroidDataPathPropertyName); | 
|  | 225 |  | 
|  | 226 | if (system_properties_.GetProperty(kAndroidRootPathPropertyName) == nullptr) { | 
|  | 227 | return false; | 
|  | 228 | } | 
|  | 229 | android_root_ = *system_properties_.GetProperty(kAndroidRootPathPropertyName); | 
|  | 230 |  | 
|  | 231 | if (system_properties_.GetProperty(kBootClassPathPropertyName) == nullptr) { | 
|  | 232 | return false; | 
|  | 233 | } | 
|  | 234 | boot_classpath_ = *system_properties_.GetProperty(kBootClassPathPropertyName); | 
|  | 235 |  | 
|  | 236 | if (system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) == nullptr) { | 
|  | 237 | return false; | 
|  | 238 | } | 
|  | 239 | asec_mountpoint_ = *system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME); | 
|  | 240 |  | 
|  | 241 | return true; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | const std::string& GetAndroidData() const { | 
|  | 245 | return android_data_; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | const std::string& GetAndroidRoot() const { | 
|  | 249 | return android_root_; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | const std::string GetOtaDirectoryPrefix() const { | 
|  | 253 | return GetAndroidData() + "/ota"; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | bool CheckAndInitializeInstalldGlobals() { | 
|  | 257 | // init_globals_from_data_and_root requires "ASEC_MOUNTPOINT" in the environment. We | 
|  | 258 | // do not use any datapath that includes this, but we'll still have to set it. | 
|  | 259 | CHECK(system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) != nullptr); | 
|  | 260 | int result = setenv(ASEC_MOUNTPOINT_ENV_NAME, asec_mountpoint_.c_str(), 0); | 
|  | 261 | if (result != 0) { | 
|  | 262 | LOG(ERROR) << "Could not set ASEC_MOUNTPOINT environment variable"; | 
|  | 263 | return false; | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | if (!init_globals_from_data_and_root(GetAndroidData().c_str(), GetAndroidRoot().c_str())) { | 
|  | 267 | LOG(ERROR) << "Could not initialize globals; exiting."; | 
|  | 268 | return false; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | // This is different from the normal installd. We only do the base | 
|  | 272 | // directory, the rest will be created on demand when each app is compiled. | 
|  | 273 | if (access(GetOtaDirectoryPrefix().c_str(), R_OK) < 0) { | 
|  | 274 | LOG(ERROR) << "Could not access " << GetOtaDirectoryPrefix(); | 
|  | 275 | return false; | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 276 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 277 |  | 
|  | 278 | return true; | 
|  | 279 | } | 
|  | 280 |  | 
| Shubham Ajmera | 45c8743 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 281 | bool ParseBool(const char* in) { | 
|  | 282 | if (strcmp(in, "true") == 0) { | 
|  | 283 | return true; | 
|  | 284 | } | 
|  | 285 | return false; | 
|  | 286 | } | 
|  | 287 |  | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 288 | bool ParseUInt(const char* in, uint32_t* out) { | 
|  | 289 | char* end; | 
|  | 290 | long long int result = strtoll(in, &end, 0); | 
|  | 291 | if (in == end || *end != '\0') { | 
|  | 292 | return false; | 
|  | 293 | } | 
|  | 294 | if (result < std::numeric_limits<uint32_t>::min() || | 
|  | 295 | std::numeric_limits<uint32_t>::max() < result) { | 
|  | 296 | return false; | 
|  | 297 | } | 
|  | 298 | *out = static_cast<uint32_t>(result); | 
|  | 299 | return true; | 
|  | 300 | } | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 301 |  | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 302 | bool ReadArguments(int argc, char** argv) { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 303 | return parameters_.ReadArguments(argc, const_cast<const char**>(argv)); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
|  | 306 | void PrepareEnvironment() { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 307 | environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_classpath_.c_str())); | 
|  | 308 | environ_.push_back(StringPrintf("ANDROID_DATA=%s", GetOTADataDirectory().c_str())); | 
|  | 309 | environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root_.c_str())); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 310 |  | 
|  | 311 | for (const std::string& e : environ_) { | 
|  | 312 | putenv(const_cast<char*>(e.c_str())); | 
|  | 313 | } | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | // Ensure that we have the right boot image. The first time any app is | 
|  | 317 | // compiled, we'll try to generate it. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 318 | bool PrepareBootImage(bool force) const { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 319 | if (parameters_.instruction_set == nullptr) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 320 | LOG(ERROR) << "Instruction set missing."; | 
|  | 321 | return false; | 
|  | 322 | } | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 323 | const char* isa = parameters_.instruction_set; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 324 |  | 
|  | 325 | // Check whether the file exists where expected. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 326 | std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 327 | std::string isa_path = dalvik_cache + "/" + isa; | 
|  | 328 | std::string art_path = isa_path + "/system@framework@boot.art"; | 
|  | 329 | std::string oat_path = isa_path + "/system@framework@boot.oat"; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 330 | bool cleared = false; | 
|  | 331 | if (access(art_path.c_str(), F_OK) == 0 && access(oat_path.c_str(), F_OK) == 0) { | 
|  | 332 | // Files exist, assume everything is alright if not forced. Otherwise clean up. | 
|  | 333 | if (!force) { | 
|  | 334 | return true; | 
|  | 335 | } | 
|  | 336 | ClearDirectory(isa_path); | 
|  | 337 | cleared = true; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 340 | // Reset umask in otapreopt, so that we control the the access for the files we create. | 
|  | 341 | umask(0); | 
|  | 342 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 343 | // Create the directories, if necessary. | 
|  | 344 | if (access(dalvik_cache.c_str(), F_OK) != 0) { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 345 | if (!CreatePath(dalvik_cache)) { | 
|  | 346 | PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 347 | return false; | 
|  | 348 | } | 
|  | 349 | } | 
|  | 350 | if (access(isa_path.c_str(), F_OK) != 0) { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 351 | if (!CreatePath(isa_path)) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 352 | PLOG(ERROR) << "Could not create dalvik-cache isa dir"; | 
|  | 353 | return false; | 
|  | 354 | } | 
|  | 355 | } | 
|  | 356 |  | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 357 | // Prepare to create. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 358 | if (!cleared) { | 
|  | 359 | ClearDirectory(isa_path); | 
|  | 360 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 361 |  | 
| Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 362 | std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa); | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 363 | if (access(preopted_boot_art_path.c_str(), F_OK) == 0) { | 
| Chris Morin | 77c4875 | 2018-02-13 15:44:47 -0800 | [diff] [blame] | 364 | return PatchoatBootImage(isa_path, isa); | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 365 | } else { | 
|  | 366 | // No preopted boot image. Try to compile. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 367 | return Dex2oatBootImage(boot_classpath_, art_path, oat_path, isa); | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 371 | static bool CreatePath(const std::string& path) { | 
|  | 372 | // Create the given path. Use string processing instead of dirname, as dirname's need for | 
|  | 373 | // a writable char buffer is painful. | 
|  | 374 |  | 
|  | 375 | // First, try to use the full path. | 
|  | 376 | if (mkdir(path.c_str(), 0711) == 0) { | 
|  | 377 | return true; | 
|  | 378 | } | 
|  | 379 | if (errno != ENOENT) { | 
|  | 380 | PLOG(ERROR) << "Could not create path " << path; | 
|  | 381 | return false; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | // Now find the parent and try that first. | 
|  | 385 | size_t last_slash = path.find_last_of('/'); | 
|  | 386 | if (last_slash == std::string::npos || last_slash == 0) { | 
|  | 387 | PLOG(ERROR) << "Could not create " << path; | 
|  | 388 | return false; | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | if (!CreatePath(path.substr(0, last_slash))) { | 
|  | 392 | return false; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | if (mkdir(path.c_str(), 0711) == 0) { | 
|  | 396 | return true; | 
|  | 397 | } | 
|  | 398 | PLOG(ERROR) << "Could not create " << path; | 
|  | 399 | return false; | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | static void ClearDirectory(const std::string& dir) { | 
|  | 403 | DIR* c_dir = opendir(dir.c_str()); | 
|  | 404 | if (c_dir == nullptr) { | 
|  | 405 | PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents"; | 
|  | 406 | return; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) { | 
|  | 410 | const char* name = de->d_name; | 
|  | 411 | if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { | 
|  | 412 | continue; | 
|  | 413 | } | 
|  | 414 | // We only want to delete regular files and symbolic links. | 
|  | 415 | std::string file = StringPrintf("%s/%s", dir.c_str(), name); | 
|  | 416 | if (de->d_type != DT_REG && de->d_type != DT_LNK) { | 
|  | 417 | LOG(WARNING) << "Unexpected file " | 
|  | 418 | << file | 
|  | 419 | << " of type " | 
|  | 420 | << std::hex | 
|  | 421 | << de->d_type | 
|  | 422 | << " encountered."; | 
|  | 423 | } else { | 
|  | 424 | // Try to unlink the file. | 
|  | 425 | if (unlink(file.c_str()) != 0) { | 
|  | 426 | PLOG(ERROR) << "Unable to unlink " << file; | 
|  | 427 | } | 
|  | 428 | } | 
|  | 429 | } | 
|  | 430 | CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory."; | 
|  | 431 | } | 
|  | 432 |  | 
| Chris Morin | 77c4875 | 2018-02-13 15:44:47 -0800 | [diff] [blame] | 433 | bool PatchoatBootImage(const std::string& output_dir, const char* isa) const { | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 434 | // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc. | 
|  | 435 |  | 
|  | 436 | std::vector<std::string> cmd; | 
| Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 437 | cmd.push_back("/system/bin/patchoat"); | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 438 |  | 
|  | 439 | cmd.push_back("--input-image-location=/system/framework/boot.art"); | 
| Chris Morin | 77c4875 | 2018-02-13 15:44:47 -0800 | [diff] [blame] | 440 | cmd.push_back(StringPrintf("--output-image-directory=%s", output_dir.c_str())); | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 441 |  | 
|  | 442 | cmd.push_back(StringPrintf("--instruction-set=%s", isa)); | 
|  | 443 |  | 
|  | 444 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, | 
|  | 445 | ART_BASE_ADDRESS_MAX_DELTA); | 
| Andreas Gampe | febf0bf | 2016-02-29 18:04:17 -0800 | [diff] [blame] | 446 | cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset)); | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 447 |  | 
|  | 448 | std::string error_msg; | 
|  | 449 | bool result = Exec(cmd, &error_msg); | 
|  | 450 | if (!result) { | 
|  | 451 | LOG(ERROR) << "Could not generate boot image: " << error_msg; | 
|  | 452 | } | 
|  | 453 | return result; | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | bool Dex2oatBootImage(const std::string& boot_cp, | 
|  | 457 | const std::string& art_path, | 
|  | 458 | const std::string& oat_path, | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 459 | const char* isa) const { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 460 | // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc. | 
|  | 461 | std::vector<std::string> cmd; | 
| Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 462 | cmd.push_back("/system/bin/dex2oat"); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 463 | cmd.push_back(StringPrintf("--image=%s", art_path.c_str())); | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 464 | for (const std::string& boot_part : Split(boot_cp, ":")) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 465 | cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str())); | 
|  | 466 | } | 
|  | 467 | cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str())); | 
|  | 468 |  | 
|  | 469 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, | 
|  | 470 | ART_BASE_ADDRESS_MAX_DELTA); | 
|  | 471 | cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset)); | 
|  | 472 |  | 
|  | 473 | cmd.push_back(StringPrintf("--instruction-set=%s", isa)); | 
|  | 474 |  | 
|  | 475 | // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp. | 
|  | 476 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms", | 
|  | 477 | "-Xms", | 
|  | 478 | true, | 
|  | 479 | cmd); | 
|  | 480 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx", | 
|  | 481 | "-Xmx", | 
|  | 482 | true, | 
|  | 483 | cmd); | 
|  | 484 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter", | 
|  | 485 | "--compiler-filter=", | 
|  | 486 | false, | 
|  | 487 | cmd); | 
| Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 488 | cmd.push_back("--image-classes=/system/etc/preloaded-classes"); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 489 | // TODO: Compiled-classes. | 
|  | 490 | const std::string* extra_opts = | 
|  | 491 | system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags"); | 
|  | 492 | if (extra_opts != nullptr) { | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 493 | std::vector<std::string> extra_vals = Split(*extra_opts, " "); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 494 | cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end()); | 
|  | 495 | } | 
|  | 496 | // TODO: Should we lower this? It's usually set close to max, because | 
|  | 497 | //       normally there's not much else going on at boot. | 
|  | 498 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads", | 
|  | 499 | "-j", | 
|  | 500 | false, | 
|  | 501 | cmd); | 
|  | 502 | AddCompilerOptionFromSystemProperty( | 
|  | 503 | StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(), | 
|  | 504 | "--instruction-set-variant=", | 
|  | 505 | false, | 
|  | 506 | cmd); | 
|  | 507 | AddCompilerOptionFromSystemProperty( | 
|  | 508 | StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(), | 
|  | 509 | "--instruction-set-features=", | 
|  | 510 | false, | 
|  | 511 | cmd); | 
|  | 512 |  | 
|  | 513 | std::string error_msg; | 
|  | 514 | bool result = Exec(cmd, &error_msg); | 
|  | 515 | if (!result) { | 
|  | 516 | LOG(ERROR) << "Could not generate boot image: " << error_msg; | 
|  | 517 | } | 
|  | 518 | return result; | 
|  | 519 | } | 
|  | 520 |  | 
|  | 521 | static const char* ParseNull(const char* arg) { | 
|  | 522 | return (strcmp(arg, "!") == 0) ? nullptr : arg; | 
|  | 523 | } | 
|  | 524 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 525 | bool ShouldSkipPreopt() const { | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 526 | // There's one thing we have to be careful about: we may/will be asked to compile an app | 
|  | 527 | // living in the system image. This may be a valid request - if the app wasn't compiled, | 
|  | 528 | // e.g., if the system image wasn't large enough to include preopted files. However, the | 
|  | 529 | // data we have is from the old system, so the driver (the OTA service) can't actually | 
|  | 530 | // know. Thus, we will get requests for apps that have preopted components. To avoid | 
|  | 531 | // duplication (we'd generate files that are not used and are *not* cleaned up), do two | 
|  | 532 | // simple checks: | 
|  | 533 | // | 
|  | 534 | // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image) | 
|  | 535 | //    (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.) | 
|  | 536 | // | 
|  | 537 | // 2) If you replace the name in the apk_path with "oat," does the path exist? | 
|  | 538 | //    (=have a subdirectory for preopted files) | 
|  | 539 | // | 
|  | 540 | // If the answer to both is yes, skip the dexopt. | 
|  | 541 | // | 
|  | 542 | // Note: while one may think it's OK to call dexopt and it will fail (because APKs should | 
|  | 543 | //       be stripped), that's not true for APKs signed outside the build system (so the | 
|  | 544 | //       jar content must be exactly the same). | 
|  | 545 |  | 
|  | 546 | //       (This is ugly as it's the only thing where we need to understand the contents | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 547 | //        of parameters_, but it beats postponing the decision or using the call- | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 548 | //        backs to do weird things.) | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 549 | const char* apk_path = parameters_.apk_path; | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 550 | CHECK(apk_path != nullptr); | 
| Elliott Hughes | 969e4f8 | 2017-12-20 12:34:09 -0800 | [diff] [blame] | 551 | if (StartsWith(apk_path, android_root_)) { | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 552 | const char* last_slash = strrchr(apk_path, '/'); | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 553 | if (last_slash != nullptr) { | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 554 | std::string path(apk_path, last_slash - apk_path + 1); | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 555 | CHECK(EndsWith(path, "/")); | 
|  | 556 | path = path + "oat"; | 
|  | 557 | if (access(path.c_str(), F_OK) == 0) { | 
| Calin Juravle | b3591f6 | 2017-11-17 16:38:17 -0800 | [diff] [blame] | 558 | LOG(INFO) << "Skipping A/B OTA preopt of already preopted package " << apk_path; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 559 | return true; | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 560 | } | 
|  | 561 | } | 
|  | 562 | } | 
|  | 563 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 564 | // Another issue is unavailability of files in the new system. If the partition | 
|  | 565 | // layout changes, otapreopt_chroot may not know about this. Then files from that | 
|  | 566 | // partition will not be available and fail to build. This is problematic, as | 
|  | 567 | // this tool will wipe the OTA artifact cache and try again (for robustness after | 
|  | 568 | // a failed OTA with remaining cache artifacts). | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 569 | if (access(apk_path, F_OK) != 0) { | 
| Calin Juravle | b3591f6 | 2017-11-17 16:38:17 -0800 | [diff] [blame] | 570 | LOG(WARNING) << "Skipping A/B OTA preopt of non-existing package " << apk_path; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 571 | return true; | 
|  | 572 | } | 
|  | 573 |  | 
|  | 574 | return false; | 
|  | 575 | } | 
|  | 576 |  | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 577 | // Run dexopt with the parameters of parameters_. | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 578 | // TODO(calin): embed the profile name in the parameters. | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 579 | int Dexopt() { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 580 | std::string dummy; | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 581 | return dexopt(parameters_.apk_path, | 
|  | 582 | parameters_.uid, | 
|  | 583 | parameters_.pkgName, | 
|  | 584 | parameters_.instruction_set, | 
|  | 585 | parameters_.dexopt_needed, | 
|  | 586 | parameters_.oat_dir, | 
|  | 587 | parameters_.dexopt_flags, | 
|  | 588 | parameters_.compiler_filter, | 
|  | 589 | parameters_.volume_uuid, | 
|  | 590 | parameters_.shared_libraries, | 
|  | 591 | parameters_.se_info, | 
|  | 592 | parameters_.downgrade, | 
|  | 593 | parameters_.target_sdk_version, | 
| Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 594 | parameters_.profile_name, | 
| Calin Juravle | dcccd83 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 595 | parameters_.dex_metadata_path, | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 596 | parameters_.compilation_reason, | 
|  | 597 | &dummy); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 600 | int RunPreopt() { | 
|  | 601 | if (ShouldSkipPreopt()) { | 
|  | 602 | return 0; | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | int dexopt_result = Dexopt(); | 
|  | 606 | if (dexopt_result == 0) { | 
|  | 607 | return 0; | 
|  | 608 | } | 
|  | 609 |  | 
|  | 610 | // If the dexopt failed, we may have a stale boot image from a previous OTA run. | 
|  | 611 | // Then regenerate and retry. | 
|  | 612 | if (WEXITSTATUS(dexopt_result) == | 
|  | 613 | static_cast<int>(art::dex2oat::ReturnCode::kCreateRuntime)) { | 
|  | 614 | if (!PrepareBootImage(/* force */ true)) { | 
|  | 615 | LOG(ERROR) << "Forced boot image creating failed. Original error return was " | 
|  | 616 | << dexopt_result; | 
|  | 617 | return dexopt_result; | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | int dexopt_result_boot_image_retry = Dexopt(); | 
|  | 621 | if (dexopt_result_boot_image_retry == 0) { | 
|  | 622 | return 0; | 
|  | 623 | } | 
|  | 624 | } | 
|  | 625 |  | 
|  | 626 | // If this was a profile-guided run, we may have profile version issues. Try to downgrade, | 
|  | 627 | // if possible. | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 628 | if ((parameters_.dexopt_flags & DEXOPT_PROFILE_GUIDED) == 0) { | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 629 | return dexopt_result; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | LOG(WARNING) << "Downgrading compiler filter in an attempt to progress compilation"; | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 633 | parameters_.dexopt_flags &= ~DEXOPT_PROFILE_GUIDED; | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 634 | return Dexopt(); | 
|  | 635 | } | 
|  | 636 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 637 | //////////////////////////////////// | 
|  | 638 | // Helpers, mostly taken from ART // | 
|  | 639 | //////////////////////////////////// | 
|  | 640 |  | 
|  | 641 | // Wrapper on fork/execv to run a command in a subprocess. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 642 | static bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) { | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 643 | const std::string command_line = Join(arg_vector, ' '); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 644 |  | 
|  | 645 | CHECK_GE(arg_vector.size(), 1U) << command_line; | 
|  | 646 |  | 
|  | 647 | // Convert the args to char pointers. | 
|  | 648 | const char* program = arg_vector[0].c_str(); | 
|  | 649 | std::vector<char*> args; | 
|  | 650 | for (size_t i = 0; i < arg_vector.size(); ++i) { | 
|  | 651 | const std::string& arg = arg_vector[i]; | 
|  | 652 | char* arg_str = const_cast<char*>(arg.c_str()); | 
|  | 653 | CHECK(arg_str != nullptr) << i; | 
|  | 654 | args.push_back(arg_str); | 
|  | 655 | } | 
|  | 656 | args.push_back(nullptr); | 
|  | 657 |  | 
|  | 658 | // Fork and exec. | 
|  | 659 | pid_t pid = fork(); | 
|  | 660 | if (pid == 0) { | 
|  | 661 | // No allocation allowed between fork and exec. | 
|  | 662 |  | 
|  | 663 | // Change process groups, so we don't get reaped by ProcessManager. | 
|  | 664 | setpgid(0, 0); | 
|  | 665 |  | 
|  | 666 | execv(program, &args[0]); | 
|  | 667 |  | 
|  | 668 | PLOG(ERROR) << "Failed to execv(" << command_line << ")"; | 
|  | 669 | // _exit to avoid atexit handlers in child. | 
|  | 670 | _exit(1); | 
|  | 671 | } else { | 
|  | 672 | if (pid == -1) { | 
|  | 673 | *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", | 
|  | 674 | command_line.c_str(), strerror(errno)); | 
|  | 675 | return false; | 
|  | 676 | } | 
|  | 677 |  | 
|  | 678 | // wait for subprocess to finish | 
|  | 679 | int status; | 
|  | 680 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); | 
|  | 681 | if (got_pid != pid) { | 
|  | 682 | *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " | 
|  | 683 | "wanted %d, got %d: %s", | 
|  | 684 | command_line.c_str(), pid, got_pid, strerror(errno)); | 
|  | 685 | return false; | 
|  | 686 | } | 
|  | 687 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { | 
|  | 688 | *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", | 
|  | 689 | command_line.c_str()); | 
|  | 690 | return false; | 
|  | 691 | } | 
|  | 692 | } | 
|  | 693 | return true; | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc. | 
|  | 697 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { | 
|  | 698 | constexpr size_t kPageSize = PAGE_SIZE; | 
|  | 699 | CHECK_EQ(min_delta % kPageSize, 0u); | 
|  | 700 | CHECK_EQ(max_delta % kPageSize, 0u); | 
|  | 701 | CHECK_LT(min_delta, max_delta); | 
|  | 702 |  | 
|  | 703 | std::default_random_engine generator; | 
|  | 704 | generator.seed(GetSeed()); | 
|  | 705 | std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta); | 
|  | 706 | int32_t r = distribution(generator); | 
|  | 707 | if (r % 2 == 0) { | 
|  | 708 | r = RoundUp(r, kPageSize); | 
|  | 709 | } else { | 
|  | 710 | r = RoundDown(r, kPageSize); | 
|  | 711 | } | 
|  | 712 | CHECK_LE(min_delta, r); | 
|  | 713 | CHECK_GE(max_delta, r); | 
|  | 714 | CHECK_EQ(r % kPageSize, 0u); | 
|  | 715 | return r; | 
|  | 716 | } | 
|  | 717 |  | 
|  | 718 | static uint64_t GetSeed() { | 
|  | 719 | #ifdef __BIONIC__ | 
|  | 720 | // Bionic exposes arc4random, use it. | 
|  | 721 | uint64_t random_data; | 
|  | 722 | arc4random_buf(&random_data, sizeof(random_data)); | 
|  | 723 | return random_data; | 
|  | 724 | #else | 
|  | 725 | #error "This is only supposed to run with bionic. Otherwise, implement..." | 
|  | 726 | #endif | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | void AddCompilerOptionFromSystemProperty(const char* system_property, | 
|  | 730 | const char* prefix, | 
|  | 731 | bool runtime, | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 732 | std::vector<std::string>& out) const { | 
|  | 733 | const std::string* value = system_properties_.GetProperty(system_property); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 734 | if (value != nullptr) { | 
|  | 735 | if (runtime) { | 
|  | 736 | out.push_back("--runtime-arg"); | 
|  | 737 | } | 
|  | 738 | if (prefix != nullptr) { | 
|  | 739 | out.push_back(StringPrintf("%s%s", prefix, value->c_str())); | 
|  | 740 | } else { | 
|  | 741 | out.push_back(*value); | 
|  | 742 | } | 
|  | 743 | } | 
|  | 744 | } | 
|  | 745 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 746 | static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH"; | 
|  | 747 | static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT"; | 
|  | 748 | static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA"; | 
|  | 749 | // The index of the instruction-set string inside the package parameters. Needed for | 
|  | 750 | // some special-casing that requires knowledge of the instruction-set. | 
|  | 751 | static constexpr size_t kISAIndex = 3; | 
|  | 752 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 753 | // Stores the system properties read out of the B partition. We need to use these properties | 
|  | 754 | // to compile, instead of the A properties we could get from init/get_property. | 
|  | 755 | SystemProperties system_properties_; | 
|  | 756 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 757 | // Some select properties that are always needed. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 758 | std::string android_root_; | 
|  | 759 | std::string android_data_; | 
|  | 760 | std::string boot_classpath_; | 
|  | 761 | std::string asec_mountpoint_; | 
|  | 762 |  | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 763 | OTAPreoptParameters parameters_; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 764 |  | 
|  | 765 | // Store environment values we need to set. | 
|  | 766 | std::vector<std::string> environ_; | 
|  | 767 | }; | 
|  | 768 |  | 
|  | 769 | OTAPreoptService gOps; | 
|  | 770 |  | 
|  | 771 | //////////////////////// | 
|  | 772 | // Plug-in functions. // | 
|  | 773 | //////////////////////// | 
|  | 774 |  | 
|  | 775 | int get_property(const char *key, char *value, const char *default_value) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 776 | return gOps.GetProperty(key, value, default_value); | 
|  | 777 | } | 
|  | 778 |  | 
|  | 779 | // Compute the output path of | 
|  | 780 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, | 
|  | 781 | const char *apk_path, | 
|  | 782 | const char *instruction_set) { | 
| Dan Austin | 9c8f93a | 2016-06-03 16:15:54 -0700 | [diff] [blame] | 783 | const char *file_name_start; | 
|  | 784 | const char *file_name_end; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 785 |  | 
|  | 786 | file_name_start = strrchr(apk_path, '/'); | 
|  | 787 | if (file_name_start == nullptr) { | 
|  | 788 | ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); | 
|  | 789 | return false; | 
|  | 790 | } | 
|  | 791 | file_name_end = strrchr(file_name_start, '.'); | 
|  | 792 | if (file_name_end == nullptr) { | 
|  | 793 | ALOGE("apk_path '%s' has no extension\n", apk_path); | 
|  | 794 | return false; | 
|  | 795 | } | 
|  | 796 |  | 
|  | 797 | // Calculate file_name | 
|  | 798 | file_name_start++;  // Move past '/', is valid as file_name_end is valid. | 
|  | 799 | size_t file_name_len = file_name_end - file_name_start; | 
|  | 800 | std::string file_name(file_name_start, file_name_len); | 
|  | 801 |  | 
|  | 802 | // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 803 | snprintf(path, | 
|  | 804 | PKG_PATH_MAX, | 
|  | 805 | "%s/%s/%s.odex.%s", | 
|  | 806 | oat_dir, | 
|  | 807 | instruction_set, | 
|  | 808 | file_name.c_str(), | 
|  | 809 | gOps.GetTargetSlot().c_str()); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 810 | return true; | 
|  | 811 | } | 
|  | 812 |  | 
|  | 813 | /* | 
|  | 814 | * Computes the odex file for the given apk_path and instruction_set. | 
|  | 815 | * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex | 
|  | 816 | * | 
|  | 817 | * Returns false if it failed to determine the odex file path. | 
|  | 818 | */ | 
|  | 819 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, | 
|  | 820 | const char *instruction_set) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 821 | const char *path_end = strrchr(apk_path, '/'); | 
|  | 822 | if (path_end == nullptr) { | 
|  | 823 | ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); | 
|  | 824 | return false; | 
|  | 825 | } | 
|  | 826 | std::string path_component(apk_path, path_end - apk_path); | 
|  | 827 |  | 
|  | 828 | const char *name_begin = path_end + 1; | 
|  | 829 | const char *extension_start = strrchr(name_begin, '.'); | 
|  | 830 | if (extension_start == nullptr) { | 
|  | 831 | ALOGE("apk_path '%s' has no extension.\n", apk_path); | 
|  | 832 | return false; | 
|  | 833 | } | 
|  | 834 | std::string name_component(name_begin, extension_start - name_begin); | 
|  | 835 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 836 | std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s", | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 837 | path_component.c_str(), | 
|  | 838 | instruction_set, | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 839 | name_component.c_str(), | 
|  | 840 | gOps.GetTargetSlot().c_str()); | 
|  | 841 | if (new_path.length() >= PKG_PATH_MAX) { | 
|  | 842 | LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path; | 
|  | 843 | return false; | 
|  | 844 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 845 | strcpy(path, new_path.c_str()); | 
|  | 846 | return true; | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | bool create_cache_path(char path[PKG_PATH_MAX], | 
|  | 850 | const char *src, | 
|  | 851 | const char *instruction_set) { | 
|  | 852 | size_t srclen = strlen(src); | 
|  | 853 |  | 
|  | 854 | /* demand that we are an absolute path */ | 
|  | 855 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { | 
|  | 856 | return false; | 
|  | 857 | } | 
|  | 858 |  | 
|  | 859 | if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX? | 
|  | 860 | return false; | 
|  | 861 | } | 
|  | 862 |  | 
|  | 863 | std::string from_src = std::string(src + 1); | 
|  | 864 | std::replace(from_src.begin(), from_src.end(), '/', '@'); | 
|  | 865 |  | 
|  | 866 | std::string assembled_path = StringPrintf("%s/%s/%s/%s%s", | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 867 | gOps.GetOTADataDirectory().c_str(), | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 868 | DALVIK_CACHE, | 
|  | 869 | instruction_set, | 
|  | 870 | from_src.c_str(), | 
| David Brazdil | 249c179 | 2016-09-06 15:35:28 +0100 | [diff] [blame] | 871 | DALVIK_CACHE_POSTFIX); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 872 |  | 
|  | 873 | if (assembled_path.length() + 1 > PKG_PATH_MAX) { | 
|  | 874 | return false; | 
|  | 875 | } | 
|  | 876 | strcpy(path, assembled_path.c_str()); | 
|  | 877 |  | 
|  | 878 | return true; | 
|  | 879 | } | 
|  | 880 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 881 | static int log_callback(int type, const char *fmt, ...) { | 
|  | 882 | va_list ap; | 
|  | 883 | int priority; | 
|  | 884 |  | 
|  | 885 | switch (type) { | 
|  | 886 | case SELINUX_WARNING: | 
|  | 887 | priority = ANDROID_LOG_WARN; | 
|  | 888 | break; | 
|  | 889 | case SELINUX_INFO: | 
|  | 890 | priority = ANDROID_LOG_INFO; | 
|  | 891 | break; | 
|  | 892 | default: | 
|  | 893 | priority = ANDROID_LOG_ERROR; | 
|  | 894 | break; | 
|  | 895 | } | 
|  | 896 | va_start(ap, fmt); | 
|  | 897 | LOG_PRI_VA(priority, "SELinux", fmt, ap); | 
|  | 898 | va_end(ap); | 
|  | 899 | return 0; | 
|  | 900 | } | 
|  | 901 |  | 
|  | 902 | static int otapreopt_main(const int argc, char *argv[]) { | 
|  | 903 | int selinux_enabled = (is_selinux_enabled() > 0); | 
|  | 904 |  | 
|  | 905 | setenv("ANDROID_LOG_TAGS", "*:v", 1); | 
|  | 906 | android::base::InitLogging(argv); | 
|  | 907 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 908 | if (argc < 2) { | 
|  | 909 | ALOGE("Expecting parameters"); | 
|  | 910 | exit(1); | 
|  | 911 | } | 
|  | 912 |  | 
|  | 913 | union selinux_callback cb; | 
|  | 914 | cb.func_log = log_callback; | 
|  | 915 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
|  | 916 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 917 | if (selinux_enabled && selinux_status_open(true) < 0) { | 
|  | 918 | ALOGE("Could not open selinux status; exiting.\n"); | 
|  | 919 | exit(1); | 
|  | 920 | } | 
|  | 921 |  | 
|  | 922 | int ret = android::installd::gOps.Main(argc, argv); | 
|  | 923 |  | 
|  | 924 | return ret; | 
|  | 925 | } | 
|  | 926 |  | 
|  | 927 | }  // namespace installd | 
|  | 928 | }  // namespace android | 
|  | 929 |  | 
|  | 930 | int main(const int argc, char *argv[]) { | 
|  | 931 | return android::installd::otapreopt_main(argc, argv); | 
|  | 932 | } |