dex2oat.cc revision 9433ec60b325b708b9fa87e699ab4a6565741494
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 <stdio.h>
18#include <stdlib.h>
19#include <sys/stat.h>
20#include <valgrind.h>
21
22#include <fstream>
23#include <iostream>
24#include <sstream>
25#include <string>
26#include <vector>
27
28#if defined(__linux__) && defined(__arm__)
29#include <sys/personality.h>
30#include <sys/utsname.h>
31#endif
32
33#include "base/stl_util.h"
34#include "base/stringpiece.h"
35#include "base/timing_logger.h"
36#include "base/unix_file/fd_file.h"
37#include "class_linker.h"
38#include "compiler.h"
39#include "compiler_callbacks.h"
40#include "dex_file-inl.h"
41#include "dex/pass_driver_me_opts.h"
42#include "dex/verification_results.h"
43#include "dex/quick_compiler_callbacks.h"
44#include "dex/quick/dex_file_to_method_inliner_map.h"
45#include "driver/compiler_driver.h"
46#include "driver/compiler_options.h"
47#include "elf_fixup.h"
48#include "elf_patcher.h"
49#include "elf_stripper.h"
50#include "gc/space/image_space.h"
51#include "gc/space/space-inl.h"
52#include "image_writer.h"
53#include "leb128.h"
54#include "mirror/art_method-inl.h"
55#include "mirror/class-inl.h"
56#include "mirror/class_loader.h"
57#include "mirror/object-inl.h"
58#include "mirror/object_array-inl.h"
59#include "oat_writer.h"
60#include "os.h"
61#include "runtime.h"
62#include "ScopedLocalRef.h"
63#include "scoped_thread_state_change.h"
64#include "utils.h"
65#include "vector_output_stream.h"
66#include "well_known_classes.h"
67#include "zip_archive.h"
68
69namespace art {
70
71static int original_argc;
72static char** original_argv;
73
74static std::string CommandLine() {
75  std::vector<std::string> command;
76  for (int i = 0; i < original_argc; ++i) {
77    command.push_back(original_argv[i]);
78  }
79  return Join(command, ' ');
80}
81
82static void UsageErrorV(const char* fmt, va_list ap) {
83  std::string error;
84  StringAppendV(&error, fmt, ap);
85  LOG(ERROR) << error;
86}
87
88static void UsageError(const char* fmt, ...) {
89  va_list ap;
90  va_start(ap, fmt);
91  UsageErrorV(fmt, ap);
92  va_end(ap);
93}
94
95static void Usage(const char* fmt, ...) {
96  va_list ap;
97  va_start(ap, fmt);
98  UsageErrorV(fmt, ap);
99  va_end(ap);
100
101  UsageError("Command: %s", CommandLine().c_str());
102
103  UsageError("Usage: dex2oat [options]...");
104  UsageError("");
105  UsageError("  --dex-file=<dex-file>: specifies a .dex file to compile.");
106  UsageError("      Example: --dex-file=/system/framework/core.jar");
107  UsageError("");
108  UsageError("  --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
109  UsageError("      containing a classes.dex file to compile.");
110  UsageError("      Example: --zip-fd=5");
111  UsageError("");
112  UsageError("  --zip-location=<zip-location>: specifies a symbolic name for the file");
113  UsageError("      corresponding to the file descriptor specified by --zip-fd.");
114  UsageError("      Example: --zip-location=/system/app/Calculator.apk");
115  UsageError("");
116  UsageError("  --oat-file=<file.oat>: specifies the oat output destination via a filename.");
117  UsageError("      Example: --oat-file=/system/framework/boot.oat");
118  UsageError("");
119  UsageError("  --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
120  UsageError("      Example: --oat-fd=6");
121  UsageError("");
122  UsageError("  --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
123  UsageError("      to the file descriptor specified by --oat-fd.");
124  UsageError("      Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
125  UsageError("");
126  UsageError("  --oat-symbols=<file.oat>: specifies the oat output destination with full symbols.");
127  UsageError("      Example: --oat-symbols=/symbols/system/framework/boot.oat");
128  UsageError("");
129  UsageError("  --bitcode=<file.bc>: specifies the optional bitcode filename.");
130  UsageError("      Example: --bitcode=/system/framework/boot.bc");
131  UsageError("");
132  UsageError("  --image=<file.art>: specifies the output image filename.");
133  UsageError("      Example: --image=/system/framework/boot.art");
134  UsageError("");
135  UsageError("  --image-classes=<classname-file>: specifies classes to include in an image.");
136  UsageError("      Example: --image=frameworks/base/preloaded-classes");
137  UsageError("");
138  UsageError("  --base=<hex-address>: specifies the base address when creating a boot image.");
139  UsageError("      Example: --base=0x50000000");
140  UsageError("");
141  UsageError("  --boot-image=<file.art>: provide the image file for the boot class path.");
142  UsageError("      Example: --boot-image=/system/framework/boot.art");
143  UsageError("      Default: $ANDROID_ROOT/system/framework/boot.art");
144  UsageError("");
145  UsageError("  --android-root=<path>: used to locate libraries for portable linking.");
146  UsageError("      Example: --android-root=out/host/linux-x86");
147  UsageError("      Default: $ANDROID_ROOT");
148  UsageError("");
149  UsageError("  --instruction-set=(arm|arm64|mips|x86|x86_64): compile for a particular");
150  UsageError("      instruction set.");
151  UsageError("      Example: --instruction-set=x86");
152  UsageError("      Default: arm");
153  UsageError("");
154  UsageError("  --instruction-set-features=...,: Specify instruction set features");
155  UsageError("      Example: --instruction-set-features=div");
156  UsageError("      Default: default");
157  UsageError("");
158  UsageError("  --compile-pic: Force indirect use of code, methods, and classes");
159  UsageError("      Default: disabled");
160  UsageError("");
161  UsageError("  --compiler-backend=(Quick|Optimizing|Portable): select compiler backend");
162  UsageError("      set.");
163  UsageError("      Example: --compiler-backend=Portable");
164  UsageError("      Default: Quick");
165  UsageError("");
166  UsageError("  --compiler-filter=(verify-none|interpret-only|space|balanced|speed|everything):");
167  UsageError("      select compiler filter.");
168  UsageError("      Example: --compiler-filter=everything");
169#if ART_SMALL_MODE
170  UsageError("      Default: interpret-only");
171#else
172  UsageError("      Default: speed");
173#endif
174  UsageError("");
175  UsageError("  --huge-method-max=<method-instruction-count>: the threshold size for a huge");
176  UsageError("      method for compiler filter tuning.");
177  UsageError("      Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold);
178  UsageError("      Default: %d", CompilerOptions::kDefaultHugeMethodThreshold);
179  UsageError("");
180  UsageError("  --huge-method-max=<method-instruction-count>: threshold size for a huge");
181  UsageError("      method for compiler filter tuning.");
182  UsageError("      Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold);
183  UsageError("      Default: %d", CompilerOptions::kDefaultHugeMethodThreshold);
184  UsageError("");
185  UsageError("  --large-method-max=<method-instruction-count>: threshold size for a large");
186  UsageError("      method for compiler filter tuning.");
187  UsageError("      Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold);
188  UsageError("      Default: %d", CompilerOptions::kDefaultLargeMethodThreshold);
189  UsageError("");
190  UsageError("  --small-method-max=<method-instruction-count>: threshold size for a small");
191  UsageError("      method for compiler filter tuning.");
192  UsageError("      Example: --small-method-max=%d", CompilerOptions::kDefaultSmallMethodThreshold);
193  UsageError("      Default: %d", CompilerOptions::kDefaultSmallMethodThreshold);
194  UsageError("");
195  UsageError("  --tiny-method-max=<method-instruction-count>: threshold size for a tiny");
196  UsageError("      method for compiler filter tuning.");
197  UsageError("      Example: --tiny-method-max=%d", CompilerOptions::kDefaultTinyMethodThreshold);
198  UsageError("      Default: %d", CompilerOptions::kDefaultTinyMethodThreshold);
199  UsageError("");
200  UsageError("  --num-dex-methods=<method-count>: threshold size for a small dex file for");
201  UsageError("      compiler filter tuning. If the input has fewer than this many methods");
202  UsageError("      and the filter is not interpret-only or verify-none, overrides the");
203  UsageError("      filter to use speed");
204  UsageError("      Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold);
205  UsageError("      Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold);
206  UsageError("");
207  UsageError("  --host: used with Portable backend to link against host runtime libraries");
208  UsageError("");
209  UsageError("  --dump-timing: display a breakdown of where time was spent");
210  UsageError("");
211  UsageError("  --include-patch-information: Include patching information so the generated code");
212  UsageError("      can have its base address moved without full recompilation.");
213  UsageError("");
214  UsageError("  --no-include-patch-information: Do not include patching information.");
215  UsageError("");
216  UsageError("  --include-debug-symbols: Include ELF symbols in this oat file");
217  UsageError("");
218  UsageError("  --no-include-debug-symbols: Do not include ELF symbols in this oat file");
219  UsageError("");
220  UsageError("  --runtime-arg <argument>: used to specify various arguments for the runtime,");
221  UsageError("      such as initial heap size, maximum heap size, and verbose output.");
222  UsageError("      Use a separate --runtime-arg switch for each argument.");
223  UsageError("      Example: --runtime-arg -Xms256m");
224  UsageError("");
225  UsageError("  --profile-file=<filename>: specify profiler output file to use for compilation.");
226  UsageError("");
227  UsageError("  --print-pass-names: print a list of pass names");
228  UsageError("");
229  UsageError("  --disable-passes=<pass-names>:  disable one or more passes separated by comma.");
230  UsageError("      Example: --disable-passes=UseCount,BBOptimizations");
231  UsageError("");
232  std::cerr << "See log for usage error information\n";
233  exit(EXIT_FAILURE);
234}
235
236class Dex2Oat {
237 public:
238  static bool Create(Dex2Oat** p_dex2oat,
239                     const RuntimeOptions& runtime_options,
240                     const CompilerOptions& compiler_options,
241                     Compiler::Kind compiler_kind,
242                     InstructionSet instruction_set,
243                     InstructionSetFeatures instruction_set_features,
244                     VerificationResults* verification_results,
245                     DexFileToMethodInlinerMap* method_inliner_map,
246                     size_t thread_count)
247      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
248    CHECK(verification_results != nullptr);
249    CHECK(method_inliner_map != nullptr);
250    std::unique_ptr<Dex2Oat> dex2oat(new Dex2Oat(&compiler_options,
251                                           compiler_kind,
252                                           instruction_set,
253                                           instruction_set_features,
254                                           verification_results,
255                                           method_inliner_map,
256                                           thread_count));
257    if (!dex2oat->CreateRuntime(runtime_options, instruction_set)) {
258      *p_dex2oat = nullptr;
259      return false;
260    }
261    *p_dex2oat = dex2oat.release();
262    return true;
263  }
264
265  ~Dex2Oat() {
266    delete runtime_;
267    LogCompletionTime();
268  }
269
270  void LogCompletionTime() {
271    LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_)
272              << " (threads: " << thread_count_ << ")";
273  }
274
275
276  // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
277  std::set<std::string>* ReadImageClassesFromFile(const char* image_classes_filename) {
278    std::unique_ptr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename,
279                                                                  std::ifstream::in));
280    if (image_classes_file.get() == nullptr) {
281      LOG(ERROR) << "Failed to open image classes file " << image_classes_filename;
282      return nullptr;
283    }
284    std::unique_ptr<std::set<std::string>> result(ReadImageClasses(*image_classes_file));
285    image_classes_file->close();
286    return result.release();
287  }
288
289  std::set<std::string>* ReadImageClasses(std::istream& image_classes_stream) {
290    std::unique_ptr<std::set<std::string>> image_classes(new std::set<std::string>);
291    while (image_classes_stream.good()) {
292      std::string dot;
293      std::getline(image_classes_stream, dot);
294      if (StartsWith(dot, "#") || dot.empty()) {
295        continue;
296      }
297      std::string descriptor(DotToDescriptor(dot.c_str()));
298      image_classes->insert(descriptor);
299    }
300    return image_classes.release();
301  }
302
303  // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
304  std::set<std::string>* ReadImageClassesFromZip(const char* zip_filename,
305                                                         const char* image_classes_filename,
306                                                         std::string* error_msg) {
307    std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg));
308    if (zip_archive.get() == nullptr) {
309      return nullptr;
310    }
311    std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename, error_msg));
312    if (zip_entry.get() == nullptr) {
313      *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", image_classes_filename,
314                                zip_filename, error_msg->c_str());
315      return nullptr;
316    }
317    std::unique_ptr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(zip_filename,
318                                                                          image_classes_filename,
319                                                                          error_msg));
320    if (image_classes_file.get() == nullptr) {
321      *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", image_classes_filename,
322                                zip_filename, error_msg->c_str());
323      return nullptr;
324    }
325    const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()),
326                                           image_classes_file->Size());
327    std::istringstream image_classes_stream(image_classes_string);
328    return ReadImageClasses(image_classes_stream);
329  }
330
331  bool PatchOatCode(const CompilerDriver* compiler_driver, File* oat_file,
332                    const std::string& oat_location, std::string* error_msg) {
333    // We asked to include patch information but we are not making an image. We need to fix
334    // everything up manually.
335    std::unique_ptr<ElfFile> elf_file(ElfFile::Open(oat_file, PROT_READ|PROT_WRITE,
336                                                    MAP_SHARED, error_msg));
337    if (elf_file.get() == NULL) {
338      LOG(ERROR) << error_msg;
339      return false;
340    }
341    {
342      ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
343      return ElfPatcher::Patch(compiler_driver, elf_file.get(), oat_location, error_msg);
344    }
345  }
346
347  const CompilerDriver* CreateOatFile(const std::string& boot_image_option,
348                                      const std::string& android_root,
349                                      bool is_host,
350                                      const std::vector<const DexFile*>& dex_files,
351                                      File* oat_file,
352                                      const std::string& oat_location,
353                                      const std::string& bitcode_filename,
354                                      bool image,
355                                      std::unique_ptr<std::set<std::string>>& image_classes,
356                                      std::unique_ptr<std::set<std::string>>& compiled_classes,
357                                      bool dump_stats,
358                                      bool dump_passes,
359                                      TimingLogger& timings,
360                                      CumulativeLogger& compiler_phases_timings,
361                                      std::string profile_file,
362                                      SafeMap<std::string, std::string>* key_value_store) {
363    CHECK(key_value_store != nullptr);
364
365    // Handle and ClassLoader creation needs to come after Runtime::Create
366    jobject class_loader = nullptr;
367    Thread* self = Thread::Current();
368    if (!boot_image_option.empty()) {
369      ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
370      std::vector<const DexFile*> class_path_files(dex_files);
371      OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files);
372      ScopedObjectAccess soa(self);
373      for (size_t i = 0; i < class_path_files.size(); i++) {
374        class_linker->RegisterDexFile(*class_path_files[i]);
375      }
376      soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
377      ScopedLocalRef<jobject> class_loader_local(soa.Env(),
378          soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
379      class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
380      Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files);
381    }
382
383    std::unique_ptr<CompilerDriver> driver(new CompilerDriver(compiler_options_,
384                                                              verification_results_,
385                                                              method_inliner_map_,
386                                                              compiler_kind_,
387                                                              instruction_set_,
388                                                              instruction_set_features_,
389                                                              image,
390                                                              image_classes.release(),
391                                                              compiled_classes.release(),
392                                                              thread_count_,
393                                                              dump_stats,
394                                                              dump_passes,
395                                                              &compiler_phases_timings,
396                                                              profile_file));
397
398    driver->GetCompiler()->SetBitcodeFileName(*driver.get(), bitcode_filename);
399
400    driver->CompileAll(class_loader, dex_files, &timings);
401
402    TimingLogger::ScopedTiming t2("dex2oat OatWriter", &timings);
403    std::string image_file_location;
404    uint32_t image_file_location_oat_checksum = 0;
405    uintptr_t image_file_location_oat_data_begin = 0;
406    int32_t image_patch_delta = 0;
407    if (!driver->IsImage()) {
408      TimingLogger::ScopedTiming t3("Loading image checksum", &timings);
409      gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
410      image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
411      image_file_location_oat_data_begin =
412          reinterpret_cast<uintptr_t>(image_space->GetImageHeader().GetOatDataBegin());
413      image_file_location = image_space->GetImageFilename();
414      image_patch_delta = image_space->GetImageHeader().GetPatchDelta();
415    }
416
417    if (!image_file_location.empty()) {
418      key_value_store->Put(OatHeader::kImageLocationKey, image_file_location);
419    }
420
421    OatWriter oat_writer(dex_files, image_file_location_oat_checksum,
422                         image_file_location_oat_data_begin,
423                         image_patch_delta,
424                         driver.get(),
425                         &timings,
426                         key_value_store);
427
428    t2.NewTiming("Writing ELF");
429    if (!driver->WriteElf(android_root, is_host, dex_files, &oat_writer, oat_file)) {
430      LOG(ERROR) << "Failed to write ELF file " << oat_file->GetPath();
431      return nullptr;
432    }
433
434    // Flush result to disk. Patching code will re-open the file (mmap), so ensure that our view
435    // of the file already made it there and won't be re-ordered with writes from PatchOat or
436    // image patching.
437    if (oat_file->Flush() != 0) {
438      PLOG(ERROR) << "Failed flushing oat file " << oat_file->GetPath();
439      oat_file->Erase();
440      return nullptr;
441    }
442
443    if (!driver->IsImage() && driver->GetCompilerOptions().GetIncludePatchInformation()) {
444      t2.NewTiming("Patching ELF");
445      std::string error_msg;
446      if (!PatchOatCode(driver.get(), oat_file, oat_location, &error_msg)) {
447        LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath() << ": " << error_msg;
448        return nullptr;
449      }
450    }
451
452    return driver.release();
453  }
454
455  bool CreateImageFile(const std::string& image_filename,
456                       uintptr_t image_base,
457                       const std::string& oat_filename,
458                       const std::string& oat_location,
459                       const CompilerDriver& compiler)
460      LOCKS_EXCLUDED(Locks::mutator_lock_) {
461    uintptr_t oat_data_begin;
462    {
463      // ImageWriter is scoped so it can free memory before doing FixupElf
464      ImageWriter image_writer(compiler);
465      if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location,
466                              compiler_options_->GetCompilePic())) {
467        LOG(ERROR) << "Failed to create image file " << image_filename;
468        return false;
469      }
470      oat_data_begin = image_writer.GetOatDataBegin();
471    }
472
473
474    // Do not fix up the ELF file if we are --compile-pic
475    if (!compiler_options_->GetCompilePic()) {
476      std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
477      if (oat_file.get() == nullptr) {
478        PLOG(ERROR) << "Failed to open ELF file: " << oat_filename;
479        return false;
480      }
481
482      if (!ElfFixup::Fixup(oat_file.get(), oat_data_begin)) {
483        LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
484        oat_file->Erase();
485        return false;
486      }
487
488      if (oat_file->FlushCloseOrErase() != 0) {
489        PLOG(ERROR) << "Failed to flush and close patched oat file " << oat_filename;
490        return false;
491      }
492    }
493
494    return true;
495  }
496
497 private:
498  explicit Dex2Oat(const CompilerOptions* compiler_options,
499                   Compiler::Kind compiler_kind,
500                   InstructionSet instruction_set,
501                   InstructionSetFeatures instruction_set_features,
502                   VerificationResults* verification_results,
503                   DexFileToMethodInlinerMap* method_inliner_map,
504                   size_t thread_count)
505      : compiler_options_(compiler_options),
506        compiler_kind_(compiler_kind),
507        instruction_set_(instruction_set),
508        instruction_set_features_(instruction_set_features),
509        verification_results_(verification_results),
510        method_inliner_map_(method_inliner_map),
511        runtime_(nullptr),
512        thread_count_(thread_count),
513        start_ns_(NanoTime()) {
514    CHECK(compiler_options != nullptr);
515    CHECK(verification_results != nullptr);
516    CHECK(method_inliner_map != nullptr);
517  }
518
519  bool CreateRuntime(const RuntimeOptions& runtime_options, InstructionSet instruction_set)
520      SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
521    if (!Runtime::Create(runtime_options, false)) {
522      LOG(ERROR) << "Failed to create runtime";
523      return false;
524    }
525    Runtime* runtime = Runtime::Current();
526    runtime->SetInstructionSet(instruction_set);
527    for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
528      Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
529      if (!runtime->HasCalleeSaveMethod(type)) {
530        runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(type), type);
531      }
532    }
533    runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
534    runtime->GetClassLinker()->RunRootClinits();
535    runtime_ = runtime;
536    return true;
537  }
538
539  // Appends to dex_files any elements of class_path that it doesn't already
540  // contain. This will open those dex files as necessary.
541  static void OpenClassPathFiles(const std::string& class_path,
542                                 std::vector<const DexFile*>& dex_files) {
543    std::vector<std::string> parsed;
544    Split(class_path, ':', parsed);
545    // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained.
546    ScopedObjectAccess soa(Thread::Current());
547    for (size_t i = 0; i < parsed.size(); ++i) {
548      if (DexFilesContains(dex_files, parsed[i])) {
549        continue;
550      }
551      std::string error_msg;
552      if (!DexFile::Open(parsed[i].c_str(), parsed[i].c_str(), &error_msg, &dex_files)) {
553        LOG(WARNING) << "Failed to open dex file '" << parsed[i] << "': " << error_msg;
554      }
555    }
556  }
557
558  // Returns true if dex_files has a dex with the named location.
559  static bool DexFilesContains(const std::vector<const DexFile*>& dex_files,
560                               const std::string& location) {
561    for (size_t i = 0; i < dex_files.size(); ++i) {
562      if (dex_files[i]->GetLocation() == location) {
563        return true;
564      }
565    }
566    return false;
567  }
568
569  const CompilerOptions* const compiler_options_;
570  const Compiler::Kind compiler_kind_;
571
572  const InstructionSet instruction_set_;
573  const InstructionSetFeatures instruction_set_features_;
574
575  VerificationResults* const verification_results_;
576  DexFileToMethodInlinerMap* const method_inliner_map_;
577  Runtime* runtime_;
578  size_t thread_count_;
579  uint64_t start_ns_;
580
581  DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
582};
583
584static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
585                           const std::vector<const char*>& dex_locations,
586                           std::vector<const DexFile*>& dex_files) {
587  size_t failure_count = 0;
588  for (size_t i = 0; i < dex_filenames.size(); i++) {
589    const char* dex_filename = dex_filenames[i];
590    const char* dex_location = dex_locations[i];
591    ATRACE_BEGIN(StringPrintf("Opening dex file '%s'", dex_filenames[i]).c_str());
592    std::string error_msg;
593    if (!OS::FileExists(dex_filename)) {
594      LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'";
595      continue;
596    }
597    if (!DexFile::Open(dex_filename, dex_location, &error_msg, &dex_files)) {
598      LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg;
599      ++failure_count;
600    }
601    ATRACE_END();
602  }
603  return failure_count;
604}
605
606// The primary goal of the watchdog is to prevent stuck build servers
607// during development when fatal aborts lead to a cascade of failures
608// that result in a deadlock.
609class WatchDog {
610// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks
611#undef CHECK_PTHREAD_CALL
612#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
613  do { \
614    int rc = call args; \
615    if (rc != 0) { \
616      errno = rc; \
617      std::string message(# call); \
618      message += " failed for "; \
619      message += reason; \
620      Fatal(message); \
621    } \
622  } while (false)
623
624 public:
625  explicit WatchDog(bool is_watch_dog_enabled) {
626    is_watch_dog_enabled_ = is_watch_dog_enabled;
627    if (!is_watch_dog_enabled_) {
628      return;
629    }
630    shutting_down_ = false;
631    const char* reason = "dex2oat watch dog thread startup";
632    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason);
633    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, nullptr), reason);
634    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
635    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
636    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
637  }
638  ~WatchDog() {
639    if (!is_watch_dog_enabled_) {
640      return;
641    }
642    const char* reason = "dex2oat watch dog thread shutdown";
643    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
644    shutting_down_ = true;
645    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
646    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
647
648    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason);
649
650    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
651    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
652  }
653
654 private:
655  static void* CallBack(void* arg) {
656    WatchDog* self = reinterpret_cast<WatchDog*>(arg);
657    ::art::SetThreadName("dex2oat watch dog");
658    self->Wait();
659    return nullptr;
660  }
661
662  static void Message(char severity, const std::string& message) {
663    // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error
664    //       cases.
665    fprintf(stderr, "dex2oat%s %c %d %d %s\n",
666            kIsDebugBuild ? "d" : "",
667            severity,
668            getpid(),
669            GetTid(),
670            message.c_str());
671  }
672
673  static void Warn(const std::string& message) {
674    Message('W', message);
675  }
676
677  static void Fatal(const std::string& message) {
678    Message('F', message);
679    exit(1);
680  }
681
682  void Wait() {
683    bool warning = true;
684    CHECK_GT(kWatchDogTimeoutSeconds, kWatchDogWarningSeconds);
685    // TODO: tune the multiplier for GC verification, the following is just to make the timeout
686    //       large.
687    int64_t multiplier = kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1;
688    timespec warning_ts;
689    InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogWarningSeconds * 1000, 0, &warning_ts);
690    timespec timeout_ts;
691    InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts);
692    const char* reason = "dex2oat watch dog thread waiting";
693    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
694    while (!shutting_down_) {
695      int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_,
696                                                         warning ? &warning_ts
697                                                                 : &timeout_ts));
698      if (rc == ETIMEDOUT) {
699        std::string message(StringPrintf("dex2oat did not finish after %d seconds",
700                                         warning ? kWatchDogWarningSeconds
701                                                 : kWatchDogTimeoutSeconds));
702        if (warning) {
703          Warn(message.c_str());
704          warning = false;
705        } else {
706          Fatal(message.c_str());
707        }
708      } else if (rc != 0) {
709        std::string message(StringPrintf("pthread_cond_timedwait failed: %s",
710                                         strerror(errno)));
711        Fatal(message.c_str());
712      }
713    }
714    CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
715  }
716
717  // When setting timeouts, keep in mind that the build server may not be as fast as your desktop.
718  // Debug builds are slower so they have larger timeouts.
719  static const unsigned int kSlowdownFactor = kIsDebugBuild ? 5U : 1U;
720#if ART_USE_PORTABLE_COMPILER
721  // 2 minutes scaled by kSlowdownFactor.
722  static const unsigned int kWatchDogWarningSeconds = kSlowdownFactor * 2 * 60;
723  // 30 minutes scaled by kSlowdownFactor.
724  static const unsigned int kWatchDogTimeoutSeconds = kSlowdownFactor * 30 * 60;
725#else
726  // 1 minutes scaled by kSlowdownFactor.
727  static const unsigned int kWatchDogWarningSeconds = kSlowdownFactor * 1 * 60;
728  // 6 minutes scaled by kSlowdownFactor.
729  static const unsigned int kWatchDogTimeoutSeconds = kSlowdownFactor * 6 * 60;
730#endif
731
732  bool is_watch_dog_enabled_;
733  bool shutting_down_;
734  // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
735  pthread_mutex_t mutex_;
736  pthread_cond_t cond_;
737  pthread_attr_t attr_;
738  pthread_t pthread_;
739};
740const unsigned int WatchDog::kWatchDogWarningSeconds;
741const unsigned int WatchDog::kWatchDogTimeoutSeconds;
742
743// Given a set of instruction features from the build, parse it.  The
744// input 'str' is a comma separated list of feature names.  Parse it and
745// return the InstructionSetFeatures object.
746static InstructionSetFeatures ParseFeatureList(std::string str) {
747  InstructionSetFeatures result;
748  typedef std::vector<std::string> FeatureList;
749  FeatureList features;
750  Split(str, ',', features);
751  for (FeatureList::iterator i = features.begin(); i != features.end(); i++) {
752    std::string feature = Trim(*i);
753    if (feature == "default") {
754      // Nothing to do.
755    } else if (feature == "div") {
756      // Supports divide instruction.
757       result.SetHasDivideInstruction(true);
758    } else if (feature == "nodiv") {
759      // Turn off support for divide instruction.
760      result.SetHasDivideInstruction(false);
761    } else if (feature == "lpae") {
762      // Supports Large Physical Address Extension.
763      result.SetHasLpae(true);
764    } else if (feature == "nolpae") {
765      // Turn off support for Large Physical Address Extension.
766      result.SetHasLpae(false);
767    } else {
768      Usage("Unknown instruction set feature: '%s'", feature.c_str());
769    }
770  }
771  // others...
772  return result;
773}
774
775void ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
776  std::string::size_type colon = s.find(c);
777  if (colon == std::string::npos) {
778    Usage("Missing char %c in option %s\n", c, s.c_str());
779  }
780  // Add one to remove the char we were trimming until.
781  *parsed_value = s.substr(colon + 1);
782}
783
784void ParseDouble(const std::string& option, char after_char,
785                 double min, double max, double* parsed_value) {
786  std::string substring;
787  ParseStringAfterChar(option, after_char, &substring);
788  bool sane_val = true;
789  double value;
790  if (false) {
791    // TODO: this doesn't seem to work on the emulator.  b/15114595
792    std::stringstream iss(substring);
793    iss >> value;
794    // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
795    sane_val = iss.eof() && (value >= min) && (value <= max);
796  } else {
797    char* end = nullptr;
798    value = strtod(substring.c_str(), &end);
799    sane_val = *end == '\0' && value >= min && value <= max;
800  }
801  if (!sane_val) {
802    Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
803  }
804  *parsed_value = value;
805}
806
807static void b13564922() {
808#if defined(__linux__) && defined(__arm__)
809  int major, minor;
810  struct utsname uts;
811  if (uname(&uts) != -1 &&
812      sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
813      ((major < 3) || ((major == 3) && (minor < 4)))) {
814    // Kernels before 3.4 don't handle the ASLR well and we can run out of address
815    // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization.
816    int old_personality = personality(0xffffffff);
817    if ((old_personality & ADDR_NO_RANDOMIZE) == 0) {
818      int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
819      if (new_personality == -1) {
820        LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed.";
821      }
822    }
823  }
824#endif
825}
826
827static int dex2oat(int argc, char** argv) {
828  b13564922();
829
830  original_argc = argc;
831  original_argv = argv;
832
833  TimingLogger timings("compiler", false, false);
834  CumulativeLogger compiler_phases_timings("compilation times");
835
836  InitLogging(argv);
837
838  // Skip over argv[0].
839  argv++;
840  argc--;
841
842  if (argc == 0) {
843    Usage("No arguments specified");
844  }
845
846  std::vector<const char*> dex_filenames;
847  std::vector<const char*> dex_locations;
848  int zip_fd = -1;
849  std::string zip_location;
850  std::string oat_filename;
851  std::string oat_symbols;
852  std::string oat_location;
853  int oat_fd = -1;
854  std::string bitcode_filename;
855  const char* image_classes_zip_filename = nullptr;
856  const char* image_classes_filename = nullptr;
857  const char* compiled_classes_zip_filename = nullptr;
858  const char* compiled_classes_filename = nullptr;
859  std::string image_filename;
860  std::string boot_image_filename;
861  uintptr_t image_base = 0;
862  std::string android_root;
863  std::vector<const char*> runtime_args;
864  int thread_count = sysconf(_SC_NPROCESSORS_CONF);
865  Compiler::Kind compiler_kind = kUsePortableCompiler
866      ? Compiler::kPortable
867      : Compiler::kQuick;
868  const char* compiler_filter_string = nullptr;
869  bool compile_pic = false;
870  int huge_method_threshold = CompilerOptions::kDefaultHugeMethodThreshold;
871  int large_method_threshold = CompilerOptions::kDefaultLargeMethodThreshold;
872  int small_method_threshold = CompilerOptions::kDefaultSmallMethodThreshold;
873  int tiny_method_threshold = CompilerOptions::kDefaultTinyMethodThreshold;
874  int num_dex_methods_threshold = CompilerOptions::kDefaultNumDexMethodsThreshold;
875
876  // Take the default set of instruction features from the build.
877  InstructionSetFeatures instruction_set_features =
878      ParseFeatureList(Runtime::GetDefaultInstructionSetFeatures());
879
880  InstructionSet instruction_set = kRuntimeISA;
881
882  // Profile file to use
883  std::string profile_file;
884  double top_k_profile_threshold = CompilerOptions::kDefaultTopKProfileThreshold;
885
886  bool is_host = false;
887  bool dump_stats = false;
888  bool dump_timing = false;
889  bool dump_passes = false;
890  bool include_patch_information = CompilerOptions::kDefaultIncludePatchInformation;
891  bool include_debug_symbols = kIsDebugBuild;
892  bool dump_slow_timing = kIsDebugBuild;
893  bool watch_dog_enabled = true;
894  bool generate_gdb_information = kIsDebugBuild;
895
896  // Checks are all explicit until we know the architecture.
897  bool implicit_null_checks = false;
898  bool implicit_so_checks = false;
899  bool implicit_suspend_checks = false;
900
901  for (int i = 0; i < argc; i++) {
902    const StringPiece option(argv[i]);
903    const bool log_options = false;
904    if (log_options) {
905      LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
906    }
907    if (option.starts_with("--dex-file=")) {
908      dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
909    } else if (option.starts_with("--dex-location=")) {
910      dex_locations.push_back(option.substr(strlen("--dex-location=")).data());
911    } else if (option.starts_with("--zip-fd=")) {
912      const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
913      if (!ParseInt(zip_fd_str, &zip_fd)) {
914        Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str);
915      }
916      if (zip_fd < 0) {
917        Usage("--zip-fd passed a negative value %d", zip_fd);
918      }
919    } else if (option.starts_with("--zip-location=")) {
920      zip_location = option.substr(strlen("--zip-location=")).data();
921    } else if (option.starts_with("--oat-file=")) {
922      oat_filename = option.substr(strlen("--oat-file=")).data();
923    } else if (option.starts_with("--oat-symbols=")) {
924      oat_symbols = option.substr(strlen("--oat-symbols=")).data();
925    } else if (option.starts_with("--oat-fd=")) {
926      const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
927      if (!ParseInt(oat_fd_str, &oat_fd)) {
928        Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str);
929      }
930      if (oat_fd < 0) {
931        Usage("--oat-fd passed a negative value %d", oat_fd);
932      }
933    } else if (option == "--watch-dog") {
934      watch_dog_enabled = true;
935    } else if (option == "--no-watch-dog") {
936      watch_dog_enabled = false;
937    } else if (option == "--gen-gdb-info") {
938      generate_gdb_information = true;
939      // Debug symbols are needed for gdb information.
940      include_debug_symbols = true;
941    } else if (option == "--no-gen-gdb-info") {
942      generate_gdb_information = false;
943    } else if (option.starts_with("-j")) {
944      const char* thread_count_str = option.substr(strlen("-j")).data();
945      if (!ParseInt(thread_count_str, &thread_count)) {
946        Usage("Failed to parse -j argument '%s' as an integer", thread_count_str);
947      }
948    } else if (option.starts_with("--oat-location=")) {
949      oat_location = option.substr(strlen("--oat-location=")).data();
950    } else if (option.starts_with("--bitcode=")) {
951      bitcode_filename = option.substr(strlen("--bitcode=")).data();
952    } else if (option.starts_with("--image=")) {
953      image_filename = option.substr(strlen("--image=")).data();
954    } else if (option.starts_with("--image-classes=")) {
955      image_classes_filename = option.substr(strlen("--image-classes=")).data();
956    } else if (option.starts_with("--image-classes-zip=")) {
957      image_classes_zip_filename = option.substr(strlen("--image-classes-zip=")).data();
958    } else if (option.starts_with("--compiled-classes=")) {
959      compiled_classes_filename = option.substr(strlen("--compiled-classes=")).data();
960    } else if (option.starts_with("--compiled-classes-zip=")) {
961      compiled_classes_zip_filename = option.substr(strlen("--compiled-classes-zip=")).data();
962    } else if (option.starts_with("--base=")) {
963      const char* image_base_str = option.substr(strlen("--base=")).data();
964      char* end;
965      image_base = strtoul(image_base_str, &end, 16);
966      if (end == image_base_str || *end != '\0') {
967        Usage("Failed to parse hexadecimal value for option %s", option.data());
968      }
969    } else if (option.starts_with("--boot-image=")) {
970      boot_image_filename = option.substr(strlen("--boot-image=")).data();
971    } else if (option.starts_with("--android-root=")) {
972      android_root = option.substr(strlen("--android-root=")).data();
973    } else if (option.starts_with("--instruction-set=")) {
974      StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
975      if (instruction_set_str == "arm") {
976        instruction_set = kThumb2;
977      } else if (instruction_set_str == "arm64") {
978        instruction_set = kArm64;
979      } else if (instruction_set_str == "mips") {
980        instruction_set = kMips;
981      } else if (instruction_set_str == "x86") {
982        instruction_set = kX86;
983      } else if (instruction_set_str == "x86_64") {
984        instruction_set = kX86_64;
985      }
986    } else if (option.starts_with("--instruction-set-features=")) {
987      StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
988      instruction_set_features = ParseFeatureList(str.as_string());
989    } else if (option.starts_with("--compiler-backend=")) {
990      StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
991      if (backend_str == "Quick") {
992        compiler_kind = Compiler::kQuick;
993      } else if (backend_str == "Optimizing") {
994        compiler_kind = Compiler::kOptimizing;
995      } else if (backend_str == "Portable") {
996        compiler_kind = Compiler::kPortable;
997      }
998    } else if (option.starts_with("--compiler-filter=")) {
999      compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
1000    } else if (option == "--compile-pic") {
1001      compile_pic = true;
1002    } else if (option.starts_with("--huge-method-max=")) {
1003      const char* threshold = option.substr(strlen("--huge-method-max=")).data();
1004      if (!ParseInt(threshold, &huge_method_threshold)) {
1005        Usage("Failed to parse --huge-method-max '%s' as an integer", threshold);
1006      }
1007      if (huge_method_threshold < 0) {
1008        Usage("--huge-method-max passed a negative value %s", huge_method_threshold);
1009      }
1010    } else if (option.starts_with("--large-method-max=")) {
1011      const char* threshold = option.substr(strlen("--large-method-max=")).data();
1012      if (!ParseInt(threshold, &large_method_threshold)) {
1013        Usage("Failed to parse --large-method-max '%s' as an integer", threshold);
1014      }
1015      if (large_method_threshold < 0) {
1016        Usage("--large-method-max passed a negative value %s", large_method_threshold);
1017      }
1018    } else if (option.starts_with("--small-method-max=")) {
1019      const char* threshold = option.substr(strlen("--small-method-max=")).data();
1020      if (!ParseInt(threshold, &small_method_threshold)) {
1021        Usage("Failed to parse --small-method-max '%s' as an integer", threshold);
1022      }
1023      if (small_method_threshold < 0) {
1024        Usage("--small-method-max passed a negative value %s", small_method_threshold);
1025      }
1026    } else if (option.starts_with("--tiny-method-max=")) {
1027      const char* threshold = option.substr(strlen("--tiny-method-max=")).data();
1028      if (!ParseInt(threshold, &tiny_method_threshold)) {
1029        Usage("Failed to parse --tiny-method-max '%s' as an integer", threshold);
1030      }
1031      if (tiny_method_threshold < 0) {
1032        Usage("--tiny-method-max passed a negative value %s", tiny_method_threshold);
1033      }
1034    } else if (option.starts_with("--num-dex-methods=")) {
1035      const char* threshold = option.substr(strlen("--num-dex-methods=")).data();
1036      if (!ParseInt(threshold, &num_dex_methods_threshold)) {
1037        Usage("Failed to parse --num-dex-methods '%s' as an integer", threshold);
1038      }
1039      if (num_dex_methods_threshold < 0) {
1040        Usage("--num-dex-methods passed a negative value %s", num_dex_methods_threshold);
1041      }
1042    } else if (option == "--host") {
1043      is_host = true;
1044    } else if (option == "--runtime-arg") {
1045      if (++i >= argc) {
1046        Usage("Missing required argument for --runtime-arg");
1047      }
1048      if (log_options) {
1049        LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
1050      }
1051      runtime_args.push_back(argv[i]);
1052    } else if (option == "--dump-timing") {
1053      dump_timing = true;
1054    } else if (option == "--dump-passes") {
1055      dump_passes = true;
1056    } else if (option == "--dump-stats") {
1057      dump_stats = true;
1058    } else if (option == "--include-debug-symbols" || option == "--no-strip-symbols") {
1059      include_debug_symbols = true;
1060    } else if (option == "--no-include-debug-symbols" || option == "--strip-symbols") {
1061      include_debug_symbols = false;
1062      generate_gdb_information = false;  // Depends on debug symbols, see above.
1063    } else if (option.starts_with("--profile-file=")) {
1064      profile_file = option.substr(strlen("--profile-file=")).data();
1065      VLOG(compiler) << "dex2oat: profile file is " << profile_file;
1066    } else if (option == "--no-profile-file") {
1067      // No profile
1068    } else if (option.starts_with("--top-k-profile-threshold=")) {
1069      ParseDouble(option.data(), '=', 0.0, 100.0, &top_k_profile_threshold);
1070    } else if (option == "--print-pass-names") {
1071      PassDriverMEOpts::PrintPassNames();
1072    } else if (option.starts_with("--disable-passes=")) {
1073      std::string disable_passes = option.substr(strlen("--disable-passes=")).data();
1074      PassDriverMEOpts::CreateDefaultPassList(disable_passes);
1075    } else if (option.starts_with("--print-passes=")) {
1076      std::string print_passes = option.substr(strlen("--print-passes=")).data();
1077      PassDriverMEOpts::SetPrintPassList(print_passes);
1078    } else if (option == "--print-all-passes") {
1079      PassDriverMEOpts::SetPrintAllPasses();
1080    } else if (option.starts_with("--dump-cfg-passes=")) {
1081      std::string dump_passes = option.substr(strlen("--dump-cfg-passes=")).data();
1082      PassDriverMEOpts::SetDumpPassList(dump_passes);
1083    } else if (option == "--include-patch-information") {
1084      include_patch_information = true;
1085    } else if (option == "--no-include-patch-information") {
1086      include_patch_information = false;
1087    } else {
1088      Usage("Unknown argument %s", option.data());
1089    }
1090  }
1091
1092  if (oat_filename.empty() && oat_fd == -1) {
1093    Usage("Output must be supplied with either --oat-file or --oat-fd");
1094  }
1095
1096  if (!oat_filename.empty() && oat_fd != -1) {
1097    Usage("--oat-file should not be used with --oat-fd");
1098  }
1099
1100  if (!oat_symbols.empty() && oat_fd != -1) {
1101    Usage("--oat-symbols should not be used with --oat-fd");
1102  }
1103
1104  if (!oat_symbols.empty() && is_host) {
1105    Usage("--oat-symbols should not be used with --host");
1106  }
1107
1108  if (oat_fd != -1 && !image_filename.empty()) {
1109    Usage("--oat-fd should not be used with --image");
1110  }
1111
1112  if (android_root.empty()) {
1113    const char* android_root_env_var = getenv("ANDROID_ROOT");
1114    if (android_root_env_var == nullptr) {
1115      Usage("--android-root unspecified and ANDROID_ROOT not set");
1116    }
1117    android_root += android_root_env_var;
1118  }
1119
1120  bool image = (!image_filename.empty());
1121  if (!image && boot_image_filename.empty()) {
1122    boot_image_filename += android_root;
1123    boot_image_filename += "/framework/boot.art";
1124  }
1125  std::string boot_image_option;
1126  if (!boot_image_filename.empty()) {
1127    boot_image_option += "-Ximage:";
1128    boot_image_option += boot_image_filename;
1129  }
1130
1131  if (image_classes_filename != nullptr && !image) {
1132    Usage("--image-classes should only be used with --image");
1133  }
1134
1135  if (image_classes_filename != nullptr && !boot_image_option.empty()) {
1136    Usage("--image-classes should not be used with --boot-image");
1137  }
1138
1139  if (image_classes_zip_filename != nullptr && image_classes_filename == nullptr) {
1140    Usage("--image-classes-zip should be used with --image-classes");
1141  }
1142
1143  if (compiled_classes_filename != nullptr && !image) {
1144    Usage("--compiled-classes should only be used with --image");
1145  }
1146
1147  if (compiled_classes_filename != nullptr && !boot_image_option.empty()) {
1148    Usage("--compiled-classes should not be used with --boot-image");
1149  }
1150
1151  if (compiled_classes_zip_filename != nullptr && compiled_classes_filename == nullptr) {
1152    Usage("--compiled-classes-zip should be used with --compiled-classes");
1153  }
1154
1155  if (dex_filenames.empty() && zip_fd == -1) {
1156    Usage("Input must be supplied with either --dex-file or --zip-fd");
1157  }
1158
1159  if (!dex_filenames.empty() && zip_fd != -1) {
1160    Usage("--dex-file should not be used with --zip-fd");
1161  }
1162
1163  if (!dex_filenames.empty() && !zip_location.empty()) {
1164    Usage("--dex-file should not be used with --zip-location");
1165  }
1166
1167  if (dex_locations.empty()) {
1168    for (size_t i = 0; i < dex_filenames.size(); i++) {
1169      dex_locations.push_back(dex_filenames[i]);
1170    }
1171  } else if (dex_locations.size() != dex_filenames.size()) {
1172    Usage("--dex-location arguments do not match --dex-file arguments");
1173  }
1174
1175  if (zip_fd != -1 && zip_location.empty()) {
1176    Usage("--zip-location should be supplied with --zip-fd");
1177  }
1178
1179  if (boot_image_option.empty()) {
1180    if (image_base == 0) {
1181      Usage("Non-zero --base not specified");
1182    }
1183  }
1184
1185  std::string oat_stripped(oat_filename);
1186  std::string oat_unstripped;
1187  if (!oat_symbols.empty()) {
1188    oat_unstripped += oat_symbols;
1189  } else {
1190    oat_unstripped += oat_filename;
1191  }
1192
1193  if (compiler_filter_string == nullptr) {
1194    if (instruction_set == kMips64) {
1195      // TODO: fix compiler for Mips64.
1196      compiler_filter_string = "interpret-only";
1197    } else if (image) {
1198      compiler_filter_string = "speed";
1199    } else {
1200#if ART_SMALL_MODE
1201      compiler_filter_string = "interpret-only";
1202#else
1203      compiler_filter_string = "speed";
1204#endif
1205    }
1206  }
1207  CHECK(compiler_filter_string != nullptr);
1208  CompilerOptions::CompilerFilter compiler_filter = CompilerOptions::kDefaultCompilerFilter;
1209  if (strcmp(compiler_filter_string, "verify-none") == 0) {
1210    compiler_filter = CompilerOptions::kVerifyNone;
1211  } else if (strcmp(compiler_filter_string, "interpret-only") == 0) {
1212    compiler_filter = CompilerOptions::kInterpretOnly;
1213  } else if (strcmp(compiler_filter_string, "space") == 0) {
1214    compiler_filter = CompilerOptions::kSpace;
1215  } else if (strcmp(compiler_filter_string, "balanced") == 0) {
1216    compiler_filter = CompilerOptions::kBalanced;
1217  } else if (strcmp(compiler_filter_string, "speed") == 0) {
1218    compiler_filter = CompilerOptions::kSpeed;
1219  } else if (strcmp(compiler_filter_string, "everything") == 0) {
1220    compiler_filter = CompilerOptions::kEverything;
1221  } else {
1222    Usage("Unknown --compiler-filter value %s", compiler_filter_string);
1223  }
1224
1225  // Set the compilation target's implicit checks options.
1226  switch (instruction_set) {
1227    case kArm:
1228    case kThumb2:
1229    case kArm64:
1230    case kX86:
1231    case kX86_64:
1232      implicit_null_checks = true;
1233      implicit_so_checks = true;
1234      break;
1235
1236    default:
1237      // Defaults are correct.
1238      break;
1239  }
1240
1241  std::unique_ptr<CompilerOptions> compiler_options(new CompilerOptions(compiler_filter,
1242                                                                        huge_method_threshold,
1243                                                                        large_method_threshold,
1244                                                                        small_method_threshold,
1245                                                                        tiny_method_threshold,
1246                                                                        num_dex_methods_threshold,
1247                                                                        generate_gdb_information,
1248                                                                        include_patch_information,
1249                                                                        top_k_profile_threshold,
1250                                                                        include_debug_symbols,
1251                                                                        implicit_null_checks,
1252                                                                        implicit_so_checks,
1253                                                                        implicit_suspend_checks,
1254                                                                        compile_pic
1255#ifdef ART_SEA_IR_MODE
1256                                                                        , compiler_options.sea_ir_ =
1257                                                                              true;
1258#endif
1259  ));  // NOLINT(whitespace/parens)
1260
1261  // Done with usage checks, enable watchdog if requested
1262  WatchDog watch_dog(watch_dog_enabled);
1263
1264  // Check early that the result of compilation can be written
1265  std::unique_ptr<File> oat_file;
1266  bool create_file = !oat_unstripped.empty();  // as opposed to using open file descriptor
1267  if (create_file) {
1268    oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str()));
1269    if (oat_location.empty()) {
1270      oat_location = oat_filename;
1271    }
1272  } else {
1273    oat_file.reset(new File(oat_fd, oat_location, true));
1274    oat_file->DisableAutoClose();
1275    if (oat_file->SetLength(0)) {  // Only warn for truncation error.
1276      PLOG(WARNING) << "Truncating oat file " << oat_location << " failed.";
1277    }
1278  }
1279  if (oat_file.get() == nullptr) {
1280    PLOG(ERROR) << "Failed to create oat file: " << oat_location;
1281    return EXIT_FAILURE;
1282  }
1283  if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
1284    PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
1285    return EXIT_FAILURE;
1286  }
1287
1288  timings.StartTiming("dex2oat Setup");
1289  LOG(INFO) << CommandLine();
1290
1291  RuntimeOptions runtime_options;
1292  std::vector<const DexFile*> boot_class_path;
1293  art::MemMap::Init();  // For ZipEntry::ExtractToMemMap.
1294  if (boot_image_option.empty()) {
1295    size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path);
1296    if (failure_count > 0) {
1297      LOG(ERROR) << "Failed to open some dex files: " << failure_count;
1298      return EXIT_FAILURE;
1299    }
1300    runtime_options.push_back(std::make_pair("bootclasspath", &boot_class_path));
1301  } else {
1302    runtime_options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
1303  }
1304  for (size_t i = 0; i < runtime_args.size(); i++) {
1305    runtime_options.push_back(std::make_pair(runtime_args[i], nullptr));
1306  }
1307
1308  std::unique_ptr<VerificationResults> verification_results(new VerificationResults(
1309                                                            compiler_options.get()));
1310  DexFileToMethodInlinerMap method_inliner_map;
1311  QuickCompilerCallbacks callbacks(verification_results.get(), &method_inliner_map);
1312  runtime_options.push_back(std::make_pair("compilercallbacks", &callbacks));
1313  runtime_options.push_back(
1314      std::make_pair("imageinstructionset",
1315                     reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
1316
1317  Dex2Oat* p_dex2oat;
1318  if (!Dex2Oat::Create(&p_dex2oat,
1319                       runtime_options,
1320                       *compiler_options,
1321                       compiler_kind,
1322                       instruction_set,
1323                       instruction_set_features,
1324                       verification_results.get(),
1325                       &method_inliner_map,
1326                       thread_count)) {
1327    LOG(ERROR) << "Failed to create dex2oat";
1328    timings.EndTiming();
1329    return EXIT_FAILURE;
1330  }
1331  std::unique_ptr<Dex2Oat> dex2oat(p_dex2oat);
1332
1333  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1334  // give it away now so that we don't starve GC.
1335  Thread* self = Thread::Current();
1336  self->TransitionFromRunnableToSuspended(kNative);
1337  // If we're doing the image, override the compiler filter to force full compilation. Must be
1338  // done ahead of WellKnownClasses::Init that causes verification.  Note: doesn't force
1339  // compilation of class initializers.
1340  // Whilst we're in native take the opportunity to initialize well known classes.
1341  WellKnownClasses::Init(self->GetJniEnv());
1342
1343  // If --image-classes was specified, calculate the full list of classes to include in the image
1344  std::unique_ptr<std::set<std::string>> image_classes(nullptr);
1345  if (image_classes_filename != nullptr) {
1346    std::string error_msg;
1347    if (image_classes_zip_filename != nullptr) {
1348      image_classes.reset(dex2oat->ReadImageClassesFromZip(image_classes_zip_filename,
1349                                                           image_classes_filename,
1350                                                           &error_msg));
1351    } else {
1352      image_classes.reset(dex2oat->ReadImageClassesFromFile(image_classes_filename));
1353    }
1354    if (image_classes.get() == nullptr) {
1355      LOG(ERROR) << "Failed to create list of image classes from '" << image_classes_filename <<
1356          "': " << error_msg;
1357      timings.EndTiming();
1358      return EXIT_FAILURE;
1359    }
1360  } else if (image) {
1361    image_classes.reset(new std::set<std::string>);
1362  }
1363  // If --compiled-classes was specified, calculate the full list of classes to compile in the
1364  // image.
1365  std::unique_ptr<std::set<std::string>> compiled_classes(nullptr);
1366  if (compiled_classes_filename != nullptr) {
1367    std::string error_msg;
1368    if (compiled_classes_zip_filename != nullptr) {
1369      compiled_classes.reset(dex2oat->ReadImageClassesFromZip(compiled_classes_zip_filename,
1370                                                              compiled_classes_filename,
1371                                                              &error_msg));
1372    } else {
1373      compiled_classes.reset(dex2oat->ReadImageClassesFromFile(compiled_classes_filename));
1374    }
1375    if (compiled_classes.get() == nullptr) {
1376      LOG(ERROR) << "Failed to create list of compiled classes from '" << compiled_classes_filename
1377                 << "': " << error_msg;
1378      timings.EndTiming();
1379      return EXIT_FAILURE;
1380    }
1381  } else if (image) {
1382    compiled_classes.reset(nullptr);  // By default compile everything.
1383  }
1384
1385  std::vector<const DexFile*> dex_files;
1386  if (boot_image_option.empty()) {
1387    dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath();
1388  } else {
1389    if (dex_filenames.empty()) {
1390      ATRACE_BEGIN("Opening zip archive from file descriptor");
1391      std::string error_msg;
1392      std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd, zip_location.c_str(),
1393                                                               &error_msg));
1394      if (zip_archive.get() == nullptr) {
1395        LOG(ERROR) << "Failed to open zip from file descriptor for '" << zip_location << "': "
1396            << error_msg;
1397        timings.EndTiming();
1398        return EXIT_FAILURE;
1399      }
1400      if (!DexFile::OpenFromZip(*zip_archive.get(), zip_location, &error_msg, &dex_files)) {
1401        LOG(ERROR) << "Failed to open dex from file descriptor for zip file '" << zip_location
1402            << "': " << error_msg;
1403        timings.EndTiming();
1404        return EXIT_FAILURE;
1405      }
1406      ATRACE_END();
1407    } else {
1408      size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files);
1409      if (failure_count > 0) {
1410        LOG(ERROR) << "Failed to open some dex files: " << failure_count;
1411        timings.EndTiming();
1412        return EXIT_FAILURE;
1413      }
1414    }
1415
1416    const bool kSaveDexInput = false;
1417    if (kSaveDexInput) {
1418      for (size_t i = 0; i < dex_files.size(); ++i) {
1419        const DexFile* dex_file = dex_files[i];
1420        std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex", getpid(), i));
1421        std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str()));
1422        if (tmp_file.get() == nullptr) {
1423            PLOG(ERROR) << "Failed to open file " << tmp_file_name
1424                        << ". Try: adb shell chmod 777 /data/local/tmp";
1425            continue;
1426        }
1427        // This is just dumping files for debugging. Ignore errors, and leave remnants.
1428        UNUSED(tmp_file->WriteFully(dex_file->Begin(), dex_file->Size()));
1429        UNUSED(tmp_file->Flush());
1430        UNUSED(tmp_file->Close());
1431        LOG(INFO) << "Wrote input to " << tmp_file_name;
1432      }
1433    }
1434  }
1435  // Ensure opened dex files are writable for dex-to-dex transformations.
1436  for (const auto& dex_file : dex_files) {
1437    if (!dex_file->EnableWrite()) {
1438      PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n";
1439    }
1440  }
1441
1442  /*
1443   * If we're not in interpret-only or verify-none mode, go ahead and compile small applications.
1444   * Don't bother to check if we're doing the image.
1445   */
1446  if (!image && compiler_options->IsCompilationEnabled()) {
1447    size_t num_methods = 0;
1448    for (size_t i = 0; i != dex_files.size(); ++i) {
1449      const DexFile* dex_file = dex_files[i];
1450      CHECK(dex_file != nullptr);
1451      num_methods += dex_file->NumMethodIds();
1452    }
1453    if (num_methods <= compiler_options->GetNumDexMethodsThreshold()) {
1454      compiler_options->SetCompilerFilter(CompilerOptions::kSpeed);
1455      VLOG(compiler) << "Below method threshold, compiling anyways";
1456    }
1457  }
1458
1459  // Fill some values into the key-value store for the oat header.
1460  std::unique_ptr<SafeMap<std::string, std::string> > key_value_store(
1461      new SafeMap<std::string, std::string>());
1462
1463  // Insert some compiler things.
1464  {
1465    std::ostringstream oss;
1466    for (int i = 0; i < argc; ++i) {
1467      if (i > 0) {
1468        oss << ' ';
1469      }
1470      oss << argv[i];
1471    }
1472    key_value_store->Put(OatHeader::kDex2OatCmdLineKey, oss.str());
1473    oss.str("");  // Reset.
1474    oss << kRuntimeISA;
1475    key_value_store->Put(OatHeader::kDex2OatHostKey, oss.str());
1476    key_value_store->Put(OatHeader::kPicKey, compile_pic ? "true" : "false");
1477  }
1478
1479  std::unique_ptr<const CompilerDriver> compiler(dex2oat->CreateOatFile(boot_image_option,
1480                                                                        android_root,
1481                                                                        is_host,
1482                                                                        dex_files,
1483                                                                        oat_file.get(),
1484                                                                        oat_location,
1485                                                                        bitcode_filename,
1486                                                                        image,
1487                                                                        image_classes,
1488                                                                        compiled_classes,
1489                                                                        dump_stats,
1490                                                                        dump_passes,
1491                                                                        timings,
1492                                                                        compiler_phases_timings,
1493                                                                        profile_file,
1494                                                                        key_value_store.get()));
1495  if (compiler.get() == nullptr) {
1496    LOG(ERROR) << "Failed to create oat file: " << oat_location;
1497    timings.EndTiming();
1498    return EXIT_FAILURE;
1499  }
1500
1501  if (!kUsePortableCompiler) {
1502    if (oat_file->FlushCloseOrErase() != 0) {
1503      PLOG(ERROR) << "Failed to flush and close oat file: " << oat_location;
1504      timings.EndTiming();
1505      return EXIT_FAILURE;
1506    }
1507    oat_file.reset();
1508  }
1509
1510  VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location;
1511  // Notes on the interleaving of creating the image and oat file to
1512  // ensure the references between the two are correct.
1513  //
1514  // Currently we have a memory layout that looks something like this:
1515  //
1516  // +--------------+
1517  // | image        |
1518  // +--------------+
1519  // | boot oat     |
1520  // +--------------+
1521  // | alloc spaces |
1522  // +--------------+
1523  //
1524  // There are several constraints on the loading of the image and boot.oat.
1525  //
1526  // 1. The image is expected to be loaded at an absolute address and
1527  // contains Objects with absolute pointers within the image.
1528  //
1529  // 2. There are absolute pointers from Methods in the image to their
1530  // code in the oat.
1531  //
1532  // 3. There are absolute pointers from the code in the oat to Methods
1533  // in the image.
1534  //
1535  // 4. There are absolute pointers from code in the oat to other code
1536  // in the oat.
1537  //
1538  // To get this all correct, we go through several steps.
1539  //
1540  // 1. We have already created that oat file above with
1541  // CreateOatFile. Originally this was just our own proprietary file
1542  // but now it is contained within an ELF dynamic object (aka an .so
1543  // file). The Compiler returned by CreateOatFile provides
1544  // PatchInformation for references to oat code and Methods that need
1545  // to be update once we know where the oat file will be located
1546  // after the image.
1547  //
1548  // 2. We create the image file. It needs to know where the oat file
1549  // will be loaded after itself. Originally when oat file was simply
1550  // memory mapped so we could predict where its contents were based
1551  // on the file size. Now that it is an ELF file, we need to inspect
1552  // the ELF file to understand the in memory segment layout including
1553  // where the oat header is located within. ElfPatcher's Patch method
1554  // uses the PatchInformation from the Compiler to touch up absolute
1555  // references in the oat file.
1556  //
1557  // 3. We fixup the ELF program headers so that dlopen will try to
1558  // load the .so at the desired location at runtime by offsetting the
1559  // Elf32_Phdr.p_vaddr values by the desired base address.
1560  //
1561  if (image) {
1562    TimingLogger::ScopedTiming t("dex2oat ImageWriter", &timings);
1563    bool image_creation_success = dex2oat->CreateImageFile(image_filename,
1564                                                           image_base,
1565                                                           oat_unstripped,
1566                                                           oat_location,
1567                                                           *compiler.get());
1568    if (!image_creation_success) {
1569      timings.EndTiming();
1570      return EXIT_FAILURE;
1571    }
1572    VLOG(compiler) << "Image written successfully: " << image_filename;
1573  }
1574
1575  if (is_host) {
1576    timings.EndTiming();
1577    if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
1578      LOG(INFO) << Dumpable<TimingLogger>(timings);
1579    }
1580    if (dump_passes) {
1581      LOG(INFO) << Dumpable<CumulativeLogger>(*compiler.get()->GetTimingsLogger());
1582    }
1583    return EXIT_SUCCESS;
1584  }
1585
1586  // If we don't want to strip in place, copy from unstripped location to stripped location.
1587  // We need to strip after image creation because FixupElf needs to use .strtab.
1588  if (oat_unstripped != oat_stripped) {
1589    TimingLogger::ScopedTiming t("dex2oat OatFile copy", &timings);
1590    if (kUsePortableCompiler) {
1591      if (oat_file->FlushCloseOrErase() != 0) {
1592        PLOG(ERROR) << "Failed to flush and close oat file: " << oat_location;
1593        return EXIT_FAILURE;
1594      }
1595      oat_file.reset();
1596    }
1597    std::unique_ptr<File> in(OS::OpenFileForReading(oat_unstripped.c_str()));
1598    std::unique_ptr<File> out(OS::CreateEmptyFile(oat_stripped.c_str()));
1599    size_t buffer_size = 8192;
1600    std::unique_ptr<uint8_t> buffer(new uint8_t[buffer_size]);
1601    while (true) {
1602      int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
1603      if (bytes_read <= 0) {
1604        break;
1605      }
1606      bool write_ok = out->WriteFully(buffer.get(), bytes_read);
1607      CHECK(write_ok);
1608    }
1609    oat_file.reset(out.release());
1610    VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped;
1611  }
1612
1613  if (kUsePortableCompiler) {
1614    if (!compiler_options->GetIncludeDebugSymbols()) {
1615      timings.NewTiming("dex2oat ElfStripper");
1616      // Strip unneeded sections for target
1617      off_t seek_actual = lseek(oat_file->Fd(), 0, SEEK_SET);
1618      CHECK_EQ(0, seek_actual);
1619      std::string error_msg;
1620      CHECK(ElfStripper::Strip(oat_file.get(), &error_msg)) << error_msg;
1621
1622
1623      // We wrote the oat file successfully, and want to keep it.
1624      VLOG(compiler) << "Oat file written successfully (stripped): " << oat_location;
1625    } else {
1626      VLOG(compiler) << "Oat file written successfully without stripping: " << oat_location;
1627    }
1628    if (oat_file->FlushCloseOrErase() != 0) {
1629      LOG(ERROR) << "Failed to flush and close oat file: " << oat_location;
1630      return EXIT_FAILURE;
1631    }
1632    oat_file.reset(nullptr);
1633  }
1634
1635  if (oat_file.get() != nullptr) {
1636    if (oat_file->FlushCloseOrErase() != 0) {
1637      PLOG(ERROR) << "Failed to flush and close oat file: " << oat_location << "/" << oat_filename;
1638      return EXIT_FAILURE;
1639    }
1640  }
1641
1642  timings.EndTiming();
1643
1644  if (dump_timing || (dump_slow_timing && timings.GetTotalNs() > MsToNs(1000))) {
1645    LOG(INFO) << Dumpable<TimingLogger>(timings);
1646  }
1647  if (dump_passes) {
1648    LOG(INFO) << Dumpable<CumulativeLogger>(compiler_phases_timings);
1649  }
1650
1651  // Everything was successfully written, do an explicit exit here to avoid running Runtime
1652  // destructors that take time (bug 10645725) unless we're a debug build or running on valgrind.
1653  if (!kIsDebugBuild && (RUNNING_ON_VALGRIND == 0)) {
1654    dex2oat->LogCompletionTime();
1655    exit(EXIT_SUCCESS);
1656  }
1657
1658  return EXIT_SUCCESS;
1659}  // NOLINT(readability/fn_size)
1660}  // namespace art
1661
1662int main(int argc, char** argv) {
1663  return art::dex2oat(argc, argv);
1664}
1665