blob: df02588a228cd431883a0a12e86bbe9220291f4e [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 */
16
17#ifndef DEXOPT_H_
18#define DEXOPT_H_
19
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060020#include "installd_constants.h"
Jiakai Zhangdb8c0f92023-08-03 14:55:16 +010021#include "unique_file.h"
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060022
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070023#include <sys/types.h>
24
Jooyung Han9fcc4ef2020-01-23 12:45:10 +090025#include <optional>
26
Jeff Sharkey90aff262016-12-12 14:28:24 -070027#include <cutils/multiuser.h>
28
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070029namespace android {
30namespace installd {
31
Jeff Sharkey90aff262016-12-12 14:28:24 -070032/* dexopt needed flags matching those in dalvik.system.DexFile */
Calin Juravle80a21252017-01-17 14:43:25 -080033static constexpr int NO_DEXOPT_NEEDED = 0;
Jeff Sharkey90aff262016-12-12 14:28:24 -070034static constexpr int DEX2OAT_FROM_SCRATCH = 1;
35static constexpr int DEX2OAT_FOR_BOOT_IMAGE = 2;
36static constexpr int DEX2OAT_FOR_FILTER = 3;
Jeff Sharkey90aff262016-12-12 14:28:24 -070037
Martin Stjernholmf4caaa02019-07-17 22:14:14 +010038#define ANDROID_ART_APEX_BIN "/apex/com.android.art/bin"
Roland Levillain67a14f62019-01-23 15:59:50 +000039// Location of binaries in the Android Runtime APEX.
David Sehr6df89a62020-04-15 20:43:48 -070040static constexpr const char* kDex2oat32Path = ANDROID_ART_APEX_BIN "/dex2oat32";
41static constexpr const char* kDex2oat64Path = ANDROID_ART_APEX_BIN "/dex2oat64";
42static constexpr const char* kDex2oatDebug32Path = ANDROID_ART_APEX_BIN "/dex2oatd32";
43static constexpr const char* kDex2oatDebug64Path = ANDROID_ART_APEX_BIN "/dex2oatd64";
Martin Stjernholmf4caaa02019-07-17 22:14:14 +010044static constexpr const char* kProfmanPath = ANDROID_ART_APEX_BIN "/profman";
45static constexpr const char* kProfmanDebugPath = ANDROID_ART_APEX_BIN "/profmand";
46static constexpr const char* kDexoptanalyzerPath = ANDROID_ART_APEX_BIN "/dexoptanalyzer";
47static constexpr const char* kDexoptanalyzerDebugPath = ANDROID_ART_APEX_BIN "/dexoptanalyzerd";
48#undef ANDROID_ART_APEX_BIN
Roland Levillain67a14f62019-01-23 15:59:50 +000049
Calin Juravle824a64d2018-01-18 20:23:17 -080050// Clear the reference profile identified by the given profile name.
51bool clear_primary_reference_profile(const std::string& pkgname, const std::string& profile_name);
52// Clear the current profile identified by the given profile name (for single user).
53bool clear_primary_current_profile(const std::string& pkgname, const std::string& profile_name,
54 userid_t user);
55// Clear all current profiles identified by the given profile name (all users).
56bool clear_primary_current_profiles(const std::string& pkgname, const std::string& profile_name);
Jeff Sharkey90aff262016-12-12 14:28:24 -070057
Calin Juravlee90de862021-06-08 08:04:52 -070058// Decides if profile guided compilation is needed or not based on existing profiles.
Calin Juravle824a64d2018-01-18 20:23:17 -080059// The analysis is done for a single profile name (which corresponds to a single code path).
Calin Juravlee90de862021-06-08 08:04:52 -070060//
61// Returns PROFILES_ANALYSIS_OPTIMIZE if there is enough information in the current profiles
62// that makes it worth to recompile the package.
63// If the return value is PROFILES_ANALYSIS_OPTIMIZE all the current profiles would have been
64// merged into the reference profiles accessible with open_reference_profile().
65//
66// Return PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA if the package should not optimize.
67// As a special case returns PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES if all profiles are
68// empty.
69int analyze_primary_profiles(uid_t uid,
70 const std::string& pkgname,
71 const std::string& profile_name);
Calin Juravle114f0812017-03-08 19:05:07 -080072
Calin Juravle824a64d2018-01-18 20:23:17 -080073// Create a snapshot of the profile information for the given package profile.
Calin Juravle0d0a4922018-01-23 19:54:11 -080074// If appId is -1, the method creates the profile snapshot for the boot image.
75//
Calin Juravle29591732017-11-20 17:46:19 -080076// The profile snapshot is the aggregation of all existing profiles (all current user
77// profiles & the reference profile) and is meant to capture the all the profile information
78// without performing a merge into the reference profile which might impact future dex2oat
79// compilations.
80// The snapshot is created next to the reference profile of the package and the
81// ownership is assigned to AID_SYSTEM.
82// The snapshot location is reference_profile_location.snapshot. If a snapshot is already
83// there, it will be truncated and overwritten.
Calin Juravle0d0a4922018-01-23 19:54:11 -080084//
85// The classpath acts as filter: only profiling data belonging to elements of the classpath
86// will end up in the snapshot.
87bool create_profile_snapshot(int32_t app_id,
88 const std::string& package,
89 const std::string& profile_name,
90 const std::string& classpath);
Calin Juravle29591732017-11-20 17:46:19 -080091
Martin Stjernholmfd000402022-04-11 18:04:59 +010092bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name,
93 const std::string& code_path, bool dump_classes_and_methods);
Jeff Sharkey90aff262016-12-12 14:28:24 -070094
Mathieu Chartierf966f2a2017-05-10 12:48:37 -070095bool copy_system_profile(const std::string& system_profile,
96 uid_t packageUid,
Calin Juravle824a64d2018-01-18 20:23:17 -080097 const std::string& pkgname,
98 const std::string& profile_name);
Mathieu Chartierf966f2a2017-05-10 12:48:37 -070099
Jiakai Zhangf515a1b2022-02-23 18:33:26 +0000100// Prepares the app profile for the package at the given path:
101// - Creates the current profile for the given user ID, unless the user ID is `USER_NULL`.
102// - Merges the profile from the dex metadata file (if present) into the reference profile.
Calin Juravlec3b049e2018-01-18 22:32:58 -0800103bool prepare_app_profile(const std::string& package_name,
104 userid_t user_id,
105 appid_t app_id,
106 const std::string& profile_name,
107 const std::string& code_path,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900108 const std::optional<std::string>& dex_metadata);
Calin Juravlec3b049e2018-01-18 22:32:58 -0800109
Calin Juravle0f3beba2021-06-11 09:17:20 -0700110// Returns the total bytes that were freed, or -1 in case of errors.
111int64_t delete_odex(const char* apk_path, const char* instruction_set, const char* output_path);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700112
Calin Juravlec9eab382017-01-25 01:17:17 -0800113bool reconcile_secondary_dex_file(const std::string& dex_path,
114 const std::string& pkgname, int uid, const std::vector<std::string>& isas,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900115 const std::optional<std::string>& volumeUuid, int storage_flag,
Calin Juravlec9eab382017-01-25 01:17:17 -0800116 /*out*/bool* out_secondary_dex_exists);
117
Alan Stokesa25d90c2017-10-16 10:56:00 +0100118bool hash_secondary_dex_file(const std::string& dex_path,
Jooyung Han9fcc4ef2020-01-23 12:45:10 +0900119 const std::string& pkgname, int uid, const std::optional<std::string>& volume_uuid,
Alan Stokesa25d90c2017-10-16 10:56:00 +0100120 int storage_flag, std::vector<uint8_t>* out_secondary_dex_hash);
121
Keun young Park65578d62021-06-28 17:52:05 -0700122// completed pass false if it is canceled. Otherwise it will be true even if there is other
123// error.
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700124int dexopt(const char *apk_path, uid_t uid, const char *pkgName, const char *instruction_set,
125 int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
Calin Juravle52c45822017-07-13 22:50:21 -0700126 const char* volume_uuid, const char* class_loader_context, const char* se_info,
Calin Juravle62c5a372018-02-01 17:03:23 +0000127 bool downgrade, int target_sdk_version, const char* profile_name,
Keun young Park65578d62021-06-28 17:52:05 -0700128 const char* dexMetadataPath, const char* compilation_reason, std::string* error_msg,
129 /* out */ bool* completed = nullptr);
130
131bool is_dexopt_blocked();
132
133void control_dexopt_blocking(bool block);
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700134
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600135bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir,
136 const char *apk_path, const char *instruction_set);
137
138bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path,
139 const char *instruction_set);
140
141bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src,
142 const char *instruction_set);
143
Calin Juravle824a64d2018-01-18 20:23:17 -0800144bool move_ab(const char* apk_path, const char* instruction_set, const char* output_path);
145
Calin Juravlef74a7372019-02-28 20:29:41 -0800146const char* select_execution_binary(
147 const char* binary,
148 const char* debug_binary,
149 bool background_job_compile,
150 bool is_debug_runtime,
151 bool is_release,
152 bool is_debuggable_build);
153
Jiakai Zhangf515a1b2022-02-23 18:33:26 +0000154// Returns `ODEX_NOT_FOUND` if the optimized artifacts are not found, or `ODEX_IS_PUBLIC` if the
155// optimized artifacts are accessible by all apps, or `ODEX_IS_PRIVATE` if the optimized artifacts
156// are only accessible by this app, or -1 if failed to get the visibility of the optimized
157// artifacts.
158int get_odex_visibility(const char* apk_path, const char* instruction_set, const char* oat_dir);
159
Jiakai Zhangdb8c0f92023-08-03 14:55:16 +0100160UniqueFile maybe_open_reference_profile(const std::string& pkgname, const std::string& dex_path,
161 const char* profile_name, bool profile_guided,
162 bool is_public, int uid, bool is_secondary_dex);
163
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700164} // namespace installd
165} // namespace android
166
167#endif // DEXOPT_H_