1 /*
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
17 #include <jni.h>
18 #include <vector>
19
20 #include "art_field-inl.h"
21 #include "base/pointer_size.h"
22 #include "common_runtime_test.h"
23 #include "mirror/field.h"
24 #include "proxy_test.h"
25 #include "scoped_thread_state_change-inl.h"
26 #include "well_known_classes.h"
27
28 namespace art HIDDEN {
29 namespace proxy_test {
30
31 class ProxyTest : public CommonRuntimeTest {
32 protected:
ProxyTest()33 ProxyTest() {
34 use_boot_image_ = true; // Make the Runtime creation cheaper.
35 }
36 };
37
38 // Creates a proxy class and check ClassHelper works correctly.
TEST_F(ProxyTest,ProxyClassHelper)39 TEST_F(ProxyTest, ProxyClassHelper) {
40 ScopedObjectAccess soa(Thread::Current());
41 jobject jclass_loader = LoadDex("Interfaces");
42 StackHandleScope<4> hs(soa.Self());
43 Handle<mirror::ClassLoader> class_loader(
44 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
45
46 Handle<mirror::Class> I = hs.NewHandle(FindClass("LInterfaces$I;", class_loader));
47 Handle<mirror::Class> J = hs.NewHandle(FindClass("LInterfaces$J;", class_loader));
48 ASSERT_TRUE(I != nullptr);
49 ASSERT_TRUE(J != nullptr);
50
51 std::vector<Handle<mirror::Class>> interfaces;
52 interfaces.push_back(I);
53 interfaces.push_back(J);
54 Handle<mirror::Class> proxy_class(hs.NewHandle(
55 GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces)));
56 interfaces.clear(); // Don't least possibly stale objects in the array as good practice.
57 ASSERT_TRUE(proxy_class != nullptr);
58 ASSERT_TRUE(proxy_class->IsProxyClass());
59 ASSERT_TRUE(proxy_class->IsInitialized());
60
61 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
62 EXPECT_OBJ_PTR_EQ(I.Get(), proxy_class->GetDirectInterface(0));
63 EXPECT_OBJ_PTR_EQ(J.Get(), proxy_class->GetDirectInterface(1));
64 std::string temp;
65 const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
66 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
67 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
68 }
69
70 // Creates a proxy class and check FieldHelper works correctly.
TEST_F(ProxyTest,ProxyFieldHelper)71 TEST_F(ProxyTest, ProxyFieldHelper) {
72 ScopedObjectAccess soa(Thread::Current());
73 jobject jclass_loader = LoadDex("Interfaces");
74 StackHandleScope<9> hs(soa.Self());
75 Handle<mirror::ClassLoader> class_loader(
76 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
77
78 Handle<mirror::Class> I = hs.NewHandle(FindClass("LInterfaces$I;", class_loader));
79 Handle<mirror::Class> J = hs.NewHandle(FindClass("LInterfaces$J;", class_loader));
80 ASSERT_TRUE(I != nullptr);
81 ASSERT_TRUE(J != nullptr);
82
83 Handle<mirror::Class> proxyClass;
84 {
85 std::vector<Handle<mirror::Class>> interfaces;
86 interfaces.push_back(I);
87 interfaces.push_back(J);
88 proxyClass = hs.NewHandle(
89 GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces));
90 }
91
92 ASSERT_TRUE(proxyClass != nullptr);
93 ASSERT_TRUE(proxyClass->IsProxyClass());
94 ASSERT_TRUE(proxyClass->IsInitialized());
95
96 LengthPrefixedArray<ArtField>* fields = proxyClass->GetFieldsPtr();
97 ASSERT_TRUE(fields != nullptr);
98 ASSERT_EQ(2u, proxyClass->NumFields());
99 ASSERT_EQ(0u, proxyClass->ComputeNumInstanceFields());
100
101 Handle<mirror::Class> interfacesFieldClass(
102 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
103 ASSERT_TRUE(interfacesFieldClass != nullptr);
104 Handle<mirror::Class> throwsFieldClass(
105 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
106 ASSERT_TRUE(throwsFieldClass != nullptr);
107
108 // Test "Class[] interfaces" field.
109 ArtField* field = &fields->At(0);
110 EXPECT_STREQ("interfaces", field->GetName());
111 EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
112 EXPECT_EQ("[Ljava/lang/Class;", field->GetTypeDescriptorView());
113 EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->ResolveType());
114 std::string temp;
115 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
116 EXPECT_FALSE(field->IsPrimitiveType());
117
118 // Test "Class[][] throws" field.
119 field = &fields->At(1);
120 EXPECT_STREQ("throws", field->GetName());
121 EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
122 EXPECT_EQ("[[Ljava/lang/Class;", field->GetTypeDescriptorView());
123 EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->ResolveType());
124 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
125 EXPECT_FALSE(field->IsPrimitiveType());
126 }
127
128 // Creates two proxy classes and check the art/mirror fields of their static fields.
TEST_F(ProxyTest,CheckArtMirrorFieldsOfProxyStaticFields)129 TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
130 ScopedObjectAccess soa(Thread::Current());
131 jobject jclass_loader = LoadDex("Interfaces");
132 StackHandleScope<7> hs(soa.Self());
133
134 Handle<mirror::Class> proxyClass0;
135 Handle<mirror::Class> proxyClass1;
136 {
137 std::vector<Handle<mirror::Class>> interfaces;
138 proxyClass0 = hs.NewHandle(
139 GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy0", interfaces));
140 proxyClass1 = hs.NewHandle(
141 GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1", interfaces));
142 }
143
144 ASSERT_TRUE(proxyClass0 != nullptr);
145 ASSERT_TRUE(proxyClass0->IsProxyClass());
146 ASSERT_TRUE(proxyClass0->IsInitialized());
147 ASSERT_TRUE(proxyClass1 != nullptr);
148 ASSERT_TRUE(proxyClass1->IsProxyClass());
149 ASSERT_TRUE(proxyClass1->IsInitialized());
150
151 LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetFieldsPtr();
152 ASSERT_TRUE(static_fields0 != nullptr);
153 ASSERT_EQ(2u, static_fields0->size());
154 LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetFieldsPtr();
155 ASSERT_TRUE(static_fields1 != nullptr);
156 ASSERT_EQ(2u, static_fields1->size());
157
158 EXPECT_OBJ_PTR_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get());
159 EXPECT_OBJ_PTR_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get());
160 EXPECT_OBJ_PTR_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get());
161 EXPECT_OBJ_PTR_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get());
162
163 ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
164 ASSERT_FALSE(Runtime::Current()->IsActiveTransaction());
165 Handle<mirror::Field> field00 =
166 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0->At(0), true));
167 Handle<mirror::Field> field01 =
168 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0->At(1), true));
169 Handle<mirror::Field> field10 =
170 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1->At(0), true));
171 Handle<mirror::Field> field11 =
172 hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1->At(1), true));
173 EXPECT_EQ(field00->GetArtField(), &static_fields0->At(0));
174 EXPECT_EQ(field01->GetArtField(), &static_fields0->At(1));
175 EXPECT_EQ(field10->GetArtField(), &static_fields1->At(0));
176 EXPECT_EQ(field11->GetArtField(), &static_fields1->At(1));
177 }
178
179 } // namespace proxy_test
180 } // namespace art
181