1 /*
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 
20 #include "installd_constants.h"
21 
22 #include <sys/types.h>
23 
24 #include <optional>
25 
26 #include <cutils/multiuser.h>
27 
28 namespace android {
29 namespace installd {
30 
31 /* dexopt needed flags matching those in dalvik.system.DexFile */
32 static constexpr int NO_DEXOPT_NEEDED            = 0;
33 static constexpr int DEX2OAT_FROM_SCRATCH        = 1;
34 static constexpr int DEX2OAT_FOR_BOOT_IMAGE      = 2;
35 static constexpr int DEX2OAT_FOR_FILTER          = 3;
36 
37 #define ANDROID_ART_APEX_BIN "/apex/com.android.art/bin"
38 // Location of binaries in the Android Runtime APEX.
39 static constexpr const char* kDex2oat32Path = ANDROID_ART_APEX_BIN "/dex2oat32";
40 static constexpr const char* kDex2oat64Path = ANDROID_ART_APEX_BIN "/dex2oat64";
41 static constexpr const char* kDex2oatDebug32Path = ANDROID_ART_APEX_BIN "/dex2oatd32";
42 static constexpr const char* kDex2oatDebug64Path = ANDROID_ART_APEX_BIN "/dex2oatd64";
43 static constexpr const char* kProfmanPath = ANDROID_ART_APEX_BIN "/profman";
44 static constexpr const char* kProfmanDebugPath = ANDROID_ART_APEX_BIN "/profmand";
45 static constexpr const char* kDexoptanalyzerPath = ANDROID_ART_APEX_BIN "/dexoptanalyzer";
46 static constexpr const char* kDexoptanalyzerDebugPath = ANDROID_ART_APEX_BIN "/dexoptanalyzerd";
47 #undef ANDROID_ART_APEX_BIN
48 
49 // Clear the reference profile identified by the given profile name.
50 bool clear_primary_reference_profile(const std::string& pkgname, const std::string& profile_name);
51 // Clear the current profile identified by the given profile name (for single user).
52 bool clear_primary_current_profile(const std::string& pkgname, const std::string& profile_name,
53          userid_t user);
54 // Clear all current profiles identified by the given profile name (all users).
55 bool clear_primary_current_profiles(const std::string& pkgname, const std::string& profile_name);
56 
57 // Decide if profile guided compilation is needed or not based on existing profiles.
58 // The analysis is done for a single profile name (which corresponds to a single code path).
59 // Returns true if there is enough information in the current profiles that makes it
60 // worth to recompile the package.
61 // If the return value is true all the current profiles would have been merged into
62 // the reference profiles accessible with open_reference_profile().
63 bool analyze_primary_profiles(uid_t uid,
64                               const std::string& pkgname,
65                               const std::string& profile_name);
66 
67 // Create a snapshot of the profile information for the given package profile.
68 // If appId is -1, the method creates the profile snapshot for the boot image.
69 //
70 // The profile snapshot is the aggregation of all existing profiles (all current user
71 // profiles & the reference profile) and is meant to capture the all the profile information
72 // without performing a merge into the reference profile which might impact future dex2oat
73 // compilations.
74 // The snapshot is created next to the reference profile of the package and the
75 // ownership is assigned to AID_SYSTEM.
76 // The snapshot location is reference_profile_location.snapshot. If a snapshot is already
77 // there, it will be truncated and overwritten.
78 //
79 // The classpath acts as filter: only profiling data belonging to elements of the classpath
80 // will end up in the snapshot.
81 bool create_profile_snapshot(int32_t app_id,
82                              const std::string& package,
83                              const std::string& profile_name,
84                              const std::string& classpath);
85 
86 bool dump_profiles(int32_t uid,
87                    const std::string& pkgname,
88                    const std::string& profile_name,
89                    const std::string& code_path);
90 
91 bool copy_system_profile(const std::string& system_profile,
92                          uid_t packageUid,
93                          const std::string& pkgname,
94                          const std::string& profile_name);
95 
96 // Prepare the app profile for the given code path:
97 //  - create the current profile using profile_name
98 //  - merge the profile from the dex metadata file (if present) into
99 //    the reference profile.
100 bool prepare_app_profile(const std::string& package_name,
101                          userid_t user_id,
102                          appid_t app_id,
103                          const std::string& profile_name,
104                          const std::string& code_path,
105                          const std::optional<std::string>& dex_metadata);
106 
107 bool delete_odex(const char* apk_path, const char* instruction_set, const char* output_path);
108 
109 bool reconcile_secondary_dex_file(const std::string& dex_path,
110         const std::string& pkgname, int uid, const std::vector<std::string>& isas,
111         const std::optional<std::string>& volumeUuid, int storage_flag,
112         /*out*/bool* out_secondary_dex_exists);
113 
114 bool hash_secondary_dex_file(const std::string& dex_path,
115         const std::string& pkgname, int uid, const std::optional<std::string>& volume_uuid,
116         int storage_flag, std::vector<uint8_t>* out_secondary_dex_hash);
117 
118 int dexopt(const char *apk_path, uid_t uid, const char *pkgName, const char *instruction_set,
119         int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter,
120         const char* volume_uuid, const char* class_loader_context, const char* se_info,
121         bool downgrade, int target_sdk_version, const char* profile_name,
122         const char* dexMetadataPath, const char* compilation_reason, std::string* error_msg);
123 
124 bool calculate_oat_file_path_default(char path[PKG_PATH_MAX], const char *oat_dir,
125         const char *apk_path, const char *instruction_set);
126 
127 bool calculate_odex_file_path_default(char path[PKG_PATH_MAX], const char *apk_path,
128         const char *instruction_set);
129 
130 bool create_cache_path_default(char path[PKG_PATH_MAX], const char *src,
131         const char *instruction_set);
132 
133 bool move_ab(const char* apk_path, const char* instruction_set, const char* output_path);
134 
135 const char* select_execution_binary(
136         const char* binary,
137         const char* debug_binary,
138         bool background_job_compile,
139         bool is_debug_runtime,
140         bool is_release,
141         bool is_debuggable_build);
142 
143 }  // namespace installd
144 }  // namespace android
145 
146 #endif  // DEXOPT_H_
147