Merge "Revert "Add LockSupport.park to 004-ThreadStress""
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index e2a0a39..e610bb1 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -163,6 +163,16 @@
$(ART_TEST_TARGET_GTEST_VerifierDepsMulti_DEX): $(ART_TEST_GTEST_VerifierDepsMulti_SRC) $(HOST_OUT_EXECUTABLES)/smali
$(HOST_OUT_EXECUTABLES)/smali assemble --output $@ $(filter %.smali,$^)
+ART_TEST_GTEST_VerifySoftFailDuringClinit_SRC := $(abspath $(wildcard $(LOCAL_PATH)/VerifySoftFailDuringClinit/*.smali))
+ART_TEST_HOST_GTEST_VerifySoftFailDuringClinit_DEX := $(dir $(ART_TEST_HOST_GTEST_Main_DEX))$(subst Main,VerifySoftFailDuringClinit,$(basename $(notdir $(ART_TEST_HOST_GTEST_Main_DEX))))$(suffix $(ART_TEST_HOST_GTEST_Main_DEX))
+ART_TEST_TARGET_GTEST_VerifySoftFailDuringClinit_DEX := $(dir $(ART_TEST_TARGET_GTEST_Main_DEX))$(subst Main,VerifySoftFailDuringClinit,$(basename $(notdir $(ART_TEST_TARGET_GTEST_Main_DEX))))$(suffix $(ART_TEST_TARGET_GTEST_Main_DEX))
+
+$(ART_TEST_HOST_GTEST_VerifySoftFailDuringClinit_DEX): $(ART_TEST_GTEST_VerifySoftFailDuringClinit_SRC) $(HOST_OUT_EXECUTABLES)/smali
+ $(HOST_OUT_EXECUTABLES)/smali assemble --output $@ $(filter %.smali,$^)
+
+$(ART_TEST_TARGET_GTEST_VerifySoftFailDuringClinit_DEX): $(ART_TEST_GTEST_VerifySoftFailDuringClinit_SRC) $(HOST_OUT_EXECUTABLES)/smali
+ $(HOST_OUT_EXECUTABLES)/smali assemble --output $@ $(filter %.smali,$^)
+
# Dex file dependencies for each gtest.
ART_GTEST_art_dex_file_loader_test_DEX_DEPS := GetMethodSignature Main Nested MultiDex
ART_GTEST_dex2oat_environment_tests_DEX_DEPS := Main MainStripped MultiDex MultiDexModifiedSecondary MyClassNatives Nested VerifierDeps VerifierDepsMulti
@@ -180,7 +190,7 @@
ART_GTEST_exception_test_DEX_DEPS := ExceptionHandle
ART_GTEST_hiddenapi_test_DEX_DEPS := HiddenApi
ART_GTEST_hidden_api_test_DEX_DEPS := HiddenApiSignatures
-ART_GTEST_image_test_DEX_DEPS := ImageLayoutA ImageLayoutB DefaultMethods
+ART_GTEST_image_test_DEX_DEPS := ImageLayoutA ImageLayoutB DefaultMethods VerifySoftFailDuringClinit
ART_GTEST_imtable_test_DEX_DEPS := IMTA IMTB
ART_GTEST_instrumentation_test_DEX_DEPS := Instrumentation
ART_GTEST_jni_compiler_test_DEX_DEPS := MyClassNatives
@@ -752,5 +762,8 @@
ART_TEST_GTEST_VerifierDeps_SRC :=
ART_TEST_HOST_GTEST_VerifierDeps_DEX :=
ART_TEST_TARGET_GTEST_VerifierDeps_DEX :=
+ART_TEST_GTEST_VerifySoftFailDuringClinit_SRC :=
+ART_TEST_HOST_GTEST_VerifySoftFailDuringClinit_DEX :=
+ART_TEST_TARGET_GTEST_VerifySoftFailDuringClinit_DEX :=
GTEST_DEX_DIRECTORIES :=
LOCAL_PATH :=
diff --git a/compiler/dex/dex_to_dex_compiler.cc b/compiler/dex/dex_to_dex_compiler.cc
index ad9a30f..fe99eaa 100644
--- a/compiler/dex/dex_to_dex_compiler.cc
+++ b/compiler/dex/dex_to_dex_compiler.cc
@@ -376,9 +376,7 @@
DCHECK_EQ(inst->Opcode(), Instruction::RETURN_VOID);
if (unit_.IsConstructor()) {
// Are we compiling a non clinit constructor which needs a barrier ?
- if (!unit_.IsStatic() &&
- driver_.RequiresConstructorBarrier(Thread::Current(), unit_.GetDexFile(),
- unit_.GetClassDefIndex())) {
+ if (!unit_.IsStatic() && unit_.RequiresConstructorBarrier()) {
return;
}
}
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index df6e8a8..8c276bb 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -254,7 +254,6 @@
verification_results_(verification_results),
compiler_(Compiler::Create(this, compiler_kind)),
compiler_kind_(compiler_kind),
- requires_constructor_barrier_lock_("constructor barrier lock"),
image_classes_(std::move(image_classes)),
number_of_soft_verifier_failures_(0),
had_hard_verifier_failure_(false),
@@ -1580,18 +1579,6 @@
self->ClearException();
}
-bool CompilerDriver::RequiresConstructorBarrier(const DexFile& dex_file,
- uint16_t class_def_idx) const {
- ClassAccessor accessor(dex_file, class_def_idx);
- // We require a constructor barrier if there are final instance fields.
- for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
- if (field.IsFinal()) {
- return true;
- }
- }
- return false;
-}
-
class ResolveClassFieldsAndMethodsVisitor : public CompilationVisitor {
public:
explicit ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager* manager)
@@ -1635,57 +1622,42 @@
// We want to resolve the methods and fields eagerly.
resolve_fields_and_methods = true;
}
- // If an instance field is final then we need to have a barrier on the return, static final
- // fields are assigned within the lock held for class initialization.
- bool requires_constructor_barrier = false;
- ClassAccessor accessor(dex_file, class_def_index);
- // Optionally resolve fields and methods and figure out if we need a constructor barrier.
- auto method_visitor = [&](const ClassAccessor::Method& method)
- REQUIRES_SHARED(Locks::mutator_lock_) {
- if (resolve_fields_and_methods) {
+ if (resolve_fields_and_methods) {
+ ClassAccessor accessor(dex_file, class_def_index);
+ // Optionally resolve fields and methods and figure out if we need a constructor barrier.
+ auto method_visitor = [&](const ClassAccessor::Method& method)
+ REQUIRES_SHARED(Locks::mutator_lock_) {
ArtMethod* resolved = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
method.GetIndex(),
dex_cache,
class_loader,
- /* referrer */ nullptr,
+ /*referrer=*/ nullptr,
method.GetInvokeType(class_def.access_flags_));
if (resolved == nullptr) {
CheckAndClearResolveException(soa.Self());
}
- }
- };
- accessor.VisitFieldsAndMethods(
- // static fields
- [&](ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
- if (resolve_fields_and_methods) {
+ };
+ accessor.VisitFieldsAndMethods(
+ // static fields
+ [&](ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
ArtField* resolved = class_linker->ResolveField(
- field.GetIndex(), dex_cache, class_loader, /* is_static */ true);
+ field.GetIndex(), dex_cache, class_loader, /*is_static=*/ true);
if (resolved == nullptr) {
CheckAndClearResolveException(soa.Self());
}
- }
- },
- // instance fields
- [&](ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
- if (field.IsFinal()) {
- // We require a constructor barrier if there are final instance fields.
- requires_constructor_barrier = true;
- }
- if (resolve_fields_and_methods) {
+ },
+ // instance fields
+ [&](ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
ArtField* resolved = class_linker->ResolveField(
- field.GetIndex(), dex_cache, class_loader, /* is_static */ false);
+ field.GetIndex(), dex_cache, class_loader, /*is_static=*/ false);
if (resolved == nullptr) {
CheckAndClearResolveException(soa.Self());
}
- }
- },
- /*direct methods*/ method_visitor,
- /*virtual methods*/ method_visitor);
- manager_->GetCompiler()->SetRequiresConstructorBarrier(self,
- &dex_file,
- class_def_index,
- requires_constructor_barrier);
+ },
+ /*direct_method_visitor=*/ method_visitor,
+ /*virtual_method_visitor=*/ method_visitor);
+ }
}
private:
@@ -2832,31 +2804,6 @@
return is_system_class;
}
-void CompilerDriver::SetRequiresConstructorBarrier(Thread* self,
- const DexFile* dex_file,
- uint16_t class_def_index,
- bool requires) {
- WriterMutexLock mu(self, requires_constructor_barrier_lock_);
- requires_constructor_barrier_.emplace(ClassReference(dex_file, class_def_index), requires);
-}
-
-bool CompilerDriver::RequiresConstructorBarrier(Thread* self,
- const DexFile* dex_file,
- uint16_t class_def_index) {
- ClassReference class_ref(dex_file, class_def_index);
- {
- ReaderMutexLock mu(self, requires_constructor_barrier_lock_);
- auto it = requires_constructor_barrier_.find(class_ref);
- if (it != requires_constructor_barrier_.end()) {
- return it->second;
- }
- }
- WriterMutexLock mu(self, requires_constructor_barrier_lock_);
- const bool requires = RequiresConstructorBarrier(*dex_file, class_def_index);
- requires_constructor_barrier_.emplace(class_ref, requires);
- return requires;
-}
-
std::string CompilerDriver::GetMemoryUsageString(bool extended) const {
std::ostringstream oss;
const gc::Heap* const heap = Runtime::Current()->GetHeap();
diff --git a/compiler/driver/compiler_driver.h b/compiler/driver/compiler_driver.h
index 9a83e55..f42e555 100644
--- a/compiler/driver/compiler_driver.h
+++ b/compiler/driver/compiler_driver.h
@@ -151,50 +151,6 @@
void AddCompiledMethod(const MethodReference& method_ref, CompiledMethod* const compiled_method);
CompiledMethod* RemoveCompiledMethod(const MethodReference& method_ref);
- void SetRequiresConstructorBarrier(Thread* self,
- const DexFile* dex_file,
- uint16_t class_def_index,
- bool requires)
- REQUIRES(!requires_constructor_barrier_lock_);
-
- // Do the <init> methods for this class require a constructor barrier (prior to the return)?
- // The answer is "yes", if and only if this class has any instance final fields.
- // (This must not be called for any non-<init> methods; the answer would be "no").
- //
- // ---
- //
- // JLS 17.5.1 "Semantics of final fields" mandates that all final fields are frozen at the end
- // of the invoked constructor. The constructor barrier is a conservative implementation means of
- // enforcing the freezes happen-before the object being constructed is observable by another
- // thread.
- //
- // Note: This question only makes sense for instance constructors;
- // static constructors (despite possibly having finals) never need
- // a barrier.
- //
- // JLS 12.4.2 "Detailed Initialization Procedure" approximately describes
- // class initialization as:
- //
- // lock(class.lock)
- // class.state = initializing
- // unlock(class.lock)
- //
- // invoke <clinit>
- //
- // lock(class.lock)
- // class.state = initialized
- // unlock(class.lock) <-- acts as a release
- //
- // The last operation in the above example acts as an atomic release
- // for any stores in <clinit>, which ends up being stricter
- // than what a constructor barrier needs.
- //
- // See also QuasiAtomic::ThreadFenceForConstructor().
- bool RequiresConstructorBarrier(Thread* self,
- const DexFile* dex_file,
- uint16_t class_def_index)
- REQUIRES(!requires_constructor_barrier_lock_);
-
// Resolve compiling method's class. Returns null on failure.
ObjPtr<mirror::Class> ResolveCompilingMethodsClass(const ScopedObjectAccess& soa,
Handle<mirror::DexCache> dex_cache,
@@ -407,20 +363,12 @@
void FreeThreadPools();
void CheckThreadPools();
- bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
-
const CompilerOptions* const compiler_options_;
VerificationResults* const verification_results_;
std::unique_ptr<Compiler> compiler_;
Compiler::Kind compiler_kind_;
- // All class references that require constructor barriers. If the class reference is not in the
- // set then the result has not yet been computed.
- mutable ReaderWriterMutex requires_constructor_barrier_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
- std::map<ClassReference, bool> requires_constructor_barrier_
- GUARDED_BY(requires_constructor_barrier_lock_);
-
// All class references that this compiler has compiled. Indexed by class defs.
using ClassStateTable = AtomicDexRefMap<ClassReference, ClassStatus>;
ClassStateTable compiled_classes_;
diff --git a/compiler/driver/compiler_options_map.def b/compiler/driver/compiler_options_map.def
index 1ec34ec..a593240 100644
--- a/compiler/driver/compiler_options_map.def
+++ b/compiler/driver/compiler_options_map.def
@@ -52,7 +52,7 @@
COMPILER_OPTIONS_KEY (double, TopKProfileThreshold)
COMPILER_OPTIONS_KEY (bool, AbortOnHardVerifierFailure)
COMPILER_OPTIONS_KEY (bool, AbortOnSoftVerifierFailure)
-COMPILER_OPTIONS_KEY (bool, ResolveStartupConstStrings, kIsDebugBuild)
+COMPILER_OPTIONS_KEY (bool, ResolveStartupConstStrings, false)
COMPILER_OPTIONS_KEY (std::string, DumpInitFailures)
COMPILER_OPTIONS_KEY (std::string, DumpCFG)
COMPILER_OPTIONS_KEY (Unit, DumpCFGAppend)
diff --git a/compiler/driver/dex_compilation_unit.cc b/compiler/driver/dex_compilation_unit.cc
index c90c37d..e5a6f0e 100644
--- a/compiler/driver/dex_compilation_unit.cc
+++ b/compiler/driver/dex_compilation_unit.cc
@@ -16,10 +16,14 @@
#include "dex_compilation_unit.h"
+#include "art_field.h"
#include "base/utils.h"
+#include "dex/class_accessor-inl.h"
#include "dex/code_item_accessors-inl.h"
#include "dex/descriptors_names.h"
+#include "mirror/class-inl.h"
#include "mirror/dex_cache.h"
+#include "scoped_thread_state_change-inl.h"
namespace art {
@@ -31,7 +35,8 @@
uint32_t method_idx,
uint32_t access_flags,
const VerifiedMethod* verified_method,
- Handle<mirror::DexCache> dex_cache)
+ Handle<mirror::DexCache> dex_cache,
+ Handle<mirror::Class> compiling_class)
: class_loader_(class_loader),
class_linker_(class_linker),
dex_file_(&dex_file),
@@ -41,7 +46,8 @@
access_flags_(access_flags),
verified_method_(verified_method),
dex_cache_(dex_cache),
- code_item_accessor_(dex_file, code_item) {}
+ code_item_accessor_(dex_file, code_item),
+ compiling_class_(compiling_class) {}
const std::string& DexCompilationUnit::GetSymbol() {
if (symbol_.empty()) {
@@ -51,4 +57,32 @@
return symbol_;
}
+bool DexCompilationUnit::RequiresConstructorBarrier() const {
+ // Constructor barriers are applicable only for <init> methods.
+ DCHECK(!IsStatic());
+ DCHECK(IsConstructor());
+
+ // We require a constructor barrier if there are final instance fields.
+ if (GetCompilingClass().GetReference() != nullptr && !GetCompilingClass().IsNull()) {
+ // Decoding class data can be slow, so iterate over fields of the compiling class if resolved.
+ ScopedObjectAccess soa(Thread::Current());
+ ObjPtr<mirror::Class> compiling_class = GetCompilingClass().Get();
+ for (size_t i = 0, size = compiling_class->NumInstanceFields(); i != size; ++i) {
+ ArtField* field = compiling_class->GetInstanceField(i);
+ if (field->IsFinal()) {
+ return true;
+ }
+ }
+ } else {
+ // Iterate over field definitions in the class data.
+ ClassAccessor accessor(*GetDexFile(), GetClassDefIndex());
+ for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
+ if (field.IsFinal()) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
} // namespace art
diff --git a/compiler/driver/dex_compilation_unit.h b/compiler/driver/dex_compilation_unit.h
index c1ae3c9..757f0e7 100644
--- a/compiler/driver/dex_compilation_unit.h
+++ b/compiler/driver/dex_compilation_unit.h
@@ -27,6 +27,7 @@
namespace art {
namespace mirror {
+class Class;
class ClassLoader;
class DexCache;
} // namespace mirror
@@ -43,7 +44,8 @@
uint32_t method_idx,
uint32_t access_flags,
const VerifiedMethod* verified_method,
- Handle<mirror::DexCache> dex_cache);
+ Handle<mirror::DexCache> dex_cache,
+ Handle<mirror::Class> compiling_class = Handle<mirror::Class>());
Handle<mirror::ClassLoader> GetClassLoader() const {
return class_loader_;
@@ -117,6 +119,45 @@
return code_item_accessor_;
}
+ Handle<mirror::Class> GetCompilingClass() const {
+ return compiling_class_;
+ }
+
+ // Does this <init> method require a constructor barrier (prior to the return)?
+ // The answer is "yes", if and only if the class has any instance final fields.
+ // (This must not be called for any non-<init> methods; the answer would be "no").
+ //
+ // ---
+ //
+ // JLS 17.5.1 "Semantics of final fields" mandates that all final fields are frozen at the end
+ // of the invoked constructor. The constructor barrier is a conservative implementation means of
+ // enforcing the freezes happen-before the object being constructed is observable by another
+ // thread.
+ //
+ // Note: This question only makes sense for instance constructors;
+ // static constructors (despite possibly having finals) never need
+ // a barrier.
+ //
+ // JLS 12.4.2 "Detailed Initialization Procedure" approximately describes
+ // class initialization as:
+ //
+ // lock(class.lock)
+ // class.state = initializing
+ // unlock(class.lock)
+ //
+ // invoke <clinit>
+ //
+ // lock(class.lock)
+ // class.state = initialized
+ // unlock(class.lock) <-- acts as a release
+ //
+ // The last operation in the above example acts as an atomic release
+ // for any stores in <clinit>, which ends up being stricter
+ // than what a constructor barrier needs.
+ //
+ // See also QuasiAtomic::ThreadFenceForConstructor().
+ bool RequiresConstructorBarrier() const;
+
private:
const Handle<mirror::ClassLoader> class_loader_;
@@ -134,6 +175,8 @@
const CodeItemDataAccessor code_item_accessor_;
+ Handle<mirror::Class> compiling_class_;
+
std::string symbol_;
};
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index a1a5692..64aa1b9 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -21,6 +21,7 @@
#include "base/bit_vector-inl.h"
#include "base/logging.h"
#include "block_builder.h"
+#include "code_generator.h"
#include "data_type-inl.h"
#include "dex/verified_method.h"
#include "driver/compiler_options.h"
@@ -40,7 +41,6 @@
const CodeItemDebugInfoAccessor& accessor,
const DexCompilationUnit* dex_compilation_unit,
const DexCompilationUnit* outer_compilation_unit,
- CompilerDriver* driver,
CodeGenerator* code_generator,
OptimizingCompilerStats* compiler_stats,
ArrayRef<const uint8_t> interpreter_metadata,
@@ -50,7 +50,6 @@
code_item_accessor_(accessor),
dex_compilation_unit_(dex_compilation_unit),
outer_compilation_unit_(outer_compilation_unit),
- compiler_driver_(driver),
code_generator_(code_generator),
compilation_stats_(compiler_stats),
interpreter_metadata_(interpreter_metadata),
@@ -67,19 +66,18 @@
code_item_accessor_(accessor),
dex_compilation_unit_(dex_compilation_unit),
outer_compilation_unit_(nullptr),
- compiler_driver_(nullptr),
code_generator_(nullptr),
compilation_stats_(nullptr),
handles_(handles),
return_type_(return_type) {}
bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
- if (compiler_driver_ == nullptr) {
- // Note that the compiler driver is null when unit testing.
+ if (code_generator_ == nullptr) {
+ // Note that the codegen is null when unit testing.
return false;
}
- const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions();
+ const CompilerOptions& compiler_options = code_generator_->GetCompilerOptions();
CompilerFilter::Filter compiler_filter = compiler_options.GetCompilerFilter();
if (compiler_filter == CompilerFilter::kEverything) {
return false;
@@ -131,7 +129,6 @@
return_type_,
dex_compilation_unit_,
outer_compilation_unit_,
- compiler_driver_,
code_generator_,
interpreter_metadata_,
compilation_stats_,
@@ -203,7 +200,6 @@
return_type_,
dex_compilation_unit_,
outer_compilation_unit_,
- compiler_driver_,
code_generator_,
interpreter_metadata_,
compilation_stats_,
diff --git a/compiler/optimizing/builder.h b/compiler/optimizing/builder.h
index 5a1914c..6152740 100644
--- a/compiler/optimizing/builder.h
+++ b/compiler/optimizing/builder.h
@@ -22,7 +22,6 @@
#include "dex/code_item_accessors.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_file.h"
-#include "driver/compiler_driver.h"
#include "nodes.h"
namespace art {
@@ -38,7 +37,6 @@
const CodeItemDebugInfoAccessor& accessor,
const DexCompilationUnit* dex_compilation_unit,
const DexCompilationUnit* outer_compilation_unit,
- CompilerDriver* driver,
CodeGenerator* code_generator,
OptimizingCompilerStats* compiler_stats,
ArrayRef<const uint8_t> interpreter_metadata,
@@ -70,7 +68,6 @@
// The compilation unit of the enclosing method being compiled.
const DexCompilationUnit* const outer_compilation_unit_;
- CompilerDriver* const compiler_driver_;
CodeGenerator* const code_generator_;
OptimizingCompilerStats* const compilation_stats_;
diff --git a/compiler/optimizing/code_generator.h b/compiler/optimizing/code_generator.h
index 3f56078..39966ff 100644
--- a/compiler/optimizing/code_generator.h
+++ b/compiler/optimizing/code_generator.h
@@ -59,7 +59,6 @@
class Assembler;
class CodeGenerator;
-class CompilerDriver;
class CompilerOptions;
class StackMapStream;
class ParallelMoveResolver;
diff --git a/compiler/optimizing/inliner.cc b/compiler/optimizing/inliner.cc
index dd781c2..f56f9cb 100644
--- a/compiler/optimizing/inliner.cc
+++ b/compiler/optimizing/inliner.cc
@@ -1757,6 +1757,7 @@
caller_compilation_unit_.GetClassLoader(),
handles_);
+ Handle<mirror::Class> compiling_class = handles_->NewHandle(resolved_method->GetDeclaringClass());
DexCompilationUnit dex_compilation_unit(
class_loader,
class_linker,
@@ -1766,7 +1767,8 @@
method_index,
resolved_method->GetAccessFlags(),
/* verified_method */ nullptr,
- dex_cache);
+ dex_cache,
+ compiling_class);
InvokeType invoke_type = invoke_instruction->GetInvokeType();
if (invoke_type == kInterface) {
@@ -1805,7 +1807,6 @@
code_item_accessor,
&dex_compilation_unit,
&outer_compilation_unit_,
- compiler_driver_,
codegen_,
inline_stats_,
resolved_method->GetQuickenedInfo(),
diff --git a/compiler/optimizing/instruction_builder.cc b/compiler/optimizing/instruction_builder.cc
index 63b2705..e9b5b5a 100644
--- a/compiler/optimizing/instruction_builder.cc
+++ b/compiler/optimizing/instruction_builder.cc
@@ -20,11 +20,11 @@
#include "base/arena_bit_vector.h"
#include "base/bit_vector-inl.h"
#include "block_builder.h"
-#include "class_linker.h"
+#include "class_linker-inl.h"
+#include "code_generator.h"
#include "data_type-inl.h"
#include "dex/bytecode_utils.h"
#include "dex/dex_instruction-inl.h"
-#include "driver/compiler_driver-inl.h"
#include "driver/dex_compilation_unit.h"
#include "driver/compiler_options.h"
#include "imtable-inl.h"
@@ -47,7 +47,6 @@
DataType::Type return_type,
const DexCompilationUnit* dex_compilation_unit,
const DexCompilationUnit* outer_compilation_unit,
- CompilerDriver* compiler_driver,
CodeGenerator* code_generator,
ArrayRef<const uint8_t> interpreter_metadata,
OptimizingCompilerStats* compiler_stats,
@@ -61,7 +60,6 @@
return_type_(return_type),
block_builder_(block_builder),
ssa_builder_(ssa_builder),
- compiler_driver_(compiler_driver),
code_generator_(code_generator),
dex_compilation_unit_(dex_compilation_unit),
outer_compilation_unit_(outer_compilation_unit),
@@ -73,7 +71,8 @@
current_locals_(nullptr),
latest_result_(nullptr),
current_this_parameter_(nullptr),
- loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
+ loop_headers_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
+ class_cache_(std::less<dex::TypeIndex>(), local_allocator->Adapter(kArenaAllocGraphBuilder)) {
loop_headers_.reserve(kDefaultNumberOfLoops);
}
@@ -319,8 +318,8 @@
// Find locations where we want to generate extra stackmaps for native debugging.
// This allows us to generate the info only at interesting points (for example,
// at start of java statement) rather than before every dex instruction.
- const bool native_debuggable = compiler_driver_ != nullptr &&
- compiler_driver_->GetCompilerOptions().GetNativeDebuggable();
+ const bool native_debuggable = code_generator_ != nullptr &&
+ code_generator_->GetCompilerOptions().GetNativeDebuggable();
ArenaBitVector* native_debug_info_locations = nullptr;
if (native_debuggable) {
native_debug_info_locations = FindNativeDebugInfoLocations();
@@ -709,20 +708,18 @@
// Does the method being compiled need any constructor barriers being inserted?
// (Always 'false' for methods that aren't <init>.)
-static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, CompilerDriver* driver) {
+static bool RequiresConstructorBarrier(const DexCompilationUnit* cu) {
// Can be null in unit tests only.
if (UNLIKELY(cu == nullptr)) {
return false;
}
- Thread* self = Thread::Current();
- return cu->IsConstructor()
- && !cu->IsStatic()
- // RequiresConstructorBarrier must only be queried for <init> methods;
- // it's effectively "false" for every other method.
- //
- // See CompilerDriver::RequiresConstructBarrier for more explanation.
- && driver->RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex());
+ // Constructor barriers are applicable only for <init> methods.
+ if (LIKELY(!cu->IsConstructor() || cu->IsStatic())) {
+ return false;
+ }
+
+ return cu->RequiresConstructorBarrier();
}
// Returns true if `block` has only one successor which starts at the next
@@ -768,7 +765,7 @@
// Only <init> (which is a return-void) could possibly have a constructor fence.
// This may insert additional redundant constructor fences from the super constructors.
// TODO: remove redundant constructor fences (b/36656456).
- if (RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_)) {
+ if (RequiresConstructorBarrier(dex_compilation_unit_)) {
// Compiling instance constructor.
DCHECK_STREQ("<init>", graph_->GetMethodName());
@@ -782,7 +779,7 @@
}
AppendInstruction(new (allocator_) HReturnVoid(dex_pc));
} else {
- DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_, compiler_driver_));
+ DCHECK(!RequiresConstructorBarrier(dex_compilation_unit_));
HInstruction* value = LoadLocal(instruction.VRegA(), type);
AppendInstruction(new (allocator_) HReturn(value, dex_pc));
}
@@ -849,7 +846,7 @@
// make this an invoke-unresolved to handle cross-dex invokes or abstract super methods, both of
// which require runtime handling.
if (invoke_type == kSuper) {
- ObjPtr<mirror::Class> compiling_class = ResolveCompilingClass(soa);
+ ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
if (compiling_class == nullptr) {
// We could not determine the method's class we need to wait until runtime.
DCHECK(Runtime::Current()->IsAotCompiler());
@@ -879,8 +876,8 @@
// The back-end code generator relies on this check in order to ensure that it will not
// attempt to read the dex_cache with a dex_method_index that is not from the correct
// dex_file. If we didn't do this check then the dex_method_index will not be updated in the
- // builder, which means that the code-generator (and compiler driver during sharpening and
- // inliner, maybe) might invoke an incorrect method.
+ // builder, which means that the code-generator (and sharpening and inliner, maybe)
+ // might invoke an incorrect method.
// TODO: The actual method could still be referenced in the current dex file, so we
// could try locating it.
// TODO: Remove the dex_file restriction.
@@ -969,7 +966,7 @@
ScopedObjectAccess soa(Thread::Current());
if (invoke_type == kStatic) {
clinit_check =
- ProcessClinitCheckForInvoke(soa, dex_pc, resolved_method, &clinit_check_requirement);
+ ProcessClinitCheckForInvoke(dex_pc, resolved_method, &clinit_check_requirement);
} else if (invoke_type == kSuper) {
if (IsSameDexFile(*resolved_method->GetDexFile(), *dex_compilation_unit_->GetDexFile())) {
// Update the method index to the one resolved. Note that this may be a no-op if
@@ -1055,7 +1052,7 @@
HInstruction* cls = load_class;
Handle<mirror::Class> klass = load_class->GetClass();
- if (!IsInitialized(soa, klass)) {
+ if (!IsInitialized(klass)) {
cls = new (allocator_) HClinitCheck(load_class, dex_pc);
AppendInstruction(cls);
}
@@ -1284,7 +1281,7 @@
return true;
}
-bool HInstructionBuilder::IsInitialized(ScopedObjectAccess& soa, Handle<mirror::Class> cls) const {
+bool HInstructionBuilder::IsInitialized(Handle<mirror::Class> cls) const {
if (cls == nullptr) {
return false;
}
@@ -1299,7 +1296,7 @@
}
// Assume loaded only if klass is in the boot image. App classes cannot be assumed
// loaded because we don't even know what class loader will be used to load them.
- if (IsInBootImage(cls.Get(), compiler_driver_->GetCompilerOptions())) {
+ if (IsInBootImage(cls.Get(), code_generator_->GetCompilerOptions())) {
return true;
}
}
@@ -1312,29 +1309,20 @@
// can be completely initialized while the superclass is initializing and the subclass
// remains initialized when the superclass initializer throws afterwards. b/62478025
// Note: The HClinitCheck+HInvokeStaticOrDirect merging can still apply.
- ObjPtr<mirror::Class> outermost_cls = ResolveOutermostCompilingClass(soa);
- bool is_outer_static_or_constructor =
- (outer_compilation_unit_->GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u;
- if (is_outer_static_or_constructor && outermost_cls == cls.Get()) {
+ auto is_static_method_or_constructor_of_cls = [cls](const DexCompilationUnit& compilation_unit)
+ REQUIRES_SHARED(Locks::mutator_lock_) {
+ return (compilation_unit.GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u &&
+ compilation_unit.GetCompilingClass().Get() == cls.Get();
+ };
+ if (is_static_method_or_constructor_of_cls(*outer_compilation_unit_) ||
+ // Check also the innermost method. Though excessive copies of ClinitCheck can be
+ // eliminated by GVN, that happens only after the decision whether to inline the
+ // graph or not and that may depend on the presence of the ClinitCheck.
+ // TODO: We should walk over the entire inlined method chain, but we don't pass that
+ // information to the builder.
+ is_static_method_or_constructor_of_cls(*dex_compilation_unit_)) {
return true;
}
- // Remember if the compiled class is a subclass of `cls`. By the time this is used
- // below the `outermost_cls` may be invalidated by calling ResolveCompilingClass().
- bool is_subclass = IsSubClass(outermost_cls, cls.Get());
- if (dex_compilation_unit_ != outer_compilation_unit_) {
- // Check also the innermost method. Though excessive copies of ClinitCheck can be
- // eliminated by GVN, that happens only after the decision whether to inline the
- // graph or not and that may depend on the presence of the ClinitCheck.
- // TODO: We should walk over the entire inlined method chain, but we don't pass that
- // information to the builder.
- ObjPtr<mirror::Class> innermost_cls = ResolveCompilingClass(soa);
- bool is_inner_static_or_constructor =
- (dex_compilation_unit_->GetAccessFlags() & (kAccStatic | kAccConstructor)) != 0u;
- if (is_inner_static_or_constructor && innermost_cls == cls.Get()) {
- return true;
- }
- is_subclass = is_subclass || IsSubClass(innermost_cls, cls.Get());
- }
// Otherwise, we may be able to avoid the check if `cls` is a superclass of a method being
// compiled here (anywhere in the inlining chain) as the `cls` must have started initializing
@@ -1355,7 +1343,12 @@
// TODO: We should walk over the entire inlined methods chain, but we don't pass that
// information to the builder. (We could also check if we're guaranteed a non-null instance
// of `cls` at this location but that's outside the scope of the instruction builder.)
- if (is_subclass && HasTrivialInitialization(cls.Get(), compiler_driver_->GetCompilerOptions())) {
+ bool is_subclass = IsSubClass(outer_compilation_unit_->GetCompilingClass().Get(), cls.Get());
+ if (dex_compilation_unit_ != outer_compilation_unit_) {
+ is_subclass = is_subclass ||
+ IsSubClass(dex_compilation_unit_->GetCompilingClass().Get(), cls.Get());
+ }
+ if (is_subclass && HasTrivialInitialization(cls.Get(), code_generator_->GetCompilerOptions())) {
return true;
}
@@ -1363,18 +1356,16 @@
}
HClinitCheck* HInstructionBuilder::ProcessClinitCheckForInvoke(
- ScopedObjectAccess& soa,
uint32_t dex_pc,
ArtMethod* resolved_method,
HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement) {
Handle<mirror::Class> klass = handles_->NewHandle(resolved_method->GetDeclaringClass());
HClinitCheck* clinit_check = nullptr;
- if (IsInitialized(soa, klass)) {
+ if (IsInitialized(klass)) {
*clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone;
} else {
- HLoadClass* cls = BuildLoadClass(soa,
- klass->GetDexTypeIndex(),
+ HLoadClass* cls = BuildLoadClass(klass->GetDexTypeIndex(),
klass->GetDexFile(),
klass,
dex_pc,
@@ -1610,43 +1601,6 @@
return true;
}
-static ObjPtr<mirror::Class> ResolveClassFrom(ScopedObjectAccess& soa,
- CompilerDriver* driver,
- const DexCompilationUnit& compilation_unit)
- REQUIRES_SHARED(Locks::mutator_lock_) {
- Handle<mirror::ClassLoader> class_loader = compilation_unit.GetClassLoader();
- Handle<mirror::DexCache> dex_cache = compilation_unit.GetDexCache();
-
- return driver->ResolveCompilingMethodsClass(soa, dex_cache, class_loader, &compilation_unit);
-}
-
-ObjPtr<mirror::Class> HInstructionBuilder::ResolveOutermostCompilingClass(
- ScopedObjectAccess& soa) const {
- return ResolveClassFrom(soa, compiler_driver_, *outer_compilation_unit_);
-}
-
-ObjPtr<mirror::Class> HInstructionBuilder::ResolveCompilingClass(ScopedObjectAccess& soa) const {
- return ResolveClassFrom(soa, compiler_driver_, *dex_compilation_unit_);
-}
-
-bool HInstructionBuilder::IsOutermostCompilingClass(dex::TypeIndex type_index) const {
- ScopedObjectAccess soa(Thread::Current());
- StackHandleScope<2> hs(soa.Self());
- Handle<mirror::DexCache> dex_cache = dex_compilation_unit_->GetDexCache();
- Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
- Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass(
- soa, dex_cache, class_loader, type_index, dex_compilation_unit_)));
- Handle<mirror::Class> outer_class(hs.NewHandle(ResolveOutermostCompilingClass(soa)));
-
- // GetOutermostCompilingClass returns null when the class is unresolved
- // (e.g. if it derives from an unresolved class). This is bogus knowing that
- // we are compiling it.
- // When this happens we cannot establish a direct relation between the current
- // class and the outer class, so we return false.
- // (Note that this is only used for optimizing invokes and field accesses)
- return (cls != nullptr) && (outer_class.Get() == cls.Get());
-}
-
void HInstructionBuilder::BuildUnresolvedStaticFieldAccess(const Instruction& instruction,
uint32_t dex_pc,
bool is_put,
@@ -1666,18 +1620,17 @@
ArtField* HInstructionBuilder::ResolveField(uint16_t field_idx, bool is_static, bool is_put) {
ScopedObjectAccess soa(Thread::Current());
- StackHandleScope<2> hs(soa.Self());
ClassLinker* class_linker = dex_compilation_unit_->GetClassLinker();
Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
- Handle<mirror::Class> compiling_class(hs.NewHandle(ResolveCompilingClass(soa)));
ArtField* resolved_field = class_linker->ResolveField(field_idx,
dex_compilation_unit_->GetDexCache(),
class_loader,
is_static);
+ DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
if (UNLIKELY(resolved_field == nullptr)) {
- // Clean up any exception left by type resolution.
+ // Clean up any exception left by field resolution.
soa.Self()->ClearException();
return nullptr;
}
@@ -1689,6 +1642,7 @@
}
// Check access.
+ Handle<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass();
if (compiling_class == nullptr) {
if (!resolved_field->IsPublic()) {
return nullptr;
@@ -1731,8 +1685,7 @@
DataType::Type field_type = GetFieldAccessType(*dex_file_, field_index);
Handle<mirror::Class> klass = handles_->NewHandle(resolved_field->GetDeclaringClass());
- HLoadClass* constant = BuildLoadClass(soa,
- klass->GetDexTypeIndex(),
+ HLoadClass* constant = BuildLoadClass(klass->GetDexTypeIndex(),
klass->GetDexFile(),
klass,
dex_pc,
@@ -1748,7 +1701,7 @@
}
HInstruction* cls = constant;
- if (!IsInitialized(soa, klass)) {
+ if (!IsInitialized(klass)) {
cls = new (allocator_) HClinitCheck(constant, dex_pc);
AppendInstruction(cls);
}
@@ -1989,12 +1942,11 @@
ScopedObjectAccess soa(Thread::Current());
const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Handle<mirror::Class> klass = ResolveClass(soa, type_index);
- bool needs_access_check = LoadClassNeedsAccessCheck(soa, klass);
- return BuildLoadClass(soa, type_index, dex_file, klass, dex_pc, needs_access_check);
+ bool needs_access_check = LoadClassNeedsAccessCheck(klass);
+ return BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
}
-HLoadClass* HInstructionBuilder::BuildLoadClass(ScopedObjectAccess& soa,
- dex::TypeIndex type_index,
+HLoadClass* HInstructionBuilder::BuildLoadClass(dex::TypeIndex type_index,
const DexFile& dex_file,
Handle<mirror::Class> klass,
uint32_t dex_pc,
@@ -2011,11 +1963,8 @@
}
// Note: `klass` must be from `handles_`.
- bool is_referrers_class = false;
- if (klass != nullptr) {
- ObjPtr<mirror::Class> outermost_cls = ResolveOutermostCompilingClass(soa);
- is_referrers_class = (outermost_cls == klass.Get());
- }
+ bool is_referrers_class =
+ (klass != nullptr) && (outer_compilation_unit_->GetCompilingClass().Get() == klass.Get());
HLoadClass* load_class = new (allocator_) HLoadClass(
graph_->GetCurrentMethod(),
type_index,
@@ -2041,22 +1990,28 @@
Handle<mirror::Class> HInstructionBuilder::ResolveClass(ScopedObjectAccess& soa,
dex::TypeIndex type_index) {
- Handle<mirror::ClassLoader> class_loader = dex_compilation_unit_->GetClassLoader();
- ObjPtr<mirror::Class> klass = compiler_driver_->ResolveClass(
- soa, dex_compilation_unit_->GetDexCache(), class_loader, type_index, dex_compilation_unit_);
- // TODO: Avoid creating excessive handles if the method references the same class repeatedly.
- // (Use a map on the local_allocator_.)
- return handles_->NewHandle(klass);
+ auto it = class_cache_.find(type_index);
+ if (it != class_cache_.end()) {
+ return it->second;
+ }
+
+ ObjPtr<mirror::Class> klass = dex_compilation_unit_->GetClassLinker()->ResolveType(
+ type_index, dex_compilation_unit_->GetDexCache(), dex_compilation_unit_->GetClassLoader());
+ DCHECK_EQ(klass == nullptr, soa.Self()->IsExceptionPending());
+ soa.Self()->ClearException(); // Clean up the exception left by type resolution if any.
+
+ Handle<mirror::Class> h_klass = handles_->NewHandle(klass);
+ class_cache_.Put(type_index, h_klass);
+ return h_klass;
}
-bool HInstructionBuilder::LoadClassNeedsAccessCheck(ScopedObjectAccess& soa,
- Handle<mirror::Class> klass) {
+bool HInstructionBuilder::LoadClassNeedsAccessCheck(Handle<mirror::Class> klass) {
if (klass == nullptr) {
return true;
} else if (klass->IsPublic()) {
return false;
} else {
- ObjPtr<mirror::Class> compiling_class = ResolveCompilingClass(soa);
+ ObjPtr<mirror::Class> compiling_class = dex_compilation_unit_->GetCompilingClass().Get();
return compiling_class == nullptr || !compiling_class->CanAccess(klass.Get());
}
}
@@ -2085,7 +2040,7 @@
ScopedObjectAccess soa(Thread::Current());
const DexFile& dex_file = *dex_compilation_unit_->GetDexFile();
Handle<mirror::Class> klass = ResolveClass(soa, type_index);
- bool needs_access_check = LoadClassNeedsAccessCheck(soa, klass);
+ bool needs_access_check = LoadClassNeedsAccessCheck(klass);
TypeCheckKind check_kind = HSharpening::ComputeTypeCheckKind(
klass.Get(), code_generator_, needs_access_check);
@@ -2103,7 +2058,7 @@
bitstring_path_to_root = graph_->GetIntConstant(static_cast<int32_t>(path_to_root), dex_pc);
bitstring_mask = graph_->GetIntConstant(static_cast<int32_t>(mask), dex_pc);
} else {
- class_or_null = BuildLoadClass(soa, type_index, dex_file, klass, dex_pc, needs_access_check);
+ class_or_null = BuildLoadClass(type_index, dex_file, klass, dex_pc, needs_access_check);
}
DCHECK(class_or_null != nullptr);
diff --git a/compiler/optimizing/instruction_builder.h b/compiler/optimizing/instruction_builder.h
index 2ab2139..d701445 100644
--- a/compiler/optimizing/instruction_builder.h
+++ b/compiler/optimizing/instruction_builder.h
@@ -34,7 +34,6 @@
class ArtField;
class ArtMethod;
class CodeGenerator;
-class CompilerDriver;
class DexCompilationUnit;
class HBasicBlockBuilder;
class Instruction;
@@ -59,7 +58,6 @@
DataType::Type return_type,
const DexCompilationUnit* dex_compilation_unit,
const DexCompilationUnit* outer_compilation_unit,
- CompilerDriver* compiler_driver,
CodeGenerator* code_generator,
ArrayRef<const uint8_t> interpreter_metadata,
OptimizingCompilerStats* compiler_stats,
@@ -222,8 +220,7 @@
// Builds a `HLoadClass` loading the given `type_index`.
HLoadClass* BuildLoadClass(dex::TypeIndex type_index, uint32_t dex_pc);
- HLoadClass* BuildLoadClass(ScopedObjectAccess& soa,
- dex::TypeIndex type_index,
+ HLoadClass* BuildLoadClass(dex::TypeIndex type_index,
const DexFile& dex_file,
Handle<mirror::Class> klass,
uint32_t dex_pc,
@@ -233,7 +230,7 @@
Handle<mirror::Class> ResolveClass(ScopedObjectAccess& soa, dex::TypeIndex type_index)
REQUIRES_SHARED(Locks::mutator_lock_);
- bool LoadClassNeedsAccessCheck(ScopedObjectAccess& soa, Handle<mirror::Class> klass)
+ bool LoadClassNeedsAccessCheck(Handle<mirror::Class> klass)
REQUIRES_SHARED(Locks::mutator_lock_);
// Builds a `HLoadMethodHandle` loading the given `method_handle_index`.
@@ -242,17 +239,6 @@
// Builds a `HLoadMethodType` loading the given `proto_index`.
void BuildLoadMethodType(dex::ProtoIndex proto_index, uint32_t dex_pc);
- // Returns the outer-most compiling method's class.
- ObjPtr<mirror::Class> ResolveOutermostCompilingClass(ScopedObjectAccess& soa) const
- REQUIRES_SHARED(Locks::mutator_lock_);
-
- // Returns the class whose method is being compiled.
- ObjPtr<mirror::Class> ResolveCompilingClass(ScopedObjectAccess& soa) const
- REQUIRES_SHARED(Locks::mutator_lock_);
-
- // Returns whether `type_index` points to the outer-most compiling method's class.
- bool IsOutermostCompilingClass(dex::TypeIndex type_index) const;
-
void PotentiallySimplifyFakeString(uint16_t original_dex_register,
uint32_t dex_pc,
HInvoke* invoke);
@@ -275,7 +261,6 @@
void HandleStringInitResult(HInvokeStaticOrDirect* invoke);
HClinitCheck* ProcessClinitCheckForInvoke(
- ScopedObjectAccess& soa,
uint32_t dex_pc,
ArtMethod* method,
HInvokeStaticOrDirect::ClinitCheckRequirement* clinit_check_requirement)
@@ -289,7 +274,7 @@
void BuildConstructorFenceForAllocation(HInstruction* allocation);
// Return whether the compiler can assume `cls` is initialized.
- bool IsInitialized(ScopedObjectAccess& soa, Handle<mirror::Class> cls) const
+ bool IsInitialized(Handle<mirror::Class> cls) const
REQUIRES_SHARED(Locks::mutator_lock_);
// Try to resolve a method using the class linker. Return null if a method could
@@ -320,8 +305,6 @@
HBasicBlockBuilder* const block_builder_;
SsaBuilder* const ssa_builder_;
- CompilerDriver* const compiler_driver_;
-
CodeGenerator* const code_generator_;
// The compilation unit of the current method being compiled. Note that
@@ -351,6 +334,10 @@
ScopedArenaVector<HBasicBlock*> loop_headers_;
+ // Cached resolved types for the current compilation unit's DexFile.
+ // Handle<>s reference entries in the `handles_`.
+ ScopedArenaSafeMap<dex::TypeIndex, Handle<mirror::Class>> class_cache_;
+
static constexpr int kDefaultNumberOfLoops = 2;
DISALLOW_COPY_AND_ASSIGN(HInstructionBuilder);
diff --git a/compiler/optimizing/intrinsics.h b/compiler/optimizing/intrinsics.h
index 8245453..5bd1122 100644
--- a/compiler/optimizing/intrinsics.h
+++ b/compiler/optimizing/intrinsics.h
@@ -24,7 +24,6 @@
namespace art {
-class CompilerDriver;
class DexFile;
// Positive floating-point infinities.
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index 6ebe89e..2124380 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -7402,7 +7402,7 @@
// }
//
// See also:
-// * CompilerDriver::RequiresConstructorBarrier
+// * DexCompilationUnit::RequiresConstructorBarrier
// * QuasiAtomic::ThreadFenceForConstructor
//
class HConstructorFence final : public HVariableInputSizeInstruction {
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index a95ddff..4f495b6 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -870,7 +870,6 @@
code_item_accessor,
&dex_compilation_unit,
&dex_compilation_unit,
- compiler_driver,
codegen.get(),
compilation_stats_.get(),
interpreter_metadata,
@@ -991,7 +990,6 @@
CodeItemDebugInfoAccessor(), // Null code item.
&dex_compilation_unit,
&dex_compilation_unit,
- compiler_driver,
codegen.get(),
compilation_stats_.get(),
/* interpreter_metadata */ ArrayRef<const uint8_t>(),
@@ -1056,6 +1054,15 @@
std::unique_ptr<CodeGenerator> codegen;
bool compiled_intrinsic = false;
{
+ ScopedObjectAccess soa(Thread::Current());
+ ArtMethod* method =
+ runtime->GetClassLinker()->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
+ method_idx, dex_cache, jclass_loader, /*referrer=*/ nullptr, invoke_type);
+ DCHECK_EQ(method == nullptr, soa.Self()->IsExceptionPending());
+ soa.Self()->ClearException(); // Suppress exception if any.
+ VariableSizedHandleScope handles(soa.Self());
+ Handle<mirror::Class> compiling_class =
+ handles.NewHandle(method != nullptr ? method->GetDeclaringClass() : nullptr);
DexCompilationUnit dex_compilation_unit(
jclass_loader,
runtime->GetClassLinker(),
@@ -1064,12 +1071,9 @@
class_def_idx,
method_idx,
access_flags,
- /* verified_method */ nullptr, // Not needed by the Optimizing compiler.
- dex_cache);
- ScopedObjectAccess soa(Thread::Current());
- ArtMethod* method = compiler_driver->ResolveMethod(
- soa, dex_cache, jclass_loader, &dex_compilation_unit, method_idx, invoke_type);
- VariableSizedHandleScope handles(soa.Self());
+ /*verified_method=*/ nullptr, // Not needed by the Optimizing compiler.
+ dex_cache,
+ compiling_class);
// Go to native so that we don't block GC during compilation.
ScopedThreadSuspension sts(soa.Self(), kNative);
if (method != nullptr && UNLIKELY(method->IsIntrinsic())) {
@@ -1171,21 +1175,23 @@
if (compiler_options.IsBootImage()) {
ScopedObjectAccess soa(Thread::Current());
ArtMethod* method = runtime->GetClassLinker()->LookupResolvedMethod(
- method_idx, dex_cache.Get(), /* class_loader */ nullptr);
+ method_idx, dex_cache.Get(), /*class_loader=*/ nullptr);
if (method != nullptr && UNLIKELY(method->IsIntrinsic())) {
+ VariableSizedHandleScope handles(soa.Self());
ScopedNullHandle<mirror::ClassLoader> class_loader; // null means boot class path loader.
+ Handle<mirror::Class> compiling_class = handles.NewHandle(method->GetDeclaringClass());
DexCompilationUnit dex_compilation_unit(
class_loader,
runtime->GetClassLinker(),
dex_file,
- /* code_item */ nullptr,
- /* class_def_idx */ DexFile::kDexNoIndex16,
+ /*code_item=*/ nullptr,
+ /*class_def_idx=*/ DexFile::kDexNoIndex16,
method_idx,
access_flags,
- /* verified_method */ nullptr,
- dex_cache);
+ /*verified_method=*/ nullptr,
+ dex_cache,
+ compiling_class);
CodeVectorAllocator code_allocator(&allocator);
- VariableSizedHandleScope handles(soa.Self());
// Go to native so that we don't block GC during compilation.
ScopedThreadSuspension sts(soa.Self(), kNative);
std::unique_ptr<CodeGenerator> codegen(
@@ -1349,6 +1355,7 @@
std::unique_ptr<CodeGenerator> codegen;
{
+ Handle<mirror::Class> compiling_class = handles.NewHandle(method->GetDeclaringClass());
DexCompilationUnit dex_compilation_unit(
class_loader,
runtime->GetClassLinker(),
@@ -1357,8 +1364,9 @@
class_def_idx,
method_idx,
access_flags,
- /* verified_method */ nullptr,
- dex_cache);
+ /*verified_method=*/ nullptr,
+ dex_cache,
+ compiling_class);
// Go to native so that we don't block GC during compilation.
ScopedThreadSuspension sts(self, kNative);
diff --git a/dex2oat/dex2oat_test.cc b/dex2oat/dex2oat_test.cc
index 10d2b6f..d22b301 100644
--- a/dex2oat/dex2oat_test.cc
+++ b/dex2oat/dex2oat_test.cc
@@ -2178,6 +2178,22 @@
EXPECT_TRUE(preresolved_seen.find("Other class init") == preresolved_seen.end());
// Expect the sets match.
EXPECT_GE(seen.size(), preresolved_seen.size());
+
+ // Verify what strings are marked as boot image.
+ std::set<std::string> boot_image_strings;
+ std::set<std::string> app_image_strings;
+
+ MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
+ intern_table.VisitInterns([&](const GcRoot<mirror::String>& root)
+ REQUIRES_SHARED(Locks::mutator_lock_) {
+ boot_image_strings.insert(root.Read()->ToModifiedUtf8());
+ }, /*visit_boot_images=*/true, /*visit_non_boot_images=*/false);
+ intern_table.VisitInterns([&](const GcRoot<mirror::String>& root)
+ REQUIRES_SHARED(Locks::mutator_lock_) {
+ app_image_strings.insert(root.Read()->ToModifiedUtf8());
+ }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true);
+ EXPECT_EQ(boot_image_strings.size(), 0u);
+ EXPECT_TRUE(app_image_strings == seen);
}
}
diff --git a/dex2oat/linker/image_test.cc b/dex2oat/linker/image_test.cc
index b628c9e..69dac19 100644
--- a/dex2oat/linker/image_test.cc
+++ b/dex2oat/linker/image_test.cc
@@ -99,9 +99,9 @@
TEST_F(ImageTest, TestDefaultMethods) {
CompilationHelper helper;
Compile(ImageHeader::kStorageModeUncompressed,
- helper,
- "DefaultMethods",
- {"LIface;", "LImpl;", "LIterableBase;"});
+ helper,
+ "DefaultMethods",
+ {"LIface;", "LImpl;", "LIterableBase;"});
PointerSize pointer_size = class_linker_->GetImagePointerSize();
Thread* self = Thread::Current();
@@ -152,5 +152,17 @@
ASSERT_TRUE(class_linker_->IsQuickToInterpreterBridge(code));
}
+// Regression test for dex2oat crash for soft verification failure during
+// class initialization check from the transactional interpreter while
+// running the class initializer for another class.
+TEST_F(ImageTest, TestSoftVerificationFailureDuringClassInitialization) {
+ CompilationHelper helper;
+ Compile(ImageHeader::kStorageModeUncompressed,
+ helper,
+ "VerifySoftFailDuringClinit",
+ /*image_classes=*/ {"LClassToInitialize;"},
+ /*image_classes_failing_aot_clinit=*/ {"LClassToInitialize;"});
+}
+
} // namespace linker
} // namespace art
diff --git a/dex2oat/linker/image_test.h b/dex2oat/linker/image_test.h
index 443ee52..9d1a4e7 100644
--- a/dex2oat/linker/image_test.h
+++ b/dex2oat/linker/image_test.h
@@ -28,6 +28,7 @@
#include "art_method-inl.h"
#include "base/file_utils.h"
#include "base/hash_set.h"
+#include "base/stl_util.h"
#include "base/unix_file/fd_file.h"
#include "base/utils.h"
#include "class_linker-inl.h"
@@ -81,7 +82,8 @@
void Compile(ImageHeader::StorageMode storage_mode,
/*out*/ CompilationHelper& out_helper,
const std::string& extra_dex = "",
- const std::initializer_list<std::string>& image_classes = {});
+ const std::initializer_list<std::string>& image_classes = {},
+ const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {});
void SetUpRuntimeOptions(RuntimeOptions* options) override {
CommonCompilerTest::SetUpRuntimeOptions(options);
@@ -370,10 +372,15 @@
}
}
-inline void ImageTest::Compile(ImageHeader::StorageMode storage_mode,
- CompilationHelper& helper,
- const std::string& extra_dex,
- const std::initializer_list<std::string>& image_classes) {
+inline void ImageTest::Compile(
+ ImageHeader::StorageMode storage_mode,
+ CompilationHelper& helper,
+ const std::string& extra_dex,
+ const std::initializer_list<std::string>& image_classes,
+ const std::initializer_list<std::string>& image_classes_failing_aot_clinit) {
+ for (const std::string& image_class : image_classes_failing_aot_clinit) {
+ ASSERT_TRUE(ContainsElement(image_classes, image_class));
+ }
for (const std::string& image_class : image_classes) {
image_classes_.insert(image_class);
}
@@ -394,7 +401,12 @@
ObjPtr<mirror::Class> klass =
class_linker->FindSystemClass(Thread::Current(), image_class.c_str());
EXPECT_TRUE(klass != nullptr);
- EXPECT_TRUE(klass->IsInitialized());
+ EXPECT_TRUE(klass->IsResolved());
+ if (ContainsElement(image_classes_failing_aot_clinit, image_class)) {
+ EXPECT_FALSE(klass->IsInitialized());
+ } else {
+ EXPECT_TRUE(klass->IsInitialized());
+ }
}
}
}
diff --git a/dex2oat/linker/image_writer.cc b/dex2oat/linker/image_writer.cc
index fd10b6b..5ca7f07 100644
--- a/dex2oat/linker/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -2614,17 +2614,19 @@
CHECK_EQ(intern_table_bytes, image_info.intern_table_bytes_);
// Fixup the pointers in the newly written intern table to contain image addresses.
InternTable temp_intern_table;
- // Note that we require that ReadFromMemory does not make an internal copy of the elements so that
- // the VisitRoots() will update the memory directly rather than the copies.
+ // Note that we require that ReadFromMemory does not make an internal copy of the elements so
+ // that the VisitRoots() will update the memory directly rather than the copies.
// This also relies on visit roots not doing any verification which could fail after we update
// the roots to be the image addresses.
- temp_intern_table.AddTableFromMemory(intern_table_memory_ptr, VoidFunctor());
+ temp_intern_table.AddTableFromMemory(intern_table_memory_ptr,
+ VoidFunctor(),
+ /*is_boot_image=*/ false);
CHECK_EQ(temp_intern_table.Size(), intern_table->Size());
temp_intern_table.VisitRoots(&root_visitor, kVisitRootFlagAllRoots);
// Record relocations. (The root visitor does not get to see the slot addresses.)
MutexLock lock(Thread::Current(), *Locks::intern_table_lock_);
DCHECK(!temp_intern_table.strong_interns_.tables_.empty());
- DCHECK(!temp_intern_table.strong_interns_.tables_[0].empty()); // Inserted at the beginning.
+ DCHECK(!temp_intern_table.strong_interns_.tables_[0].Empty()); // Inserted at the beginning.
}
// Write the class table(s) into the image. class_table_bytes_ may be 0 if there are multiple
// class loaders. Writing multiple class tables into the image is currently unsupported.
diff --git a/libartbase/arch/instruction_set.cc b/libartbase/arch/instruction_set.cc
index a187663..d47f936 100644
--- a/libartbase/arch/instruction_set.cc
+++ b/libartbase/arch/instruction_set.cc
@@ -105,18 +105,7 @@
UNREACHABLE();
}
-#if !defined(ART_STACK_OVERFLOW_GAP_arm) || !defined(ART_STACK_OVERFLOW_GAP_arm64) || \
- !defined(ART_STACK_OVERFLOW_GAP_mips) || !defined(ART_STACK_OVERFLOW_GAP_mips64) || \
- !defined(ART_STACK_OVERFLOW_GAP_x86) || !defined(ART_STACK_OVERFLOW_GAP_x86_64)
-#error "Missing defines for stack overflow gap"
-#endif
-
-static constexpr size_t kArmStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm;
-static constexpr size_t kArm64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm64;
-static constexpr size_t kMipsStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips;
-static constexpr size_t kMips64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips64;
-static constexpr size_t kX86StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86;
-static constexpr size_t kX86_64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86_64;
+namespace instruction_set_details {
static_assert(IsAligned<kPageSize>(kArmStackOverflowReservedBytes), "ARM gap not page aligned");
static_assert(IsAligned<kPageSize>(kArm64StackOverflowReservedBytes), "ARM64 gap not page aligned");
@@ -144,32 +133,10 @@
static_assert(ART_FRAME_SIZE_LIMIT < kX86_64StackOverflowReservedBytes,
"Frame size limit too large");
-size_t GetStackOverflowReservedBytes(InstructionSet isa) {
- switch (isa) {
- case InstructionSet::kArm: // Intentional fall-through.
- case InstructionSet::kThumb2:
- return kArmStackOverflowReservedBytes;
+} // namespace instruction_set_details
- case InstructionSet::kArm64:
- return kArm64StackOverflowReservedBytes;
-
- case InstructionSet::kMips:
- return kMipsStackOverflowReservedBytes;
-
- case InstructionSet::kMips64:
- return kMips64StackOverflowReservedBytes;
-
- case InstructionSet::kX86:
- return kX86StackOverflowReservedBytes;
-
- case InstructionSet::kX86_64:
- return kX86_64StackOverflowReservedBytes;
-
- case InstructionSet::kNone:
- LOG(FATAL) << "kNone has no stack overflow size";
- UNREACHABLE();
- }
- LOG(FATAL) << "Unknown instruction set" << isa;
+NO_RETURN void GetStackOverflowReservedBytesFailure(const char* error_msg) {
+ LOG(FATAL) << error_msg;
UNREACHABLE();
}
diff --git a/libartbase/arch/instruction_set.h b/libartbase/arch/instruction_set.h
index 06bd53a..7e071bd 100644
--- a/libartbase/arch/instruction_set.h
+++ b/libartbase/arch/instruction_set.h
@@ -226,7 +226,53 @@
InstructionSetAbort(isa);
}
-size_t GetStackOverflowReservedBytes(InstructionSet isa);
+namespace instruction_set_details {
+
+#if !defined(ART_STACK_OVERFLOW_GAP_arm) || !defined(ART_STACK_OVERFLOW_GAP_arm64) || \
+ !defined(ART_STACK_OVERFLOW_GAP_mips) || !defined(ART_STACK_OVERFLOW_GAP_mips64) || \
+ !defined(ART_STACK_OVERFLOW_GAP_x86) || !defined(ART_STACK_OVERFLOW_GAP_x86_64)
+#error "Missing defines for stack overflow gap"
+#endif
+
+static constexpr size_t kArmStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm;
+static constexpr size_t kArm64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm64;
+static constexpr size_t kMipsStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips;
+static constexpr size_t kMips64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips64;
+static constexpr size_t kX86StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86;
+static constexpr size_t kX86_64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86_64;
+
+NO_RETURN void GetStackOverflowReservedBytesFailure(const char* error_msg);
+
+} // namespace instruction_set_details
+
+ALWAYS_INLINE
+constexpr size_t GetStackOverflowReservedBytes(InstructionSet isa) {
+ switch (isa) {
+ case InstructionSet::kArm: // Intentional fall-through.
+ case InstructionSet::kThumb2:
+ return instruction_set_details::kArmStackOverflowReservedBytes;
+
+ case InstructionSet::kArm64:
+ return instruction_set_details::kArm64StackOverflowReservedBytes;
+
+ case InstructionSet::kMips:
+ return instruction_set_details::kMipsStackOverflowReservedBytes;
+
+ case InstructionSet::kMips64:
+ return instruction_set_details::kMips64StackOverflowReservedBytes;
+
+ case InstructionSet::kX86:
+ return instruction_set_details::kX86StackOverflowReservedBytes;
+
+ case InstructionSet::kX86_64:
+ return instruction_set_details::kX86_64StackOverflowReservedBytes;
+
+ case InstructionSet::kNone:
+ instruction_set_details::GetStackOverflowReservedBytesFailure(
+ "kNone has no stack overflow size");
+ }
+ instruction_set_details::GetStackOverflowReservedBytesFailure("Unknown instruction set");
+}
// The following definitions create return types for two word-sized entities that will be passed
// in registers so that memory operations for the interface trampolines can be avoided. The entities
diff --git a/libdexfile/dex/dex_file-inl.h b/libdexfile/dex/dex_file-inl.h
index eae7efc..c884eee 100644
--- a/libdexfile/dex/dex_file-inl.h
+++ b/libdexfile/dex/dex_file-inl.h
@@ -110,6 +110,14 @@
return StringDataByIdx(method_id.name_idx_);
}
+inline const char* DexFile::GetMethodName(const MethodId& method_id, uint32_t* utf_length) const {
+ return StringDataAndUtf16LengthByIdx(method_id.name_idx_, utf_length);
+}
+
+inline const char* DexFile::GetMethodName(uint32_t idx, uint32_t* utf_length) const {
+ return StringDataAndUtf16LengthByIdx(GetMethodId(idx).name_idx_, utf_length);
+}
+
inline const char* DexFile::GetMethodShorty(uint32_t idx) const {
return StringDataByIdx(GetProtoId(GetMethodId(idx).proto_idx_).shorty_idx_);
}
diff --git a/libdexfile/dex/dex_file.h b/libdexfile/dex/dex_file.h
index 6a52f67..b3e7ad4 100644
--- a/libdexfile/dex/dex_file.h
+++ b/libdexfile/dex/dex_file.h
@@ -641,6 +641,8 @@
// Returns the name of a method id.
const char* GetMethodName(const MethodId& method_id) const;
+ const char* GetMethodName(const MethodId& method_id, uint32_t* utf_length) const;
+ const char* GetMethodName(uint32_t idx, uint32_t* utf_length) const;
// Returns the shorty of a method by its index.
const char* GetMethodShorty(uint32_t idx) const;
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index e3dfdb3..9ba52c4 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -1495,18 +1495,44 @@
intern_table->AddImageStringsToTable(space, [&](InternTable::UnorderedSet& interns)
REQUIRES_SHARED(Locks::mutator_lock_)
REQUIRES(Locks::intern_table_lock_) {
+ const size_t non_boot_image_strings = intern_table->CountInterns(
+ /*visit_boot_images=*/false,
+ /*visit_non_boot_images=*/true);
VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size();
- for (auto it = interns.begin(); it != interns.end(); ) {
- ObjPtr<mirror::String> string = it->Read();
- ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string);
- if (existing == nullptr) {
- existing = intern_table->LookupStrongLocked(string);
+ VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings;
+ // Visit the smaller of the two sets to compute the intersection.
+ if (interns.size() < non_boot_image_strings) {
+ for (auto it = interns.begin(); it != interns.end(); ) {
+ ObjPtr<mirror::String> string = it->Read();
+ ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string);
+ if (existing == nullptr) {
+ existing = intern_table->LookupStrongLocked(string);
+ }
+ if (existing != nullptr) {
+ intern_remap.Put(string.Ptr(), existing.Ptr());
+ it = interns.erase(it);
+ } else {
+ ++it;
+ }
}
- if (existing != nullptr) {
- intern_remap.Put(string.Ptr(), existing.Ptr());
- it = interns.erase(it);
- } else {
- ++it;
+ } else {
+ intern_table->VisitInterns([&](const GcRoot<mirror::String>& root)
+ REQUIRES_SHARED(Locks::mutator_lock_)
+ REQUIRES(Locks::intern_table_lock_) {
+ auto it = interns.find(root);
+ if (it != interns.end()) {
+ ObjPtr<mirror::String> existing = root.Read();
+ intern_remap.Put(it->Read(), existing.Ptr());
+ it = interns.erase(it);
+ }
+ }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true);
+ }
+ // Sanity check to ensure correctness.
+ if (kIsDebugBuild) {
+ for (GcRoot<mirror::String>& root : interns) {
+ ObjPtr<mirror::String> string = root.Read();
+ CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8();
+ CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8();
}
}
});
@@ -4903,7 +4929,10 @@
} else {
CHECK(Runtime::Current()->IsAotCompiler());
CHECK_EQ(klass->GetStatus(), ClassStatus::kRetryVerificationAtRuntime);
+ self->AssertNoPendingException();
+ self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError());
}
+ self->AssertPendingException();
return false;
} else {
self->AssertNoPendingException();
diff --git a/runtime/class_loader_context.cc b/runtime/class_loader_context.cc
index dd10f3c..de9fe22 100644
--- a/runtime/class_loader_context.cc
+++ b/runtime/class_loader_context.cc
@@ -42,6 +42,9 @@
static constexpr char kDelegateLastClassLoaderString[] = "DLC";
static constexpr char kClassLoaderOpeningMark = '[';
static constexpr char kClassLoaderClosingMark = ']';
+static constexpr char kClassLoaderSharedLibraryOpeningMark = '{';
+static constexpr char kClassLoaderSharedLibraryClosingMark = '}';
+static constexpr char kClassLoaderSharedLibrarySeparator = '#';
static constexpr char kClassLoaderSeparator = ';';
static constexpr char kClasspathSeparator = ':';
static constexpr char kDexFileChecksumSeparator = '*';
@@ -58,17 +61,35 @@
dex_files_open_result_(true),
owns_the_dex_files_(owns_the_dex_files) {}
+// Utility method to add parent and shared libraries of `info` into
+// the `work_list`.
+static void AddToWorkList(
+ ClassLoaderContext::ClassLoaderInfo* info,
+ std::vector<ClassLoaderContext::ClassLoaderInfo*>& work_list) {
+ if (info->parent != nullptr) {
+ work_list.push_back(info->parent.get());
+ }
+ for (size_t i = 0; i < info->shared_libraries.size(); ++i) {
+ work_list.push_back(info->shared_libraries[i].get());
+ }
+}
+
ClassLoaderContext::~ClassLoaderContext() {
- if (!owns_the_dex_files_) {
+ if (!owns_the_dex_files_ && class_loader_chain_ != nullptr) {
// If the context does not own the dex/oat files release the unique pointers to
// make sure we do not de-allocate them.
- for (ClassLoaderInfo& info : class_loader_chain_) {
- for (std::unique_ptr<OatFile>& oat_file : info.opened_oat_files) {
+ std::vector<ClassLoaderInfo*> work_list;
+ work_list.push_back(class_loader_chain_.get());
+ while (!work_list.empty()) {
+ ClassLoaderInfo* info = work_list.back();
+ work_list.pop_back();
+ for (std::unique_ptr<OatFile>& oat_file : info->opened_oat_files) {
oat_file.release(); // NOLINT b/117926937
}
- for (std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) {
+ for (std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
dex_file.release(); // NOLINT b/117926937
}
+ AddToWorkList(info, work_list);
}
}
}
@@ -86,11 +107,16 @@
}
}
-// The expected format is: "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]".
+// The expected format is:
+// "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]{ClassLoaderType2[...]}".
// The checksum part of the format is expected only if parse_cheksums is true.
-bool ClassLoaderContext::ParseClassLoaderSpec(const std::string& class_loader_spec,
- ClassLoaderType class_loader_type,
- bool parse_checksums) {
+std::unique_ptr<ClassLoaderContext::ClassLoaderInfo> ClassLoaderContext::ParseClassLoaderSpec(
+ const std::string& class_loader_spec,
+ bool parse_checksums) {
+ ClassLoaderType class_loader_type = ExtractClassLoaderType(class_loader_spec);
+ if (class_loader_type == kInvalidClassLoader) {
+ return nullptr;
+ }
const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type);
size_t type_str_size = strlen(class_loader_type_str);
@@ -98,21 +124,24 @@
// Check the opening and closing markers.
if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) {
- return false;
+ return nullptr;
}
- if (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) {
- return false;
+ if ((class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) &&
+ (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderSharedLibraryClosingMark)) {
+ return nullptr;
}
+ size_t closing_index = class_loader_spec.find_first_of(kClassLoaderClosingMark);
+
// At this point we know the format is ok; continue and extract the classpath.
// Note that class loaders with an empty class path are allowed.
std::string classpath = class_loader_spec.substr(type_str_size + 1,
- class_loader_spec.length() - type_str_size - 2);
+ closing_index - type_str_size - 1);
- class_loader_chain_.push_back(ClassLoaderInfo(class_loader_type));
+ std::unique_ptr<ClassLoaderInfo> info(new ClassLoaderInfo(class_loader_type));
if (!parse_checksums) {
- Split(classpath, kClasspathSeparator, &class_loader_chain_.back().classpath);
+ Split(classpath, kClasspathSeparator, &info->classpath);
} else {
std::vector<std::string> classpath_elements;
Split(classpath, kClasspathSeparator, &classpath_elements);
@@ -120,18 +149,37 @@
std::vector<std::string> dex_file_with_checksum;
Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum);
if (dex_file_with_checksum.size() != 2) {
- return false;
+ return nullptr;
}
uint32_t checksum = 0;
if (!android::base::ParseUint(dex_file_with_checksum[1].c_str(), &checksum)) {
- return false;
+ return nullptr;
}
- class_loader_chain_.back().classpath.push_back(dex_file_with_checksum[0]);
- class_loader_chain_.back().checksums.push_back(checksum);
+ info->classpath.push_back(dex_file_with_checksum[0]);
+ info->checksums.push_back(checksum);
}
}
- return true;
+ if (class_loader_spec[class_loader_spec.length() - 1] == kClassLoaderSharedLibraryClosingMark) {
+ size_t start_index = class_loader_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark);
+ if (start_index == std::string::npos) {
+ return nullptr;
+ }
+ std::string shared_libraries_spec =
+ class_loader_spec.substr(start_index + 1, class_loader_spec.length() - start_index - 2);
+ std::vector<std::string> shared_libraries;
+ Split(shared_libraries_spec, kClassLoaderSharedLibrarySeparator, &shared_libraries);
+ for (const std::string& shared_library_spec : shared_libraries) {
+ std::unique_ptr<ClassLoaderInfo> shared_library(
+ ParseInternal(shared_library_spec, parse_checksums));
+ if (shared_library == nullptr) {
+ return nullptr;
+ }
+ info->shared_libraries.push_back(std::move(shared_library));
+ }
+ }
+
+ return info;
}
// Extracts the class loader type from the given spec.
@@ -157,7 +205,7 @@
// By default we load the dex files in a PathClassLoader.
// So an empty spec is equivalent to an empty PathClassLoader (this happens when running
// tests)
- class_loader_chain_.push_back(ClassLoaderInfo(kPathClassLoader));
+ class_loader_chain_.reset(new ClassLoaderInfo(kPathClassLoader));
return true;
}
@@ -169,21 +217,102 @@
return true;
}
- std::vector<std::string> class_loaders;
- Split(spec, kClassLoaderSeparator, &class_loaders);
+ CHECK(class_loader_chain_ == nullptr);
+ class_loader_chain_.reset(ParseInternal(spec, parse_checksums));
+ return class_loader_chain_ != nullptr;
+}
- for (const std::string& class_loader : class_loaders) {
- ClassLoaderType type = ExtractClassLoaderType(class_loader);
- if (type == kInvalidClassLoader) {
- LOG(ERROR) << "Invalid class loader type: " << class_loader;
- return false;
+ClassLoaderContext::ClassLoaderInfo* ClassLoaderContext::ParseInternal(
+ const std::string& spec, bool parse_checksums) {
+ CHECK(!spec.empty());
+ CHECK_NE(spec, OatFile::kSpecialSharedLibrary);
+ std::string remaining = spec;
+ std::unique_ptr<ClassLoaderInfo> first(nullptr);
+ ClassLoaderInfo* previous_iteration = nullptr;
+ while (!remaining.empty()) {
+ std::string class_loader_spec;
+ size_t first_class_loader_separator = remaining.find_first_of(kClassLoaderSeparator);
+ size_t first_shared_library_open =
+ remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark);
+ if (first_class_loader_separator == std::string::npos) {
+ // Only one class loader, for example:
+ // PCL[...]
+ class_loader_spec = remaining;
+ remaining = "";
+ } else if ((first_shared_library_open == std::string::npos) ||
+ (first_shared_library_open > first_class_loader_separator)) {
+ // We found a class loader spec without shared libraries, for example:
+ // PCL[...];PCL[...]{...}
+ class_loader_spec = remaining.substr(0, first_class_loader_separator);
+ remaining = remaining.substr(first_class_loader_separator + 1,
+ remaining.size() - first_class_loader_separator - 1);
+ } else {
+ // The class loader spec contains shared libraries. Find the matching closing
+ // shared library marker for it.
+
+ // Counter of opened shared library marker we've encountered so far.
+ uint32_t counter = 1;
+ // The index at which we're operating in the loop.
+ uint32_t string_index = first_shared_library_open + 1;
+ while (counter != 0) {
+ size_t shared_library_close =
+ remaining.find_first_of(kClassLoaderSharedLibraryClosingMark, string_index);
+ size_t shared_library_open =
+ remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark, string_index);
+ if (shared_library_close == std::string::npos) {
+ // No matching closing market. Return an error.
+ LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
+ return nullptr;
+ }
+
+ if ((shared_library_open == std::string::npos) ||
+ (shared_library_close < shared_library_open)) {
+ // We have seen a closing marker. Decrement the counter.
+ --counter;
+ if (counter == 0) {
+ // Found the matching closing marker.
+ class_loader_spec = remaining.substr(0, shared_library_close + 1);
+
+ // Compute the remaining string to analyze.
+ if (remaining.size() == shared_library_close + 1) {
+ remaining = "";
+ } else if ((remaining.size() == shared_library_close + 2) ||
+ (remaining.at(shared_library_close + 1) != kClassLoaderSeparator)) {
+ LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
+ return nullptr;
+ } else {
+ remaining = remaining.substr(shared_library_close + 2,
+ remaining.size() - shared_library_close - 2);
+ }
+ } else {
+ // Move the search index forward.
+ string_index = shared_library_close + 1;
+ }
+ } else {
+ // New nested opening marker. Increment the counter and move the search
+ // index after the marker.
+ ++counter;
+ string_index = shared_library_open + 1;
+ }
+ }
}
- if (!ParseClassLoaderSpec(class_loader, type, parse_checksums)) {
- LOG(ERROR) << "Invalid class loader spec: " << class_loader;
- return false;
+
+ std::unique_ptr<ClassLoaderInfo> info =
+ ParseClassLoaderSpec(class_loader_spec, parse_checksums);
+ if (info == nullptr) {
+ LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
+ return nullptr;
+ }
+ if (first == nullptr) {
+ first.reset(info.release());
+ previous_iteration = first.get();
+ } else {
+ CHECK(previous_iteration != nullptr);
+ previous_iteration->parent.reset(info.release());
+ previous_iteration = previous_iteration->parent.get();
}
}
- return true;
+ return first.release();
}
// Opens requested class path files and appends them to opened_dex_files. If the dex files have
@@ -208,9 +337,13 @@
// TODO(calin): Refine the dex opening interface to be able to tell if an archive contains
// no dex files. So that we can distinguish the real failures...
const ArtDexFileLoader dex_file_loader;
- for (ClassLoaderInfo& info : class_loader_chain_) {
- size_t opened_dex_files_index = info.opened_dex_files.size();
- for (const std::string& cp_elem : info.classpath) {
+ std::vector<ClassLoaderInfo*> work_list;
+ work_list.push_back(class_loader_chain_.get());
+ while (!work_list.empty()) {
+ ClassLoaderInfo* info = work_list.back();
+ work_list.pop_back();
+ size_t opened_dex_files_index = info->opened_dex_files.size();
+ for (const std::string& cp_elem : info->classpath) {
// If path is relative, append it to the provided base directory.
std::string location = cp_elem;
if (location[0] != '/' && !classpath_dir.empty()) {
@@ -225,7 +358,7 @@
Runtime::Current()->IsVerificationEnabled(),
/*verify_checksum=*/ true,
&error_msg,
- &info.opened_dex_files)) {
+ &info->opened_dex_files)) {
// If we fail to open the dex file because it's been stripped, try to open the dex file
// from its corresponding oat file.
// This could happen when we need to recompile a pre-build whose dex code has been stripped.
@@ -237,10 +370,10 @@
std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
if (oat_file != nullptr &&
OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
- info.opened_oat_files.push_back(std::move(oat_file));
- info.opened_dex_files.insert(info.opened_dex_files.end(),
- std::make_move_iterator(oat_dex_files.begin()),
- std::make_move_iterator(oat_dex_files.end()));
+ info->opened_oat_files.push_back(std::move(oat_file));
+ info->opened_dex_files.insert(info->opened_dex_files.end(),
+ std::make_move_iterator(oat_dex_files.begin()),
+ std::make_move_iterator(oat_dex_files.end()));
} else {
LOG(WARNING) << "Could not open dex files from location: " << location;
dex_files_open_result_ = false;
@@ -257,14 +390,15 @@
// This will allow the context to VerifyClassLoaderContextMatch which expects or multidex
// location in the class paths.
// Note that this will also remove the paths that could not be opened.
- info.original_classpath = std::move(info.classpath);
- info.classpath.clear();
- info.checksums.clear();
- for (size_t k = opened_dex_files_index; k < info.opened_dex_files.size(); k++) {
- std::unique_ptr<const DexFile>& dex = info.opened_dex_files[k];
- info.classpath.push_back(dex->GetLocation());
- info.checksums.push_back(dex->GetLocationChecksum());
+ info->original_classpath = std::move(info->classpath);
+ info->classpath.clear();
+ info->checksums.clear();
+ for (size_t k = opened_dex_files_index; k < info->opened_dex_files.size(); k++) {
+ std::unique_ptr<const DexFile>& dex = info->opened_dex_files[k];
+ info->classpath.push_back(dex->GetLocation());
+ info->checksums.push_back(dex->GetLocationChecksum());
}
+ AddToWorkList(info, work_list);
}
return dex_files_open_result_;
@@ -275,24 +409,33 @@
CHECK(!dex_files_open_attempted_)
<< "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles";
+ if (class_loader_chain_ == nullptr) {
+ return false;
+ }
+
std::set<std::string> canonical_locations;
for (const std::string& location : locations) {
canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str()));
}
bool removed_locations = false;
- for (ClassLoaderInfo& info : class_loader_chain_) {
- size_t initial_size = info.classpath.size();
+ std::vector<ClassLoaderInfo*> work_list;
+ work_list.push_back(class_loader_chain_.get());
+ while (!work_list.empty()) {
+ ClassLoaderInfo* info = work_list.back();
+ work_list.pop_back();
+ size_t initial_size = info->classpath.size();
auto kept_it = std::remove_if(
- info.classpath.begin(),
- info.classpath.end(),
+ info->classpath.begin(),
+ info->classpath.end(),
[canonical_locations](const std::string& location) {
return ContainsElement(canonical_locations,
DexFileLoader::GetDexCanonicalLocation(location.c_str()));
});
- info.classpath.erase(kept_it, info.classpath.end());
- if (initial_size != info.classpath.size()) {
+ info->classpath.erase(kept_it, info->classpath.end());
+ if (initial_size != info->classpath.size()) {
removed_locations = true;
}
+ AddToWorkList(info, work_list);
}
return removed_locations;
}
@@ -315,11 +458,11 @@
}
if (stored_context != nullptr) {
- DCHECK_EQ(class_loader_chain_.size(), stored_context->class_loader_chain_.size());
+ DCHECK_EQ(GetParentChainSize(), stored_context->GetParentChainSize());
}
std::ostringstream out;
- if (class_loader_chain_.empty()) {
+ if (class_loader_chain_ == nullptr) {
// We can get in this situation if the context was created with a class path containing the
// source dex files which were later removed (happens during run-tests).
out << GetClassLoaderTypeName(kPathClassLoader)
@@ -328,62 +471,122 @@
return out.str();
}
- for (size_t i = 0; i < class_loader_chain_.size(); i++) {
- const ClassLoaderInfo& info = class_loader_chain_[i];
- if (i > 0) {
- out << kClassLoaderSeparator;
- }
- out << GetClassLoaderTypeName(info.type);
- out << kClassLoaderOpeningMark;
- std::set<std::string> seen_locations;
- SafeMap<std::string, std::string> remap;
- if (stored_context != nullptr) {
- DCHECK_EQ(info.original_classpath.size(),
- stored_context->class_loader_chain_[i].classpath.size());
- for (size_t k = 0; k < info.original_classpath.size(); ++k) {
- // Note that we don't care if the same name appears twice.
- remap.Put(info.original_classpath[k], stored_context->class_loader_chain_[i].classpath[k]);
- }
- }
- for (size_t k = 0; k < info.opened_dex_files.size(); k++) {
- const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k];
- if (for_dex2oat) {
- // dex2oat only needs the base location. It cannot accept multidex locations.
- // So ensure we only add each file once.
- bool new_insert = seen_locations.insert(
- DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second;
- if (!new_insert) {
- continue;
- }
- }
- std::string location = dex_file->GetLocation();
- // If there is a stored class loader remap, fix up the multidex strings.
- if (!remap.empty()) {
- std::string base_dex_location = DexFileLoader::GetBaseLocation(location);
- auto it = remap.find(base_dex_location);
- CHECK(it != remap.end()) << base_dex_location;
- location = it->second + DexFileLoader::GetMultiDexSuffix(location);
- }
- if (k > 0) {
- out << kClasspathSeparator;
- }
- // Find paths that were relative and convert them back from absolute.
- if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) {
- out << location.substr(base_dir.length() + 1).c_str();
- } else {
- out << location.c_str();
- }
- // dex2oat does not need the checksums.
- if (!for_dex2oat) {
- out << kDexFileChecksumSeparator;
- out << dex_file->GetLocationChecksum();
- }
- }
- out << kClassLoaderClosingMark;
- }
+ EncodeContextInternal(
+ *class_loader_chain_,
+ base_dir,
+ for_dex2oat,
+ (stored_context == nullptr ? nullptr : stored_context->class_loader_chain_.get()),
+ out);
return out.str();
}
+void ClassLoaderContext::EncodeContextInternal(const ClassLoaderInfo& info,
+ const std::string& base_dir,
+ bool for_dex2oat,
+ ClassLoaderInfo* stored_info,
+ std::ostringstream& out) const {
+ out << GetClassLoaderTypeName(info.type);
+ out << kClassLoaderOpeningMark;
+ std::set<std::string> seen_locations;
+ SafeMap<std::string, std::string> remap;
+ if (stored_info != nullptr) {
+ for (size_t k = 0; k < info.original_classpath.size(); ++k) {
+ // Note that we don't care if the same name appears twice.
+ remap.Put(info.original_classpath[k], stored_info->classpath[k]);
+ }
+ }
+ for (size_t k = 0; k < info.opened_dex_files.size(); k++) {
+ const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k];
+ if (for_dex2oat) {
+ // dex2oat only needs the base location. It cannot accept multidex locations.
+ // So ensure we only add each file once.
+ bool new_insert = seen_locations.insert(
+ DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second;
+ if (!new_insert) {
+ continue;
+ }
+ }
+ std::string location = dex_file->GetLocation();
+ // If there is a stored class loader remap, fix up the multidex strings.
+ if (!remap.empty()) {
+ std::string base_dex_location = DexFileLoader::GetBaseLocation(location);
+ auto it = remap.find(base_dex_location);
+ CHECK(it != remap.end()) << base_dex_location;
+ location = it->second + DexFileLoader::GetMultiDexSuffix(location);
+ }
+ if (k > 0) {
+ out << kClasspathSeparator;
+ }
+ // Find paths that were relative and convert them back from absolute.
+ if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) {
+ out << location.substr(base_dir.length() + 1).c_str();
+ } else {
+ out << location.c_str();
+ }
+ // dex2oat does not need the checksums.
+ if (!for_dex2oat) {
+ out << kDexFileChecksumSeparator;
+ out << dex_file->GetLocationChecksum();
+ }
+ }
+ out << kClassLoaderClosingMark;
+
+ if (!info.shared_libraries.empty()) {
+ out << kClassLoaderSharedLibraryOpeningMark;
+ for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
+ if (i > 0) {
+ out << kClassLoaderSharedLibrarySeparator;
+ }
+ EncodeContextInternal(
+ *info.shared_libraries[i].get(),
+ base_dir,
+ for_dex2oat,
+ (stored_info == nullptr ? nullptr : stored_info->shared_libraries[i].get()),
+ out);
+ }
+ out << kClassLoaderSharedLibraryClosingMark;
+ }
+ if (info.parent != nullptr) {
+ out << kClassLoaderSeparator;
+ EncodeContextInternal(
+ *info.parent.get(),
+ base_dir,
+ for_dex2oat,
+ (stored_info == nullptr ? nullptr : stored_info->parent.get()),
+ out);
+ }
+}
+
+// Returns the WellKnownClass for the given class loader type.
+static jclass GetClassLoaderClass(ClassLoaderContext::ClassLoaderType type) {
+ switch (type) {
+ case ClassLoaderContext::kPathClassLoader:
+ return WellKnownClasses::dalvik_system_PathClassLoader;
+ case ClassLoaderContext::kDelegateLastClassLoader:
+ return WellKnownClasses::dalvik_system_DelegateLastClassLoader;
+ case ClassLoaderContext::kInvalidClassLoader: break; // will fail after the switch.
+ }
+ LOG(FATAL) << "Invalid class loader type " << type;
+ UNREACHABLE();
+}
+
+static jobject CreateClassLoaderInternal(Thread* self,
+ const ClassLoaderContext::ClassLoaderInfo& info)
+ REQUIRES_SHARED(Locks::mutator_lock_) {
+ CHECK(info.shared_libraries.empty()) << "Class loader shared library not implemented yet";
+ jobject parent = nullptr;
+ if (info.parent != nullptr) {
+ parent = CreateClassLoaderInternal(self, *info.parent.get());
+ }
+ std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(
+ info.opened_dex_files);
+ return Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
+ self,
+ class_path_files,
+ GetClassLoaderClass(info.type),
+ parent);
+}
+
jobject ClassLoaderContext::CreateClassLoader(
const std::vector<const DexFile*>& compilation_sources) const {
CheckDexFilesOpened("CreateClassLoader");
@@ -393,22 +596,14 @@
ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
- if (class_loader_chain_.empty()) {
+ if (class_loader_chain_ == nullptr) {
return class_linker->CreatePathClassLoader(self, compilation_sources);
}
- // Create the class loaders starting from the top most parent (the one on the last position
- // in the chain) but omit the first class loader which will contain the compilation_sources and
- // needs special handling.
- jobject current_parent = nullptr; // the starting parent is the BootClassLoader.
- for (size_t i = class_loader_chain_.size() - 1; i > 0; i--) {
- std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(
- class_loader_chain_[i].opened_dex_files);
- current_parent = class_linker->CreateWellKnownClassLoader(
- self,
- class_path_files,
- GetClassLoaderClass(class_loader_chain_[i].type),
- current_parent);
+ // Create the class loader of the parent.
+ jobject parent = nullptr;
+ if (class_loader_chain_->parent != nullptr) {
+ parent = CreateClassLoaderInternal(self, *class_loader_chain_->parent.get());
}
// We set up all the parents. Move on to create the first class loader.
@@ -416,26 +611,34 @@
// we need to resolve classes from it the classpath elements come first.
std::vector<const DexFile*> first_class_loader_classpath = MakeNonOwningPointerVector(
- class_loader_chain_[0].opened_dex_files);
+ class_loader_chain_->opened_dex_files);
first_class_loader_classpath.insert(first_class_loader_classpath.end(),
- compilation_sources.begin(),
- compilation_sources.end());
+ compilation_sources.begin(),
+ compilation_sources.end());
return class_linker->CreateWellKnownClassLoader(
self,
first_class_loader_classpath,
- GetClassLoaderClass(class_loader_chain_[0].type),
- current_parent);
+ GetClassLoaderClass(class_loader_chain_->type),
+ parent);
}
std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const {
CheckDexFilesOpened("FlattenOpenedDexFiles");
std::vector<const DexFile*> result;
- for (const ClassLoaderInfo& info : class_loader_chain_) {
- for (const std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) {
+ if (class_loader_chain_ == nullptr) {
+ return result;
+ }
+ std::vector<ClassLoaderInfo*> work_list;
+ work_list.push_back(class_loader_chain_.get());
+ while (!work_list.empty()) {
+ ClassLoaderInfo* info = work_list.back();
+ work_list.pop_back();
+ for (const std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
result.push_back(dex_file.get());
}
+ AddToWorkList(info, work_list);
}
return result;
}
@@ -632,12 +835,21 @@
GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded);
}
- class_loader_chain_.push_back(ClassLoaderContext::ClassLoaderInfo(type));
- ClassLoaderInfo& info = class_loader_chain_.back();
+ ClassLoaderInfo* info = new ClassLoaderContext::ClassLoaderInfo(type);
+ if (class_loader_chain_ == nullptr) {
+ class_loader_chain_.reset(info);
+ } else {
+ ClassLoaderInfo* child = class_loader_chain_.get();
+ while (child->parent != nullptr) {
+ child = child->parent.get();
+ }
+ child->parent.reset(info);
+ }
+
for (const DexFile* dex_file : dex_files_loaded) {
- info.classpath.push_back(dex_file->GetLocation());
- info.checksums.push_back(dex_file->GetLocationChecksum());
- info.opened_dex_files.emplace_back(dex_file);
+ info->classpath.push_back(dex_file->GetLocation());
+ info->checksums.push_back(dex_file->GetLocationChecksum());
+ info->opened_dex_files.emplace_back(dex_file);
}
// We created the ClassLoaderInfo for the current loader. Move on to its parent.
@@ -696,7 +908,7 @@
// collision check.
if (expected_context.special_shared_library_) {
// Special case where we are the only entry in the class path.
- if (class_loader_chain_.size() == 1 && class_loader_chain_[0].classpath.size() == 0) {
+ if (class_loader_chain_->parent == nullptr && class_loader_chain_->classpath.size() == 0) {
return VerificationResult::kVerifies;
}
return VerificationResult::kForcedToSkipChecks;
@@ -704,41 +916,43 @@
return VerificationResult::kForcedToSkipChecks;
}
- if (expected_context.class_loader_chain_.size() != class_loader_chain_.size()) {
- LOG(WARNING) << "ClassLoaderContext size mismatch. expected="
- << expected_context.class_loader_chain_.size()
- << ", actual=" << class_loader_chain_.size()
- << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
+ ClassLoaderInfo* info = class_loader_chain_.get();
+ ClassLoaderInfo* expected = expected_context.class_loader_chain_.get();
+ CHECK(info != nullptr);
+ CHECK(expected != nullptr);
+ if (!ClassLoaderInfoMatch(*info, *expected, context_spec, verify_names, verify_checksums)) {
return VerificationResult::kMismatch;
}
+ return VerificationResult::kVerifies;
+}
- for (size_t i = 0; i < class_loader_chain_.size(); i++) {
- const ClassLoaderInfo& info = class_loader_chain_[i];
- const ClassLoaderInfo& expected_info = expected_context.class_loader_chain_[i];
- if (info.type != expected_info.type) {
- LOG(WARNING) << "ClassLoaderContext type mismatch for position " << i
- << ". expected=" << GetClassLoaderTypeName(expected_info.type)
- << ", found=" << GetClassLoaderTypeName(info.type)
+bool ClassLoaderContext::ClassLoaderInfoMatch(
+ const ClassLoaderInfo& info,
+ const ClassLoaderInfo& expected_info,
+ const std::string& context_spec,
+ bool verify_names,
+ bool verify_checksums) const {
+ if (info.type != expected_info.type) {
+ LOG(WARNING) << "ClassLoaderContext type mismatch"
+ << ". expected=" << GetClassLoaderTypeName(expected_info.type)
+ << ", found=" << GetClassLoaderTypeName(info.type)
+ << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
+ return false;
+ }
+ if (info.classpath.size() != expected_info.classpath.size()) {
+ LOG(WARNING) << "ClassLoaderContext classpath size mismatch"
+ << ". expected=" << expected_info.classpath.size()
+ << ", found=" << info.classpath.size()
<< " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
- return VerificationResult::kMismatch;
- }
- if (info.classpath.size() != expected_info.classpath.size()) {
- LOG(WARNING) << "ClassLoaderContext classpath size mismatch for position " << i
- << ". expected=" << expected_info.classpath.size()
- << ", found=" << info.classpath.size()
- << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
- return VerificationResult::kMismatch;
- }
+ return false;
+ }
- if (verify_checksums) {
- DCHECK_EQ(info.classpath.size(), info.checksums.size());
- DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size());
- }
+ if (verify_checksums) {
+ DCHECK_EQ(info.classpath.size(), info.checksums.size());
+ DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size());
+ }
- if (!verify_names) {
- continue;
- }
-
+ if (verify_names) {
for (size_t k = 0; k < info.classpath.size(); k++) {
// Compute the dex location that must be compared.
// We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]`
@@ -778,34 +992,58 @@
// Compare the locations.
if (dex_name != expected_dex_name) {
- LOG(WARNING) << "ClassLoaderContext classpath element mismatch for position " << i
+ LOG(WARNING) << "ClassLoaderContext classpath element mismatch"
<< ". expected=" << expected_info.classpath[k]
<< ", found=" << info.classpath[k]
<< " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
- return VerificationResult::kMismatch;
+ return false;
}
// Compare the checksums.
if (info.checksums[k] != expected_info.checksums[k]) {
- LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch for position " << i
+ LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch"
<< ". expected=" << expected_info.checksums[k]
<< ", found=" << info.checksums[k]
<< " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
- return VerificationResult::kMismatch;
+ return false;
}
}
}
- return VerificationResult::kVerifies;
-}
-jclass ClassLoaderContext::GetClassLoaderClass(ClassLoaderType type) {
- switch (type) {
- case kPathClassLoader: return WellKnownClasses::dalvik_system_PathClassLoader;
- case kDelegateLastClassLoader: return WellKnownClasses::dalvik_system_DelegateLastClassLoader;
- case kInvalidClassLoader: break; // will fail after the switch.
+ if (info.shared_libraries.size() != expected_info.shared_libraries.size()) {
+ LOG(WARNING) << "ClassLoaderContext shared library size mismatch. "
+ << "Expected=" << expected_info.classpath.size()
+ << ", found=" << info.classpath.size()
+ << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
+ return false;
}
- LOG(FATAL) << "Invalid class loader type " << type;
- UNREACHABLE();
+ for (size_t i = 0; i < info.shared_libraries.size(); ++i) {
+ if (!ClassLoaderInfoMatch(*info.shared_libraries[i].get(),
+ *expected_info.shared_libraries[i].get(),
+ context_spec,
+ verify_names,
+ verify_checksums)) {
+ return false;
+ }
+ }
+ if (info.parent.get() == nullptr) {
+ if (expected_info.parent.get() != nullptr) {
+ LOG(WARNING) << "ClassLoaderContext parent mismatch. "
+ << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
+ return false;
+ }
+ return true;
+ } else if (expected_info.parent.get() == nullptr) {
+ LOG(WARNING) << "ClassLoaderContext parent mismatch. "
+ << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
+ return false;
+ } else {
+ return ClassLoaderInfoMatch(*info.parent.get(),
+ *expected_info.parent.get(),
+ context_spec,
+ verify_names,
+ verify_checksums);
+ }
}
} // namespace art
diff --git a/runtime/class_loader_context.h b/runtime/class_loader_context.h
index a4268aa..37cef81 100644
--- a/runtime/class_loader_context.h
+++ b/runtime/class_loader_context.h
@@ -154,10 +154,11 @@
// This will return a context with a single and empty PathClassLoader.
static std::unique_ptr<ClassLoaderContext> Default();
- private:
struct ClassLoaderInfo {
// The type of this class loader.
ClassLoaderType type;
+ // Shared libraries this context has.
+ std::vector<std::unique_ptr<ClassLoaderInfo>> shared_libraries;
// The list of class path elements that this loader loads.
// Note that this list may contain relative paths.
std::vector<std::string> classpath;
@@ -171,13 +172,35 @@
// After OpenDexFiles, in case some of the dex files were opened from their oat files
// this holds the list of opened oat files.
std::vector<std::unique_ptr<OatFile>> opened_oat_files;
+ // The parent class loader.
+ std::unique_ptr<ClassLoaderInfo> parent;
explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {}
};
+ private:
// Creates an empty context (with no class loaders).
ClassLoaderContext();
+ // Get the parent of the class loader chain at depth `index`.
+ ClassLoaderInfo* GetParent(size_t index) const {
+ ClassLoaderInfo* result = class_loader_chain_.get();
+ while ((result != nullptr) && (index-- != 0)) {
+ result = result->parent.get();
+ }
+ return result;
+ }
+
+ size_t GetParentChainSize() const {
+ size_t result = 0;
+ ClassLoaderInfo* info = class_loader_chain_.get();
+ while (info != nullptr) {
+ ++result;
+ info = info->parent.get();
+ }
+ return result;
+ }
+
// Constructs an empty context.
// `owns_the_dex_files` specifies whether or not the context will own the opened dex files
// present in the class loader chain. If `owns_the_dex_files` is true then OpenDexFiles cannot
@@ -188,13 +211,13 @@
// Reads the class loader spec in place and returns true if the spec is valid and the
// compilation context was constructed.
bool Parse(const std::string& spec, bool parse_checksums = false);
+ ClassLoaderInfo* ParseInternal(const std::string& spec, bool parse_checksums);
- // Attempts to parse a single class loader spec for the given class_loader_type.
- // If successful the class loader spec will be added to the chain.
- // Returns whether or not the operation was successful.
- bool ParseClassLoaderSpec(const std::string& class_loader_spec,
- ClassLoaderType class_loader_type,
- bool parse_checksums = false);
+ // Attempts to parse a single class loader spec.
+ // Returns the ClassLoaderInfo abstraction for this spec, or null if it cannot be parsed.
+ std::unique_ptr<ClassLoaderInfo> ParseClassLoaderSpec(
+ const std::string& class_loader_spec,
+ bool parse_checksums = false);
// CHECKs that the dex files were opened (OpenDexFiles was called and set dex_files_open_result_
// to true). Aborts if not. The `calling_method` is used in the log message to identify the source
@@ -219,6 +242,20 @@
bool for_dex2oat,
ClassLoaderContext* stored_context) const;
+ // Internal version of `EncodeContext`, which will be called recursively
+ // on the parent and shared libraries.
+ void EncodeContextInternal(const ClassLoaderInfo& info,
+ const std::string& base_dir,
+ bool for_dex2oat,
+ ClassLoaderInfo* stored_info,
+ std::ostringstream& out) const;
+
+ bool ClassLoaderInfoMatch(const ClassLoaderInfo& info,
+ const ClassLoaderInfo& expected_info,
+ const std::string& context_spec,
+ bool verify_names,
+ bool verify_checksums) const;
+
// Extracts the class loader type from the given spec.
// Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
// recognized.
@@ -228,13 +265,8 @@
// The returned format can be used when parsing a context spec.
static const char* GetClassLoaderTypeName(ClassLoaderType type);
- // Returns the WellKnownClass for the given class loader type.
- static jclass GetClassLoaderClass(ClassLoaderType type);
-
- // The class loader chain represented as a vector.
- // The parent of class_loader_chain_[i] is class_loader_chain_[i++].
- // The parent of the last element is assumed to be the boot class loader.
- std::vector<ClassLoaderInfo> class_loader_chain_;
+ // The class loader chain.
+ std::unique_ptr<ClassLoaderInfo> class_loader_chain_;
// Whether or not the class loader context should be ignored at runtime when loading the oat
// files. When true, dex2oat will use OatFile::kSpecialSharedLibrary as the classpath key in
diff --git a/runtime/class_loader_context_test.cc b/runtime/class_loader_context_test.cc
index ea624f1..cb3dc65 100644
--- a/runtime/class_loader_context_test.cc
+++ b/runtime/class_loader_context_test.cc
@@ -40,7 +40,7 @@
public:
void VerifyContextSize(ClassLoaderContext* context, size_t expected_size) {
ASSERT_TRUE(context != nullptr);
- ASSERT_EQ(expected_size, context->class_loader_chain_.size());
+ ASSERT_EQ(expected_size, context->GetParentChainSize());
}
void VerifyClassLoaderPCL(ClassLoaderContext* context,
@@ -57,6 +57,33 @@
context, index, ClassLoaderContext::kDelegateLastClassLoader, classpath);
}
+ void VerifyClassLoaderSharedLibraryPCL(ClassLoaderContext* context,
+ size_t loader_index,
+ size_t shared_library_index,
+ const std::string& classpath) {
+ VerifyClassLoaderInfoSL(
+ context, loader_index, shared_library_index, ClassLoaderContext::kPathClassLoader,
+ classpath);
+ }
+
+ void VerifySharedLibrariesSize(ClassLoaderContext* context,
+ size_t loader_index,
+ size_t expected_size) {
+ ASSERT_TRUE(context != nullptr);
+ ASSERT_GT(context->GetParentChainSize(), loader_index);
+ const ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(loader_index);
+ ASSERT_EQ(info.shared_libraries.size(), expected_size);
+ }
+
+ void VerifyClassLoaderSharedLibraryDLC(ClassLoaderContext* context,
+ size_t loader_index,
+ size_t shared_library_index,
+ const std::string& classpath) {
+ VerifyClassLoaderInfoSL(
+ context, loader_index, shared_library_index, ClassLoaderContext::kDelegateLastClassLoader,
+ classpath);
+ }
+
void VerifyClassLoaderPCLFromTestDex(ClassLoaderContext* context,
size_t index,
const std::string& test_name) {
@@ -91,7 +118,7 @@
ASSERT_TRUE(context != nullptr);
ASSERT_TRUE(context->dex_files_open_attempted_);
ASSERT_TRUE(context->dex_files_open_result_);
- ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
+ ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(index);
ASSERT_EQ(all_dex_files->size(), info.classpath.size());
ASSERT_EQ(all_dex_files->size(), info.opened_dex_files.size());
size_t cur_open_dex_index = 0;
@@ -168,14 +195,31 @@
ClassLoaderContext::ClassLoaderType type,
const std::string& classpath) {
ASSERT_TRUE(context != nullptr);
- ASSERT_GT(context->class_loader_chain_.size(), index);
- ClassLoaderContext::ClassLoaderInfo& info = context->class_loader_chain_[index];
+ ASSERT_GT(context->GetParentChainSize(), index);
+ ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(index);
ASSERT_EQ(type, info.type);
std::vector<std::string> expected_classpath;
Split(classpath, ':', &expected_classpath);
ASSERT_EQ(expected_classpath, info.classpath);
}
+ void VerifyClassLoaderInfoSL(ClassLoaderContext* context,
+ size_t loader_index,
+ size_t shared_library_index,
+ ClassLoaderContext::ClassLoaderType type,
+ const std::string& classpath) {
+ ASSERT_TRUE(context != nullptr);
+ ASSERT_GT(context->GetParentChainSize(), loader_index);
+ const ClassLoaderContext::ClassLoaderInfo& info = *context->GetParent(loader_index);
+ ASSERT_GT(info.shared_libraries.size(), shared_library_index);
+ const ClassLoaderContext::ClassLoaderInfo& sl =
+ *info.shared_libraries[shared_library_index].get();
+ ASSERT_EQ(type, info.type);
+ std::vector<std::string> expected_classpath;
+ Split(classpath, ':', &expected_classpath);
+ ASSERT_EQ(expected_classpath, sl.classpath);
+ }
+
void VerifyClassLoaderFromTestDex(ClassLoaderContext* context,
size_t index,
ClassLoaderContext::ClassLoaderType type,
@@ -223,6 +267,23 @@
VerifyClassLoaderPCL(context.get(), 2, "e.dex");
}
+TEST_F(ClassLoaderContextTest, ParseSharedLibraries) {
+ std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(
+ "PCL[a.dex:b.dex]{PCL[s1.dex]#PCL[s2.dex:s3.dex]};DLC[c.dex:d.dex]{DLC[s4.dex]}");
+ VerifyContextSize(context.get(), 2);
+ VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 0, "s1.dex");
+ VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 1, "s2.dex:s3.dex");
+ VerifyClassLoaderDLC(context.get(), 1, "c.dex:d.dex");
+ VerifyClassLoaderSharedLibraryDLC(context.get(), 1, 0, "s4.dex");
+}
+
+TEST_F(ClassLoaderContextTest, ParseEnclosingSharedLibraries) {
+ std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(
+ "PCL[a.dex:b.dex]{PCL[s1.dex]{PCL[s2.dex:s3.dex];PCL[s4.dex]}}");
+ VerifyContextSize(context.get(), 1);
+ VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 0, "s1.dex");
+}
+
TEST_F(ClassLoaderContextTest, ParseValidEmptyContextDLC) {
std::unique_ptr<ClassLoaderContext> context =
ClassLoaderContext::Create("DLC[]");
@@ -230,6 +291,13 @@
VerifyClassLoaderDLC(context.get(), 0, "");
}
+TEST_F(ClassLoaderContextTest, ParseValidEmptyContextSharedLibrary) {
+ std::unique_ptr<ClassLoaderContext> context =
+ ClassLoaderContext::Create("DLC[]{}");
+ VerifyContextSize(context.get(), 1);
+ VerifySharedLibrariesSize(context.get(), 0, 0);
+}
+
TEST_F(ClassLoaderContextTest, ParseValidContextSpecialSymbol) {
std::unique_ptr<ClassLoaderContext> context =
ClassLoaderContext::Create(OatFile::kSpecialSharedLibrary);
@@ -243,6 +311,11 @@
ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCLa.dex]"));
ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL{a.dex}"));
ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex];DLC[b.dex"));
+ ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex]{ABC};DLC[b.dex"));
+ ASSERT_TRUE(nullptr == ClassLoaderContext::Create("PCL[a.dex]{};DLC[b.dex"));
+ ASSERT_TRUE(nullptr == ClassLoaderContext::Create("DLC[s4.dex]}"));
+ ASSERT_TRUE(nullptr == ClassLoaderContext::Create("DLC[s4.dex]{"));
+ ASSERT_TRUE(nullptr == ClassLoaderContext::Create("DLC{DLC[s4.dex]}"));
}
TEST_F(ClassLoaderContextTest, OpenInvalidDexFiles) {
@@ -662,6 +735,62 @@
ClassLoaderContext::VerificationResult::kMismatch);
}
+TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchWithSL) {
+ std::string context_spec =
+ "PCL[a.dex*123:b.dex*456]{PCL[d.dex*321];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999]}"
+ ";DLC[c.dex*890]";
+ std::unique_ptr<ClassLoaderContext> context = ParseContextWithChecksums(context_spec);
+ // Pretend that we successfully open the dex files to pass the DCHECKS.
+ // (as it's much easier to test all the corner cases without relying on actual dex files).
+ PretendContextOpenedDexFiles(context.get());
+
+ VerifyContextSize(context.get(), 2);
+ VerifyClassLoaderPCL(context.get(), 0, "a.dex:b.dex");
+ VerifyClassLoaderDLC(context.get(), 1, "c.dex");
+ VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 0, "d.dex");
+ VerifyClassLoaderSharedLibraryPCL(context.get(), 0, 1, "f.dex:g.dex");
+
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(context_spec),
+ ClassLoaderContext::VerificationResult::kVerifies);
+
+ std::string wrong_class_loader_type =
+ "PCL[a.dex*123:b.dex*456]{DLC[d.dex*321];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999]}"
+ ";DLC[c.dex*890]";
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_class_loader_type),
+ ClassLoaderContext::VerificationResult::kMismatch);
+
+ std::string wrong_class_loader_order =
+ "PCL[a.dex*123:b.dex*456]{PCL[f.dex#098:g.dex#999}#PCL[d.dex*321];PCL[e.dex*654]}"
+ ";DLC[c.dex*890]";
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_class_loader_order),
+ ClassLoaderContext::VerificationResult::kMismatch);
+
+ std::string wrong_classpath_order =
+ "PCL[a.dex*123:b.dex*456]{PCL[d.dex*321];PCL[e.dex*654]#PCL[g.dex*999:f.dex*098]}"
+ ";DLC[c.dex*890]";
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_classpath_order),
+ ClassLoaderContext::VerificationResult::kMismatch);
+
+ std::string wrong_checksum =
+ "PCL[a.dex*123:b.dex*456]{PCL[d.dex*333];PCL[e.dex*654]#PCL[g.dex*999:f.dex*098]}"
+ ";DLC[c.dex*890]";
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_checksum),
+ ClassLoaderContext::VerificationResult::kMismatch);
+
+ std::string wrong_extra_class_loader =
+ "PCL[a.dex*123:b.dex*456]"
+ "{PCL[d.dex*321];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999];PCL[i.dex#444]}"
+ ";DLC[c.dex*890]";
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_extra_class_loader),
+ ClassLoaderContext::VerificationResult::kMismatch);
+
+ std::string wrong_extra_classpath =
+ "PCL[a.dex*123:b.dex*456]{PCL[d.dex*321:i.dex#444];PCL[e.dex*654]#PCL[f.dex*098:g.dex*999]}"
+ ";DLC[c.dex*890]";
+ ASSERT_EQ(context->VerifyClassLoaderContextMatch(wrong_extra_classpath),
+ ClassLoaderContext::VerificationResult::kMismatch);
+}
+
TEST_F(ClassLoaderContextTest, VerifyClassLoaderContextMatchAfterEncoding) {
jobject class_loader_a = LoadDexInPathClassLoader("ForClassLoaderA", nullptr);
jobject class_loader_b = LoadDexInDelegateLastClassLoader("ForClassLoaderB", class_loader_a);
diff --git a/runtime/gc/space/image_space.cc b/runtime/gc/space/image_space.cc
index 96a2cea..b46abfb 100644
--- a/runtime/gc/space/image_space.cc
+++ b/runtime/gc/space/image_space.cc
@@ -1241,7 +1241,7 @@
for (GcRoot<mirror::String>& root : strings) {
root = GcRoot<mirror::String>(fixup_adapter(root.Read<kWithoutReadBarrier>()));
}
- });
+ }, /*is_boot_image=*/ false);
}
}
if (VLOG_IS_ON(image)) {
diff --git a/runtime/handle.h b/runtime/handle.h
index 18e503d..b13c43e 100644
--- a/runtime/handle.h
+++ b/runtime/handle.h
@@ -62,8 +62,9 @@
return down_cast<T*>(reference_->AsMirrorPtr());
}
- ALWAYS_INLINE bool IsNull() const REQUIRES_SHARED(Locks::mutator_lock_) {
- return Get() == nullptr;
+ ALWAYS_INLINE bool IsNull() const {
+ // It's safe to null-check it without a read barrier.
+ return reference_->IsNull();
}
ALWAYS_INLINE jobject ToJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
diff --git a/runtime/intern_table-inl.h b/runtime/intern_table-inl.h
index 84754fa..6fc53e9 100644
--- a/runtime/intern_table-inl.h
+++ b/runtime/intern_table-inl.h
@@ -29,14 +29,17 @@
const Visitor& visitor) {
DCHECK(image_space != nullptr);
// Only add if we have the interned strings section.
- const ImageSection& section = image_space->GetImageHeader().GetInternedStringsSection();
+ const ImageHeader& header = image_space->GetImageHeader();
+ const ImageSection& section = header.GetInternedStringsSection();
if (section.Size() > 0) {
- AddTableFromMemory(image_space->Begin() + section.Offset(), visitor);
+ AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage());
}
}
template <typename Visitor>
-inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor) {
+inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr,
+ const Visitor& visitor,
+ bool is_boot_image) {
size_t read_count = 0;
UnorderedSet set(ptr, /*make copy*/false, &read_count);
{
@@ -46,13 +49,14 @@
// Visit the unordered set, may remove elements.
visitor(set);
if (!set.empty()) {
- strong_interns_.AddInternStrings(std::move(set));
+ strong_interns_.AddInternStrings(std::move(set), is_boot_image);
}
}
return read_count;
}
-inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings) {
+inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings,
+ bool is_boot_image) {
static constexpr bool kCheckDuplicates = kIsDebugBuild;
if (kCheckDuplicates) {
// Avoid doing read barriers since the space might not yet be added to the heap.
@@ -64,7 +68,50 @@
}
}
// Insert at the front since we add new interns into the back.
- tables_.insert(tables_.begin(), std::move(intern_strings));
+ tables_.insert(tables_.begin(),
+ InternalTable(std::move(intern_strings), is_boot_image));
+}
+
+template <typename Visitor>
+inline void InternTable::VisitInterns(const Visitor& visitor,
+ bool visit_boot_images,
+ bool visit_non_boot_images) {
+ auto visit_tables = [&](std::vector<Table::InternalTable>& tables)
+ NO_THREAD_SAFETY_ANALYSIS {
+ for (Table::InternalTable& table : tables) {
+ // Determine if we want to visit the table based on the flags..
+ const bool visit =
+ (visit_boot_images && table.IsBootImage()) ||
+ (visit_non_boot_images && !table.IsBootImage());
+ if (visit) {
+ for (auto& intern : table.set_) {
+ visitor(intern);
+ }
+ }
+ }
+ };
+ visit_tables(strong_interns_.tables_);
+ visit_tables(weak_interns_.tables_);
+}
+
+inline size_t InternTable::CountInterns(bool visit_boot_images,
+ bool visit_non_boot_images) const {
+ size_t ret = 0u;
+ auto visit_tables = [&](const std::vector<Table::InternalTable>& tables)
+ NO_THREAD_SAFETY_ANALYSIS {
+ for (const Table::InternalTable& table : tables) {
+ // Determine if we want to visit the table based on the flags..
+ const bool visit =
+ (visit_boot_images && table.IsBootImage()) ||
+ (visit_non_boot_images && !table.IsBootImage());
+ if (visit) {
+ ret += table.set_.size();
+ }
+ }
+ };
+ visit_tables(strong_interns_.tables_);
+ visit_tables(weak_interns_.tables_);
+ return ret;
}
} // namespace art
diff --git a/runtime/intern_table.cc b/runtime/intern_table.cc
index 2449121..9ac9927 100644
--- a/runtime/intern_table.cc
+++ b/runtime/intern_table.cc
@@ -351,22 +351,22 @@
UnorderedSet combined;
if (tables_.size() > 1) {
table_to_write = &combined;
- for (UnorderedSet& table : tables_) {
- for (GcRoot<mirror::String>& string : table) {
+ for (InternalTable& table : tables_) {
+ for (GcRoot<mirror::String>& string : table.set_) {
combined.insert(string);
}
}
} else {
- table_to_write = &tables_.back();
+ table_to_write = &tables_.back().set_;
}
return table_to_write->WriteToMemory(ptr);
}
void InternTable::Table::Remove(ObjPtr<mirror::String> s) {
- for (UnorderedSet& table : tables_) {
- auto it = table.find(GcRoot<mirror::String>(s));
- if (it != table.end()) {
- table.erase(it);
+ for (InternalTable& table : tables_) {
+ auto it = table.set_.find(GcRoot<mirror::String>(s));
+ if (it != table.set_.end()) {
+ table.set_.erase(it);
return;
}
}
@@ -375,9 +375,9 @@
ObjPtr<mirror::String> InternTable::Table::Find(ObjPtr<mirror::String> s) {
Locks::intern_table_lock_->AssertHeld(Thread::Current());
- for (UnorderedSet& table : tables_) {
- auto it = table.find(GcRoot<mirror::String>(s));
- if (it != table.end()) {
+ for (InternalTable& table : tables_) {
+ auto it = table.set_.find(GcRoot<mirror::String>(s));
+ if (it != table.set_.end()) {
return it->Read();
}
}
@@ -386,9 +386,9 @@
ObjPtr<mirror::String> InternTable::Table::Find(const Utf8String& string) {
Locks::intern_table_lock_->AssertHeld(Thread::Current());
- for (UnorderedSet& table : tables_) {
- auto it = table.find(string);
- if (it != table.end()) {
+ for (InternalTable& table : tables_) {
+ auto it = table.set_.find(string);
+ if (it != table.set_.end()) {
return it->Read();
}
}
@@ -396,29 +396,29 @@
}
void InternTable::Table::AddNewTable() {
- tables_.push_back(UnorderedSet());
+ tables_.push_back(InternalTable());
}
void InternTable::Table::Insert(ObjPtr<mirror::String> s) {
// Always insert the last table, the image tables are before and we avoid inserting into these
// to prevent dirty pages.
DCHECK(!tables_.empty());
- tables_.back().insert(GcRoot<mirror::String>(s));
+ tables_.back().set_.insert(GcRoot<mirror::String>(s));
}
void InternTable::Table::VisitRoots(RootVisitor* visitor) {
BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(
visitor, RootInfo(kRootInternedString));
- for (UnorderedSet& table : tables_) {
- for (auto& intern : table) {
+ for (InternalTable& table : tables_) {
+ for (auto& intern : table.set_) {
buffered_visitor.VisitRoot(intern);
}
}
}
void InternTable::Table::SweepWeaks(IsMarkedVisitor* visitor) {
- for (UnorderedSet& table : tables_) {
- SweepWeaks(&table, visitor);
+ for (InternalTable& table : tables_) {
+ SweepWeaks(&table.set_, visitor);
}
}
@@ -440,8 +440,8 @@
return std::accumulate(tables_.begin(),
tables_.end(),
0U,
- [](size_t sum, const UnorderedSet& set) {
- return sum + set.size();
+ [](size_t sum, const InternalTable& table) {
+ return sum + table.Size();
});
}
@@ -460,10 +460,10 @@
InternTable::Table::Table() {
Runtime* const runtime = Runtime::Current();
- // Initial table.
- tables_.push_back(UnorderedSet());
- tables_.back().SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
- runtime->GetHashTableMaxLoadFactor());
+ InternalTable initial_table;
+ initial_table.set_.SetLoadFactor(runtime->GetHashTableMinLoadFactor(),
+ runtime->GetHashTableMaxLoadFactor());
+ tables_.push_back(std::move(initial_table));
}
} // namespace art
diff --git a/runtime/intern_table.h b/runtime/intern_table.h
index e918a45..165d56c 100644
--- a/runtime/intern_table.h
+++ b/runtime/intern_table.h
@@ -167,6 +167,17 @@
void VisitRoots(RootVisitor* visitor, VisitRootFlags flags)
REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::intern_table_lock_);
+ // Visit all of the interns in the table.
+ template <typename Visitor>
+ void VisitInterns(const Visitor& visitor,
+ bool visit_boot_images,
+ bool visit_non_boot_images)
+ REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
+
+ // Count the number of intern strings in the table.
+ size_t CountInterns(bool visit_boot_images, bool visit_non_boot_images) const
+ REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
+
void DumpForSigQuit(std::ostream& os) const REQUIRES(!Locks::intern_table_lock_);
void BroadcastForNewInterns();
@@ -198,6 +209,33 @@
// weak interns and strong interns.
class Table {
public:
+ class InternalTable {
+ public:
+ InternalTable() = default;
+ InternalTable(UnorderedSet&& set, bool is_boot_image)
+ : set_(std::move(set)), is_boot_image_(is_boot_image) {}
+
+ bool Empty() const {
+ return set_.empty();
+ }
+
+ size_t Size() const {
+ return set_.size();
+ }
+
+ bool IsBootImage() const {
+ return is_boot_image_;
+ }
+
+ private:
+ UnorderedSet set_;
+ bool is_boot_image_ = false;
+
+ friend class InternTable;
+ friend class Table;
+ ART_FRIEND_TEST(InternTableTest, CrossHash);
+ };
+
Table();
ObjPtr<mirror::String> Find(ObjPtr<mirror::String> s) REQUIRES_SHARED(Locks::mutator_lock_)
REQUIRES(Locks::intern_table_lock_);
@@ -219,7 +257,7 @@
// debug builds. Returns how many bytes were read.
// NO_THREAD_SAFETY_ANALYSIS for the visitor that may require locks.
template <typename Visitor>
- size_t AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor)
+ size_t AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor, bool is_boot_image)
REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
// Write the intern tables to ptr, if there are multiple tables they are combined into a single
// one. Returns how many bytes were written.
@@ -231,12 +269,12 @@
REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::intern_table_lock_);
// Add a table to the front of the tables vector.
- void AddInternStrings(UnorderedSet&& intern_strings)
+ void AddInternStrings(UnorderedSet&& intern_strings, bool is_boot_image)
REQUIRES(Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
// We call AddNewTable when we create the zygote to reduce private dirty pages caused by
// modifying the zygote intern table. The back of table is modified when strings are interned.
- std::vector<UnorderedSet> tables_;
+ std::vector<InternalTable> tables_;
friend class InternTable;
friend class linker::ImageWriter;
@@ -251,7 +289,7 @@
// Add a table from memory to the strong interns.
template <typename Visitor>
- size_t AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor)
+ size_t AddTableFromMemory(const uint8_t* ptr, const Visitor& visitor, bool is_boot_image)
REQUIRES(!Locks::intern_table_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
ObjPtr<mirror::String> InsertStrong(ObjPtr<mirror::String> s)
diff --git a/runtime/intern_table_test.cc b/runtime/intern_table_test.cc
index b3bf1ba..b64ca7d 100644
--- a/runtime/intern_table_test.cc
+++ b/runtime/intern_table_test.cc
@@ -78,9 +78,9 @@
GcRoot<mirror::String> str(mirror::String::AllocFromModifiedUtf8(soa.Self(), "00000000"));
MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
- for (InternTable::UnorderedSet& table : t.strong_interns_.tables_) {
+ for (InternTable::Table::InternalTable& table : t.strong_interns_.tables_) {
// The negative hash value shall be 32-bit wide on every host.
- ASSERT_TRUE(IsUint<32>(table.hashfn_(str)));
+ ASSERT_TRUE(IsUint<32>(table.set_.hashfn_(str)));
}
}
diff --git a/runtime/mirror/class.cc b/runtime/mirror/class.cc
index e655052..e33e407 100644
--- a/runtime/mirror/class.cc
+++ b/runtime/mirror/class.cc
@@ -630,9 +630,14 @@
// If we do not have a dex_cache match, try to find the declared method in this class now.
if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
DCHECK(name.empty());
- name = dex_file.StringDataByIdx(method_id.name_idx_);
+ // Avoid string comparisons by comparing the respective unicode lengths first.
+ uint32_t length, other_length; // UTF16 length.
+ name = dex_file.GetMethodName(method_id, &length);
for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
- if (method.GetName() == name && method.GetSignature() == signature) {
+ DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex);
+ const char* other_name = method.GetDexFile()->GetMethodName(
+ method.GetDexMethodIndex(), &other_length);
+ if (length == other_length && name == other_name && signature == method.GetSignature()) {
return &method;
}
}
diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc
index 0d7160e..32b88e6 100644
--- a/runtime/native/dalvik_system_VMRuntime.cc
+++ b/runtime/native/dalvik_system_VMRuntime.cc
@@ -76,10 +76,6 @@
static void VMRuntime_disableJitCompilation(JNIEnv*, jobject) {
}
-static jboolean VMRuntime_hasUsedHiddenApi(JNIEnv*, jobject) {
- return Runtime::Current()->HasPendingHiddenApiWarning() ? JNI_TRUE : JNI_FALSE;
-}
-
static void VMRuntime_setHiddenApiExemptions(JNIEnv* env,
jclass,
jobjectArray exemptions) {
@@ -697,7 +693,6 @@
NATIVE_METHOD(VMRuntime, concurrentGC, "()V"),
NATIVE_METHOD(VMRuntime, disableJitCompilation, "()V"),
FAST_NATIVE_METHOD(VMRuntime, hasBootImageSpaces, "()Z"), // Could be CRITICAL.
- NATIVE_METHOD(VMRuntime, hasUsedHiddenApi, "()Z"),
NATIVE_METHOD(VMRuntime, setHiddenApiExemptions, "([Ljava/lang/String;)V"),
NATIVE_METHOD(VMRuntime, setHiddenApiAccessLogSamplingRate, "(I)V"),
NATIVE_METHOD(VMRuntime, getTargetHeapUtilization, "()F"),
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 7c320d8..af87637 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -1039,11 +1039,8 @@
}
#ifdef ART_TARGET_ANDROID
android_dlextinfo extinfo = {};
- extinfo.flags = ANDROID_DLEXT_FORCE_LOAD | // Force-load, don't reuse handle
- // (open oat files multiple
- // times).
- ANDROID_DLEXT_FORCE_FIXED_VADDR; // Take a non-zero vaddr as absolute
- // (non-pic boot image).
+ extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; // Force-load, don't reuse handle
+ // (open oat files multiple times).
if (reservation != nullptr) {
if (!reservation->IsValid()) {
*error_msg = StringPrintf("Invalid reservation for %s", elf_filename.c_str());
diff --git a/test/050-sync-test/src/Main.java b/test/050-sync-test/src/Main.java
index 734b51e..ba37818 100644
--- a/test/050-sync-test/src/Main.java
+++ b/test/050-sync-test/src/Main.java
@@ -133,11 +133,13 @@
class SleepyThread extends Thread {
private SleepyThread mOther;
private Integer[] mWaitOnMe; // any type of object will do
+ private volatile boolean otherDone;
private static int count = 0;
SleepyThread(SleepyThread other) {
mOther = other;
+ otherDone = false;
mWaitOnMe = new Integer[] { 1, 2 };
setName("thread#" + count);
@@ -158,9 +160,11 @@
boolean intr = false;
try {
+ do {
synchronized (mWaitOnMe) {
mWaitOnMe.wait(9000);
}
+ } while (!otherDone);
} catch (InterruptedException ie) {
// Expecting this; interrupted should be false.
System.out.println(Thread.currentThread().getName() +
@@ -182,6 +186,7 @@
System.out.println("interrupting other (isAlive="
+ mOther.isAlive() + ")");
mOther.interrupt();
+ mOther.otherDone = true;
}
}
}
diff --git a/test/VerifySoftFailDuringClinit/ClassToInitialize.smali b/test/VerifySoftFailDuringClinit/ClassToInitialize.smali
new file mode 100644
index 0000000..0d12ec8
--- /dev/null
+++ b/test/VerifySoftFailDuringClinit/ClassToInitialize.smali
@@ -0,0 +1,22 @@
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+.class public LClassToInitialize;
+.super Ljava/lang/Object;
+
+.method public static constructor <clinit>()V
+ .registers 0
+ invoke-static {}, LVerifySoftFail;->empty()V
+ return-void
+.end method
diff --git a/test/VerifySoftFailDuringClinit/VerifySoftFail.smali b/test/VerifySoftFailDuringClinit/VerifySoftFail.smali
new file mode 100644
index 0000000..e0f4946
--- /dev/null
+++ b/test/VerifySoftFailDuringClinit/VerifySoftFail.smali
@@ -0,0 +1,27 @@
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+.class public LVerifySoftFail;
+.super Ljava/lang/Object;
+
+.method public static empty()V
+ .registers 0
+ return-void
+.end method
+
+.method public static softFail()V
+ .registers 0
+ invoke-static {}, LMissingClass;->test()V
+ return-void
+.end method
diff --git a/test/knownfailures.json b/test/knownfailures.json
index 7b40160..f0eacfa 100644
--- a/test/knownfailures.json
+++ b/test/knownfailures.json
@@ -1110,11 +1110,5 @@
"tests": ["454-get-vreg", "457-regs"],
"variant": "baseline",
"description": ["Tests are expected to fail with baseline."]
- },
- {
- "tests": ["050-sync-test"],
- "variant": "target & gcstress & debug",
- "bug": "b/117597114",
- "description": ["Looks timing dependent"]
}
]