blob: 007b8a6f63422f95ecd68154e73db64cabb01a22 [file] [log] [blame]
/*
* Copyright (C) 2011 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.
*/
#include "class_linker.h"
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <deque>
#include <string>
#include <utility>
#include <vector>
#include "casts.h"
#include "class_loader.h"
#include "debugger.h"
#include "dex_cache.h"
#include "dex_file.h"
#include "heap.h"
#include "intern_table.h"
#include "leb128.h"
#include "logging.h"
#include "oat_file.h"
#include "object.h"
#include "object_utils.h"
#include "os.h"
#include "runtime.h"
#include "runtime_support.h"
#if defined(ART_USE_LLVM_COMPILER)
#include "compiler_llvm/runtime_support_llvm.h"
#endif
#include "ScopedLocalRef.h"
#include "scoped_thread_state_change.h"
#include "sirt_ref.h"
#include "gc/space.h"
#include "gc/space_bitmap.h"
#include "stack_indirect_reference_table.h"
#include "stl_util.h"
#include "thread.h"
#include "UniquePtr.h"
#include "utils.h"
#include "well_known_classes.h"
namespace art {
static void ThrowNoClassDefFoundError(const char* fmt, ...)
__attribute__((__format__(__printf__, 1, 2)))
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
static void ThrowNoClassDefFoundError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
Thread::Current()->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args);
va_end(args);
}
static void ThrowClassFormatError(const char* fmt, ...)
__attribute__((__format__(__printf__, 1, 2)))
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
static void ThrowClassFormatError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
Thread::Current()->ThrowNewExceptionV("Ljava/lang/ClassFormatError;", fmt, args);
va_end(args);
}
static void ThrowLinkageError(const char* fmt, ...)
__attribute__((__format__(__printf__, 1, 2)))
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
static void ThrowLinkageError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
Thread::Current()->ThrowNewExceptionV("Ljava/lang/LinkageError;", fmt, args);
va_end(args);
}
static void ThrowNoSuchFieldError(const StringPiece& scope, Class* c, const StringPiece& type,
const StringPiece& name)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
ClassHelper kh(c);
std::ostringstream msg;
msg << "No " << scope << "field " << name << " of type " << type
<< " in class " << kh.GetDescriptor() << " or its superclasses";
std::string location(kh.GetLocation());
if (!location.empty()) {
msg << " (defined in " << location << ")";
}
Thread::Current()->ThrowNewException("Ljava/lang/NoSuchFieldError;", msg.str().c_str());
}
static void ThrowNullPointerException(const char* fmt, ...)
__attribute__((__format__(__printf__, 1, 2)))
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
static void ThrowNullPointerException(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
Thread::Current()->ThrowNewExceptionV("Ljava/lang/NullPointerException;", fmt, args);
va_end(args);
}
static void ThrowEarlierClassFailure(Class* c)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
// The class failed to initialize on a previous attempt, so we want to throw
// a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we
// failed in verification, in which case v2 5.4.1 says we need to re-throw
// the previous error.
if (!Runtime::Current()->IsCompiler()) { // Give info if this occurs at runtime.
LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c);
}
CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus();
if (c->GetVerifyErrorClass() != NULL) {
// TODO: change the verifier to store an _instance_, with a useful detail message?
ClassHelper ve_ch(c->GetVerifyErrorClass());
std::string error_descriptor(ve_ch.GetDescriptor());
Thread::Current()->ThrowNewException(error_descriptor.c_str(), PrettyDescriptor(c).c_str());
} else {
ThrowNoClassDefFoundError("%s", PrettyDescriptor(c).c_str());
}
}
static void WrapExceptionInInitializer()
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Thread* self = Thread::Current();
JNIEnv* env = self->GetJniEnv();
ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
CHECK(cause.get() != NULL);
env->ExceptionClear();
bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error);
env->Throw(cause.get());
// We only wrap non-Error exceptions; an Error can just be used as-is.
if (!is_error) {
self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", NULL);
}
}
static size_t Hash(const char* s) {
// This is the java.lang.String hashcode for convenience, not interoperability.
size_t hash = 0;
for (; *s != '\0'; ++s) {
hash = hash * 31 + *s;
}
return hash;
}
const char* ClassLinker::class_roots_descriptors_[] = {
"Ljava/lang/Class;",
"Ljava/lang/Object;",
"[Ljava/lang/Class;",
"[Ljava/lang/Object;",
"Ljava/lang/String;",
"Ljava/lang/DexCache;",
"Ljava/lang/ref/Reference;",
"Ljava/lang/reflect/Constructor;",
"Ljava/lang/reflect/Field;",
"Ljava/lang/reflect/AbstractMethod;",
"Ljava/lang/reflect/Method;",
"Ljava/lang/reflect/Proxy;",
"[Ljava/lang/String;",
"[Ljava/lang/reflect/AbstractMethod;",
"[Ljava/lang/reflect/Field;",
"[Ljava/lang/reflect/Method;",
"Ljava/lang/ClassLoader;",
"Ljava/lang/Throwable;",
"Ljava/lang/ClassNotFoundException;",
"Ljava/lang/StackTraceElement;",
"Z",
"B",
"C",
"D",
"F",
"I",
"J",
"S",
"V",
"[Z",
"[B",
"[C",
"[D",
"[F",
"[I",
"[J",
"[S",
"[Ljava/lang/StackTraceElement;",
};
ClassLinker* ClassLinker::CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
InternTable* intern_table) {
CHECK_NE(boot_class_path.size(), 0U);
UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table));
class_linker->InitFromCompiler(boot_class_path);
return class_linker.release();
}
ClassLinker* ClassLinker::CreateFromImage(InternTable* intern_table) {
UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table));
class_linker->InitFromImage();
return class_linker.release();
}
ClassLinker::ClassLinker(InternTable* intern_table)
// dex_lock_ is recursive as it may be used in stack dumping.
: dex_lock_("ClassLinker dex lock", kDefaultMutexLevel, true),
class_roots_(NULL),
array_iftable_(NULL),
init_done_(false),
is_dirty_(false),
intern_table_(intern_table) {
CHECK_EQ(arraysize(class_roots_descriptors_), size_t(kClassRootsMax));
}
void ClassLinker::InitFromCompiler(const std::vector<const DexFile*>& boot_class_path) {
VLOG(startup) << "ClassLinker::Init";
CHECK(Runtime::Current()->IsCompiler());
CHECK(!init_done_);
// java_lang_Class comes first, it's needed for AllocClass
Thread* self = Thread::Current();
Heap* heap = Runtime::Current()->GetHeap();
SirtRef<Class>
java_lang_Class(self, down_cast<Class*>(heap->AllocObject(self, NULL, sizeof(ClassClass))));
CHECK(java_lang_Class.get() != NULL);
java_lang_Class->SetClass(java_lang_Class.get());
java_lang_Class->SetClassSize(sizeof(ClassClass));
// AllocClass(Class*) can now be used
// Class[] is used for reflection support.
SirtRef<Class> class_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
class_array_class->SetComponentType(java_lang_Class.get());
// java_lang_Object comes next so that object_array_class can be created.
SirtRef<Class> java_lang_Object(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
CHECK(java_lang_Object.get() != NULL);
// backfill Object as the super class of Class.
java_lang_Class->SetSuperClass(java_lang_Object.get());
java_lang_Object->SetStatus(Class::kStatusLoaded);
// Object[] next to hold class roots.
SirtRef<Class> object_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
object_array_class->SetComponentType(java_lang_Object.get());
// Setup the char class to be used for char[].
SirtRef<Class> char_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
// Setup the char[] class to be used for String.
SirtRef<Class> char_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
char_array_class->SetComponentType(char_class.get());
CharArray::SetArrayClass(char_array_class.get());
// Setup String.
SirtRef<Class> java_lang_String(self, AllocClass(self, java_lang_Class.get(), sizeof(StringClass)));
String::SetClass(java_lang_String.get());
java_lang_String->SetObjectSize(sizeof(String));
java_lang_String->SetStatus(Class::kStatusResolved);
// Create storage for root classes, save away our work so far (requires descriptors).
class_roots_ = ObjectArray<Class>::Alloc(self, object_array_class.get(), kClassRootsMax);
CHECK(class_roots_ != NULL);
SetClassRoot(kJavaLangClass, java_lang_Class.get());
SetClassRoot(kJavaLangObject, java_lang_Object.get());
SetClassRoot(kClassArrayClass, class_array_class.get());
SetClassRoot(kObjectArrayClass, object_array_class.get());
SetClassRoot(kCharArrayClass, char_array_class.get());
SetClassRoot(kJavaLangString, java_lang_String.get());
// Setup the primitive type classes.
SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean));
SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte));
SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort));
SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt));
SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong));
SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat));
SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble));
SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid));
// Create array interface entries to populate once we can load system classes.
array_iftable_ = AllocIfTable(self, 2);
// Create int array type for AllocDexCache (done in AppendToBootClassPath).
SirtRef<Class> int_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt));
IntArray::SetArrayClass(int_array_class.get());
SetClassRoot(kIntArrayClass, int_array_class.get());
// now that these are registered, we can use AllocClass() and AllocObjectArray
// Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache.
SirtRef<Class>
java_lang_DexCache(self, AllocClass(self, java_lang_Class.get(), sizeof(DexCacheClass)));
SetClassRoot(kJavaLangDexCache, java_lang_DexCache.get());
java_lang_DexCache->SetObjectSize(sizeof(DexCacheClass));
java_lang_DexCache->SetStatus(Class::kStatusResolved);
// Constructor, Field, Method, and AbstractMethod are necessary so that FindClass can link members.
SirtRef<Class> java_lang_reflect_Field(self, AllocClass(self, java_lang_Class.get(),
sizeof(FieldClass)));
CHECK(java_lang_reflect_Field.get() != NULL);
java_lang_reflect_Field->SetObjectSize(sizeof(Field));
SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field.get());
java_lang_reflect_Field->SetStatus(Class::kStatusResolved);
Field::SetClass(java_lang_reflect_Field.get());
SirtRef<Class> java_lang_reflect_AbstractMethod(self, AllocClass(self, java_lang_Class.get(),
sizeof(MethodClass)));
CHECK(java_lang_reflect_AbstractMethod.get() != NULL);
java_lang_reflect_AbstractMethod->SetObjectSize(sizeof(AbstractMethod));
SetClassRoot(kJavaLangReflectAbstractMethod, java_lang_reflect_AbstractMethod.get());
java_lang_reflect_AbstractMethod->SetStatus(Class::kStatusResolved);
SirtRef<Class> java_lang_reflect_Constructor(self, AllocClass(self, java_lang_Class.get(),
sizeof(MethodClass)));
CHECK(java_lang_reflect_Constructor.get() != NULL);
java_lang_reflect_Constructor->SetObjectSize(sizeof(Constructor));
java_lang_reflect_Constructor->SetSuperClass(java_lang_reflect_AbstractMethod.get());
SetClassRoot(kJavaLangReflectConstructor, java_lang_reflect_Constructor.get());
java_lang_reflect_Constructor->SetStatus(Class::kStatusResolved);
SirtRef<Class> java_lang_reflect_Method(self, AllocClass(self, java_lang_Class.get(),
sizeof(MethodClass)));
CHECK(java_lang_reflect_Method.get() != NULL);
java_lang_reflect_Method->SetObjectSize(sizeof(Method));
java_lang_reflect_Method->SetSuperClass(java_lang_reflect_AbstractMethod.get());
SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method.get());
java_lang_reflect_Method->SetStatus(Class::kStatusResolved);
AbstractMethod::SetClasses(java_lang_reflect_Constructor.get(), java_lang_reflect_Method.get());
// Set up array classes for string, field, method
SirtRef<Class> object_array_string(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
object_array_string->SetComponentType(java_lang_String.get());
SetClassRoot(kJavaLangStringArrayClass, object_array_string.get());
SirtRef<Class> object_array_abstract_method(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
object_array_abstract_method->SetComponentType(java_lang_reflect_AbstractMethod.get());
SetClassRoot(kJavaLangReflectAbstractMethodArrayClass, object_array_abstract_method.get());
SirtRef<Class> object_array_field(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
object_array_field->SetComponentType(java_lang_reflect_Field.get());
SetClassRoot(kJavaLangReflectFieldArrayClass, object_array_field.get());
SirtRef<Class> object_array_method(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
object_array_method->SetComponentType(java_lang_reflect_Method.get());
SetClassRoot(kJavaLangReflectMethodArrayClass, object_array_method.get());
// Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create
// DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses
// these roots.
CHECK_NE(0U, boot_class_path.size());
for (size_t i = 0; i != boot_class_path.size(); ++i) {
const DexFile* dex_file = boot_class_path[i];
CHECK(dex_file != NULL);
AppendToBootClassPath(*dex_file);
}
// now we can use FindSystemClass
// run char class through InitializePrimitiveClass to finish init
InitializePrimitiveClass(char_class.get(), Primitive::kPrimChar);
SetClassRoot(kPrimitiveChar, char_class.get()); // needs descriptor
// Object, String and DexCache need to be rerun through FindSystemClass to finish init
java_lang_Object->SetStatus(Class::kStatusNotReady);
Class* Object_class = FindSystemClass("Ljava/lang/Object;");
CHECK_EQ(java_lang_Object.get(), Object_class);
CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object));
java_lang_String->SetStatus(Class::kStatusNotReady);
Class* String_class = FindSystemClass("Ljava/lang/String;");
CHECK_EQ(java_lang_String.get(), String_class);
CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String));
java_lang_DexCache->SetStatus(Class::kStatusNotReady);
Class* DexCache_class = FindSystemClass("Ljava/lang/DexCache;");
CHECK_EQ(java_lang_String.get(), String_class);
CHECK_EQ(java_lang_DexCache.get(), DexCache_class);
CHECK_EQ(java_lang_DexCache->GetObjectSize(), sizeof(DexCache));
// Setup the primitive array type classes - can't be done until Object has a vtable.
SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
Class* found_char_array_class = FindSystemClass("[C");
CHECK_EQ(char_array_class.get(), found_char_array_class);
SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Class* found_int_array_class = FindSystemClass("[I");
CHECK_EQ(int_array_class.get(), found_int_array_class);
SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
Class* found_class_array_class = FindSystemClass("[Ljava/lang/Class;");
CHECK_EQ(class_array_class.get(), found_class_array_class);
Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
CHECK_EQ(object_array_class.get(), found_object_array_class);
// Setup the single, global copy of "iftable".
Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
CHECK(java_lang_Cloneable != NULL);
Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
CHECK(java_io_Serializable != NULL);
// We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to
// crawl up and explicitly list all of the supers as well.
array_iftable_->SetInterface(0, java_lang_Cloneable);
array_iftable_->SetInterface(1, java_io_Serializable);
// Sanity check Class[] and Object[]'s interfaces.
ClassHelper kh(class_array_class.get(), this);
CHECK_EQ(java_lang_Cloneable, kh.GetDirectInterface(0));
CHECK_EQ(java_io_Serializable, kh.GetDirectInterface(1));
kh.ChangeClass(object_array_class.get());
CHECK_EQ(java_lang_Cloneable, kh.GetDirectInterface(0));
CHECK_EQ(java_io_Serializable, kh.GetDirectInterface(1));
// Run Class, Constructor, Field, and Method through FindSystemClass. This initializes their
// dex_cache_ fields and register them in classes_.
Class* Class_class = FindSystemClass("Ljava/lang/Class;");
CHECK_EQ(java_lang_Class.get(), Class_class);
java_lang_reflect_AbstractMethod->SetStatus(Class::kStatusNotReady);
Class* Abstract_method_class = FindSystemClass("Ljava/lang/reflect/AbstractMethod;");
CHECK_EQ(java_lang_reflect_AbstractMethod.get(), Abstract_method_class);
// Method extends AbstractMethod so must reset after.
java_lang_reflect_Method->SetStatus(Class::kStatusNotReady);
Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
CHECK_EQ(java_lang_reflect_Method.get(), Method_class);
// Constructor extends AbstractMethod so must reset after.
java_lang_reflect_Constructor->SetStatus(Class::kStatusNotReady);
Class* Constructor_class = FindSystemClass("Ljava/lang/reflect/Constructor;");
CHECK_EQ(java_lang_reflect_Constructor.get(), Constructor_class);
java_lang_reflect_Field->SetStatus(Class::kStatusNotReady);
Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
CHECK_EQ(java_lang_reflect_Field.get(), Field_class);
Class* String_array_class = FindSystemClass(class_roots_descriptors_[kJavaLangStringArrayClass]);
CHECK_EQ(object_array_string.get(), String_array_class);
Class* Abstract_method_array_class =
FindSystemClass(class_roots_descriptors_[kJavaLangReflectAbstractMethodArrayClass]);
CHECK_EQ(object_array_abstract_method.get(), Abstract_method_array_class);
Class* Field_array_class = FindSystemClass(class_roots_descriptors_[kJavaLangReflectFieldArrayClass]);
CHECK_EQ(object_array_field.get(), Field_array_class);
Class* Method_array_class =
FindSystemClass(class_roots_descriptors_[kJavaLangReflectMethodArrayClass]);
CHECK_EQ(object_array_method.get(), Method_array_class);
// End of special init trickery, subsequent classes may be loaded via FindSystemClass.
// Create java.lang.reflect.Proxy root.
Class* java_lang_reflect_Proxy = FindSystemClass("Ljava/lang/reflect/Proxy;");
SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy);
// java.lang.ref classes need to be specially flagged, but otherwise are normal classes
Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;");
SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference);
Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;");
java_lang_ref_FinalizerReference->SetAccessFlags(
java_lang_ref_FinalizerReference->GetAccessFlags() |
kAccClassIsReference | kAccClassIsFinalizerReference);
Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;");
java_lang_ref_PhantomReference->SetAccessFlags(
java_lang_ref_PhantomReference->GetAccessFlags() |
kAccClassIsReference | kAccClassIsPhantomReference);
Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;");
java_lang_ref_SoftReference->SetAccessFlags(
java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference);
Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;");
java_lang_ref_WeakReference->SetAccessFlags(
java_lang_ref_WeakReference->GetAccessFlags() |
kAccClassIsReference | kAccClassIsWeakReference);
// Setup the ClassLoader, verifying the object_size_.
Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader));
SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
// Set up java.lang.Throwable, java.lang.ClassNotFoundException, and
// java.lang.StackTraceElement as a convenience.
SetClassRoot(kJavaLangThrowable, FindSystemClass("Ljava/lang/Throwable;"));
Throwable::SetClass(GetClassRoot(kJavaLangThrowable));
SetClassRoot(kJavaLangClassNotFoundException, FindSystemClass("Ljava/lang/ClassNotFoundException;"));
SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;"));
SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;"));
StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
FinishInit();
VLOG(startup) << "ClassLinker::InitFromCompiler exiting";
}
void ClassLinker::FinishInit() {
VLOG(startup) << "ClassLinker::FinishInit entering";
// Let the heap know some key offsets into java.lang.ref instances
// Note: we hard code the field indexes here rather than using FindInstanceField
// as the types of the field can't be resolved prior to the runtime being
// fully initialized
Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference);
Class* java_lang_ref_ReferenceQueue = FindSystemClass("Ljava/lang/ref/ReferenceQueue;");
Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;");
const DexFile& java_lang_dex = *java_lang_ref_Reference->GetDexCache()->GetDexFile();
Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
FieldHelper fh(pendingNext, this);
CHECK_STREQ(fh.GetName(), "pendingNext");
CHECK_EQ(java_lang_dex.GetFieldId(pendingNext->GetDexFieldIndex()).type_idx_,
java_lang_ref_Reference->GetDexTypeIndex());
Field* queue = java_lang_ref_Reference->GetInstanceField(1);
fh.ChangeField(queue);
CHECK_STREQ(fh.GetName(), "queue");
CHECK_EQ(java_lang_dex.GetFieldId(queue->GetDexFieldIndex()).type_idx_,
java_lang_ref_ReferenceQueue->GetDexTypeIndex());
Field* queueNext = java_lang_ref_Reference->GetInstanceField(2);
fh.ChangeField(queueNext);
CHECK_STREQ(fh.GetName(), "queueNext");
CHECK_EQ(java_lang_dex.GetFieldId(queueNext->GetDexFieldIndex()).type_idx_,
java_lang_ref_Reference->GetDexTypeIndex());
Field* referent = java_lang_ref_Reference->GetInstanceField(3);
fh.ChangeField(referent);
CHECK_STREQ(fh.GetName(), "referent");
CHECK_EQ(java_lang_dex.GetFieldId(referent->GetDexFieldIndex()).type_idx_,
GetClassRoot(kJavaLangObject)->GetDexTypeIndex());
Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
fh.ChangeField(zombie);
CHECK_STREQ(fh.GetName(), "zombie");
CHECK_EQ(java_lang_dex.GetFieldId(zombie->GetDexFieldIndex()).type_idx_,
GetClassRoot(kJavaLangObject)->GetDexTypeIndex());
Heap* heap = Runtime::Current()->GetHeap();
heap->SetReferenceOffsets(referent->GetOffset(),
queue->GetOffset(),
queueNext->GetOffset(),
pendingNext->GetOffset(),
zombie->GetOffset());
// ensure all class_roots_ are initialized
for (size_t i = 0; i < kClassRootsMax; i++) {
ClassRoot class_root = static_cast<ClassRoot>(i);
Class* klass = GetClassRoot(class_root);
CHECK(klass != NULL);
DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL);
// note SetClassRoot does additional validation.
// if possible add new checks there to catch errors early
}
CHECK(array_iftable_ != NULL);
// disable the slow paths in FindClass and CreatePrimitiveClass now
// that Object, Class, and Object[] are setup
init_done_ = true;
VLOG(startup) << "ClassLinker::FinishInit exiting";
}
void ClassLinker::RunRootClinits() {
Thread* self = Thread::Current();
for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) {
Class* c = GetClassRoot(ClassRoot(i));
if (!c->IsArrayClass() && !c->IsPrimitive()) {
EnsureInitialized(GetClassRoot(ClassRoot(i)), true, true);
self->AssertNoPendingException();
}
}
}
bool ClassLinker::GenerateOatFile(const std::string& dex_filename,
int oat_fd,
const std::string& oat_cache_filename) {
std::string dex2oat_string(GetAndroidRoot());
dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
const char* dex2oat = dex2oat_string.c_str();
const char* class_path = Runtime::Current()->GetClassPathString().c_str();
Heap* heap = Runtime::Current()->GetHeap();
std::string boot_image_option_string("--boot-image=");
boot_image_option_string += heap->GetImageSpace()->GetImageFilename();
const char* boot_image_option = boot_image_option_string.c_str();
std::string dex_file_option_string("--dex-file=");
dex_file_option_string += dex_filename;
const char* dex_file_option = dex_file_option_string.c_str();
std::string oat_fd_option_string("--oat-fd=");
StringAppendF(&oat_fd_option_string, "%d", oat_fd);
const char* oat_fd_option = oat_fd_option_string.c_str();
std::string oat_location_option_string("--oat-location=");
oat_location_option_string += oat_cache_filename;
const char* oat_location_option = oat_location_option_string.c_str();
// fork and exec dex2oat
pid_t pid = fork();
if (pid == 0) {
// no allocation allowed between fork and exec
// change process groups, so we don't get reaped by ProcessManager
setpgid(0, 0);
VLOG(class_linker) << dex2oat
<< " --runtime-arg -Xms64m"
<< " --runtime-arg -Xmx64m"
<< " --runtime-arg -classpath"
<< " --runtime-arg " << class_path
<< " " << boot_image_option
<< " " << dex_file_option
<< " " << oat_fd_option
<< " " << oat_location_option;
execl(dex2oat, dex2oat,
"--runtime-arg", "-Xms64m",
"--runtime-arg", "-Xmx64m",
"--runtime-arg", "-classpath",
"--runtime-arg", class_path,
boot_image_option,
dex_file_option,
oat_fd_option,
oat_location_option,
NULL);
PLOG(FATAL) << "execl(" << dex2oat << ") failed";
return false;
} else {
// wait for dex2oat to finish
int status;
pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
if (got_pid != pid) {
PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid;
return false;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
LOG(ERROR) << dex2oat << " failed with dex-file=" << dex_filename;
return false;
}
}
return true;
}
void ClassLinker::RegisterOatFile(const OatFile& oat_file) {
MutexLock mu(Thread::Current(), dex_lock_);
RegisterOatFileLocked(oat_file);
}
void ClassLinker::RegisterOatFileLocked(const OatFile& oat_file) {
dex_lock_.AssertHeld(Thread::Current());
oat_files_.push_back(&oat_file);
}
OatFile* ClassLinker::OpenOat(const ImageSpace* space) {
MutexLock mu(Thread::Current(), dex_lock_);
const Runtime* runtime = Runtime::Current();
const ImageHeader& image_header = space->GetImageHeader();
// Grab location but don't use Object::AsString as we haven't yet initialized the roots to
// check the down cast
String* oat_location = down_cast<String*>(image_header.GetImageRoot(ImageHeader::kOatLocation));
std::string oat_filename;
oat_filename += runtime->GetHostPrefix();
oat_filename += oat_location->ToModifiedUtf8();
runtime->GetHeap()->UnReserveOatFileAddressRange();
OatFile* oat_file = OatFile::Open(oat_filename, oat_filename,
image_header.GetOatBegin(),
OatFile::kRelocNone);
VLOG(startup) << "ClassLinker::OpenOat entering oat_filename=" << oat_filename;
if (oat_file == NULL) {
LOG(ERROR) << "Failed to open oat file " << oat_filename << " referenced from image.";
return NULL;
}
uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
uint32_t image_oat_checksum = image_header.GetOatChecksum();
if (oat_checksum != image_oat_checksum) {
LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum
<< " to expected oat checksum " << std::hex << oat_checksum
<< " in image";
return NULL;
}
RegisterOatFileLocked(*oat_file);
VLOG(startup) << "ClassLinker::OpenOat exiting";
return oat_file;
}
const OatFile* ClassLinker::FindOpenedOatFileForDexFile(const DexFile& dex_file) {
MutexLock mu(Thread::Current(), dex_lock_);
return FindOpenedOatFileFromDexLocation(dex_file.GetLocation());
}
const OatFile* ClassLinker::FindOpenedOatFileFromDexLocation(const std::string& dex_location) {
for (size_t i = 0; i < oat_files_.size(); i++) {
const OatFile* oat_file = oat_files_[i];
DCHECK(oat_file != NULL);
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, false);
if (oat_dex_file != NULL) {
return oat_file;
}
}
return NULL;
}
static const DexFile* FindDexFileInOatLocation(const std::string& dex_location,
uint32_t dex_location_checksum,
const std::string& oat_location) {
UniquePtr<OatFile> oat_file(
OatFile::Open(oat_location, oat_location, NULL, OatFile::kRelocAll));
if (oat_file.get() == NULL) {
return NULL;
}
Runtime* runtime = Runtime::Current();
const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader();
if (oat_file->GetOatHeader().GetImageFileLocationChecksum() != image_header.GetOatChecksum()) {
return NULL;
}
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
if (oat_dex_file == NULL) {
return NULL;
}
if (oat_dex_file->GetDexFileLocationChecksum() != dex_location_checksum) {
return NULL;
}
runtime->GetClassLinker()->RegisterOatFile(*oat_file.release());
return oat_dex_file->OpenDexFile();
}
const DexFile* ClassLinker::FindOrCreateOatFileForDexLocation(const std::string& dex_location,
const std::string& oat_location) {
MutexLock mu(Thread::Current(), dex_lock_);
return FindOrCreateOatFileForDexLocationLocked(dex_location, oat_location);
}
const DexFile* ClassLinker::FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
const std::string& oat_location) {
uint32_t dex_location_checksum;
if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
LOG(ERROR) << "Failed to compute checksum '" << dex_location << "'";
return NULL;
}
// Check if we already have an up-to-date output file
const DexFile* dex_file = FindDexFileInOatLocation(dex_location,
dex_location_checksum,
oat_location);
if (dex_file != NULL) {
return dex_file;
}
// Generate the output oat file for the dex file
UniquePtr<File> file(OS::OpenFile(oat_location.c_str(), true));
if (file.get() == NULL) {
LOG(ERROR) << "Failed to create oat file: " << oat_location;
return NULL;
}
if (!GenerateOatFile(dex_location, file->Fd(), oat_location)) {
LOG(ERROR) << "Failed to generate oat file: " << oat_location;
return NULL;
}
// Open the oat from file descriptor we passed to GenerateOatFile
if (lseek(file->Fd(), 0, SEEK_SET) != 0) {
LOG(ERROR) << "Failed to seek to start of generated oat file: " << oat_location;
return NULL;
}
const OatFile* oat_file =
OatFile::Open(*file.get(), oat_location, NULL, OatFile::kRelocAll);
if (oat_file == NULL) {
LOG(ERROR) << "Failed to open generated oat file: " << oat_location;
return NULL;
}
RegisterOatFileLocked(*oat_file);
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
if (oat_dex_file == NULL) {
LOG(ERROR) << "Failed to find dex file in generated oat file: " << oat_location;
return NULL;
}
return oat_dex_file->OpenDexFile();
}
bool ClassLinker::VerifyOatFileChecksums(const OatFile* oat_file,
const std::string& dex_location,
uint32_t dex_location_checksum) {
Runtime* runtime = Runtime::Current();
const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader();
uint32_t image_checksum = image_header.GetOatChecksum();
bool image_check = (oat_file->GetOatHeader().GetImageFileLocationChecksum() == image_checksum);
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
if (oat_dex_file == NULL) {
LOG(ERROR) << ".oat file " << oat_file->GetLocation()
<< " does not contain contents for " << dex_location;
std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
for (size_t i = 0; i < oat_dex_files.size(); i++) {
const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
LOG(ERROR) << ".oat file " << oat_file->GetLocation()
<< " contains contents for " << oat_dex_file->GetDexFileLocation();
}
return false;
}
bool dex_check = (dex_location_checksum == oat_dex_file->GetDexFileLocationChecksum());
if (image_check && dex_check) {
return true;
}
if (!image_check) {
std::string image_file(image_header.GetImageRoot(
ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8());
LOG(WARNING) << ".oat file " << oat_file->GetLocation()
<< " checksum ( " << std::hex << oat_dex_file->GetDexFileLocationChecksum()
<< ") mismatch with " << image_file
<< " (" << std::hex << image_checksum << ")";
}
if (!dex_check) {
LOG(WARNING) << ".oat file " << oat_file->GetLocation()
<< " checksum ( " << std::hex << oat_dex_file->GetDexFileLocationChecksum()
<< ") mismatch with " << dex_location
<< " (" << std::hex << dex_location_checksum << ")";
}
return false;
}
const DexFile* ClassLinker::VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
const std::string& dex_location,
uint32_t dex_location_checksum) {
bool verified = VerifyOatFileChecksums(oat_file, dex_location, dex_location_checksum);
if (!verified) {
return NULL;
}
RegisterOatFileLocked(*oat_file);
return oat_file->GetOatDexFile(dex_location)->OpenDexFile();
}
const DexFile* ClassLinker::FindDexFileInOatFileFromDexLocation(const std::string& dex_location) {
MutexLock mu(Thread::Current(), dex_lock_);
const OatFile* open_oat_file = FindOpenedOatFileFromDexLocation(dex_location);
if (open_oat_file != NULL) {
return open_oat_file->GetOatDexFile(dex_location)->OpenDexFile();
}
// Look for an existing file next to dex. for example, for
// /foo/bar/baz.jar, look for /foo/bar/baz.jar.oat.
std::string oat_filename(OatFile::DexFilenameToOatFilename(dex_location));
const OatFile* oat_file = FindOatFileFromOatLocationLocked(oat_filename);
if (oat_file != NULL) {
uint32_t dex_location_checksum;
if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
// If no classes.dex found in dex_location, it has been stripped, assume oat is up-to-date.
// This is the common case in user builds for jar's and apk's in the /system directory.
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
CHECK(oat_dex_file != NULL) << oat_filename << " " << dex_location;
RegisterOatFileLocked(*oat_file);
return oat_dex_file->OpenDexFile();
}
const DexFile* dex_file = VerifyAndOpenDexFileFromOatFile(oat_file,
dex_location,
dex_location_checksum);
if (dex_file != NULL) {
return dex_file;
}
}
// Look for an existing file in the art-cache, validating the result if found
// not found in /foo/bar/baz.oat? try /data/art-cache/foo@bar@baz.oat
std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
oat_file = FindOatFileFromOatLocationLocked(cache_location);
if (oat_file != NULL) {
uint32_t dex_location_checksum;
if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
LOG(WARNING) << "Failed to compute checksum: " << dex_location;
return NULL;
}
const DexFile* dex_file = VerifyAndOpenDexFileFromOatFile(oat_file,
dex_location,
dex_location_checksum);
if (dex_file != NULL) {
return dex_file;
}
if (TEMP_FAILURE_RETRY(unlink(oat_file->GetLocation().c_str())) != 0) {
PLOG(FATAL) << "Failed to remove obsolete .oat file " << oat_file->GetLocation();
}
}
LOG(INFO) << "Failed to open oat file from " << oat_filename << " or " << cache_location << ".";
// Try to generate oat file if it wasn't found or was obsolete.
std::string oat_cache_filename(GetArtCacheFilenameOrDie(oat_filename));
return FindOrCreateOatFileForDexLocationLocked(dex_location, oat_cache_filename);
}
const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) {
for (size_t i = 0; i < oat_files_.size(); i++) {
const OatFile* oat_file = oat_files_[i];
DCHECK(oat_file != NULL);
if (oat_file->GetLocation() == oat_location) {
return oat_file;
}
}
return NULL;
}
const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location) {
MutexLock mu(Thread::Current(), dex_lock_);
return FindOatFileFromOatLocationLocked(oat_location);
}
const OatFile* ClassLinker::FindOatFileFromOatLocationLocked(const std::string& oat_location) {
const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location);
if (oat_file != NULL) {
return oat_file;
}
oat_file = OatFile::Open(oat_location, oat_location, NULL,
OatFile::kRelocAll);
if (oat_file == NULL) {
return NULL;
}
CHECK(oat_file != NULL) << oat_location;
return oat_file;
}
void ClassLinker::InitFromImage() {
VLOG(startup) << "ClassLinker::InitFromImage entering";
CHECK(!init_done_);
Heap* heap = Runtime::Current()->GetHeap();
ImageSpace* space = heap->GetImageSpace();
OatFile* oat_file = OpenOat(space);
CHECK(oat_file != NULL) << "Failed to open oat file for image";
CHECK_EQ(oat_file->GetOatHeader().GetImageFileLocationChecksum(), 0U);
CHECK(oat_file->GetOatHeader().GetImageFileLocation().empty());
Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
ObjectArray<DexCache>* dex_caches = dex_caches_object->AsObjectArray<DexCache>();
// Special case of setting up the String class early so that we can test arbitrary objects
// as being Strings or not
Class* java_lang_String = space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)
->AsObjectArray<Class>()->Get(kJavaLangString);
String::SetClass(java_lang_String);
CHECK_EQ(oat_file->GetOatHeader().GetDexFileCount(),
static_cast<uint32_t>(dex_caches->GetLength()));
Thread* self = Thread::Current();
for (int i = 0; i < dex_caches->GetLength(); i++) {
SirtRef<DexCache> dex_cache(self, dex_caches->Get(i));
const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8());
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file_location);
const DexFile* dex_file = oat_dex_file->OpenDexFile();
if (dex_file == NULL) {
LOG(FATAL) << "Failed to open dex file " << dex_file_location
<< " from within oat file " << oat_file->GetLocation();
}
CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
AppendToBootClassPath(*dex_file, dex_cache);
}
// reinit clases_ table
{
ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
heap->FlushAllocStack();
heap->GetLiveBitmap()->Walk(InitFromImageCallback, this);
}
// reinit class_roots_
Object* class_roots_object =
heap->GetImageSpace()->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots);
class_roots_ = class_roots_object->AsObjectArray<Class>();
// reinit array_iftable_ from any array class instance, they should be ==
array_iftable_ = GetClassRoot(kObjectArrayClass)->GetIfTable();
DCHECK(array_iftable_ == GetClassRoot(kBooleanArrayClass)->GetIfTable());
// String class root was set above
Field::SetClass(GetClassRoot(kJavaLangReflectField));
AbstractMethod::SetClasses(GetClassRoot(kJavaLangReflectConstructor),
GetClassRoot(kJavaLangReflectMethod));
BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Throwable::SetClass(GetClassRoot(kJavaLangThrowable));
StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
FinishInit();
VLOG(startup) << "ClassLinker::InitFromImage exiting";
}
void ClassLinker::InitFromImageCallback(Object* obj, void* arg) {
DCHECK(obj != NULL);
DCHECK(arg != NULL);
ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg);
if (obj->GetClass()->IsStringClass()) {
class_linker->intern_table_->RegisterStrong(obj->AsString());
return;
}
if (obj->IsClass()) {
// restore class to ClassLinker::classes_ table
Class* klass = obj->AsClass();
ClassHelper kh(klass, class_linker);
Class* existing = class_linker->InsertClass(kh.GetDescriptor(), klass, true);
DCHECK(existing == NULL) << kh.GetDescriptor();
return;
}
}
// Keep in sync with InitCallback. Anything we visit, we need to
// reinit references to when reinitializing a ClassLinker from a
// mapped image.
void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
visitor(class_roots_, arg);
Thread* self = Thread::Current();
{
MutexLock mu(self, dex_lock_);
for (size_t i = 0; i < dex_caches_.size(); i++) {
visitor(dex_caches_[i], arg);
}
}
{
MutexLock mu(self, *Locks::classlinker_classes_lock_);
typedef Table::const_iterator It; // TODO: C++0x auto
for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
visitor(it->second, arg);
}
// We deliberately ignore the class roots in the image since we
// handle image roots by using the MS/CMS rescanning of dirty cards.
}
visitor(array_iftable_, arg);
is_dirty_ = false;
}
void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) const {
MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
typedef Table::const_iterator It; // TODO: C++0x auto
for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
if (!visitor(it->second, arg)) {
return;
}
}
for (It it = image_classes_.begin(), end = image_classes_.end(); it != end; ++it) {
if (!visitor(it->second, arg)) {
return;
}
}
}
static bool GetClassesVisitor(Class* c, void* arg) {
std::set<Class*>* classes = reinterpret_cast<std::set<Class*>*>(arg);
classes->insert(c);
return true;
}
void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) const {
std::set<Class*> classes;
VisitClasses(GetClassesVisitor, &classes);
typedef std::set<Class*>::const_iterator It; // TODO: C++0x auto
for (It it = classes.begin(), end = classes.end(); it != end; ++it) {
if (!visitor(*it, arg)) {
return;
}
}
}
ClassLinker::~ClassLinker() {
String::ResetClass();
Field::ResetClass();
AbstractMethod::ResetClasses();
BooleanArray::ResetArrayClass();
ByteArray::ResetArrayClass();
CharArray::ResetArrayClass();
DoubleArray::ResetArrayClass();
FloatArray::ResetArrayClass();
IntArray::ResetArrayClass();
LongArray::ResetArrayClass();
ShortArray::ResetArrayClass();
Throwable::ResetClass();
StackTraceElement::ResetClass();
STLDeleteElements(&boot_class_path_);
STLDeleteElements(&oat_files_);
}
DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) {
Heap* heap = Runtime::Current()->GetHeap();
Class* dex_cache_class = GetClassRoot(kJavaLangDexCache);
SirtRef<DexCache> dex_cache(self,
down_cast<DexCache*>(heap->AllocObject(self, dex_cache_class,
dex_cache_class->GetObjectSize())));
if (dex_cache.get() == NULL) {
return NULL;
}
SirtRef<String> location(self, intern_table_->InternStrong(dex_file.GetLocation().c_str()));
if (location.get() == NULL) {
return NULL;
}
SirtRef<ObjectArray<String> > strings(self, AllocStringArray(self, dex_file.NumStringIds()));
if (strings.get() == NULL) {
return NULL;
}
SirtRef<ObjectArray<Class> > types(self, AllocClassArray(self, dex_file.NumTypeIds()));
if (types.get() == NULL) {
return NULL;
}
SirtRef<ObjectArray<AbstractMethod> >
methods(self, AllocAbstractMethodArray(self, dex_file.NumMethodIds()));
if (methods.get() == NULL) {
return NULL;
}
SirtRef<ObjectArray<Field> > fields(self, AllocFieldArray(self, dex_file.NumFieldIds()));
if (fields.get() == NULL) {
return NULL;
}
SirtRef<ObjectArray<StaticStorageBase> >
initialized_static_storage(self,
AllocObjectArray<StaticStorageBase>(self, dex_file.NumTypeIds()));
if (initialized_static_storage.get() == NULL) {
return NULL;
}
dex_cache->Init(&dex_file,
location.get(),
strings.get(),
types.get(),
methods.get(),
fields.get(),
initialized_static_storage.get());
return dex_cache.get();
}
Class* ClassLinker::AllocClass(Thread* self, Class* java_lang_Class, size_t class_size) {
DCHECK_GE(class_size, sizeof(Class));
Heap* heap = Runtime::Current()->GetHeap();
SirtRef<Class> klass(self,
heap->AllocObject(self, java_lang_Class, class_size)->AsClass());
klass->SetPrimitiveType(Primitive::kPrimNot); // default to not being primitive
klass->SetClassSize(class_size);
return klass.get();
}
Class* ClassLinker::AllocClass(Thread* self, size_t class_size) {
return AllocClass(self, GetClassRoot(kJavaLangClass), class_size);
}
Field* ClassLinker::AllocField(Thread* self) {
return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject(self));
}
Method* ClassLinker::AllocMethod(Thread* self) {
return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject(self));
}
Constructor* ClassLinker::AllocConstructor(Thread* self) {
return down_cast<Constructor*>(GetClassRoot(kJavaLangReflectConstructor)->AllocObject(self));
}
ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(Thread* self,
size_t length) {
return ObjectArray<StackTraceElement>::Alloc(self,
GetClassRoot(kJavaLangStackTraceElementArrayClass),
length);
}
static Class* EnsureResolved(Thread* self, Class* klass)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
DCHECK(klass != NULL);
// Wait for the class if it has not already been linked.
if (!klass->IsResolved() && !klass->IsErroneous()) {
ObjectLock lock(self, klass);
// Check for circular dependencies between classes.
if (!klass->IsResolved() && klass->GetClinitThreadId() == self->GetTid()) {
self->ThrowNewException("Ljava/lang/ClassCircularityError;",
PrettyDescriptor(klass).c_str());
klass->SetStatus(Class::kStatusError);
return NULL;
}
// Wait for the pending initialization to complete.
while (!klass->IsResolved() && !klass->IsErroneous()) {
lock.Wait();
}
}
if (klass->IsErroneous()) {
ThrowEarlierClassFailure(klass);
return NULL;
}
// Return the loaded class. No exceptions should be pending.
CHECK(klass->IsResolved()) << PrettyClass(klass);
CHECK(!self->IsExceptionPending())
<< PrettyClass(klass) << " " << PrettyTypeOf(self->GetException()) << "\n"
<< self->GetException()->Dump();
return klass;
}
Class* ClassLinker::FindSystemClass(const char* descriptor) {
return FindClass(descriptor, NULL);
}
Class* ClassLinker::FindClass(const char* descriptor, ClassLoader* class_loader) {
DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
Thread* self = Thread::Current();
DCHECK(self != NULL);
self->AssertNoPendingException();
if (descriptor[1] == '\0') {
// only the descriptors of primitive types should be 1 character long, also avoid class lookup
// for primitive classes that aren't backed by dex files.
return FindPrimitiveClass(descriptor[0]);
}
// Find the class in the loaded classes table.
Class* klass = LookupClass(descriptor, class_loader);
if (klass != NULL) {
return EnsureResolved(self, klass);
}
// Class is not yet loaded.
if (descriptor[0] == '[') {
return CreateArrayClass(descriptor, class_loader);
} else if (class_loader == NULL) {
DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_);
if (pair.second != NULL) {
return DefineClass(descriptor, NULL, *pair.first, *pair.second);
}
} else if (Runtime::Current()->UseCompileTimeClassPath()) {
// first try the boot class path
Class* system_class = FindSystemClass(descriptor);
if (system_class != NULL) {
return system_class;
}
CHECK(self->IsExceptionPending());
self->ClearException();
// next try the compile time class path
const std::vector<const DexFile*>* class_path;
{
ScopedObjectAccessUnchecked soa(Thread::Current());
ScopedLocalRef<jobject> jclass_loader(soa.Env(), soa.AddLocalReference<jobject>(class_loader));
class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get());
}
DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, *class_path);
if (pair.second != NULL) {
return DefineClass(descriptor, class_loader, *pair.first, *pair.second);
}
} else {
ScopedObjectAccessUnchecked soa(self->GetJniEnv());
ScopedLocalRef<jobject> class_loader_object(soa.Env(),
soa.AddLocalReference<jobject>(class_loader));
std::string class_name_string(DescriptorToDot(descriptor));
ScopedLocalRef<jobject> result(soa.Env(), NULL);
{
ScopedThreadStateChange tsc(self, kNative);
ScopedLocalRef<jobject> class_name_object(soa.Env(),
soa.Env()->NewStringUTF(class_name_string.c_str()));
if (class_name_object.get() == NULL) {
return NULL;
}
CHECK(class_loader_object.get() != NULL);
result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(),
WellKnownClasses::java_lang_ClassLoader_loadClass,
class_name_object.get()));
}
if (soa.Env()->ExceptionCheck()) {
// If the ClassLoader threw, pass that exception up.
return NULL;
} else if (result.get() == NULL) {
// broken loader - throw NPE to be compatible with Dalvik
ThrowNullPointerException("ClassLoader.loadClass returned null for %s",
class_name_string.c_str());
return NULL;
} else {
// success, return Class*
return soa.Decode<Class*>(result.get());
}
}
ThrowNoClassDefFoundError("Class %s not found", PrintableString(descriptor).c_str());
return NULL;
}
Class* ClassLinker::DefineClass(const StringPiece& descriptor,
ClassLoader* class_loader,
const DexFile& dex_file,
const DexFile::ClassDef& dex_class_def) {
Thread* self = Thread::Current();
SirtRef<Class> klass(self, NULL);
// Load the class from the dex file.
if (!init_done_) {
// finish up init of hand crafted class_roots_
if (descriptor == "Ljava/lang/Object;") {
klass.reset(GetClassRoot(kJavaLangObject));
} else if (descriptor == "Ljava/lang/Class;") {
klass.reset(GetClassRoot(kJavaLangClass));
} else if (descriptor == "Ljava/lang/String;") {
klass.reset(GetClassRoot(kJavaLangString));
} else if (descriptor == "Ljava/lang/DexCache;") {
klass.reset(GetClassRoot(kJavaLangDexCache));
} else if (descriptor == "Ljava/lang/reflect/Field;") {
klass.reset(GetClassRoot(kJavaLangReflectField));
} else if (descriptor == "Ljava/lang/reflect/AbstractMethod;") {
klass.reset(GetClassRoot(kJavaLangReflectAbstractMethod));
} else if (descriptor == "Ljava/lang/reflect/Constructor;") {
klass.reset(GetClassRoot(kJavaLangReflectConstructor));
} else if (descriptor == "Ljava/lang/reflect/Method;") {
klass.reset(GetClassRoot(kJavaLangReflectMethod));
} else {
klass.reset(AllocClass(self, SizeOfClass(dex_file, dex_class_def)));
}
} else {
klass.reset(AllocClass(self, SizeOfClass(dex_file, dex_class_def)));
}
klass->SetDexCache(FindDexCache(dex_file));
LoadClass(dex_file, dex_class_def, klass, class_loader);
// Check for a pending exception during load
if (self->IsExceptionPending()) {
klass->SetStatus(Class::kStatusError);
return NULL;
}
ObjectLock lock(self, klass.get());
klass->SetClinitThreadId(self->GetTid());
// Add the newly loaded class to the loaded classes table.
SirtRef<Class> existing(self, InsertClass(descriptor, klass.get(), false));
if (existing.get() != NULL) {
// We failed to insert because we raced with another thread.
return EnsureResolved(self, existing.get());
}
// Finish loading (if necessary) by finding parents
CHECK(!klass->IsLoaded());
if (!LoadSuperAndInterfaces(klass, dex_file)) {
// Loading failed.
klass->SetStatus(Class::kStatusError);
lock.NotifyAll();
return NULL;
}
CHECK(klass->IsLoaded());
// Link the class (if necessary)
CHECK(!klass->IsResolved());
if (!LinkClass(klass, NULL)) {
// Linking failed.
klass->SetStatus(Class::kStatusError);
lock.NotifyAll();
return NULL;
}
CHECK(klass->IsResolved());
/*
* We send CLASS_PREPARE events to the debugger from here. The
* definition of "preparation" is creating the static fields for a
* class and initializing them to the standard default values, but not
* executing any code (that comes later, during "initialization").
*
* We did the static preparation in LinkClass.
*
* The class has been prepared and resolved but possibly not yet verified
* at this point.
*/
Dbg::PostClassPrepare(klass.get());
return klass.get();
}
// Precomputes size that will be needed for Class, matching LinkStaticFields
size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
const DexFile::ClassDef& dex_class_def) {
const byte* class_data = dex_file.GetClassData(dex_class_def);
size_t num_ref = 0;
size_t num_32 = 0;
size_t num_64 = 0;
if (class_data != NULL) {
for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) {
const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex());
const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id);
char c = descriptor[0];
if (c == 'L' || c == '[') {
num_ref++;
} else if (c == 'J' || c == 'D') {
num_64++;
} else {
num_32++;
}
}
}
// start with generic class data
size_t size = sizeof(Class);
// follow with reference fields which must be contiguous at start
size += (num_ref * sizeof(uint32_t));
// if there are 64-bit fields to add, make sure they are aligned
if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
if (num_32 != 0) {
// use an available 32-bit field for padding
num_32--;
}
size += sizeof(uint32_t); // either way, we are adding a word
DCHECK_EQ(size, RoundUp(size, 8));
}
// tack on any 64-bit fields now that alignment is assured
size += (num_64 * sizeof(uint64_t));
// tack on any remaining 32-bit fields
size += (num_32 * sizeof(uint32_t));
return size;
}
const OatFile::OatClass* ClassLinker::GetOatClass(const DexFile& dex_file, const char* descriptor) {
DCHECK(descriptor != NULL);
const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file);
CHECK(oat_file != NULL) << dex_file.GetLocation() << " " << descriptor;
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
CHECK(oat_dex_file != NULL) << dex_file.GetLocation() << " " << descriptor;
uint32_t class_def_index;
bool found = dex_file.FindClassDefIndex(descriptor, class_def_index);
CHECK(found) << dex_file.GetLocation() << " " << descriptor;
const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
CHECK(oat_class != NULL) << dex_file.GetLocation() << " " << descriptor;
return oat_class;
}
const OatFile::OatMethod ClassLinker::GetOatMethodFor(const AbstractMethod* method) {
// Although we overwrite the trampoline of non-static methods, we may get here via the resolution
// method for direct methods (or virtual methods made direct).
Class* declaring_class = method->GetDeclaringClass();
size_t oat_method_index;
if (method->IsStatic() || method->IsDirect()) {
// Simple case where the oat method index was stashed at load time.
oat_method_index = method->GetMethodIndex();
} else {
// We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index
// by search for its position in the declared virtual methods.
oat_method_index = declaring_class->NumDirectMethods();
size_t end = declaring_class->NumVirtualMethods();
bool found = false;
for (size_t i = 0; i < end; i++) {
if (declaring_class->GetVirtualMethod(i) == method) {
found = true;
break;
}
oat_method_index++;
}
CHECK(found) << "Didn't find oat method index for virtual method: " << PrettyMethod(method);
}
ClassHelper kh(declaring_class);
UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(kh.GetDexFile(), kh.GetDescriptor()));
CHECK(oat_class.get() != NULL);
return oat_class->GetOatMethod(oat_method_index);
}
// Special case to get oat code without overwriting a trampoline.
const void* ClassLinker::GetOatCodeFor(const AbstractMethod* method) {
CHECK(Runtime::Current()->IsCompiler() || method->GetDeclaringClass()->IsInitializing());
return GetOatMethodFor(method).GetCode();
}
void ClassLinker::FixupStaticTrampolines(Class* klass) {
ClassHelper kh(klass);
const DexFile::ClassDef* dex_class_def = kh.GetClassDef();
CHECK(dex_class_def != NULL);
const DexFile& dex_file = kh.GetDexFile();
const byte* class_data = dex_file.GetClassData(*dex_class_def);
if (class_data == NULL) {
return; // no fields or methods - for example a marker interface
}
if (!Runtime::Current()->IsStarted() || Runtime::Current()->UseCompileTimeClassPath()) {
// OAT file unavailable
return;
}
UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(dex_file, kh.GetDescriptor()));
CHECK(oat_class.get() != NULL);
ClassDataItemIterator it(dex_file, class_data);
// Skip fields
while (it.HasNextStaticField()) {
it.Next();
}
while (it.HasNextInstanceField()) {
it.Next();
}
size_t method_index = 0;
// Link the code of methods skipped by LinkCode
const void* trampoline = Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData();
for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) {
AbstractMethod* method = klass->GetDirectMethod(i);
if (Runtime::Current()->IsMethodTracingActive()) {
Trace* tracer = Runtime::Current()->GetTracer();
if (tracer->GetSavedCodeFromMap(method) == trampoline) {
const void* code = oat_class->GetOatMethod(method_index).GetCode();
tracer->ResetSavedCode(method);
method->SetCode(code);
tracer->SaveAndUpdateCode(method);
}
} else if (method->GetCode() == trampoline) {
const void* code = oat_class->GetOatMethod(method_index).GetCode();
CHECK(code != NULL)
<< "Resolving a static trampoline but found no code for: " << PrettyMethod(method);
method->SetCode(code);
}
method_index++;
}
}
static void LinkCode(SirtRef<AbstractMethod>& method, const OatFile::OatClass* oat_class,
uint32_t method_index)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
// Every kind of method should at least get an invoke stub from the oat_method.
// non-abstract methods also get their code pointers.
const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
oat_method.LinkMethodPointers(method.get());
Runtime* runtime = Runtime::Current();
if (method->IsAbstract()) {
method->SetCode(runtime->GetAbstractMethodErrorStubArray()->GetData());
return;
}
if (method->IsStatic() && !method->IsConstructor()) {
// For static methods excluding the class initializer, install the trampoline
method->SetCode(runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
}
if (method->IsNative()) {
// unregistering restores the dlsym lookup stub
method->UnregisterNative(Thread::Current());
}
if (Runtime::Current()->IsMethodTracingActive()) {
Trace* tracer = Runtime::Current()->GetTracer();
tracer->SaveAndUpdateCode(method.get());
}
}
void ClassLinker::LoadClass(const DexFile& dex_file,
const DexFile::ClassDef& dex_class_def,
SirtRef<Class>& klass,
ClassLoader* class_loader) {
CHECK(klass.get() != NULL);
CHECK(klass->GetDexCache() != NULL);
CHECK_EQ(Class::kStatusNotReady, klass->GetStatus());
const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
CHECK(descriptor != NULL);
klass->SetClass(GetClassRoot(kJavaLangClass));
uint32_t access_flags = dex_class_def.access_flags_;
// Make sure that none of our runtime-only flags are set.
CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U);
klass->SetAccessFlags(access_flags);
klass->SetClassLoader(class_loader);
DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
klass->SetStatus(Class::kStatusIdx);
klass->SetDexTypeIndex(dex_class_def.class_idx_);
// Load fields fields.
const byte* class_data = dex_file.GetClassData(dex_class_def);
if (class_data == NULL) {
return; // no fields or methods - for example a marker interface
}
ClassDataItemIterator it(dex_file, class_data);
Thread* self = Thread::Current();
if (it.NumStaticFields() != 0) {
klass->SetSFields(AllocFieldArray(self, it.NumStaticFields()));
}
if (it.NumInstanceFields() != 0) {
klass->SetIFields(AllocFieldArray(self, it.NumInstanceFields()));
}
for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) {
SirtRef<Field> sfield(self, AllocField(self));
klass->SetStaticField(i, sfield.get());
LoadField(dex_file, it, klass, sfield);
}
for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) {
SirtRef<Field> ifield(self, AllocField(self));
klass->SetInstanceField(i, ifield.get());
LoadField(dex_file, it, klass, ifield);
}
UniquePtr<const OatFile::OatClass> oat_class;
if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) {
oat_class.reset(GetOatClass(dex_file, descriptor));
}
// Load methods.
if (it.NumDirectMethods() != 0) {
// TODO: append direct methods to class object
klass->SetDirectMethods(AllocAbstractMethodArray(self, it.NumDirectMethods()));
}
if (it.NumVirtualMethods() != 0) {
// TODO: append direct methods to class object
klass->SetVirtualMethods(AllocMethodArray(self, it.NumVirtualMethods()));
}
size_t class_def_method_index = 0;
for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) {
SirtRef<AbstractMethod> method(self, LoadMethod(self, dex_file, it, klass));
klass->SetDirectMethod(i, method.get());
if (oat_class.get() != NULL) {
LinkCode(method, oat_class.get(), class_def_method_index);
}
method->SetMethodIndex(class_def_method_index);
class_def_method_index++;
}
for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) {
SirtRef<AbstractMethod> method(self, LoadMethod(self, dex_file, it, klass));
klass->SetVirtualMethod(i, method.get());
DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i);
if (oat_class.get() != NULL) {
LinkCode(method, oat_class.get(), class_def_method_index);
}
class_def_method_index++;
}
DCHECK(!it.HasNext());
}
void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it,
SirtRef<Class>& klass, SirtRef<Field>& dst) {
uint32_t field_idx = it.GetMemberIndex();
dst->SetDexFieldIndex(field_idx);
dst->SetDeclaringClass(klass.get());
dst->SetAccessFlags(it.GetMemberAccessFlags());
}
AbstractMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file,
const ClassDataItemIterator& it,
SirtRef<Class>& klass) {
uint32_t dex_method_idx = it.GetMemberIndex();
const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
StringPiece method_name(dex_file.GetMethodName(method_id));
AbstractMethod* dst = NULL;
if (method_name == "<init>") {
dst = AllocConstructor(self);
} else {
dst = AllocMethod(self);
}
DCHECK(dst->IsMethod()) << PrettyDescriptor(dst->GetClass());
const char* old_cause = self->StartAssertNoThreadSuspension("LoadMethod");
dst->SetDexMethodIndex(dex_method_idx);
dst->SetDeclaringClass(klass.get());
if (method_name == "finalize") {
// Create the prototype for a signature of "()V"
const DexFile::StringId* void_string_id = dex_file.FindStringId("V");
if (void_string_id != NULL) {
const DexFile::TypeId* void_type_id =
dex_file.FindTypeId(dex_file.GetIndexForStringId(*void_string_id));
if (void_type_id != NULL) {
std::vector<uint16_t> no_args;
const DexFile::ProtoId* finalizer_proto =
dex_file.FindProtoId(dex_file.GetIndexForTypeId(*void_type_id), no_args);
if (finalizer_proto != NULL) {
// We have the prototype in the dex file
if (klass->GetClassLoader() != NULL) { // All non-boot finalizer methods are flagged
klass->SetFinalizable();
} else {
StringPiece klass_descriptor(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
// The Enum class declares a "final" finalize() method to prevent subclasses from
// introducing a finalizer. We don't want to set the finalizable flag for Enum or its
// subclasses, so we exclude it here.
// We also want to avoid setting the flag on Object, where we know that finalize() is
// empty.
if (klass_descriptor != "Ljava/lang/Object;" &&
klass_descriptor != "Ljava/lang/Enum;") {
klass->SetFinalizable();
}
}
}
}
}
}
dst->SetCodeItemOffset(it.GetMethodCodeItemOffset());
dst->SetAccessFlags(it.GetMemberAccessFlags());
dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings());
dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());
dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes());
dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage());
CHECK(dst->IsMethod());
self->EndAssertNoThreadSuspension(old_cause);
return dst;
}
void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Thread* self = Thread::Current();
SirtRef<DexCache> dex_cache(self, AllocDexCache(self, dex_file));
AppendToBootClassPath(dex_file, dex_cache);
}
void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) {
CHECK(dex_cache.get() != NULL) << dex_file.GetLocation();
boot_class_path_.push_back(&dex_file);
RegisterDexFile(dex_file, dex_cache);
}
bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) const {
dex_lock_.AssertHeld(Thread::Current());
for (size_t i = 0; i != dex_caches_.size(); ++i) {
if (dex_caches_[i]->GetDexFile() == &dex_file) {
return true;
}
}
return false;
}
bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) const {
MutexLock mu(Thread::Current(), dex_lock_);
return IsDexFileRegisteredLocked(dex_file);
}
void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) {
dex_lock_.AssertHeld(Thread::Current());
CHECK(dex_cache.get() != NULL) << dex_file.GetLocation();
CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation()));
dex_caches_.push_back(dex_cache.get());
dex_cache->SetDexFile(&dex_file);
Dirty();
}
void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Thread* self = Thread::Current();
{
MutexLock mu(self, dex_lock_);
if (IsDexFileRegisteredLocked(dex_file)) {
return;
}
}
// Don't alloc while holding the lock, since allocation may need to
// suspend all threads and another thread may need the dex_lock_ to
// get to a suspend point.
SirtRef<DexCache> dex_cache(self, AllocDexCache(self, dex_file));
{
MutexLock mu(self, dex_lock_);
if (IsDexFileRegisteredLocked(dex_file)) {
return;
}
RegisterDexFileLocked(dex_file, dex_cache);
}
}
void ClassLinker::RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) {
MutexLock mu(Thread::Current(), dex_lock_);
RegisterDexFileLocked(dex_file, dex_cache);
}
DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
MutexLock mu(Thread::Current(), dex_lock_);
for (size_t i = 0; i != dex_caches_.size(); ++i) {
DexCache* dex_cache = dex_caches_[i];
if (dex_cache->GetDexFile() == &dex_file) {
return dex_cache;
}
}
LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation();
return NULL;
}
void ClassLinker::FixupDexCaches(AbstractMethod* resolution_method) const {
MutexLock mu(Thread::Current(), dex_lock_);
for (size_t i = 0; i != dex_caches_.size(); ++i) {
dex_caches_[i]->Fixup(resolution_method);
}
}
Class* ClassLinker::InitializePrimitiveClass(Class* primitive_class, Primitive::Type type) {
CHECK(primitive_class != NULL);
// Must hold lock on object when initializing.
ObjectLock lock(Thread::Current(), primitive_class);
primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract);
primitive_class->SetPrimitiveType(type);
primitive_class->SetStatus(Class::kStatusInitialized);
Class* existing = InsertClass(Primitive::Descriptor(type), primitive_class, false);
CHECK(existing == NULL) << "InitPrimitiveClass(" << type << ") failed";
return primitive_class;
}
// Create an array class (i.e. the class object for the array, not the
// array itself). "descriptor" looks like "[C" or "[[[[B" or
// "[Ljava/lang/String;".
//
// If "descriptor" refers to an array of primitives, look up the
// primitive type's internally-generated class object.
//
// "class_loader" is the class loader of the class that's referring to
// us. It's used to ensure that we're looking for the element type in
// the right context. It does NOT become the class loader for the
// array class; that always comes from the base element class.
//
// Returns NULL with an exception raised on failure.
Class* ClassLinker::CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader) {
CHECK_EQ('[', descriptor[0]);
// Identify the underlying component type
Class* component_type = FindClass(descriptor.substr(1).c_str(), class_loader);
if (component_type == NULL) {
DCHECK(Thread::Current()->IsExceptionPending());
return NULL;
}
// See if the component type is already loaded. Array classes are
// always associated with the class loader of their underlying
// element type -- an array of Strings goes with the loader for
// java/lang/String -- so we need to look for it there. (The
// caller should have checked for the existence of the class
// before calling here, but they did so with *their* class loader,
// not the component type's loader.)
//
// If we find it, the caller adds "loader" to the class' initiating
// loader list, which should prevent us from going through this again.
//
// This call is unnecessary if "loader" and "component_type->GetClassLoader()"
// are the same, because our caller (FindClass) just did the
// lookup. (Even if we get this wrong we still have correct behavior,
// because we effectively do this lookup again when we add the new
// class to the hash table --- necessary because of possible races with
// other threads.)
if (class_loader != component_type->GetClassLoader()) {
Class* new_class = LookupClass(descriptor.c_str(), component_type->GetClassLoader());
if (new_class != NULL) {
return new_class;
}
}
// Fill out the fields in the Class.
//
// It is possible to execute some methods against arrays, because
// all arrays are subclasses of java_lang_Object_, so we need to set
// up a vtable. We can just point at the one in java_lang_Object_.
//
// Array classes are simple enough that we don't need to do a full
// link step.
Thread* self = Thread::Current();
SirtRef<Class> new_class(self, NULL);
if (!init_done_) {
// Classes that were hand created, ie not by FindSystemClass
if (descriptor == "[Ljava/lang/Class;") {
new_class.reset(GetClassRoot(kClassArrayClass));
} else if (descriptor == "[Ljava/lang/Object;") {
new_class.reset(GetClassRoot(kObjectArrayClass));
} else if (descriptor == class_roots_descriptors_[kJavaLangStringArrayClass]) {
new_class.reset(GetClassRoot(kJavaLangStringArrayClass));
} else if (descriptor == class_roots_descriptors_[kJavaLangReflectAbstractMethodArrayClass]) {
new_class.reset(GetClassRoot(kJavaLangReflectAbstractMethodArrayClass));
} else if (descriptor == class_roots_descriptors_[kJavaLangReflectFieldArrayClass]) {
new_class.reset(GetClassRoot(kJavaLangReflectFieldArrayClass));
} else if (descriptor == class_roots_descriptors_[kJavaLangReflectMethodArrayClass]) {
new_class.reset(GetClassRoot(kJavaLangReflectMethodArrayClass));
} else if (descriptor == "[C") {
new_class.reset(GetClassRoot(kCharArrayClass));
} else if (descriptor == "[I") {
new_class.reset(GetClassRoot(kIntArrayClass));
}
}
if (new_class.get() == NULL) {
new_class.reset(AllocClass(self, sizeof(Class)));
if (new_class.get() == NULL) {
return NULL;
}
new_class->SetComponentType(component_type);
}
ObjectLock lock(self, new_class.get()); // Must hold lock on object when initializing.
DCHECK(new_class->GetComponentType() != NULL);
Class* java_lang_Object = GetClassRoot(kJavaLangObject);
new_class->SetSuperClass(java_lang_Object);
new_class->SetVTable(java_lang_Object->GetVTable());
new_class->SetPrimitiveType(Primitive::kPrimNot);
new_class->SetClassLoader(component_type->GetClassLoader());
new_class->SetStatus(Class::kStatusInitialized);
// don't need to set new_class->SetObjectSize(..)
// because Object::SizeOf delegates to Array::SizeOf
// All arrays have java/lang/Cloneable and java/io/Serializable as
// interfaces. We need to set that up here, so that stuff like
// "instanceof" works right.
//
// Note: The GC could run during the call to FindSystemClass,
// so we need to make sure the class object is GC-valid while we're in
// there. Do this by clearing the interface list so the GC will just
// think that the entries are null.
// Use the single, global copies of "interfaces" and "iftable"
// (remember not to free them for arrays).
CHECK(array_iftable_ != NULL);
new_class->SetIfTable(array_iftable_);
// Inherit access flags from the component type. Arrays can't be
// used as a superclass or interface, so we want to add "final"
// and remove "interface".
//
// Don't inherit any non-standard flags (e.g., kAccFinal)
// from component_type. We assume that the array class does not
// override finalize().
new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() &
~kAccInterface) | kAccFinal) & kAccJavaFlagsMask);
Class* existing = InsertClass(descriptor, new_class.get(), false);
if (existing == NULL) {
return new_class.get();
}
// Another thread must have loaded the class after we
// started but before we finished. Abandon what we've
// done.
//
// (Yes, this happens.)
return existing;
}
Class* ClassLinker::FindPrimitiveClass(char type) {
switch (Primitive::GetType(type)) {
case Primitive::kPrimByte:
return GetClassRoot(kPrimitiveByte);
case Primitive::kPrimChar:
return GetClassRoot(kPrimitiveChar);
case Primitive::kPrimDouble:
return GetClassRoot(kPrimitiveDouble);
case Primitive::kPrimFloat:
return GetClassRoot(kPrimitiveFloat);
case Primitive::kPrimInt:
return GetClassRoot(kPrimitiveInt);
case Primitive::kPrimLong:
return GetClassRoot(kPrimitiveLong);
case Primitive::kPrimShort:
return GetClassRoot(kPrimitiveShort);
case Primitive::kPrimBoolean:
return GetClassRoot(kPrimitiveBoolean);
case Primitive::kPrimVoid:
return GetClassRoot(kPrimitiveVoid);
case Primitive::kPrimNot:
break;
}
std::string printable_type(PrintableChar(type));
ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str());
return NULL;
}
Class* ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass, bool image_class) {
if (VLOG_IS_ON(class_linker)) {
DexCache* dex_cache = klass->GetDexCache();
std::string source;
if (dex_cache != NULL) {
source += " from ";
source += dex_cache->GetLocation()->ToModifiedUtf8();
}
LOG(INFO) << "Loaded class " << descriptor << source;
}
size_t hash = StringPieceHash()(descriptor);
MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Table& classes = image_class ? image_classes_ : classes_;
Class* existing = LookupClassLocked(descriptor.data(), klass->GetClassLoader(), hash, classes);
#ifndef NDEBUG
// Check we don't have the class in the other table in error
Table& other_classes = image_class ? classes_ : image_classes_;
CHECK(LookupClassLocked(descriptor.data(), klass->GetClassLoader(), hash, other_classes) == NULL);
#endif
if (existing != NULL) {
return existing;
}
classes.insert(std::make_pair(hash, klass));
Dirty();
return NULL;
}
bool ClassLinker::RemoveClass(const char* descriptor, const ClassLoader* class_loader) {
size_t hash = Hash(descriptor);
MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
typedef Table::iterator It; // TODO: C++0x auto
// TODO: determine if its better to search classes_ or image_classes_ first
ClassHelper kh;
for (It it = classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Class* klass = it->second;
kh.ChangeClass(klass);
if (strcmp(kh.GetDescriptor(), descriptor) == 0 && klass->GetClassLoader() == class_loader) {
classes_.erase(it);
return true;
}
}
for (It it = image_classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Class* klass = it->second;
kh.ChangeClass(klass);
if (strcmp(kh.GetDescriptor(), descriptor) == 0 && klass->GetClassLoader() == class_loader) {
image_classes_.erase(it);
return true;
}
}
return false;
}
Class* ClassLinker::LookupClass(const char* descriptor, const ClassLoader* class_loader) {
size_t hash = Hash(descriptor);
MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
// TODO: determine if its better to search classes_ or image_classes_ first
Class* klass = LookupClassLocked(descriptor, class_loader, hash, classes_);
if (klass != NULL) {
return klass;
}
return LookupClassLocked(descriptor, class_loader, hash, image_classes_);
}
Class* ClassLinker::LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
size_t hash, const Table& classes) {
ClassHelper kh(NULL, this);
typedef Table::const_iterator It; // TODO: C++0x auto
for (It it = classes.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Class* klass = it->second;
kh.ChangeClass(klass);
if (strcmp(descriptor, kh.GetDescriptor()) == 0 && klass->GetClassLoader() == class_loader) {
#ifndef NDEBUG
for (++it; it != end && it->first == hash; ++it) {
Class* klass2 = it->second;
kh.ChangeClass(klass2);
CHECK(!(strcmp(descriptor, kh.GetDescriptor()) == 0 && klass2->GetClassLoader() == class_loader))
<< PrettyClass(klass) << " " << klass << " " << klass->GetClassLoader() << " "
<< PrettyClass(klass2) << " " << klass2 << " " << klass2->GetClassLoader();
}
#endif
return klass;
}
}
return NULL;
}
void ClassLinker::LookupClasses(const char* descriptor, std::vector<Class*>& classes) {
classes.clear();
size_t hash = Hash(descriptor);
MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
typedef Table::const_iterator It; // TODO: C++0x auto
// TODO: determine if its better to search classes_ or image_classes_ first
ClassHelper kh(NULL, this);
for (It it = classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Class* klass = it->second;
kh.ChangeClass(klass);
if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
classes.push_back(klass);
}
}
for (It it = image_classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Class* klass = it->second;
kh.ChangeClass(klass);
if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
classes.push_back(klass);
}
}
}
void ClassLinker::VerifyClass(Class* klass) {
// TODO: assert that the monitor on the Class is held
Thread* self = Thread::Current();
ObjectLock lock(self, klass);
// Don't attempt to re-verify if already sufficiently verified.
if (klass->IsVerified() ||
(klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler())) {
return;
}
// The class might already be erroneous, for example at compile time if we attempted to verify
// this class as a parent to another.
if (klass->IsErroneous()) {
ThrowEarlierClassFailure(klass);
return;
}
if (klass->GetStatus() == Class::kStatusResolved) {
klass->SetStatus(Class::kStatusVerifying);
} else {
CHECK_EQ(klass->GetStatus(), Class::kStatusRetryVerificationAtRuntime) << PrettyClass(klass);
CHECK(!Runtime::Current()->IsCompiler());
klass->SetStatus(Class::kStatusVerifyingAtRuntime);
}
// Verify super class.
Class* super = klass->GetSuperClass();
std::string error_msg;
if (super != NULL) {
// Acquire lock to prevent races on verifying the super class.
ObjectLock lock(self, super);
if (!super->IsVerified() && !super->IsErroneous()) {
Runtime::Current()->GetClassLinker()->VerifyClass(super);
}
if (!super->IsCompileTimeVerified()) {
error_msg = "Rejecting class ";
error_msg += PrettyDescriptor(klass);
error_msg += " that attempts to sub-class erroneous class ";
error_msg += PrettyDescriptor(super);
LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
SirtRef<Throwable> cause(self, self->GetException());
if (cause.get() != NULL) {
self->ClearException();
}
self->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str());
if (cause.get() != NULL) {
self->GetException()->SetCause(cause.get());
}
klass->SetStatus(Class::kStatusError);
return;
}
}
// Try to use verification information from the oat file, otherwise do runtime verification.
const DexFile& dex_file = *klass->GetDexCache()->GetDexFile();
Class::Status oat_file_class_status(Class::kStatusNotReady);
bool preverified = VerifyClassUsingOatFile(dex_file, klass, oat_file_class_status);
verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure;
if (oat_file_class_status == Class::kStatusError) {
LOG(WARNING) << "Skipping runtime verification of erroneous class " << PrettyDescriptor(klass)
<< " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
error_msg = "Rejecting class ";
error_msg += PrettyDescriptor(klass);
error_msg += " because it failed compile-time verification";
Thread::Current()->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str());
klass->SetStatus(Class::kStatusError);
return;
}
if (!preverified) {
verifier_failure = verifier::MethodVerifier::VerifyClass(klass, error_msg);
}
if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) {
if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) {
LOG(WARNING) << "Soft verification failure in class " << PrettyDescriptor(klass)
<< " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
<< " because: " << error_msg;
}
self->AssertNoPendingException();
// Make sure all classes referenced by catch blocks are resolved.
ResolveClassExceptionHandlerTypes(dex_file, klass);
if (verifier_failure == verifier::MethodVerifier::kNoFailure) {
klass->SetStatus(Class::kStatusVerified);
} else {
CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure);
// Soft failures at compile time should be retried at runtime. Soft
// failures at runtime will be handled by slow paths in the generated
// code. Set status accordingly.
if (Runtime::Current()->IsCompiler()) {
klass->SetStatus(Class::kStatusRetryVerificationAtRuntime);
} else {
klass->SetStatus(Class::kStatusVerified);
}
}
} else {
LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass)
<< " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
<< " because: " << error_msg;
self->AssertNoPendingException();
self->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str());
klass->SetStatus(Class::kStatusError);
}
}
bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass,
Class::Status& oat_file_class_status) {
if (!Runtime::Current()->IsStarted()) {
return false;
}
if (Runtime::Current()->UseCompileTimeClassPath()) {
return false;
}
const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file);
CHECK(oat_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass);
const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
CHECK(oat_dex_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass);
const char* descriptor = ClassHelper(klass).GetDescriptor();
uint32_t class_def_index;
bool found = dex_file.FindClassDefIndex(descriptor, class_def_index);
CHECK(found) << dex_file.GetLocation() << " " << PrettyClass(klass) << " " << descriptor;
UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
CHECK(oat_class.get() != NULL)
<< dex_file.GetLocation() << " " << PrettyClass(klass) << " " << descriptor;
oat_file_class_status = oat_class->GetStatus();
if (oat_file_class_status == Class::kStatusVerified ||
oat_file_class_status == Class::kStatusInitialized) {
return true;
}
if (oat_file_class_status == Class::kStatusRetryVerificationAtRuntime) {
// Compile time verification failed with a soft error. Compile time verification can fail
// because we have incomplete type information. Consider the following:
// class ... {
// Foo x;
// .... () {
// if (...) {
// v1 gets assigned a type of resolved class Foo
// } else {
// v1 gets assigned a type of unresolved class Bar
// }
// iput x = v1
// } }
// when we merge v1 following the if-the-else it results in Conflict
// (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be
// allowing an unsafe assignment to the field x in the iput (javac may have compiled this as
// it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk
// at compile time).
return false;
}
if (oat_file_class_status == Class::kStatusError) {
// Compile time verification failed with a hard error. This is caused by invalid instructions
// in the class. These errors are unrecoverable.
return false;
}
if (oat_file_class_status == Class::kStatusNotReady) {
// Status is uninitialized if we couldn't determine the status at compile time, for example,
// not loading the class.
// TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy
// isn't a problem and this case shouldn't occur
return false;
}
LOG(FATAL) << "Unexpected class status: " << oat_file_class_status
<< " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " << descriptor;
return false;
}
void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass) {
for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i));
}
for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i));
}
}
void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, AbstractMethod* method) {
// similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod.
const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
if (code_item == NULL) {
return; // native or abstract method
}
if (code_item->tries_size_ == 0) {
return; // nothing to process
}
const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
ClassLinker* linker = Runtime::Current()->GetClassLinker();
for (uint32_t idx = 0; idx < handlers_size; idx++) {
CatchHandlerIterator iterator(handlers_ptr);
for (; iterator.HasNext(); iterator.Next()) {
// Ensure exception types are resolved so that they don't need resolution to be delivered,
// unresolved exception types will be ignored by exception delivery
if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method);
if (exception_type == NULL) {
DCHECK(Thread::Current()->IsExceptionPending());
Thread::Current()->ClearException();
}
}
}
handlers_ptr = iterator.EndDataPointer();
}
}
static void CheckProxyConstructor(AbstractMethod* constructor);
static void CheckProxyMethod(AbstractMethod* method, SirtRef<AbstractMethod>& prototype);
Class* ClassLinker::CreateProxyClass(String* name, ObjectArray<Class>* interfaces,
ClassLoader* loader, ObjectArray<AbstractMethod>* methods,
ObjectArray<ObjectArray<Class> >* throws) {
Thread* self = Thread::Current();
SirtRef<Class> klass(self, AllocClass(self, GetClassRoot(kJavaLangClass),
sizeof(SynthesizedProxyClass)));
CHECK(klass.get() != NULL);
DCHECK(klass->GetClass() != NULL);
klass->SetObjectSize(sizeof(Proxy));
klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal);
klass->SetClassLoader(loader);
DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
klass->SetName(name);
Class* proxy_class = GetClassRoot(kJavaLangReflectProxy);
klass->SetDexCache(proxy_class->GetDexCache());
klass->SetStatus(Class::kStatusIdx);
klass->SetDexTypeIndex(DexFile::kDexNoIndex16);
// Instance fields are inherited, but we add a couple of static fields...
klass->SetSFields(AllocFieldArray(self, 2));
// 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by
// our proxy, so Class.getInterfaces doesn't return the flattened set.
SirtRef<Field> interfaces_sfield(self, AllocField(self));
klass->SetStaticField(0, interfaces_sfield.get());
interfaces_sfield->SetDexFieldIndex(0);
interfaces_sfield->SetDeclaringClass(klass.get());
interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
// 2. Create a static field 'throws' that holds exceptions thrown by our methods.
SirtRef<Field> throws_sfield(self, AllocField(self));
klass->SetStaticField(1, throws_sfield.get());
throws_sfield->SetDexFieldIndex(1);
throws_sfield->SetDeclaringClass(klass.get());
throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
// Proxies have 1 direct method, the constructor
klass->SetDirectMethods(AllocAbstractMethodArray(self, 1));
klass->SetDirectMethod(0, CreateProxyConstructor(self, klass, proxy_class));
// Create virtual method using specified prototypes
size_t num_virtual_methods = methods->GetLength();
klass->SetVirtualMethods(AllocMethodArray(self, num_virtual_methods));
for (size_t i = 0; i < num_virtual_methods; ++i) {
SirtRef<AbstractMethod> prototype(self, methods->Get(i));
klass->SetVirtualMethod(i, CreateProxyMethod(self, klass, prototype));
}
klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy
klass->SetStatus(Class::kStatusLoaded); // Class is now effectively in the loaded state
DCHECK(!Thread::Current()->IsExceptionPending());
// Link the fields and virtual methods, creating vtable and iftables
if (!LinkClass(klass, interfaces)) {
klass->SetStatus(Class::kStatusError);
return NULL;
}
{
ObjectLock lock(self, klass.get()); // Must hold lock on object when initializing.
interfaces_sfield->SetObject(NULL, interfaces);
throws_sfield->SetObject(NULL, throws);
klass->SetStatus(Class::kStatusInitialized);
}
<