blob: 7648265f0f8dff4fc4954e0008ed2e9a9abc4240 [file] [log] [blame]
Victor Hsiehc9821f12020-08-07 11:32:29 -07001/*
2 * Copyright (C) 2020 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#define LOG_TAG "installd"
17
18#include "run_dex2oat.h"
19
20#include <memory>
21#include <string>
22#include <vector>
23
24#include <android-base/file.h>
25#include <android-base/logging.h>
26#include <android-base/properties.h>
27#include <android-base/scopeguard.h>
28#include <android-base/stringprintf.h>
29#include <android-base/strings.h>
30#include <log/log.h>
31#include <server_configurable_flags/get_flags.h>
32
Victor Hsiehcb35a062020-08-13 16:11:13 -070033#include "unique_file.h"
34
Victor Hsiehc9821f12020-08-07 11:32:29 -070035using android::base::Basename;
36using android::base::StringPrintf;
37
38namespace android {
39namespace installd {
40
41namespace {
42
43// Should minidebug info be included in compiled artifacts? Even if this value is
44// "true," usage might still be conditional to other constraints, e.g., system
45// property overrides.
46static constexpr bool kEnableMinidebugInfo = true;
47
48static constexpr const char* kMinidebugInfoSystemProperty = "dalvik.vm.dex2oat-minidebuginfo";
49static constexpr bool kMinidebugInfoSystemPropertyDefault = false;
50static constexpr const char* kMinidebugDex2oatFlag = "--generate-mini-debug-info";
51static constexpr const char* kDisableCompactDexFlag = "--compact-dex-level=none";
52
Victor Hsiehc9821f12020-08-07 11:32:29 -070053std::vector<std::string> SplitBySpaces(const std::string& str) {
54 if (str.empty()) {
55 return {};
56 }
57 return android::base::Split(str, " ");
58}
59
60} // namespace
61
62RunDex2Oat::RunDex2Oat(const char* dex2oat_bin, ExecVHelper* execv_helper)
63 : dex2oat_bin_(dex2oat_bin), execv_helper_(execv_helper) {}
64
Victor Hsiehcb35a062020-08-13 16:11:13 -070065void RunDex2Oat::Initialize(const UniqueFile& output_oat,
66 const UniqueFile& output_vdex,
67 const UniqueFile& output_image,
68 const UniqueFile& input_dex,
69 const UniqueFile& input_vdex,
70 const UniqueFile& dex_metadata,
71 const UniqueFile& profile,
72 const char* class_loader_context,
73 const std::string& class_loader_context_fds,
Victor Hsiehc9821f12020-08-07 11:32:29 -070074 int swap_fd,
75 const char* instruction_set,
76 const char* compiler_filter,
77 bool debuggable,
78 bool post_bootcomplete,
79 bool for_restore,
Victor Hsiehc9821f12020-08-07 11:32:29 -070080 int target_sdk_version,
81 bool enable_hidden_api_checks,
82 bool generate_compact_dex,
Jiakai Zhang202524b2021-12-22 16:40:52 +000083 bool use_jitzygote,
Martin Stjernholm02570a72022-11-01 13:56:46 +000084 bool background_job_compile,
Victor Hsiehc9821f12020-08-07 11:32:29 -070085 const char* compilation_reason) {
Jiakai Zhang202524b2021-12-22 16:40:52 +000086 PrepareBootImageFlags(use_jitzygote);
Victor Hsiehc9821f12020-08-07 11:32:29 -070087
Victor Hsiehcb35a062020-08-13 16:11:13 -070088 PrepareInputFileFlags(output_oat, output_vdex, output_image, input_dex, input_vdex,
89 dex_metadata, profile, swap_fd, class_loader_context,
90 class_loader_context_fds);
Victor Hsiehc9821f12020-08-07 11:32:29 -070091
Victor Hsiehcb35a062020-08-13 16:11:13 -070092 PrepareCompilerConfigFlags(input_vdex, output_vdex, instruction_set, compiler_filter,
Victor Hsiehb3459402020-07-07 12:34:54 -070093 debuggable, target_sdk_version, enable_hidden_api_checks,
94 generate_compact_dex, compilation_reason);
Victor Hsiehc9821f12020-08-07 11:32:29 -070095
Martin Stjernholm02570a72022-11-01 13:56:46 +000096 PrepareCompilerRuntimeAndPerfConfigFlags(post_bootcomplete, for_restore,
97 background_job_compile);
Victor Hsiehc9821f12020-08-07 11:32:29 -070098
99 const std::string dex2oat_flags = GetProperty("dalvik.vm.dex2oat-flags", "");
100 std::vector<std::string> dex2oat_flags_args = SplitBySpaces(dex2oat_flags);
101 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags.c_str());
102
Victor Hsiehc9821f12020-08-07 11:32:29 -0700103 // Do not add args after dex2oat_flags, they should override others for debugging.
104 for (auto it = dex2oat_flags_args.begin(); it != dex2oat_flags_args.end(); ++it) {
105 AddArg(*it);
106 }
107
108 execv_helper_->PrepareArgs(dex2oat_bin_);
109}
110
111RunDex2Oat::~RunDex2Oat() {}
112
Jiakai Zhang202524b2021-12-22 16:40:52 +0000113void RunDex2Oat::PrepareBootImageFlags(bool use_jitzygote) {
114 if (use_jitzygote) {
115 // Don't pass a boot image because JIT Zygote should decide which image to use. Typically,
116 // it does not use any boot image on disk.
117 AddArg("--force-jit-zygote");
Victor Hsiehb3459402020-07-07 12:34:54 -0700118 } else {
Jiakai Zhang202524b2021-12-22 16:40:52 +0000119 AddArg(MapPropertyToArg("dalvik.vm.boot-image", "--boot-image=%s"));
Victor Hsiehb3459402020-07-07 12:34:54 -0700120 }
Victor Hsiehb3459402020-07-07 12:34:54 -0700121}
122
Victor Hsiehcb35a062020-08-13 16:11:13 -0700123void RunDex2Oat::PrepareInputFileFlags(const UniqueFile& output_oat,
124 const UniqueFile& output_vdex,
125 const UniqueFile& output_image,
126 const UniqueFile& input_dex,
127 const UniqueFile& input_vdex,
128 const UniqueFile& dex_metadata,
129 const UniqueFile& profile,
Victor Hsiehb3459402020-07-07 12:34:54 -0700130 int swap_fd,
131 const char* class_loader_context,
132 const std::string& class_loader_context_fds) {
Victor Hsiehcb35a062020-08-13 16:11:13 -0700133 std::string input_basename = Basename(input_dex.path());
134 LOG(VERBOSE) << "Running " << dex2oat_bin_ << " in=" << input_basename << " out="
135 << output_oat.path();
Victor Hsiehb3459402020-07-07 12:34:54 -0700136
Victor Hsiehcb35a062020-08-13 16:11:13 -0700137 AddArg(StringPrintf("--zip-fd=%d", input_dex.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700138 AddArg(StringPrintf("--zip-location=%s", input_basename.c_str()));
Victor Hsiehcb35a062020-08-13 16:11:13 -0700139 AddArg(StringPrintf("--oat-fd=%d", output_oat.fd()));
140 AddArg(StringPrintf("--oat-location=%s", output_oat.path().c_str()));
141 AddArg(StringPrintf("--input-vdex-fd=%d", input_vdex.fd()));
142 AddArg(StringPrintf("--output-vdex-fd=%d", output_vdex.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700143
Victor Hsiehcb35a062020-08-13 16:11:13 -0700144 if (output_image.fd() >= 0) {
145 AddArg(StringPrintf("--app-image-fd=%d", output_image.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700146 AddArg(MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s"));
147 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700148 if (dex_metadata.fd() > -1) {
149 AddArg("--dm-fd=" + std::to_string(dex_metadata.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700150 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700151 if (profile.fd() != -1) {
152 AddArg(StringPrintf("--profile-file-fd=%d", profile.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700153 }
154 if (swap_fd >= 0) {
155 AddArg(StringPrintf("--swap-fd=%d", swap_fd));
156 }
157
158 // Get the directory of the apk to pass as a base classpath directory.
159 {
Victor Hsiehcb35a062020-08-13 16:11:13 -0700160 std::string apk_dir(input_dex.path());
Victor Hsiehb3459402020-07-07 12:34:54 -0700161 size_t dir_index = apk_dir.rfind('/');
162 if (dir_index != std::string::npos) {
163 apk_dir = apk_dir.substr(0, dir_index);
164 AddArg(StringPrintf("--classpath-dir=%s", apk_dir.c_str()));
165 }
166 }
167
168 if (class_loader_context != nullptr) {
169 AddArg(StringPrintf("--class-loader-context=%s", class_loader_context));
170 if (!class_loader_context_fds.empty()) {
171 AddArg(StringPrintf("--class-loader-context-fds=%s",
172 class_loader_context_fds.c_str()));
173 }
174 }
175}
176
Victor Hsiehcb35a062020-08-13 16:11:13 -0700177void RunDex2Oat::PrepareCompilerConfigFlags(const UniqueFile& input_vdex,
178 const UniqueFile& output_vdex,
Victor Hsiehb3459402020-07-07 12:34:54 -0700179 const char* instruction_set,
180 const char* compiler_filter,
181 bool debuggable,
182 int target_sdk_version,
183 bool enable_hidden_api_checks,
184 bool generate_compact_dex,
185 const char* compilation_reason) {
186 // Disable cdex if update input vdex is true since this combination of options is not
187 // supported.
Victor Hsiehcb35a062020-08-13 16:11:13 -0700188 const bool disable_cdex = !generate_compact_dex || (input_vdex.fd() == output_vdex.fd());
Victor Hsiehb3459402020-07-07 12:34:54 -0700189 if (disable_cdex) {
190 AddArg(kDisableCompactDexFlag);
191 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700192
Victor Hsiehb3459402020-07-07 12:34:54 -0700193 // ISA related
194 {
195 AddArg(StringPrintf("--instruction-set=%s", instruction_set));
196
197 const std::string dex2oat_isa_features_key =
198 StringPrintf("dalvik.vm.isa.%s.features", instruction_set);
199 std::string instruction_set_features_arg =
200 MapPropertyToArg(dex2oat_isa_features_key, "--instruction-set-features=%s");
201 AddArg(instruction_set_features_arg);
202
203 const std::string dex2oat_isa_variant_key =
204 StringPrintf("dalvik.vm.isa.%s.variant", instruction_set);
205 std::string instruction_set_variant_arg =
206 MapPropertyToArg(dex2oat_isa_variant_key, "--instruction-set-variant=%s");
207 AddArg(instruction_set_variant_arg);
208 }
209
210 // Compute compiler filter.
Eric Biggersa48858e2023-08-11 16:30:37 +0000211 if (compiler_filter != nullptr) {
212 AddArg(StringPrintf("--compiler-filter=%s", compiler_filter));
213 } else {
214 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-filter", "--compiler-filter=%s"));
215 }
216 if (compilation_reason != nullptr) {
217 AddArg(std::string("--compilation-reason=") + compilation_reason);
Victor Hsiehb3459402020-07-07 12:34:54 -0700218 }
219
220 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
221 "--max-image-block-size=%s"));
222
223 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-very-large",
224 "--very-large-app-threshold=%s"));
225
226 std::string resolve_startup_string_arg = MapPropertyToArg(
227 "persist.device_config.runtime.dex2oat_resolve_startup_strings",
228 "--resolve-startup-const-strings=%s");
229 if (resolve_startup_string_arg.empty()) {
230 // If empty, fall back to system property.
231 resolve_startup_string_arg =
232 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
233 "--resolve-startup-const-strings=%s");
234 }
235 AddArg(resolve_startup_string_arg);
236
237 // Debug related
238 {
239 // Check whether all apps should be compiled debuggable.
240 if (!debuggable) {
241 debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
242 }
243 if (debuggable) {
244 AddArg("--debuggable");
245 }
246
247 const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
248 if (generate_debug_info) {
249 AddArg("--generate-debug-info");
250 }
251 {
252 bool generate_minidebug_info = kEnableMinidebugInfo &&
253 GetBoolProperty(kMinidebugInfoSystemProperty,
254 kMinidebugInfoSystemPropertyDefault);
255 if (generate_minidebug_info) {
256 AddArg(kMinidebugDex2oatFlag);
257 }
258 }
259 }
260
Orion Hodson2b12e7c2021-07-01 12:12:40 +0100261 // On-device signing related. odsign sets the system property odsign.verification.success if
262 // AOT artifacts have the expected signatures.
263 const bool trust_art_apex_data_files = GetBoolProperty("odsign.verification.success", false);
264 if (!trust_art_apex_data_files) {
265 AddRuntimeArg("-Xdeny-art-apex-data-files");
266 }
267
Victor Hsiehb3459402020-07-07 12:34:54 -0700268 if (target_sdk_version != 0) {
269 AddRuntimeArg(StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version));
270 }
271
272 if (enable_hidden_api_checks) {
273 AddRuntimeArg("-Xhidden-api-policy:enabled");
274 }
275}
276
277void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
Martin Stjernholm02570a72022-11-01 13:56:46 +0000278 bool for_restore,
279 bool background_job_compile) {
Victor Hsiehb3459402020-07-07 12:34:54 -0700280 // CPU set
281 {
282 std::string cpu_set_format = "--cpu-set=%s";
283 std::string dex2oat_cpu_set_arg = post_bootcomplete
284 ? (for_restore
285 ? MapPropertyToArgWithBackup(
286 "dalvik.vm.restore-dex2oat-cpu-set",
287 "dalvik.vm.dex2oat-cpu-set",
288 cpu_set_format)
Martin Stjernholm02570a72022-11-01 13:56:46 +0000289 : (background_job_compile
290 ? MapPropertyToArgWithBackup(
291 "dalvik.vm.background-dex2oat-cpu-set",
292 "dalvik.vm.dex2oat-cpu-set",
293 cpu_set_format)
294 : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format)))
Victor Hsiehb3459402020-07-07 12:34:54 -0700295 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
296 AddArg(dex2oat_cpu_set_arg);
297 }
298
299 // Number of threads
300 {
301 std::string threads_format = "-j%s";
302 std::string dex2oat_threads_arg = post_bootcomplete
303 ? (for_restore
304 ? MapPropertyToArgWithBackup(
305 "dalvik.vm.restore-dex2oat-threads",
306 "dalvik.vm.dex2oat-threads",
307 threads_format)
Martin Stjernholm02570a72022-11-01 13:56:46 +0000308 : (background_job_compile
309 ? MapPropertyToArgWithBackup(
310 "dalvik.vm.background-dex2oat-threads",
311 "dalvik.vm.dex2oat-threads",
312 threads_format)
313 : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format)))
Victor Hsiehb3459402020-07-07 12:34:54 -0700314 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
315 AddArg(dex2oat_threads_arg);
316 }
317
318 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s"));
319 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s"));
Calin Juravle380fff72021-06-17 15:37:08 -0700320
321 // Enable compiling dex files in isolation on low ram devices.
322 // It takes longer but reduces the memory footprint.
323 if (GetBoolProperty("ro.config.low_ram", false)) {
324 AddArg("--compile-individually");
325 }
Victor Hsiehb3459402020-07-07 12:34:54 -0700326}
327
Victor Hsiehc9821f12020-08-07 11:32:29 -0700328void RunDex2Oat::Exec(int exit_code) {
Victor Hsiehc9821f12020-08-07 11:32:29 -0700329 execv_helper_->Exec(exit_code);
330}
331
332void RunDex2Oat::AddArg(const std::string& arg) {
333 execv_helper_->AddArg(arg);
334}
335
336void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
337 execv_helper_->AddRuntimeArg(arg);
338}
339
340std::string RunDex2Oat::GetProperty(const std::string& key,
341 const std::string& default_value) {
342 return android::base::GetProperty(key, default_value);
343}
344
345bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
346 return android::base::GetBoolProperty(key, default_value);
347}
348
349std::string RunDex2Oat::MapPropertyToArg(const std::string& property,
350 const std::string& format,
351 const std::string& default_value) {
352 std::string prop = GetProperty(property, default_value);
353 if (!prop.empty()) {
354 return StringPrintf(format.c_str(), prop.c_str());
355 }
356 return "";
357}
358
359std::string RunDex2Oat::MapPropertyToArgWithBackup(
360 const std::string& property,
361 const std::string& backupProperty,
362 const std::string& format,
363 const std::string& default_value) {
364 std::string value = GetProperty(property, default_value);
365 if (!value.empty()) {
366 return StringPrintf(format.c_str(), value.c_str());
367 }
368 return MapPropertyToArg(backupProperty, format, default_value);
369}
370
371} // namespace installd
372} // namespace android