From b7054baf28d4d652fbd98a94b089344a31898d53 Mon Sep 17 00:00:00 2001 From: Sebastien Hertz Date: Thu, 13 Mar 2014 11:52:31 +0100 Subject: [PATCH] Fix debugger crashes in presence of proxy objects. Fix ClassHelper::NumDirectInterfaces to use IfTable::Count instead of the iftable array's length (which is twice the interface count). This happens when processing ReferenceType::Interfaces command and was causing a crash. Return ABSENT_INFORMATION error code when we're asked for the source file of a proxy class. Otherwise we call ClassHelper::GetSourceFile which expect the class has a corresponding ClassDef item in the DEX file which is not true for proxy classes. Add new proxy_test to check ClassHelper works correctly with proxy classes. Bug: 13426918 Change-Id: I5c1212b1a697dd7dc1ab18e99552ee113c533a5a --- build/Android.gtest.mk | 1 + runtime/debugger.cc | 3 ++ runtime/object_utils.h | 4 +- runtime/proxy_test.cc | 139 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 runtime/proxy_test.cc diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk index 7829f9b01..954ca996f 100644 --- a/build/Android.gtest.mk +++ b/build/Android.gtest.mk @@ -62,6 +62,7 @@ RUNTIME_GTEST_COMMON_SRC_FILES := \ COMPILER_GTEST_COMMON_SRC_FILES := \ runtime/jni_internal_test.cc \ + runtime/proxy_test.cc \ compiler/dex/local_value_numbering_test.cc \ compiler/driver/compiler_driver_test.cc \ compiler/elf_writer_test.cc \ diff --git a/runtime/debugger.cc b/runtime/debugger.cc index 7e2dfd276..1276cd909 100644 --- a/runtime/debugger.cc +++ b/runtime/debugger.cc @@ -1025,6 +1025,9 @@ JDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result if (c == NULL) { return status; } + if (c->IsProxyClass()) { + return JDWP::ERR_ABSENT_INFORMATION; + } result = ClassHelper(c).GetSourceFile(); return JDWP::ERR_NONE; } diff --git a/runtime/object_utils.h b/runtime/object_utils.h index dd2bd4faf..9ddeeca84 100644 --- a/runtime/object_utils.h +++ b/runtime/object_utils.h @@ -133,7 +133,7 @@ class ClassHelper { } else if (klass_->IsArrayClass()) { return 2; } else if (klass_->IsProxyClass()) { - return klass_->GetIfTable()->GetLength(); + return klass_->GetIfTable()->Count(); } else { const DexFile::TypeList* interfaces = GetInterfaceTypeList(); if (interfaces == nullptr) { @@ -180,7 +180,7 @@ class ClassHelper { std::string descriptor(GetDescriptor()); const DexFile& dex_file = GetDexFile(); const DexFile::ClassDef* dex_class_def = GetClassDef(); - CHECK(dex_class_def != nullptr); + CHECK(dex_class_def != nullptr) << "No class def for class " << PrettyClass(klass_); return dex_file.GetSourceFile(*dex_class_def); } diff --git a/runtime/proxy_test.cc b/runtime/proxy_test.cc new file mode 100644 index 000000000..6453cb4d4 --- /dev/null +++ b/runtime/proxy_test.cc @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2014 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 "common_compiler_test.h" +#include "mirror/art_field-inl.h" + +#include +#include + +namespace art { + +class ProxyTest : public CommonCompilerTest { + public: + // Generate a proxy class with the given name and interfaces. This is a simplification from what + // libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and + // we do not declare exceptions. + mirror::Class* GenerateProxyClass(ScopedObjectAccess& soa, jobject jclass_loader, + const char* className, + const std::vector& interfaces) + SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { + mirror::Class* javaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); + CHECK(javaLangObject != nullptr); + + jclass javaLangClass = soa.AddLocalReference(mirror::Class::GetJavaLangClass()); + + // Builds the interfaces array. + jobjectArray proxyClassInterfaces = soa.Env()->NewObjectArray(interfaces.size(), javaLangClass, + nullptr); + soa.Self()->AssertNoPendingException(); + for (size_t i = 0; i < interfaces.size(); ++i) { + soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i, + soa.AddLocalReference(interfaces[i])); + } + + // Builds the method array. + jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString. + for (mirror::Class* interface : interfaces) { + mirror::ObjectArray* virtual_methods = interface->GetVirtualMethods(); + methods_count += (virtual_methods == nullptr) ? 0 : virtual_methods->GetLength(); + } + jclass javaLangReflectArtMethod = + soa.AddLocalReference(mirror::ArtMethod::GetJavaLangReflectArtMethod()); + jobjectArray proxyClassMethods = soa.Env()->NewObjectArray(methods_count, + javaLangReflectArtMethod, nullptr); + soa.Self()->AssertNoPendingException(); + + // Fill the method array + mirror::ArtMethod* equalsMethod = javaLangObject->FindDeclaredVirtualMethod("equals", + "(Ljava/lang/Object;)Z"); + mirror::ArtMethod* hashCodeMethod = javaLangObject->FindDeclaredVirtualMethod("hashCode", + "()I"); + mirror::ArtMethod* toStringMethod = javaLangObject->FindDeclaredVirtualMethod("toString", + "()Ljava/lang/String;"); + CHECK(equalsMethod != nullptr); + CHECK(hashCodeMethod != nullptr); + CHECK(toStringMethod != nullptr); + + jsize array_index = 0; + // Adds Object methods. + soa.Env()->SetObjectArrayElement(proxyClassMethods, array_index++, + soa.AddLocalReference(equalsMethod)); + soa.Env()->SetObjectArrayElement(proxyClassMethods, array_index++, + soa.AddLocalReference(hashCodeMethod)); + soa.Env()->SetObjectArrayElement(proxyClassMethods, array_index++, + soa.AddLocalReference(toStringMethod)); + + // Now adds all interfaces virtual methods. + for (mirror::Class* interface : interfaces) { + mirror::ObjectArray* virtual_methods = interface->GetVirtualMethods(); + if (virtual_methods != nullptr) { + for (int32_t mth_index = 0; mth_index < virtual_methods->GetLength(); ++mth_index) { + mirror::ArtMethod* method = virtual_methods->Get(mth_index); + soa.Env()->SetObjectArrayElement(proxyClassMethods, array_index++, + soa.AddLocalReference(method)); + } + } + } + CHECK_EQ(array_index, methods_count); + + // Builds an empty exception array. + jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr); + soa.Self()->AssertNoPendingException(); + + mirror::Class* proxyClass = class_linker_->CreateProxyClass(soa, + soa.Env()->NewStringUTF(className), + proxyClassInterfaces, jclass_loader, + proxyClassMethods, proxyClassThrows); + soa.Self()->AssertNoPendingException(); + return proxyClass; + } +}; + +// Creates a proxy class and check ClassHelper works correctly. +TEST_F(ProxyTest, ProxyClassHelper) { + ScopedObjectAccess soa(Thread::Current()); + jobject jclass_loader = LoadDex("Interfaces"); + SirtRef class_loader(soa.Self(), soa.Decode(jclass_loader)); + + mirror::Class* I = class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader); + mirror::Class* J = class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader); + ASSERT_TRUE(I != nullptr); + ASSERT_TRUE(J != nullptr); + std::vector interfaces; + interfaces.push_back(I); + interfaces.push_back(J); + + mirror::Class* proxyClass = GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces); + ASSERT_TRUE(proxyClass != nullptr); + ASSERT_TRUE(proxyClass->IsProxyClass()); + + mirror::Class* javaIoSerializable = class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"); + ASSERT_TRUE(javaIoSerializable != nullptr); + + // Check ClassHelper for proxy. + ClassHelper kh(proxyClass); + EXPECT_EQ(kh.NumDirectInterfaces(), 3U); // java.io.Serializable, Interfaces$I and Interfaces$J. + EXPECT_EQ(javaIoSerializable, kh.GetDirectInterface(0)); + EXPECT_EQ(I, kh.GetDirectInterface(1)); + EXPECT_EQ(J, kh.GetDirectInterface(2)); + std::string proxyClassDescriptor(kh.GetDescriptor()); + EXPECT_EQ("L$Proxy1234;", proxyClassDescriptor); +// EXPECT_EQ(nullptr, kh.GetSourceFile()); +} + + +} // namespace art -- 2.11.0