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