• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,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                             const char* compilation_reason) {
85     PrepareBootImageFlags(use_jitzygote);
86 
87     PrepareInputFileFlags(output_oat, output_vdex, output_image, input_dex, input_vdex,
88                           dex_metadata, profile, swap_fd, class_loader_context,
89                           class_loader_context_fds);
90 
91     PrepareCompilerConfigFlags(input_vdex, output_vdex, instruction_set, compiler_filter,
92                                debuggable, target_sdk_version, enable_hidden_api_checks,
93                                generate_compact_dex, compilation_reason);
94 
95     PrepareCompilerRuntimeAndPerfConfigFlags(post_bootcomplete, for_restore);
96 
97     const std::string dex2oat_flags = GetProperty("dalvik.vm.dex2oat-flags", "");
98     std::vector<std::string> dex2oat_flags_args = SplitBySpaces(dex2oat_flags);
99     ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags.c_str());
100 
101     // Do not add args after dex2oat_flags, they should override others for debugging.
102     for (auto it = dex2oat_flags_args.begin(); it != dex2oat_flags_args.end(); ++it) {
103         AddArg(*it);
104     }
105 
106     execv_helper_->PrepareArgs(dex2oat_bin_);
107 }
108 
~RunDex2Oat()109 RunDex2Oat::~RunDex2Oat() {}
110 
PrepareBootImageFlags(bool use_jitzygote)111 void RunDex2Oat::PrepareBootImageFlags(bool use_jitzygote) {
112     if (use_jitzygote) {
113         // Don't pass a boot image because JIT Zygote should decide which image to use. Typically,
114         // it does not use any boot image on disk.
115         AddArg("--force-jit-zygote");
116     } else {
117         AddArg(MapPropertyToArg("dalvik.vm.boot-image", "--boot-image=%s"));
118     }
119 }
120 
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)121 void RunDex2Oat::PrepareInputFileFlags(const UniqueFile& output_oat,
122                                        const UniqueFile& output_vdex,
123                                        const UniqueFile& output_image,
124                                        const UniqueFile& input_dex,
125                                        const UniqueFile& input_vdex,
126                                        const UniqueFile& dex_metadata,
127                                        const UniqueFile& profile,
128                                        int swap_fd,
129                                        const char* class_loader_context,
130                                        const std::string& class_loader_context_fds) {
131     std::string input_basename = Basename(input_dex.path());
132     LOG(VERBOSE) << "Running " << dex2oat_bin_ << " in=" << input_basename << " out="
133                  << output_oat.path();
134 
135     AddArg(StringPrintf("--zip-fd=%d", input_dex.fd()));
136     AddArg(StringPrintf("--zip-location=%s", input_basename.c_str()));
137     AddArg(StringPrintf("--oat-fd=%d", output_oat.fd()));
138     AddArg(StringPrintf("--oat-location=%s", output_oat.path().c_str()));
139     AddArg(StringPrintf("--input-vdex-fd=%d", input_vdex.fd()));
140     AddArg(StringPrintf("--output-vdex-fd=%d", output_vdex.fd()));
141 
142     if (output_image.fd() >= 0) {
143         AddArg(StringPrintf("--app-image-fd=%d", output_image.fd()));
144         AddArg(MapPropertyToArg("dalvik.vm.appimageformat", "--image-format=%s"));
145     }
146     if (dex_metadata.fd() > -1) {
147         AddArg("--dm-fd=" + std::to_string(dex_metadata.fd()));
148     }
149     if (profile.fd() != -1) {
150         AddArg(StringPrintf("--profile-file-fd=%d", profile.fd()));
151     }
152     if (swap_fd >= 0) {
153         AddArg(StringPrintf("--swap-fd=%d", swap_fd));
154     }
155 
156     // Get the directory of the apk to pass as a base classpath directory.
157     {
158         std::string apk_dir(input_dex.path());
159         size_t dir_index = apk_dir.rfind('/');
160         if (dir_index != std::string::npos) {
161             apk_dir = apk_dir.substr(0, dir_index);
162             AddArg(StringPrintf("--classpath-dir=%s", apk_dir.c_str()));
163         }
164     }
165 
166     if (class_loader_context != nullptr) {
167         AddArg(StringPrintf("--class-loader-context=%s", class_loader_context));
168         if (!class_loader_context_fds.empty()) {
169             AddArg(StringPrintf("--class-loader-context-fds=%s",
170                                 class_loader_context_fds.c_str()));
171         }
172     }
173 }
174 
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)175 void RunDex2Oat::PrepareCompilerConfigFlags(const UniqueFile& input_vdex,
176                                             const UniqueFile& output_vdex,
177                                             const char* instruction_set,
178                                             const char* compiler_filter,
179                                             bool debuggable,
180                                             int target_sdk_version,
181                                             bool enable_hidden_api_checks,
182                                             bool generate_compact_dex,
183                                             const char* compilation_reason) {
184     // Disable cdex if update input vdex is true since this combination of options is not
185     // supported.
186     const bool disable_cdex = !generate_compact_dex || (input_vdex.fd() == output_vdex.fd());
187     if (disable_cdex) {
188         AddArg(kDisableCompactDexFlag);
189     }
190 
191     // ISA related
192     {
193         AddArg(StringPrintf("--instruction-set=%s", instruction_set));
194 
195         const std::string dex2oat_isa_features_key =
196                 StringPrintf("dalvik.vm.isa.%s.features", instruction_set);
197         std::string instruction_set_features_arg =
198                 MapPropertyToArg(dex2oat_isa_features_key, "--instruction-set-features=%s");
199         AddArg(instruction_set_features_arg);
200 
201         const std::string dex2oat_isa_variant_key =
202                 StringPrintf("dalvik.vm.isa.%s.variant", instruction_set);
203         std::string instruction_set_variant_arg =
204                 MapPropertyToArg(dex2oat_isa_variant_key, "--instruction-set-variant=%s");
205         AddArg(instruction_set_variant_arg);
206     }
207 
208     // Compute compiler filter.
209     {
210         std::string dex2oat_compiler_filter_arg;
211         {
212             // If we are booting without the real /data, don't spend time compiling.
213             std::string vold_decrypt = GetProperty("vold.decrypt", "");
214             bool skip_compilation = vold_decrypt == "trigger_restart_min_framework" ||
215                     vold_decrypt == "1";
216 
217             bool have_dex2oat_relocation_skip_flag = false;
218             if (skip_compilation) {
219                 dex2oat_compiler_filter_arg = "--compiler-filter=extract";
220                 have_dex2oat_relocation_skip_flag = true;
221             } else if (compiler_filter != nullptr) {
222                 dex2oat_compiler_filter_arg = StringPrintf("--compiler-filter=%s",
223                                                            compiler_filter);
224             }
225             if (have_dex2oat_relocation_skip_flag) {
226                 AddRuntimeArg("-Xnorelocate");
227             }
228         }
229 
230         if (dex2oat_compiler_filter_arg.empty()) {
231             dex2oat_compiler_filter_arg = MapPropertyToArg("dalvik.vm.dex2oat-filter",
232                                                            "--compiler-filter=%s");
233         }
234         AddArg(dex2oat_compiler_filter_arg);
235 
236         if (compilation_reason != nullptr) {
237             AddArg(std::string("--compilation-reason=") + compilation_reason);
238         }
239     }
240 
241     AddArg(MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
242                             "--max-image-block-size=%s"));
243 
244     AddArg(MapPropertyToArg("dalvik.vm.dex2oat-very-large",
245                             "--very-large-app-threshold=%s"));
246 
247     std::string resolve_startup_string_arg = MapPropertyToArg(
248         "persist.device_config.runtime.dex2oat_resolve_startup_strings",
249         "--resolve-startup-const-strings=%s");
250     if (resolve_startup_string_arg.empty()) {
251         // If empty, fall back to system property.
252         resolve_startup_string_arg =
253                 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
254                                  "--resolve-startup-const-strings=%s");
255     }
256     AddArg(resolve_startup_string_arg);
257 
258     // Debug related
259     {
260         // Check whether all apps should be compiled debuggable.
261         if (!debuggable) {
262             debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
263         }
264         if (debuggable) {
265             AddArg("--debuggable");
266         }
267 
268         const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
269         if (generate_debug_info) {
270             AddArg("--generate-debug-info");
271         }
272         {
273             bool generate_minidebug_info = kEnableMinidebugInfo &&
274                     GetBoolProperty(kMinidebugInfoSystemProperty,
275                                     kMinidebugInfoSystemPropertyDefault);
276             if (generate_minidebug_info) {
277                 AddArg(kMinidebugDex2oatFlag);
278             }
279         }
280     }
281 
282     // On-device signing related. odsign sets the system property odsign.verification.success if
283     // AOT artifacts have the expected signatures.
284     const bool trust_art_apex_data_files = GetBoolProperty("odsign.verification.success", false);
285     if (!trust_art_apex_data_files) {
286         AddRuntimeArg("-Xdeny-art-apex-data-files");
287     }
288 
289     if (target_sdk_version != 0) {
290         AddRuntimeArg(StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version));
291     }
292 
293     if (enable_hidden_api_checks) {
294         AddRuntimeArg("-Xhidden-api-policy:enabled");
295     }
296 }
297 
PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,bool for_restore)298 void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
299                                                           bool for_restore) {
300     // CPU set
301     {
302         std::string cpu_set_format = "--cpu-set=%s";
303         std::string dex2oat_cpu_set_arg = post_bootcomplete
304                 ? (for_restore
305                    ? MapPropertyToArgWithBackup(
306                            "dalvik.vm.restore-dex2oat-cpu-set",
307                            "dalvik.vm.dex2oat-cpu-set",
308                            cpu_set_format)
309                    : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format))
310                 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
311         AddArg(dex2oat_cpu_set_arg);
312     }
313 
314     // Number of threads
315     {
316         std::string threads_format = "-j%s";
317         std::string dex2oat_threads_arg = post_bootcomplete
318                 ? (for_restore
319                    ? MapPropertyToArgWithBackup(
320                            "dalvik.vm.restore-dex2oat-threads",
321                            "dalvik.vm.dex2oat-threads",
322                            threads_format)
323                    : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format))
324                 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
325         AddArg(dex2oat_threads_arg);
326     }
327 
328     AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s"));
329     AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s"));
330 
331     // Enable compiling dex files in isolation on low ram devices.
332     // It takes longer but reduces the memory footprint.
333     if (GetBoolProperty("ro.config.low_ram", false)) {
334       AddArg("--compile-individually");
335     }
336 }
337 
Exec(int exit_code)338 void RunDex2Oat::Exec(int exit_code) {
339     execv_helper_->Exec(exit_code);
340 }
341 
AddArg(const std::string & arg)342 void RunDex2Oat::AddArg(const std::string& arg) {
343     execv_helper_->AddArg(arg);
344 }
345 
AddRuntimeArg(const std::string & arg)346 void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
347     execv_helper_->AddRuntimeArg(arg);
348 }
349 
GetProperty(const std::string & key,const std::string & default_value)350 std::string RunDex2Oat::GetProperty(const std::string& key,
351                                     const std::string& default_value) {
352     return android::base::GetProperty(key, default_value);
353 }
354 
GetBoolProperty(const std::string & key,bool default_value)355 bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
356     return android::base::GetBoolProperty(key, default_value);
357 }
358 
MapPropertyToArg(const std::string & property,const std::string & format,const std::string & default_value)359 std::string RunDex2Oat::MapPropertyToArg(const std::string& property,
360                                          const std::string& format,
361                                          const std::string& default_value) {
362     std::string prop = GetProperty(property, default_value);
363     if (!prop.empty()) {
364         return StringPrintf(format.c_str(), prop.c_str());
365     }
366     return "";
367 }
368 
MapPropertyToArgWithBackup(const std::string & property,const std::string & backupProperty,const std::string & format,const std::string & default_value)369 std::string RunDex2Oat::MapPropertyToArgWithBackup(
370         const std::string& property,
371         const std::string& backupProperty,
372         const std::string& format,
373         const std::string& default_value) {
374     std::string value = GetProperty(property, default_value);
375     if (!value.empty()) {
376         return StringPrintf(format.c_str(), value.c_str());
377     }
378     return MapPropertyToArg(backupProperty, format, default_value);
379 }
380 
381 }  // namespace installd
382 }  // namespace android
383