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