Catch classes inheriting from themselves in the class linker.
(cherry picked from commit 90328ac545f65759a8e4fb217a75332906795518)
Bug: 28685551
Bug: 27682580
Bug: 28830038
Change-Id: If568013bf3c82c1df9b282522712d9af5ca5945d
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 1835c72..8fcb6b2 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -5316,6 +5316,19 @@
const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex());
uint16_t super_class_idx = class_def.superclass_idx_;
if (super_class_idx != DexFile::kDexNoIndex16) {
+ // Check that a class does not inherit from itself directly.
+ //
+ // TODO: This is a cheap check to detect the straightforward case
+ // of a class extending itself (b/28685551), but we should do a
+ // proper cycle detection on loaded classes, to detect all cases
+ // of class circularity errors (b/28830038).
+ if (super_class_idx == class_def.class_idx_) {
+ ThrowClassCircularityError(klass.Get(),
+ "Class %s extends itself",
+ PrettyDescriptor(klass.Get()).c_str());
+ return false;
+ }
+
mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get());
if (super_class == nullptr) {
DCHECK(Thread::Current()->IsExceptionPending());
diff --git a/runtime/common_throws.cc b/runtime/common_throws.cc
index f8e32c4..75cce42 100644
--- a/runtime/common_throws.cc
+++ b/runtime/common_throws.cc
@@ -137,6 +137,13 @@
ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
}
+void ThrowClassCircularityError(mirror::Class* c, const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
+ va_end(args);
+}
+
// ClassFormatError
void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
diff --git a/runtime/common_throws.h b/runtime/common_throws.h
index 39c4e52..c3a1f09 100644
--- a/runtime/common_throws.h
+++ b/runtime/common_throws.h
@@ -58,6 +58,9 @@
void ThrowClassCircularityError(mirror::Class* c)
SHARED_REQUIRES(Locks::mutator_lock_) COLD_ATTR;
+void ThrowClassCircularityError(mirror::Class* c, const char* fmt, ...)
+ SHARED_REQUIRES(Locks::mutator_lock_) COLD_ATTR;
+
// ClassCastException
void ThrowClassCastException(mirror::Class* dest_type, mirror::Class* src_type)