Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [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 "otapreopt_parameters.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "dexopt.h" |
| 22 | #include "installd_constants.h" |
| 23 | #include "otapreopt_utils.h" |
| 24 | |
| 25 | #ifndef LOG_TAG |
| 26 | #define LOG_TAG "otapreopt" |
| 27 | #endif |
| 28 | |
| 29 | namespace android { |
| 30 | namespace installd { |
| 31 | |
| 32 | static bool ParseBool(const char* in) { |
| 33 | if (strcmp(in, "true") == 0) { |
| 34 | return true; |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | static const char* ParseNull(const char* arg) { |
| 40 | return (strcmp(arg, "!") == 0) ? nullptr : arg; |
| 41 | } |
| 42 | |
| 43 | static bool ParseUInt(const char* in, uint32_t* out) { |
| 44 | char* end; |
| 45 | long long int result = strtoll(in, &end, 0); |
| 46 | if (in == end || *end != '\0') { |
| 47 | return false; |
| 48 | } |
| 49 | if (result < std::numeric_limits<uint32_t>::min() || |
| 50 | std::numeric_limits<uint32_t>::max() < result) { |
| 51 | return false; |
| 52 | } |
| 53 | *out = static_cast<uint32_t>(result); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool OTAPreoptParameters::ReadArguments(int argc, const char** argv) { |
| 58 | // Expected command line: |
| 59 | // target-slot [version] dexopt {DEXOPT_PARAMETERS} |
| 60 | |
| 61 | const char* target_slot_arg = argv[1]; |
| 62 | if (target_slot_arg == nullptr) { |
| 63 | LOG(ERROR) << "Missing parameters"; |
| 64 | return false; |
| 65 | } |
| 66 | // Sanitize value. Only allow (a-zA-Z0-9_)+. |
| 67 | target_slot = target_slot_arg; |
| 68 | if (!ValidateTargetSlotSuffix(target_slot)) { |
| 69 | LOG(ERROR) << "Target slot suffix not legal: " << target_slot; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // Check for version or "dexopt" next. |
| 74 | if (argv[2] == nullptr) { |
| 75 | LOG(ERROR) << "Missing parameters"; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if (std::string("dexopt").compare(argv[2]) == 0) { |
| 80 | // This is version 1 (N) or pre-versioning version 2. |
| 81 | constexpr int kV2ArgCount = 1 // "otapreopt" |
| 82 | + 1 // slot |
| 83 | + 1 // "dexopt" |
| 84 | + 1 // apk_path |
| 85 | + 1 // uid |
| 86 | + 1 // pkg |
| 87 | + 1 // isa |
| 88 | + 1 // dexopt_needed |
| 89 | + 1 // oat_dir |
| 90 | + 1 // dexopt_flags |
| 91 | + 1 // filter |
| 92 | + 1 // volume |
| 93 | + 1 // libs |
| 94 | + 1; // seinfo |
| 95 | if (argc == kV2ArgCount) { |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 96 | return ReadArgumentsPostV1(2, argv, false); |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 97 | } else { |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 98 | return ReadArgumentsV1(argv); |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | uint32_t version; |
| 103 | if (!ParseUInt(argv[2], &version)) { |
| 104 | LOG(ERROR) << "Could not parse version: " << argv[2]; |
| 105 | return false; |
| 106 | } |
| 107 | |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 108 | return ReadArgumentsPostV1(version, argv, true); |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static int ReplaceMask(int input, int old_mask, int new_mask) { |
| 112 | return (input & old_mask) != 0 ? new_mask : 0; |
| 113 | } |
| 114 | |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 115 | void OTAPreoptParameters::SetDefaultsForPostV1Arguments() { |
| 116 | // Set se_info to null. It is only relevant for secondary dex files, which we won't |
| 117 | // receive from a v1 A side. |
| 118 | se_info = nullptr; |
| 119 | |
| 120 | // Set downgrade to false. It is only relevant when downgrading compiler |
| 121 | // filter, which is not the case during ota. |
| 122 | downgrade = false; |
| 123 | |
| 124 | // Set target_sdk_version to 0, ie the platform SDK version. This is |
| 125 | // conservative and may force some classes to verify at runtime. |
| 126 | target_sdk_version = 0; |
| 127 | |
| 128 | // Set the profile name to the primary apk profile. |
| 129 | profile_name = "primary.prof"; |
| 130 | |
| 131 | // By default we don't have a dex metadata file. |
| 132 | dex_metadata_path = nullptr; |
| 133 | |
| 134 | // The compilation reason is ab-ota (match the system property pm.dexopt.ab-ota) |
| 135 | compilation_reason = "ab-ota"; |
Mathieu Chartier | dbc4ce6 | 2018-03-07 09:38:16 -0800 | [diff] [blame^] | 136 | |
| 137 | // Flag is enabled by default for A/B otas. |
| 138 | dexopt_flags = DEXOPT_GENERATE_COMPACT_DEX; |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 141 | bool OTAPreoptParameters::ReadArgumentsV1(const char** argv) { |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 142 | // Check for "dexopt". |
| 143 | if (argv[2] == nullptr) { |
| 144 | LOG(ERROR) << "Missing parameters"; |
| 145 | return false; |
| 146 | } |
| 147 | if (std::string("dexopt").compare(argv[2]) != 0) { |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 148 | LOG(ERROR) << "Expected \"dexopt\" but found: " << argv[2]; |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | |
Mathieu Chartier | dbc4ce6 | 2018-03-07 09:38:16 -0800 | [diff] [blame^] | 152 | SetDefaultsForPostV1Arguments(); |
| 153 | |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 154 | size_t param_index = 0; |
| 155 | for (;; ++param_index) { |
| 156 | const char* param = argv[3 + param_index]; |
| 157 | if (param == nullptr) { |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | switch (param_index) { |
| 162 | case 0: |
| 163 | apk_path = param; |
| 164 | break; |
| 165 | |
| 166 | case 1: |
| 167 | uid = atoi(param); |
| 168 | break; |
| 169 | |
| 170 | case 2: |
| 171 | pkgName = param; |
| 172 | break; |
| 173 | |
| 174 | case 3: |
| 175 | instruction_set = param; |
| 176 | break; |
| 177 | |
| 178 | case 4: { |
| 179 | // Version 1 had: |
| 180 | // DEXOPT_DEX2OAT_NEEDED = 1 |
| 181 | // DEXOPT_PATCHOAT_NEEDED = 2 |
| 182 | // DEXOPT_SELF_PATCHOAT_NEEDED = 3 |
| 183 | // We will simply use DEX2OAT_FROM_SCRATCH. |
| 184 | dexopt_needed = DEX2OAT_FROM_SCRATCH; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | case 5: |
| 189 | oat_dir = param; |
| 190 | break; |
| 191 | |
| 192 | case 6: { |
| 193 | // Version 1 had: |
| 194 | constexpr int OLD_DEXOPT_PUBLIC = 1 << 1; |
| 195 | // Note: DEXOPT_SAFEMODE has been removed. |
| 196 | // constexpr int OLD_DEXOPT_SAFEMODE = 1 << 2; |
| 197 | constexpr int OLD_DEXOPT_DEBUGGABLE = 1 << 3; |
| 198 | constexpr int OLD_DEXOPT_BOOTCOMPLETE = 1 << 4; |
| 199 | constexpr int OLD_DEXOPT_PROFILE_GUIDED = 1 << 5; |
| 200 | constexpr int OLD_DEXOPT_OTA = 1 << 6; |
Mathieu Chartier | dbc4ce6 | 2018-03-07 09:38:16 -0800 | [diff] [blame^] | 201 | static_assert(DEXOPT_GENERATE_COMPACT_DEX > OLD_DEXOPT_OTA, "must not overlap"); |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 202 | int input = atoi(param); |
Mathieu Chartier | dbc4ce6 | 2018-03-07 09:38:16 -0800 | [diff] [blame^] | 203 | dexopt_flags |= |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 204 | ReplaceMask(input, OLD_DEXOPT_PUBLIC, DEXOPT_PUBLIC) | |
| 205 | ReplaceMask(input, OLD_DEXOPT_DEBUGGABLE, DEXOPT_DEBUGGABLE) | |
| 206 | ReplaceMask(input, OLD_DEXOPT_BOOTCOMPLETE, DEXOPT_BOOTCOMPLETE) | |
| 207 | ReplaceMask(input, OLD_DEXOPT_PROFILE_GUIDED, DEXOPT_PROFILE_GUIDED) | |
| 208 | ReplaceMask(input, OLD_DEXOPT_OTA, 0); |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | case 7: |
| 213 | compiler_filter = param; |
| 214 | break; |
| 215 | |
| 216 | case 8: |
| 217 | volume_uuid = ParseNull(param); |
| 218 | break; |
| 219 | |
| 220 | case 9: |
| 221 | shared_libraries = ParseNull(param); |
| 222 | break; |
| 223 | |
| 224 | default: |
| 225 | LOG(ERROR) << "Too many arguments, got " << param; |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (param_index != 10) { |
| 231 | LOG(ERROR) << "Not enough parameters"; |
| 232 | return false; |
| 233 | } |
| 234 | |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 238 | bool OTAPreoptParameters::ReadArgumentsPostV1(uint32_t version, const char** argv, bool versioned) { |
| 239 | size_t num_args_expected = 0; |
| 240 | switch (version) { |
| 241 | case 2: num_args_expected = 11; break; |
| 242 | case 3: num_args_expected = 12; break; |
| 243 | case 4: num_args_expected = 13; break; |
| 244 | case 5: num_args_expected = 14; break; |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 245 | case 6: num_args_expected = 15; break; |
Mathieu Chartier | dbc4ce6 | 2018-03-07 09:38:16 -0800 | [diff] [blame^] | 246 | case 7: |
| 247 | // Version 8 adds a new dexopt flag: DEXOPT_GENERATE_COMPACT_DEX |
| 248 | case 8: num_args_expected = 16; break; |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 249 | default: |
| 250 | LOG(ERROR) << "Don't know how to read arguments for version " << version; |
| 251 | return false; |
| 252 | } |
| 253 | size_t dexopt_index = versioned ? 3 : 2; |
| 254 | |
| 255 | // Check for "dexopt". |
| 256 | if (argv[dexopt_index] == nullptr) { |
| 257 | LOG(ERROR) << "Missing parameters"; |
| 258 | return false; |
| 259 | } |
| 260 | if (std::string("dexopt").compare(argv[dexopt_index]) != 0) { |
| 261 | LOG(ERROR) << "Expected \"dexopt\" but found: " << argv[dexopt_index]; |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | // Validate the number of arguments. |
| 266 | size_t num_args_actual = 0; |
| 267 | while (argv[dexopt_index + 1 + num_args_actual] != nullptr) { |
| 268 | num_args_actual++; |
| 269 | } |
| 270 | |
| 271 | if (num_args_actual != num_args_expected) { |
| 272 | LOG(ERROR) << "Invalid number of arguments. expected=" |
| 273 | << num_args_expected << " actual=" << num_args_actual; |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | // The number of arguments is OK. |
| 278 | // Configure the default values for the parameters that were added after V1. |
| 279 | // The default values will be overwritten in case they are passed as arguments. |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 280 | SetDefaultsForPostV1Arguments(); |
Calin Juravle | 0c609c2 | 2018-02-12 17:39:37 -0800 | [diff] [blame] | 281 | |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 282 | for (size_t param_index = 0; param_index < num_args_actual; ++param_index) { |
| 283 | const char* param = argv[dexopt_index + 1 + param_index]; |
| 284 | switch (param_index) { |
| 285 | case 0: |
| 286 | apk_path = param; |
| 287 | break; |
| 288 | |
| 289 | case 1: |
| 290 | uid = atoi(param); |
| 291 | break; |
| 292 | |
| 293 | case 2: |
| 294 | pkgName = param; |
| 295 | break; |
| 296 | |
| 297 | case 3: |
| 298 | instruction_set = param; |
| 299 | break; |
| 300 | |
| 301 | case 4: |
| 302 | dexopt_needed = atoi(param); |
| 303 | break; |
| 304 | |
| 305 | case 5: |
| 306 | oat_dir = param; |
| 307 | break; |
| 308 | |
| 309 | case 6: |
| 310 | dexopt_flags = atoi(param); |
Mathieu Chartier | dbc4ce6 | 2018-03-07 09:38:16 -0800 | [diff] [blame^] | 311 | // Add CompactDex generation flag for versions less than 8 since it wasn't passed |
| 312 | // from the package manager. Only conditionally set the flag here so that it can |
| 313 | // be fully controlled by the package manager. |
| 314 | dexopt_flags |= (version < 8) ? DEXOPT_GENERATE_COMPACT_DEX : 0u; |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 315 | break; |
| 316 | |
| 317 | case 7: |
| 318 | compiler_filter = param; |
| 319 | break; |
| 320 | |
| 321 | case 8: |
| 322 | volume_uuid = ParseNull(param); |
| 323 | break; |
| 324 | |
| 325 | case 9: |
| 326 | shared_libraries = ParseNull(param); |
| 327 | break; |
| 328 | |
| 329 | case 10: |
| 330 | se_info = ParseNull(param); |
| 331 | break; |
| 332 | |
| 333 | case 11: |
| 334 | downgrade = ParseBool(param); |
| 335 | break; |
| 336 | |
| 337 | case 12: |
| 338 | target_sdk_version = atoi(param); |
| 339 | break; |
| 340 | |
| 341 | case 13: |
| 342 | profile_name = ParseNull(param); |
| 343 | break; |
| 344 | |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 345 | case 14: |
Calin Juravle | 0c609c2 | 2018-02-12 17:39:37 -0800 | [diff] [blame] | 346 | dex_metadata_path = ParseNull(param); |
| 347 | break; |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 348 | |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 349 | case 15: |
| 350 | compilation_reason = ParseNull(param); |
| 351 | break; |
| 352 | |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 353 | default: |
Calin Juravle | 0c609c2 | 2018-02-12 17:39:37 -0800 | [diff] [blame] | 354 | LOG(FATAL) << "Should not get here. Did you call ReadArguments " |
| 355 | << "with the right expectation? index=" << param_index |
| 356 | << " num_args=" << num_args_actual; |
| 357 | return false; |
Calin Juravle | 315d1f5 | 2018-02-01 14:56:14 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 364 | } // namespace installd |
| 365 | } // namespace android |