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