blob: ebb78913b13ba087ee68121012b204b0f7518bb6 [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;
Jiakai Zhang495142a2022-02-21 19:38:14 +000077using android::base::borrowed_fd;
Calin Juravle1a0af3b2017-03-09 14:33:33 -080078using android::base::unique_fd;
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070079
Keun young Park65578d62021-06-28 17:52:05 -070080namespace {
81
Jiakai Zhang495142a2022-02-21 19:38:14 +000082// Timeout for short operations, such as merging profiles.
83constexpr int kShortTimeoutMs = 60000; // 1 minute.
84
85// Timeout for long operations, such as compilation. This should be smaller than the Package Manager
86// watchdog (PackageManagerService.WATCHDOG_TIMEOUT, 10 minutes), so that the operation will be
87// aborted before that watchdog would take down the system server.
88constexpr int kLongTimeoutMs = 570000; // 9.5 minutes.
89
Keun young Park65578d62021-06-28 17:52:05 -070090class DexOptStatus {
91 public:
92 // Check if dexopt is cancelled and fork if it is not cancelled.
93 // cancelled is set to true if cancelled. Otherwise it will be set to false.
94 // If it is not cancelled, it will return the return value of fork() call.
95 // If cancelled, fork will not happen and it will return -1.
96 pid_t check_cancellation_and_fork(/* out */ bool *cancelled) {
97 std::lock_guard<std::mutex> lock(dexopt_lock_);
98 if (dexopt_blocked_) {
99 *cancelled = true;
100 return -1;
101 }
102 pid_t pid = fork();
103 *cancelled = false;
104 if (pid > 0) { // parent
105 dexopt_pids_.insert(pid);
106 }
107 return pid;
108 }
109
110 // Returns true if pid was killed (is in killed list). It could have finished if killing
111 // happened after the process is finished.
112 bool check_if_killed_and_remove_dexopt_pid(pid_t pid) {
113 std::lock_guard<std::mutex> lock(dexopt_lock_);
114 dexopt_pids_.erase(pid);
115 if (dexopt_killed_pids_.erase(pid) == 1) {
116 return true;
117 }
118 return false;
119 }
120
121 // Tells whether dexopt is blocked or not.
122 bool is_dexopt_blocked() {
123 std::lock_guard<std::mutex> lock(dexopt_lock_);
124 return dexopt_blocked_;
125 }
126
127 // Enable or disable dexopt blocking.
128 void control_dexopt_blocking(bool block) {
129 std::lock_guard<std::mutex> lock(dexopt_lock_);
130 dexopt_blocked_ = block;
131 if (!block) {
132 return;
133 }
134 // Blocked, also kill currently running tasks
135 for (auto pid : dexopt_pids_) {
136 LOG(INFO) << "control_dexopt_blocking kill pid:" << pid;
137 kill(pid, SIGKILL);
138 dexopt_killed_pids_.insert(pid);
139 }
140 dexopt_pids_.clear();
141 }
142
143 private:
144 std::mutex dexopt_lock_;
145 // when true, dexopt is blocked and will not run.
146 bool dexopt_blocked_ GUARDED_BY(dexopt_lock_) = false;
147 // PIDs of child process while runinng dexopt.
148 // If the child process is finished, it should be removed.
149 std::unordered_set<pid_t> dexopt_pids_ GUARDED_BY(dexopt_lock_);
150 // PIDs of child processes killed by cancellation.
151 std::unordered_set<pid_t> dexopt_killed_pids_ GUARDED_BY(dexopt_lock_);
152};
153
154android::base::NoDestructor<DexOptStatus> dexopt_status_;
155
156} // namespace
157
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700158namespace android {
159namespace installd {
160
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800161
Calin Juravle114f0812017-03-08 19:05:07 -0800162// Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below.
163struct FreeDelete {
164 // NOTE: Deleting a const object is valid but free() takes a non-const pointer.
165 void operator()(const void* ptr) const {
166 free(const_cast<void*>(ptr));
167 }
168};
169
170// Alias for std::unique_ptr<> that uses the C function free() to delete objects.
171template <typename T>
172using UniqueCPtr = std::unique_ptr<T, FreeDelete>;
173
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800174static unique_fd invalid_unique_fd() {
175 return unique_fd(-1);
176}
177
Andreas Gampe6a9cf722017-07-24 16:49:10 -0700178static bool is_debug_runtime() {
179 return android::base::GetProperty("persist.sys.dalvik.vm.lib.2", "") == "libartd.so";
180}
181
David Sehra3b5ab62017-10-25 14:27:29 -0700182static bool is_debuggable_build() {
183 return android::base::GetBoolProperty("ro.debuggable", false);
184}
185
Jeff Sharkey90aff262016-12-12 14:28:24 -0700186static bool clear_profile(const std::string& profile) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800187 unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700188 if (ufd.get() < 0) {
189 if (errno != ENOENT) {
190 PLOG(WARNING) << "Could not open profile " << profile;
191 return false;
192 } else {
193 // Nothing to clear. That's ok.
194 return true;
195 }
196 }
197
198 if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) {
199 if (errno != EWOULDBLOCK) {
200 PLOG(WARNING) << "Error locking profile " << profile;
201 }
202 // This implies that the app owning this profile is running
203 // (and has acquired the lock).
204 //
205 // If we can't acquire the lock bail out since clearing is useless anyway
206 // (the app will write again to the profile).
207 //
208 // Note:
209 // This does not impact the this is not an issue for the profiling correctness.
210 // In case this is needed because of an app upgrade, profiles will still be
211 // eventually cleared by the app itself due to checksum mismatch.
212 // If this is needed because profman advised, then keeping the data around
213 // until the next run is again not an issue.
214 //
215 // If the app attempts to acquire a lock while we've held one here,
216 // it will simply skip the current write cycle.
217 return false;
218 }
219
220 bool truncated = ftruncate(ufd.get(), 0) == 0;
221 if (!truncated) {
222 PLOG(WARNING) << "Could not truncate " << profile;
223 }
224 if (flock(ufd.get(), LOCK_UN) != 0) {
225 PLOG(WARNING) << "Error unlocking profile " << profile;
226 }
227 return truncated;
228}
229
Calin Juravle114f0812017-03-08 19:05:07 -0800230// Clear the reference profile for the given location.
Calin Juravle824a64d2018-01-18 20:23:17 -0800231// The location is the profile name for primary apks or the dex path for secondary dex files.
232static bool clear_reference_profile(const std::string& package_name, const std::string& location,
233 bool is_secondary_dex) {
234 return clear_profile(create_reference_profile_path(package_name, location, is_secondary_dex));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700235}
236
Calin Juravle114f0812017-03-08 19:05:07 -0800237// Clear the reference profile for the given location.
Calin Juravle824a64d2018-01-18 20:23:17 -0800238// The location is the profile name for primary apks or the dex path for secondary dex files.
239static bool clear_current_profile(const std::string& package_name, const std::string& location,
240 userid_t user, bool is_secondary_dex) {
241 return clear_profile(create_current_profile_path(user, package_name, location,
242 is_secondary_dex));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700243}
244
Calin Juravle114f0812017-03-08 19:05:07 -0800245// Clear the reference profile for the primary apk of the given package.
Calin Juravle824a64d2018-01-18 20:23:17 -0800246// The location is the profile name for primary apks or the dex path for secondary dex files.
247bool clear_primary_reference_profile(const std::string& package_name,
248 const std::string& location) {
249 return clear_reference_profile(package_name, location, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800250}
251
252// Clear all current profile for the primary apk of the given package.
Calin Juravle824a64d2018-01-18 20:23:17 -0800253// The location is the profile name for primary apks or the dex path for secondary dex files.
254bool clear_primary_current_profiles(const std::string& package_name, const std::string& location) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700255 bool success = true;
Hanna Nizhnikavab35b8722022-01-21 16:59:56 +0000256 // For secondary dex files, we don't really need the user but we use it for validity checks.
Jeff Sharkey90aff262016-12-12 14:28:24 -0700257 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
258 for (auto user : users) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800259 success &= clear_current_profile(package_name, location, user, /*is_secondary_dex*/false);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700260 }
261 return success;
262}
263
Calin Juravle114f0812017-03-08 19:05:07 -0800264// Clear the current profile for the primary apk of the given package and user.
Calin Juravle824a64d2018-01-18 20:23:17 -0800265bool clear_primary_current_profile(const std::string& package_name, const std::string& location,
266 userid_t user) {
267 return clear_current_profile(package_name, location, user, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800268}
269
Calin Juravlef74a7372019-02-28 20:29:41 -0800270// Determines which binary we should use for execution (the debug or non-debug version).
271// e.g. dex2oatd vs dex2oat
272static const char* select_execution_binary(const char* binary, const char* debug_binary,
273 bool background_job_compile) {
274 return select_execution_binary(
275 binary,
276 debug_binary,
277 background_job_compile,
278 is_debug_runtime(),
279 (android::base::GetProperty("ro.build.version.codename", "") == "REL"),
280 is_debuggable_build());
281}
282
283// Determines which binary we should use for execution (the debug or non-debug version).
284// e.g. dex2oatd vs dex2oat
285// This is convenient method which is much easier to test because it doesn't read
286// system properties.
287const char* select_execution_binary(
288 const char* binary,
289 const char* debug_binary,
290 bool background_job_compile,
291 bool is_debug_runtime,
292 bool is_release,
293 bool is_debuggable_build) {
294 // Do not use debug binaries for release candidates (to give more soak time).
295 bool is_debug_bg_job = background_job_compile && is_debuggable_build && !is_release;
296
297 // If the runtime was requested to use libartd.so, we'll run the debug version - assuming
298 // the file is present (it may not be on images with very little space available).
299 bool useDebug = (is_debug_runtime || is_debug_bg_job) && (access(debug_binary, X_OK) == 0);
300
301 return useDebug ? debug_binary : binary;
302}
303
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000304// Namespace for Android Runtime flags applied during boot time.
305static const char* RUNTIME_NATIVE_BOOT_NAMESPACE = "runtime_native_boot";
306// Feature flag name for running the JIT in Zygote experiment, b/119800099.
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000307static const char* ENABLE_JITZYGOTE_IMAGE = "enable_apex_image";
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000308
Mathieu Chartiere97261e2019-10-01 15:36:01 -0700309// Phenotype property name for enabling profiling the boot class path.
310static const char* PROFILE_BOOT_CLASS_PATH = "profilebootclasspath";
311
Calin Juravlef85ddb92020-05-01 14:05:40 -0700312static bool IsBootClassPathProfilingEnable() {
313 std::string profile_boot_class_path = GetProperty("dalvik.vm.profilebootclasspath", "");
314 profile_boot_class_path =
315 server_configurable_flags::GetServerConfigurableFlag(
316 RUNTIME_NATIVE_BOOT_NAMESPACE,
317 PROFILE_BOOT_CLASS_PATH,
318 /*default_value=*/ profile_boot_class_path);
319 return profile_boot_class_path == "true";
320}
321
Jeff Sharkey90aff262016-12-12 14:28:24 -0700322/*
323 * Whether dexopt should use a swap file when compiling an APK.
324 *
325 * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
326 * itself, anyways).
327 *
328 * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
329 *
330 * Otherwise, return true if this is a low-mem device.
331 *
332 * Otherwise, return default value.
333 */
334static bool kAlwaysProvideSwapFile = false;
335static bool kDefaultProvideSwapFile = true;
336
337static bool ShouldUseSwapFileForDexopt() {
338 if (kAlwaysProvideSwapFile) {
339 return true;
340 }
341
342 // Check the "override" property. If it exists, return value == "true".
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700343 std::string dex2oat_prop_buf = GetProperty("dalvik.vm.dex2oat-swap", "");
344 if (!dex2oat_prop_buf.empty()) {
345 return dex2oat_prop_buf == "true";
Jeff Sharkey90aff262016-12-12 14:28:24 -0700346 }
347
348 // Shortcut for default value. This is an implementation optimization for the process sketched
349 // above. If the default value is true, we can avoid to check whether this is a low-mem device,
350 // as low-mem is never returning false. The compiler will optimize this away if it can.
351 if (kDefaultProvideSwapFile) {
352 return true;
353 }
354
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700355 if (GetBoolProperty("ro.config.low_ram", false)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700356 return true;
357 }
358
359 // Default value must be false here.
360 return kDefaultProvideSwapFile;
361}
362
Richard Uhler76cc0272016-12-08 10:46:35 +0000363static void SetDex2OatScheduling(bool set_to_bg) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700364 if (set_to_bg) {
Wei Wang0efe8d52021-07-07 17:32:38 -0700365 if (!SetTaskProfiles(0, {"Dex2OatBootComplete"})) {
366 LOG(ERROR) << "Failed to set dex2oat task profile";
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800367 exit(DexoptReturnCodes::kSetSchedPolicy);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700368 }
369 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800370 PLOG(ERROR) << "setpriority failed";
371 exit(DexoptReturnCodes::kSetPriority);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700372 }
373 }
374}
375
Calin Juravlee01f8712021-06-03 18:16:54 -0700376static unique_fd create_profile(uid_t uid, const std::string& profile, int32_t flags, mode_t mode) {
377 unique_fd fd(TEMP_FAILURE_RETRY(open(profile.c_str(), flags, mode)));
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800378 if (fd.get() < 0) {
Calin Juravle29591732017-11-20 17:46:19 -0800379 if (errno != EEXIST) {
Calin Juravle114f0812017-03-08 19:05:07 -0800380 PLOG(ERROR) << "Failed to create profile " << profile;
Calin Juravle29591732017-11-20 17:46:19 -0800381 return invalid_unique_fd();
Calin Juravle114f0812017-03-08 19:05:07 -0800382 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700383 }
Calin Juravle114f0812017-03-08 19:05:07 -0800384 // Profiles should belong to the app; make sure of that by giving ownership to
385 // the app uid. If we cannot do that, there's no point in returning the fd
386 // since dex2oat/profman will fail with SElinux denials.
387 if (fchown(fd.get(), uid, uid) < 0) {
Roland Levillain019db5b2019-03-14 14:31:59 +0000388 PLOG(ERROR) << "Could not chown profile " << profile;
Calin Juravle29591732017-11-20 17:46:19 -0800389 return invalid_unique_fd();
Calin Juravle114f0812017-03-08 19:05:07 -0800390 }
Calin Juravle29591732017-11-20 17:46:19 -0800391 return fd;
Calin Juravle114f0812017-03-08 19:05:07 -0800392}
393
Calin Juravlee01f8712021-06-03 18:16:54 -0700394static unique_fd open_profile(uid_t uid, const std::string& profile, int32_t flags, mode_t mode) {
Calin Juravle114f0812017-03-08 19:05:07 -0800395 // Do not follow symlinks when opening a profile:
396 // - primary profiles should not contain symlinks in their paths
397 // - secondary dex paths should have been already resolved and validated
398 flags |= O_NOFOLLOW;
399
Calin Juravle29591732017-11-20 17:46:19 -0800400 // Check if we need to create the profile
401 // Reference profiles and snapshots are created on the fly; so they might not exist beforehand.
402 unique_fd fd;
403 if ((flags & O_CREAT) != 0) {
Calin Juravlee01f8712021-06-03 18:16:54 -0700404 fd = create_profile(uid, profile, flags, mode);
Calin Juravle29591732017-11-20 17:46:19 -0800405 } else {
406 fd.reset(TEMP_FAILURE_RETRY(open(profile.c_str(), flags)));
407 }
408
Calin Juravle114f0812017-03-08 19:05:07 -0800409 if (fd.get() < 0) {
410 if (errno != ENOENT) {
411 // Profiles might be missing for various reasons. For example, in a
412 // multi-user environment, the profile directory for one user can be created
413 // after we start a merge. In this case the current profile for that user
414 // will not be found.
415 // Also, the secondary dex profiles might be deleted by the app at any time,
416 // so we can't we need to prepare if they are missing.
417 PLOG(ERROR) << "Failed to open profile " << profile;
418 }
419 return invalid_unique_fd();
Calin Juravlee01f8712021-06-03 18:16:54 -0700420 } else {
421 // If we just create the file we need to set its mode because on Android
422 // open has a mask that only allows owner access.
423 if ((flags & O_CREAT) != 0) {
424 if (fchmod(fd.get(), mode) != 0) {
425 PLOG(ERROR) << "Could not set mode " << std::hex << mode << std::dec
426 << " on profile" << profile;
427 // Not a terminal failure.
428 }
429 }
Calin Juravle114f0812017-03-08 19:05:07 -0800430 }
431
Jeff Sharkey90aff262016-12-12 14:28:24 -0700432 return fd;
433}
434
Calin Juravle824a64d2018-01-18 20:23:17 -0800435static unique_fd open_current_profile(uid_t uid, userid_t user, const std::string& package_name,
436 const std::string& location, bool is_secondary_dex) {
437 std::string profile = create_current_profile_path(user, package_name, location,
438 is_secondary_dex);
Calin Juravlee01f8712021-06-03 18:16:54 -0700439 return open_profile(uid, profile, O_RDONLY, /*mode=*/ 0);
Calin Juravle114f0812017-03-08 19:05:07 -0800440}
441
Calin Juravle824a64d2018-01-18 20:23:17 -0800442static unique_fd open_reference_profile(uid_t uid, const std::string& package_name,
443 const std::string& location, bool read_write, bool is_secondary_dex) {
444 std::string profile = create_reference_profile_path(package_name, location, is_secondary_dex);
Calin Juravlee01f8712021-06-03 18:16:54 -0700445 return open_profile(
446 uid,
447 profile,
448 read_write ? (O_CREAT | O_RDWR) : O_RDONLY,
449 S_IRUSR | S_IWUSR | S_IRGRP); // so that ART can also read it when apps run.
Calin Juravle29591732017-11-20 17:46:19 -0800450}
451
Victor Hsiehcb35a062020-08-13 16:11:13 -0700452static UniqueFile open_reference_profile_as_unique_file(uid_t uid, const std::string& package_name,
453 const std::string& location, bool read_write, bool is_secondary_dex) {
454 std::string profile_path = create_reference_profile_path(package_name, location,
455 is_secondary_dex);
Calin Juravlee01f8712021-06-03 18:16:54 -0700456 unique_fd ufd = open_profile(
457 uid,
458 profile_path,
459 read_write ? (O_CREAT | O_RDWR) : O_RDONLY,
460 S_IRUSR | S_IWUSR | S_IRGRP); // so that ART can also read it when apps run.
461
Victor Hsiehcb35a062020-08-13 16:11:13 -0700462 return UniqueFile(ufd.release(), profile_path, [](const std::string& path) {
463 clear_profile(path);
464 });
465}
466
Alex Buynytskyy79dcab52021-11-19 11:43:04 -0800467static unique_fd open_snapshot_profile(uid_t uid, const std::string& package_name,
468 const std::string& location) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800469 std::string profile = create_snapshot_profile_path(package_name, location);
Calin Juravlee01f8712021-06-03 18:16:54 -0700470 return open_profile(uid, profile, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
Calin Juravle114f0812017-03-08 19:05:07 -0800471}
472
Calin Juravle824a64d2018-01-18 20:23:17 -0800473static void open_profile_files(uid_t uid, const std::string& package_name,
474 const std::string& location, bool is_secondary_dex,
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800475 /*out*/ std::vector<unique_fd>* profiles_fd, /*out*/ unique_fd* reference_profile_fd) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700476 // Open the reference profile in read-write mode as profman might need to save the merge.
Calin Juravle824a64d2018-01-18 20:23:17 -0800477 *reference_profile_fd = open_reference_profile(uid, package_name, location,
478 /*read_write*/ true, is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700479
Hanna Nizhnikavab35b8722022-01-21 16:59:56 +0000480 // For secondary dex files, we don't really need the user but we use it for validity checks.
Calin Juravle114f0812017-03-08 19:05:07 -0800481 // Note: the user owning the dex file should be the current user.
482 std::vector<userid_t> users;
483 if (is_secondary_dex){
484 users.push_back(multiuser_get_user_id(uid));
485 } else {
486 users = get_known_users(/*volume_uuid*/ nullptr);
487 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700488 for (auto user : users) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800489 unique_fd profile_fd = open_current_profile(uid, user, package_name, location,
490 is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700491 // Add to the lists only if both fds are valid.
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800492 if (profile_fd.get() >= 0) {
493 profiles_fd->push_back(std::move(profile_fd));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700494 }
495 }
496}
497
Jiakai Zhang495142a2022-02-21 19:38:14 +0000498// Cleans up an output file specified by a file descriptor. This function should be called whenever
499// a subprocess that modifies a system-managed file crashes.
500// If the subprocess crashes while it's writing to the file, the file is likely corrupted, so we
501// should remove it.
502// If the subprocess times out and is killed while it's acquiring a flock on the file, there is
503// probably a deadlock, so it's also good to remove the file so that later operations won't
504// encounter the same problem. It's safe to do so because the process that is holding the flock will
505// still have access to the file until the file descriptor is closed.
506// Note that we can't do `clear_reference_profile` here even if the fd points to a reference profile
507// because that also requires a flock and is therefore likely to be stuck in the second case.
508static bool cleanup_output_fd(int fd) {
509 std::string path;
510 bool ret = remove_file_at_fd(fd, &path);
511 if (ret) {
512 LOG(INFO) << "Removed file at path " << path;
513 }
514 return ret;
515}
516
Calin Juravle76561322019-11-14 09:08:52 -0800517static constexpr int PROFMAN_BIN_RETURN_CODE_SUCCESS = 0;
518static constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 1;
Calin Juravle3b758282021-06-08 08:04:52 -0700519static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_NOT_ENOUGH_DELTA = 2;
Calin Juravle76561322019-11-14 09:08:52 -0800520static constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 3;
521static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 4;
522static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 5;
523static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS = 6;
Calin Juravle3b758282021-06-08 08:04:52 -0700524static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_EMPTY_PROFILES = 7;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700525
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800526class RunProfman : public ExecVHelper {
527 public:
Jiakai Zhang495142a2022-02-21 19:38:14 +0000528 template <typename T, typename U>
529 void SetupArgs(const std::vector<T>& profile_fds,
530 const unique_fd& reference_profile_fd,
531 const std::vector<U>& apk_fds,
532 const std::vector<std::string>& dex_locations,
533 bool copy_and_update,
534 bool for_snapshot,
535 bool for_boot_image) {
Calin Juravlef74a7372019-02-28 20:29:41 -0800536
537 // TODO(calin): Assume for now we run in the bg compile job (which is in
538 // most of the invocation). With the current data flow, is not very easy or
539 // clean to discover this in RunProfman (it will require quite a messy refactoring).
540 const char* profman_bin = select_execution_binary(
541 kProfmanPath, kProfmanDebugPath, /*background_job_compile=*/ true);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700542
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800543 if (copy_and_update) {
544 CHECK_EQ(1u, profile_fds.size());
545 CHECK_EQ(1u, apk_fds.size());
Mathieu Chartier31636522018-11-09 23:53:07 +0000546 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800547 if (reference_profile_fd != -1) {
548 AddArg("--reference-profile-file-fd=" + std::to_string(reference_profile_fd.get()));
Mathieu Chartier31636522018-11-09 23:53:07 +0000549 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800550
Jiakai Zhang495142a2022-02-21 19:38:14 +0000551 for (const T& fd : profile_fds) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800552 AddArg("--profile-file-fd=" + std::to_string(fd.get()));
553 }
554
Jiakai Zhang495142a2022-02-21 19:38:14 +0000555 for (const U& fd : apk_fds) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800556 AddArg("--apk-fd=" + std::to_string(fd.get()));
557 }
558
559 for (const std::string& dex_location : dex_locations) {
560 AddArg("--dex-location=" + dex_location);
561 }
562
563 if (copy_and_update) {
564 AddArg("--copy-and-update-profile-key");
565 }
566
Calin Juravle78728f32019-11-08 17:55:46 -0800567 if (for_snapshot) {
568 AddArg("--force-merge");
569 }
570
571 if (for_boot_image) {
572 AddArg("--boot-image-merge");
573 }
574
yawannga27559e2020-11-13 19:17:44 +0000575 // The percent won't exceed 100, otherwise, don't set it and use the
576 // default one set in profman.
yawanng6fe34a52020-11-06 05:13:53 +0000577 uint32_t min_new_classes_percent_change = ::android::base::GetUintProperty<uint32_t>(
yawannga27559e2020-11-13 19:17:44 +0000578 "dalvik.vm.bgdexopt.new-classes-percent",
579 /*default*/std::numeric_limits<uint32_t>::max());
580 if (min_new_classes_percent_change <= 100) {
yawanng6fe34a52020-11-06 05:13:53 +0000581 AddArg("--min-new-classes-percent-change=" +
582 std::to_string(min_new_classes_percent_change));
583 }
584
yawannga27559e2020-11-13 19:17:44 +0000585 // The percent won't exceed 100, otherwise, don't set it and use the
586 // default one set in profman.
yawanng6fe34a52020-11-06 05:13:53 +0000587 uint32_t min_new_methods_percent_change = ::android::base::GetUintProperty<uint32_t>(
yawannga27559e2020-11-13 19:17:44 +0000588 "dalvik.vm.bgdexopt.new-methods-percent",
589 /*default*/std::numeric_limits<uint32_t>::max());
590 if (min_new_methods_percent_change <= 100) {
yawanng6fe34a52020-11-06 05:13:53 +0000591 AddArg("--min-new-methods-percent-change=" +
592 std::to_string(min_new_methods_percent_change));
593 }
594
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800595 // Do not add after dex2oat_flags, they should override others for debugging.
596 PrepareArgs(profman_bin);
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800597 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700598
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800599 void SetupMerge(const std::vector<unique_fd>& profiles_fd,
600 const unique_fd& reference_profile_fd,
601 const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>(),
Calin Juravle78728f32019-11-08 17:55:46 -0800602 const std::vector<std::string>& dex_locations = std::vector<std::string>(),
603 bool for_snapshot = false,
604 bool for_boot_image = false) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800605 SetupArgs(profiles_fd,
Calin Juravleb3a929d2018-12-11 14:40:00 -0800606 reference_profile_fd,
607 apk_fds,
608 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800609 /*copy_and_update=*/ false,
610 for_snapshot,
611 for_boot_image);
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800612 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700613
Jiakai Zhang495142a2022-02-21 19:38:14 +0000614 void SetupCopyAndUpdate(const unique_fd& profile_fd,
615 const unique_fd& reference_profile_fd,
616 const unique_fd& apk_fd,
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800617 const std::string& dex_location) {
Jiakai Zhang495142a2022-02-21 19:38:14 +0000618 SetupArgs(std::vector<borrowed_fd>{profile_fd},
619 reference_profile_fd,
620 std::vector<borrowed_fd>{apk_fd},
621 {dex_location},
Calin Juravle78728f32019-11-08 17:55:46 -0800622 /*copy_and_update=*/true,
623 /*for_snapshot*/false,
624 /*for_boot_image*/false);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800625 }
Calin Juravlef63d4792018-01-30 17:43:34 +0000626
Martin Stjernholmfd000402022-04-11 18:04:59 +0100627 void SetupDump(const std::vector<unique_fd>& profiles_fd, const unique_fd& reference_profile_fd,
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800628 const std::vector<std::string>& dex_locations,
Martin Stjernholmfd000402022-04-11 18:04:59 +0100629 const std::vector<unique_fd>& apk_fds, bool dump_classes_and_methods,
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800630 const unique_fd& output_fd) {
Martin Stjernholmfd000402022-04-11 18:04:59 +0100631 if (dump_classes_and_methods) {
632 AddArg("--dump-classes-and-methods");
633 } else {
634 AddArg("--dump-only");
635 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800636 AddArg(StringPrintf("--dump-output-to-fd=%d", output_fd.get()));
Calin Juravleb3a929d2018-12-11 14:40:00 -0800637 SetupArgs(profiles_fd,
638 reference_profile_fd,
639 apk_fds,
640 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800641 /*copy_and_update=*/false,
642 /*for_snapshot*/false,
643 /*for_boot_image*/false);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800644 }
Calin Juravlef63d4792018-01-30 17:43:34 +0000645
Victor Hsiehc9821f12020-08-07 11:32:29 -0700646 using ExecVHelper::Exec; // To suppress -Wno-overloaded-virtual
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800647 void Exec() {
648 ExecVHelper::Exec(DexoptReturnCodes::kProfmanExec);
649 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800650};
Mathieu Chartier31636522018-11-09 23:53:07 +0000651
Calin Juravle3b758282021-06-08 08:04:52 -0700652static int analyze_profiles(uid_t uid, const std::string& package_name,
Calin Juravle824a64d2018-01-18 20:23:17 -0800653 const std::string& location, bool is_secondary_dex) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800654 std::vector<unique_fd> profiles_fd;
655 unique_fd reference_profile_fd;
Calin Juravle824a64d2018-01-18 20:23:17 -0800656 open_profile_files(uid, package_name, location, is_secondary_dex,
657 &profiles_fd, &reference_profile_fd);
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800658 if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700659 // Skip profile guided compilation because no profiles were found.
660 // Or if the reference profile info couldn't be opened.
Calin Juravle3b758282021-06-08 08:04:52 -0700661 return PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700662 }
663
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800664 RunProfman profman_merge;
Calin Juravlef85ddb92020-05-01 14:05:40 -0700665 const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>();
666 const std::vector<std::string>& dex_locations = std::vector<std::string>();
667 profman_merge.SetupMerge(
668 profiles_fd,
669 reference_profile_fd,
670 apk_fds,
671 dex_locations,
672 /* for_snapshot= */ false,
673 IsBootClassPathProfilingEnable());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700674 pid_t pid = fork();
675 if (pid == 0) {
676 /* child -- drop privileges before continuing */
677 drop_capabilities(uid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800678 profman_merge.Exec();
Jeff Sharkey90aff262016-12-12 14:28:24 -0700679 }
680 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +0000681 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700682 bool need_to_compile = false;
Calin Juravle3b758282021-06-08 08:04:52 -0700683 bool empty_profiles = false;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700684 bool should_clear_current_profiles = false;
685 bool should_clear_reference_profile = false;
686 if (!WIFEXITED(return_code)) {
Calin Juravle114f0812017-03-08 19:05:07 -0800687 LOG(WARNING) << "profman failed for location " << location << ": " << return_code;
Jiakai Zhang495142a2022-02-21 19:38:14 +0000688 cleanup_output_fd(reference_profile_fd.get());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700689 } else {
690 return_code = WEXITSTATUS(return_code);
691 switch (return_code) {
692 case PROFMAN_BIN_RETURN_CODE_COMPILE:
693 need_to_compile = true;
694 should_clear_current_profiles = true;
695 should_clear_reference_profile = false;
696 break;
Calin Juravle3b758282021-06-08 08:04:52 -0700697 case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_NOT_ENOUGH_DELTA:
Jeff Sharkey90aff262016-12-12 14:28:24 -0700698 need_to_compile = false;
699 should_clear_current_profiles = false;
700 should_clear_reference_profile = false;
701 break;
Calin Juravle3b758282021-06-08 08:04:52 -0700702 case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION_EMPTY_PROFILES:
703 need_to_compile = false;
704 empty_profiles = true;
705 should_clear_current_profiles = false;
706 should_clear_reference_profile = false;
707 break;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700708 case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES:
Calin Juravle114f0812017-03-08 19:05:07 -0800709 LOG(WARNING) << "Bad profiles for location " << location;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700710 need_to_compile = false;
711 should_clear_current_profiles = true;
712 should_clear_reference_profile = true;
713 break;
714 case PROFMAN_BIN_RETURN_CODE_ERROR_IO: // fall-through
715 case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING:
716 // Temporary IO problem (e.g. locking). Ignore but log a warning.
Calin Juravle114f0812017-03-08 19:05:07 -0800717 LOG(WARNING) << "IO error while reading profiles for location " << location;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700718 need_to_compile = false;
719 should_clear_current_profiles = false;
720 should_clear_reference_profile = false;
721 break;
Calin Juravle76561322019-11-14 09:08:52 -0800722 case PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS:
723 need_to_compile = false;
724 should_clear_current_profiles = true;
725 should_clear_reference_profile = true;
726 break;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700727 default:
728 // Unknown return code or error. Unlink profiles.
Calin Juravle78728f32019-11-08 17:55:46 -0800729 LOG(WARNING) << "Unexpected error code while processing profiles for location "
Calin Juravle114f0812017-03-08 19:05:07 -0800730 << location << ": " << return_code;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700731 need_to_compile = false;
732 should_clear_current_profiles = true;
733 should_clear_reference_profile = true;
734 break;
735 }
736 }
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800737
Jeff Sharkey90aff262016-12-12 14:28:24 -0700738 if (should_clear_current_profiles) {
Calin Juravle114f0812017-03-08 19:05:07 -0800739 if (is_secondary_dex) {
740 // For secondary dex files, the owning user is the current user.
Calin Juravle824a64d2018-01-18 20:23:17 -0800741 clear_current_profile(package_name, location, multiuser_get_user_id(uid),
742 is_secondary_dex);
Calin Juravle114f0812017-03-08 19:05:07 -0800743 } else {
Calin Juravle824a64d2018-01-18 20:23:17 -0800744 clear_primary_current_profiles(package_name, location);
Calin Juravle114f0812017-03-08 19:05:07 -0800745 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700746 }
747 if (should_clear_reference_profile) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800748 clear_reference_profile(package_name, location, is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700749 }
Calin Juravle3b758282021-06-08 08:04:52 -0700750 int result = 0;
751 if (need_to_compile) {
752 result = PROFILES_ANALYSIS_OPTIMIZE;
753 } else if (empty_profiles) {
754 result = PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES;
755 } else {
756 result = PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA;
757 }
758 return result;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700759}
760
Calin Juravle114f0812017-03-08 19:05:07 -0800761// Decides if profile guided compilation is needed or not based on existing profiles.
Calin Juravle3b758282021-06-08 08:04:52 -0700762// The analysis is done for a single profile name (which corresponds to a single code path).
763//
764// Returns PROFILES_ANALYSIS_OPTIMIZE if there is enough information in the current profiles
765// that makes it worth to recompile the package.
766// If the return value is PROFILES_ANALYSIS_OPTIMIZE all the current profiles would have been
767// merged into the reference profiles accessible with open_reference_profile().
768//
769// Return PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA if the package should not optimize.
770// As a special case returns PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES if all profiles are
771// empty.
772int analyze_primary_profiles(uid_t uid, const std::string& package_name,
Calin Juravle824a64d2018-01-18 20:23:17 -0800773 const std::string& profile_name) {
774 return analyze_profiles(uid, package_name, profile_name, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800775}
776
Calin Juravle408cd4a2018-01-20 23:34:18 -0800777bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name,
Martin Stjernholmfd000402022-04-11 18:04:59 +0100778 const std::string& code_path, bool dump_classes_and_methods) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800779 std::vector<unique_fd> profile_fds;
780 unique_fd reference_profile_fd;
Calin Juravle408cd4a2018-01-20 23:34:18 -0800781 std::string out_file_name = StringPrintf("/data/misc/profman/%s-%s.txt",
782 pkgname.c_str(), profile_name.c_str());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700783
Calin Juravle408cd4a2018-01-20 23:34:18 -0800784 open_profile_files(uid, pkgname, profile_name, /*is_secondary_dex*/false,
Calin Juravle114f0812017-03-08 19:05:07 -0800785 &profile_fds, &reference_profile_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700786
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800787 const bool has_reference_profile = (reference_profile_fd.get() != -1);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700788 const bool has_profiles = !profile_fds.empty();
789
790 if (!has_reference_profile && !has_profiles) {
Calin Juravle76268c52017-03-09 13:19:42 -0800791 LOG(ERROR) << "profman dump: no profiles to dump for " << pkgname;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700792 return false;
793 }
794
Calin Juravle114f0812017-03-08 19:05:07 -0800795 unique_fd output_fd(open(out_file_name.c_str(),
796 O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700797 if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
Calin Juravle408cd4a2018-01-20 23:34:18 -0800798 LOG(ERROR) << "installd cannot chmod file for dump_profile" << out_file_name;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700799 return false;
800 }
Calin Juravle408cd4a2018-01-20 23:34:18 -0800801
Jeff Sharkey90aff262016-12-12 14:28:24 -0700802 std::vector<std::string> dex_locations;
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800803 std::vector<unique_fd> apk_fds;
Calin Juravle408cd4a2018-01-20 23:34:18 -0800804 unique_fd apk_fd(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW));
805 if (apk_fd == -1) {
806 PLOG(ERROR) << "installd cannot open " << code_path.c_str();
807 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700808 }
Victor Hsieh76f19ba2020-08-10 14:37:26 -0700809 dex_locations.push_back(Basename(code_path));
Calin Juravle408cd4a2018-01-20 23:34:18 -0800810 apk_fds.push_back(std::move(apk_fd));
811
Jeff Sharkey90aff262016-12-12 14:28:24 -0700812
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800813 RunProfman profman_dump;
Martin Stjernholmfd000402022-04-11 18:04:59 +0100814 profman_dump.SetupDump(profile_fds, reference_profile_fd, dex_locations, apk_fds,
815 dump_classes_and_methods, output_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700816 pid_t pid = fork();
817 if (pid == 0) {
818 /* child -- drop privileges before continuing */
819 drop_capabilities(uid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800820 profman_dump.Exec();
Jeff Sharkey90aff262016-12-12 14:28:24 -0700821 }
822 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +0000823 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700824 if (!WIFEXITED(return_code)) {
Jiakai Zhang495142a2022-02-21 19:38:14 +0000825 LOG(WARNING) << "profman failed for package " << pkgname << ": " << return_code;
826 cleanup_output_fd(output_fd.get());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700827 return false;
828 }
829 return true;
830}
831
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700832bool copy_system_profile(const std::string& system_profile,
Calin Juravle824a64d2018-01-18 20:23:17 -0800833 uid_t packageUid, const std::string& package_name, const std::string& profile_name) {
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700834 unique_fd in_fd(open(system_profile.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
835 unique_fd out_fd(open_reference_profile(packageUid,
Calin Juravle824a64d2018-01-18 20:23:17 -0800836 package_name,
837 profile_name,
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700838 /*read_write*/ true,
839 /*secondary*/ false));
840 if (in_fd.get() < 0) {
841 PLOG(WARNING) << "Could not open profile " << system_profile;
842 return false;
843 }
844 if (out_fd.get() < 0) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800845 PLOG(WARNING) << "Could not open profile " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700846 return false;
847 }
848
Mathieu Chartier78f71fe2017-06-14 13:02:26 -0700849 // As a security measure we want to write the profile information with the reduced capabilities
850 // of the package user id. So we fork and drop capabilities in the child.
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700851 pid_t pid = fork();
852 if (pid == 0) {
853 /* child -- drop privileges before continuing */
854 drop_capabilities(packageUid);
855
856 if (flock(out_fd.get(), LOCK_EX | LOCK_NB) != 0) {
857 if (errno != EWOULDBLOCK) {
Martijn Coenen6de402a2021-04-26 16:23:40 +0200858 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Error locking profile %s: %d",
859 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700860 }
861 // This implies that the app owning this profile is running
862 // (and has acquired the lock).
863 //
864 // The app never acquires the lock for the reference profiles of primary apks.
865 // Only dex2oat from installd will do that. Since installd is single threaded
866 // we should not see this case. Nevertheless be prepared for it.
Martijn Coenen6de402a2021-04-26 16:23:40 +0200867 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Failed to flock %s: %d",
868 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700869 return false;
870 }
871
872 bool truncated = ftruncate(out_fd.get(), 0) == 0;
873 if (!truncated) {
Martijn Coenen6de402a2021-04-26 16:23:40 +0200874 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Could not truncate %s: %d",
875 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700876 }
877
878 // Copy over data.
879 static constexpr size_t kBufferSize = 4 * 1024;
880 char buffer[kBufferSize];
881 while (true) {
882 ssize_t bytes = read(in_fd.get(), buffer, kBufferSize);
883 if (bytes == 0) {
884 break;
885 }
886 write(out_fd.get(), buffer, bytes);
887 }
888 if (flock(out_fd.get(), LOCK_UN) != 0) {
Martijn Coenen6de402a2021-04-26 16:23:40 +0200889 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Error unlocking profile %s: %d",
890 package_name.c_str(), errno);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700891 }
Mathieu Chartier78f71fe2017-06-14 13:02:26 -0700892 // Use _exit since we don't want to run the global destructors in the child.
893 // b/62597429
894 _exit(0);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700895 }
896 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +0000897 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
898 if (!WIFEXITED(return_code)) {
899 cleanup_output_fd(out_fd.get());
900 return false;
901 }
Mathieu Chartierf966f2a2017-05-10 12:48:37 -0700902 return return_code == 0;
903}
904
Jeff Sharkey90aff262016-12-12 14:28:24 -0700905static std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) {
906 // A standard dalvik-cache entry. Replace ".dex" with `new_ext`.
907 if (EndsWith(oat_path, ".dex")) {
908 std::string new_path = oat_path;
909 new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext);
Elliott Hughes969e4f82017-12-20 12:34:09 -0800910 CHECK(EndsWith(new_path, new_ext));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700911 return new_path;
912 }
913
914 // An odex entry. Not that this may not be an extension, e.g., in the OTA
915 // case (where the base name will have an extension for the B artifact).
916 size_t odex_pos = oat_path.rfind(".odex");
917 if (odex_pos != std::string::npos) {
918 std::string new_path = oat_path;
919 new_path.replace(odex_pos, strlen(".odex"), new_ext);
920 CHECK_NE(new_path.find(new_ext), std::string::npos);
921 return new_path;
922 }
923
924 // Don't know how to handle this.
925 return "";
926}
927
928// Translate the given oat path to an art (app image) path. An empty string
929// denotes an error.
930static std::string create_image_filename(const std::string& oat_path) {
931 return replace_file_extension(oat_path, ".art");
932}
933
934// Translate the given oat path to a vdex path. An empty string denotes an error.
935static std::string create_vdex_filename(const std::string& oat_path) {
936 return replace_file_extension(oat_path, ".vdex");
937}
938
Jeff Sharkey90aff262016-12-12 14:28:24 -0700939static int open_output_file(const char* file_name, bool recreate, int permissions) {
940 int flags = O_RDWR | O_CREAT;
941 if (recreate) {
942 if (unlink(file_name) < 0) {
943 if (errno != ENOENT) {
944 PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name;
945 }
946 }
947 flags |= O_EXCL;
948 }
949 return open(file_name, flags, permissions);
950}
951
Calin Juravle2289c0a2017-02-15 12:44:14 -0800952static bool set_permissions_and_ownership(
953 int fd, bool is_public, int uid, const char* path, bool is_secondary_dex) {
954 // Primary apks are owned by the system. Secondary dex files are owned by the app.
955 int owning_uid = is_secondary_dex ? uid : AID_SYSTEM;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700956 if (fchmod(fd,
957 S_IRUSR|S_IWUSR|S_IRGRP |
958 (is_public ? S_IROTH : 0)) < 0) {
959 ALOGE("installd cannot chmod '%s' during dexopt\n", path);
960 return false;
Calin Juravle2289c0a2017-02-15 12:44:14 -0800961 } else if (fchown(fd, owning_uid, uid) < 0) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700962 ALOGE("installd cannot chown '%s' during dexopt\n", path);
963 return false;
964 }
965 return true;
966}
967
968static bool IsOutputDalvikCache(const char* oat_dir) {
969 // InstallerConnection.java (which invokes installd) transforms Java null arguments
970 // into '!'. Play it safe by handling it both.
971 // TODO: ensure we never get null.
972 // TODO: pass a flag instead of inferring if the output is dalvik cache.
973 return oat_dir == nullptr || oat_dir[0] == '!';
974}
975
Calin Juravled23dee72017-07-06 16:29:11 -0700976// Best-effort check whether we can fit the the path into our buffers.
977// Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
978// without a swap file, if necessary. Reference profiles file also add an extra ".prof"
979// extension to the cache path (5 bytes).
980// TODO(calin): move away from char* buffers and PKG_PATH_MAX.
981static bool validate_dex_path_size(const std::string& dex_path) {
982 if (dex_path.size() >= (PKG_PATH_MAX - 8)) {
983 LOG(ERROR) << "dex_path too long: " << dex_path;
984 return false;
985 }
986 return true;
987}
988
Jeff Sharkey90aff262016-12-12 14:28:24 -0700989static bool create_oat_out_path(const char* apk_path, const char* instruction_set,
Calin Juravle80a21252017-01-17 14:43:25 -0800990 const char* oat_dir, bool is_secondary_dex, /*out*/ char* out_oat_path) {
Calin Juravled23dee72017-07-06 16:29:11 -0700991 if (!validate_dex_path_size(apk_path)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700992 return false;
993 }
994
995 if (!IsOutputDalvikCache(oat_dir)) {
Calin Juravle80a21252017-01-17 14:43:25 -0800996 // Oat dirs for secondary dex files are already validated.
997 if (!is_secondary_dex && validate_apk_path(oat_dir)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700998 ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir);
999 return false;
1000 }
1001 if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) {
1002 return false;
1003 }
1004 } else {
1005 if (!create_cache_path(out_oat_path, apk_path, instruction_set)) {
1006 return false;
1007 }
1008 }
1009 return true;
1010}
1011
Calin Juravle7a570e82017-01-14 16:23:30 -08001012// (re)Creates the app image if needed.
Keun young Parkb7342262021-10-25 08:09:27 -07001013RestorableFile maybe_open_app_image(const std::string& out_oat_path, bool generate_app_image,
1014 bool is_public, int uid, bool is_secondary_dex) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001015 const std::string image_path = create_image_filename(out_oat_path);
1016 if (image_path.empty()) {
1017 // Happens when the out_oat_path has an unknown extension.
Keun young Parkb7342262021-10-25 08:09:27 -07001018 return RestorableFile();
Calin Juravle7a570e82017-01-14 16:23:30 -08001019 }
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +01001020
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -07001021 // Not enabled, exit.
1022 if (!generate_app_image) {
Keun young Parkb7342262021-10-25 08:09:27 -07001023 RestorableFile::RemoveAllFiles(image_path);
1024 return RestorableFile();
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +01001025 }
Mathieu Chartier9b2da082018-10-26 13:23:11 -07001026 std::string app_image_format = GetProperty("dalvik.vm.appimageformat", "");
1027 if (app_image_format.empty()) {
Keun young Parkb7342262021-10-25 08:09:27 -07001028 RestorableFile::RemoveAllFiles(image_path);
1029 return RestorableFile();
Calin Juravle7a570e82017-01-14 16:23:30 -08001030 }
Keun young Parkb7342262021-10-25 08:09:27 -07001031 // If the app is already running and we modify the image file, it can cause crashes
1032 // (b/27493510).
1033 RestorableFile image_file = RestorableFile::CreateWritableFile(image_path,
1034 /*permissions*/ 0600);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001035 if (image_file.fd() < 0) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001036 // Could not create application image file. Go on since we can compile without it.
1037 LOG(ERROR) << "installd could not create '" << image_path
1038 << "' for image file during dexopt";
Keun young Parkb7342262021-10-25 08:09:27 -07001039 // If we have a valid image file path but cannot create tmp file, reset it.
1040 image_file.reset();
Calin Juravle7a570e82017-01-14 16:23:30 -08001041 } else if (!set_permissions_and_ownership(
Victor Hsiehcb35a062020-08-13 16:11:13 -07001042 image_file.fd(), is_public, uid, image_path.c_str(), is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001043 ALOGE("installd cannot set owner '%s' for image during dexopt\n", image_path.c_str());
Victor Hsiehcb35a062020-08-13 16:11:13 -07001044 image_file.reset();
Calin Juravle7a570e82017-01-14 16:23:30 -08001045 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001046
Victor Hsiehcb35a062020-08-13 16:11:13 -07001047 return image_file;
Calin Juravle7a570e82017-01-14 16:23:30 -08001048}
1049
1050// Creates the dexopt swap file if necessary and return its fd.
1051// Returns -1 if there's no need for a swap or in case of errors.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001052unique_fd maybe_open_dexopt_swap_file(const std::string& out_oat_path) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001053 if (!ShouldUseSwapFileForDexopt()) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001054 return invalid_unique_fd();
Calin Juravle7a570e82017-01-14 16:23:30 -08001055 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001056 auto swap_file_name = out_oat_path + ".swap";
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001057 unique_fd swap_fd(open_output_file(
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001058 swap_file_name.c_str(), /*recreate*/true, /*permissions*/0600));
Calin Juravle7a570e82017-01-14 16:23:30 -08001059 if (swap_fd.get() < 0) {
1060 // Could not create swap file. Optimistically go on and hope that we can compile
1061 // without it.
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001062 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name.c_str());
Calin Juravle7a570e82017-01-14 16:23:30 -08001063 } else {
1064 // Immediately unlink. We don't really want to hit flash.
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001065 if (unlink(swap_file_name.c_str()) < 0) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001066 PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name;
1067 }
1068 }
1069 return swap_fd;
1070}
1071
1072// Opens the reference profiles if needed.
1073// 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 -07001074UniqueFile maybe_open_reference_profile(const std::string& pkgname,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001075 const std::string& dex_path, const char* profile_name, bool profile_guided,
Calin Juravle824a64d2018-01-18 20:23:17 -08001076 bool is_public, int uid, bool is_secondary_dex) {
Calin Juravle5bd1c722018-02-01 17:23:54 +00001077 // If we are not profile guided compilation, or we are compiling system server
1078 // do not bother to open the profiles; we won't be using them.
1079 if (!profile_guided || (pkgname[0] == '*')) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001080 return UniqueFile();
Calin Juravle5bd1c722018-02-01 17:23:54 +00001081 }
1082
1083 // If this is a secondary dex path which is public do not open the profile.
1084 // We cannot compile public secondary dex paths with profiles. That's because
1085 // it will expose how the dex files are used by their owner.
1086 //
1087 // Note that the PackageManager is responsible to set the is_public flag for
1088 // primary apks and we do not check it here. In some cases, e.g. when
1089 // compiling with a public profile from the .dm file the PackageManager will
1090 // set is_public toghether with the profile guided compilation.
1091 if (is_secondary_dex && is_public) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001092 return UniqueFile();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001093 }
Calin Juravle114f0812017-03-08 19:05:07 -08001094
1095 // Open reference profile in read only mode as dex2oat does not get write permissions.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001096 std::string location;
1097 if (is_secondary_dex) {
1098 location = dex_path;
1099 } else {
1100 if (profile_name == nullptr) {
1101 // This path is taken for system server re-compilation lunched from ZygoteInit.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001102 return UniqueFile();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001103 } else {
1104 location = profile_name;
1105 }
1106 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001107 return open_reference_profile_as_unique_file(uid, pkgname, location, /*read_write*/false,
1108 is_secondary_dex);
Calin Juravle7a570e82017-01-14 16:23:30 -08001109}
Jeff Sharkey90aff262016-12-12 14:28:24 -07001110
Victor Hsiehcb35a062020-08-13 16:11:13 -07001111// Opens the vdex files and assigns the input fd to in_vdex_wrapper and the output fd to
1112// out_vdex_wrapper. Returns true for success or false in case of errors.
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001113bool 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 -07001114 const char* instruction_set, bool is_public, int uid,
1115 bool is_secondary_dex, bool profile_guided,
1116 UniqueFile* in_vdex_wrapper, RestorableFile* out_vdex_wrapper) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001117 CHECK(in_vdex_wrapper != nullptr);
1118 CHECK(out_vdex_wrapper != nullptr);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001119 // Open the existing VDEX. We do this before creating the new output VDEX, which will
1120 // unlink the old one.
Richard Uhler76cc0272016-12-08 10:46:35 +00001121 char in_odex_path[PKG_PATH_MAX];
1122 int dexopt_action = abs(dexopt_needed);
1123 bool is_odex_location = dexopt_needed < 0;
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001124
1125 // Infer the name of the output VDEX.
1126 const std::string out_vdex_path_str = create_vdex_filename(out_oat_path);
1127 if (out_vdex_path_str.empty()) {
1128 return false;
1129 }
1130
Keun young Parkb7342262021-10-25 08:09:27 -07001131 // Create work file first. All files will be deleted when it fails.
1132 *out_vdex_wrapper = RestorableFile::CreateWritableFile(out_vdex_path_str,
1133 /*permissions*/ 0644);
1134 if (out_vdex_wrapper->fd() < 0) {
1135 ALOGE("installd cannot open vdex '%s' during dexopt\n", out_vdex_path_str.c_str());
1136 return false;
1137 }
1138
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001139 bool update_vdex_in_place = false;
Nicolas Geoffray3c95f2d2017-04-24 13:34:59 +00001140 if (dexopt_action != DEX2OAT_FROM_SCRATCH) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001141 // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd.
1142 const char* path = nullptr;
1143 if (is_odex_location) {
1144 if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1145 path = in_odex_path;
1146 } else {
1147 ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path);
Calin Juravle7a570e82017-01-14 16:23:30 -08001148 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001149 }
1150 } else {
1151 path = out_oat_path;
1152 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001153 std::string in_vdex_path_str = create_vdex_filename(path);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001154 if (in_vdex_path_str.empty()) {
1155 ALOGE("installd cannot compute input vdex location for '%s'\n", path);
Calin Juravle7a570e82017-01-14 16:23:30 -08001156 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001157 }
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001158 // We can update in place when all these conditions are met:
1159 // 1) The vdex location to write to is the same as the vdex location to read (vdex files
1160 // on /system typically cannot be updated in place).
1161 // 2) We dex2oat due to boot image change, because we then know the existing vdex file
1162 // cannot be currently used by a running process.
1163 // 3) We are not doing a profile guided compilation, because dexlayout requires two
1164 // different vdex files to operate.
1165 update_vdex_in_place =
1166 (in_vdex_path_str == out_vdex_path_str) &&
1167 (dexopt_action == DEX2OAT_FOR_BOOT_IMAGE) &&
1168 !profile_guided;
1169 if (update_vdex_in_place) {
Keun young Parkb7342262021-10-25 08:09:27 -07001170 // dex2oat marks it invalid anyway. So delete it and set work file fd.
1171 unlink(in_vdex_path_str.c_str());
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001172 // Open the file read-write to be able to update it.
Keun young Parkb7342262021-10-25 08:09:27 -07001173 in_vdex_wrapper->reset(out_vdex_wrapper->fd(), in_vdex_path_str);
1174 // Disable auto close for the in wrapper fd (it will be done when destructing the out
1175 // wrapper).
1176 in_vdex_wrapper->DisableAutoClose();
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001177 } else {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001178 in_vdex_wrapper->reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0),
1179 in_vdex_path_str);
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001180 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001181 }
1182
Victor Hsiehcb35a062020-08-13 16:11:13 -07001183 if (!set_permissions_and_ownership(out_vdex_wrapper->fd(), is_public, uid,
Calin Juravle2289c0a2017-02-15 12:44:14 -08001184 out_vdex_path_str.c_str(), is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001185 ALOGE("installd cannot set owner '%s' for vdex during dexopt\n", out_vdex_path_str.c_str());
1186 return false;
1187 }
1188
1189 // If we got here we successfully opened the vdex files.
1190 return true;
1191}
1192
1193// Opens the output oat file for the given apk.
Keun young Parkb7342262021-10-25 08:09:27 -07001194RestorableFile open_oat_out_file(const char* apk_path, const char* oat_dir, bool is_public, int uid,
1195 const char* instruction_set, bool is_secondary_dex) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001196 char out_oat_path[PKG_PATH_MAX];
Calin Juravle80a21252017-01-17 14:43:25 -08001197 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 -07001198 return RestorableFile();
Calin Juravle7a570e82017-01-14 16:23:30 -08001199 }
Keun young Parkb7342262021-10-25 08:09:27 -07001200 RestorableFile oat = RestorableFile::CreateWritableFile(out_oat_path, /*permissions*/ 0644);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001201 if (oat.fd() < 0) {
Calin Juravle80a21252017-01-17 14:43:25 -08001202 PLOG(ERROR) << "installd cannot open output during dexopt" << out_oat_path;
Calin Juravle2289c0a2017-02-15 12:44:14 -08001203 } else if (!set_permissions_and_ownership(
Victor Hsiehcb35a062020-08-13 16:11:13 -07001204 oat.fd(), is_public, uid, out_oat_path, is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001205 ALOGE("installd cannot set owner '%s' for output during dexopt\n", out_oat_path);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001206 oat.reset();
Calin Juravle7a570e82017-01-14 16:23:30 -08001207 }
Victor Hsiehcb35a062020-08-13 16:11:13 -07001208 return oat;
Calin Juravle7a570e82017-01-14 16:23:30 -08001209}
1210
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001211// Creates RDONLY fds for oat and vdex files, if exist.
1212// Returns false if it fails to create oat out path for the given apk path.
1213// Note that the method returns true even if the files could not be opened.
1214bool maybe_open_oat_and_vdex_file(const std::string& apk_path,
1215 const std::string& oat_dir,
1216 const std::string& instruction_set,
1217 bool is_secondary_dex,
1218 unique_fd* oat_file_fd,
1219 unique_fd* vdex_file_fd) {
1220 char oat_path[PKG_PATH_MAX];
1221 if (!create_oat_out_path(apk_path.c_str(),
1222 instruction_set.c_str(),
1223 oat_dir.c_str(),
1224 is_secondary_dex,
1225 oat_path)) {
Calin Juravle7d765462017-09-04 15:57:10 -07001226 LOG(ERROR) << "Could not create oat out path for "
1227 << apk_path << " with oat dir " << oat_dir;
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001228 return false;
1229 }
1230 oat_file_fd->reset(open(oat_path, O_RDONLY));
1231 if (oat_file_fd->get() < 0) {
1232 PLOG(INFO) << "installd cannot open oat file during dexopt" << oat_path;
1233 }
1234
1235 std::string vdex_filename = create_vdex_filename(oat_path);
1236 vdex_file_fd->reset(open(vdex_filename.c_str(), O_RDONLY));
1237 if (vdex_file_fd->get() < 0) {
1238 PLOG(INFO) << "installd cannot open vdex file during dexopt" << vdex_filename;
1239 }
1240
1241 return true;
1242}
1243
Calin Juravle80a21252017-01-17 14:43:25 -08001244// Runs (execv) dexoptanalyzer on the given arguments.
Calin Juravle114f0812017-03-08 19:05:07 -08001245// The analyzer will check if the dex_file needs to be (re)compiled to match the compiler_filter.
1246// If this is for a profile guided compilation, profile_was_updated will tell whether or not
1247// the profile has changed.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001248class RunDexoptAnalyzer : public ExecVHelper {
1249 public:
1250 RunDexoptAnalyzer(const std::string& dex_file,
David Brazdil4f6027a2019-03-19 11:44:21 +00001251 int vdex_fd,
1252 int oat_fd,
1253 int zip_fd,
1254 const std::string& instruction_set,
1255 const std::string& compiler_filter,
Calin Juravle3b758282021-06-08 08:04:52 -07001256 int profile_analysis_result,
David Brazdil4f6027a2019-03-19 11:44:21 +00001257 bool downgrade,
1258 const char* class_loader_context,
1259 const std::string& class_loader_context_fds) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001260 CHECK_GE(zip_fd, 0);
Calin Juravlef74a7372019-02-28 20:29:41 -08001261
1262 // We always run the analyzer in the background job.
1263 const char* dexoptanalyzer_bin = select_execution_binary(
1264 kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true);
Calin Juravle80a21252017-01-17 14:43:25 -08001265
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001266 std::string dex_file_arg = "--dex-file=" + dex_file;
1267 std::string oat_fd_arg = "--oat-fd=" + std::to_string(oat_fd);
1268 std::string vdex_fd_arg = "--vdex-fd=" + std::to_string(vdex_fd);
1269 std::string zip_fd_arg = "--zip-fd=" + std::to_string(zip_fd);
1270 std::string isa_arg = "--isa=" + instruction_set;
1271 std::string compiler_filter_arg = "--compiler-filter=" + compiler_filter;
Calin Juravle3b758282021-06-08 08:04:52 -07001272 std::string profile_analysis_arg = "--profile-analysis-result="
1273 + std::to_string(profile_analysis_result);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001274 const char* downgrade_flag = "--downgrade";
1275 std::string class_loader_context_arg = "--class-loader-context=";
1276 if (class_loader_context != nullptr) {
1277 class_loader_context_arg += class_loader_context;
1278 }
David Brazdil4f6027a2019-03-19 11:44:21 +00001279 std::string class_loader_context_fds_arg = "--class-loader-context-fds=";
1280 if (!class_loader_context_fds.empty()) {
1281 class_loader_context_fds_arg += class_loader_context_fds;
1282 }
Mathieu Chartier31636522018-11-09 23:53:07 +00001283
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001284 // program name, dex file, isa, filter
1285 AddArg(dex_file_arg);
1286 AddArg(isa_arg);
1287 AddArg(compiler_filter_arg);
1288 if (oat_fd >= 0) {
1289 AddArg(oat_fd_arg);
1290 }
1291 if (vdex_fd >= 0) {
1292 AddArg(vdex_fd_arg);
1293 }
Greg Kaiser8042c372019-03-26 06:23:19 -07001294 AddArg(zip_fd_arg);
Calin Juravle3b758282021-06-08 08:04:52 -07001295 AddArg(profile_analysis_arg);
1296
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001297 if (downgrade) {
1298 AddArg(downgrade_flag);
1299 }
1300 if (class_loader_context != nullptr) {
Greg Kaiser8042c372019-03-26 06:23:19 -07001301 AddArg(class_loader_context_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +00001302 if (!class_loader_context_fds.empty()) {
Greg Kaiser8042c372019-03-26 06:23:19 -07001303 AddArg(class_loader_context_fds_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +00001304 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001305 }
Mathieu Chartier31636522018-11-09 23:53:07 +00001306
Orion Hodson5f6b3ad2021-07-01 12:12:40 +01001307 // On-device signing related. odsign sets the system property odsign.verification.success if
1308 // AOT artifacts have the expected signatures.
1309 const bool trust_art_apex_data_files =
1310 ::android::base::GetBoolProperty("odsign.verification.success", false);
1311 if (!trust_art_apex_data_files) {
1312 AddRuntimeArg("-Xdeny-art-apex-data-files");
1313 }
1314
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001315 PrepareArgs(dexoptanalyzer_bin);
1316 }
David Brazdil4f6027a2019-03-19 11:44:21 +00001317
1318 // Dexoptanalyzer mode which flattens the given class loader context and
1319 // prints a list of its dex files in that flattened order.
1320 RunDexoptAnalyzer(const char* class_loader_context) {
1321 CHECK(class_loader_context != nullptr);
1322
1323 // We always run the analyzer in the background job.
1324 const char* dexoptanalyzer_bin = select_execution_binary(
1325 kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true);
1326
1327 AddArg("--flatten-class-loader-context");
1328 AddArg(std::string("--class-loader-context=") + class_loader_context);
1329 PrepareArgs(dexoptanalyzer_bin);
1330 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001331};
Calin Juravle80a21252017-01-17 14:43:25 -08001332
1333// Prepares the oat dir for the secondary dex files.
Calin Juravle114f0812017-03-08 19:05:07 -08001334static bool prepare_secondary_dex_oat_dir(const std::string& dex_path, int uid,
Calin Juravle7d765462017-09-04 15:57:10 -07001335 const char* instruction_set) {
Calin Juravle114f0812017-03-08 19:05:07 -08001336 unsigned long dirIndex = dex_path.rfind('/');
Calin Juravle80a21252017-01-17 14:43:25 -08001337 if (dirIndex == std::string::npos) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001338 LOG(ERROR ) << "Unexpected dir structure for secondary dex " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001339 return false;
1340 }
Calin Juravle114f0812017-03-08 19:05:07 -08001341 std::string dex_dir = dex_path.substr(0, dirIndex);
Calin Juravle80a21252017-01-17 14:43:25 -08001342
Calin Juravle80a21252017-01-17 14:43:25 -08001343 // Create oat file output directory.
Calin Juravleebc8a792017-04-04 20:21:05 -07001344 mode_t oat_dir_mode = S_IRWXU | S_IRWXG | S_IXOTH;
1345 if (prepare_app_cache_dir(dex_dir, "oat", oat_dir_mode, uid, uid) != 0) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001346 LOG(ERROR) << "Could not prepare oat dir for secondary dex: " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001347 return false;
1348 }
1349
1350 char oat_dir[PKG_PATH_MAX];
Calin Juravle114f0812017-03-08 19:05:07 -08001351 snprintf(oat_dir, PKG_PATH_MAX, "%s/oat", dex_dir.c_str());
Calin Juravle80a21252017-01-17 14:43:25 -08001352
Calin Juravle7d765462017-09-04 15:57:10 -07001353 if (prepare_app_cache_dir(oat_dir, instruction_set, oat_dir_mode, uid, uid) != 0) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001354 LOG(ERROR) << "Could not prepare oat/isa dir for secondary dex: " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001355 return false;
1356 }
1357
1358 return true;
1359}
1360
Calin Juravle7d765462017-09-04 15:57:10 -07001361// Return codes for identifying the reason why dexoptanalyzer was not invoked when processing
1362// secondary dex files. This return codes are returned by the child process created for
1363// analyzing secondary dex files in process_secondary_dex_dexopt.
Calin Juravle80a21252017-01-17 14:43:25 -08001364
Andreas Gampe194fe422018-02-28 20:16:19 -08001365enum DexoptAnalyzerSkipCodes {
1366 // The dexoptanalyzer was not invoked because of validation or IO errors.
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001367 // Specific errors are encoded in the name.
1368 kSecondaryDexDexoptAnalyzerSkippedValidatePath = 200,
1369 kSecondaryDexDexoptAnalyzerSkippedOpenZip = 201,
1370 kSecondaryDexDexoptAnalyzerSkippedPrepareDir = 202,
1371 kSecondaryDexDexoptAnalyzerSkippedOpenOutput = 203,
1372 kSecondaryDexDexoptAnalyzerSkippedFailExec = 204,
Andreas Gampe194fe422018-02-28 20:16:19 -08001373 // The dexoptanalyzer was not invoked because the dex file does not exist anymore.
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001374 kSecondaryDexDexoptAnalyzerSkippedNoFile = 205,
Andreas Gampe194fe422018-02-28 20:16:19 -08001375};
Calin Juravle7d765462017-09-04 15:57:10 -07001376
1377// Verifies the result of analyzing secondary dex files from process_secondary_dex_dexopt.
Calin Juravle80a21252017-01-17 14:43:25 -08001378// If the result is valid returns true and sets dexopt_needed_out to a valid value.
1379// Returns false for errors or unexpected result values.
Calin Juravle7d765462017-09-04 15:57:10 -07001380// The result is expected to be either one of SECONDARY_DEX_* codes or a valid exit code
1381// of dexoptanalyzer.
1382static bool process_secondary_dexoptanalyzer_result(const std::string& dex_path, int result,
Andreas Gampe194fe422018-02-28 20:16:19 -08001383 int* dexopt_needed_out, std::string* error_msg) {
Calin Juravle80a21252017-01-17 14:43:25 -08001384 // The result values are defined in dexoptanalyzer.
1385 switch (result) {
Calin Juravle7d765462017-09-04 15:57:10 -07001386 case 0: // dexoptanalyzer: no_dexopt_needed
Calin Juravle80a21252017-01-17 14:43:25 -08001387 *dexopt_needed_out = NO_DEXOPT_NEEDED; return true;
Calin Juravle7d765462017-09-04 15:57:10 -07001388 case 1: // dexoptanalyzer: dex2oat_from_scratch
Calin Juravle80a21252017-01-17 14:43:25 -08001389 *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; return true;
Vladimir Marko1752a112018-09-03 18:15:16 +01001390 case 4: // dexoptanalyzer: dex2oat_for_bootimage_odex
Calin Juravle80a21252017-01-17 14:43:25 -08001391 *dexopt_needed_out = -DEX2OAT_FOR_BOOT_IMAGE; return true;
Vladimir Marko1752a112018-09-03 18:15:16 +01001392 case 5: // dexoptanalyzer: dex2oat_for_filter_odex
Calin Juravle80a21252017-01-17 14:43:25 -08001393 *dexopt_needed_out = -DEX2OAT_FOR_FILTER; return true;
Calin Juravle7d765462017-09-04 15:57:10 -07001394 case 2: // dexoptanalyzer: dex2oat_for_bootimage_oat
1395 case 3: // dexoptanalyzer: dex2oat_for_filter_oat
Andreas Gampe194fe422018-02-28 20:16:19 -08001396 *error_msg = StringPrintf("Dexoptanalyzer return the status of an oat file."
1397 " Expected odex file status for secondary dex %s"
1398 " : dexoptanalyzer result=%d",
1399 dex_path.c_str(),
1400 result);
Calin Juravle80a21252017-01-17 14:43:25 -08001401 return false;
Andreas Gampe194fe422018-02-28 20:16:19 -08001402 }
1403
1404 // Use a second switch for enum switch-case analysis.
1405 switch (static_cast<DexoptAnalyzerSkipCodes>(result)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001406 case kSecondaryDexDexoptAnalyzerSkippedNoFile:
Calin Juravle7d765462017-09-04 15:57:10 -07001407 // If the file does not exist there's no need for dexopt.
1408 *dexopt_needed_out = NO_DEXOPT_NEEDED;
1409 return true;
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001410
1411 case kSecondaryDexDexoptAnalyzerSkippedValidatePath:
1412 *error_msg = "Dexoptanalyzer path validation failed";
1413 return false;
1414 case kSecondaryDexDexoptAnalyzerSkippedOpenZip:
1415 *error_msg = "Dexoptanalyzer open zip failed";
1416 return false;
1417 case kSecondaryDexDexoptAnalyzerSkippedPrepareDir:
1418 *error_msg = "Dexoptanalyzer dir preparation failed";
1419 return false;
1420 case kSecondaryDexDexoptAnalyzerSkippedOpenOutput:
1421 *error_msg = "Dexoptanalyzer open output failed";
1422 return false;
1423 case kSecondaryDexDexoptAnalyzerSkippedFailExec:
1424 *error_msg = "Dexoptanalyzer failed to execute";
Calin Juravle80a21252017-01-17 14:43:25 -08001425 return false;
1426 }
Andreas Gampe194fe422018-02-28 20:16:19 -08001427
1428 *error_msg = StringPrintf("Unexpected result from analyzing secondary dex %s result=%d",
1429 dex_path.c_str(),
1430 result);
1431 return false;
Calin Juravle80a21252017-01-17 14:43:25 -08001432}
1433
Calin Juravle7d765462017-09-04 15:57:10 -07001434enum SecondaryDexAccess {
1435 kSecondaryDexAccessReadOk = 0,
1436 kSecondaryDexAccessDoesNotExist = 1,
1437 kSecondaryDexAccessPermissionError = 2,
1438 kSecondaryDexAccessIOError = 3
1439};
1440
1441static SecondaryDexAccess check_secondary_dex_access(const std::string& dex_path) {
1442 // Check if the path exists and can be read. If not, there's nothing to do.
1443 if (access(dex_path.c_str(), R_OK) == 0) {
1444 return kSecondaryDexAccessReadOk;
1445 } else {
1446 if (errno == ENOENT) {
Martijn Coenenc417edf2021-07-12 11:47:39 +02001447 async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG,
1448 "Secondary dex does not exist: %s", dex_path.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07001449 return kSecondaryDexAccessDoesNotExist;
1450 } else {
Martijn Coenenc417edf2021-07-12 11:47:39 +02001451 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
1452 "Could not access secondary dex: %s (%d)", dex_path.c_str(), errno);
Calin Juravle7d765462017-09-04 15:57:10 -07001453 return errno == EACCES
1454 ? kSecondaryDexAccessPermissionError
1455 : kSecondaryDexAccessIOError;
1456 }
1457 }
1458}
1459
1460static bool is_file_public(const std::string& filename) {
1461 struct stat file_stat;
1462 if (stat(filename.c_str(), &file_stat) == 0) {
1463 return (file_stat.st_mode & S_IROTH) != 0;
1464 }
1465 return false;
1466}
1467
1468// Create the oat file structure for the secondary dex 'dex_path' and assign
1469// the individual path component to the 'out_' parameters.
1470static bool create_secondary_dex_oat_layout(const std::string& dex_path, const std::string& isa,
Andreas Gampe194fe422018-02-28 20:16:19 -08001471 char* out_oat_dir, char* out_oat_isa_dir, char* out_oat_path, std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001472 size_t dirIndex = dex_path.rfind('/');
1473 if (dirIndex == std::string::npos) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001474 *error_msg = std::string("Unexpected dir structure for dex file ").append(dex_path);
Calin Juravle7d765462017-09-04 15:57:10 -07001475 return false;
1476 }
1477 // TODO(calin): we have similar computations in at lest 3 other places
1478 // (InstalldNativeService, otapropt and dexopt). Unify them and get rid of snprintf by
1479 // using string append.
1480 std::string apk_dir = dex_path.substr(0, dirIndex);
1481 snprintf(out_oat_dir, PKG_PATH_MAX, "%s/oat", apk_dir.c_str());
1482 snprintf(out_oat_isa_dir, PKG_PATH_MAX, "%s/%s", out_oat_dir, isa.c_str());
1483
1484 if (!create_oat_out_path(dex_path.c_str(), isa.c_str(), out_oat_dir,
1485 /*is_secondary_dex*/true, out_oat_path)) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001486 *error_msg = std::string("Could not create oat path for secondary dex ").append(dex_path);
Calin Juravle7d765462017-09-04 15:57:10 -07001487 return false;
1488 }
1489 return true;
1490}
1491
1492// Validate that the dexopt_flags contain a valid storage flag and convert that to an installd
1493// recognized storage flags (FLAG_STORAGE_CE or FLAG_STORAGE_DE).
Andreas Gampe194fe422018-02-28 20:16:19 -08001494static bool validate_dexopt_storage_flags(int dexopt_flags,
1495 int* out_storage_flag,
1496 std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001497 if ((dexopt_flags & DEXOPT_STORAGE_CE) != 0) {
1498 *out_storage_flag = FLAG_STORAGE_CE;
1499 if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001500 *error_msg = "Ambiguous secondary dex storage flag. Both, CE and DE, flags are set";
Calin Juravle7d765462017-09-04 15:57:10 -07001501 return false;
1502 }
1503 } else if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) {
1504 *out_storage_flag = FLAG_STORAGE_DE;
1505 } else {
Andreas Gampe194fe422018-02-28 20:16:19 -08001506 *error_msg = "Secondary dex storage flag must be set";
Calin Juravle7d765462017-09-04 15:57:10 -07001507 return false;
1508 }
1509 return true;
1510}
1511
David Brazdil4f6027a2019-03-19 11:44:21 +00001512static bool get_class_loader_context_dex_paths(const char* class_loader_context, int uid,
1513 /* out */ std::vector<std::string>* context_dex_paths) {
1514 if (class_loader_context == nullptr) {
1515 return true;
1516 }
1517
1518 LOG(DEBUG) << "Getting dex paths for context " << class_loader_context;
1519
1520 // Pipe to get the hash result back from our child process.
1521 unique_fd pipe_read, pipe_write;
1522 if (!Pipe(&pipe_read, &pipe_write)) {
1523 PLOG(ERROR) << "Failed to create pipe";
1524 return false;
1525 }
1526
1527 pid_t pid = fork();
1528 if (pid == 0) {
1529 // child -- drop privileges before continuing.
1530 drop_capabilities(uid);
1531
1532 // Route stdout to `pipe_write`
1533 while ((dup2(pipe_write, STDOUT_FILENO) == -1) && (errno == EINTR)) {}
1534 pipe_write.reset();
1535 pipe_read.reset();
1536
1537 RunDexoptAnalyzer run_dexopt_analyzer(class_loader_context);
1538 run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec);
1539 }
1540
1541 /* parent */
1542 pipe_write.reset();
1543
1544 std::string str_dex_paths;
1545 if (!ReadFdToString(pipe_read, &str_dex_paths)) {
1546 PLOG(ERROR) << "Failed to read from pipe";
1547 return false;
1548 }
1549 pipe_read.reset();
1550
Jiakai Zhang495142a2022-02-21 19:38:14 +00001551 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
David Brazdil4f6027a2019-03-19 11:44:21 +00001552 if (!WIFEXITED(return_code)) {
1553 PLOG(ERROR) << "Error waiting for child dexoptanalyzer process";
1554 return false;
1555 }
1556
1557 constexpr int kFlattenClassLoaderContextSuccess = 50;
1558 return_code = WEXITSTATUS(return_code);
1559 if (return_code != kFlattenClassLoaderContextSuccess) {
1560 LOG(ERROR) << "Dexoptanalyzer could not flatten class loader context, code=" << return_code;
1561 return false;
1562 }
1563
1564 if (!str_dex_paths.empty()) {
1565 *context_dex_paths = android::base::Split(str_dex_paths, ":");
1566 }
1567 return true;
1568}
1569
1570static int open_dex_paths(const std::vector<std::string>& dex_paths,
1571 /* out */ std::vector<unique_fd>* zip_fds, /* out */ std::string* error_msg) {
1572 for (const std::string& dex_path : dex_paths) {
1573 zip_fds->emplace_back(open(dex_path.c_str(), O_RDONLY));
1574 if (zip_fds->back().get() < 0) {
1575 *error_msg = StringPrintf(
1576 "installd cannot open '%s' for input during dexopt", dex_path.c_str());
1577 if (errno == ENOENT) {
1578 return kSecondaryDexDexoptAnalyzerSkippedNoFile;
1579 } else {
1580 return kSecondaryDexDexoptAnalyzerSkippedOpenZip;
1581 }
1582 }
1583 }
1584 return 0;
1585}
1586
1587static std::string join_fds(const std::vector<unique_fd>& fds) {
1588 std::stringstream ss;
1589 bool is_first = true;
1590 for (const unique_fd& fd : fds) {
1591 if (is_first) {
1592 is_first = false;
1593 } else {
1594 ss << ":";
1595 }
1596 ss << fd.get();
1597 }
1598 return ss.str();
1599}
1600
Keun young Park65578d62021-06-28 17:52:05 -07001601void control_dexopt_blocking(bool block) {
1602 dexopt_status_->control_dexopt_blocking(block);
1603}
1604
1605bool is_dexopt_blocked() {
1606 return dexopt_status_->is_dexopt_blocked();
1607}
1608
1609enum SecondaryDexOptProcessResult {
1610 kSecondaryDexOptProcessOk = 0,
1611 kSecondaryDexOptProcessCancelled = 1,
1612 kSecondaryDexOptProcessError = 2
1613};
1614
Calin Juravlec9eab382017-01-25 01:17:17 -08001615// 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 -07001616// be compiled.
1617// Returns: kSecondaryDexOptProcessError for errors (logged).
1618// kSecondaryDexOptProcessOk if the secondary dex path was process successfully.
1619// kSecondaryDexOptProcessCancelled if the processing was cancelled.
1620//
1621// When returning kSecondaryDexOptProcessOk, the output parameters will be:
Calin Juravleebc8a792017-04-04 20:21:05 -07001622// - is_public_out: whether or not the oat file should not be made public
1623// - dexopt_needed_out: valid OatFileAsssitant::DexOptNeeded
1624// - oat_dir_out: the oat dir path where the oat file should be stored
Keun young Park65578d62021-06-28 17:52:05 -07001625static SecondaryDexOptProcessResult process_secondary_dex_dexopt(const std::string& dex_path,
1626 const char* pkgname, int dexopt_flags, const char* volume_uuid, int uid,
1627 const char* instruction_set, const char* compiler_filter, bool* is_public_out,
1628 int* dexopt_needed_out, std::string* oat_dir_out, bool downgrade,
1629 const char* class_loader_context, const std::vector<std::string>& context_dex_paths,
1630 /* out */ std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001631 LOG(DEBUG) << "Processing secondary dex path " << dex_path;
Keun young Park65578d62021-06-28 17:52:05 -07001632
1633 if (dexopt_status_->is_dexopt_blocked()) {
1634 return kSecondaryDexOptProcessCancelled;
1635 }
1636
Calin Juravle80a21252017-01-17 14:43:25 -08001637 int storage_flag;
Andreas Gampe194fe422018-02-28 20:16:19 -08001638 if (!validate_dexopt_storage_flags(dexopt_flags, &storage_flag, error_msg)) {
1639 LOG(ERROR) << *error_msg;
Keun young Park65578d62021-06-28 17:52:05 -07001640 return kSecondaryDexOptProcessError;
Calin Juravle80a21252017-01-17 14:43:25 -08001641 }
Calin Juravle7d765462017-09-04 15:57:10 -07001642 // Compute the oat dir as it's not easy to extract it from the child computation.
1643 char oat_path[PKG_PATH_MAX];
1644 char oat_dir[PKG_PATH_MAX];
1645 char oat_isa_dir[PKG_PATH_MAX];
1646 if (!create_secondary_dex_oat_layout(
Andreas Gampe194fe422018-02-28 20:16:19 -08001647 dex_path, instruction_set, oat_dir, oat_isa_dir, oat_path, error_msg)) {
1648 LOG(ERROR) << "Could not create secondary odex layout: " << *error_msg;
Keun young Park65578d62021-06-28 17:52:05 -07001649 return kSecondaryDexOptProcessError;
Calin Juravled23dee72017-07-06 16:29:11 -07001650 }
Calin Juravle7d765462017-09-04 15:57:10 -07001651 oat_dir_out->assign(oat_dir);
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001652
Keun young Park65578d62021-06-28 17:52:05 -07001653 bool cancelled = false;
1654 pid_t pid = dexopt_status_->check_cancellation_and_fork(&cancelled);
1655 if (cancelled) {
1656 return kSecondaryDexOptProcessCancelled;
1657 }
Calin Juravle80a21252017-01-17 14:43:25 -08001658 if (pid == 0) {
1659 // child -- drop privileges before continuing.
1660 drop_capabilities(uid);
Calin Juravle7d765462017-09-04 15:57:10 -07001661
1662 // Validate the path structure.
1663 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid, uid, storage_flag)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02001664 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
1665 "Could not validate secondary dex path %s", dex_path.c_str());
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001666 _exit(kSecondaryDexDexoptAnalyzerSkippedValidatePath);
Calin Juravle7d765462017-09-04 15:57:10 -07001667 }
1668
1669 // Open the dex file.
1670 unique_fd zip_fd;
1671 zip_fd.reset(open(dex_path.c_str(), O_RDONLY));
1672 if (zip_fd.get() < 0) {
1673 if (errno == ENOENT) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001674 _exit(kSecondaryDexDexoptAnalyzerSkippedNoFile);
Calin Juravle7d765462017-09-04 15:57:10 -07001675 } else {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001676 _exit(kSecondaryDexDexoptAnalyzerSkippedOpenZip);
Calin Juravle7d765462017-09-04 15:57:10 -07001677 }
1678 }
1679
David Brazdil4f6027a2019-03-19 11:44:21 +00001680 // Open class loader context dex files.
1681 std::vector<unique_fd> context_zip_fds;
1682 int open_dex_paths_rc = open_dex_paths(context_dex_paths, &context_zip_fds, error_msg);
1683 if (open_dex_paths_rc != 0) {
1684 _exit(open_dex_paths_rc);
1685 }
1686
Calin Juravle7d765462017-09-04 15:57:10 -07001687 // Prepare the oat directories.
1688 if (!prepare_secondary_dex_oat_dir(dex_path, uid, instruction_set)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001689 _exit(kSecondaryDexDexoptAnalyzerSkippedPrepareDir);
Calin Juravle7d765462017-09-04 15:57:10 -07001690 }
1691
1692 // Open the vdex/oat files if any.
1693 unique_fd oat_file_fd;
1694 unique_fd vdex_file_fd;
1695 if (!maybe_open_oat_and_vdex_file(dex_path,
1696 *oat_dir_out,
1697 instruction_set,
1698 true /* is_secondary_dex */,
1699 &oat_file_fd,
1700 &vdex_file_fd)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001701 _exit(kSecondaryDexDexoptAnalyzerSkippedOpenOutput);
Calin Juravle7d765462017-09-04 15:57:10 -07001702 }
1703
1704 // Analyze profiles.
Calin Juravle3b758282021-06-08 08:04:52 -07001705 int profile_analysis_result = analyze_profiles(uid, pkgname, dex_path,
Calin Juravle824a64d2018-01-18 20:23:17 -08001706 /*is_secondary_dex*/true);
Calin Juravle7d765462017-09-04 15:57:10 -07001707
1708 // Run dexoptanalyzer to get dexopt_needed code. This is not expected to return.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001709 // Note that we do not do it before the fork since opening the files is required to happen
1710 // after forking.
1711 RunDexoptAnalyzer run_dexopt_analyzer(dex_path,
1712 vdex_file_fd.get(),
1713 oat_file_fd.get(),
1714 zip_fd.get(),
1715 instruction_set,
Calin Juravle3b758282021-06-08 08:04:52 -07001716 compiler_filter,
1717 profile_analysis_result,
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001718 downgrade,
David Brazdil4f6027a2019-03-19 11:44:21 +00001719 class_loader_context,
1720 join_fds(context_zip_fds));
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001721 run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec);
Calin Juravle80a21252017-01-17 14:43:25 -08001722 }
1723
1724 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +00001725 int result = wait_child_with_timeout(pid, kShortTimeoutMs);
Keun young Park65578d62021-06-28 17:52:05 -07001726 cancelled = dexopt_status_->check_if_killed_and_remove_dexopt_pid(pid);
Calin Juravle80a21252017-01-17 14:43:25 -08001727 if (!WIFEXITED(result)) {
Keun young Park65578d62021-06-28 17:52:05 -07001728 if ((WTERMSIG(result) == SIGKILL) && cancelled) {
1729 LOG(INFO) << "dexoptanalyzer cancelled for path:" << dex_path;
1730 return kSecondaryDexOptProcessCancelled;
1731 }
Andreas Gampe194fe422018-02-28 20:16:19 -08001732 *error_msg = StringPrintf("dexoptanalyzer failed for path %s: 0x%04x",
1733 dex_path.c_str(),
1734 result);
1735 LOG(ERROR) << *error_msg;
Keun young Park65578d62021-06-28 17:52:05 -07001736 return kSecondaryDexOptProcessError;
Calin Juravle80a21252017-01-17 14:43:25 -08001737 }
1738 result = WEXITSTATUS(result);
Calin Juravle7d765462017-09-04 15:57:10 -07001739 // Check that we successfully executed dexoptanalyzer.
Andreas Gampe194fe422018-02-28 20:16:19 -08001740 bool success = process_secondary_dexoptanalyzer_result(dex_path,
1741 result,
1742 dexopt_needed_out,
1743 error_msg);
1744 if (!success) {
1745 LOG(ERROR) << *error_msg;
1746 }
Calin Juravle7d765462017-09-04 15:57:10 -07001747
1748 LOG(DEBUG) << "Processed secondary dex file " << dex_path << " result=" << result;
1749
Calin Juravle80a21252017-01-17 14:43:25 -08001750 // Run dexopt only if needed or forced.
Calin Juravle7d765462017-09-04 15:57:10 -07001751 // Note that dexoptanalyzer is executed even if force compilation is enabled (because it
1752 // makes the code simpler; force compilation is only needed during tests).
1753 if (success &&
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001754 (result != kSecondaryDexDexoptAnalyzerSkippedNoFile) &&
Calin Juravle7d765462017-09-04 15:57:10 -07001755 ((dexopt_flags & DEXOPT_FORCE) != 0)) {
Calin Juravle80a21252017-01-17 14:43:25 -08001756 *dexopt_needed_out = DEX2OAT_FROM_SCRATCH;
1757 }
1758
Calin Juravle7d765462017-09-04 15:57:10 -07001759 // Check if we should make the oat file public.
1760 // Note that if the dex file is not public the compiled code cannot be made public.
1761 // It is ok to check this flag outside in the parent process.
1762 *is_public_out = ((dexopt_flags & DEXOPT_PUBLIC) != 0) && is_file_public(dex_path);
1763
Keun young Park65578d62021-06-28 17:52:05 -07001764 return success ? kSecondaryDexOptProcessOk : kSecondaryDexOptProcessError;
Calin Juravle80a21252017-01-17 14:43:25 -08001765}
1766
Andreas Gampefa2dadd2018-02-28 19:52:47 -08001767static std::string format_dexopt_error(int status, const char* dex_path) {
1768 if (WIFEXITED(status)) {
1769 int int_code = WEXITSTATUS(status);
1770 const char* code_name = get_return_code_name(static_cast<DexoptReturnCodes>(int_code));
1771 if (code_name != nullptr) {
1772 return StringPrintf("Dex2oat invocation for %s failed: %s", dex_path, code_name);
1773 }
1774 }
1775 return StringPrintf("Dex2oat invocation for %s failed with 0x%04x", dex_path, status);
Andreas Gampe023b2242018-02-28 16:03:25 -08001776}
1777
Keun young Park65578d62021-06-28 17:52:05 -07001778
Calin Juravlec9eab382017-01-25 01:17:17 -08001779int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* instruction_set,
Calin Juravle80a21252017-01-17 14:43:25 -08001780 int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
Calin Juravle52c45822017-07-13 22:50:21 -07001781 const char* volume_uuid, const char* class_loader_context, const char* se_info,
Calin Juravle62c5a372018-02-01 17:03:23 +00001782 bool downgrade, int target_sdk_version, const char* profile_name,
Keun young Park65578d62021-06-28 17:52:05 -07001783 const char* dex_metadata_path, const char* compilation_reason, std::string* error_msg,
1784 /* out */ bool* completed) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001785 CHECK(pkgname != nullptr);
1786 CHECK(pkgname[0] != 0);
Andreas Gampe023b2242018-02-28 16:03:25 -08001787 CHECK(error_msg != nullptr);
Andreas Gamped32eec22018-02-28 16:02:51 -08001788 CHECK_EQ(dexopt_flags & ~DEXOPT_MASK, 0)
1789 << "dexopt flags contains unknown fields: " << dexopt_flags;
Calin Juravle7a570e82017-01-14 16:23:30 -08001790
Keun young Park65578d62021-06-28 17:52:05 -07001791 bool local_completed; // local placeholder for nullptr case
1792 if (completed == nullptr) {
1793 completed = &local_completed;
1794 }
1795 *completed = true;
1796 if (dexopt_status_->is_dexopt_blocked()) {
1797 *completed = false;
1798 return 0;
1799 }
1800
Calin Juravled23dee72017-07-06 16:29:11 -07001801 if (!validate_dex_path_size(dex_path)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001802 *error_msg = StringPrintf("Failed to validate %s", dex_path);
Calin Juravle52c45822017-07-13 22:50:21 -07001803 return -1;
1804 }
1805
1806 if (class_loader_context != nullptr && strlen(class_loader_context) > PKG_PATH_MAX) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001807 *error_msg = StringPrintf("Class loader context exceeds the allowed size: %s",
1808 class_loader_context);
1809 LOG(ERROR) << *error_msg;
Calin Juravle52c45822017-07-13 22:50:21 -07001810 return -1;
Calin Juravled23dee72017-07-06 16:29:11 -07001811 }
1812
Calin Juravleebc8a792017-04-04 20:21:05 -07001813 bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0;
Calin Juravle7a570e82017-01-14 16:23:30 -08001814 bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
1815 bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
1816 bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0;
Calin Juravle80a21252017-01-17 14:43:25 -08001817 bool is_secondary_dex = (dexopt_flags & DEXOPT_SECONDARY_DEX) != 0;
Andreas Gampea73a0cb2017-11-02 18:14:42 -07001818 bool background_job_compile = (dexopt_flags & DEXOPT_IDLE_BACKGROUND_JOB) != 0;
David Brazdil52249162018-02-12 18:04:59 -08001819 bool enable_hidden_api_checks = (dexopt_flags & DEXOPT_ENABLE_HIDDEN_API_CHECKS) != 0;
Mathieu Chartierf69c2f72018-03-06 13:55:58 -08001820 bool generate_compact_dex = (dexopt_flags & DEXOPT_GENERATE_COMPACT_DEX) != 0;
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -07001821 bool generate_app_image = (dexopt_flags & DEXOPT_GENERATE_APP_IMAGE) != 0;
Patrick Baumann2271b3e2020-04-14 17:03:00 -07001822 bool for_restore = (dexopt_flags & DEXOPT_FOR_RESTORE) != 0;
Calin Juravle80a21252017-01-17 14:43:25 -08001823
1824 // Check if we're dealing with a secondary dex file and if we need to compile it.
1825 std::string oat_dir_str;
David Brazdil4f6027a2019-03-19 11:44:21 +00001826 std::vector<std::string> context_dex_paths;
Calin Juravle80a21252017-01-17 14:43:25 -08001827 if (is_secondary_dex) {
David Brazdil4f6027a2019-03-19 11:44:21 +00001828 if (!get_class_loader_context_dex_paths(class_loader_context, uid, &context_dex_paths)) {
1829 *error_msg = "Failed acquiring context dex paths";
1830 return -1; // We had an error, logged in the process method.
1831 }
Keun young Park65578d62021-06-28 17:52:05 -07001832 SecondaryDexOptProcessResult sec_dex_result = process_secondary_dex_dexopt(dex_path,
1833 pkgname, dexopt_flags, volume_uuid, uid,instruction_set, compiler_filter,
1834 &is_public, &dexopt_needed, &oat_dir_str, downgrade, class_loader_context,
1835 context_dex_paths, error_msg);
1836 if (sec_dex_result == kSecondaryDexOptProcessOk) {
Calin Juravle80a21252017-01-17 14:43:25 -08001837 oat_dir = oat_dir_str.c_str();
1838 if (dexopt_needed == NO_DEXOPT_NEEDED) {
Keun young Parkb7342262021-10-25 08:09:27 -07001839 *completed = true;
Calin Juravle80a21252017-01-17 14:43:25 -08001840 return 0; // Nothing to do, report success.
1841 }
Keun young Park65578d62021-06-28 17:52:05 -07001842 } else if (sec_dex_result == kSecondaryDexOptProcessCancelled) {
1843 // cancelled, not an error.
1844 *completed = false;
1845 return 0;
Calin Juravle80a21252017-01-17 14:43:25 -08001846 } else {
Andreas Gampe194fe422018-02-28 20:16:19 -08001847 if (error_msg->empty()) { // TODO: Make this a CHECK.
1848 *error_msg = "Failed processing secondary.";
1849 }
Calin Juravle80a21252017-01-17 14:43:25 -08001850 return -1; // We had an error, logged in the process method.
1851 }
1852 } else {
David Brazdil4f6027a2019-03-19 11:44:21 +00001853 // Currently these flags are only used for secondary dex files.
Calin Juravlec9eab382017-01-25 01:17:17 -08001854 // Verify that they are not set for primary apks.
Calin Juravle80a21252017-01-17 14:43:25 -08001855 CHECK((dexopt_flags & DEXOPT_STORAGE_CE) == 0);
1856 CHECK((dexopt_flags & DEXOPT_STORAGE_DE) == 0);
1857 }
Calin Juravle7a570e82017-01-14 16:23:30 -08001858
1859 // Open the input file.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001860 UniqueFile in_dex(open(dex_path, O_RDONLY, 0), dex_path);
1861 if (in_dex.fd() < 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001862 *error_msg = StringPrintf("installd cannot open '%s' for input during dexopt", dex_path);
1863 LOG(ERROR) << *error_msg;
Calin Juravle7a570e82017-01-14 16:23:30 -08001864 return -1;
1865 }
1866
David Brazdil4f6027a2019-03-19 11:44:21 +00001867 // Open class loader context dex files.
1868 std::vector<unique_fd> context_input_fds;
1869 if (open_dex_paths(context_dex_paths, &context_input_fds, error_msg) != 0) {
1870 LOG(ERROR) << *error_msg;
1871 return -1;
1872 }
1873
Calin Juravle7a570e82017-01-14 16:23:30 -08001874 // Create the output OAT file.
Keun young Parkb7342262021-10-25 08:09:27 -07001875 RestorableFile out_oat =
1876 open_oat_out_file(dex_path, oat_dir, is_public, uid, instruction_set, is_secondary_dex);
Victor Hsiehcb35a062020-08-13 16:11:13 -07001877 if (out_oat.fd() < 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001878 *error_msg = "Could not open out oat file.";
Calin Juravle7a570e82017-01-14 16:23:30 -08001879 return -1;
1880 }
1881
1882 // Open vdex files.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001883 UniqueFile in_vdex;
Keun young Parkb7342262021-10-25 08:09:27 -07001884 RestorableFile out_vdex;
Victor Hsiehcb35a062020-08-13 16:11:13 -07001885 if (!open_vdex_files_for_dex2oat(dex_path, out_oat.path().c_str(), dexopt_needed,
1886 instruction_set, is_public, uid, is_secondary_dex, profile_guided, &in_vdex,
1887 &out_vdex)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001888 *error_msg = "Could not open vdex files.";
Jeff Sharkey90aff262016-12-12 14:28:24 -07001889 return -1;
1890 }
1891
Calin Juravlecb556e32017-04-04 20:22:50 -07001892 // Ensure that the oat dir and the compiler artifacts of secondary dex files have the correct
1893 // selinux context (we generate them on the fly during the dexopt invocation and they don't
1894 // fully inherit their parent context).
1895 // Note that for primary apk the oat files are created before, in a separate installd
1896 // call which also does the restorecon. TODO(calin): unify the paths.
1897 if (is_secondary_dex) {
1898 if (selinux_android_restorecon_pkgdir(oat_dir, se_info, uid,
1899 SELINUX_ANDROID_RESTORECON_RECURSE)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001900 *error_msg = std::string("Failed to restorecon ").append(oat_dir);
1901 LOG(ERROR) << *error_msg;
Calin Juravlecb556e32017-04-04 20:22:50 -07001902 return -1;
1903 }
1904 }
1905
Jeff Sharkey90aff262016-12-12 14:28:24 -07001906 // Create a swap file if necessary.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001907 unique_fd swap_fd = maybe_open_dexopt_swap_file(out_oat.path());
Jeff Sharkey90aff262016-12-12 14:28:24 -07001908
Calin Juravle7a570e82017-01-14 16:23:30 -08001909 // Open the reference profile if needed.
Victor Hsiehcb35a062020-08-13 16:11:13 -07001910 UniqueFile reference_profile = maybe_open_reference_profile(
Calin Juravle824a64d2018-01-18 20:23:17 -08001911 pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex);
Calin Juravle7a570e82017-01-14 16:23:30 -08001912
Victor Hsiehcb35a062020-08-13 16:11:13 -07001913 if (reference_profile.fd() == -1) {
liulvping61907742018-08-21 09:36:52 +08001914 // We don't create an app image without reference profile since there is no speedup from
1915 // loading it in that case and instead will be a small overhead.
1916 generate_app_image = false;
1917 }
1918
1919 // Create the app image file if needed.
Keun young Parkb7342262021-10-25 08:09:27 -07001920 RestorableFile out_image = maybe_open_app_image(out_oat.path(), generate_app_image, is_public,
1921 uid, is_secondary_dex);
liulvping61907742018-08-21 09:36:52 +08001922
Victor Hsiehcb35a062020-08-13 16:11:13 -07001923 UniqueFile dex_metadata;
Calin Juravle62c5a372018-02-01 17:03:23 +00001924 if (dex_metadata_path != nullptr) {
Victor Hsiehcb35a062020-08-13 16:11:13 -07001925 dex_metadata.reset(TEMP_FAILURE_RETRY(open(dex_metadata_path, O_RDONLY | O_NOFOLLOW)),
1926 dex_metadata_path);
1927 if (dex_metadata.fd() < 0) {
Calin Juravle62c5a372018-02-01 17:03:23 +00001928 PLOG(ERROR) << "Failed to open dex metadata file " << dex_metadata_path;
1929 }
1930 }
1931
Victor Hsieh8948a862020-08-07 11:30:55 -07001932 std::string jitzygote_flag = server_configurable_flags::GetServerConfigurableFlag(
1933 RUNTIME_NATIVE_BOOT_NAMESPACE,
1934 ENABLE_JITZYGOTE_IMAGE,
1935 /*default_value=*/ "");
1936 bool use_jitzygote_image = jitzygote_flag == "true" || IsBootClassPathProfilingEnable();
1937
Victor Hsiehe98e6512020-08-10 15:39:22 -07001938 // Decide whether to use dex2oat64.
1939 bool use_dex2oat64 = false;
1940 // Check whether the device even supports 64-bit ABIs.
1941 if (!GetProperty("ro.product.cpu.abilist64", "").empty()) {
1942 use_dex2oat64 = GetBoolProperty("dalvik.vm.dex2oat64.enabled", false);
1943 }
1944 const char* dex2oat_bin = select_execution_binary(
1945 (use_dex2oat64 ? kDex2oat64Path : kDex2oat32Path),
1946 (use_dex2oat64 ? kDex2oatDebug64Path : kDex2oatDebug32Path),
1947 background_job_compile);
1948
Victor Hsiehc9821f12020-08-07 11:32:29 -07001949 auto execv_helper = std::make_unique<ExecVHelper>();
1950
Andreas Gampe023b2242018-02-28 16:03:25 -08001951 LOG(VERBOSE) << "DexInv: --- BEGIN '" << dex_path << "' ---";
Jeff Sharkey90aff262016-12-12 14:28:24 -07001952
Victor Hsiehc9821f12020-08-07 11:32:29 -07001953 RunDex2Oat runner(dex2oat_bin, execv_helper.get());
Keun young Parkb7342262021-10-25 08:09:27 -07001954 runner.Initialize(out_oat.GetUniqueFile(), out_vdex.GetUniqueFile(), out_image.GetUniqueFile(),
1955 in_dex, in_vdex, dex_metadata, reference_profile, class_loader_context,
1956 join_fds(context_input_fds), swap_fd.get(), instruction_set, compiler_filter,
1957 debuggable, boot_complete, for_restore, target_sdk_version,
1958 enable_hidden_api_checks, generate_compact_dex, use_jitzygote_image,
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001959 compilation_reason);
1960
Keun young Park65578d62021-06-28 17:52:05 -07001961 bool cancelled = false;
1962 pid_t pid = dexopt_status_->check_cancellation_and_fork(&cancelled);
1963 if (cancelled) {
Keun young Parkb7342262021-10-25 08:09:27 -07001964 *completed = false;
1965 reference_profile.DisableCleanup();
Keun young Park65578d62021-06-28 17:52:05 -07001966 return 0;
1967 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001968 if (pid == 0) {
Wei Wange0dbd9b2020-12-11 17:24:19 +00001969 // Need to set schedpolicy before dropping privileges
1970 // for cgroup migration. See details at b/175178520.
1971 SetDex2OatScheduling(boot_complete);
1972
Jeff Sharkey90aff262016-12-12 14:28:24 -07001973 /* child -- drop privileges before continuing */
1974 drop_capabilities(uid);
1975
Victor Hsiehcb35a062020-08-13 16:11:13 -07001976 if (flock(out_oat.fd(), LOCK_EX | LOCK_NB) != 0) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02001977 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "flock(%s) failed",
1978 out_oat.path().c_str());
Andreas Gampefa2dadd2018-02-28 19:52:47 -08001979 _exit(DexoptReturnCodes::kFlock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001980 }
1981
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001982 runner.Exec(DexoptReturnCodes::kDex2oatExec);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001983 } else {
Jiakai Zhang495142a2022-02-21 19:38:14 +00001984 int res = wait_child_with_timeout(pid, kLongTimeoutMs);
Keun young Park65578d62021-06-28 17:52:05 -07001985 bool cancelled = dexopt_status_->check_if_killed_and_remove_dexopt_pid(pid);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001986 if (res == 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08001987 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' (success) ---";
Jeff Sharkey90aff262016-12-12 14:28:24 -07001988 } else {
Keun young Park65578d62021-06-28 17:52:05 -07001989 if ((WTERMSIG(res) == SIGKILL) && cancelled) {
1990 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' --- cancelled";
1991 // cancelled, not an error
1992 *completed = false;
Keun young Parkb7342262021-10-25 08:09:27 -07001993 reference_profile.DisableCleanup();
Keun young Park65578d62021-06-28 17:52:05 -07001994 return 0;
1995 }
Andreas Gampe023b2242018-02-28 16:03:25 -08001996 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' --- status=0x"
1997 << std::hex << std::setw(4) << res << ", process failed";
1998 *error_msg = format_dexopt_error(res, dex_path);
Andreas Gampe013f02e2017-03-20 18:36:54 -07001999 return res;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002000 }
2001 }
2002
Keun young Parkb7342262021-10-25 08:09:27 -07002003 // dex2oat ran successfully, so profile is safe to keep.
Victor Hsiehcb35a062020-08-13 16:11:13 -07002004 reference_profile.DisableCleanup();
Jeff Sharkey90aff262016-12-12 14:28:24 -07002005
Keun young Parkb7342262021-10-25 08:09:27 -07002006 // We've been successful, commit work files.
2007 // If committing (=renaming tmp to regular) fails, try to restore backup files.
2008 // If restoring fails as well, as a last resort, remove all files.
2009 if (!out_oat.CreateBackupFile() || !out_vdex.CreateBackupFile() ||
2010 !out_image.CreateBackupFile()) {
2011 // Renaming failure can mean that the original file may not be accessible from installd.
2012 LOG(ERROR) << "Cannot create backup file from existing file, file in wrong state?"
2013 << ", out_oat:" << out_oat.path() << " ,out_vdex:" << out_vdex.path()
2014 << " ,out_image:" << out_image.path();
2015 out_oat.ResetAndRemoveAllFiles();
2016 out_vdex.ResetAndRemoveAllFiles();
2017 out_image.ResetAndRemoveAllFiles();
2018 return -1;
2019 }
2020 if (!out_oat.CommitWorkFile() || !out_vdex.CommitWorkFile() || !out_image.CommitWorkFile()) {
2021 LOG(ERROR) << "Cannot commit, out_oat:" << out_oat.path()
2022 << " ,out_vdex:" << out_vdex.path() << " ,out_image:" << out_image.path();
2023 if (!out_oat.RestoreBackupFile() || !out_vdex.RestoreBackupFile() ||
2024 !out_image.RestoreBackupFile()) {
2025 LOG(ERROR) << "Cannot cancel commit, out_oat:" << out_oat.path()
2026 << " ,out_vdex:" << out_vdex.path() << " ,out_image:" << out_image.path();
2027 // Restoring failed.
2028 out_oat.ResetAndRemoveAllFiles();
2029 out_vdex.ResetAndRemoveAllFiles();
2030 out_image.ResetAndRemoveAllFiles();
2031 }
2032 return -1;
2033 }
2034 // Now remove remaining backup files.
2035 out_oat.RemoveBackupFile();
2036 out_vdex.RemoveBackupFile();
2037 out_image.RemoveBackupFile();
2038
Keun young Park65578d62021-06-28 17:52:05 -07002039 *completed = true;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002040 return 0;
2041}
2042
Calin Juravlec9eab382017-01-25 01:17:17 -08002043// Try to remove the given directory. Log an error if the directory exists
2044// and is empty but could not be removed.
2045static bool rmdir_if_empty(const char* dir) {
2046 if (rmdir(dir) == 0) {
2047 return true;
2048 }
2049 if (errno == ENOENT || errno == ENOTEMPTY) {
2050 return true;
2051 }
2052 PLOG(ERROR) << "Failed to remove dir: " << dir;
2053 return false;
2054}
2055
2056// Try to unlink the given file. Log an error if the file exists and could not
2057// be unlinked.
2058static bool unlink_if_exists(const std::string& file) {
2059 if (unlink(file.c_str()) == 0) {
2060 return true;
2061 }
2062 if (errno == ENOENT) {
2063 return true;
2064
2065 }
2066 PLOG(ERROR) << "Could not unlink: " << file;
2067 return false;
2068}
2069
Calin Juravle7d765462017-09-04 15:57:10 -07002070enum ReconcileSecondaryDexResult {
2071 kReconcileSecondaryDexExists = 0,
2072 kReconcileSecondaryDexCleanedUp = 1,
2073 kReconcileSecondaryDexValidationError = 2,
2074 kReconcileSecondaryDexCleanUpError = 3,
2075 kReconcileSecondaryDexAccessIOError = 4,
2076};
Calin Juravlec9eab382017-01-25 01:17:17 -08002077
2078// Reconcile the secondary dex 'dex_path' and its generated oat files.
2079// Return true if all the parameters are valid and the secondary dex file was
2080// processed successfully (i.e. the dex_path either exists, or if not, its corresponding
2081// oat/vdex/art files where deleted successfully). In this case, out_secondary_dex_exists
2082// will be true if the secondary dex file still exists. If the secondary dex file does not exist,
2083// the method cleans up any previously generated compiler artifacts (oat, vdex, art).
2084// Return false if there were errors during processing. In this case
2085// out_secondary_dex_exists will be set to false.
2086bool reconcile_secondary_dex_file(const std::string& dex_path,
2087 const std::string& pkgname, int uid, const std::vector<std::string>& isas,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002088 const std::optional<std::string>& volume_uuid, int storage_flag,
Calin Juravlec9eab382017-01-25 01:17:17 -08002089 /*out*/bool* out_secondary_dex_exists) {
Calin Juravle7d765462017-09-04 15:57:10 -07002090 *out_secondary_dex_exists = false; // start by assuming the file does not exist.
Calin Juravlec9eab382017-01-25 01:17:17 -08002091 if (isas.size() == 0) {
2092 LOG(ERROR) << "reconcile_secondary_dex_file called with empty isas vector";
2093 return false;
2094 }
2095
Calin Juravle7d765462017-09-04 15:57:10 -07002096 if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) {
2097 LOG(ERROR) << "reconcile_secondary_dex_file called with invalid storage_flag: "
2098 << storage_flag;
Calin Juravlec9eab382017-01-25 01:17:17 -08002099 return false;
2100 }
2101
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002102 // As a security measure we want to unlink art artifacts with the reduced capabilities
2103 // of the package user id. So we fork and drop capabilities in the child.
2104 pid_t pid = fork();
2105 if (pid == 0) {
Calin Juravle7d765462017-09-04 15:57:10 -07002106 /* child -- drop privileges before continuing */
2107 drop_capabilities(uid);
2108
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002109 const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr;
Greg Kaiser8042c372019-03-26 06:23:19 -07002110 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr,
Calin Juravle7d765462017-09-04 15:57:10 -07002111 uid, storage_flag)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002112 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2113 "Could not validate secondary dex path %s", dex_path.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07002114 _exit(kReconcileSecondaryDexValidationError);
2115 }
2116
2117 SecondaryDexAccess access_check = check_secondary_dex_access(dex_path);
2118 switch (access_check) {
2119 case kSecondaryDexAccessDoesNotExist:
2120 // File does not exist. Proceed with cleaning.
2121 break;
2122 case kSecondaryDexAccessReadOk: _exit(kReconcileSecondaryDexExists);
2123 case kSecondaryDexAccessIOError: _exit(kReconcileSecondaryDexAccessIOError);
2124 case kSecondaryDexAccessPermissionError: _exit(kReconcileSecondaryDexValidationError);
2125 default:
Martijn Coenen6de402a2021-04-26 16:23:40 +02002126 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2127 "Unexpected result from check_secondary_dex_access: %d", access_check);
Calin Juravle7d765462017-09-04 15:57:10 -07002128 _exit(kReconcileSecondaryDexValidationError);
2129 }
2130
2131 // The secondary dex does not exist anymore or it's. Clear any generated files.
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002132 char oat_path[PKG_PATH_MAX];
2133 char oat_dir[PKG_PATH_MAX];
2134 char oat_isa_dir[PKG_PATH_MAX];
2135 bool result = true;
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002136 for (size_t i = 0; i < isas.size(); i++) {
Andreas Gampe194fe422018-02-28 20:16:19 -08002137 std::string error_msg;
Calin Juravle7d765462017-09-04 15:57:10 -07002138 if (!create_secondary_dex_oat_layout(
Andreas Gampe194fe422018-02-28 20:16:19 -08002139 dex_path,isas[i], oat_dir, oat_isa_dir, oat_path, &error_msg)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002140 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, "%s", error_msg.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07002141 _exit(kReconcileSecondaryDexValidationError);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002142 }
Calin Juravle51314092017-05-18 15:33:05 -07002143
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002144 // Delete oat/vdex/art files.
2145 result = unlink_if_exists(oat_path) && result;
2146 result = unlink_if_exists(create_vdex_filename(oat_path)) && result;
2147 result = unlink_if_exists(create_image_filename(oat_path)) && result;
Calin Juravlec9eab382017-01-25 01:17:17 -08002148
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002149 // Delete profiles.
2150 std::string current_profile = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -08002151 multiuser_get_user_id(uid), pkgname, dex_path, /*is_secondary*/true);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002152 std::string reference_profile = create_reference_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -08002153 pkgname, dex_path, /*is_secondary*/true);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002154 result = unlink_if_exists(current_profile) && result;
2155 result = unlink_if_exists(reference_profile) && result;
Calin Juravle51314092017-05-18 15:33:05 -07002156
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002157 // We upgraded once the location of current profile for secondary dex files.
2158 // Check for any previous left-overs and remove them as well.
2159 std::string old_current_profile = dex_path + ".prof";
2160 result = unlink_if_exists(old_current_profile);
Calin Juravle3760ad32017-07-27 16:31:55 -07002161
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002162 // Try removing the directories as well, they might be empty.
2163 result = rmdir_if_empty(oat_isa_dir) && result;
2164 result = rmdir_if_empty(oat_dir) && result;
2165 }
Calin Juravle7d765462017-09-04 15:57:10 -07002166 if (!result) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002167 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2168 "Could not validate secondary dex path %s", dex_path.c_str());
Calin Juravle7d765462017-09-04 15:57:10 -07002169 }
2170 _exit(result ? kReconcileSecondaryDexCleanedUp : kReconcileSecondaryDexAccessIOError);
Calin Juravlec9eab382017-01-25 01:17:17 -08002171 }
2172
Jiakai Zhang495142a2022-02-21 19:38:14 +00002173 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
Calin Juravle7d765462017-09-04 15:57:10 -07002174 if (!WIFEXITED(return_code)) {
2175 LOG(WARNING) << "reconcile dex failed for location " << dex_path << ": " << return_code;
2176 } else {
2177 return_code = WEXITSTATUS(return_code);
2178 }
2179
2180 LOG(DEBUG) << "Reconcile secondary dex path " << dex_path << " result=" << return_code;
2181
2182 switch (return_code) {
2183 case kReconcileSecondaryDexCleanedUp:
2184 case kReconcileSecondaryDexValidationError:
2185 // If we couldn't validate assume the dex file does not exist.
2186 // This will purge the entry from the PM records.
2187 *out_secondary_dex_exists = false;
2188 return true;
2189 case kReconcileSecondaryDexExists:
2190 *out_secondary_dex_exists = true;
2191 return true;
2192 case kReconcileSecondaryDexAccessIOError:
2193 // We had an access IO error.
2194 // Return false so that we can try again.
2195 // The value of out_secondary_dex_exists does not matter in this case and by convention
2196 // is set to false.
2197 *out_secondary_dex_exists = false;
2198 return false;
2199 default:
2200 LOG(ERROR) << "Unexpected code from reconcile_secondary_dex_file: " << return_code;
2201 *out_secondary_dex_exists = false;
2202 return false;
2203 }
Calin Juravlec9eab382017-01-25 01:17:17 -08002204}
2205
Alan Stokesa25d90c2017-10-16 10:56:00 +01002206// Compute and return the hash (SHA-256) of the secondary dex file at dex_path.
2207// Returns true if all parameters are valid and the hash successfully computed and stored in
2208// out_secondary_dex_hash.
2209// Also returns true with an empty hash if the file does not currently exist or is not accessible to
2210// the app.
2211// For any other errors (e.g. if any of the parameters are invalid) returns false.
2212bool hash_secondary_dex_file(const std::string& dex_path, const std::string& pkgname, int uid,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002213 const std::optional<std::string>& volume_uuid, int storage_flag,
Alan Stokesa25d90c2017-10-16 10:56:00 +01002214 std::vector<uint8_t>* out_secondary_dex_hash) {
2215 out_secondary_dex_hash->clear();
2216
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002217 const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr;
Alan Stokesa25d90c2017-10-16 10:56:00 +01002218
2219 if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) {
2220 LOG(ERROR) << "hash_secondary_dex_file called with invalid storage_flag: "
2221 << storage_flag;
2222 return false;
2223 }
2224
2225 // Pipe to get the hash result back from our child process.
2226 unique_fd pipe_read, pipe_write;
2227 if (!Pipe(&pipe_read, &pipe_write)) {
2228 PLOG(ERROR) << "Failed to create pipe";
2229 return false;
2230 }
2231
2232 // Fork so that actual access to the files is done in the app's own UID, to ensure we only
2233 // access data the app itself can access.
2234 pid_t pid = fork();
2235 if (pid == 0) {
2236 // child -- drop privileges before continuing
2237 drop_capabilities(uid);
2238 pipe_read.reset();
2239
2240 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr, uid, storage_flag)) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002241 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2242 "Could not validate secondary dex path %s", dex_path.c_str());
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002243 _exit(DexoptReturnCodes::kHashValidatePath);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002244 }
2245
2246 unique_fd fd(TEMP_FAILURE_RETRY(open(dex_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)));
2247 if (fd == -1) {
2248 if (errno == EACCES || errno == ENOENT) {
2249 // Not treated as an error.
2250 _exit(0);
2251 }
2252 PLOG(ERROR) << "Failed to open secondary dex " << dex_path;
Martijn Coenen6de402a2021-04-26 16:23:40 +02002253 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2254 "Failed to open secondary dex %s: %d", dex_path.c_str(), errno);
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002255 _exit(DexoptReturnCodes::kHashOpenPath);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002256 }
2257
2258 SHA256_CTX ctx;
2259 SHA256_Init(&ctx);
2260
2261 std::vector<uint8_t> buffer(65536);
2262 while (true) {
2263 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size()));
2264 if (bytes_read == 0) {
2265 break;
2266 } else if (bytes_read == -1) {
Martijn Coenen6de402a2021-04-26 16:23:40 +02002267 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
2268 "Failed to read secondary dex %s: %d", dex_path.c_str(), errno);
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002269 _exit(DexoptReturnCodes::kHashReadDex);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002270 }
2271
2272 SHA256_Update(&ctx, buffer.data(), bytes_read);
2273 }
2274
2275 std::array<uint8_t, SHA256_DIGEST_LENGTH> hash;
2276 SHA256_Final(hash.data(), &ctx);
2277 if (!WriteFully(pipe_write, hash.data(), hash.size())) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002278 _exit(DexoptReturnCodes::kHashWrite);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002279 }
2280
2281 _exit(0);
2282 }
2283
2284 // parent
2285 pipe_write.reset();
2286
2287 out_secondary_dex_hash->resize(SHA256_DIGEST_LENGTH);
2288 if (!ReadFully(pipe_read, out_secondary_dex_hash->data(), out_secondary_dex_hash->size())) {
2289 out_secondary_dex_hash->clear();
2290 }
Jiakai Zhang495142a2022-02-21 19:38:14 +00002291 return wait_child_with_timeout(pid, kShortTimeoutMs) == 0;
Alan Stokesa25d90c2017-10-16 10:56:00 +01002292}
2293
Jeff Sharkey90aff262016-12-12 14:28:24 -07002294// Helper for move_ab, so that we can have common failure-case cleanup.
2295static bool unlink_and_rename(const char* from, const char* to) {
2296 // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise,
2297 // return a failure.
2298 struct stat s;
2299 if (stat(to, &s) == 0) {
2300 if (!S_ISREG(s.st_mode)) {
2301 LOG(ERROR) << from << " is not a regular file to replace for A/B.";
2302 return false;
2303 }
2304 if (unlink(to) != 0) {
2305 LOG(ERROR) << "Could not unlink " << to << " to move A/B.";
2306 return false;
2307 }
2308 } else {
2309 // This may be a permission problem. We could investigate the error code, but we'll just
2310 // let the rename failure do the work for us.
2311 }
2312
2313 // Try to rename "to" to "from."
2314 if (rename(from, to) != 0) {
2315 PLOG(ERROR) << "Could not rename " << from << " to " << to;
2316 return false;
2317 }
2318 return true;
2319}
2320
2321// Move/rename a B artifact (from) to an A artifact (to).
2322static bool move_ab_path(const std::string& b_path, const std::string& a_path) {
2323 // Check whether B exists.
2324 {
2325 struct stat s;
2326 if (stat(b_path.c_str(), &s) != 0) {
Alex Light9f295662021-02-25 11:50:33 -08002327 // Ignore for now. The service calling this isn't smart enough to
2328 // understand lack of artifacts at the moment.
2329 LOG(VERBOSE) << "A/B artifact " << b_path << " does not exist!";
Jeff Sharkey90aff262016-12-12 14:28:24 -07002330 return false;
2331 }
2332 if (!S_ISREG(s.st_mode)) {
2333 LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file.";
2334 // Try to unlink, but swallow errors.
2335 unlink(b_path.c_str());
2336 return false;
2337 }
2338 }
2339
2340 // Rename B to A.
2341 if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) {
2342 // Delete the b_path so we don't try again (or fail earlier).
2343 if (unlink(b_path.c_str()) != 0) {
2344 PLOG(ERROR) << "Could not unlink " << b_path;
2345 }
2346
2347 return false;
2348 }
2349
2350 return true;
2351}
2352
2353bool move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) {
2354 // Get the current slot suffix. No suffix, no A/B.
Mathieu Chartier9b2da082018-10-26 13:23:11 -07002355 const std::string slot_suffix = GetProperty("ro.boot.slot_suffix", "");
2356 if (slot_suffix.empty()) {
2357 return false;
2358 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07002359
Mathieu Chartier9b2da082018-10-26 13:23:11 -07002360 if (!ValidateTargetSlotSuffix(slot_suffix)) {
2361 LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix;
2362 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002363 }
2364
2365 // Validate other inputs.
2366 if (validate_apk_path(apk_path) != 0) {
2367 LOG(ERROR) << "Invalid apk_path: " << apk_path;
2368 return false;
2369 }
2370 if (validate_apk_path(oat_dir) != 0) {
2371 LOG(ERROR) << "Invalid oat_dir: " << oat_dir;
2372 return false;
2373 }
2374
2375 char a_path[PKG_PATH_MAX];
2376 if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) {
2377 return false;
2378 }
2379 const std::string a_vdex_path = create_vdex_filename(a_path);
2380 const std::string a_image_path = create_image_filename(a_path);
2381
2382 // B path = A path + slot suffix.
2383 const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str());
2384 const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str());
2385 const std::string b_image_path = StringPrintf("%s.%s",
2386 a_image_path.c_str(),
2387 slot_suffix.c_str());
2388
2389 bool success = true;
2390 if (move_ab_path(b_path, a_path)) {
2391 if (move_ab_path(b_vdex_path, a_vdex_path)) {
2392 // Note: we can live without an app image. As such, ignore failure to move the image file.
2393 // If we decide to require the app image, or the app image being moved correctly,
2394 // then change accordingly.
2395 constexpr bool kIgnoreAppImageFailure = true;
2396
2397 if (!a_image_path.empty()) {
2398 if (!move_ab_path(b_image_path, a_image_path)) {
2399 unlink(a_image_path.c_str());
2400 if (!kIgnoreAppImageFailure) {
2401 success = false;
2402 }
2403 }
2404 }
2405 } else {
2406 // Cleanup: delete B image, ignore errors.
2407 unlink(b_image_path.c_str());
2408 success = false;
2409 }
2410 } else {
2411 // Cleanup: delete B image, ignore errors.
2412 unlink(b_vdex_path.c_str());
2413 unlink(b_image_path.c_str());
2414 success = false;
2415 }
2416 return success;
2417}
2418
Calin Juravleea214ad2021-06-11 09:17:20 -07002419int64_t delete_odex(const char* apk_path, const char* instruction_set, const char* oat_dir) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07002420 // Delete the oat/odex file.
2421 char out_path[PKG_PATH_MAX];
Calin Juravle80a21252017-01-17 14:43:25 -08002422 if (!create_oat_out_path(apk_path, instruction_set, oat_dir,
Calin Juravle114f0812017-03-08 19:05:07 -08002423 /*is_secondary_dex*/false, out_path)) {
Calin Juravleea214ad2021-06-11 09:17:20 -07002424 LOG(ERROR) << "Cannot create apk path for " << apk_path;
2425 return -1;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002426 }
2427
2428 // In case of a permission failure report the issue. Otherwise just print a warning.
Calin Juravleea214ad2021-06-11 09:17:20 -07002429 auto unlink_and_check = [](const char* path) -> int64_t {
2430 struct stat file_stat;
2431 if (stat(path, &file_stat) != 0) {
2432 if (errno != ENOENT) {
2433 PLOG(ERROR) << "Could not stat " << path;
2434 return -1;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002435 }
Calin Juravleea214ad2021-06-11 09:17:20 -07002436 return 0;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002437 }
Calin Juravleea214ad2021-06-11 09:17:20 -07002438
2439 if (unlink(path) != 0) {
2440 if (errno != ENOENT) {
2441 PLOG(ERROR) << "Could not unlink " << path;
2442 return -1;
2443 }
2444 }
2445 return static_cast<int64_t>(file_stat.st_size);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002446 };
2447
2448 // Delete the oat/odex file.
Calin Juravleea214ad2021-06-11 09:17:20 -07002449 int64_t return_value_oat = unlink_and_check(out_path);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002450
2451 // Derive and delete the app image.
Calin Juravleea214ad2021-06-11 09:17:20 -07002452 int64_t return_value_art = unlink_and_check(create_image_filename(out_path).c_str());
Jeff Sharkey90aff262016-12-12 14:28:24 -07002453
Nicolas Geoffray192fb962017-05-25 13:58:06 +01002454 // Derive and delete the vdex file.
Calin Juravleea214ad2021-06-11 09:17:20 -07002455 int64_t return_value_vdex = unlink_and_check(create_vdex_filename(out_path).c_str());
Nicolas Geoffray192fb962017-05-25 13:58:06 +01002456
Calin Juravleea214ad2021-06-11 09:17:20 -07002457 // Report result
2458 if (return_value_oat == -1
2459 || return_value_art == -1
2460 || return_value_vdex == -1) {
2461 return -1;
2462 }
2463
2464 return return_value_oat + return_value_art + return_value_vdex;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002465}
2466
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06002467static bool is_absolute_path(const std::string& path) {
2468 if (path.find('/') != 0 || path.find("..") != std::string::npos) {
2469 LOG(ERROR) << "Invalid absolute path " << path;
2470 return false;
2471 } else {
2472 return true;
2473 }
2474}
2475
2476static bool is_valid_instruction_set(const std::string& instruction_set) {
2477 // TODO: add explicit whitelisting of instruction sets
2478 if (instruction_set.find('/') != std::string::npos) {
2479 LOG(ERROR) << "Invalid instruction set " << instruction_set;
2480 return false;
2481 } else {
2482 return true;
2483 }
2484}
2485
2486bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir,
2487 const char *apk_path, const char *instruction_set) {
2488 std::string oat_dir_ = oat_dir;
2489 std::string apk_path_ = apk_path;
2490 std::string instruction_set_ = instruction_set;
2491
2492 if (!is_absolute_path(oat_dir_)) return false;
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 res_ = oat_dir_ + '/' + instruction_set + '/'
2504 + apk_path_.substr(start + 1, end - start - 1) + ".odex";
2505 const char* res = res_.c_str();
2506 if (strlen(res) >= PKG_PATH_MAX) {
2507 LOG(ERROR) << "Result too large";
2508 return false;
2509 } else {
2510 strlcpy(path, res, PKG_PATH_MAX);
2511 return true;
2512 }
2513}
2514
2515bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path,
2516 const char *instruction_set) {
2517 std::string apk_path_ = apk_path;
2518 std::string instruction_set_ = instruction_set;
2519
2520 if (!is_absolute_path(apk_path_)) return false;
2521 if (!is_valid_instruction_set(instruction_set_)) return false;
2522
2523 std::string::size_type end = apk_path_.rfind('.');
2524 std::string::size_type start = apk_path_.rfind('/', end);
2525 if (end == std::string::npos || start == std::string::npos) {
2526 LOG(ERROR) << "Invalid apk_path " << apk_path_;
2527 return false;
2528 }
2529
2530 std::string oat_dir = apk_path_.substr(0, start + 1) + "oat";
2531 return calculate_oat_file_path_default(path, oat_dir.c_str(), apk_path, instruction_set);
2532}
2533
2534bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src,
2535 const char *instruction_set) {
2536 std::string src_ = src;
2537 std::string instruction_set_ = instruction_set;
2538
2539 if (!is_absolute_path(src_)) return false;
2540 if (!is_valid_instruction_set(instruction_set_)) return false;
2541
2542 for (auto it = src_.begin() + 1; it < src_.end(); ++it) {
2543 if (*it == '/') {
2544 *it = '@';
2545 }
2546 }
2547
2548 std::string res_ = android_data_dir + DALVIK_CACHE + '/' + instruction_set_ + src_
2549 + DALVIK_CACHE_POSTFIX;
2550 const char* res = res_.c_str();
2551 if (strlen(res) >= PKG_PATH_MAX) {
2552 LOG(ERROR) << "Result too large";
2553 return false;
2554 } else {
2555 strlcpy(path, res, PKG_PATH_MAX);
2556 return true;
2557 }
2558}
2559
Calin Juravle59f7ab82018-04-27 17:50:23 -07002560bool open_classpath_files(const std::string& classpath, std::vector<unique_fd>* apk_fds,
2561 std::vector<std::string>* dex_locations) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002562 std::vector<std::string> classpaths_elems = base::Split(classpath, ":");
2563 for (const std::string& elem : classpaths_elems) {
2564 unique_fd fd(TEMP_FAILURE_RETRY(open(elem.c_str(), O_RDONLY)));
2565 if (fd < 0) {
2566 PLOG(ERROR) << "Could not open classpath elem " << elem;
2567 return false;
2568 } else {
2569 apk_fds->push_back(std::move(fd));
Calin Juravle59f7ab82018-04-27 17:50:23 -07002570 dex_locations->push_back(elem);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002571 }
2572 }
2573 return true;
2574}
2575
2576static bool create_app_profile_snapshot(int32_t app_id,
2577 const std::string& package_name,
2578 const std::string& profile_name,
2579 const std::string& classpath) {
Calin Juravle29591732017-11-20 17:46:19 -08002580 int app_shared_gid = multiuser_get_shared_gid(/*user_id*/ 0, app_id);
2581
Alex Buynytskyy79dcab52021-11-19 11:43:04 -08002582 unique_fd snapshot_fd = open_snapshot_profile(AID_SYSTEM, package_name, profile_name);
Calin Juravle29591732017-11-20 17:46:19 -08002583 if (snapshot_fd < 0) {
2584 return false;
2585 }
2586
2587 std::vector<unique_fd> profiles_fd;
2588 unique_fd reference_profile_fd;
Calin Juravle824a64d2018-01-18 20:23:17 -08002589 open_profile_files(app_shared_gid, package_name, profile_name, /*is_secondary_dex*/ false,
2590 &profiles_fd, &reference_profile_fd);
Calin Juravle29591732017-11-20 17:46:19 -08002591 if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) {
2592 return false;
2593 }
2594
2595 profiles_fd.push_back(std::move(reference_profile_fd));
2596
Calin Juravle0d0a4922018-01-23 19:54:11 -08002597 // Open the class paths elements. These will be used to filter out profile data that does
2598 // not belong to the classpath during merge.
2599 std::vector<unique_fd> apk_fds;
Calin Juravle59f7ab82018-04-27 17:50:23 -07002600 std::vector<std::string> dex_locations;
2601 if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002602 return false;
2603 }
2604
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002605 RunProfman args;
Calin Juravlef85ddb92020-05-01 14:05:40 -07002606 // This is specifically a snapshot for an app, so don't use boot image profiles.
2607 args.SetupMerge(profiles_fd,
2608 snapshot_fd,
2609 apk_fds,
2610 dex_locations,
2611 /* for_snapshot= */ true,
2612 /* for_boot_image= */ false);
Calin Juravle29591732017-11-20 17:46:19 -08002613 pid_t pid = fork();
2614 if (pid == 0) {
2615 /* child -- drop privileges before continuing */
2616 drop_capabilities(app_shared_gid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002617 args.Exec();
Calin Juravle29591732017-11-20 17:46:19 -08002618 }
2619
2620 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +00002621 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
Calin Juravle29591732017-11-20 17:46:19 -08002622 if (!WIFEXITED(return_code)) {
Calin Juravle824a64d2018-01-18 20:23:17 -08002623 LOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
Jiakai Zhang495142a2022-02-21 19:38:14 +00002624 cleanup_output_fd(snapshot_fd.get());
Calin Juravle29591732017-11-20 17:46:19 -08002625 return false;
2626 }
2627
Calin Juravle78728f32019-11-08 17:55:46 -08002628 // Verify that profman finished successfully.
2629 int profman_code = WEXITSTATUS(return_code);
2630 if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) {
2631 LOG(WARNING) << "profman error for " << package_name << ":" << profile_name
2632 << ":" << profman_code;
2633 return false;
2634 }
Calin Juravle29591732017-11-20 17:46:19 -08002635 return true;
2636}
2637
Calin Juravle0d0a4922018-01-23 19:54:11 -08002638static bool create_boot_image_profile_snapshot(const std::string& package_name,
2639 const std::string& profile_name,
2640 const std::string& classpath) {
2641 // The reference profile directory for the android package might not be prepared. Do it now.
2642 const std::string ref_profile_dir =
2643 create_primary_reference_profile_package_dir_path(package_name);
2644 if (fs_prepare_dir(ref_profile_dir.c_str(), 0770, AID_SYSTEM, AID_SYSTEM) != 0) {
2645 PLOG(ERROR) << "Failed to prepare " << ref_profile_dir;
2646 return false;
2647 }
2648
Mathieu Chartiere0d64a12018-11-01 12:07:26 -07002649 // Return false for empty class path since it may otherwise return true below if profiles is
2650 // empty.
2651 if (classpath.empty()) {
2652 PLOG(ERROR) << "Class path is empty";
2653 return false;
2654 }
2655
Calin Juravle0d0a4922018-01-23 19:54:11 -08002656 // Open and create the snapshot profile.
Alex Buynytskyy79dcab52021-11-19 11:43:04 -08002657 unique_fd snapshot_fd = open_snapshot_profile(AID_SYSTEM, package_name, profile_name);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002658
2659 // Collect all non empty profiles.
2660 // The collection will traverse all applications profiles and find the non empty files.
2661 // This has the potential of inspecting a large number of files and directories (depending
2662 // on the number of applications and users). So there is a slight increase in the chance
2663 // to get get occasionally I/O errors (e.g. for opening the file). When that happens do not
2664 // fail the snapshot and aggregate whatever profile we could open.
2665 //
2666 // The profile snapshot is a best effort based on available data it's ok if some data
2667 // from some apps is missing. It will be counter productive for the snapshot to fail
2668 // because we could not open or read some of the files.
2669 std::vector<std::string> profiles;
2670 if (!collect_profiles(&profiles)) {
2671 LOG(WARNING) << "There were errors while collecting the profiles for the boot image.";
2672 }
2673
2674 // If we have no profiles return early.
2675 if (profiles.empty()) {
2676 return true;
2677 }
2678
2679 // Open the classpath elements. These will be used to filter out profile data that does
2680 // not belong to the classpath during merge.
2681 std::vector<unique_fd> apk_fds;
Calin Juravle59f7ab82018-04-27 17:50:23 -07002682 std::vector<std::string> dex_locations;
2683 if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002684 return false;
2685 }
2686
2687 // If we could not open any files from the classpath return an error.
2688 if (apk_fds.empty()) {
2689 LOG(ERROR) << "Could not open any of the classpath elements.";
2690 return false;
2691 }
2692
2693 // Aggregate the profiles in batches of kAggregationBatchSize.
2694 // We do this to avoid opening a huge a amount of files.
2695 static constexpr size_t kAggregationBatchSize = 10;
2696
Calin Juravle0d0a4922018-01-23 19:54:11 -08002697 for (size_t i = 0; i < profiles.size(); ) {
Calin Juravlea64fb512019-11-06 16:11:14 -08002698 std::vector<unique_fd> profiles_fd;
Calin Juravle0d0a4922018-01-23 19:54:11 -08002699 for (size_t k = 0; k < kAggregationBatchSize && i < profiles.size(); k++, i++) {
Calin Juravlee01f8712021-06-03 18:16:54 -07002700 unique_fd fd = open_profile(AID_SYSTEM, profiles[i], O_RDONLY, /*mode=*/ 0);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002701 if (fd.get() >= 0) {
2702 profiles_fd.push_back(std::move(fd));
2703 }
2704 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002705
2706 // We aggregate (read & write) into the same fd multiple times in a row.
2707 // We need to reset the cursor every time to ensure we read the whole file every time.
2708 if (TEMP_FAILURE_RETRY(lseek(snapshot_fd, 0, SEEK_SET)) == static_cast<off_t>(-1)) {
2709 PLOG(ERROR) << "Cannot reset position for snapshot profile";
2710 return false;
2711 }
2712
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002713 RunProfman args;
Calin Juravleb3a929d2018-12-11 14:40:00 -08002714 args.SetupMerge(profiles_fd,
2715 snapshot_fd,
2716 apk_fds,
Calin Juravle78728f32019-11-08 17:55:46 -08002717 dex_locations,
2718 /*for_snapshot=*/true,
2719 /*for_boot_image=*/true);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002720 pid_t pid = fork();
2721 if (pid == 0) {
2722 /* child -- drop privileges before continuing */
2723 drop_capabilities(AID_SYSTEM);
2724
Calin Juravle59f7ab82018-04-27 17:50:23 -07002725 // The introduction of new access flags into boot jars causes them to
2726 // fail dex file verification.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002727 args.Exec();
Calin Juravle0d0a4922018-01-23 19:54:11 -08002728 }
2729
2730 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +00002731 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
Calin Juravlea64fb512019-11-06 16:11:14 -08002732
Calin Juravle0d0a4922018-01-23 19:54:11 -08002733 if (!WIFEXITED(return_code)) {
2734 PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
Jiakai Zhang495142a2022-02-21 19:38:14 +00002735 cleanup_output_fd(snapshot_fd.get());
Calin Juravle0d0a4922018-01-23 19:54:11 -08002736 return false;
2737 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002738
2739 // Verify that profman finished successfully.
2740 int profman_code = WEXITSTATUS(return_code);
Calin Juravle78728f32019-11-08 17:55:46 -08002741 if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) {
2742 LOG(WARNING) << "profman error for " << package_name << ":" << profile_name
2743 << ":" << profman_code;
2744 return false;
Calin Juravlea64fb512019-11-06 16:11:14 -08002745 }
Calin Juravle0d0a4922018-01-23 19:54:11 -08002746 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002747
Calin Juravle0d0a4922018-01-23 19:54:11 -08002748 return true;
2749}
2750
2751bool create_profile_snapshot(int32_t app_id, const std::string& package_name,
2752 const std::string& profile_name, const std::string& classpath) {
2753 if (app_id == -1) {
2754 return create_boot_image_profile_snapshot(package_name, profile_name, classpath);
2755 } else {
2756 return create_app_profile_snapshot(app_id, package_name, profile_name, classpath);
2757 }
2758}
2759
Yang Tianping8f2a40e2021-10-29 14:39:02 +08002760static bool check_profile_exists_in_dexmetadata(const std::string& dex_metadata) {
2761 ZipArchiveHandle zip = nullptr;
2762 if (OpenArchive(dex_metadata.c_str(), &zip) != 0) {
2763 PLOG(ERROR) << "Failed to open dm '" << dex_metadata << "'";
2764 return false;
2765 }
2766
2767 ZipEntry64 entry;
2768 int result = FindEntry(zip, "primary.prof", &entry);
2769 CloseArchive(zip);
2770
2771 return result != 0 ? false : true;
2772}
2773
Calin Juravlec3b049e2018-01-18 22:32:58 -08002774bool prepare_app_profile(const std::string& package_name,
2775 userid_t user_id,
2776 appid_t app_id,
2777 const std::string& profile_name,
Calin Juravlef63d4792018-01-30 17:43:34 +00002778 const std::string& code_path,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002779 const std::optional<std::string>& dex_metadata) {
Jiakai Zhangf515a1b2022-02-23 18:33:26 +00002780 if (user_id != USER_NULL) {
2781 if (user_id < 0) {
2782 LOG(ERROR) << "Unexpected user ID " << user_id;
2783 return false;
2784 }
2785
2786 // Prepare the current profile.
2787 std::string cur_profile = create_current_profile_path(user_id, package_name, profile_name,
2788 /*is_secondary_dex*/ false);
2789 uid_t uid = multiuser_get_uid(user_id, app_id);
2790 if (fs_prepare_file_strict(cur_profile.c_str(), 0600, uid, uid) != 0) {
2791 PLOG(ERROR) << "Failed to prepare " << cur_profile;
2792 return false;
2793 }
2794 } else {
2795 // Prepare the reference profile as the system user.
2796 user_id = USER_SYSTEM;
Calin Juravlec3b049e2018-01-18 22:32:58 -08002797 }
2798
2799 // Check if we need to install the profile from the dex metadata.
Yang Tianping8f2a40e2021-10-29 14:39:02 +08002800 if (!dex_metadata || !check_profile_exists_in_dexmetadata(dex_metadata->c_str())) {
Calin Juravlec3b049e2018-01-18 22:32:58 -08002801 return true;
2802 }
2803
2804 // We have a dex metdata. Merge the profile into the reference profile.
Jiakai Zhangf515a1b2022-02-23 18:33:26 +00002805 unique_fd ref_profile_fd =
2806 open_reference_profile(multiuser_get_uid(user_id, app_id), package_name, profile_name,
2807 /*read_write*/ true, /*is_secondary_dex*/ false);
Calin Juravlec3b049e2018-01-18 22:32:58 -08002808 unique_fd dex_metadata_fd(TEMP_FAILURE_RETRY(
2809 open(dex_metadata->c_str(), O_RDONLY | O_NOFOLLOW)));
Calin Juravlef63d4792018-01-30 17:43:34 +00002810 unique_fd apk_fd(TEMP_FAILURE_RETRY(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW)));
2811 if (apk_fd < 0) {
2812 PLOG(ERROR) << "Could not open code path " << code_path;
2813 return false;
2814 }
Calin Juravlec3b049e2018-01-18 22:32:58 -08002815
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002816 RunProfman args;
Jiakai Zhang495142a2022-02-21 19:38:14 +00002817 args.SetupCopyAndUpdate(dex_metadata_fd,
2818 ref_profile_fd,
2819 apk_fd,
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002820 code_path);
Calin Juravlec3b049e2018-01-18 22:32:58 -08002821 pid_t pid = fork();
2822 if (pid == 0) {
2823 /* child -- drop privileges before continuing */
2824 gid_t app_shared_gid = multiuser_get_shared_gid(user_id, app_id);
2825 drop_capabilities(app_shared_gid);
2826
Calin Juravlef63d4792018-01-30 17:43:34 +00002827 // The copy and update takes ownership over the fds.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002828 args.Exec();
Calin Juravlec3b049e2018-01-18 22:32:58 -08002829 }
2830
2831 /* parent */
Jiakai Zhang495142a2022-02-21 19:38:14 +00002832 int return_code = wait_child_with_timeout(pid, kShortTimeoutMs);
Calin Juravlec3b049e2018-01-18 22:32:58 -08002833 if (!WIFEXITED(return_code)) {
2834 PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
Jiakai Zhang495142a2022-02-21 19:38:14 +00002835 cleanup_output_fd(ref_profile_fd.get());
Calin Juravlec3b049e2018-01-18 22:32:58 -08002836 return false;
2837 }
2838 return true;
2839}
2840
Jiakai Zhangf515a1b2022-02-23 18:33:26 +00002841int get_odex_visibility(const char* apk_path, const char* instruction_set, const char* oat_dir) {
2842 char oat_path[PKG_PATH_MAX];
2843 if (!create_oat_out_path(apk_path, instruction_set, oat_dir, /*is_secondary_dex=*/false,
2844 oat_path)) {
2845 return -1;
2846 }
2847 struct stat st;
2848 if (stat(oat_path, &st) == -1) {
2849 if (errno == ENOENT) {
2850 return ODEX_NOT_FOUND;
2851 }
2852 PLOG(ERROR) << "Could not stat " << oat_path;
2853 return -1;
2854 }
2855 return (st.st_mode & S_IROTH) ? ODEX_IS_PUBLIC : ODEX_IS_PRIVATE;
2856}
2857
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07002858} // namespace installd
2859} // namespace android