blob: b6616841f0b0c637567f7e28ebf53c782d60919e [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
53// Location of the JIT Zygote image.
54static const char* kJitZygoteImage =
55 "boot.art:/nonx/boot-framework.art!/system/etc/boot-image.prof";
56
57std::vector<std::string> SplitBySpaces(const std::string& str) {
58 if (str.empty()) {
59 return {};
60 }
61 return android::base::Split(str, " ");
62}
63
64} // namespace
65
66RunDex2Oat::RunDex2Oat(const char* dex2oat_bin, ExecVHelper* execv_helper)
67 : dex2oat_bin_(dex2oat_bin), execv_helper_(execv_helper) {}
68
Victor Hsiehcb35a062020-08-13 16:11:13 -070069void RunDex2Oat::Initialize(const UniqueFile& output_oat,
70 const UniqueFile& output_vdex,
71 const UniqueFile& output_image,
72 const UniqueFile& input_dex,
73 const UniqueFile& input_vdex,
74 const UniqueFile& dex_metadata,
75 const UniqueFile& profile,
76 const char* class_loader_context,
77 const std::string& class_loader_context_fds,
Victor Hsiehc9821f12020-08-07 11:32:29 -070078 int swap_fd,
79 const char* instruction_set,
80 const char* compiler_filter,
81 bool debuggable,
82 bool post_bootcomplete,
83 bool for_restore,
Victor Hsiehc9821f12020-08-07 11:32:29 -070084 int target_sdk_version,
85 bool enable_hidden_api_checks,
86 bool generate_compact_dex,
Victor Hsiehc9821f12020-08-07 11:32:29 -070087 bool use_jitzygote_image,
88 const char* compilation_reason) {
Nicolas Geoffraya69482e2021-03-29 10:53:55 +010089 PrepareBootImageFlags(use_jitzygote_image);
Victor Hsiehc9821f12020-08-07 11:32:29 -070090
Victor Hsiehcb35a062020-08-13 16:11:13 -070091 PrepareInputFileFlags(output_oat, output_vdex, output_image, input_dex, input_vdex,
92 dex_metadata, profile, swap_fd, class_loader_context,
93 class_loader_context_fds);
Victor Hsiehc9821f12020-08-07 11:32:29 -070094
Victor Hsiehcb35a062020-08-13 16:11:13 -070095 PrepareCompilerConfigFlags(input_vdex, output_vdex, instruction_set, compiler_filter,
Victor Hsiehb3459402020-07-07 12:34:54 -070096 debuggable, target_sdk_version, enable_hidden_api_checks,
97 generate_compact_dex, compilation_reason);
Victor Hsiehc9821f12020-08-07 11:32:29 -070098
Victor Hsiehb3459402020-07-07 12:34:54 -070099 PrepareCompilerRuntimeAndPerfConfigFlags(post_bootcomplete, for_restore);
Victor Hsiehc9821f12020-08-07 11:32:29 -0700100
101 const std::string dex2oat_flags = GetProperty("dalvik.vm.dex2oat-flags", "");
102 std::vector<std::string> dex2oat_flags_args = SplitBySpaces(dex2oat_flags);
103 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags.c_str());
104
Victor Hsiehc9821f12020-08-07 11:32:29 -0700105 // Do not add args after dex2oat_flags, they should override others for debugging.
106 for (auto it = dex2oat_flags_args.begin(); it != dex2oat_flags_args.end(); ++it) {
107 AddArg(*it);
108 }
109
110 execv_helper_->PrepareArgs(dex2oat_bin_);
111}
112
113RunDex2Oat::~RunDex2Oat() {}
114
Nicolas Geoffraya69482e2021-03-29 10:53:55 +0100115void RunDex2Oat::PrepareBootImageFlags(bool use_jitzygote_image) {
Victor Hsiehb3459402020-07-07 12:34:54 -0700116 std::string boot_image;
117 if (use_jitzygote_image) {
118 boot_image = StringPrintf("--boot-image=%s", kJitZygoteImage);
119 } else {
120 boot_image = MapPropertyToArg("dalvik.vm.boot-image", "--boot-image=%s");
121 }
122 AddArg(boot_image);
Victor Hsiehb3459402020-07-07 12:34:54 -0700123}
124
Victor Hsiehcb35a062020-08-13 16:11:13 -0700125void RunDex2Oat::PrepareInputFileFlags(const UniqueFile& output_oat,
126 const UniqueFile& output_vdex,
127 const UniqueFile& output_image,
128 const UniqueFile& input_dex,
129 const UniqueFile& input_vdex,
130 const UniqueFile& dex_metadata,
131 const UniqueFile& profile,
Victor Hsiehb3459402020-07-07 12:34:54 -0700132 int swap_fd,
133 const char* class_loader_context,
134 const std::string& class_loader_context_fds) {
Victor Hsiehcb35a062020-08-13 16:11:13 -0700135 std::string input_basename = Basename(input_dex.path());
136 LOG(VERBOSE) << "Running " << dex2oat_bin_ << " in=" << input_basename << " out="
137 << output_oat.path();
Victor Hsiehb3459402020-07-07 12:34:54 -0700138
Victor Hsiehcb35a062020-08-13 16:11:13 -0700139 AddArg(StringPrintf("--zip-fd=%d", input_dex.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700140 AddArg(StringPrintf("--zip-location=%s", input_basename.c_str()));
Victor Hsiehcb35a062020-08-13 16:11:13 -0700141 AddArg(StringPrintf("--oat-fd=%d", output_oat.fd()));
142 AddArg(StringPrintf("--oat-location=%s", output_oat.path().c_str()));
143 AddArg(StringPrintf("--input-vdex-fd=%d", input_vdex.fd()));
144 AddArg(StringPrintf("--output-vdex-fd=%d", output_vdex.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700145
Victor Hsiehcb35a062020-08-13 16:11:13 -0700146 if (output_image.fd() >= 0) {
147 AddArg(StringPrintf("--app-image-fd=%d", output_image.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700148 AddArg(MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s"));
149 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700150 if (dex_metadata.fd() > -1) {
151 AddArg("--dm-fd=" + std::to_string(dex_metadata.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700152 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700153 if (profile.fd() != -1) {
154 AddArg(StringPrintf("--profile-file-fd=%d", profile.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700155 }
156 if (swap_fd >= 0) {
157 AddArg(StringPrintf("--swap-fd=%d", swap_fd));
158 }
159
160 // Get the directory of the apk to pass as a base classpath directory.
161 {
Victor Hsiehcb35a062020-08-13 16:11:13 -0700162 std::string apk_dir(input_dex.path());
Victor Hsiehb3459402020-07-07 12:34:54 -0700163 size_t dir_index = apk_dir.rfind('/');
164 if (dir_index != std::string::npos) {
165 apk_dir = apk_dir.substr(0, dir_index);
166 AddArg(StringPrintf("--classpath-dir=%s", apk_dir.c_str()));
167 }
168 }
169
170 if (class_loader_context != nullptr) {
171 AddArg(StringPrintf("--class-loader-context=%s", class_loader_context));
172 if (!class_loader_context_fds.empty()) {
173 AddArg(StringPrintf("--class-loader-context-fds=%s",
174 class_loader_context_fds.c_str()));
175 }
176 }
177}
178
Victor Hsiehcb35a062020-08-13 16:11:13 -0700179void RunDex2Oat::PrepareCompilerConfigFlags(const UniqueFile& input_vdex,
180 const UniqueFile& output_vdex,
Victor Hsiehb3459402020-07-07 12:34:54 -0700181 const char* instruction_set,
182 const char* compiler_filter,
183 bool debuggable,
184 int target_sdk_version,
185 bool enable_hidden_api_checks,
186 bool generate_compact_dex,
187 const char* compilation_reason) {
188 // Disable cdex if update input vdex is true since this combination of options is not
189 // supported.
Victor Hsiehcb35a062020-08-13 16:11:13 -0700190 const bool disable_cdex = !generate_compact_dex || (input_vdex.fd() == output_vdex.fd());
Victor Hsiehb3459402020-07-07 12:34:54 -0700191 if (disable_cdex) {
192 AddArg(kDisableCompactDexFlag);
193 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700194
Victor Hsiehb3459402020-07-07 12:34:54 -0700195 // ISA related
196 {
197 AddArg(StringPrintf("--instruction-set=%s", instruction_set));
198
199 const std::string dex2oat_isa_features_key =
200 StringPrintf("dalvik.vm.isa.%s.features", instruction_set);
201 std::string instruction_set_features_arg =
202 MapPropertyToArg(dex2oat_isa_features_key, "--instruction-set-features=%s");
203 AddArg(instruction_set_features_arg);
204
205 const std::string dex2oat_isa_variant_key =
206 StringPrintf("dalvik.vm.isa.%s.variant", instruction_set);
207 std::string instruction_set_variant_arg =
208 MapPropertyToArg(dex2oat_isa_variant_key, "--instruction-set-variant=%s");
209 AddArg(instruction_set_variant_arg);
210 }
211
212 // Compute compiler filter.
213 {
214 std::string dex2oat_compiler_filter_arg;
215 {
216 // If we are booting without the real /data, don't spend time compiling.
217 std::string vold_decrypt = GetProperty("vold.decrypt", "");
218 bool skip_compilation = vold_decrypt == "trigger_restart_min_framework" ||
219 vold_decrypt == "1";
220
221 bool have_dex2oat_relocation_skip_flag = false;
222 if (skip_compilation) {
223 dex2oat_compiler_filter_arg = "--compiler-filter=extract";
224 have_dex2oat_relocation_skip_flag = true;
225 } else if (compiler_filter != nullptr) {
226 dex2oat_compiler_filter_arg = StringPrintf("--compiler-filter=%s",
227 compiler_filter);
228 }
229 if (have_dex2oat_relocation_skip_flag) {
230 AddRuntimeArg("-Xnorelocate");
231 }
232 }
233
234 if (dex2oat_compiler_filter_arg.empty()) {
235 dex2oat_compiler_filter_arg = MapPropertyToArg("dalvik.vm.dex2oat-filter",
236 "--compiler-filter=%s");
237 }
238 AddArg(dex2oat_compiler_filter_arg);
239
240 if (compilation_reason != nullptr) {
241 AddArg(std::string("--compilation-reason=") + compilation_reason);
242 }
243 }
244
245 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
246 "--max-image-block-size=%s"));
247
248 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-very-large",
249 "--very-large-app-threshold=%s"));
250
251 std::string resolve_startup_string_arg = MapPropertyToArg(
252 "persist.device_config.runtime.dex2oat_resolve_startup_strings",
253 "--resolve-startup-const-strings=%s");
254 if (resolve_startup_string_arg.empty()) {
255 // If empty, fall back to system property.
256 resolve_startup_string_arg =
257 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
258 "--resolve-startup-const-strings=%s");
259 }
260 AddArg(resolve_startup_string_arg);
261
262 // Debug related
263 {
264 // Check whether all apps should be compiled debuggable.
265 if (!debuggable) {
266 debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
267 }
268 if (debuggable) {
269 AddArg("--debuggable");
270 }
271
272 const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
273 if (generate_debug_info) {
274 AddArg("--generate-debug-info");
275 }
276 {
277 bool generate_minidebug_info = kEnableMinidebugInfo &&
278 GetBoolProperty(kMinidebugInfoSystemProperty,
279 kMinidebugInfoSystemPropertyDefault);
280 if (generate_minidebug_info) {
281 AddArg(kMinidebugDex2oatFlag);
282 }
283 }
284 }
285
Orion Hodson2b12e7c2021-07-01 12:12:40 +0100286 // On-device signing related. odsign sets the system property odsign.verification.success if
287 // AOT artifacts have the expected signatures.
288 const bool trust_art_apex_data_files = GetBoolProperty("odsign.verification.success", false);
289 if (!trust_art_apex_data_files) {
290 AddRuntimeArg("-Xdeny-art-apex-data-files");
291 }
292
Victor Hsiehb3459402020-07-07 12:34:54 -0700293 if (target_sdk_version != 0) {
294 AddRuntimeArg(StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version));
295 }
296
297 if (enable_hidden_api_checks) {
298 AddRuntimeArg("-Xhidden-api-policy:enabled");
299 }
300}
301
302void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
303 bool for_restore) {
304 // CPU set
305 {
306 std::string cpu_set_format = "--cpu-set=%s";
307 std::string dex2oat_cpu_set_arg = post_bootcomplete
308 ? (for_restore
309 ? MapPropertyToArgWithBackup(
310 "dalvik.vm.restore-dex2oat-cpu-set",
311 "dalvik.vm.dex2oat-cpu-set",
312 cpu_set_format)
313 : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format))
314 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
315 AddArg(dex2oat_cpu_set_arg);
316 }
317
318 // Number of threads
319 {
320 std::string threads_format = "-j%s";
321 std::string dex2oat_threads_arg = post_bootcomplete
322 ? (for_restore
323 ? MapPropertyToArgWithBackup(
324 "dalvik.vm.restore-dex2oat-threads",
325 "dalvik.vm.dex2oat-threads",
326 threads_format)
327 : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format))
328 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
329 AddArg(dex2oat_threads_arg);
330 }
331
332 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s"));
333 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s"));
Calin Juravle380fff72021-06-17 15:37:08 -0700334
335 // Enable compiling dex files in isolation on low ram devices.
336 // It takes longer but reduces the memory footprint.
337 if (GetBoolProperty("ro.config.low_ram", false)) {
338 AddArg("--compile-individually");
339 }
Victor Hsiehb3459402020-07-07 12:34:54 -0700340}
341
Victor Hsiehc9821f12020-08-07 11:32:29 -0700342void RunDex2Oat::Exec(int exit_code) {
Victor Hsiehc9821f12020-08-07 11:32:29 -0700343 execv_helper_->Exec(exit_code);
344}
345
346void RunDex2Oat::AddArg(const std::string& arg) {
347 execv_helper_->AddArg(arg);
348}
349
350void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
351 execv_helper_->AddRuntimeArg(arg);
352}
353
354std::string RunDex2Oat::GetProperty(const std::string& key,
355 const std::string& default_value) {
356 return android::base::GetProperty(key, default_value);
357}
358
359bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
360 return android::base::GetBoolProperty(key, default_value);
361}
362
363std::string RunDex2Oat::MapPropertyToArg(const std::string& property,
364 const std::string& format,
365 const std::string& default_value) {
366 std::string prop = GetProperty(property, default_value);
367 if (!prop.empty()) {
368 return StringPrintf(format.c_str(), prop.c_str());
369 }
370 return "";
371}
372
373std::string RunDex2Oat::MapPropertyToArgWithBackup(
374 const std::string& property,
375 const std::string& backupProperty,
376 const std::string& format,
377 const std::string& default_value) {
378 std::string value = GetProperty(property, default_value);
379 if (!value.empty()) {
380 return StringPrintf(format.c_str(), value.c_str());
381 }
382 return MapPropertyToArg(backupProperty, format, default_value);
383}
384
385} // namespace installd
386} // namespace android