blob: abb0bd1367874d979fe70020cf5e1517b238ce30 [file] [log] [blame]
* Copyright (C) 2011 The Android Open Source Project
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "base/memory_tool.h"
#include <forward_list>
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#if defined(__linux__)
#include <sched.h>
#if defined(__arm__)
#include <sys/personality.h>
#include <sys/utsname.h>
#endif // __arm__
#include "android-base/parseint.h"
#include "android-base/stringprintf.h"
#include "android-base/strings.h"
#include "aot_class_linker.h"
#include "arch/instruction_set_features.h"
#include "art_method-inl.h"
#include "base/callee_save_type.h"
#include "base/dumpable.h"
#include "base/file_utils.h"
#include "base/leb128.h"
#include "base/macros.h"
#include "base/mutex.h"
#include "base/os.h"
#include "base/scoped_flock.h"
#include "base/stl_util.h"
#include "base/time_utils.h"
#include "base/timing_logger.h"
#include "base/unix_file/fd_file.h"
#include "base/utils.h"
#include "base/zip_archive.h"
#include "class_linker.h"
#include "class_loader_context.h"
#include "cmdline_parser.h"
#include "compiler.h"
#include "compiler_callbacks.h"
#include "debug/elf_debug_writer.h"
#include "debug/method_debug_info.h"
#include "dex/descriptors_names.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_file_loader.h"
#include "dex/quick_compiler_callbacks.h"
#include "dex/verification_results.h"
#include "dex2oat_options.h"
#include "dex2oat_return_codes.h"
#include "dexlayout.h"
#include "driver/compiler_driver.h"
#include "driver/compiler_options.h"
#include "driver/compiler_options_map-inl.h"
#include "elf_file.h"
#include "gc/space/image_space.h"
#include "gc/space/space-inl.h"
#include "gc/verification.h"
#include "interpreter/unstarted_runtime.h"
#include "jni/java_vm_ext.h"
#include "linker/elf_writer.h"
#include "linker/elf_writer_quick.h"
#include "linker/image_writer.h"
#include "linker/multi_oat_relative_patcher.h"
#include "linker/oat_writer.h"
#include "mirror/class-alloc-inl.h"
#include "mirror/class_loader.h"
#include "mirror/object-inl.h"
#include "mirror/object_array-inl.h"
#include "oat.h"
#include "oat_file.h"
#include "oat_file_assistant.h"
#include "profile/profile_compilation_info.h"
#include "runtime.h"
#include "runtime_options.h"
#include "scoped_thread_state_change-inl.h"
#include "stream/buffered_output_stream.h"
#include "stream/file_output_stream.h"
#include "vdex_file.h"
#include "verifier/verifier_deps.h"
#include "well_known_classes.h"
namespace art {
using android::base::StringAppendV;
using android::base::StringPrintf;
using gc::space::ImageSpace;
static constexpr size_t kDefaultMinDexFilesForSwap = 2;
static constexpr size_t kDefaultMinDexFileCumulativeSizeForSwap = 20 * MB;
// Compiler filter override for very large apps.
static constexpr CompilerFilter::Filter kLargeAppFilter = CompilerFilter::kVerify;
static int original_argc;
static char** original_argv;
static std::string CommandLine() {
std::vector<std::string> command;
for (int i = 0; i < original_argc; ++i) {
return android::base::Join(command, ' ');
// A stripped version. Remove some less essential parameters. If we see a "--zip-fd=" parameter, be
// even more aggressive. There won't be much reasonable data here for us in that case anyways (the
// locations are all staged).
static std::string StrippedCommandLine() {
std::vector<std::string> command;
// Do a pre-pass to look for zip-fd and the compiler filter.
bool saw_zip_fd = false;
bool saw_compiler_filter = false;
for (int i = 0; i < original_argc; ++i) {
if (android::base::StartsWith(original_argv[i], "--zip-fd=")) {
saw_zip_fd = true;
if (android::base::StartsWith(original_argv[i], "--compiler-filter=")) {
saw_compiler_filter = true;
// Now filter out things.
for (int i = 0; i < original_argc; ++i) {
// All runtime-arg parameters are dropped.
if (strcmp(original_argv[i], "--runtime-arg") == 0) {
i++; // Drop the next part, too.
// Any instruction-setXXX is dropped.
if (android::base::StartsWith(original_argv[i], "--instruction-set")) {
// The boot image is dropped.
if (android::base::StartsWith(original_argv[i], "--boot-image=")) {
// The image format is dropped.
if (android::base::StartsWith(original_argv[i], "--image-format=")) {
// This should leave any dex-file and oat-file options, describing what we compiled.
// However, we prefer to drop this when we saw --zip-fd.
if (saw_zip_fd) {
// Drop anything --zip-X, --dex-X, --oat-X, --swap-X, or --app-image-X
if (android::base::StartsWith(original_argv[i], "--zip-") ||
android::base::StartsWith(original_argv[i], "--dex-") ||
android::base::StartsWith(original_argv[i], "--oat-") ||
android::base::StartsWith(original_argv[i], "--swap-") ||
android::base::StartsWith(original_argv[i], "--app-image-")) {
if (!saw_compiler_filter) {
command.push_back("--compiler-filter=" +
// Construct the final output.
if (command.size() <= 1U) {
// It seems only "/apex/" is left, or not
// even that. Use a pretty line.
return "Starting dex2oat.";
return android::base::Join(command, ' ');
static void UsageErrorV(const char* fmt, va_list ap) {
std::string error;
StringAppendV(&error, fmt, ap);
LOG(ERROR) << error;
static void UsageError(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
UsageErrorV(fmt, ap);
NO_RETURN static void Usage(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
UsageErrorV(fmt, ap);
UsageError("Command: %s", CommandLine().c_str());
UsageError("Usage: dex2oat [options]...");
UsageError(" -j<number>: specifies the number of threads used for compilation.");
UsageError(" Default is the number of detected hardware threads available on the");
UsageError(" host system.");
UsageError(" Example: -j12");
UsageError(" --cpu-set=<set>: sets the cpu affinity to <set>. The <set> argument is a comma");
UsageError(" separated list of CPUs.");
UsageError(" Example: --cpu-set=0,1,2,3");
UsageError(" --dex-file=<dex-file>: specifies a .dex, .jar, or .apk file to compile.");
UsageError(" Example: --dex-file=/system/framework/core.jar");
UsageError(" --dex-location=<dex-location>: specifies an alternative dex location to");
UsageError(" encode in the oat file for the corresponding --dex-file argument.");
UsageError(" Example: --dex-file=/home/build/out/system/framework/core.jar");
UsageError(" --dex-location=/system/framework/core.jar");
UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
UsageError(" containing a classes.dex file to compile.");
UsageError(" Example: --zip-fd=5");
UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file");
UsageError(" corresponding to the file descriptor specified by --zip-fd.");
UsageError(" Example: --zip-location=/system/app/Calculator.apk");
UsageError(" --oat-file=<file.oat>: specifies an oat output destination via a filename.");
UsageError(" Example: --oat-file=/system/framework/boot.oat");
UsageError(" --oat-symbols=<file.oat>: specifies a symbolized oat output destination.");
UsageError(" Example: --oat-file=symbols/system/framework/boot.oat");
UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
UsageError(" Example: --oat-fd=6");
UsageError(" --input-vdex-fd=<number>: specifies the vdex input source via a file descriptor.");
UsageError(" Example: --input-vdex-fd=6");
UsageError(" --output-vdex-fd=<number>: specifies the vdex output destination via a file");
UsageError(" descriptor.");
UsageError(" Example: --output-vdex-fd=6");
UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
UsageError(" to the file descriptor specified by --oat-fd.");
UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
UsageError(" --oat-symbols=<file.oat>: specifies a destination where the oat file is copied.");
UsageError(" This is equivalent to file copy as build post-processing step.");
UsageError(" It is intended to be used with --strip and it happens before it.");
UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat");
UsageError(" --strip: remove all debugging sections at the end (but keep mini-debug-info).");
UsageError(" This is equivalent to the \"strip\" command as build post-processing step.");
UsageError(" It is intended to be used with --oat-symbols and it happens after it.");
UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat");
UsageError(" --image=<>: specifies an output image filename.");
UsageError(" Example: --image=/system/framework/");
UsageError(" --image-fd=<number>: same as --image but accepts a file descriptor instead.");
UsageError(" Cannot be used together with --image.");
UsageError(" --image-format=(uncompressed|lz4|lz4hc):");
UsageError(" Which format to store the image.");
UsageError(" Example: --image-format=lz4");
UsageError(" Default: uncompressed");
UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
UsageError(" Example: --base=0x50000000");
UsageError(" --boot-image=<>: provide the image file for the boot class path.");
UsageError(" Do not include the arch as part of the name, it is added automatically.");
UsageError(" Example: --boot-image=/system/framework/");
UsageError(" (specifies /system/framework/<arch>/ as the image file)");
UsageError(" Example:");
UsageError(" (specifies <bcp-path1>/<arch>/ as the image file and");
UsageError(" <bcp-path2>/<arch>/ as the image extension file");
UsageError(" with paths taken from corresponding boot class path components)");
UsageError(" Example: --boot-image=/apex/*:*");
UsageError(" (specifies /apex/<arch>/ as the image");
UsageError(" file and search for extensions in /framework/system and boot");
UsageError(" class path components' paths)");
UsageError(" Default: $ANDROID_ROOT/system/framework/");
UsageError(" --android-root=<path>: used to locate libraries for portable linking.");
UsageError(" Example: --android-root=out/host/linux-x86");
UsageError(" Default: $ANDROID_ROOT");
UsageError(" --instruction-set=(arm|arm64|x86|x86_64): compile for a particular");
UsageError(" instruction set.");
UsageError(" Example: --instruction-set=x86");
UsageError(" Default: arm");
UsageError(" --instruction-set-features=...,: Specify instruction set features");
UsageError(" On target the value 'runtime' can be used to detect features at run time.");
UsageError(" If target does not support run-time detection the value 'runtime'");
UsageError(" has the same effect as the value 'default'.");
UsageError(" Note: the value 'runtime' has no effect if it is used on host.");
UsageError(" Example: --instruction-set-features=div");
UsageError(" Default: default");
UsageError(" --compiler-backend=(Quick|Optimizing): select compiler backend");
UsageError(" set.");
UsageError(" Example: --compiler-backend=Optimizing");
UsageError(" Default: Optimizing");
UsageError(" --compiler-filter="
UsageError(" select compiler filter.");
UsageError(" Example: --compiler-filter=everything");
UsageError(" Default: speed-profile if --profile-file or --profile-file-fd is used,");
UsageError(" speed otherwise");
UsageError(" --huge-method-max=<method-instruction-count>: threshold size for a huge");
UsageError(" method for compiler filter tuning.");
UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold);
UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold);
UsageError(" --large-method-max=<method-instruction-count>: threshold size for a large");
UsageError(" method for compiler filter tuning.");
UsageError(" Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold);
UsageError(" Default: %d", CompilerOptions::kDefaultLargeMethodThreshold);
UsageError(" --num-dex-methods=<method-count>: threshold size for a small dex file for");
UsageError(" compiler filter tuning. If the input has fewer than this many methods");
UsageError(" and the filter is not interpret-only or verify-none or verify-at-runtime, ");
UsageError(" overrides the filter to use speed");
UsageError(" Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold);
UsageError(" Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold);
UsageError(" --inline-max-code-units=<code-units-count>: the maximum code units that a method");
UsageError(" can have to be considered for inlining. A zero value will disable inlining.");
UsageError(" Honored only by Optimizing. Has priority over the --compiler-filter option.");
UsageError(" Intended for development/experimental use.");
UsageError(" Example: --inline-max-code-units=%d",
UsageError(" Default: %d", CompilerOptions::kDefaultInlineMaxCodeUnits);
UsageError(" --dump-timings: display a breakdown of where time was spent");
UsageError(" --dump-pass-timings: display a breakdown of time spent in optimization");
UsageError(" passes for each compiled method.");
UsageError(" -g");
UsageError(" --generate-debug-info: Generate debug information for native debugging,");
UsageError(" such as stack unwinding information, ELF symbols and DWARF sections.");
UsageError(" If used without --debuggable, it will be best-effort only.");
UsageError(" This option does not affect the generated code. (disabled by default)");
UsageError(" --no-generate-debug-info: Do not generate debug information for native debugging.");
UsageError(" --generate-mini-debug-info: Generate minimal amount of LZMA-compressed");
UsageError(" debug information necessary to print backtraces. (disabled by default)");
UsageError(" --no-generate-mini-debug-info: Do not generate backtrace info.");
UsageError(" --generate-build-id: Generate GNU-compatible linker build ID ELF section with");
UsageError(" SHA-1 of the file content (and thus stable across identical builds)");
UsageError(" --no-generate-build-id: Do not generate the build ID ELF section.");
UsageError(" --debuggable: Produce code debuggable with Java debugger.");
UsageError(" --avoid-storing-invocation: Avoid storing the invocation args in the key value");
UsageError(" store. Used to test determinism with different args.");
UsageError(" --write-invocation-to=<file>: Write the invocation commandline to the given file");
UsageError(" for later use. Used to test determinism with different host architectures.");
UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
UsageError(" such as initial heap size, maximum heap size, and verbose output.");
UsageError(" Use a separate --runtime-arg switch for each argument.");
UsageError(" Example: --runtime-arg -Xms256m");
UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
UsageError(" --profile-file-fd=<number>: same as --profile-file but accepts a file descriptor.");
UsageError(" Cannot be used together with --profile-file.");
UsageError(" --swap-file=<file-name>: specifies a file to use for swap.");
UsageError(" Example: --swap-file=/data/tmp/swap.001");
UsageError(" --swap-fd=<file-descriptor>: specifies a file to use for swap (by descriptor).");
UsageError(" Example: --swap-fd=10");
UsageError(" --swap-dex-size-threshold=<size>: specifies the minimum total dex file size in");
UsageError(" bytes to allow the use of swap.");
UsageError(" Example: --swap-dex-size-threshold=1000000");
UsageError(" Default: %zu", kDefaultMinDexFileCumulativeSizeForSwap);
UsageError(" --swap-dex-count-threshold=<count>: specifies the minimum number of dex files to");
UsageError(" allow the use of swap.");
UsageError(" Example: --swap-dex-count-threshold=10");
UsageError(" Default: %zu", kDefaultMinDexFilesForSwap);
UsageError(" --very-large-app-threshold=<size>: specifies the minimum total dex file size in");
UsageError(" bytes to consider the input \"very large\" and reduce compilation done.");
UsageError(" Example: --very-large-app-threshold=100000000");
UsageError(" --app-image-fd=<file-descriptor>: specify output file descriptor for app image.");
UsageError(" The image is non-empty only if a profile is passed in.");
UsageError(" Example: --app-image-fd=10");
UsageError(" --app-image-file=<file-name>: specify a file name for app image.");
UsageError(" Example: --app-image-file=/data/dalvik-cache/");
UsageError(" --multi-image: specify that separate oat and image files be generated for ");
UsageError(" each input dex file; the default for boot image and boot image extension.");
UsageError(" --single-image: specify that a single oat and image file be generated for ");
UsageError(" all input dex files; the default for app image.");
UsageError(" --force-determinism: force the compiler to emit a deterministic output.");
UsageError(" --dump-cfg=<cfg-file>: dump control-flow graphs (CFGs) to specified file.");
UsageError(" Example: --dump-cfg=output.cfg");
UsageError(" --dump-cfg-append: when dumping CFGs to an existing file, append new CFG data to");
UsageError(" existing data (instead of overwriting existing data with new data, which is");
UsageError(" the default behavior). This option is only meaningful when used with");
UsageError(" --dump-cfg.");
UsageError(" --verbose-methods=<method-names>: Restrict dumped CFG data to methods whose name");
UsageError(" contain one of the method names passed as argument");
UsageError(" Example: --verbose-methods=toString,hashCode");
UsageError(" --classpath-dir=<directory-path>: directory used to resolve relative class paths.");
UsageError(" --class-loader-context=<string spec>: a string specifying the intended");
UsageError(" runtime loading context for the compiled dex files.");
UsageError(" --stored-class-loader-context=<string spec>: a string specifying the intended");
UsageError(" runtime loading context that is stored in the oat file. Overrides");
UsageError(" --class-loader-context. Note that this ignores the classpath_dir arg.");
UsageError(" It describes how the class loader chain should be built in order to ensure");
UsageError(" classes are resolved during dex2aot as they would be resolved at runtime.");
UsageError(" This spec will be encoded in the oat file. If at runtime the dex file is");
UsageError(" loaded in a different context, the oat file will be rejected.");
UsageError(" The chain is interpreted in the natural 'parent order', meaning that class");
UsageError(" loader 'i+1' will be the parent of class loader 'i'.");
UsageError(" The compilation sources will be appended to the classpath of the first class");
UsageError(" loader.");
UsageError(" E.g. if the context is 'PCL[lib1.dex];DLC[lib2.dex]' and ");
UsageError(" --dex-file=src.dex then dex2oat will setup a PathClassLoader with classpath ");
UsageError(" 'lib1.dex:src.dex' and set its parent to a DelegateLastClassLoader with ");
UsageError(" classpath 'lib2.dex'.");
UsageError(" Note that the compiler will be tolerant if the source dex files specified");
UsageError(" with --dex-file are found in the classpath. The source dex files will be");
UsageError(" removed from any class loader's classpath possibly resulting in empty");
UsageError(" class loaders.");
UsageError(" Example: --class-loader-context=PCL[lib1.dex:lib2.dex];DLC[lib3.dex]");
UsageError(" --class-loader-context-fds=<fds>: a colon-separated list of file descriptors");
UsageError(" for dex files in --class-loader-context. Their order must be the same as");
UsageError(" dex files in flattened class loader context.");
UsageError(" --dirty-image-objects=<file-path>: list of known dirty objects in the image.");
UsageError(" The image writer will group them together.");
UsageError(" --updatable-bcp-packages-file=<file-path>: file with a list of updatable");
UsageError(" boot class path packages. Classes in these packages and sub-packages");
UsageError(" shall not be resolved during app compilation to avoid AOT assumptions");
UsageError(" being invalidated after applying updates to these components.");
UsageError(" --compact-dex-level=none|fast: None avoids generating compact dex, fast");
UsageError(" generates compact dex with low compile time. If speed-profile is specified as");
UsageError(" the compiler filter and the profile is not empty, the default compact dex");
UsageError(" level is always used.");
UsageError(" --deduplicate-code=true|false: enable|disable code deduplication. Deduplicated");
UsageError(" code will have an arbitrary symbol tagged with [DEDUPED].");
UsageError(" --copy-dex-files=true|false: enable|disable copying the dex files into the");
UsageError(" output vdex.");
UsageError(" --compilation-reason=<string>: optional metadata specifying the reason for");
UsageError(" compiling the apk. If specified, the string will be embedded verbatim in");
UsageError(" the key value store of the oat file.");
UsageError(" Example: --compilation-reason=install");
UsageError(" --resolve-startup-const-strings=true|false: If true, the compiler eagerly");
UsageError(" resolves strings referenced from const-string of startup methods.");
UsageError(" --max-image-block-size=<size>: Maximum solid block size for compressed images.");
std::cerr << "See log for usage error information\n";
// Set CPU affinity from a string containing a comma-separated list of numeric CPU identifiers.
static void SetCpuAffinity(const std::vector<int32_t>& cpu_list) {
#ifdef __linux__
int cpu_count = sysconf(_SC_NPROCESSORS_CONF);
cpu_set_t target_cpu_set;
for (int32_t cpu : cpu_list) {
if (cpu >= 0 && cpu < cpu_count) {
CPU_SET(cpu, &target_cpu_set);
} else {
// Argument error is considered fatal, suggests misconfigured system properties.
Usage("Invalid cpu \"d\" specified in --cpu-set argument (nprocessors = %d)",
cpu, cpu_count);
if (sched_setaffinity(getpid(), sizeof(target_cpu_set), &target_cpu_set) == -1) {
// Failure to set affinity may be outside control of requestor, log warning rather than
// treating as fatal.
PLOG(WARNING) << "Failed to set CPU affinity.";
LOG(WARNING) << "--cpu-set not supported on this platform.";
#endif // __linux__
// The primary goal of the watchdog is to prevent stuck build servers
// during development when fatal aborts lead to a cascade of failures
// that result in a deadlock.
class WatchDog {
// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks
#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
do { \
int rc = call args; \
if (rc != 0) { \
errno = rc; \
std::string message(# call); \
message += " failed for "; \
message += reason; \
Fatal(message); \
} \
} while (false)
explicit WatchDog(int64_t timeout_in_milliseconds)
: timeout_in_milliseconds_(timeout_in_milliseconds),
shutting_down_(false) {
const char* reason = "dex2oat watch dog thread startup";
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason);
#ifndef __APPLE__
pthread_condattr_t condattr;
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_condattr_init, (&condattr), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_condattr_setclock, (&condattr, CLOCK_MONOTONIC), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, &condattr), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_condattr_destroy, (&condattr), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
~WatchDog() {
const char* reason = "dex2oat watch dog thread shutdown";
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
shutting_down_ = true;
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
static void SetRuntime(Runtime* runtime) {
const char* reason = "dex2oat watch dog set runtime";
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&runtime_mutex_), reason);
runtime_ = runtime;
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&runtime_mutex_), reason);
// TODO: tune the multiplier for GC verification, the following is just to make the timeout
// large.
static constexpr int64_t kWatchdogVerifyMultiplier =
kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1;
// When setting timeouts, keep in mind that the build server may not be as fast as your
// desktop. Debug builds are slower so they have larger timeouts.
static constexpr int64_t kWatchdogSlowdownFactor = kIsDebugBuild ? 5U : 1U;
// 9.5 minutes scaled by kSlowdownFactor. This is slightly smaller than the Package Manager
// watchdog (PackageManagerService.WATCHDOG_TIMEOUT, 10 minutes), so that dex2oat will abort
// itself before that watchdog would take down the system server.
static constexpr int64_t kWatchDogTimeoutSeconds = kWatchdogSlowdownFactor * (9 * 60 + 30);
static constexpr int64_t kDefaultWatchdogTimeoutInMS =
kWatchdogVerifyMultiplier * kWatchDogTimeoutSeconds * 1000;
static void* CallBack(void* arg) {
WatchDog* self = reinterpret_cast<WatchDog*>(arg);
::art::SetThreadName("dex2oat watch dog");
return nullptr;
NO_RETURN static void Fatal(const std::string& message) {
// TODO: When we can guarantee it won't prevent shutdown in error cases, move to LOG. However,
// it's rather easy to hang in unwinding.
// LogLine also avoids ART logging lock issues, as it's really only a wrapper around
// logcat logging or stderr output.
LogHelper::LogLineLowStack(__FILE__, __LINE__, LogSeverity::FATAL, message.c_str());
// If we're on the host, try to dump all threads to get a sense of what's going on. This is
// restricted to the host as the dump may itself go bad.
// TODO: Use a double watchdog timeout, so we can enable this on-device.
Runtime* runtime = GetRuntime();
if (!kIsTargetBuild && runtime != nullptr) {
runtime->AttachCurrentThread("Watchdog thread attached for dumping",
void Wait() {
timespec timeout_ts;
#if defined(__APPLE__)
InitTimeSpec(true, CLOCK_REALTIME, timeout_in_milliseconds_, 0, &timeout_ts);
InitTimeSpec(true, CLOCK_MONOTONIC, timeout_in_milliseconds_, 0, &timeout_ts);
const char* reason = "dex2oat watch dog thread waiting";
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
while (!shutting_down_) {
int rc = pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts);
if (rc == EINTR) {
} else if (rc == ETIMEDOUT) {
Fatal(StringPrintf("dex2oat did not finish after %" PRId64 " seconds",
} else if (rc != 0) {
std::string message(StringPrintf("pthread_cond_timedwait failed: %s", strerror(rc)));
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
static Runtime* GetRuntime() {
const char* reason = "dex2oat watch dog get runtime";
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&runtime_mutex_), reason);
Runtime* runtime = runtime_;
CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&runtime_mutex_), reason);
return runtime;
static pthread_mutex_t runtime_mutex_;
static Runtime* runtime_;
// TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
pthread_mutex_t mutex_;
pthread_cond_t cond_;
pthread_attr_t attr_;
pthread_t pthread_;
const int64_t timeout_in_milliseconds_;
bool shutting_down_;
pthread_mutex_t WatchDog::runtime_mutex_ = PTHREAD_MUTEX_INITIALIZER;
Runtime* WatchDog::runtime_ = nullptr;
// Helper class for overriding `java.lang.ThreadLocal.nextHashCode`.
// The class ThreadLocal has a static field nextHashCode used for assigning hash codes to
// new ThreadLocal objects. Since the class and the object referenced by the field are
// in the boot image, they cannot be modified under normal rules for AOT compilation.
// However, since this is a private detail that's used only for assigning hash codes and
// everything should work fine with different hash codes, we override the field for the
// compilation, providing another object that the AOT class initialization can modify.
class ThreadLocalHashOverride {
ThreadLocalHashOverride(bool apply, int32_t initial_value) {
Thread* self = Thread::Current();
ScopedObjectAccess soa(self);
hs_.emplace(self); // While holding the mutator lock.
Runtime* runtime = Runtime::Current();
klass_ = hs_->NewHandle(apply
? runtime->GetClassLinker()->LookupClass(self,
/*class_loader=*/ nullptr)
: nullptr);
field_ = ((klass_ != nullptr) && klass_->IsVisiblyInitialized())
? klass_->FindDeclaredStaticField("nextHashCode",
: nullptr;
old_field_value_ =
hs_->NewHandle(field_ != nullptr ? field_->GetObject(klass_.Get()) : nullptr);
if (old_field_value_ != nullptr) {
gc::AllocatorType allocator_type = runtime->GetHeap()->GetCurrentAllocator();
StackHandleScope<1u> hs2(self);
Handle<mirror::Object> new_field_value = hs2.NewHandle(
old_field_value_->GetClass()->Alloc(self, allocator_type));
PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
ArtMethod* constructor = old_field_value_->GetClass()->FindConstructor("(I)V", pointer_size);
CHECK(constructor != nullptr);
uint32_t args[] = {
JValue result;
constructor->Invoke(self, args, sizeof(args), &result, /*shorty=*/ "VI");
field_->SetObject</*kTransactionActive=*/ false>(klass_.Get(), new_field_value.Get());
if (apply && old_field_value_ == nullptr) {
if ((klass_ != nullptr) && klass_->IsVisiblyInitialized()) {
// This would mean that the implementation of ThreadLocal has changed
// and the code above is no longer applicable.
LOG(ERROR) << "Failed to override ThreadLocal.nextHashCode";
} else {
VLOG(compiler) << "ThreadLocal is not initialized in the primary boot image.";
~ThreadLocalHashOverride() {
ScopedObjectAccess soa(hs_->Self());
if (old_field_value_ != nullptr) {
// Allow the overriding object to be collected.
field_->SetObject</*kTransactionActive=*/ false>(klass_.Get(), old_field_value_.Get());
hs_.reset(); // While holding the mutator lock.
std::optional<StackHandleScope<2u>> hs_;
Handle<mirror::Class> klass_;
ArtField* field_;
Handle<mirror::Object> old_field_value_;
class Dex2Oat final {
explicit Dex2Oat(TimingLogger* timings) :
// Take the default set of instruction features from the build.
~Dex2Oat() {
// Log completion time before deleting the runtime_, because this accesses
// the runtime.
if (!kIsDebugBuild && !(kRunningOnMemoryTool && kMemoryToolDetectsLeaks)) {
// We want to just exit on non-debug builds, not bringing the runtime down
// in an orderly fashion. So release the following fields.
driver_.release(); // NOLINT
image_writer_.release(); // NOLINT
for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files_) {
dex_file.release(); // NOLINT
new std::vector<MemMap>(std::move(opened_dex_files_maps_)); // Leak MemMaps.
for (std::unique_ptr<File>& vdex_file : vdex_files_) {
vdex_file.release(); // NOLINT
for (std::unique_ptr<File>& oat_file : oat_files_) {
oat_file.release(); // NOLINT
runtime_.release(); // NOLINT
verification_results_.release(); // NOLINT
key_value_store_.release(); // NOLINT
struct ParserOptions {
std::vector<std::string> oat_symbols;
std::string boot_image_filename;
int64_t watch_dog_timeout_in_ms = -1;
bool watch_dog_enabled = true;
bool requested_specific_compiler = false;
std::string error_msg;
void ParseBase(const std::string& option) {
char* end;
image_base_ = strtoul(option.c_str(), &end, 16);
if (end == option.c_str() || *end != '\0') {
Usage("Failed to parse hexadecimal value for option %s",;
bool VerifyProfileData() {
return profile_compilation_info_->VerifyProfileData(compiler_options_->dex_files_for_oat_file_);
void ParseInstructionSetVariant(const std::string& option, ParserOptions* parser_options) {
compiler_options_->instruction_set_features_ = InstructionSetFeatures::FromVariant(
compiler_options_->instruction_set_, option, &parser_options->error_msg);
if (compiler_options_->instruction_set_features_ == nullptr) {
Usage("%s", parser_options->error_msg.c_str());
void ParseInstructionSetFeatures(const std::string& option, ParserOptions* parser_options) {
if (compiler_options_->instruction_set_features_ == nullptr) {
compiler_options_->instruction_set_features_ = InstructionSetFeatures::FromVariant(
compiler_options_->instruction_set_, "default", &parser_options->error_msg);
if (compiler_options_->instruction_set_features_ == nullptr) {
Usage("Problem initializing default instruction set features variant: %s",
compiler_options_->instruction_set_features_ =
option, &parser_options->error_msg);
if (compiler_options_->instruction_set_features_ == nullptr) {
Usage("Error parsing '%s': %s", option.c_str(), parser_options->error_msg.c_str());
void ProcessOptions(ParserOptions* parser_options) {
compiler_options_->compile_pic_ = true; // All AOT compilation is PIC.
if (android_root_.empty()) {
const char* android_root_env_var = getenv("ANDROID_ROOT");
if (android_root_env_var == nullptr) {
Usage("--android-root unspecified and ANDROID_ROOT not set");
android_root_ += android_root_env_var;
if (!parser_options->boot_image_filename.empty()) {
boot_image_filename_ = parser_options->boot_image_filename;
DCHECK(compiler_options_->image_type_ == CompilerOptions::ImageType::kNone);
if (!image_filenames_.empty() || image_fd_ != -1) {
// If no boot image is provided, then dex2oat is compiling the primary boot image,
// otherwise it is compiling the boot image extension.
compiler_options_->image_type_ = boot_image_filename_.empty()
? CompilerOptions::ImageType::kBootImage
: CompilerOptions::ImageType::kBootImageExtension;
if (app_image_fd_ != -1 || !app_image_file_name_.empty()) {
if (compiler_options_->IsBootImage() || compiler_options_->IsBootImageExtension()) {
Usage("Can't have both (--image or --image-fd) and (--app-image-fd or --app-image-file)");
compiler_options_->image_type_ = CompilerOptions::ImageType::kAppImage;
if (!image_filenames_.empty() && image_fd_ != -1) {
Usage("Can't have both --image and --image-fd");
if (oat_filenames_.empty() && oat_fd_ == -1) {
Usage("Output must be supplied with either --oat-file or --oat-fd");
if (input_vdex_fd_ != -1 && !input_vdex_.empty()) {
Usage("Can't have both --input-vdex-fd and --input-vdex");
if (output_vdex_fd_ != -1 && !output_vdex_.empty()) {
Usage("Can't have both --output-vdex-fd and --output-vdex");
if (!oat_filenames_.empty() && oat_fd_ != -1) {
Usage("--oat-file should not be used with --oat-fd");
if ((output_vdex_fd_ == -1) != (oat_fd_ == -1)) {
Usage("VDEX and OAT output must be specified either with one --oat-file "
"or with --oat-fd and --output-vdex-fd file descriptors");
if ((image_fd_ != -1) && (oat_fd_ == -1)) {
Usage("--image-fd must be used with --oat_fd and --output_vdex_fd");
if (!parser_options->oat_symbols.empty() && oat_fd_ != -1) {
Usage("--oat-symbols should not be used with --oat-fd");
if (!parser_options->oat_symbols.empty() && is_host_) {
Usage("--oat-symbols should not be used with --host");
if (output_vdex_fd_ != -1 && !image_filenames_.empty()) {
Usage("--output-vdex-fd should not be used with --image");
if (oat_fd_ != -1 && !image_filenames_.empty()) {
Usage("--oat-fd should not be used with --image");
if ((input_vdex_fd_ != -1 || !input_vdex_.empty()) &&
(dm_fd_ != -1 || !dm_file_location_.empty())) {
Usage("An input vdex should not be passed with a .dm file");
if (!parser_options->oat_symbols.empty() &&
parser_options->oat_symbols.size() != oat_filenames_.size()) {
Usage("--oat-file arguments do not match --oat-symbols arguments");
if (!image_filenames_.empty() && image_filenames_.size() != oat_filenames_.size()) {
Usage("--oat-file arguments do not match --image arguments");
if (!IsBootImage() && boot_image_filename_.empty()) {
boot_image_filename_ = GetDefaultBootImageLocation(android_root_);
if (dex_filenames_.empty() && zip_fd_ == -1) {
Usage("Input must be supplied with either --dex-file or --zip-fd");
if (!dex_filenames_.empty() && zip_fd_ != -1) {
Usage("--dex-file should not be used with --zip-fd");
if (!dex_filenames_.empty() && !zip_location_.empty()) {
Usage("--dex-file should not be used with --zip-location");
if (dex_locations_.empty()) {
dex_locations_ = dex_filenames_;
} else if (dex_locations_.size() != dex_filenames_.size()) {
Usage("--dex-location arguments do not match --dex-file arguments");
if (!dex_filenames_.empty() && !oat_filenames_.empty()) {
if (oat_filenames_.size() != 1 && oat_filenames_.size() != dex_filenames_.size()) {
Usage("--oat-file arguments must be singular or match --dex-file arguments");
if (zip_fd_ != -1 && zip_location_.empty()) {
Usage("--zip-location should be supplied with --zip-fd");
if (boot_image_filename_.empty()) {
if (image_base_ == 0) {
Usage("Non-zero --base not specified for boot image");
} else {
if (image_base_ != 0) {
Usage("Non-zero --base specified for app image or boot image extension");
if (have_multi_image_arg_) {
if (!IsImage()) {
Usage("--multi-image or --single-image specified for non-image compilation");
} else {
// Use the default, i.e. multi-image for boot image and boot image extension.
multi_image_ = IsBootImage() || IsBootImageExtension(); // Shall pass checks below.
if (IsBootImage() && !multi_image_) {
Usage("--single-image specified for primary boot image");
if (IsAppImage() && multi_image_) {
Usage("--multi-image specified for app image");
if (image_fd_ != -1 && multi_image_) {
Usage("--single-image not specified for --image-fd");
const bool have_profile_file = !profile_file_.empty();
const bool have_profile_fd = profile_file_fd_ != kInvalidFd;
if (have_profile_file && have_profile_fd) {
Usage("Profile file should not be specified with both --profile-file-fd and --profile-file");
if (!parser_options->oat_symbols.empty()) {
oat_unstripped_ = std::move(parser_options->oat_symbols);
if (compiler_options_->instruction_set_features_ == nullptr) {
// '--instruction-set-features/--instruction-set-variant' were not used.
// Use features for the 'default' variant.
compiler_options_->instruction_set_features_ = InstructionSetFeatures::FromVariant(
compiler_options_->instruction_set_, "default", &parser_options->error_msg);
if (compiler_options_->instruction_set_features_ == nullptr) {
Usage("Problem initializing default instruction set features variant: %s",
if (compiler_options_->instruction_set_ == kRuntimeISA) {
std::unique_ptr<const InstructionSetFeatures> runtime_features(
if (!compiler_options_->GetInstructionSetFeatures()->Equals(runtime_features.get())) {
LOG(WARNING) << "Mismatch between dex2oat instruction set features to use ("
<< *compiler_options_->GetInstructionSetFeatures()
<< ") and those from CPP defines (" << *runtime_features
<< ") for the command line:\n" << CommandLine();
if ((IsBootImage() || IsBootImageExtension()) && updatable_bcp_packages_filename_ != nullptr) {
Usage("Do not specify --updatable-bcp-packages-file for boot image compilation.");
if (!cpu_set_.empty()) {
if (compiler_options_->inline_max_code_units_ == CompilerOptions::kUnsetInlineMaxCodeUnits) {
compiler_options_->inline_max_code_units_ = CompilerOptions::kDefaultInlineMaxCodeUnits;
// Checks are all explicit until we know the architecture.
// Set the compilation target's implicit checks options.
switch (compiler_options_->GetInstructionSet()) {
case InstructionSet::kArm:
case InstructionSet::kThumb2:
case InstructionSet::kArm64:
case InstructionSet::kX86:
case InstructionSet::kX86_64:
compiler_options_->implicit_null_checks_ = true;
compiler_options_->implicit_so_checks_ = true;
// Defaults are correct.
// Done with usage checks, enable watchdog if requested
if (parser_options->watch_dog_enabled) {
int64_t timeout = parser_options->watch_dog_timeout_in_ms > 0
? parser_options->watch_dog_timeout_in_ms
: WatchDog::kDefaultWatchdogTimeoutInMS;
watchdog_.reset(new WatchDog(timeout));
// Fill some values into the key-value store for the oat header.
key_value_store_.reset(new SafeMap<std::string, std::string>());
// Automatically force determinism for the boot image and boot image extensions in a host build.
if (!kIsTargetBuild && (IsBootImage() || IsBootImageExtension())) {
force_determinism_ = true;
compiler_options_->force_determinism_ = force_determinism_;
if (passes_to_run_filename_ != nullptr) {
passes_to_run_ = ReadCommentedInputFromFile<std::vector<std::string>>(
nullptr); // No post-processing.
if (passes_to_run_.get() == nullptr) {
Usage("Failed to read list of passes to run.");
// Trim the boot image location to not include any specified profile. Note
// that the logic below will include the first boot image extension, but not
// the ones that could be listed after the profile of that extension. This
// works for our current top use case:
// But this would need to be adjusted if we had to support different use
// cases.
size_t profile_separator_pos = boot_image_filename_.find(ImageSpace::kProfileSeparator);
if (profile_separator_pos != std::string::npos) {
DCHECK(!IsBootImage()); // For primary boot image the boot_image_filename_ is empty.
if (IsBootImageExtension()) {
Usage("Unsupported profile specification in boot image location (%s) for extension.",
<< "Truncating boot image location " << boot_image_filename_
<< " because it contains profile specification. Truncated: "
<< boot_image_filename_.substr(/*pos*/ 0u, /*length*/ profile_separator_pos);
compiler_options_->passes_to_run_ = passes_to_run_.get();
compiler_options_->compiling_with_core_image_ =
!boot_image_filename_.empty() &&
void ExpandOatAndImageFilenames() {
ArrayRef<const std::string> locations(dex_locations_);
if (!multi_image_) {
locations = locations.SubArray(/*pos=*/ 0u, /*length=*/ 1u);
if (image_fd_ == -1) {
if (image_filenames_[0].rfind('/') == std::string::npos) {
Usage("Unusable boot image filename %s", image_filenames_[0].c_str());
image_filenames_ = ImageSpace::ExpandMultiImageLocations(
locations, image_filenames_[0], IsBootImageExtension());
if (oat_filenames_[0].rfind('/') == std::string::npos) {
Usage("Unusable boot image oat filename %s", oat_filenames_[0].c_str());
oat_filenames_ = ImageSpace::ExpandMultiImageLocations(
locations, oat_filenames_[0], IsBootImageExtension());
} else {
std::vector<std::string> oat_locations = ImageSpace::ExpandMultiImageLocations(
locations, oat_location_, IsBootImageExtension());
DCHECK_EQ(1u, oat_locations.size());
oat_location_ = oat_locations[0];
if (!oat_unstripped_.empty()) {
if (oat_unstripped_[0].rfind('/') == std::string::npos) {
Usage("Unusable boot image symbol filename %s", oat_unstripped_[0].c_str());
oat_unstripped_ = ImageSpace::ExpandMultiImageLocations(
locations, oat_unstripped_[0], IsBootImageExtension());
void InsertCompileOptions(int argc, char** argv) {
if (!avoid_storing_invocation_) {
std::ostringstream oss;
for (int i = 0; i < argc; ++i) {
if (i > 0) {
oss << ' ';
oss << argv[i];
key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str());
compiler_options_->debuggable_ ? OatHeader::kTrueValue : OatHeader::kFalseValue);
compiler_options_->GetNativeDebuggable() ? OatHeader::kTrueValue : OatHeader::kFalseValue);
kUseReadBarrier ? OatHeader::kTrueValue : OatHeader::kFalseValue);
if (invocation_file_.get() != -1) {
std::ostringstream oss;
for (int i = 0; i < argc; ++i) {
if (i > 0) {
oss << std::endl;
oss << argv[i];
std::string invocation(oss.str());
if (TEMP_FAILURE_RETRY(write(invocation_file_.get(),
invocation.size())) == -1) {
Usage("Unable to write invocation file");
// This simple forward is here so the string specializations below don't look out of place.
template <typename T, typename U>
void AssignIfExists(Dex2oatArgumentMap& map,
const Dex2oatArgumentMap::Key<T>& key,
U* out) {
map.AssignIfExists(key, out);
// Specializations to handle const char* vs std::string.
void AssignIfExists(Dex2oatArgumentMap& map,
const Dex2oatArgumentMap::Key<std::string>& key,
const char** out) {
if (map.Exists(key)) {
*out = char_backing_storage_.front().c_str();
void AssignIfExists(Dex2oatArgumentMap& map,
const Dex2oatArgumentMap::Key<std::vector<std::string>>& key,
std::vector<const char*>* out) {
if (map.Exists(key)) {
for (auto& val : *map.Get(key)) {
template <typename T>
void AssignTrueIfExists(Dex2oatArgumentMap& map,
const Dex2oatArgumentMap::Key<T>& key,
bool* out) {
if (map.Exists(key)) {
*out = true;
void AssignIfExists(Dex2oatArgumentMap& map,
const Dex2oatArgumentMap::Key<std::string>& key,
std::vector<std::string>* out) {
if (map.Exists(key)) {
// Parse the arguments from the command line. In case of an unrecognized option or impossible
// values/combinations, a usage error will be displayed and exit() is called. Thus, if the method
// returns, arguments have been successfully parsed.
void ParseArgs(int argc, char** argv) {
original_argc = argc;
original_argv = argv;
InitLogging(argv, Runtime::Abort);
compiler_options_.reset(new CompilerOptions());
using M = Dex2oatArgumentMap;
std::string error_msg;
std::unique_ptr<M> args_uptr = M::Parse(argc, const_cast<const char**>(argv), &error_msg);
if (args_uptr == nullptr) {
Usage("Failed to parse command line: %s", error_msg.c_str());
M& args = *args_uptr;
std::unique_ptr<ParserOptions> parser_options(new ParserOptions());
AssignIfExists(args, M::CompactDexLevel, &compact_dex_level_);
AssignIfExists(args, M::DexFiles, &dex_filenames_);
AssignIfExists(args, M::DexLocations, &dex_locations_);
AssignIfExists(args, M::OatFile, &oat_filenames_);
AssignIfExists(args, M::OatSymbols, &parser_options->oat_symbols);
AssignTrueIfExists(args, M::Strip, &strip_);
AssignIfExists(args, M::ImageFilename, &image_filenames_);
AssignIfExists(args, M::ImageFd, &image_fd_);
AssignIfExists(args, M::ZipFd, &zip_fd_);
AssignIfExists(args, M::ZipLocation, &zip_location_);
AssignIfExists(args, M::InputVdexFd, &input_vdex_fd_);
AssignIfExists(args, M::OutputVdexFd, &output_vdex_fd_);
AssignIfExists(args, M::InputVdex, &input_vdex_);
AssignIfExists(args, M::OutputVdex, &output_vdex_);
AssignIfExists(args, M::DmFd, &dm_fd_);
AssignIfExists(args, M::DmFile, &dm_file_location_);
AssignIfExists(args, M::OatFd, &oat_fd_);
AssignIfExists(args, M::OatLocation, &oat_location_);
AssignIfExists(args, M::Watchdog, &parser_options->watch_dog_enabled);
AssignIfExists(args, M::WatchdogTimeout, &parser_options->watch_dog_timeout_in_ms);
AssignIfExists(args, M::Threads, &thread_count_);
AssignIfExists(args, M::CpuSet, &cpu_set_);
AssignIfExists(args, M::Passes, &passes_to_run_filename_);
AssignIfExists(args, M::BootImage, &parser_options->boot_image_filename);
AssignIfExists(args, M::AndroidRoot, &android_root_);
AssignIfExists(args, M::Profile, &profile_file_);
AssignIfExists(args, M::ProfileFd, &profile_file_fd_);
AssignIfExists(args, M::RuntimeOptions, &runtime_args_);
AssignIfExists(args, M::SwapFile, &swap_file_name_);
AssignIfExists(args, M::SwapFileFd, &swap_fd_);
AssignIfExists(args, M::SwapDexSizeThreshold, &min_dex_file_cumulative_size_for_swap_);
AssignIfExists(args, M::SwapDexCountThreshold, &min_dex_files_for_swap_);
AssignIfExists(args, M::VeryLargeAppThreshold, &very_large_threshold_);
AssignIfExists(args, M::AppImageFile, &app_image_file_name_);
AssignIfExists(args, M::AppImageFileFd, &app_image_fd_);
AssignIfExists(args, M::NoInlineFrom, &no_inline_from_string_);
AssignIfExists(args, M::ClasspathDir, &classpath_dir_);
AssignIfExists(args, M::DirtyImageObjects, &dirty_image_objects_filename_);
AssignIfExists(args, M::UpdatableBcpPackagesFile, &updatable_bcp_packages_filename_);
AssignIfExists(args, M::ImageFormat, &image_storage_mode_);
AssignIfExists(args, M::CompilationReason, &compilation_reason_);
AssignIfExists(args, M::Backend, &compiler_kind_);
parser_options->requested_specific_compiler = args.Exists(M::Backend);
AssignIfExists(args, M::TargetInstructionSet, &compiler_options_->instruction_set_);
// arm actually means thumb2.
if (compiler_options_->instruction_set_ == InstructionSet::kArm) {
compiler_options_->instruction_set_ = InstructionSet::kThumb2;
AssignTrueIfExists(args, M::Host, &is_host_);
AssignTrueIfExists(args, M::AvoidStoringInvocation, &avoid_storing_invocation_);
if (args.Exists(M::InvocationFile)) {
if (invocation_file_.get() == -1) {
int err = errno;
Usage("Unable to open invocation file '%s' for writing due to %s.",
args.Get(M::InvocationFile)->c_str(), strerror(err));
AssignIfExists(args, M::CopyDexFiles, &copy_dex_files_);
AssignTrueIfExists(args, M::MultiImage, &have_multi_image_arg_);
AssignIfExists(args, M::MultiImage, &multi_image_);
if (args.Exists(M::ForceDeterminism)) {
force_determinism_ = true;
if (args.Exists(M::Base)) {
if (args.Exists(M::TargetInstructionSetVariant)) {
ParseInstructionSetVariant(*args.Get(M::TargetInstructionSetVariant), parser_options.get());
if (args.Exists(M::TargetInstructionSetFeatures)) {
ParseInstructionSetFeatures(*args.Get(M::TargetInstructionSetFeatures), parser_options.get());
if (args.Exists(M::ClassLoaderContext)) {
std::string class_loader_context_arg = *args.Get(M::ClassLoaderContext);
class_loader_context_ = ClassLoaderContext::Create(class_loader_context_arg);
if (class_loader_context_ == nullptr) {
Usage("Option --class-loader-context has an incorrect format: %s",
if (args.Exists(M::ClassLoaderContextFds)) {
std::string str_fds_arg = *args.Get(M::ClassLoaderContextFds);
std::vector<std::string> str_fds = android::base::Split(str_fds_arg, ":");
for (const std::string& str_fd : str_fds) {
class_loader_context_fds_.push_back(std::stoi(str_fd, nullptr, 0));
if (class_loader_context_fds_.back() < 0) {
Usage("Option --class-loader-context-fds has incorrect format: %s",
if (args.Exists(M::StoredClassLoaderContext)) {
const std::string stored_context_arg = *args.Get(M::StoredClassLoaderContext);
stored_class_loader_context_ = ClassLoaderContext::Create(stored_context_arg);
if (stored_class_loader_context_ == nullptr) {
Usage("Option --stored-class-loader-context has an incorrect format: %s",
} else if (class_loader_context_->VerifyClassLoaderContextMatch(
/*verify_names*/ false,
/*verify_checksums*/ false) != ClassLoaderContext::VerificationResult::kVerifies) {
"Option --stored-class-loader-context '%s' mismatches --class-loader-context '%s'",
} else if (args.Exists(M::StoredClassLoaderContext)) {
Usage("Option --stored-class-loader-context should only be used if "
"--class-loader-context is also specified");
// If we have a profile, change the default compiler filter to speed-profile
// before reading compiler options.
static_assert(CompilerFilter::kDefaultCompilerFilter == CompilerFilter::kSpeed);
DCHECK_EQ(compiler_options_->GetCompilerFilter(), CompilerFilter::kSpeed);
if (UseProfile()) {
if (!ReadCompilerOptions(args, compiler_options_.get(), &error_msg)) {
// Insert some compiler things.
InsertCompileOptions(argc, argv);
// Check whether the oat output files are writable, and open them for later. Also open a swap
// file, if a name is given.
bool OpenFile() {
// Prune non-existent dex files now so that we don't create empty oat files for multi-image.
// Expand oat and image filenames for boot image and boot image extension.
// This is mostly for multi-image but single-image also needs some processing.
if (IsBootImage() || IsBootImageExtension()) {
// OAT and VDEX file handling
if (oat_fd_ == -1) {
for (const std::string& oat_filename : oat_filenames_) {
std::unique_ptr<File> oat_file(OS::CreateEmptyFile(oat_filename.c_str()));
if (oat_file == nullptr) {
PLOG(ERROR) << "Failed to create oat file: " << oat_filename;
return false;
if (fchmod(oat_file->Fd(), 0644) != 0) {
PLOG(ERROR) << "Failed to make oat file world readable: " << oat_filename;
return false;
DCHECK_EQ(input_vdex_fd_, -1);
if (!input_vdex_.empty()) {
std::string error_msg;
input_vdex_file_ = VdexFile::Open(input_vdex_,
/* writable */ false,
/* low_4gb */ false,
DCHECK_EQ(output_vdex_fd_, -1);
std::string vdex_filename = output_vdex_.empty()
? ReplaceFileExtension(oat_filename, "vdex")
: output_vdex_;
if (vdex_filename == input_vdex_ && output_vdex_.empty()) {
update_input_vdex_ = true;
std::unique_ptr<File> vdex_file(OS::OpenFileReadWrite(vdex_filename.c_str()));
} else {
std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_filename.c_str()));
if (vdex_file == nullptr) {
PLOG(ERROR) << "Failed to open vdex file: " << vdex_filename;
return false;
if (fchmod(vdex_file->Fd(), 0644) != 0) {
PLOG(ERROR) << "Failed to make vdex file world readable: " << vdex_filename;
return false;
} else {
std::unique_ptr<File> oat_file(
new File(DupCloexec(oat_fd_), oat_location_, /* check_usage */ true));
if (!oat_file->IsOpened()) {
PLOG(ERROR) << "Failed to create oat file: " << oat_location_;
return false;
if (oat_file->SetLength(0) != 0) {
PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed.";
return false;
if (input_vdex_fd_ != -1) {
struct stat s;
int rc = TEMP_FAILURE_RETRY(fstat(input_vdex_fd_, &s));
if (rc == -1) {
PLOG(WARNING) << "Failed getting length of vdex file";
} else {
std::string error_msg;
input_vdex_file_ = VdexFile::Open(input_vdex_fd_,
/* writable */ false,
/* low_4gb */ false,
// If there's any problem with the passed vdex, just warn and proceed
// without it.
if (input_vdex_file_ == nullptr) {
PLOG(WARNING) << "Failed opening vdex file: " << error_msg;
DCHECK_NE(output_vdex_fd_, -1);
std::string vdex_location = ReplaceFileExtension(oat_location_, "vdex");
std::unique_ptr<File> vdex_file(new File(
DupCloexec(output_vdex_fd_), vdex_location, /* check_usage */ true));
if (!vdex_file->IsOpened()) {
PLOG(ERROR) << "Failed to create vdex file: " << vdex_location;
return false;
if (input_vdex_file_ != nullptr && output_vdex_fd_ == input_vdex_fd_) {
update_input_vdex_ = true;
} else {
if (vdex_file->SetLength(0) != 0) {
PLOG(ERROR) << "Truncating vdex file " << vdex_location << " failed.";
return false;
// If we're updating in place a vdex file, be defensive and put an invalid vdex magic in case
// dex2oat gets killed.
// Note: we're only invalidating the magic data in the file, as dex2oat needs the rest of
// the information to remain valid.
if (update_input_vdex_) {
File* vdex_file = vdex_files_.back().get();
if (!vdex_file->PwriteFully(&VdexFile::VerifierDepsHeader::kVdexInvalidMagic,
/*offset=*/ 0u)) {
PLOG(ERROR) << "Failed to invalidate vdex header. File: " << vdex_file->GetPath();
return false;
if (vdex_file->Flush() != 0) {
PLOG(ERROR) << "Failed to flush stream after invalidating header of vdex file."
<< " File: " << vdex_file->GetPath();
return false;
if (dm_fd_ != -1 || !dm_file_location_.empty()) {
std::string error_msg;
if (dm_fd_ != -1) {
dm_file_.reset(ZipArchive::OpenFromFd(dm_fd_, "DexMetadata", &error_msg));
} else {
dm_file_.reset(ZipArchive::Open(dm_file_location_.c_str(), &error_msg));
if (dm_file_ == nullptr) {
LOG(WARNING) << "Could not open DexMetadata archive " << error_msg;
if (dm_file_ != nullptr) {
DCHECK(input_vdex_file_ == nullptr);
std::string error_msg;
static const char* kDexMetadata = "DexMetadata";
std::unique_ptr<ZipEntry> zip_entry(dm_file_->Find(VdexFile::kVdexNameInDmFile, &error_msg));
if (zip_entry == nullptr) {
LOG(INFO) << "No " << VdexFile::kVdexNameInDmFile << " file in DexMetadata archive. "
<< "Not doing fast verification.";
} else {
MemMap input_file = zip_entry->MapDirectlyOrExtract(
if (!input_file.IsValid()) {
LOG(WARNING) << "Could not open vdex file in DexMetadata archive: " << error_msg;
} else {
input_vdex_file_ = std::make_unique<VdexFile>(std::move(input_file));
if (input_vdex_file_->HasDexSection()) {
LOG(ERROR) << "The dex metadata is not allowed to contain dex files";
return false;
VLOG(verifier) << "Doing fast verification with vdex from DexMetadata archive";
// Swap file handling
// If the swap fd is not -1, we assume this is the file descriptor of an open but unlinked file
// that we can use for swap.
// If the swap fd is -1 and we have a swap-file string, open the given file as a swap file. We
// will immediately unlink to satisfy the swap fd assumption.
if (swap_fd_ == -1 && !swap_file_name_.empty()) {
std::unique_ptr<File> swap_file(OS::CreateEmptyFile(swap_file_name_.c_str()));
if (swap_file.get() == nullptr) {
PLOG(ERROR) << "Failed to create swap file: " << swap_file_name_;
return false;
swap_fd_ = swap_file->Release();
return true;
void EraseOutputFiles() {
for (auto& files : { &vdex_files_, &oat_files_ }) {
for (size_t i = 0; i < files->size(); ++i) {
if ((*files)[i].get() != nullptr) {
void LoadClassProfileDescriptors() {
if (!IsImage()) {
if (profile_compilation_info_ != nullptr) {
// TODO: The following comment looks outdated or misplaced.
// Filter out class path classes since we don't want to include these in the image.
HashSet<std::string> image_classes = profile_compilation_info_->GetClassDescriptors(
VLOG(compiler) << "Loaded " << image_classes.size()
<< " image class descriptors from profile";
if (VLOG_IS_ON(compiler)) {
for (const std::string& s : image_classes) {
LOG(INFO) << "Image class " << s;
// Set up the environment for compilation. Includes starting the runtime and loading/opening the
// boot class path.
dex2oat::ReturnCode Setup() {
TimingLogger::ScopedTiming t("dex2oat Setup", timings_);
if (!PrepareDirtyObjects()) {
return dex2oat::ReturnCode::kOther;
// Verification results are null since we don't know if we will need them yet as the compiler
// filter may change.
callbacks_.reset(new QuickCompilerCallbacks(
// For class verification purposes, boot image extension is the same as boot image.
(IsBootImage() || IsBootImageExtension())
? CompilerCallbacks::CallbackMode::kCompileBootImage
: CompilerCallbacks::CallbackMode::kCompileApp));
RuntimeArgumentMap runtime_options;
if (!PrepareRuntimeOptions(&runtime_options, callbacks_.get())) {
return dex2oat::ReturnCode::kOther;
if (!AddDexFileSources()) {
return dex2oat::ReturnCode::kOther;
TimingLogger::ScopedTiming t_dex("Writing and opening dex files", timings_);
for (size_t i = 0, size = oat_writers_.size(); i != size; ++i) {
// Unzip or copy dex files straight to the oat file.
std::vector<MemMap> opened_dex_files_map;
std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
// No need to verify the dex file when we have a vdex file, which means it was already
// verified.
const bool verify =
(input_vdex_file_ == nullptr) && !compiler_options_->AssumeDexFilesAreVerified();
if (!oat_writers_[i]->WriteAndOpenDexFiles(
&opened_dex_files)) {
return dex2oat::ReturnCode::kOther;
if (opened_dex_files_map.empty()) {
} else {
for (MemMap& map : opened_dex_files_map) {
for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
dex_file_oat_index_map_.emplace(dex_file.get(), i);
compiler_options_->dex_files_for_oat_file_ = MakeNonOwningPointerVector(opened_dex_files_);
const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
// Check if we need to downgrade the compiler-filter for size reasons.
// Note: This does not affect the compiler filter already stored in the key-value
// store which is used for determining whether the oat file is up to date,
// together with the boot class path locations and checksums stored below.
CompilerFilter::Filter original_compiler_filter = compiler_options_->GetCompilerFilter();
if (!IsBootImage() && !IsBootImageExtension() && IsVeryLarge(dex_files)) {
// Disable app image to make sure dex2oat unloading is enabled.
compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
// If we need to downgrade the compiler-filter for size reasons, do that early before we read
// it below for creating verification callbacks.
if (!CompilerFilter::IsAsGoodAs(kLargeAppFilter, compiler_options_->GetCompilerFilter())) {
LOG(INFO) << "Very large app, downgrading to verify.";
if (CompilerFilter::IsAnyCompilationEnabled(compiler_options_->GetCompilerFilter()) ||
IsImage()) {
// Only modes with compilation or image generation require verification results.
// Do this here instead of when we
// create the compilation callbacks since the compilation mode may have been changed by the
// very large app logic.
// Avoiding setting the verification results saves RAM by not adding the dex files later in
// the function.
// Note: When compiling boot image, this must be done before creating the Runtime.
verification_results_.reset(new VerificationResults(compiler_options_.get()));
if (IsBootImage() || IsBootImageExtension()) {
// For boot image or boot image extension, pass opened dex files to the Runtime::Create().
// Note: Runtime acquires ownership of these dex files.
runtime_options.Set(RuntimeArgumentMap::BootClassPathDexList, &opened_dex_files_);
if (!CreateRuntime(std::move(runtime_options))) {
return dex2oat::ReturnCode::kCreateRuntime;
ArrayRef<const DexFile* const> bcp_dex_files(runtime_->GetClassLinker()->GetBootClassPath());
if (IsBootImage() || IsBootImageExtension()) {
// Check boot class path dex files and, if compiling an extension, the images it depends on.
if ((IsBootImage() && bcp_dex_files.size() != dex_files.size()) ||
(IsBootImageExtension() && bcp_dex_files.size() <= dex_files.size())) {
LOG(ERROR) << "Unexpected number of boot class path dex files for boot image or extension, "
<< bcp_dex_files.size() << (IsBootImage() ? " != " : " <= ") << dex_files.size();
return dex2oat::ReturnCode::kOther;
if (!std::equal(dex_files.begin(), dex_files.end(), bcp_dex_files.end() - dex_files.size())) {
LOG(ERROR) << "Boot class path dex files do not end with the compiled dex files.";
return dex2oat::ReturnCode::kOther;
size_t bcp_df_pos = 0u;
size_t bcp_df_end = bcp_dex_files.size();
for (const std::string& bcp_location : runtime_->GetBootClassPathLocations()) {
if (bcp_df_pos == bcp_df_end || bcp_dex_files[bcp_df_pos]->GetLocation() != bcp_location) {
LOG(ERROR) << "Missing dex file for boot class component " << bcp_location;
return dex2oat::ReturnCode::kOther;
while (bcp_df_pos != bcp_df_end &&
DexFileLoader::IsMultiDexLocation(bcp_dex_files[bcp_df_pos]->GetLocation().c_str())) {
if (bcp_df_pos != bcp_df_end) {
LOG(ERROR) << "Unexpected dex file in boot class path "
<< bcp_dex_files[bcp_df_pos]->GetLocation();
return dex2oat::ReturnCode::kOther;
auto lacks_image = [](const DexFile* df) {
if (kIsDebugBuild && df->GetOatDexFile() != nullptr) {
const OatFile* oat_file = df->GetOatDexFile()->GetOatFile();
CHECK(oat_file != nullptr);
const auto& image_spaces = Runtime::Current()->GetHeap()->GetBootImageSpaces();
[=](const ImageSpace* space) {
return oat_file == space->GetOatFile();
return df->GetOatDexFile() == nullptr;
if (std::any_of(bcp_dex_files.begin(), bcp_dex_files.end() - dex_files.size(), lacks_image)) {
LOG(ERROR) << "Missing required boot image(s) for boot image extension.";
return dex2oat::ReturnCode::kOther;
if (!compilation_reason_.empty()) {
key_value_store_->Put(OatHeader::kCompilationReasonKey, compilation_reason_);
if (IsBootImage()) {
// If we're compiling the boot image, store the boot classpath into the Key-Value store.
// We use this when loading the boot image.
key_value_store_->Put(OatHeader::kBootClassPathKey, android::base::Join(dex_locations_, ':'));
} else if (IsBootImageExtension()) {
// Validate the boot class path and record the dependency on the loaded boot images.
TimingLogger::ScopedTiming t3("Loading image checksum", timings_);
Runtime* runtime = Runtime::Current();
std::string full_bcp = android::base::Join(runtime->GetBootClassPathLocations(), ':');
std::string extension_part = ":" + android::base::Join(dex_locations_, ':');
if (!android::base::EndsWith(full_bcp, extension_part)) {
LOG(ERROR) << "Full boot class path does not end with extension parts, full: " << full_bcp
<< ", extension: " << extension_part.substr(1u);
return dex2oat::ReturnCode::kOther;
std::string bcp_dependency = full_bcp.substr(0u, full_bcp.size() - extension_part.size());
key_value_store_->Put(OatHeader::kBootClassPathKey, bcp_dependency);
ArrayRef<const DexFile* const> bcp_dex_files_dependency =
bcp_dex_files.SubArray(/*pos=*/ 0u, bcp_dex_files.size() - dex_files.size());
ArrayRef<ImageSpace* const> image_spaces(runtime->GetHeap()->GetBootImageSpaces());
gc::space::ImageSpace::GetBootClassPathChecksums(image_spaces, bcp_dex_files_dependency));
} else {
if (CompilerFilter::DependsOnImageChecksum(original_compiler_filter)) {
TimingLogger::ScopedTiming t3("Loading image checksum", timings_);
Runtime* runtime = Runtime::Current();
android::base::Join(runtime->GetBootClassPathLocations(), ':'));
ArrayRef<ImageSpace* const> image_spaces(runtime->GetHeap()->GetBootImageSpaces());
gc::space::ImageSpace::GetBootClassPathChecksums(image_spaces, bcp_dex_files));
// Open dex files for class path.
if (class_loader_context_ == nullptr) {
// If no context was specified use the default one (which is an empty PathClassLoader).
class_loader_context_ = ClassLoaderContext::Default();
DCHECK_EQ(oat_writers_.size(), 1u);
// Note: Ideally we would reject context where the source dex files are also
// specified in the classpath (as it doesn't make sense). However this is currently
// needed for non-prebuild tests and benchmarks which expects on the fly compilation.
// Also, for secondary dex files we do not have control on the actual classpath.
// Instead of aborting, remove all the source location from the context classpaths.
if (class_loader_context_->RemoveLocationsFromClassPaths(
oat_writers_[0]->GetSourceLocations())) {
LOG(WARNING) << "The source files to be compiled are also in the classpath.";
// We need to open the dex files before encoding the context in the oat file.
// (because the encoding adds the dex checksum...)
// TODO(calin): consider redesigning this so we don't have to open the dex files before
// creating the actual class loader.
if (!class_loader_context_->OpenDexFiles(runtime_->GetInstructionSet(),
class_loader_context_fds_)) {
// Do not abort if we couldn't open files from the classpath. They might be
// apks without dex files and right now are opening flow will fail them.
LOG(WARNING) << "Failed to open classpath dex files";
// Store the class loader context in the oat header.
// TODO: deprecate this since store_class_loader_context should be enough to cover the users
// of classpath_dir as well.
std::string class_path_key =
key_value_store_->Put(OatHeader::kClassPathKey, class_path_key);
// Prepare exclusion list for updatable boot class path packages.
if (!PrepareUpdatableBcpPackages()) {
return dex2oat::ReturnCode::kOther;
// Now that we have finalized key_value_store_, start writing the .rodata section.
// Among other things, this creates type lookup tables that speed up the compilation.
TimingLogger::ScopedTiming t_dex("Starting .rodata", timings_);
for (size_t i = 0, size = oat_writers_.size(); i != size; ++i) {
if (!oat_writers_[i]->StartRoData(dex_files_per_oat_file_[i],
(i == 0u) ? key_value_store_.get() : nullptr)) {
return dex2oat::ReturnCode::kOther;
// We had to postpone the swap decision till now, as this is the point when we actually
// know about the dex files we're going to use.
// Make sure that we didn't create the driver, yet.
CHECK(driver_ == nullptr);
// If we use a swap file, ensure we are above the threshold to make it necessary.
if (swap_fd_ != -1) {
if (!UseSwap(IsBootImage() || IsBootImageExtension(), dex_files)) {
swap_fd_ = -1;
VLOG(compiler) << "Decided to run without swap.";
} else {
LOG(INFO) << "Large app, accepted running with swap.";
// Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that.
// If we're doing the image, override the compiler filter to force full compilation. Must be
// done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force
// compilation of class initializers.
// Whilst we're in native take the opportunity to initialize well known classes.
Thread* self = Thread::Current();
if (!IsBootImage() && !IsBootImageExtension()) {
constexpr bool kSaveDexInput = false;
if (kSaveDexInput) {
// Ensure opened dex files are writable for dex-to-dex transformations.
for (MemMap& map : opened_dex_files_maps_) {
if (!map.Protect(PROT_READ | PROT_WRITE)) {
PLOG(ERROR) << "Failed to make .dex files writeable.";
return dex2oat::ReturnCode::kOther;
// Verification results are only required for modes that have any compilation. Avoid
// adding the dex files if possible to prevent allocating large arrays.
if (verification_results_ != nullptr) {
for (const auto& dex_file : dex_files) {
// Pre-register dex files so that we can access verification results without locks during
// compilation and verification.
return dex2oat::ReturnCode::kNoFailure;
// If we need to keep the oat file open for the image writer.
bool ShouldKeepOatFileOpen() const {
return IsImage() && oat_fd_ != kInvalidFd;
// Doesn't return the class loader since it's not meant to be used for image compilation.
void CompileDexFilesIndividually() {
CHECK(!IsImage()) << "Not supported with image";
for (const DexFile* dex_file : compiler_options_->dex_files_for_oat_file_) {
std::vector<const DexFile*> dex_files(1u, dex_file);
VLOG(compiler) << "Compiling " << dex_file->GetLocation();
jobject class_loader = CompileDexFiles(dex_files);
CHECK(class_loader != nullptr);
ScopedObjectAccess soa(Thread::Current());
// Unload class loader to free RAM.
jweak weak_class_loader = soa.Env()->GetVm()->AddWeakGlobalRef(
soa.Env()->GetVm()->DeleteGlobalRef(soa.Self(), class_loader);
runtime_->GetHeap()->CollectGarbage(/* clear_soft_references */ true);
ObjPtr<mirror::ClassLoader> decoded_weak = soa.Decode<mirror::ClassLoader>(weak_class_loader);
if (decoded_weak != nullptr) {
LOG(FATAL) << "Failed to unload class loader, path from root set: "
<< runtime_->GetHeap()->GetVerification()->FirstPathFromRootSet(decoded_weak);
VLOG(compiler) << "Unloaded classloader";
bool ShouldCompileDexFilesIndividually() const {
// Compile individually if we are:
// 1. not building an image,
// 2. not verifying a vdex file,
// 3. using multidex,
// 4. not doing any AOT compilation.
// This means extract, no-vdex verify, and quicken, will use the individual compilation
// mode (to reduce RAM used by the compiler).
return !IsImage() &&
!update_input_vdex_ &&
compiler_options_->dex_files_for_oat_file_.size() > 1 &&
uint32_t GetCombinedChecksums() const {
uint32_t combined_checksums = 0u;
for (const DexFile* dex_file : compiler_options_->GetDexFilesForOatFile()) {
combined_checksums ^= dex_file->GetLocationChecksum();
return combined_checksums;
// Set up and create the compiler driver and then invoke it to compile all the dex files.
jobject Compile() {
ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
TimingLogger::ScopedTiming t("dex2oat Compile", timings_);
// Find the dex files we should not inline from.
std::vector<std::string> no_inline_filters;
Split(no_inline_from_string_, ',', &no_inline_filters);
// For now, on the host always have core-oj removed.
const std::string core_oj = "core-oj";
if (!kIsTargetBuild && !ContainsElement(no_inline_filters, core_oj)) {
if (!no_inline_filters.empty()) {
std::vector<const DexFile*> class_path_files;
if (!IsBootImage() && !IsBootImageExtension()) {
// The class loader context is used only for apps.
class_path_files = class_loader_context_->FlattenOpenedDexFiles();
const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
std::vector<const DexFile*> no_inline_from_dex_files;
const std::vector<const DexFile*>* dex_file_vectors[] = {
for (const std::vector<const DexFile*>* dex_file_vector : dex_file_vectors) {
for (const DexFile* dex_file : *dex_file_vector) {
for (const std::string& filter : no_inline_filters) {
// Use dex_file->GetLocation() rather than dex_file->GetBaseLocation(). This
// allows tests to specify <test-dexfile>!classes2.dex if needed but if the
// base location passes the StartsWith() test, so do all extra locations.
std::string dex_location = dex_file->GetLocation();
if (filter.find('/') == std::string::npos) {
// The filter does not contain the path. Remove the path from dex_location as well.
size_t last_slash = dex_file->GetLocation().rfind('/');
if (last_slash != std::string::npos) {
dex_location = dex_location.substr(last_slash + 1);
if (android::base::StartsWith(dex_location, filter.c_str())) {
VLOG(compiler) << "Disabling inlining from " << dex_file->GetLocation();
if (!no_inline_from_dex_files.empty()) {
compiler_options_->profile_compilation_info_ = profile_compilation_info_.get();
driver_.reset(new CompilerDriver(compiler_options_.get(),
if (!IsBootImage() && !IsBootImageExtension()) {
const bool compile_individually = ShouldCompileDexFilesIndividually();
if (compile_individually) {
// Set the compiler driver in the callbacks so that we can avoid re-verification. This not
// only helps performance but also prevents reverifying quickened bytecodes. Attempting
// verify quickened bytecode causes verification failures.
// Only set the compiler filter if we are doing separate compilation since there is a bit
// of overhead when checking if a class was previously verified.
callbacks_->SetDoesClassUnloading(true, driver_.get());
// Setup vdex for compilation.
const std::vector<const DexFile*>& dex_files = compiler_options_->dex_files_for_oat_file_;
if (!DoEagerUnquickeningOfVdex() && input_vdex_file_ != nullptr) {
new verifier::VerifierDeps(dex_files, input_vdex_file_->GetVerifierDepsData()));
// TODO: we unquicken unconditionally, as we don't know
// if the boot image has changed. How exactly we'll know is under
// experimentation.
TimingLogger::ScopedTiming time_unquicken("Unquicken", timings_);
// We do not decompile a RETURN_VOID_NO_BARRIER into a RETURN_VOID, as the quickening
// optimization does not depend on the boot image (the optimization relies on not
// having final fields in a class, which does not change for an app).
input_vdex_file_->Unquicken(dex_files, /* decompile_return_instruction */ false);
} else {
// Create the main VerifierDeps, here instead of in the compiler since we want to aggregate
// the results for all the dex files, not just the results for the current dex file.
callbacks_->SetVerifierDeps(new verifier::VerifierDeps(dex_files));
// To allow initialization of classes that construct ThreadLocal objects in class initializer,
// re-initialize the ThreadLocal.nextHashCode to a new object that's not in the boot image.
ThreadLocalHashOverride thread_local_hash_override(
/*apply=*/ !IsBootImage(), /*initial_value=*/ 123456789u ^ GetCombinedChecksums());
// Invoke the compilation.
if (compile_individually) {
// Return a null classloader since we already freed released it.
return nullptr;
return CompileDexFiles(dex_files);
// Create the class loader, use it to compile, and return.
jobject CompileDexFiles(const std::vector<const DexFile*>& dex_files) {
ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
jobject class_loader = nullptr;
if (!IsBootImage() && !IsBootImageExtension()) {
class_loader =
if (!IsBootImage()) {
// Register dex caches and key them to the class loader so that they only unload when the
// class loader unloads.
for (const auto& dex_file : dex_files) {
ScopedObjectAccess soa(Thread::Current());
// Registering the dex cache adds a strong root in the class loader that prevents the dex
// cache from being unloaded early.
ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
if (dex_cache == nullptr) {
LOG(FATAL) << "Failed to register dex file " << dex_file->GetLocation() << " "
<< soa.Self()->GetException()->Dump();
callbacks_->SetVerificationResults(nullptr); // Should not be needed anymore.
compiler_options_->verification_results_ = verification_results_.get();
driver_->CompileAll(class_loader, dex_files, timings_);
return class_loader;
// Notes on the interleaving of creating the images and oat files to
// ensure the references between the two are correct.
// Currently we have a memory layout that looks something like this:
// +--------------+
// | images |
// +--------------+
// | oat files |
// +--------------+
// | alloc spaces |
// +--------------+
// There are several constraints on the loading of the images and oat files.
// 1. The images are expected to be loaded at an absolute address and
// contain Objects with absolute pointers within the images.
// 2. There are absolute pointers from Methods in the images to their
// code in the oat files.
// 3. There are absolute pointers from the code in the oat files to Methods
// in the images.
// 4. There are absolute pointers from code in the oat files to other code
// in the oat files.
// To get this all correct, we go through several steps.
// 1. We prepare offsets for all data in the oat files and calculate
// the oat data size and code size. During this stage, we also set
// oat code offsets in methods for use by the image writer.
// 2. We prepare offsets for the objects in the images and calculate
// the image sizes.
// 3. We create the oat files. Originally this was just our own proprietary
// file but now it is contained within an ELF dynamic object (aka an .so
// file). Since we know the image sizes and oat data sizes and code sizes we
// can prepare the ELF headers and we then know the ELF memory segment
// layout and we can now resolve all references. The compiler provides
// LinkerPatch information in each CompiledMethod and we resolve these,
// using the layout information and image object locations provided by
// image writer, as we're writing the method code.
// 4. We create the image files. They need to know where the oat files
// will be loaded after itself. Originally oat files were simply
// memory mapped so we could predict where their contents were based
// on the file size. Now that they are ELF files, we need to inspect
// the ELF files to understand the in memory segment layout including
// where the oat header is located within.
// TODO: We could just remember this information from step 3.
// 5. We fixup the ELF program headers so that dlopen will try to
// load the .so at the desired location at runtime by offsetting the
// Elf32_Phdr.p_vaddr values by the desired base address.
// TODO: Do this in step 3. We already know the layout there.
// Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5.
// are done by the CreateImageFile() below.
// Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the
// ImageWriter, if necessary.
// Note: Flushing (and closing) the file is the caller's responsibility, except for the failure
// case (when the file will be explicitly erased).
bool WriteOutputFiles(jobject class_loader) {
TimingLogger::ScopedTiming t("dex2oat Oat", timings_);
// Sync the data to the file, in case we did dex2dex transformations.
for (MemMap& map : opened_dex_files_maps_) {
if (!map.Sync()) {
PLOG(ERROR) << "Failed to Sync() dex2dex output. Map: " << map.GetName();
return false;
if (IsImage()) {
if (!IsBootImage()) {
DCHECK_EQ(image_base_, 0u);
gc::Heap* const heap = Runtime::Current()->GetHeap();
image_base_ = heap->GetBootImagesStartAddress() + heap->GetBootImagesSize();
VLOG(compiler) << "Image base=" << reinterpret_cast<void*>(image_base_);
image_writer_.reset(new linker::ImageWriter(*compiler_options_,
// We need to prepare method offsets in the image address space for resolving linker patches.
TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_);
// Do not preload dex caches for "assume-verified". This filter is used for in-memory
// compilation of boot image extension; in that scenario it is undesirable to use a lot
// of time to look up things now in hope it will be somewhat useful later.
bool preload_dex_caches = !compiler_options_->AssumeDexFilesAreVerified();
if (!image_writer_->PrepareImageAddressSpace(preload_dex_caches, timings_)) {
LOG(ERROR) << "Failed to prepare image address space.";
return false;
// Initialize the writers with the compiler driver, image writer, and their
// dex files. The writers were created without those being there yet.
for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
std::vector<const DexFile*>& dex_files = dex_files_per_oat_file_[i];
oat_writer->Initialize(driver_.get(), image_writer_.get(), dex_files);
TimingLogger::ScopedTiming t2("dex2oat Write VDEX", timings_);
DCHECK(IsBootImage() || IsBootImageExtension() || oat_files_.size() == 1u);
verifier::VerifierDeps* verifier_deps = callbacks_->GetVerifierDeps();
for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
File* vdex_file = vdex_files_[i].get();
std::unique_ptr<BufferedOutputStream> vdex_out =
if (!oat_writers_[i]->WriteVerifierDeps(vdex_out.get(), verifier_deps)) {
LOG(ERROR) << "Failed to write verifier dependencies into VDEX " << vdex_file->GetPath();
return false;
if (!oat_writers_[i]->WriteQuickeningInfo(vdex_out.get())) {
LOG(ERROR) << "Failed to write quickening info into VDEX " << vdex_file->GetPath();
return false;
// VDEX finalized, seek back to the beginning and write checksums and the header.
if (!oat_writers_[i]->WriteChecksumsAndVdexHeader(vdex_out.get())) {
LOG(ERROR) << "Failed to write vdex header into VDEX " << vdex_file->GetPath();
return false;
TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_);
linker::MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i];
std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
if (IsImage()) {
// Update oat layout.
DCHECK(image_writer_ != nullptr);
DCHECK_LT(i, oat_filenames_.size());
for (size_t i = 0, size = oat_files_.size(); i != size; ++i) {
std::unique_ptr<File>& oat_file = oat_files_[i];
std::unique_ptr<linker::ElfWriter>& elf_writer = elf_writers_[i];
std::unique_ptr<linker::OatWriter>& oat_writer = oat_writers_[i];
// We need to mirror the layout of the ELF file in the compressed debug-info.
// Therefore PrepareDebugInfo() relies on the SetLoadedSectionSizes() call further above.
debug::DebugInfo debug_info = oat_writer->GetDebugInfo(); // Keep the variable alive.
elf_writer->PrepareDebugInfo(debug_info); // Processes the data on background thread.
OutputStream* rodata = rodata_[i];
DCHECK(rodata != nullptr);
if (!oat_writer->WriteRodata(rodata)) {
LOG(ERROR) << "Failed to write .rodata section to the ELF file " << oat_file->GetPath();
return false;
rodata = nullptr;
OutputStream* text = elf_writer->StartText();
if (!oat_writer->WriteCode(text)) {
LOG(ERROR) << "Failed to write .text section to the ELF file " << oat_file->GetPath();
return false;
if (oat_writer->GetDataBimgRelRoSize() != 0u) {
OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
if (!oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro)) {
LOG(ERROR) << "Failed to write section to the ELF file "
<< oat_file->GetPath();
return false;
if (!oat_writer->WriteHeader(elf_writer->GetStream())) {
LOG(ERROR) << "Failed to write oat header to the ELF file " << oat_file->GetPath();
return false;
if (IsImage()) {
// Update oat header information.
DCHECK(image_writer_ != nullptr);
DCHECK_LT(i, oat_filenames_.size());
image_writer_->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
if (!elf_writer->End()) {
LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
return false;
if (!FlushOutputFile(&vdex_files_[i]) || !FlushOutputFile(&oat_files_[i])) {
return false;
VLOG(compiler) << "Oat file written successfully: " << oat_filenames_[i];
// We may still need the ELF writer later for stripping.
return true;
// If we are compiling an image, invoke the image creation routine. Else just skip.
bool HandleImage() {
if (IsImage()) {
TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_);
if (!CreateImageFile()) {
return false;
VLOG(compiler) << "Images written successfully";
return true;
// Copy the full oat files to symbols directory and then strip the originals.
bool CopyOatFilesToSymbolsDirectoryAndStrip() {
for (size_t i = 0; i < oat_unstripped_.size(); ++i) {
// If we don't want to strip in place, copy from stripped location to unstripped location.
// We need to strip after image creation because FixupElf needs to use .strtab.
if (oat_unstripped_[i] != oat_filenames_[i]) {
DCHECK(oat_files_[i].get() != nullptr && oat_files_[i]->IsOpened());
TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_);
std::unique_ptr<File>& in = oat_files_[i];
std::unique_ptr<File> out(OS::CreateEmptyFile(oat_unstripped_[i].c_str()));
int64_t in_length = in->GetLength();
if (in_length < 0) {
PLOG(ERROR) << "Failed to get the length of oat file: " << in->GetPath();
return false;
if (!out->Copy(in.get(), 0, in_length)) {
PLOG(ERROR) << "Failed to copy oat file to file: " << out->GetPath();
return false;
if (out->FlushCloseOrErase() != 0) {
PLOG(ERROR) << "Failed to flush and close copied oat file: " << oat_unstripped_[i];
return false;
VLOG(compiler) << "Oat file copied successfully (unstripped): " << oat_unstripped_[i];
if (strip_) {
TimingLogger::ScopedTiming t2("dex2oat OatFile strip", timings_);
if (!elf_writers_[i]->StripDebugInfo()) {
PLOG(ERROR) << "Failed strip oat file: " << in->GetPath();
return false;
return true;