xref: /aosp_15_r20/frameworks/native/cmds/installd/dexopt.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #ifndef DEXOPT_H_
18*38e8c45fSAndroid Build Coastguard Worker #define DEXOPT_H_
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include "installd_constants.h"
21*38e8c45fSAndroid Build Coastguard Worker #include "unique_file.h"
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <optional>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include <cutils/multiuser.h>
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker namespace android {
30*38e8c45fSAndroid Build Coastguard Worker namespace installd {
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker /* dexopt needed flags matching those in dalvik.system.DexFile */
33*38e8c45fSAndroid Build Coastguard Worker static constexpr int NO_DEXOPT_NEEDED            = 0;
34*38e8c45fSAndroid Build Coastguard Worker static constexpr int DEX2OAT_FROM_SCRATCH        = 1;
35*38e8c45fSAndroid Build Coastguard Worker static constexpr int DEX2OAT_FOR_BOOT_IMAGE      = 2;
36*38e8c45fSAndroid Build Coastguard Worker static constexpr int DEX2OAT_FOR_FILTER          = 3;
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker #define ANDROID_ART_APEX_BIN "/apex/com.android.art/bin"
39*38e8c45fSAndroid Build Coastguard Worker // Location of binaries in the Android Runtime APEX.
40*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kDex2oat32Path = ANDROID_ART_APEX_BIN "/dex2oat32";
41*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kDex2oat64Path = ANDROID_ART_APEX_BIN "/dex2oat64";
42*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kDex2oatDebug32Path = ANDROID_ART_APEX_BIN "/dex2oatd32";
43*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kDex2oatDebug64Path = ANDROID_ART_APEX_BIN "/dex2oatd64";
44*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kProfmanPath = ANDROID_ART_APEX_BIN "/profman";
45*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kProfmanDebugPath = ANDROID_ART_APEX_BIN "/profmand";
46*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kDexoptanalyzerPath = ANDROID_ART_APEX_BIN "/dexoptanalyzer";
47*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kDexoptanalyzerDebugPath = ANDROID_ART_APEX_BIN "/dexoptanalyzerd";
48*38e8c45fSAndroid Build Coastguard Worker #undef ANDROID_ART_APEX_BIN
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker // Clear the reference profile identified by the given profile name.
51*38e8c45fSAndroid Build Coastguard Worker bool clear_primary_reference_profile(const std::string& pkgname, const std::string& profile_name);
52*38e8c45fSAndroid Build Coastguard Worker // Clear the current profile identified by the given profile name (for single user).
53*38e8c45fSAndroid Build Coastguard Worker bool clear_primary_current_profile(const std::string& pkgname, const std::string& profile_name,
54*38e8c45fSAndroid Build Coastguard Worker          userid_t user);
55*38e8c45fSAndroid Build Coastguard Worker // Clear all current profiles identified by the given profile name (all users).
56*38e8c45fSAndroid Build Coastguard Worker bool clear_primary_current_profiles(const std::string& pkgname, const std::string& profile_name);
57*38e8c45fSAndroid Build Coastguard Worker 
58*38e8c45fSAndroid Build Coastguard Worker // Decides if profile guided compilation is needed or not based on existing profiles.
59*38e8c45fSAndroid Build Coastguard Worker // The analysis is done for a single profile name (which corresponds to a single code path).
60*38e8c45fSAndroid Build Coastguard Worker //
61*38e8c45fSAndroid Build Coastguard Worker // Returns PROFILES_ANALYSIS_OPTIMIZE if there is enough information in the current profiles
62*38e8c45fSAndroid Build Coastguard Worker // that makes it worth to recompile the package.
63*38e8c45fSAndroid Build Coastguard Worker // If the return value is PROFILES_ANALYSIS_OPTIMIZE all the current profiles would have been
64*38e8c45fSAndroid Build Coastguard Worker // merged into the reference profiles accessible with open_reference_profile().
65*38e8c45fSAndroid Build Coastguard Worker //
66*38e8c45fSAndroid Build Coastguard Worker // Return PROFILES_ANALYSIS_DONT_OPTIMIZE_SMALL_DELTA if the package should not optimize.
67*38e8c45fSAndroid Build Coastguard Worker // As a special case returns PROFILES_ANALYSIS_DONT_OPTIMIZE_EMPTY_PROFILES if all profiles are
68*38e8c45fSAndroid Build Coastguard Worker // empty.
69*38e8c45fSAndroid Build Coastguard Worker int analyze_primary_profiles(uid_t uid,
70*38e8c45fSAndroid Build Coastguard Worker                              const std::string& pkgname,
71*38e8c45fSAndroid Build Coastguard Worker                              const std::string& profile_name);
72*38e8c45fSAndroid Build Coastguard Worker 
73*38e8c45fSAndroid Build Coastguard Worker // Create a snapshot of the profile information for the given package profile.
74*38e8c45fSAndroid Build Coastguard Worker // If appId is -1, the method creates the profile snapshot for the boot image.
75*38e8c45fSAndroid Build Coastguard Worker //
76*38e8c45fSAndroid Build Coastguard Worker // The profile snapshot is the aggregation of all existing profiles (all current user
77*38e8c45fSAndroid Build Coastguard Worker // profiles & the reference profile) and is meant to capture the all the profile information
78*38e8c45fSAndroid Build Coastguard Worker // without performing a merge into the reference profile which might impact future dex2oat
79*38e8c45fSAndroid Build Coastguard Worker // compilations.
80*38e8c45fSAndroid Build Coastguard Worker // The snapshot is created next to the reference profile of the package and the
81*38e8c45fSAndroid Build Coastguard Worker // ownership is assigned to AID_SYSTEM.
82*38e8c45fSAndroid Build Coastguard Worker // The snapshot location is reference_profile_location.snapshot. If a snapshot is already
83*38e8c45fSAndroid Build Coastguard Worker // there, it will be truncated and overwritten.
84*38e8c45fSAndroid Build Coastguard Worker //
85*38e8c45fSAndroid Build Coastguard Worker // The classpath acts as filter: only profiling data belonging to elements of the classpath
86*38e8c45fSAndroid Build Coastguard Worker // will end up in the snapshot.
87*38e8c45fSAndroid Build Coastguard Worker bool create_profile_snapshot(int32_t app_id,
88*38e8c45fSAndroid Build Coastguard Worker                              const std::string& package,
89*38e8c45fSAndroid Build Coastguard Worker                              const std::string& profile_name,
90*38e8c45fSAndroid Build Coastguard Worker                              const std::string& classpath);
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker bool dump_profiles(int32_t uid, const std::string& pkgname, const std::string& profile_name,
93*38e8c45fSAndroid Build Coastguard Worker                    const std::string& code_path, bool dump_classes_and_methods);
94*38e8c45fSAndroid Build Coastguard Worker 
95*38e8c45fSAndroid Build Coastguard Worker bool copy_system_profile(const std::string& system_profile,
96*38e8c45fSAndroid Build Coastguard Worker                          uid_t packageUid,
97*38e8c45fSAndroid Build Coastguard Worker                          const std::string& pkgname,
98*38e8c45fSAndroid Build Coastguard Worker                          const std::string& profile_name);
99*38e8c45fSAndroid Build Coastguard Worker 
100*38e8c45fSAndroid Build Coastguard Worker // Prepares the app profile for the package at the given path:
101*38e8c45fSAndroid Build Coastguard Worker // - Creates the current profile for the given user ID, unless the user ID is `USER_NULL`.
102*38e8c45fSAndroid Build Coastguard Worker // - Merges the profile from the dex metadata file (if present) into the reference profile.
103*38e8c45fSAndroid Build Coastguard Worker bool prepare_app_profile(const std::string& package_name,
104*38e8c45fSAndroid Build Coastguard Worker                          userid_t user_id,
105*38e8c45fSAndroid Build Coastguard Worker                          appid_t app_id,
106*38e8c45fSAndroid Build Coastguard Worker                          const std::string& profile_name,
107*38e8c45fSAndroid Build Coastguard Worker                          const std::string& code_path,
108*38e8c45fSAndroid Build Coastguard Worker                          const std::optional<std::string>& dex_metadata);
109*38e8c45fSAndroid Build Coastguard Worker 
110*38e8c45fSAndroid Build Coastguard Worker // Returns the total bytes that were freed, or -1 in case of errors.
111*38e8c45fSAndroid Build Coastguard Worker int64_t delete_odex(const char* apk_path, const char* instruction_set, const char* output_path);
112*38e8c45fSAndroid Build Coastguard Worker 
113*38e8c45fSAndroid Build Coastguard Worker bool reconcile_secondary_dex_file(const std::string& dex_path,
114*38e8c45fSAndroid Build Coastguard Worker         const std::string& pkgname, int uid, const std::vector<std::string>& isas,
115*38e8c45fSAndroid Build Coastguard Worker         const std::optional<std::string>& volumeUuid, int storage_flag,
116*38e8c45fSAndroid Build Coastguard Worker         /*out*/bool* out_secondary_dex_exists);
117*38e8c45fSAndroid Build Coastguard Worker 
118*38e8c45fSAndroid Build Coastguard Worker bool hash_secondary_dex_file(const std::string& dex_path,
119*38e8c45fSAndroid Build Coastguard Worker         const std::string& pkgname, int uid, const std::optional<std::string>& volume_uuid,
120*38e8c45fSAndroid Build Coastguard Worker         int storage_flag, std::vector<uint8_t>* out_secondary_dex_hash);
121*38e8c45fSAndroid Build Coastguard Worker 
122*38e8c45fSAndroid Build Coastguard Worker // completed pass false if it is canceled. Otherwise it will be true even if there is other
123*38e8c45fSAndroid Build Coastguard Worker // error.
124*38e8c45fSAndroid Build Coastguard Worker int dexopt(const char *apk_path, uid_t uid, const char *pkgName, const char *instruction_set,
125*38e8c45fSAndroid Build Coastguard Worker         int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
126*38e8c45fSAndroid Build Coastguard Worker         const char* volume_uuid, const char* class_loader_context, const char* se_info,
127*38e8c45fSAndroid Build Coastguard Worker         bool downgrade, int target_sdk_version, const char* profile_name,
128*38e8c45fSAndroid Build Coastguard Worker         const char* dexMetadataPath, const char* compilation_reason, std::string* error_msg,
129*38e8c45fSAndroid Build Coastguard Worker         /* out */ bool* completed = nullptr);
130*38e8c45fSAndroid Build Coastguard Worker 
131*38e8c45fSAndroid Build Coastguard Worker bool is_dexopt_blocked();
132*38e8c45fSAndroid Build Coastguard Worker 
133*38e8c45fSAndroid Build Coastguard Worker void control_dexopt_blocking(bool block);
134*38e8c45fSAndroid Build Coastguard Worker 
135*38e8c45fSAndroid Build Coastguard Worker bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir,
136*38e8c45fSAndroid Build Coastguard Worker         const char *apk_path, const char *instruction_set);
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path,
139*38e8c45fSAndroid Build Coastguard Worker         const char *instruction_set);
140*38e8c45fSAndroid Build Coastguard Worker 
141*38e8c45fSAndroid Build Coastguard Worker bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src,
142*38e8c45fSAndroid Build Coastguard Worker         const char *instruction_set);
143*38e8c45fSAndroid Build Coastguard Worker 
144*38e8c45fSAndroid Build Coastguard Worker bool move_ab(const char* apk_path, const char* instruction_set, const char* output_path);
145*38e8c45fSAndroid Build Coastguard Worker 
146*38e8c45fSAndroid Build Coastguard Worker const char* select_execution_binary(
147*38e8c45fSAndroid Build Coastguard Worker         const char* binary,
148*38e8c45fSAndroid Build Coastguard Worker         const char* debug_binary,
149*38e8c45fSAndroid Build Coastguard Worker         bool background_job_compile,
150*38e8c45fSAndroid Build Coastguard Worker         bool is_debug_runtime,
151*38e8c45fSAndroid Build Coastguard Worker         bool is_release,
152*38e8c45fSAndroid Build Coastguard Worker         bool is_debuggable_build);
153*38e8c45fSAndroid Build Coastguard Worker 
154*38e8c45fSAndroid Build Coastguard Worker // Returns `ODEX_NOT_FOUND` if the optimized artifacts are not found, or `ODEX_IS_PUBLIC` if the
155*38e8c45fSAndroid Build Coastguard Worker // optimized artifacts are accessible by all apps, or `ODEX_IS_PRIVATE` if the optimized artifacts
156*38e8c45fSAndroid Build Coastguard Worker // are only accessible by this app, or -1 if failed to get the visibility of the optimized
157*38e8c45fSAndroid Build Coastguard Worker // artifacts.
158*38e8c45fSAndroid Build Coastguard Worker int get_odex_visibility(const char* apk_path, const char* instruction_set, const char* oat_dir);
159*38e8c45fSAndroid Build Coastguard Worker 
160*38e8c45fSAndroid Build Coastguard Worker UniqueFile maybe_open_reference_profile(const std::string& pkgname, const std::string& dex_path,
161*38e8c45fSAndroid Build Coastguard Worker                                         const char* profile_name, bool profile_guided,
162*38e8c45fSAndroid Build Coastguard Worker                                         bool is_public, int uid, bool is_secondary_dex);
163*38e8c45fSAndroid Build Coastguard Worker 
164*38e8c45fSAndroid Build Coastguard Worker }  // namespace installd
165*38e8c45fSAndroid Build Coastguard Worker }  // namespace android
166*38e8c45fSAndroid Build Coastguard Worker 
167*38e8c45fSAndroid Build Coastguard Worker #endif  // DEXOPT_H_
168