blob: 17ea90359c31fbad8cb3f1d81a9c5ef8d18ee8c1 [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) {
Victor Hsiehb3459402020-07-07 12:34:54 -070089 PrepareBootImageAndBootClasspathFlags(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
Victor Hsiehb3459402020-07-07 12:34:54 -0700115void RunDex2Oat::PrepareBootImageAndBootClasspathFlags(bool use_jitzygote_image) {
116 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);
123
124 // If DEX2OATBOOTCLASSPATH is not in the environment, dex2oat is going to query
125 // BOOTCLASSPATH.
126 char* dex2oat_bootclasspath = getenv("DEX2OATBOOTCLASSPATH");
127 if (dex2oat_bootclasspath != nullptr) {
128 AddRuntimeArg(StringPrintf("-Xbootclasspath:%s", dex2oat_bootclasspath));
129 }
130
131 std::string updatable_bcp_packages =
132 MapPropertyToArg("dalvik.vm.dex2oat-updatable-bcp-packages-file",
133 "--updatable-bcp-packages-file=%s");
134 if (updatable_bcp_packages.empty()) {
135 // Make dex2oat fail by providing non-existent file name.
136 updatable_bcp_packages =
137 "--updatable-bcp-packages-file=/nonx/updatable-bcp-packages.txt";
138 }
139 AddArg(updatable_bcp_packages);
140}
141
Victor Hsiehcb35a062020-08-13 16:11:13 -0700142void RunDex2Oat::PrepareInputFileFlags(const UniqueFile& output_oat,
143 const UniqueFile& output_vdex,
144 const UniqueFile& output_image,
145 const UniqueFile& input_dex,
146 const UniqueFile& input_vdex,
147 const UniqueFile& dex_metadata,
148 const UniqueFile& profile,
Victor Hsiehb3459402020-07-07 12:34:54 -0700149 int swap_fd,
150 const char* class_loader_context,
151 const std::string& class_loader_context_fds) {
Victor Hsiehcb35a062020-08-13 16:11:13 -0700152 std::string input_basename = Basename(input_dex.path());
153 LOG(VERBOSE) << "Running " << dex2oat_bin_ << " in=" << input_basename << " out="
154 << output_oat.path();
Victor Hsiehb3459402020-07-07 12:34:54 -0700155
Victor Hsiehcb35a062020-08-13 16:11:13 -0700156 AddArg(StringPrintf("--zip-fd=%d", input_dex.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700157 AddArg(StringPrintf("--zip-location=%s", input_basename.c_str()));
Victor Hsiehcb35a062020-08-13 16:11:13 -0700158 AddArg(StringPrintf("--oat-fd=%d", output_oat.fd()));
159 AddArg(StringPrintf("--oat-location=%s", output_oat.path().c_str()));
160 AddArg(StringPrintf("--input-vdex-fd=%d", input_vdex.fd()));
161 AddArg(StringPrintf("--output-vdex-fd=%d", output_vdex.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700162
Victor Hsiehcb35a062020-08-13 16:11:13 -0700163 if (output_image.fd() >= 0) {
164 AddArg(StringPrintf("--app-image-fd=%d", output_image.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700165 AddArg(MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s"));
166 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700167 if (dex_metadata.fd() > -1) {
168 AddArg("--dm-fd=" + std::to_string(dex_metadata.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700169 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700170 if (profile.fd() != -1) {
171 AddArg(StringPrintf("--profile-file-fd=%d", profile.fd()));
Victor Hsiehb3459402020-07-07 12:34:54 -0700172 }
173 if (swap_fd >= 0) {
174 AddArg(StringPrintf("--swap-fd=%d", swap_fd));
175 }
176
177 // Get the directory of the apk to pass as a base classpath directory.
178 {
Victor Hsiehcb35a062020-08-13 16:11:13 -0700179 std::string apk_dir(input_dex.path());
Victor Hsiehb3459402020-07-07 12:34:54 -0700180 size_t dir_index = apk_dir.rfind('/');
181 if (dir_index != std::string::npos) {
182 apk_dir = apk_dir.substr(0, dir_index);
183 AddArg(StringPrintf("--classpath-dir=%s", apk_dir.c_str()));
184 }
185 }
186
187 if (class_loader_context != nullptr) {
188 AddArg(StringPrintf("--class-loader-context=%s", class_loader_context));
189 if (!class_loader_context_fds.empty()) {
190 AddArg(StringPrintf("--class-loader-context-fds=%s",
191 class_loader_context_fds.c_str()));
192 }
193 }
194}
195
Victor Hsiehcb35a062020-08-13 16:11:13 -0700196void RunDex2Oat::PrepareCompilerConfigFlags(const UniqueFile& input_vdex,
197 const UniqueFile& output_vdex,
Victor Hsiehb3459402020-07-07 12:34:54 -0700198 const char* instruction_set,
199 const char* compiler_filter,
200 bool debuggable,
201 int target_sdk_version,
202 bool enable_hidden_api_checks,
203 bool generate_compact_dex,
204 const char* compilation_reason) {
205 // Disable cdex if update input vdex is true since this combination of options is not
206 // supported.
Victor Hsiehcb35a062020-08-13 16:11:13 -0700207 const bool disable_cdex = !generate_compact_dex || (input_vdex.fd() == output_vdex.fd());
Victor Hsiehb3459402020-07-07 12:34:54 -0700208 if (disable_cdex) {
209 AddArg(kDisableCompactDexFlag);
210 }
Victor Hsiehcb35a062020-08-13 16:11:13 -0700211
Victor Hsiehb3459402020-07-07 12:34:54 -0700212 // ISA related
213 {
214 AddArg(StringPrintf("--instruction-set=%s", instruction_set));
215
216 const std::string dex2oat_isa_features_key =
217 StringPrintf("dalvik.vm.isa.%s.features", instruction_set);
218 std::string instruction_set_features_arg =
219 MapPropertyToArg(dex2oat_isa_features_key, "--instruction-set-features=%s");
220 AddArg(instruction_set_features_arg);
221
222 const std::string dex2oat_isa_variant_key =
223 StringPrintf("dalvik.vm.isa.%s.variant", instruction_set);
224 std::string instruction_set_variant_arg =
225 MapPropertyToArg(dex2oat_isa_variant_key, "--instruction-set-variant=%s");
226 AddArg(instruction_set_variant_arg);
227 }
228
229 // Compute compiler filter.
230 {
231 std::string dex2oat_compiler_filter_arg;
232 {
233 // If we are booting without the real /data, don't spend time compiling.
234 std::string vold_decrypt = GetProperty("vold.decrypt", "");
235 bool skip_compilation = vold_decrypt == "trigger_restart_min_framework" ||
236 vold_decrypt == "1";
237
238 bool have_dex2oat_relocation_skip_flag = false;
239 if (skip_compilation) {
240 dex2oat_compiler_filter_arg = "--compiler-filter=extract";
241 have_dex2oat_relocation_skip_flag = true;
242 } else if (compiler_filter != nullptr) {
243 dex2oat_compiler_filter_arg = StringPrintf("--compiler-filter=%s",
244 compiler_filter);
245 }
246 if (have_dex2oat_relocation_skip_flag) {
247 AddRuntimeArg("-Xnorelocate");
248 }
249 }
250
251 if (dex2oat_compiler_filter_arg.empty()) {
252 dex2oat_compiler_filter_arg = MapPropertyToArg("dalvik.vm.dex2oat-filter",
253 "--compiler-filter=%s");
254 }
255 AddArg(dex2oat_compiler_filter_arg);
256
257 if (compilation_reason != nullptr) {
258 AddArg(std::string("--compilation-reason=") + compilation_reason);
259 }
260 }
261
262 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
263 "--max-image-block-size=%s"));
264
265 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-very-large",
266 "--very-large-app-threshold=%s"));
267
268 std::string resolve_startup_string_arg = MapPropertyToArg(
269 "persist.device_config.runtime.dex2oat_resolve_startup_strings",
270 "--resolve-startup-const-strings=%s");
271 if (resolve_startup_string_arg.empty()) {
272 // If empty, fall back to system property.
273 resolve_startup_string_arg =
274 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
275 "--resolve-startup-const-strings=%s");
276 }
277 AddArg(resolve_startup_string_arg);
278
279 // Debug related
280 {
281 // Check whether all apps should be compiled debuggable.
282 if (!debuggable) {
283 debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
284 }
285 if (debuggable) {
286 AddArg("--debuggable");
287 }
288
289 const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
290 if (generate_debug_info) {
291 AddArg("--generate-debug-info");
292 }
293 {
294 bool generate_minidebug_info = kEnableMinidebugInfo &&
295 GetBoolProperty(kMinidebugInfoSystemProperty,
296 kMinidebugInfoSystemPropertyDefault);
297 if (generate_minidebug_info) {
298 AddArg(kMinidebugDex2oatFlag);
299 }
300 }
301 }
302
303 if (target_sdk_version != 0) {
304 AddRuntimeArg(StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version));
305 }
306
307 if (enable_hidden_api_checks) {
308 AddRuntimeArg("-Xhidden-api-policy:enabled");
309 }
310}
311
312void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
313 bool for_restore) {
314 // CPU set
315 {
316 std::string cpu_set_format = "--cpu-set=%s";
317 std::string dex2oat_cpu_set_arg = post_bootcomplete
318 ? (for_restore
319 ? MapPropertyToArgWithBackup(
320 "dalvik.vm.restore-dex2oat-cpu-set",
321 "dalvik.vm.dex2oat-cpu-set",
322 cpu_set_format)
323 : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format))
324 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
325 AddArg(dex2oat_cpu_set_arg);
326 }
327
328 // Number of threads
329 {
330 std::string threads_format = "-j%s";
331 std::string dex2oat_threads_arg = post_bootcomplete
332 ? (for_restore
333 ? MapPropertyToArgWithBackup(
334 "dalvik.vm.restore-dex2oat-threads",
335 "dalvik.vm.dex2oat-threads",
336 threads_format)
337 : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format))
338 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
339 AddArg(dex2oat_threads_arg);
340 }
341
342 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s"));
343 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s"));
344}
345
Victor Hsiehc9821f12020-08-07 11:32:29 -0700346void RunDex2Oat::Exec(int exit_code) {
Victor Hsiehc9821f12020-08-07 11:32:29 -0700347 execv_helper_->Exec(exit_code);
348}
349
350void RunDex2Oat::AddArg(const std::string& arg) {
351 execv_helper_->AddArg(arg);
352}
353
354void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
355 execv_helper_->AddRuntimeArg(arg);
356}
357
358std::string RunDex2Oat::GetProperty(const std::string& key,
359 const std::string& default_value) {
360 return android::base::GetProperty(key, default_value);
361}
362
363bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
364 return android::base::GetBoolProperty(key, default_value);
365}
366
367std::string RunDex2Oat::MapPropertyToArg(const std::string& property,
368 const std::string& format,
369 const std::string& default_value) {
370 std::string prop = GetProperty(property, default_value);
371 if (!prop.empty()) {
372 return StringPrintf(format.c_str(), prop.c_str());
373 }
374 return "";
375}
376
377std::string RunDex2Oat::MapPropertyToArgWithBackup(
378 const std::string& property,
379 const std::string& backupProperty,
380 const std::string& format,
381 const std::string& default_value) {
382 std::string value = GetProperty(property, default_value);
383 if (!value.empty()) {
384 return StringPrintf(format.c_str(), value.c_str());
385 }
386 return MapPropertyToArg(backupProperty, format, default_value);
387}
388
389} // namespace installd
390} // namespace android