blob: 9e6103b4249e5a60d555d0e145f73d9a69412db1 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 Gamped687e372015-04-28 23:16:03 -070017#include <inttypes.h>
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +000018#include <log/log.h>
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22
Andreas Gampe097f34c2017-08-23 08:57:51 -070023#include <forward_list>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include <fstream>
25#include <iostream>
Andreas Gampe67f02822016-06-24 21:05:23 -070026#include <limits>
Victor Hsieh2bac0492021-08-19 13:16:00 -070027#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include <sstream>
29#include <string>
Andreas Gampe097f34c2017-08-23 08:57:51 -070030#include <type_traits>
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include <vector>
32
Orion Hodsonffc791c2019-11-14 19:19:28 +000033#if defined(__linux__)
34#include <sched.h>
35#if defined(__arm__)
Vladimir Markof94b7812014-06-05 15:48:04 +010036#include <sys/personality.h>
37#include <sys/utsname.h>
Orion Hodsonffc791c2019-11-14 19:19:28 +000038#endif // __arm__
Vladimir Markof94b7812014-06-05 15:48:04 +010039#endif
40
Orion Hodsonffc791c2019-11-14 19:19:28 +000041#include "android-base/parseint.h"
Jiakai Zhang19f95682021-12-06 16:19:39 +000042#include "android-base/scopeguard.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080044#include "android-base/strings.h"
Nicolas Geoffray1ece2992021-01-06 17:02:04 +000045#include "android-base/unique_fd.h"
Vladimir Markod1f73512020-04-02 10:50:35 +010046#include "aot_class_linker.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080047#include "arch/instruction_set_features.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000048#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070049#include "base/callee_save_type.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070050#include "base/dumpable.h"
Jiakai Zhang19f95682021-12-06 16:19:39 +000051#include "base/fast_exit.h"
David Sehr891a50e2017-10-27 17:01:07 -070052#include "base/file_utils.h"
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +000053#include "base/globals.h"
David Sehr67bf42e2018-02-26 16:43:04 -080054#include "base/leb128.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080055#include "base/macros.h"
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +000056#include "base/memory_tool.h"
David Sehrc431b9d2018-03-02 12:01:51 -080057#include "base/mutex.h"
58#include "base/os.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000059#include "base/scoped_flock.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070060#include "base/stl_util.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010061#include "base/time_utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070062#include "base/timing_logger.h"
63#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080064#include "base/utils.h"
David Sehr79e26072018-04-06 17:58:50 -070065#include "base/zip_archive.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070066#include "class_linker.h"
Calin Juravle1ce70852017-06-28 10:59:03 -070067#include "class_loader_context.h"
Andreas Gampe097f34c2017-08-23 08:57:51 -070068#include "cmdline_parser.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000069#include "compiler.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000070#include "compiler_callbacks.h"
David Srbecky09c2a6b2016-03-11 17:11:44 +000071#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000072#include "debug/method_debug_info.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080073#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080074#include "dex/dex_file-inl.h"
Vladimir Marko21910692019-11-06 13:27:03 +000075#include "dex/dex_file_loader.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000076#include "dex/quick_compiler_callbacks.h"
77#include "dex/verification_results.h"
Andreas Gampe097f34c2017-08-23 08:57:51 -070078#include "dex2oat_options.h"
David Srbecky2faab002019-02-12 16:35:48 +000079#include "dexlayout.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070080#include "driver/compiler_driver.h"
Brian Carlstrom6449c622014-02-10 23:48:36 -080081#include "driver/compiler_options.h"
Andreas Gampe097f34c2017-08-23 08:57:51 -070082#include "driver/compiler_options_map-inl.h"
Andreas Gampe88ec7f42014-11-05 10:18:32 -080083#include "elf_file.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070084#include "gc/space/image_space.h"
85#include "gc/space/space-inl.h"
Mathieu Chartier72041a02017-07-14 18:23:25 -070086#include "gc/verification.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070087#include "interpreter/unstarted_runtime.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010088#include "jni/java_vm_ext.h"
Vladimir Marko74527972016-11-29 15:57:32 +000089#include "linker/elf_writer.h"
90#include "linker/elf_writer_quick.h"
Vladimir Marko74527972016-11-29 15:57:32 +000091#include "linker/image_writer.h"
Vladimir Marko944da602016-02-19 12:27:55 +000092#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko74527972016-11-29 15:57:32 +000093#include "linker/oat_writer.h"
Vladimir Marko37d6d772020-03-05 17:13:28 +000094#include "mirror/class-alloc-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070095#include "mirror/class_loader.h"
96#include "mirror/object-inl.h"
97#include "mirror/object_array-inl.h"
Vladimir Markod3d00c02019-11-07 15:09:07 +000098#include "oat.h"
Andreas Gampe2ff3b972017-06-05 18:14:53 -070099#include "oat_file.h"
Jeff Haob11ffb72016-04-07 15:40:54 -0700100#include "oat_file_assistant.h"
Nicolas Geoffray1d4f0092020-08-07 14:01:05 +0100101#include "palette/palette.h"
David Sehr82d046e2018-04-23 08:14:19 -0700102#include "profile/profile_compilation_info.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103#include "runtime.h"
Nicolas Geoffray48eb8392022-02-24 16:20:18 +0000104#include "runtime_intrinsics.h"
Vladimir Marko49b0f452015-12-10 13:49:19 +0000105#include "runtime_options.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -0700106#include "scoped_thread_state_change-inl.h"
David Srbecky2faab002019-02-12 16:35:48 +0000107#include "stream/buffered_output_stream.h"
108#include "stream/file_output_stream.h"
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000109#include "vdex_file.h"
David Brazdilca3c8c32016-09-06 14:04:48 +0100110#include "verifier/verifier_deps.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111#include "well_known_classes.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112
113namespace art {
114
Nicolas Geoffray2cee2132020-08-24 15:31:52 +0100115namespace 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 Gampe46ee31b2016-12-14 10:11:49 -0800123using android::base::StringAppendV;
124using android::base::StringPrintf;
Vladimir Marko91f10322018-12-07 18:04:10 +0000125using gc::space::ImageSpace;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800126
Andreas Gampee1459ae2016-06-29 09:36:30 -0700127static constexpr size_t kDefaultMinDexFilesForSwap = 2;
128static constexpr size_t kDefaultMinDexFileCumulativeSizeForSwap = 20 * MB;
129
Mathieu Chartier010126f2017-07-15 15:44:18 -0700130// Compiler filter override for very large apps.
131static constexpr CompilerFilter::Filter kLargeAppFilter = CompilerFilter::kVerify;
132
Brian Carlstrom6449c622014-02-10 23:48:36 -0800133static int original_argc;
134static char** original_argv;
135
136static std::string CommandLine() {
137 std::vector<std::string> command;
Andreas Gampe2a487eb2018-11-19 11:41:22 -0800138 command.reserve(original_argc);
Brian Carlstrom6449c622014-02-10 23:48:36 -0800139 for (int i = 0; i < original_argc; ++i) {
140 command.push_back(original_argv[i]);
141 }
Andreas Gampe9186ced2016-12-12 14:28:21 -0800142 return android::base::Join(command, ' ');
Brian Carlstrom6449c622014-02-10 23:48:36 -0800143}
144
Andreas Gampe046c7062015-05-18 23:22:54 -0700145// 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).
148static std::string StrippedCommandLine() {
149 std::vector<std::string> command;
150
Calin Juravle07c6d722017-06-07 17:06:12 +0000151 // Do a pre-pass to look for zip-fd and the compiler filter.
Andreas Gampe046c7062015-05-18 23:22:54 -0700152 bool saw_zip_fd = false;
Calin Juravle07c6d722017-06-07 17:06:12 +0000153 bool saw_compiler_filter = false;
Andreas Gampe046c7062015-05-18 23:22:54 -0700154 for (int i = 0; i < original_argc; ++i) {
Andreas Gampe9186ced2016-12-12 14:28:21 -0800155 if (android::base::StartsWith(original_argv[i], "--zip-fd=")) {
Andreas Gampe046c7062015-05-18 23:22:54 -0700156 saw_zip_fd = true;
Andreas Gampe046c7062015-05-18 23:22:54 -0700157 }
Calin Juravle07c6d722017-06-07 17:06:12 +0000158 if (android::base::StartsWith(original_argv[i], "--compiler-filter=")) {
159 saw_compiler_filter = true;
Calin Juravle07c6d722017-06-07 17:06:12 +0000160 }
Andreas Gampe046c7062015-05-18 23:22:54 -0700161 }
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 Gampe9186ced2016-12-12 14:28:21 -0800172 if (android::base::StartsWith(original_argv[i], "--instruction-set")) {
Andreas Gampe046c7062015-05-18 23:22:54 -0700173 continue;
174 }
175
176 // The boot image is dropped.
Andreas Gampe9186ced2016-12-12 14:28:21 -0800177 if (android::base::StartsWith(original_argv[i], "--boot-image=")) {
Andreas Gampe046c7062015-05-18 23:22:54 -0700178 continue;
179 }
180
Mathieu Chartier97590cc2016-02-03 15:50:29 -0800181 // The image format is dropped.
Andreas Gampe9186ced2016-12-12 14:28:21 -0800182 if (android::base::StartsWith(original_argv[i], "--image-format=")) {
Mathieu Chartier97590cc2016-02-03 15:50:29 -0800183 continue;
184 }
185
Andreas Gampe046c7062015-05-18 23:22:54 -0700186 // 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 Chartiera90c7722015-10-29 15:41:36 -0700190 // Drop anything --zip-X, --dex-X, --oat-X, --swap-X, or --app-image-X
Andreas Gampe9186ced2016-12-12 14:28:21 -0800191 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 Gampe046c7062015-05-18 23:22:54 -0700196 continue;
197 }
198 }
199
200 command.push_back(original_argv[i]);
201 }
202
Calin Juravle07c6d722017-06-07 17:06:12 +0000203 if (!saw_compiler_filter) {
204 command.push_back("--compiler-filter=" +
205 CompilerFilter::NameOfFilter(CompilerFilter::kDefaultCompilerFilter));
206 }
207
Andreas Gampe046c7062015-05-18 23:22:54 -0700208 // Construct the final output.
209 if (command.size() <= 1U) {
Martin Stjernholmad909af2019-07-16 17:02:44 +0100210 // It seems only "/apex/com.android.art/bin/dex2oat" is left, or not
Roland Levillain38a938e2018-09-21 10:55:51 +0100211 // even that. Use a pretty line.
Andreas Gampe046c7062015-05-18 23:22:54 -0700212 return "Starting dex2oat.";
213 }
Andreas Gampe9186ced2016-12-12 14:28:21 -0800214 return android::base::Join(command, ' ');
Andreas Gampe046c7062015-05-18 23:22:54 -0700215}
216
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217static void UsageErrorV(const char* fmt, va_list ap) {
218 std::string error;
219 StringAppendV(&error, fmt, ap);
220 LOG(ERROR) << error;
221}
222
223static 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 Gampe794ad762015-02-23 08:12:24 -0800230NO_RETURN static void Usage(const char* fmt, ...) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 va_list ap;
232 va_start(ap, fmt);
233 UsageErrorV(fmt, ap);
234 va_end(ap);
235
Brian Carlstrom6449c622014-02-10 23:48:36 -0800236 UsageError("Command: %s", CommandLine().c_str());
237
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 UsageError("Usage: dex2oat [options]...");
239 UsageError("");
Alex Light0d47a822020-08-25 09:16:34 -0700240
241 std::stringstream oss;
242 VariableIndentationOutputStream vios(&oss);
243 auto parser = CreateDex2oatArgumentParser();
244 parser.DumpHelp(vios);
245 UsageError(oss.str().c_str());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 std::cerr << "See log for usage error information\n";
247 exit(EXIT_FAILURE);
248}
249
Orion Hodsonffc791c2019-11-14 19:19:28 +0000250
251// Set CPU affinity from a string containing a comma-separated list of numeric CPU identifiers.
252static 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 Carlstrom7940e442013-07-12 13:46:57 -0700280// 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.
283class WatchDog {
Brian Carlstrom95b033b2014-12-03 22:29:37 -0800284// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285#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 Gampe2e8a2562017-01-18 20:39:02 -0800299 explicit WatchDog(int64_t timeout_in_milliseconds)
Andreas Gampe0f54b0d2017-12-27 08:37:02 -0800300 : timeout_in_milliseconds_(timeout_in_milliseconds),
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800301 shutting_down_(false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 const char* reason = "dex2oat watch dog thread startup";
Kenny Root51316382014-05-13 14:59:37 -0700303 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason);
Mathieu Chartierdbf44402017-02-22 16:07:23 -0800304#ifndef __APPLE__
305 pthread_condattr_t condattr;
liulvping8f7101e2017-01-16 15:14:43 +0800306 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 Chartierdbf44402017-02-22 16:07:23 -0800310#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 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 Carlstrom7940e442013-07-12 13:46:57 -0700316 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 Root51316382014-05-13 14:59:37 -0700322 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323
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 Fukuia72ca6c2019-03-25 17:39:07 +0900328 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 Gampe2e8a2562017-01-18 20:39:02 -0800335 // 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 Carlstrom7940e442013-07-12 13:46:57 -0700352 private:
353 static void* CallBack(void* arg) {
354 WatchDog* self = reinterpret_cast<WatchDog*>(arg);
355 ::art::SetThreadName("dex2oat watch dog");
356 self->Wait();
Kenny Root51316382014-05-13 14:59:37 -0700357 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 }
359
Andreas Gampe794ad762015-02-23 08:12:24 -0800360 NO_RETURN static void Fatal(const std::string& message) {
Andreas Gamped687e372015-04-28 23:16:03 -0700361 // 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 Wasilczyk8d840902017-12-20 08:19:53 -0800365 LogHelper::LogLineLowStack(__FILE__, __LINE__, LogSeverity::FATAL, message.c_str());
366
Andreas Gampe48fde372017-06-06 07:43:46 -0700367 // 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 Fukuia72ca6c2019-03-25 17:39:07 +0900370 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 Gampecd607312017-06-05 17:03:05 -0700377 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 exit(1);
379 }
380
381 void Wait() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 timespec timeout_ts;
Mathieu Chartierdbf44402017-02-22 16:07:23 -0800383#if defined(__APPLE__)
384 InitTimeSpec(true, CLOCK_REALTIME, timeout_in_milliseconds_, 0, &timeout_ts);
385#else
liulvping8f7101e2017-01-16 15:14:43 +0800386 InitTimeSpec(true, CLOCK_MONOTONIC, timeout_in_milliseconds_, 0, &timeout_ts);
Mathieu Chartierdbf44402017-02-22 16:07:23 -0800387#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 const char* reason = "dex2oat watch dog thread waiting";
389 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
390 while (!shutting_down_) {
Josh Gao2d899c42018-10-17 16:03:42 -0700391 int rc = pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts);
392 if (rc == EINTR) {
393 continue;
394 } else if (rc == ETIMEDOUT) {
Roland Levillain60abdd92021-10-28 17:53:38 +0100395 Fatal(StringPrintf("dex2oat did not finish after %" PRId64 " milliseconds",
396 timeout_in_milliseconds_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 } else if (rc != 0) {
Roland Levillaind6f8c8b2018-12-18 15:27:28 +0000398 std::string message(StringPrintf("pthread_cond_timedwait failed: %s", strerror(rc)));
Greg Kaiserf276f952019-03-26 08:15:44 -0700399 Fatal(message);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 }
401 }
402 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
403 }
404
Koji Fukuia72ca6c2019-03-25 17:39:07 +0900405 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 Carlstrom7940e442013-07-12 13:46:57 -0700416 // 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 Gampef45d61c2017-06-07 10:29:33 -0700421
422 const int64_t timeout_in_milliseconds_;
423 bool shutting_down_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424};
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425
Koji Fukuia72ca6c2019-03-25 17:39:07 +0900426pthread_mutex_t WatchDog::runtime_mutex_ = PTHREAD_MUTEX_INITIALIZER;
427Runtime* WatchDog::runtime_ = nullptr;
428
Vladimir Marko37d6d772020-03-05 17:13:28 +0000429// 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.
437class 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 Marko7bbbe252020-03-20 12:14:55 +0000449 field_ = ((klass_ != nullptr) && klass_->IsVisiblyInitialized())
Vladimir Marko37d6d772020-03-05 17:13:28 +0000450 ? 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 Marko7bbbe252020-03-20 12:14:55 +0000473 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 Marko37d6d772020-03-05 17:13:28 +0000480 }
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 Holk94a810b2020-05-06 14:33:16 -0700499class 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 Levillainbbc6e7e2018-08-24 16:58:47 +0100508class Dex2Oat final {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800509 public:
Jiakai Zhang19f95682021-12-06 16:19:39 +0000510 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 Zhang19f95682021-12-06 16:19:39 +0000528 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 Stjernholmd4aba912022-01-25 21:05:47 +0000549 profile_load_attempted_(false),
550 should_report_dex2oat_compilation_(false) {}
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800551
552 ~Dex2Oat() {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800553 // Log completion time before deleting the runtime_, because this accesses
554 // the runtime.
555 LogCompletionTime();
556
Roland Levillain05e34f42018-05-24 13:19:05 +0000557 if (!kIsDebugBuild && !(kRunningOnMemoryTool && kMemoryToolDetectsLeaks)) {
Vladimir Marko307dac92015-10-20 11:20:09 +0100558 // 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 Solanes79b74d72021-10-12 13:17:46 +0100560 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 Gampeafaf7f82018-10-16 11:32:38 -0700565 image_writer_.release(); // NOLINT
Vladimir Marko307dac92015-10-20 11:20:09 +0100566 for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files_) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700567 dex_file.release(); // NOLINT
Vladimir Marko307dac92015-10-20 11:20:09 +0100568 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100569 new std::vector<MemMap>(std::move(opened_dex_files_maps_)); // Leak MemMaps.
David Brazdil7b49e6c2016-09-01 11:06:18 +0100570 for (std::unique_ptr<File>& vdex_file : vdex_files_) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700571 vdex_file.release(); // NOLINT
David Brazdil7b49e6c2016-09-01 11:06:18 +0100572 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800573 for (std::unique_ptr<File>& oat_file : oat_files_) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700574 oat_file.release(); // NOLINT
Jeff Haodcdc85b2015-12-04 14:06:18 -0800575 }
Andreas Gampeafaf7f82018-10-16 11:32:38 -0700576 runtime_.release(); // NOLINT
577 verification_results_.release(); // NOLINT
578 key_value_store_.release(); // NOLINT
Vladimir Markof94b7812014-06-05 15:48:04 +0100579 }
Alex Light4531afd2021-03-30 17:13:30 -0700580
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 Carlstrom7940e442013-07-12 13:46:57 -0700586 }
587
Roland Levillain9ec5e222015-08-17 20:18:41 +0100588 struct ParserOptions {
Vladimir Markod011d812018-11-29 12:35:24 +0000589 std::vector<std::string> oat_symbols;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800590 std::string boot_image_filename;
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800591 int64_t watch_dog_timeout_in_ms = -1;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800592 bool watch_dog_enabled = true;
Nicolas Geoffray1412dfa2015-03-20 14:48:13 +0000593 bool requested_specific_compiler = false;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800594 std::string error_msg;
Roland Levillain9ec5e222015-08-17 20:18:41 +0100595 };
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800596
Andreas Gampe097f34c2017-08-23 08:57:51 -0700597 void ParseBase(const std::string& option) {
Roland Levillain9ec5e222015-08-17 20:18:41 +0100598 char* end;
Andreas Gampe097f34c2017-08-23 08:57:51 -0700599 image_base_ = strtoul(option.c_str(), &end, 16);
600 if (end == option.c_str() || *end != '\0') {
Roland Levillain9ec5e222015-08-17 20:18:41 +0100601 Usage("Failed to parse hexadecimal value for option %s", option.data());
602 }
603 }
604
Shubham Ajmera188b2bf2017-09-20 15:53:35 -0700605 bool VerifyProfileData() {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100606 return profile_compilation_info_->VerifyProfileData(compiler_options_->dex_files_for_oat_file_);
Shubham Ajmera188b2bf2017-09-20 15:53:35 -0700607 }
608
Andreas Gampe097f34c2017-08-23 08:57:51 -0700609 void ParseInstructionSetVariant(const std::string& option, ParserOptions* parser_options) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100610 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 Levillain9ec5e222015-08-17 20:18:41 +0100613 Usage("%s", parser_options->error_msg.c_str());
614 }
615 }
616
Andreas Gampe097f34c2017-08-23 08:57:51 -0700617 void ParseInstructionSetFeatures(const std::string& option, ParserOptions* parser_options) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100618 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 Levillain9ec5e222015-08-17 20:18:41 +0100622 Usage("Problem initializing default instruction set features variant: %s",
623 parser_options->error_msg.c_str());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700624 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800625 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100626 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 Gampe097f34c2017-08-23 08:57:51 -0700630 Usage("Error parsing '%s': %s", option.c_str(), parser_options->error_msg.c_str());
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800631 }
632 }
633
Jeff Hao436183f2016-01-12 16:36:50 -0800634 void ProcessOptions(ParserOptions* parser_options) {
Vladimir Marko695348f2020-05-19 14:42:02 +0100635 compiler_options_->compiler_type_ = CompilerOptions::CompilerType::kAotCompiler;
Vladimir Markoa2da9b92018-10-10 14:21:55 +0100636 compiler_options_->compile_pic_ = true; // All AOT compilation is PIC.
Vladimir Marko21910692019-11-06 13:27:03 +0000637
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 Marko9c4b9702018-11-14 15:09:02 +0000650 DCHECK(compiler_options_->image_type_ == CompilerOptions::ImageType::kNone);
Vladimir Markod0036ac2019-11-21 11:47:12 +0000651 if (!image_filenames_.empty() || image_fd_ != -1) {
Ulya Trafimovichc124d1d2020-01-27 14:50:38 +0000652 // If no boot image is provided, then dex2oat is compiling the primary boot image,
Nicolas Geoffray4cbb51a2020-02-07 11:25:54 +0000653 // otherwise it is compiling the boot image extension.
Ulya Trafimovichc124d1d2020-01-27 14:50:38 +0000654 compiler_options_->image_type_ = boot_image_filename_.empty()
655 ? CompilerOptions::ImageType::kBootImage
Nicolas Geoffray4cbb51a2020-02-07 11:25:54 +0000656 : CompilerOptions::ImageType::kBootImageExtension;
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000657 }
658 if (app_image_fd_ != -1 || !app_image_file_name_.empty()) {
Vladimir Marko21910692019-11-06 13:27:03 +0000659 if (compiler_options_->IsBootImage() || compiler_options_->IsBootImageExtension()) {
Vladimir Markod0036ac2019-11-21 11:47:12 +0000660 Usage("Can't have both (--image or --image-fd) and (--app-image-fd or --app-image-file)");
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000661 }
662 compiler_options_->image_type_ = CompilerOptions::ImageType::kAppImage;
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700663 }
664
Vladimir Markod0036ac2019-11-21 11:47:12 +0000665 if (!image_filenames_.empty() && image_fd_ != -1) {
666 Usage("Can't have both --image and --image-fd");
667 }
668
Jeff Haodcdc85b2015-12-04 14:06:18 -0800669 if (oat_filenames_.empty() && oat_fd_ == -1) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800670 Usage("Output must be supplied with either --oat-file or --oat-fd");
671 }
672
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000673 if (input_vdex_fd_ != -1 && !input_vdex_.empty()) {
674 Usage("Can't have both --input-vdex-fd and --input-vdex");
675 }
676
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100677 if (output_vdex_fd_ != -1 && !output_vdex_.empty()) {
678 Usage("Can't have both --output-vdex-fd and --output-vdex");
679 }
680
Jeff Haodcdc85b2015-12-04 14:06:18 -0800681 if (!oat_filenames_.empty() && oat_fd_ != -1) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800682 Usage("--oat-file should not be used with --oat-fd");
683 }
684
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000685 if ((output_vdex_fd_ == -1) != (oat_fd_ == -1)) {
Sascha Roth5f71c652018-04-10 00:27:15 +0200686 Usage("VDEX and OAT output must be specified either with one --oat-file "
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000687 "or with --oat-fd and --output-vdex-fd file descriptors");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100688 }
689
Vladimir Markod0036ac2019-11-21 11:47:12 +0000690 if ((image_fd_ != -1) && (oat_fd_ == -1)) {
691 Usage("--image-fd must be used with --oat_fd and --output_vdex_fd");
692 }
693
Roland Levillain9ec5e222015-08-17 20:18:41 +0100694 if (!parser_options->oat_symbols.empty() && oat_fd_ != -1) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800695 Usage("--oat-symbols should not be used with --oat-fd");
696 }
697
Roland Levillain9ec5e222015-08-17 20:18:41 +0100698 if (!parser_options->oat_symbols.empty() && is_host_) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800699 Usage("--oat-symbols should not be used with --host");
700 }
701
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +0000702 if (output_vdex_fd_ != -1 && !image_filenames_.empty()) {
703 Usage("--output-vdex-fd should not be used with --image");
David Brazdil7b49e6c2016-09-01 11:06:18 +0100704 }
705
Jeff Haodcdc85b2015-12-04 14:06:18 -0800706 if (oat_fd_ != -1 && !image_filenames_.empty()) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800707 Usage("--oat-fd should not be used with --image");
708 }
709
Jeff Haodcdc85b2015-12-04 14:06:18 -0800710 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 Marko21910692019-11-06 13:27:03 +0000719 if (!IsBootImage() && boot_image_filename_.empty()) {
720 DCHECK(!IsBootImageExtension());
Orion Hodson971068d2021-06-30 21:17:53 +0100721 boot_image_filename_ =
722 GetDefaultBootImageLocation(android_root_, /*deny_art_apex_data_files=*/false);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800723 }
724
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800725 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 Markod011d812018-11-29 12:35:24 +0000738 dex_locations_ = dex_filenames_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800739 } else if (dex_locations_.size() != dex_filenames_.size()) {
740 Usage("--dex-location arguments do not match --dex-file arguments");
741 }
742
Jeff Haodcdc85b2015-12-04 14:06:18 -0800743 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 Hsieh826e6662021-05-17 15:40:13 -0700749 if (!dex_fds_.empty() && dex_fds_.size() != dex_filenames_.size()) {
750 Usage("--dex-fd arguments do not match --dex-file arguments");
751 }
752
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800753 if (zip_fd_ != -1 && zip_location_.empty()) {
754 Usage("--zip-location should be supplied with --zip-fd");
755 }
756
Vladimir Marko49b0f452015-12-10 13:49:19 +0000757 if (boot_image_filename_.empty()) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800758 if (image_base_ == 0) {
Vladimir Marko66235942019-06-03 16:05:17 +0100759 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 Gampe88ec7f42014-11-05 10:18:32 -0800764 }
765 }
766
Vladimir Markod0036ac2019-11-21 11:47:12 +0000767 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 Markof67e8c32022-03-17 12:55:27 +0000773 // This shall pass the checks below.
774 compiler_options_->multi_image_ = IsBootImage() || IsBootImageExtension();
Vladimir Markod0036ac2019-11-21 11:47:12 +0000775 }
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +0000776 // On target we support generating a single image for the primary boot image.
777 if (!kIsTargetBuild) {
Vladimir Markof67e8c32022-03-17 12:55:27 +0000778 if (IsBootImage() && !compiler_options_->multi_image_) {
Jiakai Zhangfb8cdb52021-12-06 18:51:28 +0000779 Usage("--single-image specified for primary boot image on host");
780 }
Vladimir Markod0036ac2019-11-21 11:47:12 +0000781 }
Vladimir Markof67e8c32022-03-17 12:55:27 +0000782 if (IsAppImage() && compiler_options_->multi_image_) {
Vladimir Markod0036ac2019-11-21 11:47:12 +0000783 Usage("--multi-image specified for app image");
784 }
785
Vladimir Markof67e8c32022-03-17 12:55:27 +0000786 if (image_fd_ != -1 && compiler_options_->multi_image_) {
Vladimir Markod0036ac2019-11-21 11:47:12 +0000787 Usage("--single-image not specified for --image-fd");
788 }
789
Jiakai Zhang19f95682021-12-06 16:19:39 +0000790 const bool have_profile_file = !profile_files_.empty();
791 const bool have_profile_fd = !profile_file_fds_.empty();
Mathieu Chartiera8077802016-03-16 19:08:31 -0700792 if (have_profile_file && have_profile_fd) {
Jiakai Zhang19f95682021-12-06 16:19:39 +0000793 Usage("Profile files should not be specified with both --profile-file-fd and --profile-file");
Calin Juravle998c2162015-12-21 15:39:33 +0200794 }
795
Roland Levillain9ec5e222015-08-17 20:18:41 +0100796 if (!parser_options->oat_symbols.empty()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800797 oat_unstripped_ = std::move(parser_options->oat_symbols);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800798 }
799
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000800 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 Markoa0431112018-06-25 09:32:54 +0100803 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 Rogersd582fa42014-11-05 23:46:43 -0800806 Usage("Problem initializing default instruction set features variant: %s",
Roland Levillain9ec5e222015-08-17 20:18:41 +0100807 parser_options->error_msg.c_str());
Ian Rogersd582fa42014-11-05 23:46:43 -0800808 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800809 }
810
Vladimir Markoa0431112018-06-25 09:32:54 +0100811 if (compiler_options_->instruction_set_ == kRuntimeISA) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800812 std::unique_ptr<const InstructionSetFeatures> runtime_features(
813 InstructionSetFeatures::FromCppDefines());
Vladimir Markoa0431112018-06-25 09:32:54 +0100814 if (!compiler_options_->GetInstructionSetFeatures()->Equals(runtime_features.get())) {
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000815 LOG(WARNING) << "Mismatch between dex2oat instruction set features to use ("
Vladimir Markoa0431112018-06-25 09:32:54 +0100816 << *compiler_options_->GetInstructionSetFeatures()
xueliang.zhong7f88c1a2018-11-06 11:42:41 +0000817 << ") and those from CPP defines (" << *runtime_features
Vladimir Markoa0431112018-06-25 09:32:54 +0100818 << ") for the command line:\n" << CommandLine();
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800819 }
820 }
821
Victor Hsieh4ab59232021-06-10 12:24:02 -0700822 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 Geoffray4c1e0ca2022-03-18 16:11:39 +0000826 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 Hodsonffc791c2019-11-14 19:19:28 +0000830 if (!cpu_set_.empty()) {
831 SetCpuAffinity(cpu_set_);
832 }
833
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000834 if (compiler_options_->inline_max_code_units_ == CompilerOptions::kUnsetInlineMaxCodeUnits) {
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000835 compiler_options_->inline_max_code_units_ = CompilerOptions::kDefaultInlineMaxCodeUnits;
Roland Levillaina215b952015-08-07 11:38:32 +0100836 }
837
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800838 // Checks are all explicit until we know the architecture.
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800839 // Set the compilation target's implicit checks options.
Vladimir Markoa0431112018-06-25 09:32:54 +0100840 switch (compiler_options_->GetInstructionSet()) {
Vladimir Marko7039c112018-04-05 17:59:39 +0100841 case InstructionSet::kArm64:
Vladimir Marko9e143272022-01-04 09:28:42 +0000842 // 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 Marko7039c112018-04-05 17:59:39 +0100845 FALLTHROUGH_INTENDED;
Vladimir Marko33bff252017-11-01 14:35:42 +0000846 case InstructionSet::kArm:
847 case InstructionSet::kThumb2:
Vladimir Marko33bff252017-11-01 14:35:42 +0000848 case InstructionSet::kX86:
849 case InstructionSet::kX86_64:
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000850 compiler_options_->implicit_null_checks_ = true;
851 compiler_options_->implicit_so_checks_ = true;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800852 break;
853
854 default:
855 // Defaults are correct.
856 break;
857 }
858
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800859 // Done with usage checks, enable watchdog if requested
Roland Levillain9ec5e222015-08-17 20:18:41 +0100860 if (parser_options->watch_dog_enabled) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800861 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 Gampe88ec7f42014-11-05 10:18:32 -0800865 }
866
867 // Fill some values into the key-value store for the oat header.
Eric Holk94a810b2020-05-06 14:33:16 -0700868 key_value_store_.reset(new OatKeyValueStore());
Andreas Gampeace0dc12016-01-20 13:33:13 -0800869
Vladimir Marko21910692019-11-06 13:27:03 +0000870 // Automatically force determinism for the boot image and boot image extensions in a host build.
871 if (!kIsTargetBuild && (IsBootImage() || IsBootImageExtension())) {
Vladimir Marko78342412019-07-25 17:28:02 +0100872 force_determinism_ = true;
Andreas Gampeace0dc12016-01-20 13:33:13 -0800873 }
874 compiler_options_->force_determinism_ = force_determinism_;
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700875
David Sehrfe57c2b2020-03-27 14:58:54 +0000876 compiler_options_->check_linkage_conditions_ = check_linkage_conditions_;
877 compiler_options_->crash_on_linkage_violation_ = crash_on_linkage_violation_;
878
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700879 if (passes_to_run_filename_ != nullptr) {
Vladimir Marko54159c62018-06-20 14:30:08 +0100880 passes_to_run_ = ReadCommentedInputFromFile<std::vector<std::string>>(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700881 passes_to_run_filename_,
Vladimir Marko54159c62018-06-20 14:30:08 +0100882 nullptr); // No post-processing.
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700883 if (passes_to_run_.get() == nullptr) {
884 Usage("Failed to read list of passes to run.");
885 }
886 }
Vladimir Markof6afcc92020-02-05 10:10:03 +0000887
Jiakai Zhangd294d9d2021-12-22 15:22:33 +0000888 // 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 Markof6afcc92020-02-05 10:10:03 +0000897 }
Jiakai Zhangd294d9d2021-12-22 15:22:33 +0000898 }
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 Markof6afcc92020-02-05 10:10:03 +0000905 }
906
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700907 compiler_options_->passes_to_run_ = passes_to_run_.get();
Roland Levillain9ec5e222015-08-17 20:18:41 +0100908 }
909
Jeff Hao436183f2016-01-12 16:36:50 -0800910 void ExpandOatAndImageFilenames() {
Vladimir Markod0036ac2019-11-21 11:47:12 +0000911 ArrayRef<const std::string> locations(dex_locations_);
Vladimir Markof67e8c32022-03-17 12:55:27 +0000912 if (!compiler_options_->multi_image_) {
Vladimir Markod0036ac2019-11-21 11:47:12 +0000913 locations = locations.SubArray(/*pos=*/ 0u, /*length=*/ 1u);
Jeff Hao436183f2016-01-12 16:36:50 -0800914 }
Vladimir Markod0036ac2019-11-21 11:47:12 +0000915 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 Hao436183f2016-01-12 16:36:50 -0800921
Vladimir Markod0036ac2019-11-21 11:47:12 +0000922 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 Markof67e8c32022-03-17 12:55:27 +0000928 DCHECK(!compiler_options_->multi_image_);
Vladimir Markod0036ac2019-11-21 11:47:12 +0000929 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 Markodb4b1de2018-12-07 17:18:07 +0000933 }
Vladimir Markodb4b1de2018-12-07 17:18:07 +0000934
bowen_lai8bd36c02016-10-12 13:28:55 +0800935 if (!oat_unstripped_.empty()) {
Vladimir Marko91f10322018-12-07 18:04:10 +0000936 if (oat_unstripped_[0].rfind('/') == std::string::npos) {
937 Usage("Unusable boot image symbol filename %s", oat_unstripped_[0].c_str());
bowen_lai8bd36c02016-10-12 13:28:55 +0800938 }
Vladimir Marko21910692019-11-06 13:27:03 +0000939 oat_unstripped_ = ImageSpace::ExpandMultiImageLocations(
Vladimir Markod0036ac2019-11-21 11:47:12 +0000940 locations, oat_unstripped_[0], IsBootImageExtension());
bowen_lai8bd36c02016-10-12 13:28:55 +0800941 }
Jeff Haodcdc85b2015-12-04 14:06:18 -0800942 }
943
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000944 void InsertCompileOptions(int argc, char** argv) {
Mathieu Chartierd1ab1bd2017-08-04 10:21:50 -0700945 if (!avoid_storing_invocation_) {
Vladimir Marko56581d82018-11-07 12:30:52 +0000946 std::ostringstream oss;
Mathieu Chartierd1ab1bd2017-08-04 10:21:50 -0700947 for (int i = 0; i < argc; ++i) {
948 if (i > 0) {
949 oss << ' ';
950 }
951 oss << argv[i];
Roland Levillain9ec5e222015-08-17 20:18:41 +0100952 }
Mathieu Chartierd1ab1bd2017-08-04 10:21:50 -0700953 key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str());
Roland Levillain9ec5e222015-08-17 20:18:41 +0100954 }
Eric Holk94a810b2020-05-06 14:33:16 -0700955 key_value_store_->Put(OatHeader::kDebuggableKey, compiler_options_->debuggable_);
956 key_value_store_->Put(OatHeader::kNativeDebuggableKey,
957 compiler_options_->GetNativeDebuggable());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000958 key_value_store_->Put(OatHeader::kCompilerFilter,
Eric Holk94a810b2020-05-06 14:33:16 -0700959 CompilerFilter::NameOfFilter(compiler_options_->GetCompilerFilter()));
960 key_value_store_->Put(OatHeader::kConcurrentCopying, kUseReadBarrier);
Alex Light62afcf52018-12-18 14:19:25 -0800961 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 Levillain9ec5e222015-08-17 20:18:41 +0100976 }
977
Andreas Gampe097f34c2017-08-23 08:57:51 -0700978 // 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 Markod0036ac2019-11-21 11:47:12 +00001015 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 Levillain9ec5e222015-08-17 20:18:41 +01001024 // 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 Sehrc431b9d2018-03-02 12:01:51 -08001031 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -07001032 InitLogging(argv, Runtime::Abort);
Roland Levillain9ec5e222015-08-17 20:18:41 +01001033
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +00001034 compiler_options_.reset(new CompilerOptions());
Roland Levillain9ec5e222015-08-17 20:18:41 +01001035
Andreas Gampe097f34c2017-08-23 08:57:51 -07001036 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 Chartiereafe2a52017-10-19 15:29:42 -07001048 AssignIfExists(args, M::CompactDexLevel, &compact_dex_level_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001049 AssignIfExists(args, M::DexFiles, &dex_filenames_);
1050 AssignIfExists(args, M::DexLocations, &dex_locations_);
Victor Hsieh826e6662021-05-17 15:40:13 -07001051 AssignIfExists(args, M::DexFds, &dex_fds_);
Vladimir Markod0036ac2019-11-21 11:47:12 +00001052 AssignIfExists(args, M::OatFile, &oat_filenames_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001053 AssignIfExists(args, M::OatSymbols, &parser_options->oat_symbols);
David Srbeckyde91fd42018-07-05 22:27:08 +01001054 AssignTrueIfExists(args, M::Strip, &strip_);
Vladimir Markod0036ac2019-11-21 11:47:12 +00001055 AssignIfExists(args, M::ImageFilename, &image_filenames_);
1056 AssignIfExists(args, M::ImageFd, &image_fd_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001057 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 Geoffraybaeaa9b2018-01-26 14:31:17 +00001063 AssignIfExists(args, M::DmFd, &dm_fd_);
1064 AssignIfExists(args, M::DmFile, &dm_file_location_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001065 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 Hodsonffc791c2019-11-14 19:19:28 +00001070 AssignIfExists(args, M::CpuSet, &cpu_set_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001071 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 Zhang19f95682021-12-06 16:19:39 +00001074 AssignIfExists(args, M::Profile, &profile_files_);
1075 AssignIfExists(args, M::ProfileFd, &profile_file_fds_);
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00001076 AssignIfExists(args, M::PreloadedClasses, &preloaded_classes_files_);
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00001077 AssignIfExists(args, M::PreloadedClassesFds, &preloaded_classes_fds_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001078 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 Hsieh4ab59232021-06-10 12:24:02 -07001089 AssignIfExists(args, M::DirtyImageObjectsFd, &dirty_image_objects_fd_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001090 AssignIfExists(args, M::ImageFormat, &image_storage_mode_);
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001091 AssignIfExists(args, M::CompilationReason, &compilation_reason_);
David Sehrfe57c2b2020-03-27 14:58:54 +00001092 AssignTrueIfExists(args, M::CheckLinkageConditions, &check_linkage_conditions_);
1093 AssignTrueIfExists(args, M::CrashOnLinkageViolation, &crash_on_linkage_violation_);
Alex Light4531afd2021-03-30 17:13:30 -07001094 AssignTrueIfExists(args, M::ForceAllowOjInlines, &force_allow_oj_inlines_);
Calin Juravle33787682019-07-26 14:27:18 -07001095 AssignIfExists(args, M::PublicSdk, &public_sdk_);
Nicolas Geoffrayd3c45c22021-04-27 13:21:28 +01001096 AssignIfExists(args, M::ApexVersions, &apex_versions_argument_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001097
1098 AssignIfExists(args, M::Backend, &compiler_kind_);
1099 parser_options->requested_specific_compiler = args.Exists(M::Backend);
1100
Vladimir Markoa0431112018-06-25 09:32:54 +01001101 AssignIfExists(args, M::TargetInstructionSet, &compiler_options_->instruction_set_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001102 // arm actually means thumb2.
Vladimir Markoa0431112018-06-25 09:32:54 +01001103 if (compiler_options_->instruction_set_ == InstructionSet::kArm) {
1104 compiler_options_->instruction_set_ = InstructionSet::kThumb2;
Andreas Gampe097f34c2017-08-23 08:57:51 -07001105 }
1106
1107 AssignTrueIfExists(args, M::Host, &is_host_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001108 AssignTrueIfExists(args, M::AvoidStoringInvocation, &avoid_storing_invocation_);
Alex Light62afcf52018-12-18 14:19:25 -08001109 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 Chartier792111c2018-02-15 13:02:15 -08001119 AssignIfExists(args, M::CopyDexFiles, &copy_dex_files_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001120
Vladimir Markod0036ac2019-11-21 11:47:12 +00001121 AssignTrueIfExists(args, M::MultiImage, &have_multi_image_arg_);
Vladimir Markof67e8c32022-03-17 12:55:27 +00001122 AssignIfExists(args, M::MultiImage, &compiler_options_->multi_image_);
Vladimir Markod0036ac2019-11-21 11:47:12 +00001123
Andreas Gampe097f34c2017-08-23 08:57:51 -07001124 if (args.Exists(M::ForceDeterminism)) {
Andreas Gampe097f34c2017-08-23 08:57:51 -07001125 force_determinism_ = true;
1126 }
Eric Holkbc89ed42020-04-29 19:59:24 +00001127 AssignTrueIfExists(args, M::CompileIndividually, &compile_individually_);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001128
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 Chartierf5abfc42018-03-23 21:51:54 -07001139 std::string class_loader_context_arg = *args.Get(M::ClassLoaderContext);
1140 class_loader_context_ = ClassLoaderContext::Create(class_loader_context_arg);
Andreas Gampe097f34c2017-08-23 08:57:51 -07001141 if (class_loader_context_ == nullptr) {
1142 Usage("Option --class-loader-context has an incorrect format: %s",
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001143 class_loader_context_arg.c_str());
Roland Levillain9ec5e222015-08-17 20:18:41 +01001144 }
David Brazdil89821862019-03-19 13:57:43 +00001145 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 Chartierf5abfc42018-03-23 21:51:54 -07001156 if (args.Exists(M::StoredClassLoaderContext)) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07001157 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 Chartierf5abfc42018-03-23 21:51:54 -07001160 Usage("Option --stored-class-loader-context has an incorrect format: %s",
Mathieu Chartierc4440772018-04-16 14:40:56 -07001161 stored_context_arg.c_str());
Vladimir Markod8860b42018-05-02 14:58:12 +01001162 } else if (class_loader_context_->VerifyClassLoaderContextMatch(
Mathieu Chartierc4440772018-04-16 14:40:56 -07001163 stored_context_arg,
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001164 /*verify_names*/ false,
Vladimir Markod8860b42018-05-02 14:58:12 +01001165 /*verify_checksums*/ false) != ClassLoaderContext::VerificationResult::kVerifies) {
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001166 Usage(
1167 "Option --stored-class-loader-context '%s' mismatches --class-loader-context '%s'",
Mathieu Chartierc4440772018-04-16 14:40:56 -07001168 stored_context_arg.c_str(),
1169 class_loader_context_arg.c_str());
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001170 }
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 Levillain9ec5e222015-08-17 20:18:41 +01001175 }
1176
Jiakai Zhangebf496f2021-10-11 05:04:34 +00001177 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 Zhange7815b82021-12-23 14:39:58 +00001186 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 Marko89b85eb2020-02-04 15:51:44 +00001193 // 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 Juravle028c7ef2021-05-26 15:37:00 -07001197 if (HasProfileInput()) {
Vladimir Marko89b85eb2020-02-04 15:51:44 +00001198 compiler_options_->SetCompilerFilter(CompilerFilter::kSpeedProfile);
1199 }
1200
Andreas Gampe097f34c2017-08-23 08:57:51 -07001201 if (!ReadCompilerOptions(args, compiler_options_.get(), &error_msg)) {
1202 Usage(error_msg.c_str());
1203 }
1204
Santiago Aboy Solanesac4e4a22021-11-02 11:31:08 +00001205 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 Geoffrayad67bbb2022-02-04 17:26:40 +00001212 // 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 Stjernholmd4aba912022-01-25 21:05:47 +00001218 PaletteShouldReportDex2oatCompilation(&should_report_dex2oat_compilation_);
1219 AssignTrueIfExists(args, M::ForcePaletteCompilationHooks, &should_report_dex2oat_compilation_);
1220
Jeff Hao436183f2016-01-12 16:36:50 -08001221 ProcessOptions(parser_options.get());
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001222 }
1223
Jeff Haodcdc85b2015-12-04 14:06:18 -08001224 // 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 Gampe88ec7f42014-11-05 10:18:32 -08001226 bool OpenFile() {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001227 // Prune non-existent dex files now so that we don't create empty oat files for multi-image.
1228 PruneNonExistentDexFiles();
1229
Vladimir Markod0036ac2019-11-21 11:47:12 +00001230 // 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 Hao436183f2016-01-12 16:36:50 -08001233 ExpandOatAndImageFilenames();
1234 }
1235
David Brazdil7b49e6c2016-09-01 11:06:18 +01001236 // OAT and VDEX file handling
David Brazdil7b49e6c2016-09-01 11:06:18 +01001237 if (oat_fd_ == -1) {
1238 DCHECK(!oat_filenames_.empty());
Vladimir Markod011d812018-11-29 12:35:24 +00001239 for (const std::string& oat_filename : oat_filenames_) {
1240 std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_filename.c_str()));
Calin Juravleb09be722017-09-25 16:42:48 -07001241 if (oat_file == nullptr) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001242 PLOG(ERROR) << "Failed to create oat file: " << oat_filename;
1243 return false;
1244 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01001245 if (fchmod(oat_file->Fd(), 0644) != 0) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001246 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 Geoffrayb0bbe8e2016-11-19 10:42:37 +00001251 DCHECK_EQ(input_vdex_fd_, -1);
1252 if (!input_vdex_.empty()) {
1253 std::string error_msg;
Richard Uhlerb8ab63a2017-01-31 11:27:37 +00001254 input_vdex_file_ = VdexFile::Open(input_vdex_,
1255 /* writable */ false,
1256 /* low_4gb */ false,
1257 &error_msg);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00001258 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01001259
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +00001260 DCHECK_EQ(output_vdex_fd_, -1);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01001261 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 Geoffray84caad62022-01-10 09:48:55 +00001265 use_existing_vdex_ = true;
1266 std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_filename.c_str()));
Nicolas Geoffray81f57d12016-12-20 13:17:09 +00001267 vdex_files_.push_back(std::move(vdex_file));
1268 } else {
1269 std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_filename.c_str()));
Calin Juravleb09be722017-09-25 16:42:48 -07001270 if (vdex_file == nullptr) {
Nicolas Geoffray81f57d12016-12-20 13:17:09 +00001271 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 Brazdil7b49e6c2016-09-01 11:06:18 +01001280 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001281 }
1282 } else {
Josh Gaoafeec9f2018-08-30 14:05:56 -07001283 std::unique_ptr<File> oat_file(
1284 new File(DupCloexec(oat_fd_), oat_location_, /* check_usage */ true));
1285 if (!oat_file->IsOpened()) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001286 PLOG(ERROR) << "Failed to create oat file: " << oat_location_;
1287 return false;
1288 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01001289 if (oat_file->SetLength(0) != 0) {
1290 PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed.";
Calin Juravleb09be722017-09-25 16:42:48 -07001291 oat_file->Erase();
1292 return false;
David Brazdil7b49e6c2016-09-01 11:06:18 +01001293 }
1294 oat_files_.push_back(std::move(oat_file));
1295
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00001296 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 Uhlerb8ab63a2017-01-31 11:27:37 +00001303 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 Geoffrayb0bbe8e2016-11-19 10:42:37 +00001309 // If there's any problem with the passed vdex, just warn and proceed
1310 // without it.
1311 if (input_vdex_file_ == nullptr) {
Richard Uhlerb8ab63a2017-01-31 11:27:37 +00001312 PLOG(WARNING) << "Failed opening vdex file: " << error_msg;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00001313 }
1314 }
1315 }
1316
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +00001317 DCHECK_NE(output_vdex_fd_, -1);
David Brazdil7b49e6c2016-09-01 11:06:18 +01001318 std::string vdex_location = ReplaceFileExtension(oat_location_, "vdex");
Nicolas Geoffray84caad62022-01-10 09:48:55 +00001319 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 Gaoafeec9f2018-08-30 14:05:56 -07001327 if (!vdex_file->IsOpened()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01001328 PLOG(ERROR) << "Failed to create vdex file: " << vdex_location;
Jeff Haodcdc85b2015-12-04 14:06:18 -08001329 return false;
1330 }
Nicolas Geoffray84caad62022-01-10 09:48:55 +00001331
1332 if (!use_existing_vdex_) {
Nicolas Geoffray81f57d12016-12-20 13:17:09 +00001333 if (vdex_file->SetLength(0) != 0) {
1334 PLOG(ERROR) << "Truncating vdex file " << vdex_location << " failed.";
Calin Juravleb09be722017-09-25 16:42:48 -07001335 vdex_file->Erase();
Nicolas Geoffray81f57d12016-12-20 13:17:09 +00001336 return false;
1337 }
David Brazdil7b49e6c2016-09-01 11:06:18 +01001338 }
1339 vdex_files_.push_back(std::move(vdex_file));
1340
Vladimir Markod011d812018-11-29 12:35:24 +00001341 oat_filenames_.push_back(oat_location_);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001342 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001343
Nicolas Geoffraybaeaa9b2018-01-26 14:31:17 +00001344 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 Geoffray84092812021-12-15 14:08:44 +00001356 // 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 Geoffraybaeaa9b2018-01-26 14:31:17 +00001358 if (dm_file_ != nullptr) {
Nicolas Geoffray84092812021-12-15 14:08:44 +00001359 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 Geoffraybaeaa9b2018-01-26 14:31:17 +00001366 }
1367 }
1368
David Brazdil7b49e6c2016-09-01 11:06:18 +01001369 // Swap file handling
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001370 //
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 Gaoafeec9f2018-08-30 14:05:56 -07001382 swap_fd_ = swap_file->Release();
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001383 unlink(swap_file_name_.c_str());
1384 }
Jeff Hao436183f2016-01-12 16:36:50 -08001385
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001386 return true;
1387 }
1388
David Brazdil7b49e6c2016-09-01 11:06:18 +01001389 void EraseOutputFiles() {
1390 for (auto& files : { &vdex_files_, &oat_files_ }) {
1391 for (size_t i = 0; i < files->size(); ++i) {
Nicolas Geoffray84caad62022-01-10 09:48:55 +00001392 auto& file = (*files)[i];
1393 if (file != nullptr) {
1394 if (!file->ReadOnlyMode()) {
1395 file->Erase();
1396 }
1397 file.reset();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001398 }
1399 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001400 }
Andreas Gampea650e702014-12-03 14:28:02 -08001401 }
1402
Mathieu Chartierc5dd3192015-12-09 16:38:30 -08001403 void LoadClassProfileDescriptors() {
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07001404 if (!IsImage()) {
1405 return;
1406 }
Calin Juravle028c7ef2021-05-26 15:37:00 -07001407 if (DoProfileGuidedOptimizations()) {
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001408 // TODO: The following comment looks outdated or misplaced.
Mathieu Chartier0b490842016-05-25 15:05:59 -07001409 // Filter out class path classes since we don't want to include these in the image.
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001410 HashSet<std::string> image_classes = profile_compilation_info_->GetClassDescriptors(
1411 compiler_options_->dex_files_for_oat_file_);
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001412 VLOG(compiler) << "Loaded " << image_classes.size()
Mathieu Chartierc5dd3192015-12-09 16:38:30 -08001413 << " image class descriptors from profile";
1414 if (VLOG_IS_ON(compiler)) {
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001415 for (const std::string& s : image_classes) {
Mathieu Chartierc5dd3192015-12-09 16:38:30 -08001416 LOG(INFO) << "Image class " << s;
1417 }
1418 }
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001419 compiler_options_->image_classes_.swap(image_classes);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -08001420 }
1421 }
1422
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001423 // Set up the environment for compilation. Includes starting the runtime and loading/opening the
1424 // boot class path.
Andreas Gampef7882972017-03-20 16:35:24 -07001425 dex2oat::ReturnCode Setup() {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001426 TimingLogger::ScopedTiming t("dex2oat Setup", timings_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00001427
Mathieu Chartierf68b6982019-06-27 09:40:20 -07001428 if (!PrepareDirtyObjects()) {
Andreas Gampef7882972017-03-20 16:35:24 -07001429 return dex2oat::ReturnCode::kOther;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001430 }
Brian Carlstromd76e0832013-08-29 15:17:42 -07001431
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00001432 if (!PreparePreloadedClasses()) {
1433 return dex2oat::ReturnCode::kOther;
1434 }
1435
Andreas Gampe4585f872015-03-27 23:45:15 -07001436 callbacks_.reset(new QuickCompilerCallbacks(
Vladimir Marko21910692019-11-06 13:27:03 +00001437 // 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 Gampe88ec7f42014-11-05 10:18:32 -08001441
Vladimir Marko49b0f452015-12-10 13:49:19 +00001442 RuntimeArgumentMap runtime_options;
Mathieu Chartier46331a62017-07-21 09:49:10 -07001443 if (!PrepareRuntimeOptions(&runtime_options, callbacks_.get())) {
Andreas Gampef7882972017-03-20 16:35:24 -07001444 return dex2oat::ReturnCode::kOther;
Andreas Gampe1d00add2015-02-27 19:35:46 -08001445 }
1446
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001447 CreateOatWriters();
1448 if (!AddDexFileSources()) {
Andreas Gampef7882972017-03-20 16:35:24 -07001449 return dex2oat::ReturnCode::kOther;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001450 }
1451
Vladimir Marko0866ea42019-10-15 12:04:17 +00001452 {
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 Markoef46e102020-02-20 13:44:11 +00001460 const bool verify =
1461 (input_vdex_file_ == nullptr) && !compiler_options_->AssumeDexFilesAreVerified();
Vladimir Marko0866ea42019-10-15 12:04:17 +00001462 if (!oat_writers_[i]->WriteAndOpenDexFiles(
1463 vdex_files_[i].get(),
1464 verify,
Nicolas Geoffray84caad62022-01-10 09:48:55 +00001465 use_existing_vdex_,
Vladimir Marko0866ea42019-10-15 12:04:17 +00001466 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 Marko26de8962020-12-22 14:36:47 +00001479 dex_file_oat_index_map_.insert(std::make_pair(dex_file.get(), i));
Vladimir Marko0866ea42019-10-15 12:04:17 +00001480 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 Juravle92a78572021-04-05 17:43:17 -07001489 if (!ValidateInputVdexChecksums()) {
1490 return dex2oat::ReturnCode::kOther;
1491 }
1492
Vladimir Marko0866ea42019-10-15 12:04:17 +00001493 // 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 Marko21910692019-11-06 13:27:03 +00001498 if (!IsBootImage() && !IsBootImageExtension() && IsVeryLarge(dex_files)) {
Vladimir Marko0866ea42019-10-15 12:04:17 +00001499 // 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 Geoffray6cf7a9b2020-02-27 21:51:29 +00001510 if (CompilerFilter::IsAnyCompilationEnabled(compiler_options_->GetCompilerFilter()) ||
1511 IsImage()) {
1512 // Only modes with compilation or image generation require verification results.
Nicolas Geoffray9e050ab2021-07-14 14:59:25 +01001513 verification_results_.reset(new VerificationResults());
Vladimir Marko0866ea42019-10-15 12:04:17 +00001514 callbacks_->SetVerificationResults(verification_results_.get());
1515 }
1516
Vladimir Marko21910692019-11-06 13:27:03 +00001517 if (IsBootImage() || IsBootImageExtension()) {
1518 // For boot image or boot image extension, pass opened dex files to the Runtime::Create().
Vladimir Marko0866ea42019-10-15 12:04:17 +00001519 // 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 Zhanged0dcad2022-01-21 15:52:32 +00001525 if (runtime_->GetHeap()->GetBootImageSpaces().empty() &&
1526 (IsBootImageExtension() || IsAppImage())) {
Nicolas Geoffrayfc1ba6d2022-03-23 14:18:23 +00001527 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 Zhanged0dcad2022-01-21 15:52:32 +00001531 }
Vladimir Marko21910692019-11-06 13:27:03 +00001532 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 Marko0866ea42019-10-15 12:04:17 +00001582
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001583 if (!compilation_reason_.empty()) {
1584 key_value_store_->Put(OatHeader::kCompilationReasonKey, compilation_reason_);
1585 }
1586
Jiakai Zhang57a45be2022-02-07 16:12:57 +00001587 Runtime* runtime = Runtime::Current();
1588
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001589 if (IsBootImage()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001590 // If we're compiling the boot image, store the boot classpath into the Key-Value store.
Vladimir Marko91f10322018-12-07 18:04:10 +00001591 // We use this when loading the boot image.
1592 key_value_store_->Put(OatHeader::kBootClassPathKey, android::base::Join(dex_locations_, ':'));
Vladimir Marko21910692019-11-06 13:27:03 +00001593 } 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 Marko21910692019-11-06 13:27:03 +00001596 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 Marko0866ea42019-10-15 12:04:17 +00001612 if (CompilerFilter::DependsOnImageChecksum(original_compiler_filter)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001613 TimingLogger::ScopedTiming t3("Loading image checksum", timings_);
Vladimir Marko0ace5632018-12-14 11:11:47 +00001614 key_value_store_->Put(OatHeader::kBootClassPathKey,
1615 android::base::Join(runtime->GetBootClassPathLocations(), ':'));
Vladimir Marko436c6f52019-07-25 14:50:14 +01001616 ArrayRef<ImageSpace* const> image_spaces(runtime->GetHeap()->GetBootImageSpaces());
Vladimir Marko0ace5632018-12-14 11:11:47 +00001617 key_value_store_->Put(
1618 OatHeader::kBootClassPathChecksumsKey,
1619 gc::space::ImageSpace::GetBootClassPathChecksums(image_spaces, bcp_dex_files));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001620 }
1621
1622 // Open dex files for class path.
Calin Juravle19915892017-08-03 17:10:36 +00001623
Calin Juravle1ce70852017-06-28 10:59:03 -07001624 if (class_loader_context_ == nullptr) {
Calin Juravle19915892017-08-03 17:10:36 +00001625 // If no context was specified use the default one (which is an empty PathClassLoader).
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001626 class_loader_context_ = ClassLoaderContext::Default();
Jeff Haof0192c82016-03-28 20:39:50 -07001627 }
Calin Juravle19915892017-08-03 17:10:36 +00001628
Calin Juravle1ce70852017-06-28 10:59:03 -07001629 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 Juravle5ff23932020-12-11 18:26:14 -08001645 if (!class_loader_context_->OpenDexFiles(classpath_dir_,
David Brazdil89821862019-03-19 13:57:43 +00001646 class_loader_context_fds_)) {
Calin Juravle1ce70852017-06-28 10:59:03 -07001647 // 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 Chartierf5abfc42018-03-23 21:51:54 -07001653 // TODO: deprecate this since store_class_loader_context should be enough to cover the users
1654 // of classpath_dir as well.
Mathieu Chartierc4440772018-04-16 14:40:56 -07001655 std::string class_path_key =
1656 class_loader_context_->EncodeContextForOatFile(classpath_dir_,
1657 stored_class_loader_context_.get());
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001658 key_value_store_->Put(OatHeader::kClassPathKey, class_path_key);
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001659 }
1660
Jiakai Zhang57a45be2022-02-07 16:12:57 +00001661 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 Geoffrayfc1ba6d2022-03-23 14:18:23 +00001669 // 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 Marko4e6abea2019-10-23 10:20:20 +01001673 // 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 Gampe7386da42016-06-08 18:18:47 -07001688 // 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 Marko21910692019-11-06 13:27:03 +00001695 if (!UseSwap(IsBootImage() || IsBootImageExtension(), dex_files)) {
Andreas Gampe7386da42016-06-08 18:18:47 -07001696 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 Gampe88ec7f42014-11-05 10:18:32 -08001704
Vladimir Marko21910692019-11-06 13:27:03 +00001705 if (!IsBootImage() && !IsBootImageExtension()) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001706 constexpr bool kSaveDexInput = false;
1707 if (kSaveDexInput) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00001708 SaveDexInput();
Brian Carlstromf79fccb2014-02-20 08:55:10 -08001709 }
1710 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00001711
Vladimir Markoc3908792020-04-06 14:52:04 +01001712 // Setup VerifierDeps for compilation and report if we fail to parse the data.
Nicolas Geoffray84caad62022-01-10 09:48:55 +00001713 // When we do profile guided optimizations, the compiler currently needs to run
1714 // full verification.
1715 if (!DoProfileGuidedOptimizations() && input_vdex_file_ != nullptr) {
Vladimir Markoc3908792020-04-06 14:52:04 +01001716 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 Geoffray84caad62022-01-10 09:48:55 +00001721 // We can do fast verification.
Vladimir Markoc3908792020-04-06 14:52:04 +01001722 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 Gampef7882972017-03-20 16:35:24 -07001729 return dex2oat::ReturnCode::kNoFailure;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001730 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001731
Calin Juravle92a78572021-04-05 17:43:17 -07001732 // 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 Chartierfbc31082016-01-24 11:59:56 -08001766 // If we need to keep the oat file open for the image writer.
1767 bool ShouldKeepOatFileOpen() const {
Orion Hodson365f94f2021-01-13 16:27:57 +00001768 return IsImage() && oat_fd_ != File::kInvalidFd;
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001769 }
1770
Mathieu Chartier72041a02017-07-14 18:23:25 -07001771 // 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 Marko213ee2d2018-06-22 11:56:34 +01001774 for (const DexFile* dex_file : compiler_options_->dex_files_for_oat_file_) {
Mathieu Chartier72041a02017-07-14 18:23:25 -07001775 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 Rogers55256cb2017-12-21 17:07:11 -08001781 jweak weak_class_loader = soa.Env()->GetVm()->AddWeakGlobalRef(
Mathieu Chartier72041a02017-07-14 18:23:25 -07001782 soa.Self(),
1783 soa.Decode<mirror::ClassLoader>(class_loader));
Ian Rogers55256cb2017-12-21 17:07:11 -08001784 soa.Env()->GetVm()->DeleteGlobalRef(soa.Self(), class_loader);
Roland Levillainaf290312018-02-27 20:02:17 +00001785 runtime_->GetHeap()->CollectGarbage(/* clear_soft_references */ true);
Mathieu Chartier72041a02017-07-14 18:23:25 -07001786 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 Juravlec893d152021-06-17 15:40:52 -07001796 // Compile individually if we are allowed to, and
Eric Holkbc89ed42020-04-29 19:59:24 +00001797 // 1. not building an image, and
1798 // 2. not verifying a vdex file, and
1799 // 3. using multidex, and
Nicolas Geoffraya6fc7872017-08-14 11:29:22 +01001800 // 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 Juravlec893d152021-06-17 15:40:52 -07001803 return compile_individually_ &&
Nicolas Geoffray84caad62022-01-10 09:48:55 +00001804 (!IsImage() && !use_existing_vdex_ &&
Eric Holkbc89ed42020-04-29 19:59:24 +00001805 compiler_options_->dex_files_for_oat_file_.size() > 1 &&
1806 !CompilerFilter::IsAotCompilationEnabled(compiler_options_->GetCompilerFilter()));
Mathieu Chartier72041a02017-07-14 18:23:25 -07001807 }
1808
Vladimir Markoe1ee6f92020-03-13 11:30:59 +00001809 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 Chartier72041a02017-07-14 18:23:25 -07001817 // 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 Gampe88ec7f42014-11-05 10:18:32 -08001821 TimingLogger::ScopedTiming t("dex2oat Compile", timings_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001822
Vladimir Marko47496c22016-01-27 16:15:08 +00001823 // Find the dex files we should not inline from.
Vladimir Marko47496c22016-01-27 16:15:08 +00001824 std::vector<std::string> no_inline_filters;
1825 Split(no_inline_from_string_, ',', &no_inline_filters);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001826
1827 // For now, on the host always have core-oj removed.
Vladimir Marko47496c22016-01-27 16:15:08 +00001828 const std::string core_oj = "core-oj";
1829 if (!kIsTargetBuild && !ContainsElement(no_inline_filters, core_oj)) {
Alex Light4531afd2021-03-30 17:13:30 -07001830 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 Haodcdc85b2015-12-04 14:06:18 -08001836 }
1837
Vladimir Marko47496c22016-01-27 16:15:08 +00001838 if (!no_inline_filters.empty()) {
Calin Juravle1ce70852017-06-28 10:59:03 -07001839 std::vector<const DexFile*> class_path_files;
Vladimir Marko21910692019-11-06 13:27:03 +00001840 if (!IsBootImage() && !IsBootImageExtension()) {
Calin Juravle1ce70852017-06-28 10:59:03 -07001841 // The class loader context is used only for apps.
1842 class_path_files = class_loader_context_->FlattenOpenedDexFiles();
1843 }
1844
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001845 const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001846 std::vector<const DexFile*> no_inline_from_dex_files;
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001847 const std::vector<const DexFile*>* dex_file_vectors[] = {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001848 &class_linker->GetBootClassPath(),
1849 &class_path_files,
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001850 &dex_files
Jeff Haodcdc85b2015-12-04 14:06:18 -08001851 };
1852 for (const std::vector<const DexFile*>* dex_file_vector : dex_file_vectors) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001853 for (const DexFile* dex_file : *dex_file_vector) {
Vladimir Marko47496c22016-01-27 16:15:08 +00001854 for (const std::string& filter : no_inline_filters) {
1855 // Use dex_file->GetLocation() rather than dex_file->GetBaseLocation(). This
Calin Juravlea308a322017-07-18 16:51:51 -07001856 // allows tests to specify <test-dexfile>!classes2.dex if needed but if the
Vladimir Marko47496c22016-01-27 16:15:08 +00001857 // 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 Gampe9186ced2016-12-12 14:28:21 -08001867 if (android::base::StartsWith(dex_location, filter.c_str())) {
Vladimir Marko47496c22016-01-27 16:15:08 +00001868 VLOG(compiler) << "Disabling inlining from " << dex_file->GetLocation();
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001869 no_inline_from_dex_files.push_back(dex_file);
Vladimir Marko47496c22016-01-27 16:15:08 +00001870 break;
Jeff Haodcdc85b2015-12-04 14:06:18 -08001871 }
1872 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08001873 }
Vladimir Marko47496c22016-01-27 16:15:08 +00001874 }
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001875 if (!no_inline_from_dex_files.empty()) {
1876 compiler_options_->no_inline_from_.swap(no_inline_from_dex_files);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001877 }
1878 }
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +00001879 compiler_options_->profile_compilation_info_ = profile_compilation_info_.get();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001880
Vladimir Marko307dac92015-10-20 11:20:09 +01001881 driver_.reset(new CompilerDriver(compiler_options_.get(),
Vladimir Marko307dac92015-10-20 11:20:09 +01001882 compiler_kind_,
Vladimir Marko307dac92015-10-20 11:20:09 +01001883 thread_count_,
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +00001884 swap_fd_));
wulonghuabdf146c2020-03-27 11:15:42 +08001885
1886 driver_->PrepareDexFilesForOatFile(timings_);
1887
Vladimir Marko21910692019-11-06 13:27:03 +00001888 if (!IsBootImage() && !IsBootImageExtension()) {
Nicolas Geoffray486dda02017-09-11 14:15:52 +01001889 driver_->SetClasspathDexFiles(class_loader_context_->FlattenOpenedDexFiles());
1890 }
Nicolas Geoffray1cfea7a2017-05-24 14:44:38 +01001891
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001892 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 Geoffray1cfea7a2017-05-24 14:44:38 +01001902 // Setup vdex for compilation.
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001903 const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
Vladimir Marko37d6d772020-03-05 17:13:28 +00001904 // 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 Markoe1ee6f92020-03-13 11:30:59 +00001907 /*apply=*/ !IsBootImage(), /*initial_value=*/ 123456789u ^ GetCombinedChecksums());
Vladimir Marko37d6d772020-03-05 17:13:28 +00001908
Mathieu Chartier72041a02017-07-14 18:23:25 -07001909 // Invoke the compilation.
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001910 if (compile_individually) {
Mathieu Chartier72041a02017-07-14 18:23:25 -07001911 CompileDexFilesIndividually();
1912 // Return a null classloader since we already freed released it.
1913 return nullptr;
1914 }
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001915 return CompileDexFiles(dex_files);
Mathieu Chartier72041a02017-07-14 18:23:25 -07001916 }
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 Marko21910692019-11-06 13:27:03 +00001923 if (!IsBootImage() && !IsBootImageExtension()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001924 class_loader =
Vladimir Marko37d6d772020-03-05 17:13:28 +00001925 class_loader_context_->CreateClassLoader(compiler_options_->GetDexFilesForOatFile());
Vladimir Markoa879bdf2019-11-19 16:25:47 +00001926 }
1927 if (!IsBootImage()) {
Andreas Gampee9934582018-01-19 21:23:04 -08001928 callbacks_->SetDexFiles(&dex_files);
Calin Juravle33787682019-07-26 14:27:18 -07001929
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 Chartier72041a02017-07-14 18:23:25 -07001944 }
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 Marko2afaff72018-11-30 17:01:50 +00001961 driver_->InitializeThreadPools();
1962 driver_->PreCompile(class_loader,
1963 dex_files,
1964 timings_,
Nicolas Geoffray9e050ab2021-07-14 14:59:25 +01001965 &compiler_options_->image_classes_);
Vladimir Marko2afaff72018-11-30 17:01:50 +00001966 callbacks_->SetVerificationResults(nullptr); // Should not be needed anymore.
1967 compiler_options_->verification_results_ = verification_results_.get();
Mathieu Chartier72041a02017-07-14 18:23:25 -07001968 driver_->CompileAll(class_loader, dex_files, timings_);
Vladimir Marko2afaff72018-11-30 17:01:50 +00001969 driver_->FreeThreadPools();
Mathieu Chartier72041a02017-07-14 18:23:25 -07001970 return class_loader;
Vladimir Markof4da6752014-08-01 19:04:18 +01001971 }
1972
Jeff Hao436183f2016-01-12 16:36:50 -08001973 // Notes on the interleaving of creating the images and oat files to
Brian Carlstrom7940e442013-07-12 13:46:57 -07001974 // ensure the references between the two are correct.
1975 //
1976 // Currently we have a memory layout that looks something like this:
1977 //
1978 // +--------------+
Jeff Hao436183f2016-01-12 16:36:50 -08001979 // | images |
Brian Carlstrom7940e442013-07-12 13:46:57 -07001980 // +--------------+
Jeff Hao436183f2016-01-12 16:36:50 -08001981 // | oat files |
Brian Carlstrom7940e442013-07-12 13:46:57 -07001982 // +--------------+
1983 // | alloc spaces |
1984 // +--------------+
1985 //
Jeff Hao436183f2016-01-12 16:36:50 -08001986 // There are several constraints on the loading of the images and oat files.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001987 //
Jeff Hao436183f2016-01-12 16:36:50 -08001988 // 1. The images are expected to be loaded at an absolute address and
1989 // contain Objects with absolute pointers within the images.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001990 //
Jeff Hao436183f2016-01-12 16:36:50 -08001991 // 2. There are absolute pointers from Methods in the images to their
1992 // code in the oat files.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001993 //
Jeff Hao436183f2016-01-12 16:36:50 -08001994 // 3. There are absolute pointers from the code in the oat files to Methods
1995 // in the images.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001996 //
Jeff Hao436183f2016-01-12 16:36:50 -08001997 // 4. There are absolute pointers from code in the oat files to other code
1998 // in the oat files.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 //
2000 // To get this all correct, we go through several steps.
2001 //
Jeff Hao436183f2016-01-12 16:36:50 -08002002 // 1. We prepare offsets for all data in the oat files and calculate
Vladimir Markof4da6752014-08-01 19:04:18 +01002003 // 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 Carlstrom7940e442013-07-12 13:46:57 -07002005 //
Jeff Hao436183f2016-01-12 16:36:50 -08002006 // 2. We prepare offsets for the objects in the images and calculate
2007 // the image sizes.
Vladimir Markof4da6752014-08-01 19:04:18 +01002008 //
Jeff Hao436183f2016-01-12 16:36:50 -08002009 // 3. We create the oat files. Originally this was just our own proprietary
Vladimir Markof4da6752014-08-01 19:04:18 +01002010 // file but now it is contained within an ELF dynamic object (aka an .so
Jeff Hao436183f2016-01-12 16:36:50 -08002011 // file). Since we know the image sizes and oat data sizes and code sizes we
Vladimir Markof4da6752014-08-01 19:04:18 +01002012 // 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 Hao436183f2016-01-12 16:36:50 -08002018 // 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 Markof4da6752014-08-01 19:04:18 +01002023 // where the oat header is located within.
2024 // TODO: We could just remember this information from step 3.
Brian Carlstrom7940e442013-07-12 13:46:57 -07002025 //
Vladimir Markof4da6752014-08-01 19:04:18 +01002026 // 5. We fixup the ELF program headers so that dlopen will try to
Brian Carlstrom7940e442013-07-12 13:46:57 -07002027 // load the .so at the desired location at runtime by offsetting the
2028 // Elf32_Phdr.p_vaddr values by the desired base address.
Vladimir Markof4da6752014-08-01 19:04:18 +01002029 // 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 Gampe88ec7f42014-11-05 10:18:32 -08002033
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002034 // Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the
2035 // ImageWriter, if necessary.
Andreas Gampe10e477d2014-11-19 12:57:42 -08002036 // 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 Geoffrayf0d30022018-11-20 17:45:38 +00002038 bool WriteOutputFiles(jobject class_loader) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002039 TimingLogger::ScopedTiming t("dex2oat Oat", timings_);
2040
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002041 // Sync the data to the file, in case we did dex2dex transformations.
Vladimir Markoc34bebf2018-08-16 16:12:49 +01002042 for (MemMap& map : opened_dex_files_maps_) {
2043 if (!map.Sync()) {
2044 PLOG(ERROR) << "Failed to Sync() dex2dex output. Map: " << map.GetName();
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002045 return false;
2046 }
2047 }
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08002048
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002049 if (IsImage()) {
Vladimir Marko66235942019-06-03 16:05:17 +01002050 if (!IsBootImage()) {
2051 DCHECK_EQ(image_base_, 0u);
Mathieu Chartierfbc31082016-01-24 11:59:56 -08002052 gc::Heap* const heap = Runtime::Current()->GetHeap();
Vladimir Marko66235942019-06-03 16:05:17 +01002053 image_base_ = heap->GetBootImagesStartAddress() + heap->GetBootImagesSize();
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08002054 }
Vladimir Marko66235942019-06-03 16:05:17 +01002055 VLOG(compiler) << "Image base=" << reinterpret_cast<void*>(image_base_);
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08002056
Vladimir Markoa0431112018-06-25 09:32:54 +01002057 image_writer_.reset(new linker::ImageWriter(*compiler_options_,
Vladimir Marko74527972016-11-29 15:57:32 +00002058 image_base_,
Vladimir Marko74527972016-11-29 15:57:32 +00002059 image_storage_mode_,
2060 oat_filenames_,
2061 dex_file_oat_index_map_,
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +00002062 class_loader,
Vladimir Marko74527972016-11-29 15:57:32 +00002063 dirty_image_objects_.get()));
Mathieu Chartierda5b28a2015-11-05 08:03:47 -08002064
Vladimir Marko4b90b862020-03-13 17:19:06 +00002065 // We need to prepare method offsets in the image address space for resolving linker patches.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002066 TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_);
David Srbecky86d6cd52020-12-02 18:13:10 +00002067 if (!image_writer_->PrepareImageAddressSpace(timings_)) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002068 LOG(ERROR) << "Failed to prepare image address space.";
2069 return false;
2070 }
2071 }
2072
Vladimir Marko4e6abea2019-10-23 10:20:20 +01002073 // 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 Geoffray4acefd32016-10-24 13:14:58 +01002079 }
2080
Nicolas Geoffray84caad62022-01-10 09:48:55 +00002081 if (!use_existing_vdex_) {
David Brazdil5d5a36b2016-09-14 15:34:10 +01002082 TimingLogger::ScopedTiming t2("dex2oat Write VDEX", timings_);
Vladimir Marko21910692019-11-06 13:27:03 +00002083 DCHECK(IsBootImage() || IsBootImageExtension() || oat_files_.size() == 1u);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00002084 verifier::VerifierDeps* verifier_deps = callbacks_->GetVerifierDeps();
David Brazdil5d5a36b2016-09-14 15:34:10 +01002085 for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
2086 File* vdex_file = vdex_files_[i].get();
Vladimir Marko9e4b42a2020-04-28 12:28:21 +01002087 if (!oat_writers_[i]->FinishVdexFile(vdex_file, verifier_deps)) {
2088 LOG(ERROR) << "Failed to finish VDEX file " << vdex_file->GetPath();
David Brazdil5d5a36b2016-09-14 15:34:10 +01002089 return false;
2090 }
2091 }
2092 }
2093
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002094 {
2095 TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_);
Vladimir Markoa0431112018-06-25 09:32:54 +01002096 linker::MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
2097 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +00002098 driver_->GetCompiledMethodStorage());
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002099 for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
Vladimir Marko74527972016-11-29 15:57:32 +00002100 std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i];
2101 std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
Vladimir Marko944da602016-02-19 12:27:55 +00002102
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01002103 oat_writer->PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +00002104 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
2105 oat_writer->GetCodeSize(),
2106 oat_writer->GetDataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +00002107 oat_writer->GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002108 oat_writer->GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +00002109 oat_writer->GetBssRootsOffset(),
2110 oat_writer->GetVdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +00002111 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 Brazdil7b49e6c2016-09-01 11:06:18 +01002118 oat_writer->GetOatSize());
Vladimir Marko944da602016-02-19 12:27:55 +00002119 }
2120 }
2121
2122 for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002123 std::unique_ptr<File>& oat_file = oat_files_[i];
Vladimir Marko74527972016-11-29 15:57:32 +00002124 std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i];
2125 std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
Vladimir Marko10c13562015-11-25 14:33:36 +00002126
David Srbecky0c4572e2016-01-22 19:19:25 +00002127 // We need to mirror the layout of the ELF file in the compressed debug-info.
Vladimir Marko944da602016-02-19 12:27:55 +00002128 // Therefore PrepareDebugInfo() relies on the SetLoadedSectionSizes() call further above.
David Srbecky32210b92017-12-04 14:39:21 +00002129 debug::DebugInfo debug_info = oat_writer->GetDebugInfo(); // Keep the variable alive.
2130 elf_writer->PrepareDebugInfo(debug_info); // Processes the data on background thread.
David Srbecky0c4572e2016-01-22 19:19:25 +00002131
Vladimir Marko0866ea42019-10-15 12:04:17 +00002132 OutputStream* rodata = rodata_[i];
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002133 DCHECK(rodata != nullptr);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002134 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 Marko9bdf1082016-01-21 12:15:52 +00002139 rodata = nullptr;
Vladimir Marko10c13562015-11-25 14:33:36 +00002140
David Srbecky2faab002019-02-12 16:35:48 +00002141 OutputStream* text = elf_writer->StartText();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002142 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 Marko10c13562015-11-25 14:33:36 +00002147
Vladimir Markob066d432018-01-03 13:14:37 +00002148 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
David Srbecky2faab002019-02-12 16:35:48 +00002149 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
Vladimir Markob066d432018-01-03 13:14:37 +00002150 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 Marko0ace5632018-12-14 11:11:47 +00002158 if (!oat_writer->WriteHeader(elf_writer->GetStream())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002159 LOG(ERROR) << "Failed to write oat header to the ELF file " << oat_file->GetPath();
2160 return false;
2161 }
2162
Vladimir Marko944da602016-02-19 12:27:55 +00002163 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 Haodcdc85b2015-12-04 14:06:18 -08002170 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +00002171 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
Vladimir Marko10c13562015-11-25 14:33:36 +00002172
Jeff Haodcdc85b2015-12-04 14:06:18 -08002173 if (!elf_writer->End()) {
2174 LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
2175 return false;
2176 }
2177
David Brazdil7b49e6c2016-09-01 11:06:18 +01002178 if (!FlushOutputFile(&vdex_files_[i]) || !FlushOutputFile(&oat_files_[i])) {
2179 return false;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002180 }
2181
Jeff Haodcdc85b2015-12-04 14:06:18 -08002182 VLOG(compiler) << "Oat file written successfully: " << oat_filenames_[i];
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002183
2184 oat_writer.reset();
David Srbeckyde91fd42018-07-05 22:27:08 +01002185 // We may still need the ELF writer later for stripping.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002186 }
2187 }
2188
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002189 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002190 }
2191
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002192 // If we are compiling an image, invoke the image creation routine. Else just skip.
2193 bool HandleImage() {
Mathieu Chartiera90c7722015-10-29 15:41:36 -07002194 if (IsImage()) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002195 TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_);
2196 if (!CreateImageFile()) {
2197 return false;
2198 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002199 VLOG(compiler) << "Images written successfully";
Brian Carlstrom45602482013-07-21 22:07:55 -07002200 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002201 return true;
2202 }
2203
David Srbeckyde91fd42018-07-05 22:27:08 +01002204 // Copy the full oat files to symbols directory and then strip the originals.
2205 bool CopyOatFilesToSymbolsDirectoryAndStrip() {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002206 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 Markod011d812018-11-29 12:35:24 +00002209 if (oat_unstripped_[i] != oat_filenames_[i]) {
David Srbeckyde91fd42018-07-05 22:27:08 +01002210 DCHECK(oat_files_[i].get() != nullptr && oat_files_[i]->IsOpened());
Jeff Haodcdc85b2015-12-04 14:06:18 -08002211
2212 TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_);
David Srbeckyde91fd42018-07-05 22:27:08 +01002213 std::unique_ptr<File>& in = oat_files_[i];
Vladimir Markob55cc6d2016-10-17 15:25:10 +01002214 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 Hodson923141b2020-08-06 14:01:16 +01002219 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 Markob55cc6d2016-10-17 15:25:10 +01002224 if (!out->Copy(in.get(), 0, in_length)) {
2225 PLOG(ERROR) << "Failed to copy oat file to file: " << out->GetPath();
2226 return false;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002227 }
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 Srbeckyde91fd42018-07-05 22:27:08 +01002233
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 Haodcdc85b2015-12-04 14:06:18 -08002241 }
2242 }
2243 return true;
2244 }
2245
David Brazdil7b49e6c2016-09-01 11:06:18 +01002246 bool FlushOutputFile(std::unique_ptr<File>* file) {
Nicolas Geoffray84caad62022-01-10 09:48:55 +00002247 if ((file->get() != nullptr) && !file->get()->ReadOnlyMode()) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01002248 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 Srbeckyde91fd42018-07-05 22:27:08 +01002256 bool FlushCloseOutputFile(File* file) {
Nicolas Geoffray84caad62022-01-10 09:48:55 +00002257 if ((file != nullptr) && !file->ReadOnlyMode()) {
David Srbeckyde91fd42018-07-05 22:27:08 +01002258 if (file->FlushCloseOrErase() != 0) {
2259 PLOG(ERROR) << "Failed to flush and close output file: " << file->GetPath();
David Brazdil7b49e6c2016-09-01 11:06:18 +01002260 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 Gampe10e477d2014-11-19 12:57:42 -08002271 return false;
Andreas Gampe4303ba92014-11-06 01:00:46 -08002272 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08002273 }
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +00002274 }
Andreas Gampe10e477d2014-11-19 12:57:42 -08002275 return true;
2276 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002277
David Brazdil7b49e6c2016-09-01 11:06:18 +01002278 bool FlushCloseOutputFiles() {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002279 bool result = true;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002280 for (auto& files : { &vdex_files_, &oat_files_ }) {
2281 for (size_t i = 0; i < files->size(); ++i) {
David Srbeckyde91fd42018-07-05 22:27:08 +01002282 result &= FlushCloseOutputFile((*files)[i].get());
David Brazdil7b49e6c2016-09-01 11:06:18 +01002283 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002284 }
2285 return result;
2286 }
2287
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002288 void DumpTiming() {
Nicolas Geoffray2d8801f2017-11-28 15:50:07 +00002289 if (compiler_options_->GetDumpTimings() ||
2290 (kIsDebugBuild && timings_->GetTotalNs() > MsToNs(1000))) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002291 LOG(INFO) << Dumpable<TimingLogger>(*timings_);
2292 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002293 }
2294
Andreas Gampe10e477d2014-11-19 12:57:42 -08002295 bool IsImage() const {
Vladimir Marko21910692019-11-06 13:27:03 +00002296 return IsAppImage() || IsBootImage() || IsBootImageExtension();
Mathieu Chartiera90c7722015-10-29 15:41:36 -07002297 }
2298
2299 bool IsAppImage() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00002300 return compiler_options_->IsAppImage();
Mathieu Chartiera90c7722015-10-29 15:41:36 -07002301 }
2302
2303 bool IsBootImage() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00002304 return compiler_options_->IsBootImage();
Andreas Gampe10e477d2014-11-19 12:57:42 -08002305 }
2306
Vladimir Marko21910692019-11-06 13:27:03 +00002307 bool IsBootImageExtension() const {
2308 return compiler_options_->IsBootImageExtension();
2309 }
2310
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002311 bool IsHost() const {
2312 return is_host_;
2313 }
2314
Jiakai Zhang19f95682021-12-06 16:19:39 +00002315 bool HasProfileInput() const { return !profile_file_fds_.empty() || !profile_files_.empty(); }
Calin Juravle998c2162015-12-21 15:39:33 +02002316
Calin Juravle028c7ef2021-05-26 15:37:00 -07002317 // Must be called after the profile is loaded.
Mathieu Chartier11b3df32017-02-21 13:41:54 -08002318 bool DoProfileGuidedOptimizations() const {
Calin Juravle028c7ef2021-05-26 15:37:00 -07002319 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 Chartier11b3df32017-02-21 13:41:54 -08002323 }
2324
Mathieu Chartiereafe2a52017-10-19 15:29:42 -07002325 bool DoGenerateCompactDex() const {
2326 return compact_dex_level_ != CompactDexLevel::kCompactDexLevelNone;
2327 }
2328
Mathieu Chartier11b3df32017-02-21 13:41:54 -08002329 bool DoDexLayoutOptimizations() const {
Nicolas Geoffraydb7c76b2022-03-21 16:23:30 +00002330 // 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 Chartier11b3df32017-02-21 13:41:54 -08002335 }
2336
Igor Murashkin545412b2017-08-17 15:26:54 -07002337 bool DoOatLayoutOptimizations() const {
2338 return DoProfileGuidedOptimizations();
2339 }
2340
Calin Juravle2e2db782016-02-23 12:00:03 +00002341 bool LoadProfile() {
Calin Juravle028c7ef2021-05-26 15:37:00 -07002342 DCHECK(HasProfileInput());
2343 profile_load_attempted_ = true;
Calin Juravlee6f87cc2017-05-24 17:41:05 -07002344 // 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 Markoa2209802021-04-23 13:28:29 +00002348 bool for_boot_image = IsBootImage() || IsBootImageExtension();
2349 profile_compilation_info_.reset(new ProfileCompilationInfo(for_boot_image));
Jiakai Zhang19f95682021-12-06 16:19:39 +00002350
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 Hsieh52fe49e2020-07-20 15:20:12 -07002354 // 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 Zhang890beb02022-01-06 14:05:44 +00002357 std::vector<std::unique_ptr<File>> profile_files;
Jiakai Zhang19f95682021-12-06 16:19:39 +00002358 if (!profile_file_fds_.empty()) {
2359 for (int fd : profile_file_fds_) {
Jiakai Zhang890beb02022-01-06 14:05:44 +00002360 profile_files.push_back(std::make_unique<File>(DupCloexec(fd),
2361 "profile",
2362 /*check_usage=*/ false,
2363 /*read_only_mode=*/ true));
Jiakai Zhang19f95682021-12-06 16:19:39 +00002364 }
2365 } else {
2366 for (const std::string& file : profile_files_) {
Jiakai Zhang890beb02022-01-06 14:05:44 +00002367 profile_files.emplace_back(OS::OpenFileForReading(file.c_str()));
2368 if (profile_files.back().get() == nullptr) {
Jiakai Zhang19f95682021-12-06 16:19:39 +00002369 PLOG(ERROR) << "Cannot open profiles";
2370 return false;
2371 }
Jiakai Zhang19f95682021-12-06 16:19:39 +00002372 }
Calin Juravle998c2162015-12-21 15:39:33 +02002373 }
Calin Juravle877fd962016-01-05 14:29:29 +00002374
Jiakai Zhang890beb02022-01-06 14:05:44 +00002375 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 Zhang19f95682021-12-06 16:19:39 +00002399 cleanup.Disable();
Narayan Kamatha3d27eb2017-05-11 13:50:59 +01002400 return true;
Calin Juravle998c2162015-12-21 15:39:33 +02002401 }
2402
Calin Juravle028c7ef2021-05-26 15:37:00 -07002403 // 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 Geoffray1ece2992021-01-06 17:02:04 +00002426 class ScopedDex2oatReporting {
2427 public:
Martin Stjernholmd4aba912022-01-25 21:05:47 +00002428 explicit ScopedDex2oatReporting(const Dex2Oat& dex2oat) :
2429 should_report_(dex2oat.should_report_dex2oat_compilation_) {
2430 if (should_report_) {
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00002431 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 Geoffray4f6bb442021-06-02 18:05:51 +01002452 PaletteNotifyStartDex2oatCompilation(zip_dup_fd_,
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00002453 image_dup_fd_,
2454 oat_dup_fd_,
2455 vdex_dup_fd_);
2456 }
2457 error_reporting_ = false;
2458 }
2459
2460 ~ScopedDex2oatReporting() {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +01002461 if (!error_reporting_) {
Martin Stjernholmd4aba912022-01-25 21:05:47 +00002462 if (should_report_) {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +01002463 PaletteNotifyEndDex2oatCompilation(zip_dup_fd_,
2464 image_dup_fd_,
2465 oat_dup_fd_,
2466 vdex_dup_fd_);
2467 }
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00002468 }
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 Stjernholmd4aba912022-01-25 21:05:47 +00002487 bool should_report_;
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00002488 };
2489
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002490 private:
Andreas Gampee1459ae2016-06-29 09:36:30 -07002491 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 Marko213ee2d2018-06-22 11:56:34 +01002507 bool IsVeryLarge(const std::vector<const DexFile*>& dex_files) {
Andreas Gampe67f02822016-06-24 21:05:23 -07002508 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 Haoc23b0c02017-07-27 18:19:38 -07002515 bool PrepareDirtyObjects() {
Victor Hsieh4ab59232021-06-10 12:24:02 -07002516 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 Marko54159c62018-06-20 14:30:08 +01002528 dirty_image_objects_ = ReadCommentedInputFromFile<HashSet<std::string>>(
Jeff Haoc23b0c02017-07-27 18:19:38 -07002529 dirty_image_objects_filename_,
Vladimir Marko54159c62018-06-20 14:30:08 +01002530 nullptr);
Jeff Haoc23b0c02017-07-27 18:19:38 -07002531 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 Haoc23b0c02017-07-27 18:19:38 -07002536 }
2537 return true;
2538 }
2539
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002540 bool PreparePreloadedClasses() {
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002541 preloaded_classes_ = std::make_unique<HashSet<std::string>>();
2542 if (!preloaded_classes_fds_.empty()) {
2543 for (int fd : preloaded_classes_fds_) {
Nicolas Geoffray2bd92d32022-03-24 16:06:50 +00002544 if (!ReadCommentedInputFromFd(fd, nullptr, preloaded_classes_.get())) {
2545 return false;
2546 }
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002547 }
2548 } else {
2549 for (const std::string& file : preloaded_classes_files_) {
Nicolas Geoffray2bd92d32022-03-24 16:06:50 +00002550 if (!ReadCommentedInputFromFile(file.c_str(), nullptr, preloaded_classes_.get())) {
2551 return false;
2552 }
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002553 }
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002554 }
2555 return true;
2556 }
2557
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002558 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 Hsieh59edf4b2021-06-17 12:48:18 -07002562 // Keep if the file exist, or is passed as FD.
2563 if (!OS::FileExists(dex_filenames_[i].c_str()) && i >= dex_fds_.size()) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002564 LOG(WARNING) << "Skipping non-existent dex file '" << dex_filenames_[i] << "'";
2565 } else {
Vladimir Markod011d812018-11-29 12:35:24 +00002566 if (kept != i) {
2567 dex_filenames_[kept] = dex_filenames_[i];
2568 dex_locations_[kept] = dex_locations_[i];
2569 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002570 ++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 Geoffraybc177272018-01-24 14:55:32 +00002579 if (input_vdex_file_ != nullptr && input_vdex_file_->HasDexSection()) {
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00002580 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 Marko9bdf1082016-01-21 12:15:52 +00002587 DCHECK_EQ(oat_writers_.size(), 1u);
wangshumin3949e5e2019-11-08 19:25:50 +08002588 if (!oat_writers_[0]->AddDexFileSource(File(zip_fd_, /* check_usage */ false),
2589 zip_location_.c_str())) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002590 return false;
2591 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002592 } else {
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002593 DCHECK_EQ(dex_filenames_.size(), dex_locations_.size());
Victor Hsiehb3bc8922021-05-14 15:02:21 -07002594 DCHECK_GE(oat_writers_.size(), 1u);
2595
Victor Hsieh826e6662021-05-17 15:40:13 -07002596 bool use_dex_fds = !dex_fds_.empty();
2597 if (use_dex_fds) {
2598 DCHECK_EQ(dex_fds_.size(), dex_filenames_.size());
2599 }
2600
Victor Hsiehb3bc8922021-05-14 15:02:21 -07002601 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 Marko9bdf1082016-01-21 12:15:52 +00002606 for (size_t i = 0; i != dex_filenames_.size(); ++i) {
Victor Hsiehb3bc8922021-05-14 15:02:21 -07002607 int oat_index = is_multi_image ? i : 0;
Victor Hsieh826e6662021-05-17 15:40:13 -07002608 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 Marko9bdf1082016-01-21 12:15:52 +00002620 }
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 Markoa0431112018-06-25 09:32:54 +01002631 elf_writers_.emplace_back(linker::CreateElfWriterQuick(*compiler_options_, oat_file.get()));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002632 elf_writers_.back()->Start();
Mathieu Chartier4a265c22018-04-09 14:25:14 -07002633 bool do_oat_writer_layout = DoDexLayoutOptimizations() || DoOatLayoutOptimizations();
Vladimir Marko74527972016-11-29 15:57:32 +00002634 oat_writers_.emplace_back(new linker::OatWriter(
Vladimir Markoa0431112018-06-25 09:32:54 +01002635 *compiler_options_,
Igor Murashkin545412b2017-08-17 15:26:54 -07002636 timings_,
Mathieu Chartier603ccab2017-10-20 14:34:28 -07002637 do_oat_writer_layout ? profile_compilation_info_.get() : nullptr,
2638 compact_dex_level_));
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002639 }
2640 }
2641
Vladimir Marko49b0f452015-12-10 13:49:19 +00002642 void SaveDexInput() {
Vladimir Marko213ee2d2018-06-22 11:56:34 +01002643 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 Marko49b0f452015-12-10 13:49:19 +00002646 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 Chartier46331a62017-07-21 09:49:10 -07002662 bool PrepareRuntimeOptions(RuntimeArgumentMap* runtime_options,
2663 QuickCompilerCallbacks* callbacks) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00002664 RuntimeOptions raw_options;
Vladimir Marko21910692019-11-06 13:27:03 +00002665 if (IsBootImage()) {
Vladimir Marko49b0f452015-12-10 13:49:19 +00002666 std::string boot_class_path = "-Xbootclasspath:";
Andreas Gampe9186ced2016-12-12 14:28:21 -08002667 boot_class_path += android::base::Join(dex_filenames_, ':');
Vladimir Marko49b0f452015-12-10 13:49:19 +00002668 raw_options.push_back(std::make_pair(boot_class_path, nullptr));
2669 std::string boot_class_path_locations = "-Xbootclasspath-locations:";
Andreas Gampe9186ced2016-12-12 14:28:21 -08002670 boot_class_path_locations += android::base::Join(dex_locations_, ':');
Vladimir Marko49b0f452015-12-10 13:49:19 +00002671 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 Chartier46331a62017-07-21 09:49:10 -07002681 raw_options.push_back(std::make_pair("compilercallbacks", callbacks));
Vladimir Marko49b0f452015-12-10 13:49:19 +00002682 raw_options.push_back(
Vladimir Markoa0431112018-06-25 09:32:54 +01002683 std::make_pair("imageinstructionset",
2684 GetInstructionSetString(compiler_options_->GetInstructionSet())));
Vladimir Marko49b0f452015-12-10 13:49:19 +00002685
Andreas Gampec8e35202017-04-28 15:47:12 -07002686 // Never allow implicit image compilation.
2687 raw_options.push_back(std::make_pair("-Xnoimage-dex2oat", nullptr));
Vladimir Marko49b0f452015-12-10 13:49:19 +00002688 // 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 Zang97f3f7d2016-01-13 10:25:00 +08002691 // 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 Marko49b0f452015-12-10 13:49:19 +00002695
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 Gampe88ec7f42014-11-05 10:18:32 -08002703 // Create a runtime necessary for compilation.
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002704 bool CreateRuntime(RuntimeArgumentMap&& runtime_options) {
Vladimir Markoe1ee6f92020-03-13 11:30:59 +00002705 // 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 Marko9bdf1082016-01-21 12:15:52 +00002710 TimingLogger::ScopedTiming t_runtime("Create runtime", timings_);
Vladimir Marko49b0f452015-12-10 13:49:19 +00002711 if (!Runtime::Create(std::move(runtime_options))) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002712 LOG(ERROR) << "Failed to create runtime";
2713 return false;
2714 }
Andreas Gampe49c144d2016-11-14 10:11:16 -08002715
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 Marko307dac92015-10-20 11:20:09 +01002720 runtime_.reset(Runtime::Current());
Vladimir Markoa0431112018-06-25 09:32:54 +01002721 runtime_->SetInstructionSet(compiler_options_->GetInstructionSet());
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002722 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
2723 CalleeSaveType type = CalleeSaveType(i);
Vladimir Marko307dac92015-10-20 11:20:09 +01002724 if (!runtime_->HasCalleeSaveMethod(type)) {
2725 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002726 }
2727 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002728
2729 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
2730 // set up.
Andreas Gampe799681b2015-05-15 19:24:12 -07002731 interpreter::UnstartedRuntime::Initialize();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002732
Vladimir Markodcfcce42018-06-27 10:00:28 +00002733 Thread* self = Thread::Current();
2734 runtime_->RunRootClinits(self);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002735
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002736 // 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 Markoddf4fd32021-11-22 16:31:57 +00002738 self->TransitionFromRunnableToSuspended(ThreadState::kNative);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002739
Nicolas Geoffray48eb8392022-02-24 16:20:18 +00002740 // 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 Fukuia72ca6c2019-03-25 17:39:07 +09002747 WatchDog::SetRuntime(runtime_.get());
2748
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002749 return true;
Vladimir Marko919f5532016-01-20 19:13:01 +00002750 }
2751
Jeff Haodcdc85b2015-12-04 14:06:18 -08002752 // Let the ImageWriter write the image files. If we do not compile PIC, also fix up the oat files.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002753 bool CreateImageFile()
Mathieu Chartier90443472015-07-16 20:32:27 -07002754 REQUIRES(!Locks::mutator_lock_) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002755 CHECK(image_writer_ != nullptr);
Vladimir Markod0036ac2019-11-21 11:47:12 +00002756 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 Haodcdc85b2015-12-04 14:06:18 -08002763 }
Vladimir Markod0036ac2019-11-21 11:47:12 +00002764 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 Chartierfbc31082016-01-24 11:59:56 -08002769 image_filenames_,
Vladimir Markod0036ac2019-11-21 11:47:12 +00002770 IsAppImage() ? 1u : dex_locations_.size())) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002771 LOG(ERROR) << "Failure during image file creation";
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002772 return false;
2773 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002774
Jeff Haodcdc85b2015-12-04 14:06:18 -08002775 // We need the OatDataBegin entries.
Vladimir Marko944da602016-02-19 12:27:55 +00002776 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 Haodcdc85b2015-12-04 14:06:18 -08002779 }
Vladimir Markoa2da9b92018-10-10 14:21:55 +01002780 // Destroy ImageWriter.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002781 image_writer_.reset();
2782
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002783 return true;
2784 }
2785
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002786 template <typename T>
Nicolas Geoffray2bd92d32022-03-24 16:06:50 +00002787 static bool ReadCommentedInputFromFile(
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002788 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 Geoffray2bd92d32022-03-24 16:06:50 +00002792 return false;
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002793 }
2794 ReadCommentedInputStream<T>(input_file.get(), process, output);
Nicolas Geoffray2bd92d32022-03-24 16:06:50 +00002795 return true;
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002796 }
2797
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002798 template <typename T>
Nicolas Geoffray2bd92d32022-03-24 16:06:50 +00002799 static bool ReadCommentedInputFromFd(
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002800 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 Geoffray2bd92d32022-03-24 16:06:50 +00002804 return false;
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002805 }
2806 ReadCommentedInputStream<T>(input_file.get(), process, output);
Nicolas Geoffray2bd92d32022-03-24 16:06:50 +00002807 return true;
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002808 }
2809
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002810 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
2811 // the given function.
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -07002812 template <typename T>
Vladimir Marko54159c62018-06-20 14:30:08 +01002813 static std::unique_ptr<T> ReadCommentedInputFromFile(
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002814 const char* input_filename, std::function<std::string(const char*)>* process) {
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002815 std::unique_ptr<T> output(new T());
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002816 ReadCommentedInputFromFile(input_filename, process, output.get());
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002817 return output;
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002818 }
2819
Victor Hsieh4ab59232021-06-10 12:24:02 -07002820 // Read lines from the given fd, dropping comments and empty lines. Post-process each line with
2821 // the given function.
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -07002822 template <typename T>
Victor Hsieh4ab59232021-06-10 12:24:02 -07002823 static std::unique_ptr<T> ReadCommentedInputFromFd(
2824 int input_fd, std::function<std::string(const char*)>* process) {
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002825 std::unique_ptr<T> output(new T());
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002826 ReadCommentedInputFromFd(input_fd, process, output.get());
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002827 return output;
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002828 }
2829
2830 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
2831 // with the given function.
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002832 template <typename T> static void ReadCommentedInputStream(
Victor Hsieh2bac0492021-08-19 13:16:00 -07002833 std::FILE* in_stream,
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002834 std::function<std::string(const char*)>* process,
2835 T* output) {
Victor Hsieh2bac0492021-08-19 13:16:00 -07002836 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 Gampe70bef0d2015-04-15 02:37:28 -07002841 continue;
2842 }
Victor Hsieh2bac0492021-08-19 13:16:00 -07002843 if (line[len - 1] == '\n') {
2844 line[len - 1] = '\0';
2845 }
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002846 if (process != nullptr) {
Victor Hsieh2bac0492021-08-19 13:16:00 -07002847 std::string descriptor((*process)(line));
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -07002848 output->insert(output->end(), descriptor);
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002849 } else {
Victor Hsieh2bac0492021-08-19 13:16:00 -07002850 output->insert(output->end(), line);
Andreas Gampe70bef0d2015-04-15 02:37:28 -07002851 }
2852 }
Victor Hsieh2bac0492021-08-19 13:16:00 -07002853 free(line);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002854 }
2855
Mathieu Chartier49285c52014-12-02 15:43:48 -08002856 void LogCompletionTime() {
Andreas Gampe1d00add2015-02-27 19:35:46 -08002857 // 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 Carlstroma11a34c2015-03-06 08:44:45 -08002859 // Note: driver creation can fail when loading an invalid dex file.
Andreas Gampec560fc02014-07-16 09:57:39 -07002860 LOG(INFO) << "dex2oat took "
2861 << PrettyDuration(NanoTime() - start_ns_)
Andreas Gampecc967752016-12-12 13:49:00 -08002862 << " (" << PrettyDuration(ProcessCpuNanoTime() - start_cputime_ns_) << " cpu)"
Mathieu Chartierab972ef2014-12-03 17:38:22 -08002863 << " (threads: " << thread_count_ << ") "
Andreas Gampe3f30e122015-09-17 14:03:21 -07002864 << ((Runtime::Current() != nullptr && driver_ != nullptr) ?
Andreas Gampe1d00add2015-02-27 19:35:46 -08002865 driver_->GetMemoryUsageString(kIsDebugBuild || VLOG_IS_ON(compiler)) :
2866 "");
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002867 }
2868
Jeff Haodcdc85b2015-12-04 14:06:18 -08002869 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 Gampe88ec7f42014-11-05 10:18:32 -08002888 std::unique_ptr<CompilerOptions> compiler_options_;
2889 Compiler::Kind compiler_kind_;
2890
Eric Holk94a810b2020-05-06 14:33:16 -07002891 std::unique_ptr<OatKeyValueStore> key_value_store_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002892
Vladimir Marko307dac92015-10-20 11:20:09 +01002893 std::unique_ptr<VerificationResults> verification_results_;
Andreas Gampe3f30e122015-09-17 14:03:21 -07002894
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002895 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
2896
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002897 std::unique_ptr<Runtime> runtime_;
2898
Calin Juravle1ce70852017-06-28 10:59:03 -07002899 // The spec describing how the class loader should be setup for compilation.
2900 std::unique_ptr<ClassLoaderContext> class_loader_context_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002901
David Brazdil89821862019-03-19 13:57:43 +00002902 // 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 Chartierf5abfc42018-03-23 21:51:54 -07002906 // The class loader context stored in the oat file. May be equal to class_loader_context_.
Mathieu Chartierc4440772018-04-16 14:40:56 -07002907 std::unique_ptr<ClassLoaderContext> stored_class_loader_context_;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002908
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002909 size_t thread_count_;
Orion Hodsonffc791c2019-11-14 19:19:28 +00002910 std::vector<int32_t> cpu_set_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002911 uint64_t start_ns_;
Andreas Gampec560fc02014-07-16 09:57:39 -07002912 uint64_t start_cputime_ns_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002913 std::unique_ptr<WatchDog> watchdog_;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002914 std::vector<std::unique_ptr<File>> oat_files_;
David Brazdil7b49e6c2016-09-01 11:06:18 +01002915 std::vector<std::unique_ptr<File>> vdex_files_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002916 std::string oat_location_;
Vladimir Markod011d812018-11-29 12:35:24 +00002917 std::vector<std::string> oat_filenames_;
2918 std::vector<std::string> oat_unstripped_;
David Srbeckyde91fd42018-07-05 22:27:08 +01002919 bool strip_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002920 int oat_fd_;
Nicolas Geoffraya6a448a2016-11-10 10:49:40 +00002921 int input_vdex_fd_;
2922 int output_vdex_fd_;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00002923 std::string input_vdex_;
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +01002924 std::string output_vdex_;
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00002925 std::unique_ptr<VdexFile> input_vdex_file_;
Nicolas Geoffraybaeaa9b2018-01-26 14:31:17 +00002926 int dm_fd_;
2927 std::string dm_file_location_;
2928 std::unique_ptr<ZipArchive> dm_file_;
Vladimir Markod011d812018-11-29 12:35:24 +00002929 std::vector<std::string> dex_filenames_;
2930 std::vector<std::string> dex_locations_;
Victor Hsieh826e6662021-05-17 15:40:13 -07002931 std::vector<int> dex_fds_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002932 int zip_fd_;
2933 std::string zip_location_;
Vladimir Marko49b0f452015-12-10 13:49:19 +00002934 std::string boot_image_filename_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002935 std::vector<const char*> runtime_args_;
Vladimir Markod011d812018-11-29 12:35:24 +00002936 std::vector<std::string> image_filenames_;
Vladimir Markod0036ac2019-11-21 11:47:12 +00002937 int image_fd_;
2938 bool have_multi_image_arg_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002939 uintptr_t image_base_;
Mathieu Chartierceb07b32015-12-10 09:33:21 -08002940 ImageHeader::StorageMode image_storage_mode_;
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -07002941 const char* passes_to_run_filename_;
Jeff Haoc23b0c02017-07-27 18:19:38 -07002942 const char* dirty_image_objects_filename_;
Victor Hsieh4ab59232021-06-10 12:24:02 -07002943 int dirty_image_objects_fd_;
Vladimir Marko54159c62018-06-20 14:30:08 +01002944 std::unique_ptr<HashSet<std::string>> dirty_image_objects_;
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002945 std::unique_ptr<HashSet<std::string>> preloaded_classes_;
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -07002946 std::unique_ptr<std::vector<std::string>> passes_to_run_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002947 bool is_host_;
2948 std::string android_root_;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002949 std::string no_inline_from_string_;
Alex Light4531afd2021-03-30 17:13:30 -07002950 bool force_allow_oj_inlines_ = false;
Mathieu Chartiera7f6b812017-12-11 13:34:29 -08002951 CompactDexLevel compact_dex_level_ = kDefaultCompactDexLevel;
Andreas Gampe3f30e122015-09-17 14:03:21 -07002952
Vladimir Marko74527972016-11-29 15:57:32 +00002953 std::vector<std::unique_ptr<linker::ElfWriter>> elf_writers_;
2954 std::vector<std::unique_ptr<linker::OatWriter>> oat_writers_;
David Srbecky2faab002019-02-12 16:35:48 +00002955 std::vector<OutputStream*> rodata_;
2956 std::vector<std::unique_ptr<OutputStream>> vdex_out_;
Vladimir Marko74527972016-11-29 15:57:32 +00002957 std::unique_ptr<linker::ImageWriter> image_writer_;
Vladimir Marko307dac92015-10-20 11:20:09 +01002958 std::unique_ptr<CompilerDriver> driver_;
Andreas Gampe3f30e122015-09-17 14:03:21 -07002959
Vladimir Markoc34bebf2018-08-16 16:12:49 +01002960 std::vector<MemMap> opened_dex_files_maps_;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00002961 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
2962
Mathieu Chartierd1ab1bd2017-08-04 10:21:50 -07002963 bool avoid_storing_invocation_;
Alex Light62afcf52018-12-18 14:19:25 -08002964 android::base::unique_fd invocation_file_;
Andreas Gampee21dc3d2014-12-08 16:59:43 -08002965 std::string swap_file_name_;
2966 int swap_fd_;
Andreas Gampee1459ae2016-06-29 09:36:30 -07002967 size_t min_dex_files_for_swap_ = kDefaultMinDexFilesForSwap;
2968 size_t min_dex_file_cumulative_size_for_swap_ = kDefaultMinDexFileCumulativeSizeForSwap;
Andreas Gampe67f02822016-06-24 21:05:23 -07002969 size_t very_large_threshold_ = std::numeric_limits<size_t>::max();
Mathieu Chartiera90c7722015-10-29 15:41:36 -07002970 std::string app_image_file_name_;
2971 int app_image_fd_;
Jiakai Zhang19f95682021-12-06 16:19:39 +00002972 std::vector<std::string> profile_files_;
2973 std::vector<int> profile_file_fds_;
Nicolas Geoffray3dd62192022-03-14 15:30:16 +00002974 std::vector<std::string> preloaded_classes_files_;
Nicolas Geoffray4c1e0ca2022-03-18 16:11:39 +00002975 std::vector<int> preloaded_classes_fds_;
Calin Juravle998c2162015-12-21 15:39:33 +02002976 std::unique_ptr<ProfileCompilationInfo> profile_compilation_info_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002977 TimingLogger* timings_;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002978 std::vector<std::vector<const DexFile*>> dex_files_per_oat_file_;
Vladimir Marko26de8962020-12-22 14:36:47 +00002979 HashMap<const DexFile*, size_t> dex_file_oat_index_map_;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002980
2981 // Backing storage.
Andreas Gampe097f34c2017-08-23 08:57:51 -07002982 std::forward_list<std::string> char_backing_storage_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002983
Andreas Gampeace0dc12016-01-20 13:33:13 -08002984 // See CompilerOptions.force_determinism_.
2985 bool force_determinism_;
David Sehrfe57c2b2020-03-27 14:58:54 +00002986 // 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 Gampeace0dc12016-01-20 13:33:13 -08002990
Jeff Hao16d48432017-04-05 17:05:46 -07002991 // Directory of relative classpaths.
2992 std::string classpath_dir_;
2993
Nicolas Geoffray81f57d12016-12-20 13:17:09 +00002994 // Whether the given input vdex is also the output.
Nicolas Geoffray84caad62022-01-10 09:48:55 +00002995 bool use_existing_vdex_ = false;
Nicolas Geoffray81f57d12016-12-20 13:17:09 +00002996
Nicolas Geoffray66ff8a82018-02-28 13:27:55 +00002997 // By default, copy the dex to the vdex file only if dex files are
2998 // compressed in APK.
Andreas Gampe15544172018-06-25 15:09:06 -07002999 linker::CopyOption copy_dex_files_ = linker::CopyOption::kOnlyIfCompressed;
Mathieu Chartier792111c2018-02-15 13:02:15 -08003000
Calin Juravle0e09dfc2018-02-12 19:01:09 -08003001 // The reason for invoking the compiler.
3002 std::string compilation_reason_;
3003
Eric Holkbc89ed42020-04-29 19:59:24 +00003004 // Whether to force individual compilation.
3005 bool compile_individually_;
3006
Calin Juravle33787682019-07-26 14:27:18 -07003007 // The classpath that determines if a given symbol should be resolved at compile time or not.
3008 std::string public_sdk_;
3009
Nicolas Geoffrayd3c45c22021-04-27 13:21:28 +01003010 // The apex versions of jars in the boot classpath. Set through command line
3011 // argument.
3012 std::string apex_versions_argument_;
3013
Calin Juravle028c7ef2021-05-26 15:37:00 -07003014 // Whether or we attempted to load the profile (if given).
3015 bool profile_load_attempted_;
3016
Martin Stjernholmd4aba912022-01-25 21:05:47 +00003017 // Whether PaletteNotify{Start,End}Dex2oatCompilation should be called.
3018 bool should_report_dex2oat_compilation_;
3019
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003020 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
3021};
3022
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003023static void b13564922() {
3024#if defined(__linux__) && defined(__arm__)
Lokesh Gidra58520df2020-05-13 17:08:43 +00003025 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 Gampe88ec7f42014-11-05 10:18:32 -08003030 // 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 Chartier72041a02017-07-14 18:23:25 -07003043class ScopedGlobalRef {
3044 public:
3045 explicit ScopedGlobalRef(jobject obj) : obj_(obj) {}
3046 ~ScopedGlobalRef() {
3047 if (obj_ != nullptr) {
3048 ScopedObjectAccess soa(Thread::Current());
Ian Rogers55256cb2017-12-21 17:07:11 -08003049 soa.Env()->GetVm()->DeleteGlobalRef(soa.Self(), obj_);
Mathieu Chartier72041a02017-07-14 18:23:25 -07003050 }
3051 }
3052
3053 private:
3054 jobject obj_;
3055};
3056
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00003057static dex2oat::ReturnCode DoCompilation(Dex2Oat& dex2oat) {
Mathieu Chartierc5dd3192015-12-09 16:38:30 -08003058 dex2oat.LoadClassProfileDescriptors();
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +00003059 jobject class_loader = dex2oat.Compile();
Mathieu Chartier9e050df2017-08-09 10:05:47 -07003060 // Keep the class loader that was used for compilation live for the rest of the compilation
3061 // process.
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +00003062 ScopedGlobalRef global_ref(class_loader);
Andreas Gampe10e477d2014-11-19 12:57:42 -08003063
Nicolas Geoffrayf0d30022018-11-20 17:45:38 +00003064 if (!dex2oat.WriteOutputFiles(class_loader)) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01003065 dex2oat.EraseOutputFiles();
Andreas Gampe7b565912017-03-20 15:48:52 -07003066 return dex2oat::ReturnCode::kOther;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003067 }
3068
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00003069 // Flush output files. Keep them open as we might still modify them later (strip them).
David Srbeckyde91fd42018-07-05 22:27:08 +01003070 if (!dex2oat.FlushOutputFiles()) {
3071 dex2oat.EraseOutputFiles();
Andreas Gampe7b565912017-03-20 15:48:52 -07003072 return dex2oat::ReturnCode::kOther;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003073 }
3074
Jeff Haodcdc85b2015-12-04 14:06:18 -08003075 // Creates the boot.art and patches the oat files.
Andreas Gampe10e477d2014-11-19 12:57:42 -08003076 if (!dex2oat.HandleImage()) {
Andreas Gampe7b565912017-03-20 15:48:52 -07003077 return dex2oat::ReturnCode::kOther;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003078 }
3079
3080 // When given --host, finish early without stripping.
3081 if (dex2oat.IsHost()) {
Mathieu Chartier00bf2602017-03-02 10:14:08 -08003082 if (!dex2oat.FlushCloseOutputFiles()) {
Andreas Gampe7b565912017-03-20 15:48:52 -07003083 return dex2oat::ReturnCode::kOther;
Mathieu Chartier00bf2602017-03-02 10:14:08 -08003084 }
Andreas Gampe10e477d2014-11-19 12:57:42 -08003085 dex2oat.DumpTiming();
Andreas Gampe7b565912017-03-20 15:48:52 -07003086 return dex2oat::ReturnCode::kNoFailure;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003087 }
3088
Jeff Haodcdc85b2015-12-04 14:06:18 -08003089 // 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 Srbeckyde91fd42018-07-05 22:27:08 +01003091 if (!dex2oat.CopyOatFilesToSymbolsDirectoryAndStrip()) {
Andreas Gampe7b565912017-03-20 15:48:52 -07003092 return dex2oat::ReturnCode::kOther;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003093 }
3094
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00003095 // FlushClose again, as stripping might have re-opened the oat files.
David Brazdil7b49e6c2016-09-01 11:06:18 +01003096 if (!dex2oat.FlushCloseOutputFiles()) {
Andreas Gampe7b565912017-03-20 15:48:52 -07003097 return dex2oat::ReturnCode::kOther;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003098 }
3099
3100 dex2oat.DumpTiming();
Andreas Gampe7b565912017-03-20 15:48:52 -07003101 return dex2oat::ReturnCode::kNoFailure;
Andreas Gampe10e477d2014-11-19 12:57:42 -08003102}
3103
Andreas Gampe7b565912017-03-20 15:48:52 -07003104static dex2oat::ReturnCode Dex2oat(int argc, char** argv) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003105 b13564922();
3106
3107 TimingLogger timings("compiler", false, false);
3108
Roland Levillain5c7e2602016-02-23 18:08:53 +00003109 // 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 Gampe8bdda5a2017-06-08 15:30:36 -07003113 std::unique_ptr<Dex2Oat> dex2oat = std::make_unique<Dex2Oat>(&timings);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003114
3115 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
Roland Levillain5c7e2602016-02-23 18:08:53 +00003116 dex2oat->ParseArgs(argc, argv);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003117
Calin Juravle814a80f2019-06-26 15:01:10 -07003118 art::MemMap::Init(); // For ZipEntry::ExtractToMemMap, vdex and profiles.
3119
Calin Juravleb077e152016-02-18 18:47:37 +00003120 // If needed, process profile information for profile guided compilation.
Calin Juravle998c2162015-12-21 15:39:33 +02003121 // This operation involves I/O.
Calin Juravle028c7ef2021-05-26 15:37:00 -07003122 if (dex2oat->HasProfileInput()) {
Calin Juravleb077e152016-02-18 18:47:37 +00003123 if (!dex2oat->LoadProfile()) {
3124 LOG(ERROR) << "Failed to process profile file";
Andreas Gampe7b565912017-03-20 15:48:52 -07003125 return dex2oat::ReturnCode::kOther;
Calin Juravle998c2162015-12-21 15:39:33 +02003126 }
3127 }
3128
Calin Juravle028c7ef2021-05-26 15:37:00 -07003129 // 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 Geoffrayb0bbe8e2016-11-19 10:42:37 +00003138
Vladimir Marko9bdf1082016-01-21 12:15:52 +00003139 // Check early that the result of compilation can be written
Roland Levillain5c7e2602016-02-23 18:08:53 +00003140 if (!dex2oat->OpenFile()) {
Calin Juravle046cecf2020-12-07 18:31:28 -08003141 // Flush close so that the File Guard checks don't fail the assertions.
3142 dex2oat->FlushCloseOutputFiles();
Andreas Gampe7b565912017-03-20 15:48:52 -07003143 return dex2oat::ReturnCode::kOther;
Vladimir Marko9bdf1082016-01-21 12:15:52 +00003144 }
3145
Andreas Gampe046c7062015-05-18 23:22:54 -07003146 // 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 Marko21910692019-11-06 13:27:03 +00003152 if (kIsDebugBuild ||
3153 dex2oat->IsBootImage() || dex2oat->IsBootImageExtension() ||
3154 dex2oat->IsHost() ||
3155 !kIsTargetBuild) {
Andreas Gampe046c7062015-05-18 23:22:54 -07003156 LOG(INFO) << CommandLine();
3157 } else {
3158 LOG(INFO) << StrippedCommandLine();
3159 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003160
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00003161 Dex2Oat::ScopedDex2oatReporting sdr(*dex2oat.get());
3162
3163 if (sdr.ErrorReporting()) {
3164 dex2oat->EraseOutputFiles();
3165 return dex2oat::ReturnCode::kOther;
3166 }
3167
Andreas Gampef7882972017-03-20 16:35:24 -07003168 dex2oat::ReturnCode setup_code = dex2oat->Setup();
3169 if (setup_code != dex2oat::ReturnCode::kNoFailure) {
David Brazdil7b49e6c2016-09-01 11:06:18 +01003170 dex2oat->EraseOutputFiles();
Andreas Gampef7882972017-03-20 16:35:24 -07003171 return setup_code;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003172 }
3173
Shubham Ajmera188b2bf2017-09-20 15:53:35 -07003174 // 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 Juravle028c7ef2021-05-26 15:37:00 -07003183 if (dex2oat->DoProfileGuidedOptimizations()) {
Shubham Ajmera188b2bf2017-09-20 15:53:35 -07003184 dex2oat->VerifyProfileData();
3185 }
3186
Wojciech Staszkiewicz529827d2016-09-15 10:30:30 -07003187 // 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 Staszkiewicz0a4c75f2016-09-13 12:06:36 -07003189 VLOG(compiler) << "Running dex2oat (parent PID = " << getppid() << ")";
3190
Nicolas Geoffray1ece2992021-01-06 17:02:04 +00003191 dex2oat::ReturnCode result = DoCompilation(*dex2oat);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07003192
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07003193 return result;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003194}
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003195} // namespace art
Brian Carlstrom7940e442013-07-12 13:46:57 -07003196
3197int main(int argc, char** argv) {
Andreas Gampe7b565912017-03-20 15:48:52 -07003198 int result = static_cast<int>(art::Dex2oat(argc, argv));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003199 // Everything was done, do an explicit exit here to avoid running Runtime destructors that take
Roland Levillain05e34f42018-05-24 13:19:05 +00003200 // 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 Levillainbdf17372021-05-06 00:19:19 +01003203 art::FastExit(result);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08003204 }
3205 return result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07003206}