blob: b89b20d1ca129c125bde027ebc683d8090f7d2f1 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070017#include "dex_cache.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080018
19#include <stdio.h>
20
Andreas Gampec6ea7d02017-02-01 16:46:28 -080021#include "art_method-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "class_linker.h"
23#include "common_runtime_test.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "handle_scope-inl.h"
Mathieu Chartierd57d4542015-10-14 10:55:30 -070025#include "linear_alloc.h"
26#include "mirror/class_loader-inl.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070027#include "mirror/dex_cache-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070030namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070032
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080033class DexCacheTest : public CommonRuntimeTest {};
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070034
Narayan Kamath25352fc2016-08-03 12:46:58 +010035class DexCacheMethodHandlesTest : public DexCacheTest {
36 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +010037 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Narayan Kamath25352fc2016-08-03 12:46:58 +010038 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +010039 }
40};
41
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042TEST_F(DexCacheTest, Open) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070047 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
David Srbeckye153a622021-03-02 15:07:26 +000048 soa.Self(), *java_lang_dex_file_, /*class_loader=*/nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080049 ASSERT_TRUE(dex_cache != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -070050
David Srbeckye153a622021-03-02 15:07:26 +000051 // The cache is initially empty.
52 EXPECT_EQ(0u, dex_cache->NumStrings());
53 EXPECT_EQ(0u, dex_cache->NumResolvedTypes());
54 EXPECT_EQ(0u, dex_cache->NumResolvedMethods());
55 EXPECT_EQ(0u, dex_cache->NumResolvedFields());
56 EXPECT_EQ(0u, dex_cache->NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010057}
58
59TEST_F(DexCacheMethodHandlesTest, Open) {
60 ScopedObjectAccess soa(Thread::Current());
61 StackHandleScope<1> hs(soa.Self());
62 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
63 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070064 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
David Srbeckye153a622021-03-02 15:07:26 +000065 soa.Self(), *java_lang_dex_file_, /*class_loader=*/nullptr)));
Narayan Kamath25352fc2016-08-03 12:46:58 +010066
David Srbeckye153a622021-03-02 15:07:26 +000067 EXPECT_EQ(0u, dex_cache->NumResolvedMethodTypes());
Mathieu Chartierd57d4542015-10-14 10:55:30 -070068}
69
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070070TEST_F(DexCacheTest, TestResolvedFieldAccess) {
71 ScopedObjectAccess soa(Thread::Current());
72 jobject jclass_loader(LoadDex("Packages"));
73 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070074 StackHandleScope<3> hs(soa.Self());
75 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070076 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070077 Handle<mirror::Class> klass1 =
Narayan Kamath25352fc2016-08-03 12:46:58 +010078 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -080079 ASSERT_TRUE(klass1 != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070080 Handle<mirror::Class> klass2 =
Narayan Kamath25352fc2016-08-03 12:46:58 +010081 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -080082 ASSERT_TRUE(klass2 != nullptr);
Vladimir Markoc524e9e2019-03-26 10:54:50 +000083 EXPECT_OBJ_PTR_EQ(klass1->GetDexCache(), klass2->GetDexCache());
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070084
85 EXPECT_NE(klass1->NumStaticFields(), 0u);
86 for (ArtField& field : klass2->GetSFields()) {
Vladimir Markof79aa7f2017-07-04 16:58:55 +010087 EXPECT_FALSE(
Andreas Gampe98ea9d92018-10-19 14:06:15 -070088 klass1->ResolvedFieldAccessTest</*throw_on_failure=*/ false>(
Vladimir Markof79aa7f2017-07-04 16:58:55 +010089 klass2.Get(),
90 &field,
91 klass1->GetDexCache(),
92 field.GetDexFieldIndex()));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070093 }
94}
95
Narayan Kamath25352fc2016-08-03 12:46:58 +010096TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
97 ScopedObjectAccess soa(Thread::Current());
98 jobject jclass_loader(LoadDex("MethodTypes"));
99 ASSERT_TRUE(jclass_loader != nullptr);
100
101 StackHandleScope<5> hs(soa.Self());
102 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
103 soa.Decode<mirror::ClassLoader>(jclass_loader)));
104
105 Handle<mirror::Class> method_types(
106 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
107 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
108
Vladimir Markoba118822017-06-12 15:41:56 +0100109 ArtMethod* method1 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +0100110 "method1",
111 "(Ljava/lang/String;)Ljava/lang/String;",
112 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +0100113 ASSERT_TRUE(method1 != nullptr);
114 ASSERT_FALSE(method1->IsDirect());
115 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +0100116 "method2",
117 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
118 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +0100119 ASSERT_TRUE(method2 != nullptr);
120 ASSERT_FALSE(method2->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100121
122 const DexFile& dex_file = *(method1->GetDexFile());
123 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
124 class_linker_->FindDexCache(Thread::Current(), dex_file));
125
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800126 const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
127 const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100128 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +0100129 class_linker_->ResolveMethodType(soa.Self(),
Orion Hodsone7732be2017-10-11 14:35:20 +0100130 method1_id.proto_idx_,
131 dex_cache,
132 class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +0100133 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +0100134 class_linker_->ResolveMethodType(soa.Self(),
Orion Hodsone7732be2017-10-11 14:35:20 +0100135 method2_id.proto_idx_,
136 dex_cache,
137 class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +0100138 EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
139 EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
140
141 // The MethodTypes dex file contains a single interface with two abstract
142 // methods. It must therefore contain precisely two method IDs.
143 ASSERT_EQ(2u, dex_file.NumProtoIds());
144 ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypes());
145 MethodTypeDexCacheType* method_types_cache = dex_cache->GetResolvedMethodTypes();
146
147 for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
148 const MethodTypeDexCachePair pair = method_types_cache[i].load(std::memory_order_relaxed);
Orion Hodson06d10a72018-05-14 08:53:38 +0100149 if (dex::ProtoIndex(pair.index) == method1_id.proto_idx_) {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100150 ASSERT_EQ(method1_type.Get(), pair.object.Read());
Orion Hodson06d10a72018-05-14 08:53:38 +0100151 } else if (dex::ProtoIndex(pair.index) == method2_id.proto_idx_) {
Narayan Kamath25352fc2016-08-03 12:46:58 +0100152 ASSERT_EQ(method2_type.Get(), pair.object.Read());
153 } else {
154 ASSERT_TRUE(false);
155 }
156 }
157}
158
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700160} // namespace art