xref: /aosp_15_r20/art/runtime/proxy_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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   EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr);
97 
98   LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr();
99   ASSERT_TRUE(static_fields != nullptr);
100   ASSERT_EQ(2u, proxyClass->NumStaticFields());
101 
102   Handle<mirror::Class> interfacesFieldClass(
103       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
104   ASSERT_TRUE(interfacesFieldClass != nullptr);
105   Handle<mirror::Class> throwsFieldClass(
106       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
107   ASSERT_TRUE(throwsFieldClass != nullptr);
108 
109   // Test "Class[] interfaces" field.
110   ArtField* field = &static_fields->At(0);
111   EXPECT_STREQ("interfaces", field->GetName());
112   EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
113   EXPECT_EQ("[Ljava/lang/Class;", field->GetTypeDescriptorView());
114   EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->ResolveType());
115   std::string temp;
116   EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
117   EXPECT_FALSE(field->IsPrimitiveType());
118 
119   // Test "Class[][] throws" field.
120   field = &static_fields->At(1);
121   EXPECT_STREQ("throws", field->GetName());
122   EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
123   EXPECT_EQ("[[Ljava/lang/Class;", field->GetTypeDescriptorView());
124   EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->ResolveType());
125   EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
126   EXPECT_FALSE(field->IsPrimitiveType());
127 }
128 
129 // Creates two proxy classes and check the art/mirror fields of their static fields.
TEST_F(ProxyTest,CheckArtMirrorFieldsOfProxyStaticFields)130 TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
131   ScopedObjectAccess soa(Thread::Current());
132   jobject jclass_loader = LoadDex("Interfaces");
133   StackHandleScope<7> hs(soa.Self());
134 
135   Handle<mirror::Class> proxyClass0;
136   Handle<mirror::Class> proxyClass1;
137   {
138     std::vector<Handle<mirror::Class>> interfaces;
139     proxyClass0 = hs.NewHandle(
140         GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy0", interfaces));
141     proxyClass1 = hs.NewHandle(
142         GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1", interfaces));
143   }
144 
145   ASSERT_TRUE(proxyClass0 != nullptr);
146   ASSERT_TRUE(proxyClass0->IsProxyClass());
147   ASSERT_TRUE(proxyClass0->IsInitialized());
148   ASSERT_TRUE(proxyClass1 != nullptr);
149   ASSERT_TRUE(proxyClass1->IsProxyClass());
150   ASSERT_TRUE(proxyClass1->IsInitialized());
151 
152   LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr();
153   ASSERT_TRUE(static_fields0 != nullptr);
154   ASSERT_EQ(2u, static_fields0->size());
155   LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr();
156   ASSERT_TRUE(static_fields1 != nullptr);
157   ASSERT_EQ(2u, static_fields1->size());
158 
159   EXPECT_OBJ_PTR_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get());
160   EXPECT_OBJ_PTR_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get());
161   EXPECT_OBJ_PTR_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get());
162   EXPECT_OBJ_PTR_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get());
163 
164   ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
165   ASSERT_FALSE(Runtime::Current()->IsActiveTransaction());
166   Handle<mirror::Field> field00 =
167       hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0->At(0), true));
168   Handle<mirror::Field> field01 =
169       hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields0->At(1), true));
170   Handle<mirror::Field> field10 =
171       hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1->At(0), true));
172   Handle<mirror::Field> field11 =
173       hs.NewHandle(mirror::Field::CreateFromArtField(soa.Self(), &static_fields1->At(1), true));
174   EXPECT_EQ(field00->GetArtField(), &static_fields0->At(0));
175   EXPECT_EQ(field01->GetArtField(), &static_fields0->At(1));
176   EXPECT_EQ(field10->GetArtField(), &static_fields1->At(0));
177   EXPECT_EQ(field11->GetArtField(), &static_fields1->At(1));
178 }
179 
180 }  // namespace proxy_test
181 }  // namespace art
182