blob: 5693f676460e9b8b5b20fcc512d22ad6079cac05 [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());
Vladimir Markoec786222016-12-20 16:24:13 +000054 EXPECT_TRUE(dex_cache->StaticTypeSize() == dex_cache->NumResolvedTypes()
55 || java_lang_dex_file_->NumTypeIds() == dex_cache->NumResolvedTypes());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070056 EXPECT_EQ(java_lang_dex_file_->NumMethodIds(), dex_cache->NumResolvedMethods());
57 EXPECT_EQ(java_lang_dex_file_->NumFieldIds(), dex_cache->NumResolvedFields());
Narayan Kamath269cb432016-10-28 10:19:54 +010058 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
59 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010060}
61
62TEST_F(DexCacheMethodHandlesTest, Open) {
63 ScopedObjectAccess soa(Thread::Current());
64 StackHandleScope<1> hs(soa.Self());
65 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
66 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070067 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
68 soa.Self(),
69 *java_lang_dex_file_,
70 Runtime::Current()->GetLinearAlloc())));
Narayan Kamath25352fc2016-08-03 12:46:58 +010071
72 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
73 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070074}
75
Mathieu Chartierd57d4542015-10-14 10:55:30 -070076TEST_F(DexCacheTest, LinearAlloc) {
77 ScopedObjectAccess soa(Thread::Current());
78 jobject jclass_loader(LoadDex("Main"));
79 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070080 StackHandleScope<1> hs(soa.Self());
81 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070082 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Narayan Kamath25352fc2016-08-03 12:46:58 +010083 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LMain;", class_loader);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070084 ASSERT_TRUE(klass != nullptr);
85 LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator();
Narayan Kamath25352fc2016-08-03 12:46:58 +010086 EXPECT_NE(linear_alloc, runtime_->GetLinearAlloc());
Mathieu Chartierd57d4542015-10-14 10:55:30 -070087 EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods()));
88}
89
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070090TEST_F(DexCacheTest, TestResolvedFieldAccess) {
91 ScopedObjectAccess soa(Thread::Current());
92 jobject jclass_loader(LoadDex("Packages"));
93 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070094 StackHandleScope<3> hs(soa.Self());
95 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070096 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070097 Handle<mirror::Class> klass1 =
Narayan Kamath25352fc2016-08-03 12:46:58 +010098 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070099 ASSERT_TRUE(klass1.Get() != nullptr);
100 Handle<mirror::Class> klass2 =
Narayan Kamath25352fc2016-08-03 12:46:58 +0100101 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700102 ASSERT_TRUE(klass2.Get() != nullptr);
103 EXPECT_EQ(klass1->GetDexCache(), klass2->GetDexCache());
104
105 EXPECT_NE(klass1->NumStaticFields(), 0u);
106 for (ArtField& field : klass2->GetSFields()) {
107 EXPECT_FALSE((
108 klass1->ResolvedFieldAccessTest</*throw_on_failure*/ false,
109 /*use_referrers_cache*/ false>(klass2.Get(),
110 &field,
111 field.GetDexFieldIndex(),
112 klass1->GetDexCache())));
113 }
114}
115
Narayan Kamath25352fc2016-08-03 12:46:58 +0100116TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
117 ScopedObjectAccess soa(Thread::Current());
118 jobject jclass_loader(LoadDex("MethodTypes"));
119 ASSERT_TRUE(jclass_loader != nullptr);
120
121 StackHandleScope<5> hs(soa.Self());
122 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
123 soa.Decode<mirror::ClassLoader>(jclass_loader)));
124
125 Handle<mirror::Class> method_types(
126 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
127 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
128
129 ArtMethod* method1 = method_types->FindVirtualMethod(
130 "method1",
131 "(Ljava/lang/String;)Ljava/lang/String;",
132 kRuntimePointerSize);
133 ArtMethod* method2 = method_types->FindVirtualMethod(
134 "method2",
135 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
136 kRuntimePointerSize);
137
138 const DexFile& dex_file = *(method1->GetDexFile());
139 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
140 class_linker_->FindDexCache(Thread::Current(), dex_file));
141
142 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
143 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
144
145 Handle<mirror::MethodType> method1_type = hs.NewHandle(
146 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
147 Handle<mirror::MethodType> method2_type = hs.NewHandle(
148 class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader));
149
150 EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
151 EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
152
153 // The MethodTypes dex file contains a single interface with two abstract
154 // methods. It must therefore contain precisely two method IDs.
155 ASSERT_EQ(2u, dex_file.NumProtoIds());
156 ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypes());
157 MethodTypeDexCacheType* method_types_cache = dex_cache->GetResolvedMethodTypes();
158
159 for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
160 const MethodTypeDexCachePair pair = method_types_cache[i].load(std::memory_order_relaxed);
161 if (pair.index == method1_id.proto_idx_) {
162 ASSERT_EQ(method1_type.Get(), pair.object.Read());
163 } else if (pair.index == method2_id.proto_idx_) {
164 ASSERT_EQ(method2_type.Get(), pair.object.Read());
165 } else {
166 ASSERT_TRUE(false);
167 }
168 }
169}
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700172} // namespace art