dex2oat.cc revision 19ad58245b5fac4bdf02045ac47472935b0717cd
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <inttypes.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include <sys/stat.h> 21#include <valgrind.h> 22 23#include <fstream> 24#include <iostream> 25#include <sstream> 26#include <string> 27#include <unordered_set> 28#include <vector> 29 30#if defined(__linux__) && defined(__arm__) 31#include <sys/personality.h> 32#include <sys/utsname.h> 33#endif 34 35#define ATRACE_TAG ATRACE_TAG_DALVIK 36#include <cutils/trace.h> 37 38#include "arch/instruction_set_features.h" 39#include "arch/mips/instruction_set_features_mips.h" 40#include "base/dumpable.h" 41#include "base/macros.h" 42#include "base/stl_util.h" 43#include "base/stringpiece.h" 44#include "base/timing_logger.h" 45#include "base/unix_file/fd_file.h" 46#include "class_linker.h" 47#include "compiler.h" 48#include "compiler_callbacks.h" 49#include "dex_file-inl.h" 50#include "dex/pass_manager.h" 51#include "dex/verification_results.h" 52#include "dex/quick_compiler_callbacks.h" 53#include "dex/quick/dex_file_to_method_inliner_map.h" 54#include "driver/compiler_driver.h" 55#include "driver/compiler_options.h" 56#include "elf_file.h" 57#include "elf_writer.h" 58#include "gc/space/image_space.h" 59#include "gc/space/space-inl.h" 60#include "image_writer.h" 61#include "interpreter/unstarted_runtime.h" 62#include "leb128.h" 63#include "mirror/art_method-inl.h" 64#include "mirror/class-inl.h" 65#include "mirror/class_loader.h" 66#include "mirror/object-inl.h" 67#include "mirror/object_array-inl.h" 68#include "oat_writer.h" 69#include "os.h" 70#include "runtime.h" 71#include "ScopedLocalRef.h" 72#include "scoped_thread_state_change.h" 73#include "utils.h" 74#include "vector_output_stream.h" 75#include "well_known_classes.h" 76#include "zip_archive.h" 77 78namespace art { 79 80static int original_argc; 81static char** original_argv; 82 83static std::string CommandLine() { 84 std::vector<std::string> command; 85 for (int i = 0; i < original_argc; ++i) { 86 command.push_back(original_argv[i]); 87 } 88 return Join(command, ' '); 89} 90 91static void UsageErrorV(const char* fmt, va_list ap) { 92 std::string error; 93 StringAppendV(&error, fmt, ap); 94 LOG(ERROR) << error; 95} 96 97static void UsageError(const char* fmt, ...) { 98 va_list ap; 99 va_start(ap, fmt); 100 UsageErrorV(fmt, ap); 101 va_end(ap); 102} 103 104NO_RETURN static void Usage(const char* fmt, ...) { 105 va_list ap; 106 va_start(ap, fmt); 107 UsageErrorV(fmt, ap); 108 va_end(ap); 109 110 UsageError("Command: %s", CommandLine().c_str()); 111 112 UsageError("Usage: dex2oat [options]..."); 113 UsageError(""); 114 UsageError(" -j<number>: specifies the number of threads used for compilation."); 115 UsageError(" Default is the number of detected hardware threads available on the"); 116 UsageError(" host system."); 117 UsageError(" Example: -j12"); 118 UsageError(""); 119 UsageError(" --dex-file=<dex-file>: specifies a .dex, .jar, or .apk file to compile."); 120 UsageError(" Example: --dex-file=/system/framework/core.jar"); 121 UsageError(""); 122 UsageError(" --dex-location=<dex-location>: specifies an alternative dex location to"); 123 UsageError(" encode in the oat file for the corresponding --dex-file argument."); 124 UsageError(" Example: --dex-file=/home/build/out/system/framework/core.jar"); 125 UsageError(" --dex-location=/system/framework/core.jar"); 126 UsageError(""); 127 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); 128 UsageError(" containing a classes.dex file to compile."); 129 UsageError(" Example: --zip-fd=5"); 130 UsageError(""); 131 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file"); 132 UsageError(" corresponding to the file descriptor specified by --zip-fd."); 133 UsageError(" Example: --zip-location=/system/app/Calculator.apk"); 134 UsageError(""); 135 UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); 136 UsageError(" Example: --oat-file=/system/framework/boot.oat"); 137 UsageError(""); 138 UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); 139 UsageError(" Example: --oat-fd=6"); 140 UsageError(""); 141 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); 142 UsageError(" to the file descriptor specified by --oat-fd."); 143 UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat"); 144 UsageError(""); 145 UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); 146 UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); 147 UsageError(""); 148 UsageError(" --image=<file.art>: specifies the output image filename."); 149 UsageError(" Example: --image=/system/framework/boot.art"); 150 UsageError(""); 151 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); 152 UsageError(" Example: --image=frameworks/base/preloaded-classes"); 153 UsageError(""); 154 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); 155 UsageError(" Example: --base=0x50000000"); 156 UsageError(""); 157 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); 158 UsageError(" Example: --boot-image=/system/framework/boot.art"); 159 UsageError(" Default: $ANDROID_ROOT/system/framework/boot.art"); 160 UsageError(""); 161 UsageError(" --android-root=<path>: used to locate libraries for portable linking."); 162 UsageError(" Example: --android-root=out/host/linux-x86"); 163 UsageError(" Default: $ANDROID_ROOT"); 164 UsageError(""); 165 UsageError(" --instruction-set=(arm|arm64|mips|mips64|x86|x86_64): compile for a particular"); 166 UsageError(" instruction set."); 167 UsageError(" Example: --instruction-set=x86"); 168 UsageError(" Default: arm"); 169 UsageError(""); 170 UsageError(" --instruction-set-features=...,: Specify instruction set features"); 171 UsageError(" Example: --instruction-set-features=div"); 172 UsageError(" Default: default"); 173 UsageError(""); 174 UsageError(" --compile-pic: Force indirect use of code, methods, and classes"); 175 UsageError(" Default: disabled"); 176 UsageError(""); 177 UsageError(" --compiler-backend=(Quick|Optimizing): select compiler backend"); 178 UsageError(" set."); 179 UsageError(" Example: --compiler-backend=Optimizing"); 180 if (kUseOptimizingCompiler) { 181 UsageError(" Default: Optimizing"); 182 } else { 183 UsageError(" Default: Quick"); 184 } 185 UsageError(""); 186 UsageError(" --compiler-filter=" 187 "(verify-none" 188 "|interpret-only" 189 "|space" 190 "|balanced" 191 "|speed" 192 "|everything" 193 "|time):"); 194 UsageError(" select compiler filter."); 195 UsageError(" Example: --compiler-filter=everything"); 196 UsageError(" Default: speed"); 197 UsageError(""); 198 UsageError(" --huge-method-max=<method-instruction-count>: threshold size for a huge"); 199 UsageError(" method for compiler filter tuning."); 200 UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold); 201 UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold); 202 UsageError(""); 203 UsageError(" --large-method-max=<method-instruction-count>: threshold size for a large"); 204 UsageError(" method for compiler filter tuning."); 205 UsageError(" Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold); 206 UsageError(" Default: %d", CompilerOptions::kDefaultLargeMethodThreshold); 207 UsageError(""); 208 UsageError(" --small-method-max=<method-instruction-count>: threshold size for a small"); 209 UsageError(" method for compiler filter tuning."); 210 UsageError(" Example: --small-method-max=%d", CompilerOptions::kDefaultSmallMethodThreshold); 211 UsageError(" Default: %d", CompilerOptions::kDefaultSmallMethodThreshold); 212 UsageError(""); 213 UsageError(" --tiny-method-max=<method-instruction-count>: threshold size for a tiny"); 214 UsageError(" method for compiler filter tuning."); 215 UsageError(" Example: --tiny-method-max=%d", CompilerOptions::kDefaultTinyMethodThreshold); 216 UsageError(" Default: %d", CompilerOptions::kDefaultTinyMethodThreshold); 217 UsageError(""); 218 UsageError(" --num-dex-methods=<method-count>: threshold size for a small dex file for"); 219 UsageError(" compiler filter tuning. If the input has fewer than this many methods"); 220 UsageError(" and the filter is not interpret-only or verify-none, overrides the"); 221 UsageError(" filter to use speed"); 222 UsageError(" Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold); 223 UsageError(" Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold); 224 UsageError(""); 225 UsageError(" --dump-timing: display a breakdown of where time was spent"); 226 UsageError(""); 227 UsageError(" --include-patch-information: Include patching information so the generated code"); 228 UsageError(" can have its base address moved without full recompilation."); 229 UsageError(""); 230 UsageError(" --no-include-patch-information: Do not include patching information."); 231 UsageError(""); 232 UsageError(" --include-debug-symbols: Include ELF symbols in this oat file"); 233 UsageError(""); 234 UsageError(" --no-include-debug-symbols: Do not include ELF symbols in this oat file"); 235 UsageError(""); 236 UsageError(" --include-cfi: Include call frame information in the .eh_frame section."); 237 UsageError(" The --include-debug-symbols option implies --include-cfi."); 238 UsageError(""); 239 UsageError(" --no-include-cfi: Do not include call frame information in the .eh_frame section."); 240 UsageError(""); 241 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); 242 UsageError(" such as initial heap size, maximum heap size, and verbose output."); 243 UsageError(" Use a separate --runtime-arg switch for each argument."); 244 UsageError(" Example: --runtime-arg -Xms256m"); 245 UsageError(""); 246 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); 247 UsageError(""); 248 UsageError(" --print-pass-names: print a list of pass names"); 249 UsageError(""); 250 UsageError(" --disable-passes=<pass-names>: disable one or more passes separated by comma."); 251 UsageError(" Example: --disable-passes=UseCount,BBOptimizations"); 252 UsageError(""); 253 UsageError(" --print-pass-options: print a list of passes that have configurable options along " 254 "with the setting."); 255 UsageError(" Will print default if no overridden setting exists."); 256 UsageError(""); 257 UsageError(" --pass-options=Pass1Name:Pass1OptionName:Pass1Option#," 258 "Pass2Name:Pass2OptionName:Pass2Option#"); 259 UsageError(" Used to specify a pass specific option. The setting itself must be integer."); 260 UsageError(" Separator used between options is a comma."); 261 UsageError(""); 262 UsageError(" --swap-file=<file-name>: specifies a file to use for swap."); 263 UsageError(" Example: --swap-file=/data/tmp/swap.001"); 264 UsageError(""); 265 UsageError(" --swap-fd=<file-descriptor>: specifies a file to use for swap (by descriptor)."); 266 UsageError(" Example: --swap-fd=10"); 267 UsageError(""); 268 std::cerr << "See log for usage error information\n"; 269 exit(EXIT_FAILURE); 270} 271 272// The primary goal of the watchdog is to prevent stuck build servers 273// during development when fatal aborts lead to a cascade of failures 274// that result in a deadlock. 275class WatchDog { 276// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks 277#undef CHECK_PTHREAD_CALL 278#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ 279 do { \ 280 int rc = call args; \ 281 if (rc != 0) { \ 282 errno = rc; \ 283 std::string message(# call); \ 284 message += " failed for "; \ 285 message += reason; \ 286 Fatal(message); \ 287 } \ 288 } while (false) 289 290 public: 291 explicit WatchDog(bool is_watch_dog_enabled) { 292 is_watch_dog_enabled_ = is_watch_dog_enabled; 293 if (!is_watch_dog_enabled_) { 294 return; 295 } 296 shutting_down_ = false; 297 const char* reason = "dex2oat watch dog thread startup"; 298 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason); 299 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, nullptr), reason); 300 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); 301 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); 302 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); 303 } 304 ~WatchDog() { 305 if (!is_watch_dog_enabled_) { 306 return; 307 } 308 const char* reason = "dex2oat watch dog thread shutdown"; 309 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 310 shutting_down_ = true; 311 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); 312 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 313 314 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason); 315 316 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); 317 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); 318 } 319 320 private: 321 static void* CallBack(void* arg) { 322 WatchDog* self = reinterpret_cast<WatchDog*>(arg); 323 ::art::SetThreadName("dex2oat watch dog"); 324 self->Wait(); 325 return nullptr; 326 } 327 328 NO_RETURN static void Fatal(const std::string& message) { 329 // TODO: When we can guarantee it won't prevent shutdown in error cases, move to LOG. However, 330 // it's rather easy to hang in unwinding. 331 // LogLine also avoids ART logging lock issues, as it's really only a wrapper around 332 // logcat logging or stderr output. 333 LogMessage::LogLine(__FILE__, __LINE__, LogSeverity::FATAL, message.c_str()); 334 exit(1); 335 } 336 337 void Wait() { 338 // TODO: tune the multiplier for GC verification, the following is just to make the timeout 339 // large. 340 constexpr int64_t multiplier = kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1; 341 timespec timeout_ts; 342 InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); 343 const char* reason = "dex2oat watch dog thread waiting"; 344 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); 345 while (!shutting_down_) { 346 int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts)); 347 if (rc == ETIMEDOUT) { 348 Fatal(StringPrintf("dex2oat did not finish after %" PRId64 " seconds", 349 kWatchDogTimeoutSeconds)); 350 } else if (rc != 0) { 351 std::string message(StringPrintf("pthread_cond_timedwait failed: %s", 352 strerror(errno))); 353 Fatal(message.c_str()); 354 } 355 } 356 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); 357 } 358 359 // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. 360 // Debug builds are slower so they have larger timeouts. 361 static constexpr int64_t kSlowdownFactor = kIsDebugBuild ? 5U : 1U; 362 363 // 10 minutes scaled by kSlowdownFactor. 364 static constexpr int64_t kWatchDogTimeoutSeconds = kSlowdownFactor * 10 * 60; 365 366 bool is_watch_dog_enabled_; 367 bool shutting_down_; 368 // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. 369 pthread_mutex_t mutex_; 370 pthread_cond_t cond_; 371 pthread_attr_t attr_; 372 pthread_t pthread_; 373}; 374 375static void ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) { 376 std::string::size_type colon = s.find(c); 377 if (colon == std::string::npos) { 378 Usage("Missing char %c in option %s\n", c, s.c_str()); 379 } 380 // Add one to remove the char we were trimming until. 381 *parsed_value = s.substr(colon + 1); 382} 383 384static void ParseDouble(const std::string& option, char after_char, double min, double max, 385 double* parsed_value) { 386 std::string substring; 387 ParseStringAfterChar(option, after_char, &substring); 388 bool sane_val = true; 389 double value; 390 if (false) { 391 // TODO: this doesn't seem to work on the emulator. b/15114595 392 std::stringstream iss(substring); 393 iss >> value; 394 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range. 395 sane_val = iss.eof() && (value >= min) && (value <= max); 396 } else { 397 char* end = nullptr; 398 value = strtod(substring.c_str(), &end); 399 sane_val = *end == '\0' && value >= min && value <= max; 400 } 401 if (!sane_val) { 402 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str()); 403 } 404 *parsed_value = value; 405} 406 407static constexpr size_t kMinDexFilesForSwap = 2; 408static constexpr size_t kMinDexFileCumulativeSizeForSwap = 20 * MB; 409 410static bool UseSwap(bool is_image, std::vector<const DexFile*>& dex_files) { 411 if (is_image) { 412 // Don't use swap, we know generation should succeed, and we don't want to slow it down. 413 return false; 414 } 415 if (dex_files.size() < kMinDexFilesForSwap) { 416 // If there are less dex files than the threshold, assume it's gonna be fine. 417 return false; 418 } 419 size_t dex_files_size = 0; 420 for (const auto* dex_file : dex_files) { 421 dex_files_size += dex_file->GetHeader().file_size_; 422 } 423 return dex_files_size >= kMinDexFileCumulativeSizeForSwap; 424} 425 426class Dex2Oat FINAL { 427 public: 428 explicit Dex2Oat(TimingLogger* timings) : 429 compiler_kind_(kUseOptimizingCompiler ? Compiler::kOptimizing : Compiler::kQuick), 430 instruction_set_(kRuntimeISA), 431 // Take the default set of instruction features from the build. 432 method_inliner_map_(), 433 runtime_(nullptr), 434 thread_count_(sysconf(_SC_NPROCESSORS_CONF)), 435 start_ns_(NanoTime()), 436 oat_fd_(-1), 437 zip_fd_(-1), 438 image_base_(0U), 439 image_classes_zip_filename_(nullptr), 440 image_classes_filename_(nullptr), 441 compiled_classes_zip_filename_(nullptr), 442 compiled_classes_filename_(nullptr), 443 compiled_methods_zip_filename_(nullptr), 444 compiled_methods_filename_(nullptr), 445 image_(false), 446 is_host_(false), 447 dump_stats_(false), 448 dump_passes_(false), 449 dump_timing_(false), 450 dump_slow_timing_(kIsDebugBuild), 451 swap_fd_(-1), 452 timings_(timings) {} 453 454 ~Dex2Oat() { 455 // Free opened dex files before deleting the runtime_, because ~DexFile 456 // uses MemMap, which is shut down by ~Runtime. 457 class_path_files_.clear(); 458 opened_dex_files_.clear(); 459 460 // Log completion time before deleting the runtime_, because this accesses 461 // the runtime. 462 LogCompletionTime(); 463 464 if (kIsDebugBuild || (RUNNING_ON_VALGRIND != 0)) { 465 delete runtime_; // See field declaration for why this is manual. 466 } 467 } 468 469 // Parse the arguments from the command line. In case of an unrecognized option or impossible 470 // values/combinations, a usage error will be displayed and exit() is called. Thus, if the method 471 // returns, arguments have been successfully parsed. 472 void ParseArgs(int argc, char** argv) { 473 original_argc = argc; 474 original_argv = argv; 475 476 InitLogging(argv); 477 478 // Skip over argv[0]. 479 argv++; 480 argc--; 481 482 if (argc == 0) { 483 Usage("No arguments specified"); 484 } 485 486 std::string oat_symbols; 487 std::string boot_image_filename; 488 const char* compiler_filter_string = nullptr; 489 bool compile_pic = false; 490 int huge_method_threshold = CompilerOptions::kDefaultHugeMethodThreshold; 491 int large_method_threshold = CompilerOptions::kDefaultLargeMethodThreshold; 492 int small_method_threshold = CompilerOptions::kDefaultSmallMethodThreshold; 493 int tiny_method_threshold = CompilerOptions::kDefaultTinyMethodThreshold; 494 int num_dex_methods_threshold = CompilerOptions::kDefaultNumDexMethodsThreshold; 495 496 // Profile file to use 497 double top_k_profile_threshold = CompilerOptions::kDefaultTopKProfileThreshold; 498 499 bool debuggable = false; 500 bool include_patch_information = CompilerOptions::kDefaultIncludePatchInformation; 501 bool include_debug_symbols = kIsDebugBuild; 502 bool include_cfi = kIsDebugBuild; 503 bool watch_dog_enabled = true; 504 bool abort_on_hard_verifier_error = false; 505 bool requested_specific_compiler = false; 506 507 PassManagerOptions pass_manager_options; 508 509 std::string error_msg; 510 511 for (int i = 0; i < argc; i++) { 512 const StringPiece option(argv[i]); 513 const bool log_options = false; 514 if (log_options) { 515 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 516 } 517 if (option.starts_with("--dex-file=")) { 518 dex_filenames_.push_back(option.substr(strlen("--dex-file=")).data()); 519 } else if (option.starts_with("--dex-location=")) { 520 dex_locations_.push_back(option.substr(strlen("--dex-location=")).data()); 521 } else if (option.starts_with("--zip-fd=")) { 522 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); 523 if (!ParseInt(zip_fd_str, &zip_fd_)) { 524 Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str); 525 } 526 if (zip_fd_ < 0) { 527 Usage("--zip-fd passed a negative value %d", zip_fd_); 528 } 529 } else if (option.starts_with("--zip-location=")) { 530 zip_location_ = option.substr(strlen("--zip-location=")).data(); 531 } else if (option.starts_with("--oat-file=")) { 532 oat_filename_ = option.substr(strlen("--oat-file=")).data(); 533 } else if (option.starts_with("--oat-symbols=")) { 534 oat_symbols = option.substr(strlen("--oat-symbols=")).data(); 535 } else if (option.starts_with("--oat-fd=")) { 536 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); 537 if (!ParseInt(oat_fd_str, &oat_fd_)) { 538 Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str); 539 } 540 if (oat_fd_ < 0) { 541 Usage("--oat-fd passed a negative value %d", oat_fd_); 542 } 543 } else if (option == "--watch-dog") { 544 watch_dog_enabled = true; 545 } else if (option == "--no-watch-dog") { 546 watch_dog_enabled = false; 547 } else if (option.starts_with("-j")) { 548 const char* thread_count_str = option.substr(strlen("-j")).data(); 549 if (!ParseUint(thread_count_str, &thread_count_)) { 550 Usage("Failed to parse -j argument '%s' as an integer", thread_count_str); 551 } 552 } else if (option.starts_with("--oat-location=")) { 553 oat_location_ = option.substr(strlen("--oat-location=")).data(); 554 } else if (option.starts_with("--image=")) { 555 image_filename_ = option.substr(strlen("--image=")).data(); 556 } else if (option.starts_with("--image-classes=")) { 557 image_classes_filename_ = option.substr(strlen("--image-classes=")).data(); 558 } else if (option.starts_with("--image-classes-zip=")) { 559 image_classes_zip_filename_ = option.substr(strlen("--image-classes-zip=")).data(); 560 } else if (option.starts_with("--compiled-classes=")) { 561 compiled_classes_filename_ = option.substr(strlen("--compiled-classes=")).data(); 562 } else if (option.starts_with("--compiled-classes-zip=")) { 563 compiled_classes_zip_filename_ = option.substr(strlen("--compiled-classes-zip=")).data(); 564 } else if (option.starts_with("--compiled-methods=")) { 565 compiled_methods_filename_ = option.substr(strlen("--compiled-methods=")).data(); 566 } else if (option.starts_with("--compiled-methods-zip=")) { 567 compiled_methods_zip_filename_ = option.substr(strlen("--compiled-methods-zip=")).data(); 568 } else if (option.starts_with("--base=")) { 569 const char* image_base_str = option.substr(strlen("--base=")).data(); 570 char* end; 571 image_base_ = strtoul(image_base_str, &end, 16); 572 if (end == image_base_str || *end != '\0') { 573 Usage("Failed to parse hexadecimal value for option %s", option.data()); 574 } 575 } else if (option.starts_with("--boot-image=")) { 576 boot_image_filename = option.substr(strlen("--boot-image=")).data(); 577 } else if (option.starts_with("--android-root=")) { 578 android_root_ = option.substr(strlen("--android-root=")).data(); 579 } else if (option.starts_with("--instruction-set=")) { 580 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); 581 // StringPiece is not necessarily zero-terminated, so need to make a copy and ensure it. 582 std::unique_ptr<char[]> buf(new char[instruction_set_str.length() + 1]); 583 strncpy(buf.get(), instruction_set_str.data(), instruction_set_str.length()); 584 buf.get()[instruction_set_str.length()] = 0; 585 instruction_set_ = GetInstructionSetFromString(buf.get()); 586 // arm actually means thumb2. 587 if (instruction_set_ == InstructionSet::kArm) { 588 instruction_set_ = InstructionSet::kThumb2; 589 } 590 } else if (option.starts_with("--instruction-set-variant=")) { 591 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); 592 instruction_set_features_.reset( 593 InstructionSetFeatures::FromVariant(instruction_set_, str.as_string(), &error_msg)); 594 if (instruction_set_features_.get() == nullptr) { 595 Usage("%s", error_msg.c_str()); 596 } 597 } else if (option.starts_with("--instruction-set-features=")) { 598 StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); 599 if (instruction_set_features_.get() == nullptr) { 600 instruction_set_features_.reset( 601 InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg)); 602 if (instruction_set_features_.get() == nullptr) { 603 Usage("Problem initializing default instruction set features variant: %s", 604 error_msg.c_str()); 605 } 606 } 607 instruction_set_features_.reset( 608 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg)); 609 if (instruction_set_features_.get() == nullptr) { 610 Usage("Error parsing '%s': %s", option.data(), error_msg.c_str()); 611 } 612 } else if (option.starts_with("--compiler-backend=")) { 613 requested_specific_compiler = true; 614 StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); 615 if (backend_str == "Quick") { 616 compiler_kind_ = Compiler::kQuick; 617 } else if (backend_str == "Optimizing") { 618 compiler_kind_ = Compiler::kOptimizing; 619 } else { 620 Usage("Unknown compiler backend: %s", backend_str.data()); 621 } 622 } else if (option.starts_with("--compiler-filter=")) { 623 compiler_filter_string = option.substr(strlen("--compiler-filter=")).data(); 624 } else if (option == "--compile-pic") { 625 compile_pic = true; 626 } else if (option.starts_with("--huge-method-max=")) { 627 const char* threshold = option.substr(strlen("--huge-method-max=")).data(); 628 if (!ParseInt(threshold, &huge_method_threshold)) { 629 Usage("Failed to parse --huge-method-max '%s' as an integer", threshold); 630 } 631 if (huge_method_threshold < 0) { 632 Usage("--huge-method-max passed a negative value %s", huge_method_threshold); 633 } 634 } else if (option.starts_with("--large-method-max=")) { 635 const char* threshold = option.substr(strlen("--large-method-max=")).data(); 636 if (!ParseInt(threshold, &large_method_threshold)) { 637 Usage("Failed to parse --large-method-max '%s' as an integer", threshold); 638 } 639 if (large_method_threshold < 0) { 640 Usage("--large-method-max passed a negative value %s", large_method_threshold); 641 } 642 } else if (option.starts_with("--small-method-max=")) { 643 const char* threshold = option.substr(strlen("--small-method-max=")).data(); 644 if (!ParseInt(threshold, &small_method_threshold)) { 645 Usage("Failed to parse --small-method-max '%s' as an integer", threshold); 646 } 647 if (small_method_threshold < 0) { 648 Usage("--small-method-max passed a negative value %s", small_method_threshold); 649 } 650 } else if (option.starts_with("--tiny-method-max=")) { 651 const char* threshold = option.substr(strlen("--tiny-method-max=")).data(); 652 if (!ParseInt(threshold, &tiny_method_threshold)) { 653 Usage("Failed to parse --tiny-method-max '%s' as an integer", threshold); 654 } 655 if (tiny_method_threshold < 0) { 656 Usage("--tiny-method-max passed a negative value %s", tiny_method_threshold); 657 } 658 } else if (option.starts_with("--num-dex-methods=")) { 659 const char* threshold = option.substr(strlen("--num-dex-methods=")).data(); 660 if (!ParseInt(threshold, &num_dex_methods_threshold)) { 661 Usage("Failed to parse --num-dex-methods '%s' as an integer", threshold); 662 } 663 if (num_dex_methods_threshold < 0) { 664 Usage("--num-dex-methods passed a negative value %s", num_dex_methods_threshold); 665 } 666 } else if (option == "--host") { 667 is_host_ = true; 668 } else if (option == "--runtime-arg") { 669 if (++i >= argc) { 670 Usage("Missing required argument for --runtime-arg"); 671 } 672 if (log_options) { 673 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; 674 } 675 runtime_args_.push_back(argv[i]); 676 } else if (option == "--dump-timing") { 677 dump_timing_ = true; 678 } else if (option == "--dump-passes") { 679 dump_passes_ = true; 680 } else if (option.starts_with("--dump-cfg=")) { 681 dump_cfg_file_name_ = option.substr(strlen("--dump-cfg=")).data(); 682 } else if (option == "--dump-stats") { 683 dump_stats_ = true; 684 } else if (option == "--include-debug-symbols" || option == "--no-strip-symbols") { 685 include_debug_symbols = true; 686 } else if (option == "--no-include-debug-symbols" || option == "--strip-symbols") { 687 include_debug_symbols = false; 688 } else if (option == "--include-cfi") { 689 include_cfi = true; 690 } else if (option == "--no-include-cfi") { 691 include_cfi = false; 692 } else if (option == "--debuggable") { 693 debuggable = true; 694 } else if (option.starts_with("--profile-file=")) { 695 profile_file_ = option.substr(strlen("--profile-file=")).data(); 696 VLOG(compiler) << "dex2oat: profile file is " << profile_file_; 697 } else if (option == "--no-profile-file") { 698 // No profile 699 } else if (option.starts_with("--top-k-profile-threshold=")) { 700 ParseDouble(option.data(), '=', 0.0, 100.0, &top_k_profile_threshold); 701 } else if (option == "--print-pass-names") { 702 pass_manager_options.SetPrintPassNames(true); 703 } else if (option.starts_with("--disable-passes=")) { 704 const std::string disable_passes = option.substr(strlen("--disable-passes=")).data(); 705 pass_manager_options.SetDisablePassList(disable_passes); 706 } else if (option.starts_with("--print-passes=")) { 707 const std::string print_passes = option.substr(strlen("--print-passes=")).data(); 708 pass_manager_options.SetPrintPassList(print_passes); 709 } else if (option == "--print-all-passes") { 710 pass_manager_options.SetPrintAllPasses(); 711 } else if (option.starts_with("--dump-cfg-passes=")) { 712 const std::string dump_passes_string = option.substr(strlen("--dump-cfg-passes=")).data(); 713 pass_manager_options.SetDumpPassList(dump_passes_string); 714 } else if (option == "--print-pass-options") { 715 pass_manager_options.SetPrintPassOptions(true); 716 } else if (option.starts_with("--pass-options=")) { 717 const std::string options = option.substr(strlen("--pass-options=")).data(); 718 pass_manager_options.SetOverriddenPassOptions(options); 719 } else if (option == "--include-patch-information") { 720 include_patch_information = true; 721 } else if (option == "--no-include-patch-information") { 722 include_patch_information = false; 723 } else if (option.starts_with("--verbose-methods=")) { 724 // TODO: rather than switch off compiler logging, make all VLOG(compiler) messages 725 // conditional on having verbost methods. 726 gLogVerbosity.compiler = false; 727 Split(option.substr(strlen("--verbose-methods=")).ToString(), ',', &verbose_methods_); 728 } else if (option.starts_with("--dump-init-failures=")) { 729 std::string file_name = option.substr(strlen("--dump-init-failures=")).data(); 730 init_failure_output_.reset(new std::ofstream(file_name)); 731 if (init_failure_output_.get() == nullptr) { 732 LOG(ERROR) << "Failed to allocate ofstream"; 733 } else if (init_failure_output_->fail()) { 734 LOG(ERROR) << "Failed to open " << file_name << " for writing the initialization " 735 << "failures."; 736 init_failure_output_.reset(); 737 } 738 } else if (option.starts_with("--swap-file=")) { 739 swap_file_name_ = option.substr(strlen("--swap-file=")).data(); 740 } else if (option.starts_with("--swap-fd=")) { 741 const char* swap_fd_str = option.substr(strlen("--swap-fd=")).data(); 742 if (!ParseInt(swap_fd_str, &swap_fd_)) { 743 Usage("Failed to parse --swap-fd argument '%s' as an integer", swap_fd_str); 744 } 745 if (swap_fd_ < 0) { 746 Usage("--swap-fd passed a negative value %d", swap_fd_); 747 } 748 } else if (option == "--abort-on-hard-verifier-error") { 749 abort_on_hard_verifier_error = true; 750 } else { 751 Usage("Unknown argument %s", option.data()); 752 } 753 } 754 755 image_ = (!image_filename_.empty()); 756 if (!requested_specific_compiler && !kUseOptimizingCompiler) { 757 // If no specific compiler is requested, the current behavior is 758 // to compile the boot image with Quick, and the rest with Optimizing. 759 compiler_kind_ = image_ ? Compiler::kQuick : Compiler::kOptimizing; 760 } 761 762 if (compiler_kind_ == Compiler::kOptimizing) { 763 // Optimizing only supports PIC mode. 764 compile_pic = true; 765 } 766 767 if (oat_filename_.empty() && oat_fd_ == -1) { 768 Usage("Output must be supplied with either --oat-file or --oat-fd"); 769 } 770 771 if (!oat_filename_.empty() && oat_fd_ != -1) { 772 Usage("--oat-file should not be used with --oat-fd"); 773 } 774 775 if (!oat_symbols.empty() && oat_fd_ != -1) { 776 Usage("--oat-symbols should not be used with --oat-fd"); 777 } 778 779 if (!oat_symbols.empty() && is_host_) { 780 Usage("--oat-symbols should not be used with --host"); 781 } 782 783 if (oat_fd_ != -1 && !image_filename_.empty()) { 784 Usage("--oat-fd should not be used with --image"); 785 } 786 787 if (android_root_.empty()) { 788 const char* android_root_env_var = getenv("ANDROID_ROOT"); 789 if (android_root_env_var == nullptr) { 790 Usage("--android-root unspecified and ANDROID_ROOT not set"); 791 } 792 android_root_ += android_root_env_var; 793 } 794 795 if (!image_ && boot_image_filename.empty()) { 796 boot_image_filename += android_root_; 797 boot_image_filename += "/framework/boot.art"; 798 } 799 if (!boot_image_filename.empty()) { 800 boot_image_option_ += "-Ximage:"; 801 boot_image_option_ += boot_image_filename; 802 } 803 804 if (image_classes_filename_ != nullptr && !image_) { 805 Usage("--image-classes should only be used with --image"); 806 } 807 808 if (image_classes_filename_ != nullptr && !boot_image_option_.empty()) { 809 Usage("--image-classes should not be used with --boot-image"); 810 } 811 812 if (image_classes_zip_filename_ != nullptr && image_classes_filename_ == nullptr) { 813 Usage("--image-classes-zip should be used with --image-classes"); 814 } 815 816 if (compiled_classes_filename_ != nullptr && !image_) { 817 Usage("--compiled-classes should only be used with --image"); 818 } 819 820 if (compiled_classes_filename_ != nullptr && !boot_image_option_.empty()) { 821 Usage("--compiled-classes should not be used with --boot-image"); 822 } 823 824 if (compiled_classes_zip_filename_ != nullptr && compiled_classes_filename_ == nullptr) { 825 Usage("--compiled-classes-zip should be used with --compiled-classes"); 826 } 827 828 if (dex_filenames_.empty() && zip_fd_ == -1) { 829 Usage("Input must be supplied with either --dex-file or --zip-fd"); 830 } 831 832 if (!dex_filenames_.empty() && zip_fd_ != -1) { 833 Usage("--dex-file should not be used with --zip-fd"); 834 } 835 836 if (!dex_filenames_.empty() && !zip_location_.empty()) { 837 Usage("--dex-file should not be used with --zip-location"); 838 } 839 840 if (dex_locations_.empty()) { 841 for (const char* dex_file_name : dex_filenames_) { 842 dex_locations_.push_back(dex_file_name); 843 } 844 } else if (dex_locations_.size() != dex_filenames_.size()) { 845 Usage("--dex-location arguments do not match --dex-file arguments"); 846 } 847 848 if (zip_fd_ != -1 && zip_location_.empty()) { 849 Usage("--zip-location should be supplied with --zip-fd"); 850 } 851 852 if (boot_image_option_.empty()) { 853 if (image_base_ == 0) { 854 Usage("Non-zero --base not specified"); 855 } 856 } 857 858 oat_stripped_ = oat_filename_; 859 if (!oat_symbols.empty()) { 860 oat_unstripped_ = oat_symbols; 861 } else { 862 oat_unstripped_ = oat_filename_; 863 } 864 865 // If no instruction set feature was given, use the default one for the target 866 // instruction set. 867 if (instruction_set_features_.get() == nullptr) { 868 instruction_set_features_.reset( 869 InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg)); 870 if (instruction_set_features_.get() == nullptr) { 871 Usage("Problem initializing default instruction set features variant: %s", 872 error_msg.c_str()); 873 } 874 } 875 876 if (instruction_set_ == kRuntimeISA) { 877 std::unique_ptr<const InstructionSetFeatures> runtime_features( 878 InstructionSetFeatures::FromCppDefines()); 879 if (!instruction_set_features_->Equals(runtime_features.get())) { 880 LOG(WARNING) << "Mismatch between dex2oat instruction set features (" 881 << *instruction_set_features_ << ") and those of dex2oat executable (" 882 << *runtime_features <<") for the command line:\n" 883 << CommandLine(); 884 } 885 } 886 887 if (compiler_filter_string == nullptr) { 888 compiler_filter_string = "speed"; 889 } 890 891 CHECK(compiler_filter_string != nullptr); 892 CompilerOptions::CompilerFilter compiler_filter = CompilerOptions::kDefaultCompilerFilter; 893 if (strcmp(compiler_filter_string, "verify-none") == 0) { 894 compiler_filter = CompilerOptions::kVerifyNone; 895 } else if (strcmp(compiler_filter_string, "interpret-only") == 0) { 896 compiler_filter = CompilerOptions::kInterpretOnly; 897 } else if (strcmp(compiler_filter_string, "verify-at-runtime") == 0) { 898 compiler_filter = CompilerOptions::kVerifyAtRuntime; 899 } else if (strcmp(compiler_filter_string, "space") == 0) { 900 compiler_filter = CompilerOptions::kSpace; 901 } else if (strcmp(compiler_filter_string, "balanced") == 0) { 902 compiler_filter = CompilerOptions::kBalanced; 903 } else if (strcmp(compiler_filter_string, "speed") == 0) { 904 compiler_filter = CompilerOptions::kSpeed; 905 } else if (strcmp(compiler_filter_string, "everything") == 0) { 906 compiler_filter = CompilerOptions::kEverything; 907 } else if (strcmp(compiler_filter_string, "time") == 0) { 908 compiler_filter = CompilerOptions::kTime; 909 } else { 910 Usage("Unknown --compiler-filter value %s", compiler_filter_string); 911 } 912 913 // Checks are all explicit until we know the architecture. 914 bool implicit_null_checks = false; 915 bool implicit_so_checks = false; 916 bool implicit_suspend_checks = false; 917 // Set the compilation target's implicit checks options. 918 switch (instruction_set_) { 919 case kArm: 920 case kThumb2: 921 case kArm64: 922 case kX86: 923 case kX86_64: 924 implicit_null_checks = true; 925 implicit_so_checks = true; 926 break; 927 928 default: 929 // Defaults are correct. 930 break; 931 } 932 933 if (debuggable) { 934 // TODO: Consider adding CFI info and symbols here. 935 } 936 937 compiler_options_.reset(new CompilerOptions(compiler_filter, 938 huge_method_threshold, 939 large_method_threshold, 940 small_method_threshold, 941 tiny_method_threshold, 942 num_dex_methods_threshold, 943 include_patch_information, 944 top_k_profile_threshold, 945 debuggable, 946 include_debug_symbols, 947 include_cfi, 948 implicit_null_checks, 949 implicit_so_checks, 950 implicit_suspend_checks, 951 compile_pic, 952 verbose_methods_.empty() ? 953 nullptr : 954 &verbose_methods_, 955 new PassManagerOptions(pass_manager_options), 956 init_failure_output_.get(), 957 abort_on_hard_verifier_error)); 958 959 // Done with usage checks, enable watchdog if requested 960 if (watch_dog_enabled) { 961 watchdog_.reset(new WatchDog(true)); 962 } 963 964 // Fill some values into the key-value store for the oat header. 965 key_value_store_.reset(new SafeMap<std::string, std::string>()); 966 967 // Insert some compiler things. 968 { 969 std::ostringstream oss; 970 for (int i = 0; i < argc; ++i) { 971 if (i > 0) { 972 oss << ' '; 973 } 974 oss << argv[i]; 975 } 976 key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str()); 977 oss.str(""); // Reset. 978 oss << kRuntimeISA; 979 key_value_store_->Put(OatHeader::kDex2OatHostKey, oss.str()); 980 key_value_store_->Put(OatHeader::kPicKey, compile_pic ? "true" : "false"); 981 } 982 } 983 984 // Check whether the oat output file is writable, and open it for later. Also open a swap file, 985 // if a name is given. 986 bool OpenFile() { 987 bool create_file = !oat_unstripped_.empty(); // as opposed to using open file descriptor 988 if (create_file) { 989 oat_file_.reset(OS::CreateEmptyFile(oat_unstripped_.c_str())); 990 if (oat_location_.empty()) { 991 oat_location_ = oat_filename_; 992 } 993 } else { 994 oat_file_.reset(new File(oat_fd_, oat_location_, true)); 995 oat_file_->DisableAutoClose(); 996 if (oat_file_->SetLength(0) != 0) { 997 PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed."; 998 } 999 } 1000 if (oat_file_.get() == nullptr) { 1001 PLOG(ERROR) << "Failed to create oat file: " << oat_location_; 1002 return false; 1003 } 1004 if (create_file && fchmod(oat_file_->Fd(), 0644) != 0) { 1005 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location_; 1006 oat_file_->Erase(); 1007 return false; 1008 } 1009 1010 // Swap file handling. 1011 // 1012 // If the swap fd is not -1, we assume this is the file descriptor of an open but unlinked file 1013 // that we can use for swap. 1014 // 1015 // If the swap fd is -1 and we have a swap-file string, open the given file as a swap file. We 1016 // will immediately unlink to satisfy the swap fd assumption. 1017 if (swap_fd_ == -1 && !swap_file_name_.empty()) { 1018 std::unique_ptr<File> swap_file(OS::CreateEmptyFile(swap_file_name_.c_str())); 1019 if (swap_file.get() == nullptr) { 1020 PLOG(ERROR) << "Failed to create swap file: " << swap_file_name_; 1021 return false; 1022 } 1023 swap_fd_ = swap_file->Fd(); 1024 swap_file->MarkUnchecked(); // We don't we to track this, it will be unlinked immediately. 1025 swap_file->DisableAutoClose(); // We'll handle it ourselves, the File object will be 1026 // released immediately. 1027 unlink(swap_file_name_.c_str()); 1028 } 1029 1030 return true; 1031 } 1032 1033 void EraseOatFile() { 1034 DCHECK(oat_file_.get() != nullptr); 1035 oat_file_->Erase(); 1036 oat_file_.reset(); 1037 } 1038 1039 // Set up the environment for compilation. Includes starting the runtime and loading/opening the 1040 // boot class path. 1041 bool Setup() { 1042 TimingLogger::ScopedTiming t("dex2oat Setup", timings_); 1043 RuntimeOptions runtime_options; 1044 art::MemMap::Init(); // For ZipEntry::ExtractToMemMap. 1045 if (boot_image_option_.empty()) { 1046 std::string boot_class_path = "-Xbootclasspath:"; 1047 boot_class_path += Join(dex_filenames_, ':'); 1048 runtime_options.push_back(std::make_pair(boot_class_path, nullptr)); 1049 std::string boot_class_path_locations = "-Xbootclasspath-locations:"; 1050 boot_class_path_locations += Join(dex_locations_, ':'); 1051 runtime_options.push_back(std::make_pair(boot_class_path_locations, nullptr)); 1052 } else { 1053 runtime_options.push_back(std::make_pair(boot_image_option_, nullptr)); 1054 } 1055 for (size_t i = 0; i < runtime_args_.size(); i++) { 1056 runtime_options.push_back(std::make_pair(runtime_args_[i], nullptr)); 1057 } 1058 1059 verification_results_.reset(new VerificationResults(compiler_options_.get())); 1060 callbacks_.reset(new QuickCompilerCallbacks( 1061 verification_results_.get(), 1062 &method_inliner_map_, 1063 image_ ? 1064 CompilerCallbacks::CallbackMode::kCompileBootImage : 1065 CompilerCallbacks::CallbackMode::kCompileApp)); 1066 runtime_options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); 1067 runtime_options.push_back( 1068 std::make_pair("imageinstructionset", GetInstructionSetString(instruction_set_))); 1069 1070 // Only allow no boot image for the runtime if we're compiling one. When we compile an app, 1071 // we don't want fallback mode, it will abort as we do not push a boot classpath (it might 1072 // have been stripped in preopting, anyways). 1073 if (!image_) { 1074 runtime_options.push_back(std::make_pair("-Xno-dex-file-fallback", nullptr)); 1075 } 1076 1077 if (!CreateRuntime(runtime_options)) { 1078 return false; 1079 } 1080 1081 // Runtime::Create acquired the mutator_lock_ that is normally given away when we 1082 // Runtime::Start, give it away now so that we don't starve GC. 1083 Thread* self = Thread::Current(); 1084 self->TransitionFromRunnableToSuspended(kNative); 1085 // If we're doing the image, override the compiler filter to force full compilation. Must be 1086 // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force 1087 // compilation of class initializers. 1088 // Whilst we're in native take the opportunity to initialize well known classes. 1089 WellKnownClasses::Init(self->GetJniEnv()); 1090 1091 // If --image-classes was specified, calculate the full list of classes to include in the image 1092 if (image_classes_filename_ != nullptr) { 1093 std::string error_msg; 1094 if (image_classes_zip_filename_ != nullptr) { 1095 image_classes_.reset(ReadImageClassesFromZip(image_classes_zip_filename_, 1096 image_classes_filename_, 1097 &error_msg)); 1098 } else { 1099 image_classes_.reset(ReadImageClassesFromFile(image_classes_filename_)); 1100 } 1101 if (image_classes_.get() == nullptr) { 1102 LOG(ERROR) << "Failed to create list of image classes from '" << image_classes_filename_ << 1103 "': " << error_msg; 1104 return false; 1105 } 1106 } else if (image_) { 1107 image_classes_.reset(new std::unordered_set<std::string>); 1108 } 1109 // If --compiled-classes was specified, calculate the full list of classes to compile in the 1110 // image. 1111 if (compiled_classes_filename_ != nullptr) { 1112 std::string error_msg; 1113 if (compiled_classes_zip_filename_ != nullptr) { 1114 compiled_classes_.reset(ReadImageClassesFromZip(compiled_classes_zip_filename_, 1115 compiled_classes_filename_, 1116 &error_msg)); 1117 } else { 1118 compiled_classes_.reset(ReadImageClassesFromFile(compiled_classes_filename_)); 1119 } 1120 if (compiled_classes_.get() == nullptr) { 1121 LOG(ERROR) << "Failed to create list of compiled classes from '" 1122 << compiled_classes_filename_ << "': " << error_msg; 1123 return false; 1124 } 1125 } else { 1126 compiled_classes_.reset(nullptr); // By default compile everything. 1127 } 1128 // If --compiled-methods was specified, read the methods to compile from the given file(s). 1129 if (compiled_methods_filename_ != nullptr) { 1130 std::string error_msg; 1131 if (compiled_methods_zip_filename_ != nullptr) { 1132 compiled_methods_.reset(ReadCommentedInputFromZip(compiled_methods_zip_filename_, 1133 compiled_methods_filename_, 1134 nullptr, // No post-processing. 1135 &error_msg)); 1136 } else { 1137 compiled_methods_.reset(ReadCommentedInputFromFile(compiled_methods_filename_, 1138 nullptr)); // No post-processing. 1139 } 1140 if (compiled_methods_.get() == nullptr) { 1141 LOG(ERROR) << "Failed to create list of compiled methods from '" 1142 << compiled_methods_filename_ << "': " << error_msg; 1143 return false; 1144 } 1145 } else { 1146 compiled_methods_.reset(nullptr); // By default compile everything. 1147 } 1148 1149 if (boot_image_option_.empty()) { 1150 dex_files_ = Runtime::Current()->GetClassLinker()->GetBootClassPath(); 1151 } else { 1152 if (dex_filenames_.empty()) { 1153 ATRACE_BEGIN("Opening zip archive from file descriptor"); 1154 std::string error_msg; 1155 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd_, 1156 zip_location_.c_str(), 1157 &error_msg)); 1158 if (zip_archive.get() == nullptr) { 1159 LOG(ERROR) << "Failed to open zip from file descriptor for '" << zip_location_ << "': " 1160 << error_msg; 1161 return false; 1162 } 1163 if (!DexFile::OpenFromZip(*zip_archive.get(), zip_location_, &error_msg, &opened_dex_files_)) { 1164 LOG(ERROR) << "Failed to open dex from file descriptor for zip file '" << zip_location_ 1165 << "': " << error_msg; 1166 return false; 1167 } 1168 for (auto& dex_file : opened_dex_files_) { 1169 dex_files_.push_back(dex_file.get()); 1170 } 1171 ATRACE_END(); 1172 } else { 1173 size_t failure_count = OpenDexFiles(dex_filenames_, dex_locations_, &opened_dex_files_); 1174 if (failure_count > 0) { 1175 LOG(ERROR) << "Failed to open some dex files: " << failure_count; 1176 return false; 1177 } 1178 for (auto& dex_file : opened_dex_files_) { 1179 dex_files_.push_back(dex_file.get()); 1180 } 1181 } 1182 1183 constexpr bool kSaveDexInput = false; 1184 if (kSaveDexInput) { 1185 for (size_t i = 0; i < dex_files_.size(); ++i) { 1186 const DexFile* dex_file = dex_files_[i]; 1187 std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex", 1188 getpid(), i)); 1189 std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str())); 1190 if (tmp_file.get() == nullptr) { 1191 PLOG(ERROR) << "Failed to open file " << tmp_file_name 1192 << ". Try: adb shell chmod 777 /data/local/tmp"; 1193 continue; 1194 } 1195 // This is just dumping files for debugging. Ignore errors, and leave remnants. 1196 UNUSED(tmp_file->WriteFully(dex_file->Begin(), dex_file->Size())); 1197 UNUSED(tmp_file->Flush()); 1198 UNUSED(tmp_file->Close()); 1199 LOG(INFO) << "Wrote input to " << tmp_file_name; 1200 } 1201 } 1202 } 1203 // Ensure opened dex files are writable for dex-to-dex transformations. 1204 for (const auto& dex_file : dex_files_) { 1205 if (!dex_file->EnableWrite()) { 1206 PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n"; 1207 } 1208 } 1209 1210 // If we use a swap file, ensure we are above the threshold to make it necessary. 1211 if (swap_fd_ != -1) { 1212 if (!UseSwap(image_, dex_files_)) { 1213 close(swap_fd_); 1214 swap_fd_ = -1; 1215 VLOG(compiler) << "Decided to run without swap."; 1216 } else { 1217 LOG(INFO) << "Large app, accepted running with swap."; 1218 } 1219 } 1220 // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that. 1221 1222 /* 1223 * If we're not in interpret-only or verify-none mode, go ahead and compile small applications. 1224 * Don't bother to check if we're doing the image. 1225 */ 1226 if (!image_ && 1227 compiler_options_->IsCompilationEnabled() && 1228 compiler_kind_ == Compiler::kQuick) { 1229 size_t num_methods = 0; 1230 for (size_t i = 0; i != dex_files_.size(); ++i) { 1231 const DexFile* dex_file = dex_files_[i]; 1232 CHECK(dex_file != nullptr); 1233 num_methods += dex_file->NumMethodIds(); 1234 } 1235 if (num_methods <= compiler_options_->GetNumDexMethodsThreshold()) { 1236 compiler_options_->SetCompilerFilter(CompilerOptions::kSpeed); 1237 VLOG(compiler) << "Below method threshold, compiling anyways"; 1238 } 1239 } 1240 1241 return true; 1242 } 1243 1244 // Create and invoke the compiler driver. This will compile all the dex files. 1245 void Compile() { 1246 TimingLogger::ScopedTiming t("dex2oat Compile", timings_); 1247 compiler_phases_timings_.reset(new CumulativeLogger("compilation times")); 1248 1249 // Handle and ClassLoader creation needs to come after Runtime::Create 1250 jobject class_loader = nullptr; 1251 Thread* self = Thread::Current(); 1252 if (!boot_image_option_.empty()) { 1253 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1254 OpenClassPathFiles(runtime_->GetClassPathString(), dex_files_, &class_path_files_); 1255 ScopedObjectAccess soa(self); 1256 1257 // Classpath: first the class-path given. 1258 std::vector<const DexFile*> class_path_files; 1259 for (auto& class_path_file : class_path_files_) { 1260 class_path_files.push_back(class_path_file.get()); 1261 } 1262 1263 // Store the classpath we have right now. 1264 key_value_store_->Put(OatHeader::kClassPathKey, 1265 OatFile::EncodeDexFileDependencies(class_path_files)); 1266 1267 // Then the dex files we'll compile. Thus we'll resolve the class-path first. 1268 class_path_files.insert(class_path_files.end(), dex_files_.begin(), dex_files_.end()); 1269 1270 class_loader = class_linker->CreatePathClassLoader(self, class_path_files); 1271 } 1272 1273 driver_.reset(new CompilerDriver(compiler_options_.get(), 1274 verification_results_.get(), 1275 &method_inliner_map_, 1276 compiler_kind_, 1277 instruction_set_, 1278 instruction_set_features_.get(), 1279 image_, 1280 image_classes_.release(), 1281 compiled_classes_.release(), 1282 nullptr, 1283 thread_count_, 1284 dump_stats_, 1285 dump_passes_, 1286 dump_cfg_file_name_, 1287 compiler_phases_timings_.get(), 1288 swap_fd_, 1289 profile_file_)); 1290 1291 driver_->CompileAll(class_loader, dex_files_, timings_); 1292 } 1293 1294 // Notes on the interleaving of creating the image and oat file to 1295 // ensure the references between the two are correct. 1296 // 1297 // Currently we have a memory layout that looks something like this: 1298 // 1299 // +--------------+ 1300 // | image | 1301 // +--------------+ 1302 // | boot oat | 1303 // +--------------+ 1304 // | alloc spaces | 1305 // +--------------+ 1306 // 1307 // There are several constraints on the loading of the image and boot.oat. 1308 // 1309 // 1. The image is expected to be loaded at an absolute address and 1310 // contains Objects with absolute pointers within the image. 1311 // 1312 // 2. There are absolute pointers from Methods in the image to their 1313 // code in the oat. 1314 // 1315 // 3. There are absolute pointers from the code in the oat to Methods 1316 // in the image. 1317 // 1318 // 4. There are absolute pointers from code in the oat to other code 1319 // in the oat. 1320 // 1321 // To get this all correct, we go through several steps. 1322 // 1323 // 1. We prepare offsets for all data in the oat file and calculate 1324 // the oat data size and code size. During this stage, we also set 1325 // oat code offsets in methods for use by the image writer. 1326 // 1327 // 2. We prepare offsets for the objects in the image and calculate 1328 // the image size. 1329 // 1330 // 3. We create the oat file. Originally this was just our own proprietary 1331 // file but now it is contained within an ELF dynamic object (aka an .so 1332 // file). Since we know the image size and oat data size and code size we 1333 // can prepare the ELF headers and we then know the ELF memory segment 1334 // layout and we can now resolve all references. The compiler provides 1335 // LinkerPatch information in each CompiledMethod and we resolve these, 1336 // using the layout information and image object locations provided by 1337 // image writer, as we're writing the method code. 1338 // 1339 // 4. We create the image file. It needs to know where the oat file 1340 // will be loaded after itself. Originally when oat file was simply 1341 // memory mapped so we could predict where its contents were based 1342 // on the file size. Now that it is an ELF file, we need to inspect 1343 // the ELF file to understand the in memory segment layout including 1344 // where the oat header is located within. 1345 // TODO: We could just remember this information from step 3. 1346 // 1347 // 5. We fixup the ELF program headers so that dlopen will try to 1348 // load the .so at the desired location at runtime by offsetting the 1349 // Elf32_Phdr.p_vaddr values by the desired base address. 1350 // TODO: Do this in step 3. We already know the layout there. 1351 // 1352 // Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5. 1353 // are done by the CreateImageFile() below. 1354 1355 1356 // Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the 1357 // ImageWriter, if necessary. 1358 // Note: Flushing (and closing) the file is the caller's responsibility, except for the failure 1359 // case (when the file will be explicitly erased). 1360 bool CreateOatFile() { 1361 CHECK(key_value_store_.get() != nullptr); 1362 1363 TimingLogger::ScopedTiming t("dex2oat Oat", timings_); 1364 1365 std::unique_ptr<OatWriter> oat_writer; 1366 { 1367 TimingLogger::ScopedTiming t2("dex2oat OatWriter", timings_); 1368 std::string image_file_location; 1369 uint32_t image_file_location_oat_checksum = 0; 1370 uintptr_t image_file_location_oat_data_begin = 0; 1371 int32_t image_patch_delta = 0; 1372 if (image_) { 1373 PrepareImageWriter(image_base_); 1374 } else { 1375 TimingLogger::ScopedTiming t3("Loading image checksum", timings_); 1376 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); 1377 image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); 1378 image_file_location_oat_data_begin = 1379 reinterpret_cast<uintptr_t>(image_space->GetImageHeader().GetOatDataBegin()); 1380 image_file_location = image_space->GetImageFilename(); 1381 image_patch_delta = image_space->GetImageHeader().GetPatchDelta(); 1382 } 1383 1384 if (!image_file_location.empty()) { 1385 key_value_store_->Put(OatHeader::kImageLocationKey, image_file_location); 1386 } 1387 1388 oat_writer.reset(new OatWriter(dex_files_, image_file_location_oat_checksum, 1389 image_file_location_oat_data_begin, 1390 image_patch_delta, 1391 driver_.get(), 1392 image_writer_.get(), 1393 timings_, 1394 key_value_store_.get())); 1395 } 1396 1397 if (image_) { 1398 // The OatWriter constructor has already updated offsets in methods and we need to 1399 // prepare method offsets in the image address space for direct method patching. 1400 TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_); 1401 if (!image_writer_->PrepareImageAddressSpace()) { 1402 LOG(ERROR) << "Failed to prepare image address space."; 1403 return false; 1404 } 1405 } 1406 1407 { 1408 TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_); 1409 if (!driver_->WriteElf(android_root_, is_host_, dex_files_, oat_writer.get(), 1410 oat_file_.get())) { 1411 LOG(ERROR) << "Failed to write ELF file " << oat_file_->GetPath(); 1412 return false; 1413 } 1414 } 1415 1416 VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location_; 1417 return true; 1418 } 1419 1420 // If we are compiling an image, invoke the image creation routine. Else just skip. 1421 bool HandleImage() { 1422 if (image_) { 1423 TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_); 1424 if (!CreateImageFile()) { 1425 return false; 1426 } 1427 VLOG(compiler) << "Image written successfully: " << image_filename_; 1428 } 1429 return true; 1430 } 1431 1432 // Create a copy from unstripped to stripped. 1433 bool CopyUnstrippedToStripped() { 1434 // If we don't want to strip in place, copy from unstripped location to stripped location. 1435 // We need to strip after image creation because FixupElf needs to use .strtab. 1436 if (oat_unstripped_ != oat_stripped_) { 1437 // If the oat file is still open, flush it. 1438 if (oat_file_.get() != nullptr && oat_file_->IsOpened()) { 1439 if (!FlushCloseOatFile()) { 1440 return false; 1441 } 1442 } 1443 1444 TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_); 1445 std::unique_ptr<File> in(OS::OpenFileForReading(oat_unstripped_.c_str())); 1446 std::unique_ptr<File> out(OS::CreateEmptyFile(oat_stripped_.c_str())); 1447 size_t buffer_size = 8192; 1448 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); 1449 while (true) { 1450 int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); 1451 if (bytes_read <= 0) { 1452 break; 1453 } 1454 bool write_ok = out->WriteFully(buffer.get(), bytes_read); 1455 CHECK(write_ok); 1456 } 1457 if (out->FlushCloseOrErase() != 0) { 1458 PLOG(ERROR) << "Failed to flush and close copied oat file: " << oat_stripped_; 1459 return false; 1460 } 1461 VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped_; 1462 } 1463 return true; 1464 } 1465 1466 bool FlushOatFile() { 1467 if (oat_file_.get() != nullptr) { 1468 TimingLogger::ScopedTiming t2("dex2oat Flush ELF", timings_); 1469 if (oat_file_->Flush() != 0) { 1470 PLOG(ERROR) << "Failed to flush oat file: " << oat_location_ << " / " 1471 << oat_filename_; 1472 oat_file_->Erase(); 1473 return false; 1474 } 1475 } 1476 return true; 1477 } 1478 1479 bool FlushCloseOatFile() { 1480 if (oat_file_.get() != nullptr) { 1481 std::unique_ptr<File> tmp(oat_file_.release()); 1482 if (tmp->FlushCloseOrErase() != 0) { 1483 PLOG(ERROR) << "Failed to flush and close oat file: " << oat_location_ << " / " 1484 << oat_filename_; 1485 return false; 1486 } 1487 } 1488 return true; 1489 } 1490 1491 void DumpTiming() { 1492 if (dump_timing_ || (dump_slow_timing_ && timings_->GetTotalNs() > MsToNs(1000))) { 1493 LOG(INFO) << Dumpable<TimingLogger>(*timings_); 1494 } 1495 if (dump_passes_) { 1496 LOG(INFO) << Dumpable<CumulativeLogger>(*driver_->GetTimingsLogger()); 1497 } 1498 } 1499 1500 CompilerOptions* GetCompilerOptions() const { 1501 return compiler_options_.get(); 1502 } 1503 1504 bool IsImage() const { 1505 return image_; 1506 } 1507 1508 bool IsHost() const { 1509 return is_host_; 1510 } 1511 1512 private: 1513 static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, 1514 const std::vector<const char*>& dex_locations, 1515 std::vector<std::unique_ptr<const DexFile>>* dex_files) { 1516 DCHECK(dex_files != nullptr) << "OpenDexFiles out-param is nullptr"; 1517 size_t failure_count = 0; 1518 for (size_t i = 0; i < dex_filenames.size(); i++) { 1519 const char* dex_filename = dex_filenames[i]; 1520 const char* dex_location = dex_locations[i]; 1521 ATRACE_BEGIN(StringPrintf("Opening dex file '%s'", dex_filenames[i]).c_str()); 1522 std::string error_msg; 1523 if (!OS::FileExists(dex_filename)) { 1524 LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'"; 1525 continue; 1526 } 1527 if (!DexFile::Open(dex_filename, dex_location, &error_msg, dex_files)) { 1528 LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg; 1529 ++failure_count; 1530 } 1531 ATRACE_END(); 1532 } 1533 return failure_count; 1534 } 1535 1536 // Returns true if dex_files has a dex with the named location. We compare canonical locations, 1537 // so that relative and absolute paths will match. Not caching for the dex_files isn't very 1538 // efficient, but under normal circumstances the list is neither large nor is this part too 1539 // sensitive. 1540 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, 1541 const std::string& location) { 1542 std::string canonical_location(DexFile::GetDexCanonicalLocation(location.c_str())); 1543 for (size_t i = 0; i < dex_files.size(); ++i) { 1544 if (DexFile::GetDexCanonicalLocation(dex_files[i]->GetLocation().c_str()) == 1545 canonical_location) { 1546 return true; 1547 } 1548 } 1549 return false; 1550 } 1551 1552 // Appends to opened_dex_files any elements of class_path that dex_files 1553 // doesn't already contain. This will open those dex files as necessary. 1554 static void OpenClassPathFiles(const std::string& class_path, 1555 std::vector<const DexFile*> dex_files, 1556 std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) { 1557 DCHECK(opened_dex_files != nullptr) << "OpenClassPathFiles out-param is nullptr"; 1558 std::vector<std::string> parsed; 1559 Split(class_path, ':', &parsed); 1560 // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained. 1561 ScopedObjectAccess soa(Thread::Current()); 1562 for (size_t i = 0; i < parsed.size(); ++i) { 1563 if (DexFilesContains(dex_files, parsed[i])) { 1564 continue; 1565 } 1566 std::string error_msg; 1567 if (!DexFile::Open(parsed[i].c_str(), parsed[i].c_str(), &error_msg, opened_dex_files)) { 1568 LOG(WARNING) << "Failed to open dex file '" << parsed[i] << "': " << error_msg; 1569 } 1570 } 1571 } 1572 1573 // Create a runtime necessary for compilation. 1574 bool CreateRuntime(const RuntimeOptions& runtime_options) 1575 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { 1576 if (!Runtime::Create(runtime_options, false)) { 1577 LOG(ERROR) << "Failed to create runtime"; 1578 return false; 1579 } 1580 Runtime* runtime = Runtime::Current(); 1581 runtime->SetInstructionSet(instruction_set_); 1582 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 1583 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 1584 if (!runtime->HasCalleeSaveMethod(type)) { 1585 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(), type); 1586 } 1587 } 1588 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); 1589 1590 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this 1591 // set up. 1592 interpreter::UnstartedRuntimeInitialize(); 1593 1594 runtime->GetClassLinker()->RunRootClinits(); 1595 runtime_ = runtime; 1596 1597 return true; 1598 } 1599 1600 void PrepareImageWriter(uintptr_t image_base) { 1601 image_writer_.reset(new ImageWriter(*driver_, image_base, compiler_options_->GetCompilePic())); 1602 } 1603 1604 // Let the ImageWriter write the image file. If we do not compile PIC, also fix up the oat file. 1605 bool CreateImageFile() 1606 LOCKS_EXCLUDED(Locks::mutator_lock_) { 1607 CHECK(image_writer_ != nullptr); 1608 if (!image_writer_->Write(image_filename_, oat_unstripped_, oat_location_)) { 1609 LOG(ERROR) << "Failed to create image file " << image_filename_; 1610 return false; 1611 } 1612 uintptr_t oat_data_begin = image_writer_->GetOatDataBegin(); 1613 1614 // Destroy ImageWriter before doing FixupElf. 1615 image_writer_.reset(); 1616 1617 // Do not fix up the ELF file if we are --compile-pic 1618 if (!compiler_options_->GetCompilePic()) { 1619 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_unstripped_.c_str())); 1620 if (oat_file.get() == nullptr) { 1621 PLOG(ERROR) << "Failed to open ELF file: " << oat_unstripped_; 1622 return false; 1623 } 1624 1625 if (!ElfWriter::Fixup(oat_file.get(), oat_data_begin)) { 1626 oat_file->Erase(); 1627 LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); 1628 return false; 1629 } 1630 1631 if (oat_file->FlushCloseOrErase()) { 1632 PLOG(ERROR) << "Failed to flush and close fixed ELF file " << oat_file->GetPath(); 1633 return false; 1634 } 1635 } 1636 1637 return true; 1638 } 1639 1640 // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) 1641 static std::unordered_set<std::string>* ReadImageClassesFromFile( 1642 const char* image_classes_filename) { 1643 std::function<std::string(const char*)> process = DotToDescriptor; 1644 return ReadCommentedInputFromFile(image_classes_filename, &process); 1645 } 1646 1647 // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) 1648 static std::unordered_set<std::string>* ReadImageClassesFromZip( 1649 const char* zip_filename, 1650 const char* image_classes_filename, 1651 std::string* error_msg) { 1652 std::function<std::string(const char*)> process = DotToDescriptor; 1653 return ReadCommentedInputFromZip(zip_filename, image_classes_filename, &process, error_msg); 1654 } 1655 1656 // Read lines from the given file, dropping comments and empty lines. Post-process each line with 1657 // the given function. 1658 static std::unordered_set<std::string>* ReadCommentedInputFromFile( 1659 const char* input_filename, std::function<std::string(const char*)>* process) { 1660 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in)); 1661 if (input_file.get() == nullptr) { 1662 LOG(ERROR) << "Failed to open input file " << input_filename; 1663 return nullptr; 1664 } 1665 std::unique_ptr<std::unordered_set<std::string>> result( 1666 ReadCommentedInputStream(*input_file, process)); 1667 input_file->close(); 1668 return result.release(); 1669 } 1670 1671 // Read lines from the given file from the given zip file, dropping comments and empty lines. 1672 // Post-process each line with the given function. 1673 static std::unordered_set<std::string>* ReadCommentedInputFromZip( 1674 const char* zip_filename, 1675 const char* input_filename, 1676 std::function<std::string(const char*)>* process, 1677 std::string* error_msg) { 1678 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg)); 1679 if (zip_archive.get() == nullptr) { 1680 return nullptr; 1681 } 1682 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(input_filename, error_msg)); 1683 if (zip_entry.get() == nullptr) { 1684 *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", input_filename, 1685 zip_filename, error_msg->c_str()); 1686 return nullptr; 1687 } 1688 std::unique_ptr<MemMap> input_file(zip_entry->ExtractToMemMap(zip_filename, 1689 input_filename, 1690 error_msg)); 1691 if (input_file.get() == nullptr) { 1692 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", input_filename, 1693 zip_filename, error_msg->c_str()); 1694 return nullptr; 1695 } 1696 const std::string input_string(reinterpret_cast<char*>(input_file->Begin()), 1697 input_file->Size()); 1698 std::istringstream input_stream(input_string); 1699 return ReadCommentedInputStream(input_stream, process); 1700 } 1701 1702 // Read lines from the given stream, dropping comments and empty lines. Post-process each line 1703 // with the given function. 1704 static std::unordered_set<std::string>* ReadCommentedInputStream( 1705 std::istream& in_stream, 1706 std::function<std::string(const char*)>* process) { 1707 std::unique_ptr<std::unordered_set<std::string>> image_classes( 1708 new std::unordered_set<std::string>); 1709 while (in_stream.good()) { 1710 std::string dot; 1711 std::getline(in_stream, dot); 1712 if (StartsWith(dot, "#") || dot.empty()) { 1713 continue; 1714 } 1715 if (process != nullptr) { 1716 std::string descriptor((*process)(dot.c_str())); 1717 image_classes->insert(descriptor); 1718 } else { 1719 image_classes->insert(dot); 1720 } 1721 } 1722 return image_classes.release(); 1723 } 1724 1725 void LogCompletionTime() { 1726 // Note: when creation of a runtime fails, e.g., when trying to compile an app but when there 1727 // is no image, there won't be a Runtime::Current(). 1728 // Note: driver creation can fail when loading an invalid dex file. 1729 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) 1730 << " (threads: " << thread_count_ << ") " 1731 << ((Runtime::Current() != nullptr && driver_.get() != nullptr) ? 1732 driver_->GetMemoryUsageString(kIsDebugBuild || VLOG_IS_ON(compiler)) : 1733 ""); 1734 } 1735 1736 std::unique_ptr<CompilerOptions> compiler_options_; 1737 Compiler::Kind compiler_kind_; 1738 1739 InstructionSet instruction_set_; 1740 std::unique_ptr<const InstructionSetFeatures> instruction_set_features_; 1741 1742 std::unique_ptr<SafeMap<std::string, std::string> > key_value_store_; 1743 1744 std::unique_ptr<VerificationResults> verification_results_; 1745 DexFileToMethodInlinerMap method_inliner_map_; 1746 std::unique_ptr<QuickCompilerCallbacks> callbacks_; 1747 1748 // Ownership for the class path files. 1749 std::vector<std::unique_ptr<const DexFile>> class_path_files_; 1750 1751 // Not a unique_ptr as we want to just exit on non-debug builds, not bringing the runtime down 1752 // in an orderly fashion. The destructor takes care of deleting this. 1753 Runtime* runtime_; 1754 1755 size_t thread_count_; 1756 uint64_t start_ns_; 1757 std::unique_ptr<WatchDog> watchdog_; 1758 std::unique_ptr<File> oat_file_; 1759 std::string oat_stripped_; 1760 std::string oat_unstripped_; 1761 std::string oat_location_; 1762 std::string oat_filename_; 1763 int oat_fd_; 1764 std::vector<const char*> dex_filenames_; 1765 std::vector<const char*> dex_locations_; 1766 int zip_fd_; 1767 std::string zip_location_; 1768 std::string boot_image_option_; 1769 std::vector<const char*> runtime_args_; 1770 std::string image_filename_; 1771 uintptr_t image_base_; 1772 const char* image_classes_zip_filename_; 1773 const char* image_classes_filename_; 1774 const char* compiled_classes_zip_filename_; 1775 const char* compiled_classes_filename_; 1776 const char* compiled_methods_zip_filename_; 1777 const char* compiled_methods_filename_; 1778 std::unique_ptr<std::unordered_set<std::string>> image_classes_; 1779 std::unique_ptr<std::unordered_set<std::string>> compiled_classes_; 1780 std::unique_ptr<std::unordered_set<std::string>> compiled_methods_; 1781 bool image_; 1782 std::unique_ptr<ImageWriter> image_writer_; 1783 bool is_host_; 1784 std::string android_root_; 1785 std::vector<const DexFile*> dex_files_; 1786 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_; 1787 std::unique_ptr<CompilerDriver> driver_; 1788 std::vector<std::string> verbose_methods_; 1789 bool dump_stats_; 1790 bool dump_passes_; 1791 bool dump_timing_; 1792 bool dump_slow_timing_; 1793 std::string dump_cfg_file_name_; 1794 std::string swap_file_name_; 1795 int swap_fd_; 1796 std::string profile_file_; // Profile file to use 1797 TimingLogger* timings_; 1798 std::unique_ptr<CumulativeLogger> compiler_phases_timings_; 1799 std::unique_ptr<std::ostream> init_failure_output_; 1800 1801 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); 1802}; 1803 1804static void b13564922() { 1805#if defined(__linux__) && defined(__arm__) 1806 int major, minor; 1807 struct utsname uts; 1808 if (uname(&uts) != -1 && 1809 sscanf(uts.release, "%d.%d", &major, &minor) == 2 && 1810 ((major < 3) || ((major == 3) && (minor < 4)))) { 1811 // Kernels before 3.4 don't handle the ASLR well and we can run out of address 1812 // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization. 1813 int old_personality = personality(0xffffffff); 1814 if ((old_personality & ADDR_NO_RANDOMIZE) == 0) { 1815 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); 1816 if (new_personality == -1) { 1817 LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed."; 1818 } 1819 } 1820 } 1821#endif 1822} 1823 1824static int CompileImage(Dex2Oat& dex2oat) { 1825 dex2oat.Compile(); 1826 1827 // Create the boot.oat. 1828 if (!dex2oat.CreateOatFile()) { 1829 dex2oat.EraseOatFile(); 1830 return EXIT_FAILURE; 1831 } 1832 1833 // Flush and close the boot.oat. We always expect the output file by name, and it will be 1834 // re-opened from the unstripped name. 1835 if (!dex2oat.FlushCloseOatFile()) { 1836 return EXIT_FAILURE; 1837 } 1838 1839 // Creates the boot.art and patches the boot.oat. 1840 if (!dex2oat.HandleImage()) { 1841 return EXIT_FAILURE; 1842 } 1843 1844 // When given --host, finish early without stripping. 1845 if (dex2oat.IsHost()) { 1846 dex2oat.DumpTiming(); 1847 return EXIT_SUCCESS; 1848 } 1849 1850 // Copy unstripped to stripped location, if necessary. 1851 if (!dex2oat.CopyUnstrippedToStripped()) { 1852 return EXIT_FAILURE; 1853 } 1854 1855 // FlushClose again, as stripping might have re-opened the oat file. 1856 if (!dex2oat.FlushCloseOatFile()) { 1857 return EXIT_FAILURE; 1858 } 1859 1860 dex2oat.DumpTiming(); 1861 return EXIT_SUCCESS; 1862} 1863 1864static int CompileApp(Dex2Oat& dex2oat) { 1865 dex2oat.Compile(); 1866 1867 // Create the app oat. 1868 if (!dex2oat.CreateOatFile()) { 1869 dex2oat.EraseOatFile(); 1870 return EXIT_FAILURE; 1871 } 1872 1873 // Do not close the oat file here. We might haven gotten the output file by file descriptor, 1874 // which we would lose. 1875 if (!dex2oat.FlushOatFile()) { 1876 return EXIT_FAILURE; 1877 } 1878 1879 // When given --host, finish early without stripping. 1880 if (dex2oat.IsHost()) { 1881 if (!dex2oat.FlushCloseOatFile()) { 1882 return EXIT_FAILURE; 1883 } 1884 1885 dex2oat.DumpTiming(); 1886 return EXIT_SUCCESS; 1887 } 1888 1889 // Copy unstripped to stripped location, if necessary. This will implicitly flush & close the 1890 // unstripped version. If this is given, we expect to be able to open writable files by name. 1891 if (!dex2oat.CopyUnstrippedToStripped()) { 1892 return EXIT_FAILURE; 1893 } 1894 1895 // Flush and close the file. 1896 if (!dex2oat.FlushCloseOatFile()) { 1897 return EXIT_FAILURE; 1898 } 1899 1900 dex2oat.DumpTiming(); 1901 return EXIT_SUCCESS; 1902} 1903 1904static int dex2oat(int argc, char** argv) { 1905 b13564922(); 1906 1907 TimingLogger timings("compiler", false, false); 1908 1909 Dex2Oat dex2oat(&timings); 1910 1911 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. 1912 dex2oat.ParseArgs(argc, argv); 1913 1914 // Check early that the result of compilation can be written 1915 if (!dex2oat.OpenFile()) { 1916 return EXIT_FAILURE; 1917 } 1918 1919 LOG(INFO) << CommandLine(); 1920 1921 if (!dex2oat.Setup()) { 1922 dex2oat.EraseOatFile(); 1923 return EXIT_FAILURE; 1924 } 1925 1926 if (dex2oat.IsImage()) { 1927 return CompileImage(dex2oat); 1928 } else { 1929 return CompileApp(dex2oat); 1930 } 1931} 1932} // namespace art 1933 1934int main(int argc, char** argv) { 1935 int result = art::dex2oat(argc, argv); 1936 // Everything was done, do an explicit exit here to avoid running Runtime destructors that take 1937 // time (bug 10645725) unless we're a debug build or running on valgrind. Note: The Dex2Oat class 1938 // should not destruct the runtime in this case. 1939 if (!art::kIsDebugBuild && (RUNNING_ON_VALGRIND == 0)) { 1940 exit(result); 1941 } 1942 return result; 1943} 1944