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