Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 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 | |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 17 | #include <jni.h> |
| 18 | #include <vector> |
| 19 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 20 | #include "art_field-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 22 | #include "common_compiler_test.h" |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 23 | #include "mirror/field-inl.h" |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 24 | #include "proxy_test.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 26 | |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 27 | namespace art { |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 28 | namespace proxy_test { |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 29 | |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 30 | class ProxyTest : public CommonRuntimeTest {}; |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 31 | |
| 32 | // Creates a proxy class and check ClassHelper works correctly. |
| 33 | TEST_F(ProxyTest, ProxyClassHelper) { |
| 34 | ScopedObjectAccess soa(Thread::Current()); |
| 35 | jobject jclass_loader = LoadDex("Interfaces"); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 36 | StackHandleScope<4> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 37 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 39 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 40 | Handle<mirror::Class> I(hs.NewHandle( |
| 41 | class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 42 | Handle<mirror::Class> J(hs.NewHandle( |
| 43 | class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 44 | ASSERT_TRUE(I != nullptr); |
| 45 | ASSERT_TRUE(J != nullptr); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 46 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 47 | std::vector<mirror::Class*> interfaces; |
| 48 | interfaces.push_back(I.Get()); |
| 49 | interfaces.push_back(J.Get()); |
| 50 | Handle<mirror::Class> proxy_class(hs.NewHandle( |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 51 | GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 52 | interfaces.clear(); // Don't least possibly stale objects in the array as good practice. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 53 | ASSERT_TRUE(proxy_class != nullptr); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 54 | ASSERT_TRUE(proxy_class->IsProxyClass()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 55 | ASSERT_TRUE(proxy_class->IsInitialized()); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 56 | |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 57 | EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J. |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 58 | EXPECT_OBJ_PTR_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 0)); |
| 59 | EXPECT_OBJ_PTR_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 1)); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 60 | std::string temp; |
| 61 | const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp); |
| 62 | EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 63 | EXPECT_EQ(nullptr, proxy_class->GetSourceFile()); |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 66 | // Creates a proxy class and check FieldHelper works correctly. |
| 67 | TEST_F(ProxyTest, ProxyFieldHelper) { |
| 68 | ScopedObjectAccess soa(Thread::Current()); |
| 69 | jobject jclass_loader = LoadDex("Interfaces"); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 70 | StackHandleScope<9> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 71 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 72 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 73 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 74 | Handle<mirror::Class> I(hs.NewHandle( |
| 75 | class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader))); |
| 76 | Handle<mirror::Class> J(hs.NewHandle( |
| 77 | class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 78 | ASSERT_TRUE(I != nullptr); |
| 79 | ASSERT_TRUE(J != nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 80 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 81 | Handle<mirror::Class> proxyClass; |
| 82 | { |
| 83 | std::vector<mirror::Class*> interfaces; |
| 84 | interfaces.push_back(I.Get()); |
| 85 | interfaces.push_back(J.Get()); |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 86 | proxyClass = hs.NewHandle( |
| 87 | GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces)); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 90 | ASSERT_TRUE(proxyClass != nullptr); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 91 | ASSERT_TRUE(proxyClass->IsProxyClass()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 92 | ASSERT_TRUE(proxyClass->IsInitialized()); |
| 93 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 95 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 96 | LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 97 | ASSERT_TRUE(static_fields != nullptr); |
| 98 | ASSERT_EQ(2u, proxyClass->NumStaticFields()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 99 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 100 | Handle<mirror::Class> interfacesFieldClass( |
| 101 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 102 | ASSERT_TRUE(interfacesFieldClass != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 103 | Handle<mirror::Class> throwsFieldClass( |
| 104 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 105 | ASSERT_TRUE(throwsFieldClass != nullptr); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 106 | |
| 107 | // Test "Class[] interfaces" field. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 108 | ArtField* field = &static_fields->At(0); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 109 | EXPECT_STREQ("interfaces", field->GetName()); |
| 110 | EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 111 | EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->ResolveType()); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 112 | std::string temp; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 113 | EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp)); |
| 114 | EXPECT_FALSE(field->IsPrimitiveType()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 115 | |
| 116 | // Test "Class[][] throws" field. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 117 | field = &static_fields->At(1); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 118 | EXPECT_STREQ("throws", field->GetName()); |
| 119 | EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 120 | EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->ResolveType()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 121 | EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp)); |
| 122 | EXPECT_FALSE(field->IsPrimitiveType()); |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 123 | } |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 124 | |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 125 | // Creates two proxy classes and check the art/mirror fields of their static fields. |
| 126 | TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) { |
| 127 | ScopedObjectAccess soa(Thread::Current()); |
| 128 | jobject jclass_loader = LoadDex("Interfaces"); |
| 129 | StackHandleScope<7> hs(soa.Self()); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 130 | |
| 131 | Handle<mirror::Class> proxyClass0; |
| 132 | Handle<mirror::Class> proxyClass1; |
| 133 | { |
| 134 | std::vector<mirror::Class*> interfaces; |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 135 | proxyClass0 = hs.NewHandle( |
| 136 | GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy0", interfaces)); |
| 137 | proxyClass1 = hs.NewHandle( |
| 138 | GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1", interfaces)); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 141 | ASSERT_TRUE(proxyClass0 != nullptr); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 142 | ASSERT_TRUE(proxyClass0->IsProxyClass()); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 143 | ASSERT_TRUE(proxyClass0->IsInitialized()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 144 | ASSERT_TRUE(proxyClass1 != nullptr); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 145 | ASSERT_TRUE(proxyClass1->IsProxyClass()); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 146 | ASSERT_TRUE(proxyClass1->IsInitialized()); |
| 147 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 148 | LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr(); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 149 | ASSERT_TRUE(static_fields0 != nullptr); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 150 | ASSERT_EQ(2u, static_fields0->size()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 151 | LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr(); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 152 | ASSERT_TRUE(static_fields1 != nullptr); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 153 | ASSERT_EQ(2u, static_fields1->size()); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 154 | |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 155 | EXPECT_OBJ_PTR_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get()); |
| 156 | EXPECT_OBJ_PTR_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get()); |
| 157 | EXPECT_OBJ_PTR_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get()); |
| 158 | EXPECT_OBJ_PTR_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get()); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 159 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 160 | ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 161 | ASSERT_FALSE(Runtime::Current()->IsActiveTransaction()); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 162 | Handle<mirror::Field> field00 = |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 163 | hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 164 | soa.Self(), &static_fields0->At(0), true)); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 165 | Handle<mirror::Field> field01 = |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 166 | hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 167 | soa.Self(), &static_fields0->At(1), true)); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 168 | Handle<mirror::Field> field10 = |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 169 | hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 170 | soa.Self(), &static_fields1->At(0), true)); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 171 | Handle<mirror::Field> field11 = |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 172 | hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 173 | soa.Self(), &static_fields1->At(1), true)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 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)); |
Hiroshi Yamauchi | 2debd80 | 2015-05-20 15:51:29 -0700 | [diff] [blame] | 178 | } |
| 179 | |
David Brazdil | 1f9d3c3 | 2018-05-02 16:53:06 +0100 | [diff] [blame^] | 180 | } // namespace proxy_test |
Sebastien Hertz | b7054ba | 2014-03-13 11:52:31 +0100 | [diff] [blame] | 181 | } // namespace art |