• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
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 =
83       hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
84   ASSERT_TRUE(klass1 != nullptr);
85   Handle<mirror::Class> klass2 =
86       hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
87   ASSERT_TRUE(klass2 != nullptr);
88   EXPECT_OBJ_PTR_EQ(klass1->GetDexCache(), klass2->GetDexCache());
89 
90   EXPECT_NE(klass1->NumStaticFields(), 0u);
91   for (ArtField& field : klass2->GetSFields()) {
92     EXPECT_FALSE(
93         klass1->ResolvedFieldAccessTest</*throw_on_failure=*/ false>(
94             klass2.Get(),
95             &field,
96             klass1->GetDexCache(),
97             field.GetDexFieldIndex()));
98   }
99 }
100 
TEST_F(DexCacheMethodHandlesTest,TestResolvedMethodTypes)101 TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) {
102   ScopedObjectAccess soa(Thread::Current());
103   jobject jclass_loader(LoadDex("MethodTypes"));
104   ASSERT_TRUE(jclass_loader != nullptr);
105 
106   StackHandleScope<5> hs(soa.Self());
107   Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
108       soa.Decode<mirror::ClassLoader>(jclass_loader)));
109 
110   Handle<mirror::Class> method_types(
111       hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader)));
112   class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
113 
114   ArtMethod* method1 = method_types->FindClassMethod(
115       "method1",
116       "(Ljava/lang/String;)Ljava/lang/String;",
117       kRuntimePointerSize);
118   ASSERT_TRUE(method1 != nullptr);
119   ASSERT_FALSE(method1->IsDirect());
120   ArtMethod* method2 = method_types->FindClassMethod(
121       "method2",
122       "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
123       kRuntimePointerSize);
124   ASSERT_TRUE(method2 != nullptr);
125   ASSERT_FALSE(method2->IsDirect());
126 
127   const DexFile& dex_file = *(method1->GetDexFile());
128   Handle<mirror::DexCache> dex_cache = hs.NewHandle(
129       class_linker_->FindDexCache(Thread::Current(), dex_file));
130 
131   const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
132   const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
133   Handle<mirror::MethodType> method1_type = hs.NewHandle(
134       class_linker_->ResolveMethodType(soa.Self(),
135                                        method1_id.proto_idx_,
136                                        dex_cache,
137                                        class_loader));
138   Handle<mirror::MethodType> method2_type = hs.NewHandle(
139       class_linker_->ResolveMethodType(soa.Self(),
140                                        method2_id.proto_idx_,
141                                        dex_cache,
142                                        class_loader));
143   EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
144   EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
145 
146   // The MethodTypes dex file contains a single interface with two abstract
147   // methods. It must therefore contain precisely two method IDs.
148   ASSERT_EQ(2u, dex_file.NumProtoIds());
149   ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypesArray());
150   ASSERT_EQ(0u, dex_cache->NumResolvedMethodTypes());
151   auto* method_types_cache = dex_cache->GetResolvedMethodTypesArray();
152 
153   for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
154     auto* method_type = method_types_cache->Get(i);
155     if (dex::ProtoIndex(i) == method1_id.proto_idx_) {
156       ASSERT_EQ(method1_type.Get(), method_type);
157     } else if (dex::ProtoIndex(i) == method2_id.proto_idx_) {
158       ASSERT_EQ(method2_type.Get(), method_type);
159     } else {
160       ASSERT_TRUE(false);
161     }
162   }
163 }
164 
165 }  // namespace mirror
166 }  // namespace art
167