compiler_driver.cc revision 6a2ed4fb6e9a25f66d14c2075e9743b8d1198620
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#include <unordered_set> 20#include <vector> 21#include <unistd.h> 22 23#ifndef __APPLE__ 24#include <malloc.h> // For mallinfo 25#endif 26 27#include "android-base/strings.h" 28 29#include "art_field-inl.h" 30#include "art_method-inl.h" 31#include "base/arena_allocator.h" 32#include "base/array_ref.h" 33#include "base/bit_vector.h" 34#include "base/enums.h" 35#include "base/stl_util.h" 36#include "base/systrace.h" 37#include "base/time_utils.h" 38#include "base/timing_logger.h" 39#include "class_linker-inl.h" 40#include "compiled_method.h" 41#include "compiler.h" 42#include "compiler_callbacks.h" 43#include "compiler_driver-inl.h" 44#include "dex/dex_to_dex_compiler.h" 45#include "dex/verification_results.h" 46#include "dex/verified_method.h" 47#include "dex_compilation_unit.h" 48#include "dex_file-inl.h" 49#include "dex_instruction-inl.h" 50#include "driver/compiler_options.h" 51#include "gc/accounting/card_table-inl.h" 52#include "gc/accounting/heap_bitmap.h" 53#include "gc/space/image_space.h" 54#include "gc/space/space.h" 55#include "handle_scope-inl.h" 56#include "intrinsics_enum.h" 57#include "jni_internal.h" 58#include "mirror/class-inl.h" 59#include "mirror/class_loader.h" 60#include "mirror/dex_cache-inl.h" 61#include "mirror/object-inl.h" 62#include "mirror/object-refvisitor-inl.h" 63#include "mirror/object_array-inl.h" 64#include "mirror/throwable.h" 65#include "nativehelper/ScopedLocalRef.h" 66#include "object_lock.h" 67#include "runtime.h" 68#include "scoped_thread_state_change-inl.h" 69#include "thread.h" 70#include "thread_list.h" 71#include "thread_pool.h" 72#include "trampolines/trampoline_compiler.h" 73#include "transaction.h" 74#include "utils/atomic_dex_ref_map-inl.h" 75#include "utils/dex_cache_arrays_layout-inl.h" 76#include "utils/swap_space.h" 77#include "vdex_file.h" 78#include "verifier/method_verifier-inl.h" 79#include "verifier/method_verifier.h" 80#include "verifier/verifier_deps.h" 81#include "verifier/verifier_enums.h" 82 83namespace art { 84 85static constexpr bool kTimeCompileMethod = !kIsDebugBuild; 86 87// Print additional info during profile guided compilation. 88static constexpr bool kDebugProfileGuidedCompilation = false; 89 90// Max encoded fields allowed for initializing app image. Hardcode the number for now 91// because 5000 should be large enough. 92static constexpr uint32_t kMaxEncodedFields = 5000; 93 94static double Percentage(size_t x, size_t y) { 95 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y)); 96} 97 98static void DumpStat(size_t x, size_t y, const char* str) { 99 if (x == 0 && y == 0) { 100 return; 101 } 102 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases"; 103} 104 105class CompilerDriver::AOTCompilationStats { 106 public: 107 AOTCompilationStats() 108 : stats_lock_("AOT compilation statistics lock"), 109 resolved_types_(0), unresolved_types_(0), 110 resolved_instance_fields_(0), unresolved_instance_fields_(0), 111 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0), 112 type_based_devirtualization_(0), 113 safe_casts_(0), not_safe_casts_(0) { 114 for (size_t i = 0; i <= kMaxInvokeType; i++) { 115 resolved_methods_[i] = 0; 116 unresolved_methods_[i] = 0; 117 virtual_made_direct_[i] = 0; 118 direct_calls_to_boot_[i] = 0; 119 direct_methods_to_boot_[i] = 0; 120 } 121 } 122 123 void Dump() { 124 DumpStat(resolved_types_, unresolved_types_, "types resolved"); 125 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved"); 126 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_, 127 "static fields resolved"); 128 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_, 129 "static fields local to a class"); 130 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information"); 131 // Note, the code below subtracts the stat value so that when added to the stat value we have 132 // 100% of samples. TODO: clean this up. 133 DumpStat(type_based_devirtualization_, 134 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] + 135 resolved_methods_[kInterface] + unresolved_methods_[kInterface] - 136 type_based_devirtualization_, 137 "virtual/interface calls made direct based on type information"); 138 139 for (size_t i = 0; i <= kMaxInvokeType; i++) { 140 std::ostringstream oss; 141 oss << static_cast<InvokeType>(i) << " methods were AOT resolved"; 142 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str()); 143 if (virtual_made_direct_[i] > 0) { 144 std::ostringstream oss2; 145 oss2 << static_cast<InvokeType>(i) << " methods made direct"; 146 DumpStat(virtual_made_direct_[i], 147 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i], 148 oss2.str().c_str()); 149 } 150 if (direct_calls_to_boot_[i] > 0) { 151 std::ostringstream oss2; 152 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot"; 153 DumpStat(direct_calls_to_boot_[i], 154 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i], 155 oss2.str().c_str()); 156 } 157 if (direct_methods_to_boot_[i] > 0) { 158 std::ostringstream oss2; 159 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot"; 160 DumpStat(direct_methods_to_boot_[i], 161 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i], 162 oss2.str().c_str()); 163 } 164 } 165 } 166 167// Allow lossy statistics in non-debug builds. 168#ifndef NDEBUG 169#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_) 170#else 171#define STATS_LOCK() 172#endif 173 174 void TypeDoesntNeedAccessCheck() REQUIRES(!stats_lock_) { 175 STATS_LOCK(); 176 resolved_types_++; 177 } 178 179 void TypeNeedsAccessCheck() REQUIRES(!stats_lock_) { 180 STATS_LOCK(); 181 unresolved_types_++; 182 } 183 184 void ResolvedInstanceField() REQUIRES(!stats_lock_) { 185 STATS_LOCK(); 186 resolved_instance_fields_++; 187 } 188 189 void UnresolvedInstanceField() REQUIRES(!stats_lock_) { 190 STATS_LOCK(); 191 unresolved_instance_fields_++; 192 } 193 194 void ResolvedLocalStaticField() REQUIRES(!stats_lock_) { 195 STATS_LOCK(); 196 resolved_local_static_fields_++; 197 } 198 199 void ResolvedStaticField() REQUIRES(!stats_lock_) { 200 STATS_LOCK(); 201 resolved_static_fields_++; 202 } 203 204 void UnresolvedStaticField() REQUIRES(!stats_lock_) { 205 STATS_LOCK(); 206 unresolved_static_fields_++; 207 } 208 209 // Indicate that type information from the verifier led to devirtualization. 210 void PreciseTypeDevirtualization() REQUIRES(!stats_lock_) { 211 STATS_LOCK(); 212 type_based_devirtualization_++; 213 } 214 215 // A check-cast could be eliminated due to verifier type analysis. 216 void SafeCast() REQUIRES(!stats_lock_) { 217 STATS_LOCK(); 218 safe_casts_++; 219 } 220 221 // A check-cast couldn't be eliminated due to verifier type analysis. 222 void NotASafeCast() REQUIRES(!stats_lock_) { 223 STATS_LOCK(); 224 not_safe_casts_++; 225 } 226 227 private: 228 Mutex stats_lock_; 229 230 size_t resolved_types_; 231 size_t unresolved_types_; 232 233 size_t resolved_instance_fields_; 234 size_t unresolved_instance_fields_; 235 236 size_t resolved_local_static_fields_; 237 size_t resolved_static_fields_; 238 size_t unresolved_static_fields_; 239 // Type based devirtualization for invoke interface and virtual. 240 size_t type_based_devirtualization_; 241 242 size_t resolved_methods_[kMaxInvokeType + 1]; 243 size_t unresolved_methods_[kMaxInvokeType + 1]; 244 size_t virtual_made_direct_[kMaxInvokeType + 1]; 245 size_t direct_calls_to_boot_[kMaxInvokeType + 1]; 246 size_t direct_methods_to_boot_[kMaxInvokeType + 1]; 247 248 size_t safe_casts_; 249 size_t not_safe_casts_; 250 251 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats); 252}; 253 254class CompilerDriver::DexFileMethodSet { 255 public: 256 explicit DexFileMethodSet(const DexFile& dex_file) 257 : dex_file_(dex_file), 258 method_indexes_(dex_file.NumMethodIds(), false, Allocator::GetMallocAllocator()) { 259 } 260 DexFileMethodSet(DexFileMethodSet&& other) = default; 261 262 const DexFile& GetDexFile() const { return dex_file_; } 263 264 BitVector& GetMethodIndexes() { return method_indexes_; } 265 const BitVector& GetMethodIndexes() const { return method_indexes_; } 266 267 private: 268 const DexFile& dex_file_; 269 BitVector method_indexes_; 270}; 271 272CompilerDriver::CompilerDriver( 273 const CompilerOptions* compiler_options, 274 VerificationResults* verification_results, 275 Compiler::Kind compiler_kind, 276 InstructionSet instruction_set, 277 const InstructionSetFeatures* instruction_set_features, 278 std::unordered_set<std::string>* image_classes, 279 std::unordered_set<std::string>* compiled_classes, 280 std::unordered_set<std::string>* compiled_methods, 281 size_t thread_count, 282 bool dump_stats, 283 bool dump_passes, 284 CumulativeLogger* timer, 285 int swap_fd, 286 const ProfileCompilationInfo* profile_compilation_info) 287 : compiler_options_(compiler_options), 288 verification_results_(verification_results), 289 compiler_(Compiler::Create(this, compiler_kind)), 290 compiler_kind_(compiler_kind), 291 instruction_set_(instruction_set == kArm ? kThumb2 : instruction_set), 292 instruction_set_features_(instruction_set_features), 293 requires_constructor_barrier_lock_("constructor barrier lock"), 294 non_relative_linker_patch_count_(0u), 295 image_classes_(image_classes), 296 classes_to_compile_(compiled_classes), 297 methods_to_compile_(compiled_methods), 298 had_hard_verifier_failure_(false), 299 parallel_thread_count_(thread_count), 300 stats_(new AOTCompilationStats), 301 dump_stats_(dump_stats), 302 dump_passes_(dump_passes), 303 timings_logger_(timer), 304 compiler_context_(nullptr), 305 support_boot_image_fixup_(true), 306 dex_files_for_oat_file_(nullptr), 307 compiled_method_storage_(swap_fd), 308 profile_compilation_info_(profile_compilation_info), 309 max_arena_alloc_(0), 310 dex_to_dex_references_lock_("dex-to-dex references lock"), 311 dex_to_dex_references_(), 312 current_dex_to_dex_methods_(nullptr) { 313 DCHECK(compiler_options_ != nullptr); 314 315 compiler_->Init(); 316 317 if (GetCompilerOptions().IsBootImage()) { 318 CHECK(image_classes_.get() != nullptr) << "Expected image classes for boot image"; 319 } 320} 321 322CompilerDriver::~CompilerDriver() { 323 compiled_methods_.Visit([this](const DexFileReference& ref ATTRIBUTE_UNUSED, 324 CompiledMethod* method) { 325 if (method != nullptr) { 326 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, method); 327 } 328 }); 329 compiler_->UnInit(); 330} 331 332 333#define CREATE_TRAMPOLINE(type, abi, offset) \ 334 if (Is64BitInstructionSet(instruction_set_)) { \ 335 return CreateTrampoline64(instruction_set_, abi, \ 336 type ## _ENTRYPOINT_OFFSET(PointerSize::k64, offset)); \ 337 } else { \ 338 return CreateTrampoline32(instruction_set_, abi, \ 339 type ## _ENTRYPOINT_OFFSET(PointerSize::k32, offset)); \ 340 } 341 342std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateJniDlsymLookup() const { 343 CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup) 344} 345 346std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickGenericJniTrampoline() 347 const { 348 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline) 349} 350 351std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickImtConflictTrampoline() 352 const { 353 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline) 354} 355 356std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickResolutionTrampoline() 357 const { 358 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline) 359} 360 361std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickToInterpreterBridge() 362 const { 363 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge) 364} 365#undef CREATE_TRAMPOLINE 366 367static void SetupIntrinsic(Thread* self, 368 Intrinsics intrinsic, 369 InvokeType invoke_type, 370 const char* class_name, 371 const char* method_name, 372 const char* signature) 373 REQUIRES_SHARED(Locks::mutator_lock_) { 374 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 375 PointerSize image_size = class_linker->GetImagePointerSize(); 376 ObjPtr<mirror::Class> cls = class_linker->FindSystemClass(self, class_name); 377 if (cls == nullptr) { 378 LOG(FATAL) << "Could not find class of intrinsic " << class_name; 379 } 380 ArtMethod* method = cls->FindClassMethod(method_name, signature, image_size); 381 if (method == nullptr || method->GetDeclaringClass() != cls) { 382 LOG(FATAL) << "Could not find method of intrinsic " 383 << class_name << " " << method_name << " " << signature; 384 } 385 DCHECK_EQ(method->GetInvokeType(), invoke_type); 386 method->SetIntrinsic(static_cast<uint32_t>(intrinsic)); 387} 388 389void CompilerDriver::CompileAll(jobject class_loader, 390 const std::vector<const DexFile*>& dex_files, 391 TimingLogger* timings) { 392 DCHECK(!Runtime::Current()->IsStarted()); 393 394 InitializeThreadPools(); 395 396 VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false); 397 // Precompile: 398 // 1) Load image classes 399 // 2) Resolve all classes 400 // 3) Attempt to verify all classes 401 // 4) Attempt to initialize image classes, and trivially initialized classes 402 PreCompile(class_loader, dex_files, timings); 403 if (GetCompilerOptions().IsBootImage()) { 404 // We don't need to setup the intrinsics for non boot image compilation, as 405 // those compilations will pick up a boot image that have the ArtMethod already 406 // set with the intrinsics flag. 407 ScopedObjectAccess soa(Thread::Current()); 408#define SETUP_INTRINSICS(Name, InvokeType, NeedsEnvironmentOrCache, SideEffects, Exceptions, \ 409 ClassName, MethodName, Signature) \ 410 SetupIntrinsic(soa.Self(), Intrinsics::k##Name, InvokeType, ClassName, MethodName, Signature); 411#include "intrinsics_list.h" 412INTRINSICS_LIST(SETUP_INTRINSICS) 413#undef INTRINSICS_LIST 414#undef SETUP_INTRINSICS 415 } 416 // Compile: 417 // 1) Compile all classes and methods enabled for compilation. May fall back to dex-to-dex 418 // compilation. 419 if (GetCompilerOptions().IsAnyCompilationEnabled()) { 420 Compile(class_loader, dex_files, timings); 421 } 422 if (dump_stats_) { 423 stats_->Dump(); 424 } 425 426 FreeThreadPools(); 427} 428 429static optimizer::DexToDexCompilationLevel GetDexToDexCompilationLevel( 430 Thread* self, const CompilerDriver& driver, Handle<mirror::ClassLoader> class_loader, 431 const DexFile& dex_file, const DexFile::ClassDef& class_def) 432 REQUIRES_SHARED(Locks::mutator_lock_) { 433 auto* const runtime = Runtime::Current(); 434 DCHECK(driver.GetCompilerOptions().IsQuickeningCompilationEnabled()); 435 const char* descriptor = dex_file.GetClassDescriptor(class_def); 436 ClassLinker* class_linker = runtime->GetClassLinker(); 437 mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader); 438 if (klass == nullptr) { 439 CHECK(self->IsExceptionPending()); 440 self->ClearException(); 441 return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile; 442 } 443 // DexToDex at the kOptimize level may introduce quickened opcodes, which replace symbolic 444 // references with actual offsets. We cannot re-verify such instructions. 445 // 446 // We store the verification information in the class status in the oat file, which the linker 447 // can validate (checksums) and use to skip load-time verification. It is thus safe to 448 // optimize when a class has been fully verified before. 449 optimizer::DexToDexCompilationLevel max_level = optimizer::DexToDexCompilationLevel::kOptimize; 450 if (driver.GetCompilerOptions().GetDebuggable()) { 451 // We are debuggable so definitions of classes might be changed. We don't want to do any 452 // optimizations that could break that. 453 max_level = optimizer::DexToDexCompilationLevel::kDontDexToDexCompile; 454 } 455 if (klass->IsVerified()) { 456 // Class is verified so we can enable DEX-to-DEX compilation for performance. 457 return max_level; 458 } else { 459 // Class verification has failed: do not run DEX-to-DEX optimizations. 460 return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile; 461 } 462} 463 464static optimizer::DexToDexCompilationLevel GetDexToDexCompilationLevel( 465 Thread* self, 466 const CompilerDriver& driver, 467 jobject jclass_loader, 468 const DexFile& dex_file, 469 const DexFile::ClassDef& class_def) { 470 ScopedObjectAccess soa(self); 471 StackHandleScope<1> hs(soa.Self()); 472 Handle<mirror::ClassLoader> class_loader( 473 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 474 return GetDexToDexCompilationLevel(self, driver, class_loader, dex_file, class_def); 475} 476 477// Does the runtime for the InstructionSet provide an implementation returned by 478// GetQuickGenericJniStub allowing down calls that aren't compiled using a JNI compiler? 479static bool InstructionSetHasGenericJniStub(InstructionSet isa) { 480 switch (isa) { 481 case kArm: 482 case kArm64: 483 case kThumb2: 484 case kMips: 485 case kMips64: 486 case kX86: 487 case kX86_64: return true; 488 default: return false; 489 } 490} 491 492static void CompileMethod(Thread* self, 493 CompilerDriver* driver, 494 const DexFile::CodeItem* code_item, 495 uint32_t access_flags, 496 InvokeType invoke_type, 497 uint16_t class_def_idx, 498 uint32_t method_idx, 499 Handle<mirror::ClassLoader> class_loader, 500 const DexFile& dex_file, 501 optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level, 502 bool compilation_enabled, 503 Handle<mirror::DexCache> dex_cache) { 504 DCHECK(driver != nullptr); 505 CompiledMethod* compiled_method = nullptr; 506 uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0; 507 MethodReference method_ref(&dex_file, method_idx); 508 509 if (driver->GetCurrentDexToDexMethods() != nullptr) { 510 // This is the second pass when we dex-to-dex compile previously marked methods. 511 // TODO: Refactor the compilation to avoid having to distinguish the two passes 512 // here. That should be done on a higher level. http://b/29089975 513 if (driver->GetCurrentDexToDexMethods()->IsBitSet(method_idx)) { 514 VerificationResults* results = driver->GetVerificationResults(); 515 DCHECK(results != nullptr); 516 const VerifiedMethod* verified_method = results->GetVerifiedMethod(method_ref); 517 // Do not optimize if a VerifiedMethod is missing. SafeCast elision, 518 // for example, relies on it. 519 compiled_method = optimizer::ArtCompileDEX( 520 driver, 521 code_item, 522 access_flags, 523 invoke_type, 524 class_def_idx, 525 method_idx, 526 class_loader, 527 dex_file, 528 (verified_method != nullptr) 529 ? dex_to_dex_compilation_level 530 : optimizer::DexToDexCompilationLevel::kDontDexToDexCompile); 531 } 532 } else if ((access_flags & kAccNative) != 0) { 533 // Are we extracting only and have support for generic JNI down calls? 534 if (!driver->GetCompilerOptions().IsJniCompilationEnabled() && 535 InstructionSetHasGenericJniStub(driver->GetInstructionSet())) { 536 // Leaving this empty will trigger the generic JNI version 537 } else { 538 // Look-up the ArtMethod associated with this code_item (if any) 539 // -- It is later used to lookup any [optimization] annotations for this method. 540 ScopedObjectAccess soa(self); 541 542 // TODO: Lookup annotation from DexFile directly without resolving method. 543 ArtMethod* method = 544 Runtime::Current()->GetClassLinker()->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( 545 dex_file, 546 method_idx, 547 dex_cache, 548 class_loader, 549 /* referrer */ nullptr, 550 invoke_type); 551 552 // Query any JNI optimization annotations such as @FastNative or @CriticalNative. 553 Compiler::JniOptimizationFlags optimization_flags = Compiler::kNone; 554 if (UNLIKELY(method == nullptr)) { 555 // Failed method resolutions happen very rarely, e.g. ancestor class cannot be resolved. 556 DCHECK(self->IsExceptionPending()); 557 self->ClearException(); 558 } else if (method->IsAnnotatedWithFastNative()) { 559 // TODO: Will no longer need this CHECK once we have verifier checking this. 560 CHECK(!method->IsAnnotatedWithCriticalNative()); 561 optimization_flags = Compiler::kFastNative; 562 } else if (method->IsAnnotatedWithCriticalNative()) { 563 // TODO: Will no longer need this CHECK once we have verifier checking this. 564 CHECK(!method->IsAnnotatedWithFastNative()); 565 optimization_flags = Compiler::kCriticalNative; 566 } 567 568 compiled_method = driver->GetCompiler()->JniCompile(access_flags, 569 method_idx, 570 dex_file, 571 optimization_flags); 572 CHECK(compiled_method != nullptr); 573 } 574 } else if ((access_flags & kAccAbstract) != 0) { 575 // Abstract methods don't have code. 576 } else { 577 VerificationResults* results = driver->GetVerificationResults(); 578 DCHECK(results != nullptr); 579 const VerifiedMethod* verified_method = results->GetVerifiedMethod(method_ref); 580 bool compile = compilation_enabled && 581 // Basic checks, e.g., not <clinit>. 582 results->IsCandidateForCompilation(method_ref, access_flags) && 583 // Did not fail to create VerifiedMethod metadata. 584 verified_method != nullptr && 585 // Do not have failures that should punt to the interpreter. 586 !verified_method->HasRuntimeThrow() && 587 (verified_method->GetEncounteredVerificationFailures() & 588 (verifier::VERIFY_ERROR_FORCE_INTERPRETER | verifier::VERIFY_ERROR_LOCKING)) == 0 && 589 // Is eligable for compilation by methods-to-compile filter. 590 driver->IsMethodToCompile(method_ref) && 591 driver->ShouldCompileBasedOnProfile(method_ref); 592 593 if (compile) { 594 // NOTE: if compiler declines to compile this method, it will return null. 595 compiled_method = driver->GetCompiler()->Compile(code_item, 596 access_flags, 597 invoke_type, 598 class_def_idx, 599 method_idx, 600 class_loader, 601 dex_file, 602 dex_cache); 603 } 604 if (compiled_method == nullptr && 605 dex_to_dex_compilation_level != optimizer::DexToDexCompilationLevel::kDontDexToDexCompile) { 606 DCHECK(!Runtime::Current()->UseJitCompilation()); 607 // TODO: add a command-line option to disable DEX-to-DEX compilation ? 608 driver->MarkForDexToDexCompilation(self, method_ref); 609 } 610 } 611 if (kTimeCompileMethod) { 612 uint64_t duration_ns = NanoTime() - start_ns; 613 if (duration_ns > MsToNs(driver->GetCompiler()->GetMaximumCompilationTimeBeforeWarning())) { 614 LOG(WARNING) << "Compilation of " << dex_file.PrettyMethod(method_idx) 615 << " took " << PrettyDuration(duration_ns); 616 } 617 } 618 619 if (compiled_method != nullptr) { 620 // Count non-relative linker patches. 621 size_t non_relative_linker_patch_count = 0u; 622 for (const LinkerPatch& patch : compiled_method->GetPatches()) { 623 if (!patch.IsPcRelative()) { 624 ++non_relative_linker_patch_count; 625 } 626 } 627 bool compile_pic = driver->GetCompilerOptions().GetCompilePic(); // Off by default 628 // When compiling with PIC, there should be zero non-relative linker patches 629 CHECK(!compile_pic || non_relative_linker_patch_count == 0u); 630 631 driver->AddCompiledMethod(method_ref, compiled_method, non_relative_linker_patch_count); 632 } 633 634 if (self->IsExceptionPending()) { 635 ScopedObjectAccess soa(self); 636 LOG(FATAL) << "Unexpected exception compiling: " << dex_file.PrettyMethod(method_idx) << "\n" 637 << self->GetException()->Dump(); 638 } 639} 640 641void CompilerDriver::CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings) { 642 DCHECK(!Runtime::Current()->IsStarted()); 643 jobject jclass_loader; 644 const DexFile* dex_file; 645 uint16_t class_def_idx; 646 uint32_t method_idx = method->GetDexMethodIndex(); 647 uint32_t access_flags = method->GetAccessFlags(); 648 InvokeType invoke_type = method->GetInvokeType(); 649 StackHandleScope<2> hs(self); 650 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); 651 Handle<mirror::ClassLoader> class_loader( 652 hs.NewHandle(method->GetDeclaringClass()->GetClassLoader())); 653 { 654 ScopedObjectAccessUnchecked soa(self); 655 ScopedLocalRef<jobject> local_class_loader( 656 soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); 657 jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get()); 658 // Find the dex_file 659 dex_file = method->GetDexFile(); 660 class_def_idx = method->GetClassDefIndex(); 661 } 662 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 663 664 // Go to native so that we don't block GC during compilation. 665 ScopedThreadSuspension sts(self, kNative); 666 667 std::vector<const DexFile*> dex_files; 668 dex_files.push_back(dex_file); 669 670 InitializeThreadPools(); 671 672 PreCompile(jclass_loader, dex_files, timings); 673 674 // Can we run DEX-to-DEX compiler on this class ? 675 optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level = 676 GetDexToDexCompilationLevel(self, 677 *this, 678 jclass_loader, 679 *dex_file, 680 dex_file->GetClassDef(class_def_idx)); 681 682 DCHECK(current_dex_to_dex_methods_ == nullptr); 683 CompileMethod(self, 684 this, 685 code_item, 686 access_flags, 687 invoke_type, 688 class_def_idx, 689 method_idx, 690 class_loader, 691 *dex_file, 692 dex_to_dex_compilation_level, 693 true, 694 dex_cache); 695 696 ArrayRef<DexFileMethodSet> dex_to_dex_references; 697 { 698 // From this point on, we shall not modify dex_to_dex_references_, so 699 // just grab a reference to it that we use without holding the mutex. 700 MutexLock lock(Thread::Current(), dex_to_dex_references_lock_); 701 dex_to_dex_references = ArrayRef<DexFileMethodSet>(dex_to_dex_references_); 702 } 703 if (!dex_to_dex_references.empty()) { 704 DCHECK_EQ(dex_to_dex_references.size(), 1u); 705 DCHECK(&dex_to_dex_references[0].GetDexFile() == dex_file); 706 current_dex_to_dex_methods_ = &dex_to_dex_references.front().GetMethodIndexes(); 707 DCHECK(current_dex_to_dex_methods_->IsBitSet(method_idx)); 708 DCHECK_EQ(current_dex_to_dex_methods_->NumSetBits(), 1u); 709 CompileMethod(self, 710 this, 711 code_item, 712 access_flags, 713 invoke_type, 714 class_def_idx, 715 method_idx, 716 class_loader, 717 *dex_file, 718 dex_to_dex_compilation_level, 719 true, 720 dex_cache); 721 current_dex_to_dex_methods_ = nullptr; 722 } 723 724 FreeThreadPools(); 725 726 self->GetJniEnv()->DeleteGlobalRef(jclass_loader); 727} 728 729void CompilerDriver::Resolve(jobject class_loader, 730 const std::vector<const DexFile*>& dex_files, 731 TimingLogger* timings) { 732 // Resolution allocates classes and needs to run single-threaded to be deterministic. 733 bool force_determinism = GetCompilerOptions().IsForceDeterminism(); 734 ThreadPool* resolve_thread_pool = force_determinism 735 ? single_thread_pool_.get() 736 : parallel_thread_pool_.get(); 737 size_t resolve_thread_count = force_determinism ? 1U : parallel_thread_count_; 738 739 for (size_t i = 0; i != dex_files.size(); ++i) { 740 const DexFile* dex_file = dex_files[i]; 741 CHECK(dex_file != nullptr); 742 ResolveDexFile(class_loader, 743 *dex_file, 744 dex_files, 745 resolve_thread_pool, 746 resolve_thread_count, 747 timings); 748 } 749} 750 751// Resolve const-strings in the code. Done to have deterministic allocation behavior. Right now 752// this is single-threaded for simplicity. 753// TODO: Collect the relevant string indices in parallel, then allocate them sequentially in a 754// stable order. 755 756static void ResolveConstStrings(Handle<mirror::DexCache> dex_cache, 757 const DexFile& dex_file, 758 const DexFile::CodeItem* code_item) 759 REQUIRES_SHARED(Locks::mutator_lock_) { 760 if (code_item == nullptr) { 761 // Abstract or native method. 762 return; 763 } 764 765 const uint16_t* code_ptr = code_item->insns_; 766 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_; 767 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); 768 769 while (code_ptr < code_end) { 770 const Instruction* inst = Instruction::At(code_ptr); 771 switch (inst->Opcode()) { 772 case Instruction::CONST_STRING: 773 case Instruction::CONST_STRING_JUMBO: { 774 dex::StringIndex string_index((inst->Opcode() == Instruction::CONST_STRING) 775 ? inst->VRegB_21c() 776 : inst->VRegB_31c()); 777 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache); 778 CHECK(string != nullptr) << "Could not allocate a string when forcing determinism"; 779 break; 780 } 781 782 default: 783 break; 784 } 785 786 code_ptr += inst->SizeInCodeUnits(); 787 } 788} 789 790static void ResolveConstStrings(CompilerDriver* driver, 791 const std::vector<const DexFile*>& dex_files, 792 TimingLogger* timings) { 793 ScopedObjectAccess soa(Thread::Current()); 794 StackHandleScope<1> hs(soa.Self()); 795 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); 796 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr)); 797 798 for (const DexFile* dex_file : dex_files) { 799 dex_cache.Assign(class_linker->FindDexCache(soa.Self(), *dex_file)); 800 TimingLogger::ScopedTiming t("Resolve const-string Strings", timings); 801 802 size_t class_def_count = dex_file->NumClassDefs(); 803 for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) { 804 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); 805 806 const uint8_t* class_data = dex_file->GetClassData(class_def); 807 if (class_data == nullptr) { 808 // empty class, probably a marker interface 809 continue; 810 } 811 812 ClassDataItemIterator it(*dex_file, class_data); 813 it.SkipAllFields(); 814 815 bool compilation_enabled = driver->IsClassToCompile( 816 dex_file->StringByTypeIdx(class_def.class_idx_)); 817 if (!compilation_enabled) { 818 // Compilation is skipped, do not resolve const-string in code of this class. 819 // TODO: Make sure that inlining honors this. 820 continue; 821 } 822 823 // Direct methods. 824 int64_t previous_direct_method_idx = -1; 825 while (it.HasNextDirectMethod()) { 826 uint32_t method_idx = it.GetMemberIndex(); 827 if (method_idx == previous_direct_method_idx) { 828 // smali can create dex files with two encoded_methods sharing the same method_idx 829 // http://code.google.com/p/smali/issues/detail?id=119 830 it.Next(); 831 continue; 832 } 833 previous_direct_method_idx = method_idx; 834 ResolveConstStrings(dex_cache, *dex_file, it.GetMethodCodeItem()); 835 it.Next(); 836 } 837 // Virtual methods. 838 int64_t previous_virtual_method_idx = -1; 839 while (it.HasNextVirtualMethod()) { 840 uint32_t method_idx = it.GetMemberIndex(); 841 if (method_idx == previous_virtual_method_idx) { 842 // smali can create dex files with two encoded_methods sharing the same method_idx 843 // http://code.google.com/p/smali/issues/detail?id=119 844 it.Next(); 845 continue; 846 } 847 previous_virtual_method_idx = method_idx; 848 ResolveConstStrings(dex_cache, *dex_file, it.GetMethodCodeItem()); 849 it.Next(); 850 } 851 DCHECK(!it.HasNext()); 852 } 853 } 854} 855 856inline void CompilerDriver::CheckThreadPools() { 857 DCHECK(parallel_thread_pool_ != nullptr); 858 DCHECK(single_thread_pool_ != nullptr); 859} 860 861static void EnsureVerifiedOrVerifyAtRuntime(jobject jclass_loader, 862 const std::vector<const DexFile*>& dex_files) { 863 ScopedObjectAccess soa(Thread::Current()); 864 StackHandleScope<2> hs(soa.Self()); 865 Handle<mirror::ClassLoader> class_loader( 866 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 867 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr)); 868 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 869 870 for (const DexFile* dex_file : dex_files) { 871 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) { 872 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); 873 const char* descriptor = dex_file->GetClassDescriptor(class_def); 874 cls.Assign(class_linker->FindClass(soa.Self(), descriptor, class_loader)); 875 if (cls == nullptr) { 876 soa.Self()->ClearException(); 877 } else if (&cls->GetDexFile() == dex_file) { 878 DCHECK(cls->IsErroneous() || cls->IsVerified() || cls->ShouldVerifyAtRuntime()) 879 << cls->PrettyClass() 880 << " " << cls->GetStatus(); 881 } 882 } 883 } 884} 885 886void CompilerDriver::PreCompile(jobject class_loader, 887 const std::vector<const DexFile*>& dex_files, 888 TimingLogger* timings) { 889 CheckThreadPools(); 890 891 LoadImageClasses(timings); 892 VLOG(compiler) << "LoadImageClasses: " << GetMemoryUsageString(false); 893 894 if (compiler_options_->IsAnyCompilationEnabled()) { 895 // Avoid adding the dex files in the case where we aren't going to add compiled methods. 896 // This reduces RAM usage for this case. 897 for (const DexFile* dex_file : dex_files) { 898 // Can be already inserted if the caller is CompileOne. This happens for gtests. 899 if (!compiled_methods_.HaveDexFile(dex_file)) { 900 compiled_methods_.AddDexFile(dex_file, dex_file->NumMethodIds()); 901 } 902 } 903 // Resolve eagerly to prepare for compilation. 904 Resolve(class_loader, dex_files, timings); 905 VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false); 906 } 907 908 if (compiler_options_->AssumeClassesAreVerified()) { 909 VLOG(compiler) << "Verify none mode specified, skipping verification."; 910 SetVerified(class_loader, dex_files, timings); 911 } 912 913 if (!compiler_options_->IsVerificationEnabled()) { 914 return; 915 } 916 917 if (GetCompilerOptions().IsForceDeterminism() && GetCompilerOptions().IsBootImage()) { 918 // Resolve strings from const-string. Do this now to have a deterministic image. 919 ResolveConstStrings(this, dex_files, timings); 920 VLOG(compiler) << "Resolve const-strings: " << GetMemoryUsageString(false); 921 } 922 923 Verify(class_loader, dex_files, timings); 924 VLOG(compiler) << "Verify: " << GetMemoryUsageString(false); 925 926 if (had_hard_verifier_failure_ && GetCompilerOptions().AbortOnHardVerifierFailure()) { 927 LOG(FATAL) << "Had a hard failure verifying all classes, and was asked to abort in such " 928 << "situations. Please check the log."; 929 } 930 931 if (compiler_options_->IsAnyCompilationEnabled()) { 932 if (kIsDebugBuild) { 933 EnsureVerifiedOrVerifyAtRuntime(class_loader, dex_files); 934 } 935 InitializeClasses(class_loader, dex_files, timings); 936 VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false); 937 } 938 939 UpdateImageClasses(timings); 940 VLOG(compiler) << "UpdateImageClasses: " << GetMemoryUsageString(false); 941} 942 943bool CompilerDriver::IsImageClass(const char* descriptor) const { 944 if (image_classes_ != nullptr) { 945 // If we have a set of image classes, use those. 946 return image_classes_->find(descriptor) != image_classes_->end(); 947 } 948 // No set of image classes, assume we include all the classes. 949 // NOTE: Currently only reachable from InitImageMethodVisitor for the app image case. 950 return !GetCompilerOptions().IsBootImage(); 951} 952 953bool CompilerDriver::IsClassToCompile(const char* descriptor) const { 954 if (classes_to_compile_ == nullptr) { 955 return true; 956 } 957 return classes_to_compile_->find(descriptor) != classes_to_compile_->end(); 958} 959 960bool CompilerDriver::IsMethodToCompile(const MethodReference& method_ref) const { 961 if (methods_to_compile_ == nullptr) { 962 return true; 963 } 964 965 std::string tmp = method_ref.dex_file->PrettyMethod(method_ref.dex_method_index, true); 966 return methods_to_compile_->find(tmp.c_str()) != methods_to_compile_->end(); 967} 968 969bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const { 970 // Profile compilation info may be null if no profile is passed. 971 if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) { 972 // Use the compiler filter instead of the presence of profile_compilation_info_ since 973 // we may want to have full speed compilation along with profile based layout optimizations. 974 return true; 975 } 976 // If we are using a profile filter but do not have a profile compilation info, compile nothing. 977 if (profile_compilation_info_ == nullptr) { 978 return false; 979 } 980 // Compile only hot methods, it is the profile saver's job to decide what startup methods to mark 981 // as hot. 982 bool result = profile_compilation_info_->GetMethodHotness(method_ref).IsHot(); 983 984 if (kDebugProfileGuidedCompilation) { 985 LOG(INFO) << "[ProfileGuidedCompilation] " 986 << (result ? "Compiled" : "Skipped") << " method:" 987 << method_ref.dex_file->PrettyMethod(method_ref.dex_method_index, true); 988 } 989 return result; 990} 991 992class ResolveCatchBlockExceptionsClassVisitor : public ClassVisitor { 993 public: 994 ResolveCatchBlockExceptionsClassVisitor() : classes_() {} 995 996 virtual bool operator()(ObjPtr<mirror::Class> c) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { 997 classes_.push_back(c); 998 return true; 999 } 1000 1001 void FindExceptionTypesToResolve( 1002 std::set<std::pair<dex::TypeIndex, const DexFile*>>* exceptions_to_resolve) 1003 REQUIRES_SHARED(Locks::mutator_lock_) { 1004 const auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); 1005 for (ObjPtr<mirror::Class> klass : classes_) { 1006 for (ArtMethod& method : klass->GetMethods(pointer_size)) { 1007 FindExceptionTypesToResolveForMethod(&method, exceptions_to_resolve); 1008 } 1009 } 1010 } 1011 1012 private: 1013 void FindExceptionTypesToResolveForMethod( 1014 ArtMethod* method, 1015 std::set<std::pair<dex::TypeIndex, const DexFile*>>* exceptions_to_resolve) 1016 REQUIRES_SHARED(Locks::mutator_lock_) { 1017 const DexFile::CodeItem* code_item = method->GetCodeItem(); 1018 if (code_item == nullptr) { 1019 return; // native or abstract method 1020 } 1021 if (code_item->tries_size_ == 0) { 1022 return; // nothing to process 1023 } 1024 const uint8_t* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); 1025 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); 1026 for (size_t i = 0; i < num_encoded_catch_handlers; i++) { 1027 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); 1028 bool has_catch_all = false; 1029 if (encoded_catch_handler_size <= 0) { 1030 encoded_catch_handler_size = -encoded_catch_handler_size; 1031 has_catch_all = true; 1032 } 1033 for (int32_t j = 0; j < encoded_catch_handler_size; j++) { 1034 dex::TypeIndex encoded_catch_handler_handlers_type_idx = 1035 dex::TypeIndex(DecodeUnsignedLeb128(&encoded_catch_handler_list)); 1036 // Add to set of types to resolve if not already in the dex cache resolved types 1037 if (!method->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { 1038 exceptions_to_resolve->emplace(encoded_catch_handler_handlers_type_idx, 1039 method->GetDexFile()); 1040 } 1041 // ignore address associated with catch handler 1042 DecodeUnsignedLeb128(&encoded_catch_handler_list); 1043 } 1044 if (has_catch_all) { 1045 // ignore catch all address 1046 DecodeUnsignedLeb128(&encoded_catch_handler_list); 1047 } 1048 } 1049 } 1050 1051 std::vector<ObjPtr<mirror::Class>> classes_; 1052}; 1053 1054class RecordImageClassesVisitor : public ClassVisitor { 1055 public: 1056 explicit RecordImageClassesVisitor(std::unordered_set<std::string>* image_classes) 1057 : image_classes_(image_classes) {} 1058 1059 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { 1060 std::string temp; 1061 image_classes_->insert(klass->GetDescriptor(&temp)); 1062 return true; 1063 } 1064 1065 private: 1066 std::unordered_set<std::string>* const image_classes_; 1067}; 1068 1069// Make a list of descriptors for classes to include in the image 1070void CompilerDriver::LoadImageClasses(TimingLogger* timings) { 1071 CHECK(timings != nullptr); 1072 if (!GetCompilerOptions().IsBootImage()) { 1073 return; 1074 } 1075 1076 TimingLogger::ScopedTiming t("LoadImageClasses", timings); 1077 // Make a first class to load all classes explicitly listed in the file 1078 Thread* self = Thread::Current(); 1079 ScopedObjectAccess soa(self); 1080 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1081 CHECK(image_classes_.get() != nullptr); 1082 for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) { 1083 const std::string& descriptor(*it); 1084 StackHandleScope<1> hs(self); 1085 Handle<mirror::Class> klass( 1086 hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str()))); 1087 if (klass == nullptr) { 1088 VLOG(compiler) << "Failed to find class " << descriptor; 1089 image_classes_->erase(it++); 1090 self->ClearException(); 1091 } else { 1092 ++it; 1093 } 1094 } 1095 1096 // Resolve exception classes referenced by the loaded classes. The catch logic assumes 1097 // exceptions are resolved by the verifier when there is a catch block in an interested method. 1098 // Do this here so that exception classes appear to have been specified image classes. 1099 std::set<std::pair<dex::TypeIndex, const DexFile*>> unresolved_exception_types; 1100 StackHandleScope<1> hs(self); 1101 Handle<mirror::Class> java_lang_Throwable( 1102 hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;"))); 1103 do { 1104 unresolved_exception_types.clear(); 1105 { 1106 // Thread suspension is not allowed while ResolveCatchBlockExceptionsClassVisitor 1107 // is using a std::vector<ObjPtr<mirror::Class>>. 1108 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 1109 ResolveCatchBlockExceptionsClassVisitor visitor; 1110 class_linker->VisitClasses(&visitor); 1111 visitor.FindExceptionTypesToResolve(&unresolved_exception_types); 1112 } 1113 for (const auto& exception_type : unresolved_exception_types) { 1114 dex::TypeIndex exception_type_idx = exception_type.first; 1115 const DexFile* dex_file = exception_type.second; 1116 StackHandleScope<2> hs2(self); 1117 Handle<mirror::DexCache> dex_cache(hs2.NewHandle(class_linker->RegisterDexFile(*dex_file, 1118 nullptr))); 1119 Handle<mirror::Class> klass(hs2.NewHandle( 1120 (dex_cache != nullptr) 1121 ? class_linker->ResolveType(*dex_file, 1122 exception_type_idx, 1123 dex_cache, 1124 ScopedNullHandle<mirror::ClassLoader>()) 1125 : nullptr)); 1126 if (klass == nullptr) { 1127 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); 1128 const char* descriptor = dex_file->GetTypeDescriptor(type_id); 1129 LOG(FATAL) << "Failed to resolve class " << descriptor; 1130 } 1131 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.Get())); 1132 } 1133 // Resolving exceptions may load classes that reference more exceptions, iterate until no 1134 // more are found 1135 } while (!unresolved_exception_types.empty()); 1136 1137 // We walk the roots looking for classes so that we'll pick up the 1138 // above classes plus any classes them depend on such super 1139 // classes, interfaces, and the required ClassLinker roots. 1140 RecordImageClassesVisitor visitor(image_classes_.get()); 1141 class_linker->VisitClasses(&visitor); 1142 1143 CHECK_NE(image_classes_->size(), 0U); 1144} 1145 1146static void MaybeAddToImageClasses(Thread* self, 1147 ObjPtr<mirror::Class> klass, 1148 std::unordered_set<std::string>* image_classes) 1149 REQUIRES_SHARED(Locks::mutator_lock_) { 1150 DCHECK_EQ(self, Thread::Current()); 1151 StackHandleScope<1> hs(self); 1152 std::string temp; 1153 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); 1154 while (!klass->IsObjectClass()) { 1155 const char* descriptor = klass->GetDescriptor(&temp); 1156 std::pair<std::unordered_set<std::string>::iterator, bool> result = 1157 image_classes->insert(descriptor); 1158 if (!result.second) { // Previously inserted. 1159 break; 1160 } 1161 VLOG(compiler) << "Adding " << descriptor << " to image classes"; 1162 for (size_t i = 0, num_interfaces = klass->NumDirectInterfaces(); i != num_interfaces; ++i) { 1163 ObjPtr<mirror::Class> interface = mirror::Class::GetDirectInterface(self, klass, i); 1164 DCHECK(interface != nullptr); 1165 MaybeAddToImageClasses(self, interface, image_classes); 1166 } 1167 for (auto& m : klass->GetVirtualMethods(pointer_size)) { 1168 MaybeAddToImageClasses(self, m.GetDeclaringClass(), image_classes); 1169 } 1170 if (klass->IsArrayClass()) { 1171 MaybeAddToImageClasses(self, klass->GetComponentType(), image_classes); 1172 } 1173 klass.Assign(klass->GetSuperClass()); 1174 } 1175} 1176 1177// Keeps all the data for the update together. Also doubles as the reference visitor. 1178// Note: we can use object pointers because we suspend all threads. 1179class ClinitImageUpdate { 1180 public: 1181 static ClinitImageUpdate* Create(VariableSizedHandleScope& hs, 1182 std::unordered_set<std::string>* image_class_descriptors, 1183 Thread* self, 1184 ClassLinker* linker) { 1185 std::unique_ptr<ClinitImageUpdate> res(new ClinitImageUpdate(hs, 1186 image_class_descriptors, 1187 self, 1188 linker)); 1189 return res.release(); 1190 } 1191 1192 ~ClinitImageUpdate() { 1193 // Allow others to suspend again. 1194 self_->EndAssertNoThreadSuspension(old_cause_); 1195 } 1196 1197 // Visitor for VisitReferences. 1198 void operator()(ObjPtr<mirror::Object> object, 1199 MemberOffset field_offset, 1200 bool /* is_static */) const 1201 REQUIRES_SHARED(Locks::mutator_lock_) { 1202 mirror::Object* ref = object->GetFieldObject<mirror::Object>(field_offset); 1203 if (ref != nullptr) { 1204 VisitClinitClassesObject(ref); 1205 } 1206 } 1207 1208 // java.lang.ref.Reference visitor for VisitReferences. 1209 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, 1210 ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const {} 1211 1212 // Ignore class native roots. 1213 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) 1214 const {} 1215 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} 1216 1217 void Walk() REQUIRES_SHARED(Locks::mutator_lock_) { 1218 // Use the initial classes as roots for a search. 1219 for (Handle<mirror::Class> klass_root : image_classes_) { 1220 VisitClinitClassesObject(klass_root.Get()); 1221 } 1222 Thread* self = Thread::Current(); 1223 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 1224 for (Handle<mirror::Class> h_klass : to_insert_) { 1225 MaybeAddToImageClasses(self, h_klass.Get(), image_class_descriptors_); 1226 } 1227 } 1228 1229 private: 1230 class FindImageClassesVisitor : public ClassVisitor { 1231 public: 1232 explicit FindImageClassesVisitor(VariableSizedHandleScope& hs, 1233 ClinitImageUpdate* data) 1234 : data_(data), 1235 hs_(hs) {} 1236 1237 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { 1238 std::string temp; 1239 const char* name = klass->GetDescriptor(&temp); 1240 if (data_->image_class_descriptors_->find(name) != data_->image_class_descriptors_->end()) { 1241 data_->image_classes_.push_back(hs_.NewHandle(klass)); 1242 } else { 1243 // Check whether it is initialized and has a clinit. They must be kept, too. 1244 if (klass->IsInitialized() && klass->FindClassInitializer( 1245 Runtime::Current()->GetClassLinker()->GetImagePointerSize()) != nullptr) { 1246 data_->image_classes_.push_back(hs_.NewHandle(klass)); 1247 } 1248 } 1249 return true; 1250 } 1251 1252 private: 1253 ClinitImageUpdate* const data_; 1254 VariableSizedHandleScope& hs_; 1255 }; 1256 1257 ClinitImageUpdate(VariableSizedHandleScope& hs, 1258 std::unordered_set<std::string>* image_class_descriptors, 1259 Thread* self, 1260 ClassLinker* linker) REQUIRES_SHARED(Locks::mutator_lock_) 1261 : hs_(hs), 1262 image_class_descriptors_(image_class_descriptors), 1263 self_(self) { 1264 CHECK(linker != nullptr); 1265 CHECK(image_class_descriptors != nullptr); 1266 1267 // Make sure nobody interferes with us. 1268 old_cause_ = self->StartAssertNoThreadSuspension("Boot image closure"); 1269 1270 // Find all the already-marked classes. 1271 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); 1272 FindImageClassesVisitor visitor(hs_, this); 1273 linker->VisitClasses(&visitor); 1274 } 1275 1276 void VisitClinitClassesObject(mirror::Object* object) const 1277 REQUIRES_SHARED(Locks::mutator_lock_) { 1278 DCHECK(object != nullptr); 1279 if (marked_objects_.find(object) != marked_objects_.end()) { 1280 // Already processed. 1281 return; 1282 } 1283 1284 // Mark it. 1285 marked_objects_.insert(object); 1286 1287 if (object->IsClass()) { 1288 // Add to the TODO list since MaybeAddToImageClasses may cause thread suspension. Thread 1289 // suspensionb is not safe to do in VisitObjects or VisitReferences. 1290 to_insert_.push_back(hs_.NewHandle(object->AsClass())); 1291 } else { 1292 // Else visit the object's class. 1293 VisitClinitClassesObject(object->GetClass()); 1294 } 1295 1296 // If it is not a DexCache, visit all references. 1297 if (!object->IsDexCache()) { 1298 object->VisitReferences(*this, *this); 1299 } 1300 } 1301 1302 VariableSizedHandleScope& hs_; 1303 mutable std::vector<Handle<mirror::Class>> to_insert_; 1304 mutable std::unordered_set<mirror::Object*> marked_objects_; 1305 std::unordered_set<std::string>* const image_class_descriptors_; 1306 std::vector<Handle<mirror::Class>> image_classes_; 1307 Thread* const self_; 1308 const char* old_cause_; 1309 1310 DISALLOW_COPY_AND_ASSIGN(ClinitImageUpdate); 1311}; 1312 1313void CompilerDriver::UpdateImageClasses(TimingLogger* timings) { 1314 if (GetCompilerOptions().IsBootImage()) { 1315 TimingLogger::ScopedTiming t("UpdateImageClasses", timings); 1316 1317 Runtime* runtime = Runtime::Current(); 1318 1319 // Suspend all threads. 1320 ScopedSuspendAll ssa(__FUNCTION__); 1321 1322 VariableSizedHandleScope hs(Thread::Current()); 1323 std::string error_msg; 1324 std::unique_ptr<ClinitImageUpdate> update(ClinitImageUpdate::Create(hs, 1325 image_classes_.get(), 1326 Thread::Current(), 1327 runtime->GetClassLinker())); 1328 1329 // Do the marking. 1330 update->Walk(); 1331 } 1332} 1333 1334bool CompilerDriver::CanAssumeClassIsLoaded(mirror::Class* klass) { 1335 Runtime* runtime = Runtime::Current(); 1336 if (!runtime->IsAotCompiler()) { 1337 DCHECK(runtime->UseJitCompilation()); 1338 // Having the klass reference here implies that the klass is already loaded. 1339 return true; 1340 } 1341 if (!GetCompilerOptions().IsBootImage()) { 1342 // Assume loaded only if klass is in the boot image. App classes cannot be assumed 1343 // loaded because we don't even know what class loader will be used to load them. 1344 bool class_in_image = runtime->GetHeap()->FindSpaceFromObject(klass, false)->IsImageSpace(); 1345 return class_in_image; 1346 } 1347 std::string temp; 1348 const char* descriptor = klass->GetDescriptor(&temp); 1349 return IsImageClass(descriptor); 1350} 1351 1352void CompilerDriver::MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref) { 1353 MutexLock lock(self, dex_to_dex_references_lock_); 1354 // Since we're compiling one dex file at a time, we need to look for the 1355 // current dex file entry only at the end of dex_to_dex_references_. 1356 if (dex_to_dex_references_.empty() || 1357 &dex_to_dex_references_.back().GetDexFile() != method_ref.dex_file) { 1358 dex_to_dex_references_.emplace_back(*method_ref.dex_file); 1359 } 1360 dex_to_dex_references_.back().GetMethodIndexes().SetBit(method_ref.dex_method_index); 1361} 1362 1363bool CompilerDriver::CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class, 1364 ObjPtr<mirror::Class> resolved_class) { 1365 if (resolved_class == nullptr) { 1366 stats_->TypeNeedsAccessCheck(); 1367 return false; // Unknown class needs access checks. 1368 } 1369 bool is_accessible = resolved_class->IsPublic(); // Public classes are always accessible. 1370 if (!is_accessible) { 1371 if (referrer_class == nullptr) { 1372 stats_->TypeNeedsAccessCheck(); 1373 return false; // Incomplete referrer knowledge needs access check. 1374 } 1375 // Perform access check, will return true if access is ok or false if we're going to have to 1376 // check this at runtime (for example for class loaders). 1377 is_accessible = referrer_class->CanAccess(resolved_class); 1378 } 1379 if (is_accessible) { 1380 stats_->TypeDoesntNeedAccessCheck(); 1381 } else { 1382 stats_->TypeNeedsAccessCheck(); 1383 } 1384 return is_accessible; 1385} 1386 1387bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class, 1388 ObjPtr<mirror::Class> resolved_class, 1389 bool* finalizable) { 1390 if (resolved_class == nullptr) { 1391 stats_->TypeNeedsAccessCheck(); 1392 // Be conservative. 1393 *finalizable = true; 1394 return false; // Unknown class needs access checks. 1395 } 1396 *finalizable = resolved_class->IsFinalizable(); 1397 bool is_accessible = resolved_class->IsPublic(); // Public classes are always accessible. 1398 if (!is_accessible) { 1399 if (referrer_class == nullptr) { 1400 stats_->TypeNeedsAccessCheck(); 1401 return false; // Incomplete referrer knowledge needs access check. 1402 } 1403 // Perform access and instantiable checks, will return true if access is ok or false if we're 1404 // going to have to check this at runtime (for example for class loaders). 1405 is_accessible = referrer_class->CanAccess(resolved_class); 1406 } 1407 bool result = is_accessible && resolved_class->IsInstantiable(); 1408 if (result) { 1409 stats_->TypeDoesntNeedAccessCheck(); 1410 } else { 1411 stats_->TypeNeedsAccessCheck(); 1412 } 1413 return result; 1414} 1415 1416void CompilerDriver::ProcessedInstanceField(bool resolved) { 1417 if (!resolved) { 1418 stats_->UnresolvedInstanceField(); 1419 } else { 1420 stats_->ResolvedInstanceField(); 1421 } 1422} 1423 1424void CompilerDriver::ProcessedStaticField(bool resolved, bool local) { 1425 if (!resolved) { 1426 stats_->UnresolvedStaticField(); 1427 } else if (local) { 1428 stats_->ResolvedLocalStaticField(); 1429 } else { 1430 stats_->ResolvedStaticField(); 1431 } 1432} 1433 1434ArtField* CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, 1435 const DexCompilationUnit* mUnit, bool is_put, 1436 const ScopedObjectAccess& soa) { 1437 // Try to resolve the field and compiling method's class. 1438 ArtField* resolved_field; 1439 mirror::Class* referrer_class; 1440 Handle<mirror::DexCache> dex_cache(mUnit->GetDexCache()); 1441 { 1442 Handle<mirror::ClassLoader> class_loader_handle = mUnit->GetClassLoader(); 1443 resolved_field = ResolveField(soa, dex_cache, class_loader_handle, mUnit, field_idx, false); 1444 referrer_class = resolved_field != nullptr 1445 ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader_handle, mUnit) : nullptr; 1446 } 1447 bool can_link = false; 1448 if (resolved_field != nullptr && referrer_class != nullptr) { 1449 std::pair<bool, bool> fast_path = IsFastInstanceField( 1450 dex_cache.Get(), referrer_class, resolved_field, field_idx); 1451 can_link = is_put ? fast_path.second : fast_path.first; 1452 } 1453 ProcessedInstanceField(can_link); 1454 return can_link ? resolved_field : nullptr; 1455} 1456 1457bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit, 1458 bool is_put, MemberOffset* field_offset, 1459 bool* is_volatile) { 1460 ScopedObjectAccess soa(Thread::Current()); 1461 ArtField* resolved_field = ComputeInstanceFieldInfo(field_idx, mUnit, is_put, soa); 1462 1463 if (resolved_field == nullptr) { 1464 // Conservative defaults. 1465 *is_volatile = true; 1466 *field_offset = MemberOffset(static_cast<size_t>(-1)); 1467 return false; 1468 } else { 1469 *is_volatile = resolved_field->IsVolatile(); 1470 *field_offset = resolved_field->GetOffset(); 1471 return true; 1472 } 1473} 1474 1475const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file, 1476 uint32_t method_idx) const { 1477 MethodReference ref(dex_file, method_idx); 1478 return verification_results_->GetVerifiedMethod(ref); 1479} 1480 1481bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) { 1482 if (!compiler_options_->IsVerificationEnabled()) { 1483 // If we didn't verify, every cast has to be treated as non-safe. 1484 return false; 1485 } 1486 DCHECK(mUnit->GetVerifiedMethod() != nullptr); 1487 bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc); 1488 if (result) { 1489 stats_->SafeCast(); 1490 } else { 1491 stats_->NotASafeCast(); 1492 } 1493 return result; 1494} 1495 1496class CompilationVisitor { 1497 public: 1498 virtual ~CompilationVisitor() {} 1499 virtual void Visit(size_t index) = 0; 1500}; 1501 1502class ParallelCompilationManager { 1503 public: 1504 ParallelCompilationManager(ClassLinker* class_linker, 1505 jobject class_loader, 1506 CompilerDriver* compiler, 1507 const DexFile* dex_file, 1508 const std::vector<const DexFile*>& dex_files, 1509 ThreadPool* thread_pool) 1510 : index_(0), 1511 class_linker_(class_linker), 1512 class_loader_(class_loader), 1513 compiler_(compiler), 1514 dex_file_(dex_file), 1515 dex_files_(dex_files), 1516 thread_pool_(thread_pool) {} 1517 1518 ClassLinker* GetClassLinker() const { 1519 CHECK(class_linker_ != nullptr); 1520 return class_linker_; 1521 } 1522 1523 jobject GetClassLoader() const { 1524 return class_loader_; 1525 } 1526 1527 CompilerDriver* GetCompiler() const { 1528 CHECK(compiler_ != nullptr); 1529 return compiler_; 1530 } 1531 1532 const DexFile* GetDexFile() const { 1533 CHECK(dex_file_ != nullptr); 1534 return dex_file_; 1535 } 1536 1537 const std::vector<const DexFile*>& GetDexFiles() const { 1538 return dex_files_; 1539 } 1540 1541 void ForAll(size_t begin, size_t end, CompilationVisitor* visitor, size_t work_units) 1542 REQUIRES(!*Locks::mutator_lock_) { 1543 Thread* self = Thread::Current(); 1544 self->AssertNoPendingException(); 1545 CHECK_GT(work_units, 0U); 1546 1547 index_.StoreRelaxed(begin); 1548 for (size_t i = 0; i < work_units; ++i) { 1549 thread_pool_->AddTask(self, new ForAllClosure(this, end, visitor)); 1550 } 1551 thread_pool_->StartWorkers(self); 1552 1553 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker 1554 // thread destructor's called below perform join). 1555 CHECK_NE(self->GetState(), kRunnable); 1556 1557 // Wait for all the worker threads to finish. 1558 thread_pool_->Wait(self, true, false); 1559 1560 // And stop the workers accepting jobs. 1561 thread_pool_->StopWorkers(self); 1562 } 1563 1564 size_t NextIndex() { 1565 return index_.FetchAndAddSequentiallyConsistent(1); 1566 } 1567 1568 private: 1569 class ForAllClosure : public Task { 1570 public: 1571 ForAllClosure(ParallelCompilationManager* manager, size_t end, CompilationVisitor* visitor) 1572 : manager_(manager), 1573 end_(end), 1574 visitor_(visitor) {} 1575 1576 virtual void Run(Thread* self) { 1577 while (true) { 1578 const size_t index = manager_->NextIndex(); 1579 if (UNLIKELY(index >= end_)) { 1580 break; 1581 } 1582 visitor_->Visit(index); 1583 self->AssertNoPendingException(); 1584 } 1585 } 1586 1587 virtual void Finalize() { 1588 delete this; 1589 } 1590 1591 private: 1592 ParallelCompilationManager* const manager_; 1593 const size_t end_; 1594 CompilationVisitor* const visitor_; 1595 }; 1596 1597 AtomicInteger index_; 1598 ClassLinker* const class_linker_; 1599 const jobject class_loader_; 1600 CompilerDriver* const compiler_; 1601 const DexFile* const dex_file_; 1602 const std::vector<const DexFile*>& dex_files_; 1603 ThreadPool* const thread_pool_; 1604 1605 DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager); 1606}; 1607 1608// A fast version of SkipClass above if the class pointer is available 1609// that avoids the expensive FindInClassPath search. 1610static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass) 1611 REQUIRES_SHARED(Locks::mutator_lock_) { 1612 DCHECK(klass != nullptr); 1613 const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile(); 1614 if (&dex_file != &original_dex_file) { 1615 if (class_loader == nullptr) { 1616 LOG(WARNING) << "Skipping class " << klass->PrettyDescriptor() << " from " 1617 << dex_file.GetLocation() << " previously found in " 1618 << original_dex_file.GetLocation(); 1619 } 1620 return true; 1621 } 1622 return false; 1623} 1624 1625static void CheckAndClearResolveException(Thread* self) 1626 REQUIRES_SHARED(Locks::mutator_lock_) { 1627 CHECK(self->IsExceptionPending()); 1628 mirror::Throwable* exception = self->GetException(); 1629 std::string temp; 1630 const char* descriptor = exception->GetClass()->GetDescriptor(&temp); 1631 const char* expected_exceptions[] = { 1632 "Ljava/lang/IllegalAccessError;", 1633 "Ljava/lang/IncompatibleClassChangeError;", 1634 "Ljava/lang/InstantiationError;", 1635 "Ljava/lang/LinkageError;", 1636 "Ljava/lang/NoClassDefFoundError;", 1637 "Ljava/lang/NoSuchFieldError;", 1638 "Ljava/lang/NoSuchMethodError;" 1639 }; 1640 bool found = false; 1641 for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) { 1642 if (strcmp(descriptor, expected_exceptions[i]) == 0) { 1643 found = true; 1644 } 1645 } 1646 if (!found) { 1647 LOG(FATAL) << "Unexpected exception " << exception->Dump(); 1648 } 1649 self->ClearException(); 1650} 1651 1652bool CompilerDriver::RequiresConstructorBarrier(const DexFile& dex_file, 1653 uint16_t class_def_idx) const { 1654 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); 1655 const uint8_t* class_data = dex_file.GetClassData(class_def); 1656 if (class_data == nullptr) { 1657 // Empty class such as a marker interface. 1658 return false; 1659 } 1660 ClassDataItemIterator it(dex_file, class_data); 1661 it.SkipStaticFields(); 1662 // We require a constructor barrier if there are final instance fields. 1663 while (it.HasNextInstanceField()) { 1664 if (it.MemberIsFinal()) { 1665 return true; 1666 } 1667 it.Next(); 1668 } 1669 return false; 1670} 1671 1672class ResolveClassFieldsAndMethodsVisitor : public CompilationVisitor { 1673 public: 1674 explicit ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager* manager) 1675 : manager_(manager) {} 1676 1677 void Visit(size_t class_def_index) OVERRIDE REQUIRES(!Locks::mutator_lock_) { 1678 ATRACE_CALL(); 1679 Thread* const self = Thread::Current(); 1680 jobject jclass_loader = manager_->GetClassLoader(); 1681 const DexFile& dex_file = *manager_->GetDexFile(); 1682 ClassLinker* class_linker = manager_->GetClassLinker(); 1683 1684 // If an instance field is final then we need to have a barrier on the return, static final 1685 // fields are assigned within the lock held for class initialization. Conservatively assume 1686 // constructor barriers are always required. 1687 bool requires_constructor_barrier = true; 1688 1689 // Method and Field are the worst. We can't resolve without either 1690 // context from the code use (to disambiguate virtual vs direct 1691 // method and instance vs static field) or from class 1692 // definitions. While the compiler will resolve what it can as it 1693 // needs it, here we try to resolve fields and methods used in class 1694 // definitions, since many of them many never be referenced by 1695 // generated code. 1696 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 1697 ScopedObjectAccess soa(self); 1698 StackHandleScope<2> hs(soa.Self()); 1699 Handle<mirror::ClassLoader> class_loader( 1700 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 1701 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache( 1702 soa.Self(), dex_file))); 1703 // Resolve the class. 1704 mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache, 1705 class_loader); 1706 bool resolve_fields_and_methods; 1707 if (klass == nullptr) { 1708 // Class couldn't be resolved, for example, super-class is in a different dex file. Don't 1709 // attempt to resolve methods and fields when there is no declaring class. 1710 CheckAndClearResolveException(soa.Self()); 1711 resolve_fields_and_methods = false; 1712 } else { 1713 // We successfully resolved a class, should we skip it? 1714 if (SkipClass(jclass_loader, dex_file, klass)) { 1715 return; 1716 } 1717 // We want to resolve the methods and fields eagerly. 1718 resolve_fields_and_methods = true; 1719 } 1720 // Note the class_data pointer advances through the headers, 1721 // static fields, instance fields, direct methods, and virtual 1722 // methods. 1723 const uint8_t* class_data = dex_file.GetClassData(class_def); 1724 if (class_data == nullptr) { 1725 // Empty class such as a marker interface. 1726 requires_constructor_barrier = false; 1727 } else { 1728 ClassDataItemIterator it(dex_file, class_data); 1729 while (it.HasNextStaticField()) { 1730 if (resolve_fields_and_methods) { 1731 ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), 1732 dex_cache, class_loader, true); 1733 if (field == nullptr) { 1734 CheckAndClearResolveException(soa.Self()); 1735 } 1736 } 1737 it.Next(); 1738 } 1739 // We require a constructor barrier if there are final instance fields. 1740 requires_constructor_barrier = false; 1741 while (it.HasNextInstanceField()) { 1742 if (it.MemberIsFinal()) { 1743 requires_constructor_barrier = true; 1744 } 1745 if (resolve_fields_and_methods) { 1746 ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(), 1747 dex_cache, class_loader, false); 1748 if (field == nullptr) { 1749 CheckAndClearResolveException(soa.Self()); 1750 } 1751 } 1752 it.Next(); 1753 } 1754 if (resolve_fields_and_methods) { 1755 while (it.HasNextDirectMethod()) { 1756 ArtMethod* method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( 1757 dex_file, it.GetMemberIndex(), dex_cache, class_loader, nullptr, 1758 it.GetMethodInvokeType(class_def)); 1759 if (method == nullptr) { 1760 CheckAndClearResolveException(soa.Self()); 1761 } 1762 it.Next(); 1763 } 1764 while (it.HasNextVirtualMethod()) { 1765 ArtMethod* method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( 1766 dex_file, it.GetMemberIndex(), dex_cache, class_loader, nullptr, 1767 it.GetMethodInvokeType(class_def)); 1768 if (method == nullptr) { 1769 CheckAndClearResolveException(soa.Self()); 1770 } 1771 it.Next(); 1772 } 1773 DCHECK(!it.HasNext()); 1774 } 1775 } 1776 manager_->GetCompiler()->SetRequiresConstructorBarrier(self, 1777 &dex_file, 1778 class_def_index, 1779 requires_constructor_barrier); 1780 } 1781 1782 private: 1783 const ParallelCompilationManager* const manager_; 1784}; 1785 1786class ResolveTypeVisitor : public CompilationVisitor { 1787 public: 1788 explicit ResolveTypeVisitor(const ParallelCompilationManager* manager) : manager_(manager) { 1789 } 1790 void Visit(size_t type_idx) OVERRIDE REQUIRES(!Locks::mutator_lock_) { 1791 // Class derived values are more complicated, they require the linker and loader. 1792 ScopedObjectAccess soa(Thread::Current()); 1793 ClassLinker* class_linker = manager_->GetClassLinker(); 1794 const DexFile& dex_file = *manager_->GetDexFile(); 1795 StackHandleScope<2> hs(soa.Self()); 1796 Handle<mirror::ClassLoader> class_loader( 1797 hs.NewHandle(soa.Decode<mirror::ClassLoader>(manager_->GetClassLoader()))); 1798 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->RegisterDexFile( 1799 dex_file, 1800 class_loader.Get()))); 1801 ObjPtr<mirror::Class> klass = (dex_cache != nullptr) 1802 ? class_linker->ResolveType(dex_file, dex::TypeIndex(type_idx), dex_cache, class_loader) 1803 : nullptr; 1804 1805 if (klass == nullptr) { 1806 soa.Self()->AssertPendingException(); 1807 mirror::Throwable* exception = soa.Self()->GetException(); 1808 VLOG(compiler) << "Exception during type resolution: " << exception->Dump(); 1809 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) { 1810 // There's little point continuing compilation if the heap is exhausted. 1811 LOG(FATAL) << "Out of memory during type resolution for compilation"; 1812 } 1813 soa.Self()->ClearException(); 1814 } 1815 } 1816 1817 private: 1818 const ParallelCompilationManager* const manager_; 1819}; 1820 1821void CompilerDriver::ResolveDexFile(jobject class_loader, 1822 const DexFile& dex_file, 1823 const std::vector<const DexFile*>& dex_files, 1824 ThreadPool* thread_pool, 1825 size_t thread_count, 1826 TimingLogger* timings) { 1827 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1828 1829 // TODO: we could resolve strings here, although the string table is largely filled with class 1830 // and method names. 1831 1832 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files, 1833 thread_pool); 1834 if (GetCompilerOptions().IsBootImage()) { 1835 // For images we resolve all types, such as array, whereas for applications just those with 1836 // classdefs are resolved by ResolveClassFieldsAndMethods. 1837 TimingLogger::ScopedTiming t("Resolve Types", timings); 1838 ResolveTypeVisitor visitor(&context); 1839 context.ForAll(0, dex_file.NumTypeIds(), &visitor, thread_count); 1840 } 1841 1842 TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings); 1843 ResolveClassFieldsAndMethodsVisitor visitor(&context); 1844 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count); 1845} 1846 1847void CompilerDriver::SetVerified(jobject class_loader, 1848 const std::vector<const DexFile*>& dex_files, 1849 TimingLogger* timings) { 1850 // This can be run in parallel. 1851 for (const DexFile* dex_file : dex_files) { 1852 CHECK(dex_file != nullptr); 1853 SetVerifiedDexFile(class_loader, 1854 *dex_file, 1855 dex_files, 1856 parallel_thread_pool_.get(), 1857 parallel_thread_count_, 1858 timings); 1859 } 1860} 1861 1862static void PopulateVerifiedMethods(const DexFile& dex_file, 1863 uint32_t class_def_index, 1864 VerificationResults* verification_results) { 1865 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 1866 const uint8_t* class_data = dex_file.GetClassData(class_def); 1867 if (class_data == nullptr) { 1868 return; 1869 } 1870 ClassDataItemIterator it(dex_file, class_data); 1871 it.SkipAllFields(); 1872 1873 while (it.HasNextDirectMethod()) { 1874 verification_results->CreateVerifiedMethodFor(MethodReference(&dex_file, it.GetMemberIndex())); 1875 it.Next(); 1876 } 1877 1878 while (it.HasNextVirtualMethod()) { 1879 verification_results->CreateVerifiedMethodFor(MethodReference(&dex_file, it.GetMemberIndex())); 1880 it.Next(); 1881 } 1882 DCHECK(!it.HasNext()); 1883} 1884 1885static void LoadAndUpdateStatus(const DexFile& dex_file, 1886 const DexFile::ClassDef& class_def, 1887 mirror::Class::Status status, 1888 Handle<mirror::ClassLoader> class_loader, 1889 Thread* self) 1890 REQUIRES_SHARED(Locks::mutator_lock_) { 1891 StackHandleScope<1> hs(self); 1892 const char* descriptor = dex_file.GetClassDescriptor(class_def); 1893 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1894 Handle<mirror::Class> cls(hs.NewHandle<mirror::Class>( 1895 class_linker->FindClass(self, descriptor, class_loader))); 1896 if (cls != nullptr) { 1897 // Check that the class is resolved with the current dex file. We might get 1898 // a boot image class, or a class in a different dex file for multidex, and 1899 // we should not update the status in that case. 1900 if (&cls->GetDexFile() == &dex_file) { 1901 ObjectLock<mirror::Class> lock(self, cls); 1902 mirror::Class::SetStatus(cls, status, self); 1903 } 1904 } else { 1905 DCHECK(self->IsExceptionPending()); 1906 self->ClearException(); 1907 } 1908} 1909 1910bool CompilerDriver::FastVerify(jobject jclass_loader, 1911 const std::vector<const DexFile*>& dex_files, 1912 TimingLogger* timings) { 1913 verifier::VerifierDeps* verifier_deps = 1914 Runtime::Current()->GetCompilerCallbacks()->GetVerifierDeps(); 1915 // If there is an existing `VerifierDeps`, try to use it for fast verification. 1916 if (verifier_deps == nullptr) { 1917 return false; 1918 } 1919 TimingLogger::ScopedTiming t("Fast Verify", timings); 1920 ScopedObjectAccess soa(Thread::Current()); 1921 StackHandleScope<2> hs(soa.Self()); 1922 Handle<mirror::ClassLoader> class_loader( 1923 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 1924 if (!verifier_deps->ValidateDependencies(class_loader, soa.Self())) { 1925 return false; 1926 } 1927 1928 bool compiler_only_verifies = !GetCompilerOptions().IsAnyCompilationEnabled(); 1929 1930 // We successfully validated the dependencies, now update class status 1931 // of verified classes. Note that the dependencies also record which classes 1932 // could not be fully verified; we could try again, but that would hurt verification 1933 // time. So instead we assume these classes still need to be verified at 1934 // runtime. 1935 for (const DexFile* dex_file : dex_files) { 1936 // Fetch the list of unverified classes. 1937 const std::set<dex::TypeIndex>& unverified_classes = 1938 verifier_deps->GetUnverifiedClasses(*dex_file); 1939 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) { 1940 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i); 1941 if (unverified_classes.find(class_def.class_idx_) == unverified_classes.end()) { 1942 if (compiler_only_verifies) { 1943 // Just update the compiled_classes_ map. The compiler doesn't need to resolve 1944 // the type. 1945 DexFileReference ref(dex_file, i); 1946 mirror::Class::Status existing = mirror::Class::kStatusNotReady; 1947 DCHECK(compiled_classes_.Get(ref, &existing)) << ref.dex_file->GetLocation(); 1948 ClassStateTable::InsertResult result = 1949 compiled_classes_.Insert(ref, existing, mirror::Class::kStatusVerified); 1950 CHECK_EQ(result, ClassStateTable::kInsertResultSuccess); 1951 } else { 1952 // Update the class status, so later compilation stages know they don't need to verify 1953 // the class. 1954 LoadAndUpdateStatus( 1955 *dex_file, class_def, mirror::Class::kStatusVerified, class_loader, soa.Self()); 1956 // Create `VerifiedMethod`s for each methods, the compiler expects one for 1957 // quickening or compiling. 1958 // Note that this means: 1959 // - We're only going to compile methods that did verify. 1960 // - Quickening will not do checkcast ellision. 1961 // TODO(ngeoffray): Reconsider this once we refactor compiler filters. 1962 PopulateVerifiedMethods(*dex_file, i, verification_results_); 1963 } 1964 } else if (!compiler_only_verifies) { 1965 // Make sure later compilation stages know they should not try to verify 1966 // this class again. 1967 LoadAndUpdateStatus(*dex_file, 1968 class_def, 1969 mirror::Class::kStatusRetryVerificationAtRuntime, 1970 class_loader, 1971 soa.Self()); 1972 } 1973 } 1974 } 1975 return true; 1976} 1977 1978void CompilerDriver::Verify(jobject jclass_loader, 1979 const std::vector<const DexFile*>& dex_files, 1980 TimingLogger* timings) { 1981 // Always add the dex files to compiled_classes_. This happens for all compiler filters. 1982 for (const DexFile* dex_file : dex_files) { 1983 if (!compiled_classes_.HaveDexFile(dex_file)) { 1984 compiled_classes_.AddDexFile(dex_file, dex_file->NumClassDefs()); 1985 } 1986 } 1987 1988 if (FastVerify(jclass_loader, dex_files, timings)) { 1989 return; 1990 } 1991 1992 // If there is no existing `verifier_deps` (because of non-existing vdex), or 1993 // the existing `verifier_deps` is not valid anymore, create a new one for 1994 // non boot image compilation. The verifier will need it to record the new dependencies. 1995 // Then dex2oat can update the vdex file with these new dependencies. 1996 if (!GetCompilerOptions().IsBootImage()) { 1997 // Create the main VerifierDeps, and set it to this thread. 1998 verifier::VerifierDeps* verifier_deps = new verifier::VerifierDeps(dex_files); 1999 Runtime::Current()->GetCompilerCallbacks()->SetVerifierDeps(verifier_deps); 2000 Thread::Current()->SetVerifierDeps(verifier_deps); 2001 // Create per-thread VerifierDeps to avoid contention on the main one. 2002 // We will merge them after verification. 2003 for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) { 2004 worker->GetThread()->SetVerifierDeps(new verifier::VerifierDeps(dex_files)); 2005 } 2006 } 2007 2008 // Verification updates VerifierDeps and needs to run single-threaded to be deterministic. 2009 bool force_determinism = GetCompilerOptions().IsForceDeterminism(); 2010 ThreadPool* verify_thread_pool = 2011 force_determinism ? single_thread_pool_.get() : parallel_thread_pool_.get(); 2012 size_t verify_thread_count = force_determinism ? 1U : parallel_thread_count_; 2013 for (const DexFile* dex_file : dex_files) { 2014 CHECK(dex_file != nullptr); 2015 VerifyDexFile(jclass_loader, 2016 *dex_file, 2017 dex_files, 2018 verify_thread_pool, 2019 verify_thread_count, 2020 timings); 2021 } 2022 2023 if (!GetCompilerOptions().IsBootImage()) { 2024 // Merge all VerifierDeps into the main one. 2025 verifier::VerifierDeps* verifier_deps = Thread::Current()->GetVerifierDeps(); 2026 for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) { 2027 verifier::VerifierDeps* thread_deps = worker->GetThread()->GetVerifierDeps(); 2028 worker->GetThread()->SetVerifierDeps(nullptr); 2029 verifier_deps->MergeWith(*thread_deps, dex_files);; 2030 delete thread_deps; 2031 } 2032 Thread::Current()->SetVerifierDeps(nullptr); 2033 } 2034} 2035 2036class VerifyClassVisitor : public CompilationVisitor { 2037 public: 2038 VerifyClassVisitor(const ParallelCompilationManager* manager, verifier::HardFailLogMode log_level) 2039 : manager_(manager), log_level_(log_level) {} 2040 2041 virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE { 2042 ATRACE_CALL(); 2043 ScopedObjectAccess soa(Thread::Current()); 2044 const DexFile& dex_file = *manager_->GetDexFile(); 2045 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 2046 const char* descriptor = dex_file.GetClassDescriptor(class_def); 2047 ClassLinker* class_linker = manager_->GetClassLinker(); 2048 jobject jclass_loader = manager_->GetClassLoader(); 2049 StackHandleScope<3> hs(soa.Self()); 2050 Handle<mirror::ClassLoader> class_loader( 2051 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 2052 Handle<mirror::Class> klass( 2053 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader))); 2054 verifier::FailureKind failure_kind; 2055 if (klass == nullptr) { 2056 CHECK(soa.Self()->IsExceptionPending()); 2057 soa.Self()->ClearException(); 2058 2059 /* 2060 * At compile time, we can still structurally verify the class even if FindClass fails. 2061 * This is to ensure the class is structurally sound for compilation. An unsound class 2062 * will be rejected by the verifier and later skipped during compilation in the compiler. 2063 */ 2064 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache( 2065 soa.Self(), dex_file))); 2066 std::string error_msg; 2067 failure_kind = 2068 verifier::MethodVerifier::VerifyClass(soa.Self(), 2069 &dex_file, 2070 dex_cache, 2071 class_loader, 2072 class_def, 2073 Runtime::Current()->GetCompilerCallbacks(), 2074 true /* allow soft failures */, 2075 log_level_, 2076 &error_msg); 2077 if (failure_kind == verifier::FailureKind::kHardFailure) { 2078 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor) 2079 << " because: " << error_msg; 2080 manager_->GetCompiler()->SetHadHardVerifierFailure(); 2081 } else { 2082 // Force a soft failure for the VerifierDeps. This is a sanity measure, as 2083 // the vdex file already records that the class hasn't been resolved. It avoids 2084 // trying to do future verification optimizations when processing the vdex file. 2085 DCHECK(failure_kind == verifier::FailureKind::kSoftFailure || 2086 failure_kind == verifier::FailureKind::kNoFailure) 2087 << failure_kind; 2088 failure_kind = verifier::FailureKind::kSoftFailure; 2089 } 2090 } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) { 2091 CHECK(klass->IsResolved()) << klass->PrettyClass(); 2092 failure_kind = class_linker->VerifyClass(soa.Self(), klass, log_level_); 2093 2094 if (klass->IsErroneous()) { 2095 // ClassLinker::VerifyClass throws, which isn't useful in the compiler. 2096 CHECK(soa.Self()->IsExceptionPending()); 2097 soa.Self()->ClearException(); 2098 manager_->GetCompiler()->SetHadHardVerifierFailure(); 2099 } 2100 2101 CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerified() || klass->IsErroneous()) 2102 << klass->PrettyDescriptor() << ": state=" << klass->GetStatus(); 2103 2104 // Class has a meaningful status for the compiler now, record it. 2105 ClassReference ref(manager_->GetDexFile(), class_def_index); 2106 manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus()); 2107 2108 // It is *very* problematic if there are verification errors in the boot classpath. For example, 2109 // we rely on things working OK without verification when the decryption dialog is brought up. 2110 // So abort in a debug build if we find this violated. 2111 if (kIsDebugBuild) { 2112 // TODO(narayan): Remove this special case for signature polymorphic 2113 // invokes once verifier support is fully implemented. 2114 if (manager_->GetCompiler()->GetCompilerOptions().IsBootImage() && 2115 !android::base::StartsWith(descriptor, "Ljava/lang/invoke/")) { 2116 DCHECK(klass->IsVerified()) << "Boot classpath class " << klass->PrettyClass() 2117 << " failed to fully verify: state= " << klass->GetStatus(); 2118 } 2119 if (klass->IsVerified()) { 2120 DCHECK_EQ(failure_kind, verifier::FailureKind::kNoFailure); 2121 } else if (klass->ShouldVerifyAtRuntime()) { 2122 DCHECK_EQ(failure_kind, verifier::FailureKind::kSoftFailure); 2123 } else { 2124 DCHECK_EQ(failure_kind, verifier::FailureKind::kHardFailure); 2125 } 2126 } 2127 } else { 2128 // Make the skip a soft failure, essentially being considered as verify at runtime. 2129 failure_kind = verifier::FailureKind::kSoftFailure; 2130 } 2131 verifier::VerifierDeps::MaybeRecordVerificationStatus( 2132 dex_file, class_def.class_idx_, failure_kind); 2133 soa.Self()->AssertNoPendingException(); 2134 } 2135 2136 private: 2137 const ParallelCompilationManager* const manager_; 2138 const verifier::HardFailLogMode log_level_; 2139}; 2140 2141void CompilerDriver::VerifyDexFile(jobject class_loader, 2142 const DexFile& dex_file, 2143 const std::vector<const DexFile*>& dex_files, 2144 ThreadPool* thread_pool, 2145 size_t thread_count, 2146 TimingLogger* timings) { 2147 TimingLogger::ScopedTiming t("Verify Dex File", timings); 2148 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 2149 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files, 2150 thread_pool); 2151 verifier::HardFailLogMode log_level = GetCompilerOptions().AbortOnHardVerifierFailure() 2152 ? verifier::HardFailLogMode::kLogInternalFatal 2153 : verifier::HardFailLogMode::kLogWarning; 2154 VerifyClassVisitor visitor(&context, log_level); 2155 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count); 2156} 2157 2158class SetVerifiedClassVisitor : public CompilationVisitor { 2159 public: 2160 explicit SetVerifiedClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {} 2161 2162 virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE { 2163 ATRACE_CALL(); 2164 ScopedObjectAccess soa(Thread::Current()); 2165 const DexFile& dex_file = *manager_->GetDexFile(); 2166 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 2167 const char* descriptor = dex_file.GetClassDescriptor(class_def); 2168 ClassLinker* class_linker = manager_->GetClassLinker(); 2169 jobject jclass_loader = manager_->GetClassLoader(); 2170 StackHandleScope<3> hs(soa.Self()); 2171 Handle<mirror::ClassLoader> class_loader( 2172 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 2173 Handle<mirror::Class> klass( 2174 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader))); 2175 // Class might have failed resolution. Then don't set it to verified. 2176 if (klass != nullptr) { 2177 // Only do this if the class is resolved. If even resolution fails, quickening will go very, 2178 // very wrong. 2179 if (klass->IsResolved() && !klass->IsErroneousResolved()) { 2180 if (klass->GetStatus() < mirror::Class::kStatusVerified) { 2181 ObjectLock<mirror::Class> lock(soa.Self(), klass); 2182 // Set class status to verified. 2183 mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, soa.Self()); 2184 // Mark methods as pre-verified. If we don't do this, the interpreter will run with 2185 // access checks. 2186 klass->SetSkipAccessChecksFlagOnAllMethods( 2187 GetInstructionSetPointerSize(manager_->GetCompiler()->GetInstructionSet())); 2188 klass->SetVerificationAttempted(); 2189 } 2190 // Record the final class status if necessary. 2191 ClassReference ref(manager_->GetDexFile(), class_def_index); 2192 manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus()); 2193 } 2194 } else { 2195 Thread* self = soa.Self(); 2196 DCHECK(self->IsExceptionPending()); 2197 self->ClearException(); 2198 } 2199 } 2200 2201 private: 2202 const ParallelCompilationManager* const manager_; 2203}; 2204 2205void CompilerDriver::SetVerifiedDexFile(jobject class_loader, 2206 const DexFile& dex_file, 2207 const std::vector<const DexFile*>& dex_files, 2208 ThreadPool* thread_pool, 2209 size_t thread_count, 2210 TimingLogger* timings) { 2211 TimingLogger::ScopedTiming t("Verify Dex File", timings); 2212 if (!compiled_classes_.HaveDexFile(&dex_file)) { 2213 compiled_classes_.AddDexFile(&dex_file, dex_file.NumClassDefs()); 2214 } 2215 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 2216 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files, 2217 thread_pool); 2218 SetVerifiedClassVisitor visitor(&context); 2219 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count); 2220} 2221 2222class InitializeClassVisitor : public CompilationVisitor { 2223 public: 2224 explicit InitializeClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {} 2225 2226 void Visit(size_t class_def_index) OVERRIDE { 2227 ATRACE_CALL(); 2228 jobject jclass_loader = manager_->GetClassLoader(); 2229 const DexFile& dex_file = *manager_->GetDexFile(); 2230 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 2231 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_); 2232 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_); 2233 2234 ScopedObjectAccess soa(Thread::Current()); 2235 StackHandleScope<3> hs(soa.Self()); 2236 Handle<mirror::ClassLoader> class_loader( 2237 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 2238 Handle<mirror::Class> klass( 2239 hs.NewHandle(manager_->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader))); 2240 2241 if (klass != nullptr && !SkipClass(manager_->GetClassLoader(), dex_file, klass.Get())) { 2242 TryInitializeClass(klass, class_loader); 2243 } 2244 // Clear any class not found or verification exceptions. 2245 soa.Self()->ClearException(); 2246 } 2247 2248 // A helper function for initializing klass. 2249 void TryInitializeClass(Handle<mirror::Class> klass, Handle<mirror::ClassLoader>& class_loader) 2250 REQUIRES_SHARED(Locks::mutator_lock_) { 2251 const DexFile& dex_file = klass->GetDexFile(); 2252 const DexFile::ClassDef* class_def = klass->GetClassDef(); 2253 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def->class_idx_); 2254 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_); 2255 ScopedObjectAccessUnchecked soa(Thread::Current()); 2256 StackHandleScope<3> hs(soa.Self()); 2257 const bool is_boot_image = manager_->GetCompiler()->GetCompilerOptions().IsBootImage(); 2258 const bool is_app_image = manager_->GetCompiler()->GetCompilerOptions().IsAppImage(); 2259 2260 mirror::Class::Status old_status = klass->GetStatus(); 2261 // Don't initialize classes in boot space when compiling app image 2262 if (is_app_image && klass->IsBootStrapClassLoaded()) { 2263 // Also return early and don't store the class status in the recorded class status. 2264 return; 2265 } 2266 // Only try to initialize classes that were successfully verified. 2267 if (klass->IsVerified()) { 2268 // Attempt to initialize the class but bail if we either need to initialize the super-class 2269 // or static fields. 2270 manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, false); 2271 old_status = klass->GetStatus(); 2272 if (!klass->IsInitialized()) { 2273 // We don't want non-trivial class initialization occurring on multiple threads due to 2274 // deadlock problems. For example, a parent class is initialized (holding its lock) that 2275 // refers to a sub-class in its static/class initializer causing it to try to acquire the 2276 // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock) 2277 // after first initializing its parents, whose locks are acquired. This leads to a 2278 // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock. 2279 // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather 2280 // than use a special Object for the purpose we use the Class of java.lang.Class. 2281 Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass())); 2282 ObjectLock<mirror::Class> lock(soa.Self(), h_klass); 2283 // Attempt to initialize allowing initialization of parent classes but still not static 2284 // fields. 2285 // Initialize dependencies first only for app image, to make TryInitialize recursive. 2286 bool is_superclass_initialized = !is_app_image ? true : 2287 InitializeDependencies(klass, class_loader, soa.Self()); 2288 if (!is_app_image || (is_app_image && is_superclass_initialized)) { 2289 manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, true); 2290 } 2291 // Otherwise it's in app image but superclasses can't be initialized, no need to proceed. 2292 old_status = klass->GetStatus(); 2293 2294 bool too_many_encoded_fields = false; 2295 if (!is_boot_image && klass->NumStaticFields() > kMaxEncodedFields) { 2296 too_many_encoded_fields = true; 2297 } 2298 // If the class was not initialized, we can proceed to see if we can initialize static 2299 // fields. Limit the max number of encoded fields. 2300 if (!klass->IsInitialized() && 2301 (is_app_image || is_boot_image) && 2302 is_superclass_initialized && 2303 !too_many_encoded_fields && 2304 manager_->GetCompiler()->IsImageClass(descriptor)) { 2305 bool can_init_static_fields = false; 2306 if (is_boot_image) { 2307 // We need to initialize static fields, we only do this for image classes that aren't 2308 // marked with the $NoPreloadHolder (which implies this should not be initialized 2309 // early). 2310 can_init_static_fields = !StringPiece(descriptor).ends_with("$NoPreloadHolder;"); 2311 } else { 2312 CHECK(is_app_image); 2313 // The boot image case doesn't need to recursively initialize the dependencies with 2314 // special logic since the class linker already does this. 2315 can_init_static_fields = 2316 !soa.Self()->IsExceptionPending() && 2317 is_superclass_initialized && 2318 NoClinitInDependency(klass, soa.Self(), &class_loader); 2319 // TODO The checking for clinit can be removed since it's already 2320 // checked when init superclass. Currently keep it because it contains 2321 // processing of intern strings. Will be removed later when intern strings 2322 // and clinit are both initialized. 2323 } 2324 2325 if (can_init_static_fields) { 2326 VLOG(compiler) << "Initializing: " << descriptor; 2327 // TODO multithreading support. We should ensure the current compilation thread has 2328 // exclusive access to the runtime and the transaction. To achieve this, we could use 2329 // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity 2330 // checks in Thread::AssertThreadSuspensionIsAllowable. 2331 Runtime* const runtime = Runtime::Current(); 2332 Transaction transaction; 2333 2334 // Run the class initializer in transaction mode. 2335 runtime->EnterTransactionMode(&transaction); 2336 bool success = manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, true, 2337 true); 2338 // TODO we detach transaction from runtime to indicate we quit the transactional 2339 // mode which prevents the GC from visiting objects modified during the transaction. 2340 // Ensure GC is not run so don't access freed objects when aborting transaction. 2341 2342 { 2343 ScopedAssertNoThreadSuspension ants("Transaction end"); 2344 runtime->ExitTransactionMode(); 2345 2346 if (!success) { 2347 CHECK(soa.Self()->IsExceptionPending()); 2348 mirror::Throwable* exception = soa.Self()->GetException(); 2349 VLOG(compiler) << "Initialization of " << descriptor << " aborted because of " 2350 << exception->Dump(); 2351 std::ostream* file_log = manager_->GetCompiler()-> 2352 GetCompilerOptions().GetInitFailureOutput(); 2353 if (file_log != nullptr) { 2354 *file_log << descriptor << "\n"; 2355 *file_log << exception->Dump() << "\n"; 2356 } 2357 soa.Self()->ClearException(); 2358 transaction.Rollback(); 2359 CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored"; 2360 } else if (is_boot_image) { 2361 // For boot image, we want to put the updated status in the oat class since we can't 2362 // reject the image anyways. 2363 old_status = klass->GetStatus(); 2364 } 2365 } 2366 2367 if (!success) { 2368 // On failure, still intern strings of static fields and seen in <clinit>, as these 2369 // will be created in the zygote. This is separated from the transaction code just 2370 // above as we will allocate strings, so must be allowed to suspend. 2371 if (&klass->GetDexFile() == manager_->GetDexFile()) { 2372 InternStrings(klass, class_loader); 2373 } else { 2374 DCHECK(!is_boot_image) << "Boot image must have equal dex files"; 2375 } 2376 } 2377 } 2378 } 2379 // If the class still isn't initialized, at least try some checks that initialization 2380 // would do so they can be skipped at runtime. 2381 if (!klass->IsInitialized() && 2382 manager_->GetClassLinker()->ValidateSuperClassDescriptors(klass)) { 2383 old_status = mirror::Class::kStatusSuperclassValidated; 2384 } else { 2385 soa.Self()->ClearException(); 2386 } 2387 soa.Self()->AssertNoPendingException(); 2388 } 2389 } 2390 // Record the final class status if necessary. 2391 ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); 2392 // Back up the status before doing initialization for static encoded fields, 2393 // because the static encoded branch wants to keep the status to uninitialized. 2394 manager_->GetCompiler()->RecordClassStatus(ref, old_status); 2395 } 2396 2397 private: 2398 void InternStrings(Handle<mirror::Class> klass, Handle<mirror::ClassLoader> class_loader) 2399 REQUIRES_SHARED(Locks::mutator_lock_) { 2400 DCHECK(manager_->GetCompiler()->GetCompilerOptions().IsBootImage()); 2401 DCHECK(klass->IsVerified()); 2402 DCHECK(!klass->IsInitialized()); 2403 2404 StackHandleScope<1> hs(Thread::Current()); 2405 Handle<mirror::DexCache> h_dex_cache = hs.NewHandle(klass->GetDexCache()); 2406 const DexFile* dex_file = manager_->GetDexFile(); 2407 const DexFile::ClassDef* class_def = klass->GetClassDef(); 2408 ClassLinker* class_linker = manager_->GetClassLinker(); 2409 2410 // Check encoded final field values for strings and intern. 2411 annotations::RuntimeEncodedStaticFieldValueIterator value_it(*dex_file, 2412 &h_dex_cache, 2413 &class_loader, 2414 manager_->GetClassLinker(), 2415 *class_def); 2416 for ( ; value_it.HasNext(); value_it.Next()) { 2417 if (value_it.GetValueType() == annotations::RuntimeEncodedStaticFieldValueIterator::kString) { 2418 // Resolve the string. This will intern the string. 2419 art::ObjPtr<mirror::String> resolved = class_linker->ResolveString( 2420 *dex_file, dex::StringIndex(value_it.GetJavaValue().i), h_dex_cache); 2421 CHECK(resolved != nullptr); 2422 } 2423 } 2424 2425 // Intern strings seen in <clinit>. 2426 ArtMethod* clinit = klass->FindClassInitializer(class_linker->GetImagePointerSize()); 2427 if (clinit != nullptr) { 2428 const DexFile::CodeItem* code_item = clinit->GetCodeItem(); 2429 DCHECK(code_item != nullptr); 2430 const Instruction* inst = Instruction::At(code_item->insns_); 2431 2432 const uint32_t insns_size = code_item->insns_size_in_code_units_; 2433 for (uint32_t dex_pc = 0; dex_pc < insns_size;) { 2434 if (inst->Opcode() == Instruction::CONST_STRING) { 2435 ObjPtr<mirror::String> s = class_linker->ResolveString( 2436 *dex_file, dex::StringIndex(inst->VRegB_21c()), h_dex_cache); 2437 CHECK(s != nullptr); 2438 } else if (inst->Opcode() == Instruction::CONST_STRING_JUMBO) { 2439 ObjPtr<mirror::String> s = class_linker->ResolveString( 2440 *dex_file, dex::StringIndex(inst->VRegB_31c()), h_dex_cache); 2441 CHECK(s != nullptr); 2442 } 2443 dex_pc += inst->SizeInCodeUnits(); 2444 inst = inst->Next(); 2445 } 2446 } 2447 } 2448 2449 bool ResolveTypesOfMethods(Thread* self, ArtMethod* m) 2450 REQUIRES_SHARED(Locks::mutator_lock_) { 2451 auto rtn_type = m->GetReturnType(true); // return value is discarded because resolve will be done internally. 2452 if (rtn_type == nullptr) { 2453 self->ClearException(); 2454 return false; 2455 } 2456 const DexFile::TypeList* types = m->GetParameterTypeList(); 2457 if (types != nullptr) { 2458 for (uint32_t i = 0; i < types->Size(); ++i) { 2459 dex::TypeIndex param_type_idx = types->GetTypeItem(i).type_idx_; 2460 auto param_type = m->GetClassFromTypeIndex(param_type_idx, true); 2461 if (param_type == nullptr) { 2462 self->ClearException(); 2463 return false; 2464 } 2465 } 2466 } 2467 return true; 2468 } 2469 2470 // Pre resolve types mentioned in all method signatures before start a transaction 2471 // since ResolveType doesn't work in transaction mode. 2472 bool PreResolveTypes(Thread* self, const Handle<mirror::Class>& klass) 2473 REQUIRES_SHARED(Locks::mutator_lock_) { 2474 PointerSize pointer_size = manager_->GetClassLinker()->GetImagePointerSize(); 2475 for (ArtMethod& m : klass->GetMethods(pointer_size)) { 2476 if (!ResolveTypesOfMethods(self, &m)) { 2477 return false; 2478 } 2479 } 2480 if (klass->IsInterface()) { 2481 return true; 2482 } else if (klass->HasSuperClass()) { 2483 StackHandleScope<1> hs(self); 2484 MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(klass->GetSuperClass())); 2485 for (int i = super_klass->GetVTableLength() - 1; i >= 0; --i) { 2486 ArtMethod* m = klass->GetVTableEntry(i, pointer_size); 2487 ArtMethod* super_m = super_klass->GetVTableEntry(i, pointer_size); 2488 if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) { 2489 return false; 2490 } 2491 } 2492 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { 2493 super_klass.Assign(klass->GetIfTable()->GetInterface(i)); 2494 if (klass->GetClassLoader() != super_klass->GetClassLoader()) { 2495 uint32_t num_methods = super_klass->NumVirtualMethods(); 2496 for (uint32_t j = 0; j < num_methods; ++j) { 2497 ArtMethod* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( 2498 j, pointer_size); 2499 ArtMethod* super_m = super_klass->GetVirtualMethod(j, pointer_size); 2500 if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) { 2501 return false; 2502 } 2503 } 2504 } 2505 } 2506 } 2507 return true; 2508 } 2509 2510 // Initialize the klass's dependencies recursively before initializing itself. 2511 // Checking for interfaces is also necessary since interfaces can contain 2512 // both default methods and static encoded fields. 2513 bool InitializeDependencies(const Handle<mirror::Class>& klass, 2514 Handle<mirror::ClassLoader> class_loader, 2515 Thread* self) 2516 REQUIRES_SHARED(Locks::mutator_lock_) { 2517 if (klass->HasSuperClass()) { 2518 ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); 2519 StackHandleScope<1> hs(self); 2520 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); 2521 if (!handle_scope_super->IsInitialized()) { 2522 this->TryInitializeClass(handle_scope_super, class_loader); 2523 if (!handle_scope_super->IsInitialized()) { 2524 return false; 2525 } 2526 } 2527 } 2528 2529 uint32_t num_if = klass->NumDirectInterfaces(); 2530 for (size_t i = 0; i < num_if; i++) { 2531 ObjPtr<mirror::Class> 2532 interface = mirror::Class::GetDirectInterface(self, klass.Get(), i); 2533 StackHandleScope<1> hs(self); 2534 Handle<mirror::Class> handle_interface(hs.NewHandle(interface)); 2535 2536 TryInitializeClass(handle_interface, class_loader); 2537 2538 if (!handle_interface->IsInitialized()) { 2539 return false; 2540 } 2541 } 2542 2543 return PreResolveTypes(self, klass); 2544 } 2545 2546 // In this phase the classes containing class initializers are ignored. Make sure no 2547 // clinit appears in kalss's super class chain and interfaces. 2548 bool NoClinitInDependency(const Handle<mirror::Class>& klass, 2549 Thread* self, 2550 Handle<mirror::ClassLoader>* class_loader) 2551 REQUIRES_SHARED(Locks::mutator_lock_) { 2552 ArtMethod* clinit = 2553 klass->FindClassInitializer(manager_->GetClassLinker()->GetImagePointerSize()); 2554 if (clinit != nullptr) { 2555 VLOG(compiler) << klass->PrettyClass() << ' ' << clinit->PrettyMethod(true); 2556 return false; 2557 } 2558 if (klass->HasSuperClass()) { 2559 ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); 2560 StackHandleScope<1> hs(self); 2561 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); 2562 if (!NoClinitInDependency(handle_scope_super, self, class_loader)) { 2563 return false; 2564 } 2565 } 2566 2567 uint32_t num_if = klass->NumDirectInterfaces(); 2568 for (size_t i = 0; i < num_if; i++) { 2569 ObjPtr<mirror::Class> 2570 interface = mirror::Class::GetDirectInterface(self, klass.Get(), i); 2571 StackHandleScope<1> hs(self); 2572 Handle<mirror::Class> handle_interface(hs.NewHandle(interface)); 2573 if (!NoClinitInDependency(handle_interface, self, class_loader)) { 2574 return false; 2575 } 2576 } 2577 2578 return true; 2579 } 2580 2581 const ParallelCompilationManager* const manager_; 2582}; 2583 2584void CompilerDriver::InitializeClasses(jobject jni_class_loader, 2585 const DexFile& dex_file, 2586 const std::vector<const DexFile*>& dex_files, 2587 TimingLogger* timings) { 2588 TimingLogger::ScopedTiming t("InitializeNoClinit", timings); 2589 2590 // Initialization allocates objects and needs to run single-threaded to be deterministic. 2591 bool force_determinism = GetCompilerOptions().IsForceDeterminism(); 2592 ThreadPool* init_thread_pool = force_determinism 2593 ? single_thread_pool_.get() 2594 : parallel_thread_pool_.get(); 2595 size_t init_thread_count = force_determinism ? 1U : parallel_thread_count_; 2596 2597 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 2598 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files, 2599 init_thread_pool); 2600 2601 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsAppImage()) { 2602 // Set the concurrency thread to 1 to support initialization for App Images since transaction 2603 // doesn't support multithreading now. 2604 // TODO: remove this when transactional mode supports multithreading. 2605 init_thread_count = 1U; 2606 } 2607 InitializeClassVisitor visitor(&context); 2608 context.ForAll(0, dex_file.NumClassDefs(), &visitor, init_thread_count); 2609} 2610 2611class InitializeArrayClassesAndCreateConflictTablesVisitor : public ClassVisitor { 2612 public: 2613 explicit InitializeArrayClassesAndCreateConflictTablesVisitor(VariableSizedHandleScope& hs) 2614 : hs_(hs) {} 2615 2616 virtual bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE 2617 REQUIRES_SHARED(Locks::mutator_lock_) { 2618 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { 2619 return true; 2620 } 2621 if (klass->IsArrayClass()) { 2622 StackHandleScope<1> hs(Thread::Current()); 2623 auto h_klass = hs.NewHandleWrapper(&klass); 2624 Runtime::Current()->GetClassLinker()->EnsureInitialized(hs.Self(), h_klass, true, true); 2625 } 2626 // Collect handles since there may be thread suspension in future EnsureInitialized. 2627 to_visit_.push_back(hs_.NewHandle(klass)); 2628 return true; 2629 } 2630 2631 void FillAllIMTAndConflictTables() REQUIRES_SHARED(Locks::mutator_lock_) { 2632 for (Handle<mirror::Class> c : to_visit_) { 2633 // Create the conflict tables. 2634 FillIMTAndConflictTables(c.Get()); 2635 } 2636 } 2637 2638 private: 2639 void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) 2640 REQUIRES_SHARED(Locks::mutator_lock_) { 2641 if (!klass->ShouldHaveImt()) { 2642 return; 2643 } 2644 if (visited_classes_.find(klass) != visited_classes_.end()) { 2645 return; 2646 } 2647 if (klass->HasSuperClass()) { 2648 FillIMTAndConflictTables(klass->GetSuperClass()); 2649 } 2650 if (!klass->IsTemp()) { 2651 Runtime::Current()->GetClassLinker()->FillIMTAndConflictTables(klass); 2652 } 2653 visited_classes_.insert(klass); 2654 } 2655 2656 VariableSizedHandleScope& hs_; 2657 std::vector<Handle<mirror::Class>> to_visit_; 2658 std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> visited_classes_; 2659}; 2660 2661void CompilerDriver::InitializeClasses(jobject class_loader, 2662 const std::vector<const DexFile*>& dex_files, 2663 TimingLogger* timings) { 2664 for (size_t i = 0; i != dex_files.size(); ++i) { 2665 const DexFile* dex_file = dex_files[i]; 2666 CHECK(dex_file != nullptr); 2667 InitializeClasses(class_loader, *dex_file, dex_files, timings); 2668 } 2669 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsAppImage()) { 2670 // Make sure that we call EnsureIntiailized on all the array classes to call 2671 // SetVerificationAttempted so that the access flags are set. If we do not do this they get 2672 // changed at runtime resulting in more dirty image pages. 2673 // Also create conflict tables. 2674 // Only useful if we are compiling an image (image_classes_ is not null). 2675 ScopedObjectAccess soa(Thread::Current()); 2676 VariableSizedHandleScope hs(soa.Self()); 2677 InitializeArrayClassesAndCreateConflictTablesVisitor visitor(hs); 2678 Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&visitor); 2679 visitor.FillAllIMTAndConflictTables(); 2680 } 2681 if (GetCompilerOptions().IsBootImage()) { 2682 // Prune garbage objects created during aborted transactions. 2683 Runtime::Current()->GetHeap()->CollectGarbage(true); 2684 } 2685} 2686 2687void CompilerDriver::Compile(jobject class_loader, 2688 const std::vector<const DexFile*>& dex_files, 2689 TimingLogger* timings) { 2690 if (kDebugProfileGuidedCompilation) { 2691 LOG(INFO) << "[ProfileGuidedCompilation] " << 2692 ((profile_compilation_info_ == nullptr) 2693 ? "null" 2694 : profile_compilation_info_->DumpInfo(&dex_files)); 2695 } 2696 2697 DCHECK(current_dex_to_dex_methods_ == nullptr); 2698 for (const DexFile* dex_file : dex_files) { 2699 CHECK(dex_file != nullptr); 2700 CompileDexFile(class_loader, 2701 *dex_file, 2702 dex_files, 2703 parallel_thread_pool_.get(), 2704 parallel_thread_count_, 2705 timings); 2706 const ArenaPool* const arena_pool = Runtime::Current()->GetArenaPool(); 2707 const size_t arena_alloc = arena_pool->GetBytesAllocated(); 2708 max_arena_alloc_ = std::max(arena_alloc, max_arena_alloc_); 2709 Runtime::Current()->ReclaimArenaPoolMemory(); 2710 } 2711 2712 ArrayRef<DexFileMethodSet> dex_to_dex_references; 2713 { 2714 // From this point on, we shall not modify dex_to_dex_references_, so 2715 // just grab a reference to it that we use without holding the mutex. 2716 MutexLock lock(Thread::Current(), dex_to_dex_references_lock_); 2717 dex_to_dex_references = ArrayRef<DexFileMethodSet>(dex_to_dex_references_); 2718 } 2719 for (const auto& method_set : dex_to_dex_references) { 2720 current_dex_to_dex_methods_ = &method_set.GetMethodIndexes(); 2721 CompileDexFile(class_loader, 2722 method_set.GetDexFile(), 2723 dex_files, 2724 parallel_thread_pool_.get(), 2725 parallel_thread_count_, 2726 timings); 2727 } 2728 current_dex_to_dex_methods_ = nullptr; 2729 2730 VLOG(compiler) << "Compile: " << GetMemoryUsageString(false); 2731} 2732 2733class CompileClassVisitor : public CompilationVisitor { 2734 public: 2735 explicit CompileClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {} 2736 2737 virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE { 2738 ATRACE_CALL(); 2739 const DexFile& dex_file = *manager_->GetDexFile(); 2740 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index); 2741 ClassLinker* class_linker = manager_->GetClassLinker(); 2742 jobject jclass_loader = manager_->GetClassLoader(); 2743 ClassReference ref(&dex_file, class_def_index); 2744 // Skip compiling classes with generic verifier failures since they will still fail at runtime 2745 if (manager_->GetCompiler()->verification_results_->IsClassRejected(ref)) { 2746 return; 2747 } 2748 // Use a scoped object access to perform to the quick SkipClass check. 2749 const char* descriptor = dex_file.GetClassDescriptor(class_def); 2750 ScopedObjectAccess soa(Thread::Current()); 2751 StackHandleScope<3> hs(soa.Self()); 2752 Handle<mirror::ClassLoader> class_loader( 2753 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); 2754 Handle<mirror::Class> klass( 2755 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader))); 2756 Handle<mirror::DexCache> dex_cache; 2757 if (klass == nullptr) { 2758 soa.Self()->AssertPendingException(); 2759 soa.Self()->ClearException(); 2760 dex_cache = hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file)); 2761 } else if (SkipClass(jclass_loader, dex_file, klass.Get())) { 2762 return; 2763 } else { 2764 dex_cache = hs.NewHandle(klass->GetDexCache()); 2765 } 2766 2767 const uint8_t* class_data = dex_file.GetClassData(class_def); 2768 if (class_data == nullptr) { 2769 // empty class, probably a marker interface 2770 return; 2771 } 2772 2773 // Go to native so that we don't block GC during compilation. 2774 ScopedThreadSuspension sts(soa.Self(), kNative); 2775 2776 CompilerDriver* const driver = manager_->GetCompiler(); 2777 2778 // Can we run DEX-to-DEX compiler on this class ? 2779 optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level = 2780 GetDexToDexCompilationLevel(soa.Self(), *driver, jclass_loader, dex_file, class_def); 2781 2782 ClassDataItemIterator it(dex_file, class_data); 2783 it.SkipAllFields(); 2784 2785 bool compilation_enabled = driver->IsClassToCompile( 2786 dex_file.StringByTypeIdx(class_def.class_idx_)); 2787 2788 // Compile direct methods 2789 int64_t previous_direct_method_idx = -1; 2790 while (it.HasNextDirectMethod()) { 2791 uint32_t method_idx = it.GetMemberIndex(); 2792 if (method_idx == previous_direct_method_idx) { 2793 // smali can create dex files with two encoded_methods sharing the same method_idx 2794 // http://code.google.com/p/smali/issues/detail?id=119 2795 it.Next(); 2796 continue; 2797 } 2798 previous_direct_method_idx = method_idx; 2799 CompileMethod(soa.Self(), 2800 driver, 2801 it.GetMethodCodeItem(), 2802 it.GetMethodAccessFlags(), 2803 it.GetMethodInvokeType(class_def), 2804 class_def_index, 2805 method_idx, 2806 class_loader, 2807 dex_file, 2808 dex_to_dex_compilation_level, 2809 compilation_enabled, 2810 dex_cache); 2811 it.Next(); 2812 } 2813 // Compile virtual methods 2814 int64_t previous_virtual_method_idx = -1; 2815 while (it.HasNextVirtualMethod()) { 2816 uint32_t method_idx = it.GetMemberIndex(); 2817 if (method_idx == previous_virtual_method_idx) { 2818 // smali can create dex files with two encoded_methods sharing the same method_idx 2819 // http://code.google.com/p/smali/issues/detail?id=119 2820 it.Next(); 2821 continue; 2822 } 2823 previous_virtual_method_idx = method_idx; 2824 CompileMethod(soa.Self(), 2825 driver, it.GetMethodCodeItem(), 2826 it.GetMethodAccessFlags(), 2827 it.GetMethodInvokeType(class_def), 2828 class_def_index, 2829 method_idx, 2830 class_loader, 2831 dex_file, 2832 dex_to_dex_compilation_level, 2833 compilation_enabled, 2834 dex_cache); 2835 it.Next(); 2836 } 2837 DCHECK(!it.HasNext()); 2838 } 2839 2840 private: 2841 const ParallelCompilationManager* const manager_; 2842}; 2843 2844void CompilerDriver::CompileDexFile(jobject class_loader, 2845 const DexFile& dex_file, 2846 const std::vector<const DexFile*>& dex_files, 2847 ThreadPool* thread_pool, 2848 size_t thread_count, 2849 TimingLogger* timings) { 2850 TimingLogger::ScopedTiming t("Compile Dex File", timings); 2851 ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this, 2852 &dex_file, dex_files, thread_pool); 2853 CompileClassVisitor visitor(&context); 2854 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count); 2855} 2856 2857void CompilerDriver::AddCompiledMethod(const MethodReference& method_ref, 2858 CompiledMethod* const compiled_method, 2859 size_t non_relative_linker_patch_count) { 2860 DCHECK(GetCompiledMethod(method_ref) == nullptr) 2861 << method_ref.dex_file->PrettyMethod(method_ref.dex_method_index); 2862 MethodTable::InsertResult result = compiled_methods_.Insert( 2863 DexFileReference(method_ref.dex_file, method_ref.dex_method_index), 2864 /*expected*/ nullptr, 2865 compiled_method); 2866 CHECK(result == MethodTable::kInsertResultSuccess); 2867 non_relative_linker_patch_count_.FetchAndAddRelaxed(non_relative_linker_patch_count); 2868 DCHECK(GetCompiledMethod(method_ref) != nullptr) 2869 << method_ref.dex_file->PrettyMethod(method_ref.dex_method_index); 2870} 2871 2872bool CompilerDriver::GetCompiledClass(ClassReference ref, mirror::Class::Status* status) const { 2873 DCHECK(status != nullptr); 2874 // The table doesn't know if something wasn't inserted. For this case it will return 2875 // kStatusNotReady. To handle this, just assume anything we didn't try to verify is not compiled. 2876 if (!compiled_classes_.Get(DexFileReference(ref.first, ref.second), status) || 2877 *status < mirror::Class::kStatusRetryVerificationAtRuntime) { 2878 return false; 2879 } 2880 return true; 2881} 2882 2883void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) { 2884 switch (status) { 2885 case mirror::Class::kStatusErrorResolved: 2886 case mirror::Class::kStatusErrorUnresolved: 2887 case mirror::Class::kStatusNotReady: 2888 case mirror::Class::kStatusResolved: 2889 case mirror::Class::kStatusRetryVerificationAtRuntime: 2890 case mirror::Class::kStatusVerified: 2891 case mirror::Class::kStatusSuperclassValidated: 2892 case mirror::Class::kStatusInitialized: 2893 break; // Expected states. 2894 default: 2895 LOG(FATAL) << "Unexpected class status for class " 2896 << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second))) 2897 << " of " << status; 2898 } 2899 2900 ClassStateTable::InsertResult result; 2901 do { 2902 DexFileReference dex_ref(ref.first, ref.second); 2903 mirror::Class::Status existing = mirror::Class::kStatusNotReady; 2904 if (!compiled_classes_.Get(dex_ref, &existing)) { 2905 // Probably a uses library class, bail. 2906 if (kIsDebugBuild) { 2907 // Check to make sure it's not a dex file for an oat file we are compiling since these 2908 // should always succeed. These do not include classes in for used libraries. 2909 for (const DexFile* dex_file : *dex_files_for_oat_file_) { 2910 CHECK_NE(dex_ref.dex_file, dex_file) << dex_ref.dex_file->GetLocation(); 2911 } 2912 } 2913 return; 2914 } 2915 if (existing >= status) { 2916 // Existing status is already better than we expect, break. 2917 break; 2918 } 2919 // Update the status if we now have a greater one. This happens with vdex, 2920 // which records a class is verified, but does not resolve it. 2921 result = compiled_classes_.Insert(dex_ref, existing, status); 2922 CHECK(result != ClassStateTable::kInsertResultInvalidDexFile); 2923 } while (result != ClassStateTable::kInsertResultSuccess); 2924} 2925 2926CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const { 2927 CompiledMethod* compiled_method = nullptr; 2928 compiled_methods_.Get(DexFileReference(ref.dex_file, ref.dex_method_index), &compiled_method); 2929 return compiled_method; 2930} 2931 2932bool CompilerDriver::IsMethodVerifiedWithoutFailures(uint32_t method_idx, 2933 uint16_t class_def_idx, 2934 const DexFile& dex_file) const { 2935 const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx); 2936 if (verified_method != nullptr) { 2937 return !verified_method->HasVerificationFailures(); 2938 } 2939 2940 // If we can't find verification metadata, check if this is a system class (we trust that system 2941 // classes have their methods verified). If it's not, be conservative and assume the method 2942 // has not been verified successfully. 2943 2944 // TODO: When compiling the boot image it should be safe to assume that everything is verified, 2945 // even if methods are not found in the verification cache. 2946 const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx)); 2947 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 2948 Thread* self = Thread::Current(); 2949 ScopedObjectAccess soa(self); 2950 bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr; 2951 if (!is_system_class) { 2952 self->ClearException(); 2953 } 2954 return is_system_class; 2955} 2956 2957size_t CompilerDriver::GetNonRelativeLinkerPatchCount() const { 2958 return non_relative_linker_patch_count_.LoadRelaxed(); 2959} 2960 2961void CompilerDriver::SetRequiresConstructorBarrier(Thread* self, 2962 const DexFile* dex_file, 2963 uint16_t class_def_index, 2964 bool requires) { 2965 WriterMutexLock mu(self, requires_constructor_barrier_lock_); 2966 requires_constructor_barrier_.emplace(ClassReference(dex_file, class_def_index), requires); 2967} 2968 2969bool CompilerDriver::RequiresConstructorBarrier(Thread* self, 2970 const DexFile* dex_file, 2971 uint16_t class_def_index) { 2972 ClassReference class_ref(dex_file, class_def_index); 2973 { 2974 ReaderMutexLock mu(self, requires_constructor_barrier_lock_); 2975 auto it = requires_constructor_barrier_.find(class_ref); 2976 if (it != requires_constructor_barrier_.end()) { 2977 return it->second; 2978 } 2979 } 2980 WriterMutexLock mu(self, requires_constructor_barrier_lock_); 2981 const bool requires = RequiresConstructorBarrier(*dex_file, class_def_index); 2982 requires_constructor_barrier_.emplace(class_ref, requires); 2983 return requires; 2984} 2985 2986std::string CompilerDriver::GetMemoryUsageString(bool extended) const { 2987 std::ostringstream oss; 2988 const gc::Heap* const heap = Runtime::Current()->GetHeap(); 2989 const size_t java_alloc = heap->GetBytesAllocated(); 2990 oss << "arena alloc=" << PrettySize(max_arena_alloc_) << " (" << max_arena_alloc_ << "B)"; 2991 oss << " java alloc=" << PrettySize(java_alloc) << " (" << java_alloc << "B)"; 2992#if defined(__BIONIC__) || defined(__GLIBC__) 2993 const struct mallinfo info = mallinfo(); 2994 const size_t allocated_space = static_cast<size_t>(info.uordblks); 2995 const size_t free_space = static_cast<size_t>(info.fordblks); 2996 oss << " native alloc=" << PrettySize(allocated_space) << " (" << allocated_space << "B)" 2997 << " free=" << PrettySize(free_space) << " (" << free_space << "B)"; 2998#endif 2999 compiled_method_storage_.DumpMemoryUsage(oss, extended); 3000 return oss.str(); 3001} 3002 3003bool CompilerDriver::MayInlineInternal(const DexFile* inlined_from, 3004 const DexFile* inlined_into) const { 3005 // We're not allowed to inline across dex files if we're the no-inline-from dex file. 3006 if (inlined_from != inlined_into && 3007 compiler_options_->GetNoInlineFromDexFile() != nullptr && 3008 ContainsElement(*compiler_options_->GetNoInlineFromDexFile(), inlined_from)) { 3009 return false; 3010 } 3011 3012 return true; 3013} 3014 3015void CompilerDriver::InitializeThreadPools() { 3016 size_t parallel_count = parallel_thread_count_ > 0 ? parallel_thread_count_ - 1 : 0; 3017 parallel_thread_pool_.reset( 3018 new ThreadPool("Compiler driver thread pool", parallel_count)); 3019 single_thread_pool_.reset(new ThreadPool("Single-threaded Compiler driver thread pool", 0)); 3020} 3021 3022void CompilerDriver::FreeThreadPools() { 3023 parallel_thread_pool_.reset(); 3024 single_thread_pool_.reset(); 3025} 3026 3027} // namespace art 3028