blob: 9566cf66f63c4c7c2eae897699e3fc01623b5a6d [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
Alan Stokesa25d90c2017-10-16 10:56:00 +010018#include <array>
Jeff Sharkey90aff262016-12-12 14:28:24 -070019#include <fcntl.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
Andreas Gampe023b2242018-02-28 16:03:25 -080031#include <iomanip>
32
Alan Stokesa25d90c2017-10-16 10:56:00 +010033#include <android-base/file.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070034#include <android-base/logging.h>
Andreas Gampe6a9cf722017-07-24 16:49:10 -070035#include <android-base/properties.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070036#include <android-base/stringprintf.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070037#include <android-base/strings.h>
38#include <android-base/unique_fd.h>
Calin Juravle80a21252017-01-17 14:43:25 -080039#include <cutils/fs.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070040#include <cutils/properties.h>
41#include <cutils/sched_policy.h>
Andreas Gampefa2dadd2018-02-28 19:52:47 -080042#include <dex2oat_return_codes.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070043#include <log/log.h> // TODO: Move everything to base/logging.
Alan Stokesa25d90c2017-10-16 10:56:00 +010044#include <openssl/sha.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070045#include <private/android_filesystem_config.h>
Suren Baghdasaryan1cc5de62019-01-25 05:29:23 +000046#include <processgroup/sched_policy.h>
Calin Juravlecb556e32017-04-04 20:22:50 -070047#include <selinux/android.h>
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +000048#include <server_configurable_flags/get_flags.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070049#include <system/thread_defs.h>
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070050
51#include "dexopt.h"
Andreas Gampefa2dadd2018-02-28 19:52:47 -080052#include "dexopt_return_codes.h"
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060053#include "globals.h"
Jeff Sharkey90aff262016-12-12 14:28:24 -070054#include "installd_deps.h"
55#include "otapreopt_utils.h"
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070056#include "utils.h"
57
Jeff Sharkey90aff262016-12-12 14:28:24 -070058using android::base::EndsWith;
Mathieu Chartier9b2da082018-10-26 13:23:11 -070059using android::base::GetBoolProperty;
60using android::base::GetProperty;
David Brazdil4f6027a2019-03-19 11:44:21 +000061using android::base::ReadFdToString;
Alan Stokesa25d90c2017-10-16 10:56:00 +010062using android::base::ReadFully;
63using android::base::StringPrintf;
64using android::base::WriteFully;
Calin Juravle1a0af3b2017-03-09 14:33:33 -080065using android::base::unique_fd;
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070066
67namespace android {
68namespace installd {
69
Andreas Gampe2a2d7ba2017-11-02 19:39:29 -070070// Should minidebug info be included in compiled artifacts? Even if this value is
71// "true," usage might still be conditional to other constraints, e.g., system
72// property overrides.
73static constexpr bool kEnableMinidebugInfo = true;
74
75static constexpr const char* kMinidebugInfoSystemProperty = "dalvik.vm.dex2oat-minidebuginfo";
76static constexpr bool kMinidebugInfoSystemPropertyDefault = false;
77static constexpr const char* kMinidebugDex2oatFlag = "--generate-mini-debug-info";
Mathieu Chartierb41ffcc2018-01-09 00:02:17 -080078static constexpr const char* kDisableCompactDexFlag = "--compact-dex-level=none";
Andreas Gampe2a2d7ba2017-11-02 19:39:29 -070079
Andreas Gampefa2dadd2018-02-28 19:52:47 -080080
Calin Juravle114f0812017-03-08 19:05:07 -080081// Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below.
82struct FreeDelete {
83 // NOTE: Deleting a const object is valid but free() takes a non-const pointer.
84 void operator()(const void* ptr) const {
85 free(const_cast<void*>(ptr));
86 }
87};
88
89// Alias for std::unique_ptr<> that uses the C function free() to delete objects.
90template <typename T>
91using UniqueCPtr = std::unique_ptr<T, FreeDelete>;
92
Calin Juravle1a0af3b2017-03-09 14:33:33 -080093static unique_fd invalid_unique_fd() {
94 return unique_fd(-1);
95}
96
Andreas Gampe6a9cf722017-07-24 16:49:10 -070097static bool is_debug_runtime() {
98 return android::base::GetProperty("persist.sys.dalvik.vm.lib.2", "") == "libartd.so";
99}
100
David Sehra3b5ab62017-10-25 14:27:29 -0700101static bool is_debuggable_build() {
102 return android::base::GetBoolProperty("ro.debuggable", false);
103}
104
Jeff Sharkey90aff262016-12-12 14:28:24 -0700105static bool clear_profile(const std::string& profile) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800106 unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700107 if (ufd.get() < 0) {
108 if (errno != ENOENT) {
109 PLOG(WARNING) << "Could not open profile " << profile;
110 return false;
111 } else {
112 // Nothing to clear. That's ok.
113 return true;
114 }
115 }
116
117 if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) {
118 if (errno != EWOULDBLOCK) {
119 PLOG(WARNING) << "Error locking profile " << profile;
120 }
121 // This implies that the app owning this profile is running
122 // (and has acquired the lock).
123 //
124 // If we can't acquire the lock bail out since clearing is useless anyway
125 // (the app will write again to the profile).
126 //
127 // Note:
128 // This does not impact the this is not an issue for the profiling correctness.
129 // In case this is needed because of an app upgrade, profiles will still be
130 // eventually cleared by the app itself due to checksum mismatch.
131 // If this is needed because profman advised, then keeping the data around
132 // until the next run is again not an issue.
133 //
134 // If the app attempts to acquire a lock while we've held one here,
135 // it will simply skip the current write cycle.
136 return false;
137 }
138
139 bool truncated = ftruncate(ufd.get(), 0) == 0;
140 if (!truncated) {
141 PLOG(WARNING) << "Could not truncate " << profile;
142 }
143 if (flock(ufd.get(), LOCK_UN) != 0) {
144 PLOG(WARNING) << "Error unlocking profile " << profile;
145 }
146 return truncated;
147}
148
Calin Juravle114f0812017-03-08 19:05:07 -0800149// Clear the reference profile for the given location.
Calin Juravle824a64d2018-01-18 20:23:17 -0800150// The location is the profile name for primary apks or the dex path for secondary dex files.
151static bool clear_reference_profile(const std::string& package_name, const std::string& location,
152 bool is_secondary_dex) {
153 return clear_profile(create_reference_profile_path(package_name, location, is_secondary_dex));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700154}
155
Calin Juravle114f0812017-03-08 19:05:07 -0800156// Clear the reference profile for the given location.
Calin Juravle824a64d2018-01-18 20:23:17 -0800157// The location is the profile name for primary apks or the dex path for secondary dex files.
158static bool clear_current_profile(const std::string& package_name, const std::string& location,
159 userid_t user, bool is_secondary_dex) {
160 return clear_profile(create_current_profile_path(user, package_name, location,
161 is_secondary_dex));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700162}
163
Calin Juravle114f0812017-03-08 19:05:07 -0800164// Clear the reference profile for the primary apk of the given package.
Calin Juravle824a64d2018-01-18 20:23:17 -0800165// The location is the profile name for primary apks or the dex path for secondary dex files.
166bool clear_primary_reference_profile(const std::string& package_name,
167 const std::string& location) {
168 return clear_reference_profile(package_name, location, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800169}
170
171// Clear all current profile for the primary apk of the given package.
Calin Juravle824a64d2018-01-18 20:23:17 -0800172// The location is the profile name for primary apks or the dex path for secondary dex files.
173bool clear_primary_current_profiles(const std::string& package_name, const std::string& location) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700174 bool success = true;
Calin Juravle114f0812017-03-08 19:05:07 -0800175 // For secondary dex files, we don't really need the user but we use it for sanity checks.
Jeff Sharkey90aff262016-12-12 14:28:24 -0700176 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
177 for (auto user : users) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800178 success &= clear_current_profile(package_name, location, user, /*is_secondary_dex*/false);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700179 }
180 return success;
181}
182
Calin Juravle114f0812017-03-08 19:05:07 -0800183// Clear the current profile for the primary apk of the given package and user.
Calin Juravle824a64d2018-01-18 20:23:17 -0800184bool clear_primary_current_profile(const std::string& package_name, const std::string& location,
185 userid_t user) {
186 return clear_current_profile(package_name, location, user, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800187}
188
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700189static std::vector<std::string> SplitBySpaces(const std::string& str) {
190 if (str.empty()) {
191 return {};
192 }
193 return android::base::Split(str, " ");
Jeff Sharkey90aff262016-12-12 14:28:24 -0700194}
195
Jeff Hao10b8a6e2017-04-05 17:11:39 -0700196static const char* get_location_from_path(const char* path) {
197 static constexpr char kLocationSeparator = '/';
198 const char *location = strrchr(path, kLocationSeparator);
Yi Kong954cf642018-07-17 16:16:24 -0700199 if (location == nullptr) {
Jeff Hao10b8a6e2017-04-05 17:11:39 -0700200 return path;
201 } else {
202 // Skip the separator character.
203 return location + 1;
204 }
205}
206
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800207// ExecVHelper prepares and holds pointers to parsed command line arguments so that no allocations
208// need to be performed between the fork and exec.
209class ExecVHelper {
210 public:
211 // Store a placeholder for the binary name.
212 ExecVHelper() : args_(1u, std::string()) {}
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800213
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800214 void PrepareArgs(const std::string& bin) {
215 CHECK(!args_.empty());
216 CHECK(args_[0].empty());
217 args_[0] = bin;
218 // Write char* into array.
219 for (const std::string& arg : args_) {
220 argv_.push_back(arg.c_str());
221 }
222 argv_.push_back(nullptr); // Add null terminator.
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800223 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800224
225 [[ noreturn ]]
226 void Exec(int exit_code) {
227 execv(argv_[0], (char * const *)&argv_[0]);
228 PLOG(ERROR) << "execv(" << argv_[0] << ") failed";
229 exit(exit_code);
230 }
231
232 // Add an arg if it's not empty.
233 void AddArg(const std::string& arg) {
234 if (!arg.empty()) {
235 args_.push_back(arg);
236 }
237 }
238
239 // Add a runtime arg if it's not empty.
240 void AddRuntimeArg(const std::string& arg) {
241 if (!arg.empty()) {
242 args_.push_back("--runtime-arg");
243 args_.push_back(arg);
244 }
245 }
246
247 protected:
248 // Holder arrays for backing arg storage.
249 std::vector<std::string> args_;
250
251 // Argument poiners.
252 std::vector<const char*> argv_;
253};
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700254
255static std::string MapPropertyToArg(const std::string& property,
256 const std::string& format,
257 const std::string& default_value = "") {
258 std::string prop = GetProperty(property, default_value);
259 if (!prop.empty()) {
260 return StringPrintf(format.c_str(), prop.c_str());
261 }
262 return "";
263}
264
Patrick Baumann2271b3e2020-04-14 17:03:00 -0700265static std::string MapPropertyToArgWithBackup(const std::string& property,
266 const std::string& backupProperty,
267 const std::string& format,
268 const std::string& default_value = "") {
269 std::string value = GetProperty(property, default_value);
270 if (!value.empty()) {
271 return StringPrintf(format.c_str(), value.c_str());
272 }
273 return MapPropertyToArg(backupProperty, format, default_value);
274}
275
Calin Juravlef74a7372019-02-28 20:29:41 -0800276// Determines which binary we should use for execution (the debug or non-debug version).
277// e.g. dex2oatd vs dex2oat
278static const char* select_execution_binary(const char* binary, const char* debug_binary,
279 bool background_job_compile) {
280 return select_execution_binary(
281 binary,
282 debug_binary,
283 background_job_compile,
284 is_debug_runtime(),
285 (android::base::GetProperty("ro.build.version.codename", "") == "REL"),
286 is_debuggable_build());
287}
288
289// Determines which binary we should use for execution (the debug or non-debug version).
290// e.g. dex2oatd vs dex2oat
291// This is convenient method which is much easier to test because it doesn't read
292// system properties.
293const char* select_execution_binary(
294 const char* binary,
295 const char* debug_binary,
296 bool background_job_compile,
297 bool is_debug_runtime,
298 bool is_release,
299 bool is_debuggable_build) {
300 // Do not use debug binaries for release candidates (to give more soak time).
301 bool is_debug_bg_job = background_job_compile && is_debuggable_build && !is_release;
302
303 // If the runtime was requested to use libartd.so, we'll run the debug version - assuming
304 // the file is present (it may not be on images with very little space available).
305 bool useDebug = (is_debug_runtime || is_debug_bg_job) && (access(debug_binary, X_OK) == 0);
306
307 return useDebug ? debug_binary : binary;
308}
309
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000310// Namespace for Android Runtime flags applied during boot time.
311static const char* RUNTIME_NATIVE_BOOT_NAMESPACE = "runtime_native_boot";
312// Feature flag name for running the JIT in Zygote experiment, b/119800099.
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000313static const char* ENABLE_JITZYGOTE_IMAGE = "enable_apex_image";
314// Location of the JIT Zygote image.
315static const char* kJitZygoteImage =
316 "boot.art:/nonx/boot-framework.art!/system/etc/boot-image.prof";
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000317
Mathieu Chartiere97261e2019-10-01 15:36:01 -0700318// Phenotype property name for enabling profiling the boot class path.
319static const char* PROFILE_BOOT_CLASS_PATH = "profilebootclasspath";
320
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800321class RunDex2Oat : public ExecVHelper {
322 public:
323 RunDex2Oat(int zip_fd,
324 int oat_fd,
325 int input_vdex_fd,
326 int output_vdex_fd,
327 int image_fd,
328 const char* input_file_name,
329 const char* output_file_name,
330 int swap_fd,
331 const char* instruction_set,
332 const char* compiler_filter,
333 bool debuggable,
334 bool post_bootcomplete,
Patrick Baumann2271b3e2020-04-14 17:03:00 -0700335 bool for_restore,
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800336 bool background_job_compile,
337 int profile_fd,
338 const char* class_loader_context,
David Brazdil4f6027a2019-03-19 11:44:21 +0000339 const std::string& class_loader_context_fds,
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800340 int target_sdk_version,
341 bool enable_hidden_api_checks,
342 bool generate_compact_dex,
343 int dex_metadata_fd,
344 const char* compilation_reason) {
345 // Get the relative path to the input file.
346 const char* relative_input_file_name = get_location_from_path(input_file_name);
Jeff Hao10b8a6e2017-04-05 17:11:39 -0700347
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800348 std::string dex2oat_Xms_arg = MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s");
349 std::string dex2oat_Xmx_arg = MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s");
Jeff Sharkey90aff262016-12-12 14:28:24 -0700350
Patrick Baumann2271b3e2020-04-14 17:03:00 -0700351 std::string threads_format = "-j%s";
352 std::string dex2oat_threads_arg = post_bootcomplete
353 ? (for_restore
354 ? MapPropertyToArgWithBackup(
355 "dalvik.vm.restore-dex2oat-threads",
356 "dalvik.vm.dex2oat-threads",
357 threads_format)
358 : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format))
359 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
360 std::string cpu_set_format = "--cpu-set=%s";
361 std::string dex2oat_cpu_set_arg = post_bootcomplete
362 ? (for_restore
363 ? MapPropertyToArgWithBackup(
364 "dalvik.vm.restore-dex2oat-cpu-set",
365 "dalvik.vm.dex2oat-cpu-set",
366 cpu_set_format)
367 : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format))
368 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700369
Nicolas Geoffrayab0a1902019-02-22 21:42:45 +0000370 std::string bootclasspath;
371 char* dex2oat_bootclasspath = getenv("DEX2OATBOOTCLASSPATH");
372 if (dex2oat_bootclasspath != nullptr) {
373 bootclasspath = StringPrintf("-Xbootclasspath:%s", dex2oat_bootclasspath);
374 }
375 // If DEX2OATBOOTCLASSPATH is not in the environment, dex2oat is going to query
376 // BOOTCLASSPATH.
377
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800378 const std::string dex2oat_isa_features_key =
379 StringPrintf("dalvik.vm.isa.%s.features", instruction_set);
380 std::string instruction_set_features_arg =
381 MapPropertyToArg(dex2oat_isa_features_key, "--instruction-set-features=%s");
Jeff Sharkey90aff262016-12-12 14:28:24 -0700382
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800383 const std::string dex2oat_isa_variant_key =
384 StringPrintf("dalvik.vm.isa.%s.variant", instruction_set);
385 std::string instruction_set_variant_arg =
386 MapPropertyToArg(dex2oat_isa_variant_key, "--instruction-set-variant=%s");
Jeff Sharkey90aff262016-12-12 14:28:24 -0700387
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800388 const char* dex2oat_norelocation = "-Xnorelocate";
Jeff Sharkey90aff262016-12-12 14:28:24 -0700389
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800390 const std::string dex2oat_flags = GetProperty("dalvik.vm.dex2oat-flags", "");
391 std::vector<std::string> dex2oat_flags_args = SplitBySpaces(dex2oat_flags);
392 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags.c_str());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700393
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800394 // If we are booting without the real /data, don't spend time compiling.
395 std::string vold_decrypt = GetProperty("vold.decrypt", "");
396 bool skip_compilation = vold_decrypt == "trigger_restart_min_framework" ||
397 vold_decrypt == "1";
Jeff Sharkey90aff262016-12-12 14:28:24 -0700398
Vladimir Marko79dc3c62020-04-06 10:38:58 +0100399 std::string updatable_bcp_packages =
400 MapPropertyToArg("dalvik.vm.dex2oat-updatable-bcp-packages-file",
401 "--updatable-bcp-packages-file=%s");
402 if (updatable_bcp_packages.empty()) {
403 // Make dex2oat fail by providing non-existent file name.
404 updatable_bcp_packages = "--updatable-bcp-packages-file=/nonx/updatable-bcp-packages.txt";
405 }
406
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800407 std::string resolve_startup_string_arg =
408 MapPropertyToArg("persist.device_config.runtime.dex2oat_resolve_startup_strings",
409 "--resolve-startup-const-strings=%s");
410 if (resolve_startup_string_arg.empty()) {
411 // If empty, fall back to system property.
412 resolve_startup_string_arg =
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800413 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
414 "--resolve-startup-const-strings=%s");
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800415 }
Mathieu Chartier5880c032018-11-28 19:15:41 -0800416
417 const std::string image_block_size_arg =
418 MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
419 "--max-image-block-size=%s");
420
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800421 const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700422
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800423 std::string image_format_arg;
424 if (image_fd >= 0) {
425 image_format_arg = MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s");
Mathieu Chartier31636522018-11-09 23:53:07 +0000426 }
Mathieu Chartier31636522018-11-09 23:53:07 +0000427
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800428 std::string dex2oat_large_app_threshold_arg =
429 MapPropertyToArg("dalvik.vm.dex2oat-very-large", "--very-large-app-threshold=%s");
Mathieu Chartier31636522018-11-09 23:53:07 +0000430
Calin Juravlef74a7372019-02-28 20:29:41 -0800431
432 const char* dex2oat_bin = select_execution_binary(
433 kDex2oatPath, kDex2oatDebugPath, background_job_compile);
Mathieu Chartier31636522018-11-09 23:53:07 +0000434
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800435 bool generate_minidebug_info = kEnableMinidebugInfo &&
436 GetBoolProperty(kMinidebugInfoSystemProperty, kMinidebugInfoSystemPropertyDefault);
Mathieu Chartier31636522018-11-09 23:53:07 +0000437
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000438 std::string boot_image;
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000439 std::string use_jitzygote_image =
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000440 server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE,
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000441 ENABLE_JITZYGOTE_IMAGE,
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000442 /*default_value=*/ "");
Mathieu Chartiere97261e2019-10-01 15:36:01 -0700443
444 std::string profile_boot_class_path = GetProperty("dalvik.vm.profilebootclasspath", "");
445 profile_boot_class_path =
446 server_configurable_flags::GetServerConfigurableFlag(
447 RUNTIME_NATIVE_BOOT_NAMESPACE,
448 PROFILE_BOOT_CLASS_PATH,
449 /*default_value=*/ profile_boot_class_path);
450
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000451 if (use_jitzygote_image == "true" || profile_boot_class_path == "true") {
452 boot_image = StringPrintf("--boot-image=%s", kJitZygoteImage);
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000453 } else {
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000454 boot_image = MapPropertyToArg("dalvik.vm.boot-image", "--boot-image=%s");
Nicolas Geoffrayaaad21e2019-02-25 13:31:10 +0000455 }
Nicolas Geoffray8b3fa972019-02-14 15:57:47 +0000456
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800457 // clang FORTIFY doesn't let us use strlen in constant array bounds, so we
458 // use arraysize instead.
459 std::string zip_fd_arg = StringPrintf("--zip-fd=%d", zip_fd);
460 std::string zip_location_arg = StringPrintf("--zip-location=%s", relative_input_file_name);
461 std::string input_vdex_fd_arg = StringPrintf("--input-vdex-fd=%d", input_vdex_fd);
462 std::string output_vdex_fd_arg = StringPrintf("--output-vdex-fd=%d", output_vdex_fd);
463 std::string oat_fd_arg = StringPrintf("--oat-fd=%d", oat_fd);
464 std::string oat_location_arg = StringPrintf("--oat-location=%s", output_file_name);
465 std::string instruction_set_arg = StringPrintf("--instruction-set=%s", instruction_set);
466 std::string dex2oat_compiler_filter_arg;
467 std::string dex2oat_swap_fd;
468 std::string dex2oat_image_fd;
469 std::string target_sdk_version_arg;
470 if (target_sdk_version != 0) {
David Brazdilccfb3cf2019-02-20 10:39:34 +0000471 target_sdk_version_arg = StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800472 }
473 std::string class_loader_context_arg;
David Brazdil4f6027a2019-03-19 11:44:21 +0000474 std::string class_loader_context_fds_arg;
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800475 if (class_loader_context != nullptr) {
476 class_loader_context_arg = StringPrintf("--class-loader-context=%s",
477 class_loader_context);
David Brazdil4f6027a2019-03-19 11:44:21 +0000478 if (!class_loader_context_fds.empty()) {
479 class_loader_context_fds_arg = StringPrintf("--class-loader-context-fds=%s",
480 class_loader_context_fds.c_str());
481 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800482 }
Mathieu Chartier31636522018-11-09 23:53:07 +0000483
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800484 if (swap_fd >= 0) {
485 dex2oat_swap_fd = StringPrintf("--swap-fd=%d", swap_fd);
486 }
487 if (image_fd >= 0) {
488 dex2oat_image_fd = StringPrintf("--app-image-fd=%d", image_fd);
489 }
Mathieu Chartier31636522018-11-09 23:53:07 +0000490
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800491 // Compute compiler filter.
492 bool have_dex2oat_relocation_skip_flag = false;
493 if (skip_compilation) {
494 dex2oat_compiler_filter_arg = "--compiler-filter=extract";
495 have_dex2oat_relocation_skip_flag = true;
496 } else if (compiler_filter != nullptr) {
497 dex2oat_compiler_filter_arg = StringPrintf("--compiler-filter=%s", compiler_filter);
498 }
Mathieu Chartier31636522018-11-09 23:53:07 +0000499
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800500 if (dex2oat_compiler_filter_arg.empty()) {
501 dex2oat_compiler_filter_arg = MapPropertyToArg("dalvik.vm.dex2oat-filter",
502 "--compiler-filter=%s");
503 }
504
505 // Check whether all apps should be compiled debuggable.
506 if (!debuggable) {
507 debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
508 }
509 std::string profile_arg;
510 if (profile_fd != -1) {
511 profile_arg = StringPrintf("--profile-file-fd=%d", profile_fd);
512 }
513
514 // Get the directory of the apk to pass as a base classpath directory.
515 std::string base_dir;
516 std::string apk_dir(input_file_name);
517 unsigned long dir_index = apk_dir.rfind('/');
518 bool has_base_dir = dir_index != std::string::npos;
519 if (has_base_dir) {
520 apk_dir = apk_dir.substr(0, dir_index);
521 base_dir = StringPrintf("--classpath-dir=%s", apk_dir.c_str());
522 }
523
524 std::string dex_metadata_fd_arg = "--dm-fd=" + std::to_string(dex_metadata_fd);
525
526 std::string compilation_reason_arg = compilation_reason == nullptr
527 ? ""
528 : std::string("--compilation-reason=") + compilation_reason;
529
530 ALOGV("Running %s in=%s out=%s\n", dex2oat_bin, relative_input_file_name, output_file_name);
531
532 // Disable cdex if update input vdex is true since this combination of options is not
533 // supported.
534 const bool disable_cdex = !generate_compact_dex || (input_vdex_fd == output_vdex_fd);
535
536 AddArg(zip_fd_arg);
537 AddArg(zip_location_arg);
538 AddArg(input_vdex_fd_arg);
539 AddArg(output_vdex_fd_arg);
540 AddArg(oat_fd_arg);
541 AddArg(oat_location_arg);
542 AddArg(instruction_set_arg);
543
544 AddArg(instruction_set_variant_arg);
545 AddArg(instruction_set_features_arg);
546
Nicolas Geoffray5a4c4e92020-02-07 11:37:34 +0000547 AddArg(boot_image);
548
Nicolas Geoffrayab0a1902019-02-22 21:42:45 +0000549 AddRuntimeArg(bootclasspath);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800550 AddRuntimeArg(dex2oat_Xms_arg);
551 AddRuntimeArg(dex2oat_Xmx_arg);
552
Vladimir Marko79dc3c62020-04-06 10:38:58 +0100553 AddArg(updatable_bcp_packages);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800554 AddArg(resolve_startup_string_arg);
Mathieu Chartier5880c032018-11-28 19:15:41 -0800555 AddArg(image_block_size_arg);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800556 AddArg(dex2oat_compiler_filter_arg);
557 AddArg(dex2oat_threads_arg);
Orion Hodson45837462019-11-14 18:20:17 +0000558 AddArg(dex2oat_cpu_set_arg);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800559 AddArg(dex2oat_swap_fd);
560 AddArg(dex2oat_image_fd);
561
562 if (generate_debug_info) {
563 AddArg("--generate-debug-info");
564 }
565 if (debuggable) {
566 AddArg("--debuggable");
567 }
568 AddArg(image_format_arg);
569 AddArg(dex2oat_large_app_threshold_arg);
570
571 if (have_dex2oat_relocation_skip_flag) {
572 AddRuntimeArg(dex2oat_norelocation);
573 }
574 AddArg(profile_arg);
575 AddArg(base_dir);
576 AddArg(class_loader_context_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +0000577 AddArg(class_loader_context_fds_arg);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800578 if (generate_minidebug_info) {
579 AddArg(kMinidebugDex2oatFlag);
580 }
581 if (disable_cdex) {
582 AddArg(kDisableCompactDexFlag);
583 }
David Brazdilccfb3cf2019-02-20 10:39:34 +0000584 AddRuntimeArg(target_sdk_version_arg);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800585 if (enable_hidden_api_checks) {
David Brazdil36133d12019-04-12 11:08:44 +0100586 AddRuntimeArg("-Xhidden-api-policy:enabled");
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800587 }
588
589 if (dex_metadata_fd > -1) {
590 AddArg(dex_metadata_fd_arg);
591 }
592
593 AddArg(compilation_reason_arg);
594
595 // Do not add args after dex2oat_flags, they should override others for debugging.
596 args_.insert(args_.end(), dex2oat_flags_args.begin(), dex2oat_flags_args.end());
597
598 PrepareArgs(dex2oat_bin);
Mathieu Chartier31636522018-11-09 23:53:07 +0000599 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800600};
Jeff Sharkey90aff262016-12-12 14:28:24 -0700601
602/*
603 * Whether dexopt should use a swap file when compiling an APK.
604 *
605 * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
606 * itself, anyways).
607 *
608 * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
609 *
610 * Otherwise, return true if this is a low-mem device.
611 *
612 * Otherwise, return default value.
613 */
614static bool kAlwaysProvideSwapFile = false;
615static bool kDefaultProvideSwapFile = true;
616
617static bool ShouldUseSwapFileForDexopt() {
618 if (kAlwaysProvideSwapFile) {
619 return true;
620 }
621
622 // Check the "override" property. If it exists, return value == "true".
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700623 std::string dex2oat_prop_buf = GetProperty("dalvik.vm.dex2oat-swap", "");
624 if (!dex2oat_prop_buf.empty()) {
625 return dex2oat_prop_buf == "true";
Jeff Sharkey90aff262016-12-12 14:28:24 -0700626 }
627
628 // Shortcut for default value. This is an implementation optimization for the process sketched
629 // above. If the default value is true, we can avoid to check whether this is a low-mem device,
630 // as low-mem is never returning false. The compiler will optimize this away if it can.
631 if (kDefaultProvideSwapFile) {
632 return true;
633 }
634
Mathieu Chartier9b2da082018-10-26 13:23:11 -0700635 if (GetBoolProperty("ro.config.low_ram", false)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700636 return true;
637 }
638
639 // Default value must be false here.
640 return kDefaultProvideSwapFile;
641}
642
Richard Uhler76cc0272016-12-08 10:46:35 +0000643static void SetDex2OatScheduling(bool set_to_bg) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700644 if (set_to_bg) {
645 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800646 PLOG(ERROR) << "set_sched_policy failed";
647 exit(DexoptReturnCodes::kSetSchedPolicy);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700648 }
649 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800650 PLOG(ERROR) << "setpriority failed";
651 exit(DexoptReturnCodes::kSetPriority);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700652 }
653 }
654}
655
Calin Juravle29591732017-11-20 17:46:19 -0800656static unique_fd create_profile(uid_t uid, const std::string& profile, int32_t flags) {
657 unique_fd fd(TEMP_FAILURE_RETRY(open(profile.c_str(), flags, 0600)));
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800658 if (fd.get() < 0) {
Calin Juravle29591732017-11-20 17:46:19 -0800659 if (errno != EEXIST) {
Calin Juravle114f0812017-03-08 19:05:07 -0800660 PLOG(ERROR) << "Failed to create profile " << profile;
Calin Juravle29591732017-11-20 17:46:19 -0800661 return invalid_unique_fd();
Calin Juravle114f0812017-03-08 19:05:07 -0800662 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700663 }
Calin Juravle114f0812017-03-08 19:05:07 -0800664 // Profiles should belong to the app; make sure of that by giving ownership to
665 // the app uid. If we cannot do that, there's no point in returning the fd
666 // since dex2oat/profman will fail with SElinux denials.
667 if (fchown(fd.get(), uid, uid) < 0) {
Roland Levillain019db5b2019-03-14 14:31:59 +0000668 PLOG(ERROR) << "Could not chown profile " << profile;
Calin Juravle29591732017-11-20 17:46:19 -0800669 return invalid_unique_fd();
Calin Juravle114f0812017-03-08 19:05:07 -0800670 }
Calin Juravle29591732017-11-20 17:46:19 -0800671 return fd;
Calin Juravle114f0812017-03-08 19:05:07 -0800672}
673
Calin Juravle29591732017-11-20 17:46:19 -0800674static unique_fd open_profile(uid_t uid, const std::string& profile, int32_t flags) {
Calin Juravle114f0812017-03-08 19:05:07 -0800675 // Do not follow symlinks when opening a profile:
676 // - primary profiles should not contain symlinks in their paths
677 // - secondary dex paths should have been already resolved and validated
678 flags |= O_NOFOLLOW;
679
Calin Juravle29591732017-11-20 17:46:19 -0800680 // Check if we need to create the profile
681 // Reference profiles and snapshots are created on the fly; so they might not exist beforehand.
682 unique_fd fd;
683 if ((flags & O_CREAT) != 0) {
684 fd = create_profile(uid, profile, flags);
685 } else {
686 fd.reset(TEMP_FAILURE_RETRY(open(profile.c_str(), flags)));
687 }
688
Calin Juravle114f0812017-03-08 19:05:07 -0800689 if (fd.get() < 0) {
690 if (errno != ENOENT) {
691 // Profiles might be missing for various reasons. For example, in a
692 // multi-user environment, the profile directory for one user can be created
693 // after we start a merge. In this case the current profile for that user
694 // will not be found.
695 // Also, the secondary dex profiles might be deleted by the app at any time,
696 // so we can't we need to prepare if they are missing.
697 PLOG(ERROR) << "Failed to open profile " << profile;
698 }
699 return invalid_unique_fd();
700 }
701
Jeff Sharkey90aff262016-12-12 14:28:24 -0700702 return fd;
703}
704
Calin Juravle824a64d2018-01-18 20:23:17 -0800705static unique_fd open_current_profile(uid_t uid, userid_t user, const std::string& package_name,
706 const std::string& location, bool is_secondary_dex) {
707 std::string profile = create_current_profile_path(user, package_name, location,
708 is_secondary_dex);
Calin Juravle29591732017-11-20 17:46:19 -0800709 return open_profile(uid, profile, O_RDONLY);
Calin Juravle114f0812017-03-08 19:05:07 -0800710}
711
Calin Juravle824a64d2018-01-18 20:23:17 -0800712static unique_fd open_reference_profile(uid_t uid, const std::string& package_name,
713 const std::string& location, bool read_write, bool is_secondary_dex) {
714 std::string profile = create_reference_profile_path(package_name, location, is_secondary_dex);
Calin Juravle29591732017-11-20 17:46:19 -0800715 return open_profile(uid, profile, read_write ? (O_CREAT | O_RDWR) : O_RDONLY);
716}
717
718static unique_fd open_spnashot_profile(uid_t uid, const std::string& package_name,
Calin Juravle824a64d2018-01-18 20:23:17 -0800719 const std::string& location) {
720 std::string profile = create_snapshot_profile_path(package_name, location);
Calin Juravle29591732017-11-20 17:46:19 -0800721 return open_profile(uid, profile, O_CREAT | O_RDWR | O_TRUNC);
Calin Juravle114f0812017-03-08 19:05:07 -0800722}
723
Calin Juravle824a64d2018-01-18 20:23:17 -0800724static void open_profile_files(uid_t uid, const std::string& package_name,
725 const std::string& location, bool is_secondary_dex,
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800726 /*out*/ std::vector<unique_fd>* profiles_fd, /*out*/ unique_fd* reference_profile_fd) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700727 // Open the reference profile in read-write mode as profman might need to save the merge.
Calin Juravle824a64d2018-01-18 20:23:17 -0800728 *reference_profile_fd = open_reference_profile(uid, package_name, location,
729 /*read_write*/ true, is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700730
Calin Juravle114f0812017-03-08 19:05:07 -0800731 // For secondary dex files, we don't really need the user but we use it for sanity checks.
732 // Note: the user owning the dex file should be the current user.
733 std::vector<userid_t> users;
734 if (is_secondary_dex){
735 users.push_back(multiuser_get_user_id(uid));
736 } else {
737 users = get_known_users(/*volume_uuid*/ nullptr);
738 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700739 for (auto user : users) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800740 unique_fd profile_fd = open_current_profile(uid, user, package_name, location,
741 is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700742 // Add to the lists only if both fds are valid.
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800743 if (profile_fd.get() >= 0) {
744 profiles_fd->push_back(std::move(profile_fd));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700745 }
746 }
747}
748
Calin Juravle76561322019-11-14 09:08:52 -0800749static constexpr int PROFMAN_BIN_RETURN_CODE_SUCCESS = 0;
750static constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 1;
751static constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION = 2;
752static constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 3;
753static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 4;
754static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 5;
755static constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS = 6;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700756
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800757class RunProfman : public ExecVHelper {
758 public:
759 void SetupArgs(const std::vector<unique_fd>& profile_fds,
760 const unique_fd& reference_profile_fd,
761 const std::vector<unique_fd>& apk_fds,
762 const std::vector<std::string>& dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800763 bool copy_and_update,
764 bool for_snapshot,
765 bool for_boot_image) {
Calin Juravlef74a7372019-02-28 20:29:41 -0800766
767 // TODO(calin): Assume for now we run in the bg compile job (which is in
768 // most of the invocation). With the current data flow, is not very easy or
769 // clean to discover this in RunProfman (it will require quite a messy refactoring).
770 const char* profman_bin = select_execution_binary(
771 kProfmanPath, kProfmanDebugPath, /*background_job_compile=*/ true);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700772
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800773 if (copy_and_update) {
774 CHECK_EQ(1u, profile_fds.size());
775 CHECK_EQ(1u, apk_fds.size());
Mathieu Chartier31636522018-11-09 23:53:07 +0000776 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800777 if (reference_profile_fd != -1) {
778 AddArg("--reference-profile-file-fd=" + std::to_string(reference_profile_fd.get()));
Mathieu Chartier31636522018-11-09 23:53:07 +0000779 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800780
781 for (const unique_fd& fd : profile_fds) {
782 AddArg("--profile-file-fd=" + std::to_string(fd.get()));
783 }
784
785 for (const unique_fd& fd : apk_fds) {
786 AddArg("--apk-fd=" + std::to_string(fd.get()));
787 }
788
789 for (const std::string& dex_location : dex_locations) {
790 AddArg("--dex-location=" + dex_location);
791 }
792
793 if (copy_and_update) {
794 AddArg("--copy-and-update-profile-key");
795 }
796
Calin Juravle78728f32019-11-08 17:55:46 -0800797 if (for_snapshot) {
798 AddArg("--force-merge");
799 }
800
801 if (for_boot_image) {
802 AddArg("--boot-image-merge");
803 }
804
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800805 // Do not add after dex2oat_flags, they should override others for debugging.
806 PrepareArgs(profman_bin);
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800807 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700808
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800809 void SetupMerge(const std::vector<unique_fd>& profiles_fd,
810 const unique_fd& reference_profile_fd,
811 const std::vector<unique_fd>& apk_fds = std::vector<unique_fd>(),
Calin Juravle78728f32019-11-08 17:55:46 -0800812 const std::vector<std::string>& dex_locations = std::vector<std::string>(),
813 bool for_snapshot = false,
814 bool for_boot_image = false) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800815 SetupArgs(profiles_fd,
Calin Juravleb3a929d2018-12-11 14:40:00 -0800816 reference_profile_fd,
817 apk_fds,
818 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800819 /*copy_and_update=*/ false,
820 for_snapshot,
821 for_boot_image);
Mathieu Chartier62d218d2018-11-05 09:34:24 -0800822 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700823
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800824 void SetupCopyAndUpdate(unique_fd&& profile_fd,
825 unique_fd&& reference_profile_fd,
826 unique_fd&& apk_fd,
827 const std::string& dex_location) {
828 // The fds need to stay open longer than the scope of the function, so put them into a local
829 // variable vector.
830 profiles_fd_.push_back(std::move(profile_fd));
831 apk_fds_.push_back(std::move(apk_fd));
832 reference_profile_fd_ = std::move(reference_profile_fd);
833 std::vector<std::string> dex_locations = {dex_location};
Calin Juravleb3a929d2018-12-11 14:40:00 -0800834 SetupArgs(profiles_fd_,
835 reference_profile_fd_,
836 apk_fds_,
837 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800838 /*copy_and_update=*/true,
839 /*for_snapshot*/false,
840 /*for_boot_image*/false);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800841 }
Calin Juravlef63d4792018-01-30 17:43:34 +0000842
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800843 void SetupDump(const std::vector<unique_fd>& profiles_fd,
844 const unique_fd& reference_profile_fd,
845 const std::vector<std::string>& dex_locations,
846 const std::vector<unique_fd>& apk_fds,
847 const unique_fd& output_fd) {
848 AddArg("--dump-only");
849 AddArg(StringPrintf("--dump-output-to-fd=%d", output_fd.get()));
Calin Juravleb3a929d2018-12-11 14:40:00 -0800850 SetupArgs(profiles_fd,
851 reference_profile_fd,
852 apk_fds,
853 dex_locations,
Calin Juravle78728f32019-11-08 17:55:46 -0800854 /*copy_and_update=*/false,
855 /*for_snapshot*/false,
856 /*for_boot_image*/false);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800857 }
Calin Juravlef63d4792018-01-30 17:43:34 +0000858
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800859 void Exec() {
860 ExecVHelper::Exec(DexoptReturnCodes::kProfmanExec);
861 }
Mathieu Chartier31636522018-11-09 23:53:07 +0000862
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800863 private:
864 unique_fd reference_profile_fd_;
865 std::vector<unique_fd> profiles_fd_;
866 std::vector<unique_fd> apk_fds_;
867};
Mathieu Chartier31636522018-11-09 23:53:07 +0000868
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800869
Calin Juravlef63d4792018-01-30 17:43:34 +0000870
Jeff Sharkey90aff262016-12-12 14:28:24 -0700871// Decides if profile guided compilation is needed or not based on existing profiles.
Calin Juravle114f0812017-03-08 19:05:07 -0800872// The location is the package name for primary apks or the dex path for secondary dex files.
873// Returns true if there is enough information in the current profiles that makes it
874// worth to recompile the given location.
Jeff Sharkey90aff262016-12-12 14:28:24 -0700875// If the return value is true all the current profiles would have been merged into
876// the reference profiles accessible with open_reference_profile().
Calin Juravle824a64d2018-01-18 20:23:17 -0800877static bool analyze_profiles(uid_t uid, const std::string& package_name,
878 const std::string& location, bool is_secondary_dex) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800879 std::vector<unique_fd> profiles_fd;
880 unique_fd reference_profile_fd;
Calin Juravle824a64d2018-01-18 20:23:17 -0800881 open_profile_files(uid, package_name, location, is_secondary_dex,
882 &profiles_fd, &reference_profile_fd);
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800883 if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -0700884 // Skip profile guided compilation because no profiles were found.
885 // Or if the reference profile info couldn't be opened.
Jeff Sharkey90aff262016-12-12 14:28:24 -0700886 return false;
887 }
888
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800889 RunProfman profman_merge;
890 profman_merge.SetupMerge(profiles_fd, reference_profile_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700891 pid_t pid = fork();
892 if (pid == 0) {
893 /* child -- drop privileges before continuing */
894 drop_capabilities(uid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -0800895 profman_merge.Exec();
Jeff Sharkey90aff262016-12-12 14:28:24 -0700896 }
897 /* parent */
898 int return_code = wait_child(pid);
899 bool need_to_compile = false;
900 bool should_clear_current_profiles = false;
901 bool should_clear_reference_profile = false;
902 if (!WIFEXITED(return_code)) {
Calin Juravle114f0812017-03-08 19:05:07 -0800903 LOG(WARNING) << "profman failed for location " << location << ": " << return_code;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700904 } else {
905 return_code = WEXITSTATUS(return_code);
906 switch (return_code) {
907 case PROFMAN_BIN_RETURN_CODE_COMPILE:
908 need_to_compile = true;
909 should_clear_current_profiles = true;
910 should_clear_reference_profile = false;
911 break;
912 case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION:
913 need_to_compile = false;
914 should_clear_current_profiles = false;
915 should_clear_reference_profile = false;
916 break;
917 case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES:
Calin Juravle114f0812017-03-08 19:05:07 -0800918 LOG(WARNING) << "Bad profiles for location " << location;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700919 need_to_compile = false;
920 should_clear_current_profiles = true;
921 should_clear_reference_profile = true;
922 break;
923 case PROFMAN_BIN_RETURN_CODE_ERROR_IO: // fall-through
924 case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING:
925 // Temporary IO problem (e.g. locking). Ignore but log a warning.
Calin Juravle114f0812017-03-08 19:05:07 -0800926 LOG(WARNING) << "IO error while reading profiles for location " << location;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700927 need_to_compile = false;
928 should_clear_current_profiles = false;
929 should_clear_reference_profile = false;
930 break;
Calin Juravle76561322019-11-14 09:08:52 -0800931 case PROFMAN_BIN_RETURN_CODE_ERROR_DIFFERENT_VERSIONS:
932 need_to_compile = false;
933 should_clear_current_profiles = true;
934 should_clear_reference_profile = true;
935 break;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700936 default:
937 // Unknown return code or error. Unlink profiles.
Calin Juravle78728f32019-11-08 17:55:46 -0800938 LOG(WARNING) << "Unexpected error code while processing profiles for location "
Calin Juravle114f0812017-03-08 19:05:07 -0800939 << location << ": " << return_code;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700940 need_to_compile = false;
941 should_clear_current_profiles = true;
942 should_clear_reference_profile = true;
943 break;
944 }
945 }
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800946
Jeff Sharkey90aff262016-12-12 14:28:24 -0700947 if (should_clear_current_profiles) {
Calin Juravle114f0812017-03-08 19:05:07 -0800948 if (is_secondary_dex) {
949 // For secondary dex files, the owning user is the current user.
Calin Juravle824a64d2018-01-18 20:23:17 -0800950 clear_current_profile(package_name, location, multiuser_get_user_id(uid),
951 is_secondary_dex);
Calin Juravle114f0812017-03-08 19:05:07 -0800952 } else {
Calin Juravle824a64d2018-01-18 20:23:17 -0800953 clear_primary_current_profiles(package_name, location);
Calin Juravle114f0812017-03-08 19:05:07 -0800954 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700955 }
956 if (should_clear_reference_profile) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800957 clear_reference_profile(package_name, location, is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700958 }
959 return need_to_compile;
960}
961
Calin Juravle114f0812017-03-08 19:05:07 -0800962// Decides if profile guided compilation is needed or not based on existing profiles.
963// The analysis is done for the primary apks of the given package.
964// Returns true if there is enough information in the current profiles that makes it
965// worth to recompile the package.
966// If the return value is true all the current profiles would have been merged into
967// the reference profiles accessible with open_reference_profile().
Calin Juravle824a64d2018-01-18 20:23:17 -0800968bool analyze_primary_profiles(uid_t uid, const std::string& package_name,
969 const std::string& profile_name) {
970 return analyze_profiles(uid, package_name, profile_name, /*is_secondary_dex*/false);
Calin Juravle114f0812017-03-08 19:05:07 -0800971}
972
Calin Juravle408cd4a2018-01-20 23:34:18 -0800973bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name,
974 const std::string& code_path) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800975 std::vector<unique_fd> profile_fds;
976 unique_fd reference_profile_fd;
Calin Juravle408cd4a2018-01-20 23:34:18 -0800977 std::string out_file_name = StringPrintf("/data/misc/profman/%s-%s.txt",
978 pkgname.c_str(), profile_name.c_str());
Jeff Sharkey90aff262016-12-12 14:28:24 -0700979
Calin Juravle408cd4a2018-01-20 23:34:18 -0800980 open_profile_files(uid, pkgname, profile_name, /*is_secondary_dex*/false,
Calin Juravle114f0812017-03-08 19:05:07 -0800981 &profile_fds, &reference_profile_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700982
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800983 const bool has_reference_profile = (reference_profile_fd.get() != -1);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700984 const bool has_profiles = !profile_fds.empty();
985
986 if (!has_reference_profile && !has_profiles) {
Calin Juravle76268c52017-03-09 13:19:42 -0800987 LOG(ERROR) << "profman dump: no profiles to dump for " << pkgname;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700988 return false;
989 }
990
Calin Juravle114f0812017-03-08 19:05:07 -0800991 unique_fd output_fd(open(out_file_name.c_str(),
992 O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644));
Jeff Sharkey90aff262016-12-12 14:28:24 -0700993 if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
Calin Juravle408cd4a2018-01-20 23:34:18 -0800994 LOG(ERROR) << "installd cannot chmod file for dump_profile" << out_file_name;
Jeff Sharkey90aff262016-12-12 14:28:24 -0700995 return false;
996 }
Calin Juravle408cd4a2018-01-20 23:34:18 -0800997
Jeff Sharkey90aff262016-12-12 14:28:24 -0700998 std::vector<std::string> dex_locations;
Calin Juravle1a0af3b2017-03-09 14:33:33 -0800999 std::vector<unique_fd> apk_fds;
Calin Juravle408cd4a2018-01-20 23:34:18 -08001000 unique_fd apk_fd(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW));
1001 if (apk_fd == -1) {
1002 PLOG(ERROR) << "installd cannot open " << code_path.c_str();
1003 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001004 }
Calin Juravle408cd4a2018-01-20 23:34:18 -08001005 dex_locations.push_back(get_location_from_path(code_path.c_str()));
1006 apk_fds.push_back(std::move(apk_fd));
1007
Jeff Sharkey90aff262016-12-12 14:28:24 -07001008
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001009 RunProfman profman_dump;
1010 profman_dump.SetupDump(profile_fds, reference_profile_fd, dex_locations, apk_fds, output_fd);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001011 pid_t pid = fork();
1012 if (pid == 0) {
1013 /* child -- drop privileges before continuing */
1014 drop_capabilities(uid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001015 profman_dump.Exec();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001016 }
1017 /* parent */
Jeff Sharkey90aff262016-12-12 14:28:24 -07001018 int return_code = wait_child(pid);
1019 if (!WIFEXITED(return_code)) {
1020 LOG(WARNING) << "profman failed for package " << pkgname << ": "
1021 << return_code;
1022 return false;
1023 }
1024 return true;
1025}
1026
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001027bool copy_system_profile(const std::string& system_profile,
Calin Juravle824a64d2018-01-18 20:23:17 -08001028 uid_t packageUid, const std::string& package_name, const std::string& profile_name) {
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001029 unique_fd in_fd(open(system_profile.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
1030 unique_fd out_fd(open_reference_profile(packageUid,
Calin Juravle824a64d2018-01-18 20:23:17 -08001031 package_name,
1032 profile_name,
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001033 /*read_write*/ true,
1034 /*secondary*/ false));
1035 if (in_fd.get() < 0) {
1036 PLOG(WARNING) << "Could not open profile " << system_profile;
1037 return false;
1038 }
1039 if (out_fd.get() < 0) {
Calin Juravle824a64d2018-01-18 20:23:17 -08001040 PLOG(WARNING) << "Could not open profile " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001041 return false;
1042 }
1043
Mathieu Chartier78f71fe2017-06-14 13:02:26 -07001044 // As a security measure we want to write the profile information with the reduced capabilities
1045 // of the package user id. So we fork and drop capabilities in the child.
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001046 pid_t pid = fork();
1047 if (pid == 0) {
1048 /* child -- drop privileges before continuing */
1049 drop_capabilities(packageUid);
1050
1051 if (flock(out_fd.get(), LOCK_EX | LOCK_NB) != 0) {
1052 if (errno != EWOULDBLOCK) {
Calin Juravle824a64d2018-01-18 20:23:17 -08001053 PLOG(WARNING) << "Error locking profile " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001054 }
1055 // This implies that the app owning this profile is running
1056 // (and has acquired the lock).
1057 //
1058 // The app never acquires the lock for the reference profiles of primary apks.
1059 // Only dex2oat from installd will do that. Since installd is single threaded
1060 // we should not see this case. Nevertheless be prepared for it.
Calin Juravle824a64d2018-01-18 20:23:17 -08001061 PLOG(WARNING) << "Failed to flock " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001062 return false;
1063 }
1064
1065 bool truncated = ftruncate(out_fd.get(), 0) == 0;
1066 if (!truncated) {
Calin Juravle824a64d2018-01-18 20:23:17 -08001067 PLOG(WARNING) << "Could not truncate " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001068 }
1069
1070 // Copy over data.
1071 static constexpr size_t kBufferSize = 4 * 1024;
1072 char buffer[kBufferSize];
1073 while (true) {
1074 ssize_t bytes = read(in_fd.get(), buffer, kBufferSize);
1075 if (bytes == 0) {
1076 break;
1077 }
1078 write(out_fd.get(), buffer, bytes);
1079 }
1080 if (flock(out_fd.get(), LOCK_UN) != 0) {
Calin Juravle824a64d2018-01-18 20:23:17 -08001081 PLOG(WARNING) << "Error unlocking profile " << package_name;
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001082 }
Mathieu Chartier78f71fe2017-06-14 13:02:26 -07001083 // Use _exit since we don't want to run the global destructors in the child.
1084 // b/62597429
1085 _exit(0);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -07001086 }
1087 /* parent */
1088 int return_code = wait_child(pid);
1089 return return_code == 0;
1090}
1091
Jeff Sharkey90aff262016-12-12 14:28:24 -07001092static std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) {
1093 // A standard dalvik-cache entry. Replace ".dex" with `new_ext`.
1094 if (EndsWith(oat_path, ".dex")) {
1095 std::string new_path = oat_path;
1096 new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext);
Elliott Hughes969e4f82017-12-20 12:34:09 -08001097 CHECK(EndsWith(new_path, new_ext));
Jeff Sharkey90aff262016-12-12 14:28:24 -07001098 return new_path;
1099 }
1100
1101 // An odex entry. Not that this may not be an extension, e.g., in the OTA
1102 // case (where the base name will have an extension for the B artifact).
1103 size_t odex_pos = oat_path.rfind(".odex");
1104 if (odex_pos != std::string::npos) {
1105 std::string new_path = oat_path;
1106 new_path.replace(odex_pos, strlen(".odex"), new_ext);
1107 CHECK_NE(new_path.find(new_ext), std::string::npos);
1108 return new_path;
1109 }
1110
1111 // Don't know how to handle this.
1112 return "";
1113}
1114
1115// Translate the given oat path to an art (app image) path. An empty string
1116// denotes an error.
1117static std::string create_image_filename(const std::string& oat_path) {
1118 return replace_file_extension(oat_path, ".art");
1119}
1120
1121// Translate the given oat path to a vdex path. An empty string denotes an error.
1122static std::string create_vdex_filename(const std::string& oat_path) {
1123 return replace_file_extension(oat_path, ".vdex");
1124}
1125
Jeff Sharkey90aff262016-12-12 14:28:24 -07001126static int open_output_file(const char* file_name, bool recreate, int permissions) {
1127 int flags = O_RDWR | O_CREAT;
1128 if (recreate) {
1129 if (unlink(file_name) < 0) {
1130 if (errno != ENOENT) {
1131 PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name;
1132 }
1133 }
1134 flags |= O_EXCL;
1135 }
1136 return open(file_name, flags, permissions);
1137}
1138
Calin Juravle2289c0a2017-02-15 12:44:14 -08001139static bool set_permissions_and_ownership(
1140 int fd, bool is_public, int uid, const char* path, bool is_secondary_dex) {
1141 // Primary apks are owned by the system. Secondary dex files are owned by the app.
1142 int owning_uid = is_secondary_dex ? uid : AID_SYSTEM;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001143 if (fchmod(fd,
1144 S_IRUSR|S_IWUSR|S_IRGRP |
1145 (is_public ? S_IROTH : 0)) < 0) {
1146 ALOGE("installd cannot chmod '%s' during dexopt\n", path);
1147 return false;
Calin Juravle2289c0a2017-02-15 12:44:14 -08001148 } else if (fchown(fd, owning_uid, uid) < 0) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001149 ALOGE("installd cannot chown '%s' during dexopt\n", path);
1150 return false;
1151 }
1152 return true;
1153}
1154
1155static bool IsOutputDalvikCache(const char* oat_dir) {
1156 // InstallerConnection.java (which invokes installd) transforms Java null arguments
1157 // into '!'. Play it safe by handling it both.
1158 // TODO: ensure we never get null.
1159 // TODO: pass a flag instead of inferring if the output is dalvik cache.
1160 return oat_dir == nullptr || oat_dir[0] == '!';
1161}
1162
Calin Juravled23dee72017-07-06 16:29:11 -07001163// Best-effort check whether we can fit the the path into our buffers.
1164// Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
1165// without a swap file, if necessary. Reference profiles file also add an extra ".prof"
1166// extension to the cache path (5 bytes).
1167// TODO(calin): move away from char* buffers and PKG_PATH_MAX.
1168static bool validate_dex_path_size(const std::string& dex_path) {
1169 if (dex_path.size() >= (PKG_PATH_MAX - 8)) {
1170 LOG(ERROR) << "dex_path too long: " << dex_path;
1171 return false;
1172 }
1173 return true;
1174}
1175
Jeff Sharkey90aff262016-12-12 14:28:24 -07001176static bool create_oat_out_path(const char* apk_path, const char* instruction_set,
Calin Juravle80a21252017-01-17 14:43:25 -08001177 const char* oat_dir, bool is_secondary_dex, /*out*/ char* out_oat_path) {
Calin Juravled23dee72017-07-06 16:29:11 -07001178 if (!validate_dex_path_size(apk_path)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001179 return false;
1180 }
1181
1182 if (!IsOutputDalvikCache(oat_dir)) {
Calin Juravle80a21252017-01-17 14:43:25 -08001183 // Oat dirs for secondary dex files are already validated.
1184 if (!is_secondary_dex && validate_apk_path(oat_dir)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001185 ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir);
1186 return false;
1187 }
1188 if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) {
1189 return false;
1190 }
1191 } else {
1192 if (!create_cache_path(out_oat_path, apk_path, instruction_set)) {
1193 return false;
1194 }
1195 }
1196 return true;
1197}
1198
1199// Helper for fd management. This is similar to a unique_fd in that it closes the file descriptor
1200// on destruction. It will also run the given cleanup (unless told not to) after closing.
1201//
1202// Usage example:
1203//
Calin Juravle7a570e82017-01-14 16:23:30 -08001204// Dex2oatFileWrapper file(open(...),
Jeff Sharkey90aff262016-12-12 14:28:24 -07001205// [name]() {
1206// unlink(name.c_str());
1207// });
1208// // Note: care needs to be taken about name, as it needs to have a lifetime longer than the
1209// wrapper if captured as a reference.
1210//
1211// if (file.get() == -1) {
1212// // Error opening...
1213// }
1214//
1215// ...
1216// if (error) {
1217// // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will run
1218// // and delete the file (after the fd is closed).
1219// return -1;
1220// }
1221//
1222// (Success case)
1223// file.SetCleanup(false);
1224// // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will not run
1225// // (leaving the file around; after the fd is closed).
1226//
Jeff Sharkey90aff262016-12-12 14:28:24 -07001227class Dex2oatFileWrapper {
1228 public:
Calin Juravle7a570e82017-01-14 16:23:30 -08001229 Dex2oatFileWrapper() : value_(-1), cleanup_(), do_cleanup_(true), auto_close_(true) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001230 }
1231
Calin Juravle7a570e82017-01-14 16:23:30 -08001232 Dex2oatFileWrapper(int value, std::function<void ()> cleanup)
1233 : value_(value), cleanup_(cleanup), do_cleanup_(true), auto_close_(true) {}
1234
1235 Dex2oatFileWrapper(Dex2oatFileWrapper&& other) {
1236 value_ = other.value_;
1237 cleanup_ = other.cleanup_;
1238 do_cleanup_ = other.do_cleanup_;
1239 auto_close_ = other.auto_close_;
1240 other.release();
1241 }
1242
1243 Dex2oatFileWrapper& operator=(Dex2oatFileWrapper&& other) {
1244 value_ = other.value_;
1245 cleanup_ = other.cleanup_;
1246 do_cleanup_ = other.do_cleanup_;
1247 auto_close_ = other.auto_close_;
1248 other.release();
1249 return *this;
1250 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001251
1252 ~Dex2oatFileWrapper() {
1253 reset(-1);
1254 }
1255
1256 int get() {
1257 return value_;
1258 }
1259
1260 void SetCleanup(bool cleanup) {
1261 do_cleanup_ = cleanup;
1262 }
1263
1264 void reset(int new_value) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001265 if (auto_close_ && value_ >= 0) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001266 close(value_);
1267 }
1268 if (do_cleanup_ && cleanup_ != nullptr) {
1269 cleanup_();
1270 }
1271
1272 value_ = new_value;
1273 }
1274
Calin Juravle7a570e82017-01-14 16:23:30 -08001275 void reset(int new_value, std::function<void ()> new_cleanup) {
1276 if (auto_close_ && value_ >= 0) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001277 close(value_);
1278 }
1279 if (do_cleanup_ && cleanup_ != nullptr) {
1280 cleanup_();
1281 }
1282
1283 value_ = new_value;
1284 cleanup_ = new_cleanup;
1285 }
1286
Calin Juravle7a570e82017-01-14 16:23:30 -08001287 void DisableAutoClose() {
1288 auto_close_ = false;
1289 }
1290
Jeff Sharkey90aff262016-12-12 14:28:24 -07001291 private:
Calin Juravle7a570e82017-01-14 16:23:30 -08001292 void release() {
1293 value_ = -1;
1294 do_cleanup_ = false;
1295 cleanup_ = nullptr;
1296 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001297 int value_;
Calin Juravle7a570e82017-01-14 16:23:30 -08001298 std::function<void ()> cleanup_;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001299 bool do_cleanup_;
Calin Juravle7a570e82017-01-14 16:23:30 -08001300 bool auto_close_;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001301};
1302
Calin Juravle7a570e82017-01-14 16:23:30 -08001303// (re)Creates the app image if needed.
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -07001304Dex2oatFileWrapper maybe_open_app_image(const char* out_oat_path,
1305 bool generate_app_image, bool is_public, int uid, bool is_secondary_dex) {
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +01001306
Calin Juravle7a570e82017-01-14 16:23:30 -08001307 const std::string image_path = create_image_filename(out_oat_path);
1308 if (image_path.empty()) {
1309 // Happens when the out_oat_path has an unknown extension.
1310 return Dex2oatFileWrapper();
1311 }
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +01001312
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -07001313 // In case there is a stale image, remove it now. Ignore any error.
1314 unlink(image_path.c_str());
1315
1316 // Not enabled, exit.
1317 if (!generate_app_image) {
Nicolas Geoffrayaa17ab42017-08-15 14:51:05 +01001318 return Dex2oatFileWrapper();
1319 }
Mathieu Chartier9b2da082018-10-26 13:23:11 -07001320 std::string app_image_format = GetProperty("dalvik.vm.appimageformat", "");
1321 if (app_image_format.empty()) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001322 return Dex2oatFileWrapper();
1323 }
1324 // Recreate is true since we do not want to modify a mapped image. If the app is
1325 // already running and we modify the image file, it can cause crashes (b/27493510).
1326 Dex2oatFileWrapper wrapper_fd(
1327 open_output_file(image_path.c_str(), true /*recreate*/, 0600 /*permissions*/),
1328 [image_path]() { unlink(image_path.c_str()); });
1329 if (wrapper_fd.get() < 0) {
1330 // Could not create application image file. Go on since we can compile without it.
1331 LOG(ERROR) << "installd could not create '" << image_path
1332 << "' for image file during dexopt";
1333 // If we have a valid image file path but no image fd, explicitly erase the image file.
1334 if (unlink(image_path.c_str()) < 0) {
1335 if (errno != ENOENT) {
1336 PLOG(ERROR) << "Couldn't unlink image file " << image_path;
1337 }
1338 }
1339 } else if (!set_permissions_and_ownership(
Calin Juravle2289c0a2017-02-15 12:44:14 -08001340 wrapper_fd.get(), is_public, uid, image_path.c_str(), is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001341 ALOGE("installd cannot set owner '%s' for image during dexopt\n", image_path.c_str());
1342 wrapper_fd.reset(-1);
1343 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001344
Calin Juravle7a570e82017-01-14 16:23:30 -08001345 return wrapper_fd;
1346}
1347
1348// Creates the dexopt swap file if necessary and return its fd.
1349// Returns -1 if there's no need for a swap or in case of errors.
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001350unique_fd maybe_open_dexopt_swap_file(const char* out_oat_path) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001351 if (!ShouldUseSwapFileForDexopt()) {
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001352 return invalid_unique_fd();
Calin Juravle7a570e82017-01-14 16:23:30 -08001353 }
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001354 auto swap_file_name = std::string(out_oat_path) + ".swap";
Calin Juravle1a0af3b2017-03-09 14:33:33 -08001355 unique_fd swap_fd(open_output_file(
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001356 swap_file_name.c_str(), /*recreate*/true, /*permissions*/0600));
Calin Juravle7a570e82017-01-14 16:23:30 -08001357 if (swap_fd.get() < 0) {
1358 // Could not create swap file. Optimistically go on and hope that we can compile
1359 // without it.
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001360 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name.c_str());
Calin Juravle7a570e82017-01-14 16:23:30 -08001361 } else {
1362 // Immediately unlink. We don't really want to hit flash.
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06001363 if (unlink(swap_file_name.c_str()) < 0) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001364 PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name;
1365 }
1366 }
1367 return swap_fd;
1368}
1369
1370// Opens the reference profiles if needed.
1371// Note that the reference profile might not exist so it's OK if the fd will be -1.
Calin Juravle114f0812017-03-08 19:05:07 -08001372Dex2oatFileWrapper maybe_open_reference_profile(const std::string& pkgname,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001373 const std::string& dex_path, const char* profile_name, bool profile_guided,
Calin Juravle824a64d2018-01-18 20:23:17 -08001374 bool is_public, int uid, bool is_secondary_dex) {
Calin Juravle5bd1c722018-02-01 17:23:54 +00001375 // If we are not profile guided compilation, or we are compiling system server
1376 // do not bother to open the profiles; we won't be using them.
1377 if (!profile_guided || (pkgname[0] == '*')) {
1378 return Dex2oatFileWrapper();
1379 }
1380
1381 // If this is a secondary dex path which is public do not open the profile.
1382 // We cannot compile public secondary dex paths with profiles. That's because
1383 // it will expose how the dex files are used by their owner.
1384 //
1385 // Note that the PackageManager is responsible to set the is_public flag for
1386 // primary apks and we do not check it here. In some cases, e.g. when
1387 // compiling with a public profile from the .dm file the PackageManager will
1388 // set is_public toghether with the profile guided compilation.
1389 if (is_secondary_dex && is_public) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001390 return Dex2oatFileWrapper();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001391 }
Calin Juravle114f0812017-03-08 19:05:07 -08001392
1393 // Open reference profile in read only mode as dex2oat does not get write permissions.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +00001394 std::string location;
1395 if (is_secondary_dex) {
1396 location = dex_path;
1397 } else {
1398 if (profile_name == nullptr) {
1399 // This path is taken for system server re-compilation lunched from ZygoteInit.
1400 return Dex2oatFileWrapper();
1401 } else {
1402 location = profile_name;
1403 }
1404 }
Calin Juravle824a64d2018-01-18 20:23:17 -08001405 unique_fd ufd = open_reference_profile(uid, pkgname, location, /*read_write*/false,
1406 is_secondary_dex);
1407 const auto& cleanup = [pkgname, location, is_secondary_dex]() {
1408 clear_reference_profile(pkgname, location, is_secondary_dex);
Calin Juravle114f0812017-03-08 19:05:07 -08001409 };
1410 return Dex2oatFileWrapper(ufd.release(), cleanup);
Calin Juravle7a570e82017-01-14 16:23:30 -08001411}
Jeff Sharkey90aff262016-12-12 14:28:24 -07001412
Calin Juravle7a570e82017-01-14 16:23:30 -08001413// Opens the vdex files and assigns the input fd to in_vdex_wrapper_fd and the output fd to
1414// out_vdex_wrapper_fd. Returns true for success or false in case of errors.
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001415bool open_vdex_files_for_dex2oat(const char* apk_path, const char* out_oat_path, int dexopt_needed,
Nicolas Geoffray3c95f2d2017-04-24 13:34:59 +00001416 const char* instruction_set, bool is_public, int uid, bool is_secondary_dex,
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001417 bool profile_guided, Dex2oatFileWrapper* in_vdex_wrapper_fd,
Calin Juravle7a570e82017-01-14 16:23:30 -08001418 Dex2oatFileWrapper* out_vdex_wrapper_fd) {
1419 CHECK(in_vdex_wrapper_fd != nullptr);
1420 CHECK(out_vdex_wrapper_fd != nullptr);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001421 // Open the existing VDEX. We do this before creating the new output VDEX, which will
1422 // unlink the old one.
Richard Uhler76cc0272016-12-08 10:46:35 +00001423 char in_odex_path[PKG_PATH_MAX];
1424 int dexopt_action = abs(dexopt_needed);
1425 bool is_odex_location = dexopt_needed < 0;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001426 std::string in_vdex_path_str;
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001427
1428 // Infer the name of the output VDEX.
1429 const std::string out_vdex_path_str = create_vdex_filename(out_oat_path);
1430 if (out_vdex_path_str.empty()) {
1431 return false;
1432 }
1433
1434 bool update_vdex_in_place = false;
Nicolas Geoffray3c95f2d2017-04-24 13:34:59 +00001435 if (dexopt_action != DEX2OAT_FROM_SCRATCH) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07001436 // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd.
1437 const char* path = nullptr;
1438 if (is_odex_location) {
1439 if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1440 path = in_odex_path;
1441 } else {
1442 ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path);
Calin Juravle7a570e82017-01-14 16:23:30 -08001443 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001444 }
1445 } else {
1446 path = out_oat_path;
1447 }
1448 in_vdex_path_str = create_vdex_filename(path);
1449 if (in_vdex_path_str.empty()) {
1450 ALOGE("installd cannot compute input vdex location for '%s'\n", path);
Calin Juravle7a570e82017-01-14 16:23:30 -08001451 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07001452 }
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001453 // We can update in place when all these conditions are met:
1454 // 1) The vdex location to write to is the same as the vdex location to read (vdex files
1455 // on /system typically cannot be updated in place).
1456 // 2) We dex2oat due to boot image change, because we then know the existing vdex file
1457 // cannot be currently used by a running process.
1458 // 3) We are not doing a profile guided compilation, because dexlayout requires two
1459 // different vdex files to operate.
1460 update_vdex_in_place =
1461 (in_vdex_path_str == out_vdex_path_str) &&
1462 (dexopt_action == DEX2OAT_FOR_BOOT_IMAGE) &&
1463 !profile_guided;
1464 if (update_vdex_in_place) {
1465 // Open the file read-write to be able to update it.
1466 in_vdex_wrapper_fd->reset(open(in_vdex_path_str.c_str(), O_RDWR, 0));
1467 if (in_vdex_wrapper_fd->get() == -1) {
1468 // If we failed to open the file, we cannot update it in place.
1469 update_vdex_in_place = false;
1470 }
1471 } else {
1472 in_vdex_wrapper_fd->reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0));
1473 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001474 }
1475
Nicolas Geoffrayb03814f2017-06-05 12:38:10 +00001476 // If we are updating the vdex in place, we do not need to recreate a vdex,
1477 // and can use the same existing one.
1478 if (update_vdex_in_place) {
1479 // We unlink the file in case the invocation of dex2oat fails, to ensure we don't
1480 // have bogus stale vdex files.
1481 out_vdex_wrapper_fd->reset(
1482 in_vdex_wrapper_fd->get(),
1483 [out_vdex_path_str]() { unlink(out_vdex_path_str.c_str()); });
1484 // Disable auto close for the in wrapper fd (it will be done when destructing the out
1485 // wrapper).
1486 in_vdex_wrapper_fd->DisableAutoClose();
1487 } else {
1488 out_vdex_wrapper_fd->reset(
1489 open_output_file(out_vdex_path_str.c_str(), /*recreate*/true, /*permissions*/0644),
1490 [out_vdex_path_str]() { unlink(out_vdex_path_str.c_str()); });
1491 if (out_vdex_wrapper_fd->get() < 0) {
1492 ALOGE("installd cannot open vdex'%s' during dexopt\n", out_vdex_path_str.c_str());
1493 return false;
1494 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07001495 }
Calin Juravle7a570e82017-01-14 16:23:30 -08001496 if (!set_permissions_and_ownership(out_vdex_wrapper_fd->get(), is_public, uid,
Calin Juravle2289c0a2017-02-15 12:44:14 -08001497 out_vdex_path_str.c_str(), is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001498 ALOGE("installd cannot set owner '%s' for vdex during dexopt\n", out_vdex_path_str.c_str());
1499 return false;
1500 }
1501
1502 // If we got here we successfully opened the vdex files.
1503 return true;
1504}
1505
1506// Opens the output oat file for the given apk.
1507// If successful it stores the output path into out_oat_path and returns true.
1508Dex2oatFileWrapper open_oat_out_file(const char* apk_path, const char* oat_dir,
Calin Juravle80a21252017-01-17 14:43:25 -08001509 bool is_public, int uid, const char* instruction_set, bool is_secondary_dex,
1510 char* out_oat_path) {
1511 if (!create_oat_out_path(apk_path, instruction_set, oat_dir, is_secondary_dex, out_oat_path)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001512 return Dex2oatFileWrapper();
1513 }
1514 const std::string out_oat_path_str(out_oat_path);
1515 Dex2oatFileWrapper wrapper_fd(
1516 open_output_file(out_oat_path, /*recreate*/true, /*permissions*/0644),
1517 [out_oat_path_str]() { unlink(out_oat_path_str.c_str()); });
1518 if (wrapper_fd.get() < 0) {
Calin Juravle80a21252017-01-17 14:43:25 -08001519 PLOG(ERROR) << "installd cannot open output during dexopt" << out_oat_path;
Calin Juravle2289c0a2017-02-15 12:44:14 -08001520 } else if (!set_permissions_and_ownership(
1521 wrapper_fd.get(), is_public, uid, out_oat_path, is_secondary_dex)) {
Calin Juravle7a570e82017-01-14 16:23:30 -08001522 ALOGE("installd cannot set owner '%s' for output during dexopt\n", out_oat_path);
1523 wrapper_fd.reset(-1);
1524 }
1525 return wrapper_fd;
1526}
1527
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001528// Creates RDONLY fds for oat and vdex files, if exist.
1529// Returns false if it fails to create oat out path for the given apk path.
1530// Note that the method returns true even if the files could not be opened.
1531bool maybe_open_oat_and_vdex_file(const std::string& apk_path,
1532 const std::string& oat_dir,
1533 const std::string& instruction_set,
1534 bool is_secondary_dex,
1535 unique_fd* oat_file_fd,
1536 unique_fd* vdex_file_fd) {
1537 char oat_path[PKG_PATH_MAX];
1538 if (!create_oat_out_path(apk_path.c_str(),
1539 instruction_set.c_str(),
1540 oat_dir.c_str(),
1541 is_secondary_dex,
1542 oat_path)) {
Calin Juravle7d765462017-09-04 15:57:10 -07001543 LOG(ERROR) << "Could not create oat out path for "
1544 << apk_path << " with oat dir " << oat_dir;
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001545 return false;
1546 }
1547 oat_file_fd->reset(open(oat_path, O_RDONLY));
1548 if (oat_file_fd->get() < 0) {
1549 PLOG(INFO) << "installd cannot open oat file during dexopt" << oat_path;
1550 }
1551
1552 std::string vdex_filename = create_vdex_filename(oat_path);
1553 vdex_file_fd->reset(open(vdex_filename.c_str(), O_RDONLY));
1554 if (vdex_file_fd->get() < 0) {
1555 PLOG(INFO) << "installd cannot open vdex file during dexopt" << vdex_filename;
1556 }
1557
1558 return true;
1559}
1560
Calin Juravle7a570e82017-01-14 16:23:30 -08001561// Updates the access times of out_oat_path based on those from apk_path.
1562void update_out_oat_access_times(const char* apk_path, const char* out_oat_path) {
1563 struct stat input_stat;
1564 memset(&input_stat, 0, sizeof(input_stat));
1565 if (stat(apk_path, &input_stat) != 0) {
1566 PLOG(ERROR) << "Could not stat " << apk_path << " during dexopt";
1567 return;
1568 }
1569
1570 struct utimbuf ut;
1571 ut.actime = input_stat.st_atime;
1572 ut.modtime = input_stat.st_mtime;
1573 if (utime(out_oat_path, &ut) != 0) {
1574 PLOG(WARNING) << "Could not update access times for " << apk_path << " during dexopt";
1575 }
1576}
1577
Calin Juravle80a21252017-01-17 14:43:25 -08001578// Runs (execv) dexoptanalyzer on the given arguments.
Calin Juravle114f0812017-03-08 19:05:07 -08001579// The analyzer will check if the dex_file needs to be (re)compiled to match the compiler_filter.
1580// If this is for a profile guided compilation, profile_was_updated will tell whether or not
1581// the profile has changed.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001582class RunDexoptAnalyzer : public ExecVHelper {
1583 public:
1584 RunDexoptAnalyzer(const std::string& dex_file,
David Brazdil4f6027a2019-03-19 11:44:21 +00001585 int vdex_fd,
1586 int oat_fd,
1587 int zip_fd,
1588 const std::string& instruction_set,
1589 const std::string& compiler_filter,
1590 bool profile_was_updated,
1591 bool downgrade,
1592 const char* class_loader_context,
1593 const std::string& class_loader_context_fds) {
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001594 CHECK_GE(zip_fd, 0);
Calin Juravlef74a7372019-02-28 20:29:41 -08001595
1596 // We always run the analyzer in the background job.
1597 const char* dexoptanalyzer_bin = select_execution_binary(
1598 kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true);
Calin Juravle80a21252017-01-17 14:43:25 -08001599
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001600 std::string dex_file_arg = "--dex-file=" + dex_file;
1601 std::string oat_fd_arg = "--oat-fd=" + std::to_string(oat_fd);
1602 std::string vdex_fd_arg = "--vdex-fd=" + std::to_string(vdex_fd);
1603 std::string zip_fd_arg = "--zip-fd=" + std::to_string(zip_fd);
1604 std::string isa_arg = "--isa=" + instruction_set;
1605 std::string compiler_filter_arg = "--compiler-filter=" + compiler_filter;
1606 const char* assume_profile_changed = "--assume-profile-changed";
1607 const char* downgrade_flag = "--downgrade";
1608 std::string class_loader_context_arg = "--class-loader-context=";
1609 if (class_loader_context != nullptr) {
1610 class_loader_context_arg += class_loader_context;
1611 }
David Brazdil4f6027a2019-03-19 11:44:21 +00001612 std::string class_loader_context_fds_arg = "--class-loader-context-fds=";
1613 if (!class_loader_context_fds.empty()) {
1614 class_loader_context_fds_arg += class_loader_context_fds;
1615 }
Mathieu Chartier31636522018-11-09 23:53:07 +00001616
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001617 // program name, dex file, isa, filter
1618 AddArg(dex_file_arg);
1619 AddArg(isa_arg);
1620 AddArg(compiler_filter_arg);
1621 if (oat_fd >= 0) {
1622 AddArg(oat_fd_arg);
1623 }
1624 if (vdex_fd >= 0) {
1625 AddArg(vdex_fd_arg);
1626 }
Greg Kaiser8042c372019-03-26 06:23:19 -07001627 AddArg(zip_fd_arg);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001628 if (profile_was_updated) {
1629 AddArg(assume_profile_changed);
1630 }
1631 if (downgrade) {
1632 AddArg(downgrade_flag);
1633 }
1634 if (class_loader_context != nullptr) {
Greg Kaiser8042c372019-03-26 06:23:19 -07001635 AddArg(class_loader_context_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +00001636 if (!class_loader_context_fds.empty()) {
Greg Kaiser8042c372019-03-26 06:23:19 -07001637 AddArg(class_loader_context_fds_arg);
David Brazdil4f6027a2019-03-19 11:44:21 +00001638 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001639 }
Mathieu Chartier31636522018-11-09 23:53:07 +00001640
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001641 PrepareArgs(dexoptanalyzer_bin);
1642 }
David Brazdil4f6027a2019-03-19 11:44:21 +00001643
1644 // Dexoptanalyzer mode which flattens the given class loader context and
1645 // prints a list of its dex files in that flattened order.
1646 RunDexoptAnalyzer(const char* class_loader_context) {
1647 CHECK(class_loader_context != nullptr);
1648
1649 // We always run the analyzer in the background job.
1650 const char* dexoptanalyzer_bin = select_execution_binary(
1651 kDexoptanalyzerPath, kDexoptanalyzerDebugPath, /*background_job_compile=*/ true);
1652
1653 AddArg("--flatten-class-loader-context");
1654 AddArg(std::string("--class-loader-context=") + class_loader_context);
1655 PrepareArgs(dexoptanalyzer_bin);
1656 }
Mathieu Chartiercc66c442018-11-09 15:57:21 -08001657};
Calin Juravle80a21252017-01-17 14:43:25 -08001658
1659// Prepares the oat dir for the secondary dex files.
Calin Juravle114f0812017-03-08 19:05:07 -08001660static bool prepare_secondary_dex_oat_dir(const std::string& dex_path, int uid,
Calin Juravle7d765462017-09-04 15:57:10 -07001661 const char* instruction_set) {
Calin Juravle114f0812017-03-08 19:05:07 -08001662 unsigned long dirIndex = dex_path.rfind('/');
Calin Juravle80a21252017-01-17 14:43:25 -08001663 if (dirIndex == std::string::npos) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001664 LOG(ERROR ) << "Unexpected dir structure for secondary dex " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001665 return false;
1666 }
Calin Juravle114f0812017-03-08 19:05:07 -08001667 std::string dex_dir = dex_path.substr(0, dirIndex);
Calin Juravle80a21252017-01-17 14:43:25 -08001668
Calin Juravle80a21252017-01-17 14:43:25 -08001669 // Create oat file output directory.
Calin Juravleebc8a792017-04-04 20:21:05 -07001670 mode_t oat_dir_mode = S_IRWXU | S_IRWXG | S_IXOTH;
1671 if (prepare_app_cache_dir(dex_dir, "oat", oat_dir_mode, uid, uid) != 0) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001672 LOG(ERROR) << "Could not prepare oat dir for secondary dex: " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001673 return false;
1674 }
1675
1676 char oat_dir[PKG_PATH_MAX];
Calin Juravle114f0812017-03-08 19:05:07 -08001677 snprintf(oat_dir, PKG_PATH_MAX, "%s/oat", dex_dir.c_str());
Calin Juravle80a21252017-01-17 14:43:25 -08001678
Calin Juravle7d765462017-09-04 15:57:10 -07001679 if (prepare_app_cache_dir(oat_dir, instruction_set, oat_dir_mode, uid, uid) != 0) {
Calin Juravlec9eab382017-01-25 01:17:17 -08001680 LOG(ERROR) << "Could not prepare oat/isa dir for secondary dex: " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001681 return false;
1682 }
1683
1684 return true;
1685}
1686
Calin Juravle7d765462017-09-04 15:57:10 -07001687// Return codes for identifying the reason why dexoptanalyzer was not invoked when processing
1688// secondary dex files. This return codes are returned by the child process created for
1689// analyzing secondary dex files in process_secondary_dex_dexopt.
Calin Juravle80a21252017-01-17 14:43:25 -08001690
Andreas Gampe194fe422018-02-28 20:16:19 -08001691enum DexoptAnalyzerSkipCodes {
1692 // The dexoptanalyzer was not invoked because of validation or IO errors.
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001693 // Specific errors are encoded in the name.
1694 kSecondaryDexDexoptAnalyzerSkippedValidatePath = 200,
1695 kSecondaryDexDexoptAnalyzerSkippedOpenZip = 201,
1696 kSecondaryDexDexoptAnalyzerSkippedPrepareDir = 202,
1697 kSecondaryDexDexoptAnalyzerSkippedOpenOutput = 203,
1698 kSecondaryDexDexoptAnalyzerSkippedFailExec = 204,
Andreas Gampe194fe422018-02-28 20:16:19 -08001699 // The dexoptanalyzer was not invoked because the dex file does not exist anymore.
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001700 kSecondaryDexDexoptAnalyzerSkippedNoFile = 205,
Andreas Gampe194fe422018-02-28 20:16:19 -08001701};
Calin Juravle7d765462017-09-04 15:57:10 -07001702
1703// Verifies the result of analyzing secondary dex files from process_secondary_dex_dexopt.
Calin Juravle80a21252017-01-17 14:43:25 -08001704// If the result is valid returns true and sets dexopt_needed_out to a valid value.
1705// Returns false for errors or unexpected result values.
Calin Juravle7d765462017-09-04 15:57:10 -07001706// The result is expected to be either one of SECONDARY_DEX_* codes or a valid exit code
1707// of dexoptanalyzer.
1708static bool process_secondary_dexoptanalyzer_result(const std::string& dex_path, int result,
Andreas Gampe194fe422018-02-28 20:16:19 -08001709 int* dexopt_needed_out, std::string* error_msg) {
Calin Juravle80a21252017-01-17 14:43:25 -08001710 // The result values are defined in dexoptanalyzer.
1711 switch (result) {
Calin Juravle7d765462017-09-04 15:57:10 -07001712 case 0: // dexoptanalyzer: no_dexopt_needed
Calin Juravle80a21252017-01-17 14:43:25 -08001713 *dexopt_needed_out = NO_DEXOPT_NEEDED; return true;
Calin Juravle7d765462017-09-04 15:57:10 -07001714 case 1: // dexoptanalyzer: dex2oat_from_scratch
Calin Juravle80a21252017-01-17 14:43:25 -08001715 *dexopt_needed_out = DEX2OAT_FROM_SCRATCH; return true;
Vladimir Marko1752a112018-09-03 18:15:16 +01001716 case 4: // dexoptanalyzer: dex2oat_for_bootimage_odex
Calin Juravle80a21252017-01-17 14:43:25 -08001717 *dexopt_needed_out = -DEX2OAT_FOR_BOOT_IMAGE; return true;
Vladimir Marko1752a112018-09-03 18:15:16 +01001718 case 5: // dexoptanalyzer: dex2oat_for_filter_odex
Calin Juravle80a21252017-01-17 14:43:25 -08001719 *dexopt_needed_out = -DEX2OAT_FOR_FILTER; return true;
Calin Juravle7d765462017-09-04 15:57:10 -07001720 case 2: // dexoptanalyzer: dex2oat_for_bootimage_oat
1721 case 3: // dexoptanalyzer: dex2oat_for_filter_oat
Andreas Gampe194fe422018-02-28 20:16:19 -08001722 *error_msg = StringPrintf("Dexoptanalyzer return the status of an oat file."
1723 " Expected odex file status for secondary dex %s"
1724 " : dexoptanalyzer result=%d",
1725 dex_path.c_str(),
1726 result);
Calin Juravle80a21252017-01-17 14:43:25 -08001727 return false;
Andreas Gampe194fe422018-02-28 20:16:19 -08001728 }
1729
1730 // Use a second switch for enum switch-case analysis.
1731 switch (static_cast<DexoptAnalyzerSkipCodes>(result)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001732 case kSecondaryDexDexoptAnalyzerSkippedNoFile:
Calin Juravle7d765462017-09-04 15:57:10 -07001733 // If the file does not exist there's no need for dexopt.
1734 *dexopt_needed_out = NO_DEXOPT_NEEDED;
1735 return true;
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001736
1737 case kSecondaryDexDexoptAnalyzerSkippedValidatePath:
1738 *error_msg = "Dexoptanalyzer path validation failed";
1739 return false;
1740 case kSecondaryDexDexoptAnalyzerSkippedOpenZip:
1741 *error_msg = "Dexoptanalyzer open zip failed";
1742 return false;
1743 case kSecondaryDexDexoptAnalyzerSkippedPrepareDir:
1744 *error_msg = "Dexoptanalyzer dir preparation failed";
1745 return false;
1746 case kSecondaryDexDexoptAnalyzerSkippedOpenOutput:
1747 *error_msg = "Dexoptanalyzer open output failed";
1748 return false;
1749 case kSecondaryDexDexoptAnalyzerSkippedFailExec:
1750 *error_msg = "Dexoptanalyzer failed to execute";
Calin Juravle80a21252017-01-17 14:43:25 -08001751 return false;
1752 }
Andreas Gampe194fe422018-02-28 20:16:19 -08001753
1754 *error_msg = StringPrintf("Unexpected result from analyzing secondary dex %s result=%d",
1755 dex_path.c_str(),
1756 result);
1757 return false;
Calin Juravle80a21252017-01-17 14:43:25 -08001758}
1759
Calin Juravle7d765462017-09-04 15:57:10 -07001760enum SecondaryDexAccess {
1761 kSecondaryDexAccessReadOk = 0,
1762 kSecondaryDexAccessDoesNotExist = 1,
1763 kSecondaryDexAccessPermissionError = 2,
1764 kSecondaryDexAccessIOError = 3
1765};
1766
1767static SecondaryDexAccess check_secondary_dex_access(const std::string& dex_path) {
1768 // Check if the path exists and can be read. If not, there's nothing to do.
1769 if (access(dex_path.c_str(), R_OK) == 0) {
1770 return kSecondaryDexAccessReadOk;
1771 } else {
1772 if (errno == ENOENT) {
1773 LOG(INFO) << "Secondary dex does not exist: " << dex_path;
1774 return kSecondaryDexAccessDoesNotExist;
1775 } else {
1776 PLOG(ERROR) << "Could not access secondary dex " << dex_path;
1777 return errno == EACCES
1778 ? kSecondaryDexAccessPermissionError
1779 : kSecondaryDexAccessIOError;
1780 }
1781 }
1782}
1783
1784static bool is_file_public(const std::string& filename) {
1785 struct stat file_stat;
1786 if (stat(filename.c_str(), &file_stat) == 0) {
1787 return (file_stat.st_mode & S_IROTH) != 0;
1788 }
1789 return false;
1790}
1791
1792// Create the oat file structure for the secondary dex 'dex_path' and assign
1793// the individual path component to the 'out_' parameters.
1794static bool create_secondary_dex_oat_layout(const std::string& dex_path, const std::string& isa,
Andreas Gampe194fe422018-02-28 20:16:19 -08001795 char* out_oat_dir, char* out_oat_isa_dir, char* out_oat_path, std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001796 size_t dirIndex = dex_path.rfind('/');
1797 if (dirIndex == std::string::npos) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001798 *error_msg = std::string("Unexpected dir structure for dex file ").append(dex_path);
Calin Juravle7d765462017-09-04 15:57:10 -07001799 return false;
1800 }
1801 // TODO(calin): we have similar computations in at lest 3 other places
1802 // (InstalldNativeService, otapropt and dexopt). Unify them and get rid of snprintf by
1803 // using string append.
1804 std::string apk_dir = dex_path.substr(0, dirIndex);
1805 snprintf(out_oat_dir, PKG_PATH_MAX, "%s/oat", apk_dir.c_str());
1806 snprintf(out_oat_isa_dir, PKG_PATH_MAX, "%s/%s", out_oat_dir, isa.c_str());
1807
1808 if (!create_oat_out_path(dex_path.c_str(), isa.c_str(), out_oat_dir,
1809 /*is_secondary_dex*/true, out_oat_path)) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001810 *error_msg = std::string("Could not create oat path for secondary dex ").append(dex_path);
Calin Juravle7d765462017-09-04 15:57:10 -07001811 return false;
1812 }
1813 return true;
1814}
1815
1816// Validate that the dexopt_flags contain a valid storage flag and convert that to an installd
1817// recognized storage flags (FLAG_STORAGE_CE or FLAG_STORAGE_DE).
Andreas Gampe194fe422018-02-28 20:16:19 -08001818static bool validate_dexopt_storage_flags(int dexopt_flags,
1819 int* out_storage_flag,
1820 std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001821 if ((dexopt_flags & DEXOPT_STORAGE_CE) != 0) {
1822 *out_storage_flag = FLAG_STORAGE_CE;
1823 if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) {
Andreas Gampe194fe422018-02-28 20:16:19 -08001824 *error_msg = "Ambiguous secondary dex storage flag. Both, CE and DE, flags are set";
Calin Juravle7d765462017-09-04 15:57:10 -07001825 return false;
1826 }
1827 } else if ((dexopt_flags & DEXOPT_STORAGE_DE) != 0) {
1828 *out_storage_flag = FLAG_STORAGE_DE;
1829 } else {
Andreas Gampe194fe422018-02-28 20:16:19 -08001830 *error_msg = "Secondary dex storage flag must be set";
Calin Juravle7d765462017-09-04 15:57:10 -07001831 return false;
1832 }
1833 return true;
1834}
1835
David Brazdil4f6027a2019-03-19 11:44:21 +00001836static bool get_class_loader_context_dex_paths(const char* class_loader_context, int uid,
1837 /* out */ std::vector<std::string>* context_dex_paths) {
1838 if (class_loader_context == nullptr) {
1839 return true;
1840 }
1841
1842 LOG(DEBUG) << "Getting dex paths for context " << class_loader_context;
1843
1844 // Pipe to get the hash result back from our child process.
1845 unique_fd pipe_read, pipe_write;
1846 if (!Pipe(&pipe_read, &pipe_write)) {
1847 PLOG(ERROR) << "Failed to create pipe";
1848 return false;
1849 }
1850
1851 pid_t pid = fork();
1852 if (pid == 0) {
1853 // child -- drop privileges before continuing.
1854 drop_capabilities(uid);
1855
1856 // Route stdout to `pipe_write`
1857 while ((dup2(pipe_write, STDOUT_FILENO) == -1) && (errno == EINTR)) {}
1858 pipe_write.reset();
1859 pipe_read.reset();
1860
1861 RunDexoptAnalyzer run_dexopt_analyzer(class_loader_context);
1862 run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec);
1863 }
1864
1865 /* parent */
1866 pipe_write.reset();
1867
1868 std::string str_dex_paths;
1869 if (!ReadFdToString(pipe_read, &str_dex_paths)) {
1870 PLOG(ERROR) << "Failed to read from pipe";
1871 return false;
1872 }
1873 pipe_read.reset();
1874
1875 int return_code = wait_child(pid);
1876 if (!WIFEXITED(return_code)) {
1877 PLOG(ERROR) << "Error waiting for child dexoptanalyzer process";
1878 return false;
1879 }
1880
1881 constexpr int kFlattenClassLoaderContextSuccess = 50;
1882 return_code = WEXITSTATUS(return_code);
1883 if (return_code != kFlattenClassLoaderContextSuccess) {
1884 LOG(ERROR) << "Dexoptanalyzer could not flatten class loader context, code=" << return_code;
1885 return false;
1886 }
1887
1888 if (!str_dex_paths.empty()) {
1889 *context_dex_paths = android::base::Split(str_dex_paths, ":");
1890 }
1891 return true;
1892}
1893
1894static int open_dex_paths(const std::vector<std::string>& dex_paths,
1895 /* out */ std::vector<unique_fd>* zip_fds, /* out */ std::string* error_msg) {
1896 for (const std::string& dex_path : dex_paths) {
1897 zip_fds->emplace_back(open(dex_path.c_str(), O_RDONLY));
1898 if (zip_fds->back().get() < 0) {
1899 *error_msg = StringPrintf(
1900 "installd cannot open '%s' for input during dexopt", dex_path.c_str());
1901 if (errno == ENOENT) {
1902 return kSecondaryDexDexoptAnalyzerSkippedNoFile;
1903 } else {
1904 return kSecondaryDexDexoptAnalyzerSkippedOpenZip;
1905 }
1906 }
1907 }
1908 return 0;
1909}
1910
1911static std::string join_fds(const std::vector<unique_fd>& fds) {
1912 std::stringstream ss;
1913 bool is_first = true;
1914 for (const unique_fd& fd : fds) {
1915 if (is_first) {
1916 is_first = false;
1917 } else {
1918 ss << ":";
1919 }
1920 ss << fd.get();
1921 }
1922 return ss.str();
1923}
1924
Calin Juravlec9eab382017-01-25 01:17:17 -08001925// Processes the dex_path as a secondary dex files and return true if the path dex file should
Calin Juravle80a21252017-01-17 14:43:25 -08001926// be compiled. Returns false for errors (logged) or true if the secondary dex path was process
1927// successfully.
Calin Juravleebc8a792017-04-04 20:21:05 -07001928// When returning true, the output parameters will be:
1929// - is_public_out: whether or not the oat file should not be made public
1930// - dexopt_needed_out: valid OatFileAsssitant::DexOptNeeded
1931// - oat_dir_out: the oat dir path where the oat file should be stored
Calin Juravle7d765462017-09-04 15:57:10 -07001932static bool process_secondary_dex_dexopt(const std::string& dex_path, const char* pkgname,
Calin Juravle80a21252017-01-17 14:43:25 -08001933 int dexopt_flags, const char* volume_uuid, int uid, const char* instruction_set,
Calin Juravleebc8a792017-04-04 20:21:05 -07001934 const char* compiler_filter, bool* is_public_out, int* dexopt_needed_out,
Andreas Gampe194fe422018-02-28 20:16:19 -08001935 std::string* oat_dir_out, bool downgrade, const char* class_loader_context,
David Brazdil4f6027a2019-03-19 11:44:21 +00001936 const std::vector<std::string>& context_dex_paths, /* out */ std::string* error_msg) {
Calin Juravle7d765462017-09-04 15:57:10 -07001937 LOG(DEBUG) << "Processing secondary dex path " << dex_path;
Calin Juravle80a21252017-01-17 14:43:25 -08001938 int storage_flag;
Andreas Gampe194fe422018-02-28 20:16:19 -08001939 if (!validate_dexopt_storage_flags(dexopt_flags, &storage_flag, error_msg)) {
1940 LOG(ERROR) << *error_msg;
Calin Juravle80a21252017-01-17 14:43:25 -08001941 return false;
1942 }
Calin Juravle7d765462017-09-04 15:57:10 -07001943 // Compute the oat dir as it's not easy to extract it from the child computation.
1944 char oat_path[PKG_PATH_MAX];
1945 char oat_dir[PKG_PATH_MAX];
1946 char oat_isa_dir[PKG_PATH_MAX];
1947 if (!create_secondary_dex_oat_layout(
Andreas Gampe194fe422018-02-28 20:16:19 -08001948 dex_path, instruction_set, oat_dir, oat_isa_dir, oat_path, error_msg)) {
1949 LOG(ERROR) << "Could not create secondary odex layout: " << *error_msg;
Calin Juravled23dee72017-07-06 16:29:11 -07001950 return false;
1951 }
Calin Juravle7d765462017-09-04 15:57:10 -07001952 oat_dir_out->assign(oat_dir);
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07001953
Calin Juravle80a21252017-01-17 14:43:25 -08001954 pid_t pid = fork();
1955 if (pid == 0) {
1956 // child -- drop privileges before continuing.
1957 drop_capabilities(uid);
Calin Juravle7d765462017-09-04 15:57:10 -07001958
1959 // Validate the path structure.
1960 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid, uid, storage_flag)) {
1961 LOG(ERROR) << "Could not validate secondary dex path " << dex_path;
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001962 _exit(kSecondaryDexDexoptAnalyzerSkippedValidatePath);
Calin Juravle7d765462017-09-04 15:57:10 -07001963 }
1964
1965 // Open the dex file.
1966 unique_fd zip_fd;
1967 zip_fd.reset(open(dex_path.c_str(), O_RDONLY));
1968 if (zip_fd.get() < 0) {
1969 if (errno == ENOENT) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001970 _exit(kSecondaryDexDexoptAnalyzerSkippedNoFile);
Calin Juravle7d765462017-09-04 15:57:10 -07001971 } else {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001972 _exit(kSecondaryDexDexoptAnalyzerSkippedOpenZip);
Calin Juravle7d765462017-09-04 15:57:10 -07001973 }
1974 }
1975
David Brazdil4f6027a2019-03-19 11:44:21 +00001976 // Open class loader context dex files.
1977 std::vector<unique_fd> context_zip_fds;
1978 int open_dex_paths_rc = open_dex_paths(context_dex_paths, &context_zip_fds, error_msg);
1979 if (open_dex_paths_rc != 0) {
1980 _exit(open_dex_paths_rc);
1981 }
1982
Calin Juravle7d765462017-09-04 15:57:10 -07001983 // Prepare the oat directories.
1984 if (!prepare_secondary_dex_oat_dir(dex_path, uid, instruction_set)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001985 _exit(kSecondaryDexDexoptAnalyzerSkippedPrepareDir);
Calin Juravle7d765462017-09-04 15:57:10 -07001986 }
1987
1988 // Open the vdex/oat files if any.
1989 unique_fd oat_file_fd;
1990 unique_fd vdex_file_fd;
1991 if (!maybe_open_oat_and_vdex_file(dex_path,
1992 *oat_dir_out,
1993 instruction_set,
1994 true /* is_secondary_dex */,
1995 &oat_file_fd,
1996 &vdex_file_fd)) {
Andreas Gampe3008bbe2018-02-28 20:24:48 -08001997 _exit(kSecondaryDexDexoptAnalyzerSkippedOpenOutput);
Calin Juravle7d765462017-09-04 15:57:10 -07001998 }
1999
2000 // Analyze profiles.
Calin Juravle824a64d2018-01-18 20:23:17 -08002001 bool profile_was_updated = analyze_profiles(uid, pkgname, dex_path,
2002 /*is_secondary_dex*/true);
Calin Juravle7d765462017-09-04 15:57:10 -07002003
2004 // Run dexoptanalyzer to get dexopt_needed code. This is not expected to return.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002005 // Note that we do not do it before the fork since opening the files is required to happen
2006 // after forking.
2007 RunDexoptAnalyzer run_dexopt_analyzer(dex_path,
2008 vdex_file_fd.get(),
2009 oat_file_fd.get(),
2010 zip_fd.get(),
2011 instruction_set,
2012 compiler_filter, profile_was_updated,
2013 downgrade,
David Brazdil4f6027a2019-03-19 11:44:21 +00002014 class_loader_context,
2015 join_fds(context_zip_fds));
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002016 run_dexopt_analyzer.Exec(kSecondaryDexDexoptAnalyzerSkippedFailExec);
Calin Juravle80a21252017-01-17 14:43:25 -08002017 }
2018
2019 /* parent */
Calin Juravle80a21252017-01-17 14:43:25 -08002020 int result = wait_child(pid);
2021 if (!WIFEXITED(result)) {
Andreas Gampe194fe422018-02-28 20:16:19 -08002022 *error_msg = StringPrintf("dexoptanalyzer failed for path %s: 0x%04x",
2023 dex_path.c_str(),
2024 result);
2025 LOG(ERROR) << *error_msg;
Calin Juravle80a21252017-01-17 14:43:25 -08002026 return false;
2027 }
2028 result = WEXITSTATUS(result);
Calin Juravle7d765462017-09-04 15:57:10 -07002029 // Check that we successfully executed dexoptanalyzer.
Andreas Gampe194fe422018-02-28 20:16:19 -08002030 bool success = process_secondary_dexoptanalyzer_result(dex_path,
2031 result,
2032 dexopt_needed_out,
2033 error_msg);
2034 if (!success) {
2035 LOG(ERROR) << *error_msg;
2036 }
Calin Juravle7d765462017-09-04 15:57:10 -07002037
2038 LOG(DEBUG) << "Processed secondary dex file " << dex_path << " result=" << result;
2039
Calin Juravle80a21252017-01-17 14:43:25 -08002040 // Run dexopt only if needed or forced.
Calin Juravle7d765462017-09-04 15:57:10 -07002041 // Note that dexoptanalyzer is executed even if force compilation is enabled (because it
2042 // makes the code simpler; force compilation is only needed during tests).
2043 if (success &&
Andreas Gampe3008bbe2018-02-28 20:24:48 -08002044 (result != kSecondaryDexDexoptAnalyzerSkippedNoFile) &&
Calin Juravle7d765462017-09-04 15:57:10 -07002045 ((dexopt_flags & DEXOPT_FORCE) != 0)) {
Calin Juravle80a21252017-01-17 14:43:25 -08002046 *dexopt_needed_out = DEX2OAT_FROM_SCRATCH;
2047 }
2048
Calin Juravle7d765462017-09-04 15:57:10 -07002049 // Check if we should make the oat file public.
2050 // Note that if the dex file is not public the compiled code cannot be made public.
2051 // It is ok to check this flag outside in the parent process.
2052 *is_public_out = ((dexopt_flags & DEXOPT_PUBLIC) != 0) && is_file_public(dex_path);
2053
Calin Juravle80a21252017-01-17 14:43:25 -08002054 return success;
2055}
2056
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002057static std::string format_dexopt_error(int status, const char* dex_path) {
2058 if (WIFEXITED(status)) {
2059 int int_code = WEXITSTATUS(status);
2060 const char* code_name = get_return_code_name(static_cast<DexoptReturnCodes>(int_code));
2061 if (code_name != nullptr) {
2062 return StringPrintf("Dex2oat invocation for %s failed: %s", dex_path, code_name);
2063 }
2064 }
2065 return StringPrintf("Dex2oat invocation for %s failed with 0x%04x", dex_path, status);
Andreas Gampe023b2242018-02-28 16:03:25 -08002066}
2067
Calin Juravlec9eab382017-01-25 01:17:17 -08002068int dexopt(const char* dex_path, uid_t uid, const char* pkgname, const char* instruction_set,
Calin Juravle80a21252017-01-17 14:43:25 -08002069 int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
Calin Juravle52c45822017-07-13 22:50:21 -07002070 const char* volume_uuid, const char* class_loader_context, const char* se_info,
Calin Juravle62c5a372018-02-01 17:03:23 +00002071 bool downgrade, int target_sdk_version, const char* profile_name,
Andreas Gampe023b2242018-02-28 16:03:25 -08002072 const char* dex_metadata_path, const char* compilation_reason, std::string* error_msg) {
Calin Juravle7a570e82017-01-14 16:23:30 -08002073 CHECK(pkgname != nullptr);
2074 CHECK(pkgname[0] != 0);
Andreas Gampe023b2242018-02-28 16:03:25 -08002075 CHECK(error_msg != nullptr);
Andreas Gamped32eec22018-02-28 16:02:51 -08002076 CHECK_EQ(dexopt_flags & ~DEXOPT_MASK, 0)
2077 << "dexopt flags contains unknown fields: " << dexopt_flags;
Calin Juravle7a570e82017-01-14 16:23:30 -08002078
Calin Juravled23dee72017-07-06 16:29:11 -07002079 if (!validate_dex_path_size(dex_path)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002080 *error_msg = StringPrintf("Failed to validate %s", dex_path);
Calin Juravle52c45822017-07-13 22:50:21 -07002081 return -1;
2082 }
2083
2084 if (class_loader_context != nullptr && strlen(class_loader_context) > PKG_PATH_MAX) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002085 *error_msg = StringPrintf("Class loader context exceeds the allowed size: %s",
2086 class_loader_context);
2087 LOG(ERROR) << *error_msg;
Calin Juravle52c45822017-07-13 22:50:21 -07002088 return -1;
Calin Juravled23dee72017-07-06 16:29:11 -07002089 }
2090
Calin Juravleebc8a792017-04-04 20:21:05 -07002091 bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0;
Calin Juravle7a570e82017-01-14 16:23:30 -08002092 bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
2093 bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
2094 bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0;
Calin Juravle80a21252017-01-17 14:43:25 -08002095 bool is_secondary_dex = (dexopt_flags & DEXOPT_SECONDARY_DEX) != 0;
Andreas Gampea73a0cb2017-11-02 18:14:42 -07002096 bool background_job_compile = (dexopt_flags & DEXOPT_IDLE_BACKGROUND_JOB) != 0;
David Brazdil52249162018-02-12 18:04:59 -08002097 bool enable_hidden_api_checks = (dexopt_flags & DEXOPT_ENABLE_HIDDEN_API_CHECKS) != 0;
Mathieu Chartierf69c2f72018-03-06 13:55:58 -08002098 bool generate_compact_dex = (dexopt_flags & DEXOPT_GENERATE_COMPACT_DEX) != 0;
Mathieu Chartier1dc3dfa2018-03-12 17:55:06 -07002099 bool generate_app_image = (dexopt_flags & DEXOPT_GENERATE_APP_IMAGE) != 0;
Patrick Baumann2271b3e2020-04-14 17:03:00 -07002100 bool for_restore = (dexopt_flags & DEXOPT_FOR_RESTORE) != 0;
Calin Juravle80a21252017-01-17 14:43:25 -08002101
2102 // Check if we're dealing with a secondary dex file and if we need to compile it.
2103 std::string oat_dir_str;
David Brazdil4f6027a2019-03-19 11:44:21 +00002104 std::vector<std::string> context_dex_paths;
Calin Juravle80a21252017-01-17 14:43:25 -08002105 if (is_secondary_dex) {
David Brazdil4f6027a2019-03-19 11:44:21 +00002106 if (!get_class_loader_context_dex_paths(class_loader_context, uid, &context_dex_paths)) {
2107 *error_msg = "Failed acquiring context dex paths";
2108 return -1; // We had an error, logged in the process method.
2109 }
2110
Calin Juravlec9eab382017-01-25 01:17:17 -08002111 if (process_secondary_dex_dexopt(dex_path, pkgname, dexopt_flags, volume_uuid, uid,
Calin Juravleebc8a792017-04-04 20:21:05 -07002112 instruction_set, compiler_filter, &is_public, &dexopt_needed, &oat_dir_str,
David Brazdil4f6027a2019-03-19 11:44:21 +00002113 downgrade, class_loader_context, context_dex_paths, error_msg)) {
Calin Juravle80a21252017-01-17 14:43:25 -08002114 oat_dir = oat_dir_str.c_str();
2115 if (dexopt_needed == NO_DEXOPT_NEEDED) {
2116 return 0; // Nothing to do, report success.
2117 }
2118 } else {
Andreas Gampe194fe422018-02-28 20:16:19 -08002119 if (error_msg->empty()) { // TODO: Make this a CHECK.
2120 *error_msg = "Failed processing secondary.";
2121 }
Calin Juravle80a21252017-01-17 14:43:25 -08002122 return -1; // We had an error, logged in the process method.
2123 }
2124 } else {
David Brazdil4f6027a2019-03-19 11:44:21 +00002125 // Currently these flags are only used for secondary dex files.
Calin Juravlec9eab382017-01-25 01:17:17 -08002126 // Verify that they are not set for primary apks.
Calin Juravle80a21252017-01-17 14:43:25 -08002127 CHECK((dexopt_flags & DEXOPT_STORAGE_CE) == 0);
2128 CHECK((dexopt_flags & DEXOPT_STORAGE_DE) == 0);
2129 }
Calin Juravle7a570e82017-01-14 16:23:30 -08002130
2131 // Open the input file.
Calin Juravle1a0af3b2017-03-09 14:33:33 -08002132 unique_fd input_fd(open(dex_path, O_RDONLY, 0));
Calin Juravle7a570e82017-01-14 16:23:30 -08002133 if (input_fd.get() < 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002134 *error_msg = StringPrintf("installd cannot open '%s' for input during dexopt", dex_path);
2135 LOG(ERROR) << *error_msg;
Calin Juravle7a570e82017-01-14 16:23:30 -08002136 return -1;
2137 }
2138
David Brazdil4f6027a2019-03-19 11:44:21 +00002139 // Open class loader context dex files.
2140 std::vector<unique_fd> context_input_fds;
2141 if (open_dex_paths(context_dex_paths, &context_input_fds, error_msg) != 0) {
2142 LOG(ERROR) << *error_msg;
2143 return -1;
2144 }
2145
Calin Juravle7a570e82017-01-14 16:23:30 -08002146 // Create the output OAT file.
2147 char out_oat_path[PKG_PATH_MAX];
Calin Juravlec9eab382017-01-25 01:17:17 -08002148 Dex2oatFileWrapper out_oat_fd = open_oat_out_file(dex_path, oat_dir, is_public, uid,
Calin Juravle80a21252017-01-17 14:43:25 -08002149 instruction_set, is_secondary_dex, out_oat_path);
Calin Juravle7a570e82017-01-14 16:23:30 -08002150 if (out_oat_fd.get() < 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002151 *error_msg = "Could not open out oat file.";
Calin Juravle7a570e82017-01-14 16:23:30 -08002152 return -1;
2153 }
2154
2155 // Open vdex files.
2156 Dex2oatFileWrapper in_vdex_fd;
2157 Dex2oatFileWrapper out_vdex_fd;
Shubham Ajmerab6bcd222017-10-19 10:08:03 -07002158 if (!open_vdex_files_for_dex2oat(dex_path, out_oat_path, dexopt_needed, instruction_set,
2159 is_public, uid, is_secondary_dex, profile_guided, &in_vdex_fd, &out_vdex_fd)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002160 *error_msg = "Could not open vdex files.";
Jeff Sharkey90aff262016-12-12 14:28:24 -07002161 return -1;
2162 }
2163
Calin Juravlecb556e32017-04-04 20:22:50 -07002164 // Ensure that the oat dir and the compiler artifacts of secondary dex files have the correct
2165 // selinux context (we generate them on the fly during the dexopt invocation and they don't
2166 // fully inherit their parent context).
2167 // Note that for primary apk the oat files are created before, in a separate installd
2168 // call which also does the restorecon. TODO(calin): unify the paths.
2169 if (is_secondary_dex) {
2170 if (selinux_android_restorecon_pkgdir(oat_dir, se_info, uid,
2171 SELINUX_ANDROID_RESTORECON_RECURSE)) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002172 *error_msg = std::string("Failed to restorecon ").append(oat_dir);
2173 LOG(ERROR) << *error_msg;
Calin Juravlecb556e32017-04-04 20:22:50 -07002174 return -1;
2175 }
2176 }
2177
Jeff Sharkey90aff262016-12-12 14:28:24 -07002178 // Create a swap file if necessary.
Calin Juravle1a0af3b2017-03-09 14:33:33 -08002179 unique_fd swap_fd = maybe_open_dexopt_swap_file(out_oat_path);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002180
Calin Juravle7a570e82017-01-14 16:23:30 -08002181 // Open the reference profile if needed.
Calin Juravle114f0812017-03-08 19:05:07 -08002182 Dex2oatFileWrapper reference_profile_fd = maybe_open_reference_profile(
Calin Juravle824a64d2018-01-18 20:23:17 -08002183 pkgname, dex_path, profile_name, profile_guided, is_public, uid, is_secondary_dex);
Calin Juravle7a570e82017-01-14 16:23:30 -08002184
liulvping61907742018-08-21 09:36:52 +08002185 if (reference_profile_fd.get() == -1) {
2186 // We don't create an app image without reference profile since there is no speedup from
2187 // loading it in that case and instead will be a small overhead.
2188 generate_app_image = false;
2189 }
2190
2191 // Create the app image file if needed.
2192 Dex2oatFileWrapper image_fd = maybe_open_app_image(
2193 out_oat_path, generate_app_image, is_public, uid, is_secondary_dex);
2194
Calin Juravle62c5a372018-02-01 17:03:23 +00002195 unique_fd dex_metadata_fd;
2196 if (dex_metadata_path != nullptr) {
2197 dex_metadata_fd.reset(TEMP_FAILURE_RETRY(open(dex_metadata_path, O_RDONLY | O_NOFOLLOW)));
2198 if (dex_metadata_fd.get() < 0) {
2199 PLOG(ERROR) << "Failed to open dex metadata file " << dex_metadata_path;
2200 }
2201 }
2202
Andreas Gampe023b2242018-02-28 16:03:25 -08002203 LOG(VERBOSE) << "DexInv: --- BEGIN '" << dex_path << "' ---";
Jeff Sharkey90aff262016-12-12 14:28:24 -07002204
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002205 RunDex2Oat runner(input_fd.get(),
2206 out_oat_fd.get(),
2207 in_vdex_fd.get(),
2208 out_vdex_fd.get(),
2209 image_fd.get(),
2210 dex_path,
2211 out_oat_path,
2212 swap_fd.get(),
2213 instruction_set,
2214 compiler_filter,
2215 debuggable,
2216 boot_complete,
Patrick Baumann2271b3e2020-04-14 17:03:00 -07002217 for_restore,
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002218 background_job_compile,
2219 reference_profile_fd.get(),
2220 class_loader_context,
David Brazdil4f6027a2019-03-19 11:44:21 +00002221 join_fds(context_input_fds),
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002222 target_sdk_version,
2223 enable_hidden_api_checks,
2224 generate_compact_dex,
2225 dex_metadata_fd.get(),
2226 compilation_reason);
2227
Jeff Sharkey90aff262016-12-12 14:28:24 -07002228 pid_t pid = fork();
2229 if (pid == 0) {
2230 /* child -- drop privileges before continuing */
2231 drop_capabilities(uid);
2232
Richard Uhler76cc0272016-12-08 10:46:35 +00002233 SetDex2OatScheduling(boot_complete);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002234 if (flock(out_oat_fd.get(), LOCK_EX | LOCK_NB) != 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002235 PLOG(ERROR) << "flock(" << out_oat_path << ") failed";
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002236 _exit(DexoptReturnCodes::kFlock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002237 }
2238
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002239 runner.Exec(DexoptReturnCodes::kDex2oatExec);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002240 } else {
2241 int res = wait_child(pid);
2242 if (res == 0) {
Andreas Gampe023b2242018-02-28 16:03:25 -08002243 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' (success) ---";
Jeff Sharkey90aff262016-12-12 14:28:24 -07002244 } else {
Andreas Gampe023b2242018-02-28 16:03:25 -08002245 LOG(VERBOSE) << "DexInv: --- END '" << dex_path << "' --- status=0x"
2246 << std::hex << std::setw(4) << res << ", process failed";
2247 *error_msg = format_dexopt_error(res, dex_path);
Andreas Gampe013f02e2017-03-20 18:36:54 -07002248 return res;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002249 }
2250 }
2251
Calin Juravlec9eab382017-01-25 01:17:17 -08002252 update_out_oat_access_times(dex_path, out_oat_path);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002253
2254 // We've been successful, don't delete output.
2255 out_oat_fd.SetCleanup(false);
Calin Juravle7a570e82017-01-14 16:23:30 -08002256 out_vdex_fd.SetCleanup(false);
Jeff Sharkey90aff262016-12-12 14:28:24 -07002257 image_fd.SetCleanup(false);
2258 reference_profile_fd.SetCleanup(false);
2259
2260 return 0;
2261}
2262
Calin Juravlec9eab382017-01-25 01:17:17 -08002263// Try to remove the given directory. Log an error if the directory exists
2264// and is empty but could not be removed.
2265static bool rmdir_if_empty(const char* dir) {
2266 if (rmdir(dir) == 0) {
2267 return true;
2268 }
2269 if (errno == ENOENT || errno == ENOTEMPTY) {
2270 return true;
2271 }
2272 PLOG(ERROR) << "Failed to remove dir: " << dir;
2273 return false;
2274}
2275
2276// Try to unlink the given file. Log an error if the file exists and could not
2277// be unlinked.
2278static bool unlink_if_exists(const std::string& file) {
2279 if (unlink(file.c_str()) == 0) {
2280 return true;
2281 }
2282 if (errno == ENOENT) {
2283 return true;
2284
2285 }
2286 PLOG(ERROR) << "Could not unlink: " << file;
2287 return false;
2288}
2289
Calin Juravle7d765462017-09-04 15:57:10 -07002290enum ReconcileSecondaryDexResult {
2291 kReconcileSecondaryDexExists = 0,
2292 kReconcileSecondaryDexCleanedUp = 1,
2293 kReconcileSecondaryDexValidationError = 2,
2294 kReconcileSecondaryDexCleanUpError = 3,
2295 kReconcileSecondaryDexAccessIOError = 4,
2296};
Calin Juravlec9eab382017-01-25 01:17:17 -08002297
2298// Reconcile the secondary dex 'dex_path' and its generated oat files.
2299// Return true if all the parameters are valid and the secondary dex file was
2300// processed successfully (i.e. the dex_path either exists, or if not, its corresponding
2301// oat/vdex/art files where deleted successfully). In this case, out_secondary_dex_exists
2302// will be true if the secondary dex file still exists. If the secondary dex file does not exist,
2303// the method cleans up any previously generated compiler artifacts (oat, vdex, art).
2304// Return false if there were errors during processing. In this case
2305// out_secondary_dex_exists will be set to false.
2306bool reconcile_secondary_dex_file(const std::string& dex_path,
2307 const std::string& pkgname, int uid, const std::vector<std::string>& isas,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002308 const std::optional<std::string>& volume_uuid, int storage_flag,
Calin Juravlec9eab382017-01-25 01:17:17 -08002309 /*out*/bool* out_secondary_dex_exists) {
Calin Juravle7d765462017-09-04 15:57:10 -07002310 *out_secondary_dex_exists = false; // start by assuming the file does not exist.
Calin Juravlec9eab382017-01-25 01:17:17 -08002311 if (isas.size() == 0) {
2312 LOG(ERROR) << "reconcile_secondary_dex_file called with empty isas vector";
2313 return false;
2314 }
2315
Calin Juravle7d765462017-09-04 15:57:10 -07002316 if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) {
2317 LOG(ERROR) << "reconcile_secondary_dex_file called with invalid storage_flag: "
2318 << storage_flag;
Calin Juravlec9eab382017-01-25 01:17:17 -08002319 return false;
2320 }
2321
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002322 // As a security measure we want to unlink art artifacts with the reduced capabilities
2323 // of the package user id. So we fork and drop capabilities in the child.
2324 pid_t pid = fork();
2325 if (pid == 0) {
Calin Juravle7d765462017-09-04 15:57:10 -07002326 /* child -- drop privileges before continuing */
2327 drop_capabilities(uid);
2328
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002329 const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr;
Greg Kaiser8042c372019-03-26 06:23:19 -07002330 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr,
Calin Juravle7d765462017-09-04 15:57:10 -07002331 uid, storage_flag)) {
2332 LOG(ERROR) << "Could not validate secondary dex path " << dex_path;
2333 _exit(kReconcileSecondaryDexValidationError);
2334 }
2335
2336 SecondaryDexAccess access_check = check_secondary_dex_access(dex_path);
2337 switch (access_check) {
2338 case kSecondaryDexAccessDoesNotExist:
2339 // File does not exist. Proceed with cleaning.
2340 break;
2341 case kSecondaryDexAccessReadOk: _exit(kReconcileSecondaryDexExists);
2342 case kSecondaryDexAccessIOError: _exit(kReconcileSecondaryDexAccessIOError);
2343 case kSecondaryDexAccessPermissionError: _exit(kReconcileSecondaryDexValidationError);
2344 default:
2345 LOG(ERROR) << "Unexpected result from check_secondary_dex_access: " << access_check;
2346 _exit(kReconcileSecondaryDexValidationError);
2347 }
2348
2349 // The secondary dex does not exist anymore or it's. Clear any generated files.
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002350 char oat_path[PKG_PATH_MAX];
2351 char oat_dir[PKG_PATH_MAX];
2352 char oat_isa_dir[PKG_PATH_MAX];
2353 bool result = true;
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002354 for (size_t i = 0; i < isas.size(); i++) {
Andreas Gampe194fe422018-02-28 20:16:19 -08002355 std::string error_msg;
Calin Juravle7d765462017-09-04 15:57:10 -07002356 if (!create_secondary_dex_oat_layout(
Andreas Gampe194fe422018-02-28 20:16:19 -08002357 dex_path,isas[i], oat_dir, oat_isa_dir, oat_path, &error_msg)) {
2358 LOG(ERROR) << error_msg;
Calin Juravle7d765462017-09-04 15:57:10 -07002359 _exit(kReconcileSecondaryDexValidationError);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002360 }
Calin Juravle51314092017-05-18 15:33:05 -07002361
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002362 // Delete oat/vdex/art files.
2363 result = unlink_if_exists(oat_path) && result;
2364 result = unlink_if_exists(create_vdex_filename(oat_path)) && result;
2365 result = unlink_if_exists(create_image_filename(oat_path)) && result;
Calin Juravlec9eab382017-01-25 01:17:17 -08002366
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002367 // Delete profiles.
2368 std::string current_profile = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -08002369 multiuser_get_user_id(uid), pkgname, dex_path, /*is_secondary*/true);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002370 std::string reference_profile = create_reference_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -08002371 pkgname, dex_path, /*is_secondary*/true);
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002372 result = unlink_if_exists(current_profile) && result;
2373 result = unlink_if_exists(reference_profile) && result;
Calin Juravle51314092017-05-18 15:33:05 -07002374
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002375 // We upgraded once the location of current profile for secondary dex files.
2376 // Check for any previous left-overs and remove them as well.
2377 std::string old_current_profile = dex_path + ".prof";
2378 result = unlink_if_exists(old_current_profile);
Calin Juravle3760ad32017-07-27 16:31:55 -07002379
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002380 // Try removing the directories as well, they might be empty.
2381 result = rmdir_if_empty(oat_isa_dir) && result;
2382 result = rmdir_if_empty(oat_dir) && result;
2383 }
Calin Juravle7d765462017-09-04 15:57:10 -07002384 if (!result) {
2385 PLOG(ERROR) << "Failed to clean secondary dex artifacts for location " << dex_path;
2386 }
2387 _exit(result ? kReconcileSecondaryDexCleanedUp : kReconcileSecondaryDexAccessIOError);
Calin Juravlec9eab382017-01-25 01:17:17 -08002388 }
2389
Shubham Ajmerae6d7ad52017-08-25 13:07:44 -07002390 int return_code = wait_child(pid);
Calin Juravle7d765462017-09-04 15:57:10 -07002391 if (!WIFEXITED(return_code)) {
2392 LOG(WARNING) << "reconcile dex failed for location " << dex_path << ": " << return_code;
2393 } else {
2394 return_code = WEXITSTATUS(return_code);
2395 }
2396
2397 LOG(DEBUG) << "Reconcile secondary dex path " << dex_path << " result=" << return_code;
2398
2399 switch (return_code) {
2400 case kReconcileSecondaryDexCleanedUp:
2401 case kReconcileSecondaryDexValidationError:
2402 // If we couldn't validate assume the dex file does not exist.
2403 // This will purge the entry from the PM records.
2404 *out_secondary_dex_exists = false;
2405 return true;
2406 case kReconcileSecondaryDexExists:
2407 *out_secondary_dex_exists = true;
2408 return true;
2409 case kReconcileSecondaryDexAccessIOError:
2410 // We had an access IO error.
2411 // Return false so that we can try again.
2412 // The value of out_secondary_dex_exists does not matter in this case and by convention
2413 // is set to false.
2414 *out_secondary_dex_exists = false;
2415 return false;
2416 default:
2417 LOG(ERROR) << "Unexpected code from reconcile_secondary_dex_file: " << return_code;
2418 *out_secondary_dex_exists = false;
2419 return false;
2420 }
Calin Juravlec9eab382017-01-25 01:17:17 -08002421}
2422
Alan Stokesa25d90c2017-10-16 10:56:00 +01002423// Compute and return the hash (SHA-256) of the secondary dex file at dex_path.
2424// Returns true if all parameters are valid and the hash successfully computed and stored in
2425// out_secondary_dex_hash.
2426// Also returns true with an empty hash if the file does not currently exist or is not accessible to
2427// the app.
2428// For any other errors (e.g. if any of the parameters are invalid) returns false.
2429bool hash_secondary_dex_file(const std::string& dex_path, const std::string& pkgname, int uid,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002430 const std::optional<std::string>& volume_uuid, int storage_flag,
Alan Stokesa25d90c2017-10-16 10:56:00 +01002431 std::vector<uint8_t>* out_secondary_dex_hash) {
2432 out_secondary_dex_hash->clear();
2433
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002434 const char* volume_uuid_cstr = volume_uuid ? volume_uuid->c_str() : nullptr;
Alan Stokesa25d90c2017-10-16 10:56:00 +01002435
2436 if (storage_flag != FLAG_STORAGE_CE && storage_flag != FLAG_STORAGE_DE) {
2437 LOG(ERROR) << "hash_secondary_dex_file called with invalid storage_flag: "
2438 << storage_flag;
2439 return false;
2440 }
2441
2442 // Pipe to get the hash result back from our child process.
2443 unique_fd pipe_read, pipe_write;
2444 if (!Pipe(&pipe_read, &pipe_write)) {
2445 PLOG(ERROR) << "Failed to create pipe";
2446 return false;
2447 }
2448
2449 // Fork so that actual access to the files is done in the app's own UID, to ensure we only
2450 // access data the app itself can access.
2451 pid_t pid = fork();
2452 if (pid == 0) {
2453 // child -- drop privileges before continuing
2454 drop_capabilities(uid);
2455 pipe_read.reset();
2456
2457 if (!validate_secondary_dex_path(pkgname, dex_path, volume_uuid_cstr, uid, storage_flag)) {
2458 LOG(ERROR) << "Could not validate secondary dex path " << dex_path;
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002459 _exit(DexoptReturnCodes::kHashValidatePath);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002460 }
2461
2462 unique_fd fd(TEMP_FAILURE_RETRY(open(dex_path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)));
2463 if (fd == -1) {
2464 if (errno == EACCES || errno == ENOENT) {
2465 // Not treated as an error.
2466 _exit(0);
2467 }
2468 PLOG(ERROR) << "Failed to open secondary dex " << dex_path;
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002469 _exit(DexoptReturnCodes::kHashOpenPath);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002470 }
2471
2472 SHA256_CTX ctx;
2473 SHA256_Init(&ctx);
2474
2475 std::vector<uint8_t> buffer(65536);
2476 while (true) {
2477 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer.data(), buffer.size()));
2478 if (bytes_read == 0) {
2479 break;
2480 } else if (bytes_read == -1) {
2481 PLOG(ERROR) << "Failed to read secondary dex " << dex_path;
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002482 _exit(DexoptReturnCodes::kHashReadDex);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002483 }
2484
2485 SHA256_Update(&ctx, buffer.data(), bytes_read);
2486 }
2487
2488 std::array<uint8_t, SHA256_DIGEST_LENGTH> hash;
2489 SHA256_Final(hash.data(), &ctx);
2490 if (!WriteFully(pipe_write, hash.data(), hash.size())) {
Andreas Gampefa2dadd2018-02-28 19:52:47 -08002491 _exit(DexoptReturnCodes::kHashWrite);
Alan Stokesa25d90c2017-10-16 10:56:00 +01002492 }
2493
2494 _exit(0);
2495 }
2496
2497 // parent
2498 pipe_write.reset();
2499
2500 out_secondary_dex_hash->resize(SHA256_DIGEST_LENGTH);
2501 if (!ReadFully(pipe_read, out_secondary_dex_hash->data(), out_secondary_dex_hash->size())) {
2502 out_secondary_dex_hash->clear();
2503 }
2504 return wait_child(pid) == 0;
2505}
2506
Jeff Sharkey90aff262016-12-12 14:28:24 -07002507// Helper for move_ab, so that we can have common failure-case cleanup.
2508static bool unlink_and_rename(const char* from, const char* to) {
2509 // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise,
2510 // return a failure.
2511 struct stat s;
2512 if (stat(to, &s) == 0) {
2513 if (!S_ISREG(s.st_mode)) {
2514 LOG(ERROR) << from << " is not a regular file to replace for A/B.";
2515 return false;
2516 }
2517 if (unlink(to) != 0) {
2518 LOG(ERROR) << "Could not unlink " << to << " to move A/B.";
2519 return false;
2520 }
2521 } else {
2522 // This may be a permission problem. We could investigate the error code, but we'll just
2523 // let the rename failure do the work for us.
2524 }
2525
2526 // Try to rename "to" to "from."
2527 if (rename(from, to) != 0) {
2528 PLOG(ERROR) << "Could not rename " << from << " to " << to;
2529 return false;
2530 }
2531 return true;
2532}
2533
2534// Move/rename a B artifact (from) to an A artifact (to).
2535static bool move_ab_path(const std::string& b_path, const std::string& a_path) {
2536 // Check whether B exists.
2537 {
2538 struct stat s;
2539 if (stat(b_path.c_str(), &s) != 0) {
2540 // Silently ignore for now. The service calling this isn't smart enough to understand
2541 // lack of artifacts at the moment.
2542 return false;
2543 }
2544 if (!S_ISREG(s.st_mode)) {
2545 LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file.";
2546 // Try to unlink, but swallow errors.
2547 unlink(b_path.c_str());
2548 return false;
2549 }
2550 }
2551
2552 // Rename B to A.
2553 if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) {
2554 // Delete the b_path so we don't try again (or fail earlier).
2555 if (unlink(b_path.c_str()) != 0) {
2556 PLOG(ERROR) << "Could not unlink " << b_path;
2557 }
2558
2559 return false;
2560 }
2561
2562 return true;
2563}
2564
2565bool move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) {
2566 // Get the current slot suffix. No suffix, no A/B.
Mathieu Chartier9b2da082018-10-26 13:23:11 -07002567 const std::string slot_suffix = GetProperty("ro.boot.slot_suffix", "");
2568 if (slot_suffix.empty()) {
2569 return false;
2570 }
Jeff Sharkey90aff262016-12-12 14:28:24 -07002571
Mathieu Chartier9b2da082018-10-26 13:23:11 -07002572 if (!ValidateTargetSlotSuffix(slot_suffix)) {
2573 LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix;
2574 return false;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002575 }
2576
2577 // Validate other inputs.
2578 if (validate_apk_path(apk_path) != 0) {
2579 LOG(ERROR) << "Invalid apk_path: " << apk_path;
2580 return false;
2581 }
2582 if (validate_apk_path(oat_dir) != 0) {
2583 LOG(ERROR) << "Invalid oat_dir: " << oat_dir;
2584 return false;
2585 }
2586
2587 char a_path[PKG_PATH_MAX];
2588 if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) {
2589 return false;
2590 }
2591 const std::string a_vdex_path = create_vdex_filename(a_path);
2592 const std::string a_image_path = create_image_filename(a_path);
2593
2594 // B path = A path + slot suffix.
2595 const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str());
2596 const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str());
2597 const std::string b_image_path = StringPrintf("%s.%s",
2598 a_image_path.c_str(),
2599 slot_suffix.c_str());
2600
2601 bool success = true;
2602 if (move_ab_path(b_path, a_path)) {
2603 if (move_ab_path(b_vdex_path, a_vdex_path)) {
2604 // Note: we can live without an app image. As such, ignore failure to move the image file.
2605 // If we decide to require the app image, or the app image being moved correctly,
2606 // then change accordingly.
2607 constexpr bool kIgnoreAppImageFailure = true;
2608
2609 if (!a_image_path.empty()) {
2610 if (!move_ab_path(b_image_path, a_image_path)) {
2611 unlink(a_image_path.c_str());
2612 if (!kIgnoreAppImageFailure) {
2613 success = false;
2614 }
2615 }
2616 }
2617 } else {
2618 // Cleanup: delete B image, ignore errors.
2619 unlink(b_image_path.c_str());
2620 success = false;
2621 }
2622 } else {
2623 // Cleanup: delete B image, ignore errors.
2624 unlink(b_vdex_path.c_str());
2625 unlink(b_image_path.c_str());
2626 success = false;
2627 }
2628 return success;
2629}
2630
2631bool delete_odex(const char* apk_path, const char* instruction_set, const char* oat_dir) {
2632 // Delete the oat/odex file.
2633 char out_path[PKG_PATH_MAX];
Calin Juravle80a21252017-01-17 14:43:25 -08002634 if (!create_oat_out_path(apk_path, instruction_set, oat_dir,
Calin Juravle114f0812017-03-08 19:05:07 -08002635 /*is_secondary_dex*/false, out_path)) {
Jeff Sharkey90aff262016-12-12 14:28:24 -07002636 return false;
2637 }
2638
2639 // In case of a permission failure report the issue. Otherwise just print a warning.
2640 auto unlink_and_check = [](const char* path) -> bool {
2641 int result = unlink(path);
2642 if (result != 0) {
2643 if (errno == EACCES || errno == EPERM) {
2644 PLOG(ERROR) << "Could not unlink " << path;
2645 return false;
2646 }
2647 PLOG(WARNING) << "Could not unlink " << path;
2648 }
2649 return true;
2650 };
2651
2652 // Delete the oat/odex file.
2653 bool return_value_oat = unlink_and_check(out_path);
2654
2655 // Derive and delete the app image.
2656 bool return_value_art = unlink_and_check(create_image_filename(out_path).c_str());
2657
Nicolas Geoffray192fb962017-05-25 13:58:06 +01002658 // Derive and delete the vdex file.
2659 bool return_value_vdex = unlink_and_check(create_vdex_filename(out_path).c_str());
2660
Jeff Sharkey90aff262016-12-12 14:28:24 -07002661 // Report success.
Nicolas Geoffray192fb962017-05-25 13:58:06 +01002662 return return_value_oat && return_value_art && return_value_vdex;
Jeff Sharkey90aff262016-12-12 14:28:24 -07002663}
2664
Jeff Sharkeyc1149c92017-09-21 14:51:09 -06002665static bool is_absolute_path(const std::string& path) {
2666 if (path.find('/') != 0 || path.find("..") != std::string::npos) {
2667 LOG(ERROR) << "Invalid absolute path " << path;
2668 return false;
2669 } else {
2670 return true;
2671 }
2672}
2673
2674static bool is_valid_instruction_set(const std::string& instruction_set) {
2675 // TODO: add explicit whitelisting of instruction sets
2676 if (instruction_set.find('/') != std::string::npos) {
2677 LOG(ERROR) << "Invalid instruction set " << instruction_set;
2678 return false;
2679 } else {
2680 return true;
2681 }
2682}
2683
2684bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir,
2685 const char *apk_path, const char *instruction_set) {
2686 std::string oat_dir_ = oat_dir;
2687 std::string apk_path_ = apk_path;
2688 std::string instruction_set_ = instruction_set;
2689
2690 if (!is_absolute_path(oat_dir_)) return false;
2691 if (!is_absolute_path(apk_path_)) return false;
2692 if (!is_valid_instruction_set(instruction_set_)) return false;
2693
2694 std::string::size_type end = apk_path_.rfind('.');
2695 std::string::size_type start = apk_path_.rfind('/', end);
2696 if (end == std::string::npos || start == std::string::npos) {
2697 LOG(ERROR) << "Invalid apk_path " << apk_path_;
2698 return false;
2699 }
2700
2701 std::string res_ = oat_dir_ + '/' + instruction_set + '/'
2702 + apk_path_.substr(start + 1, end - start - 1) + ".odex";
2703 const char* res = res_.c_str();
2704 if (strlen(res) >= PKG_PATH_MAX) {
2705 LOG(ERROR) << "Result too large";
2706 return false;
2707 } else {
2708 strlcpy(path, res, PKG_PATH_MAX);
2709 return true;
2710 }
2711}
2712
2713bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path,
2714 const char *instruction_set) {
2715 std::string apk_path_ = apk_path;
2716 std::string instruction_set_ = instruction_set;
2717
2718 if (!is_absolute_path(apk_path_)) return false;
2719 if (!is_valid_instruction_set(instruction_set_)) return false;
2720
2721 std::string::size_type end = apk_path_.rfind('.');
2722 std::string::size_type start = apk_path_.rfind('/', end);
2723 if (end == std::string::npos || start == std::string::npos) {
2724 LOG(ERROR) << "Invalid apk_path " << apk_path_;
2725 return false;
2726 }
2727
2728 std::string oat_dir = apk_path_.substr(0, start + 1) + "oat";
2729 return calculate_oat_file_path_default(path, oat_dir.c_str(), apk_path, instruction_set);
2730}
2731
2732bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src,
2733 const char *instruction_set) {
2734 std::string src_ = src;
2735 std::string instruction_set_ = instruction_set;
2736
2737 if (!is_absolute_path(src_)) return false;
2738 if (!is_valid_instruction_set(instruction_set_)) return false;
2739
2740 for (auto it = src_.begin() + 1; it < src_.end(); ++it) {
2741 if (*it == '/') {
2742 *it = '@';
2743 }
2744 }
2745
2746 std::string res_ = android_data_dir + DALVIK_CACHE + '/' + instruction_set_ + src_
2747 + DALVIK_CACHE_POSTFIX;
2748 const char* res = res_.c_str();
2749 if (strlen(res) >= PKG_PATH_MAX) {
2750 LOG(ERROR) << "Result too large";
2751 return false;
2752 } else {
2753 strlcpy(path, res, PKG_PATH_MAX);
2754 return true;
2755 }
2756}
2757
Calin Juravle59f7ab82018-04-27 17:50:23 -07002758bool open_classpath_files(const std::string& classpath, std::vector<unique_fd>* apk_fds,
2759 std::vector<std::string>* dex_locations) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002760 std::vector<std::string> classpaths_elems = base::Split(classpath, ":");
2761 for (const std::string& elem : classpaths_elems) {
2762 unique_fd fd(TEMP_FAILURE_RETRY(open(elem.c_str(), O_RDONLY)));
2763 if (fd < 0) {
2764 PLOG(ERROR) << "Could not open classpath elem " << elem;
2765 return false;
2766 } else {
2767 apk_fds->push_back(std::move(fd));
Calin Juravle59f7ab82018-04-27 17:50:23 -07002768 dex_locations->push_back(elem);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002769 }
2770 }
2771 return true;
2772}
2773
2774static bool create_app_profile_snapshot(int32_t app_id,
2775 const std::string& package_name,
2776 const std::string& profile_name,
2777 const std::string& classpath) {
Calin Juravle29591732017-11-20 17:46:19 -08002778 int app_shared_gid = multiuser_get_shared_gid(/*user_id*/ 0, app_id);
2779
Calin Juravle824a64d2018-01-18 20:23:17 -08002780 unique_fd snapshot_fd = open_spnashot_profile(AID_SYSTEM, package_name, profile_name);
Calin Juravle29591732017-11-20 17:46:19 -08002781 if (snapshot_fd < 0) {
2782 return false;
2783 }
2784
2785 std::vector<unique_fd> profiles_fd;
2786 unique_fd reference_profile_fd;
Calin Juravle824a64d2018-01-18 20:23:17 -08002787 open_profile_files(app_shared_gid, package_name, profile_name, /*is_secondary_dex*/ false,
2788 &profiles_fd, &reference_profile_fd);
Calin Juravle29591732017-11-20 17:46:19 -08002789 if (profiles_fd.empty() || (reference_profile_fd.get() < 0)) {
2790 return false;
2791 }
2792
2793 profiles_fd.push_back(std::move(reference_profile_fd));
2794
Calin Juravle0d0a4922018-01-23 19:54:11 -08002795 // Open the class paths elements. These will be used to filter out profile data that does
2796 // not belong to the classpath during merge.
2797 std::vector<unique_fd> apk_fds;
Calin Juravle59f7ab82018-04-27 17:50:23 -07002798 std::vector<std::string> dex_locations;
2799 if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002800 return false;
2801 }
2802
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002803 RunProfman args;
Calin Juravle78728f32019-11-08 17:55:46 -08002804 args.SetupMerge(profiles_fd, snapshot_fd, apk_fds, dex_locations, /*for_snapshot=*/true);
Calin Juravle29591732017-11-20 17:46:19 -08002805 pid_t pid = fork();
2806 if (pid == 0) {
2807 /* child -- drop privileges before continuing */
2808 drop_capabilities(app_shared_gid);
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002809 args.Exec();
Calin Juravle29591732017-11-20 17:46:19 -08002810 }
2811
2812 /* parent */
2813 int return_code = wait_child(pid);
2814 if (!WIFEXITED(return_code)) {
Calin Juravle824a64d2018-01-18 20:23:17 -08002815 LOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
Calin Juravle29591732017-11-20 17:46:19 -08002816 return false;
2817 }
2818
Calin Juravle78728f32019-11-08 17:55:46 -08002819 // Verify that profman finished successfully.
2820 int profman_code = WEXITSTATUS(return_code);
2821 if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) {
2822 LOG(WARNING) << "profman error for " << package_name << ":" << profile_name
2823 << ":" << profman_code;
2824 return false;
2825 }
Calin Juravle29591732017-11-20 17:46:19 -08002826 return true;
2827}
2828
Calin Juravle0d0a4922018-01-23 19:54:11 -08002829static bool create_boot_image_profile_snapshot(const std::string& package_name,
2830 const std::string& profile_name,
2831 const std::string& classpath) {
2832 // The reference profile directory for the android package might not be prepared. Do it now.
2833 const std::string ref_profile_dir =
2834 create_primary_reference_profile_package_dir_path(package_name);
2835 if (fs_prepare_dir(ref_profile_dir.c_str(), 0770, AID_SYSTEM, AID_SYSTEM) != 0) {
2836 PLOG(ERROR) << "Failed to prepare " << ref_profile_dir;
2837 return false;
2838 }
2839
Mathieu Chartiere0d64a12018-11-01 12:07:26 -07002840 // Return false for empty class path since it may otherwise return true below if profiles is
2841 // empty.
2842 if (classpath.empty()) {
2843 PLOG(ERROR) << "Class path is empty";
2844 return false;
2845 }
2846
Calin Juravle0d0a4922018-01-23 19:54:11 -08002847 // Open and create the snapshot profile.
2848 unique_fd snapshot_fd = open_spnashot_profile(AID_SYSTEM, package_name, profile_name);
2849
2850 // Collect all non empty profiles.
2851 // The collection will traverse all applications profiles and find the non empty files.
2852 // This has the potential of inspecting a large number of files and directories (depending
2853 // on the number of applications and users). So there is a slight increase in the chance
2854 // to get get occasionally I/O errors (e.g. for opening the file). When that happens do not
2855 // fail the snapshot and aggregate whatever profile we could open.
2856 //
2857 // The profile snapshot is a best effort based on available data it's ok if some data
2858 // from some apps is missing. It will be counter productive for the snapshot to fail
2859 // because we could not open or read some of the files.
2860 std::vector<std::string> profiles;
2861 if (!collect_profiles(&profiles)) {
2862 LOG(WARNING) << "There were errors while collecting the profiles for the boot image.";
2863 }
2864
2865 // If we have no profiles return early.
2866 if (profiles.empty()) {
2867 return true;
2868 }
2869
2870 // Open the classpath elements. These will be used to filter out profile data that does
2871 // not belong to the classpath during merge.
2872 std::vector<unique_fd> apk_fds;
Calin Juravle59f7ab82018-04-27 17:50:23 -07002873 std::vector<std::string> dex_locations;
2874 if (!open_classpath_files(classpath, &apk_fds, &dex_locations)) {
Calin Juravle0d0a4922018-01-23 19:54:11 -08002875 return false;
2876 }
2877
2878 // If we could not open any files from the classpath return an error.
2879 if (apk_fds.empty()) {
2880 LOG(ERROR) << "Could not open any of the classpath elements.";
2881 return false;
2882 }
2883
2884 // Aggregate the profiles in batches of kAggregationBatchSize.
2885 // We do this to avoid opening a huge a amount of files.
2886 static constexpr size_t kAggregationBatchSize = 10;
2887
Calin Juravle0d0a4922018-01-23 19:54:11 -08002888 for (size_t i = 0; i < profiles.size(); ) {
Calin Juravlea64fb512019-11-06 16:11:14 -08002889 std::vector<unique_fd> profiles_fd;
Calin Juravle0d0a4922018-01-23 19:54:11 -08002890 for (size_t k = 0; k < kAggregationBatchSize && i < profiles.size(); k++, i++) {
2891 unique_fd fd = open_profile(AID_SYSTEM, profiles[i], O_RDONLY);
2892 if (fd.get() >= 0) {
2893 profiles_fd.push_back(std::move(fd));
2894 }
2895 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002896
2897 // We aggregate (read & write) into the same fd multiple times in a row.
2898 // We need to reset the cursor every time to ensure we read the whole file every time.
2899 if (TEMP_FAILURE_RETRY(lseek(snapshot_fd, 0, SEEK_SET)) == static_cast<off_t>(-1)) {
2900 PLOG(ERROR) << "Cannot reset position for snapshot profile";
2901 return false;
2902 }
2903
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002904 RunProfman args;
Calin Juravleb3a929d2018-12-11 14:40:00 -08002905 args.SetupMerge(profiles_fd,
2906 snapshot_fd,
2907 apk_fds,
Calin Juravle78728f32019-11-08 17:55:46 -08002908 dex_locations,
2909 /*for_snapshot=*/true,
2910 /*for_boot_image=*/true);
Calin Juravle0d0a4922018-01-23 19:54:11 -08002911 pid_t pid = fork();
2912 if (pid == 0) {
2913 /* child -- drop privileges before continuing */
2914 drop_capabilities(AID_SYSTEM);
2915
Calin Juravle59f7ab82018-04-27 17:50:23 -07002916 // The introduction of new access flags into boot jars causes them to
2917 // fail dex file verification.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002918 args.Exec();
Calin Juravle0d0a4922018-01-23 19:54:11 -08002919 }
2920
2921 /* parent */
2922 int return_code = wait_child(pid);
Calin Juravlea64fb512019-11-06 16:11:14 -08002923
Calin Juravle0d0a4922018-01-23 19:54:11 -08002924 if (!WIFEXITED(return_code)) {
2925 PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
2926 return false;
2927 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002928
2929 // Verify that profman finished successfully.
2930 int profman_code = WEXITSTATUS(return_code);
Calin Juravle78728f32019-11-08 17:55:46 -08002931 if (profman_code != PROFMAN_BIN_RETURN_CODE_SUCCESS) {
2932 LOG(WARNING) << "profman error for " << package_name << ":" << profile_name
2933 << ":" << profman_code;
2934 return false;
Calin Juravlea64fb512019-11-06 16:11:14 -08002935 }
Calin Juravle0d0a4922018-01-23 19:54:11 -08002936 }
Calin Juravlea64fb512019-11-06 16:11:14 -08002937
Calin Juravle0d0a4922018-01-23 19:54:11 -08002938 return true;
2939}
2940
2941bool create_profile_snapshot(int32_t app_id, const std::string& package_name,
2942 const std::string& profile_name, const std::string& classpath) {
2943 if (app_id == -1) {
2944 return create_boot_image_profile_snapshot(package_name, profile_name, classpath);
2945 } else {
2946 return create_app_profile_snapshot(app_id, package_name, profile_name, classpath);
2947 }
2948}
2949
Calin Juravlec3b049e2018-01-18 22:32:58 -08002950bool prepare_app_profile(const std::string& package_name,
2951 userid_t user_id,
2952 appid_t app_id,
2953 const std::string& profile_name,
Calin Juravlef63d4792018-01-30 17:43:34 +00002954 const std::string& code_path,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002955 const std::optional<std::string>& dex_metadata) {
Calin Juravlec3b049e2018-01-18 22:32:58 -08002956 // Prepare the current profile.
2957 std::string cur_profile = create_current_profile_path(user_id, package_name, profile_name,
2958 /*is_secondary_dex*/ false);
2959 uid_t uid = multiuser_get_uid(user_id, app_id);
2960 if (fs_prepare_file_strict(cur_profile.c_str(), 0600, uid, uid) != 0) {
2961 PLOG(ERROR) << "Failed to prepare " << cur_profile;
2962 return false;
2963 }
2964
2965 // Check if we need to install the profile from the dex metadata.
Jooyung Han9fcc4ef2020-01-23 12:45:10 +09002966 if (!dex_metadata) {
Calin Juravlec3b049e2018-01-18 22:32:58 -08002967 return true;
2968 }
2969
2970 // We have a dex metdata. Merge the profile into the reference profile.
2971 unique_fd ref_profile_fd = open_reference_profile(uid, package_name, profile_name,
2972 /*read_write*/ true, /*is_secondary_dex*/ false);
2973 unique_fd dex_metadata_fd(TEMP_FAILURE_RETRY(
2974 open(dex_metadata->c_str(), O_RDONLY | O_NOFOLLOW)));
Calin Juravlef63d4792018-01-30 17:43:34 +00002975 unique_fd apk_fd(TEMP_FAILURE_RETRY(open(code_path.c_str(), O_RDONLY | O_NOFOLLOW)));
2976 if (apk_fd < 0) {
2977 PLOG(ERROR) << "Could not open code path " << code_path;
2978 return false;
2979 }
Calin Juravlec3b049e2018-01-18 22:32:58 -08002980
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002981 RunProfman args;
2982 args.SetupCopyAndUpdate(std::move(dex_metadata_fd),
2983 std::move(ref_profile_fd),
2984 std::move(apk_fd),
2985 code_path);
Calin Juravlec3b049e2018-01-18 22:32:58 -08002986 pid_t pid = fork();
2987 if (pid == 0) {
2988 /* child -- drop privileges before continuing */
2989 gid_t app_shared_gid = multiuser_get_shared_gid(user_id, app_id);
2990 drop_capabilities(app_shared_gid);
2991
Calin Juravlef63d4792018-01-30 17:43:34 +00002992 // The copy and update takes ownership over the fds.
Mathieu Chartiercc66c442018-11-09 15:57:21 -08002993 args.Exec();
Calin Juravlec3b049e2018-01-18 22:32:58 -08002994 }
2995
2996 /* parent */
2997 int return_code = wait_child(pid);
2998 if (!WIFEXITED(return_code)) {
2999 PLOG(WARNING) << "profman failed for " << package_name << ":" << profile_name;
3000 return false;
3001 }
3002 return true;
3003}
3004
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07003005} // namespace installd
3006} // namespace android