blob: 5b1ba8d010533f7d1d8d47e34e9872aac3019b56 [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"
Mathieu Chartierd57d4542015-10-14 10:55:30 -070024#include "linear_alloc.h"
25#include "mirror/class_loader-inl.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070026#include "mirror/dex_cache-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-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());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070057 EXPECT_EQ(java_lang_dex_file_->NumMethodIds(), dex_cache->NumResolvedMethods());
Vladimir Markof44d36c2017-03-14 14:18:46 +000058 EXPECT_TRUE(dex_cache->StaticArtFieldSize() == dex_cache->NumResolvedFields()
59 || java_lang_dex_file_->NumFieldIds() == dex_cache->NumResolvedFields());
Narayan Kamath269cb432016-10-28 10:19:54 +010060 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
61 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Narayan Kamath25352fc2016-08-03 12:46:58 +010062}
63
64TEST_F(DexCacheMethodHandlesTest, Open) {
65 ScopedObjectAccess soa(Thread::Current());
66 StackHandleScope<1> hs(soa.Self());
67 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
68 Handle<DexCache> dex_cache(
Mathieu Chartier6c60d842016-09-15 10:24:43 -070069 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
70 soa.Self(),
71 *java_lang_dex_file_,
72 Runtime::Current()->GetLinearAlloc())));
Narayan Kamath25352fc2016-08-03 12:46:58 +010073
74 EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes()
75 || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070076}
77
Mathieu Chartierd57d4542015-10-14 10:55:30 -070078TEST_F(DexCacheTest, LinearAlloc) {
79 ScopedObjectAccess soa(Thread::Current());
80 jobject jclass_loader(LoadDex("Main"));
81 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070082 StackHandleScope<1> hs(soa.Self());
83 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070084 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Narayan Kamath25352fc2016-08-03 12:46:58 +010085 mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LMain;", class_loader);
Mathieu Chartierd57d4542015-10-14 10:55:30 -070086 ASSERT_TRUE(klass != nullptr);
87 LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator();
Narayan Kamath25352fc2016-08-03 12:46:58 +010088 EXPECT_NE(linear_alloc, runtime_->GetLinearAlloc());
Mathieu Chartierd57d4542015-10-14 10:55:30 -070089 EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods()));
90}
91
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070092TEST_F(DexCacheTest, TestResolvedFieldAccess) {
93 ScopedObjectAccess soa(Thread::Current());
94 jobject jclass_loader(LoadDex("Packages"));
95 ASSERT_TRUE(jclass_loader != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070096 StackHandleScope<3> hs(soa.Self());
97 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -070098 soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070099 Handle<mirror::Class> klass1 =
Narayan Kamath25352fc2016-08-03 12:46:58 +0100100 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800101 ASSERT_TRUE(klass1 != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700102 Handle<mirror::Class> klass2 =
Narayan Kamath25352fc2016-08-03 12:46:58 +0100103 hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800104 ASSERT_TRUE(klass2 != nullptr);
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700105 EXPECT_EQ(klass1->GetDexCache(), klass2->GetDexCache());
106
107 EXPECT_NE(klass1->NumStaticFields(), 0u);
108 for (ArtField& field : klass2->GetSFields()) {
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100109 EXPECT_FALSE(
110 klass1->ResolvedFieldAccessTest</*throw_on_failure*/ false>(
111 klass2.Get(),
112 &field,
113 klass1->GetDexCache(),
114 field.GetDexFieldIndex()));
Mathieu Chartier279ac5c2016-09-08 17:34:25 -0700115 }
116}
117
Narayan Kamath25352fc2016-08-03 12:46:58 +0100118TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
119 ScopedObjectAccess soa(Thread::Current());
120 jobject jclass_loader(LoadDex("MethodTypes"));
121 ASSERT_TRUE(jclass_loader != nullptr);
122
123 StackHandleScope<5> hs(soa.Self());
124 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
125 soa.Decode<mirror::ClassLoader>(jclass_loader)));
126
127 Handle<mirror::Class> method_types(
128 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
129 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
130
131 ArtMethod* method1 = method_types->FindVirtualMethod(
132 "method1",
133 "(Ljava/lang/String;)Ljava/lang/String;",
134 kRuntimePointerSize);
135 ArtMethod* method2 = method_types->FindVirtualMethod(
136 "method2",
137 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
138 kRuntimePointerSize);
139
140 const DexFile& dex_file = *(method1->GetDexFile());
141 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
142 class_linker_->FindDexCache(Thread::Current(), dex_file));
143
144 const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
145 const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
146
147 Handle<mirror::MethodType> method1_type = hs.NewHandle(
148 class_linker_->ResolveMethodType(dex_file, method1_id.proto_idx_, dex_cache, class_loader));
149 Handle<mirror::MethodType> method2_type = hs.NewHandle(
150 class_linker_->ResolveMethodType(dex_file, method2_id.proto_idx_, dex_cache, class_loader));
151
152 EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
153 EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
154
155 // The MethodTypes dex file contains a single interface with two abstract
156 // methods. It must therefore contain precisely two method IDs.
157 ASSERT_EQ(2u, dex_file.NumProtoIds());
158 ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypes());
159 MethodTypeDexCacheType* method_types_cache = dex_cache->GetResolvedMethodTypes();
160
161 for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
162 const MethodTypeDexCachePair pair = method_types_cache[i].load(std::memory_order_relaxed);
163 if (pair.index == method1_id.proto_idx_) {
164 ASSERT_EQ(method1_type.Get(), pair.object.Read());
165 } else if (pair.index == method2_id.proto_idx_) {
166 ASSERT_EQ(method2_type.Get(), pair.object.Read());
167 } else {
168 ASSERT_TRUE(false);
169 }
170 }
171}
172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700174} // namespace art