// Print additional info during profile guided compilation. static constexpr bool kDebugProfileGuidedCompilation = false;
// Max encoded fields allowed for initializing app image. Hardcode the number for now // because 5000 should be large enough. static constexpr uint32_t kMaxEncodedFields = 5000;
void Dump() {
DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information"); // Note, the code below subtracts the stat value so that when added to the stat value we have // 100% of samples. TODO: clean this up.
DumpStat(type_based_devirtualization_,
resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
type_based_devirtualization_, "virtual/interface calls made direct based on type information");
const size_t total = std::accumulate(
class_status_count_,
class_status_count_ + static_cast<size_t>(ClassStatus::kLast) + 1, 0u); for (size_t i = 0; i <= static_cast<size_t>(ClassStatus::kLast); ++i) {
std::ostringstream oss;
oss << "classes with status " << static_cast<ClassStatus>(i);
DumpStat(class_status_count_[i], total - class_status_count_[i], oss.str().c_str());
}
for (size_t i = 0; i <= kMaxInvokeType; i++) {
std::ostringstream oss;
oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str()); if (virtual_made_direct_[i] > 0) {
std::ostringstream oss2;
oss2 << static_cast<InvokeType>(i) << " methods made direct";
DumpStat(virtual_made_direct_[i],
resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
oss2.str().c_str());
} if (direct_calls_to_boot_[i] > 0) {
std::ostringstream oss2;
oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
DumpStat(direct_calls_to_boot_[i],
resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
oss2.str().c_str());
} if (direct_methods_to_boot_[i] > 0) {
std::ostringstream oss2;
oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
DumpStat(direct_methods_to_boot_[i],
resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
oss2.str().c_str());
}
}
}
// Indicate that type information from the verifier led to devirtualization. void PreciseTypeDevirtualization() REQUIRES(!stats_lock_) {
STATS_LOCK();
type_based_devirtualization_++;
}
// A check-cast could be eliminated due to verifier type analysis. void SafeCast() REQUIRES(!stats_lock_) {
STATS_LOCK();
safe_casts_++;
}
// A check-cast couldn't be eliminated due to verifier type analysis. void NotASafeCast() REQUIRES(!stats_lock_) {
STATS_LOCK();
not_safe_casts_++;
}
// Register a class status. void AddClassStatus(ClassStatus status) REQUIRES(!stats_lock_) {
STATS_LOCK();
++class_status_count_[static_cast<size_t>(status)];
}
private:
Mutex stats_lock_;
// Type based devirtualization for invoke interface and virtual.
size_t type_based_devirtualization_ = 0u;
std::unique_ptr<const std::vector<uint8_t>>
CompilerDriver::CreateJniDlsymLookupTrampoline() const { #ifdef ART_USE_SIMULATOR // In simulator mode the dlsymLookup trampoline is executed in native ISA, not quick code isa.
InstructionSet trampoline_isa = kRuntimeISA; // Don't assume any specialized ISA features. const InstructionSetFeatures* trampoline_isa_features = nullptr; #else
InstructionSet trampoline_isa = GetCompilerOptions().GetInstructionSet(); const InstructionSetFeatures* trampoline_isa_features =
GetCompilerOptions().GetInstructionSetFeatures(); #endif
CREATE_TRAMPOLINE_IMPL(trampoline_isa, trampoline_isa_features, JNI, kJniAbi, pDlsymLookup)
}
std::unique_ptr<const std::vector<uint8_t>>
CompilerDriver::CreateJniDlsymLookupCriticalTrampoline() const { // @CriticalNative calls do not have the `JNIEnv*` parameter, so this trampoline uses the // architecture-dependent access to `Thread*` using the managed code ABI, i.e. `kQuickAbi`.
CREATE_TRAMPOLINE(JNI, kQuickAbi, pDlsymLookupCritical)
}
std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateNterpTrampoline() const { // We use QuickToInterpreterBridge to not waste one word in the Thread object. // The Nterp trampoline gets replaced with the nterp entrypoint when loading // an image.
CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
} #undef CREATE_TRAMPOLINE
// Compile: // 1) Compile all classes and methods enabled for compilation. May fall back to dex-to-dex // compilation. if (GetCompilerOptions().IsAnyCompilationEnabled()) {
Compile(class_loader, dex_files, timings);
} if (GetCompilerOptions().GetDumpStats()) {
stats_->Dump();
}
}
// Does the runtime for the InstructionSet provide an implementation returned by // GetQuickGenericJniStub allowing down calls that aren't compiled using a JNI compiler? staticbool InstructionSetHasGenericJniStub(InstructionSet isa) { switch (isa) { case InstructionSet::kArm: case InstructionSet::kArm64: case InstructionSet::kThumb2: case InstructionSet::kX86: case InstructionSet::kX86_64: returntrue; default: returnfalse;
}
}
// Checks whether profile guided compilation is enabled and if the method should be compiled // according to the profile file. staticbool ShouldCompileBasedOnProfile(const CompilerOptions& compiler_options,
ProfileCompilationInfo::ProfileIndexType profile_index,
MethodReference method_ref) { if (profile_index == ProfileCompilationInfo::MaxProfileIndex()) { // No profile for this dex file. Check if we're actually compiling based on a profile. if (!CompilerFilter::DependsOnProfile(compiler_options.GetCompilerFilter())) { returntrue;
} // Profile-based compilation without profile for this dex file. Do not compile the method.
DCHECK(compiler_options.GetProfileCompilationInfo() == nullptr ||
compiler_options.GetProfileCompilationInfo()->FindDexFile(*method_ref.dex_file) ==
ProfileCompilationInfo::MaxProfileIndex()); returnfalse;
} else {
DCHECK(CompilerFilter::DependsOnProfile(compiler_options.GetCompilerFilter())); const ProfileCompilationInfo* profile_compilation_info =
compiler_options.GetProfileCompilationInfo();
DCHECK(profile_compilation_info != nullptr);
bool result = profile_compilation_info->IsHotMethod(profile_index, method_ref.index);
// On non-low RAM devices, compile startup methods to potentially speed up // startup. if (!result && !Runtime::Current()->GetHeap()->IsLowMemoryMode()) {
result = profile_compilation_info->IsStartupMethod(profile_index, method_ref.index);
}
if ((access_flags & kAccNative) != 0) { // Are we extracting only and have support for generic JNI down calls? const CompilerOptions& compiler_options = driver->GetCompilerOptions(); if (!compiler_options.IsJniCompilationEnabled() &&
InstructionSetHasGenericJniStub(compiler_options.GetInstructionSet())) { // Leaving this empty will trigger the generic JNI version
} else { // Query any JNI optimization annotations such as @FastNative or @CriticalNative.
access_flags |= annotations::GetNativeMethodAnnotationAccessFlags(
dex_file, dex_file.GetClassDef(class_def_idx), method_idx); constvoid* boot_jni_stub = nullptr; if (!Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()) { // Skip the compilation for native method if found an usable boot JNI stub.
ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
std::string_view shorty = dex_file.GetMethodShortyView(dex_file.GetMethodId(method_idx));
boot_jni_stub = class_linker->FindBootJniStub(access_flags, shorty);
} if (boot_jni_stub == nullptr) {
compiled_method =
driver->GetCompiler()->JniCompile(access_flags, method_idx, dex_file, dex_cache);
CHECK(compiled_method != nullptr);
}
}
} elseif ((access_flags & kAccAbstract) != 0) { // Abstract methods don't have code.
} elseif (annotations::MethodIsNeverCompile(dex_file,
dex_file.GetClassDef(class_def_idx),
method_idx)) { // Method is annotated with @NeverCompile and should not be compiled.
} else { const CompilerOptions& compiler_options = driver->GetCompilerOptions(); // Don't compile class initializers unless kEverything. bool compile = (compiler_options.GetCompilerFilter() == CompilerFilter::kEverything) ||
((access_flags & kAccConstructor) == 0) || ((access_flags & kAccStatic) == 0); // Check if we should compile based on the profile.
compile = compile && ShouldCompileBasedOnProfile(compiler_options, profile_index, method_ref);
if (compile) { // NOTE: if compiler declines to compile this method, it will return null.
compiled_method = driver->GetCompiler()->Compile(code_item,
access_flags,
class_def_idx,
method_idx,
class_loader,
dex_file,
dex_cache);
ProfileMethodsCheck check_type = compiler_options.CheckProfiledMethodsCompiled(); if (UNLIKELY(check_type != ProfileMethodsCheck::kNone)) {
DCHECK(ShouldCompileBasedOnProfile(compiler_options, profile_index, method_ref)); bool violation = (compiled_method == nullptr); if (violation) {
std::ostringstream oss;
oss << "Failed to compile "
<< method_ref.dex_file->PrettyMethod(method_ref.index)
<< "[" << method_ref.dex_file->GetLocation() << "]"
<< " as expected by profile"; switch (check_type) { case ProfileMethodsCheck::kNone: break; case ProfileMethodsCheck::kLog:
LOG(ERROR) << oss.str(); break; case ProfileMethodsCheck::kAbort:
LOG(FATAL_WITHOUT_ABORT) << oss.str();
_exit(1);
}
}
}
}
} return compiled_method;
};
CompileMethodHarness(self,
driver,
code_item,
access_flags,
class_def_idx,
method_idx,
class_loader,
dex_file,
dex_cache,
quick_fn);
}
void CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
TimingLogger* timings) {
TimingLogger::ScopedTiming t("Resolve Types", timings); // Resolution allocates classes and needs to run single-threaded to be deterministic. bool force_determinism = GetCompilerOptions().IsForceDeterminism();
ThreadPool* resolve_thread_pool = force_determinism
? single_thread_pool_.get()
: parallel_thread_pool_.get();
size_t resolve_thread_count = force_determinism ? 1U : parallel_thread_count_;
for (size_t i = 0; i != dex_files.size(); ++i) { const DexFile* dex_file = dex_files[i];
CHECK(dex_file != nullptr);
ResolveDexFile(class_loader,
*dex_file,
resolve_thread_pool,
resolve_thread_count,
timings);
}
}
ProfileCompilationInfo::ProfileIndexType profile_index =
ProfileCompilationInfo::MaxProfileIndex(); if (profile_compilation_info != nullptr) {
profile_index = profile_compilation_info->FindDexFile(*dex_file); if (profile_index == ProfileCompilationInfo::MaxProfileIndex()) { // We have a `ProfileCompilationInfo` but no data for this dex file. // The code below would not find any method to process. continue;
}
}
// TODO: Implement a profile-based filter for the boot image. See b/76145463. for (ClassAccessor accessor : dex_file->GetClasses()) { // Skip methods that failed to verify since they may contain invalid Dex code. if (GetClassStatus(ClassReference(dex_file, accessor.GetClassDefIndex())) <
ClassStatus::kRetryVerificationAtRuntime) { continue;
}
for (const ClassAccessor::Method& method : accessor.GetMethods()) { if (profile_compilation_info != nullptr) {
DCHECK_NE(profile_index, ProfileCompilationInfo::MaxProfileIndex()); // There can be at most one class initializer in a class, so we shall not // call `ProfileCompilationInfo::ContainsClass()` more than once per class.
constexpr uint32_t kMask = kAccConstructor | kAccStatic; constbool is_startup_clinit =
(method.GetAccessFlags() & kMask) == kMask &&
profile_compilation_info->ContainsClass(profile_index, accessor.GetClassIdx());
// Resolve const-strings in the code. Done to have deterministic allocation behavior. Right // now this is single-threaded for simplicity. // TODO: Collect the relevant string indices in parallel, then allocate them sequentially // in a stable order. for (const DexInstructionPcPair& inst : method.GetInstructions()) {
Instruction::Code opcode = inst->Opcode(); if (opcode != Instruction::CONST_STRING && opcode != Instruction::CONST_STRING_JUMBO) { continue;
}
dex::StringIndex string_index(
(opcode == Instruction::CONST_STRING) ? inst->VRegB_21c() : inst->VRegB_31c()); if (com::android::art::flags::weak_const_string()) {
string_indexes.SetBit(string_index.index_);
} else {
ObjPtr<mirror::String> string = class_linker->ResolveString(string_index, dex_cache);
CHECK(string != nullptr) << "Could not allocate a string";
}
++num_instructions;
}
}
}
}
VLOG(compiler) << "Resolved " << num_instructions << " const string instructions";
}
// Initialize type check bit strings for check-cast and instance-of in the code. Done to have // deterministic allocation behavior. Right now this is single-threaded for simplicity. // TODO: Collect the relevant type indices in parallel, then process them sequentially in a // stable order.
staticvoid InitializeTypeCheckBitstrings(CompilerDriver* driver,
ClassLinker* class_linker,
Handle<mirror::DexCache> dex_cache, const DexFile& dex_file, const ClassAccessor::Method& method)
REQUIRES_SHARED(Locks::mutator_lock_) { for (const DexInstructionPcPair& inst : method.GetInstructions()) { switch (inst->Opcode()) { case Instruction::CHECK_CAST: case Instruction::INSTANCE_OF: {
dex::TypeIndex type_index(
(inst->Opcode() == Instruction::CHECK_CAST) ? inst->VRegB_21c() : inst->VRegC_22c()); constchar* descriptor = dex_file.GetTypeDescriptor(type_index); // We currently do not use the bitstring type check for array or final (including // primitive) classes. We may reconsider this in future if it's deemed to be beneficial. // And we cannot use it for classes outside the boot image as we do not know the runtime // value of their bitstring when compiling (it may not even get assigned at runtime). if (descriptor[0] == 'L' &&
driver->GetCompilerOptions().IsImageClass(TypeReference(&dex_file, type_index), /*array_dim=*/ 0u)) {
ObjPtr<mirror::Class> klass =
class_linker->LookupResolvedType(type_index,
dex_cache.Get(), /* class_loader= */ nullptr);
CHECK(klass != nullptr) << descriptor << " should have been previously resolved."; // Now assign the bitstring if the class is not final. Keep this in sync with sharpening. if (!klass->IsFinal()) {
MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
}
} break;
}
class CreateConflictTablesVisitor : public ClassVisitor { public: explicit CreateConflictTablesVisitor(VariableSizedHandleScope& hs)
: hs_(hs) {}
booloperator()(ObjPtr<mirror::Class> klass) override
REQUIRES_SHARED(Locks::mutator_lock_) { if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { returntrue;
} // Collect handles since there may be thread suspension in future EnsureInitialized.
to_visit_.push_back(hs_.NewHandle(klass)); returntrue;
}
void FillAllIMTAndConflictTables() REQUIRES_SHARED(Locks::mutator_lock_) {
ScopedAssertNoThreadSuspension ants(__FUNCTION__); for (Handle<mirror::Class> c : to_visit_) { // Create the conflict tables.
FillIMTAndConflictTables(c.Get());
}
}
private: void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)
REQUIRES_SHARED(Locks::mutator_lock_) { if (!klass->ShouldHaveImt()) { return;
} if (visited_classes_.find(klass.Ptr()) != visited_classes_.end()) { return;
} if (klass->HasSuperClass()) {
FillIMTAndConflictTables(klass->GetSuperClass());
} if (!klass->IsTemp()) {
Runtime::Current()->GetClassLinker()->FillIMTAndConflictTables(klass);
}
visited_classes_.insert(klass.Ptr());
}
// Precompile: // 1) Load image classes. // 2) Resolve all classes. // 3) For deterministic boot image, resolve strings for const-string instructions. // 4) Attempt to verify all classes. // 5) Attempt to initialize image classes, and trivially initialized classes. // 6) Update the set of image classes. // 7) For deterministic boot image, initialize bitstrings for type checking.
if (compiler_options_->IsAnyCompilationEnabled()) { // Avoid adding the dex files in the case where we aren't going to add compiled methods. // This reduces RAM usage for this case. for (const DexFile* dex_file : dex_files) { // Can be already inserted. This happens for gtests. if (!compiled_methods_.HaveDexFile(dex_file)) {
compiled_methods_.AddDexFile(dex_file);
}
}
}
// Resolve eagerly for compilations always, and for verifications only if we are running with // multiple threads. constbool should_resolve_eagerly =
compiler_options_->IsAnyCompilationEnabled() ||
(!GetCompilerOptions().IsForceDeterminism() && parallel_thread_count_ > 1); if (should_resolve_eagerly) {
Resolve(class_loader, dex_files, timings);
VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false);
}
if (!com::android::art::flags::weak_const_string()) { if (GetCompilerOptions().IsForceDeterminism() &&
(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension())) { // Resolve strings from const-string. Do this now to have a deterministic image.
ResolveConstStrings(dex_files, /*only_startup_strings=*/ false, /*dex_file_string_references=*/ nullptr,
timings);
VLOG(compiler) << "Resolve const-strings: " << GetMemoryUsageString(false);
} elseif (GetCompilerOptions().ResolveStartupConstStrings()) {
ResolveConstStrings(dex_files, /*only_startup_strings=*/ true, /*dex_file_string_references=*/ nullptr,
timings);
}
}
if (had_hard_verifier_failure_ && GetCompilerOptions().AbortOnHardVerifierFailure()) { // Avoid dumping threads. Even if we shut down the thread pools, there will still be three // instances of this thread's stack.
LOG(FATAL_WITHOUT_ABORT) << "Had a hard failure verifying all classes, and was asked to abort "
<< "in such situations. Please check the log.";
_exit(1);
} elseif (number_of_soft_verifier_failures_ > 0 &&
GetCompilerOptions().AbortOnSoftVerifierFailure()) {
LOG(FATAL_WITHOUT_ABORT) << "Had " << number_of_soft_verifier_failures_ << " soft failure(s) "
<< "verifying all classes, and was asked to abort in such situations. "
<< "Please check the log.";
_exit(1);
}
if (GetCompilerOptions().IsAppImage() && had_hard_verifier_failure_) { // Prune erroneous classes and classes that depend on them.
UpdateImageClasses(timings, image_classes);
VLOG(compiler) << "verify/UpdateImageClasses: " << GetMemoryUsageString(false);
}
}
if (GetCompilerOptions().IsGeneratingImage()) { // We can only initialize classes when their verification bit is set. if (compiler_options_->AssumeClassesAreVerified() ||
compiler_options_->IsVerificationEnabled()) { if (kIsDebugBuild) {
EnsureVerifiedOrVerifyAtRuntime(class_loader, dex_files);
}
InitializeClasses(class_loader, dex_files, timings);
VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false);
}
{ // Create conflict tables, as the runtime expects boot image classes to // always have their conflict tables filled.
ScopedObjectAccess soa(Thread::Current());
VariableSizedHandleScope hs(soa.Self());
CreateConflictTablesVisitor visitor(hs);
Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&visitor);
visitor.FillAllIMTAndConflictTables();
}
if (kBitstringSubtypeCheckEnabled &&
GetCompilerOptions().IsForceDeterminism() && GetCompilerOptions().IsBootImage()) { // Initialize type check bit string used by check-cast and instanceof. // Do this now to have a deterministic image. // Note: This is done after UpdateImageClasses() at it relies on the image // classes to be final.
InitializeTypeCheckBitstrings(this, dex_files, timings);
}
}
}
class ResolveCatchBlockExceptionsClassVisitor : public ClassVisitor { public: explicit ResolveCatchBlockExceptionsClassVisitor(Thread* self)
: hs_(self),
dex_file_records_(),
unprocessed_classes_(),
exception_types_to_resolve_(),
boot_images_start_(Runtime::Current()->GetHeap()->GetBootImagesStartAddress()),
boot_images_size_(Runtime::Current()->GetHeap()->GetBootImagesSize()) {}
booloperator()(ObjPtr<mirror::Class> c) override REQUIRES_SHARED(Locks::mutator_lock_) { // Filter out classes from boot images we're compiling against. // These have been processed when we compiled those boot images. if (reinterpret_cast32<uint32_t>(c.Ptr()) - boot_images_start_ < boot_images_size_) {
DCHECK(Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(c)); returntrue;
} // Filter out classes without methods. // These include primitive types and array types which have no dex file. if (c->NumMethods() == 0u) { returntrue;
} auto it = dex_file_records_.find(&c->GetDexFile()); if (it != dex_file_records_.end()) {
DexFileRecord& record = it->second;
DCHECK_EQ(c->GetDexCache(), record.GetDexCache().Get());
DCHECK_EQ(c->GetClassLoader(), record.GetClassLoader().Get()); if (record.IsProcessedClass(c)) { returntrue;
}
}
unprocessed_classes_.push_back(c); returntrue;
}
void FindAndResolveExceptionTypes(Thread* self, ClassLinker* class_linker)
REQUIRES_SHARED(Locks::mutator_lock_) { // If we try to resolve any exception types, we need to repeat the process. // Even if we failed to resolve an exception type, we could have resolved its supertype // or some implemented interfaces as a side-effect (the exception type could implement // another unresolved interface) and we need to visit methods of such new resolved // classes as they shall be recorded as image classes. while (FindExceptionTypesToResolve(class_linker)) {
ResolveExceptionTypes(self, class_linker);
}
}
bool ResolveCatchBlockExceptionsClassVisitor::FindExceptionTypesToResolve(
ClassLinker* class_linker) { // Thread suspension is not allowed while the `ResolveCatchBlockExceptionsClassVisitor` // is using a `std::vector<ObjPtr<mirror::Class>>`.
ScopedAssertNoThreadSuspension ants(__FUNCTION__);
DCHECK(unprocessed_classes_.empty());
class_linker->VisitClasses(this); if (unprocessed_classes_.empty()) { returnfalse;
}
DCHECK(exception_types_to_resolve_.empty()); const PointerSize pointer_size = class_linker->GetImagePointerSize(); for (ObjPtr<mirror::Class> klass : unprocessed_classes_) { const DexFile* dex_file = &klass->GetDexFile();
DexFileRecord& record = dex_file_records_.GetOrCreate(
dex_file, // NO_THREAD_SAFETY_ANALYSIS: Called from unannotated `SafeMap<>::GetOrCreate()`.
[&]() NO_THREAD_SAFETY_ANALYSIS { return DexFileRecord(hs_.NewHandle(klass->GetDexCache()),
hs_.NewHandle(klass->GetClassLoader()));
});
DCHECK_EQ(klass->GetDexCache(), record.GetDexCache().Get());
DCHECK_EQ(klass->GetClassLoader(), record.GetClassLoader().Get());
DCHECK(!record.IsProcessedClass(klass));
record.MarkProcessedClass(klass); for (ArtMethod& method : klass->GetDeclaredMethods(pointer_size)) { if (method.GetCodeItem() == nullptr) { continue; // native or abstract method
}
CodeItemDataAccessor accessor(method.DexInstructionData()); if (accessor.TriesSize() == 0) { continue; // nothing to process
} const uint8_t* handlers_ptr = accessor.GetCatchHandlerData();
size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&handlers_ptr); for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
CatchHandlerIterator iterator(handlers_ptr); for (; iterator.HasNext(); iterator.Next()) {
dex::TypeIndex exception_type_idx = iterator.GetHandlerTypeIndex(); if (exception_type_idx.IsValid() &&
!record.IsProcessedExceptionType(exception_type_idx)) {
record.MarkProcessedExceptionType(exception_type_idx); // Add to set of types to resolve if not resolved yet.
ObjPtr<mirror::Class> type = class_linker->LookupResolvedType(
exception_type_idx, record.GetDexCache().Get(), record.GetClassLoader().Get()); if (type == nullptr) {
exception_types_to_resolve_.push_back(
{exception_type_idx, record.GetDexCache(), record.GetClassLoader()});
}
}
}
handlers_ptr = iterator.EndDataPointer();
}
}
}
unprocessed_classes_.clear(); return !exception_types_to_resolve_.empty();
}
staticinlinebool CanIncludeInCurrentImage(ObjPtr<mirror::Class> klass)
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK(klass != nullptr);
gc::Heap* heap = Runtime::Current()->GetHeap(); if (heap->GetBootImageSpaces().empty()) { returntrue; // We can include any class when compiling the primary boot image.
} if (heap->ObjectIsInBootImageSpace(klass)) { returnfalse; // Already included in the boot image we're compiling against.
} return AotClassLinker::CanReferenceInBootImageExtensionOrAppImage(klass, heap);
}
class RecordImageClassesVisitor : public ClassVisitor { public: explicit RecordImageClassesVisitor(ImageClassMap* image_classes)
: image_classes_(image_classes) {}
booloperator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { bool resolved = klass->IsResolved();
DCHECK(resolved || klass->IsErroneousUnresolved()); bool can_include_in_image = LIKELY(resolved) && CanIncludeInCurrentImage(klass); auto [component_type, array_dim] =
klass->GetInnermostComponentTypeAndArrayDim<kDefaultVerifyFlags, kWithoutReadBarrier>();
TypeReference type_ref(nullptr, dex::TypeIndex(DexFile::kDexNoIndex16)); if (component_type->IsPrimitive() || array_dim != 0u) {
DCHECK_EQ(can_include_in_image, resolved && CanIncludeInCurrentImage(component_type)); // Primitive classes are attributed to the first boot class path dex file. const DexFile* dex_file = component_type->IsPrimitive()
? Runtime::Current()->GetClassLinker()->GetBootClassPath().front()
: &component_type->GetDexFile();
std::string_view descriptor = component_type->IsPrimitive()
? component_type->GetPrimitiveDescriptorView()
: component_type->GetDescriptorView(); // Use descriptor-based lookup to try and avoid searching for the `TypeId` in the dex file. if (can_include_in_image == image_classes_->Contains(dex_file, descriptor, array_dim)) { returntrue;
} const dex::TypeId* type_id = dex_file->FindTypeId(descriptor);
CHECK(type_id != nullptr);
type_ref = TypeReference(dex_file, dex_file->GetIndexForTypeId(*type_id));
} else {
type_ref = TypeReference(&component_type->GetDexFile(), component_type->GetDexTypeIndex());
} if (can_include_in_image) {
image_classes_->Add(type_ref, array_dim); // Does nothing if already present.
} else { if (VLOG_IS_ON(compiler) && image_classes_->Contains(type_ref, /*array_dim=*/ 0u)) {
std::string temp;
LOG(INFO) << "Removing " << (resolved ? "unsuitable" : "unresolved")
<< " class from image classes: " << component_type->GetDescriptor(&temp);
}
image_classes_->Remove(type_ref); // Does nothing if not present.
} returntrue;
}
private:
ImageClassMap* const image_classes_;
};
// Verify that classes which contain intrinsics methods are in the list of image classes. staticvoid VerifyClassesContainingIntrinsicsAreImageClasses(ImageClassMap* image_classes) {
Thread* self = Thread::Current();
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
ScopedObjectAccess soa(self); #define CHECK_INTRINSIC_OWNER_CLASS(_, __, ___, ____, _____, ClassName, ______, _______) \
{ \
ObjPtr<mirror::Class> klass = \
class_linker->LookupClass(self, ClassName, /*class_loader=*/ nullptr); \
CHECK(klass != nullptr); \
TypeReference type_ref(&klass->GetDexFile(), klass->GetDexTypeIndex()); \
CHECK(image_classes->Contains(type_ref, /*array_dim=*/ 0u)); \
}
// We need to put classes required by app class loaders to the boot image, // otherwise we would not be able to store app class loaders in app images. staticvoid AddClassLoaderClasses(/* out */ ImageClassMap* image_classes) {
ScopedObjectAccess soa(Thread::Current()); // Well known classes have been loaded and shall be added to image classes // by the `RecordImageClassesVisitor`. However, there are fields with array // types which we need to add to the image classes explicitly.
std::pair<ArtField*, ObjPtr<mirror::Class>> class_loader_array_fields[] = {
{ WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders,
WellKnownClasses::java_lang_ClassLoader.Get() }, // BaseDexClassLoader.sharedLibraryLoadersAfter has the same array type as above.
{ WellKnownClasses::dalvik_system_DexPathList_dexElements,
WellKnownClasses::dalvik_system_DexPathList__Element.Get() },
}; for (auto [array_field, component_type] : class_loader_array_fields) {
DCHECK(array_field->GetTypeDescriptorView().starts_with('['));
DCHECK_EQ(array_field->GetTypeDescriptorView().substr(1u), component_type->GetDescriptorView());
TypeReference type_ref(&component_type->GetDexFile(), component_type->GetDexTypeIndex());
image_classes->Add(type_ref, /*array_dim=*/ 1u);
}
}
// Make a list of descriptors for classes to include in the image void CompilerDriver::LoadImageClasses(TimingLogger* timings,
jobject class_loader, /*inout*/ ImageClassMap* image_classes) {
CHECK(timings != nullptr); if (!GetCompilerOptions().IsGeneratingImage()) { return;
}
if (GetCompilerOptions().IsBootImage()) { // Image classes of intrinsics are loaded and shall be added // to image classes by the `RecordImageClassesVisitor`. // Add classes needed for storing class loaders in app images.
AddClassLoaderClasses(image_classes);
}
// Make a first pass to load all classes explicitly listed in the profile.
Thread* self = Thread::Current();
ScopedObjectAccess soa(self);
StackHandleScope<2u> hs(self);
Handle<mirror::ClassLoader> loader = hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader));
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
CHECK(image_classes != nullptr);
std::vector<TypeReference> to_remove; // NO_THREAD_SAFETY_ANALYSIS: `ImageClassMap::ForEach<>()` does not propagate locking info.
image_classes->ForEach([&](TypeReference type_ref, size_t array_dim) NO_THREAD_SAFETY_ANALYSIS {
uint32_t utf16_length; constchar* descriptor = type_ref.dex_file->GetStringDataAndUtf16Length(
type_ref.dex_file->GetTypeId(type_ref.TypeIndex()).descriptor_idx_, &utf16_length);
size_t descriptor_length = DexFile::Utf8Length(descriptor, utf16_length);
ObjPtr<mirror::Class> klass =
class_linker->FindClass(self, descriptor, descriptor_length, loader); if (klass == nullptr) {
VLOG(compiler) << "Failed to find class " << descriptor;
self->ClearException();
to_remove.push_back(type_ref); // May cause some descriptors to be revisited.
} elseif (array_dim != 0u) {
std::string array_descriptor(array_dim, '[');
array_descriptor.append(descriptor, descriptor_length);
ObjPtr<mirror::Class> array_class = class_linker->FindClass(
self, array_descriptor.c_str(), array_descriptor.length(), loader); if (UNLIKELY(array_class == nullptr)) { // Element class has been resolved but we run out of memory for the array class. // If we later find the memory to allocate the array class, it can still make it // to the image. // TODO: Should we abort `dex2oat` for OOME?
LOG(ERROR) << "Out of memory while allocating array class " << array_descriptor;
self->ClearException();
}
}
}); for (TypeReference type_ref : to_remove) {
image_classes->Remove(type_ref);
}
// Resolve exception classes referenced by the loaded classes. The catch logic assumes // exceptions are resolved by the verifier when there is a catch block in an interested method. // Do this here so that exception classes appear to have been specified image classes.
ResolveCatchBlockExceptionsClassVisitor resolve_exception_classes_visitor(self);
resolve_exception_classes_visitor.FindAndResolveExceptionTypes(self, class_linker);
// We walk the roots looking for classes so that we'll pick up the // above classes plus any classes they depend on such super // classes, interfaces, and the required ClassLinker roots.
RecordImageClassesVisitor visitor(image_classes);
class_linker->VisitClasses(&visitor);
if (kIsDebugBuild && GetCompilerOptions().IsBootImage()) {
VerifyClassesContainingIntrinsicsAreImageClasses(image_classes);
VerifyClassLoaderClassesAreImageClasses(image_classes);
}
if (GetCompilerOptions().IsBootImage()) {
CHECK(!image_classes->empty());
}
}
staticvoid MaybeAddToImageClasses(Thread* self,
ObjPtr<mirror::Class> klass,
ImageClassMap* image_classes)
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK_EQ(self, Thread::Current());
DCHECK(klass->IsResolved());
Runtime* runtime = Runtime::Current();
gc::Heap* heap = runtime->GetHeap(); if (heap->ObjectIsInBootImageSpace(klass)) { // We're compiling a boot image extension and the class is already // in the boot image we're compiling against. return;
}
size_t array_dim;
std::tie(klass, array_dim) = klass->GetInnermostComponentTypeAndArrayDim(); if (klass->IsPrimitive()) { // Primitive classes are attributed to the first boot class path dex file. const DexFile* dex_file = Runtime::Current()->GetClassLinker()->GetBootClassPath().front(); const dex::TypeId* type_id = dex_file->FindTypeId(klass->GetPrimitiveDescriptorView());
CHECK(type_id != nullptr);
TypeReference type_ref(dex_file, dex_file->GetIndexForTypeId(*type_id));
image_classes->Add(type_ref, array_dim); // Does nothing if already present. // No superclasses, interfaces or copied methods' classes to add. // Lower dimensions are implicitly added above. return;
} const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); while (!klass->IsObjectClass()) {
TypeReference type_ref(&klass->GetDexFile(), klass->GetDexTypeIndex()); if (image_classes->Contains(type_ref, array_dim)) { break; // Previously inserted.
} bool contains_component_type =
(array_dim != 0u) && image_classes->Contains(type_ref, /*array_dim=*/ 0u);
image_classes->Add(type_ref, array_dim); if (contains_component_type) { break;
}
VLOG(compiler) << "Added " << type_ref.dex_file->GetTypeDescriptorView(type_ref.TypeIndex())
<< " to image classes"; for (size_t i = 0, num_interfaces = klass->NumDirectInterfaces(); i != num_interfaces; ++i) {
ObjPtr<mirror::Class> interface = klass->GetDirectInterface(i);
DCHECK(interface != nullptr);
MaybeAddToImageClasses(self, interface, image_classes);
} for (auto& m : klass->GetCopiedMethods(pointer_size)) {
MaybeAddToImageClasses(self, m.GetDeclaringClass(), image_classes);
}
klass = klass->GetSuperClass();
array_dim = 0u;
} if (klass->IsObjectClass() && array_dim > 0) {
TypeReference type_ref(&klass->GetDexFile(), klass->GetDexTypeIndex());
image_classes->Add(type_ref, array_dim);
}
}
// Keeps all the data for the update together. Also doubles as the reference visitor. // Note: we can use object pointers because we suspend all threads. class ClinitImageUpdate { public:
ClinitImageUpdate(ImageClassMap* image_classes,
Thread* self) REQUIRES_SHARED(Locks::mutator_lock_)
: hs_(self),
image_classes_(image_classes),
self_(self) {
CHECK(image_classes != nullptr);
// Make sure nobody interferes with us.
old_cause_ = self->StartAssertNoThreadSuspension("Boot image closure");
}
~ClinitImageUpdate() { // Allow others to suspend again.
self_->EndAssertNoThreadSuspension(old_cause_);
}
void Walk() REQUIRES_SHARED(Locks::mutator_lock_) { // Find all the already-marked classes.
WriterMutexLock mu(self_, *Locks::heap_bitmap_lock_);
FindImageClassesVisitor visitor(this);
Runtime::Current()->GetClassLinker()->VisitClasses(&visitor);
// Use the initial classes as roots for a search. for (Handle<mirror::Class> klass_root : image_class_handles_) {
VisitClinitClassesObject(klass_root.Get());
}
ScopedAssertNoThreadSuspension ants(__FUNCTION__); for (Handle<mirror::Class> h_klass : to_insert_) {
MaybeAddToImageClasses(self_, h_klass.Get(), image_classes_);
}
}
private: class FindImageClassesVisitor : public ClassVisitor { public: explicit FindImageClassesVisitor(ClinitImageUpdate* data)
: data_(data) {}
booloperator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { bool resolved = klass->IsResolved();
DCHECK(resolved || klass->IsErroneousUnresolved()); bool can_include_in_image =
LIKELY(resolved) && LIKELY(!klass->IsErroneous()) && CanIncludeInCurrentImage(klass); auto [component_type, array_dim] =
klass->GetInnermostComponentTypeAndArrayDim<kDefaultVerifyFlags, kWithoutReadBarrier>();
DCHECK_EQ(
can_include_in_image,
resolved && !component_type->IsErroneous() && CanIncludeInCurrentImage(component_type));
TypeReference type_ref(nullptr, dex::TypeIndex(DexFile::kDexNoIndex16)); if (component_type->IsPrimitive()) { // Primitive classes are attributed to the first boot class path dex file. const DexFile* dex_file = Runtime::Current()->GetClassLinker()->GetBootClassPath().front();
std::string_view descriptor = component_type->GetPrimitiveDescriptorView(); // Use descriptor-based lookup to try and avoid searching for the `TypeId` in the dex file. if (can_include_in_image ==
data_->image_classes_->Contains(dex_file, descriptor, array_dim)) { returntrue;
} const dex::TypeId* type_id = dex_file->FindTypeId(descriptor);
CHECK(type_id != nullptr);
type_ref = TypeReference(dex_file, dex_file->GetIndexForTypeId(*type_id));
} else {
type_ref = TypeReference(&component_type->GetDexFile(), component_type->GetDexTypeIndex());
} if (data_->image_classes_->Contains(type_ref, array_dim)) { if (can_include_in_image) {
data_->image_class_handles_.push_back(data_->hs_.NewHandle(klass));
} else {
VLOG(compiler) << "Removing " << (resolved ? "unsuitable" : "unresolved")
<< " class from image classes: "
<< type_ref.dex_file->GetTypeDescriptorView(type_ref.TypeIndex());
data_->image_classes_->Remove(type_ref);
}
} elseif (can_include_in_image) { // Check whether the class is initialized and has a clinit or static fields. // Such classes must be kept too. if (klass->IsInitialized() && !klass->IsArrayClass()) {
PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); if (klass->FindClassInitializer(pointer_size) != nullptr || klass->HasStaticFields()) {
DCHECK(!Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass->GetDexCache()))
<< klass->PrettyDescriptor();
data_->image_class_handles_.push_back(data_->hs_.NewHandle(klass));
}
}
} returntrue;
}
if (object->IsClass()) { // Add to the TODO list since MaybeAddToImageClasses may cause thread suspension. Thread // suspensionb is not safe to do in VisitObjects or VisitReferences.
to_insert_.push_back(hs_.NewHandle(object->AsClass()));
} else { // Else visit the object's class.
VisitClinitClassesObject(object->GetClass());
}
// If it is not a DexCache, visit all references. if (!object->IsDexCache()) {
object->VisitReferences(*this, *this);
}
}
index_.store(begin, std::memory_order_relaxed); for (size_t i = 0; i < work_units; ++i) {
thread_pool_->AddTask(self, new ForAllClosureLambda<Fn>(this, end, fn));
}
thread_pool_->StartWorkers(self);
// Ensure we're suspended while we're blocked waiting for the other threads to finish (worker // thread destructor's called below perform join).
CHECK_NE(self->GetState(), ThreadState::kRunnable);
// Wait for all the worker threads to finish.
thread_pool_->Wait(self, true, false);
// And stop the workers accepting jobs.
thread_pool_->StopWorkers(self);
}
// A fast version of SkipClass above if the class pointer is available // that avoids the expensive FindInClassPath search. staticbool SkipClass(jobject class_loader, const DexFile& dex_file, ObjPtr<mirror::Class> klass)
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK(klass != nullptr); const DexFile& original_dex_file = klass->GetDexFile(); if (&dex_file != &original_dex_file) { if (class_loader == nullptr) {
LOG(WARNING) << "Skipping class " << klass->PrettyDescriptor() << " from "
<< dex_file.GetLocation() << " previously found in "
<< original_dex_file.GetLocation();
} returntrue;
} returnfalse;
}
staticvoid DCheckResolveException(mirror::Throwable* exception)
REQUIRES_SHARED(Locks::mutator_lock_) { if (!kIsDebugBuild) { return;
}
std::string temp; constchar* descriptor = exception->GetClass()->GetDescriptor(&temp); constchar* expected_exceptions[] = { "Ljava/lang/ClassFormatError;", "Ljava/lang/ClassCircularityError;", "Ljava/lang/IllegalAccessError;", "Ljava/lang/IncompatibleClassChangeError;", "Ljava/lang/InstantiationError;", "Ljava/lang/LinkageError;", "Ljava/lang/NoClassDefFoundError;", "Ljava/lang/VerifyError;",
}; bool found = false; for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) { if (strcmp(descriptor, expected_exceptions[i]) == 0) {
found = true;
}
} if (!found) {
LOG(FATAL) << "Unexpected exception " << exception->Dump();
}
}
template <bool kApp> class ResolveTypeVisitor : public CompilationVisitor { public: explicit ResolveTypeVisitor(const ParallelCompilationManager* manager) : manager_(manager) {
} void Visit(size_t index) override REQUIRES(!Locks::mutator_lock_) { const DexFile& dex_file = *manager_->GetDexFile(); // For boot images we resolve all referenced types, such as arrays, // whereas for applications just those with classdefs.
dex::TypeIndex type_idx = kApp ? dex_file.GetClassDef(index).class_idx_ : dex::TypeIndex(index);
ClassLinker* class_linker = manager_->GetClassLinker();
ScopedObjectAccess soa(Thread::Current());
StackHandleScope<kApp ? 4u : 2u> hs(soa.Self());
Handle<mirror::ClassLoader> class_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader>(manager_->GetClassLoader()))); // TODO: Fix tests that require `RegisterDexFile()` and use `FindDexCache()` in all cases.
Handle<mirror::DexCache> dex_cache = hs.NewHandle(
kApp ? class_linker->FindDexCache(soa.Self(), dex_file)
: class_linker->RegisterDexFile(dex_file, class_loader.Get()));
DCHECK(dex_cache != nullptr);
// Resolve the class.
ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, dex_cache, class_loader); if (klass == nullptr) {
mirror::Throwable* exception = soa.Self()->GetException();
DCHECK(exception != nullptr);
VLOG(compiler) << "Exception during type resolution: " << exception->Dump(); if (exception->GetClass() == WellKnownClasses::java_lang_OutOfMemoryError.Get()) { // There's little point continuing compilation if the heap is exhausted. // Trying to do so would also introduce non-deterministic compilation results.
LOG(FATAL) << "Out of memory during type resolution for compilation";
}
DCheckResolveException(exception);
soa.Self()->ClearException();
} else { if (kApp && manager_->GetCompiler()->GetCompilerOptions().IsCheckLinkageConditions()) {
Handle<mirror::Class> hklass = hs.NewHandle(klass); bool is_fatal = manager_->GetCompiler()->GetCompilerOptions().IsCrashOnLinkageViolation();
Handle<mirror::ClassLoader> defining_class_loader = hs.NewHandle(hklass->GetClassLoader()); if (defining_class_loader.Get() != class_loader.Get()) { // Redefinition via different ClassLoaders. // This OptStat stuff is to enable logging from the APK scanner. if (is_fatal)
LOG(FATAL) << "OptStat#" << hklass->PrettyClassAndClassLoader() << ": 1"; else
LOG(ERROR)
<< "LINKAGE VIOLATION: "
<< hklass->PrettyClassAndClassLoader()
<< " was redefined";
} // Check that the current class is not a subclass of java.lang.ClassLoader. if (!hklass->IsInterface() &&
hklass->IsSubClass(GetClassRoot<mirror::ClassLoader>(class_linker))) { // Subclassing of java.lang.ClassLoader. // This OptStat stuff is to enable logging from the APK scanner. if (is_fatal) {
LOG(FATAL) << "OptStat#" << hklass->PrettyClassAndClassLoader() << ": 1";
} else {
LOG(ERROR)
<< "LINKAGE VIOLATION: "
<< hklass->PrettyClassAndClassLoader()
<< " is a subclass of java.lang.ClassLoader";
}
}
CHECK(hklass->IsResolved()) << hklass->PrettyClass();
}
}
}
// TODO: we could resolve strings here, although the string table is largely filled with class // and method names.
ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, thread_pool); // For boot images we resolve all referenced types, such as arrays, // whereas for applications just those with classdefs. if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
ResolveTypeVisitor</*kApp=*/ false> visitor(&context);
context.ForAll(0, dex_file.NumTypeIds(), &visitor, thread_count);
} else {
ResolveTypeVisitor</*kApp=*/ true> visitor(&context);
context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
}
}
void CompilerDriver::SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files,
TimingLogger* timings) { // This can be run in parallel. for (const DexFile* dex_file : dex_files) {
CHECK(dex_file != nullptr);
SetVerifiedDexFile(class_loader,
*dex_file,
parallel_thread_pool_.get(),
parallel_thread_count_,
timings);
}
}
staticvoid LoadAndUpdateStatus(const ClassAccessor& accessor,
ClassStatus status,
Handle<mirror::ClassLoader> class_loader,
Thread* self)
REQUIRES_SHARED(Locks::mutator_lock_) {
StackHandleScope<1> hs(self);
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Handle<mirror::Class> cls(hs.NewHandle<mirror::Class>(
class_linker->FindClass(self, accessor.GetDexFile(), accessor.GetClassIdx(), class_loader))); if (cls != nullptr) { // Check that the class is resolved with the current dex file. We might get // a boot image class, or a class in a different dex file for multidex, and // we should not update the status in that case. if (&cls->GetDexFile() == &accessor.GetDexFile()) {
VLOG(compiler) << "Updating class status of " << accessor.GetDescriptorView()
<< " to " << status;
ObjectLock<mirror::Class> lock(self, cls);
mirror::Class::SetStatus(cls, status, self);
}
} else {
DCHECK(self->IsExceptionPending());
self->ClearException();
}
}
bool CompilerDriver::FastVerify(jobject jclass_loader, const std::vector<const DexFile*>& dex_files,
TimingLogger* timings) {
CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks();
verifier::VerifierDeps* verifier_deps = callbacks->GetVerifierDeps(); // If there exist VerifierDeps that aren't the ones we just created to output, use them to verify. if (verifier_deps == nullptr || verifier_deps->OutputOnly()) { returnfalse;
}
TimingLogger::ScopedTiming t("Fast Verify", timings);
// We successfully validated the dependencies, now update class status // of verified classes. Note that the dependencies also record which classes // could not be fully verified; we could try again, but that would hurt verification // time. So instead we assume these classes still need to be verified at // runtime. for (const DexFile* dex_file : dex_files) { // Fetch the list of verified classes. const std::vector<bool>& verified_classes = verifier_deps->GetVerifiedClasses(*dex_file);
DCHECK_EQ(verified_classes.size(), dex_file->NumClassDefs()); for (ClassAccessor accessor : dex_file->GetClasses()) {
ClassStatus status = verified_classes[accessor.GetClassDefIndex()]
? ClassStatus::kVerifiedNeedsAccessChecks
: ClassStatus::kRetryVerificationAtRuntime; if (compiler_only_verifies) { // Just update the compiled_classes_ map. The compiler doesn't need to resolve // the type.
ClassReference ref(dex_file, accessor.GetClassDefIndex()); const ClassStatus existing = ClassStatus::kNotReady; // Note: when dex files are compiled inidividually, the class may have // been verified in a previous stage. This means this insertion can // fail, but that's OK.
compiled_classes_.Insert(ref, existing, status);
} else { if (is_generating_image &&
status == ClassStatus::kVerifiedNeedsAccessChecks &&
GetCompilerOptions().IsImageClass(TypeReference(dex_file, accessor.GetClassIdx()), /*array_dim=*/ 0u)) { // If the class will be in the image, we can rely on the ArtMethods // telling that they need access checks.
VLOG(compiler) << "Promoting "
<< accessor.GetDescriptorView()
<< " from needs access checks to verified given it is an image class";
status = ClassStatus::kVerified;
} // Update the class status, so later compilation stages know they don't need to verify // the class.
LoadAndUpdateStatus(accessor, status, class_loader, soa.Self());
}
// Vdex marks class as unverified for two reasons only: // 1. It has a hard failure, or // 2. One of its method needs lock counting. // // The optimizing compiler expects a method to not have a hard failure before // compiling it, so for simplicity just disable any compilation of methods // of these classes. if (status == ClassStatus::kRetryVerificationAtRuntime) {
ClassReference ref(dex_file, accessor.GetClassDefIndex());
callbacks->AddUncompilableClass(ref);
}
}
} returntrue;
}
// If there is no existing `verifier_deps` (because of non-existing vdex), or // the existing `verifier_deps` is not valid anymore, create a new one. The // verifier will need it to record the new dependencies. Then dex2oat can update // the vdex file with these new dependencies. // Dex2oat creates the verifier deps. // Create the main VerifierDeps, and set it to this thread.
verifier::VerifierDeps* main_verifier_deps =
Runtime::Current()->GetCompilerCallbacks()->GetVerifierDeps(); // Verifier deps can be null when unit testing. if (main_verifier_deps != nullptr) {
Thread::Current()->SetVerifierDeps(main_verifier_deps); // Create per-thread VerifierDeps to avoid contention on the main one. // We will merge them after verification. for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
worker->GetThread()->SetVerifierDeps( new verifier::VerifierDeps(GetCompilerOptions().GetDexFilesForOatFile()));
}
}
{
TimingLogger::ScopedTiming t("Verify Classes", timings); // Verification updates VerifierDeps and needs to run single-threaded to be deterministic. bool force_determinism = GetCompilerOptions().IsForceDeterminism();
ThreadPool* verify_thread_pool =
force_determinism ? single_thread_pool_.get() : parallel_thread_pool_.get();
size_t verify_thread_count = force_determinism ? 1U : parallel_thread_count_; for (const DexFile* dex_file : dex_files) {
CHECK(dex_file != nullptr);
VerifyDexFile(jclass_loader,
*dex_file,
verify_thread_pool,
verify_thread_count,
timings);
}
}
if (main_verifier_deps != nullptr) { // Merge all VerifierDeps into the main one. for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
std::unique_ptr<verifier::VerifierDeps> thread_deps(worker->GetThread()->GetVerifierDeps());
worker->GetThread()->SetVerifierDeps(nullptr); // We just took ownership.
main_verifier_deps->MergeWith(std::move(thread_deps),
GetCompilerOptions().GetDexFilesForOatFile());
}
Thread::Current()->SetVerifierDeps(nullptr);
}
}
class VerifyClassVisitor : public CompilationVisitor { public:
VerifyClassVisitor(const ParallelCompilationManager* manager, verifier::HardFailLogMode log_level)
: manager_(manager),
log_level_(log_level),
sdk_version_(Runtime::Current()->GetTargetSdkVersion()) {}
// Class has a meaningful status for the compiler now, record it.
ClassStatus status = klass->GetStatus(); if (status == ClassStatus::kInitialized) { // Initialized classes shall be visibly initialized when loaded from the image.
status = ClassStatus::kVisiblyInitialized;
}
manager_->GetCompiler()->RecordClassStatus(ref, status);
// It is *very* problematic if there are resolution errors in the boot classpath. // // It is also bad if classes fail verification. For example, we rely on things working // OK without verification when the decryption dialog is brought up. It is thus highly // recommended to compile the boot classpath with // --abort-on-hard-verifier-error --abort-on-soft-verifier-error // which is the default build system configuration. if (kIsDebugBuild) { if (manager_->GetCompiler()->GetCompilerOptions().IsBootImage() ||
manager_->GetCompiler()->GetCompilerOptions().IsBootImageExtension()) { if (!klass->IsResolved() || klass->IsErroneous()) {
LOG(FATAL) << "Boot classpath class " << klass->PrettyClass()
<< " failed to resolve/is erroneous: state= " << klass->GetStatus();
UNREACHABLE();
}
} if (klass->IsVerified()) {
DCHECK_EQ(failure_kind, verifier::FailureKind::kNoFailure);
} elseif (klass->IsVerifiedNeedsAccessChecks()) {
DCHECK_EQ(failure_kind, verifier::FailureKind::kAccessChecksFailure);
} elseif (klass->ShouldVerifyAtRuntime()) {
DCHECK_NE(failure_kind, verifier::FailureKind::kHardFailure); // This could either be due to: // - kTypeChecksFailure, or // - kSoftFailure, or // - the superclass or interfaces not being verified.
} else {
DCHECK_EQ(failure_kind, verifier::FailureKind::kHardFailure);
}
}
}
verifier::VerifierDeps::MaybeRecordVerificationStatus(soa.Self()->GetVerifierDeps(),
dex_file,
class_def,
failure_kind);
soa.Self()->AssertNoPendingException();
}
// Make initialized classes visibly initialized.
class_linker->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
}
class SetVerifiedClassVisitor : public CompilationVisitor { public: explicit SetVerifiedClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) override {
ScopedTrace trace(__FUNCTION__);
ScopedObjectAccess soa(Thread::Current()); const DexFile& dex_file = *manager_->GetDexFile(); const dex::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
ClassLinker* class_linker = manager_->GetClassLinker();
jobject jclass_loader = manager_->GetClassLoader();
StackHandleScope<3> hs(soa.Self());
Handle<mirror::ClassLoader> class_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Handle<mirror::Class> klass = hs.NewHandle(
class_linker->FindClass(soa.Self(), dex_file, class_def.class_idx_, class_loader)); // Class might have failed resolution. Then don't set it to verified. if (klass != nullptr) { // Only do this if the class is resolved. If even resolution fails, quickening will go very, // very wrong. if (klass->IsResolved() && !klass->IsErroneousResolved()) { if (klass->GetStatus() < ClassStatus::kVerified) {
ObjectLock<mirror::Class> lock(soa.Self(), klass); // Set class status to verified.
mirror::Class::SetStatus(klass, ClassStatus::kVerified, soa.Self()); // Mark methods as pre-verified. If we don't do this, the interpreter will run with // access checks.
InstructionSet instruction_set =
manager_->GetCompiler()->GetCompilerOptions().GetInstructionSet();
klass->SetSkipAccessChecksFlagOnAllMethods(GetInstructionSetPointerSize(instruction_set));
} // Record the final class status if necessary.
ClassReference ref(manager_->GetDexFile(), class_def_index);
manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
}
} else {
Thread* self = soa.Self();
DCHECK(self->IsExceptionPending());
self->ClearException();
}
}
if (klass != nullptr) { if (!SkipClass(manager_->GetClassLoader(), dex_file, klass.Get())) {
TryInitializeClass(soa.Self(), klass, class_loader);
}
manager_->GetCompiler()->stats_->AddClassStatus(klass->GetStatus());
} // Clear any class not found or verification exceptions.
soa.Self()->ClearException();
}
// For boot image extension, do not initialize classes defined // in dex files belonging to the boot image we're compiling against. if (is_boot_image_extension &&
runtime->GetHeap()->ObjectIsInBootImageSpace(klass->GetDexCache())) { // Also return early and don't store the class status in the recorded class status. return;
} // Do not initialize classes in boot space when compiling app (with or without image). if ((!is_boot_image && !is_boot_image_extension) && klass->IsBootStrapClassLoaded()) { // Also return early and don't store the class status in the recorded class status. return;
}
ClassStatus old_status = klass->GetStatus(); // Only try to initialize classes that were successfully verified. if (klass->IsVerified()) { // Attempt to initialize the class but bail if we either need to initialize the super-class // or static fields.
class_linker->EnsureInitialized(self, klass, false, false);
DCHECK(!self->IsExceptionPending());
old_status = klass->GetStatus(); if (!klass->IsInitialized()) { // We don't want non-trivial class initialization occurring on multiple threads due to // deadlock problems. For example, a parent class is initialized (holding its lock) that // refers to a sub-class in its static/class initializer causing it to try to acquire the // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock) // after first initializing its parents, whose locks are acquired. This leads to a // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock. // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather // than use a special Object for the purpose we use the Class of java.lang.Class.
Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
ObjectLock<mirror::Class> lock(self, h_klass); // Attempt to initialize allowing initialization of parent classes but still not static // fields. // Initialize dependencies first only for app or boot image extension, // to make TryInitializeClass() recursive. bool try_initialize_with_superclasses =
is_boot_image ? true : InitializeDependencies(klass, class_loader, self); if (try_initialize_with_superclasses) {
class_linker->EnsureInitialized(self, klass, false, true);
DCHECK(!self->IsExceptionPending());
} // Otherwise it's in app image or boot image extension but superclasses // cannot be initialized, no need to proceed.
old_status = klass->GetStatus();
bool have_profile = (compiler_options.GetProfileCompilationInfo() != nullptr) &&
!compiler_options.GetProfileCompilationInfo()->IsEmpty(); // If the class was not initialized, we can proceed to see if we can initialize static // fields. Limit the max number of encoded fields. if (!klass->IsInitialized() &&
(is_app_image || is_boot_image || is_boot_image_extension) &&
try_initialize_with_superclasses && !too_many_encoded_fields &&
compiler_options.IsImageClass(TypeReference(&dex_file, class_def->class_idx_), /*array_dim=*/ 0u) && // TODO(b/274077782): remove this test.
(have_profile || !is_boot_image_extension)) { bool can_init_static_fields = false; if (is_boot_image || is_boot_image_extension) { // We need to initialize static fields, we only do this for image classes that aren't // marked with the $NoPreloadHolder (which implies this should not be initialized // early).
can_init_static_fields = !std::string_view(descriptor).ends_with("$NoPreloadHolder;");
} else {
CHECK(is_app_image); // The boot image case doesn't need to recursively initialize the dependencies with // special logic since the class linker already does this. // Optimization will be disabled in debuggable build, because in debuggable mode we // want the <clinit> behavior to be observable for the debugger, so we don't do the // <clinit> at compile time.
can_init_static_fields =
ClassLinker::kAppImageMayContainStrings &&
!self->IsExceptionPending() &&
!compiler_options.GetDebuggable() &&
(compiler_options.InitializeAppImageClasses() ||
NoClinitInDependency(klass, self, &class_loader)); // TODO The checking for clinit can be removed since it's already // checked when init superclass. Currently keep it because it contains // processing of intern strings. Will be removed later when intern strings // and clinit are both initialized.
}
if (can_init_static_fields) {
VLOG(compiler) << "Initializing: " << descriptor; // TODO multithreading support. We should ensure the current compilation thread has // exclusive access to the runtime and the transaction. To achieve this, we could use // a ReaderWriterMutex but we're holding the mutator lock so we fail the check of mutex // validity in Thread::AssertThreadSuspensionIsAllowable.
// Resolve and initialize the exception type before enabling the transaction in case // the transaction aborts and cannot resolve the type. // TransactionAbortError is not initialized ant not in boot image, needed only by // compiler and will be pruned by ImageWriter.
Handle<mirror::Class> exception_class =
hs.NewHandle(class_linker->FindSystemClass(self, kTransactionAbortErrorDescriptor)); bool exception_initialized =
class_linker->EnsureInitialized(self, exception_class, true, true);
DCHECK(exception_initialized);
// Run the class initializer in transaction mode.
class_linker->EnterTransactionMode(is_app_image, klass.Get());
bool success = class_linker->EnsureInitialized(self, klass, true, true); // TODO we detach transaction from runtime to indicate we quit the transactional // mode which prevents the GC from visiting objects modified during the transaction. // Ensure GC is not run so don't access freed objects when aborting transaction.
if (success) {
class_linker->ExitTransactionMode();
DCHECK(!runtime->IsActiveTransaction());
if (is_boot_image || is_boot_image_extension) { // For boot image and boot image extension, we want to put the updated // status in the oat class. This is not the case for app image as we // want to keep the ability to load the oat file without the app image.
old_status = klass->GetStatus();
}
} else {
CHECK(self->IsExceptionPending());
mirror::Throwable* exception = self->GetException();
VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
<< exception->Dump();
std::ostream* file_log = manager_->GetCompiler()->
GetCompilerOptions().GetInitFailureOutput(); if (file_log != nullptr) {
*file_log << descriptor << "\n";
*file_log << exception->Dump() << "\n";
}
self->ClearException();
class_linker->RollbackAllTransactions();
CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
}
}
if (!success && (is_boot_image || is_boot_image_extension)) { // On failure, still intern strings of static fields and seen in <clinit>, as these // will be created in the zygote. This is separated from the transaction code just // above as we will allocate strings, so must be allowed to suspend. // We only need to intern strings for boot image and boot image extension // because classes that failed to be initialized will not appear in app image. if (&klass->GetDexFile() == manager_->GetDexFile()) {
InternStrings(klass, class_loader);
} else {
DCHECK(!is_boot_image) << "Boot image must have equal dex files";
}
}
}
} // Clear exception in case EnsureInitialized has caused one in the code above. // It's OK to clear the exception here since the compiler is supposed to be fault // tolerant and will silently not initialize classes that have exceptions.
self->ClearException();
// If the class still isn't initialized, at least try some checks that initialization // would do so they can be skipped at runtime. if (!klass->IsInitialized() && class_linker->ValidateSuperClassDescriptors(klass)) {
old_status = ClassStatus::kSuperclassValidated;
} else {
self->ClearException();
}
self->AssertNoPendingException();
}
} if (old_status == ClassStatus::kInitialized) { // Initialized classes shall be visibly initialized when loaded from the image.
old_status = ClassStatus::kVisiblyInitialized;
} // Record the final class status if necessary.
ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); // Back up the status before doing initialization for static encoded fields, // because the static encoded branch wants to keep the status to uninitialized.
manager_->GetCompiler()->RecordClassStatus(ref, old_status);
if (kIsDebugBuild) { // Make sure the class initialization did not leave any local references.
self->GetJniEnv()->AssertLocalsEmpty();
}
if (compiler_options.CompileArtTest()) { // For stress testing and unit-testing the clinit check in compiled code feature. if (kIsDebugBuild || std::string_view(descriptor).ends_with("$NoPreloadHolder;")) {
klass->SetInBootImageAndNotInPreloadedClasses();
}
}
}
// Check encoded final field values for strings and intern.
annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache,
class_loader,
manager_->GetClassLinker(),
*class_def); for ( ; value_it.HasNext(); value_it.Next()) { if (value_it.GetValueType() == annotations::RuntimeEncodedStaticFieldValueIterator::kString) {
dex::StringIndex string_index(value_it.GetJavaValue().i); if (com::android::art::flags::weak_const_string()) { // Strongly intern the string to ensure it stays alive and makes it to the image.
uint32_t utf16_length; constchar* utf8_data =
dex_cache->GetDexFile()->GetStringDataAndUtf16Length(string_index, &utf16_length);
ObjPtr<mirror::String> string =
class_linker->GetInternTable()->InternStrong(utf16_length, utf8_data);
CHECK(string != nullptr) << "Could not allocate a string";
} else { // Resolve the string. This will intern the string.
art::ObjPtr<mirror::String> resolved =
class_linker->ResolveString(string_index, dex_cache);
CHECK(resolved != nullptr) << "Could not allocate a string";
}
}
}
// Intern strings seen in <clinit>.
ArtMethod* clinit = klass->FindClassInitializer(class_linker->GetImagePointerSize()); if (clinit != nullptr) { for (const DexInstructionPcPair& inst : clinit->DexInstructions()) { if (inst->Opcode() == Instruction::CONST_STRING) {
ObjPtr<mirror::String> s = class_linker->ResolveString(
dex::StringIndex(inst->VRegB_21c()), dex_cache);
CHECK(s != nullptr);
} elseif (inst->Opcode() == Instruction::CONST_STRING_JUMBO) {
ObjPtr<mirror::String> s = class_linker->ResolveString(
dex::StringIndex(inst->VRegB_31c()), dex_cache);
CHECK(s != nullptr);
}
}
}
}
bool ResolveTypesOfMethods(Thread* self, ArtMethod* m)
REQUIRES_SHARED(Locks::mutator_lock_) { // Return value of ResolveReturnType() is discarded because resolve will be done internally.
ObjPtr<mirror::Class> rtn_type = m->ResolveReturnType(); if (rtn_type == nullptr) {
self->ClearException(); returnfalse;
} const dex::TypeList* types = m->GetParameterTypeList(); if (types != nullptr) { for (uint32_t i = 0; i < types->Size(); ++i) {
dex::TypeIndex param_type_idx = types->GetTypeItem(i).type_idx_;
ObjPtr<mirror::Class> param_type = m->ResolveClassFromTypeIndex(param_type_idx); if (param_type == nullptr) {
self->ClearException(); returnfalse;
}
}
} returntrue;
}
// Pre resolve types mentioned in all method signatures before start a transaction // since ResolveType doesn't work in transaction mode. bool PreResolveTypes(Thread* self, const Handle<mirror::Class>& klass)
REQUIRES_SHARED(Locks::mutator_lock_) {
PointerSize pointer_size = manager_->GetClassLinker()->GetImagePointerSize(); for (ArtMethod& m : klass->GetMethods(pointer_size)) { if (!ResolveTypesOfMethods(self, &m)) { returnfalse;
}
} if (klass->IsInterface()) { returntrue;
} elseif (klass->HasSuperClass()) {
StackHandleScope<1> hs(self);
MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(klass->GetSuperClass())); for (int i = super_klass->GetVTableLength() - 1; i >= 0; --i) {
ArtMethod* m = klass->GetVTableEntry(i, pointer_size);
ArtMethod* super_m = super_klass->GetVTableEntry(i, pointer_size); if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) { returnfalse;
}
} for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
super_klass.Assign(klass->GetIfTable()->GetInterface(i)); if (klass->GetClassLoader() != super_klass->GetClassLoader()) { for (ArtMethod& super_m : super_klass->GetMethods(pointer_size)) { if (!super_m.IsVirtual()) { continue;
}
ArtMethod* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
super_m.GetMethodIndex(), pointer_size); if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, &super_m)) { returnfalse;
}
}
}
}
} returntrue;
}
// Initialize the klass's dependencies recursively before initializing itself. // Checking for interfaces is also necessary since interfaces that contain // default methods must be initialized before the class. bool InitializeDependencies(const Handle<mirror::Class>& klass,
Handle<mirror::ClassLoader> class_loader,
Thread* self)
REQUIRES_SHARED(Locks::mutator_lock_) { if (klass->HasSuperClass()) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> super_class = hs.NewHandle(klass->GetSuperClass()); if (!super_class->IsInitialized()) { this->TryInitializeClass(self, super_class, class_loader); if (!super_class->IsInitialized()) { returnfalse;
}
}
}
if (!klass->IsInterface()) {
size_t num_interfaces = klass->GetIfTableCount(); for (size_t i = 0; i < num_interfaces; ++i) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> iface = hs.NewHandle(klass->GetIfTable()->GetInterface(i)); if (iface->HasDefaultMethods() && !iface->IsInitialized()) {
TryInitializeClass(self, iface, class_loader); if (!iface->IsInitialized()) { returnfalse;
}
}
}
}
return PreResolveTypes(self, klass);
}
// In this phase the classes containing class initializers are ignored. Make sure no // clinit appears in klass's super class chain and interfaces. bool NoClinitInDependency(const Handle<mirror::Class>& klass,
Thread* self,
Handle<mirror::ClassLoader>* class_loader)
REQUIRES_SHARED(Locks::mutator_lock_) {
ArtMethod* clinit =
klass->FindClassInitializer(manager_->GetClassLinker()->GetImagePointerSize()); if (clinit != nullptr) {
VLOG(compiler) << klass->PrettyClass() << ' ' << clinit->PrettyMethod(true); returnfalse;
} if (klass->HasSuperClass()) {
ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
StackHandleScope<1> hs(self);
Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); if (!NoClinitInDependency(handle_scope_super, self, class_loader)) { returnfalse;
}
}
uint32_t num_if = klass->NumDirectInterfaces(); for (size_t i = 0; i < num_if; i++) {
ObjPtr<mirror::Class> interface = klass->GetDirectInterface(i);
DCHECK(interface != nullptr);
StackHandleScope<1> hs(self);
Handle<mirror::Class> handle_interface(hs.NewHandle(interface)); if (!NoClinitInDependency(handle_interface, self, class_loader)) { returnfalse;
}
}
if (GetCompilerOptions().IsBootImage() ||
GetCompilerOptions().IsBootImageExtension() ||
GetCompilerOptions().IsAppImage()) { // Set the concurrency thread to 1 to support initialization for images since transaction // doesn't support multithreading now. // TODO: remove this when transactional mode supports multithreading.
init_thread_count = 1U;
}
InitializeClassVisitor visitor(&context);
context.ForAll(0, dex_file.NumClassDefs(), &visitor, init_thread_count);
// Make initialized classes visibly initialized.
class_linker->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
}
bool CompilerDriver::GetCompiledClass(const ClassReference& ref, ClassStatus* status) const {
DCHECK(status != nullptr); // The table doesn't know if something wasn't inserted. For this case it will return // ClassStatus::kNotReady. To handle this, just assume anything we didn't try to verify // is not compiled. if (!compiled_classes_.Get(ref, status) ||
*status < ClassStatus::kRetryVerificationAtRuntime) { returnfalse;
} returntrue;
}
ClassStatus CompilerDriver::GetClassStatus(const ClassReference& ref) const {
ClassStatus status = ClassStatus::kNotReady; if (!GetCompiledClass(ref, &status)) {
classpath_classes_.Get(ref, &status);
} return status;
}
void CompilerDriver::RecordClassStatus(const ClassReference& ref, ClassStatus status) { switch (status) { case ClassStatus::kErrorResolved: case ClassStatus::kErrorUnresolved: case ClassStatus::kNotReady: case ClassStatus::kResolved: case ClassStatus::kRetryVerificationAtRuntime: case ClassStatus::kVerifiedNeedsAccessChecks: case ClassStatus::kVerified: case ClassStatus::kSuperclassValidated: case ClassStatus::kVisiblyInitialized: break; // Expected states. default:
LOG(FATAL) << "Unexpected class status for class "
<< PrettyDescriptor(
ref.dex_file->GetClassDescriptor(ref.dex_file->GetClassDef(ref.index)))
<< " of " << status;
}
ClassStateTable::InsertResult result;
ClassStateTable* table = &compiled_classes_; do {
ClassStatus existing = ClassStatus::kNotReady; if (!table->Get(ref, &existing)) { // A classpath class. if (kIsDebugBuild) { // Check to make sure it's not a dex file for an oat file we are compiling since these // should always succeed. These do not include classes in for used libraries. for (const DexFile* dex_file : GetCompilerOptions().GetDexFilesForOatFile()) {
CHECK_NE(ref.dex_file, dex_file) << ref.dex_file->GetLocation();
}
} if (!classpath_classes_.HaveDexFile(ref.dex_file)) { // Boot classpath dex file. return;
}
table = &classpath_classes_;
table->Get(ref, &existing);
} if (existing >= status) { // Existing status is already better than we expect, break. break;
} // Update the status if we now have a greater one. This happens with vdex, // which records a class is verified, but does not resolve it.
result = table->Insert(ref, existing, status);
CHECK(result != ClassStateTable::kInsertResultInvalidDexFile) << ref.dex_file->GetLocation();
} while (result != ClassStateTable::kInsertResultSuccess);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.