blob: 355fe456a29e83c144d460d112435d69d64e8a02 [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>
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 Gampe6db8db92016-06-03 10:22:19 -070034#include <android-base/strings.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 Juravledff47292018-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::Join;
61using android::base::Split;
Andreas Gampe56f79f92016-06-08 15:11:37 -070062using android::base::StartsWith;
Andreas Gampe73dae112015-11-19 14:12:14 -080063using android::base::StringPrintf;
64
65namespace android {
66namespace installd {
67
Andreas Gampeef21fd22017-05-22 13:36:06 -070068// 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
74static_assert(DEXOPT_PUBLIC == 1 << 1, "DEXOPT_PUBLIC unexpected.");
75static_assert(DEXOPT_DEBUGGABLE == 1 << 2, "DEXOPT_DEBUGGABLE unexpected.");
76static_assert(DEXOPT_BOOTCOMPLETE == 1 << 3, "DEXOPT_BOOTCOMPLETE unexpected.");
77static_assert(DEXOPT_PROFILE_GUIDED == 1 << 4, "DEXOPT_PROFILE_GUIDED unexpected.");
78static_assert(DEXOPT_SECONDARY_DEX == 1 << 5, "DEXOPT_SECONDARY_DEX unexpected.");
79static_assert(DEXOPT_FORCE == 1 << 6, "DEXOPT_FORCE unexpected.");
80static_assert(DEXOPT_STORAGE_CE == 1 << 7, "DEXOPT_STORAGE_CE unexpected.");
81static_assert(DEXOPT_STORAGE_DE == 1 << 8, "DEXOPT_STORAGE_DE unexpected.");
David Brazdil52249162018-02-12 18:04:59 -080082static_assert(DEXOPT_ENABLE_HIDDEN_API_CHECKS == 1 << 10,
83 "DEXOPT_ENABLE_HIDDEN_API_CHECKS unexpected");
Mathieu Chartierf69c2f72018-03-06 13:55:58 -080084static_assert(DEXOPT_GENERATE_COMPACT_DEX == 1 << 11, "DEXOPT_GENERATE_COMPACT_DEX unexpected");
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -070085static_assert(DEXOPT_GENERATE_APP_IMAGE == 1 << 12, "DEXOPT_GENERATE_APP_IMAGE unexpected");
Andreas Gampeef21fd22017-05-22 13:36:06 -070086
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -070087static_assert(DEXOPT_MASK == (0x1dfe | DEXOPT_IDLE_BACKGROUND_JOB),
Andreas Gamped32eec22018-02-28 16:02:51 -080088 "DEXOPT_MASK unexpected.");
Andreas Gampeef21fd22017-05-22 13:36:06 -070089
90
Andreas Gampea64a6272018-07-10 10:43:47 -070091template<typename T>
92static 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 Gampeef21fd22017-05-22 13:36:06 -070097
Andreas Gampe73dae112015-11-19 14:12:14 -080098template<typename T>
99static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
100 return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
101}
102
103template<typename T>
104static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
105 return RoundDown(x + n - 1, n);
106}
107
108class OTAPreoptService {
109 public:
Andreas Gampe73dae112015-11-19 14:12:14 -0800110 // 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 Gamped089ca12016-06-27 14:25:30 -0700122 if (!ReadArguments(argc, argv)) {
123 LOG(ERROR) << "Failed reading command line.";
124 return 1;
125 }
126
Andreas Gampe73dae112015-11-19 14:12:14 -0800127 if (!ReadSystemProperties()) {
128 LOG(ERROR)<< "Failed reading system properties.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700129 return 2;
Andreas Gampe73dae112015-11-19 14:12:14 -0800130 }
131
132 if (!ReadEnvironment()) {
133 LOG(ERROR) << "Failed reading environment properties.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700134 return 3;
Andreas Gampe73dae112015-11-19 14:12:14 -0800135 }
136
Andreas Gamped089ca12016-06-27 14:25:30 -0700137 if (!CheckAndInitializeInstalldGlobals()) {
138 LOG(ERROR) << "Failed initializing globals.";
139 return 4;
Andreas Gampe73dae112015-11-19 14:12:14 -0800140 }
141
142 PrepareEnvironment();
143
Andreas Gamped089ca12016-06-27 14:25:30 -0700144 if (!PrepareBootImage(/* force */ false)) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800145 LOG(ERROR) << "Failed preparing boot image.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700146 return 5;
Andreas Gampe73dae112015-11-19 14:12:14 -0800147 }
148
149 int dexopt_retcode = RunPreopt();
150
151 return dexopt_retcode;
152 }
153
Andreas Gamped089ca12016-06-27 14:25:30 -0700154 int GetProperty(const char* key, char* value, const char* default_value) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800155 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 Sharkeyc1149c92017-09-21 14:51:09 -0600161 strlcpy(value, default_value, kPropertyValueMax - 1);
Andreas Gampe73dae112015-11-19 14:12:14 -0800162 value[kPropertyValueMax - 1] = 0;
163 return strlen(default_value);// TODO: Need to truncate?
164 }
Andreas Gampe7e01b2b2017-09-26 20:41:48 -0700165 size_t size = std::min(kPropertyValueMax - 1, prop_value->length()) + 1;
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600166 strlcpy(value, prop_value->data(), size);
Andreas Gampe7e01b2b2017-09-26 20:41:48 -0700167 return static_cast<int>(size - 1);
Andreas Gampe73dae112015-11-19 14:12:14 -0800168 }
169
Andreas Gamped089ca12016-06-27 14:25:30 -0700170 std::string GetOTADataDirectory() const {
Calin Juravledff47292018-02-01 14:44:56 +0000171 return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), GetTargetSlot().c_str());
Andreas Gamped089ca12016-06-27 14:25:30 -0700172 }
173
174 const std::string& GetTargetSlot() const {
Calin Juravledff47292018-02-01 14:44:56 +0000175 return parameters_.target_slot;
Andreas Gamped089ca12016-06-27 14:25:30 -0700176 }
177
Andreas Gampe73dae112015-11-19 14:12:14 -0800178private:
Andreas Gamped089ca12016-06-27 14:25:30 -0700179
Andreas Gampe73dae112015-11-19 14:12:14 -0800180 bool ReadSystemProperties() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700181 static constexpr const char* kPropertyFiles[] = {
182 "/default.prop", "/system/build.prop"
183 };
Andreas Gampe73dae112015-11-19 14:12:14 -0800184
Andreas Gampe1842af32016-03-16 14:28:50 -0700185 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 Gampe73dae112015-11-19 14:12:14 -0800192 }
193
194 bool ReadEnvironment() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700195 // 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 Gampe73dae112015-11-19 14:12:14 -0800205
Andreas Gampe1842af32016-03-16 14:28:50 -0700206 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 Gampe73dae112015-11-19 14:12:14 -0800218 return false;
219 }
Andreas Gampe1842af32016-03-16 14:28:50 -0700220
Andreas Gamped089ca12016-06-27 14:25:30 -0700221 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 Gampe1842af32016-03-16 14:28:50 -0700276 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800277
278 return true;
279 }
280
Shubham Ajmera54ef8622017-06-22 11:10:27 -0700281 bool ParseBool(const char* in) {
282 if (strcmp(in, "true") == 0) {
283 return true;
284 }
285 return false;
286 }
287
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700288 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 Gamped089ca12016-06-27 14:25:30 -0700301
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700302 bool ReadArguments(int argc, char** argv) {
Calin Juravledff47292018-02-01 14:44:56 +0000303 return parameters_.ReadArguments(argc, const_cast<const char**>(argv));
Andreas Gampe73dae112015-11-19 14:12:14 -0800304 }
305
306 void PrepareEnvironment() {
Andreas Gamped089ca12016-06-27 14:25:30 -0700307 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 Gampe73dae112015-11-19 14:12:14 -0800310
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 Gamped089ca12016-06-27 14:25:30 -0700318 bool PrepareBootImage(bool force) const {
Calin Juravledff47292018-02-01 14:44:56 +0000319 if (parameters_.instruction_set == nullptr) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800320 LOG(ERROR) << "Instruction set missing.";
321 return false;
322 }
Calin Juravledff47292018-02-01 14:44:56 +0000323 const char* isa = parameters_.instruction_set;
Andreas Gampe73dae112015-11-19 14:12:14 -0800324
325 // Check whether the file exists where expected.
Andreas Gamped089ca12016-06-27 14:25:30 -0700326 std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE;
Andreas Gampe73dae112015-11-19 14:12:14 -0800327 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 Gamped089ca12016-06-27 14:25:30 -0700330 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 Gampe73dae112015-11-19 14:12:14 -0800338 }
339
Andreas Gamped089ca12016-06-27 14:25:30 -0700340 // Reset umask in otapreopt, so that we control the the access for the files we create.
341 umask(0);
342
Andreas Gampe73dae112015-11-19 14:12:14 -0800343 // Create the directories, if necessary.
344 if (access(dalvik_cache.c_str(), F_OK) != 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700345 if (!CreatePath(dalvik_cache)) {
346 PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache;
Andreas Gampe73dae112015-11-19 14:12:14 -0800347 return false;
348 }
349 }
350 if (access(isa_path.c_str(), F_OK) != 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700351 if (!CreatePath(isa_path)) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800352 PLOG(ERROR) << "Could not create dalvik-cache isa dir";
353 return false;
354 }
355 }
356
Andreas Gampe5709b572016-02-12 17:42:59 -0800357 // Prepare to create.
Andreas Gamped089ca12016-06-27 14:25:30 -0700358 if (!cleared) {
359 ClearDirectory(isa_path);
360 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800361
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700362 std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800363 if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
Chris Morin77c48752018-02-13 15:44:47 -0800364 return PatchoatBootImage(isa_path, isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800365 } else {
366 // No preopted boot image. Try to compile.
Andreas Gamped089ca12016-06-27 14:25:30 -0700367 return Dex2oatBootImage(boot_classpath_, art_path, oat_path, isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800368 }
369 }
370
Andreas Gamped089ca12016-06-27 14:25:30 -0700371 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 Morin77c48752018-02-13 15:44:47 -0800433 bool PatchoatBootImage(const std::string& output_dir, const char* isa) const {
Andreas Gampe5709b572016-02-12 17:42:59 -0800434 // 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 Gampe9fb85b02016-03-16 10:09:29 -0700437 cmd.push_back("/system/bin/patchoat");
Andreas Gampe5709b572016-02-12 17:42:59 -0800438
439 cmd.push_back("--input-image-location=/system/framework/boot.art");
Chris Morin77c48752018-02-13 15:44:47 -0800440 cmd.push_back(StringPrintf("--output-image-directory=%s", output_dir.c_str()));
Andreas Gampe5709b572016-02-12 17:42:59 -0800441
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 Gampefebf0bf2016-02-29 18:04:17 -0800446 cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset));
Andreas Gampe5709b572016-02-12 17:42:59 -0800447
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 Gamped089ca12016-06-27 14:25:30 -0700459 const char* isa) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800460 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
461 std::vector<std::string> cmd;
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700462 cmd.push_back("/system/bin/dex2oat");
Andreas Gampe73dae112015-11-19 14:12:14 -0800463 cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
Andreas Gampe6db8db92016-06-03 10:22:19 -0700464 for (const std::string& boot_part : Split(boot_cp, ":")) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800465 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 Gampe9fb85b02016-03-16 10:09:29 -0700488 cmd.push_back("--image-classes=/system/etc/preloaded-classes");
Andreas Gampe73dae112015-11-19 14:12:14 -0800489 // TODO: Compiled-classes.
490 const std::string* extra_opts =
491 system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
492 if (extra_opts != nullptr) {
Andreas Gampe6db8db92016-06-03 10:22:19 -0700493 std::vector<std::string> extra_vals = Split(*extra_opts, " ");
Andreas Gampe73dae112015-11-19 14:12:14 -0800494 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 Gamped089ca12016-06-27 14:25:30 -0700525 bool ShouldSkipPreopt() const {
Andreas Gampe56f79f92016-06-08 15:11:37 -0700526 // 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 Juravledff47292018-02-01 14:44:56 +0000547 // of parameters_, but it beats postponing the decision or using the call-
Andreas Gampe56f79f92016-06-08 15:11:37 -0700548 // backs to do weird things.)
Calin Juravledff47292018-02-01 14:44:56 +0000549 const char* apk_path = parameters_.apk_path;
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700550 CHECK(apk_path != nullptr);
Elliott Hughes969e4f82017-12-20 12:34:09 -0800551 if (StartsWith(apk_path, android_root_)) {
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700552 const char* last_slash = strrchr(apk_path, '/');
Andreas Gampe56f79f92016-06-08 15:11:37 -0700553 if (last_slash != nullptr) {
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700554 std::string path(apk_path, last_slash - apk_path + 1);
Andreas Gampe56f79f92016-06-08 15:11:37 -0700555 CHECK(EndsWith(path, "/"));
556 path = path + "oat";
557 if (access(path.c_str(), F_OK) == 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700558 return true;
Andreas Gampe56f79f92016-06-08 15:11:37 -0700559 }
560 }
561 }
562
Andreas Gamped089ca12016-06-27 14:25:30 -0700563 // Another issue is unavailability of files in the new system. If the partition
564 // layout changes, otapreopt_chroot may not know about this. Then files from that
565 // partition will not be available and fail to build. This is problematic, as
566 // this tool will wipe the OTA artifact cache and try again (for robustness after
567 // a failed OTA with remaining cache artifacts).
Andreas Gampec4ced4f2017-04-14 20:39:56 -0700568 if (access(apk_path, F_OK) != 0) {
569 LOG(WARNING) << "Skipping preopt of non-existing package " << apk_path;
Andreas Gamped089ca12016-06-27 14:25:30 -0700570 return true;
571 }
572
573 return false;
574 }
575
Calin Juravledff47292018-02-01 14:44:56 +0000576 // Run dexopt with the parameters of parameters_.
Calin Juravle824a64d2018-01-18 20:23:17 -0800577 // TODO(calin): embed the profile name in the parameters.
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700578 int Dexopt() {
Andreas Gampe023b2242018-02-28 16:03:25 -0800579 std::string dummy;
Calin Juravledff47292018-02-01 14:44:56 +0000580 return dexopt(parameters_.apk_path,
581 parameters_.uid,
582 parameters_.pkgName,
583 parameters_.instruction_set,
584 parameters_.dexopt_needed,
585 parameters_.oat_dir,
586 parameters_.dexopt_flags,
587 parameters_.compiler_filter,
588 parameters_.volume_uuid,
589 parameters_.shared_libraries,
590 parameters_.se_info,
591 parameters_.downgrade,
592 parameters_.target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000593 parameters_.profile_name,
Calin Juravle2efc4022018-02-13 18:31:32 -0800594 parameters_.dex_metadata_path,
Andreas Gampe023b2242018-02-28 16:03:25 -0800595 parameters_.compilation_reason,
596 &dummy);
Andreas Gampe73dae112015-11-19 14:12:14 -0800597 }
598
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700599 int RunPreopt() {
600 if (ShouldSkipPreopt()) {
601 return 0;
602 }
603
604 int dexopt_result = Dexopt();
605 if (dexopt_result == 0) {
606 return 0;
607 }
608
609 // If the dexopt failed, we may have a stale boot image from a previous OTA run.
610 // Then regenerate and retry.
611 if (WEXITSTATUS(dexopt_result) ==
612 static_cast<int>(art::dex2oat::ReturnCode::kCreateRuntime)) {
613 if (!PrepareBootImage(/* force */ true)) {
614 LOG(ERROR) << "Forced boot image creating failed. Original error return was "
615 << dexopt_result;
616 return dexopt_result;
617 }
618
619 int dexopt_result_boot_image_retry = Dexopt();
620 if (dexopt_result_boot_image_retry == 0) {
621 return 0;
622 }
623 }
624
625 // If this was a profile-guided run, we may have profile version issues. Try to downgrade,
626 // if possible.
Calin Juravledff47292018-02-01 14:44:56 +0000627 if ((parameters_.dexopt_flags & DEXOPT_PROFILE_GUIDED) == 0) {
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700628 return dexopt_result;
629 }
630
631 LOG(WARNING) << "Downgrading compiler filter in an attempt to progress compilation";
Calin Juravledff47292018-02-01 14:44:56 +0000632 parameters_.dexopt_flags &= ~DEXOPT_PROFILE_GUIDED;
Andreas Gampeb39d2f02017-04-17 20:04:02 -0700633 return Dexopt();
634 }
635
Andreas Gampe73dae112015-11-19 14:12:14 -0800636 ////////////////////////////////////
637 // Helpers, mostly taken from ART //
638 ////////////////////////////////////
639
640 // Wrapper on fork/execv to run a command in a subprocess.
Andreas Gamped089ca12016-06-27 14:25:30 -0700641 static bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
Andreas Gampe6db8db92016-06-03 10:22:19 -0700642 const std::string command_line = Join(arg_vector, ' ');
Andreas Gampe73dae112015-11-19 14:12:14 -0800643
644 CHECK_GE(arg_vector.size(), 1U) << command_line;
645
646 // Convert the args to char pointers.
647 const char* program = arg_vector[0].c_str();
648 std::vector<char*> args;
649 for (size_t i = 0; i < arg_vector.size(); ++i) {
650 const std::string& arg = arg_vector[i];
651 char* arg_str = const_cast<char*>(arg.c_str());
652 CHECK(arg_str != nullptr) << i;
653 args.push_back(arg_str);
654 }
655 args.push_back(nullptr);
656
657 // Fork and exec.
658 pid_t pid = fork();
659 if (pid == 0) {
660 // No allocation allowed between fork and exec.
661
662 // Change process groups, so we don't get reaped by ProcessManager.
663 setpgid(0, 0);
664
665 execv(program, &args[0]);
666
667 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
668 // _exit to avoid atexit handlers in child.
669 _exit(1);
670 } else {
671 if (pid == -1) {
672 *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
673 command_line.c_str(), strerror(errno));
674 return false;
675 }
676
677 // wait for subprocess to finish
678 int status;
679 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
680 if (got_pid != pid) {
681 *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
682 "wanted %d, got %d: %s",
683 command_line.c_str(), pid, got_pid, strerror(errno));
684 return false;
685 }
686 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
687 *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
688 command_line.c_str());
689 return false;
690 }
691 }
692 return true;
693 }
694
695 // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
696 static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
697 constexpr size_t kPageSize = PAGE_SIZE;
698 CHECK_EQ(min_delta % kPageSize, 0u);
699 CHECK_EQ(max_delta % kPageSize, 0u);
700 CHECK_LT(min_delta, max_delta);
701
702 std::default_random_engine generator;
703 generator.seed(GetSeed());
704 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
705 int32_t r = distribution(generator);
706 if (r % 2 == 0) {
707 r = RoundUp(r, kPageSize);
708 } else {
709 r = RoundDown(r, kPageSize);
710 }
711 CHECK_LE(min_delta, r);
712 CHECK_GE(max_delta, r);
713 CHECK_EQ(r % kPageSize, 0u);
714 return r;
715 }
716
717 static uint64_t GetSeed() {
718#ifdef __BIONIC__
719 // Bionic exposes arc4random, use it.
720 uint64_t random_data;
721 arc4random_buf(&random_data, sizeof(random_data));
722 return random_data;
723#else
724#error "This is only supposed to run with bionic. Otherwise, implement..."
725#endif
726 }
727
728 void AddCompilerOptionFromSystemProperty(const char* system_property,
729 const char* prefix,
730 bool runtime,
Andreas Gamped089ca12016-06-27 14:25:30 -0700731 std::vector<std::string>& out) const {
732 const std::string* value = system_properties_.GetProperty(system_property);
Andreas Gampe73dae112015-11-19 14:12:14 -0800733 if (value != nullptr) {
734 if (runtime) {
735 out.push_back("--runtime-arg");
736 }
737 if (prefix != nullptr) {
738 out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
739 } else {
740 out.push_back(*value);
741 }
742 }
743 }
744
Andreas Gamped089ca12016-06-27 14:25:30 -0700745 static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
746 static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
747 static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA";
748 // The index of the instruction-set string inside the package parameters. Needed for
749 // some special-casing that requires knowledge of the instruction-set.
750 static constexpr size_t kISAIndex = 3;
751
Andreas Gampe73dae112015-11-19 14:12:14 -0800752 // Stores the system properties read out of the B partition. We need to use these properties
753 // to compile, instead of the A properties we could get from init/get_property.
754 SystemProperties system_properties_;
755
Andreas Gamped089ca12016-06-27 14:25:30 -0700756 // Some select properties that are always needed.
Andreas Gamped089ca12016-06-27 14:25:30 -0700757 std::string android_root_;
758 std::string android_data_;
759 std::string boot_classpath_;
760 std::string asec_mountpoint_;
761
Calin Juravledff47292018-02-01 14:44:56 +0000762 OTAPreoptParameters parameters_;
Andreas Gampe73dae112015-11-19 14:12:14 -0800763
764 // Store environment values we need to set.
765 std::vector<std::string> environ_;
766};
767
768OTAPreoptService gOps;
769
770////////////////////////
771// Plug-in functions. //
772////////////////////////
773
774int get_property(const char *key, char *value, const char *default_value) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800775 return gOps.GetProperty(key, value, default_value);
776}
777
778// Compute the output path of
779bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
780 const char *apk_path,
781 const char *instruction_set) {
Dan Austin9c8f93a2016-06-03 16:15:54 -0700782 const char *file_name_start;
783 const char *file_name_end;
Andreas Gampe73dae112015-11-19 14:12:14 -0800784
785 file_name_start = strrchr(apk_path, '/');
786 if (file_name_start == nullptr) {
787 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
788 return false;
789 }
790 file_name_end = strrchr(file_name_start, '.');
791 if (file_name_end == nullptr) {
792 ALOGE("apk_path '%s' has no extension\n", apk_path);
793 return false;
794 }
795
796 // Calculate file_name
797 file_name_start++; // Move past '/', is valid as file_name_end is valid.
798 size_t file_name_len = file_name_end - file_name_start;
799 std::string file_name(file_name_start, file_name_len);
800
801 // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
Andreas Gamped089ca12016-06-27 14:25:30 -0700802 snprintf(path,
803 PKG_PATH_MAX,
804 "%s/%s/%s.odex.%s",
805 oat_dir,
806 instruction_set,
807 file_name.c_str(),
808 gOps.GetTargetSlot().c_str());
Andreas Gampe73dae112015-11-19 14:12:14 -0800809 return true;
810}
811
812/*
813 * Computes the odex file for the given apk_path and instruction_set.
814 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
815 *
816 * Returns false if it failed to determine the odex file path.
817 */
818bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
819 const char *instruction_set) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800820 const char *path_end = strrchr(apk_path, '/');
821 if (path_end == nullptr) {
822 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
823 return false;
824 }
825 std::string path_component(apk_path, path_end - apk_path);
826
827 const char *name_begin = path_end + 1;
828 const char *extension_start = strrchr(name_begin, '.');
829 if (extension_start == nullptr) {
830 ALOGE("apk_path '%s' has no extension.\n", apk_path);
831 return false;
832 }
833 std::string name_component(name_begin, extension_start - name_begin);
834
Andreas Gamped089ca12016-06-27 14:25:30 -0700835 std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s",
Andreas Gampe73dae112015-11-19 14:12:14 -0800836 path_component.c_str(),
837 instruction_set,
Andreas Gamped089ca12016-06-27 14:25:30 -0700838 name_component.c_str(),
839 gOps.GetTargetSlot().c_str());
840 if (new_path.length() >= PKG_PATH_MAX) {
841 LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path;
842 return false;
843 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800844 strcpy(path, new_path.c_str());
845 return true;
846}
847
848bool create_cache_path(char path[PKG_PATH_MAX],
849 const char *src,
850 const char *instruction_set) {
851 size_t srclen = strlen(src);
852
853 /* demand that we are an absolute path */
854 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
855 return false;
856 }
857
858 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
859 return false;
860 }
861
862 std::string from_src = std::string(src + 1);
863 std::replace(from_src.begin(), from_src.end(), '/', '@');
864
865 std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
Andreas Gamped089ca12016-06-27 14:25:30 -0700866 gOps.GetOTADataDirectory().c_str(),
Andreas Gampe73dae112015-11-19 14:12:14 -0800867 DALVIK_CACHE,
868 instruction_set,
869 from_src.c_str(),
David Brazdil249c1792016-09-06 15:35:28 +0100870 DALVIK_CACHE_POSTFIX);
Andreas Gampe73dae112015-11-19 14:12:14 -0800871
872 if (assembled_path.length() + 1 > PKG_PATH_MAX) {
873 return false;
874 }
875 strcpy(path, assembled_path.c_str());
876
877 return true;
878}
879
Andreas Gampe73dae112015-11-19 14:12:14 -0800880static int log_callback(int type, const char *fmt, ...) {
881 va_list ap;
882 int priority;
883
884 switch (type) {
885 case SELINUX_WARNING:
886 priority = ANDROID_LOG_WARN;
887 break;
888 case SELINUX_INFO:
889 priority = ANDROID_LOG_INFO;
890 break;
891 default:
892 priority = ANDROID_LOG_ERROR;
893 break;
894 }
895 va_start(ap, fmt);
896 LOG_PRI_VA(priority, "SELinux", fmt, ap);
897 va_end(ap);
898 return 0;
899}
900
901static int otapreopt_main(const int argc, char *argv[]) {
902 int selinux_enabled = (is_selinux_enabled() > 0);
903
904 setenv("ANDROID_LOG_TAGS", "*:v", 1);
905 android::base::InitLogging(argv);
906
Andreas Gampe73dae112015-11-19 14:12:14 -0800907 if (argc < 2) {
908 ALOGE("Expecting parameters");
909 exit(1);
910 }
911
912 union selinux_callback cb;
913 cb.func_log = log_callback;
914 selinux_set_callback(SELINUX_CB_LOG, cb);
915
Andreas Gampe73dae112015-11-19 14:12:14 -0800916 if (selinux_enabled && selinux_status_open(true) < 0) {
917 ALOGE("Could not open selinux status; exiting.\n");
918 exit(1);
919 }
920
921 int ret = android::installd::gOps.Main(argc, argv);
922
923 return ret;
924}
925
926} // namespace installd
927} // namespace android
928
929int main(const int argc, char *argv[]) {
930 return android::installd::otapreopt_main(argc, argv);
931}