blob: b78855fd3df86e84f62f51aef983a183bd4f1b7d [file] [log] [blame]
Sebastien Hertzb7054ba2014-03-13 11:52:31 +01001/*
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 Hertzb7054ba2014-03-13 11:52:31 +010017#include <jni.h>
18#include <vector>
19
Mathieu Chartierc7853442015-03-27 14:35:38 -070020#include "art_field-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Vladimir Marko2b076df2019-02-20 11:27:52 +000022#include "common_runtime_test.h"
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -070023#include "mirror/field-inl.h"
David Brazdil1f9d3c32018-05-02 16:53:06 +010024#include "proxy_test.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Alex Light6cae5ea2018-06-07 17:07:02 -070026#include "well_known_classes.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010028namespace art {
David Brazdil1f9d3c32018-05-02 16:53:06 +010029namespace proxy_test {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010030
Alex Light6cae5ea2018-06-07 17:07:02 -070031class ProxyTest : public CommonRuntimeTest {
32 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010033 void SetUp() override {
Alex Light6cae5ea2018-06-07 17:07:02 -070034 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 Hertzb7054ba2014-03-13 11:52:31 +010044
45// Creates a proxy class and check ClassHelper works correctly.
46TEST_F(ProxyTest, ProxyClassHelper) {
47 ScopedObjectAccess soa(Thread::Current());
48 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartierf8322842014-05-16 10:59:25 -070049 StackHandleScope<4> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070050 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -070051 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010052
Mathieu Chartierf8322842014-05-16 10:59:25 -070053 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 Gampefa4333d2017-02-14 11:10:34 -080057 ASSERT_TRUE(I != nullptr);
58 ASSERT_TRUE(J != nullptr);
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010059
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010060 std::vector<Handle<mirror::Class>> interfaces;
61 interfaces.push_back(I);
62 interfaces.push_back(J);
Mathieu Chartierf8322842014-05-16 10:59:25 -070063 Handle<mirror::Class> proxy_class(hs.NewHandle(
David Brazdil1f9d3c32018-05-02 16:53:06 +010064 GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces)));
Mathieu Chartierf8322842014-05-16 10:59:25 -070065 interfaces.clear(); // Don't least possibly stale objects in the array as good practice.
Andreas Gampefa4333d2017-02-14 11:10:34 -080066 ASSERT_TRUE(proxy_class != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +000067 ASSERT_TRUE(proxy_class->IsProxyClass());
Mathieu Chartierf8322842014-05-16 10:59:25 -070068 ASSERT_TRUE(proxy_class->IsInitialized());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010069
Sebastien Hertz4206eb52014-06-05 10:15:45 +020070 EXPECT_EQ(2U, proxy_class->NumDirectInterfaces()); // Interfaces$I and Interfaces$J.
Vladimir Marko19a4d372016-12-08 14:41:46 +000071 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 Rogers1ff3c982014-08-12 02:30:58 -070073 std::string temp;
74 const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
75 EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
Sebastien Hertz4206eb52014-06-05 10:15:45 +020076 EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
Sebastien Hertzb7054ba2014-03-13 11:52:31 +010077}
78
Sebastien Hertz80989a62014-04-01 14:39:44 +020079// Creates a proxy class and check FieldHelper works correctly.
80TEST_F(ProxyTest, ProxyFieldHelper) {
81 ScopedObjectAccess soa(Thread::Current());
82 jobject jclass_loader = LoadDex("Interfaces");
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070083 StackHandleScope<9> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -070085 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Sebastien Hertz80989a62014-04-01 14:39:44 +020086
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070087 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 Gampefa4333d2017-02-14 11:10:34 -080091 ASSERT_TRUE(I != nullptr);
92 ASSERT_TRUE(J != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +020093
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070094 Handle<mirror::Class> proxyClass;
95 {
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010096 std::vector<Handle<mirror::Class>> interfaces;
97 interfaces.push_back(I);
98 interfaces.push_back(J);
David Brazdil1f9d3c32018-05-02 16:53:06 +010099 proxyClass = hs.NewHandle(
100 GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces));
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700101 }
102
Andreas Gampefa4333d2017-02-14 11:10:34 -0800103 ASSERT_TRUE(proxyClass != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000104 ASSERT_TRUE(proxyClass->IsProxyClass());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200105 ASSERT_TRUE(proxyClass->IsInitialized());
106
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700107 EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200108
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700109 LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700110 ASSERT_TRUE(static_fields != nullptr);
111 ASSERT_EQ(2u, proxyClass->NumStaticFields());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200112
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700113 Handle<mirror::Class> interfacesFieldClass(
114 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800115 ASSERT_TRUE(interfacesFieldClass != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700116 Handle<mirror::Class> throwsFieldClass(
117 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800118 ASSERT_TRUE(throwsFieldClass != nullptr);
Sebastien Hertz80989a62014-04-01 14:39:44 +0200119
120 // Test "Class[] interfaces" field.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700121 ArtField* field = &static_fields->At(0);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700122 EXPECT_STREQ("interfaces", field->GetName());
123 EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000124 EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->ResolveType());
Ian Rogers08f1f502014-12-02 15:04:37 -0800125 std::string temp;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700126 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
127 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200128
129 // Test "Class[][] throws" field.
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700130 field = &static_fields->At(1);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700131 EXPECT_STREQ("throws", field->GetName());
132 EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000133 EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->ResolveType());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700134 EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
135 EXPECT_FALSE(field->IsPrimitiveType());
Sebastien Hertz80989a62014-04-01 14:39:44 +0200136}
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100137
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700138// Creates two proxy classes and check the art/mirror fields of their static fields.
139TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
140 ScopedObjectAccess soa(Thread::Current());
141 jobject jclass_loader = LoadDex("Interfaces");
142 StackHandleScope<7> hs(soa.Self());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700143
144 Handle<mirror::Class> proxyClass0;
145 Handle<mirror::Class> proxyClass1;
146 {
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100147 std::vector<Handle<mirror::Class>> interfaces;
David Brazdil1f9d3c32018-05-02 16:53:06 +0100148 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 Yamauchi2debd802015-05-20 15:51:29 -0700152 }
153
Andreas Gampefa4333d2017-02-14 11:10:34 -0800154 ASSERT_TRUE(proxyClass0 != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000155 ASSERT_TRUE(proxyClass0->IsProxyClass());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700156 ASSERT_TRUE(proxyClass0->IsInitialized());
Andreas Gampefa4333d2017-02-14 11:10:34 -0800157 ASSERT_TRUE(proxyClass1 != nullptr);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000158 ASSERT_TRUE(proxyClass1->IsProxyClass());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700159 ASSERT_TRUE(proxyClass1->IsInitialized());
160
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700161 LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr();
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700162 ASSERT_TRUE(static_fields0 != nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +0100163 ASSERT_EQ(2u, static_fields0->size());
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700164 LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr();
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700165 ASSERT_TRUE(static_fields1 != nullptr);
Vladimir Marko35831e82015-09-11 11:59:18 +0100166 ASSERT_EQ(2u, static_fields1->size());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700167
Mathieu Chartierf8ac97f2016-10-05 15:56:52 -0700168 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 Yamauchi2debd802015-05-20 15:51:29 -0700172
Andreas Gampe542451c2016-07-26 09:02:02 -0700173 ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700174 ASSERT_FALSE(Runtime::Current()->IsActiveTransaction());
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700175 Handle<mirror::Field> field00 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700176 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700177 soa.Self(), &static_fields0->At(0), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700178 Handle<mirror::Field> field01 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700179 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700180 soa.Self(), &static_fields0->At(1), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700181 Handle<mirror::Field> field10 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700182 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700183 soa.Self(), &static_fields1->At(0), true));
Hiroshi Yamauchi2debd802015-05-20 15:51:29 -0700184 Handle<mirror::Field> field11 =
Andreas Gampe542451c2016-07-26 09:02:02 -0700185 hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -0700186 soa.Self(), &static_fields1->At(1), true));
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700187 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 Yamauchi2debd802015-05-20 15:51:29 -0700191}
192
David Brazdil1f9d3c32018-05-02 16:53:06 +0100193} // namespace proxy_test
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100194} // namespace art