Compile time class initialization for classes without <clinit>
Change-Id: I4f167253fbfa16ed8f51928c5891e7a48b68e526
diff --git a/src/class_linker_test.cc b/src/class_linker_test.cc
index f43e47c..32f2eec 100644
--- a/src/class_linker_test.cc
+++ b/src/class_linker_test.cc
@@ -108,6 +108,7 @@
void AssertMethod(Class* klass, Method* method) {
EXPECT_TRUE(method != NULL);
+ EXPECT_TRUE(method->GetClass() != NULL);
EXPECT_TRUE(method->GetName() != NULL);
EXPECT_TRUE(method->GetSignature() != NULL);
@@ -133,6 +134,7 @@
void AssertField(Class* klass, Field* field) {
EXPECT_TRUE(field != NULL);
+ EXPECT_TRUE(field->GetClass() != NULL);
EXPECT_EQ(klass, field->GetDeclaringClass());
EXPECT_TRUE(field->GetName() != NULL);
EXPECT_TRUE(field->GetType() != NULL);
@@ -146,6 +148,8 @@
EXPECT_TRUE(klass->HasSuperClass());
EXPECT_TRUE(klass->GetSuperClass() != NULL);
}
+ EXPECT_TRUE(klass->GetClass() != NULL);
+ EXPECT_EQ(klass->GetClass(), klass->GetClass()->GetClass());
EXPECT_TRUE(klass->GetDexCache() != NULL);
EXPECT_TRUE(klass->IsResolved());
EXPECT_FALSE(klass->IsErroneous());
diff --git a/src/compiler.cc b/src/compiler.cc
index 528a0b2..0d583ac 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -43,14 +43,7 @@
void Compiler::CompileAll(const ClassLoader* class_loader) {
Resolve(class_loader);
Verify(class_loader);
-
- // TODO: mark all verified classes initialized if they have no <clinit>
- ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
- Class* Class_class = class_linker->FindSystemClass("Ljava/lang/Class;");
- Method* Class_clinit = Class_class->FindDirectMethod("<clinit>", "()V");
- CHECK(Class_clinit == NULL);
- Class_class->SetStatus(Class::kStatusInitialized);
-
+ InitializeClassesWithoutClinit(class_loader);
Compile(class_loader);
SetCodeAndDirectMethods(class_loader);
}
@@ -59,6 +52,7 @@
const ClassLoader* class_loader = method->GetDeclaringClass()->GetClassLoader();
Resolve(class_loader);
Verify(class_loader);
+ InitializeClassesWithoutClinit(class_loader);
CompileMethod(method);
SetCodeAndDirectMethods(class_loader);
}
@@ -83,7 +77,8 @@
// Class derived values are more complicated, they require the linker and loader
for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
- class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
+ Class* klass = class_linker->ResolveType(dex_file, i, dex_cache, class_loader);
+ CHECK(klass->IsResolved());
}
for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
// unknown if direct or virtual, try both
@@ -116,8 +111,44 @@
const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
const char* descriptor = dex_file.GetClassDescriptor(class_def);
Class* klass = class_linker->FindClass(descriptor, class_loader);
+ CHECK(klass->IsResolved());
CHECK(klass != NULL);
class_linker->VerifyClass(klass);
+ CHECK(klass->IsVerified() || klass->IsErroneous());
+ }
+}
+
+void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader) {
+ const std::vector<const DexFile*>& class_path = ClassLoader::GetClassPath(class_loader);
+ for (size_t i = 0; i != class_path.size(); ++i) {
+ const DexFile* dex_file = class_path[i];
+ CHECK(dex_file != NULL);
+ InitializeClassesWithoutClinit(class_loader, *dex_file);
+ }
+}
+
+void Compiler::InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file) {
+ ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
+ for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
+ const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
+ const char* descriptor = dex_file.GetClassDescriptor(class_def);
+ Class* klass = class_linker->FindClass(descriptor, class_loader);
+ CHECK(klass != NULL);
+ if (klass->IsInitialized()) {
+ continue;
+ }
+ CHECK(klass->IsVerified() || klass->IsErroneous());
+ if (!klass->IsVerified()) {
+ continue;
+ }
+ Method* clinit = klass->FindDirectMethod("<clinit>", "()V");
+ if (clinit != NULL) {
+ continue;
+ }
+ klass->SetStatus(Class::kStatusInitialized);
+ // TODO: enable after crash investigation
+ // DexCache* dex_cache = class_linker->FindDexCache(dex_file);
+ // dex_cache->GetInitializedStaticStorage()->Set(i, klass);
}
}
diff --git a/src/compiler.h b/src/compiler.h
index 5735c6b..26b33b8 100644
--- a/src/compiler.h
+++ b/src/compiler.h
@@ -38,6 +38,9 @@
void Verify(const ClassLoader* class_loader);
void VerifyDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
+ void InitializeClassesWithoutClinit(const ClassLoader* class_loader);
+ void InitializeClassesWithoutClinit(const ClassLoader* class_loader, const DexFile& dex_file);
+
void Compile(const ClassLoader* class_loader);
void CompileDexFile(const ClassLoader* class_loader, const DexFile& dex_file);
void CompileClass(Class* klass);
diff --git a/src/object.cc b/src/object.cc
index 24fe628..ea8a4bf 100644
--- a/src/object.cc
+++ b/src/object.cc
@@ -502,6 +502,7 @@
if (have_executable_code && stub != NULL) {
LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
(*stub)(this, receiver, self, args, result);
+ LOG(INFO) << "returning " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub;
} else {
LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this);
if (result != NULL) {
@@ -534,7 +535,7 @@
void Class::SetStatus(Status new_status) {
CHECK(new_status > GetStatus() || new_status == kStatusError ||
- !Runtime::Current()->IsStarted());
+ !Runtime::Current()->IsStarted()) << GetDescriptor()->ToModifiedUtf8();
CHECK(sizeof(Status) == sizeof(uint32_t));
return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_),
new_status, false);
@@ -799,6 +800,7 @@
Method* Class::FindVirtualMethodForInterface(Method* method) {
Class* declaring_class = method->GetDeclaringClass();
+ DCHECK(declaring_class != NULL);
DCHECK(declaring_class->IsInterface());
// TODO cache to improve lookup speed
int32_t iftable_count = GetIfTableCount();