compiler_driver.cc revision 081f73e888b3c246cf7635db37b7f1105cf1a2ff
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 "compiler_driver.h" 18 19#define ATRACE_TAG ATRACE_TAG_DALVIK 20#include <utils/Trace.h> 21 22#include <fstream> 23#include <vector> 24#include <unistd.h> 25#include <utility> 26 27#include "base/stl_util.h" 28#include "base/timing_logger.h" 29#include "class_linker.h" 30#include "compiler.h" 31#include "compiler_driver-inl.h" 32#include "dex_compilation_unit.h" 33#include "dex_file-inl.h" 34#include "dex/verification_results.h" 35#include "dex/verified_method.h" 36#include "dex/quick/dex_file_method_inliner.h" 37#include "driver/compiler_options.h" 38#include "jni_internal.h" 39#include "object_utils.h" 40#include "runtime.h" 41#include "gc/accounting/card_table-inl.h" 42#include "gc/accounting/heap_bitmap.h" 43#include "gc/space/space.h" 44#include "mirror/art_field-inl.h" 45#include "mirror/art_method-inl.h" 46#include "mirror/class_loader.h" 47#include "mirror/class-inl.h" 48#include "mirror/dex_cache-inl.h" 49#include "mirror/object-inl.h" 50#include "mirror/object_array-inl.h" 51#include "mirror/throwable.h" 52#include "scoped_thread_state_change.h" 53#include "ScopedLocalRef.h" 54#include "sirt_ref-inl.h" 55#include "thread.h" 56#include "thread_pool.h" 57#include "trampolines/trampoline_compiler.h" 58#include "transaction.h" 59#include "verifier/method_verifier.h" 60#include "verifier/method_verifier-inl.h" 61 62#ifdef HAVE_ANDROID_OS 63#include "cutils/properties.h" 64#endif 65 66namespace art { 67 68static double Percentage(size_t x, size_t y) { 69 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y)); 70} 71 72static void DumpStat(size_t x, size_t y, const char* str) { 73 if (x == 0 && y == 0) { 74 return; 75 } 76 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases"; 77} 78 79class CompilerDriver::AOTCompilationStats { 80 public: 81 AOTCompilationStats() 82 : stats_lock_("AOT compilation statistics lock"), 83 types_in_dex_cache_(0), types_not_in_dex_cache_(0), 84 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0), 85 resolved_types_(0), unresolved_types_(0), 86 resolved_instance_fields_(0), unresolved_instance_fields_(0), 87 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0), 88 type_based_devirtualization_(0), 89 safe_casts_(0), not_safe_casts_(0) { 90 for (size_t i = 0; i <= kMaxInvokeType; i++) { 91 resolved_methods_[i] = 0; 92 unresolved_methods_[i] = 0; 93 virtual_made_direct_[i] = 0; 94 direct_calls_to_boot_[i] = 0; 95 direct_methods_to_boot_[i] = 0; 96 } 97 } 98 99 void Dump() { 100 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache"); 101 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache"); 102 DumpStat(resolved_types_, unresolved_types_, "types resolved"); 103 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved"); 104 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_, 105 "static fields resolved"); 106 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_, 107 "static fields local to a class"); 108 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information"); 109 // Note, the code below subtracts the stat value so that when added to the stat value we have 110 // 100% of samples. TODO: clean this up. 111 DumpStat(type_based_devirtualization_, 112 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] + 113 resolved_methods_[kInterface] + unresolved_methods_[kInterface] - 114 type_based_devirtualization_, 115 "virtual/interface calls made direct based on type information"); 116 117 for (size_t i = 0; i <= kMaxInvokeType; i++) { 118 std::ostringstream oss; 119 oss << static_cast<InvokeType>(i) << " methods were AOT resolved"; 120 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str()); 121 if (virtual_made_direct_[i] > 0) { 122 std::ostringstream oss2; 123 oss2 << static_cast<InvokeType>(i) << " methods made direct"; 124 DumpStat(virtual_made_direct_[i], 125 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i], 126 oss2.str().c_str()); 127 } 128 if (direct_calls_to_boot_[i] > 0) { 129 std::ostringstream oss2; 130 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot"; 131 DumpStat(direct_calls_to_boot_[i], 132 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i], 133 oss2.str().c_str()); 134 } 135 if (direct_methods_to_boot_[i] > 0) { 136 std::ostringstream oss2; 137 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot"; 138 DumpStat(direct_methods_to_boot_[i], 139 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i], 140 oss2.str().c_str()); 141 } 142 } 143 } 144 145// Allow lossy statistics in non-debug builds. 146#ifndef NDEBUG 147#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_) 148#else 149#define STATS_LOCK() 150#endif 151 152 void TypeInDexCache() { 153 STATS_LOCK(); 154 types_in_dex_cache_++; 155 } 156 157 void TypeNotInDexCache() { 158 STATS_LOCK(); 159 types_not_in_dex_cache_++; 160 } 161 162 void StringInDexCache() { 163 STATS_LOCK(); 164 strings_in_dex_cache_++; 165 } 166 167 void StringNotInDexCache() { 168 STATS_LOCK(); 169 strings_not_in_dex_cache_++; 170 } 171 172 void TypeDoesntNeedAccessCheck() { 173 STATS_LOCK(); 174 resolved_types_++; 175 } 176 177 void TypeNeedsAccessCheck() { 178 STATS_LOCK(); 179 unresolved_types_++; 180 } 181 182 void ResolvedInstanceField() { 183 STATS_LOCK(); 184 resolved_instance_fields_++; 185 } 186 187 void UnresolvedInstanceField() { 188 STATS_LOCK(); 189 unresolved_instance_fields_++; 190 } 191 192 void ResolvedLocalStaticField() { 193 STATS_LOCK(); 194 resolved_local_static_fields_++; 195 } 196 197 void ResolvedStaticField() { 198 STATS_LOCK(); 199 resolved_static_fields_++; 200 } 201 202 void UnresolvedStaticField() { 203 STATS_LOCK(); 204 unresolved_static_fields_++; 205 } 206 207 // Indicate that type information from the verifier led to devirtualization. 208 void PreciseTypeDevirtualization() { 209 STATS_LOCK(); 210 type_based_devirtualization_++; 211 } 212 213 // Indicate that a method of the given type was resolved at compile time. 214 void ResolvedMethod(InvokeType type) { 215 DCHECK_LE(type, kMaxInvokeType); 216 STATS_LOCK(); 217 resolved_methods_[type]++; 218 } 219 220 // Indicate that a method of the given type was unresolved at compile time as it was in an 221 // unknown dex file. 222 void UnresolvedMethod(InvokeType type) { 223 DCHECK_LE(type, kMaxInvokeType); 224 STATS_LOCK(); 225 unresolved_methods_[type]++; 226 } 227 228 // Indicate that a type of virtual method dispatch has been converted into a direct method 229 // dispatch. 230 void VirtualMadeDirect(InvokeType type) { 231 DCHECK(type == kVirtual || type == kInterface || type == kSuper); 232 STATS_LOCK(); 233 virtual_made_direct_[type]++; 234 } 235 236 // Indicate that a method of the given type was able to call directly into boot. 237 void DirectCallsToBoot(InvokeType type) { 238 DCHECK_LE(type, kMaxInvokeType); 239 STATS_LOCK(); 240 direct_calls_to_boot_[type]++; 241 } 242 243 // Indicate that a method of the given type was able to be resolved directly from boot. 244 void DirectMethodsToBoot(InvokeType type) { 245 DCHECK_LE(type, kMaxInvokeType); 246 STATS_LOCK(); 247 direct_methods_to_boot_[type]++; 248 } 249 250 void ProcessedInvoke(InvokeType type, int flags) { 251 STATS_LOCK(); 252 if (flags == 0) { 253 unresolved_methods_[type]++; 254 } else { 255 DCHECK_NE((flags & kFlagMethodResolved), 0); 256 resolved_methods_[type]++; 257 if ((flags & kFlagVirtualMadeDirect) != 0) { 258 virtual_made_direct_[type]++; 259 if ((flags & kFlagPreciseTypeDevirtualization) != 0) { 260 type_based_devirtualization_++; 261 } 262 } else { 263 DCHECK_EQ((flags & kFlagPreciseTypeDevirtualization), 0); 264 } 265 if ((flags & kFlagDirectCallToBoot) != 0) { 266 direct_calls_to_boot_[type]++; 267 } 268 if ((flags & kFlagDirectMethodToBoot) != 0) { 269 direct_methods_to_boot_[type]++; 270 } 271 } 272 } 273 274 // A check-cast could be eliminated due to verifier type analysis. 275 void SafeCast() { 276 STATS_LOCK(); 277 safe_casts_++; 278 } 279 280 // A check-cast couldn't be eliminated due to verifier type analysis. 281 void NotASafeCast() { 282 STATS_LOCK(); 283 not_safe_casts_++; 284 } 285 286 private: 287 Mutex stats_lock_; 288 289 size_t types_in_dex_cache_; 290 size_t types_not_in_dex_cache_; 291 292 size_t strings_in_dex_cache_; 293 size_t strings_not_in_dex_cache_; 294 295 size_t resolved_types_; 296 size_t unresolved_types_; 297 298 size_t resolved_instance_fields_; 299 size_t unresolved_instance_fields_; 300 301 size_t resolved_local_static_fields_; 302 size_t resolved_static_fields_; 303 size_t unresolved_static_fields_; 304 // Type based devirtualization for invoke interface and virtual. 305 size_t type_based_devirtualization_; 306 307 size_t resolved_methods_[kMaxInvokeType + 1]; 308 size_t unresolved_methods_[kMaxInvokeType + 1]; 309 size_t virtual_made_direct_[kMaxInvokeType + 1]; 310 size_t direct_calls_to_boot_[kMaxInvokeType + 1]; 311 size_t direct_methods_to_boot_[kMaxInvokeType + 1]; 312 313 size_t safe_casts_; 314 size_t not_safe_casts_; 315 316 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats); 317}; 318 319 320extern "C" art::CompiledMethod* ArtCompileDEX(art::CompilerDriver& compiler, 321 const art::DexFile::CodeItem* code_item, 322 uint32_t access_flags, 323 art::InvokeType invoke_type, 324 uint16_t class_def_idx, 325 uint32_t method_idx, 326 jobject class_loader, 327 const art::DexFile& dex_file); 328 329CompilerDriver::CompilerDriver(const CompilerOptions* compiler_options, 330 VerificationResults* verification_results, 331 DexFileToMethodInlinerMap* method_inliner_map, 332 Compiler::Kind compiler_kind, 333 InstructionSet instruction_set, 334 InstructionSetFeatures instruction_set_features, 335 bool image, DescriptorSet* image_classes, size_t thread_count, 336 bool dump_stats, bool dump_passes, CumulativeLogger* timer, 337 std::string profile_file) 338 : profile_ok_(false), compiler_options_(compiler_options), 339 verification_results_(verification_results), 340 method_inliner_map_(method_inliner_map), 341 compiler_(Compiler::Create(compiler_kind)), 342 instruction_set_(instruction_set), 343 instruction_set_features_(instruction_set_features), 344 instruction_set_is_64_bit_(instruction_set == kX86_64 || instruction_set == kArm64), 345 freezing_constructor_lock_("freezing constructor lock"), 346 compiled_classes_lock_("compiled classes lock"), 347 compiled_methods_lock_("compiled method lock"), 348 image_(image), 349 image_classes_(image_classes), 350 thread_count_(thread_count), 351 start_ns_(0), 352 stats_(new AOTCompilationStats), 353 dump_stats_(dump_stats), 354 dump_passes_(dump_passes), 355 timings_logger_(timer), 356 compiler_library_(NULL), 357 compiler_context_(NULL), 358 compiler_enable_auto_elf_loading_(NULL), 359 compiler_get_method_code_addr_(NULL), 360 support_boot_image_fixup_(instruction_set != kMips), 361 cfi_info_(nullptr), 362 dedupe_code_("dedupe code"), 363 dedupe_mapping_table_("dedupe mapping table"), 364 dedupe_vmap_table_("dedupe vmap table"), 365 dedupe_gc_map_("dedupe gc map"), 366 dedupe_cfi_info_("dedupe cfi info") { 367 DCHECK(compiler_options_ != nullptr); 368 DCHECK(verification_results_ != nullptr); 369 DCHECK(method_inliner_map_ != nullptr); 370 371 CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, NULL), "compiler tls key"); 372 373 // Read the profile file if one is provided. 374 if (profile_file != "") { 375 profile_ok_ = ReadProfile(profile_file); 376 } 377 378 dex_to_dex_compiler_ = reinterpret_cast<DexToDexCompilerFn>(ArtCompileDEX); 379 380 compiler_->Init(*this); 381 382 CHECK(!Runtime::Current()->IsStarted()); 383 if (!image_) { 384 CHECK(image_classes_.get() == NULL); 385 } 386 387 // Are we generating CFI information? 388 if (compiler_options->GetGenerateGDBInformation()) { 389 cfi_info_.reset(compiler_->GetCallFrameInformationInitialization(*this)); 390 } 391} 392 393std::vector<uint8_t>* CompilerDriver::DeduplicateCode(const std::vector<uint8_t>& code) { 394 return dedupe_code_.Add(Thread::Current(), code); 395} 396 397std::vector<uint8_t>* CompilerDriver::DeduplicateMappingTable(const std::vector<uint8_t>& code) { 398 return dedupe_mapping_table_.Add(Thread::Current(), code); 399} 400 401std::vector<uint8_t>* CompilerDriver::DeduplicateVMapTable(const std::vector<uint8_t>& code) { 402 return dedupe_vmap_table_.Add(Thread::Current(), code); 403} 404 405std::vector<uint8_t>* CompilerDriver::DeduplicateGCMap(const std::vector<uint8_t>& code) { 406 return dedupe_gc_map_.Add(Thread::Current(), code); 407} 408 409std::vector<uint8_t>* CompilerDriver::DeduplicateCFIInfo(const std::vector<uint8_t>* cfi_info) { 410 if (cfi_info == nullptr) { 411 return nullptr; 412 } 413 return dedupe_cfi_info_.Add(Thread::Current(), *cfi_info); 414} 415 416CompilerDriver::~CompilerDriver() { 417 Thread* self = Thread::Current(); 418 { 419 MutexLock mu(self, compiled_classes_lock_); 420 STLDeleteValues(&compiled_classes_); 421 } 422 { 423 MutexLock mu(self, compiled_methods_lock_); 424 STLDeleteValues(&compiled_methods_); 425 } 426 { 427 MutexLock mu(self, compiled_methods_lock_); 428 STLDeleteElements(&code_to_patch_); 429 } 430 { 431 MutexLock mu(self, compiled_methods_lock_); 432 STLDeleteElements(&methods_to_patch_); 433 } 434 { 435 MutexLock mu(self, compiled_methods_lock_); 436 STLDeleteElements(&classes_to_patch_); 437 } 438 CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key"); 439 compiler_->UnInit(*this); 440} 441 442CompilerTls* CompilerDriver::GetTls() { 443 // Lazily create thread-local storage 444 CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_)); 445 if (res == NULL) { 446 res = new CompilerTls(); 447 CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls"); 448 } 449 return res; 450} 451 452#define CREATE_TRAMPOLINE(type, abi, offset) \ 453 if (instruction_set_is_64_bit_) { \ 454 return CreateTrampoline64(instruction_set_, abi, \ 455 type ## _ENTRYPOINT_OFFSET(8, offset)); \ 456 } else { \ 457 return CreateTrampoline32(instruction_set_, abi, \ 458 type ## _ENTRYPOINT_OFFSET(4, offset)); \ 459 } 460 461const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToInterpreterBridge() const { 462 CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToInterpreterBridge) 463} 464 465const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToCompiledCodeBridge() const { 466 CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToCompiledCodeBridge) 467} 468 469const std::vector<uint8_t>* CompilerDriver::CreateJniDlsymLookup() const { 470 CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup) 471} 472 473const std::vector<uint8_t>* CompilerDriver::CreatePortableImtConflictTrampoline() const { 474 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableImtConflictTrampoline) 475} 476 477const std::vector<uint8_t>* CompilerDriver::CreatePortableResolutionTrampoline() const { 478 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableResolutionTrampoline) 479} 480 481const std::vector<uint8_t>* CompilerDriver::CreatePortableToInterpreterBridge() const { 482 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableToInterpreterBridge) 483} 484 485const std::vector<uint8_t>* CompilerDriver::CreateQuickGenericJniTrampoline() const { 486 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline) 487} 488 489const std::vector<uint8_t>* CompilerDriver::CreateQuickImtConflictTrampoline() const { 490 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline) 491} 492 493const std::vector<uint8_t>* CompilerDriver::CreateQuickResolutionTrampoline() const { 494 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline) 495} 496 497const std::vector<uint8_t>* CompilerDriver::CreateQuickToInterpreterBridge() const { 498 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge) 499} 500#undef CREATE_TRAMPOLINE 501 502void CompilerDriver::CompileAll(jobject class_loader, 503 const std::vector<const DexFile*>& dex_files, 504 TimingLogger* timings) { 505 DCHECK(!Runtime::Current()->IsStarted()); 506 UniquePtr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", thread_count_ - 1)); 507 PreCompile(class_loader, dex_files, thread_pool.get(), timings); 508 Compile(class_loader, dex_files, thread_pool.get(), timings); 509 if (dump_stats_) { 510 stats_->Dump(); 511 } 512} 513 514static DexToDexCompilationLevel GetDexToDexCompilationlevel( 515 Thread* self, SirtRef<mirror::ClassLoader>& class_loader, const DexFile& dex_file, 516 const DexFile::ClassDef& class_def) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 517 const char* descriptor = dex_file.GetClassDescriptor(class_def); 518 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 519 mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader); 520 if (klass == NULL) { 521 CHECK(self->IsExceptionPending()); 522 self->ClearException(); 523 return kDontDexToDexCompile; 524 } 525 // The verifier can only run on "quick" instructions at runtime (see usage of 526 // FindAccessedFieldAtDexPc and FindInvokedMethodAtDexPc in ThrowNullPointerExceptionFromDexPC 527 // function). Since image classes can be verified again while compiling an application, 528 // we must prevent the DEX-to-DEX compiler from introducing them. 529 // TODO: find a way to enable "quick" instructions for image classes and remove this check. 530 bool compiling_image_classes = class_loader.get() == nullptr; 531 if (compiling_image_classes) { 532 return kRequired; 533 } else if (klass->IsVerified()) { 534 // Class is verified so we can enable DEX-to-DEX compilation for performance. 535 return kOptimize; 536 } else if (klass->IsCompileTimeVerified()) { 537 // Class verification has soft-failed. Anyway, ensure at least correctness. 538 DCHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); 539 return kRequired; 540 } else { 541 // Class verification has failed: do not run DEX-to-DEX compilation. 542 return kDontDexToDexCompile; 543 } 544} 545 546void CompilerDriver::CompileOne(mirror::ArtMethod* method, TimingLogger* timings) { 547 DCHECK(!Runtime::Current()->IsStarted()); 548 Thread* self = Thread::Current(); 549 jobject jclass_loader; 550 const DexFile* dex_file; 551 uint16_t class_def_idx; 552 uint32_t method_idx = method->GetDexMethodIndex(); 553 uint32_t access_flags = method->GetAccessFlags(); 554 InvokeType invoke_type = method->GetInvokeType(); 555 { 556 ScopedObjectAccessUnchecked soa(self); 557 ScopedLocalRef<jobject> 558 local_class_loader(soa.Env(), 559 soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader())); 560 jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get()); 561 // Find the dex_file 562 MethodHelper mh(method); 563 dex_file = &mh.GetDexFile(); 564 class_def_idx = mh.GetClassDefIndex(); 565 } 566 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 567 self->TransitionFromRunnableToSuspended(kNative); 568 569 std::vector<const DexFile*> dex_files; 570 dex_files.push_back(dex_file); 571 572 UniquePtr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", 0U)); 573 PreCompile(jclass_loader, dex_files, thread_pool.get(), timings); 574 575 // Can we run DEX-to-DEX compiler on this class ? 576 DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile; 577 { 578 ScopedObjectAccess soa(Thread::Current()); 579 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx); 580 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), 581 soa.Decode<mirror::ClassLoader*>(jclass_loader)); 582 dex_to_dex_compilation_level = GetDexToDexCompilationlevel(self, class_loader, *dex_file, 583 class_def); 584 } 585 CompileMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx, jclass_loader, 586 *dex_file, dex_to_dex_compilation_level); 587 588 self->GetJniEnv()->DeleteGlobalRef(jclass_loader); 589 590 self->TransitionFromSuspendedToRunnable(); 591} 592 593void CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files, 594 ThreadPool* thread_pool, TimingLogger* timings) { 595 for (size_t i = 0; i != dex_files.size(); ++i) { 596 const DexFile* dex_file = dex_files[i]; 597 CHECK(dex_file != NULL); 598 ResolveDexFile(class_loader, *dex_file, thread_pool, timings); 599 } 600} 601 602void CompilerDriver::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 603 ThreadPool* thread_pool, TimingLogger* timings) { 604 LoadImageClasses(timings); 605 606 if (!compiler_options_->IsVerificationEnabled()) { 607 VLOG(compiler) << "Verify none mode specified, skipping pre-compilation"; 608 return; 609 } 610 611 Resolve(class_loader, dex_files, thread_pool, timings); 612 613 Verify(class_loader, dex_files, thread_pool, timings); 614 615 InitializeClasses(class_loader, dex_files, thread_pool, timings); 616 617 UpdateImageClasses(timings); 618} 619 620bool CompilerDriver::IsImageClass(const char* descriptor) const { 621 if (!IsImage()) { 622 return true; 623 } else { 624 return image_classes_->find(descriptor) != image_classes_->end(); 625 } 626} 627 628static void ResolveExceptionsForMethod(MethodHelper* mh, 629 std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) 630 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 631 const DexFile::CodeItem* code_item = mh->GetCodeItem(); 632 if (code_item == NULL) { 633 return; // native or abstract method 634 } 635 if (code_item->tries_size_ == 0) { 636 return; // nothing to process 637 } 638 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); 639 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); 640 for (size_t i = 0; i < num_encoded_catch_handlers; i++) { 641 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); 642 bool has_catch_all = false; 643 if (encoded_catch_handler_size <= 0) { 644 encoded_catch_handler_size = -encoded_catch_handler_size; 645 has_catch_all = true; 646 } 647 for (int32_t j = 0; j < encoded_catch_handler_size; j++) { 648 uint16_t encoded_catch_handler_handlers_type_idx = 649 DecodeUnsignedLeb128(&encoded_catch_handler_list); 650 // Add to set of types to resolve if not already in the dex cache resolved types 651 if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { 652 exceptions_to_resolve.insert( 653 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, 654 &mh->GetDexFile())); 655 } 656 // ignore address associated with catch handler 657 DecodeUnsignedLeb128(&encoded_catch_handler_list); 658 } 659 if (has_catch_all) { 660 // ignore catch all address 661 DecodeUnsignedLeb128(&encoded_catch_handler_list); 662 } 663 } 664} 665 666static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg) 667 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 668 std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = 669 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); 670 MethodHelper mh; 671 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { 672 mirror::ArtMethod* m = c->GetVirtualMethod(i); 673 mh.ChangeMethod(m); 674 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); 675 } 676 for (size_t i = 0; i < c->NumDirectMethods(); ++i) { 677 mirror::ArtMethod* m = c->GetDirectMethod(i); 678 mh.ChangeMethod(m); 679 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); 680 } 681 return true; 682} 683 684static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg) 685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 686 CompilerDriver::DescriptorSet* image_classes = 687 reinterpret_cast<CompilerDriver::DescriptorSet*>(arg); 688 image_classes->insert(ClassHelper(klass).GetDescriptor()); 689 return true; 690} 691 692// Make a list of descriptors for classes to include in the image 693void CompilerDriver::LoadImageClasses(TimingLogger* timings) 694 LOCKS_EXCLUDED(Locks::mutator_lock_) { 695 if (!IsImage()) { 696 return; 697 } 698 699 timings->NewSplit("LoadImageClasses"); 700 // Make a first class to load all classes explicitly listed in the file 701 Thread* self = Thread::Current(); 702 ScopedObjectAccess soa(self); 703 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 704 for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) { 705 const std::string& descriptor(*it); 706 SirtRef<mirror::Class> klass(self, class_linker->FindSystemClass(self, descriptor.c_str())); 707 if (klass.get() == NULL) { 708 VLOG(compiler) << "Failed to find class " << descriptor; 709 image_classes_->erase(it++); 710 self->ClearException(); 711 } else { 712 ++it; 713 } 714 } 715 716 // Resolve exception classes referenced by the loaded classes. The catch logic assumes 717 // exceptions are resolved by the verifier when there is a catch block in an interested method. 718 // Do this here so that exception classes appear to have been specified image classes. 719 std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; 720 SirtRef<mirror::Class> java_lang_Throwable(self, 721 class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")); 722 do { 723 unresolved_exception_types.clear(); 724 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, 725 &unresolved_exception_types); 726 for (const std::pair<uint16_t, const DexFile*>& exception_type : unresolved_exception_types) { 727 uint16_t exception_type_idx = exception_type.first; 728 const DexFile* dex_file = exception_type.second; 729 SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(*dex_file)); 730 SirtRef<mirror::ClassLoader> class_loader(self, nullptr); 731 SirtRef<mirror::Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx, 732 dex_cache, class_loader)); 733 if (klass.get() == NULL) { 734 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); 735 const char* descriptor = dex_file->GetTypeDescriptor(type_id); 736 LOG(FATAL) << "Failed to resolve class " << descriptor; 737 } 738 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get())); 739 } 740 // Resolving exceptions may load classes that reference more exceptions, iterate until no 741 // more are found 742 } while (!unresolved_exception_types.empty()); 743 744 // We walk the roots looking for classes so that we'll pick up the 745 // above classes plus any classes them depend on such super 746 // classes, interfaces, and the required ClassLinker roots. 747 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes_.get()); 748 749 CHECK_NE(image_classes_->size(), 0U); 750} 751 752static void MaybeAddToImageClasses(mirror::Class* klass, CompilerDriver::DescriptorSet* image_classes) 753 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 754 while (!klass->IsObjectClass()) { 755 ClassHelper kh(klass); 756 const char* descriptor = kh.GetDescriptor(); 757 std::pair<CompilerDriver::DescriptorSet::iterator, bool> result = 758 image_classes->insert(descriptor); 759 if (result.second) { 760 VLOG(compiler) << "Adding " << descriptor << " to image classes"; 761 } else { 762 return; 763 } 764 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) { 765 MaybeAddToImageClasses(kh.GetDirectInterface(i), image_classes); 766 } 767 if (klass->IsArrayClass()) { 768 MaybeAddToImageClasses(klass->GetComponentType(), image_classes); 769 } 770 klass = klass->GetSuperClass(); 771 } 772} 773 774void CompilerDriver::FindClinitImageClassesCallback(mirror::Object* object, void* arg) { 775 DCHECK(object != NULL); 776 DCHECK(arg != NULL); 777 CompilerDriver* compiler_driver = reinterpret_cast<CompilerDriver*>(arg); 778 MaybeAddToImageClasses(object->GetClass(), compiler_driver->image_classes_.get()); 779} 780 781void CompilerDriver::UpdateImageClasses(TimingLogger* timings) { 782 if (IsImage()) { 783 timings->NewSplit("UpdateImageClasses"); 784 785 // Update image_classes_ with classes for objects created by <clinit> methods. 786 Thread* self = Thread::Current(); 787 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter"); 788 gc::Heap* heap = Runtime::Current()->GetHeap(); 789 // TODO: Image spaces only? 790 ScopedObjectAccess soa(Thread::Current()); 791 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); 792 heap->VisitObjects(FindClinitImageClassesCallback, this); 793 self->EndAssertNoThreadSuspension(old_cause); 794 } 795} 796 797bool CompilerDriver::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx) { 798 if (IsImage() && 799 IsImageClass(dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_))) { 800 if (kIsDebugBuild) { 801 ScopedObjectAccess soa(Thread::Current()); 802 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); 803 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx); 804 CHECK(resolved_class != NULL); 805 } 806 stats_->TypeInDexCache(); 807 return true; 808 } else { 809 stats_->TypeNotInDexCache(); 810 return false; 811 } 812} 813 814bool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file, 815 uint32_t string_idx) { 816 // See also Compiler::ResolveDexFile 817 818 bool result = false; 819 if (IsImage()) { 820 // We resolve all const-string strings when building for the image. 821 ScopedObjectAccess soa(Thread::Current()); 822 SirtRef<mirror::DexCache> dex_cache(soa.Self(), Runtime::Current()->GetClassLinker()->FindDexCache(dex_file)); 823 Runtime::Current()->GetClassLinker()->ResolveString(dex_file, string_idx, dex_cache); 824 result = true; 825 } 826 if (result) { 827 stats_->StringInDexCache(); 828 } else { 829 stats_->StringNotInDexCache(); 830 } 831 return result; 832} 833 834bool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file, 835 uint32_t type_idx, 836 bool* type_known_final, bool* type_known_abstract, 837 bool* equals_referrers_class) { 838 if (type_known_final != NULL) { 839 *type_known_final = false; 840 } 841 if (type_known_abstract != NULL) { 842 *type_known_abstract = false; 843 } 844 if (equals_referrers_class != NULL) { 845 *equals_referrers_class = false; 846 } 847 ScopedObjectAccess soa(Thread::Current()); 848 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); 849 // Get type from dex cache assuming it was populated by the verifier 850 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx); 851 if (resolved_class == NULL) { 852 stats_->TypeNeedsAccessCheck(); 853 return false; // Unknown class needs access checks. 854 } 855 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); 856 if (equals_referrers_class != NULL) { 857 *equals_referrers_class = (method_id.class_idx_ == type_idx); 858 } 859 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); 860 if (referrer_class == NULL) { 861 stats_->TypeNeedsAccessCheck(); 862 return false; // Incomplete referrer knowledge needs access check. 863 } 864 // Perform access check, will return true if access is ok or false if we're going to have to 865 // check this at runtime (for example for class loaders). 866 bool result = referrer_class->CanAccess(resolved_class); 867 if (result) { 868 stats_->TypeDoesntNeedAccessCheck(); 869 if (type_known_final != NULL) { 870 *type_known_final = resolved_class->IsFinal() && !resolved_class->IsArrayClass(); 871 } 872 if (type_known_abstract != NULL) { 873 *type_known_abstract = resolved_class->IsAbstract() && !resolved_class->IsArrayClass(); 874 } 875 } else { 876 stats_->TypeNeedsAccessCheck(); 877 } 878 return result; 879} 880 881bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx, 882 const DexFile& dex_file, 883 uint32_t type_idx) { 884 ScopedObjectAccess soa(Thread::Current()); 885 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); 886 // Get type from dex cache assuming it was populated by the verifier. 887 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx); 888 if (resolved_class == NULL) { 889 stats_->TypeNeedsAccessCheck(); 890 return false; // Unknown class needs access checks. 891 } 892 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx); 893 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_); 894 if (referrer_class == NULL) { 895 stats_->TypeNeedsAccessCheck(); 896 return false; // Incomplete referrer knowledge needs access check. 897 } 898 // Perform access and instantiable checks, will return true if access is ok or false if we're 899 // going to have to check this at runtime (for example for class loaders). 900 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable(); 901 if (result) { 902 stats_->TypeDoesntNeedAccessCheck(); 903 } else { 904 stats_->TypeNeedsAccessCheck(); 905 } 906 return result; 907} 908 909bool CompilerDriver::CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx, 910 bool* is_type_initialized, bool* use_direct_type_ptr, 911 uintptr_t* direct_type_ptr) { 912 ScopedObjectAccess soa(Thread::Current()); 913 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file); 914 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx); 915 if (resolved_class == nullptr) { 916 return false; 917 } 918 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot(); 919 if (compiling_boot) { 920 // boot -> boot class pointers. 921 // True if the class is in the image at boot compiling time. 922 const bool is_image_class = IsImage() && IsImageClass( 923 dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_)); 924 // True if pc relative load works. 925 const bool support_boot_image_fixup = GetSupportBootImageFixup(); 926 if (is_image_class && support_boot_image_fixup) { 927 *is_type_initialized = resolved_class->IsInitialized(); 928 *use_direct_type_ptr = false; 929 *direct_type_ptr = 0; 930 return true; 931 } else { 932 return false; 933 } 934 } else { 935 // True if the class is in the image at app compiling time. 936 const bool class_in_image = 937 Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_class, false)->IsImageSpace(); 938 if (class_in_image) { 939 // boot -> app class pointers. 940 *is_type_initialized = resolved_class->IsInitialized(); 941 *use_direct_type_ptr = true; 942 *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class); 943 return true; 944 } else { 945 // app -> app class pointers. 946 // Give up because app does not have an image and class 947 // isn't created at compile time. TODO: implement this 948 // if/when each app gets an image. 949 return false; 950 } 951 } 952} 953 954void CompilerDriver::ProcessedInstanceField(bool resolved) { 955 if (!resolved) { 956 stats_->UnresolvedInstanceField(); 957 } else { 958 stats_->ResolvedInstanceField(); 959 } 960} 961 962void CompilerDriver::ProcessedStaticField(bool resolved, bool local) { 963 if (!resolved) { 964 stats_->UnresolvedStaticField(); 965 } else if (local) { 966 stats_->ResolvedLocalStaticField(); 967 } else { 968 stats_->ResolvedStaticField(); 969 } 970} 971 972void CompilerDriver::ProcessedInvoke(InvokeType invoke_type, int flags) { 973 stats_->ProcessedInvoke(invoke_type, flags); 974} 975 976bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, 977 bool is_put, MemberOffset* field_offset, 978 bool* is_volatile) { 979 ScopedObjectAccess soa(Thread::Current()); 980 // Try to resolve the field and compiling method's class. 981 mirror::ArtField* resolved_field; 982 mirror::Class* referrer_class; 983 mirror::DexCache* dex_cache; 984 { 985 SirtRef<mirror::DexCache> dex_cache_sirt(soa.Self(), 986 mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())); 987 SirtRef<mirror::ClassLoader> class_loader_sirt(soa.Self(), 988 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); 989 SirtRef<mirror::ArtField> resolved_field_sirt(soa.Self(), 990 ResolveField(soa, dex_cache_sirt, class_loader_sirt, mUnit, field_idx, false)); 991 referrer_class = (resolved_field_sirt.get() != nullptr) 992 ? ResolveCompilingMethodsClass(soa, dex_cache_sirt, class_loader_sirt, mUnit) : nullptr; 993 resolved_field = resolved_field_sirt.get(); 994 dex_cache = dex_cache_sirt.get(); 995 } 996 bool result = false; 997 if (resolved_field != nullptr && referrer_class != nullptr) { 998 *is_volatile = IsFieldVolatile(resolved_field); 999 std::pair<bool, bool> fast_path = IsFastInstanceField( 1000 dex_cache, referrer_class, resolved_field, field_idx, field_offset); 1001 result = is_put ? fast_path.second : fast_path.first; 1002 } 1003 if (!result) { 1004 // Conservative defaults. 1005 *is_volatile = true; 1006 *field_offset = MemberOffset(static_cast<size_t>(-1)); 1007 } 1008 ProcessedInstanceField(result); 1009 return result; 1010} 1011 1012bool CompilerDriver::ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, 1013 bool is_put, MemberOffset* field_offset, 1014 uint32_t* storage_index, bool* is_referrers_class, 1015 bool* is_volatile, bool* is_initialized) { 1016 ScopedObjectAccess soa(Thread::Current()); 1017 // Try to resolve the field and compiling method's class. 1018 mirror::ArtField* resolved_field; 1019 mirror::Class* referrer_class; 1020 mirror::DexCache* dex_cache; 1021 { 1022 SirtRef<mirror::DexCache> dex_cache_sirt(soa.Self(), 1023 mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())); 1024 SirtRef<mirror::ClassLoader> class_loader_sirt(soa.Self(), 1025 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); 1026 SirtRef<mirror::ArtField> resolved_field_sirt(soa.Self(), 1027 ResolveField(soa, dex_cache_sirt, class_loader_sirt, mUnit, field_idx, true)); 1028 referrer_class = (resolved_field_sirt.get() != nullptr) 1029 ? ResolveCompilingMethodsClass(soa, dex_cache_sirt, class_loader_sirt, mUnit) : nullptr; 1030 resolved_field = resolved_field_sirt.get(); 1031 dex_cache = dex_cache_sirt.get(); 1032 } 1033 bool result = false; 1034 if (resolved_field != nullptr && referrer_class != nullptr) { 1035 *is_volatile = IsFieldVolatile(resolved_field); 1036 std::pair<bool, bool> fast_path = IsFastStaticField( 1037 dex_cache, referrer_class, resolved_field, field_idx, field_offset, 1038 storage_index, is_referrers_class, is_initialized); 1039 result = is_put ? fast_path.second : fast_path.first; 1040 } 1041 if (!result) { 1042 // Conservative defaults. 1043 *is_volatile = true; 1044 *field_offset = MemberOffset(static_cast<size_t>(-1)); 1045 *storage_index = -1; 1046 *is_referrers_class = false; 1047 *is_initialized = false; 1048 } 1049 ProcessedStaticField(result, *is_referrers_class); 1050 return result; 1051} 1052 1053void CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type, 1054 bool no_guarantee_of_dex_cache_entry, 1055 mirror::Class* referrer_class, 1056 mirror::ArtMethod* method, 1057 int* stats_flags, 1058 MethodReference* target_method, 1059 uintptr_t* direct_code, 1060 uintptr_t* direct_method) { 1061 // For direct and static methods compute possible direct_code and direct_method values, ie 1062 // an address for the Method* being invoked and an address of the code for that Method*. 1063 // For interface calls compute a value for direct_method that is the interface method being 1064 // invoked, so this can be passed to the out-of-line runtime support code. 1065 *direct_code = 0; 1066 *direct_method = 0; 1067 bool use_dex_cache = false; 1068 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot(); 1069 if (compiler_->IsPortable()) { 1070 if (sharp_type != kStatic && sharp_type != kDirect) { 1071 return; 1072 } 1073 use_dex_cache = true; 1074 } else { 1075 if (sharp_type != kStatic && sharp_type != kDirect) { 1076 return; 1077 } 1078 // TODO: support patching on all architectures. 1079 use_dex_cache = compiling_boot && !support_boot_image_fixup_; 1080 } 1081 bool method_code_in_boot = (method->GetDeclaringClass()->GetClassLoader() == nullptr); 1082 if (!use_dex_cache) { 1083 if (!method_code_in_boot) { 1084 use_dex_cache = true; 1085 } else { 1086 bool has_clinit_trampoline = 1087 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized(); 1088 if (has_clinit_trampoline && (method->GetDeclaringClass() != referrer_class)) { 1089 // Ensure we run the clinit trampoline unless we are invoking a static method in the same 1090 // class. 1091 use_dex_cache = true; 1092 } 1093 } 1094 } 1095 if (method_code_in_boot) { 1096 *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot; 1097 } 1098 if (!use_dex_cache && compiling_boot) { 1099 MethodHelper mh(method); 1100 if (!IsImageClass(mh.GetDeclaringClassDescriptor())) { 1101 // We can only branch directly to Methods that are resolved in the DexCache. 1102 // Otherwise we won't invoke the resolution trampoline. 1103 use_dex_cache = true; 1104 } 1105 } 1106 // The method is defined not within this dex file. We need a dex cache slot within the current 1107 // dex file or direct pointers. 1108 bool must_use_direct_pointers = false; 1109 if (target_method->dex_file == method->GetDeclaringClass()->GetDexCache()->GetDexFile()) { 1110 target_method->dex_method_index = method->GetDexMethodIndex(); 1111 } else { 1112 if (no_guarantee_of_dex_cache_entry) { 1113 // See if the method is also declared in this dex cache. 1114 uint32_t dex_method_idx = MethodHelper(method).FindDexMethodIndexInOtherDexFile( 1115 *target_method->dex_file, target_method->dex_method_index); 1116 if (dex_method_idx != DexFile::kDexNoIndex) { 1117 target_method->dex_method_index = dex_method_idx; 1118 } else { 1119 if (compiling_boot) { 1120 target_method->dex_method_index = method->GetDexMethodIndex(); 1121 target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 1122 } 1123 must_use_direct_pointers = true; 1124 } 1125 } 1126 } 1127 if (use_dex_cache) { 1128 if (must_use_direct_pointers) { 1129 // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct 1130 // pointers are required as the dex cache lacks an appropriate entry. 1131 VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method); 1132 } else { 1133 *type = sharp_type; 1134 } 1135 } else { 1136 if (compiling_boot) { 1137 *type = sharp_type; 1138 *direct_method = -1; 1139 *direct_code = -1; 1140 } else { 1141 bool method_in_image = 1142 Runtime::Current()->GetHeap()->FindSpaceFromObject(method, false)->IsImageSpace(); 1143 if (method_in_image) { 1144 CHECK(!method->IsAbstract()); 1145 *type = sharp_type; 1146 *direct_method = reinterpret_cast<uintptr_t>(method); 1147 *direct_code = compiler_->GetEntryPointOf(method); 1148 target_method->dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 1149 target_method->dex_method_index = method->GetDexMethodIndex(); 1150 } else if (!must_use_direct_pointers) { 1151 // Set the code and rely on the dex cache for the method. 1152 *type = sharp_type; 1153 *direct_code = compiler_->GetEntryPointOf(method); 1154 } else { 1155 // Direct pointers were required but none were available. 1156 VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method); 1157 } 1158 } 1159 } 1160} 1161 1162bool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc, 1163 bool update_stats, bool enable_devirtualization, 1164 InvokeType* invoke_type, MethodReference* target_method, 1165 int* vtable_idx, uintptr_t* direct_code, 1166 uintptr_t* direct_method) { 1167 InvokeType orig_invoke_type = *invoke_type; 1168 int stats_flags = 0; 1169 ScopedObjectAccess soa(Thread::Current()); 1170 // Try to resolve the method and compiling method's class. 1171 mirror::ArtMethod* resolved_method; 1172 mirror::Class* referrer_class; 1173 SirtRef<mirror::DexCache> dex_cache(soa.Self(), 1174 mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())); 1175 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), 1176 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); 1177 { 1178 uint32_t method_idx = target_method->dex_method_index; 1179 SirtRef<mirror::ArtMethod> resolved_method_sirt(soa.Self(), 1180 ResolveMethod(soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type)); 1181 referrer_class = (resolved_method_sirt.get() != nullptr) 1182 ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr; 1183 resolved_method = resolved_method_sirt.get(); 1184 } 1185 bool result = false; 1186 if (resolved_method != nullptr) { 1187 *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type); 1188 1189 if (enable_devirtualization) { 1190 DCHECK(mUnit->GetVerifiedMethod() != nullptr); 1191 const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc); 1192 1193 stats_flags = IsFastInvoke( 1194 soa, dex_cache, class_loader, mUnit, referrer_class, resolved_method, 1195 invoke_type, target_method, devirt_target, direct_code, direct_method); 1196 result = stats_flags != 0; 1197 } else { 1198 // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts. 1199 if (UNLIKELY(referrer_class == nullptr) || 1200 UNLIKELY(!referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(), 1201 resolved_method, dex_cache.get(), 1202 target_method->dex_method_index)) || 1203 *invoke_type == kSuper) { 1204 // Slow path. (Without devirtualization, all super calls go slow path as well.) 1205 } else { 1206 // Sharpening failed so generate a regular resolved method dispatch. 1207 stats_flags = kFlagMethodResolved; 1208 GetCodeAndMethodForDirectCall(invoke_type, *invoke_type, false, referrer_class, resolved_method, 1209 &stats_flags, target_method, direct_code, direct_method); 1210 result = true; 1211 } 1212 } 1213 } 1214 if (!result) { 1215 // Conservative defaults. 1216 *vtable_idx = -1; 1217 *direct_code = 0u; 1218 *direct_method = 0u; 1219 } 1220 if (update_stats) { 1221 ProcessedInvoke(orig_invoke_type, stats_flags); 1222 } 1223 return result; 1224} 1225 1226const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file, 1227 uint32_t method_idx) const { 1228 MethodReference ref(dex_file, method_idx); 1229 return verification_results_->GetVerifiedMethod(ref); 1230} 1231 1232bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) { 1233 DCHECK(mUnit->GetVerifiedMethod() != nullptr); 1234 bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc); 1235 if (result) { 1236 stats_->SafeCast(); 1237 } else { 1238 stats_->NotASafeCast(); 1239 } 1240 return result; 1241} 1242 1243void CompilerDriver::AddCodePatch(const DexFile* dex_file, 1244 uint16_t referrer_class_def_idx, 1245 uint32_t referrer_method_idx, 1246 InvokeType referrer_invoke_type, 1247 uint32_t target_method_idx, 1248 const DexFile* target_dex_file, 1249 InvokeType target_invoke_type, 1250 size_t literal_offset) { 1251 MutexLock mu(Thread::Current(), compiled_methods_lock_); 1252 code_to_patch_.push_back(new CallPatchInformation(dex_file, 1253 referrer_class_def_idx, 1254 referrer_method_idx, 1255 referrer_invoke_type, 1256 target_method_idx, 1257 target_dex_file, 1258 target_invoke_type, 1259 literal_offset)); 1260} 1261void CompilerDriver::AddRelativeCodePatch(const DexFile* dex_file, 1262 uint16_t referrer_class_def_idx, 1263 uint32_t referrer_method_idx, 1264 InvokeType referrer_invoke_type, 1265 uint32_t target_method_idx, 1266 const DexFile* target_dex_file, 1267 InvokeType target_invoke_type, 1268 size_t literal_offset, 1269 int32_t pc_relative_offset) { 1270 MutexLock mu(Thread::Current(), compiled_methods_lock_); 1271 code_to_patch_.push_back(new RelativeCallPatchInformation(dex_file, 1272 referrer_class_def_idx, 1273 referrer_method_idx, 1274 referrer_invoke_type, 1275 target_method_idx, 1276 target_dex_file, 1277 target_invoke_type, 1278 literal_offset, 1279 pc_relative_offset)); 1280} 1281void CompilerDriver::AddMethodPatch(const DexFile* dex_file, 1282 uint16_t referrer_class_def_idx, 1283 uint32_t referrer_method_idx, 1284 InvokeType referrer_invoke_type, 1285 uint32_t target_method_idx, 1286 const DexFile* target_dex_file, 1287 InvokeType target_invoke_type, 1288 size_t literal_offset) { 1289 MutexLock mu(Thread::Current(), compiled_methods_lock_); 1290 methods_to_patch_.push_back(new CallPatchInformation(dex_file, 1291 referrer_class_def_idx, 1292 referrer_method_idx, 1293 referrer_invoke_type, 1294 target_method_idx, 1295 target_dex_file, 1296 target_invoke_type, 1297 literal_offset)); 1298} 1299void CompilerDriver::AddClassPatch(const DexFile* dex_file, 1300 uint16_t referrer_class_def_idx, 1301 uint32_t referrer_method_idx, 1302 uint32_t target_type_idx, 1303 size_t literal_offset) { 1304 MutexLock mu(Thread::Current(), compiled_methods_lock_); 1305 classes_to_patch_.push_back(new TypePatchInformation(dex_file, 1306 referrer_class_def_idx, 1307 referrer_method_idx, 1308 target_type_idx, 1309 literal_offset)); 1310} 1311 1312class ParallelCompilationManager { 1313 public: 1314 typedef void Callback(const ParallelCompilationManager* manager, size_t index); 1315 1316 ParallelCompilationManager(ClassLinker* class_linker, 1317 jobject class_loader, 1318 CompilerDriver* compiler, 1319 const DexFile* dex_file, 1320 ThreadPool* thread_pool) 1321 : index_(0), 1322 class_linker_(class_linker), 1323 class_loader_(class_loader), 1324 compiler_(compiler), 1325 dex_file_(dex_file), 1326 thread_pool_(thread_pool) {} 1327 1328 ClassLinker* GetClassLinker() const { 1329 CHECK(class_linker_ != NULL); 1330 return class_linker_; 1331 } 1332 1333 jobject GetClassLoader() const { 1334 return class_loader_; 1335 } 1336 1337 CompilerDriver* GetCompiler() const { 1338 CHECK(compiler_ != NULL); 1339 return compiler_; 1340 } 1341 1342 const DexFile* GetDexFile() const { 1343 CHECK(dex_file_ != NULL); 1344 return dex_file_; 1345 } 1346 1347 void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) { 1348 Thread* self = Thread::Current(); 1349 self->AssertNoPendingException(); 1350 CHECK_GT(work_units, 0U); 1351 1352 index_ = begin; 1353 for (size_t i = 0; i < work_units; ++i) { 1354 thread_pool_->AddTask(self, new ForAllClosure(this, end, callback)); 1355 } 1356 thread_pool_->StartWorkers(self); 1357 1358 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker 1359 // thread destructor's called below perform join). 1360 CHECK_NE(self->GetState(), kRunnable); 1361 1362 // Wait for all the worker threads to finish. 1363 thread_pool_->Wait(self, true, false); 1364 } 1365 1366 size_t NextIndex() { 1367 return index_.FetchAndAdd(1); 1368 } 1369 1370 private: 1371 class ForAllClosure : public Task { 1372 public: 1373 ForAllClosure(ParallelCompilationManager* manager, size_t end, Callback* callback) 1374 : manager_(manager), 1375 end_(end), 1376 callback_(callback) {} 1377 1378 virtual void Run(Thread* self) { 1379 while (true) { 1380 const size_t index = manager_->NextIndex(); 1381 if (UNLIKELY(index >= end_)) { 1382 break; 1383 } 1384 callback_(manager_, index); 1385 self->AssertNoPendingException(); 1386 } 1387 } 1388 1389 virtual void Finalize() { 1390 delete this; 1391 } 1392 1393 private: 1394 ParallelCompilationManager* const manager_; 1395 const size_t end_; 1396 Callback* const callback_; 1397 }; 1398 1399 AtomicInteger index_; 1400 ClassLinker* const class_linker_; 1401 const jobject class_loader_; 1402 CompilerDriver* const compiler_; 1403 const DexFile* const dex_file_; 1404 ThreadPool* const thread_pool_; 1405 1406 DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager); 1407}; 1408 1409// Return true if the class should be skipped during compilation. 1410// 1411// The first case where we skip is for redundant class definitions in 1412// the boot classpath. We skip all but the first definition in that case. 1413// 1414// The second case where we skip is when an app bundles classes found 1415// in the boot classpath. Since at runtime we will select the class from 1416// the boot classpath, we ignore the one from the app. 1417static bool SkipClass(ClassLinker* class_linker, jobject class_loader, const DexFile& dex_file, 1418 const DexFile::ClassDef& class_def) { 1419 const char* descriptor = dex_file.GetClassDescriptor(class_def); 1420 if (class_loader == NULL) { 1421 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_linker->GetBootClassPath()); 1422 CHECK(pair.second != NULL); 1423 if (pair.first != &dex_file) { 1424 LOG(WARNING) << "Skipping class " << descriptor << " from " << dex_file.GetLocation() 1425 << " previously found in " << pair.first->GetLocation(); 1426 return true; 1427 } 1428 return false; 1429 } 1430 return class_linker->IsInBootClassPath(descriptor); 1431} 1432 1433// A fast version of SkipClass above if the class pointer is available 1434// that avoids the expensive FindInClassPath search. 1435static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass) 1436 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1437 DCHECK(klass != NULL); 1438 const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile(); 1439 if (&dex_file != &original_dex_file) { 1440 if (class_loader == NULL) { 1441 LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from " 1442 << dex_file.GetLocation() << " previously found in " 1443 << original_dex_file.GetLocation(); 1444 } 1445 return true; 1446 } 1447 return false; 1448} 1449 1450static void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager, 1451 size_t class_def_index) 1452 LOCKS_EXCLUDED(Locks::mutator_lock_) { 1453 ATRACE_CALL(); 1454 Thread* self = Thread::Current(); 1455 jobject jclass_loader = manager->GetClassLoader(); 1456 const DexFile& dex_file = *manager->GetDexFile(); 1457 ClassLinker* class_linker = manager->GetClassLinker(); 1458 1459 // If an instance field is final then we need to have a barrier on the return, static final 1460 // fields are assigned within the lock held for class initialization. Conservatively assume 1461 // constructor barriers are always required. 1462 bool requires_constructor_barrier = true; 1463 1464 // Method and Field are the worst. We can't resolve without either 1465 // context from the code use (to disambiguate virtual vs direct 1466 // method and instance vs static field) or from class 1467 // definitions. While the compiler will resolve what it can as it 1468 // needs it, here we try to resolve fields and methods used in class 1469 // definitions, since many of them many never be referenced by 1470 // generated code. 1471 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 1472 if (!SkipClass(class_linker, jclass_loader, dex_file, class_def)) { 1473 ScopedObjectAccess soa(self); 1474 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader)); 1475 SirtRef<mirror::DexCache> dex_cache(soa.Self(), class_linker->FindDexCache(dex_file)); 1476 // Resolve the class. 1477 mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache, 1478 class_loader); 1479 bool resolve_fields_and_methods; 1480 if (klass == NULL) { 1481 // Class couldn't be resolved, for example, super-class is in a different dex file. Don't 1482 // attempt to resolve methods and fields when there is no declaring class. 1483 CHECK(soa.Self()->IsExceptionPending()); 1484 soa.Self()->ClearException(); 1485 resolve_fields_and_methods = false; 1486 } else { 1487 resolve_fields_and_methods = manager->GetCompiler()->IsImage(); 1488 } 1489 // Note the class_data pointer advances through the headers, 1490 // static fields, instance fields, direct methods, and virtual 1491 // methods. 1492 const byte* class_data = dex_file.GetClassData(class_def); 1493 if (class_data == NULL) { 1494 // Empty class such as a marker interface. 1495 requires_constructor_barrier = false; 1496 } else { 1497 ClassDataItemIterator it(dex_file, class_data); 1498 while (it.HasNextStaticField()) { 1499 if (resolve_fields_and_methods) { 1500 mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), 1501 dex_cache, class_loader, true); 1502 if (field == NULL) { 1503 CHECK(soa.Self()->IsExceptionPending()); 1504 soa.Self()->ClearException(); 1505 } 1506 } 1507 it.Next(); 1508 } 1509 // We require a constructor barrier if there are final instance fields. 1510 requires_constructor_barrier = false; 1511 while (it.HasNextInstanceField()) { 1512 if ((it.GetMemberAccessFlags() & kAccFinal) != 0) { 1513 requires_constructor_barrier = true; 1514 } 1515 if (resolve_fields_and_methods) { 1516 mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), 1517 dex_cache, class_loader, false); 1518 if (field == NULL) { 1519 CHECK(soa.Self()->IsExceptionPending()); 1520 soa.Self()->ClearException(); 1521 } 1522 } 1523 it.Next(); 1524 } 1525 if (resolve_fields_and_methods) { 1526 while (it.HasNextDirectMethod()) { 1527 mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), 1528 dex_cache, class_loader, NULL, 1529 it.GetMethodInvokeType(class_def)); 1530 if (method == NULL) { 1531 CHECK(soa.Self()->IsExceptionPending()); 1532 soa.Self()->ClearException(); 1533 } 1534 it.Next(); 1535 } 1536 while (it.HasNextVirtualMethod()) { 1537 mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(), 1538 dex_cache, class_loader, NULL, 1539 it.GetMethodInvokeType(class_def)); 1540 if (method == NULL) { 1541 CHECK(soa.Self()->IsExceptionPending()); 1542 soa.Self()->ClearException(); 1543 } 1544 it.Next(); 1545 } 1546 DCHECK(!it.HasNext()); 1547 } 1548 } 1549 } 1550 if (requires_constructor_barrier) { 1551 manager->GetCompiler()->AddRequiresConstructorBarrier(self, &dex_file, class_def_index); 1552 } 1553} 1554 1555static void ResolveType(const ParallelCompilationManager* manager, size_t type_idx) 1556 LOCKS_EXCLUDED(Locks::mutator_lock_) { 1557 // Class derived values are more complicated, they require the linker and loader. 1558 ScopedObjectAccess soa(Thread::Current()); 1559 ClassLinker* class_linker = manager->GetClassLinker(); 1560 const DexFile& dex_file = *manager->GetDexFile(); 1561 SirtRef<mirror::DexCache> dex_cache(soa.Self(), class_linker->FindDexCache(dex_file)); 1562 SirtRef<mirror::ClassLoader> class_loader( 1563 soa.Self(), soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader())); 1564 mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader); 1565 1566 if (klass == NULL) { 1567 CHECK(soa.Self()->IsExceptionPending()); 1568 mirror::Throwable* exception = soa.Self()->GetException(NULL); 1569 VLOG(compiler) << "Exception during type resolution: " << exception->Dump(); 1570 if (strcmp("Ljava/lang/OutOfMemoryError;", 1571 ClassHelper(exception->GetClass()).GetDescriptor()) == 0) { 1572 // There's little point continuing compilation if the heap is exhausted. 1573 LOG(FATAL) << "Out of memory during type resolution for compilation"; 1574 } 1575 soa.Self()->ClearException(); 1576 } 1577} 1578 1579void CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file, 1580 ThreadPool* thread_pool, TimingLogger* timings) { 1581 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1582 1583 // TODO: we could resolve strings here, although the string table is largely filled with class 1584 // and method names. 1585 1586 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool); 1587 if (IsImage()) { 1588 // For images we resolve all types, such as array, whereas for applications just those with 1589 // classdefs are resolved by ResolveClassFieldsAndMethods. 1590 timings->NewSplit("Resolve Types"); 1591 context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_); 1592 } 1593 1594 timings->NewSplit("Resolve MethodsAndFields"); 1595 context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_); 1596} 1597 1598void CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files, 1599 ThreadPool* thread_pool, TimingLogger* timings) { 1600 for (size_t i = 0; i != dex_files.size(); ++i) { 1601 const DexFile* dex_file = dex_files[i]; 1602 CHECK(dex_file != NULL); 1603 VerifyDexFile(class_loader, *dex_file, thread_pool, timings); 1604 } 1605} 1606 1607static void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index) 1608 LOCKS_EXCLUDED(Locks::mutator_lock_) { 1609 ATRACE_CALL(); 1610 ScopedObjectAccess soa(Thread::Current()); 1611 const DexFile& dex_file = *manager->GetDexFile(); 1612 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 1613 const char* descriptor = dex_file.GetClassDescriptor(class_def); 1614 ClassLinker* class_linker = manager->GetClassLinker(); 1615 jobject jclass_loader = manager->GetClassLoader(); 1616 SirtRef<mirror::ClassLoader> class_loader( 1617 soa.Self(), soa.Decode<mirror::ClassLoader*>(jclass_loader)); 1618 SirtRef<mirror::Class> klass(soa.Self(), class_linker->FindClass(soa.Self(), descriptor, 1619 class_loader)); 1620 if (klass.get() == nullptr) { 1621 CHECK(soa.Self()->IsExceptionPending()); 1622 soa.Self()->ClearException(); 1623 1624 /* 1625 * At compile time, we can still structurally verify the class even if FindClass fails. 1626 * This is to ensure the class is structurally sound for compilation. An unsound class 1627 * will be rejected by the verifier and later skipped during compilation in the compiler. 1628 */ 1629 SirtRef<mirror::DexCache> dex_cache(soa.Self(), class_linker->FindDexCache(dex_file)); 1630 std::string error_msg; 1631 if (verifier::MethodVerifier::VerifyClass(&dex_file, dex_cache, class_loader, &class_def, true, 1632 &error_msg) == 1633 verifier::MethodVerifier::kHardFailure) { 1634 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor) 1635 << " because: " << error_msg; 1636 } 1637 } else if (!SkipClass(jclass_loader, dex_file, klass.get())) { 1638 CHECK(klass->IsResolved()) << PrettyClass(klass.get()); 1639 class_linker->VerifyClass(klass); 1640 1641 if (klass->IsErroneous()) { 1642 // ClassLinker::VerifyClass throws, which isn't useful in the compiler. 1643 CHECK(soa.Self()->IsExceptionPending()); 1644 soa.Self()->ClearException(); 1645 } 1646 1647 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous()) 1648 << PrettyDescriptor(klass.get()) << ": state=" << klass->GetStatus(); 1649 } 1650 soa.Self()->AssertNoPendingException(); 1651} 1652 1653void CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file, 1654 ThreadPool* thread_pool, TimingLogger* timings) { 1655 timings->NewSplit("Verify Dex File"); 1656 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1657 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool); 1658 context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_); 1659} 1660 1661static void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index) 1662 LOCKS_EXCLUDED(Locks::mutator_lock_) { 1663 ATRACE_CALL(); 1664 jobject jclass_loader = manager->GetClassLoader(); 1665 const DexFile& dex_file = *manager->GetDexFile(); 1666 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 1667 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_); 1668 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_); 1669 1670 ScopedObjectAccess soa(Thread::Current()); 1671 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), 1672 soa.Decode<mirror::ClassLoader*>(jclass_loader)); 1673 SirtRef<mirror::Class> klass(soa.Self(), 1674 manager->GetClassLinker()->FindClass(soa.Self(), descriptor, 1675 class_loader)); 1676 1677 if (klass.get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.get())) { 1678 // Only try to initialize classes that were successfully verified. 1679 if (klass->IsVerified()) { 1680 // Attempt to initialize the class but bail if we either need to initialize the super-class 1681 // or static fields. 1682 manager->GetClassLinker()->EnsureInitialized(klass, false, false); 1683 if (!klass->IsInitialized()) { 1684 // We don't want non-trivial class initialization occurring on multiple threads due to 1685 // deadlock problems. For example, a parent class is initialized (holding its lock) that 1686 // refers to a sub-class in its static/class initializer causing it to try to acquire the 1687 // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock) 1688 // after first initializing its parents, whose locks are acquired. This leads to a 1689 // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock. 1690 // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather 1691 // than use a special Object for the purpose we use the Class of java.lang.Class. 1692 SirtRef<mirror::Class> sirt_klass(soa.Self(), klass->GetClass()); 1693 ObjectLock<mirror::Class> lock(soa.Self(), &sirt_klass); 1694 // Attempt to initialize allowing initialization of parent classes but still not static 1695 // fields. 1696 manager->GetClassLinker()->EnsureInitialized(klass, false, true); 1697 if (!klass->IsInitialized()) { 1698 // We need to initialize static fields, we only do this for image classes that aren't 1699 // marked with the $NoPreloadHolder (which implies this should not be initialized early). 1700 bool can_init_static_fields = manager->GetCompiler()->IsImage() && 1701 manager->GetCompiler()->IsImageClass(descriptor) && 1702 !StringPiece(descriptor).ends_with("$NoPreloadHolder;"); 1703 if (can_init_static_fields) { 1704 VLOG(compiler) << "Initializing: " << descriptor; 1705 if (strcmp("Ljava/lang/Void;", descriptor) == 0) { 1706 // Hand initialize j.l.Void to avoid Dex file operations in un-started runtime. 1707 ObjectLock<mirror::Class> lock(soa.Self(), &klass); 1708 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields(); 1709 CHECK_EQ(fields->GetLength(), 1); 1710 fields->Get(0)->SetObj<false>(klass.get(), 1711 manager->GetClassLinker()->FindPrimitiveClass('V')); 1712 klass->SetStatus(mirror::Class::kStatusInitialized, soa.Self()); 1713 } else { 1714 // TODO multithreading support. We should ensure the current compilation thread has 1715 // exclusive access to the runtime and the transaction. To achieve this, we could use 1716 // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity 1717 // checks in Thread::AssertThreadSuspensionIsAllowable. 1718 Runtime* const runtime = Runtime::Current(); 1719 Transaction transaction; 1720 1721 // Run the class initializer in transaction mode. 1722 runtime->EnterTransactionMode(&transaction); 1723 const mirror::Class::Status old_status = klass->GetStatus(); 1724 bool success = manager->GetClassLinker()->EnsureInitialized(klass, true, true); 1725 // TODO we detach transaction from runtime to indicate we quit the transactional 1726 // mode which prevents the GC from visiting objects modified during the transaction. 1727 // Ensure GC is not run so don't access freed objects when aborting transaction. 1728 const char* old_casue = soa.Self()->StartAssertNoThreadSuspension("Transaction end"); 1729 runtime->ExitTransactionMode(); 1730 1731 if (!success) { 1732 CHECK(soa.Self()->IsExceptionPending()); 1733 ThrowLocation throw_location; 1734 mirror::Throwable* exception = soa.Self()->GetException(&throw_location); 1735 VLOG(compiler) << "Initialization of " << descriptor << " aborted because of " 1736 << exception->Dump(); 1737 soa.Self()->ClearException(); 1738 transaction.Abort(); 1739 CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored"; 1740 } 1741 soa.Self()->EndAssertNoThreadSuspension(old_casue); 1742 } 1743 } 1744 } 1745 soa.Self()->AssertNoPendingException(); 1746 } 1747 } 1748 // Record the final class status if necessary. 1749 ClassReference ref(manager->GetDexFile(), class_def_index); 1750 manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus()); 1751 } 1752 // Clear any class not found or verification exceptions. 1753 soa.Self()->ClearException(); 1754} 1755 1756void CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file, 1757 ThreadPool* thread_pool, TimingLogger* timings) { 1758 timings->NewSplit("InitializeNoClinit"); 1759 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1760 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, thread_pool); 1761 size_t thread_count; 1762 if (IsImage()) { 1763 // TODO: remove this when transactional mode supports multithreading. 1764 thread_count = 1U; 1765 } else { 1766 thread_count = thread_count_; 1767 } 1768 context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count); 1769 if (IsImage()) { 1770 // Prune garbage objects created during aborted transactions. 1771 Runtime::Current()->GetHeap()->CollectGarbage(true); 1772 } 1773} 1774 1775void CompilerDriver::InitializeClasses(jobject class_loader, 1776 const std::vector<const DexFile*>& dex_files, 1777 ThreadPool* thread_pool, TimingLogger* timings) { 1778 for (size_t i = 0; i != dex_files.size(); ++i) { 1779 const DexFile* dex_file = dex_files[i]; 1780 CHECK(dex_file != NULL); 1781 InitializeClasses(class_loader, *dex_file, thread_pool, timings); 1782 } 1783} 1784 1785void CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files, 1786 ThreadPool* thread_pool, TimingLogger* timings) { 1787 for (size_t i = 0; i != dex_files.size(); ++i) { 1788 const DexFile* dex_file = dex_files[i]; 1789 CHECK(dex_file != NULL); 1790 CompileDexFile(class_loader, *dex_file, thread_pool, timings); 1791 } 1792} 1793 1794void CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) { 1795 ATRACE_CALL(); 1796 jobject jclass_loader = manager->GetClassLoader(); 1797 const DexFile& dex_file = *manager->GetDexFile(); 1798 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 1799 ClassLinker* class_linker = manager->GetClassLinker(); 1800 if (SkipClass(class_linker, jclass_loader, dex_file, class_def)) { 1801 return; 1802 } 1803 ClassReference ref(&dex_file, class_def_index); 1804 // Skip compiling classes with generic verifier failures since they will still fail at runtime 1805 if (manager->GetCompiler()->verification_results_->IsClassRejected(ref)) { 1806 return; 1807 } 1808 const byte* class_data = dex_file.GetClassData(class_def); 1809 if (class_data == NULL) { 1810 // empty class, probably a marker interface 1811 return; 1812 } 1813 1814 // Can we run DEX-to-DEX compiler on this class ? 1815 DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile; 1816 { 1817 ScopedObjectAccess soa(Thread::Current()); 1818 SirtRef<mirror::ClassLoader> class_loader(soa.Self(), 1819 soa.Decode<mirror::ClassLoader*>(jclass_loader)); 1820 dex_to_dex_compilation_level = GetDexToDexCompilationlevel(soa.Self(), class_loader, dex_file, 1821 class_def); 1822 } 1823 ClassDataItemIterator it(dex_file, class_data); 1824 // Skip fields 1825 while (it.HasNextStaticField()) { 1826 it.Next(); 1827 } 1828 while (it.HasNextInstanceField()) { 1829 it.Next(); 1830 } 1831 CompilerDriver* driver = manager->GetCompiler(); 1832 // Compile direct methods 1833 int64_t previous_direct_method_idx = -1; 1834 while (it.HasNextDirectMethod()) { 1835 uint32_t method_idx = it.GetMemberIndex(); 1836 if (method_idx == previous_direct_method_idx) { 1837 // smali can create dex files with two encoded_methods sharing the same method_idx 1838 // http://code.google.com/p/smali/issues/detail?id=119 1839 it.Next(); 1840 continue; 1841 } 1842 previous_direct_method_idx = method_idx; 1843 driver->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), 1844 it.GetMethodInvokeType(class_def), class_def_index, 1845 method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level); 1846 it.Next(); 1847 } 1848 // Compile virtual methods 1849 int64_t previous_virtual_method_idx = -1; 1850 while (it.HasNextVirtualMethod()) { 1851 uint32_t method_idx = it.GetMemberIndex(); 1852 if (method_idx == previous_virtual_method_idx) { 1853 // smali can create dex files with two encoded_methods sharing the same method_idx 1854 // http://code.google.com/p/smali/issues/detail?id=119 1855 it.Next(); 1856 continue; 1857 } 1858 previous_virtual_method_idx = method_idx; 1859 driver->CompileMethod(it.GetMethodCodeItem(), it.GetMemberAccessFlags(), 1860 it.GetMethodInvokeType(class_def), class_def_index, 1861 method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level); 1862 it.Next(); 1863 } 1864 DCHECK(!it.HasNext()); 1865} 1866 1867void CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file, 1868 ThreadPool* thread_pool, TimingLogger* timings) { 1869 timings->NewSplit("Compile Dex File"); 1870 ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this, 1871 &dex_file, thread_pool); 1872 context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_); 1873} 1874 1875void CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags, 1876 InvokeType invoke_type, uint16_t class_def_idx, 1877 uint32_t method_idx, jobject class_loader, 1878 const DexFile& dex_file, 1879 DexToDexCompilationLevel dex_to_dex_compilation_level) { 1880 CompiledMethod* compiled_method = NULL; 1881 uint64_t start_ns = NanoTime(); 1882 1883 if ((access_flags & kAccNative) != 0) { 1884 // Are we interpreting only and have support for generic JNI down calls? 1885 if (!compiler_options_->IsCompilationEnabled() && 1886 (instruction_set_ == kX86_64 || instruction_set_ == kArm64)) { 1887 // Leaving this empty will trigger the generic JNI version 1888 } else { 1889 compiled_method = compiler_->JniCompile(*this, access_flags, method_idx, dex_file); 1890 CHECK(compiled_method != NULL); 1891 } 1892 } else if ((access_flags & kAccAbstract) != 0) { 1893 } else { 1894 MethodReference method_ref(&dex_file, method_idx); 1895 bool compile = verification_results_->IsCandidateForCompilation(method_ref, access_flags); 1896 if (compile) { 1897 // NOTE: if compiler declines to compile this method, it will return NULL. 1898 compiled_method = compiler_->Compile( 1899 *this, code_item, access_flags, invoke_type, class_def_idx, 1900 method_idx, class_loader, dex_file); 1901 } 1902 if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) { 1903 // TODO: add a command-line option to disable DEX-to-DEX compilation ? 1904 (*dex_to_dex_compiler_)(*this, code_item, access_flags, 1905 invoke_type, class_def_idx, 1906 method_idx, class_loader, dex_file, 1907 dex_to_dex_compilation_level); 1908 } 1909 } 1910 uint64_t duration_ns = NanoTime() - start_ns; 1911 if (duration_ns > MsToNs(compiler_->GetMaximumCompilationTimeBeforeWarning())) { 1912 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file) 1913 << " took " << PrettyDuration(duration_ns); 1914 } 1915 1916 Thread* self = Thread::Current(); 1917 if (compiled_method != NULL) { 1918 MethodReference ref(&dex_file, method_idx); 1919 DCHECK(GetCompiledMethod(ref) == NULL) << PrettyMethod(method_idx, dex_file); 1920 { 1921 MutexLock mu(self, compiled_methods_lock_); 1922 compiled_methods_.Put(ref, compiled_method); 1923 } 1924 DCHECK(GetCompiledMethod(ref) != NULL) << PrettyMethod(method_idx, dex_file); 1925 } 1926 1927 if (self->IsExceptionPending()) { 1928 ScopedObjectAccess soa(self); 1929 LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n" 1930 << self->GetException(NULL)->Dump(); 1931 } 1932} 1933 1934CompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const { 1935 MutexLock mu(Thread::Current(), compiled_classes_lock_); 1936 ClassTable::const_iterator it = compiled_classes_.find(ref); 1937 if (it == compiled_classes_.end()) { 1938 return NULL; 1939 } 1940 CHECK(it->second != NULL); 1941 return it->second; 1942} 1943 1944void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) { 1945 MutexLock mu(Thread::Current(), compiled_classes_lock_); 1946 auto it = compiled_classes_.find(ref); 1947 if (it == compiled_classes_.end() || it->second->GetStatus() != status) { 1948 // An entry doesn't exist or the status is lower than the new status. 1949 if (it != compiled_classes_.end()) { 1950 CHECK_GT(status, it->second->GetStatus()); 1951 delete it->second; 1952 } 1953 switch (status) { 1954 case mirror::Class::kStatusNotReady: 1955 case mirror::Class::kStatusError: 1956 case mirror::Class::kStatusRetryVerificationAtRuntime: 1957 case mirror::Class::kStatusVerified: 1958 case mirror::Class::kStatusInitialized: 1959 break; // Expected states. 1960 default: 1961 LOG(FATAL) << "Unexpected class status for class " 1962 << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second))) 1963 << " of " << status; 1964 } 1965 CompiledClass* compiled_class = new CompiledClass(status); 1966 compiled_classes_.Overwrite(ref, compiled_class); 1967 } 1968} 1969 1970CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const { 1971 MutexLock mu(Thread::Current(), compiled_methods_lock_); 1972 MethodTable::const_iterator it = compiled_methods_.find(ref); 1973 if (it == compiled_methods_.end()) { 1974 return NULL; 1975 } 1976 CHECK(it->second != NULL); 1977 return it->second; 1978} 1979 1980void CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 1981 uint16_t class_def_index) { 1982 WriterMutexLock mu(self, freezing_constructor_lock_); 1983 freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index)); 1984} 1985 1986bool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file, 1987 uint16_t class_def_index) { 1988 ReaderMutexLock mu(self, freezing_constructor_lock_); 1989 return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0; 1990} 1991 1992bool CompilerDriver::WriteElf(const std::string& android_root, 1993 bool is_host, 1994 const std::vector<const art::DexFile*>& dex_files, 1995 OatWriter* oat_writer, 1996 art::File* file) 1997 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1998 return compiler_->WriteElf(file, oat_writer, dex_files, android_root, is_host, *this); 1999} 2000void CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set, 2001 std::string* target_triple, 2002 std::string* target_cpu, 2003 std::string* target_attr) { 2004 switch (instruction_set) { 2005 case kThumb2: 2006 *target_triple = "thumb-none-linux-gnueabi"; 2007 *target_cpu = "cortex-a9"; 2008 *target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db"; 2009 break; 2010 2011 case kArm: 2012 *target_triple = "armv7-none-linux-gnueabi"; 2013 // TODO: Fix for Nexus S. 2014 *target_cpu = "cortex-a9"; 2015 // TODO: Fix for Xoom. 2016 *target_attr = "+v7,+neon,+neonfp,+vfp3,+db"; 2017 break; 2018 2019 case kX86: 2020 *target_triple = "i386-pc-linux-gnu"; 2021 *target_attr = ""; 2022 break; 2023 2024 case kX86_64: 2025 *target_triple = "x86_64-pc-linux-gnu"; 2026 *target_attr = ""; 2027 break; 2028 2029 case kMips: 2030 *target_triple = "mipsel-unknown-linux"; 2031 *target_attr = "mips32r2"; 2032 break; 2033 2034 default: 2035 LOG(FATAL) << "Unknown instruction set: " << instruction_set; 2036 } 2037 } 2038 2039bool CompilerDriver::ReadProfile(const std::string& filename) { 2040 VLOG(compiler) << "reading profile file " << filename; 2041 struct stat st; 2042 int err = stat(filename.c_str(), &st); 2043 if (err == -1) { 2044 VLOG(compiler) << "not found"; 2045 return false; 2046 } 2047 std::ifstream in(filename.c_str()); 2048 if (!in) { 2049 VLOG(compiler) << "profile file " << filename << " exists but can't be opened"; 2050 VLOG(compiler) << "file owner: " << st.st_uid << ":" << st.st_gid; 2051 VLOG(compiler) << "me: " << getuid() << ":" << getgid(); 2052 VLOG(compiler) << "file permissions: " << std::oct << st.st_mode; 2053 VLOG(compiler) << "errno: " << errno; 2054 return false; 2055 } 2056 // The first line contains summary information. 2057 std::string line; 2058 std::getline(in, line); 2059 if (in.eof()) { 2060 return false; 2061 } 2062 std::vector<std::string> summary_info; 2063 Split(line, '/', summary_info); 2064 if (summary_info.size() != 3) { 2065 // Bad summary info. It should be count/total/bootpath. 2066 return false; 2067 } 2068 // This is the number of hits in all methods. 2069 uint32_t total_count = 0; 2070 for (int i = 0 ; i < 3; ++i) { 2071 total_count += atoi(summary_info[i].c_str()); 2072 } 2073 2074 // Now read each line until the end of file. Each line consists of 3 fields separated by '/'. 2075 // Store the info in descending order given by the most used methods. 2076 typedef std::set<std::pair<int, std::vector<std::string>>> ProfileSet; 2077 ProfileSet countSet; 2078 while (!in.eof()) { 2079 std::getline(in, line); 2080 if (in.eof()) { 2081 break; 2082 } 2083 std::vector<std::string> info; 2084 Split(line, '/', info); 2085 if (info.size() != 3) { 2086 // Malformed. 2087 break; 2088 } 2089 int count = atoi(info[1].c_str()); 2090 countSet.insert(std::make_pair(-count, info)); 2091 } 2092 2093 uint32_t curTotalCount = 0; 2094 ProfileSet::iterator end = countSet.end(); 2095 const ProfileData* prevData = nullptr; 2096 for (ProfileSet::iterator it = countSet.begin(); it != end ; it++) { 2097 const std::string& methodname = it->second[0]; 2098 uint32_t count = -it->first; 2099 uint32_t size = atoi(it->second[2].c_str()); 2100 double usedPercent = (count * 100.0) / total_count; 2101 2102 curTotalCount += count; 2103 // Methods with the same count should be part of the same top K percentage bucket. 2104 double topKPercentage = (prevData != nullptr) && (prevData->GetCount() == count) 2105 ? prevData->GetTopKUsedPercentage() 2106 : 100 * static_cast<double>(curTotalCount) / static_cast<double>(total_count); 2107 2108 // Add it to the profile map. 2109 ProfileData curData = ProfileData(methodname, count, size, usedPercent, topKPercentage); 2110 profile_map_[methodname] = curData; 2111 prevData = &curData; 2112 } 2113 return true; 2114} 2115 2116bool CompilerDriver::SkipCompilation(const std::string& method_name) { 2117 if (!profile_ok_) { 2118 return true; 2119 } 2120 // Methods that comprise topKPercentThreshold % of the total samples will be compiled. 2121 double topKPercentThreshold = 90.0; 2122#ifdef HAVE_ANDROID_OS 2123 char buf[PROP_VALUE_MAX]; 2124 property_get("dalvik.vm.profile.compile_thr", buf, "90.0"); 2125 topKPercentThreshold = strtod(buf, nullptr); 2126#endif 2127 // Test for reasonable thresholds. 2128 if (topKPercentThreshold < 10.0 || topKPercentThreshold > 90.0) { 2129 topKPercentThreshold = 90.0; 2130 } 2131 2132 // First find the method in the profile map. 2133 ProfileMap::iterator i = profile_map_.find(method_name); 2134 if (i == profile_map_.end()) { 2135 // Not in profile, no information can be determined. 2136 VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile"; 2137 return true; 2138 } 2139 const ProfileData& data = i->second; 2140 2141 // Compare against the start of the topK percentage bucket just in case the threshold 2142 // falls inside a bucket. 2143 bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent() <= topKPercentThreshold; 2144 if (compile) { 2145 LOG(INFO) << "compiling method " << method_name << " because its usage is part of top " 2146 << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%"; 2147 } else { 2148 VLOG(compiler) << "not compiling method " << method_name << " because it's not part of leading " 2149 << topKPercentThreshold << "% samples)"; 2150 } 2151 return !compile; 2152} 2153} // namespace art 2154