blob: 802d5d7da757bde51543e8feafc30e4d0ce570b5 [file] [log] [blame]
Calin Juravledff47292018-02-01 14:44:56 +00001/*
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
29namespace android {
30namespace installd {
31
32static bool ParseBool(const char* in) {
33 if (strcmp(in, "true") == 0) {
34 return true;
35 }
36 return false;
37}
38
39static const char* ParseNull(const char* arg) {
40 return (strcmp(arg, "!") == 0) ? nullptr : arg;
41}
42
43static 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
57bool 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 Juravle315d1f52018-02-01 14:56:14 +000096 return ReadArgumentsPostV1(2, argv, false);
Calin Juravledff47292018-02-01 14:44:56 +000097 } else {
Calin Juravle315d1f52018-02-01 14:56:14 +000098 return ReadArgumentsV1(argv);
Calin Juravledff47292018-02-01 14:44:56 +000099 }
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 Juravle315d1f52018-02-01 14:56:14 +0000108 return ReadArgumentsPostV1(version, argv, true);
Calin Juravledff47292018-02-01 14:44:56 +0000109}
110
111static int ReplaceMask(int input, int old_mask, int new_mask) {
112 return (input & old_mask) != 0 ? new_mask : 0;
113}
114
Calin Juravle2efc4022018-02-13 18:31:32 -0800115void 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";
136}
137
Calin Juravle315d1f52018-02-01 14:56:14 +0000138bool OTAPreoptParameters::ReadArgumentsV1(const char** argv) {
Calin Juravledff47292018-02-01 14:44:56 +0000139 // Check for "dexopt".
140 if (argv[2] == nullptr) {
141 LOG(ERROR) << "Missing parameters";
142 return false;
143 }
144 if (std::string("dexopt").compare(argv[2]) != 0) {
Calin Juravle315d1f52018-02-01 14:56:14 +0000145 LOG(ERROR) << "Expected \"dexopt\" but found: " << argv[2];
Calin Juravledff47292018-02-01 14:44:56 +0000146 return false;
147 }
148
149 size_t param_index = 0;
150 for (;; ++param_index) {
151 const char* param = argv[3 + param_index];
152 if (param == nullptr) {
153 break;
154 }
155
156 switch (param_index) {
157 case 0:
158 apk_path = param;
159 break;
160
161 case 1:
162 uid = atoi(param);
163 break;
164
165 case 2:
166 pkgName = param;
167 break;
168
169 case 3:
170 instruction_set = param;
171 break;
172
173 case 4: {
174 // Version 1 had:
175 // DEXOPT_DEX2OAT_NEEDED = 1
176 // DEXOPT_PATCHOAT_NEEDED = 2
177 // DEXOPT_SELF_PATCHOAT_NEEDED = 3
178 // We will simply use DEX2OAT_FROM_SCRATCH.
179 dexopt_needed = DEX2OAT_FROM_SCRATCH;
180 break;
181 }
182
183 case 5:
184 oat_dir = param;
185 break;
186
187 case 6: {
188 // Version 1 had:
189 constexpr int OLD_DEXOPT_PUBLIC = 1 << 1;
190 // Note: DEXOPT_SAFEMODE has been removed.
191 // constexpr int OLD_DEXOPT_SAFEMODE = 1 << 2;
192 constexpr int OLD_DEXOPT_DEBUGGABLE = 1 << 3;
193 constexpr int OLD_DEXOPT_BOOTCOMPLETE = 1 << 4;
194 constexpr int OLD_DEXOPT_PROFILE_GUIDED = 1 << 5;
195 constexpr int OLD_DEXOPT_OTA = 1 << 6;
196 int input = atoi(param);
197 dexopt_flags =
198 ReplaceMask(input, OLD_DEXOPT_PUBLIC, DEXOPT_PUBLIC) |
199 ReplaceMask(input, OLD_DEXOPT_DEBUGGABLE, DEXOPT_DEBUGGABLE) |
200 ReplaceMask(input, OLD_DEXOPT_BOOTCOMPLETE, DEXOPT_BOOTCOMPLETE) |
201 ReplaceMask(input, OLD_DEXOPT_PROFILE_GUIDED, DEXOPT_PROFILE_GUIDED) |
202 ReplaceMask(input, OLD_DEXOPT_OTA, 0);
203 break;
204 }
205
206 case 7:
207 compiler_filter = param;
208 break;
209
210 case 8:
211 volume_uuid = ParseNull(param);
212 break;
213
214 case 9:
215 shared_libraries = ParseNull(param);
216 break;
217
218 default:
219 LOG(ERROR) << "Too many arguments, got " << param;
220 return false;
221 }
222 }
223
224 if (param_index != 10) {
225 LOG(ERROR) << "Not enough parameters";
226 return false;
227 }
228
Calin Juravle2efc4022018-02-13 18:31:32 -0800229 SetDefaultsForPostV1Arguments();
Calin Juravle0c609c22018-02-12 17:39:37 -0800230
Calin Juravledff47292018-02-01 14:44:56 +0000231 return true;
232}
233
Calin Juravle315d1f52018-02-01 14:56:14 +0000234bool OTAPreoptParameters::ReadArgumentsPostV1(uint32_t version, const char** argv, bool versioned) {
235 size_t num_args_expected = 0;
236 switch (version) {
237 case 2: num_args_expected = 11; break;
238 case 3: num_args_expected = 12; break;
239 case 4: num_args_expected = 13; break;
240 case 5: num_args_expected = 14; break;
Calin Juravle62c5a372018-02-01 17:03:23 +0000241 case 6: num_args_expected = 15; break;
Calin Juravle2efc4022018-02-13 18:31:32 -0800242 case 7: num_args_expected = 16; break;
Calin Juravle315d1f52018-02-01 14:56:14 +0000243 default:
244 LOG(ERROR) << "Don't know how to read arguments for version " << version;
245 return false;
246 }
247 size_t dexopt_index = versioned ? 3 : 2;
248
249 // Check for "dexopt".
250 if (argv[dexopt_index] == nullptr) {
251 LOG(ERROR) << "Missing parameters";
252 return false;
253 }
254 if (std::string("dexopt").compare(argv[dexopt_index]) != 0) {
255 LOG(ERROR) << "Expected \"dexopt\" but found: " << argv[dexopt_index];
256 return false;
257 }
258
259 // Validate the number of arguments.
260 size_t num_args_actual = 0;
261 while (argv[dexopt_index + 1 + num_args_actual] != nullptr) {
262 num_args_actual++;
263 }
264
265 if (num_args_actual != num_args_expected) {
266 LOG(ERROR) << "Invalid number of arguments. expected="
267 << num_args_expected << " actual=" << num_args_actual;
268 return false;
269 }
270
271 // The number of arguments is OK.
272 // Configure the default values for the parameters that were added after V1.
273 // The default values will be overwritten in case they are passed as arguments.
Calin Juravle2efc4022018-02-13 18:31:32 -0800274 SetDefaultsForPostV1Arguments();
Calin Juravle0c609c22018-02-12 17:39:37 -0800275
Calin Juravle315d1f52018-02-01 14:56:14 +0000276 for (size_t param_index = 0; param_index < num_args_actual; ++param_index) {
277 const char* param = argv[dexopt_index + 1 + param_index];
278 switch (param_index) {
279 case 0:
280 apk_path = param;
281 break;
282
283 case 1:
284 uid = atoi(param);
285 break;
286
287 case 2:
288 pkgName = param;
289 break;
290
291 case 3:
292 instruction_set = param;
293 break;
294
295 case 4:
296 dexopt_needed = atoi(param);
297 break;
298
299 case 5:
300 oat_dir = param;
301 break;
302
303 case 6:
304 dexopt_flags = atoi(param);
305 break;
306
307 case 7:
308 compiler_filter = param;
309 break;
310
311 case 8:
312 volume_uuid = ParseNull(param);
313 break;
314
315 case 9:
316 shared_libraries = ParseNull(param);
317 break;
318
319 case 10:
320 se_info = ParseNull(param);
321 break;
322
323 case 11:
324 downgrade = ParseBool(param);
325 break;
326
327 case 12:
328 target_sdk_version = atoi(param);
329 break;
330
331 case 13:
332 profile_name = ParseNull(param);
333 break;
334
Calin Juravle62c5a372018-02-01 17:03:23 +0000335 case 14:
Calin Juravle0c609c22018-02-12 17:39:37 -0800336 dex_metadata_path = ParseNull(param);
337 break;
Calin Juravle62c5a372018-02-01 17:03:23 +0000338
Calin Juravle2efc4022018-02-13 18:31:32 -0800339 case 15:
340 compilation_reason = ParseNull(param);
341 break;
342
Calin Juravle315d1f52018-02-01 14:56:14 +0000343 default:
Calin Juravle0c609c22018-02-12 17:39:37 -0800344 LOG(FATAL) << "Should not get here. Did you call ReadArguments "
345 << "with the right expectation? index=" << param_index
346 << " num_args=" << num_args_actual;
347 return false;
Calin Juravle315d1f52018-02-01 14:56:14 +0000348 }
349 }
350
351 return true;
352}
353
Calin Juravledff47292018-02-01 14:44:56 +0000354} // namespace installd
355} // namespace android