1 /*
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
33 #include "unique_file.h"
34
35 using android::base::Basename;
36 using android::base::StringPrintf;
37
38 namespace android {
39 namespace installd {
40
41 namespace {
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.
46 static constexpr bool kEnableMinidebugInfo = true;
47
48 static constexpr const char* kMinidebugInfoSystemProperty = "dalvik.vm.dex2oat-minidebuginfo";
49 static constexpr bool kMinidebugInfoSystemPropertyDefault = false;
50 static constexpr const char* kMinidebugDex2oatFlag = "--generate-mini-debug-info";
51 static constexpr const char* kDisableCompactDexFlag = "--compact-dex-level=none";
52
SplitBySpaces(const std::string & str)53 std::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
RunDex2Oat(const char * dex2oat_bin,ExecVHelper * execv_helper)62 RunDex2Oat::RunDex2Oat(const char* dex2oat_bin, ExecVHelper* execv_helper)
63 : dex2oat_bin_(dex2oat_bin), execv_helper_(execv_helper) {}
64
Initialize(const UniqueFile & output_oat,const UniqueFile & output_vdex,const UniqueFile & output_image,const UniqueFile & input_dex,const UniqueFile & input_vdex,const UniqueFile & dex_metadata,const UniqueFile & profile,const char * class_loader_context,const std::string & class_loader_context_fds,int swap_fd,const char * instruction_set,const char * compiler_filter,bool debuggable,bool post_bootcomplete,bool for_restore,int target_sdk_version,bool enable_hidden_api_checks,bool generate_compact_dex,bool use_jitzygote,bool background_job_compile,const char * compilation_reason)65 void 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,
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 target_sdk_version,
81 bool enable_hidden_api_checks,
82 bool generate_compact_dex,
83 bool use_jitzygote,
84 bool background_job_compile,
85 const char* compilation_reason) {
86 PrepareBootImageFlags(use_jitzygote);
87
88 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);
91
92 PrepareCompilerConfigFlags(input_vdex, output_vdex, instruction_set, compiler_filter,
93 debuggable, target_sdk_version, enable_hidden_api_checks,
94 generate_compact_dex, compilation_reason);
95
96 PrepareCompilerRuntimeAndPerfConfigFlags(post_bootcomplete, for_restore,
97 background_job_compile);
98
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
103 // 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
~RunDex2Oat()111 RunDex2Oat::~RunDex2Oat() {}
112
PrepareBootImageFlags(bool use_jitzygote)113 void 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");
118 } else {
119 AddArg(MapPropertyToArg("dalvik.vm.boot-image", "--boot-image=%s"));
120 }
121 }
122
PrepareInputFileFlags(const UniqueFile & output_oat,const UniqueFile & output_vdex,const UniqueFile & output_image,const UniqueFile & input_dex,const UniqueFile & input_vdex,const UniqueFile & dex_metadata,const UniqueFile & profile,int swap_fd,const char * class_loader_context,const std::string & class_loader_context_fds)123 void 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,
130 int swap_fd,
131 const char* class_loader_context,
132 const std::string& class_loader_context_fds) {
133 std::string input_basename = Basename(input_dex.path());
134 LOG(VERBOSE) << "Running " << dex2oat_bin_ << " in=" << input_basename << " out="
135 << output_oat.path();
136
137 AddArg(StringPrintf("--zip-fd=%d", input_dex.fd()));
138 AddArg(StringPrintf("--zip-location=%s", input_basename.c_str()));
139 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()));
143
144 if (output_image.fd() >= 0) {
145 AddArg(StringPrintf("--app-image-fd=%d", output_image.fd()));
146 AddArg(MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s"));
147 }
148 if (dex_metadata.fd() > -1) {
149 AddArg("--dm-fd=" + std::to_string(dex_metadata.fd()));
150 }
151 if (profile.fd() != -1) {
152 AddArg(StringPrintf("--profile-file-fd=%d", profile.fd()));
153 }
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 {
160 std::string apk_dir(input_dex.path());
161 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
PrepareCompilerConfigFlags(const UniqueFile & input_vdex,const UniqueFile & output_vdex,const char * instruction_set,const char * compiler_filter,bool debuggable,int target_sdk_version,bool enable_hidden_api_checks,bool generate_compact_dex,const char * compilation_reason)177 void RunDex2Oat::PrepareCompilerConfigFlags(const UniqueFile& input_vdex,
178 const UniqueFile& output_vdex,
179 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.
188 const bool disable_cdex = !generate_compact_dex || (input_vdex.fd() == output_vdex.fd());
189 if (disable_cdex) {
190 AddArg(kDisableCompactDexFlag);
191 }
192
193 // 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.
211 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);
218 }
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
261 // 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
268 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
PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,bool for_restore,bool background_job_compile)277 void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
278 bool for_restore,
279 bool background_job_compile) {
280 // 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)
289 : (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)))
295 : 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)
308 : (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)))
314 : 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"));
320
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 }
326 }
327
Exec(int exit_code)328 void RunDex2Oat::Exec(int exit_code) {
329 execv_helper_->Exec(exit_code);
330 }
331
AddArg(const std::string & arg)332 void RunDex2Oat::AddArg(const std::string& arg) {
333 execv_helper_->AddArg(arg);
334 }
335
AddRuntimeArg(const std::string & arg)336 void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
337 execv_helper_->AddRuntimeArg(arg);
338 }
339
GetProperty(const std::string & key,const std::string & default_value)340 std::string RunDex2Oat::GetProperty(const std::string& key,
341 const std::string& default_value) {
342 return android::base::GetProperty(key, default_value);
343 }
344
GetBoolProperty(const std::string & key,bool default_value)345 bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
346 return android::base::GetBoolProperty(key, default_value);
347 }
348
MapPropertyToArg(const std::string & property,const std::string & format,const std::string & default_value)349 std::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
MapPropertyToArgWithBackup(const std::string & property,const std::string & backupProperty,const std::string & format,const std::string & default_value)359 std::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
373