blob: d2bff2c19af6b41910e1125a46ace2365d3a26bf [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:
37 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
38 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(
48 soa.Self(),
49 *java_lang_dex_file_,
50 Runtime::Current()->GetLinearAlloc())));
Andreas Gampefa4333d2017-02-14 11:10:34 -080051 ASSERT_TRUE(dex_cache != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -070052
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070053 EXPECT_TRUE(dex_cache->StaticStringSize() == dex_cache->NumStrings()
54 || java_lang_dex_file_->NumStringIds() == dex_cache->NumStrings());
Vladimir Marko8d6768d2017-03-14 10:13:21 +000055 EXPECT_TRUE(dex_cache->StaticTypeSize() == dex_cache->NumResolvedTypes()
56 || java_lang_dex_file_->NumTypeIds() == dex_cache->NumResolvedTypes());
Vladimir Marko07bfbac2017-07-06 14:55:02 +010057 EXPECT_TRUE(dex_cache->StaticMethodSize() == dex_cache->NumResolvedMethods()
58 || java_lang_dex_file_->NumMethodIds() == dex_cache->NumResolvedMethods());
Vladimir Markof44d36c2017-03-14 14:18:46 +000059 EXPECT_TRUE(dex_cache->StaticArtFieldSize() == dex_cache->NumResolvedFields()
60 || java_lang_dex_file_->NumFieldIds() == dex_cache->NumResolvedFields());
Narayan Kamath269cb432016-10-28 10:19:54 +010061 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
62 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010063}
64
65TEST_F(DexCacheMethodHandlesTest, Open) {
66 ScopedObjectAccess soa(Thread::Current());
67 StackHandleScope<1> hs(soa.Self());
68 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
69 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070070 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
71 soa.Self(),
72 *java_lang_dex_file_,
73 Runtime::Current()->GetLinearAlloc())));
Narayan Kamath25352fc2016-08-03 12:46:58 +010074
75 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
76 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070077}
78
Mathieu Chartierd57d4542015-10-14 10:55:30 -070079TEST_F(DexCacheTest, LinearAlloc) {
80 ScopedObjectAccess soa(Thread::Current());
81 jobject jclass_loader(LoadDex("Main"));
82 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070083 StackHandleScope<1> hs(soa.Self());
84 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070085 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Narayan Kamath25352fc2016-08-03 12:46:58 +010086 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LMain;", class_loader);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070087 ASSERT_TRUE(klass != nullptr);
88 LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator();
Narayan Kamath25352fc2016-08-03 12:46:58 +010089 EXPECT_NE(linear_alloc, runtime_->GetLinearAlloc());
Mathieu Chartierd57d4542015-10-14 10:55:30 -070090 EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods()));
91}
92
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070093TEST_F(DexCacheTest, TestResolvedFieldAccess) {
94 ScopedObjectAccess soa(Thread::Current());
95 jobject jclass_loader(LoadDex("Packages"));
96 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070097 StackHandleScope<3> hs(soa.Self());
98 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070099 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700100 Handle<mirror::Class> klass1 =
Narayan Kamath25352fc2016-08-03 12:46:58 +0100101 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800102 ASSERT_TRUE(klass1 != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700103 Handle<mirror::Class> klass2 =
Narayan Kamath25352fc2016-08-03 12:46:58 +0100104 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800105 ASSERT_TRUE(klass2 != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700106 EXPECT_EQ(klass1->GetDexCache(), klass2->GetDexCache());
107
108 EXPECT_NE(klass1->NumStaticFields(), 0u);
109 for (ArtField& field : klass2->GetSFields()) {
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100110 EXPECT_FALSE(
111 klass1->ResolvedFieldAccessTest</*throw_on_failure*/ false>(
112 klass2.Get(),
113 &field,
114 klass1->GetDexCache(),
115 field.GetDexFieldIndex()));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700116 }
117}
118
Narayan Kamath25352fc2016-08-03 12:46:58 +0100119TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
120 ScopedObjectAccess soa(Thread::Current());
121 jobject jclass_loader(LoadDex("MethodTypes"));
122 ASSERT_TRUE(jclass_loader != nullptr);
123
124 StackHandleScope<5> hs(soa.Self());
125 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
126 soa.Decode<mirror::ClassLoader>(jclass_loader)));
127
128 Handle<mirror::Class> method_types(
129 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
130 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
131
Vladimir Markoba118822017-06-12 15:41:56 +0100132 ArtMethod* method1 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +0100133 "method1",
134 "(Ljava/lang/String;)Ljava/lang/String;",
135 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +0100136 ASSERT_TRUE(method1 != nullptr);
137 ASSERT_FALSE(method1->IsDirect());
138 ArtMethod* method2 = method_types->FindClassMethod(
Narayan Kamath25352fc2016-08-03 12:46:58 +0100139 "method2",
140 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
141 kRuntimePointerSize);
Vladimir Markoba118822017-06-12 15:41:56 +0100142 ASSERT_TRUE(method2 != nullptr);
143 ASSERT_FALSE(method2->IsDirect());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100144
145 const DexFile& dex_file = *(method1->GetDexFile());
146 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
147 class_linker_->FindDexCache(Thread::Current(), dex_file));
148
149 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
150 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
Narayan Kamath25352fc2016-08-03 12:46:58 +0100151 Handle<mirror::MethodType> method1_type = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +0100152 class_linker_->ResolveMethodType(soa.Self(),
Orion Hodsone7732be2017-10-11 14:35:20 +0100153 method1_id.proto_idx_,
154 dex_cache,
155 class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +0100156 Handle<mirror::MethodType> method2_type = hs.NewHandle(
Orion Hodsone7732be2017-10-11 14:35:20 +0100157 class_linker_->ResolveMethodType(soa.Self(),
Orion Hodsone7732be2017-10-11 14:35:20 +0100158 method2_id.proto_idx_,
159 dex_cache,
160 class_loader));
Narayan Kamath25352fc2016-08-03 12:46:58 +0100161 EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
162 EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
163
164 // The MethodTypes dex file contains a single interface with two abstract
165 // methods. It must therefore contain precisely two method IDs.
166 ASSERT_EQ(2u, dex_file.NumProtoIds());
167 ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypes());
168 MethodTypeDexCacheType* method_types_cache = dex_cache->GetResolvedMethodTypes();
169
170 for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
171 const MethodTypeDexCachePair pair = method_types_cache[i].load(std::memory_order_relaxed);
172 if (pair.index == method1_id.proto_idx_) {
173 ASSERT_EQ(method1_type.Get(), pair.object.Read());
174 } else if (pair.index == method2_id.proto_idx_) {
175 ASSERT_EQ(method2_type.Get(), pair.object.Read());
176 } else {
177 ASSERT_TRUE(false);
178 }
179 }
180}
181
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700183} // namespace art