• 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 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->ComputeNumStaticFields(), 0u);
89   for (ArtField& field : klass2->GetFields()) {
90     if (field.IsStatic()) {
91       EXPECT_FALSE(
92           klass1->ResolvedFieldAccessTest</*throw_on_failure=*/ false>(
93               klass2.Get(),
94               &field,
95               klass1->GetDexCache(),
96               field.GetDexFieldIndex()));
97     }
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 = hs.NewHandle(FindClass("LMethodTypes;", class_loader));
111   class_linker_->EnsureInitialized(soa.Self(), method_types, true, true);
112 
113   ArtMethod* method1 = method_types->FindClassMethod(
114       "method1",
115       "(Ljava/lang/String;)Ljava/lang/String;",
116       kRuntimePointerSize);
117   ASSERT_TRUE(method1 != nullptr);
118   ASSERT_FALSE(method1->IsDirect());
119   ArtMethod* method2 = method_types->FindClassMethod(
120       "method2",
121       "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
122       kRuntimePointerSize);
123   ASSERT_TRUE(method2 != nullptr);
124   ASSERT_FALSE(method2->IsDirect());
125 
126   const DexFile& dex_file = *(method1->GetDexFile());
127   Handle<mirror::DexCache> dex_cache = hs.NewHandle(
128       class_linker_->FindDexCache(Thread::Current(), dex_file));
129 
130   const dex::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex());
131   const dex::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex());
132   Handle<mirror::MethodType> method1_type = hs.NewHandle(
133       class_linker_->ResolveMethodType(soa.Self(),
134                                        method1_id.proto_idx_,
135                                        dex_cache,
136                                        class_loader));
137   Handle<mirror::MethodType> method2_type = hs.NewHandle(
138       class_linker_->ResolveMethodType(soa.Self(),
139                                        method2_id.proto_idx_,
140                                        dex_cache,
141                                        class_loader));
142   EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_));
143   EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_));
144 
145   // The MethodTypes dex file contains a single interface with two abstract
146   // methods. It must therefore contain precisely two method IDs.
147   ASSERT_EQ(2u, dex_file.NumProtoIds());
148   ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypesArray());
149   ASSERT_EQ(0u, dex_cache->NumResolvedMethodTypes());
150   auto* method_types_cache = dex_cache->GetResolvedMethodTypesArray();
151 
152   for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) {
153     auto* method_type = method_types_cache->Get(i);
154     if (dex::ProtoIndex(i) == method1_id.proto_idx_) {
155       ASSERT_EQ(method1_type.Get(), method_type);
156     } else if (dex::ProtoIndex(i) == method2_id.proto_idx_) {
157       ASSERT_EQ(method2_type.Get(), method_type);
158     } else {
159       ASSERT_TRUE(false);
160     }
161   }
162 }
163 
164 }  // namespace mirror
165 }  // namespace art
166