• 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,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     {
212         std::string dex2oat_compiler_filter_arg;
213         {
214             // If we are booting without the real /data, don't spend time compiling.
215             std::string vold_decrypt = GetProperty("vold.decrypt", "");
216             bool skip_compilation = vold_decrypt == "trigger_restart_min_framework" ||
217                     vold_decrypt == "1";
218 
219             bool have_dex2oat_relocation_skip_flag = false;
220             if (skip_compilation) {
221                 dex2oat_compiler_filter_arg = "--compiler-filter=extract";
222                 have_dex2oat_relocation_skip_flag = true;
223             } else if (compiler_filter != nullptr) {
224                 dex2oat_compiler_filter_arg = StringPrintf("--compiler-filter=%s",
225                                                            compiler_filter);
226             }
227             if (have_dex2oat_relocation_skip_flag) {
228                 AddRuntimeArg("-Xnorelocate");
229             }
230         }
231 
232         if (dex2oat_compiler_filter_arg.empty()) {
233             dex2oat_compiler_filter_arg = MapPropertyToArg("dalvik.vm.dex2oat-filter",
234                                                            "--compiler-filter=%s");
235         }
236         AddArg(dex2oat_compiler_filter_arg);
237 
238         if (compilation_reason != nullptr) {
239             AddArg(std::string("--compilation-reason=") + compilation_reason);
240         }
241     }
242 
243     AddArg(MapPropertyToArg("dalvik.vm.dex2oat-max-image-block-size",
244                             "--max-image-block-size=%s"));
245 
246     AddArg(MapPropertyToArg("dalvik.vm.dex2oat-very-large",
247                             "--very-large-app-threshold=%s"));
248 
249     std::string resolve_startup_string_arg = MapPropertyToArg(
250         "persist.device_config.runtime.dex2oat_resolve_startup_strings",
251         "--resolve-startup-const-strings=%s");
252     if (resolve_startup_string_arg.empty()) {
253         // If empty, fall back to system property.
254         resolve_startup_string_arg =
255                 MapPropertyToArg("dalvik.vm.dex2oat-resolve-startup-strings",
256                                  "--resolve-startup-const-strings=%s");
257     }
258     AddArg(resolve_startup_string_arg);
259 
260     // Debug related
261     {
262         // Check whether all apps should be compiled debuggable.
263         if (!debuggable) {
264             debuggable = GetProperty("dalvik.vm.always_debuggable", "") == "1";
265         }
266         if (debuggable) {
267             AddArg("--debuggable");
268         }
269 
270         const bool generate_debug_info = GetBoolProperty("debug.generate-debug-info", false);
271         if (generate_debug_info) {
272             AddArg("--generate-debug-info");
273         }
274         {
275             bool generate_minidebug_info = kEnableMinidebugInfo &&
276                     GetBoolProperty(kMinidebugInfoSystemProperty,
277                                     kMinidebugInfoSystemPropertyDefault);
278             if (generate_minidebug_info) {
279                 AddArg(kMinidebugDex2oatFlag);
280             }
281         }
282     }
283 
284     // On-device signing related. odsign sets the system property odsign.verification.success if
285     // AOT artifacts have the expected signatures.
286     const bool trust_art_apex_data_files = GetBoolProperty("odsign.verification.success", false);
287     if (!trust_art_apex_data_files) {
288         AddRuntimeArg("-Xdeny-art-apex-data-files");
289     }
290 
291     if (target_sdk_version != 0) {
292         AddRuntimeArg(StringPrintf("-Xtarget-sdk-version:%d", target_sdk_version));
293     }
294 
295     if (enable_hidden_api_checks) {
296         AddRuntimeArg("-Xhidden-api-policy:enabled");
297     }
298 }
299 
PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,bool for_restore,bool background_job_compile)300 void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
301                                                           bool for_restore,
302                                                           bool background_job_compile) {
303     // CPU set
304     {
305         std::string cpu_set_format = "--cpu-set=%s";
306         std::string dex2oat_cpu_set_arg = post_bootcomplete
307                 ? (for_restore
308                    ? MapPropertyToArgWithBackup(
309                            "dalvik.vm.restore-dex2oat-cpu-set",
310                            "dalvik.vm.dex2oat-cpu-set",
311                            cpu_set_format)
312                    : (background_job_compile
313                       ? MapPropertyToArgWithBackup(
314                               "dalvik.vm.background-dex2oat-cpu-set",
315                               "dalvik.vm.dex2oat-cpu-set",
316                               cpu_set_format)
317                       : MapPropertyToArg("dalvik.vm.dex2oat-cpu-set", cpu_set_format)))
318                 : MapPropertyToArg("dalvik.vm.boot-dex2oat-cpu-set", cpu_set_format);
319         AddArg(dex2oat_cpu_set_arg);
320     }
321 
322     // Number of threads
323     {
324         std::string threads_format = "-j%s";
325         std::string dex2oat_threads_arg = post_bootcomplete
326                 ? (for_restore
327                    ? MapPropertyToArgWithBackup(
328                            "dalvik.vm.restore-dex2oat-threads",
329                            "dalvik.vm.dex2oat-threads",
330                            threads_format)
331                    : (background_job_compile
332                       ? MapPropertyToArgWithBackup(
333                               "dalvik.vm.background-dex2oat-threads",
334                               "dalvik.vm.dex2oat-threads",
335                               threads_format)
336                       : MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format)))
337                 : MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
338         AddArg(dex2oat_threads_arg);
339     }
340 
341     AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xms", "-Xms%s"));
342     AddRuntimeArg(MapPropertyToArg("dalvik.vm.dex2oat-Xmx", "-Xmx%s"));
343 
344     // Enable compiling dex files in isolation on low ram devices.
345     // It takes longer but reduces the memory footprint.
346     if (GetBoolProperty("ro.config.low_ram", false)) {
347       AddArg("--compile-individually");
348     }
349 }
350 
Exec(int exit_code)351 void RunDex2Oat::Exec(int exit_code) {
352     execv_helper_->Exec(exit_code);
353 }
354 
AddArg(const std::string & arg)355 void RunDex2Oat::AddArg(const std::string& arg) {
356     execv_helper_->AddArg(arg);
357 }
358 
AddRuntimeArg(const std::string & arg)359 void RunDex2Oat::AddRuntimeArg(const std::string& arg) {
360     execv_helper_->AddRuntimeArg(arg);
361 }
362 
GetProperty(const std::string & key,const std::string & default_value)363 std::string RunDex2Oat::GetProperty(const std::string& key,
364                                     const std::string& default_value) {
365     return android::base::GetProperty(key, default_value);
366 }
367 
GetBoolProperty(const std::string & key,bool default_value)368 bool RunDex2Oat::GetBoolProperty(const std::string& key, bool default_value) {
369     return android::base::GetBoolProperty(key, default_value);
370 }
371 
MapPropertyToArg(const std::string & property,const std::string & format,const std::string & default_value)372 std::string RunDex2Oat::MapPropertyToArg(const std::string& property,
373                                          const std::string& format,
374                                          const std::string& default_value) {
375     std::string prop = GetProperty(property, default_value);
376     if (!prop.empty()) {
377         return StringPrintf(format.c_str(), prop.c_str());
378     }
379     return "";
380 }
381 
MapPropertyToArgWithBackup(const std::string & property,const std::string & backupProperty,const std::string & format,const std::string & default_value)382 std::string RunDex2Oat::MapPropertyToArgWithBackup(
383         const std::string& property,
384         const std::string& backupProperty,
385         const std::string& format,
386         const std::string& default_value) {
387     std::string value = GetProperty(property, default_value);
388     if (!value.empty()) {
389         return StringPrintf(format.c_str(), value.c_str());
390     }
391     return MapPropertyToArg(backupProperty, format, default_value);
392 }
393 
394 }  // namespace installd
395 }  // namespace android
396