blob: 8f978e122cfb037e9d50a9270948dc7ac49c1ded [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
21#include "class_linker.h"
22#include "common_runtime_test.h"
Mathieu Chartierd57d4542015-10-14 10:55:30 -070023#include "linear_alloc.h"
24#include "mirror/class_loader-inl.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070025#include "mirror/dex_cache-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026#include "handle_scope-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070028
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070031
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032class DexCacheTest : public CommonRuntimeTest {};
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070033
Narayan Kamath25352fc2016-08-03 12:46:58 +010034class DexCacheMethodHandlesTest : public DexCacheTest {
35 protected:
36 virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
37 CommonRuntimeTest::SetUpRuntimeOptions(options);
Narayan Kamath25352fc2016-08-03 12:46:58 +010038 }
39};
40
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070041TEST_F(DexCacheTest, Open) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070044 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070046 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
47 soa.Self(),
48 *java_lang_dex_file_,
49 Runtime::Current()->GetLinearAlloc())));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070050 ASSERT_TRUE(dex_cache.Get() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -070051
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070052 EXPECT_TRUE(dex_cache->StaticStringSize() == dex_cache->NumStrings()
53 || java_lang_dex_file_->NumStringIds() == dex_cache->NumStrings());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070054 EXPECT_EQ(java_lang_dex_file_->NumTypeIds(), dex_cache->NumResolvedTypes());
55 EXPECT_EQ(java_lang_dex_file_->NumMethodIds(), dex_cache->NumResolvedMethods());
56 EXPECT_EQ(java_lang_dex_file_->NumFieldIds(), dex_cache->NumResolvedFields());
Narayan Kamath269cb432016-10-28 10:19:54 +010057 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
58 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010059}
60
61TEST_F(DexCacheMethodHandlesTest, Open) {
62 ScopedObjectAccess soa(Thread::Current());
63 StackHandleScope<1> hs(soa.Self());
64 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
65 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070066 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
67 soa.Self(),
68 *java_lang_dex_file_,
69 Runtime::Current()->GetLinearAlloc())));
Narayan Kamath25352fc2016-08-03 12:46:58 +010070
71 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
72 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070073}
74
Mathieu Chartierd57d4542015-10-14 10:55:30 -070075TEST_F(DexCacheTest, LinearAlloc) {
76 ScopedObjectAccess soa(Thread::Current());
77 jobject jclass_loader(LoadDex("Main"));
78 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070079 StackHandleScope<1> hs(soa.Self());
80 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070081 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Narayan Kamath25352fc2016-08-03 12:46:58 +010082 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LMain;", class_loader);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070083 ASSERT_TRUE(klass != nullptr);
84 LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator();
Narayan Kamath25352fc2016-08-03 12:46:58 +010085 EXPECT_NE(linear_alloc, runtime_->GetLinearAlloc());
Mathieu Chartierd57d4542015-10-14 10:55:30 -070086 EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods()));
87}
88
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070089TEST_F(DexCacheTest, TestResolvedFieldAccess) {
90 ScopedObjectAccess soa(Thread::Current());
91 jobject jclass_loader(LoadDex("Packages"));
92 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070093 StackHandleScope<3> hs(soa.Self());
94 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070095 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070096 Handle<mirror::Class> klass1 =
Narayan Kamath25352fc2016-08-03 12:46:58 +010097 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070098 ASSERT_TRUE(klass1.Get() != nullptr);
99 Handle<mirror::Class> klass2 =
Narayan Kamath25352fc2016-08-03 12:46:58 +0100100 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700101 ASSERT_TRUE(klass2.Get() != nullptr);
102 EXPECT_EQ(klass1->GetDexCache(), klass2->GetDexCache());
103
104 EXPECT_NE(klass1->NumStaticFields(), 0u);
105 for (ArtField& field : klass2->GetSFields()) {
106 EXPECT_FALSE((
107 klass1->ResolvedFieldAccessTest</*throw_on_failure*/ false,
108 /*use_referrers_cache*/ false>(klass2.Get(),
109 &field,
110 field.GetDexFieldIndex(),
111 klass1->GetDexCache())));
112 }
113}
114
Narayan Kamath25352fc2016-08-03 12:46:58 +0100115TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
116 ScopedObjectAccess soa(Thread::Current());
117 jobject jclass_loader(LoadDex("MethodTypes"));
118 ASSERT_TRUE(jclass_loader != nullptr);
119
120 StackHandleScope<5> hs(soa.Self());
121 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
122 soa.Decode<mirror::ClassLoader>(jclass_loader)));
123
124 Handle<mirror::Class> method_types(
125 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
126 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
127
128 ArtMethod* method1 = method_types->FindVirtualMethod(
129 "method1",
130 "(Ljava/lang/String;)Ljava/lang/String;",
131 kRuntimePointerSize);
132 ArtMethod* method2 = method_types->FindVirtualMethod(
133 "method2",
134 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
135 kRuntimePointerSize);
136
137 const DexFile& dex_file = *(method1->GetDexFile());
138 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
139 class_linker_->FindDexCache(Thread::Current(), dex_file));
140
141 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
142 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
143
144 Handle<mirror::MethodType> method1_type = hs.NewHandle(
145 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
146 Handle<mirror::MethodType> method2_type = hs.NewHandle(
147 class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader));
148
149 EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
150 EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
151
152 // The MethodTypes dex file contains a single interface with two abstract
153 // methods. It must therefore contain precisely two method IDs.
154 ASSERT_EQ(2u, dex_file.NumProtoIds());
155 ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypes());
156 MethodTypeDexCacheType* method_types_cache = dex_cache->GetResolvedMethodTypes();
157
158 for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
159 const MethodTypeDexCachePair pair = method_types_cache[i].load(std::memory_order_relaxed);
160 if (pair.index == method1_id.proto_idx_) {
161 ASSERT_EQ(method1_type.Get(), pair.object.Read());
162 } else if (pair.index == method2_id.proto_idx_) {
163 ASSERT_EQ(method2_type.Get(), pair.object.Read());
164 } else {
165 ASSERT_TRUE(false);
166 }
167 }
168}
169
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700171} // namespace art