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