Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 17 | #include "dex_cache.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include "class_linker.h" |
| 23 | #include "common_runtime_test.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include "handle_scope-inl.h" |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 25 | #include "linear_alloc.h" |
| 26 | #include "mirror/class_loader-inl.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 27 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 30 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | namespace mirror { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 33 | class DexCacheTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 34 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 35 | class DexCacheMethodHandlesTest : public DexCacheTest { |
| 36 | protected: |
| 37 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE { |
| 38 | CommonRuntimeTest::SetUpRuntimeOptions(options); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 39 | } |
| 40 | }; |
| 41 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 42 | TEST_F(DexCacheTest, Open) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 44 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 45 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | Handle<DexCache> dex_cache( |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 47 | hs.NewHandle(class_linker_->AllocAndInitializeDexCache( |
| 48 | soa.Self(), |
| 49 | *java_lang_dex_file_, |
| 50 | Runtime::Current()->GetLinearAlloc()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 51 | ASSERT_TRUE(dex_cache != nullptr); |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 52 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 53 | EXPECT_TRUE(dex_cache->StaticStringSize() == dex_cache->NumStrings() |
| 54 | || java_lang_dex_file_->NumStringIds() == dex_cache->NumStrings()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 55 | EXPECT_TRUE(dex_cache->StaticTypeSize() == dex_cache->NumResolvedTypes() |
| 56 | || java_lang_dex_file_->NumTypeIds() == dex_cache->NumResolvedTypes()); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 57 | EXPECT_TRUE(dex_cache->StaticMethodSize() == dex_cache->NumResolvedMethods() |
| 58 | || java_lang_dex_file_->NumMethodIds() == dex_cache->NumResolvedMethods()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 59 | EXPECT_TRUE(dex_cache->StaticArtFieldSize() == dex_cache->NumResolvedFields() |
| 60 | || java_lang_dex_file_->NumFieldIds() == dex_cache->NumResolvedFields()); |
Narayan Kamath | 269cb43 | 2016-10-28 10:19:54 +0100 | [diff] [blame] | 61 | EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes() |
| 62 | || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_F(DexCacheMethodHandlesTest, Open) { |
| 66 | ScopedObjectAccess soa(Thread::Current()); |
| 67 | StackHandleScope<1> hs(soa.Self()); |
| 68 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 69 | Handle<DexCache> dex_cache( |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 70 | hs.NewHandle(class_linker_->AllocAndInitializeDexCache( |
| 71 | soa.Self(), |
| 72 | *java_lang_dex_file_, |
| 73 | Runtime::Current()->GetLinearAlloc()))); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 74 | |
| 75 | EXPECT_TRUE(dex_cache->StaticMethodTypeSize() == dex_cache->NumResolvedMethodTypes() |
| 76 | || java_lang_dex_file_->NumProtoIds() == dex_cache->NumResolvedMethodTypes()); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 79 | TEST_F(DexCacheTest, LinearAlloc) { |
| 80 | ScopedObjectAccess soa(Thread::Current()); |
| 81 | jobject jclass_loader(LoadDex("Main")); |
| 82 | ASSERT_TRUE(jclass_loader != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 83 | StackHandleScope<1> hs(soa.Self()); |
| 84 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 85 | soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 86 | mirror::Class* klass = class_linker_->FindClass(soa.Self(), "LMain;", class_loader); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 87 | ASSERT_TRUE(klass != nullptr); |
| 88 | LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 89 | EXPECT_NE(linear_alloc, runtime_->GetLinearAlloc()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 90 | EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods())); |
| 91 | } |
| 92 | |
Mathieu Chartier | 279ac5c | 2016-09-08 17:34:25 -0700 | [diff] [blame] | 93 | TEST_F(DexCacheTest, TestResolvedFieldAccess) { |
| 94 | ScopedObjectAccess soa(Thread::Current()); |
| 95 | jobject jclass_loader(LoadDex("Packages")); |
| 96 | ASSERT_TRUE(jclass_loader != nullptr); |
Mathieu Chartier | 279ac5c | 2016-09-08 17:34:25 -0700 | [diff] [blame] | 97 | StackHandleScope<3> hs(soa.Self()); |
| 98 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 99 | soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Mathieu Chartier | 279ac5c | 2016-09-08 17:34:25 -0700 | [diff] [blame] | 100 | Handle<mirror::Class> klass1 = |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 101 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 102 | ASSERT_TRUE(klass1 != nullptr); |
Mathieu Chartier | 279ac5c | 2016-09-08 17:34:25 -0700 | [diff] [blame] | 103 | Handle<mirror::Class> klass2 = |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 104 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 105 | ASSERT_TRUE(klass2 != nullptr); |
Mathieu Chartier | 279ac5c | 2016-09-08 17:34:25 -0700 | [diff] [blame] | 106 | EXPECT_EQ(klass1->GetDexCache(), klass2->GetDexCache()); |
| 107 | |
| 108 | EXPECT_NE(klass1->NumStaticFields(), 0u); |
| 109 | for (ArtField& field : klass2->GetSFields()) { |
Vladimir Marko | f79aa7f | 2017-07-04 16:58:55 +0100 | [diff] [blame] | 110 | EXPECT_FALSE( |
| 111 | klass1->ResolvedFieldAccessTest</*throw_on_failure*/ false>( |
| 112 | klass2.Get(), |
| 113 | &field, |
| 114 | klass1->GetDexCache(), |
| 115 | field.GetDexFieldIndex())); |
Mathieu Chartier | 279ac5c | 2016-09-08 17:34:25 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 119 | TEST_F(DexCacheMethodHandlesTest, TestResolvedMethodTypes) { |
| 120 | ScopedObjectAccess soa(Thread::Current()); |
| 121 | jobject jclass_loader(LoadDex("MethodTypes")); |
| 122 | ASSERT_TRUE(jclass_loader != nullptr); |
| 123 | |
| 124 | StackHandleScope<5> hs(soa.Self()); |
| 125 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 126 | soa.Decode<mirror::ClassLoader>(jclass_loader))); |
| 127 | |
| 128 | Handle<mirror::Class> method_types( |
| 129 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LMethodTypes;", class_loader))); |
| 130 | class_linker_->EnsureInitialized(soa.Self(), method_types, true, true); |
| 131 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 132 | ArtMethod* method1 = method_types->FindClassMethod( |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 133 | "method1", |
| 134 | "(Ljava/lang/String;)Ljava/lang/String;", |
| 135 | kRuntimePointerSize); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 136 | ASSERT_TRUE(method1 != nullptr); |
| 137 | ASSERT_FALSE(method1->IsDirect()); |
| 138 | ArtMethod* method2 = method_types->FindClassMethod( |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 139 | "method2", |
| 140 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 141 | kRuntimePointerSize); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 142 | ASSERT_TRUE(method2 != nullptr); |
| 143 | ASSERT_FALSE(method2->IsDirect()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 144 | |
| 145 | const DexFile& dex_file = *(method1->GetDexFile()); |
| 146 | Handle<mirror::DexCache> dex_cache = hs.NewHandle( |
| 147 | class_linker_->FindDexCache(Thread::Current(), dex_file)); |
| 148 | |
| 149 | const DexFile::MethodId& method1_id = dex_file.GetMethodId(method1->GetDexMethodIndex()); |
| 150 | const DexFile::MethodId& method2_id = dex_file.GetMethodId(method2->GetDexMethodIndex()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 151 | Handle<mirror::MethodType> method1_type = hs.NewHandle( |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 152 | class_linker_->ResolveMethodType(soa.Self(), |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 153 | method1_id.proto_idx_, |
| 154 | dex_cache, |
| 155 | class_loader)); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 156 | Handle<mirror::MethodType> method2_type = hs.NewHandle( |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 157 | class_linker_->ResolveMethodType(soa.Self(), |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 158 | method2_id.proto_idx_, |
| 159 | dex_cache, |
| 160 | class_loader)); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 161 | EXPECT_EQ(method1_type.Get(), dex_cache->GetResolvedMethodType(method1_id.proto_idx_)); |
| 162 | EXPECT_EQ(method2_type.Get(), dex_cache->GetResolvedMethodType(method2_id.proto_idx_)); |
| 163 | |
| 164 | // The MethodTypes dex file contains a single interface with two abstract |
| 165 | // methods. It must therefore contain precisely two method IDs. |
| 166 | ASSERT_EQ(2u, dex_file.NumProtoIds()); |
| 167 | ASSERT_EQ(dex_file.NumProtoIds(), dex_cache->NumResolvedMethodTypes()); |
| 168 | MethodTypeDexCacheType* method_types_cache = dex_cache->GetResolvedMethodTypes(); |
| 169 | |
| 170 | for (size_t i = 0; i < dex_file.NumProtoIds(); ++i) { |
| 171 | const MethodTypeDexCachePair pair = method_types_cache[i].load(std::memory_order_relaxed); |
| 172 | if (pair.index == method1_id.proto_idx_) { |
| 173 | ASSERT_EQ(method1_type.Get(), pair.object.Read()); |
| 174 | } else if (pair.index == method2_id.proto_idx_) { |
| 175 | ASSERT_EQ(method2_type.Get(), pair.object.Read()); |
| 176 | } else { |
| 177 | ASSERT_TRUE(false); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 182 | } // namespace mirror |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 183 | } // namespace art |