blob: b6f42ad17236f4194f2ce47c164d5904406804b3 [file] [log] [blame]
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001/*
2 * Copyright (C) 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 */
Alan Stokescb27c342018-04-20 17:09:25 +010016#define LOG_TAG "installd"
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070017
Jeff Sharkey90aff262016-12-12 14:28:24 -070018#include <fcntl.h>
Keun young Park65578d62021-06-28 17:52:05 -070019#include <signal.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070020#include <stdlib.h>
21#include <string.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070022#include <sys/capability.h>
23#include <sys/file.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070024#include <sys/stat.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070025#include <sys/time.h>
26#include <sys/types.h>
27#include <sys/resource.h>
28#include <sys/wait.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070029#include <unistd.h>
30
Keun young Park65578d62021-06-28 17:52:05 -070031#include <array>
Andreas Gampe023b2242018-02-28 16:03:25 -080032#include <iomanip>
Keun young Park65578d62021-06-28 17:52:05 -070033#include <mutex>
34#include <unordered_set>
Andreas Gampe023b2242018-02-28 16:03:25 -080035
Alan Stokesa25d90c2017-10-16 10:56:00 +010036#include <android-base/file.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070037#include <android-base/logging.h>
Keun young Park65578d62021-06-28 17:52:05 -070038#include <android-base/no_destructor.h>
Andreas Gampe6a9cf722017-07-24 16:49:10 -070039#include <android-base/properties.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070040#include <android-base/stringprintf.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070041#include <android-base/strings.h>
42#include <android-base/unique_fd.h>
Martijn Coenen6de402a2021-04-26 16:23:40 +020043#include <async_safe/log.h>
Calin Juravle80a21252017-01-17 14:43:25 -080044#include <cutils/fs.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070045#include <cutils/properties.h>
46#include <cutils/sched_policy.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070047#include <log/log.h> // TODO: Move everything to base/logging.
Alan Stokesa25d90c2017-10-16 10:56:00 +010048#include <openssl/sha.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070049#include <private/android_filesystem_config.h>
Wei Wang0efe8d52021-07-07 17:32:38 -070050#include <processgroup/processgroup.h>
Calin Juravlecb556e32017-04-04 20:22:50 -070051#include <selinux/android.h>
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +000052#include <server_configurable_flags/get_flags.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070053#include <system/thread_defs.h>
Keun young Park65578d62021-06-28 17:52:05 -070054#include <utils/Mutex.h>
Yang Tianping8f2a40e2021-10-29 14:39:02 +080055#include <ziparchive/zip_archive.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070056
57#include "dexopt.h"
Andreas Gampefa2dadd2018-02-28 19:52:47 -080058#include "dexopt_return_codes.h"
Victor Hsiehc9821f12020-08-07 11:32:29 -070059#include "execv_helper.h"
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060060#include "globals.h"
Calin Juravle3b758282021-06-08 08:04:52 -070061#include "installd_constants.h"
Keun young Parkb7342262021-10-25 08:09:27 -070062#include "installd_deps.h"
Jeff Sharkey90aff262016-12-12 14:28:24 -070063#include "otapreopt_utils.h"
Keun young Parkb7342262021-10-25 08:09:27 -070064#include "restorable_file.h"
Victor Hsiehc9821f12020-08-07 11:32:29 -070065#include "run_dex2oat.h"
Victor Hsiehcb35a062020-08-13 16:11:13 -070066#include "unique_file.h"
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070067#include "utils.h"
68
Victor Hsieh76f19ba2020-08-10 14:37:26 -070069using android::base::Basename;
Jeff Sharkey90aff262016-12-12 14:28:24 -070070using android::base::EndsWith;
Mathieu Chartier9b2da082018-10-26 13:23:11 -070071using android::base::GetBoolProperty;
72using android::base::GetProperty;
David Brazdil4f6027a2019-03-19 11:44:21 +000073using android::base::ReadFdToString;
Alan Stokesa25d90c2017-10-16 10:56:00 +010074using android::base::ReadFully;
75using android::base::StringPrintf;
76using android::base::WriteFully;
Calin Juravle1a0af3b2017-03-09 14:33:33 -080077using android::base::unique_fd;
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070078
Keun young Park65578d62021-06-28 17:52:05 -070079namespace {
80
81class DexOptStatus {
82 public:
83 // Check if dexopt is cancelled and fork if it is not cancelled.
84 // cancelled is set to true if cancelled. Otherwise it will be set to false.
85 // If it is not cancelled, it will return the return value of fork() call.
86 // If cancelled, fork will not happen and it will return -1.
87 pid_t check_cancellation_and_fork(/* out */ bool *cancelled) {
88 std::lock_guard<std::mutex> lock(dexopt_lock_);
89 if (dexopt_blocked_) {
90 *cancelled = true;
91 return -1;
92 }
93 pid_t pid = fork();
94 *cancelled = false;
95 if (pid > 0) { // parent
96 dexopt_pids_.insert(pid);
97 }
98 return pid;
99 }
100
101 // Returns true if pid was killed (is in killed list). It could have finished if killing
102 // happened after the process is finished.
103 bool check_if_killed_and_remove_dexopt_pid(pid_t pid) {
104 std::lock_guard<std::mutex> lock(dexopt_lock_);
105 dexopt_pids_.erase(pid);
106 if (dexopt_killed_pids_.erase(pid) == 1) {
107 return true;
108 }
109 return false;
110 }
111
112 // Tells whether dexopt is blocked or not.
113 bool is_dexopt_blocked() {
114 std::lock_guard<std::mutex> lock(dexopt_lock_);
115 return dexopt_blocked_;
116 }
117
118 // Enable or disable dexopt blocking.
119 void control_dexopt_blocking(bool block) {
120 std::lock_guard<std::mutex> lock(dexopt_lock_);
121 dexopt_blocked_ = block;
122 if (!block) {
123 return;
124 }
125 // Blocked, also kill currently running tasks
126 for (auto pid : dexopt_pids_) {
127 LOG(INFO) << "control_dexopt_blocking kill pid:" << pid;
128 kill(pid, SIGKILL);
129 dexopt_killed_pids_.insert(pid);
130 }
131 dexopt_pids_.clear();
132 }
133
134 private:
135 std::mutex dexopt_lock_;
136 // when true, dexopt is blocked and will not run.
137 bool dexopt_blocked_ GUARDED_BY(dexopt_lock_) = false;
138 // PIDs of child process while runinng dexopt.
139 // If the child process is finished, it should be removed.
140 std::unordered_set<pid_t> dexopt_pids_ GUARDED_BY(dexopt_lock_);
141 // PIDs of child processes killed by cancellation.
142 std::unordered_set<pid_t> dexopt_killed_pids_ GUARDED_BY(dexopt_lock_);
143};
144
145android::base::NoDestructor<DexOptStatus> dexopt_status_;
146
147} // namespace
148
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700149namespace android {
150namespace installd {
151
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800152
Calin Juravle114f0812017-03-08 19:05:07 -0800153// Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below.
154struct FreeDelete {
155 // NOTE: Deleting a const object is valid but free() takes a non-const pointer.
156 void operator()(const void* ptr) const {
157 free(const_cast<void*>(ptr));
158 }
159};
160
161// Alias for std::unique_ptr<> that uses the C function free() to delete objects.
162template <typename T>
163using UniqueCPtr = std::unique_ptr<T, FreeDelete>;
164
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800165static unique_fd invalid_unique_fd() {
166 return unique_fd(-1);
167}
168
Andreas Gampe6a9cf722017-07-24 16:49:10 -0700169static bool is_debug_runtime() {
170 return android::base::GetProperty("persist.sys.dalvik.vm.lib.2", "") == "libartd.so";
171}
172
David Sehra3b5ab62017-10-25 14:27:29 -0700173static bool is_debuggable_build() {
174 return android::base::GetBoolProperty("ro.debuggable", false);
175}
176
Jeff Sharkey90aff262016-12-12 14:28:24 -0700177static bool clear_profile(const std::string& profile) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800178 unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700179 if (ufd.get() < 0) {
180 if (errno != ENOENT) {
181 PLOG(WARNING) << "Could not open profile " << profile;
182 return false;
183 } else {
184 // Nothing to clear. That's ok.
185 return true;
186 }
187 }
188
189 if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) {
190 if (errno != EWOULDBLOCK) {
191 PLOG(WARNING) << "Error locking profile " << profile;
192 }
193 // This implies that the app owning this profile is running
194 // (and has acquired the lock).
195 //
196 // If we can't acquire the lock bail out since clearing is useless anyway
197 // (the app will write again to the profile).
198 //
199 // Note:
200 // This does not impact the this is not an issue for the profiling correctness.
201 // In case this is needed because of an app upgrade, profiles will still be
202 // eventually cleared by the app itself due to checksum mismatch.
203 // If this is needed because profman advised, then keeping the data around
204 // until the next run is again not an issue.
205 //
206 // If the app attempts to acquire a lock while we've held one here,
207 // it will simply skip the current write cycle.
208 return false;
209 }
210
211 bool truncated = ftruncate(ufd.get(), 0) == 0;
212 if (!truncated) {
213 PLOG(WARNING) << "Could not truncate " << profile;
214 }
215 if (flock(ufd.get(), LOCK_UN) != 0) {
216 PLOG(WARNING) << "Error unlocking profile " << profile;
217 }
218 return truncated;
219}
220
Calin Juravle114f0812017-03-08 19:05:07 -0800221// Clear the reference profile for the given location.
Calin Juravle824a64d2018-01-18 20:23:17 -0800222// The location is the profile name for primary apks or the dex path for secondary dex files.
223static bool clear_reference_profile(const std::string& package_name, const std::string& location,
224 bool is_secondary_dex) {
225 return clear_profile(create_reference_profile_path(package_name, location, is_secondary_dex));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700226}
227
Calin Juravle114f0812017-03-08 19:05:07 -0800228// Clear the reference profile for the given location.
Calin Juravle824a64d2018-01-18 20:23:17 -0800229// The location is the profile name for primary apks or the dex path for secondary dex files.
230static bool clear_current_profile(const std::string& package_name, const std::string& location,
231 userid_t user, bool is_secondary_dex) {
232 return clear_profile(create_current_profile_path(user, package_name, location,
233 is_secondary_dex));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700234}
235
Calin Juravle114f0812017-03-08 19:05:07 -0800236// Clear the reference profile for the primary apk of the given package.
Calin Juravle824a64d2018-01-18 20:23:17 -0800237// The location is the profile name for primary apks or the dex path for secondary dex files.
238bool clear_primary_reference_profile(const std::string& package_name,
239 const std::string& location) {
240 return clear_reference_profile(package_name, location, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800241}
242
243// Clear all current profile for the primary apk of the given package.
Calin Juravle824a64d2018-01-18 20:23:17 -0800244// The location is the profile name for primary apks or the dex path for secondary dex files.
245bool clear_primary_current_profiles(const std::string& package_name, const std::string& location) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700246 bool success = true;
Calin Juravle114f0812017-03-08 19:05:07 -0800247 // For secondary dex files, we don't really need the user but we use it for sanity checks.
Jeff Sharkey90aff262016-12-12 14:28:24 -0700248 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
249 for (auto user : users) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800250 success &= clear_current_profile(package_name, location, user, /*is_secondary_dex*/false);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700251 }
252 return success;
253}
254
Calin Juravle114f0812017-03-08 19:05:07 -0800255// Clear the current profile for the primary apk of the given package and user.
Calin Juravle824a64d2018-01-18 20:23:17 -0800256bool clear_primary_current_profile(const std::string& package_name, const std::string& location,
257 userid_t user) {
258 return clear_current_profile(package_name, location, user, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800259}
260
Calin Juravlef74a7372019-02-28 20:29:41 -0800261// Determines which binary we should use for execution (the debug or non-debug version).
262// e.g. dex2oatd vs dex2oat
263static const char* select_execution_binary(const char* binary, const char* debug_binary,
264 bool background_job_compile) {
265 return select_execution_binary(
266 binary,
267 debug_binary,
268 background_job_compile,
269 is_debug_runtime(),
270 (android::base::GetProperty("ro.build.version.codename", "") == "REL"),
271 is_debuggable_build());
272}
273
274// Determines which binary we should use for execution (the debug or non-debug version).
275// e.g. dex2oatd vs dex2oat
276// This is convenient method which is much easier to test because it doesn't read
277// system properties.
278const char* select_execution_binary(
279 const char* binary,
280 const char* debug_binary,
281 bool background_job_compile,
282 bool is_debug_runtime,
283 bool is_release,
284 bool is_debuggable_build) {
285 // Do not use debug binaries for release candidates (to give more soak time).
286 bool is_debug_bg_job = background_job_compile && is_debuggable_build && !is_release;
287
288 // If the runtime was requested to use libartd.so, we'll run the debug version - assuming
289 // the file is present (it may not be on images with very little space available).
290 bool useDebug = (is_debug_runtime || is_debug_bg_job) && (access(debug_binary, X_OK) == 0);
291
292 return useDebug ? debug_binary : binary;
293}
294
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000295// Namespace for Android Runtime flags applied during boot time.
296static const char* RUNTIME_NATIVE_BOOT_NAMESPACE = "runtime_native_boot";
297// Feature flag name for running the JIT in Zygote experiment, b/119800099.
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000298static const char* ENABLE_JITZYGOTE_IMAGE = "enable_apex_image";
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000299
Mathieu Chartiere97261e2019-10-01 15:36:01 -0700300// Phenotype property name for enabling profiling the boot class path.
301static const char* PROFILE_BOOT_CLASS_PATH = "profilebootclasspath";
302
Calin Juravlef85ddb92020-05-01 14:05:40 -0700303static bool IsBootClassPathProfilingEnable() {
304 std::string profile_boot_class_path = GetProperty("dalvik.vm.profilebootclasspath", "");
305 profile_boot_class_path =
306 server_configurable_flags::GetServerConfigurableFlag(
307 RUNTIME_NATIVE_BOOT_NAMESPACE,
308 PROFILE_BOOT_CLASS_PATH,
309 /*default_value=*/ profile_boot_class_path);
310 return profile_boot_class_path == "true";
311}
312
Jeff Sharkey90aff262016-12-12 14:28:24 -0700313/*
314 * Whether dexopt should use a swap file when compiling an APK.
315 *
316 * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
317 * itself, anyways).
318 *
319 * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
320 *
321 * Otherwise, return true if this is a low-mem device.
322 *
323 * Otherwise, return default value.
324 */
325static bool kAlwaysProvideSwapFile = false;
326static bool kDefaultProvideSwapFile = true;
327
328static bool ShouldUseSwapFileForDexopt() {
329 if (kAlwaysProvideSwapFile) {
330 return true;
331 }
332
333 // Check the "override" property. If it exists, return value == "true".
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700334 std::string dex2oat_prop_buf = GetProperty("dalvik.vm.dex2oat-swap", "");
335 if (!dex2oat_prop_buf.empty()) {
336 return dex2oat_prop_buf == "true";
Jeff Sharkey90aff262016-12-12 14:28:24 -0700337 }
338
339 // Shortcut for default value. This is an implementation optimization for the process sketched
340 // above. If the default value is true, we can avoid to check whether this is a low-mem device,
341 // as low-mem is never returning false. The compiler will optimize this away if it can.
342 if (kDefaultProvideSwapFile) {
343 return true;
344 }
345
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700346 if (GetBoolProperty("ro.config.low_ram", false)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700347 return true;
348 }
349
350 // Default value must be false here.
351 return kDefaultProvideSwapFile;
352}
353
Richard Uhler76cc0272016-12-08 10:46:35 +0000354static void SetDex2OatScheduling(bool set_to_bg) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700355 if (set_to_bg) {
Wei Wang0efe8d52021-07-07 17:32:38 -0700356 if (!SetTaskProfiles(0, {"Dex2OatBootComplete"})) {
357 LOG(ERROR) << "Failed to set dex2oat task profile";
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800358 exit(DexoptReturnCodes::kSetSchedPolicy);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700359 }
360 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800361 PLOG(ERROR) << "setpriority failed";
362 exit(DexoptReturnCodes::kSetPriority);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700363 }
364 }
365}
366
Calin Juravlee01f8712021-06-03 18:16:54 -0700367static unique_fd create_profile(uid_t uid, const std::string& profile, int32_t flags, mode_t mode) {
368 unique_fd fd(TEMP_FAILURE_RETRY(open(profile.c_str(), flags, mode)));
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800369 if (fd.get() < 0) {
Calin Juravle29591732017-11-20 17:46:19 -0800370 if (errno != EEXIST) {
Calin Juravle114f0812017-03-08 19:05:07 -0800371 PLOG(ERROR) << "Failed to create profile " << profile;
Calin Juravle29591732017-11-20 17:46:19 -0800372 return invalid_unique_fd();
Calin Juravle114f0812017-03-08 19:05:07 -0800373 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700374 }
Calin Juravle114f0812017-03-08 19:05:07 -0800375 // Profiles should belong to the app; make sure of that by giving ownership to
376 // the app uid. If we cannot do that, there's no point in returning the fd
377 // since dex2oat/profman will fail with SElinux denials.
378 if (fchown(fd.get(), uid, uid) < 0) {
Roland Levillain019db5b2019-03-14 14:31:59 +0000379 PLOG(ERROR) << "Could not chown profile " << profile;
Calin Juravle29591732017-11-20 17:46:19 -0800380 return invalid_unique_fd();
Calin Juravle114f0812017-03-08 19:05:07 -0800381 }
Calin Juravle29591732017-11-20 17:46:19 -0800382 return fd;
Calin Juravle114f0812017-03-08 19:05:07 -0800383}
384
Calin Juravlee01f8712021-06-03 18:16:54 -0700385static unique_fd open_profile(uid_t uid, const std::string& profile, int32_t flags, mode_t mode) {
Calin Juravle114f0812017-03-08 19:05:07 -0800386 // Do not follow symlinks when opening a profile:
387 // - primary profiles should not contain symlinks in their paths
388 // - secondary dex paths should have been already resolved and validated
389 flags |= O_NOFOLLOW;
390
Calin Juravle29591732017-11-20 17:46:19 -0800391 // Check if we need to create the profile
392 // Reference profiles and snapshots are created on the fly; so they might not exist beforehand.
393 unique_fd fd;
394 if ((flags & O_CREAT) != 0) {
Calin Juravlee01f8712021-06-03 18:16:54 -0700395 fd = create_profile(uid, profile, flags, mode);
Calin Juravle29591732017-11-20 17:46:19 -0800396 } else {
397 fd.reset(TEMP_FAILURE_RETRY(open(profile.c_str(), flags)));
398 }
399
Calin Juravle114f0812017-03-08 19:05:07 -0800400 if (fd.get() < 0) {
401 if (errno != ENOENT) {
402 // Profiles might be missing for various reasons. For example, in a
403 // multi-user environment, the profile directory for one user can be created
404 // after we start a merge. In this case the current profile for that user
405 // will not be found.
406 // Also, the secondary dex profiles might be deleted by the app at any time,
407 // so we can't we need to prepare if they are missing.
408 PLOG(ERROR) << "Failed to open profile " << profile;
409 }
410 return invalid_unique_fd();
Calin Juravlee01f8712021-06-03 18:16:54 -0700411 } else {
412 // If we just create the file we need to set its mode because on Android
413 // open has a mask that only allows owner access.
414 if ((flags & O_CREAT) != 0) {
415 if (fchmod(fd.get(), mode) != 0) {
416 PLOG(ERROR) << "Could not set mode " << std::hex << mode << std::dec
417 << " on profile" << profile;
418 // Not a terminal failure.
419 }
420 }
Calin Juravle114f0812017-03-08 19:05:07 -0800421 }
422
Jeff Sharkey90aff262016-12-12 14:28:24 -0700423 return fd;
424}
425
Calin Juravle824a64d2018-01-18 20:23:17 -0800426static unique_fd open_current_profile(uid_t uid, userid_t user, const std::string& package_name,
427 const std::string& location, bool is_secondary_dex) {
428 std::string profile = create_current_profile_path(user, package_name, location,
429 is_secondary_dex);
Calin Juravlee01f8712021-06-03 18:16:54 -0700430 return open_profile(uid, profile, O_RDONLY, /*mode=*/ 0);
Calin Juravle114f0812017-03-08 19:05:07 -0800431}
432
Calin Juravle824a64d2018-01-18 20:23:17 -0800433static unique_fd open_reference_profile(uid_t uid, const std::string& package_name,
434 const std::string& location, bool read_write, bool is_secondary_dex) {
435 std::string profile = create_reference_profile_path(package_name, location, is_secondary_dex);
Calin Juravlee01f8712021-06-03 18:16:54 -0700436 return open_profile(
437 uid,
438 profile,
439 read_write ? (O_CREAT | O_RDWR) : O_RDONLY,
440 S_IRUSR | S_IWUSR | S_IRGRP); // so that ART can also read it when apps run.
Calin Juravle29591732017-11-20 17:46:19 -0800441}
442
Victor Hsiehcb35a062020-08-13 16:11:13 -0700443static UniqueFile open_reference_profile_as_unique_file(uid_t uid, const std::string& package_name,
444 const std::string& location, bool read_write, bool is_secondary_dex) {
445 std::string profile_path = create_reference_profile_path(package_name, location,
446 is_secondary_dex);
Calin Juravlee01f8712021-06-03 18:16:54 -0700447 unique_fd ufd = open_profile(
448 uid,
449 profile_path,
450 read_write ? (O_CREAT | O_RDWR) : O_RDONLY,
451 S_IRUSR | S_IWUSR | S_IRGRP); // so that ART can also read it when apps run.
452
Victor Hsiehcb35a062020-08-13 16:11:13 -0700453 return UniqueFile(ufd.release(), profile_path, [](const std::string& path) {
454 clear_profile(path);
455 });
456}
457
Alex Buynytskyy79dcab52021-11-19 11:43:04 -0800458static unique_fd open_snapshot_profile(uid_t uid, const std::string& package_name,
459 const std::string& location) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800460 std::string profile = create_snapshot_profile_path(package_name, location);
Calin Juravlee01f8712021-06-03 18:16:54 -0700461 return open_profile(uid, profile, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
Calin Juravle114f0812017-03-08 19:05:07 -0800462}
463
Calin Juravle824a64d2018-01-18 20:23:17 -0800464static void open_profile_files(uid_t uid, const std::string& package_name,
465 const std::string& location, bool is_secondary_dex,
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800466 /*out*/ std::vector<unique_fd>* profiles_fd, /*out*/ unique_fd* reference_profile_fd) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700467 // Open the reference profile in read-write mode as profman might need to save the merge.
Calin Juravle824a64d2018-01-18 20:23:17 -0800468 *reference_profile_fd = open_reference_profile(uid, package_name, location,
469 /*read_write*/ true, is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700470
Calin Juravle114f0812017-03-08 19:05:07 -0800471 // For secondary dex files, we don't really need the user but we use it for sanity checks.
472 // Note: the user owning the dex file should be the current user.
473 std::vector<userid_t> users;
474 if (is_secondary_dex){
475 users.push_back(multiuser_get_user_id(uid));
476 } else {
477 users = get_known_users(/*volume_uuid*/ nullptr);
478 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700479 for (auto user : users) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800480 unique_fd profile_fd = open_current_profile(uid, user, package_name, location,
481 is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700482 // Add to the lists only if both fds are valid.
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800483 if (profile_fd.get() >= 0) {
484 profiles_fd->push_back(std::move(profile_fd));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700485 }
486 }
487}
488
Calin Juravle76561322019-11-14 09:08:52 -0800489static constexpr int PROFMAN_BIN_RETURN_CODE_SUCCESS = 0;
490static constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 1;
Calin Juravle3b758282021-06-08 08:04:52 -0700491static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_NOT_ENOUGH_DELTA = 2;
Calin Juravle76561322019-11-14 09:08:52 -0800492static constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 3;
493static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 4;
494static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 5;
495static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS = 6;
Calin Juravle3b758282021-06-08 08:04:52 -0700496static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_EMPTY_PROFILES = 7;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700497
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800498class RunProfman : public ExecVHelper {
499 public:
500 void SetupArgs(const std::vector<unique_fd>& profile_fds,
501 const unique_fd& reference_profile_fd,
502 const std::vector<unique_fd>& apk_fds,
503 const std::vector<std::string>& dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800504 bool copy_and_update,
505 bool for_snapshot,
506 bool for_boot_image) {
Calin Juravlef74a7372019-02-28 20:29:41 -0800507
508 // TODO(calin): Assume for now we run in the bg compile job (which is in
509 // most of the invocation). With the current data flow, is not very easy or
510 // clean to discover this in RunProfman (it will require quite a messy refactoring).
511 const char* profman_bin = select_execution_binary(
512 kProfmanPath, kProfmanDebugPath, /*background_job_compile=*/ true);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700513
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800514 if (copy_and_update) {
515 CHECK_EQ(1u, profile_fds.size());
516 CHECK_EQ(1u, apk_fds.size());
Mathieu Chartier31636522018-11-09 23:53:07 +0000517 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800518 if (reference_profile_fd != -1) {
519 AddArg("--reference-profile-file-fd=" + std::to_string(reference_profile_fd.get()));
Mathieu Chartier31636522018-11-09 23:53:07 +0000520 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800521
522 for (const unique_fd& fd : profile_fds) {
523 AddArg("--profile-file-fd=" + std::to_string(fd.get()));
524 }
525
526 for (const unique_fd& fd : apk_fds) {
527 AddArg("--apk-fd=" + std::to_string(fd.get()));
528 }
529
530 for (const std::string& dex_location : dex_locations) {
531 AddArg("--dex-location=" + dex_location);
532 }
533
534 if (copy_and_update) {
535 AddArg("--copy-and-update-profile-key");
536 }
537
Calin Juravle78728f32019-11-08 17:55:46 -0800538 if (for_snapshot) {
539 AddArg("--force-merge");
540 }
541
542 if (for_boot_image) {
543 AddArg("--boot-image-merge");
544 }
545
yawannga27559e2020-11-13 19:17:44 +0000546 // The percent won't exceed 100, otherwise, don't set it and use the
547 // default one set in profman.
yawanng6fe34a52020-11-06 05:13:53 +0000548 uint32_t min_new_classes_percent_change = ::android::base::GetUintProperty<uint32_t>(
yawannga27559e2020-11-13 19:17:44 +0000549 "dalvik.vm.bgdexopt.new-classes-percent",
550 /*default*/std::numeric_limits<uint32_t>::max());
551 if (min_new_classes_percent_change <= 100) {
yawanng6fe34a52020-11-06 05:13:53 +0000552 AddArg("--min-new-classes-percent-change=" +
553 std::to_string(min_new_classes_percent_change));
554 }
555
yawannga27559e2020-11-13 19:17:44 +0000556 // The percent won't exceed 100, otherwise, don't set it and use the
557 // default one set in profman.
yawanng6fe34a52020-11-06 05:13:53 +0000558 uint32_t min_new_methods_percent_change = ::android::base::GetUintProperty<uint32_t>(
yawannga27559e2020-11-13 19:17:44 +0000559 "dalvik.vm.bgdexopt.new-methods-percent",
560 /*default*/std::numeric_limits<uint32_t>::max());
561 if (min_new_methods_percent_change <= 100) {
yawanng6fe34a52020-11-06 05:13:53 +0000562 AddArg("--min-new-methods-percent-change=" +
563 std::to_string(min_new_methods_percent_change));
564 }
565
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800566 // Do not add after dex2oat_flags, they should override others for debugging.
567 PrepareArgs(profman_bin);
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800568 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700569
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800570 void SetupMerge(const std::vector<unique_fd>& profiles_fd,
571 const unique_fd& reference_profile_fd,
572 const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>(),
Calin Juravle78728f32019-11-08 17:55:46 -0800573 const std::vector<std::string>& dex_locations = std::vector<std::string>(),
574 bool for_snapshot = false,
575 bool for_boot_image = false) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800576 SetupArgs(profiles_fd,
Calin Juravleb3a929d2018-12-11 14:40:00 -0800577 reference_profile_fd,
578 apk_fds,
579 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800580 /*copy_and_update=*/ false,
581 for_snapshot,
582 for_boot_image);
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800583 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700584
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800585 void SetupCopyAndUpdate(unique_fd&& profile_fd,
586 unique_fd&& reference_profile_fd,
587 unique_fd&& apk_fd,
588 const std::string& dex_location) {
589 // The fds need to stay open longer than the scope of the function, so put them into a local
590 // variable vector.
591 profiles_fd_.push_back(std::move(profile_fd));
592 apk_fds_.push_back(std::move(apk_fd));
593 reference_profile_fd_ = std::move(reference_profile_fd);
594 std::vector<std::string> dex_locations = {dex_location};
Calin Juravleb3a929d2018-12-11 14:40:00 -0800595 SetupArgs(profiles_fd_,
596 reference_profile_fd_,
597 apk_fds_,
598 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800599 /*copy_and_update=*/true,
600 /*for_snapshot*/false,
601 /*for_boot_image*/false);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800602 }
Calin Juravlef63d4792018-01-30 17:43:34 +0000603
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800604 void SetupDump(const std::vector<unique_fd>& profiles_fd,
605 const unique_fd& reference_profile_fd,
606 const std::vector<std::string>& dex_locations,
607 const std::vector<unique_fd>& apk_fds,
608 const unique_fd& output_fd) {
609 AddArg("--dump-only");
610 AddArg(StringPrintf("--dump-output-to-fd=%d", output_fd.get()));
Calin Juravleb3a929d2018-12-11 14:40:00 -0800611 SetupArgs(profiles_fd,
612 reference_profile_fd,
613 apk_fds,
614 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800615 /*copy_and_update=*/false,
616 /*for_snapshot*/false,
617 /*for_boot_image*/false);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800618 }
Calin Juravlef63d4792018-01-30 17:43:34 +0000619
Victor Hsiehc9821f12020-08-07 11:32:29 -0700620 using ExecVHelper::Exec; // To suppress -Wno-overloaded-virtual
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800621 void Exec() {
622 ExecVHelper::Exec(DexoptReturnCodes::kProfmanExec);
623 }
Mathieu Chartier31636522018-11-09 23:53:07 +0000624
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800625 private:
626 unique_fd reference_profile_fd_;
627 std::vector<unique_fd> profiles_fd_;
628 std::vector<unique_fd> apk_fds_;
629};
Mathieu Chartier31636522018-11-09 23:53:07 +0000630
Calin Juravle3b758282021-06-08 08:04:52 -0700631static int analyze_profiles(uid_t uid, const std::string& package_name,
Calin Juravle824a64d2018-01-18 20:23:17 -0800632 const std::string& location, bool is_secondary_dex) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800633 std::vector<unique_fd> profiles_fd;
634 unique_fd reference_profile_fd;
Calin Juravle824a64d2018-01-18 20:23:17 -0800635 open_profile_files(uid, package_name, location, is_secondary_dex,
636 &profiles_fd, &reference_profile_fd);
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800637 if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700638 // Skip profile guided compilation because no profiles were found.
639 // Or if the reference profile info couldn't be opened.
Calin Juravle3b758282021-06-08 08:04:52 -0700640 return PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700641 }
642
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800643 RunProfman profman_merge;
Calin Juravlef85ddb92020-05-01 14:05:40 -0700644 const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>();
645 const std::vector<std::string>& dex_locations = std::vector<std::string>();
646 profman_merge.SetupMerge(
647 profiles_fd,
648 reference_profile_fd,
649 apk_fds,
650 dex_locations,
651 /* for_snapshot= */ false,
652 IsBootClassPathProfilingEnable());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700653 pid_t pid = fork();
654 if (pid == 0) {
655 /* child -- drop privileges before continuing */
656 drop_capabilities(uid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800657 profman_merge.Exec();
Jeff Sharkey90aff262016-12-12 14:28:24 -0700658 }
659 /* parent */
660 int return_code = wait_child(pid);
661 bool need_to_compile = false;
Calin Juravle3b758282021-06-08 08:04:52 -0700662 bool empty_profiles = false;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700663 bool should_clear_current_profiles = false;
664 bool should_clear_reference_profile = false;
665 if (!WIFEXITED(return_code)) {
Calin Juravle114f0812017-03-08 19:05:07 -0800666 LOG(WARNING) << "profman failed for location " << location << ": " << return_code;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700667 } else {
668 return_code = WEXITSTATUS(return_code);
669 switch (return_code) {
670 case PROFMAN_BIN_RETURN_CODE_COMPILE:
671 need_to_compile = true;
672 should_clear_current_profiles = true;
673 should_clear_reference_profile = false;
674 break;
Calin Juravle3b758282021-06-08 08:04:52 -0700675 case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_NOT_ENOUGH_DELTA:
Jeff Sharkey90aff262016-12-12 14:28:24 -0700676 need_to_compile = false;
677 should_clear_current_profiles = false;
678 should_clear_reference_profile = false;
679 break;
Calin Juravle3b758282021-06-08 08:04:52 -0700680 case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_EMPTY_PROFILES:
681 need_to_compile = false;
682 empty_profiles = true;
683 should_clear_current_profiles = false;
684 should_clear_reference_profile = false;
685 break;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700686 case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES:
Calin Juravle114f0812017-03-08 19:05:07 -0800687 LOG(WARNING) << "Bad profiles for location " << location;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700688 need_to_compile = false;
689 should_clear_current_profiles = true;
690 should_clear_reference_profile = true;
691 break;
692 case PROFMAN_BIN_RETURN_CODE_ERROR_IO: // fall-through
693 case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING:
694 // Temporary IO problem (e.g. locking). Ignore but log a warning.
Calin Juravle114f0812017-03-08 19:05:07 -0800695 LOG(WARNING) << "IO error while reading profiles for location " << location;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700696 need_to_compile = false;
697 should_clear_current_profiles = false;
698 should_clear_reference_profile = false;
699 break;
Calin Juravle76561322019-11-14 09:08:52 -0800700 case PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS:
701 need_to_compile = false;
702 should_clear_current_profiles = true;
703 should_clear_reference_profile = true;
704 break;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700705 default:
706 // Unknown return code or error. Unlink profiles.
Calin Juravle78728f32019-11-08 17:55:46 -0800707 LOG(WARNING) << "Unexpected error code while processing profiles for location "
Calin Juravle114f0812017-03-08 19:05:07 -0800708 << location << ": " << return_code;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700709 need_to_compile = false;
710 should_clear_current_profiles = true;
711 should_clear_reference_profile = true;
712 break;
713 }
714 }
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800715
Jeff Sharkey90aff262016-12-12 14:28:24 -0700716 if (should_clear_current_profiles) {
Calin Juravle114f0812017-03-08 19:05:07 -0800717 if (is_secondary_dex) {
718 // For secondary dex files, the owning user is the current user.
Calin Juravle824a64d2018-01-18 20:23:17 -0800719 clear_current_profile(package_name, location, multiuser_get_user_id(uid),
720 is_secondary_dex);
Calin Juravle114f0812017-03-08 19:05:07 -0800721 } else {
Calin Juravle824a64d2018-01-18 20:23:17 -0800722 clear_primary_current_profiles(package_name, location);
Calin Juravle114f0812017-03-08 19:05:07 -0800723 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700724 }
725 if (should_clear_reference_profile) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800726 clear_reference_profile(package_name, location, is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700727 }
Calin Juravle3b758282021-06-08 08:04:52 -0700728 int result = 0;
729 if (need_to_compile) {
730 result = PROFILES_ANALYSIS_OPTIMIZE;
731 } else if (empty_profiles) {
732 result = PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES;
733 } else {
734 result = PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA;
735 }
736 return result;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700737}
738
Calin Juravle114f0812017-03-08 19:05:07 -0800739// Decides if profile guided compilation is needed or not based on existing profiles.
Calin Juravle3b758282021-06-08 08:04:52 -0700740// The analysis is done for a single profile name (which corresponds to a single code path).
741//
742// Returns PROFILES_ANALYSIS_OPTIMIZE if there is enough information in the current profiles
743// that makes it worth to recompile the package.
744// If the return value is PROFILES_ANALYSIS_OPTIMIZE all the current profiles would have been
745// merged into the reference profiles accessible with open_reference_profile().
746//
747// Return PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA if the package should not optimize.
748// As a special case returns PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES if all profiles are
749// empty.
750int analyze_primary_profiles(uid_t uid, const std::string& package_name,
Calin Juravle824a64d2018-01-18 20:23:17 -0800751 const std::string& profile_name) {
752 return analyze_profiles(uid, package_name, profile_name, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800753}
754
Calin Juravle408cd4a2018-01-20 23:34:18 -0800755bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name,
756 const std::string& code_path) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800757 std::vector<unique_fd> profile_fds;
758 unique_fd reference_profile_fd;
Calin Juravle408cd4a2018-01-20 23:34:18 -0800759 std::string out_file_name = StringPrintf("/data/misc/profman/%s-%s.txt",
760 pkgname.c_str(), profile_name.c_str());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700761
Calin Juravle408cd4a2018-01-20 23:34:18 -0800762 open_profile_files(uid, pkgname, profile_name, /*is_secondary_dex*/false,
Calin Juravle114f0812017-03-08 19:05:07 -0800763 &profile_fds, &reference_profile_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700764
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800765 const bool has_reference_profile = (reference_profile_fd.get() != -1);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700766 const bool has_profiles = !profile_fds.empty();
767
768 if (!has_reference_profile && !has_profiles) {
Calin Juravle76268c52017-03-09 13:19:42 -0800769 LOG(ERROR) << "profman dump: no profiles to dump for " << pkgname;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700770 return false;
771 }
772
Calin Juravle114f0812017-03-08 19:05:07 -0800773 unique_fd output_fd(open(out_file_name.c_str(),
774 O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700775 if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
Calin Juravle408cd4a2018-01-20 23:34:18 -0800776 LOG(ERROR) << "installd cannot chmod file for dump_profile" << out_file_name;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700777 return false;
778 }
Calin Juravle408cd4a2018-01-20 23:34:18 -0800779
Jeff Sharkey90aff262016-12-12 14:28:24 -0700780 std::vector<std::string> dex_locations;
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800781 std::vector<unique_fd> apk_fds;
Calin Juravle408cd4a2018-01-20 23:34:18 -0800782 unique_fd apk_fd(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW));
783 if (apk_fd == -1) {
784 PLOG(ERROR) << "installd cannot open " << code_path.c_str();
785 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700786 }
Victor Hsieh76f19ba2020-08-10 14:37:26 -0700787 dex_locations.push_back(Basename(code_path));
Calin Juravle408cd4a2018-01-20 23:34:18 -0800788 apk_fds.push_back(std::move(apk_fd));
789
Jeff Sharkey90aff262016-12-12 14:28:24 -0700790
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800791 RunProfman profman_dump;
792 profman_dump.SetupDump(profile_fds, reference_profile_fd, dex_locations, apk_fds, output_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700793 pid_t pid = fork();
794 if (pid == 0) {
795 /* child -- drop privileges before continuing */
796 drop_capabilities(uid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800797 profman_dump.Exec();
Jeff Sharkey90aff262016-12-12 14:28:24 -0700798 }
799 /* parent */
Jeff Sharkey90aff262016-12-12 14:28:24 -0700800 int return_code = wait_child(pid);
801 if (!WIFEXITED(return_code)) {
802 LOG(WARNING) << "profman failed for package " << pkgname << ": "
803 << return_code;
804 return false;
805 }
806 return true;
807}
808
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700809bool copy_system_profile(const std::string& system_profile,
Calin Juravle824a64d2018-01-18 20:23:17 -0800810 uid_t packageUid, const std::string& package_name, const std::string& profile_name) {
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700811 unique_fd in_fd(open(system_profile.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
812 unique_fd out_fd(open_reference_profile(packageUid,
Calin Juravle824a64d2018-01-18 20:23:17 -0800813 package_name,
814 profile_name,
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700815 /*read_write*/ true,
816 /*secondary*/ false));
817 if (in_fd.get() < 0) {
818 PLOG(WARNING) << "Could not open profile " << system_profile;
819 return false;
820 }
821 if (out_fd.get() < 0) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800822 PLOG(WARNING) << "Could not open profile " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700823 return false;
824 }
825
Mathieu Chartier78f71fe2017-06-14 13:02:26 -0700826 // As a security measure we want to write the profile information with the reduced capabilities
827 // of the package user id. So we fork and drop capabilities in the child.
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700828 pid_t pid = fork();
829 if (pid == 0) {
830 /* child -- drop privileges before continuing */
831 drop_capabilities(packageUid);
832
833 if (flock(out_fd.get(), LOCK_EX | LOCK_NB) != 0) {
834 if (errno != EWOULDBLOCK) {
Martijn Coenen6de402a2021-04-26 16:23:40 +0200835 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Error locking profile %s: %d",
836 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700837 }
838 // This implies that the app owning this profile is running
839 // (and has acquired the lock).
840 //
841 // The app never acquires the lock for the reference profiles of primary apks.
842 // Only dex2oat from installd will do that. Since installd is single threaded
843 // we should not see this case. Nevertheless be prepared for it.
Martijn Coenen6de402a2021-04-26 16:23:40 +0200844 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Failed to flock %s: %d",
845 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700846 return false;
847 }
848
849 bool truncated = ftruncate(out_fd.get(), 0) == 0;
850 if (!truncated) {
Martijn Coenen6de402a2021-04-26 16:23:40 +0200851 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Could not truncate %s: %d",
852 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700853 }
854
855 // Copy over data.
856 static constexpr size_t kBufferSize = 4 * 1024;
857 char buffer[kBufferSize];
858 while (true) {
859 ssize_t bytes = read(in_fd.get(), buffer, kBufferSize);
860 if (bytes == 0) {
861 break;
862 }
863 write(out_fd.get(), buffer, bytes);
864 }
865 if (flock(out_fd.get(), LOCK_UN) != 0) {
Martijn Coenen6de402a2021-04-26 16:23:40 +0200866 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Error unlocking profile %s: %d",
867 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700868 }
Mathieu Chartier78f71fe2017-06-14 13:02:26 -0700869 // Use _exit since we don't want to run the global destructors in the child.
870 // b/62597429
871 _exit(0);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700872 }
873 /* parent */
874 int return_code = wait_child(pid);
875 return return_code == 0;
876}
877
Jeff Sharkey90aff262016-12-12 14:28:24 -0700878static std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) {
879 // A standard dalvik-cache entry. Replace ".dex" with `new_ext`.
880 if (EndsWith(oat_path, ".dex")) {
881 std::string new_path = oat_path;
882 new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext);
Elliott Hughes969e4f82017-12-20 12:34:09 -0800883 CHECK(EndsWith(new_path, new_ext));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700884 return new_path;
885 }
886
887 // An odex entry. Not that this may not be an extension, e.g., in the OTA
888 // case (where the base name will have an extension for the B artifact).
889 size_t odex_pos = oat_path.rfind(".odex");
890 if (odex_pos != std::string::npos) {
891 std::string new_path = oat_path;
892 new_path.replace(odex_pos, strlen(".odex"), new_ext);
893 CHECK_NE(new_path.find(new_ext), std::string::npos);
894 return new_path;
895 }
896
897 // Don't know how to handle this.
898 return "";
899}
900
901// Translate the given oat path to an art (app image) path. An empty string
902// denotes an error.
903static std::string create_image_filename(const std::string& oat_path) {
904 return replace_file_extension(oat_path, ".art");
905}
906
907// Translate the given oat path to a vdex path. An empty string denotes an error.
908static std::string create_vdex_filename(const std::string& oat_path) {
909 return replace_file_extension(oat_path, ".vdex");
910}
911
Jeff Sharkey90aff262016-12-12 14:28:24 -0700912static int open_output_file(const char* file_name, bool recreate, int permissions) {
913 int flags = O_RDWR | O_CREAT;
914 if (recreate) {
915 if (unlink(file_name) < 0) {
916 if (errno != ENOENT) {
917 PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name;
918 }
919 }
920 flags |= O_EXCL;
921 }
922 return open(file_name, flags, permissions);
923}
924
Calin Juravle2289c0a2017-02-15 12:44:14 -0800925static bool set_permissions_and_ownership(
926 int fd, bool is_public, int uid, const char* path, bool is_secondary_dex) {
927 // Primary apks are owned by the system. Secondary dex files are owned by the app.
928 int owning_uid = is_secondary_dex ? uid : AID_SYSTEM;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700929 if (fchmod(fd,
930 S_IRUSR|S_IWUSR|S_IRGRP |
931 (is_public ? S_IROTH : 0)) < 0) {
932 ALOGE("installd cannot chmod '%s' during dexopt\n", path);
933 return false;
Calin Juravle2289c0a2017-02-15 12:44:14 -0800934 } else if (fchown(fd, owning_uid, uid) < 0) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700935 ALOGE("installd cannot chown '%s' during dexopt\n", path);
936 return false;
937 }
938 return true;
939}
940
941static bool IsOutputDalvikCache(const char* oat_dir) {
942 // InstallerConnection.java (which invokes installd) transforms Java null arguments
943 // into '!'. Play it safe by handling it both.
944 // TODO: ensure we never get null.
945 // TODO: pass a flag instead of inferring if the output is dalvik cache.
946 return oat_dir == nullptr || oat_dir[0] == '!';
947}
948
Calin Juravled23dee72017-07-06 16:29:11 -0700949// Best-effort check whether we can fit the the path into our buffers.
950// Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
951// without a swap file, if necessary. Reference profiles file also add an extra ".prof"
952// extension to the cache path (5 bytes).
953// TODO(calin): move away from char* buffers and PKG_PATH_MAX.
954static bool validate_dex_path_size(const std::string& dex_path) {
955 if (dex_path.size() >= (PKG_PATH_MAX - 8)) {
956 LOG(ERROR) << "dex_path too long: " << dex_path;
957 return false;
958 }
959 return true;
960}
961
Jeff Sharkey90aff262016-12-12 14:28:24 -0700962static bool create_oat_out_path(const char* apk_path, const char* instruction_set,
Calin Juravle80a21252017-01-17 14:43:25 -0800963 const char* oat_dir, bool is_secondary_dex, /*out*/ char* out_oat_path) {
Calin Juravled23dee72017-07-06 16:29:11 -0700964 if (!validate_dex_path_size(apk_path)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700965 return false;
966 }
967
968 if (!IsOutputDalvikCache(oat_dir)) {
Calin Juravle80a21252017-01-17 14:43:25 -0800969 // Oat dirs for secondary dex files are already validated.
970 if (!is_secondary_dex && validate_apk_path(oat_dir)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700971 ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir);
972 return false;
973 }
974 if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) {
975 return false;
976 }
977 } else {
978 if (!create_cache_path(out_oat_path, apk_path, instruction_set)) {
979 return false;
980 }
981 }
982 return true;
983}
984
Calin Juravle7a570e82017-01-14 16:23:30 -0800985// (re)Creates the app image if needed.
Keun young Parkb7342262021-10-25 08:09:27 -0700986RestorableFile maybe_open_app_image(const std::string& out_oat_path, bool generate_app_image,
987 bool is_public, int uid, bool is_secondary_dex) {
Calin Juravle7a570e82017-01-14 16:23:30 -0800988 const std::string image_path = create_image_filename(out_oat_path);
989 if (image_path.empty()) {
990 // Happens when the out_oat_path has an unknown extension.
Keun young Parkb7342262021-10-25 08:09:27 -0700991 return RestorableFile();
Calin Juravle7a570e82017-01-14 16:23:30 -0800992 }
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +0100993
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -0700994 // Not enabled, exit.
995 if (!generate_app_image) {
Keun young Parkb7342262021-10-25 08:09:27 -0700996 RestorableFile::RemoveAllFiles(image_path);
997 return RestorableFile();
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +0100998 }
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700999 std::string app_image_format = GetProperty("dalvik.vm.appimageformat", "");
1000 if (app_image_format.empty()) {
Keun young Parkb7342262021-10-25 08:09:27 -07001001 RestorableFile::RemoveAllFiles(image_path);
1002 return RestorableFile();
Calin Juravle7a570e82017-01-14 16:23:30 -08001003 }
Keun young Parkb7342262021-10-25 08:09:27 -07001004 // If the app is already running and we modify the image file, it can cause crashes
1005 // (b/27493510).
1006 RestorableFile image_file = RestorableFile::CreateWritableFile(image_path,
1007 /*permissions*/ 0600);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001008 if (image_file.fd() < 0) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001009 // Could not create application image file. Go on since we can compile without it.
1010 LOG(ERROR) << "installd could not create '" << image_path
1011 << "' for image file during dexopt";
Keun young Parkb7342262021-10-25 08:09:27 -07001012 // If we have a valid image file path but cannot create tmp file, reset it.
1013 image_file.reset();
Calin Juravle7a570e82017-01-14 16:23:30 -08001014 } else if (!set_permissions_and_ownership(
Victor Hsiehcb35a062020-08-13 16:11:13 -07001015 image_file.fd(), is_public, uid, image_path.c_str(), is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001016 ALOGE("installd cannot set owner '%s' for image during dexopt\n", image_path.c_str());
Victor Hsiehcb35a062020-08-13 16:11:13 -07001017 image_file.reset();
Calin Juravle7a570e82017-01-14 16:23:30 -08001018 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001019
Victor Hsiehcb35a062020-08-13 16:11:13 -07001020 return image_file;
Calin Juravle7a570e82017-01-14 16:23:30 -08001021}
1022
1023// Creates the dexopt swap file if necessary and return its fd.
1024// Returns -1 if there's no need for a swap or in case of errors.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001025unique_fd maybe_open_dexopt_swap_file(const std::string& out_oat_path) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001026 if (!ShouldUseSwapFileForDexopt()) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001027 return invalid_unique_fd();
Calin Juravle7a570e82017-01-14 16:23:30 -08001028 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001029 auto swap_file_name = out_oat_path + ".swap";
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001030 unique_fd swap_fd(open_output_file(
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001031 swap_file_name.c_str(), /*recreate*/true, /*permissions*/0600));
Calin Juravle7a570e82017-01-14 16:23:30 -08001032 if (swap_fd.get() < 0) {
1033 // Could not create swap file. Optimistically go on and hope that we can compile
1034 // without it.
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001035 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name.c_str());
Calin Juravle7a570e82017-01-14 16:23:30 -08001036 } else {
1037 // Immediately unlink. We don't really want to hit flash.
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001038 if (unlink(swap_file_name.c_str()) < 0) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001039 PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name;
1040 }
1041 }
1042 return swap_fd;
1043}
1044
1045// Opens the reference profiles if needed.
1046// Note that the reference profile might not exist so it's OK if the fd will be -1.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001047UniqueFile maybe_open_reference_profile(const std::string& pkgname,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001048 const std::string& dex_path, const char* profile_name, bool profile_guided,
Calin Juravle824a64d2018-01-18 20:23:17 -08001049 bool is_public, int uid, bool is_secondary_dex) {
Calin Juravle5bd1c722018-02-01 17:23:54 +00001050 // If we are not profile guided compilation, or we are compiling system server
1051 // do not bother to open the profiles; we won't be using them.
1052 if (!profile_guided || (pkgname[0] == '*')) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001053 return UniqueFile();
Calin Juravle5bd1c722018-02-01 17:23:54 +00001054 }
1055
1056 // If this is a secondary dex path which is public do not open the profile.
1057 // We cannot compile public secondary dex paths with profiles. That's because
1058 // it will expose how the dex files are used by their owner.
1059 //
1060 // Note that the PackageManager is responsible to set the is_public flag for
1061 // primary apks and we do not check it here. In some cases, e.g. when
1062 // compiling with a public profile from the .dm file the PackageManager will
1063 // set is_public toghether with the profile guided compilation.
1064 if (is_secondary_dex && is_public) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001065 return UniqueFile();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001066 }
Calin Juravle114f0812017-03-08 19:05:07 -08001067
1068 // Open reference profile in read only mode as dex2oat does not get write permissions.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001069 std::string location;
1070 if (is_secondary_dex) {
1071 location = dex_path;
1072 } else {
1073 if (profile_name == nullptr) {
1074 // This path is taken for system server re-compilation lunched from ZygoteInit.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001075 return UniqueFile();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001076 } else {
1077 location = profile_name;
1078 }
1079 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001080 return open_reference_profile_as_unique_file(uid, pkgname, location, /*read_write*/false,
1081 is_secondary_dex);
Calin Juravle7a570e82017-01-14 16:23:30 -08001082}
Jeff Sharkey90aff262016-12-12 14:28:24 -07001083
Victor Hsiehcb35a062020-08-13 16:11:13 -07001084// Opens the vdex files and assigns the input fd to in_vdex_wrapper and the output fd to
1085// out_vdex_wrapper. Returns true for success or false in case of errors.
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001086bool open_vdex_files_for_dex2oat(const char* apk_path, const char* out_oat_path, int dexopt_needed,
Keun young Parkb7342262021-10-25 08:09:27 -07001087 const char* instruction_set, bool is_public, int uid,
1088 bool is_secondary_dex, bool profile_guided,
1089 UniqueFile* in_vdex_wrapper, RestorableFile* out_vdex_wrapper) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001090 CHECK(in_vdex_wrapper != nullptr);
1091 CHECK(out_vdex_wrapper != nullptr);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001092 // Open the existing VDEX. We do this before creating the new output VDEX, which will
1093 // unlink the old one.
Richard Uhler76cc0272016-12-08 10:46:35 +00001094 char in_odex_path[PKG_PATH_MAX];
1095 int dexopt_action = abs(dexopt_needed);
1096 bool is_odex_location = dexopt_needed < 0;
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001097
1098 // Infer the name of the output VDEX.
1099 const std::string out_vdex_path_str = create_vdex_filename(out_oat_path);
1100 if (out_vdex_path_str.empty()) {
1101 return false;
1102 }
1103
Keun young Parkb7342262021-10-25 08:09:27 -07001104 // Create work file first. All files will be deleted when it fails.
1105 *out_vdex_wrapper = RestorableFile::CreateWritableFile(out_vdex_path_str,
1106 /*permissions*/ 0644);
1107 if (out_vdex_wrapper->fd() < 0) {
1108 ALOGE("installd cannot open vdex '%s' during dexopt\n", out_vdex_path_str.c_str());
1109 return false;
1110 }
1111
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001112 bool update_vdex_in_place = false;
Nicolas Geoffray3c95f2d2017-04-24 13:34:59 +00001113 if (dexopt_action != DEX2OAT_FROM_SCRATCH) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001114 // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd.
1115 const char* path = nullptr;
1116 if (is_odex_location) {
1117 if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1118 path = in_odex_path;
1119 } else {
1120 ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path);
Calin Juravle7a570e82017-01-14 16:23:30 -08001121 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001122 }
1123 } else {
1124 path = out_oat_path;
1125 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001126 std::string in_vdex_path_str = create_vdex_filename(path);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001127 if (in_vdex_path_str.empty()) {
1128 ALOGE("installd cannot compute input vdex location for '%s'\n", path);
Calin Juravle7a570e82017-01-14 16:23:30 -08001129 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001130 }
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001131 // We can update in place when all these conditions are met:
1132 // 1) The vdex location to write to is the same as the vdex location to read (vdex files
1133 // on /system typically cannot be updated in place).
1134 // 2) We dex2oat due to boot image change, because we then know the existing vdex file
1135 // cannot be currently used by a running process.
1136 // 3) We are not doing a profile guided compilation, because dexlayout requires two
1137 // different vdex files to operate.
1138 update_vdex_in_place =
1139 (in_vdex_path_str == out_vdex_path_str) &&
1140 (dexopt_action == DEX2OAT_FOR_BOOT_IMAGE) &&
1141 !profile_guided;
1142 if (update_vdex_in_place) {
Keun young Parkb7342262021-10-25 08:09:27 -07001143 // dex2oat marks it invalid anyway. So delete it and set work file fd.
1144 unlink(in_vdex_path_str.c_str());
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001145 // Open the file read-write to be able to update it.
Keun young Parkb7342262021-10-25 08:09:27 -07001146 in_vdex_wrapper->reset(out_vdex_wrapper->fd(), in_vdex_path_str);
1147 // Disable auto close for the in wrapper fd (it will be done when destructing the out
1148 // wrapper).
1149 in_vdex_wrapper->DisableAutoClose();
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001150 } else {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001151 in_vdex_wrapper->reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0),
1152 in_vdex_path_str);
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001153 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001154 }
1155
Victor Hsiehcb35a062020-08-13 16:11:13 -07001156 if (!set_permissions_and_ownership(out_vdex_wrapper->fd(), is_public, uid,
Calin Juravle2289c0a2017-02-15 12:44:14 -08001157 out_vdex_path_str.c_str(), is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001158 ALOGE("installd cannot set owner '%s' for vdex during dexopt\n", out_vdex_path_str.c_str());
1159 return false;
1160 }
1161
1162 // If we got here we successfully opened the vdex files.
1163 return true;
1164}
1165
1166// Opens the output oat file for the given apk.
Keun young Parkb7342262021-10-25 08:09:27 -07001167RestorableFile open_oat_out_file(const char* apk_path, const char* oat_dir, bool is_public, int uid,
1168 const char* instruction_set, bool is_secondary_dex) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001169 char out_oat_path[PKG_PATH_MAX];
Calin Juravle80a21252017-01-17 14:43:25 -08001170 if (!create_oat_out_path(apk_path, instruction_set, oat_dir, is_secondary_dex, out_oat_path)) {
Keun young Parkb7342262021-10-25 08:09:27 -07001171 return RestorableFile();
Calin Juravle7a570e82017-01-14 16:23:30 -08001172 }
Keun young Parkb7342262021-10-25 08:09:27 -07001173 RestorableFile oat = RestorableFile::CreateWritableFile(out_oat_path, /*permissions*/ 0644);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001174 if (oat.fd() < 0) {
Calin Juravle80a21252017-01-17 14:43:25 -08001175 PLOG(ERROR) << "installd cannot open output during dexopt" << out_oat_path;
Calin Juravle2289c0a2017-02-15 12:44:14 -08001176 } else if (!set_permissions_and_ownership(
Victor Hsiehcb35a062020-08-13 16:11:13 -07001177 oat.fd(), is_public, uid, out_oat_path, is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001178 ALOGE("installd cannot set owner '%s' for output during dexopt\n", out_oat_path);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001179 oat.reset();
Calin Juravle7a570e82017-01-14 16:23:30 -08001180 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001181 return oat;
Calin Juravle7a570e82017-01-14 16:23:30 -08001182}
1183
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001184// Creates RDONLY fds for oat and vdex files, if exist.
1185// Returns false if it fails to create oat out path for the given apk path.
1186// Note that the method returns true even if the files could not be opened.
1187bool maybe_open_oat_and_vdex_file(const std::string& apk_path,
1188 const std::string& oat_dir,
1189 const std::string& instruction_set,
1190 bool is_secondary_dex,
1191 unique_fd* oat_file_fd,
1192 unique_fd* vdex_file_fd) {
1193 char oat_path[PKG_PATH_MAX];
1194 if (!create_oat_out_path(apk_path.c_str(),
1195 instruction_set.c_str(),
1196 oat_dir.c_str(),
1197 is_secondary_dex,
1198 oat_path)) {
Calin Juravle7d765462017-09-04 15:57:10 -07001199 LOG(ERROR) << "Could not create oat out path for "
1200 << apk_path << " with oat dir " << oat_dir;
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001201 return false;
1202 }
1203 oat_file_fd->reset(open(oat_path, O_RDONLY));
1204 if (oat_file_fd->get() < 0) {
1205 PLOG(INFO) << "installd cannot open oat file during dexopt" << oat_path;
1206 }
1207
1208 std::string vdex_filename = create_vdex_filename(oat_path);
1209 vdex_file_fd->reset(open(vdex_filename.c_str(), O_RDONLY));
1210 if (vdex_file_fd->get() < 0) {
1211 PLOG(INFO) << "installd cannot open vdex file during dexopt" << vdex_filename;
1212 }
1213
1214 return true;
1215}
1216
Calin Juravle80a21252017-01-17 14:43:25 -08001217// Runs (execv) dexoptanalyzer on the given arguments.
Calin Juravle114f0812017-03-08 19:05:07 -08001218// The analyzer will check if the dex_file needs to be (re)compiled to match the compiler_filter.
1219// If this is for a profile guided compilation, profile_was_updated will tell whether or not
1220// the profile has changed.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001221class RunDexoptAnalyzer : public ExecVHelper {
1222 public:
1223 RunDexoptAnalyzer(const std::string& dex_file,
David Brazdil4f6027a2019-03-19 11:44:21 +00001224 int vdex_fd,
1225 int oat_fd,
1226 int zip_fd,
1227 const std::string& instruction_set,
1228 const std::string& compiler_filter,
Calin Juravle3b758282021-06-08 08:04:52 -07001229 int profile_analysis_result,
David Brazdil4f6027a2019-03-19 11:44:21 +00001230 bool downgrade,
1231 const char* class_loader_context,
1232 const std::string& class_loader_context_fds) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001233 CHECK_GE(zip_fd, 0);
Calin Juravlef74a7372019-02-28 20:29:41 -08001234
1235 // We always run the analyzer in the background job.
1236 const char* dexoptanalyzer_bin = select_execution_binary(
1237 kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true);
Calin Juravle80a21252017-01-17 14:43:25 -08001238
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001239 std::string dex_file_arg = "--dex-file=" + dex_file;
1240 std::string oat_fd_arg = "--oat-fd=" + std::to_string(oat_fd);
1241 std::string vdex_fd_arg = "--vdex-fd=" + std::to_string(vdex_fd);
1242 std::string zip_fd_arg = "--zip-fd=" + std::to_string(zip_fd);
1243 std::string isa_arg = "--isa=" + instruction_set;
1244 std::string compiler_filter_arg = "--compiler-filter=" + compiler_filter;
Calin Juravle3b758282021-06-08 08:04:52 -07001245 std::string profile_analysis_arg = "--profile-analysis-result="
1246 + std::to_string(profile_analysis_result);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001247 const char* downgrade_flag = "--downgrade";
1248 std::string class_loader_context_arg = "--class-loader-context=";
1249 if (class_loader_context != nullptr) {
1250 class_loader_context_arg += class_loader_context;
1251 }
David Brazdil4f6027a2019-03-19 11:44:21 +00001252 std::string class_loader_context_fds_arg = "--class-loader-context-fds=";
1253 if (!class_loader_context_fds.empty()) {
1254 class_loader_context_fds_arg += class_loader_context_fds;
1255 }
Mathieu Chartier31636522018-11-09 23:53:07 +00001256
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001257 // program name, dex file, isa, filter
1258 AddArg(dex_file_arg);
1259 AddArg(isa_arg);
1260 AddArg(compiler_filter_arg);
1261 if (oat_fd >= 0) {
1262 AddArg(oat_fd_arg);
1263 }
1264 if (vdex_fd >= 0) {
1265 AddArg(vdex_fd_arg);
1266 }
Greg Kaiser8042c372019-03-26 06:23:19 -07001267 AddArg(zip_fd_arg);
Calin Juravle3b758282021-06-08 08:04:52 -07001268 AddArg(profile_analysis_arg);
1269
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001270 if (downgrade) {
1271 AddArg(downgrade_flag);
1272 }
1273 if (class_loader_context != nullptr) {
Greg Kaiser8042c372019-03-26 06:23:19 -07001274 AddArg(class_loader_context_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +00001275 if (!class_loader_context_fds.empty()) {
Greg Kaiser8042c372019-03-26 06:23:19 -07001276 AddArg(class_loader_context_fds_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +00001277 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001278 }
Mathieu Chartier31636522018-11-09 23:53:07 +00001279
Orion Hodson5f6b3ad2021-07-01 12:12:40 +01001280 // On-device signing related. odsign sets the system property odsign.verification.success if
1281 // AOT artifacts have the expected signatures.
1282 const bool trust_art_apex_data_files =
1283 ::android::base::GetBoolProperty("odsign.verification.success", false);
1284 if (!trust_art_apex_data_files) {
1285 AddRuntimeArg("-Xdeny-art-apex-data-files");
1286 }
1287
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001288 PrepareArgs(dexoptanalyzer_bin);
1289 }
David Brazdil4f6027a2019-03-19 11:44:21 +00001290
1291 // Dexoptanalyzer mode which flattens the given class loader context and
1292 // prints a list of its dex files in that flattened order.
1293 RunDexoptAnalyzer(const char* class_loader_context) {
1294 CHECK(class_loader_context != nullptr);
1295
1296 // We always run the analyzer in the background job.
1297 const char* dexoptanalyzer_bin = select_execution_binary(
1298 kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true);
1299
1300 AddArg("--flatten-class-loader-context");
1301 AddArg(std::string("--class-loader-context=") + class_loader_context);
1302 PrepareArgs(dexoptanalyzer_bin);
1303 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001304};
Calin Juravle80a21252017-01-17 14:43:25 -08001305
1306// Prepares the oat dir for the secondary dex files.
Calin Juravle114f0812017-03-08 19:05:07 -08001307static bool prepare_secondary_dex_oat_dir(const std::string& dex_path, int uid,
Calin Juravle7d765462017-09-04 15:57:10 -07001308 const char* instruction_set) {
Calin Juravle114f0812017-03-08 19:05:07 -08001309 unsigned long dirIndex = dex_path.rfind('/');
Calin Juravle80a21252017-01-17 14:43:25 -08001310 if (dirIndex == std::string::npos) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001311 LOG(ERROR ) << "Unexpected dir structure for secondary dex " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001312 return false;
1313 }
Calin Juravle114f0812017-03-08 19:05:07 -08001314 std::string dex_dir = dex_path.substr(0, dirIndex);
Calin Juravle80a21252017-01-17 14:43:25 -08001315
Calin Juravle80a21252017-01-17 14:43:25 -08001316 // Create oat file output directory.
Calin Juravleebc8a792017-04-04 20:21:05 -07001317 mode_t oat_dir_mode = S_IRWXU | S_IRWXG | S_IXOTH;
1318 if (prepare_app_cache_dir(dex_dir, "oat", oat_dir_mode, uid, uid) != 0) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001319 LOG(ERROR) << "Could not prepare oat dir for secondary dex: " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001320 return false;
1321 }
1322
1323 char oat_dir[PKG_PATH_MAX];
Calin Juravle114f0812017-03-08 19:05:07 -08001324 snprintf(oat_dir, PKG_PATH_MAX, "%s/oat", dex_dir.c_str());
Calin Juravle80a21252017-01-17 14:43:25 -08001325
Calin Juravle7d765462017-09-04 15:57:10 -07001326 if (prepare_app_cache_dir(oat_dir, instruction_set, oat_dir_mode, uid, uid) != 0) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001327 LOG(ERROR) << "Could not prepare oat/isa dir for secondary dex: " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001328 return false;
1329 }
1330
1331 return true;
1332}
1333
Calin Juravle7d765462017-09-04 15:57:10 -07001334// Return codes for identifying the reason why dexoptanalyzer was not invoked when processing
1335// secondary dex files. This return codes are returned by the child process created for
1336// analyzing secondary dex files in process_secondary_dex_dexopt.
Calin Juravle80a21252017-01-17 14:43:25 -08001337
Andreas Gampe194fe422018-02-28 20:16:19 -08001338enum DexoptAnalyzerSkipCodes {
1339 // The dexoptanalyzer was not invoked because of validation or IO errors.
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001340 // Specific errors are encoded in the name.
1341 kSecondaryDexDexoptAnalyzerSkippedValidatePath = 200,
1342 kSecondaryDexDexoptAnalyzerSkippedOpenZip = 201,
1343 kSecondaryDexDexoptAnalyzerSkippedPrepareDir = 202,
1344 kSecondaryDexDexoptAnalyzerSkippedOpenOutput = 203,
1345 kSecondaryDexDexoptAnalyzerSkippedFailExec = 204,
Andreas Gampe194fe422018-02-28 20:16:19 -08001346 // The dexoptanalyzer was not invoked because the dex file does not exist anymore.
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001347 kSecondaryDexDexoptAnalyzerSkippedNoFile = 205,
Andreas Gampe194fe422018-02-28 20:16:19 -08001348};
Calin Juravle7d765462017-09-04 15:57:10 -07001349
1350// Verifies the result of analyzing secondary dex files from process_secondary_dex_dexopt.
Calin Juravle80a21252017-01-17 14:43:25 -08001351// If the result is valid returns true and sets dexopt_needed_out to a valid value.
1352// Returns false for errors or unexpected result values.
Calin Juravle7d765462017-09-04 15:57:10 -07001353// The result is expected to be either one of SECONDARY_DEX_* codes or a valid exit code
1354// of dexoptanalyzer.
1355static bool process_secondary_dexoptanalyzer_result(const std::string& dex_path, int result,
Andreas Gampe194fe422018-02-28 20:16:19 -08001356 int* dexopt_needed_out, std::string* error_msg) {
Calin Juravle80a21252017-01-17 14:43:25 -08001357 // The result values are defined in dexoptanalyzer.
1358 switch (result) {
Calin Juravle7d765462017-09-04 15:57:10 -07001359 case 0: // dexoptanalyzer: no_dexopt_needed
Calin Juravle80a21252017-01-17 14:43:25 -08001360 *dexopt_needed_out = NO_DEXOPT_NEEDED; return true;
Calin Juravle7d765462017-09-04 15:57:10 -07001361 case 1: // dexoptanalyzer: dex2oat_from_scratch
Calin Juravle80a21252017-01-17 14:43:25 -08001362 *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; return true;
Vladimir Marko1752a112018-09-03 18:15:16 +01001363 case 4: // dexoptanalyzer: dex2oat_for_bootimage_odex
Calin Juravle80a21252017-01-17 14:43:25 -08001364 *dexopt_needed_out = -DEX2OAT_FOR_BOOT_IMAGE; return true;
Vladimir Marko1752a112018-09-03 18:15:16 +01001365 case 5: // dexoptanalyzer: dex2oat_for_filter_odex
Calin Juravle80a21252017-01-17 14:43:25 -08001366 *dexopt_needed_out = -DEX2OAT_FOR_FILTER; return true;
Calin Juravle7d765462017-09-04 15:57:10 -07001367 case 2: // dexoptanalyzer: dex2oat_for_bootimage_oat
1368 case 3: // dexoptanalyzer: dex2oat_for_filter_oat
Andreas Gampe194fe422018-02-28 20:16:19 -08001369 *error_msg = StringPrintf("Dexoptanalyzer return the status of an oat file."
1370 " Expected odex file status for secondary dex %s"
1371 " : dexoptanalyzer result=%d",
1372 dex_path.c_str(),
1373 result);
Calin Juravle80a21252017-01-17 14:43:25 -08001374 return false;
Andreas Gampe194fe422018-02-28 20:16:19 -08001375 }
1376
1377 // Use a second switch for enum switch-case analysis.
1378 switch (static_cast<DexoptAnalyzerSkipCodes>(result)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001379 case kSecondaryDexDexoptAnalyzerSkippedNoFile:
Calin Juravle7d765462017-09-04 15:57:10 -07001380 // If the file does not exist there's no need for dexopt.
1381 *dexopt_needed_out = NO_DEXOPT_NEEDED;
1382 return true;
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001383
1384 case kSecondaryDexDexoptAnalyzerSkippedValidatePath:
1385 *error_msg = "Dexoptanalyzer path validation failed";
1386 return false;
1387 case kSecondaryDexDexoptAnalyzerSkippedOpenZip:
1388 *error_msg = "Dexoptanalyzer open zip failed";
1389 return false;
1390 case kSecondaryDexDexoptAnalyzerSkippedPrepareDir:
1391 *error_msg = "Dexoptanalyzer dir preparation failed";
1392 return false;
1393 case kSecondaryDexDexoptAnalyzerSkippedOpenOutput:
1394 *error_msg = "Dexoptanalyzer open output failed";
1395 return false;
1396 case kSecondaryDexDexoptAnalyzerSkippedFailExec:
1397 *error_msg = "Dexoptanalyzer failed to execute";
Calin Juravle80a21252017-01-17 14:43:25 -08001398 return false;
1399 }
Andreas Gampe194fe422018-02-28 20:16:19 -08001400
1401 *error_msg = StringPrintf("Unexpected result from analyzing secondary dex %s result=%d",
1402 dex_path.c_str(),
1403 result);
1404 return false;
Calin Juravle80a21252017-01-17 14:43:25 -08001405}
1406
Calin Juravle7d765462017-09-04 15:57:10 -07001407enum SecondaryDexAccess {
1408 kSecondaryDexAccessReadOk = 0,
1409 kSecondaryDexAccessDoesNotExist = 1,
1410 kSecondaryDexAccessPermissionError = 2,
1411 kSecondaryDexAccessIOError = 3
1412};
1413
1414static SecondaryDexAccess check_secondary_dex_access(const std::string& dex_path) {
1415 // Check if the path exists and can be read. If not, there's nothing to do.
1416 if (access(dex_path.c_str(), R_OK) == 0) {
1417 return kSecondaryDexAccessReadOk;
1418 } else {
1419 if (errno == ENOENT) {
Martijn Coenenc417edf2021-07-12 11:47:39 +02001420 async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG,
1421 "Secondary dex does not exist: %s", dex_path.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07001422 return kSecondaryDexAccessDoesNotExist;
1423 } else {
Martijn Coenenc417edf2021-07-12 11:47:39 +02001424 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
1425 "Could not access secondary dex: %s (%d)", dex_path.c_str(), errno);
Calin Juravle7d765462017-09-04 15:57:10 -07001426 return errno == EACCES
1427 ? kSecondaryDexAccessPermissionError
1428 : kSecondaryDexAccessIOError;
1429 }
1430 }
1431}
1432
1433static bool is_file_public(const std::string& filename) {
1434 struct stat file_stat;
1435 if (stat(filename.c_str(), &file_stat) == 0) {
1436 return (file_stat.st_mode & S_IROTH) != 0;
1437 }
1438 return false;
1439}
1440
1441// Create the oat file structure for the secondary dex 'dex_path' and assign
1442// the individual path component to the 'out_' parameters.
1443static bool create_secondary_dex_oat_layout(const std::string& dex_path, const std::string& isa,
Andreas Gampe194fe422018-02-28 20:16:19 -08001444 char* out_oat_dir, char* out_oat_isa_dir, char* out_oat_path, std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001445 size_t dirIndex = dex_path.rfind('/');
1446 if (dirIndex == std::string::npos) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001447 *error_msg = std::string("Unexpected dir structure for dex file ").append(dex_path);
Calin Juravle7d765462017-09-04 15:57:10 -07001448 return false;
1449 }
1450 // TODO(calin): we have similar computations in at lest 3 other places
1451 // (InstalldNativeService, otapropt and dexopt). Unify them and get rid of snprintf by
1452 // using string append.
1453 std::string apk_dir = dex_path.substr(0, dirIndex);
1454 snprintf(out_oat_dir, PKG_PATH_MAX, "%s/oat", apk_dir.c_str());
1455 snprintf(out_oat_isa_dir, PKG_PATH_MAX, "%s/%s", out_oat_dir, isa.c_str());
1456
1457 if (!create_oat_out_path(dex_path.c_str(), isa.c_str(), out_oat_dir,
1458 /*is_secondary_dex*/true, out_oat_path)) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001459 *error_msg = std::string("Could not create oat path for secondary dex ").append(dex_path);
Calin Juravle7d765462017-09-04 15:57:10 -07001460 return false;
1461 }
1462 return true;
1463}
1464
1465// Validate that the dexopt_flags contain a valid storage flag and convert that to an installd
1466// recognized storage flags (FLAG_STORAGE_CE or FLAG_STORAGE_DE).
Andreas Gampe194fe422018-02-28 20:16:19 -08001467static bool validate_dexopt_storage_flags(int dexopt_flags,
1468 int* out_storage_flag,
1469 std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001470 if ((dexopt_flags & DEXOPT_STORAGE_CE) != 0) {
1471 *out_storage_flag = FLAG_STORAGE_CE;
1472 if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001473 *error_msg = "Ambiguous secondary dex storage flag. Both, CE and DE, flags are set";
Calin Juravle7d765462017-09-04 15:57:10 -07001474 return false;
1475 }
1476 } else if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) {
1477 *out_storage_flag = FLAG_STORAGE_DE;
1478 } else {
Andreas Gampe194fe422018-02-28 20:16:19 -08001479 *error_msg = "Secondary dex storage flag must be set";
Calin Juravle7d765462017-09-04 15:57:10 -07001480 return false;
1481 }
1482 return true;
1483}
1484
David Brazdil4f6027a2019-03-19 11:44:21 +00001485static bool get_class_loader_context_dex_paths(const char* class_loader_context, int uid,
1486 /* out */ std::vector<std::string>* context_dex_paths) {
1487 if (class_loader_context == nullptr) {
1488 return true;
1489 }
1490
1491 LOG(DEBUG) << "Getting dex paths for context " << class_loader_context;
1492
1493 // Pipe to get the hash result back from our child process.
1494 unique_fd pipe_read, pipe_write;
1495 if (!Pipe(&pipe_read, &pipe_write)) {
1496 PLOG(ERROR) << "Failed to create pipe";
1497 return false;
1498 }
1499
1500 pid_t pid = fork();
1501 if (pid == 0) {
1502 // child -- drop privileges before continuing.
1503 drop_capabilities(uid);
1504
1505 // Route stdout to `pipe_write`
1506 while ((dup2(pipe_write, STDOUT_FILENO) == -1) && (errno == EINTR)) {}
1507 pipe_write.reset();
1508 pipe_read.reset();
1509
1510 RunDexoptAnalyzer run_dexopt_analyzer(class_loader_context);
1511 run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec);
1512 }
1513
1514 /* parent */
1515 pipe_write.reset();
1516
1517 std::string str_dex_paths;
1518 if (!ReadFdToString(pipe_read, &str_dex_paths)) {
1519 PLOG(ERROR) << "Failed to read from pipe";
1520 return false;
1521 }
1522 pipe_read.reset();
1523
1524 int return_code = wait_child(pid);
1525 if (!WIFEXITED(return_code)) {
1526 PLOG(ERROR) << "Error waiting for child dexoptanalyzer process";
1527 return false;
1528 }
1529
1530 constexpr int kFlattenClassLoaderContextSuccess = 50;
1531 return_code = WEXITSTATUS(return_code);
1532 if (return_code != kFlattenClassLoaderContextSuccess) {
1533 LOG(ERROR) << "Dexoptanalyzer could not flatten class loader context, code=" << return_code;
1534 return false;
1535 }
1536
1537 if (!str_dex_paths.empty()) {
1538 *context_dex_paths = android::base::Split(str_dex_paths, ":");
1539 }
1540 return true;
1541}
1542
1543static int open_dex_paths(const std::vector<std::string>& dex_paths,
1544 /* out */ std::vector<unique_fd>* zip_fds, /* out */ std::string* error_msg) {
1545 for (const std::string& dex_path : dex_paths) {
1546 zip_fds->emplace_back(open(dex_path.c_str(), O_RDONLY));
1547 if (zip_fds->back().get() < 0) {
1548 *error_msg = StringPrintf(
1549 "installd cannot open '%s' for input during dexopt", dex_path.c_str());
1550 if (errno == ENOENT) {
1551 return kSecondaryDexDexoptAnalyzerSkippedNoFile;
1552 } else {
1553 return kSecondaryDexDexoptAnalyzerSkippedOpenZip;
1554 }
1555 }
1556 }
1557 return 0;
1558}
1559
1560static std::string join_fds(const std::vector<unique_fd>& fds) {
1561 std::stringstream ss;
1562 bool is_first = true;
1563 for (const unique_fd& fd : fds) {
1564 if (is_first) {
1565 is_first = false;
1566 } else {
1567 ss << ":";
1568 }
1569 ss << fd.get();
1570 }
1571 return ss.str();
1572}
1573
Keun young Park65578d62021-06-28 17:52:05 -07001574void control_dexopt_blocking(bool block) {
1575 dexopt_status_->control_dexopt_blocking(block);
1576}
1577
1578bool is_dexopt_blocked() {
1579 return dexopt_status_->is_dexopt_blocked();
1580}
1581
1582enum SecondaryDexOptProcessResult {
1583 kSecondaryDexOptProcessOk = 0,
1584 kSecondaryDexOptProcessCancelled = 1,
1585 kSecondaryDexOptProcessError = 2
1586};
1587
Calin Juravlec9eab382017-01-25 01:17:17 -08001588// Processes the dex_path as a secondary dex files and return true if the path dex file should
Keun young Park65578d62021-06-28 17:52:05 -07001589// be compiled.
1590// Returns: kSecondaryDexOptProcessError for errors (logged).
1591// kSecondaryDexOptProcessOk if the secondary dex path was process successfully.
1592// kSecondaryDexOptProcessCancelled if the processing was cancelled.
1593//
1594// When returning kSecondaryDexOptProcessOk, the output parameters will be:
Calin Juravleebc8a792017-04-04 20:21:05 -07001595// - is_public_out: whether or not the oat file should not be made public
1596// - dexopt_needed_out: valid OatFileAsssitant::DexOptNeeded
1597// - oat_dir_out: the oat dir path where the oat file should be stored
Keun young Park65578d62021-06-28 17:52:05 -07001598static SecondaryDexOptProcessResult process_secondary_dex_dexopt(const std::string& dex_path,
1599 const char* pkgname, int dexopt_flags, const char* volume_uuid, int uid,
1600 const char* instruction_set, const char* compiler_filter, bool* is_public_out,
1601 int* dexopt_needed_out, std::string* oat_dir_out, bool downgrade,
1602 const char* class_loader_context, const std::vector<std::string>& context_dex_paths,
1603 /* out */ std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001604 LOG(DEBUG) << "Processing secondary dex path " << dex_path;
Keun young Park65578d62021-06-28 17:52:05 -07001605
1606 if (dexopt_status_->is_dexopt_blocked()) {
1607 return kSecondaryDexOptProcessCancelled;
1608 }
1609
Calin Juravle80a21252017-01-17 14:43:25 -08001610 int storage_flag;
Andreas Gampe194fe422018-02-28 20:16:19 -08001611 if (!validate_dexopt_storage_flags(dexopt_flags, &storage_flag, error_msg)) {
1612 LOG(ERROR) << *error_msg;
Keun young Park65578d62021-06-28 17:52:05 -07001613 return kSecondaryDexOptProcessError;
Calin Juravle80a21252017-01-17 14:43:25 -08001614 }
Calin Juravle7d765462017-09-04 15:57:10 -07001615 // Compute the oat dir as it's not easy to extract it from the child computation.
1616 char oat_path[PKG_PATH_MAX];
1617 char oat_dir[PKG_PATH_MAX];
1618 char oat_isa_dir[PKG_PATH_MAX];
1619 if (!create_secondary_dex_oat_layout(
Andreas Gampe194fe422018-02-28 20:16:19 -08001620 dex_path, instruction_set, oat_dir, oat_isa_dir, oat_path, error_msg)) {
1621 LOG(ERROR) << "Could not create secondary odex layout: " << *error_msg;
Keun young Park65578d62021-06-28 17:52:05 -07001622 return kSecondaryDexOptProcessError;
Calin Juravled23dee72017-07-06 16:29:11 -07001623 }
Calin Juravle7d765462017-09-04 15:57:10 -07001624 oat_dir_out->assign(oat_dir);
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001625
Keun young Park65578d62021-06-28 17:52:05 -07001626 bool cancelled = false;
1627 pid_t pid = dexopt_status_->check_cancellation_and_fork(&cancelled);
1628 if (cancelled) {
1629 return kSecondaryDexOptProcessCancelled;
1630 }
Calin Juravle80a21252017-01-17 14:43:25 -08001631 if (pid == 0) {
1632 // child -- drop privileges before continuing.
1633 drop_capabilities(uid);
Calin Juravle7d765462017-09-04 15:57:10 -07001634
1635 // Validate the path structure.
1636 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid, uid, storage_flag)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02001637 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
1638 "Could not validate secondary dex path %s", dex_path.c_str());
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001639 _exit(kSecondaryDexDexoptAnalyzerSkippedValidatePath);
Calin Juravle7d765462017-09-04 15:57:10 -07001640 }
1641
1642 // Open the dex file.
1643 unique_fd zip_fd;
1644 zip_fd.reset(open(dex_path.c_str(), O_RDONLY));
1645 if (zip_fd.get() < 0) {
1646 if (errno == ENOENT) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001647 _exit(kSecondaryDexDexoptAnalyzerSkippedNoFile);
Calin Juravle7d765462017-09-04 15:57:10 -07001648 } else {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001649 _exit(kSecondaryDexDexoptAnalyzerSkippedOpenZip);
Calin Juravle7d765462017-09-04 15:57:10 -07001650 }
1651 }
1652
David Brazdil4f6027a2019-03-19 11:44:21 +00001653 // Open class loader context dex files.
1654 std::vector<unique_fd> context_zip_fds;
1655 int open_dex_paths_rc = open_dex_paths(context_dex_paths, &context_zip_fds, error_msg);
1656 if (open_dex_paths_rc != 0) {
1657 _exit(open_dex_paths_rc);
1658 }
1659
Calin Juravle7d765462017-09-04 15:57:10 -07001660 // Prepare the oat directories.
1661 if (!prepare_secondary_dex_oat_dir(dex_path, uid, instruction_set)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001662 _exit(kSecondaryDexDexoptAnalyzerSkippedPrepareDir);
Calin Juravle7d765462017-09-04 15:57:10 -07001663 }
1664
1665 // Open the vdex/oat files if any.
1666 unique_fd oat_file_fd;
1667 unique_fd vdex_file_fd;
1668 if (!maybe_open_oat_and_vdex_file(dex_path,
1669 *oat_dir_out,
1670 instruction_set,
1671 true /* is_secondary_dex */,
1672 &oat_file_fd,
1673 &vdex_file_fd)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001674 _exit(kSecondaryDexDexoptAnalyzerSkippedOpenOutput);
Calin Juravle7d765462017-09-04 15:57:10 -07001675 }
1676
1677 // Analyze profiles.
Calin Juravle3b758282021-06-08 08:04:52 -07001678 int profile_analysis_result = analyze_profiles(uid, pkgname, dex_path,
Calin Juravle824a64d2018-01-18 20:23:17 -08001679 /*is_secondary_dex*/true);
Calin Juravle7d765462017-09-04 15:57:10 -07001680
1681 // Run dexoptanalyzer to get dexopt_needed code. This is not expected to return.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001682 // Note that we do not do it before the fork since opening the files is required to happen
1683 // after forking.
1684 RunDexoptAnalyzer run_dexopt_analyzer(dex_path,
1685 vdex_file_fd.get(),
1686 oat_file_fd.get(),
1687 zip_fd.get(),
1688 instruction_set,
Calin Juravle3b758282021-06-08 08:04:52 -07001689 compiler_filter,
1690 profile_analysis_result,
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001691 downgrade,
David Brazdil4f6027a2019-03-19 11:44:21 +00001692 class_loader_context,
1693 join_fds(context_zip_fds));
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001694 run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec);
Calin Juravle80a21252017-01-17 14:43:25 -08001695 }
1696
1697 /* parent */
Calin Juravle80a21252017-01-17 14:43:25 -08001698 int result = wait_child(pid);
Keun young Park65578d62021-06-28 17:52:05 -07001699 cancelled = dexopt_status_->check_if_killed_and_remove_dexopt_pid(pid);
Calin Juravle80a21252017-01-17 14:43:25 -08001700 if (!WIFEXITED(result)) {
Keun young Park65578d62021-06-28 17:52:05 -07001701 if ((WTERMSIG(result) == SIGKILL) && cancelled) {
1702 LOG(INFO) << "dexoptanalyzer cancelled for path:" << dex_path;
1703 return kSecondaryDexOptProcessCancelled;
1704 }
Andreas Gampe194fe422018-02-28 20:16:19 -08001705 *error_msg = StringPrintf("dexoptanalyzer failed for path %s: 0x%04x",
1706 dex_path.c_str(),
1707 result);
1708 LOG(ERROR) << *error_msg;
Keun young Park65578d62021-06-28 17:52:05 -07001709 return kSecondaryDexOptProcessError;
Calin Juravle80a21252017-01-17 14:43:25 -08001710 }
1711 result = WEXITSTATUS(result);
Calin Juravle7d765462017-09-04 15:57:10 -07001712 // Check that we successfully executed dexoptanalyzer.
Andreas Gampe194fe422018-02-28 20:16:19 -08001713 bool success = process_secondary_dexoptanalyzer_result(dex_path,
1714 result,
1715 dexopt_needed_out,
1716 error_msg);
1717 if (!success) {
1718 LOG(ERROR) << *error_msg;
1719 }
Calin Juravle7d765462017-09-04 15:57:10 -07001720
1721 LOG(DEBUG) << "Processed secondary dex file " << dex_path << " result=" << result;
1722
Calin Juravle80a21252017-01-17 14:43:25 -08001723 // Run dexopt only if needed or forced.
Calin Juravle7d765462017-09-04 15:57:10 -07001724 // Note that dexoptanalyzer is executed even if force compilation is enabled (because it
1725 // makes the code simpler; force compilation is only needed during tests).
1726 if (success &&
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001727 (result != kSecondaryDexDexoptAnalyzerSkippedNoFile) &&
Calin Juravle7d765462017-09-04 15:57:10 -07001728 ((dexopt_flags & DEXOPT_FORCE) != 0)) {
Calin Juravle80a21252017-01-17 14:43:25 -08001729 *dexopt_needed_out = DEX2OAT_FROM_SCRATCH;
1730 }
1731
Calin Juravle7d765462017-09-04 15:57:10 -07001732 // Check if we should make the oat file public.
1733 // Note that if the dex file is not public the compiled code cannot be made public.
1734 // It is ok to check this flag outside in the parent process.
1735 *is_public_out = ((dexopt_flags & DEXOPT_PUBLIC) != 0) && is_file_public(dex_path);
1736
Keun young Park65578d62021-06-28 17:52:05 -07001737 return success ? kSecondaryDexOptProcessOk : kSecondaryDexOptProcessError;
Calin Juravle80a21252017-01-17 14:43:25 -08001738}
1739
Andreas Gampefa2dadd2018-02-28 19:52:47 -08001740static std::string format_dexopt_error(int status, const char* dex_path) {
1741 if (WIFEXITED(status)) {
1742 int int_code = WEXITSTATUS(status);
1743 const char* code_name = get_return_code_name(static_cast<DexoptReturnCodes>(int_code));
1744 if (code_name != nullptr) {
1745 return StringPrintf("Dex2oat invocation for %s failed: %s", dex_path, code_name);
1746 }
1747 }
1748 return StringPrintf("Dex2oat invocation for %s failed with 0x%04x", dex_path, status);
Andreas Gampe023b2242018-02-28 16:03:25 -08001749}
1750
Keun young Park65578d62021-06-28 17:52:05 -07001751
Calin Juravlec9eab382017-01-25 01:17:17 -08001752int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* instruction_set,
Calin Juravle80a21252017-01-17 14:43:25 -08001753 int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
Calin Juravle52c45822017-07-13 22:50:21 -07001754 const char* volume_uuid, const char* class_loader_context, const char* se_info,
Calin Juravle62c5a372018-02-01 17:03:23 +00001755 bool downgrade, int target_sdk_version, const char* profile_name,
Keun young Park65578d62021-06-28 17:52:05 -07001756 const char* dex_metadata_path, const char* compilation_reason, std::string* error_msg,
1757 /* out */ bool* completed) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001758 CHECK(pkgname != nullptr);
1759 CHECK(pkgname[0] != 0);
Andreas Gampe023b2242018-02-28 16:03:25 -08001760 CHECK(error_msg != nullptr);
Andreas Gamped32eec22018-02-28 16:02:51 -08001761 CHECK_EQ(dexopt_flags & ~DEXOPT_MASK, 0)
1762 << "dexopt flags contains unknown fields: " << dexopt_flags;
Calin Juravle7a570e82017-01-14 16:23:30 -08001763
Keun young Park65578d62021-06-28 17:52:05 -07001764 bool local_completed; // local placeholder for nullptr case
1765 if (completed == nullptr) {
1766 completed = &local_completed;
1767 }
1768 *completed = true;
1769 if (dexopt_status_->is_dexopt_blocked()) {
1770 *completed = false;
1771 return 0;
1772 }
1773
Calin Juravled23dee72017-07-06 16:29:11 -07001774 if (!validate_dex_path_size(dex_path)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001775 *error_msg = StringPrintf("Failed to validate %s", dex_path);
Calin Juravle52c45822017-07-13 22:50:21 -07001776 return -1;
1777 }
1778
1779 if (class_loader_context != nullptr && strlen(class_loader_context) > PKG_PATH_MAX) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001780 *error_msg = StringPrintf("Class loader context exceeds the allowed size: %s",
1781 class_loader_context);
1782 LOG(ERROR) << *error_msg;
Calin Juravle52c45822017-07-13 22:50:21 -07001783 return -1;
Calin Juravled23dee72017-07-06 16:29:11 -07001784 }
1785
Calin Juravleebc8a792017-04-04 20:21:05 -07001786 bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0;
Calin Juravle7a570e82017-01-14 16:23:30 -08001787 bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
1788 bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
1789 bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0;
Calin Juravle80a21252017-01-17 14:43:25 -08001790 bool is_secondary_dex = (dexopt_flags & DEXOPT_SECONDARY_DEX) != 0;
Andreas Gampea73a0cb2017-11-02 18:14:42 -07001791 bool background_job_compile = (dexopt_flags & DEXOPT_IDLE_BACKGROUND_JOB) != 0;
David Brazdil52249162018-02-12 18:04:59 -08001792 bool enable_hidden_api_checks = (dexopt_flags & DEXOPT_ENABLE_HIDDEN_API_CHECKS) != 0;
Mathieu Chartierf69c2f72018-03-06 13:55:58 -08001793 bool generate_compact_dex = (dexopt_flags & DEXOPT_GENERATE_COMPACT_DEX) != 0;
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -07001794 bool generate_app_image = (dexopt_flags & DEXOPT_GENERATE_APP_IMAGE) != 0;
Patrick Baumann2271b3e2020-04-14 17:03:00 -07001795 bool for_restore = (dexopt_flags & DEXOPT_FOR_RESTORE) != 0;
Calin Juravle80a21252017-01-17 14:43:25 -08001796
1797 // Check if we're dealing with a secondary dex file and if we need to compile it.
1798 std::string oat_dir_str;
David Brazdil4f6027a2019-03-19 11:44:21 +00001799 std::vector<std::string> context_dex_paths;
Calin Juravle80a21252017-01-17 14:43:25 -08001800 if (is_secondary_dex) {
David Brazdil4f6027a2019-03-19 11:44:21 +00001801 if (!get_class_loader_context_dex_paths(class_loader_context, uid, &context_dex_paths)) {
1802 *error_msg = "Failed acquiring context dex paths";
1803 return -1; // We had an error, logged in the process method.
1804 }
Keun young Park65578d62021-06-28 17:52:05 -07001805 SecondaryDexOptProcessResult sec_dex_result = process_secondary_dex_dexopt(dex_path,
1806 pkgname, dexopt_flags, volume_uuid, uid,instruction_set, compiler_filter,
1807 &is_public, &dexopt_needed, &oat_dir_str, downgrade, class_loader_context,
1808 context_dex_paths, error_msg);
1809 if (sec_dex_result == kSecondaryDexOptProcessOk) {
Calin Juravle80a21252017-01-17 14:43:25 -08001810 oat_dir = oat_dir_str.c_str();
1811 if (dexopt_needed == NO_DEXOPT_NEEDED) {
Keun young Parkb7342262021-10-25 08:09:27 -07001812 *completed = true;
Calin Juravle80a21252017-01-17 14:43:25 -08001813 return 0; // Nothing to do, report success.
1814 }
Keun young Park65578d62021-06-28 17:52:05 -07001815 } else if (sec_dex_result == kSecondaryDexOptProcessCancelled) {
1816 // cancelled, not an error.
1817 *completed = false;
1818 return 0;
Calin Juravle80a21252017-01-17 14:43:25 -08001819 } else {
Andreas Gampe194fe422018-02-28 20:16:19 -08001820 if (error_msg->empty()) { // TODO: Make this a CHECK.
1821 *error_msg = "Failed processing secondary.";
1822 }
Calin Juravle80a21252017-01-17 14:43:25 -08001823 return -1; // We had an error, logged in the process method.
1824 }
1825 } else {
David Brazdil4f6027a2019-03-19 11:44:21 +00001826 // Currently these flags are only used for secondary dex files.
Calin Juravlec9eab382017-01-25 01:17:17 -08001827 // Verify that they are not set for primary apks.
Calin Juravle80a21252017-01-17 14:43:25 -08001828 CHECK((dexopt_flags & DEXOPT_STORAGE_CE) == 0);
1829 CHECK((dexopt_flags & DEXOPT_STORAGE_DE) == 0);
1830 }
Calin Juravle7a570e82017-01-14 16:23:30 -08001831
1832 // Open the input file.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001833 UniqueFile in_dex(open(dex_path, O_RDONLY, 0), dex_path);
1834 if (in_dex.fd() < 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001835 *error_msg = StringPrintf("installd cannot open '%s' for input during dexopt", dex_path);
1836 LOG(ERROR) << *error_msg;
Calin Juravle7a570e82017-01-14 16:23:30 -08001837 return -1;
1838 }
1839
David Brazdil4f6027a2019-03-19 11:44:21 +00001840 // Open class loader context dex files.
1841 std::vector<unique_fd> context_input_fds;
1842 if (open_dex_paths(context_dex_paths, &context_input_fds, error_msg) != 0) {
1843 LOG(ERROR) << *error_msg;
1844 return -1;
1845 }
1846
Calin Juravle7a570e82017-01-14 16:23:30 -08001847 // Create the output OAT file.
Keun young Parkb7342262021-10-25 08:09:27 -07001848 RestorableFile out_oat =
1849 open_oat_out_file(dex_path, oat_dir, is_public, uid, instruction_set, is_secondary_dex);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001850 if (out_oat.fd() < 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001851 *error_msg = "Could not open out oat file.";
Calin Juravle7a570e82017-01-14 16:23:30 -08001852 return -1;
1853 }
1854
1855 // Open vdex files.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001856 UniqueFile in_vdex;
Keun young Parkb7342262021-10-25 08:09:27 -07001857 RestorableFile out_vdex;
Victor Hsiehcb35a062020-08-13 16:11:13 -07001858 if (!open_vdex_files_for_dex2oat(dex_path, out_oat.path().c_str(), dexopt_needed,
1859 instruction_set, is_public, uid, is_secondary_dex, profile_guided, &in_vdex,
1860 &out_vdex)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001861 *error_msg = "Could not open vdex files.";
Jeff Sharkey90aff262016-12-12 14:28:24 -07001862 return -1;
1863 }
1864
Calin Juravlecb556e32017-04-04 20:22:50 -07001865 // Ensure that the oat dir and the compiler artifacts of secondary dex files have the correct
1866 // selinux context (we generate them on the fly during the dexopt invocation and they don't
1867 // fully inherit their parent context).
1868 // Note that for primary apk the oat files are created before, in a separate installd
1869 // call which also does the restorecon. TODO(calin): unify the paths.
1870 if (is_secondary_dex) {
1871 if (selinux_android_restorecon_pkgdir(oat_dir, se_info, uid,
1872 SELINUX_ANDROID_RESTORECON_RECURSE)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001873 *error_msg = std::string("Failed to restorecon ").append(oat_dir);
1874 LOG(ERROR) << *error_msg;
Calin Juravlecb556e32017-04-04 20:22:50 -07001875 return -1;
1876 }
1877 }
1878
Jeff Sharkey90aff262016-12-12 14:28:24 -07001879 // Create a swap file if necessary.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001880 unique_fd swap_fd = maybe_open_dexopt_swap_file(out_oat.path());
Jeff Sharkey90aff262016-12-12 14:28:24 -07001881
Calin Juravle7a570e82017-01-14 16:23:30 -08001882 // Open the reference profile if needed.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001883 UniqueFile reference_profile = maybe_open_reference_profile(
Calin Juravle824a64d2018-01-18 20:23:17 -08001884 pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex);
Calin Juravle7a570e82017-01-14 16:23:30 -08001885
Victor Hsiehcb35a062020-08-13 16:11:13 -07001886 if (reference_profile.fd() == -1) {
liulvping61907742018-08-21 09:36:52 +08001887 // We don't create an app image without reference profile since there is no speedup from
1888 // loading it in that case and instead will be a small overhead.
1889 generate_app_image = false;
1890 }
1891
1892 // Create the app image file if needed.
Keun young Parkb7342262021-10-25 08:09:27 -07001893 RestorableFile out_image = maybe_open_app_image(out_oat.path(), generate_app_image, is_public,
1894 uid, is_secondary_dex);
liulvping61907742018-08-21 09:36:52 +08001895
Victor Hsiehcb35a062020-08-13 16:11:13 -07001896 UniqueFile dex_metadata;
Calin Juravle62c5a372018-02-01 17:03:23 +00001897 if (dex_metadata_path != nullptr) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001898 dex_metadata.reset(TEMP_FAILURE_RETRY(open(dex_metadata_path, O_RDONLY | O_NOFOLLOW)),
1899 dex_metadata_path);
1900 if (dex_metadata.fd() < 0) {
Calin Juravle62c5a372018-02-01 17:03:23 +00001901 PLOG(ERROR) << "Failed to open dex metadata file " << dex_metadata_path;
1902 }
1903 }
1904
Victor Hsieh8948a862020-08-07 11:30:55 -07001905 std::string jitzygote_flag = server_configurable_flags::GetServerConfigurableFlag(
1906 RUNTIME_NATIVE_BOOT_NAMESPACE,
1907 ENABLE_JITZYGOTE_IMAGE,
1908 /*default_value=*/ "");
1909 bool use_jitzygote_image = jitzygote_flag == "true" || IsBootClassPathProfilingEnable();
1910
Victor Hsiehe98e6512020-08-10 15:39:22 -07001911 // Decide whether to use dex2oat64.
1912 bool use_dex2oat64 = false;
1913 // Check whether the device even supports 64-bit ABIs.
1914 if (!GetProperty("ro.product.cpu.abilist64", "").empty()) {
1915 use_dex2oat64 = GetBoolProperty("dalvik.vm.dex2oat64.enabled", false);
1916 }
1917 const char* dex2oat_bin = select_execution_binary(
1918 (use_dex2oat64 ? kDex2oat64Path : kDex2oat32Path),
1919 (use_dex2oat64 ? kDex2oatDebug64Path : kDex2oatDebug32Path),
1920 background_job_compile);
1921
Victor Hsiehc9821f12020-08-07 11:32:29 -07001922 auto execv_helper = std::make_unique<ExecVHelper>();
1923
Andreas Gampe023b2242018-02-28 16:03:25 -08001924 LOG(VERBOSE) << "DexInv: --- BEGIN '" << dex_path << "' ---";
Jeff Sharkey90aff262016-12-12 14:28:24 -07001925
Victor Hsiehc9821f12020-08-07 11:32:29 -07001926 RunDex2Oat runner(dex2oat_bin, execv_helper.get());
Keun young Parkb7342262021-10-25 08:09:27 -07001927 runner.Initialize(out_oat.GetUniqueFile(), out_vdex.GetUniqueFile(), out_image.GetUniqueFile(),
1928 in_dex, in_vdex, dex_metadata, reference_profile, class_loader_context,
1929 join_fds(context_input_fds), swap_fd.get(), instruction_set, compiler_filter,
1930 debuggable, boot_complete, for_restore, target_sdk_version,
1931 enable_hidden_api_checks, generate_compact_dex, use_jitzygote_image,
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001932 compilation_reason);
1933
Keun young Park65578d62021-06-28 17:52:05 -07001934 bool cancelled = false;
1935 pid_t pid = dexopt_status_->check_cancellation_and_fork(&cancelled);
1936 if (cancelled) {
Keun young Parkb7342262021-10-25 08:09:27 -07001937 *completed = false;
1938 reference_profile.DisableCleanup();
Keun young Park65578d62021-06-28 17:52:05 -07001939 return 0;
1940 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001941 if (pid == 0) {
Wei Wange0dbd9b2020-12-11 17:24:19 +00001942 // Need to set schedpolicy before dropping privileges
1943 // for cgroup migration. See details at b/175178520.
1944 SetDex2OatScheduling(boot_complete);
1945
Jeff Sharkey90aff262016-12-12 14:28:24 -07001946 /* child -- drop privileges before continuing */
1947 drop_capabilities(uid);
1948
Victor Hsiehcb35a062020-08-13 16:11:13 -07001949 if (flock(out_oat.fd(), LOCK_EX | LOCK_NB) != 0) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02001950 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "flock(%s) failed",
1951 out_oat.path().c_str());
Andreas Gampefa2dadd2018-02-28 19:52:47 -08001952 _exit(DexoptReturnCodes::kFlock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001953 }
1954
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001955 runner.Exec(DexoptReturnCodes::kDex2oatExec);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001956 } else {
1957 int res = wait_child(pid);
Keun young Park65578d62021-06-28 17:52:05 -07001958 bool cancelled = dexopt_status_->check_if_killed_and_remove_dexopt_pid(pid);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001959 if (res == 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001960 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' (success) ---";
Jeff Sharkey90aff262016-12-12 14:28:24 -07001961 } else {
Keun young Park65578d62021-06-28 17:52:05 -07001962 if ((WTERMSIG(res) == SIGKILL) && cancelled) {
1963 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' --- cancelled";
1964 // cancelled, not an error
1965 *completed = false;
Keun young Parkb7342262021-10-25 08:09:27 -07001966 reference_profile.DisableCleanup();
Keun young Park65578d62021-06-28 17:52:05 -07001967 return 0;
1968 }
Andreas Gampe023b2242018-02-28 16:03:25 -08001969 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' --- status=0x"
1970 << std::hex << std::setw(4) << res << ", process failed";
1971 *error_msg = format_dexopt_error(res, dex_path);
Andreas Gampe013f02e2017-03-20 18:36:54 -07001972 return res;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001973 }
1974 }
1975
Keun young Parkb7342262021-10-25 08:09:27 -07001976 // dex2oat ran successfully, so profile is safe to keep.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001977 reference_profile.DisableCleanup();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001978
Keun young Parkb7342262021-10-25 08:09:27 -07001979 // We've been successful, commit work files.
1980 // If committing (=renaming tmp to regular) fails, try to restore backup files.
1981 // If restoring fails as well, as a last resort, remove all files.
1982 if (!out_oat.CreateBackupFile() || !out_vdex.CreateBackupFile() ||
1983 !out_image.CreateBackupFile()) {
1984 // Renaming failure can mean that the original file may not be accessible from installd.
1985 LOG(ERROR) << "Cannot create backup file from existing file, file in wrong state?"
1986 << ", out_oat:" << out_oat.path() << " ,out_vdex:" << out_vdex.path()
1987 << " ,out_image:" << out_image.path();
1988 out_oat.ResetAndRemoveAllFiles();
1989 out_vdex.ResetAndRemoveAllFiles();
1990 out_image.ResetAndRemoveAllFiles();
1991 return -1;
1992 }
1993 if (!out_oat.CommitWorkFile() || !out_vdex.CommitWorkFile() || !out_image.CommitWorkFile()) {
1994 LOG(ERROR) << "Cannot commit, out_oat:" << out_oat.path()
1995 << " ,out_vdex:" << out_vdex.path() << " ,out_image:" << out_image.path();
1996 if (!out_oat.RestoreBackupFile() || !out_vdex.RestoreBackupFile() ||
1997 !out_image.RestoreBackupFile()) {
1998 LOG(ERROR) << "Cannot cancel commit, out_oat:" << out_oat.path()
1999 << " ,out_vdex:" << out_vdex.path() << " ,out_image:" << out_image.path();
2000 // Restoring failed.
2001 out_oat.ResetAndRemoveAllFiles();
2002 out_vdex.ResetAndRemoveAllFiles();
2003 out_image.ResetAndRemoveAllFiles();
2004 }
2005 return -1;
2006 }
2007 // Now remove remaining backup files.
2008 out_oat.RemoveBackupFile();
2009 out_vdex.RemoveBackupFile();
2010 out_image.RemoveBackupFile();
2011
Keun young Park65578d62021-06-28 17:52:05 -07002012 *completed = true;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002013 return 0;
2014}
2015
Calin Juravlec9eab382017-01-25 01:17:17 -08002016// Try to remove the given directory. Log an error if the directory exists
2017// and is empty but could not be removed.
2018static bool rmdir_if_empty(const char* dir) {
2019 if (rmdir(dir) == 0) {
2020 return true;
2021 }
2022 if (errno == ENOENT || errno == ENOTEMPTY) {
2023 return true;
2024 }
2025 PLOG(ERROR) << "Failed to remove dir: " << dir;
2026 return false;
2027}
2028
2029// Try to unlink the given file. Log an error if the file exists and could not
2030// be unlinked.
2031static bool unlink_if_exists(const std::string& file) {
2032 if (unlink(file.c_str()) == 0) {
2033 return true;
2034 }
2035 if (errno == ENOENT) {
2036 return true;
2037
2038 }
2039 PLOG(ERROR) << "Could not unlink: " << file;
2040 return false;
2041}
2042
Calin Juravle7d765462017-09-04 15:57:10 -07002043enum ReconcileSecondaryDexResult {
2044 kReconcileSecondaryDexExists = 0,
2045 kReconcileSecondaryDexCleanedUp = 1,
2046 kReconcileSecondaryDexValidationError = 2,
2047 kReconcileSecondaryDexCleanUpError = 3,
2048 kReconcileSecondaryDexAccessIOError = 4,
2049};
Calin Juravlec9eab382017-01-25 01:17:17 -08002050
2051// Reconcile the secondary dex 'dex_path' and its generated oat files.
2052// Return true if all the parameters are valid and the secondary dex file was
2053// processed successfully (i.e. the dex_path either exists, or if not, its corresponding
2054// oat/vdex/art files where deleted successfully). In this case, out_secondary_dex_exists
2055// will be true if the secondary dex file still exists. If the secondary dex file does not exist,
2056// the method cleans up any previously generated compiler artifacts (oat, vdex, art).
2057// Return false if there were errors during processing. In this case
2058// out_secondary_dex_exists will be set to false.
2059bool reconcile_secondary_dex_file(const std::string& dex_path,
2060 const std::string& pkgname, int uid, const std::vector<std::string>& isas,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002061 const std::optional<std::string>& volume_uuid, int storage_flag,
Calin Juravlec9eab382017-01-25 01:17:17 -08002062 /*out*/bool* out_secondary_dex_exists) {
Calin Juravle7d765462017-09-04 15:57:10 -07002063 *out_secondary_dex_exists = false; // start by assuming the file does not exist.
Calin Juravlec9eab382017-01-25 01:17:17 -08002064 if (isas.size() == 0) {
2065 LOG(ERROR) << "reconcile_secondary_dex_file called with empty isas vector";
2066 return false;
2067 }
2068
Calin Juravle7d765462017-09-04 15:57:10 -07002069 if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) {
2070 LOG(ERROR) << "reconcile_secondary_dex_file called with invalid storage_flag: "
2071 << storage_flag;
Calin Juravlec9eab382017-01-25 01:17:17 -08002072 return false;
2073 }
2074
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002075 // As a security measure we want to unlink art artifacts with the reduced capabilities
2076 // of the package user id. So we fork and drop capabilities in the child.
2077 pid_t pid = fork();
2078 if (pid == 0) {
Calin Juravle7d765462017-09-04 15:57:10 -07002079 /* child -- drop privileges before continuing */
2080 drop_capabilities(uid);
2081
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002082 const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr;
Greg Kaiser8042c372019-03-26 06:23:19 -07002083 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr,
Calin Juravle7d765462017-09-04 15:57:10 -07002084 uid, storage_flag)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002085 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2086 "Could not validate secondary dex path %s", dex_path.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07002087 _exit(kReconcileSecondaryDexValidationError);
2088 }
2089
2090 SecondaryDexAccess access_check = check_secondary_dex_access(dex_path);
2091 switch (access_check) {
2092 case kSecondaryDexAccessDoesNotExist:
2093 // File does not exist. Proceed with cleaning.
2094 break;
2095 case kSecondaryDexAccessReadOk: _exit(kReconcileSecondaryDexExists);
2096 case kSecondaryDexAccessIOError: _exit(kReconcileSecondaryDexAccessIOError);
2097 case kSecondaryDexAccessPermissionError: _exit(kReconcileSecondaryDexValidationError);
2098 default:
Martijn Coenen6de402a2021-04-26 16:23:40 +02002099 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2100 "Unexpected result from check_secondary_dex_access: %d", access_check);
Calin Juravle7d765462017-09-04 15:57:10 -07002101 _exit(kReconcileSecondaryDexValidationError);
2102 }
2103
2104 // The secondary dex does not exist anymore or it's. Clear any generated files.
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002105 char oat_path[PKG_PATH_MAX];
2106 char oat_dir[PKG_PATH_MAX];
2107 char oat_isa_dir[PKG_PATH_MAX];
2108 bool result = true;
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002109 for (size_t i = 0; i < isas.size(); i++) {
Andreas Gampe194fe422018-02-28 20:16:19 -08002110 std::string error_msg;
Calin Juravle7d765462017-09-04 15:57:10 -07002111 if (!create_secondary_dex_oat_layout(
Andreas Gampe194fe422018-02-28 20:16:19 -08002112 dex_path,isas[i], oat_dir, oat_isa_dir, oat_path, &error_msg)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002113 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "%s", error_msg.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07002114 _exit(kReconcileSecondaryDexValidationError);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002115 }
Calin Juravle51314092017-05-18 15:33:05 -07002116
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002117 // Delete oat/vdex/art files.
2118 result = unlink_if_exists(oat_path) && result;
2119 result = unlink_if_exists(create_vdex_filename(oat_path)) && result;
2120 result = unlink_if_exists(create_image_filename(oat_path)) && result;
Calin Juravlec9eab382017-01-25 01:17:17 -08002121
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002122 // Delete profiles.
2123 std::string current_profile = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -08002124 multiuser_get_user_id(uid), pkgname, dex_path, /*is_secondary*/true);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002125 std::string reference_profile = create_reference_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -08002126 pkgname, dex_path, /*is_secondary*/true);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002127 result = unlink_if_exists(current_profile) && result;
2128 result = unlink_if_exists(reference_profile) && result;
Calin Juravle51314092017-05-18 15:33:05 -07002129
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002130 // We upgraded once the location of current profile for secondary dex files.
2131 // Check for any previous left-overs and remove them as well.
2132 std::string old_current_profile = dex_path + ".prof";
2133 result = unlink_if_exists(old_current_profile);
Calin Juravle3760ad32017-07-27 16:31:55 -07002134
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002135 // Try removing the directories as well, they might be empty.
2136 result = rmdir_if_empty(oat_isa_dir) && result;
2137 result = rmdir_if_empty(oat_dir) && result;
2138 }
Calin Juravle7d765462017-09-04 15:57:10 -07002139 if (!result) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002140 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2141 "Could not validate secondary dex path %s", dex_path.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07002142 }
2143 _exit(result ? kReconcileSecondaryDexCleanedUp : kReconcileSecondaryDexAccessIOError);
Calin Juravlec9eab382017-01-25 01:17:17 -08002144 }
2145
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002146 int return_code = wait_child(pid);
Calin Juravle7d765462017-09-04 15:57:10 -07002147 if (!WIFEXITED(return_code)) {
2148 LOG(WARNING) << "reconcile dex failed for location " << dex_path << ": " << return_code;
2149 } else {
2150 return_code = WEXITSTATUS(return_code);
2151 }
2152
2153 LOG(DEBUG) << "Reconcile secondary dex path " << dex_path << " result=" << return_code;
2154
2155 switch (return_code) {
2156 case kReconcileSecondaryDexCleanedUp:
2157 case kReconcileSecondaryDexValidationError:
2158 // If we couldn't validate assume the dex file does not exist.
2159 // This will purge the entry from the PM records.
2160 *out_secondary_dex_exists = false;
2161 return true;
2162 case kReconcileSecondaryDexExists:
2163 *out_secondary_dex_exists = true;
2164 return true;
2165 case kReconcileSecondaryDexAccessIOError:
2166 // We had an access IO error.
2167 // Return false so that we can try again.
2168 // The value of out_secondary_dex_exists does not matter in this case and by convention
2169 // is set to false.
2170 *out_secondary_dex_exists = false;
2171 return false;
2172 default:
2173 LOG(ERROR) << "Unexpected code from reconcile_secondary_dex_file: " << return_code;
2174 *out_secondary_dex_exists = false;
2175 return false;
2176 }
Calin Juravlec9eab382017-01-25 01:17:17 -08002177}
2178
Alan Stokesa25d90c2017-10-16 10:56:00 +01002179// Compute and return the hash (SHA-256) of the secondary dex file at dex_path.
2180// Returns true if all parameters are valid and the hash successfully computed and stored in
2181// out_secondary_dex_hash.
2182// Also returns true with an empty hash if the file does not currently exist or is not accessible to
2183// the app.
2184// For any other errors (e.g. if any of the parameters are invalid) returns false.
2185bool hash_secondary_dex_file(const std::string& dex_path, const std::string& pkgname, int uid,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002186 const std::optional<std::string>& volume_uuid, int storage_flag,
Alan Stokesa25d90c2017-10-16 10:56:00 +01002187 std::vector<uint8_t>* out_secondary_dex_hash) {
2188 out_secondary_dex_hash->clear();
2189
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002190 const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr;
Alan Stokesa25d90c2017-10-16 10:56:00 +01002191
2192 if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) {
2193 LOG(ERROR) << "hash_secondary_dex_file called with invalid storage_flag: "
2194 << storage_flag;
2195 return false;
2196 }
2197
2198 // Pipe to get the hash result back from our child process.
2199 unique_fd pipe_read, pipe_write;
2200 if (!Pipe(&pipe_read, &pipe_write)) {
2201 PLOG(ERROR) << "Failed to create pipe";
2202 return false;
2203 }
2204
2205 // Fork so that actual access to the files is done in the app's own UID, to ensure we only
2206 // access data the app itself can access.
2207 pid_t pid = fork();
2208 if (pid == 0) {
2209 // child -- drop privileges before continuing
2210 drop_capabilities(uid);
2211 pipe_read.reset();
2212
2213 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr, uid, storage_flag)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002214 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2215 "Could not validate secondary dex path %s", dex_path.c_str());
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002216 _exit(DexoptReturnCodes::kHashValidatePath);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002217 }
2218
2219 unique_fd fd(TEMP_FAILURE_RETRY(open(dex_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)));
2220 if (fd == -1) {
2221 if (errno == EACCES || errno == ENOENT) {
2222 // Not treated as an error.
2223 _exit(0);
2224 }
2225 PLOG(ERROR) << "Failed to open secondary dex " << dex_path;
Martijn Coenen6de402a2021-04-26 16:23:40 +02002226 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2227 "Failed to open secondary dex %s: %d", dex_path.c_str(), errno);
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002228 _exit(DexoptReturnCodes::kHashOpenPath);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002229 }
2230
2231 SHA256_CTX ctx;
2232 SHA256_Init(&ctx);
2233
2234 std::vector<uint8_t> buffer(65536);
2235 while (true) {
2236 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size()));
2237 if (bytes_read == 0) {
2238 break;
2239 } else if (bytes_read == -1) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002240 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2241 "Failed to read secondary dex %s: %d", dex_path.c_str(), errno);
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002242 _exit(DexoptReturnCodes::kHashReadDex);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002243 }
2244
2245 SHA256_Update(&ctx, buffer.data(), bytes_read);
2246 }
2247
2248 std::array<uint8_t, SHA256_DIGEST_LENGTH> hash;
2249 SHA256_Final(hash.data(), &ctx);
2250 if (!WriteFully(pipe_write, hash.data(), hash.size())) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002251 _exit(DexoptReturnCodes::kHashWrite);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002252 }
2253
2254 _exit(0);
2255 }
2256
2257 // parent
2258 pipe_write.reset();
2259
2260 out_secondary_dex_hash->resize(SHA256_DIGEST_LENGTH);
2261 if (!ReadFully(pipe_read, out_secondary_dex_hash->data(), out_secondary_dex_hash->size())) {
2262 out_secondary_dex_hash->clear();
2263 }
2264 return wait_child(pid) == 0;
2265}
2266
Jeff Sharkey90aff262016-12-12 14:28:24 -07002267// Helper for move_ab, so that we can have common failure-case cleanup.
2268static bool unlink_and_rename(const char* from, const char* to) {
2269 // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise,
2270 // return a failure.
2271 struct stat s;
2272 if (stat(to, &s) == 0) {
2273 if (!S_ISREG(s.st_mode)) {
2274 LOG(ERROR) << from << " is not a regular file to replace for A/B.";
2275 return false;
2276 }
2277 if (unlink(to) != 0) {
2278 LOG(ERROR) << "Could not unlink " << to << " to move A/B.";
2279 return false;
2280 }
2281 } else {
2282 // This may be a permission problem. We could investigate the error code, but we'll just
2283 // let the rename failure do the work for us.
2284 }
2285
2286 // Try to rename "to" to "from."
2287 if (rename(from, to) != 0) {
2288 PLOG(ERROR) << "Could not rename " << from << " to " << to;
2289 return false;
2290 }
2291 return true;
2292}
2293
2294// Move/rename a B artifact (from) to an A artifact (to).
2295static bool move_ab_path(const std::string& b_path, const std::string& a_path) {
2296 // Check whether B exists.
2297 {
2298 struct stat s;
2299 if (stat(b_path.c_str(), &s) != 0) {
Alex Light9f295662021-02-25 11:50:33 -08002300 // Ignore for now. The service calling this isn't smart enough to
2301 // understand lack of artifacts at the moment.
2302 LOG(VERBOSE) << "A/B artifact " << b_path << " does not exist!";
Jeff Sharkey90aff262016-12-12 14:28:24 -07002303 return false;
2304 }
2305 if (!S_ISREG(s.st_mode)) {
2306 LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file.";
2307 // Try to unlink, but swallow errors.
2308 unlink(b_path.c_str());
2309 return false;
2310 }
2311 }
2312
2313 // Rename B to A.
2314 if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) {
2315 // Delete the b_path so we don't try again (or fail earlier).
2316 if (unlink(b_path.c_str()) != 0) {
2317 PLOG(ERROR) << "Could not unlink " << b_path;
2318 }
2319
2320 return false;
2321 }
2322
2323 return true;
2324}
2325
2326bool move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) {
2327 // Get the current slot suffix. No suffix, no A/B.
Mathieu Chartier9b2da082018-10-26 13:23:11 -07002328 const std::string slot_suffix = GetProperty("ro.boot.slot_suffix", "");
2329 if (slot_suffix.empty()) {
2330 return false;
2331 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07002332
Mathieu Chartier9b2da082018-10-26 13:23:11 -07002333 if (!ValidateTargetSlotSuffix(slot_suffix)) {
2334 LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix;
2335 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002336 }
2337
2338 // Validate other inputs.
2339 if (validate_apk_path(apk_path) != 0) {
2340 LOG(ERROR) << "Invalid apk_path: " << apk_path;
2341 return false;
2342 }
2343 if (validate_apk_path(oat_dir) != 0) {
2344 LOG(ERROR) << "Invalid oat_dir: " << oat_dir;
2345 return false;
2346 }
2347
2348 char a_path[PKG_PATH_MAX];
2349 if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) {
2350 return false;
2351 }
2352 const std::string a_vdex_path = create_vdex_filename(a_path);
2353 const std::string a_image_path = create_image_filename(a_path);
2354
2355 // B path = A path + slot suffix.
2356 const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str());
2357 const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str());
2358 const std::string b_image_path = StringPrintf("%s.%s",
2359 a_image_path.c_str(),
2360 slot_suffix.c_str());
2361
2362 bool success = true;
2363 if (move_ab_path(b_path, a_path)) {
2364 if (move_ab_path(b_vdex_path, a_vdex_path)) {
2365 // Note: we can live without an app image. As such, ignore failure to move the image file.
2366 // If we decide to require the app image, or the app image being moved correctly,
2367 // then change accordingly.
2368 constexpr bool kIgnoreAppImageFailure = true;
2369
2370 if (!a_image_path.empty()) {
2371 if (!move_ab_path(b_image_path, a_image_path)) {
2372 unlink(a_image_path.c_str());
2373 if (!kIgnoreAppImageFailure) {
2374 success = false;
2375 }
2376 }
2377 }
2378 } else {
2379 // Cleanup: delete B image, ignore errors.
2380 unlink(b_image_path.c_str());
2381 success = false;
2382 }
2383 } else {
2384 // Cleanup: delete B image, ignore errors.
2385 unlink(b_vdex_path.c_str());
2386 unlink(b_image_path.c_str());
2387 success = false;
2388 }
2389 return success;
2390}
2391
Calin Juravleea214ad2021-06-11 09:17:20 -07002392int64_t delete_odex(const char* apk_path, const char* instruction_set, const char* oat_dir) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07002393 // Delete the oat/odex file.
2394 char out_path[PKG_PATH_MAX];
Calin Juravle80a21252017-01-17 14:43:25 -08002395 if (!create_oat_out_path(apk_path, instruction_set, oat_dir,
Calin Juravle114f0812017-03-08 19:05:07 -08002396 /*is_secondary_dex*/false, out_path)) {
Calin Juravleea214ad2021-06-11 09:17:20 -07002397 LOG(ERROR) << "Cannot create apk path for " << apk_path;
2398 return -1;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002399 }
2400
2401 // In case of a permission failure report the issue. Otherwise just print a warning.
Calin Juravleea214ad2021-06-11 09:17:20 -07002402 auto unlink_and_check = [](const char* path) -> int64_t {
2403 struct stat file_stat;
2404 if (stat(path, &file_stat) != 0) {
2405 if (errno != ENOENT) {
2406 PLOG(ERROR) << "Could not stat " << path;
2407 return -1;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002408 }
Calin Juravleea214ad2021-06-11 09:17:20 -07002409 return 0;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002410 }
Calin Juravleea214ad2021-06-11 09:17:20 -07002411
2412 if (unlink(path) != 0) {
2413 if (errno != ENOENT) {
2414 PLOG(ERROR) << "Could not unlink " << path;
2415 return -1;
2416 }
2417 }
2418 return static_cast<int64_t>(file_stat.st_size);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002419 };
2420
2421 // Delete the oat/odex file.
Calin Juravleea214ad2021-06-11 09:17:20 -07002422 int64_t return_value_oat = unlink_and_check(out_path);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002423
2424 // Derive and delete the app image.
Calin Juravleea214ad2021-06-11 09:17:20 -07002425 int64_t return_value_art = unlink_and_check(create_image_filename(out_path).c_str());
Jeff Sharkey90aff262016-12-12 14:28:24 -07002426
Nicolas Geoffray192fb962017-05-25 13:58:06 +01002427 // Derive and delete the vdex file.
Calin Juravleea214ad2021-06-11 09:17:20 -07002428 int64_t return_value_vdex = unlink_and_check(create_vdex_filename(out_path).c_str());
Nicolas Geoffray192fb962017-05-25 13:58:06 +01002429
Calin Juravleea214ad2021-06-11 09:17:20 -07002430 // Report result
2431 if (return_value_oat == -1
2432 || return_value_art == -1
2433 || return_value_vdex == -1) {
2434 return -1;
2435 }
2436
2437 return return_value_oat + return_value_art + return_value_vdex;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002438}
2439
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06002440static bool is_absolute_path(const std::string& path) {
2441 if (path.find('/') != 0 || path.find("..") != std::string::npos) {
2442 LOG(ERROR) << "Invalid absolute path " << path;
2443 return false;
2444 } else {
2445 return true;
2446 }
2447}
2448
2449static bool is_valid_instruction_set(const std::string& instruction_set) {
2450 // TODO: add explicit whitelisting of instruction sets
2451 if (instruction_set.find('/') != std::string::npos) {
2452 LOG(ERROR) << "Invalid instruction set " << instruction_set;
2453 return false;
2454 } else {
2455 return true;
2456 }
2457}
2458
2459bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir,
2460 const char *apk_path, const char *instruction_set) {
2461 std::string oat_dir_ = oat_dir;
2462 std::string apk_path_ = apk_path;
2463 std::string instruction_set_ = instruction_set;
2464
2465 if (!is_absolute_path(oat_dir_)) return false;
2466 if (!is_absolute_path(apk_path_)) return false;
2467 if (!is_valid_instruction_set(instruction_set_)) return false;
2468
2469 std::string::size_type end = apk_path_.rfind('.');
2470 std::string::size_type start = apk_path_.rfind('/', end);
2471 if (end == std::string::npos || start == std::string::npos) {
2472 LOG(ERROR) << "Invalid apk_path " << apk_path_;
2473 return false;
2474 }
2475
2476 std::string res_ = oat_dir_ + '/' + instruction_set + '/'
2477 + apk_path_.substr(start + 1, end - start - 1) + ".odex";
2478 const char* res = res_.c_str();
2479 if (strlen(res) >= PKG_PATH_MAX) {
2480 LOG(ERROR) << "Result too large";
2481 return false;
2482 } else {
2483 strlcpy(path, res, PKG_PATH_MAX);
2484 return true;
2485 }
2486}
2487
2488bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path,
2489 const char *instruction_set) {
2490 std::string apk_path_ = apk_path;
2491 std::string instruction_set_ = instruction_set;
2492
2493 if (!is_absolute_path(apk_path_)) return false;
2494 if (!is_valid_instruction_set(instruction_set_)) return false;
2495
2496 std::string::size_type end = apk_path_.rfind('.');
2497 std::string::size_type start = apk_path_.rfind('/', end);
2498 if (end == std::string::npos || start == std::string::npos) {
2499 LOG(ERROR) << "Invalid apk_path " << apk_path_;
2500 return false;
2501 }
2502
2503 std::string oat_dir = apk_path_.substr(0, start + 1) + "oat";
2504 return calculate_oat_file_path_default(path, oat_dir.c_str(), apk_path, instruction_set);
2505}
2506
2507bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src,
2508 const char *instruction_set) {
2509 std::string src_ = src;
2510 std::string instruction_set_ = instruction_set;
2511
2512 if (!is_absolute_path(src_)) return false;
2513 if (!is_valid_instruction_set(instruction_set_)) return false;
2514
2515 for (auto it = src_.begin() + 1; it < src_.end(); ++it) {
2516 if (*it == '/') {
2517 *it = '@';
2518 }
2519 }
2520
2521 std::string res_ = android_data_dir + DALVIK_CACHE + '/' + instruction_set_ + src_
2522 + DALVIK_CACHE_POSTFIX;
2523 const char* res = res_.c_str();
2524 if (strlen(res) >= PKG_PATH_MAX) {
2525 LOG(ERROR) << "Result too large";
2526 return false;
2527 } else {
2528 strlcpy(path, res, PKG_PATH_MAX);
2529 return true;
2530 }
2531}
2532
Calin Juravle59f7ab82018-04-27 17:50:23 -07002533bool open_classpath_files(const std::string& classpath, std::vector<unique_fd>* apk_fds,
2534 std::vector<std::string>* dex_locations) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002535 std::vector<std::string> classpaths_elems = base::Split(classpath, ":");
2536 for (const std::string& elem : classpaths_elems) {
2537 unique_fd fd(TEMP_FAILURE_RETRY(open(elem.c_str(), O_RDONLY)));
2538 if (fd < 0) {
2539 PLOG(ERROR) << "Could not open classpath elem " << elem;
2540 return false;
2541 } else {
2542 apk_fds->push_back(std::move(fd));
Calin Juravle59f7ab82018-04-27 17:50:23 -07002543 dex_locations->push_back(elem);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002544 }
2545 }
2546 return true;
2547}
2548
2549static bool create_app_profile_snapshot(int32_t app_id,
2550 const std::string& package_name,
2551 const std::string& profile_name,
2552 const std::string& classpath) {
Calin Juravle29591732017-11-20 17:46:19 -08002553 int app_shared_gid = multiuser_get_shared_gid(/*user_id*/ 0, app_id);
2554
Alex Buynytskyy79dcab52021-11-19 11:43:04 -08002555 unique_fd snapshot_fd = open_snapshot_profile(AID_SYSTEM, package_name, profile_name);
Calin Juravle29591732017-11-20 17:46:19 -08002556 if (snapshot_fd < 0) {
2557 return false;
2558 }
2559
2560 std::vector<unique_fd> profiles_fd;
2561 unique_fd reference_profile_fd;
Calin Juravle824a64d2018-01-18 20:23:17 -08002562 open_profile_files(app_shared_gid, package_name, profile_name, /*is_secondary_dex*/ false,
2563 &profiles_fd, &reference_profile_fd);
Calin Juravle29591732017-11-20 17:46:19 -08002564 if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) {
2565 return false;
2566 }
2567
2568 profiles_fd.push_back(std::move(reference_profile_fd));
2569
Calin Juravle0d0a4922018-01-23 19:54:11 -08002570 // Open the class paths elements. These will be used to filter out profile data that does
2571 // not belong to the classpath during merge.
2572 std::vector<unique_fd> apk_fds;
Calin Juravle59f7ab82018-04-27 17:50:23 -07002573 std::vector<std::string> dex_locations;
2574 if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002575 return false;
2576 }
2577
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002578 RunProfman args;
Calin Juravlef85ddb92020-05-01 14:05:40 -07002579 // This is specifically a snapshot for an app, so don't use boot image profiles.
2580 args.SetupMerge(profiles_fd,
2581 snapshot_fd,
2582 apk_fds,
2583 dex_locations,
2584 /* for_snapshot= */ true,
2585 /* for_boot_image= */ false);
Calin Juravle29591732017-11-20 17:46:19 -08002586 pid_t pid = fork();
2587 if (pid == 0) {
2588 /* child -- drop privileges before continuing */
2589 drop_capabilities(app_shared_gid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002590 args.Exec();
Calin Juravle29591732017-11-20 17:46:19 -08002591 }
2592
2593 /* parent */
2594 int return_code = wait_child(pid);
2595 if (!WIFEXITED(return_code)) {
Calin Juravle824a64d2018-01-18 20:23:17 -08002596 LOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
Calin Juravle29591732017-11-20 17:46:19 -08002597 return false;
2598 }
2599
Calin Juravle78728f32019-11-08 17:55:46 -08002600 // Verify that profman finished successfully.
2601 int profman_code = WEXITSTATUS(return_code);
2602 if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) {
2603 LOG(WARNING) << "profman error for " << package_name << ":" << profile_name
2604 << ":" << profman_code;
2605 return false;
2606 }
Calin Juravle29591732017-11-20 17:46:19 -08002607 return true;
2608}
2609
Calin Juravle0d0a4922018-01-23 19:54:11 -08002610static bool create_boot_image_profile_snapshot(const std::string& package_name,
2611 const std::string& profile_name,
2612 const std::string& classpath) {
2613 // The reference profile directory for the android package might not be prepared. Do it now.
2614 const std::string ref_profile_dir =
2615 create_primary_reference_profile_package_dir_path(package_name);
2616 if (fs_prepare_dir(ref_profile_dir.c_str(), 0770, AID_SYSTEM, AID_SYSTEM) != 0) {
2617 PLOG(ERROR) << "Failed to prepare " << ref_profile_dir;
2618 return false;
2619 }
2620
Mathieu Chartiere0d64a12018-11-01 12:07:26 -07002621 // Return false for empty class path since it may otherwise return true below if profiles is
2622 // empty.
2623 if (classpath.empty()) {
2624 PLOG(ERROR) << "Class path is empty";
2625 return false;
2626 }
2627
Calin Juravle0d0a4922018-01-23 19:54:11 -08002628 // Open and create the snapshot profile.
Alex Buynytskyy79dcab52021-11-19 11:43:04 -08002629 unique_fd snapshot_fd = open_snapshot_profile(AID_SYSTEM, package_name, profile_name);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002630
2631 // Collect all non empty profiles.
2632 // The collection will traverse all applications profiles and find the non empty files.
2633 // This has the potential of inspecting a large number of files and directories (depending
2634 // on the number of applications and users). So there is a slight increase in the chance
2635 // to get get occasionally I/O errors (e.g. for opening the file). When that happens do not
2636 // fail the snapshot and aggregate whatever profile we could open.
2637 //
2638 // The profile snapshot is a best effort based on available data it's ok if some data
2639 // from some apps is missing. It will be counter productive for the snapshot to fail
2640 // because we could not open or read some of the files.
2641 std::vector<std::string> profiles;
2642 if (!collect_profiles(&profiles)) {
2643 LOG(WARNING) << "There were errors while collecting the profiles for the boot image.";
2644 }
2645
2646 // If we have no profiles return early.
2647 if (profiles.empty()) {
2648 return true;
2649 }
2650
2651 // Open the classpath elements. These will be used to filter out profile data that does
2652 // not belong to the classpath during merge.
2653 std::vector<unique_fd> apk_fds;
Calin Juravle59f7ab82018-04-27 17:50:23 -07002654 std::vector<std::string> dex_locations;
2655 if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002656 return false;
2657 }
2658
2659 // If we could not open any files from the classpath return an error.
2660 if (apk_fds.empty()) {
2661 LOG(ERROR) << "Could not open any of the classpath elements.";
2662 return false;
2663 }
2664
2665 // Aggregate the profiles in batches of kAggregationBatchSize.
2666 // We do this to avoid opening a huge a amount of files.
2667 static constexpr size_t kAggregationBatchSize = 10;
2668
Calin Juravle0d0a4922018-01-23 19:54:11 -08002669 for (size_t i = 0; i < profiles.size(); ) {
Calin Juravlea64fb512019-11-06 16:11:14 -08002670 std::vector<unique_fd> profiles_fd;
Calin Juravle0d0a4922018-01-23 19:54:11 -08002671 for (size_t k = 0; k < kAggregationBatchSize && i < profiles.size(); k++, i++) {
Calin Juravlee01f8712021-06-03 18:16:54 -07002672 unique_fd fd = open_profile(AID_SYSTEM, profiles[i], O_RDONLY, /*mode=*/ 0);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002673 if (fd.get() >= 0) {
2674 profiles_fd.push_back(std::move(fd));
2675 }
2676 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002677
2678 // We aggregate (read & write) into the same fd multiple times in a row.
2679 // We need to reset the cursor every time to ensure we read the whole file every time.
2680 if (TEMP_FAILURE_RETRY(lseek(snapshot_fd, 0, SEEK_SET)) == static_cast<off_t>(-1)) {
2681 PLOG(ERROR) << "Cannot reset position for snapshot profile";
2682 return false;
2683 }
2684
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002685 RunProfman args;
Calin Juravleb3a929d2018-12-11 14:40:00 -08002686 args.SetupMerge(profiles_fd,
2687 snapshot_fd,
2688 apk_fds,
Calin Juravle78728f32019-11-08 17:55:46 -08002689 dex_locations,
2690 /*for_snapshot=*/true,
2691 /*for_boot_image=*/true);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002692 pid_t pid = fork();
2693 if (pid == 0) {
2694 /* child -- drop privileges before continuing */
2695 drop_capabilities(AID_SYSTEM);
2696
Calin Juravle59f7ab82018-04-27 17:50:23 -07002697 // The introduction of new access flags into boot jars causes them to
2698 // fail dex file verification.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002699 args.Exec();
Calin Juravle0d0a4922018-01-23 19:54:11 -08002700 }
2701
2702 /* parent */
2703 int return_code = wait_child(pid);
Calin Juravlea64fb512019-11-06 16:11:14 -08002704
Calin Juravle0d0a4922018-01-23 19:54:11 -08002705 if (!WIFEXITED(return_code)) {
2706 PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
2707 return false;
2708 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002709
2710 // Verify that profman finished successfully.
2711 int profman_code = WEXITSTATUS(return_code);
Calin Juravle78728f32019-11-08 17:55:46 -08002712 if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) {
2713 LOG(WARNING) << "profman error for " << package_name << ":" << profile_name
2714 << ":" << profman_code;
2715 return false;
Calin Juravlea64fb512019-11-06 16:11:14 -08002716 }
Calin Juravle0d0a4922018-01-23 19:54:11 -08002717 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002718
Calin Juravle0d0a4922018-01-23 19:54:11 -08002719 return true;
2720}
2721
2722bool create_profile_snapshot(int32_t app_id, const std::string& package_name,
2723 const std::string& profile_name, const std::string& classpath) {
2724 if (app_id == -1) {
2725 return create_boot_image_profile_snapshot(package_name, profile_name, classpath);
2726 } else {
2727 return create_app_profile_snapshot(app_id, package_name, profile_name, classpath);
2728 }
2729}
2730
Yang Tianping8f2a40e2021-10-29 14:39:02 +08002731static bool check_profile_exists_in_dexmetadata(const std::string& dex_metadata) {
2732 ZipArchiveHandle zip = nullptr;
2733 if (OpenArchive(dex_metadata.c_str(), &zip) != 0) {
2734 PLOG(ERROR) << "Failed to open dm '" << dex_metadata << "'";
2735 return false;
2736 }
2737
2738 ZipEntry64 entry;
2739 int result = FindEntry(zip, "primary.prof", &entry);
2740 CloseArchive(zip);
2741
2742 return result != 0 ? false : true;
2743}
2744
Calin Juravlec3b049e2018-01-18 22:32:58 -08002745bool prepare_app_profile(const std::string& package_name,
2746 userid_t user_id,
2747 appid_t app_id,
2748 const std::string& profile_name,
Calin Juravlef63d4792018-01-30 17:43:34 +00002749 const std::string& code_path,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002750 const std::optional<std::string>& dex_metadata) {
Calin Juravlec3b049e2018-01-18 22:32:58 -08002751 // Prepare the current profile.
2752 std::string cur_profile = create_current_profile_path(user_id, package_name, profile_name,
2753 /*is_secondary_dex*/ false);
2754 uid_t uid = multiuser_get_uid(user_id, app_id);
2755 if (fs_prepare_file_strict(cur_profile.c_str(), 0600, uid, uid) != 0) {
2756 PLOG(ERROR) << "Failed to prepare " << cur_profile;
2757 return false;
2758 }
2759
2760 // Check if we need to install the profile from the dex metadata.
Yang Tianping8f2a40e2021-10-29 14:39:02 +08002761 if (!dex_metadata || !check_profile_exists_in_dexmetadata(dex_metadata->c_str())) {
Calin Juravlec3b049e2018-01-18 22:32:58 -08002762 return true;
2763 }
2764
2765 // We have a dex metdata. Merge the profile into the reference profile.
2766 unique_fd ref_profile_fd = open_reference_profile(uid, package_name, profile_name,
2767 /*read_write*/ true, /*is_secondary_dex*/ false);
2768 unique_fd dex_metadata_fd(TEMP_FAILURE_RETRY(
2769 open(dex_metadata->c_str(), O_RDONLY | O_NOFOLLOW)));
Calin Juravlef63d4792018-01-30 17:43:34 +00002770 unique_fd apk_fd(TEMP_FAILURE_RETRY(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW)));
2771 if (apk_fd < 0) {
2772 PLOG(ERROR) << "Could not open code path " << code_path;
2773 return false;
2774 }
Calin Juravlec3b049e2018-01-18 22:32:58 -08002775
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002776 RunProfman args;
2777 args.SetupCopyAndUpdate(std::move(dex_metadata_fd),
2778 std::move(ref_profile_fd),
2779 std::move(apk_fd),
2780 code_path);
Calin Juravlec3b049e2018-01-18 22:32:58 -08002781 pid_t pid = fork();
2782 if (pid == 0) {
2783 /* child -- drop privileges before continuing */
2784 gid_t app_shared_gid = multiuser_get_shared_gid(user_id, app_id);
2785 drop_capabilities(app_shared_gid);
2786
Calin Juravlef63d4792018-01-30 17:43:34 +00002787 // The copy and update takes ownership over the fds.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002788 args.Exec();
Calin Juravlec3b049e2018-01-18 22:32:58 -08002789 }
2790
2791 /* parent */
2792 int return_code = wait_child(pid);
2793 if (!WIFEXITED(return_code)) {
2794 PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
2795 return false;
2796 }
2797 return true;
2798}
2799
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07002800} // namespace installd
2801} // namespace android