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