1 /*
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 */
16
17 #include "dex_cache.h"
18
19 #include <stdio.h>
20
21 #include "art_method-inl.h"
22 #include "class_linker.h"
23 #include "common_runtime_test.h"
24 #include "handle_scope-inl.h"
25 #include "linear_alloc.h"
26 #include "mirror/class_loader-inl.h"
27 #include "mirror/dex_cache-inl.h"
28 #include "scoped_thread_state_change-inl.h"
29
30 namespace art HIDDEN {
31 namespace mirror {
32
33 class DexCacheTest : public CommonRuntimeTest {
34 protected:
DexCacheTest()35 DexCacheTest() {
36 this->use_boot_image_ = true; // Make the Runtime creation cheaper.
37 }
38 };
39
40 class DexCacheMethodHandlesTest : public DexCacheTest {
41 protected:
SetUpRuntimeOptions(RuntimeOptions * options)42 void SetUpRuntimeOptions(RuntimeOptions* options) override {
43 CommonRuntimeTest::SetUpRuntimeOptions(options);
44 }
45 };
46
TEST_F(DexCacheTest,Open)47 TEST_F(DexCacheTest, Open) {
48 ScopedObjectAccess soa(Thread::Current());
49 StackHandleScope<1> hs(soa.Self());
50 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
51 Handle<DexCache> dex_cache(
52 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
53 soa.Self(), *java_lang_dex_file_, /*class_loader=*/nullptr)));
54 ASSERT_TRUE(dex_cache != nullptr);
55
56 // The cache is initially empty.
57 EXPECT_EQ(0u, dex_cache->NumStrings());
58 EXPECT_EQ(0u, dex_cache->NumResolvedTypes());
59 EXPECT_EQ(0u, dex_cache->NumResolvedMethods());
60 EXPECT_EQ(0u, dex_cache->NumResolvedFields());
61 EXPECT_EQ(0u, dex_cache->NumResolvedMethodTypes());
62 }
63
TEST_F(DexCacheMethodHandlesTest,Open)64 TEST_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(
69 hs.NewHandle(class_linker_->AllocAndInitializeDexCache(
70 soa.Self(), *java_lang_dex_file_, /*class_loader=*/nullptr)));
71
72 EXPECT_EQ(0u, dex_cache->NumResolvedMethodTypes());
73 }
74
TEST_F(DexCacheTest,TestResolvedFieldAccess)75 TEST_F(DexCacheTest, TestResolvedFieldAccess) {
76 ScopedObjectAccess soa(Thread::Current());
77 jobject jclass_loader(LoadDex("Packages"));
78 ASSERT_TRUE(jclass_loader != nullptr);
79 StackHandleScope<3> hs(soa.Self());
80 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
81 soa.Decode<mirror::ClassLoader>(jclass_loader)));
82 Handle<mirror::Class> klass1 = hs.NewHandle(FindClass("Lpackage1/Package1;", class_loader));
83 ASSERT_TRUE(klass1 != nullptr);
84 Handle<mirror::Class> klass2 = hs.NewHandle(FindClass("Lpackage2/Package2;", class_loader));
85 ASSERT_TRUE(klass2 != nullptr);
86 EXPECT_OBJ_PTR_EQ(klass1->GetDexCache(), klass2->GetDexCache());
87
88 EXPECT_NE(klass1->NumStaticFields(), 0u);
89 for (ArtField& field : klass2->GetSFields()) {
90 EXPECT_FALSE(
91 klass1->ResolvedFieldAccessTest</*throw_on_failure=*/ false>(
92 klass2.Get(),
93 &field,
94 klass1->GetDexCache(),
95 field.GetDexFieldIndex()));
96 }
97 }
98
TEST_F(DexCacheMethodHandlesTest,TestResolvedMethodTypes)99 TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
100 ScopedObjectAccess soa(Thread::Current());
101 jobject jclass_loader(LoadDex("MethodTypes"));
102 ASSERT_TRUE(jclass_loader != nullptr);
103
104 StackHandleScope<5> hs(soa.Self());
105 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
106 soa.Decode<mirror::ClassLoader>(jclass_loader)));
107
108 Handle<mirror::Class> method_types = hs.NewHandle(FindClass("LMethodTypes;", class_loader));
109 class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
110
111 ArtMethod* method1 = method_types->FindClassMethod(
112 "method1",
113 "(Ljava/lang/String;)Ljava/lang/String;",
114 kRuntimePointerSize);
115 ASSERT_TRUE(method1 != nullptr);
116 ASSERT_FALSE(method1->IsDirect());
117 ArtMethod* method2 = method_types->FindClassMethod(
118 "method2",
119 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
120 kRuntimePointerSize);
121 ASSERT_TRUE(method2 != nullptr);
122 ASSERT_FALSE(method2->IsDirect());
123
124 const DexFile& dex_file = *(method1->GetDexFile());
125 Handle<mirror::DexCache> dex_cache = hs.NewHandle(
126 class_linker_->FindDexCache(Thread::Current(), dex_file));
127
128 const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
129 const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
130 Handle<mirror::MethodType> method1_type = hs.NewHandle(
131 class_linker_->ResolveMethodType(soa.Self(),
132 method1_id.proto_idx_,
133 dex_cache,
134 class_loader));
135 Handle<mirror::MethodType> method2_type = hs.NewHandle(
136 class_linker_->ResolveMethodType(soa.Self(),
137 method2_id.proto_idx_,
138 dex_cache,
139 class_loader));
140 EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
141 EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
142
143 // The MethodTypes dex file contains a single interface with two abstract
144 // methods. It must therefore contain precisely two method IDs.
145 ASSERT_EQ(2u, dex_file.NumProtoIds());
146 ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypesArray());
147 ASSERT_EQ(0u, dex_cache->NumResolvedMethodTypes());
148 auto* method_types_cache = dex_cache->GetResolvedMethodTypesArray();
149
150 for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
151 auto* method_type = method_types_cache->Get(i);
152 if (dex::ProtoIndex(i) == method1_id.proto_idx_) {
153 ASSERT_EQ(method1_type.Get(), method_type);
154 } else if (dex::ProtoIndex(i) == method2_id.proto_idx_) {
155 ASSERT_EQ(method2_type.Get(), method_type);
156 } else {
157 ASSERT_TRUE(false);
158 }
159 }
160 }
161
162 } // namespace mirror
163 } // namespace art
164