Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Jiakai Zhang | fb8cdb5 | 2021-12-06 18:51:28 +0000 | [diff] [blame] | 18 | #include <log/log.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/stat.h> |
| 22 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 23 | #include <forward_list> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include <fstream> |
| 25 | #include <iostream> |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 26 | #include <limits> |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 27 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include <sstream> |
| 29 | #include <string> |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 30 | #include <type_traits> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 33 | #if defined(__linux__) |
| 34 | #include <sched.h> |
| 35 | #if defined(__arm__) |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 36 | #include <sys/personality.h> |
| 37 | #include <sys/utsname.h> |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 38 | #endif // __arm__ |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 39 | #endif |
| 40 | |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 41 | #include "android-base/parseint.h" |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 42 | #include "android-base/scopeguard.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 43 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 44 | #include "android-base/strings.h" |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 45 | #include "android-base/unique_fd.h" |
Vladimir Marko | d1f7351 | 2020-04-02 10:50:35 +0100 | [diff] [blame] | 46 | #include "aot_class_linker.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 47 | #include "arch/instruction_set_features.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 48 | #include "art_method-inl.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 49 | #include "base/callee_save_type.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 50 | #include "base/dumpable.h" |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 51 | #include "base/fast_exit.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 52 | #include "base/file_utils.h" |
Jiakai Zhang | fb8cdb5 | 2021-12-06 18:51:28 +0000 | [diff] [blame] | 53 | #include "base/globals.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 54 | #include "base/leb128.h" |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 55 | #include "base/macros.h" |
Jiakai Zhang | fb8cdb5 | 2021-12-06 18:51:28 +0000 | [diff] [blame] | 56 | #include "base/memory_tool.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 57 | #include "base/mutex.h" |
| 58 | #include "base/os.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 59 | #include "base/scoped_flock.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 60 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 61 | #include "base/time_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 62 | #include "base/timing_logger.h" |
| 63 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 64 | #include "base/utils.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 65 | #include "base/zip_archive.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 66 | #include "class_linker.h" |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 67 | #include "class_loader_context.h" |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 68 | #include "cmdline_parser.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 69 | #include "compiler.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 70 | #include "compiler_callbacks.h" |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 71 | #include "debug/elf_debug_writer.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 72 | #include "debug/method_debug_info.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 73 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 74 | #include "dex/dex_file-inl.h" |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 75 | #include "dex/dex_file_loader.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 76 | #include "dex/quick_compiler_callbacks.h" |
| 77 | #include "dex/verification_results.h" |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 78 | #include "dex2oat_options.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 79 | #include "dexlayout.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 80 | #include "driver/compiler_driver.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 81 | #include "driver/compiler_options.h" |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 82 | #include "driver/compiler_options_map-inl.h" |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 83 | #include "elf_file.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 84 | #include "gc/space/image_space.h" |
| 85 | #include "gc/space/space-inl.h" |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 86 | #include "gc/verification.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 87 | #include "interpreter/unstarted_runtime.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 88 | #include "jni/java_vm_ext.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 89 | #include "linker/elf_writer.h" |
| 90 | #include "linker/elf_writer_quick.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 91 | #include "linker/image_writer.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 92 | #include "linker/multi_oat_relative_patcher.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 93 | #include "linker/oat_writer.h" |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 94 | #include "mirror/class-alloc-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 95 | #include "mirror/class_loader.h" |
| 96 | #include "mirror/object-inl.h" |
| 97 | #include "mirror/object_array-inl.h" |
Vladimir Marko | d3d00c0 | 2019-11-07 15:09:07 +0000 | [diff] [blame] | 98 | #include "oat.h" |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 99 | #include "oat_file.h" |
Jeff Hao | b11ffb7 | 2016-04-07 15:40:54 -0700 | [diff] [blame] | 100 | #include "oat_file_assistant.h" |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 101 | #include "palette/palette.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 102 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | #include "runtime.h" |
Nicolas Geoffray | 48eb839 | 2022-02-24 16:20:18 +0000 | [diff] [blame] | 104 | #include "runtime_intrinsics.h" |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 105 | #include "runtime_options.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 106 | #include "scoped_thread_state_change-inl.h" |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 107 | #include "stream/buffered_output_stream.h" |
| 108 | #include "stream/file_output_stream.h" |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 109 | #include "vdex_file.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 110 | #include "verifier/verifier_deps.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 111 | #include "well_known_classes.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 112 | |
| 113 | namespace art { |
| 114 | |
Nicolas Geoffray | 2cee213 | 2020-08-24 15:31:52 +0100 | [diff] [blame] | 115 | namespace dex2oat { |
| 116 | enum class ReturnCode : int { |
| 117 | kNoFailure = 0, // No failure, execution completed successfully. |
| 118 | kOther = 1, // Some other not closer specified error occurred. |
| 119 | kCreateRuntime = 2, // Dex2oat failed creating a runtime. |
| 120 | }; |
| 121 | } // namespace dex2oat |
| 122 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 123 | using android::base::StringAppendV; |
| 124 | using android::base::StringPrintf; |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 125 | using gc::space::ImageSpace; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 126 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 127 | static constexpr size_t kDefaultMinDexFilesForSwap = 2; |
| 128 | static constexpr size_t kDefaultMinDexFileCumulativeSizeForSwap = 20 * MB; |
| 129 | |
Mathieu Chartier | 010126f | 2017-07-15 15:44:18 -0700 | [diff] [blame] | 130 | // Compiler filter override for very large apps. |
| 131 | static constexpr CompilerFilter::Filter kLargeAppFilter = CompilerFilter::kVerify; |
| 132 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 133 | static int original_argc; |
| 134 | static char** original_argv; |
| 135 | |
| 136 | static std::string CommandLine() { |
| 137 | std::vector<std::string> command; |
Andreas Gampe | 2a487eb | 2018-11-19 11:41:22 -0800 | [diff] [blame] | 138 | command.reserve(original_argc); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 139 | for (int i = 0; i < original_argc; ++i) { |
| 140 | command.push_back(original_argv[i]); |
| 141 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 142 | return android::base::Join(command, ' '); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 145 | // A stripped version. Remove some less essential parameters. If we see a "--zip-fd=" parameter, be |
| 146 | // even more aggressive. There won't be much reasonable data here for us in that case anyways (the |
| 147 | // locations are all staged). |
| 148 | static std::string StrippedCommandLine() { |
| 149 | std::vector<std::string> command; |
| 150 | |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 151 | // Do a pre-pass to look for zip-fd and the compiler filter. |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 152 | bool saw_zip_fd = false; |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 153 | bool saw_compiler_filter = false; |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 154 | for (int i = 0; i < original_argc; ++i) { |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 155 | if (android::base::StartsWith(original_argv[i], "--zip-fd=")) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 156 | saw_zip_fd = true; |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 157 | } |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 158 | if (android::base::StartsWith(original_argv[i], "--compiler-filter=")) { |
| 159 | saw_compiler_filter = true; |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 160 | } |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Now filter out things. |
| 164 | for (int i = 0; i < original_argc; ++i) { |
| 165 | // All runtime-arg parameters are dropped. |
| 166 | if (strcmp(original_argv[i], "--runtime-arg") == 0) { |
| 167 | i++; // Drop the next part, too. |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | // Any instruction-setXXX is dropped. |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 172 | if (android::base::StartsWith(original_argv[i], "--instruction-set")) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 173 | continue; |
| 174 | } |
| 175 | |
| 176 | // The boot image is dropped. |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 177 | if (android::base::StartsWith(original_argv[i], "--boot-image=")) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 178 | continue; |
| 179 | } |
| 180 | |
Mathieu Chartier | 97590cc | 2016-02-03 15:50:29 -0800 | [diff] [blame] | 181 | // The image format is dropped. |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 182 | if (android::base::StartsWith(original_argv[i], "--image-format=")) { |
Mathieu Chartier | 97590cc | 2016-02-03 15:50:29 -0800 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 186 | // This should leave any dex-file and oat-file options, describing what we compiled. |
| 187 | |
| 188 | // However, we prefer to drop this when we saw --zip-fd. |
| 189 | if (saw_zip_fd) { |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 190 | // Drop anything --zip-X, --dex-X, --oat-X, --swap-X, or --app-image-X |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 191 | if (android::base::StartsWith(original_argv[i], "--zip-") || |
| 192 | android::base::StartsWith(original_argv[i], "--dex-") || |
| 193 | android::base::StartsWith(original_argv[i], "--oat-") || |
| 194 | android::base::StartsWith(original_argv[i], "--swap-") || |
| 195 | android::base::StartsWith(original_argv[i], "--app-image-")) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 196 | continue; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | command.push_back(original_argv[i]); |
| 201 | } |
| 202 | |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 203 | if (!saw_compiler_filter) { |
| 204 | command.push_back("--compiler-filter=" + |
| 205 | CompilerFilter::NameOfFilter(CompilerFilter::kDefaultCompilerFilter)); |
| 206 | } |
| 207 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 208 | // Construct the final output. |
| 209 | if (command.size() <= 1U) { |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 210 | // It seems only "/apex/com.android.art/bin/dex2oat" is left, or not |
Roland Levillain | 38a938e | 2018-09-21 10:55:51 +0100 | [diff] [blame] | 211 | // even that. Use a pretty line. |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 212 | return "Starting dex2oat."; |
| 213 | } |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 214 | return android::base::Join(command, ' '); |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 217 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 218 | std::string error; |
| 219 | StringAppendV(&error, fmt, ap); |
| 220 | LOG(ERROR) << error; |
| 221 | } |
| 222 | |
| 223 | static void UsageError(const char* fmt, ...) { |
| 224 | va_list ap; |
| 225 | va_start(ap, fmt); |
| 226 | UsageErrorV(fmt, ap); |
| 227 | va_end(ap); |
| 228 | } |
| 229 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 230 | NO_RETURN static void Usage(const char* fmt, ...) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 231 | va_list ap; |
| 232 | va_start(ap, fmt); |
| 233 | UsageErrorV(fmt, ap); |
| 234 | va_end(ap); |
| 235 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 236 | UsageError("Command: %s", CommandLine().c_str()); |
| 237 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 238 | UsageError("Usage: dex2oat [options]..."); |
| 239 | UsageError(""); |
Alex Light | 0d47a82 | 2020-08-25 09:16:34 -0700 | [diff] [blame] | 240 | |
| 241 | std::stringstream oss; |
| 242 | VariableIndentationOutputStream vios(&oss); |
| 243 | auto parser = CreateDex2oatArgumentParser(); |
| 244 | parser.DumpHelp(vios); |
| 245 | UsageError(oss.str().c_str()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 246 | std::cerr << "See log for usage error information\n"; |
| 247 | exit(EXIT_FAILURE); |
| 248 | } |
| 249 | |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 250 | |
| 251 | // Set CPU affinity from a string containing a comma-separated list of numeric CPU identifiers. |
| 252 | static void SetCpuAffinity(const std::vector<int32_t>& cpu_list) { |
| 253 | #ifdef __linux__ |
| 254 | int cpu_count = sysconf(_SC_NPROCESSORS_CONF); |
| 255 | cpu_set_t target_cpu_set; |
| 256 | CPU_ZERO(&target_cpu_set); |
| 257 | |
| 258 | for (int32_t cpu : cpu_list) { |
| 259 | if (cpu >= 0 && cpu < cpu_count) { |
| 260 | CPU_SET(cpu, &target_cpu_set); |
| 261 | } else { |
| 262 | // Argument error is considered fatal, suggests misconfigured system properties. |
| 263 | Usage("Invalid cpu \"d\" specified in --cpu-set argument (nprocessors = %d)", |
| 264 | cpu, cpu_count); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (sched_setaffinity(getpid(), sizeof(target_cpu_set), &target_cpu_set) == -1) { |
| 269 | // Failure to set affinity may be outside control of requestor, log warning rather than |
| 270 | // treating as fatal. |
| 271 | PLOG(WARNING) << "Failed to set CPU affinity."; |
| 272 | } |
| 273 | #else |
| 274 | LOG(WARNING) << "--cpu-set not supported on this platform."; |
| 275 | #endif // __linux__ |
| 276 | } |
| 277 | |
| 278 | |
| 279 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 280 | // The primary goal of the watchdog is to prevent stuck build servers |
| 281 | // during development when fatal aborts lead to a cascade of failures |
| 282 | // that result in a deadlock. |
| 283 | class WatchDog { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 284 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 285 | #undef CHECK_PTHREAD_CALL |
| 286 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 287 | do { \ |
| 288 | int rc = call args; \ |
| 289 | if (rc != 0) { \ |
| 290 | errno = rc; \ |
| 291 | std::string message(# call); \ |
| 292 | message += " failed for "; \ |
| 293 | message += reason; \ |
| 294 | Fatal(message); \ |
| 295 | } \ |
| 296 | } while (false) |
| 297 | |
| 298 | public: |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 299 | explicit WatchDog(int64_t timeout_in_milliseconds) |
Andreas Gampe | 0f54b0d | 2017-12-27 08:37:02 -0800 | [diff] [blame] | 300 | : timeout_in_milliseconds_(timeout_in_milliseconds), |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 301 | shutting_down_(false) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 302 | const char* reason = "dex2oat watch dog thread startup"; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 303 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason); |
Mathieu Chartier | dbf4440 | 2017-02-22 16:07:23 -0800 | [diff] [blame] | 304 | #ifndef __APPLE__ |
| 305 | pthread_condattr_t condattr; |
liulvping | 8f7101e | 2017-01-16 15:14:43 +0800 | [diff] [blame] | 306 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_condattr_init, (&condattr), reason); |
| 307 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_condattr_setclock, (&condattr, CLOCK_MONOTONIC), reason); |
| 308 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, &condattr), reason); |
| 309 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_condattr_destroy, (&condattr), reason); |
Mathieu Chartier | dbf4440 | 2017-02-22 16:07:23 -0800 | [diff] [blame] | 310 | #endif |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 311 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 312 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 313 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 314 | } |
| 315 | ~WatchDog() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 316 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 317 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 318 | shutting_down_ = true; |
| 319 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 320 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 321 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 322 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 323 | |
| 324 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 325 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 326 | } |
| 327 | |
Koji Fukui | a72ca6c | 2019-03-25 17:39:07 +0900 | [diff] [blame] | 328 | static void SetRuntime(Runtime* runtime) { |
| 329 | const char* reason = "dex2oat watch dog set runtime"; |
| 330 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&runtime_mutex_), reason); |
| 331 | runtime_ = runtime; |
| 332 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&runtime_mutex_), reason); |
| 333 | } |
| 334 | |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 335 | // TODO: tune the multiplier for GC verification, the following is just to make the timeout |
| 336 | // large. |
| 337 | static constexpr int64_t kWatchdogVerifyMultiplier = |
| 338 | kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1; |
| 339 | |
| 340 | // When setting timeouts, keep in mind that the build server may not be as fast as your |
| 341 | // desktop. Debug builds are slower so they have larger timeouts. |
| 342 | static constexpr int64_t kWatchdogSlowdownFactor = kIsDebugBuild ? 5U : 1U; |
| 343 | |
| 344 | // 9.5 minutes scaled by kSlowdownFactor. This is slightly smaller than the Package Manager |
| 345 | // watchdog (PackageManagerService.WATCHDOG_TIMEOUT, 10 minutes), so that dex2oat will abort |
| 346 | // itself before that watchdog would take down the system server. |
| 347 | static constexpr int64_t kWatchDogTimeoutSeconds = kWatchdogSlowdownFactor * (9 * 60 + 30); |
| 348 | |
| 349 | static constexpr int64_t kDefaultWatchdogTimeoutInMS = |
| 350 | kWatchdogVerifyMultiplier * kWatchDogTimeoutSeconds * 1000; |
| 351 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 352 | private: |
| 353 | static void* CallBack(void* arg) { |
| 354 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 355 | ::art::SetThreadName("dex2oat watch dog"); |
| 356 | self->Wait(); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 357 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 360 | NO_RETURN static void Fatal(const std::string& message) { |
Andreas Gampe | d687e37 | 2015-04-28 23:16:03 -0700 | [diff] [blame] | 361 | // TODO: When we can guarantee it won't prevent shutdown in error cases, move to LOG. However, |
| 362 | // it's rather easy to hang in unwinding. |
| 363 | // LogLine also avoids ART logging lock issues, as it's really only a wrapper around |
| 364 | // logcat logging or stderr output. |
Tomasz Wasilczyk | 8d84090 | 2017-12-20 08:19:53 -0800 | [diff] [blame] | 365 | LogHelper::LogLineLowStack(__FILE__, __LINE__, LogSeverity::FATAL, message.c_str()); |
| 366 | |
Andreas Gampe | 48fde37 | 2017-06-06 07:43:46 -0700 | [diff] [blame] | 367 | // If we're on the host, try to dump all threads to get a sense of what's going on. This is |
| 368 | // restricted to the host as the dump may itself go bad. |
| 369 | // TODO: Use a double watchdog timeout, so we can enable this on-device. |
Koji Fukui | a72ca6c | 2019-03-25 17:39:07 +0900 | [diff] [blame] | 370 | Runtime* runtime = GetRuntime(); |
| 371 | if (!kIsTargetBuild && runtime != nullptr) { |
| 372 | runtime->AttachCurrentThread("Watchdog thread attached for dumping", |
| 373 | true, |
| 374 | nullptr, |
| 375 | false); |
| 376 | runtime->DumpForSigQuit(std::cerr); |
Andreas Gampe | cd60731 | 2017-06-05 17:03:05 -0700 | [diff] [blame] | 377 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 378 | exit(1); |
| 379 | } |
| 380 | |
| 381 | void Wait() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 382 | timespec timeout_ts; |
Mathieu Chartier | dbf4440 | 2017-02-22 16:07:23 -0800 | [diff] [blame] | 383 | #if defined(__APPLE__) |
| 384 | InitTimeSpec(true, CLOCK_REALTIME, timeout_in_milliseconds_, 0, &timeout_ts); |
| 385 | #else |
liulvping | 8f7101e | 2017-01-16 15:14:43 +0800 | [diff] [blame] | 386 | InitTimeSpec(true, CLOCK_MONOTONIC, timeout_in_milliseconds_, 0, &timeout_ts); |
Mathieu Chartier | dbf4440 | 2017-02-22 16:07:23 -0800 | [diff] [blame] | 387 | #endif |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 388 | const char* reason = "dex2oat watch dog thread waiting"; |
| 389 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 390 | while (!shutting_down_) { |
Josh Gao | 2d899c4 | 2018-10-17 16:03:42 -0700 | [diff] [blame] | 391 | int rc = pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts); |
| 392 | if (rc == EINTR) { |
| 393 | continue; |
| 394 | } else if (rc == ETIMEDOUT) { |
Roland Levillain | 60abdd9 | 2021-10-28 17:53:38 +0100 | [diff] [blame] | 395 | Fatal(StringPrintf("dex2oat did not finish after %" PRId64 " milliseconds", |
| 396 | timeout_in_milliseconds_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 397 | } else if (rc != 0) { |
Roland Levillain | d6f8c8b | 2018-12-18 15:27:28 +0000 | [diff] [blame] | 398 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", strerror(rc))); |
Greg Kaiser | f276f95 | 2019-03-26 08:15:44 -0700 | [diff] [blame] | 399 | Fatal(message); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 403 | } |
| 404 | |
Koji Fukui | a72ca6c | 2019-03-25 17:39:07 +0900 | [diff] [blame] | 405 | static Runtime* GetRuntime() { |
| 406 | const char* reason = "dex2oat watch dog get runtime"; |
| 407 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&runtime_mutex_), reason); |
| 408 | Runtime* runtime = runtime_; |
| 409 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&runtime_mutex_), reason); |
| 410 | return runtime; |
| 411 | } |
| 412 | |
| 413 | static pthread_mutex_t runtime_mutex_; |
| 414 | static Runtime* runtime_; |
| 415 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 416 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. |
| 417 | pthread_mutex_t mutex_; |
| 418 | pthread_cond_t cond_; |
| 419 | pthread_attr_t attr_; |
| 420 | pthread_t pthread_; |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame] | 421 | |
| 422 | const int64_t timeout_in_milliseconds_; |
| 423 | bool shutting_down_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 424 | }; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 425 | |
Koji Fukui | a72ca6c | 2019-03-25 17:39:07 +0900 | [diff] [blame] | 426 | pthread_mutex_t WatchDog::runtime_mutex_ = PTHREAD_MUTEX_INITIALIZER; |
| 427 | Runtime* WatchDog::runtime_ = nullptr; |
| 428 | |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 429 | // Helper class for overriding `java.lang.ThreadLocal.nextHashCode`. |
| 430 | // |
| 431 | // The class ThreadLocal has a static field nextHashCode used for assigning hash codes to |
| 432 | // new ThreadLocal objects. Since the class and the object referenced by the field are |
| 433 | // in the boot image, they cannot be modified under normal rules for AOT compilation. |
| 434 | // However, since this is a private detail that's used only for assigning hash codes and |
| 435 | // everything should work fine with different hash codes, we override the field for the |
| 436 | // compilation, providing another object that the AOT class initialization can modify. |
| 437 | class ThreadLocalHashOverride { |
| 438 | public: |
| 439 | ThreadLocalHashOverride(bool apply, int32_t initial_value) { |
| 440 | Thread* self = Thread::Current(); |
| 441 | ScopedObjectAccess soa(self); |
| 442 | hs_.emplace(self); // While holding the mutator lock. |
| 443 | Runtime* runtime = Runtime::Current(); |
| 444 | klass_ = hs_->NewHandle(apply |
| 445 | ? runtime->GetClassLinker()->LookupClass(self, |
| 446 | "Ljava/lang/ThreadLocal;", |
| 447 | /*class_loader=*/ nullptr) |
| 448 | : nullptr); |
Vladimir Marko | 7bbbe25 | 2020-03-20 12:14:55 +0000 | [diff] [blame] | 449 | field_ = ((klass_ != nullptr) && klass_->IsVisiblyInitialized()) |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 450 | ? klass_->FindDeclaredStaticField("nextHashCode", |
| 451 | "Ljava/util/concurrent/atomic/AtomicInteger;") |
| 452 | : nullptr; |
| 453 | old_field_value_ = |
| 454 | hs_->NewHandle(field_ != nullptr ? field_->GetObject(klass_.Get()) : nullptr); |
| 455 | if (old_field_value_ != nullptr) { |
| 456 | gc::AllocatorType allocator_type = runtime->GetHeap()->GetCurrentAllocator(); |
| 457 | StackHandleScope<1u> hs2(self); |
| 458 | Handle<mirror::Object> new_field_value = hs2.NewHandle( |
| 459 | old_field_value_->GetClass()->Alloc(self, allocator_type)); |
| 460 | PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); |
| 461 | ArtMethod* constructor = old_field_value_->GetClass()->FindConstructor("(I)V", pointer_size); |
| 462 | CHECK(constructor != nullptr); |
| 463 | uint32_t args[] = { |
| 464 | reinterpret_cast32<uint32_t>(new_field_value.Get()), |
| 465 | static_cast<uint32_t>(initial_value) |
| 466 | }; |
| 467 | JValue result; |
| 468 | constructor->Invoke(self, args, sizeof(args), &result, /*shorty=*/ "VI"); |
| 469 | CHECK(!self->IsExceptionPending()); |
| 470 | field_->SetObject</*kTransactionActive=*/ false>(klass_.Get(), new_field_value.Get()); |
| 471 | } |
| 472 | if (apply && old_field_value_ == nullptr) { |
Vladimir Marko | 7bbbe25 | 2020-03-20 12:14:55 +0000 | [diff] [blame] | 473 | if ((klass_ != nullptr) && klass_->IsVisiblyInitialized()) { |
| 474 | // This would mean that the implementation of ThreadLocal has changed |
| 475 | // and the code above is no longer applicable. |
| 476 | LOG(ERROR) << "Failed to override ThreadLocal.nextHashCode"; |
| 477 | } else { |
| 478 | VLOG(compiler) << "ThreadLocal is not initialized in the primary boot image."; |
| 479 | } |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | ~ThreadLocalHashOverride() { |
| 484 | ScopedObjectAccess soa(hs_->Self()); |
| 485 | if (old_field_value_ != nullptr) { |
| 486 | // Allow the overriding object to be collected. |
| 487 | field_->SetObject</*kTransactionActive=*/ false>(klass_.Get(), old_field_value_.Get()); |
| 488 | } |
| 489 | hs_.reset(); // While holding the mutator lock. |
| 490 | } |
| 491 | |
| 492 | private: |
| 493 | std::optional<StackHandleScope<2u>> hs_; |
| 494 | Handle<mirror::Class> klass_; |
| 495 | ArtField* field_; |
| 496 | Handle<mirror::Object> old_field_value_; |
| 497 | }; |
| 498 | |
Eric Holk | 94a810b | 2020-05-06 14:33:16 -0700 | [diff] [blame] | 499 | class OatKeyValueStore : public SafeMap<std::string, std::string> { |
| 500 | public: |
| 501 | using SafeMap::Put; |
| 502 | |
| 503 | iterator Put(const std::string& k, bool v) { |
| 504 | return SafeMap::Put(k, v ? OatHeader::kTrueValue : OatHeader::kFalseValue); |
| 505 | } |
| 506 | }; |
| 507 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 508 | class Dex2Oat final { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 509 | public: |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 510 | explicit Dex2Oat(TimingLogger* timings) |
| 511 | : compiler_kind_(Compiler::kOptimizing), |
| 512 | // Take the default set of instruction features from the build. |
| 513 | key_value_store_(nullptr), |
| 514 | verification_results_(nullptr), |
| 515 | runtime_(nullptr), |
| 516 | thread_count_(sysconf(_SC_NPROCESSORS_CONF)), |
| 517 | start_ns_(NanoTime()), |
| 518 | start_cputime_ns_(ProcessCpuNanoTime()), |
| 519 | strip_(false), |
| 520 | oat_fd_(-1), |
| 521 | input_vdex_fd_(-1), |
| 522 | output_vdex_fd_(-1), |
| 523 | input_vdex_file_(nullptr), |
| 524 | dm_fd_(-1), |
| 525 | zip_fd_(-1), |
| 526 | image_fd_(-1), |
| 527 | have_multi_image_arg_(false), |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 528 | image_base_(0U), |
| 529 | image_storage_mode_(ImageHeader::kStorageModeUncompressed), |
| 530 | passes_to_run_filename_(nullptr), |
| 531 | dirty_image_objects_filename_(nullptr), |
| 532 | dirty_image_objects_fd_(-1), |
| 533 | is_host_(false), |
| 534 | elf_writers_(), |
| 535 | oat_writers_(), |
| 536 | rodata_(), |
| 537 | image_writer_(nullptr), |
| 538 | driver_(nullptr), |
| 539 | opened_dex_files_maps_(), |
| 540 | opened_dex_files_(), |
| 541 | avoid_storing_invocation_(false), |
| 542 | swap_fd_(File::kInvalidFd), |
| 543 | app_image_fd_(File::kInvalidFd), |
| 544 | timings_(timings), |
| 545 | force_determinism_(false), |
| 546 | check_linkage_conditions_(false), |
| 547 | crash_on_linkage_violation_(false), |
| 548 | compile_individually_(false), |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 549 | profile_load_attempted_(false), |
| 550 | should_report_dex2oat_compilation_(false) {} |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 551 | |
| 552 | ~Dex2Oat() { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 553 | // Log completion time before deleting the runtime_, because this accesses |
| 554 | // the runtime. |
| 555 | LogCompletionTime(); |
| 556 | |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 557 | if (!kIsDebugBuild && !(kRunningOnMemoryTool && kMemoryToolDetectsLeaks)) { |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 558 | // We want to just exit on non-debug builds, not bringing the runtime down |
| 559 | // in an orderly fashion. So release the following fields. |
Santiago Aboy Solanes | 79b74d7 | 2021-10-12 13:17:46 +0100 | [diff] [blame] | 560 | if (!compiler_options_->GetDumpStats()) { |
| 561 | // The --dump-stats get logged when the optimizing compiler gets destroyed, so we can't |
| 562 | // release the driver_. |
| 563 | driver_.release(); // NOLINT |
| 564 | } |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 565 | image_writer_.release(); // NOLINT |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 566 | for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files_) { |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 567 | dex_file.release(); // NOLINT |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 568 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 569 | new std::vector<MemMap>(std::move(opened_dex_files_maps_)); // Leak MemMaps. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 570 | for (std::unique_ptr<File>& vdex_file : vdex_files_) { |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 571 | vdex_file.release(); // NOLINT |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 572 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 573 | for (std::unique_ptr<File>& oat_file : oat_files_) { |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 574 | oat_file.release(); // NOLINT |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 575 | } |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 576 | runtime_.release(); // NOLINT |
| 577 | verification_results_.release(); // NOLINT |
| 578 | key_value_store_.release(); // NOLINT |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 579 | } |
Alex Light | 4531afd | 2021-03-30 17:13:30 -0700 | [diff] [blame] | 580 | |
| 581 | // Remind the user if they passed testing only flags. |
| 582 | if (!kIsTargetBuild && force_allow_oj_inlines_) { |
| 583 | LOG(ERROR) << "Inlines allowed from core-oj! FOR TESTING USE ONLY! DO NOT DISTRIBUTE" |
| 584 | << " BINARIES BUILT WITH THIS OPTION!"; |
| 585 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 588 | struct ParserOptions { |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 589 | std::vector<std::string> oat_symbols; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 590 | std::string boot_image_filename; |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 591 | int64_t watch_dog_timeout_in_ms = -1; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 592 | bool watch_dog_enabled = true; |
Nicolas Geoffray | 1412dfa | 2015-03-20 14:48:13 +0000 | [diff] [blame] | 593 | bool requested_specific_compiler = false; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 594 | std::string error_msg; |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 595 | }; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 596 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 597 | void ParseBase(const std::string& option) { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 598 | char* end; |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 599 | image_base_ = strtoul(option.c_str(), &end, 16); |
| 600 | if (end == option.c_str() || *end != '\0') { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 601 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
| 602 | } |
| 603 | } |
| 604 | |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 605 | bool VerifyProfileData() { |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 606 | return profile_compilation_info_->VerifyProfileData(compiler_options_->dex_files_for_oat_file_); |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 609 | void ParseInstructionSetVariant(const std::string& option, ParserOptions* parser_options) { |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 610 | compiler_options_->instruction_set_features_ = InstructionSetFeatures::FromVariant( |
| 611 | compiler_options_->instruction_set_, option, &parser_options->error_msg); |
| 612 | if (compiler_options_->instruction_set_features_ == nullptr) { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 613 | Usage("%s", parser_options->error_msg.c_str()); |
| 614 | } |
| 615 | } |
| 616 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 617 | void ParseInstructionSetFeatures(const std::string& option, ParserOptions* parser_options) { |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 618 | if (compiler_options_->instruction_set_features_ == nullptr) { |
| 619 | compiler_options_->instruction_set_features_ = InstructionSetFeatures::FromVariant( |
| 620 | compiler_options_->instruction_set_, "default", &parser_options->error_msg); |
| 621 | if (compiler_options_->instruction_set_features_ == nullptr) { |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 622 | Usage("Problem initializing default instruction set features variant: %s", |
| 623 | parser_options->error_msg.c_str()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 624 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 625 | } |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 626 | compiler_options_->instruction_set_features_ = |
| 627 | compiler_options_->instruction_set_features_->AddFeaturesFromString( |
| 628 | option, &parser_options->error_msg); |
| 629 | if (compiler_options_->instruction_set_features_ == nullptr) { |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 630 | Usage("Error parsing '%s': %s", option.c_str(), parser_options->error_msg.c_str()); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 634 | void ProcessOptions(ParserOptions* parser_options) { |
Vladimir Marko | 695348f | 2020-05-19 14:42:02 +0100 | [diff] [blame] | 635 | compiler_options_->compiler_type_ = CompilerOptions::CompilerType::kAotCompiler; |
Vladimir Marko | a2da9b9 | 2018-10-10 14:21:55 +0100 | [diff] [blame] | 636 | compiler_options_->compile_pic_ = true; // All AOT compilation is PIC. |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 637 | |
| 638 | if (android_root_.empty()) { |
| 639 | const char* android_root_env_var = getenv("ANDROID_ROOT"); |
| 640 | if (android_root_env_var == nullptr) { |
| 641 | Usage("--android-root unspecified and ANDROID_ROOT not set"); |
| 642 | } |
| 643 | android_root_ += android_root_env_var; |
| 644 | } |
| 645 | |
| 646 | if (!parser_options->boot_image_filename.empty()) { |
| 647 | boot_image_filename_ = parser_options->boot_image_filename; |
| 648 | } |
| 649 | |
Vladimir Marko | 9c4b970 | 2018-11-14 15:09:02 +0000 | [diff] [blame] | 650 | DCHECK(compiler_options_->image_type_ == CompilerOptions::ImageType::kNone); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 651 | if (!image_filenames_.empty() || image_fd_ != -1) { |
Ulya Trafimovich | c124d1d | 2020-01-27 14:50:38 +0000 | [diff] [blame] | 652 | // If no boot image is provided, then dex2oat is compiling the primary boot image, |
Nicolas Geoffray | 4cbb51a | 2020-02-07 11:25:54 +0000 | [diff] [blame] | 653 | // otherwise it is compiling the boot image extension. |
Ulya Trafimovich | c124d1d | 2020-01-27 14:50:38 +0000 | [diff] [blame] | 654 | compiler_options_->image_type_ = boot_image_filename_.empty() |
| 655 | ? CompilerOptions::ImageType::kBootImage |
Nicolas Geoffray | 4cbb51a | 2020-02-07 11:25:54 +0000 | [diff] [blame] | 656 | : CompilerOptions::ImageType::kBootImageExtension; |
Vladimir Marko | 9c4b970 | 2018-11-14 15:09:02 +0000 | [diff] [blame] | 657 | } |
| 658 | if (app_image_fd_ != -1 || !app_image_file_name_.empty()) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 659 | if (compiler_options_->IsBootImage() || compiler_options_->IsBootImageExtension()) { |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 660 | Usage("Can't have both (--image or --image-fd) and (--app-image-fd or --app-image-file)"); |
Vladimir Marko | 9c4b970 | 2018-11-14 15:09:02 +0000 | [diff] [blame] | 661 | } |
| 662 | compiler_options_->image_type_ = CompilerOptions::ImageType::kAppImage; |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 665 | if (!image_filenames_.empty() && image_fd_ != -1) { |
| 666 | Usage("Can't have both --image and --image-fd"); |
| 667 | } |
| 668 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 669 | if (oat_filenames_.empty() && oat_fd_ == -1) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 670 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
| 671 | } |
| 672 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 673 | if (input_vdex_fd_ != -1 && !input_vdex_.empty()) { |
| 674 | Usage("Can't have both --input-vdex-fd and --input-vdex"); |
| 675 | } |
| 676 | |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 677 | if (output_vdex_fd_ != -1 && !output_vdex_.empty()) { |
| 678 | Usage("Can't have both --output-vdex-fd and --output-vdex"); |
| 679 | } |
| 680 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 681 | if (!oat_filenames_.empty() && oat_fd_ != -1) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 682 | Usage("--oat-file should not be used with --oat-fd"); |
| 683 | } |
| 684 | |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 685 | if ((output_vdex_fd_ == -1) != (oat_fd_ == -1)) { |
Sascha Roth | 5f71c65 | 2018-04-10 00:27:15 +0200 | [diff] [blame] | 686 | Usage("VDEX and OAT output must be specified either with one --oat-file " |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 687 | "or with --oat-fd and --output-vdex-fd file descriptors"); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 690 | if ((image_fd_ != -1) && (oat_fd_ == -1)) { |
| 691 | Usage("--image-fd must be used with --oat_fd and --output_vdex_fd"); |
| 692 | } |
| 693 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 694 | if (!parser_options->oat_symbols.empty() && oat_fd_ != -1) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 695 | Usage("--oat-symbols should not be used with --oat-fd"); |
| 696 | } |
| 697 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 698 | if (!parser_options->oat_symbols.empty() && is_host_) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 699 | Usage("--oat-symbols should not be used with --host"); |
| 700 | } |
| 701 | |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 702 | if (output_vdex_fd_ != -1 && !image_filenames_.empty()) { |
| 703 | Usage("--output-vdex-fd should not be used with --image"); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 706 | if (oat_fd_ != -1 && !image_filenames_.empty()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 707 | Usage("--oat-fd should not be used with --image"); |
| 708 | } |
| 709 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 710 | if (!parser_options->oat_symbols.empty() && |
| 711 | parser_options->oat_symbols.size() != oat_filenames_.size()) { |
| 712 | Usage("--oat-file arguments do not match --oat-symbols arguments"); |
| 713 | } |
| 714 | |
| 715 | if (!image_filenames_.empty() && image_filenames_.size() != oat_filenames_.size()) { |
| 716 | Usage("--oat-file arguments do not match --image arguments"); |
| 717 | } |
| 718 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 719 | if (!IsBootImage() && boot_image_filename_.empty()) { |
| 720 | DCHECK(!IsBootImageExtension()); |
Orion Hodson | 971068d | 2021-06-30 21:17:53 +0100 | [diff] [blame] | 721 | boot_image_filename_ = |
| 722 | GetDefaultBootImageLocation(android_root_, /*deny_art_apex_data_files=*/false); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 723 | } |
| 724 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 725 | if (dex_filenames_.empty() && zip_fd_ == -1) { |
| 726 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
| 727 | } |
| 728 | |
| 729 | if (!dex_filenames_.empty() && zip_fd_ != -1) { |
| 730 | Usage("--dex-file should not be used with --zip-fd"); |
| 731 | } |
| 732 | |
| 733 | if (!dex_filenames_.empty() && !zip_location_.empty()) { |
| 734 | Usage("--dex-file should not be used with --zip-location"); |
| 735 | } |
| 736 | |
| 737 | if (dex_locations_.empty()) { |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 738 | dex_locations_ = dex_filenames_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 739 | } else if (dex_locations_.size() != dex_filenames_.size()) { |
| 740 | Usage("--dex-location arguments do not match --dex-file arguments"); |
| 741 | } |
| 742 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 743 | if (!dex_filenames_.empty() && !oat_filenames_.empty()) { |
| 744 | if (oat_filenames_.size() != 1 && oat_filenames_.size() != dex_filenames_.size()) { |
| 745 | Usage("--oat-file arguments must be singular or match --dex-file arguments"); |
| 746 | } |
| 747 | } |
| 748 | |
Victor Hsieh | 826e666 | 2021-05-17 15:40:13 -0700 | [diff] [blame] | 749 | if (!dex_fds_.empty() && dex_fds_.size() != dex_filenames_.size()) { |
| 750 | Usage("--dex-fd arguments do not match --dex-file arguments"); |
| 751 | } |
| 752 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 753 | if (zip_fd_ != -1 && zip_location_.empty()) { |
| 754 | Usage("--zip-location should be supplied with --zip-fd"); |
| 755 | } |
| 756 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 757 | if (boot_image_filename_.empty()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 758 | if (image_base_ == 0) { |
Vladimir Marko | 6623594 | 2019-06-03 16:05:17 +0100 | [diff] [blame] | 759 | Usage("Non-zero --base not specified for boot image"); |
| 760 | } |
| 761 | } else { |
| 762 | if (image_base_ != 0) { |
| 763 | Usage("Non-zero --base specified for app image or boot image extension"); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 767 | if (have_multi_image_arg_) { |
| 768 | if (!IsImage()) { |
| 769 | Usage("--multi-image or --single-image specified for non-image compilation"); |
| 770 | } |
| 771 | } else { |
| 772 | // Use the default, i.e. multi-image for boot image and boot image extension. |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 773 | // This shall pass the checks below. |
| 774 | compiler_options_->multi_image_ = IsBootImage() || IsBootImageExtension(); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 775 | } |
Jiakai Zhang | fb8cdb5 | 2021-12-06 18:51:28 +0000 | [diff] [blame] | 776 | // On target we support generating a single image for the primary boot image. |
| 777 | if (!kIsTargetBuild) { |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 778 | if (IsBootImage() && !compiler_options_->multi_image_) { |
Jiakai Zhang | fb8cdb5 | 2021-12-06 18:51:28 +0000 | [diff] [blame] | 779 | Usage("--single-image specified for primary boot image on host"); |
| 780 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 781 | } |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 782 | if (IsAppImage() && compiler_options_->multi_image_) { |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 783 | Usage("--multi-image specified for app image"); |
| 784 | } |
| 785 | |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 786 | if (image_fd_ != -1 && compiler_options_->multi_image_) { |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 787 | Usage("--single-image not specified for --image-fd"); |
| 788 | } |
| 789 | |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 790 | const bool have_profile_file = !profile_files_.empty(); |
| 791 | const bool have_profile_fd = !profile_file_fds_.empty(); |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 792 | if (have_profile_file && have_profile_fd) { |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 793 | Usage("Profile files should not be specified with both --profile-file-fd and --profile-file"); |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 796 | if (!parser_options->oat_symbols.empty()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 797 | oat_unstripped_ = std::move(parser_options->oat_symbols); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 798 | } |
| 799 | |
xueliang.zhong | 7f88c1a | 2018-11-06 11:42:41 +0000 | [diff] [blame] | 800 | if (compiler_options_->instruction_set_features_ == nullptr) { |
| 801 | // '--instruction-set-features/--instruction-set-variant' were not used. |
| 802 | // Use features for the 'default' variant. |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 803 | compiler_options_->instruction_set_features_ = InstructionSetFeatures::FromVariant( |
| 804 | compiler_options_->instruction_set_, "default", &parser_options->error_msg); |
| 805 | if (compiler_options_->instruction_set_features_ == nullptr) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 806 | Usage("Problem initializing default instruction set features variant: %s", |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 807 | parser_options->error_msg.c_str()); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 808 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 811 | if (compiler_options_->instruction_set_ == kRuntimeISA) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 812 | std::unique_ptr<const InstructionSetFeatures> runtime_features( |
| 813 | InstructionSetFeatures::FromCppDefines()); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 814 | if (!compiler_options_->GetInstructionSetFeatures()->Equals(runtime_features.get())) { |
xueliang.zhong | 7f88c1a | 2018-11-06 11:42:41 +0000 | [diff] [blame] | 815 | LOG(WARNING) << "Mismatch between dex2oat instruction set features to use (" |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 816 | << *compiler_options_->GetInstructionSetFeatures() |
xueliang.zhong | 7f88c1a | 2018-11-06 11:42:41 +0000 | [diff] [blame] | 817 | << ") and those from CPP defines (" << *runtime_features |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 818 | << ") for the command line:\n" << CommandLine(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Victor Hsieh | 4ab5923 | 2021-06-10 12:24:02 -0700 | [diff] [blame] | 822 | if (dirty_image_objects_filename_ != nullptr && dirty_image_objects_fd_ != -1) { |
| 823 | Usage("--dirty-image-objects and --dirty-image-objects-fd should not be both specified"); |
| 824 | } |
| 825 | |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 826 | if (!preloaded_classes_files_.empty() && !preloaded_classes_fds_.empty()) { |
| 827 | Usage("--preloaded-classes and --preloaded-classes-fds should not be both specified"); |
| 828 | } |
| 829 | |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 830 | if (!cpu_set_.empty()) { |
| 831 | SetCpuAffinity(cpu_set_); |
| 832 | } |
| 833 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 834 | if (compiler_options_->inline_max_code_units_ == CompilerOptions::kUnsetInlineMaxCodeUnits) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 835 | compiler_options_->inline_max_code_units_ = CompilerOptions::kDefaultInlineMaxCodeUnits; |
Roland Levillain | a215b95 | 2015-08-07 11:38:32 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 838 | // Checks are all explicit until we know the architecture. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 839 | // Set the compilation target's implicit checks options. |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 840 | switch (compiler_options_->GetInstructionSet()) { |
Vladimir Marko | 7039c11 | 2018-04-05 17:59:39 +0100 | [diff] [blame] | 841 | case InstructionSet::kArm64: |
Vladimir Marko | 9e14327 | 2022-01-04 09:28:42 +0000 | [diff] [blame] | 842 | // TODO: Implicit suspend checks are currently disabled to facilitate search |
| 843 | // for unrelated memory use regressions. Bug: 213757852. |
| 844 | compiler_options_->implicit_suspend_checks_ = false; |
Vladimir Marko | 7039c11 | 2018-04-05 17:59:39 +0100 | [diff] [blame] | 845 | FALLTHROUGH_INTENDED; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 846 | case InstructionSet::kArm: |
| 847 | case InstructionSet::kThumb2: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 848 | case InstructionSet::kX86: |
| 849 | case InstructionSet::kX86_64: |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 850 | compiler_options_->implicit_null_checks_ = true; |
| 851 | compiler_options_->implicit_so_checks_ = true; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 852 | break; |
| 853 | |
| 854 | default: |
| 855 | // Defaults are correct. |
| 856 | break; |
| 857 | } |
| 858 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 859 | // Done with usage checks, enable watchdog if requested |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 860 | if (parser_options->watch_dog_enabled) { |
Andreas Gampe | 2e8a256 | 2017-01-18 20:39:02 -0800 | [diff] [blame] | 861 | int64_t timeout = parser_options->watch_dog_timeout_in_ms > 0 |
| 862 | ? parser_options->watch_dog_timeout_in_ms |
| 863 | : WatchDog::kDefaultWatchdogTimeoutInMS; |
| 864 | watchdog_.reset(new WatchDog(timeout)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | // Fill some values into the key-value store for the oat header. |
Eric Holk | 94a810b | 2020-05-06 14:33:16 -0700 | [diff] [blame] | 868 | key_value_store_.reset(new OatKeyValueStore()); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 869 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 870 | // Automatically force determinism for the boot image and boot image extensions in a host build. |
| 871 | if (!kIsTargetBuild && (IsBootImage() || IsBootImageExtension())) { |
Vladimir Marko | 7834241 | 2019-07-25 17:28:02 +0100 | [diff] [blame] | 872 | force_determinism_ = true; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 873 | } |
| 874 | compiler_options_->force_determinism_ = force_determinism_; |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 875 | |
David Sehr | fe57c2b | 2020-03-27 14:58:54 +0000 | [diff] [blame] | 876 | compiler_options_->check_linkage_conditions_ = check_linkage_conditions_; |
| 877 | compiler_options_->crash_on_linkage_violation_ = crash_on_linkage_violation_; |
| 878 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 879 | if (passes_to_run_filename_ != nullptr) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 880 | passes_to_run_ = ReadCommentedInputFromFile<std::vector<std::string>>( |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 881 | passes_to_run_filename_, |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 882 | nullptr); // No post-processing. |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 883 | if (passes_to_run_.get() == nullptr) { |
| 884 | Usage("Failed to read list of passes to run."); |
| 885 | } |
| 886 | } |
Vladimir Marko | f6afcc9 | 2020-02-05 10:10:03 +0000 | [diff] [blame] | 887 | |
Jiakai Zhang | d294d9d | 2021-12-22 15:22:33 +0000 | [diff] [blame] | 888 | // Prune profile specifications of the boot image location. |
| 889 | std::vector<std::string> boot_images = |
| 890 | android::base::Split(boot_image_filename_, {ImageSpace::kComponentSeparator}); |
| 891 | bool boot_image_filename_pruned = false; |
| 892 | for (std::string& boot_image : boot_images) { |
| 893 | size_t profile_separator_pos = boot_image.find(ImageSpace::kProfileSeparator); |
| 894 | if (profile_separator_pos != std::string::npos) { |
| 895 | boot_image.resize(profile_separator_pos); |
| 896 | boot_image_filename_pruned = true; |
Vladimir Marko | f6afcc9 | 2020-02-05 10:10:03 +0000 | [diff] [blame] | 897 | } |
Jiakai Zhang | d294d9d | 2021-12-22 15:22:33 +0000 | [diff] [blame] | 898 | } |
| 899 | if (boot_image_filename_pruned) { |
| 900 | std::string new_boot_image_filename = |
| 901 | android::base::Join(boot_images, ImageSpace::kComponentSeparator); |
| 902 | VLOG(compiler) << "Pruning profile specifications of the boot image location. Before: " |
| 903 | << boot_image_filename_ << ", After: " << new_boot_image_filename; |
| 904 | boot_image_filename_ = std::move(new_boot_image_filename); |
Vladimir Marko | f6afcc9 | 2020-02-05 10:10:03 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 907 | compiler_options_->passes_to_run_ = passes_to_run_.get(); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 910 | void ExpandOatAndImageFilenames() { |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 911 | ArrayRef<const std::string> locations(dex_locations_); |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 912 | if (!compiler_options_->multi_image_) { |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 913 | locations = locations.SubArray(/*pos=*/ 0u, /*length=*/ 1u); |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 914 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 915 | if (image_fd_ == -1) { |
| 916 | if (image_filenames_[0].rfind('/') == std::string::npos) { |
| 917 | Usage("Unusable boot image filename %s", image_filenames_[0].c_str()); |
| 918 | } |
| 919 | image_filenames_ = ImageSpace::ExpandMultiImageLocations( |
| 920 | locations, image_filenames_[0], IsBootImageExtension()); |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 921 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 922 | if (oat_filenames_[0].rfind('/') == std::string::npos) { |
| 923 | Usage("Unusable boot image oat filename %s", oat_filenames_[0].c_str()); |
| 924 | } |
| 925 | oat_filenames_ = ImageSpace::ExpandMultiImageLocations( |
| 926 | locations, oat_filenames_[0], IsBootImageExtension()); |
| 927 | } else { |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 928 | DCHECK(!compiler_options_->multi_image_); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 929 | std::vector<std::string> oat_locations = ImageSpace::ExpandMultiImageLocations( |
| 930 | locations, oat_location_, IsBootImageExtension()); |
| 931 | DCHECK_EQ(1u, oat_locations.size()); |
| 932 | oat_location_ = oat_locations[0]; |
Vladimir Marko | db4b1de | 2018-12-07 17:18:07 +0000 | [diff] [blame] | 933 | } |
Vladimir Marko | db4b1de | 2018-12-07 17:18:07 +0000 | [diff] [blame] | 934 | |
bowen_lai | 8bd36c0 | 2016-10-12 13:28:55 +0800 | [diff] [blame] | 935 | if (!oat_unstripped_.empty()) { |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 936 | if (oat_unstripped_[0].rfind('/') == std::string::npos) { |
| 937 | Usage("Unusable boot image symbol filename %s", oat_unstripped_[0].c_str()); |
bowen_lai | 8bd36c0 | 2016-10-12 13:28:55 +0800 | [diff] [blame] | 938 | } |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 939 | oat_unstripped_ = ImageSpace::ExpandMultiImageLocations( |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 940 | locations, oat_unstripped_[0], IsBootImageExtension()); |
bowen_lai | 8bd36c0 | 2016-10-12 13:28:55 +0800 | [diff] [blame] | 941 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 944 | void InsertCompileOptions(int argc, char** argv) { |
Mathieu Chartier | d1ab1bd | 2017-08-04 10:21:50 -0700 | [diff] [blame] | 945 | if (!avoid_storing_invocation_) { |
Vladimir Marko | 56581d8 | 2018-11-07 12:30:52 +0000 | [diff] [blame] | 946 | std::ostringstream oss; |
Mathieu Chartier | d1ab1bd | 2017-08-04 10:21:50 -0700 | [diff] [blame] | 947 | for (int i = 0; i < argc; ++i) { |
| 948 | if (i > 0) { |
| 949 | oss << ' '; |
| 950 | } |
| 951 | oss << argv[i]; |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 952 | } |
Mathieu Chartier | d1ab1bd | 2017-08-04 10:21:50 -0700 | [diff] [blame] | 953 | key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 954 | } |
Eric Holk | 94a810b | 2020-05-06 14:33:16 -0700 | [diff] [blame] | 955 | key_value_store_->Put(OatHeader::kDebuggableKey, compiler_options_->debuggable_); |
| 956 | key_value_store_->Put(OatHeader::kNativeDebuggableKey, |
| 957 | compiler_options_->GetNativeDebuggable()); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 958 | key_value_store_->Put(OatHeader::kCompilerFilter, |
Eric Holk | 94a810b | 2020-05-06 14:33:16 -0700 | [diff] [blame] | 959 | CompilerFilter::NameOfFilter(compiler_options_->GetCompilerFilter())); |
| 960 | key_value_store_->Put(OatHeader::kConcurrentCopying, kUseReadBarrier); |
Alex Light | 62afcf5 | 2018-12-18 14:19:25 -0800 | [diff] [blame] | 961 | if (invocation_file_.get() != -1) { |
| 962 | std::ostringstream oss; |
| 963 | for (int i = 0; i < argc; ++i) { |
| 964 | if (i > 0) { |
| 965 | oss << std::endl; |
| 966 | } |
| 967 | oss << argv[i]; |
| 968 | } |
| 969 | std::string invocation(oss.str()); |
| 970 | if (TEMP_FAILURE_RETRY(write(invocation_file_.get(), |
| 971 | invocation.c_str(), |
| 972 | invocation.size())) == -1) { |
| 973 | Usage("Unable to write invocation file"); |
| 974 | } |
| 975 | } |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 978 | // This simple forward is here so the string specializations below don't look out of place. |
| 979 | template <typename T, typename U> |
| 980 | void AssignIfExists(Dex2oatArgumentMap& map, |
| 981 | const Dex2oatArgumentMap::Key<T>& key, |
| 982 | U* out) { |
| 983 | map.AssignIfExists(key, out); |
| 984 | } |
| 985 | |
| 986 | // Specializations to handle const char* vs std::string. |
| 987 | void AssignIfExists(Dex2oatArgumentMap& map, |
| 988 | const Dex2oatArgumentMap::Key<std::string>& key, |
| 989 | const char** out) { |
| 990 | if (map.Exists(key)) { |
| 991 | char_backing_storage_.push_front(std::move(*map.Get(key))); |
| 992 | *out = char_backing_storage_.front().c_str(); |
| 993 | } |
| 994 | } |
| 995 | void AssignIfExists(Dex2oatArgumentMap& map, |
| 996 | const Dex2oatArgumentMap::Key<std::vector<std::string>>& key, |
| 997 | std::vector<const char*>* out) { |
| 998 | if (map.Exists(key)) { |
| 999 | for (auto& val : *map.Get(key)) { |
| 1000 | char_backing_storage_.push_front(std::move(val)); |
| 1001 | out->push_back(char_backing_storage_.front().c_str()); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | template <typename T> |
| 1007 | void AssignTrueIfExists(Dex2oatArgumentMap& map, |
| 1008 | const Dex2oatArgumentMap::Key<T>& key, |
| 1009 | bool* out) { |
| 1010 | if (map.Exists(key)) { |
| 1011 | *out = true; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 1015 | void AssignIfExists(Dex2oatArgumentMap& map, |
| 1016 | const Dex2oatArgumentMap::Key<std::string>& key, |
| 1017 | std::vector<std::string>* out) { |
| 1018 | DCHECK(out->empty()); |
| 1019 | if (map.Exists(key)) { |
| 1020 | out->push_back(*map.Get(key)); |
| 1021 | } |
| 1022 | } |
| 1023 | |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1024 | // Parse the arguments from the command line. In case of an unrecognized option or impossible |
| 1025 | // values/combinations, a usage error will be displayed and exit() is called. Thus, if the method |
| 1026 | // returns, arguments have been successfully parsed. |
| 1027 | void ParseArgs(int argc, char** argv) { |
| 1028 | original_argc = argc; |
| 1029 | original_argv = argv; |
| 1030 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 1031 | Locks::Init(); |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 1032 | InitLogging(argv, Runtime::Abort); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1033 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1034 | compiler_options_.reset(new CompilerOptions()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1035 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1036 | using M = Dex2oatArgumentMap; |
| 1037 | std::string error_msg; |
| 1038 | std::unique_ptr<M> args_uptr = M::Parse(argc, const_cast<const char**>(argv), &error_msg); |
| 1039 | if (args_uptr == nullptr) { |
| 1040 | Usage("Failed to parse command line: %s", error_msg.c_str()); |
| 1041 | UNREACHABLE(); |
| 1042 | } |
| 1043 | |
| 1044 | M& args = *args_uptr; |
| 1045 | |
| 1046 | std::unique_ptr<ParserOptions> parser_options(new ParserOptions()); |
| 1047 | |
Mathieu Chartier | eafe2a5 | 2017-10-19 15:29:42 -0700 | [diff] [blame] | 1048 | AssignIfExists(args, M::CompactDexLevel, &compact_dex_level_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1049 | AssignIfExists(args, M::DexFiles, &dex_filenames_); |
| 1050 | AssignIfExists(args, M::DexLocations, &dex_locations_); |
Victor Hsieh | 826e666 | 2021-05-17 15:40:13 -0700 | [diff] [blame] | 1051 | AssignIfExists(args, M::DexFds, &dex_fds_); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 1052 | AssignIfExists(args, M::OatFile, &oat_filenames_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1053 | AssignIfExists(args, M::OatSymbols, &parser_options->oat_symbols); |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 1054 | AssignTrueIfExists(args, M::Strip, &strip_); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 1055 | AssignIfExists(args, M::ImageFilename, &image_filenames_); |
| 1056 | AssignIfExists(args, M::ImageFd, &image_fd_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1057 | AssignIfExists(args, M::ZipFd, &zip_fd_); |
| 1058 | AssignIfExists(args, M::ZipLocation, &zip_location_); |
| 1059 | AssignIfExists(args, M::InputVdexFd, &input_vdex_fd_); |
| 1060 | AssignIfExists(args, M::OutputVdexFd, &output_vdex_fd_); |
| 1061 | AssignIfExists(args, M::InputVdex, &input_vdex_); |
| 1062 | AssignIfExists(args, M::OutputVdex, &output_vdex_); |
Nicolas Geoffray | baeaa9b | 2018-01-26 14:31:17 +0000 | [diff] [blame] | 1063 | AssignIfExists(args, M::DmFd, &dm_fd_); |
| 1064 | AssignIfExists(args, M::DmFile, &dm_file_location_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1065 | AssignIfExists(args, M::OatFd, &oat_fd_); |
| 1066 | AssignIfExists(args, M::OatLocation, &oat_location_); |
| 1067 | AssignIfExists(args, M::Watchdog, &parser_options->watch_dog_enabled); |
| 1068 | AssignIfExists(args, M::WatchdogTimeout, &parser_options->watch_dog_timeout_in_ms); |
| 1069 | AssignIfExists(args, M::Threads, &thread_count_); |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 1070 | AssignIfExists(args, M::CpuSet, &cpu_set_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1071 | AssignIfExists(args, M::Passes, &passes_to_run_filename_); |
| 1072 | AssignIfExists(args, M::BootImage, &parser_options->boot_image_filename); |
| 1073 | AssignIfExists(args, M::AndroidRoot, &android_root_); |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 1074 | AssignIfExists(args, M::Profile, &profile_files_); |
| 1075 | AssignIfExists(args, M::ProfileFd, &profile_file_fds_); |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 1076 | AssignIfExists(args, M::PreloadedClasses, &preloaded_classes_files_); |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 1077 | AssignIfExists(args, M::PreloadedClassesFds, &preloaded_classes_fds_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1078 | AssignIfExists(args, M::RuntimeOptions, &runtime_args_); |
| 1079 | AssignIfExists(args, M::SwapFile, &swap_file_name_); |
| 1080 | AssignIfExists(args, M::SwapFileFd, &swap_fd_); |
| 1081 | AssignIfExists(args, M::SwapDexSizeThreshold, &min_dex_file_cumulative_size_for_swap_); |
| 1082 | AssignIfExists(args, M::SwapDexCountThreshold, &min_dex_files_for_swap_); |
| 1083 | AssignIfExists(args, M::VeryLargeAppThreshold, &very_large_threshold_); |
| 1084 | AssignIfExists(args, M::AppImageFile, &app_image_file_name_); |
| 1085 | AssignIfExists(args, M::AppImageFileFd, &app_image_fd_); |
| 1086 | AssignIfExists(args, M::NoInlineFrom, &no_inline_from_string_); |
| 1087 | AssignIfExists(args, M::ClasspathDir, &classpath_dir_); |
| 1088 | AssignIfExists(args, M::DirtyImageObjects, &dirty_image_objects_filename_); |
Victor Hsieh | 4ab5923 | 2021-06-10 12:24:02 -0700 | [diff] [blame] | 1089 | AssignIfExists(args, M::DirtyImageObjectsFd, &dirty_image_objects_fd_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1090 | AssignIfExists(args, M::ImageFormat, &image_storage_mode_); |
Calin Juravle | 0e09dfc | 2018-02-12 19:01:09 -0800 | [diff] [blame] | 1091 | AssignIfExists(args, M::CompilationReason, &compilation_reason_); |
David Sehr | fe57c2b | 2020-03-27 14:58:54 +0000 | [diff] [blame] | 1092 | AssignTrueIfExists(args, M::CheckLinkageConditions, &check_linkage_conditions_); |
| 1093 | AssignTrueIfExists(args, M::CrashOnLinkageViolation, &crash_on_linkage_violation_); |
Alex Light | 4531afd | 2021-03-30 17:13:30 -0700 | [diff] [blame] | 1094 | AssignTrueIfExists(args, M::ForceAllowOjInlines, &force_allow_oj_inlines_); |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 1095 | AssignIfExists(args, M::PublicSdk, &public_sdk_); |
Nicolas Geoffray | d3c45c2 | 2021-04-27 13:21:28 +0100 | [diff] [blame] | 1096 | AssignIfExists(args, M::ApexVersions, &apex_versions_argument_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1097 | |
| 1098 | AssignIfExists(args, M::Backend, &compiler_kind_); |
| 1099 | parser_options->requested_specific_compiler = args.Exists(M::Backend); |
| 1100 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 1101 | AssignIfExists(args, M::TargetInstructionSet, &compiler_options_->instruction_set_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1102 | // arm actually means thumb2. |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 1103 | if (compiler_options_->instruction_set_ == InstructionSet::kArm) { |
| 1104 | compiler_options_->instruction_set_ = InstructionSet::kThumb2; |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | AssignTrueIfExists(args, M::Host, &is_host_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1108 | AssignTrueIfExists(args, M::AvoidStoringInvocation, &avoid_storing_invocation_); |
Alex Light | 62afcf5 | 2018-12-18 14:19:25 -0800 | [diff] [blame] | 1109 | if (args.Exists(M::InvocationFile)) { |
| 1110 | invocation_file_.reset(open(args.Get(M::InvocationFile)->c_str(), |
| 1111 | O_CREAT|O_WRONLY|O_TRUNC|O_CLOEXEC, |
| 1112 | S_IRUSR|S_IWUSR)); |
| 1113 | if (invocation_file_.get() == -1) { |
| 1114 | int err = errno; |
| 1115 | Usage("Unable to open invocation file '%s' for writing due to %s.", |
| 1116 | args.Get(M::InvocationFile)->c_str(), strerror(err)); |
| 1117 | } |
| 1118 | } |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 1119 | AssignIfExists(args, M::CopyDexFiles, ©_dex_files_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1120 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 1121 | AssignTrueIfExists(args, M::MultiImage, &have_multi_image_arg_); |
Vladimir Marko | f67e8c3 | 2022-03-17 12:55:27 +0000 | [diff] [blame] | 1122 | AssignIfExists(args, M::MultiImage, &compiler_options_->multi_image_); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 1123 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1124 | if (args.Exists(M::ForceDeterminism)) { |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1125 | force_determinism_ = true; |
| 1126 | } |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 1127 | AssignTrueIfExists(args, M::CompileIndividually, &compile_individually_); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1128 | |
| 1129 | if (args.Exists(M::Base)) { |
| 1130 | ParseBase(*args.Get(M::Base)); |
| 1131 | } |
| 1132 | if (args.Exists(M::TargetInstructionSetVariant)) { |
| 1133 | ParseInstructionSetVariant(*args.Get(M::TargetInstructionSetVariant), parser_options.get()); |
| 1134 | } |
| 1135 | if (args.Exists(M::TargetInstructionSetFeatures)) { |
| 1136 | ParseInstructionSetFeatures(*args.Get(M::TargetInstructionSetFeatures), parser_options.get()); |
| 1137 | } |
| 1138 | if (args.Exists(M::ClassLoaderContext)) { |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1139 | std::string class_loader_context_arg = *args.Get(M::ClassLoaderContext); |
| 1140 | class_loader_context_ = ClassLoaderContext::Create(class_loader_context_arg); |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1141 | if (class_loader_context_ == nullptr) { |
| 1142 | Usage("Option --class-loader-context has an incorrect format: %s", |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1143 | class_loader_context_arg.c_str()); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1144 | } |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 1145 | if (args.Exists(M::ClassLoaderContextFds)) { |
| 1146 | std::string str_fds_arg = *args.Get(M::ClassLoaderContextFds); |
| 1147 | std::vector<std::string> str_fds = android::base::Split(str_fds_arg, ":"); |
| 1148 | for (const std::string& str_fd : str_fds) { |
| 1149 | class_loader_context_fds_.push_back(std::stoi(str_fd, nullptr, 0)); |
| 1150 | if (class_loader_context_fds_.back() < 0) { |
| 1151 | Usage("Option --class-loader-context-fds has incorrect format: %s", |
| 1152 | str_fds_arg.c_str()); |
| 1153 | } |
| 1154 | } |
| 1155 | } |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1156 | if (args.Exists(M::StoredClassLoaderContext)) { |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 1157 | const std::string stored_context_arg = *args.Get(M::StoredClassLoaderContext); |
| 1158 | stored_class_loader_context_ = ClassLoaderContext::Create(stored_context_arg); |
| 1159 | if (stored_class_loader_context_ == nullptr) { |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1160 | Usage("Option --stored-class-loader-context has an incorrect format: %s", |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 1161 | stored_context_arg.c_str()); |
Vladimir Marko | d8860b4 | 2018-05-02 14:58:12 +0100 | [diff] [blame] | 1162 | } else if (class_loader_context_->VerifyClassLoaderContextMatch( |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 1163 | stored_context_arg, |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1164 | /*verify_names*/ false, |
Vladimir Marko | d8860b4 | 2018-05-02 14:58:12 +0100 | [diff] [blame] | 1165 | /*verify_checksums*/ false) != ClassLoaderContext::VerificationResult::kVerifies) { |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1166 | Usage( |
| 1167 | "Option --stored-class-loader-context '%s' mismatches --class-loader-context '%s'", |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 1168 | stored_context_arg.c_str(), |
| 1169 | class_loader_context_arg.c_str()); |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | } else if (args.Exists(M::StoredClassLoaderContext)) { |
| 1173 | Usage("Option --stored-class-loader-context should only be used if " |
| 1174 | "--class-loader-context is also specified"); |
Roland Levillain | 9ec5e22 | 2015-08-17 20:18:41 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
Jiakai Zhang | ebf496f | 2021-10-11 05:04:34 +0000 | [diff] [blame] | 1177 | if (args.Exists(M::UpdatableBcpPackagesFile)) { |
| 1178 | LOG(WARNING) |
| 1179 | << "Option --updatable-bcp-packages-file is deprecated and no longer takes effect"; |
| 1180 | } |
| 1181 | |
| 1182 | if (args.Exists(M::UpdatableBcpPackagesFd)) { |
| 1183 | LOG(WARNING) << "Option --updatable-bcp-packages-fd is deprecated and no longer takes effect"; |
| 1184 | } |
| 1185 | |
Jiakai Zhang | e7815b8 | 2021-12-23 14:39:58 +0000 | [diff] [blame] | 1186 | if (args.Exists(M::ForceJitZygote)) { |
| 1187 | if (!parser_options->boot_image_filename.empty()) { |
| 1188 | Usage("Option --boot-image and --force-jit-zygote cannot be specified together"); |
| 1189 | } |
| 1190 | parser_options->boot_image_filename = "boot.art:/nonx/boot-framework.art"; |
| 1191 | } |
| 1192 | |
Vladimir Marko | 89b85eb | 2020-02-04 15:51:44 +0000 | [diff] [blame] | 1193 | // If we have a profile, change the default compiler filter to speed-profile |
| 1194 | // before reading compiler options. |
| 1195 | static_assert(CompilerFilter::kDefaultCompilerFilter == CompilerFilter::kSpeed); |
| 1196 | DCHECK_EQ(compiler_options_->GetCompilerFilter(), CompilerFilter::kSpeed); |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 1197 | if (HasProfileInput()) { |
Vladimir Marko | 89b85eb | 2020-02-04 15:51:44 +0000 | [diff] [blame] | 1198 | compiler_options_->SetCompilerFilter(CompilerFilter::kSpeedProfile); |
| 1199 | } |
| 1200 | |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 1201 | if (!ReadCompilerOptions(args, compiler_options_.get(), &error_msg)) { |
| 1202 | Usage(error_msg.c_str()); |
| 1203 | } |
| 1204 | |
Santiago Aboy Solanes | ac4e4a2 | 2021-11-02 11:31:08 +0000 | [diff] [blame] | 1205 | if (!compiler_options_->GetDumpCfgFileName().empty() && thread_count_ != 1) { |
| 1206 | LOG(INFO) << "Since we are dumping the CFG to " << compiler_options_->GetDumpCfgFileName() |
| 1207 | << ", we override thread number to 1 to have determinism. It was " << thread_count_ |
| 1208 | << "."; |
| 1209 | thread_count_ = 1; |
| 1210 | } |
| 1211 | |
Nicolas Geoffray | ad67bbb | 2022-02-04 17:26:40 +0000 | [diff] [blame] | 1212 | // For debuggable apps, we do not want to generate compact dex as class |
| 1213 | // redefinition will want a proper dex file. |
| 1214 | if (compiler_options_->GetDebuggable()) { |
| 1215 | compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone; |
| 1216 | } |
| 1217 | |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 1218 | PaletteShouldReportDex2oatCompilation(&should_report_dex2oat_compilation_); |
| 1219 | AssignTrueIfExists(args, M::ForcePaletteCompilationHooks, &should_report_dex2oat_compilation_); |
| 1220 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1221 | ProcessOptions(parser_options.get()); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1224 | // Check whether the oat output files are writable, and open them for later. Also open a swap |
| 1225 | // file, if a name is given. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1226 | bool OpenFile() { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1227 | // Prune non-existent dex files now so that we don't create empty oat files for multi-image. |
| 1228 | PruneNonExistentDexFiles(); |
| 1229 | |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 1230 | // Expand oat and image filenames for boot image and boot image extension. |
| 1231 | // This is mostly for multi-image but single-image also needs some processing. |
| 1232 | if (IsBootImage() || IsBootImageExtension()) { |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1233 | ExpandOatAndImageFilenames(); |
| 1234 | } |
| 1235 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1236 | // OAT and VDEX file handling |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1237 | if (oat_fd_ == -1) { |
| 1238 | DCHECK(!oat_filenames_.empty()); |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 1239 | for (const std::string& oat_filename : oat_filenames_) { |
| 1240 | std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_filename.c_str())); |
Calin Juravle | b09be72 | 2017-09-25 16:42:48 -0700 | [diff] [blame] | 1241 | if (oat_file == nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1242 | PLOG(ERROR) << "Failed to create oat file: " << oat_filename; |
| 1243 | return false; |
| 1244 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1245 | if (fchmod(oat_file->Fd(), 0644) != 0) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1246 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_filename; |
| 1247 | oat_file->Erase(); |
| 1248 | return false; |
| 1249 | } |
| 1250 | oat_files_.push_back(std::move(oat_file)); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 1251 | DCHECK_EQ(input_vdex_fd_, -1); |
| 1252 | if (!input_vdex_.empty()) { |
| 1253 | std::string error_msg; |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 1254 | input_vdex_file_ = VdexFile::Open(input_vdex_, |
| 1255 | /* writable */ false, |
| 1256 | /* low_4gb */ false, |
| 1257 | &error_msg); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 1258 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1259 | |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 1260 | DCHECK_EQ(output_vdex_fd_, -1); |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 1261 | std::string vdex_filename = output_vdex_.empty() |
| 1262 | ? ReplaceFileExtension(oat_filename, "vdex") |
| 1263 | : output_vdex_; |
| 1264 | if (vdex_filename == input_vdex_ && output_vdex_.empty()) { |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1265 | use_existing_vdex_ = true; |
| 1266 | std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_filename.c_str())); |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 1267 | vdex_files_.push_back(std::move(vdex_file)); |
| 1268 | } else { |
| 1269 | std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_filename.c_str())); |
Calin Juravle | b09be72 | 2017-09-25 16:42:48 -0700 | [diff] [blame] | 1270 | if (vdex_file == nullptr) { |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 1271 | PLOG(ERROR) << "Failed to open vdex file: " << vdex_filename; |
| 1272 | return false; |
| 1273 | } |
| 1274 | if (fchmod(vdex_file->Fd(), 0644) != 0) { |
| 1275 | PLOG(ERROR) << "Failed to make vdex file world readable: " << vdex_filename; |
| 1276 | vdex_file->Erase(); |
| 1277 | return false; |
| 1278 | } |
| 1279 | vdex_files_.push_back(std::move(vdex_file)); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1280 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1281 | } |
| 1282 | } else { |
Josh Gao | afeec9f | 2018-08-30 14:05:56 -0700 | [diff] [blame] | 1283 | std::unique_ptr<File> oat_file( |
| 1284 | new File(DupCloexec(oat_fd_), oat_location_, /* check_usage */ true)); |
| 1285 | if (!oat_file->IsOpened()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1286 | PLOG(ERROR) << "Failed to create oat file: " << oat_location_; |
| 1287 | return false; |
| 1288 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1289 | if (oat_file->SetLength(0) != 0) { |
| 1290 | PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed."; |
Calin Juravle | b09be72 | 2017-09-25 16:42:48 -0700 | [diff] [blame] | 1291 | oat_file->Erase(); |
| 1292 | return false; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1293 | } |
| 1294 | oat_files_.push_back(std::move(oat_file)); |
| 1295 | |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 1296 | if (input_vdex_fd_ != -1) { |
| 1297 | struct stat s; |
| 1298 | int rc = TEMP_FAILURE_RETRY(fstat(input_vdex_fd_, &s)); |
| 1299 | if (rc == -1) { |
| 1300 | PLOG(WARNING) << "Failed getting length of vdex file"; |
| 1301 | } else { |
| 1302 | std::string error_msg; |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 1303 | input_vdex_file_ = VdexFile::Open(input_vdex_fd_, |
| 1304 | s.st_size, |
| 1305 | "vdex", |
| 1306 | /* writable */ false, |
| 1307 | /* low_4gb */ false, |
| 1308 | &error_msg); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 1309 | // If there's any problem with the passed vdex, just warn and proceed |
| 1310 | // without it. |
| 1311 | if (input_vdex_file_ == nullptr) { |
Richard Uhler | b8ab63a | 2017-01-31 11:27:37 +0000 | [diff] [blame] | 1312 | PLOG(WARNING) << "Failed opening vdex file: " << error_msg; |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 1317 | DCHECK_NE(output_vdex_fd_, -1); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1318 | std::string vdex_location = ReplaceFileExtension(oat_location_, "vdex"); |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1319 | if (input_vdex_file_ != nullptr && output_vdex_fd_ == input_vdex_fd_) { |
| 1320 | use_existing_vdex_ = true; |
| 1321 | } |
| 1322 | |
| 1323 | std::unique_ptr<File> vdex_file(new File(DupCloexec(output_vdex_fd_), |
| 1324 | vdex_location, |
| 1325 | /* check_usage= */ true, |
| 1326 | /* read_only_mode= */ use_existing_vdex_)); |
Josh Gao | afeec9f | 2018-08-30 14:05:56 -0700 | [diff] [blame] | 1327 | if (!vdex_file->IsOpened()) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1328 | PLOG(ERROR) << "Failed to create vdex file: " << vdex_location; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1329 | return false; |
| 1330 | } |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1331 | |
| 1332 | if (!use_existing_vdex_) { |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 1333 | if (vdex_file->SetLength(0) != 0) { |
| 1334 | PLOG(ERROR) << "Truncating vdex file " << vdex_location << " failed."; |
Calin Juravle | b09be72 | 2017-09-25 16:42:48 -0700 | [diff] [blame] | 1335 | vdex_file->Erase(); |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 1336 | return false; |
| 1337 | } |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1338 | } |
| 1339 | vdex_files_.push_back(std::move(vdex_file)); |
| 1340 | |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 1341 | oat_filenames_.push_back(oat_location_); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1342 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1343 | |
Nicolas Geoffray | baeaa9b | 2018-01-26 14:31:17 +0000 | [diff] [blame] | 1344 | if (dm_fd_ != -1 || !dm_file_location_.empty()) { |
| 1345 | std::string error_msg; |
| 1346 | if (dm_fd_ != -1) { |
| 1347 | dm_file_.reset(ZipArchive::OpenFromFd(dm_fd_, "DexMetadata", &error_msg)); |
| 1348 | } else { |
| 1349 | dm_file_.reset(ZipArchive::Open(dm_file_location_.c_str(), &error_msg)); |
| 1350 | } |
| 1351 | if (dm_file_ == nullptr) { |
| 1352 | LOG(WARNING) << "Could not open DexMetadata archive " << error_msg; |
| 1353 | } |
| 1354 | } |
| 1355 | |
Nicolas Geoffray | 8409281 | 2021-12-15 14:08:44 +0000 | [diff] [blame] | 1356 | // If we have a dm file and a vdex file, we (arbitrarily) pick the vdex file. |
| 1357 | // In theory the files should be the same. |
Nicolas Geoffray | baeaa9b | 2018-01-26 14:31:17 +0000 | [diff] [blame] | 1358 | if (dm_file_ != nullptr) { |
Nicolas Geoffray | 8409281 | 2021-12-15 14:08:44 +0000 | [diff] [blame] | 1359 | if (input_vdex_file_ == nullptr) { |
| 1360 | input_vdex_file_ = VdexFile::OpenFromDm(dm_file_location_, *dm_file_); |
| 1361 | if (input_vdex_file_ != nullptr) { |
| 1362 | VLOG(verifier) << "Doing fast verification with vdex from DexMetadata archive"; |
| 1363 | } |
| 1364 | } else { |
| 1365 | LOG(INFO) << "Ignoring vdex file in dex metadata due to vdex file already being passed"; |
Nicolas Geoffray | baeaa9b | 2018-01-26 14:31:17 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1369 | // Swap file handling |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1370 | // |
| 1371 | // If the swap fd is not -1, we assume this is the file descriptor of an open but unlinked file |
| 1372 | // that we can use for swap. |
| 1373 | // |
| 1374 | // If the swap fd is -1 and we have a swap-file string, open the given file as a swap file. We |
| 1375 | // will immediately unlink to satisfy the swap fd assumption. |
| 1376 | if (swap_fd_ == -1 && !swap_file_name_.empty()) { |
| 1377 | std::unique_ptr<File> swap_file(OS::CreateEmptyFile(swap_file_name_.c_str())); |
| 1378 | if (swap_file.get() == nullptr) { |
| 1379 | PLOG(ERROR) << "Failed to create swap file: " << swap_file_name_; |
| 1380 | return false; |
| 1381 | } |
Josh Gao | afeec9f | 2018-08-30 14:05:56 -0700 | [diff] [blame] | 1382 | swap_fd_ = swap_file->Release(); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1383 | unlink(swap_file_name_.c_str()); |
| 1384 | } |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1385 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1386 | return true; |
| 1387 | } |
| 1388 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1389 | void EraseOutputFiles() { |
| 1390 | for (auto& files : { &vdex_files_, &oat_files_ }) { |
| 1391 | for (size_t i = 0; i < files->size(); ++i) { |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1392 | auto& file = (*files)[i]; |
| 1393 | if (file != nullptr) { |
| 1394 | if (!file->ReadOnlyMode()) { |
| 1395 | file->Erase(); |
| 1396 | } |
| 1397 | file.reset(); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 1398 | } |
| 1399 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1400 | } |
Andreas Gampe | a650e70 | 2014-12-03 14:28:02 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1403 | void LoadClassProfileDescriptors() { |
Mathieu Chartier | f85b3db | 2018-04-02 18:16:21 -0700 | [diff] [blame] | 1404 | if (!IsImage()) { |
| 1405 | return; |
| 1406 | } |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 1407 | if (DoProfileGuidedOptimizations()) { |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1408 | // TODO: The following comment looks outdated or misplaced. |
Mathieu Chartier | 0b49084 | 2016-05-25 15:05:59 -0700 | [diff] [blame] | 1409 | // Filter out class path classes since we don't want to include these in the image. |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1410 | HashSet<std::string> image_classes = profile_compilation_info_->GetClassDescriptors( |
| 1411 | compiler_options_->dex_files_for_oat_file_); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1412 | VLOG(compiler) << "Loaded " << image_classes.size() |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1413 | << " image class descriptors from profile"; |
| 1414 | if (VLOG_IS_ON(compiler)) { |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1415 | for (const std::string& s : image_classes) { |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1416 | LOG(INFO) << "Image class " << s; |
| 1417 | } |
| 1418 | } |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1419 | compiler_options_->image_classes_.swap(image_classes); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1423 | // Set up the environment for compilation. Includes starting the runtime and loading/opening the |
| 1424 | // boot class path. |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1425 | dex2oat::ReturnCode Setup() { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1426 | TimingLogger::ScopedTiming t("dex2oat Setup", timings_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1427 | |
Mathieu Chartier | f68b698 | 2019-06-27 09:40:20 -0700 | [diff] [blame] | 1428 | if (!PrepareDirtyObjects()) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1429 | return dex2oat::ReturnCode::kOther; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1430 | } |
Brian Carlstrom | d76e083 | 2013-08-29 15:17:42 -0700 | [diff] [blame] | 1431 | |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 1432 | if (!PreparePreloadedClasses()) { |
| 1433 | return dex2oat::ReturnCode::kOther; |
| 1434 | } |
| 1435 | |
Andreas Gampe | 4585f87 | 2015-03-27 23:45:15 -0700 | [diff] [blame] | 1436 | callbacks_.reset(new QuickCompilerCallbacks( |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1437 | // For class verification purposes, boot image extension is the same as boot image. |
| 1438 | (IsBootImage() || IsBootImageExtension()) |
| 1439 | ? CompilerCallbacks::CallbackMode::kCompileBootImage |
| 1440 | : CompilerCallbacks::CallbackMode::kCompileApp)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1441 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1442 | RuntimeArgumentMap runtime_options; |
Mathieu Chartier | 46331a6 | 2017-07-21 09:49:10 -0700 | [diff] [blame] | 1443 | if (!PrepareRuntimeOptions(&runtime_options, callbacks_.get())) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1444 | return dex2oat::ReturnCode::kOther; |
Andreas Gampe | 1d00add | 2015-02-27 19:35:46 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1447 | CreateOatWriters(); |
| 1448 | if (!AddDexFileSources()) { |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1449 | return dex2oat::ReturnCode::kOther; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1452 | { |
| 1453 | TimingLogger::ScopedTiming t_dex("Writing and opening dex files", timings_); |
| 1454 | for (size_t i = 0, size = oat_writers_.size(); i != size; ++i) { |
| 1455 | // Unzip or copy dex files straight to the oat file. |
| 1456 | std::vector<MemMap> opened_dex_files_map; |
| 1457 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 1458 | // No need to verify the dex file when we have a vdex file, which means it was already |
| 1459 | // verified. |
Vladimir Marko | ef46e10 | 2020-02-20 13:44:11 +0000 | [diff] [blame] | 1460 | const bool verify = |
| 1461 | (input_vdex_file_ == nullptr) && !compiler_options_->AssumeDexFilesAreVerified(); |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1462 | if (!oat_writers_[i]->WriteAndOpenDexFiles( |
| 1463 | vdex_files_[i].get(), |
| 1464 | verify, |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1465 | use_existing_vdex_, |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1466 | copy_dex_files_, |
| 1467 | &opened_dex_files_map, |
| 1468 | &opened_dex_files)) { |
| 1469 | return dex2oat::ReturnCode::kOther; |
| 1470 | } |
| 1471 | dex_files_per_oat_file_.push_back(MakeNonOwningPointerVector(opened_dex_files)); |
| 1472 | if (opened_dex_files_map.empty()) { |
| 1473 | DCHECK(opened_dex_files.empty()); |
| 1474 | } else { |
| 1475 | for (MemMap& map : opened_dex_files_map) { |
| 1476 | opened_dex_files_maps_.push_back(std::move(map)); |
| 1477 | } |
| 1478 | for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) { |
Vladimir Marko | 26de896 | 2020-12-22 14:36:47 +0000 | [diff] [blame] | 1479 | dex_file_oat_index_map_.insert(std::make_pair(dex_file.get(), i)); |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1480 | opened_dex_files_.push_back(std::move(dex_file)); |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | compiler_options_->dex_files_for_oat_file_ = MakeNonOwningPointerVector(opened_dex_files_); |
| 1487 | const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_; |
| 1488 | |
Calin Juravle | 92a7857 | 2021-04-05 17:43:17 -0700 | [diff] [blame] | 1489 | if (!ValidateInputVdexChecksums()) { |
| 1490 | return dex2oat::ReturnCode::kOther; |
| 1491 | } |
| 1492 | |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1493 | // Check if we need to downgrade the compiler-filter for size reasons. |
| 1494 | // Note: This does not affect the compiler filter already stored in the key-value |
| 1495 | // store which is used for determining whether the oat file is up to date, |
| 1496 | // together with the boot class path locations and checksums stored below. |
| 1497 | CompilerFilter::Filter original_compiler_filter = compiler_options_->GetCompilerFilter(); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1498 | if (!IsBootImage() && !IsBootImageExtension() && IsVeryLarge(dex_files)) { |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1499 | // Disable app image to make sure dex2oat unloading is enabled. |
| 1500 | compiler_options_->image_type_ = CompilerOptions::ImageType::kNone; |
| 1501 | |
| 1502 | // If we need to downgrade the compiler-filter for size reasons, do that early before we read |
| 1503 | // it below for creating verification callbacks. |
| 1504 | if (!CompilerFilter::IsAsGoodAs(kLargeAppFilter, compiler_options_->GetCompilerFilter())) { |
| 1505 | LOG(INFO) << "Very large app, downgrading to verify."; |
| 1506 | compiler_options_->SetCompilerFilter(kLargeAppFilter); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Nicolas Geoffray | 6cf7a9b | 2020-02-27 21:51:29 +0000 | [diff] [blame] | 1510 | if (CompilerFilter::IsAnyCompilationEnabled(compiler_options_->GetCompilerFilter()) || |
| 1511 | IsImage()) { |
| 1512 | // Only modes with compilation or image generation require verification results. |
Nicolas Geoffray | 9e050ab | 2021-07-14 14:59:25 +0100 | [diff] [blame] | 1513 | verification_results_.reset(new VerificationResults()); |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1514 | callbacks_->SetVerificationResults(verification_results_.get()); |
| 1515 | } |
| 1516 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1517 | if (IsBootImage() || IsBootImageExtension()) { |
| 1518 | // For boot image or boot image extension, pass opened dex files to the Runtime::Create(). |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1519 | // Note: Runtime acquires ownership of these dex files. |
| 1520 | runtime_options.Set(RuntimeArgumentMap::BootClassPathDexList, &opened_dex_files_); |
| 1521 | } |
| 1522 | if (!CreateRuntime(std::move(runtime_options))) { |
| 1523 | return dex2oat::ReturnCode::kCreateRuntime; |
| 1524 | } |
Jiakai Zhang | ed0dcad | 2022-01-21 15:52:32 +0000 | [diff] [blame] | 1525 | if (runtime_->GetHeap()->GetBootImageSpaces().empty() && |
| 1526 | (IsBootImageExtension() || IsAppImage())) { |
Nicolas Geoffray | fc1ba6d | 2022-03-23 14:18:23 +0000 | [diff] [blame] | 1527 | LOG(WARNING) << "Cannot create " |
| 1528 | << (IsBootImageExtension() ? "boot image extension" : "app image") |
| 1529 | << " without a primary boot image."; |
| 1530 | compiler_options_->image_type_ = CompilerOptions::ImageType::kNone; |
Jiakai Zhang | ed0dcad | 2022-01-21 15:52:32 +0000 | [diff] [blame] | 1531 | } |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1532 | ArrayRef<const DexFile* const> bcp_dex_files(runtime_->GetClassLinker()->GetBootClassPath()); |
| 1533 | if (IsBootImage() || IsBootImageExtension()) { |
| 1534 | // Check boot class path dex files and, if compiling an extension, the images it depends on. |
| 1535 | if ((IsBootImage() && bcp_dex_files.size() != dex_files.size()) || |
| 1536 | (IsBootImageExtension() && bcp_dex_files.size() <= dex_files.size())) { |
| 1537 | LOG(ERROR) << "Unexpected number of boot class path dex files for boot image or extension, " |
| 1538 | << bcp_dex_files.size() << (IsBootImage() ? " != " : " <= ") << dex_files.size(); |
| 1539 | return dex2oat::ReturnCode::kOther; |
| 1540 | } |
| 1541 | if (!std::equal(dex_files.begin(), dex_files.end(), bcp_dex_files.end() - dex_files.size())) { |
| 1542 | LOG(ERROR) << "Boot class path dex files do not end with the compiled dex files."; |
| 1543 | return dex2oat::ReturnCode::kOther; |
| 1544 | } |
| 1545 | size_t bcp_df_pos = 0u; |
| 1546 | size_t bcp_df_end = bcp_dex_files.size(); |
| 1547 | for (const std::string& bcp_location : runtime_->GetBootClassPathLocations()) { |
| 1548 | if (bcp_df_pos == bcp_df_end || bcp_dex_files[bcp_df_pos]->GetLocation() != bcp_location) { |
| 1549 | LOG(ERROR) << "Missing dex file for boot class component " << bcp_location; |
| 1550 | return dex2oat::ReturnCode::kOther; |
| 1551 | } |
| 1552 | CHECK(!DexFileLoader::IsMultiDexLocation(bcp_dex_files[bcp_df_pos]->GetLocation().c_str())); |
| 1553 | ++bcp_df_pos; |
| 1554 | while (bcp_df_pos != bcp_df_end && |
| 1555 | DexFileLoader::IsMultiDexLocation(bcp_dex_files[bcp_df_pos]->GetLocation().c_str())) { |
| 1556 | ++bcp_df_pos; |
| 1557 | } |
| 1558 | } |
| 1559 | if (bcp_df_pos != bcp_df_end) { |
| 1560 | LOG(ERROR) << "Unexpected dex file in boot class path " |
| 1561 | << bcp_dex_files[bcp_df_pos]->GetLocation(); |
| 1562 | return dex2oat::ReturnCode::kOther; |
| 1563 | } |
| 1564 | auto lacks_image = [](const DexFile* df) { |
| 1565 | if (kIsDebugBuild && df->GetOatDexFile() != nullptr) { |
| 1566 | const OatFile* oat_file = df->GetOatDexFile()->GetOatFile(); |
| 1567 | CHECK(oat_file != nullptr); |
| 1568 | const auto& image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 1569 | CHECK(std::any_of(image_spaces.begin(), |
| 1570 | image_spaces.end(), |
| 1571 | [=](const ImageSpace* space) { |
| 1572 | return oat_file == space->GetOatFile(); |
| 1573 | })); |
| 1574 | } |
| 1575 | return df->GetOatDexFile() == nullptr; |
| 1576 | }; |
| 1577 | if (std::any_of(bcp_dex_files.begin(), bcp_dex_files.end() - dex_files.size(), lacks_image)) { |
| 1578 | LOG(ERROR) << "Missing required boot image(s) for boot image extension."; |
| 1579 | return dex2oat::ReturnCode::kOther; |
| 1580 | } |
| 1581 | } |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1582 | |
Calin Juravle | 0e09dfc | 2018-02-12 19:01:09 -0800 | [diff] [blame] | 1583 | if (!compilation_reason_.empty()) { |
| 1584 | key_value_store_->Put(OatHeader::kCompilationReasonKey, compilation_reason_); |
| 1585 | } |
| 1586 | |
Jiakai Zhang | 57a45be | 2022-02-07 16:12:57 +0000 | [diff] [blame] | 1587 | Runtime* runtime = Runtime::Current(); |
| 1588 | |
Vladimir Marko | c10a0c6 | 2018-11-16 11:39:22 +0000 | [diff] [blame] | 1589 | if (IsBootImage()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1590 | // If we're compiling the boot image, store the boot classpath into the Key-Value store. |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1591 | // We use this when loading the boot image. |
| 1592 | key_value_store_->Put(OatHeader::kBootClassPathKey, android::base::Join(dex_locations_, ':')); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1593 | } else if (IsBootImageExtension()) { |
| 1594 | // Validate the boot class path and record the dependency on the loaded boot images. |
| 1595 | TimingLogger::ScopedTiming t3("Loading image checksum", timings_); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1596 | std::string full_bcp = android::base::Join(runtime->GetBootClassPathLocations(), ':'); |
| 1597 | std::string extension_part = ":" + android::base::Join(dex_locations_, ':'); |
| 1598 | if (!android::base::EndsWith(full_bcp, extension_part)) { |
| 1599 | LOG(ERROR) << "Full boot class path does not end with extension parts, full: " << full_bcp |
| 1600 | << ", extension: " << extension_part.substr(1u); |
| 1601 | return dex2oat::ReturnCode::kOther; |
| 1602 | } |
| 1603 | std::string bcp_dependency = full_bcp.substr(0u, full_bcp.size() - extension_part.size()); |
| 1604 | key_value_store_->Put(OatHeader::kBootClassPathKey, bcp_dependency); |
| 1605 | ArrayRef<const DexFile* const> bcp_dex_files_dependency = |
| 1606 | bcp_dex_files.SubArray(/*pos=*/ 0u, bcp_dex_files.size() - dex_files.size()); |
| 1607 | ArrayRef<ImageSpace* const> image_spaces(runtime->GetHeap()->GetBootImageSpaces()); |
| 1608 | key_value_store_->Put( |
| 1609 | OatHeader::kBootClassPathChecksumsKey, |
| 1610 | gc::space::ImageSpace::GetBootClassPathChecksums(image_spaces, bcp_dex_files_dependency)); |
| 1611 | } else { |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 1612 | if (CompilerFilter::DependsOnImageChecksum(original_compiler_filter)) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1613 | TimingLogger::ScopedTiming t3("Loading image checksum", timings_); |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1614 | key_value_store_->Put(OatHeader::kBootClassPathKey, |
| 1615 | android::base::Join(runtime->GetBootClassPathLocations(), ':')); |
Vladimir Marko | 436c6f5 | 2019-07-25 14:50:14 +0100 | [diff] [blame] | 1616 | ArrayRef<ImageSpace* const> image_spaces(runtime->GetHeap()->GetBootImageSpaces()); |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1617 | key_value_store_->Put( |
| 1618 | OatHeader::kBootClassPathChecksumsKey, |
| 1619 | gc::space::ImageSpace::GetBootClassPathChecksums(image_spaces, bcp_dex_files)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | // Open dex files for class path. |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 1623 | |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1624 | if (class_loader_context_ == nullptr) { |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 1625 | // If no context was specified use the default one (which is an empty PathClassLoader). |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1626 | class_loader_context_ = ClassLoaderContext::Default(); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 1627 | } |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 1628 | |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1629 | DCHECK_EQ(oat_writers_.size(), 1u); |
| 1630 | |
| 1631 | // Note: Ideally we would reject context where the source dex files are also |
| 1632 | // specified in the classpath (as it doesn't make sense). However this is currently |
| 1633 | // needed for non-prebuild tests and benchmarks which expects on the fly compilation. |
| 1634 | // Also, for secondary dex files we do not have control on the actual classpath. |
| 1635 | // Instead of aborting, remove all the source location from the context classpaths. |
| 1636 | if (class_loader_context_->RemoveLocationsFromClassPaths( |
| 1637 | oat_writers_[0]->GetSourceLocations())) { |
| 1638 | LOG(WARNING) << "The source files to be compiled are also in the classpath."; |
| 1639 | } |
| 1640 | |
| 1641 | // We need to open the dex files before encoding the context in the oat file. |
| 1642 | // (because the encoding adds the dex checksum...) |
| 1643 | // TODO(calin): consider redesigning this so we don't have to open the dex files before |
| 1644 | // creating the actual class loader. |
Calin Juravle | 5ff2393 | 2020-12-11 18:26:14 -0800 | [diff] [blame] | 1645 | if (!class_loader_context_->OpenDexFiles(classpath_dir_, |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 1646 | class_loader_context_fds_)) { |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1647 | // Do not abort if we couldn't open files from the classpath. They might be |
| 1648 | // apks without dex files and right now are opening flow will fail them. |
| 1649 | LOG(WARNING) << "Failed to open classpath dex files"; |
| 1650 | } |
| 1651 | |
| 1652 | // Store the class loader context in the oat header. |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1653 | // TODO: deprecate this since store_class_loader_context should be enough to cover the users |
| 1654 | // of classpath_dir as well. |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 1655 | std::string class_path_key = |
| 1656 | class_loader_context_->EncodeContextForOatFile(classpath_dir_, |
| 1657 | stored_class_loader_context_.get()); |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1658 | key_value_store_->Put(OatHeader::kClassPathKey, class_path_key); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Jiakai Zhang | 57a45be | 2022-02-07 16:12:57 +0000 | [diff] [blame] | 1661 | if (IsBootImage() || |
| 1662 | IsBootImageExtension() || |
| 1663 | CompilerFilter::DependsOnImageChecksum(original_compiler_filter)) { |
| 1664 | std::string versions = |
| 1665 | apex_versions_argument_.empty() ? runtime->GetApexVersions() : apex_versions_argument_; |
| 1666 | key_value_store_->Put(OatHeader::kApexVersionsKey, versions); |
| 1667 | } |
| 1668 | |
Nicolas Geoffray | fc1ba6d | 2022-03-23 14:18:23 +0000 | [diff] [blame] | 1669 | // Now that we have adjusted whether we generate an image, encode it in the |
| 1670 | // key/value store. |
| 1671 | key_value_store_->Put(OatHeader::kRequiresImage, compiler_options_->IsGeneratingImage()); |
| 1672 | |
Vladimir Marko | 4e6abea | 2019-10-23 10:20:20 +0100 | [diff] [blame] | 1673 | // Now that we have finalized key_value_store_, start writing the .rodata section. |
| 1674 | // Among other things, this creates type lookup tables that speed up the compilation. |
| 1675 | { |
| 1676 | TimingLogger::ScopedTiming t_dex("Starting .rodata", timings_); |
| 1677 | rodata_.reserve(oat_writers_.size()); |
| 1678 | for (size_t i = 0, size = oat_writers_.size(); i != size; ++i) { |
| 1679 | rodata_.push_back(elf_writers_[i]->StartRoData()); |
| 1680 | if (!oat_writers_[i]->StartRoData(dex_files_per_oat_file_[i], |
| 1681 | rodata_.back(), |
| 1682 | (i == 0u) ? key_value_store_.get() : nullptr)) { |
| 1683 | return dex2oat::ReturnCode::kOther; |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | |
Andreas Gampe | 7386da4 | 2016-06-08 18:18:47 -0700 | [diff] [blame] | 1688 | // We had to postpone the swap decision till now, as this is the point when we actually |
| 1689 | // know about the dex files we're going to use. |
| 1690 | |
| 1691 | // Make sure that we didn't create the driver, yet. |
| 1692 | CHECK(driver_ == nullptr); |
| 1693 | // If we use a swap file, ensure we are above the threshold to make it necessary. |
| 1694 | if (swap_fd_ != -1) { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1695 | if (!UseSwap(IsBootImage() || IsBootImageExtension(), dex_files)) { |
Andreas Gampe | 7386da4 | 2016-06-08 18:18:47 -0700 | [diff] [blame] | 1696 | close(swap_fd_); |
| 1697 | swap_fd_ = -1; |
| 1698 | VLOG(compiler) << "Decided to run without swap."; |
| 1699 | } else { |
| 1700 | LOG(INFO) << "Large app, accepted running with swap."; |
| 1701 | } |
| 1702 | } |
| 1703 | // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1704 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1705 | if (!IsBootImage() && !IsBootImageExtension()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1706 | constexpr bool kSaveDexInput = false; |
| 1707 | if (kSaveDexInput) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 1708 | SaveDexInput(); |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1709 | } |
| 1710 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 1711 | |
Vladimir Marko | c390879 | 2020-04-06 14:52:04 +0100 | [diff] [blame] | 1712 | // Setup VerifierDeps for compilation and report if we fail to parse the data. |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1713 | // When we do profile guided optimizations, the compiler currently needs to run |
| 1714 | // full verification. |
| 1715 | if (!DoProfileGuidedOptimizations() && input_vdex_file_ != nullptr) { |
Vladimir Marko | c390879 | 2020-04-06 14:52:04 +0100 | [diff] [blame] | 1716 | std::unique_ptr<verifier::VerifierDeps> verifier_deps( |
| 1717 | new verifier::VerifierDeps(dex_files, /*output_only=*/ false)); |
| 1718 | if (!verifier_deps->ParseStoredData(dex_files, input_vdex_file_->GetVerifierDepsData())) { |
| 1719 | return dex2oat::ReturnCode::kOther; |
| 1720 | } |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1721 | // We can do fast verification. |
Vladimir Marko | c390879 | 2020-04-06 14:52:04 +0100 | [diff] [blame] | 1722 | callbacks_->SetVerifierDeps(verifier_deps.release()); |
| 1723 | } else { |
| 1724 | // Create the main VerifierDeps, here instead of in the compiler since we want to aggregate |
| 1725 | // the results for all the dex files, not just the results for the current dex file. |
| 1726 | callbacks_->SetVerifierDeps(new verifier::VerifierDeps(dex_files)); |
| 1727 | } |
| 1728 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 1729 | return dex2oat::ReturnCode::kNoFailure; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1730 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1731 | |
Calin Juravle | 92a7857 | 2021-04-05 17:43:17 -0700 | [diff] [blame] | 1732 | // Validates that the input vdex checksums match the source dex checksums. |
| 1733 | // Note that this is only effective and relevant if the input_vdex_file does not |
| 1734 | // contain a dex section (e.g. when they come from .dm files). |
| 1735 | // If the input vdex does contain dex files, the dex files will be opened from there |
| 1736 | // and so this check is redundant. |
| 1737 | bool ValidateInputVdexChecksums() { |
| 1738 | if (input_vdex_file_ == nullptr) { |
| 1739 | // Nothing to validate |
| 1740 | return true; |
| 1741 | } |
| 1742 | if (input_vdex_file_->GetNumberOfDexFiles() |
| 1743 | != compiler_options_->dex_files_for_oat_file_.size()) { |
| 1744 | LOG(ERROR) << "Vdex file contains a different number of dex files than the source. " |
| 1745 | << " vdex_num=" << input_vdex_file_->GetNumberOfDexFiles() |
| 1746 | << " dex_source_num=" << compiler_options_->dex_files_for_oat_file_.size(); |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
| 1750 | for (size_t i = 0; i < compiler_options_->dex_files_for_oat_file_.size(); i++) { |
| 1751 | uint32_t dex_source_checksum = |
| 1752 | compiler_options_->dex_files_for_oat_file_[i]->GetLocationChecksum(); |
| 1753 | uint32_t vdex_checksum = input_vdex_file_->GetLocationChecksum(i); |
| 1754 | if (dex_source_checksum != vdex_checksum) { |
| 1755 | LOG(ERROR) << "Vdex file checksum different than source dex checksum for position " << i |
| 1756 | << std::hex |
| 1757 | << " vdex_checksum=0x" << vdex_checksum |
| 1758 | << " dex_source_checksum=0x" << dex_source_checksum |
| 1759 | << std::dec; |
| 1760 | return false; |
| 1761 | } |
| 1762 | } |
| 1763 | return true; |
| 1764 | } |
| 1765 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1766 | // If we need to keep the oat file open for the image writer. |
| 1767 | bool ShouldKeepOatFileOpen() const { |
Orion Hodson | 365f94f | 2021-01-13 16:27:57 +0000 | [diff] [blame] | 1768 | return IsImage() && oat_fd_ != File::kInvalidFd; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1769 | } |
| 1770 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1771 | // Doesn't return the class loader since it's not meant to be used for image compilation. |
| 1772 | void CompileDexFilesIndividually() { |
| 1773 | CHECK(!IsImage()) << "Not supported with image"; |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1774 | for (const DexFile* dex_file : compiler_options_->dex_files_for_oat_file_) { |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1775 | std::vector<const DexFile*> dex_files(1u, dex_file); |
| 1776 | VLOG(compiler) << "Compiling " << dex_file->GetLocation(); |
| 1777 | jobject class_loader = CompileDexFiles(dex_files); |
| 1778 | CHECK(class_loader != nullptr); |
| 1779 | ScopedObjectAccess soa(Thread::Current()); |
| 1780 | // Unload class loader to free RAM. |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 1781 | jweak weak_class_loader = soa.Env()->GetVm()->AddWeakGlobalRef( |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1782 | soa.Self(), |
| 1783 | soa.Decode<mirror::ClassLoader>(class_loader)); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 1784 | soa.Env()->GetVm()->DeleteGlobalRef(soa.Self(), class_loader); |
Roland Levillain | af29031 | 2018-02-27 20:02:17 +0000 | [diff] [blame] | 1785 | runtime_->GetHeap()->CollectGarbage(/* clear_soft_references */ true); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1786 | ObjPtr<mirror::ClassLoader> decoded_weak = soa.Decode<mirror::ClassLoader>(weak_class_loader); |
| 1787 | if (decoded_weak != nullptr) { |
| 1788 | LOG(FATAL) << "Failed to unload class loader, path from root set: " |
| 1789 | << runtime_->GetHeap()->GetVerification()->FirstPathFromRootSet(decoded_weak); |
| 1790 | } |
| 1791 | VLOG(compiler) << "Unloaded classloader"; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | bool ShouldCompileDexFilesIndividually() const { |
Calin Juravle | c893d15 | 2021-06-17 15:40:52 -0700 | [diff] [blame] | 1796 | // Compile individually if we are allowed to, and |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 1797 | // 1. not building an image, and |
| 1798 | // 2. not verifying a vdex file, and |
| 1799 | // 3. using multidex, and |
Nicolas Geoffray | a6fc787 | 2017-08-14 11:29:22 +0100 | [diff] [blame] | 1800 | // 4. not doing any AOT compilation. |
| 1801 | // This means extract, no-vdex verify, and quicken, will use the individual compilation |
| 1802 | // mode (to reduce RAM used by the compiler). |
Calin Juravle | c893d15 | 2021-06-17 15:40:52 -0700 | [diff] [blame] | 1803 | return compile_individually_ && |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 1804 | (!IsImage() && !use_existing_vdex_ && |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 1805 | compiler_options_->dex_files_for_oat_file_.size() > 1 && |
| 1806 | !CompilerFilter::IsAotCompilationEnabled(compiler_options_->GetCompilerFilter())); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Vladimir Marko | e1ee6f9 | 2020-03-13 11:30:59 +0000 | [diff] [blame] | 1809 | uint32_t GetCombinedChecksums() const { |
| 1810 | uint32_t combined_checksums = 0u; |
| 1811 | for (const DexFile* dex_file : compiler_options_->GetDexFilesForOatFile()) { |
| 1812 | combined_checksums ^= dex_file->GetLocationChecksum(); |
| 1813 | } |
| 1814 | return combined_checksums; |
| 1815 | } |
| 1816 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1817 | // Set up and create the compiler driver and then invoke it to compile all the dex files. |
| 1818 | jobject Compile() { |
| 1819 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 1820 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1821 | TimingLogger::ScopedTiming t("dex2oat Compile", timings_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1822 | |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1823 | // Find the dex files we should not inline from. |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1824 | std::vector<std::string> no_inline_filters; |
| 1825 | Split(no_inline_from_string_, ',', &no_inline_filters); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1826 | |
| 1827 | // For now, on the host always have core-oj removed. |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1828 | const std::string core_oj = "core-oj"; |
| 1829 | if (!kIsTargetBuild && !ContainsElement(no_inline_filters, core_oj)) { |
Alex Light | 4531afd | 2021-03-30 17:13:30 -0700 | [diff] [blame] | 1830 | if (force_allow_oj_inlines_) { |
| 1831 | LOG(ERROR) << "Inlines allowed from core-oj! FOR TESTING USE ONLY! DO NOT DISTRIBUTE" |
| 1832 | << " BINARIES BUILT WITH THIS OPTION!"; |
| 1833 | } else { |
| 1834 | no_inline_filters.push_back(core_oj); |
| 1835 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1838 | if (!no_inline_filters.empty()) { |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1839 | std::vector<const DexFile*> class_path_files; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1840 | if (!IsBootImage() && !IsBootImageExtension()) { |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 1841 | // The class loader context is used only for apps. |
| 1842 | class_path_files = class_loader_context_->FlattenOpenedDexFiles(); |
| 1843 | } |
| 1844 | |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1845 | const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_; |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1846 | std::vector<const DexFile*> no_inline_from_dex_files; |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1847 | const std::vector<const DexFile*>* dex_file_vectors[] = { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1848 | &class_linker->GetBootClassPath(), |
| 1849 | &class_path_files, |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1850 | &dex_files |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1851 | }; |
| 1852 | for (const std::vector<const DexFile*>* dex_file_vector : dex_file_vectors) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1853 | for (const DexFile* dex_file : *dex_file_vector) { |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1854 | for (const std::string& filter : no_inline_filters) { |
| 1855 | // Use dex_file->GetLocation() rather than dex_file->GetBaseLocation(). This |
Calin Juravle | a308a32 | 2017-07-18 16:51:51 -0700 | [diff] [blame] | 1856 | // allows tests to specify <test-dexfile>!classes2.dex if needed but if the |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1857 | // base location passes the StartsWith() test, so do all extra locations. |
| 1858 | std::string dex_location = dex_file->GetLocation(); |
| 1859 | if (filter.find('/') == std::string::npos) { |
| 1860 | // The filter does not contain the path. Remove the path from dex_location as well. |
| 1861 | size_t last_slash = dex_file->GetLocation().rfind('/'); |
| 1862 | if (last_slash != std::string::npos) { |
| 1863 | dex_location = dex_location.substr(last_slash + 1); |
| 1864 | } |
| 1865 | } |
| 1866 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 1867 | if (android::base::StartsWith(dex_location, filter.c_str())) { |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1868 | VLOG(compiler) << "Disabling inlining from " << dex_file->GetLocation(); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1869 | no_inline_from_dex_files.push_back(dex_file); |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1870 | break; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1871 | } |
| 1872 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1873 | } |
Vladimir Marko | 47496c2 | 2016-01-27 16:15:08 +0000 | [diff] [blame] | 1874 | } |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 1875 | if (!no_inline_from_dex_files.empty()) { |
| 1876 | compiler_options_->no_inline_from_.swap(no_inline_from_dex_files); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1877 | } |
| 1878 | } |
Vladimir Marko | 1a2a5cd | 2018-11-07 15:39:48 +0000 | [diff] [blame] | 1879 | compiler_options_->profile_compilation_info_ = profile_compilation_info_.get(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1880 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1881 | driver_.reset(new CompilerDriver(compiler_options_.get(), |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1882 | compiler_kind_, |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 1883 | thread_count_, |
Vladimir Marko | 1a2a5cd | 2018-11-07 15:39:48 +0000 | [diff] [blame] | 1884 | swap_fd_)); |
wulonghua | bdf146c | 2020-03-27 11:15:42 +0800 | [diff] [blame] | 1885 | |
| 1886 | driver_->PrepareDexFilesForOatFile(timings_); |
| 1887 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1888 | if (!IsBootImage() && !IsBootImageExtension()) { |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 1889 | driver_->SetClasspathDexFiles(class_loader_context_->FlattenOpenedDexFiles()); |
| 1890 | } |
Nicolas Geoffray | 1cfea7a | 2017-05-24 14:44:38 +0100 | [diff] [blame] | 1891 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 1892 | const bool compile_individually = ShouldCompileDexFilesIndividually(); |
| 1893 | if (compile_individually) { |
| 1894 | // Set the compiler driver in the callbacks so that we can avoid re-verification. This not |
| 1895 | // only helps performance but also prevents reverifying quickened bytecodes. Attempting |
| 1896 | // verify quickened bytecode causes verification failures. |
| 1897 | // Only set the compiler filter if we are doing separate compilation since there is a bit |
| 1898 | // of overhead when checking if a class was previously verified. |
| 1899 | callbacks_->SetDoesClassUnloading(true, driver_.get()); |
| 1900 | } |
| 1901 | |
Nicolas Geoffray | 1cfea7a | 2017-05-24 14:44:38 +0100 | [diff] [blame] | 1902 | // Setup vdex for compilation. |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1903 | const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_; |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 1904 | // To allow initialization of classes that construct ThreadLocal objects in class initializer, |
| 1905 | // re-initialize the ThreadLocal.nextHashCode to a new object that's not in the boot image. |
| 1906 | ThreadLocalHashOverride thread_local_hash_override( |
Vladimir Marko | e1ee6f9 | 2020-03-13 11:30:59 +0000 | [diff] [blame] | 1907 | /*apply=*/ !IsBootImage(), /*initial_value=*/ 123456789u ^ GetCombinedChecksums()); |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 1908 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1909 | // Invoke the compilation. |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 1910 | if (compile_individually) { |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1911 | CompileDexFilesIndividually(); |
| 1912 | // Return a null classloader since we already freed released it. |
| 1913 | return nullptr; |
| 1914 | } |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1915 | return CompileDexFiles(dex_files); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | // Create the class loader, use it to compile, and return. |
| 1919 | jobject CompileDexFiles(const std::vector<const DexFile*>& dex_files) { |
| 1920 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 1921 | |
| 1922 | jobject class_loader = nullptr; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 1923 | if (!IsBootImage() && !IsBootImageExtension()) { |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 1924 | class_loader = |
Vladimir Marko | 37d6d77 | 2020-03-05 17:13:28 +0000 | [diff] [blame] | 1925 | class_loader_context_->CreateClassLoader(compiler_options_->GetDexFilesForOatFile()); |
Vladimir Marko | a879bdf | 2019-11-19 16:25:47 +0000 | [diff] [blame] | 1926 | } |
| 1927 | if (!IsBootImage()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 1928 | callbacks_->SetDexFiles(&dex_files); |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 1929 | |
| 1930 | // We need to set this after we create the class loader so that the runtime can access |
| 1931 | // the hidden fields of the well known class loaders. |
| 1932 | if (!public_sdk_.empty()) { |
| 1933 | std::string error_msg; |
| 1934 | std::unique_ptr<SdkChecker> sdk_checker(SdkChecker::Create(public_sdk_, &error_msg)); |
| 1935 | if (sdk_checker != nullptr) { |
| 1936 | AotClassLinker* aot_class_linker = down_cast<AotClassLinker*>(class_linker); |
| 1937 | aot_class_linker->SetSdkChecker(std::move(sdk_checker)); |
| 1938 | } else { |
| 1939 | LOG(FATAL) << "Failed to create SdkChecker with dex files " |
| 1940 | << public_sdk_ << " Error: " << error_msg; |
| 1941 | UNREACHABLE(); |
| 1942 | } |
| 1943 | } |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | // Register dex caches and key them to the class loader so that they only unload when the |
| 1947 | // class loader unloads. |
| 1948 | for (const auto& dex_file : dex_files) { |
| 1949 | ScopedObjectAccess soa(Thread::Current()); |
| 1950 | // Registering the dex cache adds a strong root in the class loader that prevents the dex |
| 1951 | // cache from being unloaded early. |
| 1952 | ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile( |
| 1953 | *dex_file, |
| 1954 | soa.Decode<mirror::ClassLoader>(class_loader)); |
| 1955 | if (dex_cache == nullptr) { |
| 1956 | soa.Self()->AssertPendingException(); |
| 1957 | LOG(FATAL) << "Failed to register dex file " << dex_file->GetLocation() << " " |
| 1958 | << soa.Self()->GetException()->Dump(); |
| 1959 | } |
| 1960 | } |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 1961 | driver_->InitializeThreadPools(); |
| 1962 | driver_->PreCompile(class_loader, |
| 1963 | dex_files, |
| 1964 | timings_, |
Nicolas Geoffray | 9e050ab | 2021-07-14 14:59:25 +0100 | [diff] [blame] | 1965 | &compiler_options_->image_classes_); |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 1966 | callbacks_->SetVerificationResults(nullptr); // Should not be needed anymore. |
| 1967 | compiler_options_->verification_results_ = verification_results_.get(); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1968 | driver_->CompileAll(class_loader, dex_files, timings_); |
Vladimir Marko | 2afaff7 | 2018-11-30 17:01:50 +0000 | [diff] [blame] | 1969 | driver_->FreeThreadPools(); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1970 | return class_loader; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1971 | } |
| 1972 | |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1973 | // Notes on the interleaving of creating the images and oat files to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1974 | // ensure the references between the two are correct. |
| 1975 | // |
| 1976 | // Currently we have a memory layout that looks something like this: |
| 1977 | // |
| 1978 | // +--------------+ |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1979 | // | images | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1980 | // +--------------+ |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1981 | // | oat files | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1982 | // +--------------+ |
| 1983 | // | alloc spaces | |
| 1984 | // +--------------+ |
| 1985 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1986 | // There are several constraints on the loading of the images and oat files. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1987 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1988 | // 1. The images are expected to be loaded at an absolute address and |
| 1989 | // contain Objects with absolute pointers within the images. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1990 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1991 | // 2. There are absolute pointers from Methods in the images to their |
| 1992 | // code in the oat files. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1993 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1994 | // 3. There are absolute pointers from the code in the oat files to Methods |
| 1995 | // in the images. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1996 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 1997 | // 4. There are absolute pointers from code in the oat files to other code |
| 1998 | // in the oat files. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1999 | // |
| 2000 | // To get this all correct, we go through several steps. |
| 2001 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 2002 | // 1. We prepare offsets for all data in the oat files and calculate |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2003 | // the oat data size and code size. During this stage, we also set |
| 2004 | // oat code offsets in methods for use by the image writer. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2005 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 2006 | // 2. We prepare offsets for the objects in the images and calculate |
| 2007 | // the image sizes. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2008 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 2009 | // 3. We create the oat files. Originally this was just our own proprietary |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2010 | // file but now it is contained within an ELF dynamic object (aka an .so |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 2011 | // file). Since we know the image sizes and oat data sizes and code sizes we |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2012 | // can prepare the ELF headers and we then know the ELF memory segment |
| 2013 | // layout and we can now resolve all references. The compiler provides |
| 2014 | // LinkerPatch information in each CompiledMethod and we resolve these, |
| 2015 | // using the layout information and image object locations provided by |
| 2016 | // image writer, as we're writing the method code. |
| 2017 | // |
Jeff Hao | 436183f | 2016-01-12 16:36:50 -0800 | [diff] [blame] | 2018 | // 4. We create the image files. They need to know where the oat files |
| 2019 | // will be loaded after itself. Originally oat files were simply |
| 2020 | // memory mapped so we could predict where their contents were based |
| 2021 | // on the file size. Now that they are ELF files, we need to inspect |
| 2022 | // the ELF files to understand the in memory segment layout including |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2023 | // where the oat header is located within. |
| 2024 | // TODO: We could just remember this information from step 3. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2025 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2026 | // 5. We fixup the ELF program headers so that dlopen will try to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2027 | // load the .so at the desired location at runtime by offsetting the |
| 2028 | // Elf32_Phdr.p_vaddr values by the desired base address. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2029 | // TODO: Do this in step 3. We already know the layout there. |
| 2030 | // |
| 2031 | // Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5. |
| 2032 | // are done by the CreateImageFile() below. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2033 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2034 | // Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the |
| 2035 | // ImageWriter, if necessary. |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2036 | // Note: Flushing (and closing) the file is the caller's responsibility, except for the failure |
| 2037 | // case (when the file will be explicitly erased). |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2038 | bool WriteOutputFiles(jobject class_loader) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2039 | TimingLogger::ScopedTiming t("dex2oat Oat", timings_); |
| 2040 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2041 | // Sync the data to the file, in case we did dex2dex transformations. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 2042 | for (MemMap& map : opened_dex_files_maps_) { |
| 2043 | if (!map.Sync()) { |
| 2044 | PLOG(ERROR) << "Failed to Sync() dex2dex output. Map: " << map.GetName(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2045 | return false; |
| 2046 | } |
| 2047 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2048 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2049 | if (IsImage()) { |
Vladimir Marko | 6623594 | 2019-06-03 16:05:17 +0100 | [diff] [blame] | 2050 | if (!IsBootImage()) { |
| 2051 | DCHECK_EQ(image_base_, 0u); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2052 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Vladimir Marko | 6623594 | 2019-06-03 16:05:17 +0100 | [diff] [blame] | 2053 | image_base_ = heap->GetBootImagesStartAddress() + heap->GetBootImagesSize(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2054 | } |
Vladimir Marko | 6623594 | 2019-06-03 16:05:17 +0100 | [diff] [blame] | 2055 | VLOG(compiler) << "Image base=" << reinterpret_cast<void*>(image_base_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2056 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 2057 | image_writer_.reset(new linker::ImageWriter(*compiler_options_, |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2058 | image_base_, |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2059 | image_storage_mode_, |
| 2060 | oat_filenames_, |
| 2061 | dex_file_oat_index_map_, |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2062 | class_loader, |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2063 | dirty_image_objects_.get())); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2064 | |
Vladimir Marko | 4b90b86 | 2020-03-13 17:19:06 +0000 | [diff] [blame] | 2065 | // We need to prepare method offsets in the image address space for resolving linker patches. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2066 | TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2067 | if (!image_writer_->PrepareImageAddressSpace(timings_)) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2068 | LOG(ERROR) << "Failed to prepare image address space."; |
| 2069 | return false; |
| 2070 | } |
| 2071 | } |
| 2072 | |
Vladimir Marko | 4e6abea | 2019-10-23 10:20:20 +0100 | [diff] [blame] | 2073 | // Initialize the writers with the compiler driver, image writer, and their |
| 2074 | // dex files. The writers were created without those being there yet. |
| 2075 | for (size_t i = 0, size = oat_files_.size(); i != size; ++i) { |
| 2076 | std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i]; |
| 2077 | std::vector<const DexFile*>& dex_files = dex_files_per_oat_file_[i]; |
| 2078 | oat_writer->Initialize(driver_.get(), image_writer_.get(), dex_files); |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2079 | } |
| 2080 | |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 2081 | if (!use_existing_vdex_) { |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2082 | TimingLogger::ScopedTiming t2("dex2oat Write VDEX", timings_); |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 2083 | DCHECK(IsBootImage() || IsBootImageExtension() || oat_files_.size() == 1u); |
Nicolas Geoffray | 6bb7f1b | 2016-11-03 10:52:49 +0000 | [diff] [blame] | 2084 | verifier::VerifierDeps* verifier_deps = callbacks_->GetVerifierDeps(); |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2085 | for (size_t i = 0, size = oat_files_.size(); i != size; ++i) { |
| 2086 | File* vdex_file = vdex_files_[i].get(); |
Vladimir Marko | 9e4b42a | 2020-04-28 12:28:21 +0100 | [diff] [blame] | 2087 | if (!oat_writers_[i]->FinishVdexFile(vdex_file, verifier_deps)) { |
| 2088 | LOG(ERROR) << "Failed to finish VDEX file " << vdex_file->GetPath(); |
David Brazdil | 5d5a36b | 2016-09-14 15:34:10 +0100 | [diff] [blame] | 2089 | return false; |
| 2090 | } |
| 2091 | } |
| 2092 | } |
| 2093 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2094 | { |
| 2095 | TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 2096 | linker::MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(), |
| 2097 | compiler_options_->GetInstructionSetFeatures(), |
Vladimir Marko | ca1e038 | 2018-04-11 09:58:41 +0000 | [diff] [blame] | 2098 | driver_->GetCompiledMethodStorage()); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2099 | for (size_t i = 0, size = oat_files_.size(); i != size; ++i) { |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2100 | std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i]; |
| 2101 | std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i]; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2102 | |
Nicolas Geoffray | 4acefd3 | 2016-10-24 13:14:58 +0100 | [diff] [blame] | 2103 | oat_writer->PrepareLayout(&patcher); |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 2104 | elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(), |
| 2105 | oat_writer->GetCodeSize(), |
| 2106 | oat_writer->GetDataBimgRelRoSize(), |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2107 | oat_writer->GetBssSize(), |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 2108 | oat_writer->GetBssMethodsOffset(), |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 2109 | oat_writer->GetBssRootsOffset(), |
| 2110 | oat_writer->GetVdexSize()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2111 | if (IsImage()) { |
| 2112 | // Update oat layout. |
| 2113 | DCHECK(image_writer_ != nullptr); |
| 2114 | DCHECK_LT(i, oat_filenames_.size()); |
| 2115 | image_writer_->UpdateOatFileLayout(i, |
| 2116 | elf_writer->GetLoadedSize(), |
| 2117 | oat_writer->GetOatDataOffset(), |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2118 | oat_writer->GetOatSize()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | for (size_t i = 0, size = oat_files_.size(); i != size; ++i) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2123 | std::unique_ptr<File>& oat_file = oat_files_[i]; |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2124 | std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i]; |
| 2125 | std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i]; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 2126 | |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 2127 | // We need to mirror the layout of the ELF file in the compressed debug-info. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2128 | // Therefore PrepareDebugInfo() relies on the SetLoadedSectionSizes() call further above. |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 2129 | debug::DebugInfo debug_info = oat_writer->GetDebugInfo(); // Keep the variable alive. |
| 2130 | elf_writer->PrepareDebugInfo(debug_info); // Processes the data on background thread. |
David Srbecky | 0c4572e | 2016-01-22 19:19:25 +0000 | [diff] [blame] | 2131 | |
Vladimir Marko | 0866ea4 | 2019-10-15 12:04:17 +0000 | [diff] [blame] | 2132 | OutputStream* rodata = rodata_[i]; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2133 | DCHECK(rodata != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2134 | if (!oat_writer->WriteRodata(rodata)) { |
| 2135 | LOG(ERROR) << "Failed to write .rodata section to the ELF file " << oat_file->GetPath(); |
| 2136 | return false; |
| 2137 | } |
| 2138 | elf_writer->EndRoData(rodata); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2139 | rodata = nullptr; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 2140 | |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 2141 | OutputStream* text = elf_writer->StartText(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2142 | if (!oat_writer->WriteCode(text)) { |
| 2143 | LOG(ERROR) << "Failed to write .text section to the ELF file " << oat_file->GetPath(); |
| 2144 | return false; |
| 2145 | } |
| 2146 | elf_writer->EndText(text); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 2147 | |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 2148 | if (oat_writer->GetDataBimgRelRoSize() != 0u) { |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 2149 | OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo(); |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 2150 | if (!oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro)) { |
| 2151 | LOG(ERROR) << "Failed to write .data.bimg.rel.ro section to the ELF file " |
| 2152 | << oat_file->GetPath(); |
| 2153 | return false; |
| 2154 | } |
| 2155 | elf_writer->EndDataBimgRelRo(data_bimg_rel_ro); |
| 2156 | } |
| 2157 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 2158 | if (!oat_writer->WriteHeader(elf_writer->GetStream())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2159 | LOG(ERROR) << "Failed to write oat header to the ELF file " << oat_file->GetPath(); |
| 2160 | return false; |
| 2161 | } |
| 2162 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2163 | if (IsImage()) { |
| 2164 | // Update oat header information. |
| 2165 | DCHECK(image_writer_ != nullptr); |
| 2166 | DCHECK_LT(i, oat_filenames_.size()); |
| 2167 | image_writer_->UpdateOatFileHeader(i, oat_writer->GetOatHeader()); |
| 2168 | } |
| 2169 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2170 | elf_writer->WriteDynamicSection(); |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 2171 | elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo()); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 2172 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2173 | if (!elf_writer->End()) { |
| 2174 | LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath(); |
| 2175 | return false; |
| 2176 | } |
| 2177 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2178 | if (!FlushOutputFile(&vdex_files_[i]) || !FlushOutputFile(&oat_files_[i])) { |
| 2179 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2180 | } |
| 2181 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2182 | VLOG(compiler) << "Oat file written successfully: " << oat_filenames_[i]; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2183 | |
| 2184 | oat_writer.reset(); |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2185 | // We may still need the ELF writer later for stripping. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2189 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2192 | // If we are compiling an image, invoke the image creation routine. Else just skip. |
| 2193 | bool HandleImage() { |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 2194 | if (IsImage()) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2195 | TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_); |
| 2196 | if (!CreateImageFile()) { |
| 2197 | return false; |
| 2198 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2199 | VLOG(compiler) << "Images written successfully"; |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 2200 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2201 | return true; |
| 2202 | } |
| 2203 | |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2204 | // Copy the full oat files to symbols directory and then strip the originals. |
| 2205 | bool CopyOatFilesToSymbolsDirectoryAndStrip() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2206 | for (size_t i = 0; i < oat_unstripped_.size(); ++i) { |
| 2207 | // If we don't want to strip in place, copy from stripped location to unstripped location. |
| 2208 | // We need to strip after image creation because FixupElf needs to use .strtab. |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 2209 | if (oat_unstripped_[i] != oat_filenames_[i]) { |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2210 | DCHECK(oat_files_[i].get() != nullptr && oat_files_[i]->IsOpened()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2211 | |
| 2212 | TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_); |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2213 | std::unique_ptr<File>& in = oat_files_[i]; |
Vladimir Marko | b55cc6d | 2016-10-17 15:25:10 +0100 | [diff] [blame] | 2214 | int64_t in_length = in->GetLength(); |
| 2215 | if (in_length < 0) { |
| 2216 | PLOG(ERROR) << "Failed to get the length of oat file: " << in->GetPath(); |
| 2217 | return false; |
| 2218 | } |
Orion Hodson | 923141b | 2020-08-06 14:01:16 +0100 | [diff] [blame] | 2219 | std::unique_ptr<File> out(OS::CreateEmptyFile(oat_unstripped_[i].c_str())); |
| 2220 | if (out == nullptr) { |
| 2221 | PLOG(ERROR) << "Failed to open oat file for writing: " << oat_unstripped_[i]; |
| 2222 | return false; |
| 2223 | } |
Vladimir Marko | b55cc6d | 2016-10-17 15:25:10 +0100 | [diff] [blame] | 2224 | if (!out->Copy(in.get(), 0, in_length)) { |
| 2225 | PLOG(ERROR) << "Failed to copy oat file to file: " << out->GetPath(); |
| 2226 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2227 | } |
| 2228 | if (out->FlushCloseOrErase() != 0) { |
| 2229 | PLOG(ERROR) << "Failed to flush and close copied oat file: " << oat_unstripped_[i]; |
| 2230 | return false; |
| 2231 | } |
| 2232 | VLOG(compiler) << "Oat file copied successfully (unstripped): " << oat_unstripped_[i]; |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2233 | |
| 2234 | if (strip_) { |
| 2235 | TimingLogger::ScopedTiming t2("dex2oat OatFile strip", timings_); |
| 2236 | if (!elf_writers_[i]->StripDebugInfo()) { |
| 2237 | PLOG(ERROR) << "Failed strip oat file: " << in->GetPath(); |
| 2238 | return false; |
| 2239 | } |
| 2240 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | return true; |
| 2244 | } |
| 2245 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2246 | bool FlushOutputFile(std::unique_ptr<File>* file) { |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 2247 | if ((file->get() != nullptr) && !file->get()->ReadOnlyMode()) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2248 | if (file->get()->Flush() != 0) { |
| 2249 | PLOG(ERROR) << "Failed to flush output file: " << file->get()->GetPath(); |
| 2250 | return false; |
| 2251 | } |
| 2252 | } |
| 2253 | return true; |
| 2254 | } |
| 2255 | |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2256 | bool FlushCloseOutputFile(File* file) { |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 2257 | if ((file != nullptr) && !file->ReadOnlyMode()) { |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2258 | if (file->FlushCloseOrErase() != 0) { |
| 2259 | PLOG(ERROR) << "Failed to flush and close output file: " << file->GetPath(); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2260 | return false; |
| 2261 | } |
| 2262 | } |
| 2263 | return true; |
| 2264 | } |
| 2265 | |
| 2266 | bool FlushOutputFiles() { |
| 2267 | TimingLogger::ScopedTiming t2("dex2oat Flush Output Files", timings_); |
| 2268 | for (auto& files : { &vdex_files_, &oat_files_ }) { |
| 2269 | for (size_t i = 0; i < files->size(); ++i) { |
| 2270 | if (!FlushOutputFile(&(*files)[i])) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2271 | return false; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 2272 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 2273 | } |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 2274 | } |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2275 | return true; |
| 2276 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2277 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2278 | bool FlushCloseOutputFiles() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2279 | bool result = true; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2280 | for (auto& files : { &vdex_files_, &oat_files_ }) { |
| 2281 | for (size_t i = 0; i < files->size(); ++i) { |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2282 | result &= FlushCloseOutputFile((*files)[i].get()); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2283 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2284 | } |
| 2285 | return result; |
| 2286 | } |
| 2287 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2288 | void DumpTiming() { |
Nicolas Geoffray | 2d8801f | 2017-11-28 15:50:07 +0000 | [diff] [blame] | 2289 | if (compiler_options_->GetDumpTimings() || |
| 2290 | (kIsDebugBuild && timings_->GetTotalNs() > MsToNs(1000))) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2291 | LOG(INFO) << Dumpable<TimingLogger>(*timings_); |
| 2292 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2293 | } |
| 2294 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2295 | bool IsImage() const { |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 2296 | return IsAppImage() || IsBootImage() || IsBootImageExtension(); |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | bool IsAppImage() const { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2300 | return compiler_options_->IsAppImage(); |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | bool IsBootImage() const { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 2304 | return compiler_options_->IsBootImage(); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 2305 | } |
| 2306 | |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 2307 | bool IsBootImageExtension() const { |
| 2308 | return compiler_options_->IsBootImageExtension(); |
| 2309 | } |
| 2310 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2311 | bool IsHost() const { |
| 2312 | return is_host_; |
| 2313 | } |
| 2314 | |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2315 | bool HasProfileInput() const { return !profile_file_fds_.empty() || !profile_files_.empty(); } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2316 | |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 2317 | // Must be called after the profile is loaded. |
Mathieu Chartier | 11b3df3 | 2017-02-21 13:41:54 -0800 | [diff] [blame] | 2318 | bool DoProfileGuidedOptimizations() const { |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 2319 | DCHECK(!HasProfileInput() || profile_load_attempted_) |
| 2320 | << "The profile has to be loaded before we can decided " |
| 2321 | << "if we do profile guided optimizations"; |
| 2322 | return profile_compilation_info_ != nullptr && !profile_compilation_info_->IsEmpty(); |
Mathieu Chartier | 11b3df3 | 2017-02-21 13:41:54 -0800 | [diff] [blame] | 2323 | } |
| 2324 | |
Mathieu Chartier | eafe2a5 | 2017-10-19 15:29:42 -0700 | [diff] [blame] | 2325 | bool DoGenerateCompactDex() const { |
| 2326 | return compact_dex_level_ != CompactDexLevel::kCompactDexLevelNone; |
| 2327 | } |
| 2328 | |
Mathieu Chartier | 11b3df3 | 2017-02-21 13:41:54 -0800 | [diff] [blame] | 2329 | bool DoDexLayoutOptimizations() const { |
Nicolas Geoffray | db7c76b | 2022-03-21 16:23:30 +0000 | [diff] [blame] | 2330 | // Only run dexlayout when being asked to generate compact dex. We do this |
| 2331 | // to avoid having multiple arguments being passed to dex2oat and the main |
| 2332 | // user of dex2oat (installd) will have the same reasons for |
| 2333 | // disabling/enabling compact dex and dex layout. |
| 2334 | return DoGenerateCompactDex(); |
Mathieu Chartier | 11b3df3 | 2017-02-21 13:41:54 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 2337 | bool DoOatLayoutOptimizations() const { |
| 2338 | return DoProfileGuidedOptimizations(); |
| 2339 | } |
| 2340 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 2341 | bool LoadProfile() { |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 2342 | DCHECK(HasProfileInput()); |
| 2343 | profile_load_attempted_ = true; |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 2344 | // TODO(calin): We should be using the runtime arena pool (instead of the |
| 2345 | // default profile arena). However the setup logic is messy and needs |
| 2346 | // cleaning up before that (e.g. the oat writers are created before the |
| 2347 | // runtime). |
Vladimir Marko | a220980 | 2021-04-23 13:28:29 +0000 | [diff] [blame] | 2348 | bool for_boot_image = IsBootImage() || IsBootImageExtension(); |
| 2349 | profile_compilation_info_.reset(new ProfileCompilationInfo(for_boot_image)); |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2350 | |
| 2351 | // Cleanup profile compilation info if we encounter any error when reading profiles. |
| 2352 | auto cleanup = android::base::ScopeGuard([&]() { profile_compilation_info_.reset(nullptr); }); |
| 2353 | |
Victor Hsieh | 52fe49e | 2020-07-20 15:20:12 -0700 | [diff] [blame] | 2354 | // Dex2oat only uses the reference profile and that is not updated concurrently by the app or |
| 2355 | // other processes. So we don't need to lock (as we have to do in profman or when writing the |
| 2356 | // profile info). |
Jiakai Zhang | 890beb0 | 2022-01-06 14:05:44 +0000 | [diff] [blame] | 2357 | std::vector<std::unique_ptr<File>> profile_files; |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2358 | if (!profile_file_fds_.empty()) { |
| 2359 | for (int fd : profile_file_fds_) { |
Jiakai Zhang | 890beb0 | 2022-01-06 14:05:44 +0000 | [diff] [blame] | 2360 | profile_files.push_back(std::make_unique<File>(DupCloexec(fd), |
| 2361 | "profile", |
| 2362 | /*check_usage=*/ false, |
| 2363 | /*read_only_mode=*/ true)); |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2364 | } |
| 2365 | } else { |
| 2366 | for (const std::string& file : profile_files_) { |
Jiakai Zhang | 890beb0 | 2022-01-06 14:05:44 +0000 | [diff] [blame] | 2367 | profile_files.emplace_back(OS::OpenFileForReading(file.c_str())); |
| 2368 | if (profile_files.back().get() == nullptr) { |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2369 | PLOG(ERROR) << "Cannot open profiles"; |
| 2370 | return false; |
| 2371 | } |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2372 | } |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2373 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 2374 | |
Jiakai Zhang | 890beb0 | 2022-01-06 14:05:44 +0000 | [diff] [blame] | 2375 | std::map<std::string, uint32_t> old_profile_keys, new_profile_keys; |
| 2376 | auto filter_fn = [&](const std::string& profile_key, uint32_t checksum) { |
| 2377 | auto it = old_profile_keys.find(profile_key); |
| 2378 | if (it != old_profile_keys.end() && it->second != checksum) { |
| 2379 | // Filter out this entry. We have already loaded data for the same profile key with a |
| 2380 | // different checksum from an earlier profile file. |
| 2381 | return false; |
| 2382 | } |
| 2383 | // Insert the new profile key and checksum. |
| 2384 | // Note: If the profile contains the same key with different checksums, this insertion fails |
| 2385 | // but we still return `true` and let the `ProfileCompilationInfo::Load()` report an error. |
| 2386 | new_profile_keys.insert(std::make_pair(profile_key, checksum)); |
| 2387 | return true; |
| 2388 | }; |
| 2389 | for (const std::unique_ptr<File>& profile_file : profile_files) { |
| 2390 | if (!profile_compilation_info_->Load(profile_file->Fd(), |
| 2391 | /*merge_classes=*/ true, |
| 2392 | filter_fn)) { |
| 2393 | return false; |
| 2394 | } |
| 2395 | old_profile_keys.merge(new_profile_keys); |
| 2396 | new_profile_keys.clear(); |
| 2397 | } |
| 2398 | |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2399 | cleanup.Disable(); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 2400 | return true; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 2403 | // If we're asked to speed-profile the app but we have no profile, or the profile |
| 2404 | // is empty, change the filter to verify, and the image_type to none. |
| 2405 | // A speed-profile compilation without profile data is equivalent to verify and |
| 2406 | // this change will increase the precision of the telemetry data. |
| 2407 | void UpdateCompilerOptionsBasedOnProfile() { |
| 2408 | if (!DoProfileGuidedOptimizations() && |
| 2409 | compiler_options_->GetCompilerFilter() == CompilerFilter::kSpeedProfile) { |
| 2410 | VLOG(compiler) << "Changing compiler filter to verify from speed-profile " |
| 2411 | << "because of empty or non existing profile"; |
| 2412 | |
| 2413 | compiler_options_->SetCompilerFilter(CompilerFilter::kVerify); |
| 2414 | |
| 2415 | // Note that we could reset the image_type to CompilerOptions::ImageType::kNone |
| 2416 | // to prevent an app image generation. |
| 2417 | // However, if we were pass an image file we would essentially leave the image |
| 2418 | // file empty (possibly triggering some harmless errors when we try to load it). |
| 2419 | // |
| 2420 | // Letting the image_type_ be determined by whether or not we passed an image |
| 2421 | // file will at least write the appropriate header making it an empty but valid |
| 2422 | // image. |
| 2423 | } |
| 2424 | } |
| 2425 | |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 2426 | class ScopedDex2oatReporting { |
| 2427 | public: |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 2428 | explicit ScopedDex2oatReporting(const Dex2Oat& dex2oat) : |
| 2429 | should_report_(dex2oat.should_report_dex2oat_compilation_) { |
| 2430 | if (should_report_) { |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 2431 | if (dex2oat.zip_fd_ != -1) { |
| 2432 | zip_dup_fd_.reset(DupCloexecOrError(dex2oat.zip_fd_)); |
| 2433 | if (zip_dup_fd_ < 0) { |
| 2434 | return; |
| 2435 | } |
| 2436 | } |
| 2437 | int image_fd = dex2oat.IsAppImage() ? dex2oat.app_image_fd_ : dex2oat.image_fd_; |
| 2438 | if (image_fd != -1) { |
| 2439 | image_dup_fd_.reset(DupCloexecOrError(image_fd)); |
| 2440 | if (image_dup_fd_ < 0) { |
| 2441 | return; |
| 2442 | } |
| 2443 | } |
| 2444 | oat_dup_fd_.reset(DupCloexecOrError(dex2oat.oat_fd_)); |
| 2445 | if (oat_dup_fd_ < 0) { |
| 2446 | return; |
| 2447 | } |
| 2448 | vdex_dup_fd_.reset(DupCloexecOrError(dex2oat.output_vdex_fd_)); |
| 2449 | if (vdex_dup_fd_ < 0) { |
| 2450 | return; |
| 2451 | } |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 2452 | PaletteNotifyStartDex2oatCompilation(zip_dup_fd_, |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 2453 | image_dup_fd_, |
| 2454 | oat_dup_fd_, |
| 2455 | vdex_dup_fd_); |
| 2456 | } |
| 2457 | error_reporting_ = false; |
| 2458 | } |
| 2459 | |
| 2460 | ~ScopedDex2oatReporting() { |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 2461 | if (!error_reporting_) { |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 2462 | if (should_report_) { |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 2463 | PaletteNotifyEndDex2oatCompilation(zip_dup_fd_, |
| 2464 | image_dup_fd_, |
| 2465 | oat_dup_fd_, |
| 2466 | vdex_dup_fd_); |
| 2467 | } |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | bool ErrorReporting() const { return error_reporting_; } |
| 2472 | |
| 2473 | private: |
| 2474 | int DupCloexecOrError(int fd) { |
| 2475 | int dup_fd = DupCloexec(fd); |
| 2476 | if (dup_fd < 0) { |
| 2477 | LOG(ERROR) << "Error dup'ing a file descriptor " << strerror(errno); |
| 2478 | error_reporting_ = true; |
| 2479 | } |
| 2480 | return dup_fd; |
| 2481 | } |
| 2482 | android::base::unique_fd oat_dup_fd_; |
| 2483 | android::base::unique_fd vdex_dup_fd_; |
| 2484 | android::base::unique_fd zip_dup_fd_; |
| 2485 | android::base::unique_fd image_dup_fd_; |
| 2486 | bool error_reporting_ = false; |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 2487 | bool should_report_; |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 2488 | }; |
| 2489 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2490 | private: |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 2491 | bool UseSwap(bool is_image, const std::vector<const DexFile*>& dex_files) { |
| 2492 | if (is_image) { |
| 2493 | // Don't use swap, we know generation should succeed, and we don't want to slow it down. |
| 2494 | return false; |
| 2495 | } |
| 2496 | if (dex_files.size() < min_dex_files_for_swap_) { |
| 2497 | // If there are less dex files than the threshold, assume it's gonna be fine. |
| 2498 | return false; |
| 2499 | } |
| 2500 | size_t dex_files_size = 0; |
| 2501 | for (const auto* dex_file : dex_files) { |
| 2502 | dex_files_size += dex_file->GetHeader().file_size_; |
| 2503 | } |
| 2504 | return dex_files_size >= min_dex_file_cumulative_size_for_swap_; |
| 2505 | } |
| 2506 | |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 2507 | bool IsVeryLarge(const std::vector<const DexFile*>& dex_files) { |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 2508 | size_t dex_files_size = 0; |
| 2509 | for (const auto* dex_file : dex_files) { |
| 2510 | dex_files_size += dex_file->GetHeader().file_size_; |
| 2511 | } |
| 2512 | return dex_files_size >= very_large_threshold_; |
| 2513 | } |
| 2514 | |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2515 | bool PrepareDirtyObjects() { |
Victor Hsieh | 4ab5923 | 2021-06-10 12:24:02 -0700 | [diff] [blame] | 2516 | if (dirty_image_objects_fd_ != -1) { |
| 2517 | dirty_image_objects_ = ReadCommentedInputFromFd<HashSet<std::string>>( |
| 2518 | dirty_image_objects_fd_, |
| 2519 | nullptr); |
| 2520 | // Close since we won't need it again. |
| 2521 | close(dirty_image_objects_fd_); |
| 2522 | dirty_image_objects_fd_ = -1; |
| 2523 | if (dirty_image_objects_ == nullptr) { |
| 2524 | LOG(ERROR) << "Failed to create list of dirty objects from fd " << dirty_image_objects_fd_; |
| 2525 | return false; |
| 2526 | } |
| 2527 | } else if (dirty_image_objects_filename_ != nullptr) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 2528 | dirty_image_objects_ = ReadCommentedInputFromFile<HashSet<std::string>>( |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2529 | dirty_image_objects_filename_, |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 2530 | nullptr); |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2531 | if (dirty_image_objects_ == nullptr) { |
| 2532 | LOG(ERROR) << "Failed to create list of dirty objects from '" |
| 2533 | << dirty_image_objects_filename_ << "'"; |
| 2534 | return false; |
| 2535 | } |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2536 | } |
| 2537 | return true; |
| 2538 | } |
| 2539 | |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2540 | bool PreparePreloadedClasses() { |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2541 | preloaded_classes_ = std::make_unique<HashSet<std::string>>(); |
| 2542 | if (!preloaded_classes_fds_.empty()) { |
| 2543 | for (int fd : preloaded_classes_fds_) { |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2544 | if (!ReadCommentedInputFromFd(fd, nullptr, preloaded_classes_.get())) { |
| 2545 | return false; |
| 2546 | } |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2547 | } |
| 2548 | } else { |
| 2549 | for (const std::string& file : preloaded_classes_files_) { |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2550 | if (!ReadCommentedInputFromFile(file.c_str(), nullptr, preloaded_classes_.get())) { |
| 2551 | return false; |
| 2552 | } |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2553 | } |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2554 | } |
| 2555 | return true; |
| 2556 | } |
| 2557 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2558 | void PruneNonExistentDexFiles() { |
| 2559 | DCHECK_EQ(dex_filenames_.size(), dex_locations_.size()); |
| 2560 | size_t kept = 0u; |
| 2561 | for (size_t i = 0, size = dex_filenames_.size(); i != size; ++i) { |
Victor Hsieh | 59edf4b | 2021-06-17 12:48:18 -0700 | [diff] [blame] | 2562 | // Keep if the file exist, or is passed as FD. |
| 2563 | if (!OS::FileExists(dex_filenames_[i].c_str()) && i >= dex_fds_.size()) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2564 | LOG(WARNING) << "Skipping non-existent dex file '" << dex_filenames_[i] << "'"; |
| 2565 | } else { |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 2566 | if (kept != i) { |
| 2567 | dex_filenames_[kept] = dex_filenames_[i]; |
| 2568 | dex_locations_[kept] = dex_locations_[i]; |
| 2569 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2570 | ++kept; |
| 2571 | } |
| 2572 | } |
| 2573 | dex_filenames_.resize(kept); |
| 2574 | dex_locations_.resize(kept); |
| 2575 | } |
| 2576 | |
| 2577 | bool AddDexFileSources() { |
| 2578 | TimingLogger::ScopedTiming t2("AddDexFileSources", timings_); |
Nicolas Geoffray | bc17727 | 2018-01-24 14:55:32 +0000 | [diff] [blame] | 2579 | if (input_vdex_file_ != nullptr && input_vdex_file_->HasDexSection()) { |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 2580 | DCHECK_EQ(oat_writers_.size(), 1u); |
| 2581 | const std::string& name = zip_location_.empty() ? dex_locations_[0] : zip_location_; |
| 2582 | DCHECK(!name.empty()); |
| 2583 | if (!oat_writers_[0]->AddVdexDexFilesSource(*input_vdex_file_.get(), name.c_str())) { |
| 2584 | return false; |
| 2585 | } |
| 2586 | } else if (zip_fd_ != -1) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2587 | DCHECK_EQ(oat_writers_.size(), 1u); |
wangshumin | 3949e5e | 2019-11-08 19:25:50 +0800 | [diff] [blame] | 2588 | if (!oat_writers_[0]->AddDexFileSource(File(zip_fd_, /* check_usage */ false), |
| 2589 | zip_location_.c_str())) { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2590 | return false; |
| 2591 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2592 | } else { |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2593 | DCHECK_EQ(dex_filenames_.size(), dex_locations_.size()); |
Victor Hsieh | b3bc892 | 2021-05-14 15:02:21 -0700 | [diff] [blame] | 2594 | DCHECK_GE(oat_writers_.size(), 1u); |
| 2595 | |
Victor Hsieh | 826e666 | 2021-05-17 15:40:13 -0700 | [diff] [blame] | 2596 | bool use_dex_fds = !dex_fds_.empty(); |
| 2597 | if (use_dex_fds) { |
| 2598 | DCHECK_EQ(dex_fds_.size(), dex_filenames_.size()); |
| 2599 | } |
| 2600 | |
Victor Hsieh | b3bc892 | 2021-05-14 15:02:21 -0700 | [diff] [blame] | 2601 | bool is_multi_image = oat_writers_.size() > 1u; |
| 2602 | if (is_multi_image) { |
| 2603 | DCHECK_EQ(oat_writers_.size(), dex_filenames_.size()); |
| 2604 | } |
| 2605 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2606 | for (size_t i = 0; i != dex_filenames_.size(); ++i) { |
Victor Hsieh | b3bc892 | 2021-05-14 15:02:21 -0700 | [diff] [blame] | 2607 | int oat_index = is_multi_image ? i : 0; |
Victor Hsieh | 826e666 | 2021-05-17 15:40:13 -0700 | [diff] [blame] | 2608 | auto oat_writer = oat_writers_[oat_index].get(); |
| 2609 | |
| 2610 | if (use_dex_fds) { |
| 2611 | if (!oat_writer->AddDexFileSource(File(dex_fds_[i], /* check_usage */ false), |
| 2612 | dex_locations_[i].c_str())) { |
| 2613 | return false; |
| 2614 | } |
| 2615 | } else { |
| 2616 | if (!oat_writer->AddDexFileSource(dex_filenames_[i].c_str(), |
| 2617 | dex_locations_[i].c_str())) { |
| 2618 | return false; |
| 2619 | } |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | } |
| 2623 | return true; |
| 2624 | } |
| 2625 | |
| 2626 | void CreateOatWriters() { |
| 2627 | TimingLogger::ScopedTiming t2("CreateOatWriters", timings_); |
| 2628 | elf_writers_.reserve(oat_files_.size()); |
| 2629 | oat_writers_.reserve(oat_files_.size()); |
| 2630 | for (const std::unique_ptr<File>& oat_file : oat_files_) { |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 2631 | elf_writers_.emplace_back(linker::CreateElfWriterQuick(*compiler_options_, oat_file.get())); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2632 | elf_writers_.back()->Start(); |
Mathieu Chartier | 4a265c2 | 2018-04-09 14:25:14 -0700 | [diff] [blame] | 2633 | bool do_oat_writer_layout = DoDexLayoutOptimizations() || DoOatLayoutOptimizations(); |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2634 | oat_writers_.emplace_back(new linker::OatWriter( |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 2635 | *compiler_options_, |
Igor Murashkin | 545412b | 2017-08-17 15:26:54 -0700 | [diff] [blame] | 2636 | timings_, |
Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 2637 | do_oat_writer_layout ? profile_compilation_info_.get() : nullptr, |
| 2638 | compact_dex_level_)); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2639 | } |
| 2640 | } |
| 2641 | |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2642 | void SaveDexInput() { |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame] | 2643 | const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_; |
| 2644 | for (size_t i = 0, size = dex_files.size(); i != size; ++i) { |
| 2645 | const DexFile* dex_file = dex_files[i]; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2646 | std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex", |
| 2647 | getpid(), i)); |
| 2648 | std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str())); |
| 2649 | if (tmp_file.get() == nullptr) { |
| 2650 | PLOG(ERROR) << "Failed to open file " << tmp_file_name |
| 2651 | << ". Try: adb shell chmod 777 /data/local/tmp"; |
| 2652 | continue; |
| 2653 | } |
| 2654 | // This is just dumping files for debugging. Ignore errors, and leave remnants. |
| 2655 | UNUSED(tmp_file->WriteFully(dex_file->Begin(), dex_file->Size())); |
| 2656 | UNUSED(tmp_file->Flush()); |
| 2657 | UNUSED(tmp_file->Close()); |
| 2658 | LOG(INFO) << "Wrote input to " << tmp_file_name; |
| 2659 | } |
| 2660 | } |
| 2661 | |
Mathieu Chartier | 46331a6 | 2017-07-21 09:49:10 -0700 | [diff] [blame] | 2662 | bool PrepareRuntimeOptions(RuntimeArgumentMap* runtime_options, |
| 2663 | QuickCompilerCallbacks* callbacks) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2664 | RuntimeOptions raw_options; |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 2665 | if (IsBootImage()) { |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2666 | std::string boot_class_path = "-Xbootclasspath:"; |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 2667 | boot_class_path += android::base::Join(dex_filenames_, ':'); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2668 | raw_options.push_back(std::make_pair(boot_class_path, nullptr)); |
| 2669 | std::string boot_class_path_locations = "-Xbootclasspath-locations:"; |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 2670 | boot_class_path_locations += android::base::Join(dex_locations_, ':'); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2671 | raw_options.push_back(std::make_pair(boot_class_path_locations, nullptr)); |
| 2672 | } else { |
| 2673 | std::string boot_image_option = "-Ximage:"; |
| 2674 | boot_image_option += boot_image_filename_; |
| 2675 | raw_options.push_back(std::make_pair(boot_image_option, nullptr)); |
| 2676 | } |
| 2677 | for (size_t i = 0; i < runtime_args_.size(); i++) { |
| 2678 | raw_options.push_back(std::make_pair(runtime_args_[i], nullptr)); |
| 2679 | } |
| 2680 | |
Mathieu Chartier | 46331a6 | 2017-07-21 09:49:10 -0700 | [diff] [blame] | 2681 | raw_options.push_back(std::make_pair("compilercallbacks", callbacks)); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2682 | raw_options.push_back( |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 2683 | std::make_pair("imageinstructionset", |
| 2684 | GetInstructionSetString(compiler_options_->GetInstructionSet()))); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2685 | |
Andreas Gampe | c8e3520 | 2017-04-28 15:47:12 -0700 | [diff] [blame] | 2686 | // Never allow implicit image compilation. |
| 2687 | raw_options.push_back(std::make_pair("-Xnoimage-dex2oat", nullptr)); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2688 | // Disable libsigchain. We don't don't need it during compilation and it prevents us |
| 2689 | // from getting a statically linked version of dex2oat (because of dlsym and RTLD_NEXT). |
| 2690 | raw_options.push_back(std::make_pair("-Xno-sig-chain", nullptr)); |
Lin Zang | 97f3f7d | 2016-01-13 10:25:00 +0800 | [diff] [blame] | 2691 | // Disable Hspace compaction to save heap size virtual space. |
| 2692 | // Only need disable Hspace for OOM becasue background collector is equal to |
| 2693 | // foreground collector by default for dex2oat. |
| 2694 | raw_options.push_back(std::make_pair("-XX:DisableHSpaceCompactForOOM", nullptr)); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2695 | |
| 2696 | if (!Runtime::ParseOptions(raw_options, false, runtime_options)) { |
| 2697 | LOG(ERROR) << "Failed to parse runtime options"; |
| 2698 | return false; |
| 2699 | } |
| 2700 | return true; |
| 2701 | } |
| 2702 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2703 | // Create a runtime necessary for compilation. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2704 | bool CreateRuntime(RuntimeArgumentMap&& runtime_options) { |
Vladimir Marko | e1ee6f9 | 2020-03-13 11:30:59 +0000 | [diff] [blame] | 2705 | // To make identity hashcode deterministic, set a seed based on the dex file checksums. |
| 2706 | // That makes the seed also most likely different for different inputs, for example |
| 2707 | // for primary boot image and different extensions that could be loaded together. |
| 2708 | mirror::Object::SetHashCodeSeed(987654321u ^ GetCombinedChecksums()); |
| 2709 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2710 | TimingLogger::ScopedTiming t_runtime("Create runtime", timings_); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2711 | if (!Runtime::Create(std::move(runtime_options))) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2712 | LOG(ERROR) << "Failed to create runtime"; |
| 2713 | return false; |
| 2714 | } |
Andreas Gampe | 49c144d | 2016-11-14 10:11:16 -0800 | [diff] [blame] | 2715 | |
| 2716 | // Runtime::Init will rename this thread to be "main". Prefer "dex2oat" so that "top" and |
| 2717 | // "ps -a" don't change to non-descript "main." |
| 2718 | SetThreadName(kIsDebugBuild ? "dex2oatd" : "dex2oat"); |
| 2719 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2720 | runtime_.reset(Runtime::Current()); |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame] | 2721 | runtime_->SetInstructionSet(compiler_options_->GetInstructionSet()); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 2722 | for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) { |
| 2723 | CalleeSaveType type = CalleeSaveType(i); |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2724 | if (!runtime_->HasCalleeSaveMethod(type)) { |
| 2725 | runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2726 | } |
| 2727 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 2728 | |
| 2729 | // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this |
| 2730 | // set up. |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 2731 | interpreter::UnstartedRuntime::Initialize(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 2732 | |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 2733 | Thread* self = Thread::Current(); |
| 2734 | runtime_->RunRootClinits(self); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 2735 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2736 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 2737 | // Runtime::Start, give it away now so that we don't starve GC. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 2738 | self->TransitionFromRunnableToSuspended(ThreadState::kNative); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2739 | |
Nicolas Geoffray | 48eb839 | 2022-02-24 16:20:18 +0000 | [diff] [blame] | 2740 | // Now that we are in native state, initialize well known classes and |
| 2741 | // intrinsics if we don't have a boot image. |
| 2742 | WellKnownClasses::Init(self->GetJniEnv()); |
| 2743 | if (IsBootImage() || runtime_->GetHeap()->GetBootImageSpaces().empty()) { |
| 2744 | InitializeIntrinsics(); |
| 2745 | } |
| 2746 | |
Koji Fukui | a72ca6c | 2019-03-25 17:39:07 +0900 | [diff] [blame] | 2747 | WatchDog::SetRuntime(runtime_.get()); |
| 2748 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2749 | return true; |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2752 | // Let the ImageWriter write the image files. If we do not compile PIC, also fix up the oat files. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2753 | bool CreateImageFile() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2754 | REQUIRES(!Locks::mutator_lock_) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2755 | CHECK(image_writer_ != nullptr); |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 2756 | if (IsAppImage()) { |
| 2757 | DCHECK(image_filenames_.empty()); |
| 2758 | if (app_image_fd_ != -1) { |
| 2759 | image_filenames_.push_back(StringPrintf("FileDescriptor[%d]", app_image_fd_)); |
| 2760 | } else { |
| 2761 | image_filenames_.push_back(app_image_file_name_); |
| 2762 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2763 | } |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 2764 | if (image_fd_ != -1) { |
| 2765 | DCHECK(image_filenames_.empty()); |
| 2766 | image_filenames_.push_back(StringPrintf("FileDescriptor[%d]", image_fd_)); |
| 2767 | } |
| 2768 | if (!image_writer_->Write(IsAppImage() ? app_image_fd_ : image_fd_, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2769 | image_filenames_, |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 2770 | IsAppImage() ? 1u : dex_locations_.size())) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2771 | LOG(ERROR) << "Failure during image file creation"; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2772 | return false; |
| 2773 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2774 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2775 | // We need the OatDataBegin entries. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2776 | dchecked_vector<uintptr_t> oat_data_begins; |
| 2777 | for (size_t i = 0, size = oat_filenames_.size(); i != size; ++i) { |
| 2778 | oat_data_begins.push_back(image_writer_->GetOatDataBegin(i)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2779 | } |
Vladimir Marko | a2da9b9 | 2018-10-10 14:21:55 +0100 | [diff] [blame] | 2780 | // Destroy ImageWriter. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2781 | image_writer_.reset(); |
| 2782 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2783 | return true; |
| 2784 | } |
| 2785 | |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2786 | template <typename T> |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2787 | static bool ReadCommentedInputFromFile( |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2788 | const char* input_filename, std::function<std::string(const char*)>* process, T* output) { |
| 2789 | auto input_file = std::unique_ptr<FILE, decltype(&fclose)>{fopen(input_filename, "r"), fclose}; |
| 2790 | if (!input_file) { |
| 2791 | LOG(ERROR) << "Failed to open input file " << input_filename; |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2792 | return false; |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2793 | } |
| 2794 | ReadCommentedInputStream<T>(input_file.get(), process, output); |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2795 | return true; |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2798 | template <typename T> |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2799 | static bool ReadCommentedInputFromFd( |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2800 | int input_fd, std::function<std::string(const char*)>* process, T* output) { |
| 2801 | auto input_file = std::unique_ptr<FILE, decltype(&fclose)>{fdopen(input_fd, "r"), fclose}; |
| 2802 | if (!input_file) { |
| 2803 | LOG(ERROR) << "Failed to re-open input fd from /prof/self/fd/" << input_fd; |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2804 | return false; |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2805 | } |
| 2806 | ReadCommentedInputStream<T>(input_file.get(), process, output); |
Nicolas Geoffray | 2bd92d3 | 2022-03-24 16:06:50 +0000 | [diff] [blame] | 2807 | return true; |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2810 | // Read lines from the given file, dropping comments and empty lines. Post-process each line with |
| 2811 | // the given function. |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 2812 | template <typename T> |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 2813 | static std::unique_ptr<T> ReadCommentedInputFromFile( |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2814 | const char* input_filename, std::function<std::string(const char*)>* process) { |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2815 | std::unique_ptr<T> output(new T()); |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2816 | ReadCommentedInputFromFile(input_filename, process, output.get()); |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2817 | return output; |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2818 | } |
| 2819 | |
Victor Hsieh | 4ab5923 | 2021-06-10 12:24:02 -0700 | [diff] [blame] | 2820 | // Read lines from the given fd, dropping comments and empty lines. Post-process each line with |
| 2821 | // the given function. |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 2822 | template <typename T> |
Victor Hsieh | 4ab5923 | 2021-06-10 12:24:02 -0700 | [diff] [blame] | 2823 | static std::unique_ptr<T> ReadCommentedInputFromFd( |
| 2824 | int input_fd, std::function<std::string(const char*)>* process) { |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2825 | std::unique_ptr<T> output(new T()); |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2826 | ReadCommentedInputFromFd(input_fd, process, output.get()); |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2827 | return output; |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | // Read lines from the given stream, dropping comments and empty lines. Post-process each line |
| 2831 | // with the given function. |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2832 | template <typename T> static void ReadCommentedInputStream( |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 2833 | std::FILE* in_stream, |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2834 | std::function<std::string(const char*)>* process, |
| 2835 | T* output) { |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 2836 | char* line = nullptr; |
| 2837 | size_t line_alloc = 0; |
| 2838 | ssize_t len = 0; |
| 2839 | while ((len = getline(&line, &line_alloc, in_stream)) > 0) { |
| 2840 | if (line[0] == '\0' || line[0] == '#' || line[0] == '\n') { |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2841 | continue; |
| 2842 | } |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 2843 | if (line[len - 1] == '\n') { |
| 2844 | line[len - 1] = '\0'; |
| 2845 | } |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2846 | if (process != nullptr) { |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 2847 | std::string descriptor((*process)(line)); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 2848 | output->insert(output->end(), descriptor); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2849 | } else { |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 2850 | output->insert(output->end(), line); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 2851 | } |
| 2852 | } |
Victor Hsieh | 2bac049 | 2021-08-19 13:16:00 -0700 | [diff] [blame] | 2853 | free(line); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2854 | } |
| 2855 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 2856 | void LogCompletionTime() { |
Andreas Gampe | 1d00add | 2015-02-27 19:35:46 -0800 | [diff] [blame] | 2857 | // Note: when creation of a runtime fails, e.g., when trying to compile an app but when there |
| 2858 | // is no image, there won't be a Runtime::Current(). |
Brian Carlstrom | a11a34c | 2015-03-06 08:44:45 -0800 | [diff] [blame] | 2859 | // Note: driver creation can fail when loading an invalid dex file. |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 2860 | LOG(INFO) << "dex2oat took " |
| 2861 | << PrettyDuration(NanoTime() - start_ns_) |
Andreas Gampe | cc96775 | 2016-12-12 13:49:00 -0800 | [diff] [blame] | 2862 | << " (" << PrettyDuration(ProcessCpuNanoTime() - start_cputime_ns_) << " cpu)" |
Mathieu Chartier | ab972ef | 2014-12-03 17:38:22 -0800 | [diff] [blame] | 2863 | << " (threads: " << thread_count_ << ") " |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2864 | << ((Runtime::Current() != nullptr && driver_ != nullptr) ? |
Andreas Gampe | 1d00add | 2015-02-27 19:35:46 -0800 | [diff] [blame] | 2865 | driver_->GetMemoryUsageString(kIsDebugBuild || VLOG_IS_ON(compiler)) : |
| 2866 | ""); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2867 | } |
| 2868 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2869 | std::string StripIsaFrom(const char* image_filename, InstructionSet isa) { |
| 2870 | std::string res(image_filename); |
| 2871 | size_t last_slash = res.rfind('/'); |
| 2872 | if (last_slash == std::string::npos || last_slash == 0) { |
| 2873 | return res; |
| 2874 | } |
| 2875 | size_t penultimate_slash = res.rfind('/', last_slash - 1); |
| 2876 | if (penultimate_slash == std::string::npos) { |
| 2877 | return res; |
| 2878 | } |
| 2879 | // Check that the string in-between is the expected one. |
| 2880 | if (res.substr(penultimate_slash + 1, last_slash - penultimate_slash - 1) != |
| 2881 | GetInstructionSetString(isa)) { |
| 2882 | LOG(WARNING) << "Unexpected string when trying to strip isa: " << res; |
| 2883 | return res; |
| 2884 | } |
| 2885 | return res.substr(0, penultimate_slash) + res.substr(last_slash); |
| 2886 | } |
| 2887 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2888 | std::unique_ptr<CompilerOptions> compiler_options_; |
| 2889 | Compiler::Kind compiler_kind_; |
| 2890 | |
Eric Holk | 94a810b | 2020-05-06 14:33:16 -0700 | [diff] [blame] | 2891 | std::unique_ptr<OatKeyValueStore> key_value_store_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2892 | |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2893 | std::unique_ptr<VerificationResults> verification_results_; |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2894 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2895 | std::unique_ptr<QuickCompilerCallbacks> callbacks_; |
| 2896 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2897 | std::unique_ptr<Runtime> runtime_; |
| 2898 | |
Calin Juravle | 1ce7085 | 2017-06-28 10:59:03 -0700 | [diff] [blame] | 2899 | // The spec describing how the class loader should be setup for compilation. |
| 2900 | std::unique_ptr<ClassLoaderContext> class_loader_context_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 2901 | |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 2902 | // Optional list of file descriptors corresponding to dex file locations in |
| 2903 | // flattened `class_loader_context_`. |
| 2904 | std::vector<int> class_loader_context_fds_; |
| 2905 | |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 2906 | // The class loader context stored in the oat file. May be equal to class_loader_context_. |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 2907 | std::unique_ptr<ClassLoaderContext> stored_class_loader_context_; |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 2908 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2909 | size_t thread_count_; |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 2910 | std::vector<int32_t> cpu_set_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2911 | uint64_t start_ns_; |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 2912 | uint64_t start_cputime_ns_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2913 | std::unique_ptr<WatchDog> watchdog_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2914 | std::vector<std::unique_ptr<File>> oat_files_; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 2915 | std::vector<std::unique_ptr<File>> vdex_files_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2916 | std::string oat_location_; |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 2917 | std::vector<std::string> oat_filenames_; |
| 2918 | std::vector<std::string> oat_unstripped_; |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 2919 | bool strip_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2920 | int oat_fd_; |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 2921 | int input_vdex_fd_; |
| 2922 | int output_vdex_fd_; |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 2923 | std::string input_vdex_; |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 2924 | std::string output_vdex_; |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 2925 | std::unique_ptr<VdexFile> input_vdex_file_; |
Nicolas Geoffray | baeaa9b | 2018-01-26 14:31:17 +0000 | [diff] [blame] | 2926 | int dm_fd_; |
| 2927 | std::string dm_file_location_; |
| 2928 | std::unique_ptr<ZipArchive> dm_file_; |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 2929 | std::vector<std::string> dex_filenames_; |
| 2930 | std::vector<std::string> dex_locations_; |
Victor Hsieh | 826e666 | 2021-05-17 15:40:13 -0700 | [diff] [blame] | 2931 | std::vector<int> dex_fds_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2932 | int zip_fd_; |
| 2933 | std::string zip_location_; |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 2934 | std::string boot_image_filename_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2935 | std::vector<const char*> runtime_args_; |
Vladimir Marko | d011d81 | 2018-11-29 12:35:24 +0000 | [diff] [blame] | 2936 | std::vector<std::string> image_filenames_; |
Vladimir Marko | d0036ac | 2019-11-21 11:47:12 +0000 | [diff] [blame] | 2937 | int image_fd_; |
| 2938 | bool have_multi_image_arg_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2939 | uintptr_t image_base_; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 2940 | ImageHeader::StorageMode image_storage_mode_; |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 2941 | const char* passes_to_run_filename_; |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2942 | const char* dirty_image_objects_filename_; |
Victor Hsieh | 4ab5923 | 2021-06-10 12:24:02 -0700 | [diff] [blame] | 2943 | int dirty_image_objects_fd_; |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 2944 | std::unique_ptr<HashSet<std::string>> dirty_image_objects_; |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2945 | std::unique_ptr<HashSet<std::string>> preloaded_classes_; |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 2946 | std::unique_ptr<std::vector<std::string>> passes_to_run_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2947 | bool is_host_; |
| 2948 | std::string android_root_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2949 | std::string no_inline_from_string_; |
Alex Light | 4531afd | 2021-03-30 17:13:30 -0700 | [diff] [blame] | 2950 | bool force_allow_oj_inlines_ = false; |
Mathieu Chartier | a7f6b81 | 2017-12-11 13:34:29 -0800 | [diff] [blame] | 2951 | CompactDexLevel compact_dex_level_ = kDefaultCompactDexLevel; |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2952 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2953 | std::vector<std::unique_ptr<linker::ElfWriter>> elf_writers_; |
| 2954 | std::vector<std::unique_ptr<linker::OatWriter>> oat_writers_; |
David Srbecky | 2faab00 | 2019-02-12 16:35:48 +0000 | [diff] [blame] | 2955 | std::vector<OutputStream*> rodata_; |
| 2956 | std::vector<std::unique_ptr<OutputStream>> vdex_out_; |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 2957 | std::unique_ptr<linker::ImageWriter> image_writer_; |
Vladimir Marko | 307dac9 | 2015-10-20 11:20:09 +0100 | [diff] [blame] | 2958 | std::unique_ptr<CompilerDriver> driver_; |
Andreas Gampe | 3f30e12 | 2015-09-17 14:03:21 -0700 | [diff] [blame] | 2959 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 2960 | std::vector<MemMap> opened_dex_files_maps_; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 2961 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files_; |
| 2962 | |
Mathieu Chartier | d1ab1bd | 2017-08-04 10:21:50 -0700 | [diff] [blame] | 2963 | bool avoid_storing_invocation_; |
Alex Light | 62afcf5 | 2018-12-18 14:19:25 -0800 | [diff] [blame] | 2964 | android::base::unique_fd invocation_file_; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 2965 | std::string swap_file_name_; |
| 2966 | int swap_fd_; |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 2967 | size_t min_dex_files_for_swap_ = kDefaultMinDexFilesForSwap; |
| 2968 | size_t min_dex_file_cumulative_size_for_swap_ = kDefaultMinDexFileCumulativeSizeForSwap; |
Andreas Gampe | 67f0282 | 2016-06-24 21:05:23 -0700 | [diff] [blame] | 2969 | size_t very_large_threshold_ = std::numeric_limits<size_t>::max(); |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 2970 | std::string app_image_file_name_; |
| 2971 | int app_image_fd_; |
Jiakai Zhang | 19f9568 | 2021-12-06 16:19:39 +0000 | [diff] [blame] | 2972 | std::vector<std::string> profile_files_; |
| 2973 | std::vector<int> profile_file_fds_; |
Nicolas Geoffray | 3dd6219 | 2022-03-14 15:30:16 +0000 | [diff] [blame] | 2974 | std::vector<std::string> preloaded_classes_files_; |
Nicolas Geoffray | 4c1e0ca | 2022-03-18 16:11:39 +0000 | [diff] [blame] | 2975 | std::vector<int> preloaded_classes_fds_; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 2976 | std::unique_ptr<ProfileCompilationInfo> profile_compilation_info_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2977 | TimingLogger* timings_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2978 | std::vector<std::vector<const DexFile*>> dex_files_per_oat_file_; |
Vladimir Marko | 26de896 | 2020-12-22 14:36:47 +0000 | [diff] [blame] | 2979 | HashMap<const DexFile*, size_t> dex_file_oat_index_map_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2980 | |
| 2981 | // Backing storage. |
Andreas Gampe | 097f34c | 2017-08-23 08:57:51 -0700 | [diff] [blame] | 2982 | std::forward_list<std::string> char_backing_storage_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 2983 | |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2984 | // See CompilerOptions.force_determinism_. |
| 2985 | bool force_determinism_; |
David Sehr | fe57c2b | 2020-03-27 14:58:54 +0000 | [diff] [blame] | 2986 | // See CompilerOptions.crash_on_linkage_violation_. |
| 2987 | bool check_linkage_conditions_; |
| 2988 | // See CompilerOptions.crash_on_linkage_violation_. |
| 2989 | bool crash_on_linkage_violation_; |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2990 | |
Jeff Hao | 16d4843 | 2017-04-05 17:05:46 -0700 | [diff] [blame] | 2991 | // Directory of relative classpaths. |
| 2992 | std::string classpath_dir_; |
| 2993 | |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 2994 | // Whether the given input vdex is also the output. |
Nicolas Geoffray | 84caad6 | 2022-01-10 09:48:55 +0000 | [diff] [blame] | 2995 | bool use_existing_vdex_ = false; |
Nicolas Geoffray | 81f57d1 | 2016-12-20 13:17:09 +0000 | [diff] [blame] | 2996 | |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 2997 | // By default, copy the dex to the vdex file only if dex files are |
| 2998 | // compressed in APK. |
Andreas Gampe | 1554417 | 2018-06-25 15:09:06 -0700 | [diff] [blame] | 2999 | linker::CopyOption copy_dex_files_ = linker::CopyOption::kOnlyIfCompressed; |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame] | 3000 | |
Calin Juravle | 0e09dfc | 2018-02-12 19:01:09 -0800 | [diff] [blame] | 3001 | // The reason for invoking the compiler. |
| 3002 | std::string compilation_reason_; |
| 3003 | |
Eric Holk | bc89ed4 | 2020-04-29 19:59:24 +0000 | [diff] [blame] | 3004 | // Whether to force individual compilation. |
| 3005 | bool compile_individually_; |
| 3006 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 3007 | // The classpath that determines if a given symbol should be resolved at compile time or not. |
| 3008 | std::string public_sdk_; |
| 3009 | |
Nicolas Geoffray | d3c45c2 | 2021-04-27 13:21:28 +0100 | [diff] [blame] | 3010 | // The apex versions of jars in the boot classpath. Set through command line |
| 3011 | // argument. |
| 3012 | std::string apex_versions_argument_; |
| 3013 | |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 3014 | // Whether or we attempted to load the profile (if given). |
| 3015 | bool profile_load_attempted_; |
| 3016 | |
Martin Stjernholm | d4aba91 | 2022-01-25 21:05:47 +0000 | [diff] [blame] | 3017 | // Whether PaletteNotify{Start,End}Dex2oatCompilation should be called. |
| 3018 | bool should_report_dex2oat_compilation_; |
| 3019 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3020 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 3021 | }; |
| 3022 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3023 | static void b13564922() { |
| 3024 | #if defined(__linux__) && defined(__arm__) |
Lokesh Gidra | 58520df | 2020-05-13 17:08:43 +0000 | [diff] [blame] | 3025 | int major, minor; |
| 3026 | struct utsname uts; |
| 3027 | if (uname(&uts) != -1 && |
| 3028 | sscanf(uts.release, "%d.%d", &major, &minor) == 2 && |
| 3029 | ((major < 3) || ((major == 3) && (minor < 4)))) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3030 | // Kernels before 3.4 don't handle the ASLR well and we can run out of address |
| 3031 | // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization. |
| 3032 | int old_personality = personality(0xffffffff); |
| 3033 | if ((old_personality & ADDR_NO_RANDOMIZE) == 0) { |
| 3034 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 3035 | if (new_personality == -1) { |
| 3036 | LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed."; |
| 3037 | } |
| 3038 | } |
| 3039 | } |
| 3040 | #endif |
| 3041 | } |
| 3042 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3043 | class ScopedGlobalRef { |
| 3044 | public: |
| 3045 | explicit ScopedGlobalRef(jobject obj) : obj_(obj) {} |
| 3046 | ~ScopedGlobalRef() { |
| 3047 | if (obj_ != nullptr) { |
| 3048 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 3049 | soa.Env()->GetVm()->DeleteGlobalRef(soa.Self(), obj_); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | private: |
| 3054 | jobject obj_; |
| 3055 | }; |
| 3056 | |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 3057 | static dex2oat::ReturnCode DoCompilation(Dex2Oat& dex2oat) { |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 3058 | dex2oat.LoadClassProfileDescriptors(); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 3059 | jobject class_loader = dex2oat.Compile(); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 3060 | // Keep the class loader that was used for compilation live for the rest of the compilation |
| 3061 | // process. |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 3062 | ScopedGlobalRef global_ref(class_loader); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3063 | |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 3064 | if (!dex2oat.WriteOutputFiles(class_loader)) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3065 | dex2oat.EraseOutputFiles(); |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3066 | return dex2oat::ReturnCode::kOther; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3067 | } |
| 3068 | |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 3069 | // Flush output files. Keep them open as we might still modify them later (strip them). |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 3070 | if (!dex2oat.FlushOutputFiles()) { |
| 3071 | dex2oat.EraseOutputFiles(); |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3072 | return dex2oat::ReturnCode::kOther; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3073 | } |
| 3074 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3075 | // Creates the boot.art and patches the oat files. |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3076 | if (!dex2oat.HandleImage()) { |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3077 | return dex2oat::ReturnCode::kOther; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | // When given --host, finish early without stripping. |
| 3081 | if (dex2oat.IsHost()) { |
Mathieu Chartier | 00bf260 | 2017-03-02 10:14:08 -0800 | [diff] [blame] | 3082 | if (!dex2oat.FlushCloseOutputFiles()) { |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3083 | return dex2oat::ReturnCode::kOther; |
Mathieu Chartier | 00bf260 | 2017-03-02 10:14:08 -0800 | [diff] [blame] | 3084 | } |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3085 | dex2oat.DumpTiming(); |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3086 | return dex2oat::ReturnCode::kNoFailure; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3087 | } |
| 3088 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3089 | // Copy stripped to unstripped location, if necessary. This will implicitly flush & close the |
| 3090 | // stripped versions. If this is given, we expect to be able to open writable files by name. |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 3091 | if (!dex2oat.CopyOatFilesToSymbolsDirectoryAndStrip()) { |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3092 | return dex2oat::ReturnCode::kOther; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3093 | } |
| 3094 | |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 3095 | // FlushClose again, as stripping might have re-opened the oat files. |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3096 | if (!dex2oat.FlushCloseOutputFiles()) { |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3097 | return dex2oat::ReturnCode::kOther; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | dex2oat.DumpTiming(); |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3101 | return dex2oat::ReturnCode::kNoFailure; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 3102 | } |
| 3103 | |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3104 | static dex2oat::ReturnCode Dex2oat(int argc, char** argv) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3105 | b13564922(); |
| 3106 | |
| 3107 | TimingLogger timings("compiler", false, false); |
| 3108 | |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame] | 3109 | // Allocate `dex2oat` on the heap instead of on the stack, as Clang |
| 3110 | // might produce a stack frame too large for this function or for |
| 3111 | // functions inlining it (such as main), that would not fit the |
| 3112 | // requirements of the `-Wframe-larger-than` option. |
Andreas Gampe | 8bdda5a | 2017-06-08 15:30:36 -0700 | [diff] [blame] | 3113 | std::unique_ptr<Dex2Oat> dex2oat = std::make_unique<Dex2Oat>(&timings); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3114 | |
| 3115 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame] | 3116 | dex2oat->ParseArgs(argc, argv); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3117 | |
Calin Juravle | 814a80f | 2019-06-26 15:01:10 -0700 | [diff] [blame] | 3118 | art::MemMap::Init(); // For ZipEntry::ExtractToMemMap, vdex and profiles. |
| 3119 | |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 3120 | // If needed, process profile information for profile guided compilation. |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 3121 | // This operation involves I/O. |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 3122 | if (dex2oat->HasProfileInput()) { |
Calin Juravle | b077e15 | 2016-02-18 18:47:37 +0000 | [diff] [blame] | 3123 | if (!dex2oat->LoadProfile()) { |
| 3124 | LOG(ERROR) << "Failed to process profile file"; |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3125 | return dex2oat::ReturnCode::kOther; |
Calin Juravle | 998c216 | 2015-12-21 15:39:33 +0200 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 3129 | // Check if we need to update any of the compiler options (such as the filter) |
| 3130 | // and do it before anything else (so that the other operations have a true |
| 3131 | // view of the state). |
| 3132 | dex2oat->UpdateCompilerOptionsBasedOnProfile(); |
| 3133 | |
| 3134 | // Insert the compiler options in the key value store. |
| 3135 | // We have to do this after we altered any incoming arguments |
| 3136 | // (such as the compiler filter). |
| 3137 | dex2oat->InsertCompileOptions(argc, argv); |
Nicolas Geoffray | b0bbe8e | 2016-11-19 10:42:37 +0000 | [diff] [blame] | 3138 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3139 | // Check early that the result of compilation can be written |
Roland Levillain | 5c7e260 | 2016-02-23 18:08:53 +0000 | [diff] [blame] | 3140 | if (!dex2oat->OpenFile()) { |
Calin Juravle | 046cecf | 2020-12-07 18:31:28 -0800 | [diff] [blame] | 3141 | // Flush close so that the File Guard checks don't fail the assertions. |
| 3142 | dex2oat->FlushCloseOutputFiles(); |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3143 | return dex2oat::ReturnCode::kOther; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 3146 | // Print the complete line when any of the following is true: |
| 3147 | // 1) Debug build |
| 3148 | // 2) Compiling an image |
| 3149 | // 3) Compiling with --host |
| 3150 | // 4) Compiling on the host (not a target build) |
| 3151 | // Otherwise, print a stripped command line. |
Vladimir Marko | 2191069 | 2019-11-06 13:27:03 +0000 | [diff] [blame] | 3152 | if (kIsDebugBuild || |
| 3153 | dex2oat->IsBootImage() || dex2oat->IsBootImageExtension() || |
| 3154 | dex2oat->IsHost() || |
| 3155 | !kIsTargetBuild) { |
Andreas Gampe | 046c706 | 2015-05-18 23:22:54 -0700 | [diff] [blame] | 3156 | LOG(INFO) << CommandLine(); |
| 3157 | } else { |
| 3158 | LOG(INFO) << StrippedCommandLine(); |
| 3159 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3160 | |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 3161 | Dex2Oat::ScopedDex2oatReporting sdr(*dex2oat.get()); |
| 3162 | |
| 3163 | if (sdr.ErrorReporting()) { |
| 3164 | dex2oat->EraseOutputFiles(); |
| 3165 | return dex2oat::ReturnCode::kOther; |
| 3166 | } |
| 3167 | |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 3168 | dex2oat::ReturnCode setup_code = dex2oat->Setup(); |
| 3169 | if (setup_code != dex2oat::ReturnCode::kNoFailure) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 3170 | dex2oat->EraseOutputFiles(); |
Andreas Gampe | f788297 | 2017-03-20 16:35:24 -0700 | [diff] [blame] | 3171 | return setup_code; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 3174 | // TODO: Due to the cyclic dependencies, profile loading and verifying are |
| 3175 | // being done separately. Refactor and place the two next to each other. |
| 3176 | // If verification fails, we don't abort the compilation and instead log an |
| 3177 | // error. |
| 3178 | // TODO(b/62602192, b/65260586): We should consider aborting compilation when |
| 3179 | // the profile verification fails. |
| 3180 | // Note: If dex2oat fails, installd will remove the oat files causing the app |
| 3181 | // to fallback to apk with possible in-memory extraction. We want to avoid |
| 3182 | // that, and thus we're lenient towards profile corruptions. |
Calin Juravle | 028c7ef | 2021-05-26 15:37:00 -0700 | [diff] [blame] | 3183 | if (dex2oat->DoProfileGuidedOptimizations()) { |
Shubham Ajmera | 188b2bf | 2017-09-20 15:53:35 -0700 | [diff] [blame] | 3184 | dex2oat->VerifyProfileData(); |
| 3185 | } |
| 3186 | |
Wojciech Staszkiewicz | 529827d | 2016-09-15 10:30:30 -0700 | [diff] [blame] | 3187 | // Helps debugging on device. Can be used to determine which dalvikvm instance invoked a dex2oat |
| 3188 | // instance. Used by tools/bisection_search/bisection_search.py. |
Wojciech Staszkiewicz | 0a4c75f | 2016-09-13 12:06:36 -0700 | [diff] [blame] | 3189 | VLOG(compiler) << "Running dex2oat (parent PID = " << getppid() << ")"; |
| 3190 | |
Nicolas Geoffray | 1ece299 | 2021-01-06 17:02:04 +0000 | [diff] [blame] | 3191 | dex2oat::ReturnCode result = DoCompilation(*dex2oat); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3192 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3193 | return result; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3194 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3195 | } // namespace art |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 3196 | |
| 3197 | int main(int argc, char** argv) { |
Andreas Gampe | 7b56591 | 2017-03-20 15:48:52 -0700 | [diff] [blame] | 3198 | int result = static_cast<int>(art::Dex2oat(argc, argv)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3199 | // Everything was done, do an explicit exit here to avoid running Runtime destructors that take |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 3200 | // time (bug 10645725) unless we're a debug or instrumented build or running on a memory tool. |
| 3201 | // Note: The Dex2Oat class should not destruct the runtime in this case. |
| 3202 | if (!art::kIsDebugBuild && !art::kIsPGOInstrumentation && !art::kRunningOnMemoryTool) { |
Roland Levillain | bdf1737 | 2021-05-06 00:19:19 +0100 | [diff] [blame] | 3203 | art::FastExit(result); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 3204 | } |
| 3205 | return result; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 3206 | } |