blob: 1292a819a382a34d41d27d79ba2898acef87c9c3 [file] [log] [blame]
Sebastien Hertzb7054ba2014-03-13 11:52:31 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010017#include <jni.h>
18#include <vector>
19
Mathieu Chartierc7853442015-03-27 14:35:38 -070020#include "art_field-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "class_linker-inl.h"
Jeff Haodb8a6642014-08-14 17:18:52 -070023#include "common_compiler_test.h"
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070024#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070025#include "mirror/method.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010028namespace art {
29
Jeff Haodb8a6642014-08-14 17:18:52 -070030class ProxyTest : public CommonCompilerTest {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010031 public:
32 // Generate a proxy class with the given name and interfaces. This is a simplification from what
33 // libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and
34 // we do not declare exceptions.
35 mirror::Class* GenerateProxyClass(ScopedObjectAccess& soa, jobject jclass_loader,
36 const char* className,
37 const std::vector<mirror::Class*>& interfaces)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070038 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010039 mirror::Class* javaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
40 CHECK(javaLangObject != nullptr);
41
42 jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass());
43
44 // Builds the interfaces array.
45 jobjectArray proxyClassInterfaces = soa.Env()->NewObjectArray(interfaces.size(), javaLangClass,
46 nullptr);
47 soa.Self()->AssertNoPendingException();
48 for (size_t i = 0; i < interfaces.size(); ++i) {
49 soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i,
50 soa.AddLocalReference<jclass>(interfaces[i]));
51 }
52
53 // Builds the method array.
54 jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString.
55 for (mirror::Class* interface : interfaces) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 methods_count += interface->NumVirtualMethods();
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010057 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -070058 jobjectArray proxyClassMethods = soa.Env()->NewObjectArray(
59 methods_count, soa.AddLocalReference<jclass>(mirror::Method::StaticClass()), nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010060 soa.Self()->AssertNoPendingException();
61
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010062 jsize array_index = 0;
Mathieu Chartierfc58af42015-04-16 18:00:39 -070063 // Fill the method array
Andreas Gampe542451c2016-07-26 09:02:02 -070064 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 ArtMethod* method = javaLangObject->FindDeclaredVirtualMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -070066 "equals", "(Ljava/lang/Object;)Z", kRuntimePointerSize);
Mathieu Chartierfc58af42015-04-16 18:00:39 -070067 CHECK(method != nullptr);
Andreas Gampee01e3642016-07-25 13:06:04 -070068 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -070069 soa.Env()->SetObjectArrayElement(
70 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -070071 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
72 method = javaLangObject->FindDeclaredVirtualMethod("hashCode", "()I", kRuntimePointerSize);
Mathieu Chartierfc58af42015-04-16 18:00:39 -070073 CHECK(method != nullptr);
74 soa.Env()->SetObjectArrayElement(
75 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -070076 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 method = javaLangObject->FindDeclaredVirtualMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -070078 "toString", "()Ljava/lang/String;", kRuntimePointerSize);
Mathieu Chartierfc58af42015-04-16 18:00:39 -070079 CHECK(method != nullptr);
80 soa.Env()->SetObjectArrayElement(
81 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -070082 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), method)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010083 // Now adds all interfaces virtual methods.
84 for (mirror::Class* interface : interfaces) {
Andreas Gampe542451c2016-07-26 09:02:02 -070085 for (auto& m : interface->GetDeclaredVirtualMethods(kRuntimePointerSize)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070086 soa.Env()->SetObjectArrayElement(
87 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -070088 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(), &m)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010089 }
90 }
91 CHECK_EQ(array_index, methods_count);
92
93 // Builds an empty exception array.
94 jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr);
95 soa.Self()->AssertNoPendingException();
96
Mathieu Chartierfc58af42015-04-16 18:00:39 -070097 mirror::Class* proxyClass = class_linker_->CreateProxyClass(
98 soa, soa.Env()->NewStringUTF(className), proxyClassInterfaces, jclass_loader,
99 proxyClassMethods, proxyClassThrows);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100100 soa.Self()->AssertNoPendingException();
101 return proxyClass;
102 }
103};
104
105// Creates a proxy class and check ClassHelper works correctly.
106TEST_F(ProxyTest, ProxyClassHelper) {
107 ScopedObjectAccess soa(Thread::Current());
108 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700109 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700110 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700111 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100112
Mathieu Chartierf8322842014-05-16 10:59:25 -0700113 Handle<mirror::Class> I(hs.NewHandle(
114 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
115 Handle<mirror::Class> J(hs.NewHandle(
116 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
117 ASSERT_TRUE(I.Get() != nullptr);
118 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100119
Mathieu Chartierf8322842014-05-16 10:59:25 -0700120 std::vector<mirror::Class*> interfaces;
121 interfaces.push_back(I.Get());
122 interfaces.push_back(J.Get());
123 Handle<mirror::Class> proxy_class(hs.NewHandle(
124 GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces)));
125 interfaces.clear(); // Don't least possibly stale objects in the array as good practice.
126 ASSERT_TRUE(proxy_class.Get() != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000127 ASSERT_TRUE(proxy_class->IsProxyClass());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700128 ASSERT_TRUE(proxy_class->IsInitialized());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100129
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200130 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
Vladimir Marko19a4d372016-12-08 14:41:46 +0000131 EXPECT_OBJ_PTR_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 0));
132 EXPECT_OBJ_PTR_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 1));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700133 std::string temp;
134 const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
135 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200136 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100137}
138
Sebastien Hertz80989a62014-04-01 14:39:44 +0200139// Creates a proxy class and check FieldHelper works correctly.
140TEST_F(ProxyTest, ProxyFieldHelper) {
141 ScopedObjectAccess soa(Thread::Current());
142 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700143 StackHandleScope<9> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700145 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +0200146
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700147 Handle<mirror::Class> I(hs.NewHandle(
148 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
149 Handle<mirror::Class> J(hs.NewHandle(
150 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
151 ASSERT_TRUE(I.Get() != nullptr);
152 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200153
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700154 Handle<mirror::Class> proxyClass;
155 {
156 std::vector<mirror::Class*> interfaces;
157 interfaces.push_back(I.Get());
158 interfaces.push_back(J.Get());
159 proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces));
160 }
161
162 ASSERT_TRUE(proxyClass.Get() != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000163 ASSERT_TRUE(proxyClass->IsProxyClass());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200164 ASSERT_TRUE(proxyClass->IsInitialized());
165
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700166 EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200167
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700168 LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700169 ASSERT_TRUE(static_fields != nullptr);
170 ASSERT_EQ(2u, proxyClass->NumStaticFields());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200171
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700172 Handle<mirror::Class> interfacesFieldClass(
173 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
174 ASSERT_TRUE(interfacesFieldClass.Get() != nullptr);
175 Handle<mirror::Class> throwsFieldClass(
176 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
177 ASSERT_TRUE(throwsFieldClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200178
179 // Test "Class[] interfaces" field.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700180 ArtField* field = &static_fields->At(0);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700181 EXPECT_STREQ("interfaces", field->GetName());
182 EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700183 EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->GetType<true>());
Ian Rogers08f1f502014-12-02 15:04:37 -0800184 std::string temp;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700185 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
186 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200187
188 // Test "Class[][] throws" field.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700189 field = &static_fields->At(1);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700190 EXPECT_STREQ("throws", field->GetName());
191 EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700192 EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->GetType<true>());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700193 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
194 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200195}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100196
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700197// Creates two proxy classes and check the art/mirror fields of their static fields.
198TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
199 ScopedObjectAccess soa(Thread::Current());
200 jobject jclass_loader = LoadDex("Interfaces");
201 StackHandleScope<7> hs(soa.Self());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700202
203 Handle<mirror::Class> proxyClass0;
204 Handle<mirror::Class> proxyClass1;
205 {
206 std::vector<mirror::Class*> interfaces;
207 proxyClass0 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy0", interfaces));
208 proxyClass1 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1", interfaces));
209 }
210
211 ASSERT_TRUE(proxyClass0.Get() != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000212 ASSERT_TRUE(proxyClass0->IsProxyClass());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700213 ASSERT_TRUE(proxyClass0->IsInitialized());
214 ASSERT_TRUE(proxyClass1.Get() != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000215 ASSERT_TRUE(proxyClass1->IsProxyClass());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700216 ASSERT_TRUE(proxyClass1->IsInitialized());
217
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700218 LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr();
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700219 ASSERT_TRUE(static_fields0 != nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +0100220 ASSERT_EQ(2u, static_fields0->size());
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700221 LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr();
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700222 ASSERT_TRUE(static_fields1 != nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +0100223 ASSERT_EQ(2u, static_fields1->size());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700224
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700225 EXPECT_OBJ_PTR_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get());
226 EXPECT_OBJ_PTR_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get());
227 EXPECT_OBJ_PTR_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get());
228 EXPECT_OBJ_PTR_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700229
Andreas Gampe542451c2016-07-26 09:02:02 -0700230 ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700231 ASSERT_FALSE(Runtime::Current()->IsActiveTransaction());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700232 Handle<mirror::Field> field00 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700233 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700234 soa.Self(), &static_fields0->At(0), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700235 Handle<mirror::Field> field01 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700236 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700237 soa.Self(), &static_fields0->At(1), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700238 Handle<mirror::Field> field10 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700239 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700240 soa.Self(), &static_fields1->At(0), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700241 Handle<mirror::Field> field11 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700242 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700243 soa.Self(), &static_fields1->At(1), true));
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700244 EXPECT_EQ(field00->GetArtField(), &static_fields0->At(0));
245 EXPECT_EQ(field01->GetArtField(), &static_fields0->At(1));
246 EXPECT_EQ(field10->GetArtField(), &static_fields1->At(0));
247 EXPECT_EQ(field11->GetArtField(), &static_fields1->At(1));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700248}
249
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100250} // namespace art