blob: 43ba362acaa63d182e99d30b94d53fcc5d334d05 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom7e49dca2011-07-22 18:07:34 -070016
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070017#include "dex_cache.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080018
19#include <stdio.h>
20
21#include "class_linker.h"
22#include "common_runtime_test.h"
Mathieu Chartierd57d4542015-10-14 10:55:30 -070023#include "linear_alloc.h"
24#include "mirror/class_loader-inl.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070025#include "mirror/dex_cache-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "scoped_thread_state_change.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070028
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070031
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032class DexCacheTest : public CommonRuntimeTest {};
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070033
34TEST_F(DexCacheTest, Open) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036 StackHandleScope<1> hs(soa.Self());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070037 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 Handle<DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -070039 hs.NewHandle(class_linker_->AllocDexCache(soa.Self(),
40 *java_lang_dex_file_,
41 Runtime::Current()->GetLinearAlloc())));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070042 ASSERT_TRUE(dex_cache.Get() != nullptr);
Brian Carlstromc4fa2c02011-08-21 03:00:12 -070043
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070044 EXPECT_TRUE(dex_cache->StaticStringSize() == dex_cache->NumStrings()
45 || java_lang_dex_file_->NumStringIds() == dex_cache->NumStrings());
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070046 EXPECT_EQ(java_lang_dex_file_->NumTypeIds(), dex_cache->NumResolvedTypes());
47 EXPECT_EQ(java_lang_dex_file_->NumMethodIds(), dex_cache->NumResolvedMethods());
48 EXPECT_EQ(java_lang_dex_file_->NumFieldIds(), dex_cache->NumResolvedFields());
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070049}
50
Mathieu Chartierd57d4542015-10-14 10:55:30 -070051TEST_F(DexCacheTest, LinearAlloc) {
52 ScopedObjectAccess soa(Thread::Current());
53 jobject jclass_loader(LoadDex("Main"));
54 ASSERT_TRUE(jclass_loader != nullptr);
55 Runtime* const runtime = Runtime::Current();
56 ClassLinker* const class_linker = runtime->GetClassLinker();
57 StackHandleScope<1> hs(soa.Self());
58 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
59 soa.Decode<mirror::ClassLoader*>(jclass_loader)));
60 mirror::Class* klass = class_linker->FindClass(soa.Self(), "LMain;", class_loader);
61 ASSERT_TRUE(klass != nullptr);
62 LinearAlloc* const linear_alloc = klass->GetClassLoader()->GetAllocator();
63 EXPECT_NE(linear_alloc, runtime->GetLinearAlloc());
64 EXPECT_TRUE(linear_alloc->Contains(klass->GetDexCache()->GetResolvedMethods()));
65}
66
Mathieu Chartier279ac5c2016-09-08 17:34:25 -070067TEST_F(DexCacheTest, TestResolvedFieldAccess) {
68 ScopedObjectAccess soa(Thread::Current());
69 jobject jclass_loader(LoadDex("Packages"));
70 ASSERT_TRUE(jclass_loader != nullptr);
71 Runtime* const runtime = Runtime::Current();
72 ClassLinker* const class_linker = runtime->GetClassLinker();
73 StackHandleScope<3> hs(soa.Self());
74 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
75 soa.Decode<mirror::ClassLoader*>(jclass_loader)));
76 Handle<mirror::Class> klass1 =
77 hs.NewHandle(class_linker->FindClass(soa.Self(), "Lpackage1/Package1;", class_loader));
78 ASSERT_TRUE(klass1.Get() != nullptr);
79 Handle<mirror::Class> klass2 =
80 hs.NewHandle(class_linker->FindClass(soa.Self(), "Lpackage2/Package2;", class_loader));
81 ASSERT_TRUE(klass2.Get() != nullptr);
82 EXPECT_EQ(klass1->GetDexCache(), klass2->GetDexCache());
83
84 EXPECT_NE(klass1->NumStaticFields(), 0u);
85 for (ArtField& field : klass2->GetSFields()) {
86 EXPECT_FALSE((
87 klass1->ResolvedFieldAccessTest</*throw_on_failure*/ false,
88 /*use_referrers_cache*/ false>(klass2.Get(),
89 &field,
90 field.GetDexFieldIndex(),
91 klass1->GetDexCache())));
92 }
93}
94
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070096} // namespace art