blob: eefbe4ffe2e97bf4064febe4c417ff1e588cf47b [file] [log] [blame]
Andreas Gampe73dae112015-11-19 14:12:14 -08001/*
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 Gampec4ced4f2017-04-14 20:39:56 -070019#include <limits>
Andreas Gampe73dae112015-11-19 14:12:14 -080020#include <random>
Andreas Gampe1842af32016-03-16 14:28:50 -070021#include <regex>
Andreas Gampe73dae112015-11-19 14:12:14 -080022#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>
Andreas Gampe73dae112015-11-19 14:12:14 -080029
30#include <android-base/logging.h>
31#include <android-base/macros.h>
32#include <android-base/stringprintf.h>
Andreas Gampe6db8db92016-06-03 10:22:19 -070033#include <android-base/strings.h>
Andreas Gampece9fe7f2018-09-18 10:25:58 -070034#include <art_image_values.h>
Andreas Gampe73dae112015-11-19 14:12:14 -080035#include <cutils/fs.h>
Andreas Gampe73dae112015-11-19 14:12:14 -080036#include <cutils/properties.h>
Andreas Gampe54e1a402017-03-20 18:42:49 -070037#include <dex2oat_return_codes.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070038#include <log/log.h>
Andreas Gampe73dae112015-11-19 14:12:14 -080039#include <private/android_filesystem_config.h>
40
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070041#include "dexopt.h"
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070042#include "file_parsing.h"
43#include "globals.h"
Andreas Gampec4ced4f2017-04-14 20:39:56 -070044#include "installd_constants.h"
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070045#include "installd_deps.h" // Need to fill in requirements of commands.
Calin Juravlec9e76792018-02-01 14:44:56 +000046#include "otapreopt_parameters.h"
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070047#include "otapreopt_utils.h"
48#include "system_properties.h"
49#include "utils.h"
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070050
Andreas Gampe73dae112015-11-19 14:12:14 -080051#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 Gampe56f79f92016-06-08 15:11:37 -070059using android::base::EndsWith;
Andreas Gampe6db8db92016-06-03 10:22:19 -070060using android::base::Split;
Andreas Gampe56f79f92016-06-08 15:11:37 -070061using android::base::StartsWith;
Andreas Gampe73dae112015-11-19 14:12:14 -080062using android::base::StringPrintf;
63
64namespace android {
65namespace installd {
66
Andreas Gampeef21fd22017-05-22 13:36:06 -070067// Check expected values for dexopt flags. If you need to change this:
68//
69// RUN AN A/B OTA TO MAKE SURE THINGS STILL WORK!
70//
71// You most likely need to increase the protocol version and all that entails!
72
73static_assert(DEXOPT_PUBLIC == 1 << 1, "DEXOPT_PUBLIC unexpected.");
74static_assert(DEXOPT_DEBUGGABLE == 1 << 2, "DEXOPT_DEBUGGABLE unexpected.");
75static_assert(DEXOPT_BOOTCOMPLETE == 1 << 3, "DEXOPT_BOOTCOMPLETE unexpected.");
76static_assert(DEXOPT_PROFILE_GUIDED == 1 << 4, "DEXOPT_PROFILE_GUIDED unexpected.");
77static_assert(DEXOPT_SECONDARY_DEX == 1 << 5, "DEXOPT_SECONDARY_DEX unexpected.");
78static_assert(DEXOPT_FORCE == 1 << 6, "DEXOPT_FORCE unexpected.");
79static_assert(DEXOPT_STORAGE_CE == 1 << 7, "DEXOPT_STORAGE_CE unexpected.");
80static_assert(DEXOPT_STORAGE_DE == 1 << 8, "DEXOPT_STORAGE_DE unexpected.");
David Brazdil22cce5a2018-02-12 18:04:59 -080081static_assert(DEXOPT_ENABLE_HIDDEN_API_CHECKS == 1 << 10,
82 "DEXOPT_ENABLE_HIDDEN_API_CHECKS unexpected");
Mathieu Chartier351bc942018-03-06 13:55:58 -080083static_assert(DEXOPT_GENERATE_COMPACT_DEX == 1 << 11, "DEXOPT_GENERATE_COMPACT_DEX unexpected");
Mathieu Chartierad45a1b2018-03-12 17:55:06 -070084static_assert(DEXOPT_GENERATE_APP_IMAGE == 1 << 12, "DEXOPT_GENERATE_APP_IMAGE unexpected");
Andreas Gampeef21fd22017-05-22 13:36:06 -070085
Mathieu Chartierad45a1b2018-03-12 17:55:06 -070086static_assert(DEXOPT_MASK == (0x1dfe | DEXOPT_IDLE_BACKGROUND_JOB),
Andreas Gamped32eec22018-02-28 16:02:51 -080087 "DEXOPT_MASK unexpected.");
Andreas Gampeef21fd22017-05-22 13:36:06 -070088
89
Andreas Gampea64a6272018-07-10 10:43:47 -070090template<typename T>
91static constexpr bool IsPowerOfTwo(T x) {
92 static_assert(std::is_integral<T>::value, "T must be integral");
93 // TODO: assert unsigned. There is currently many uses with signed values.
94 return (x & (x - 1)) == 0;
95}
Andreas Gampeef21fd22017-05-22 13:36:06 -070096
Andreas Gampe73dae112015-11-19 14:12:14 -080097template<typename T>
98static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
99 return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
100}
101
102template<typename T>
103static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
104 return RoundDown(x + n - 1, n);
105}
106
107class OTAPreoptService {
108 public:
Andreas Gampe73dae112015-11-19 14:12:14 -0800109 // Main driver. Performs the following steps.
110 //
111 // 1) Parse options (read system properties etc from B partition).
112 //
113 // 2) Read in package data.
114 //
115 // 3) Prepare environment variables.
116 //
117 // 4) Prepare(compile) boot image, if necessary.
118 //
119 // 5) Run update.
120 int Main(int argc, char** argv) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700121 if (!ReadArguments(argc, argv)) {
122 LOG(ERROR) << "Failed reading command line.";
123 return 1;
124 }
125
Andreas Gampe73dae112015-11-19 14:12:14 -0800126 if (!ReadSystemProperties()) {
127 LOG(ERROR)<< "Failed reading system properties.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700128 return 2;
Andreas Gampe73dae112015-11-19 14:12:14 -0800129 }
130
131 if (!ReadEnvironment()) {
132 LOG(ERROR) << "Failed reading environment properties.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700133 return 3;
Andreas Gampe73dae112015-11-19 14:12:14 -0800134 }
135
Andreas Gamped089ca12016-06-27 14:25:30 -0700136 if (!CheckAndInitializeInstalldGlobals()) {
137 LOG(ERROR) << "Failed initializing globals.";
138 return 4;
Andreas Gampe73dae112015-11-19 14:12:14 -0800139 }
140
141 PrepareEnvironment();
142
Andreas Gamped089ca12016-06-27 14:25:30 -0700143 if (!PrepareBootImage(/* force */ false)) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800144 LOG(ERROR) << "Failed preparing boot image.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700145 return 5;
Andreas Gampe73dae112015-11-19 14:12:14 -0800146 }
147
148 int dexopt_retcode = RunPreopt();
149
150 return dexopt_retcode;
151 }
152
Andreas Gamped089ca12016-06-27 14:25:30 -0700153 int GetProperty(const char* key, char* value, const char* default_value) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800154 const std::string* prop_value = system_properties_.GetProperty(key);
155 if (prop_value == nullptr) {
156 if (default_value == nullptr) {
157 return 0;
158 }
159 // Copy in the default value.
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -0600160 strlcpy(value, default_value, kPropertyValueMax - 1);
Andreas Gampe73dae112015-11-19 14:12:14 -0800161 value[kPropertyValueMax - 1] = 0;
162 return strlen(default_value);// TODO: Need to truncate?
163 }
Andreas Gampe5696e632017-09-26 20:41:48 -0700164 size_t size = std::min(kPropertyValueMax - 1, prop_value->length()) + 1;
Jeff Sharkey1b9d9a62017-09-21 14:51:09 -0600165 strlcpy(value, prop_value->data(), size);
Andreas Gampe5696e632017-09-26 20:41:48 -0700166 return static_cast<int>(size - 1);
Andreas Gampe73dae112015-11-19 14:12:14 -0800167 }
168
Andreas Gamped089ca12016-06-27 14:25:30 -0700169 std::string GetOTADataDirectory() const {
Calin Juravlec9e76792018-02-01 14:44:56 +0000170 return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), GetTargetSlot().c_str());
Andreas Gamped089ca12016-06-27 14:25:30 -0700171 }
172
173 const std::string& GetTargetSlot() const {
Calin Juravlec9e76792018-02-01 14:44:56 +0000174 return parameters_.target_slot;
Andreas Gamped089ca12016-06-27 14:25:30 -0700175 }
176
Andreas Gampe73dae112015-11-19 14:12:14 -0800177private:
Andreas Gamped089ca12016-06-27 14:25:30 -0700178
Andreas Gampe73dae112015-11-19 14:12:14 -0800179 bool ReadSystemProperties() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700180 static constexpr const char* kPropertyFiles[] = {
181 "/default.prop", "/system/build.prop"
182 };
Andreas Gampe73dae112015-11-19 14:12:14 -0800183
Andreas Gampe1842af32016-03-16 14:28:50 -0700184 for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) {
185 if (!system_properties_.Load(kPropertyFiles[i])) {
186 return false;
187 }
188 }
189
190 return true;
Andreas Gampe73dae112015-11-19 14:12:14 -0800191 }
192
193 bool ReadEnvironment() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700194 // Parse the environment variables from init.environ.rc, which have the form
195 // export NAME VALUE
196 // For simplicity, don't respect string quotation. The values we are interested in can be
197 // encoded without them.
198 std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)");
199 bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) {
200 std::smatch export_match;
201 if (!std::regex_match(line, export_match, export_regex)) {
202 return true;
203 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800204
Andreas Gampe1842af32016-03-16 14:28:50 -0700205 if (export_match.size() != 3) {
206 return true;
207 }
208
209 std::string name = export_match[1].str();
210 std::string value = export_match[2].str();
211
212 system_properties_.SetProperty(name, value);
213
214 return true;
215 });
216 if (!parse_result) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800217 return false;
218 }
Andreas Gampe1842af32016-03-16 14:28:50 -0700219
Andreas Gamped089ca12016-06-27 14:25:30 -0700220 if (system_properties_.GetProperty(kAndroidDataPathPropertyName) == nullptr) {
221 return false;
222 }
223 android_data_ = *system_properties_.GetProperty(kAndroidDataPathPropertyName);
224
225 if (system_properties_.GetProperty(kAndroidRootPathPropertyName) == nullptr) {
226 return false;
227 }
228 android_root_ = *system_properties_.GetProperty(kAndroidRootPathPropertyName);
229
230 if (system_properties_.GetProperty(kBootClassPathPropertyName) == nullptr) {
231 return false;
232 }
233 boot_classpath_ = *system_properties_.GetProperty(kBootClassPathPropertyName);
234
235 if (system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) == nullptr) {
236 return false;
237 }
238 asec_mountpoint_ = *system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME);
239
240 return true;
241 }
242
243 const std::string& GetAndroidData() const {
244 return android_data_;
245 }
246
247 const std::string& GetAndroidRoot() const {
248 return android_root_;
249 }
250
251 const std::string GetOtaDirectoryPrefix() const {
252 return GetAndroidData() + "/ota";
253 }
254
255 bool CheckAndInitializeInstalldGlobals() {
256 // init_globals_from_data_and_root requires "ASEC_MOUNTPOINT" in the environment. We
257 // do not use any datapath that includes this, but we'll still have to set it.
258 CHECK(system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) != nullptr);
259 int result = setenv(ASEC_MOUNTPOINT_ENV_NAME, asec_mountpoint_.c_str(), 0);
260 if (result != 0) {
261 LOG(ERROR) << "Could not set ASEC_MOUNTPOINT environment variable";
262 return false;
263 }
264
265 if (!init_globals_from_data_and_root(GetAndroidData().c_str(), GetAndroidRoot().c_str())) {
266 LOG(ERROR) << "Could not initialize globals; exiting.";
267 return false;
268 }
269
270 // This is different from the normal installd. We only do the base
271 // directory, the rest will be created on demand when each app is compiled.
272 if (access(GetOtaDirectoryPrefix().c_str(), R_OK) < 0) {
273 LOG(ERROR) << "Could not access " << GetOtaDirectoryPrefix();
274 return false;
Andreas Gampe1842af32016-03-16 14:28:50 -0700275 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800276
277 return true;
278 }
279
Shubham Ajmera45c87432017-06-22 11:10:27 -0700280 bool ParseBool(const char* in) {
281 if (strcmp(in, "true") == 0) {
282 return true;
283 }
284 return false;
285 }
286
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700287 bool ParseUInt(const char* in, uint32_t* out) {
288 char* end;
289 long long int result = strtoll(in, &end, 0);
290 if (in == end || *end != '\0') {
291 return false;
292 }
293 if (result < std::numeric_limits<uint32_t>::min() ||
294 std::numeric_limits<uint32_t>::max() < result) {
295 return false;
296 }
297 *out = static_cast<uint32_t>(result);
298 return true;
299 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700300
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700301 bool ReadArguments(int argc, char** argv) {
Calin Juravlec9e76792018-02-01 14:44:56 +0000302 return parameters_.ReadArguments(argc, const_cast<const char**>(argv));
Andreas Gampe73dae112015-11-19 14:12:14 -0800303 }
304
305 void PrepareEnvironment() {
Andreas Gamped089ca12016-06-27 14:25:30 -0700306 environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_classpath_.c_str()));
307 environ_.push_back(StringPrintf("ANDROID_DATA=%s", GetOTADataDirectory().c_str()));
308 environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root_.c_str()));
Andreas Gampe73dae112015-11-19 14:12:14 -0800309
310 for (const std::string& e : environ_) {
311 putenv(const_cast<char*>(e.c_str()));
312 }
313 }
314
315 // Ensure that we have the right boot image. The first time any app is
316 // compiled, we'll try to generate it.
Andreas Gamped089ca12016-06-27 14:25:30 -0700317 bool PrepareBootImage(bool force) const {
Calin Juravlec9e76792018-02-01 14:44:56 +0000318 if (parameters_.instruction_set == nullptr) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800319 LOG(ERROR) << "Instruction set missing.";
320 return false;
321 }
Calin Juravlec9e76792018-02-01 14:44:56 +0000322 const char* isa = parameters_.instruction_set;
Andreas Gamped089ca12016-06-27 14:25:30 -0700323 std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE;
Andreas Gampe73dae112015-11-19 14:12:14 -0800324 std::string isa_path = dalvik_cache + "/" + isa;
Andreas Gampe73dae112015-11-19 14:12:14 -0800325
Andreas Gamped089ca12016-06-27 14:25:30 -0700326 // Reset umask in otapreopt, so that we control the the access for the files we create.
327 umask(0);
328
Andreas Gampe73dae112015-11-19 14:12:14 -0800329 // Create the directories, if necessary.
330 if (access(dalvik_cache.c_str(), F_OK) != 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700331 if (!CreatePath(dalvik_cache)) {
332 PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache;
Andreas Gampe73dae112015-11-19 14:12:14 -0800333 return false;
334 }
335 }
336 if (access(isa_path.c_str(), F_OK) != 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700337 if (!CreatePath(isa_path)) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800338 PLOG(ERROR) << "Could not create dalvik-cache isa dir";
339 return false;
340 }
341 }
342
Andreas Gampebd7aef12018-10-23 13:58:44 -0700343 // Check whether we have files in /data.
344 // TODO: check that the files are correct wrt/ jars.
345 std::string art_path = isa_path + "/system@framework@boot.art";
346 std::string oat_path = isa_path + "/system@framework@boot.oat";
347 bool cleared = false;
348 if (access(art_path.c_str(), F_OK) == 0 && access(oat_path.c_str(), F_OK) == 0) {
349 // Files exist, assume everything is alright if not forced. Otherwise clean up.
350 if (!force) {
351 return true;
352 }
353 ClearDirectory(isa_path);
354 cleared = true;
355 }
356
357 // Check whether we have an image in /system.
358 // TODO: check that the files are correct wrt/ jars.
359 std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
360 if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
361 // Note: we ignore |force| here.
362 return true;
363 }
364
365
Andreas Gamped089ca12016-06-27 14:25:30 -0700366 if (!cleared) {
367 ClearDirectory(isa_path);
368 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800369
Andreas Gampebd7aef12018-10-23 13:58:44 -0700370 return Dex2oatBootImage(boot_classpath_, art_path, oat_path, isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800371 }
372
Andreas Gamped089ca12016-06-27 14:25:30 -0700373 static bool CreatePath(const std::string& path) {
374 // Create the given path. Use string processing instead of dirname, as dirname's need for
375 // a writable char buffer is painful.
376
377 // First, try to use the full path.
378 if (mkdir(path.c_str(), 0711) == 0) {
379 return true;
380 }
381 if (errno != ENOENT) {
382 PLOG(ERROR) << "Could not create path " << path;
383 return false;
384 }
385
386 // Now find the parent and try that first.
387 size_t last_slash = path.find_last_of('/');
388 if (last_slash == std::string::npos || last_slash == 0) {
389 PLOG(ERROR) << "Could not create " << path;
390 return false;
391 }
392
393 if (!CreatePath(path.substr(0, last_slash))) {
394 return false;
395 }
396
397 if (mkdir(path.c_str(), 0711) == 0) {
398 return true;
399 }
400 PLOG(ERROR) << "Could not create " << path;
401 return false;
402 }
403
404 static void ClearDirectory(const std::string& dir) {
405 DIR* c_dir = opendir(dir.c_str());
406 if (c_dir == nullptr) {
407 PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
408 return;
409 }
410
411 for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
412 const char* name = de->d_name;
413 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
414 continue;
415 }
416 // We only want to delete regular files and symbolic links.
417 std::string file = StringPrintf("%s/%s", dir.c_str(), name);
418 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
419 LOG(WARNING) << "Unexpected file "
420 << file
421 << " of type "
422 << std::hex
423 << de->d_type
424 << " encountered.";
425 } else {
426 // Try to unlink the file.
427 if (unlink(file.c_str()) != 0) {
428 PLOG(ERROR) << "Unable to unlink " << file;
429 }
430 }
431 }
432 CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
433 }
434
Andreas Gampe5709b572016-02-12 17:42:59 -0800435 bool Dex2oatBootImage(const std::string& boot_cp,
436 const std::string& art_path,
437 const std::string& oat_path,
Andreas Gamped089ca12016-06-27 14:25:30 -0700438 const char* isa) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800439 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
440 std::vector<std::string> cmd;
Roland Levillain67a14f62019-01-23 15:59:50 +0000441 cmd.push_back(kDex2oatPath);
Andreas Gampe73dae112015-11-19 14:12:14 -0800442 cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
Andreas Gampe6db8db92016-06-03 10:22:19 -0700443 for (const std::string& boot_part : Split(boot_cp, ":")) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800444 cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str()));
445 }
446 cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str()));
447
Paul Duffinb3f16852019-06-27 12:40:25 +0100448 int32_t base_offset = ChooseRelocationOffsetDelta(
449 art::imagevalues::GetImageMinBaseAddressDelta(),
450 art::imagevalues::GetImageMaxBaseAddressDelta());
451 cmd.push_back(StringPrintf("--base=0x%x",
452 art::imagevalues::GetImageBaseAddress() + base_offset));
Andreas Gampe73dae112015-11-19 14:12:14 -0800453
454 cmd.push_back(StringPrintf("--instruction-set=%s", isa));
455
456 // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp.
457 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms",
458 "-Xms",
459 true,
460 cmd);
461 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx",
462 "-Xmx",
463 true,
464 cmd);
465 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter",
466 "--compiler-filter=",
467 false,
468 cmd);
Mathieu Chartiereaa784c2019-06-27 15:04:55 -0700469 cmd.push_back("--profile-file=/system/etc/boot-image.prof");
Andreas Gampe73dae112015-11-19 14:12:14 -0800470 // TODO: Compiled-classes.
471 const std::string* extra_opts =
472 system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
473 if (extra_opts != nullptr) {
Andreas Gampe6db8db92016-06-03 10:22:19 -0700474 std::vector<std::string> extra_vals = Split(*extra_opts, " ");
Andreas Gampe73dae112015-11-19 14:12:14 -0800475 cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end());
476 }
477 // TODO: Should we lower this? It's usually set close to max, because
478 // normally there's not much else going on at boot.
479 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads",
480 "-j",
481 false,
482 cmd);
Orion Hodson45837462019-11-14 18:20:17 +0000483 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-cpu-set",
484 "--cpu-set=",
485 false,
486 cmd);
Andreas Gampe73dae112015-11-19 14:12:14 -0800487 AddCompilerOptionFromSystemProperty(
488 StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(),
489 "--instruction-set-variant=",
490 false,
491 cmd);
492 AddCompilerOptionFromSystemProperty(
493 StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(),
494 "--instruction-set-features=",
495 false,
496 cmd);
497
498 std::string error_msg;
499 bool result = Exec(cmd, &error_msg);
500 if (!result) {
501 LOG(ERROR) << "Could not generate boot image: " << error_msg;
502 }
503 return result;
504 }
505
506 static const char* ParseNull(const char* arg) {
507 return (strcmp(arg, "!") == 0) ? nullptr : arg;
508 }
509
Andreas Gamped089ca12016-06-27 14:25:30 -0700510 bool ShouldSkipPreopt() const {
Andreas Gampe56f79f92016-06-08 15:11:37 -0700511 // There's one thing we have to be careful about: we may/will be asked to compile an app
512 // living in the system image. This may be a valid request - if the app wasn't compiled,
513 // e.g., if the system image wasn't large enough to include preopted files. However, the
514 // data we have is from the old system, so the driver (the OTA service) can't actually
515 // know. Thus, we will get requests for apps that have preopted components. To avoid
516 // duplication (we'd generate files that are not used and are *not* cleaned up), do two
517 // simple checks:
518 //
519 // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image)
520 // (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.)
521 //
522 // 2) If you replace the name in the apk_path with "oat," does the path exist?
523 // (=have a subdirectory for preopted files)
524 //
525 // If the answer to both is yes, skip the dexopt.
526 //
527 // Note: while one may think it's OK to call dexopt and it will fail (because APKs should
528 // be stripped), that's not true for APKs signed outside the build system (so the
529 // jar content must be exactly the same).
530
531 // (This is ugly as it's the only thing where we need to understand the contents
Calin Juravlec9e76792018-02-01 14:44:56 +0000532 // of parameters_, but it beats postponing the decision or using the call-
Andreas Gampe56f79f92016-06-08 15:11:37 -0700533 // backs to do weird things.)
Calin Juravlec9e76792018-02-01 14:44:56 +0000534 const char* apk_path = parameters_.apk_path;
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700535 CHECK(apk_path != nullptr);
Elliott Hughes969e4f82017-12-20 12:34:09 -0800536 if (StartsWith(apk_path, android_root_)) {
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700537 const char* last_slash = strrchr(apk_path, '/');
Andreas Gampe56f79f92016-06-08 15:11:37 -0700538 if (last_slash != nullptr) {
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700539 std::string path(apk_path, last_slash - apk_path + 1);
Andreas Gampe56f79f92016-06-08 15:11:37 -0700540 CHECK(EndsWith(path, "/"));
541 path = path + "oat";
542 if (access(path.c_str(), F_OK) == 0) {
Calin Juravleb3591f62017-11-17 16:38:17 -0800543 LOG(INFO) << "Skipping A/B OTA preopt of already preopted package " << apk_path;
Andreas Gamped089ca12016-06-27 14:25:30 -0700544 return true;
Andreas Gampe56f79f92016-06-08 15:11:37 -0700545 }
546 }
547 }
548
Andreas Gamped089ca12016-06-27 14:25:30 -0700549 // Another issue is unavailability of files in the new system. If the partition
550 // layout changes, otapreopt_chroot may not know about this. Then files from that
551 // partition will not be available and fail to build. This is problematic, as
552 // this tool will wipe the OTA artifact cache and try again (for robustness after
553 // a failed OTA with remaining cache artifacts).
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700554 if (access(apk_path, F_OK) != 0) {
Calin Juravleb3591f62017-11-17 16:38:17 -0800555 LOG(WARNING) << "Skipping A/B OTA preopt of non-existing package " << apk_path;
Andreas Gamped089ca12016-06-27 14:25:30 -0700556 return true;
557 }
558
559 return false;
560 }
561
Calin Juravlec9e76792018-02-01 14:44:56 +0000562 // Run dexopt with the parameters of parameters_.
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800563 // TODO(calin): embed the profile name in the parameters.
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700564 int Dexopt() {
Andreas Gampe023b2242018-02-28 16:03:25 -0800565 std::string dummy;
Calin Juravlec9e76792018-02-01 14:44:56 +0000566 return dexopt(parameters_.apk_path,
567 parameters_.uid,
568 parameters_.pkgName,
569 parameters_.instruction_set,
570 parameters_.dexopt_needed,
571 parameters_.oat_dir,
572 parameters_.dexopt_flags,
573 parameters_.compiler_filter,
574 parameters_.volume_uuid,
575 parameters_.shared_libraries,
576 parameters_.se_info,
577 parameters_.downgrade,
578 parameters_.target_sdk_version,
Calin Juravlecc3b8ae2018-02-01 17:03:23 +0000579 parameters_.profile_name,
Calin Juravledcccd832018-02-13 18:31:32 -0800580 parameters_.dex_metadata_path,
Andreas Gampe023b2242018-02-28 16:03:25 -0800581 parameters_.compilation_reason,
582 &dummy);
Andreas Gampe73dae112015-11-19 14:12:14 -0800583 }
584
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700585 int RunPreopt() {
586 if (ShouldSkipPreopt()) {
587 return 0;
588 }
589
590 int dexopt_result = Dexopt();
591 if (dexopt_result == 0) {
592 return 0;
593 }
594
595 // If the dexopt failed, we may have a stale boot image from a previous OTA run.
596 // Then regenerate and retry.
597 if (WEXITSTATUS(dexopt_result) ==
Andreas Gampece9fe7f2018-09-18 10:25:58 -0700598 static_cast<int>(::art::dex2oat::ReturnCode::kCreateRuntime)) {
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700599 if (!PrepareBootImage(/* force */ true)) {
600 LOG(ERROR) << "Forced boot image creating failed. Original error return was "
601 << dexopt_result;
602 return dexopt_result;
603 }
604
605 int dexopt_result_boot_image_retry = Dexopt();
606 if (dexopt_result_boot_image_retry == 0) {
607 return 0;
608 }
609 }
610
611 // If this was a profile-guided run, we may have profile version issues. Try to downgrade,
612 // if possible.
Calin Juravlec9e76792018-02-01 14:44:56 +0000613 if ((parameters_.dexopt_flags & DEXOPT_PROFILE_GUIDED) == 0) {
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700614 return dexopt_result;
615 }
616
617 LOG(WARNING) << "Downgrading compiler filter in an attempt to progress compilation";
Calin Juravlec9e76792018-02-01 14:44:56 +0000618 parameters_.dexopt_flags &= ~DEXOPT_PROFILE_GUIDED;
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700619 return Dexopt();
620 }
621
Andreas Gampe73dae112015-11-19 14:12:14 -0800622 ////////////////////////////////////
623 // Helpers, mostly taken from ART //
624 ////////////////////////////////////
625
Andreas Gampe73dae112015-11-19 14:12:14 -0800626 // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
627 static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
628 constexpr size_t kPageSize = PAGE_SIZE;
629 CHECK_EQ(min_delta % kPageSize, 0u);
630 CHECK_EQ(max_delta % kPageSize, 0u);
631 CHECK_LT(min_delta, max_delta);
632
633 std::default_random_engine generator;
634 generator.seed(GetSeed());
635 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
636 int32_t r = distribution(generator);
637 if (r % 2 == 0) {
638 r = RoundUp(r, kPageSize);
639 } else {
640 r = RoundDown(r, kPageSize);
641 }
642 CHECK_LE(min_delta, r);
643 CHECK_GE(max_delta, r);
644 CHECK_EQ(r % kPageSize, 0u);
645 return r;
646 }
647
648 static uint64_t GetSeed() {
649#ifdef __BIONIC__
650 // Bionic exposes arc4random, use it.
651 uint64_t random_data;
652 arc4random_buf(&random_data, sizeof(random_data));
653 return random_data;
654#else
655#error "This is only supposed to run with bionic. Otherwise, implement..."
656#endif
657 }
658
659 void AddCompilerOptionFromSystemProperty(const char* system_property,
660 const char* prefix,
661 bool runtime,
Andreas Gamped089ca12016-06-27 14:25:30 -0700662 std::vector<std::string>& out) const {
663 const std::string* value = system_properties_.GetProperty(system_property);
Andreas Gampe73dae112015-11-19 14:12:14 -0800664 if (value != nullptr) {
665 if (runtime) {
666 out.push_back("--runtime-arg");
667 }
668 if (prefix != nullptr) {
669 out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
670 } else {
671 out.push_back(*value);
672 }
673 }
674 }
675
Andreas Gamped089ca12016-06-27 14:25:30 -0700676 static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
677 static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
678 static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA";
679 // The index of the instruction-set string inside the package parameters. Needed for
680 // some special-casing that requires knowledge of the instruction-set.
681 static constexpr size_t kISAIndex = 3;
682
Andreas Gampe73dae112015-11-19 14:12:14 -0800683 // Stores the system properties read out of the B partition. We need to use these properties
684 // to compile, instead of the A properties we could get from init/get_property.
685 SystemProperties system_properties_;
686
Andreas Gamped089ca12016-06-27 14:25:30 -0700687 // Some select properties that are always needed.
Andreas Gamped089ca12016-06-27 14:25:30 -0700688 std::string android_root_;
689 std::string android_data_;
690 std::string boot_classpath_;
691 std::string asec_mountpoint_;
692
Calin Juravlec9e76792018-02-01 14:44:56 +0000693 OTAPreoptParameters parameters_;
Andreas Gampe73dae112015-11-19 14:12:14 -0800694
695 // Store environment values we need to set.
696 std::vector<std::string> environ_;
697};
698
699OTAPreoptService gOps;
700
701////////////////////////
702// Plug-in functions. //
703////////////////////////
704
705int get_property(const char *key, char *value, const char *default_value) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800706 return gOps.GetProperty(key, value, default_value);
707}
708
709// Compute the output path of
710bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
711 const char *apk_path,
712 const char *instruction_set) {
Dan Austin9c8f93a2016-06-03 16:15:54 -0700713 const char *file_name_start;
714 const char *file_name_end;
Andreas Gampe73dae112015-11-19 14:12:14 -0800715
716 file_name_start = strrchr(apk_path, '/');
717 if (file_name_start == nullptr) {
718 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
719 return false;
720 }
721 file_name_end = strrchr(file_name_start, '.');
722 if (file_name_end == nullptr) {
723 ALOGE("apk_path '%s' has no extension\n", apk_path);
724 return false;
725 }
726
727 // Calculate file_name
728 file_name_start++; // Move past '/', is valid as file_name_end is valid.
729 size_t file_name_len = file_name_end - file_name_start;
730 std::string file_name(file_name_start, file_name_len);
731
732 // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
Andreas Gamped089ca12016-06-27 14:25:30 -0700733 snprintf(path,
734 PKG_PATH_MAX,
735 "%s/%s/%s.odex.%s",
736 oat_dir,
737 instruction_set,
738 file_name.c_str(),
739 gOps.GetTargetSlot().c_str());
Andreas Gampe73dae112015-11-19 14:12:14 -0800740 return true;
741}
742
743/*
744 * Computes the odex file for the given apk_path and instruction_set.
745 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
746 *
747 * Returns false if it failed to determine the odex file path.
748 */
749bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
750 const char *instruction_set) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800751 const char *path_end = strrchr(apk_path, '/');
752 if (path_end == nullptr) {
753 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
754 return false;
755 }
756 std::string path_component(apk_path, path_end - apk_path);
757
758 const char *name_begin = path_end + 1;
759 const char *extension_start = strrchr(name_begin, '.');
760 if (extension_start == nullptr) {
761 ALOGE("apk_path '%s' has no extension.\n", apk_path);
762 return false;
763 }
764 std::string name_component(name_begin, extension_start - name_begin);
765
Andreas Gamped089ca12016-06-27 14:25:30 -0700766 std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s",
Andreas Gampe73dae112015-11-19 14:12:14 -0800767 path_component.c_str(),
768 instruction_set,
Andreas Gamped089ca12016-06-27 14:25:30 -0700769 name_component.c_str(),
770 gOps.GetTargetSlot().c_str());
771 if (new_path.length() >= PKG_PATH_MAX) {
772 LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path;
773 return false;
774 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800775 strcpy(path, new_path.c_str());
776 return true;
777}
778
779bool create_cache_path(char path[PKG_PATH_MAX],
780 const char *src,
781 const char *instruction_set) {
782 size_t srclen = strlen(src);
783
784 /* demand that we are an absolute path */
785 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
786 return false;
787 }
788
789 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
790 return false;
791 }
792
793 std::string from_src = std::string(src + 1);
794 std::replace(from_src.begin(), from_src.end(), '/', '@');
795
796 std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
Andreas Gamped089ca12016-06-27 14:25:30 -0700797 gOps.GetOTADataDirectory().c_str(),
Andreas Gampe73dae112015-11-19 14:12:14 -0800798 DALVIK_CACHE,
799 instruction_set,
800 from_src.c_str(),
David Brazdil249c1792016-09-06 15:35:28 +0100801 DALVIK_CACHE_POSTFIX);
Andreas Gampe73dae112015-11-19 14:12:14 -0800802
803 if (assembled_path.length() + 1 > PKG_PATH_MAX) {
804 return false;
805 }
806 strcpy(path, assembled_path.c_str());
807
808 return true;
809}
810
Andreas Gampe73dae112015-11-19 14:12:14 -0800811static int log_callback(int type, const char *fmt, ...) {
812 va_list ap;
813 int priority;
814
815 switch (type) {
816 case SELINUX_WARNING:
817 priority = ANDROID_LOG_WARN;
818 break;
819 case SELINUX_INFO:
820 priority = ANDROID_LOG_INFO;
821 break;
822 default:
823 priority = ANDROID_LOG_ERROR;
824 break;
825 }
826 va_start(ap, fmt);
827 LOG_PRI_VA(priority, "SELinux", fmt, ap);
828 va_end(ap);
829 return 0;
830}
831
832static int otapreopt_main(const int argc, char *argv[]) {
833 int selinux_enabled = (is_selinux_enabled() > 0);
834
835 setenv("ANDROID_LOG_TAGS", "*:v", 1);
836 android::base::InitLogging(argv);
837
Andreas Gampe73dae112015-11-19 14:12:14 -0800838 if (argc < 2) {
839 ALOGE("Expecting parameters");
840 exit(1);
841 }
842
843 union selinux_callback cb;
844 cb.func_log = log_callback;
845 selinux_set_callback(SELINUX_CB_LOG, cb);
846
Andreas Gampe73dae112015-11-19 14:12:14 -0800847 if (selinux_enabled && selinux_status_open(true) < 0) {
848 ALOGE("Could not open selinux status; exiting.\n");
849 exit(1);
850 }
851
852 int ret = android::installd::gOps.Main(argc, argv);
853
854 return ret;
855}
856
857} // namespace installd
858} // namespace android
859
860int main(const int argc, char *argv[]) {
861 return android::installd::otapreopt_main(argc, argv);
862}