blob: 1572e3b2a46127329f1e529df0b3201a67a5cb07 [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
33using android::base::Basename;
34using android::base::StringPrintf;
35
36namespace android {
37namespace installd {
38
39namespace {
40
41// Should minidebug info be included in compiled artifacts? Even if this value is
42// "true," usage might still be conditional to other constraints, e.g., system
43// property overrides.
44static constexpr bool kEnableMinidebugInfo = true;
45
46static constexpr const char* kMinidebugInfoSystemProperty = "dalvik.vm.dex2oat-minidebuginfo";
47static constexpr bool kMinidebugInfoSystemPropertyDefault = false;
48static constexpr const char* kMinidebugDex2oatFlag = "--generate-mini-debug-info";
49static constexpr const char* kDisableCompactDexFlag = "--compact-dex-level=none";
50
51// Location of the JIT Zygote image.
52static const char* kJitZygoteImage =
53 "boot.art:/nonx/boot-framework.art!/system/etc/boot-image.prof";
54
55std::vector<std::string> SplitBySpaces(const std::string& str) {
56 if (str.empty()) {
57 return {};
58 }
59 return android::base::Split(str, " ");
60}
61
62} // namespace
63
64RunDex2Oat::RunDex2Oat(const char* dex2oat_bin, ExecVHelper* execv_helper)
65 : dex2oat_bin_(dex2oat_bin), execv_helper_(execv_helper) {}
66
67void RunDex2Oat::Initialize(int zip_fd,
68 int oat_fd,
69 int input_vdex_fd,
70 int output_vdex_fd,
71 int image_fd,
72 const char* input_file_name,
73 const char* output_file_name,
74 int swap_fd,
75 const char* instruction_set,
76 const char* compiler_filter,
77 bool debuggable,
78 bool post_bootcomplete,
79 bool for_restore,
80 int profile_fd,
81 const char* class_loader_context,
82 const std::string& class_loader_context_fds,
83 int target_sdk_version,
84 bool enable_hidden_api_checks,
85 bool generate_compact_dex,
86 int dex_metadata_fd,
87 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 Hsiehb3459402020-07-07 12:34:54 -070091 PrepareInputFileFlags(zip_fd, oat_fd, input_vdex_fd, output_vdex_fd, image_fd, input_file_name,
92 output_file_name, profile_fd, dex_metadata_fd, swap_fd,
93 class_loader_context, class_loader_context_fds);
Victor Hsiehc9821f12020-08-07 11:32:29 -070094
Victor Hsiehb3459402020-07-07 12:34:54 -070095 PrepareCompilerConfigFlags(input_vdex_fd, output_vdex_fd, instruction_set, compiler_filter,
96 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
142void RunDex2Oat::PrepareInputFileFlags(int zip_fd,
143 int oat_fd,
144 int input_vdex_fd,
145 int output_vdex_fd,
146 int image_fd,
147 const char* input_file_name,
148 const char* output_file_name,
149 int profile_fd,
150 int dex_metadata_fd,
151 int swap_fd,
152 const char* class_loader_context,
153 const std::string& class_loader_context_fds) {
154 std::string input_basename = Basename(input_file_name);
155 ALOGV("Running %s in=%s out=%s\n", dex2oat_bin_.c_str(), input_basename.c_str(),
156 output_file_name);
157
158 AddArg(StringPrintf("--zip-fd=%d", zip_fd));
159 AddArg(StringPrintf("--zip-location=%s", input_basename.c_str()));
160 AddArg(StringPrintf("--oat-fd=%d", oat_fd));
161 AddArg(StringPrintf("--oat-location=%s", output_file_name));
162 AddArg(StringPrintf("--input-vdex-fd=%d", input_vdex_fd));
163 AddArg(StringPrintf("--output-vdex-fd=%d", output_vdex_fd));
164
165 if (image_fd >= 0) {
166 AddArg(StringPrintf("--app-image-fd=%d", image_fd));
167 AddArg(MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s"));
168 }
169 if (dex_metadata_fd > -1) {
170 AddArg("--dm-fd=" + std::to_string(dex_metadata_fd));
171 }
172 if (profile_fd != -1) {
173 AddArg(StringPrintf("--profile-file-fd=%d", profile_fd));
174 }
175 if (swap_fd >= 0) {
176 AddArg(StringPrintf("--swap-fd=%d", swap_fd));
177 }
178
179 // Get the directory of the apk to pass as a base classpath directory.
180 {
181 std::string apk_dir(input_file_name);
182 size_t dir_index = apk_dir.rfind('/');
183 if (dir_index != std::string::npos) {
184 apk_dir = apk_dir.substr(0, dir_index);
185 AddArg(StringPrintf("--classpath-dir=%s", apk_dir.c_str()));
186 }
187 }
188
189 if (class_loader_context != nullptr) {
190 AddArg(StringPrintf("--class-loader-context=%s", class_loader_context));
191 if (!class_loader_context_fds.empty()) {
192 AddArg(StringPrintf("--class-loader-context-fds=%s",
193 class_loader_context_fds.c_str()));
194 }
195 }
196}
197
198void RunDex2Oat::PrepareCompilerConfigFlags(int input_vdex_fd,
199 int output_vdex_fd,
200 const char* instruction_set,
201 const char* compiler_filter,
202 bool debuggable,
203 int target_sdk_version,
204 bool enable_hidden_api_checks,
205 bool generate_compact_dex,
206 const char* compilation_reason) {
207 // Disable cdex if update input vdex is true since this combination of options is not
208 // supported.
209 const bool disable_cdex = !generate_compact_dex || (input_vdex_fd == output_vdex_fd);
210 if (disable_cdex) {
211 AddArg(kDisableCompactDexFlag);
212 }
213
214 // ISA related
215 {
216 AddArg(StringPrintf("--instruction-set=%s", instruction_set));
217
218 const std::string dex2oat_isa_features_key =
219 StringPrintf("dalvik.vm.isa.%s.features", instruction_set);
220 std::string instruction_set_features_arg =
221 MapPropertyToArg(dex2oat_isa_features_key, "--instruction-set-features=%s");
222 AddArg(instruction_set_features_arg);
223
224 const std::string dex2oat_isa_variant_key =
225 StringPrintf("dalvik.vm.isa.%s.variant", instruction_set);
226 std::string instruction_set_variant_arg =
227 MapPropertyToArg(dex2oat_isa_variant_key, "--instruction-set-variant=%s");
228 AddArg(instruction_set_variant_arg);
229 }
230
231 // Compute compiler filter.
232 {
233 std::string dex2oat_compiler_filter_arg;
234 {
235 // If we are booting without the real /data, don't spend time compiling.
236 std::string vold_decrypt = GetProperty("vold.decrypt", "");
237 bool skip_compilation = vold_decrypt == "trigger_restart_min_framework" ||
238 vold_decrypt == "1";
239
240 bool have_dex2oat_relocation_skip_flag = false;
241 if (skip_compilation) {
242 dex2oat_compiler_filter_arg = "--compiler-filter=extract";
243 have_dex2oat_relocation_skip_flag = true;
244 } else if (compiler_filter != nullptr) {
245 dex2oat_compiler_filter_arg = StringPrintf("--compiler-filter=%s",
246 compiler_filter);
247 }
248 if (have_dex2oat_relocation_skip_flag) {
249 AddRuntimeArg("-Xnorelocate");
250 }
251 }
252
253 if (dex2oat_compiler_filter_arg.empty()) {
254 dex2oat_compiler_filter_arg = MapPropertyToArg("dalvik.vm.dex2oat-filter",
255 "--compiler-filter=%s");
256 }
257 AddArg(dex2oat_compiler_filter_arg);
258
259 if (compilation_reason != nullptr) {
260 AddArg(std::string("--compilation-reason=") + compilation_reason);
261 }
262 }
263
264 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
265 "--max-image-block-size=%s"));
266
267 AddArg(MapPropertyToArg("dalvik.vm.dex2oat-very-large",
268 "--very-large-app-threshold=%s"));
269
270 std::string resolve_startup_string_arg = MapPropertyToArg(
271 "persist.device_config.runtime.dex2oat_resolve_startup_strings",
272 "--resolve-startup-const-strings=%s");
273 if (resolve_startup_string_arg.empty()) {
274 // If empty, fall back to system property.
275 resolve_startup_string_arg =
276 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
277 "--resolve-startup-const-strings=%s");
278 }
279 AddArg(resolve_startup_string_arg);
280
281 // Debug related
282 {
283 // Check whether all apps should be compiled debuggable.
284 if (!debuggable) {
285 debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
286 }
287 if (debuggable) {
288 AddArg("--debuggable");
289 }
290
291 const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
292 if (generate_debug_info) {
293 AddArg("--generate-debug-info");
294 }
295 {
296 bool generate_minidebug_info = kEnableMinidebugInfo &&
297 GetBoolProperty(kMinidebugInfoSystemProperty,
298 kMinidebugInfoSystemPropertyDefault);
299 if (generate_minidebug_info) {
300 AddArg(kMinidebugDex2oatFlag);
301 }
302 }
303 }
304
305 if (target_sdk_version != 0) {
306 AddRuntimeArg(StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version));
307 }
308
309 if (enable_hidden_api_checks) {
310 AddRuntimeArg("-Xhidden-api-policy:enabled");
311 }
312}
313
314void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
315 bool for_restore) {
316 // CPU set
317 {
318 std::string cpu_set_format = "--cpu-set=%s";
319 std::string dex2oat_cpu_set_arg = post_bootcomplete
320 ? (for_restore
321 ? MapPropertyToArgWithBackup(
322 "dalvik.vm.restore-dex2oat-cpu-set",
323 "dalvik.vm.dex2oat-cpu-set",
324 cpu_set_format)
325 : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format))
326 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
327 AddArg(dex2oat_cpu_set_arg);
328 }
329
330 // Number of threads
331 {
332 std::string threads_format = "-j%s";
333 std::string dex2oat_threads_arg = post_bootcomplete
334 ? (for_restore
335 ? MapPropertyToArgWithBackup(
336 "dalvik.vm.restore-dex2oat-threads",
337 "dalvik.vm.dex2oat-threads",
338 threads_format)
339 : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format))
340 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
341 AddArg(dex2oat_threads_arg);
342 }
343
344 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s"));
345 AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s"));
346}
347
Victor Hsiehc9821f12020-08-07 11:32:29 -0700348void RunDex2Oat::Exec(int exit_code) {
Victor Hsiehc9821f12020-08-07 11:32:29 -0700349 execv_helper_->Exec(exit_code);
350}
351
352void RunDex2Oat::AddArg(const std::string& arg) {
353 execv_helper_->AddArg(arg);
354}
355
356void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
357 execv_helper_->AddRuntimeArg(arg);
358}
359
360std::string RunDex2Oat::GetProperty(const std::string& key,
361 const std::string& default_value) {
362 return android::base::GetProperty(key, default_value);
363}
364
365bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
366 return android::base::GetBoolProperty(key, default_value);
367}
368
369std::string RunDex2Oat::MapPropertyToArg(const std::string& property,
370 const std::string& format,
371 const std::string& default_value) {
372 std::string prop = GetProperty(property, default_value);
373 if (!prop.empty()) {
374 return StringPrintf(format.c_str(), prop.c_str());
375 }
376 return "";
377}
378
379std::string RunDex2Oat::MapPropertyToArgWithBackup(
380 const std::string& property,
381 const std::string& backupProperty,
382 const std::string& format,
383 const std::string& default_value) {
384 std::string value = GetProperty(property, default_value);
385 if (!value.empty()) {
386 return StringPrintf(format.c_str(), value.c_str());
387 }
388 return MapPropertyToArg(backupProperty, format, default_value);
389}
390
391} // namespace installd
392} // namespace android