blob: bc9ba377bc2b25b8fc5c7f6a15708df6909b729a [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"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Jeff Haodb8a6642014-08-14 17:18:52 -070022#include "common_compiler_test.h"
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070023#include "mirror/field-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070024#include "mirror/method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "scoped_thread_state_change.h"
26
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010027namespace art {
28
Jeff Haodb8a6642014-08-14 17:18:52 -070029class ProxyTest : public CommonCompilerTest {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010030 public:
31 // Generate a proxy class with the given name and interfaces. This is a simplification from what
32 // libcore does to fit to our test needs. We do not check for duplicated interfaces or methods and
33 // we do not declare exceptions.
34 mirror::Class* GenerateProxyClass(ScopedObjectAccess& soa, jobject jclass_loader,
35 const char* className,
36 const std::vector<mirror::Class*>& interfaces)
Mathieu Chartier90443472015-07-16 20:32:27 -070037 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010038 mirror::Class* javaLangObject = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
39 CHECK(javaLangObject != nullptr);
40
41 jclass javaLangClass = soa.AddLocalReference<jclass>(mirror::Class::GetJavaLangClass());
42
43 // Builds the interfaces array.
44 jobjectArray proxyClassInterfaces = soa.Env()->NewObjectArray(interfaces.size(), javaLangClass,
45 nullptr);
46 soa.Self()->AssertNoPendingException();
47 for (size_t i = 0; i < interfaces.size(); ++i) {
48 soa.Env()->SetObjectArrayElement(proxyClassInterfaces, i,
49 soa.AddLocalReference<jclass>(interfaces[i]));
50 }
51
52 // Builds the method array.
53 jsize methods_count = 3; // Object.equals, Object.hashCode and Object.toString.
54 for (mirror::Class* interface : interfaces) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070055 methods_count += interface->NumVirtualMethods();
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010056 }
Mathieu Chartierfc58af42015-04-16 18:00:39 -070057 jobjectArray proxyClassMethods = soa.Env()->NewObjectArray(
58 methods_count, soa.AddLocalReference<jclass>(mirror::Method::StaticClass()), nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010059 soa.Self()->AssertNoPendingException();
60
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010061 jsize array_index = 0;
Mathieu Chartierfc58af42015-04-16 18:00:39 -070062 // Fill the method array
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 ArtMethod* method = javaLangObject->FindDeclaredVirtualMethod(
64 "equals", "(Ljava/lang/Object;)Z", sizeof(void*));
Mathieu Chartierfc58af42015-04-16 18:00:39 -070065 CHECK(method != nullptr);
66 soa.Env()->SetObjectArrayElement(
67 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
68 mirror::Method::CreateFromArtMethod(soa.Self(), method)));
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 method = javaLangObject->FindDeclaredVirtualMethod("hashCode", "()I", sizeof(void*));
Mathieu Chartierfc58af42015-04-16 18:00:39 -070070 CHECK(method != nullptr);
71 soa.Env()->SetObjectArrayElement(
72 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
73 mirror::Method::CreateFromArtMethod(soa.Self(), method)));
Mathieu Chartiere401d142015-04-22 13:56:20 -070074 method = javaLangObject->FindDeclaredVirtualMethod(
75 "toString", "()Ljava/lang/String;", sizeof(void*));
Mathieu Chartierfc58af42015-04-16 18:00:39 -070076 CHECK(method != nullptr);
77 soa.Env()->SetObjectArrayElement(
78 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
79 mirror::Method::CreateFromArtMethod(soa.Self(), method)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010080 // Now adds all interfaces virtual methods.
81 for (mirror::Class* interface : interfaces) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070082 for (auto& m : interface->GetVirtualMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070083 soa.Env()->SetObjectArrayElement(
84 proxyClassMethods, array_index++, soa.AddLocalReference<jobject>(
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 mirror::Method::CreateFromArtMethod(soa.Self(), &m)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010086 }
87 }
88 CHECK_EQ(array_index, methods_count);
89
90 // Builds an empty exception array.
91 jobjectArray proxyClassThrows = soa.Env()->NewObjectArray(0, javaLangClass, nullptr);
92 soa.Self()->AssertNoPendingException();
93
Mathieu Chartierfc58af42015-04-16 18:00:39 -070094 mirror::Class* proxyClass = class_linker_->CreateProxyClass(
95 soa, soa.Env()->NewStringUTF(className), proxyClassInterfaces, jclass_loader,
96 proxyClassMethods, proxyClassThrows);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010097 soa.Self()->AssertNoPendingException();
98 return proxyClass;
99 }
100};
101
102// Creates a proxy class and check ClassHelper works correctly.
103TEST_F(ProxyTest, ProxyClassHelper) {
104 ScopedObjectAccess soa(Thread::Current());
105 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartierf8322842014-05-16 10:59:25 -0700106 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 Handle<mirror::ClassLoader> class_loader(
108 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100109
Mathieu Chartierf8322842014-05-16 10:59:25 -0700110 Handle<mirror::Class> I(hs.NewHandle(
111 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
112 Handle<mirror::Class> J(hs.NewHandle(
113 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
114 ASSERT_TRUE(I.Get() != nullptr);
115 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100116
Mathieu Chartierf8322842014-05-16 10:59:25 -0700117 std::vector<mirror::Class*> interfaces;
118 interfaces.push_back(I.Get());
119 interfaces.push_back(J.Get());
120 Handle<mirror::Class> proxy_class(hs.NewHandle(
121 GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces)));
122 interfaces.clear(); // Don't least possibly stale objects in the array as good practice.
123 ASSERT_TRUE(proxy_class.Get() != nullptr);
124 ASSERT_TRUE(proxy_class->IsProxyClass());
125 ASSERT_TRUE(proxy_class->IsInitialized());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100126
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200127 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700128 EXPECT_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 0));
129 EXPECT_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class, 1));
Ian Rogers1ff3c982014-08-12 02:30:58 -0700130 std::string temp;
131 const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
132 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200133 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100134}
135
Sebastien Hertz80989a62014-04-01 14:39:44 +0200136// Creates a proxy class and check FieldHelper works correctly.
137TEST_F(ProxyTest, ProxyFieldHelper) {
138 ScopedObjectAccess soa(Thread::Current());
139 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700140 StackHandleScope<9> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141 Handle<mirror::ClassLoader> class_loader(
142 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +0200143
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700144 Handle<mirror::Class> I(hs.NewHandle(
145 class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
146 Handle<mirror::Class> J(hs.NewHandle(
147 class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
148 ASSERT_TRUE(I.Get() != nullptr);
149 ASSERT_TRUE(J.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200150
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700151 Handle<mirror::Class> proxyClass;
152 {
153 std::vector<mirror::Class*> interfaces;
154 interfaces.push_back(I.Get());
155 interfaces.push_back(J.Get());
156 proxyClass = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1234", interfaces));
157 }
158
159 ASSERT_TRUE(proxyClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200160 ASSERT_TRUE(proxyClass->IsProxyClass());
161 ASSERT_TRUE(proxyClass->IsInitialized());
162
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700163 EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200164
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700165 LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700166 ASSERT_TRUE(static_fields != nullptr);
167 ASSERT_EQ(2u, proxyClass->NumStaticFields());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200168
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700169 Handle<mirror::Class> interfacesFieldClass(
170 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
171 ASSERT_TRUE(interfacesFieldClass.Get() != nullptr);
172 Handle<mirror::Class> throwsFieldClass(
173 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
174 ASSERT_TRUE(throwsFieldClass.Get() != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200175
176 // Test "Class[] interfaces" field.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700177 ArtField* field = &static_fields->At(0);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700178 EXPECT_STREQ("interfaces", field->GetName());
179 EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
180 EXPECT_EQ(interfacesFieldClass.Get(), field->GetType<true>());
Ian Rogers08f1f502014-12-02 15:04:37 -0800181 std::string temp;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700182 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
183 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200184
185 // Test "Class[][] throws" field.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700186 field = &static_fields->At(1);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700187 EXPECT_STREQ("throws", field->GetName());
188 EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
189 EXPECT_EQ(throwsFieldClass.Get(), field->GetType<true>());
190 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
191 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200192}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100193
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700194// Creates two proxy classes and check the art/mirror fields of their static fields.
195TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
196 ScopedObjectAccess soa(Thread::Current());
197 jobject jclass_loader = LoadDex("Interfaces");
198 StackHandleScope<7> hs(soa.Self());
199 Handle<mirror::ClassLoader> class_loader(
200 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
201
202 Handle<mirror::Class> proxyClass0;
203 Handle<mirror::Class> proxyClass1;
204 {
205 std::vector<mirror::Class*> interfaces;
206 proxyClass0 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy0", interfaces));
207 proxyClass1 = hs.NewHandle(GenerateProxyClass(soa, jclass_loader, "$Proxy1", interfaces));
208 }
209
210 ASSERT_TRUE(proxyClass0.Get() != nullptr);
211 ASSERT_TRUE(proxyClass0->IsProxyClass());
212 ASSERT_TRUE(proxyClass0->IsInitialized());
213 ASSERT_TRUE(proxyClass1.Get() != nullptr);
214 ASSERT_TRUE(proxyClass1->IsProxyClass());
215 ASSERT_TRUE(proxyClass1->IsInitialized());
216
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700217 LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr();
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700218 ASSERT_TRUE(static_fields0 != nullptr);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700219 ASSERT_EQ(2u, static_fields0->Length());
220 LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr();
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700221 ASSERT_TRUE(static_fields1 != nullptr);
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700222 ASSERT_EQ(2u, static_fields1->Length());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700223
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700224 EXPECT_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get());
225 EXPECT_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get());
226 EXPECT_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get());
227 EXPECT_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700228
229 Handle<mirror::Field> field00 =
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700230 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0->At(0), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700231 Handle<mirror::Field> field01 =
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700232 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0->At(1), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700233 Handle<mirror::Field> field10 =
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700234 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1->At(0), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700235 Handle<mirror::Field> field11 =
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700236 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1->At(1), true));
237 EXPECT_EQ(field00->GetArtField(), &static_fields0->At(0));
238 EXPECT_EQ(field01->GetArtField(), &static_fields0->At(1));
239 EXPECT_EQ(field10->GetArtField(), &static_fields1->At(0));
240 EXPECT_EQ(field11->GetArtField(), &static_fields1->At(1));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700241}
242
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100243} // namespace art