blob: 3103a92c83335cbd12a9535d99af17dfcfa48783 [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
Vladimir Marko05792b92015-08-03 11:56:49 +010017#include "dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070021#include "class_linker.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/accounting/card_table-inl.h"
23#include "gc/heap.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070024#include "globals.h"
Andreas Gampecc1b5352016-12-01 16:58:38 -080025#include "linear_alloc.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070026#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "object-inl.h"
28#include "object_array-inl.h"
29#include "runtime.h"
30#include "string.h"
Andreas Gampecc1b5352016-12-01 16:58:38 -080031#include "thread.h"
32#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070033
34namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035namespace mirror {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070036
Andreas Gampecc1b5352016-12-01 16:58:38 -080037void DexCache::InitializeDexCache(Thread* self,
38 ObjPtr<mirror::DexCache> dex_cache,
39 ObjPtr<mirror::String> location,
40 const DexFile* dex_file,
41 LinearAlloc* linear_alloc,
42 PointerSize image_pointer_size) {
43 DCHECK(dex_file != nullptr);
44 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
45 DexCacheArraysLayout layout(image_pointer_size, dex_file);
46 uint8_t* raw_arrays = nullptr;
47
48 const OatDexFile* const oat_dex = dex_file->GetOatDexFile();
49 if (oat_dex != nullptr && oat_dex->GetDexCacheArrays() != nullptr) {
50 raw_arrays = oat_dex->GetDexCacheArrays();
51 } else if (dex_file->NumStringIds() != 0u ||
52 dex_file->NumTypeIds() != 0u ||
53 dex_file->NumMethodIds() != 0u ||
54 dex_file->NumFieldIds() != 0u) {
55 // Zero-initialized.
56 raw_arrays = reinterpret_cast<uint8_t*>(linear_alloc->Alloc(self, layout.Size()));
57 }
58
59 mirror::StringDexCacheType* strings = (dex_file->NumStringIds() == 0u) ? nullptr :
60 reinterpret_cast<mirror::StringDexCacheType*>(raw_arrays + layout.StringsOffset());
Vladimir Markoec786222016-12-20 16:24:13 +000061 mirror::TypeDexCacheType* types = (dex_file->NumTypeIds() == 0u) ? nullptr :
62 reinterpret_cast<mirror::TypeDexCacheType*>(raw_arrays + layout.TypesOffset());
Andreas Gampecc1b5352016-12-01 16:58:38 -080063 ArtMethod** methods = (dex_file->NumMethodIds() == 0u) ? nullptr :
64 reinterpret_cast<ArtMethod**>(raw_arrays + layout.MethodsOffset());
65 ArtField** fields = (dex_file->NumFieldIds() == 0u) ? nullptr :
66 reinterpret_cast<ArtField**>(raw_arrays + layout.FieldsOffset());
67
68 size_t num_strings = mirror::DexCache::kDexCacheStringCacheSize;
69 if (dex_file->NumStringIds() < num_strings) {
70 num_strings = dex_file->NumStringIds();
71 }
Vladimir Markoec786222016-12-20 16:24:13 +000072 size_t num_types = mirror::DexCache::kDexCacheTypeCacheSize;
73 if (dex_file->NumTypeIds() < num_types) {
74 num_types = dex_file->NumTypeIds();
75 }
Andreas Gampecc1b5352016-12-01 16:58:38 -080076
77 // Note that we allocate the method type dex caches regardless of this flag,
78 // and we make sure here that they're not used by the runtime. This is in the
79 // interest of simplicity and to avoid extensive compiler and layout class changes.
80 //
81 // If this needs to be mitigated in a production system running this code,
82 // DexCache::kDexCacheMethodTypeCacheSize can be set to zero.
83 mirror::MethodTypeDexCacheType* method_types = nullptr;
84 size_t num_method_types = 0;
85
86 if (dex_file->NumProtoIds() < mirror::DexCache::kDexCacheMethodTypeCacheSize) {
87 num_method_types = dex_file->NumProtoIds();
88 } else {
89 num_method_types = mirror::DexCache::kDexCacheMethodTypeCacheSize;
90 }
91
92 if (num_method_types > 0) {
93 method_types = reinterpret_cast<mirror::MethodTypeDexCacheType*>(
94 raw_arrays + layout.MethodTypesOffset());
95 }
96
97 DCHECK_ALIGNED(raw_arrays, alignof(mirror::StringDexCacheType)) <<
98 "Expected raw_arrays to align to StringDexCacheType.";
99 DCHECK_ALIGNED(layout.StringsOffset(), alignof(mirror::StringDexCacheType)) <<
100 "Expected StringsOffset() to align to StringDexCacheType.";
101 DCHECK_ALIGNED(strings, alignof(mirror::StringDexCacheType)) <<
102 "Expected strings to align to StringDexCacheType.";
103 static_assert(alignof(mirror::StringDexCacheType) == 8u,
104 "Expected StringDexCacheType to have align of 8.");
105 if (kIsDebugBuild) {
106 // Sanity check to make sure all the dex cache arrays are empty. b/28992179
107 for (size_t i = 0; i < num_strings; ++i) {
108 CHECK_EQ(strings[i].load(std::memory_order_relaxed).index, 0u);
109 CHECK(strings[i].load(std::memory_order_relaxed).object.IsNull());
110 }
Vladimir Markoec786222016-12-20 16:24:13 +0000111 for (size_t i = 0; i < num_types; ++i) {
112 CHECK_EQ(types[i].load(std::memory_order_relaxed).index, 0u);
113 CHECK(types[i].load(std::memory_order_relaxed).object.IsNull());
Andreas Gampecc1b5352016-12-01 16:58:38 -0800114 }
115 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
116 CHECK(mirror::DexCache::GetElementPtrSize(methods, i, image_pointer_size) == nullptr);
117 }
118 for (size_t i = 0; i < dex_file->NumFieldIds(); ++i) {
119 CHECK(mirror::DexCache::GetElementPtrSize(fields, i, image_pointer_size) == nullptr);
120 }
121 for (size_t i = 0; i < num_method_types; ++i) {
122 CHECK_EQ(method_types[i].load(std::memory_order_relaxed).index, 0u);
123 CHECK(method_types[i].load(std::memory_order_relaxed).object.IsNull());
124 }
125 }
126 if (strings != nullptr) {
127 mirror::StringDexCachePair::Initialize(strings);
128 }
Vladimir Markoec786222016-12-20 16:24:13 +0000129 if (types != nullptr) {
130 mirror::TypeDexCachePair::Initialize(types);
131 }
Andreas Gampecc1b5352016-12-01 16:58:38 -0800132 if (method_types != nullptr) {
133 mirror::MethodTypeDexCachePair::Initialize(method_types);
134 }
135 dex_cache->Init(dex_file,
136 location,
137 strings,
138 num_strings,
139 types,
Vladimir Markoec786222016-12-20 16:24:13 +0000140 num_types,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800141 methods,
142 dex_file->NumMethodIds(),
143 fields,
144 dex_file->NumFieldIds(),
145 method_types,
146 num_method_types,
147 image_pointer_size);
148}
149
Vladimir Marko05792b92015-08-03 11:56:49 +0100150void DexCache::Init(const DexFile* dex_file,
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700151 ObjPtr<String> location,
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700152 StringDexCacheType* strings,
Vladimir Marko05792b92015-08-03 11:56:49 +0100153 uint32_t num_strings,
Vladimir Markoec786222016-12-20 16:24:13 +0000154 TypeDexCacheType* resolved_types,
Vladimir Marko05792b92015-08-03 11:56:49 +0100155 uint32_t num_resolved_types,
156 ArtMethod** resolved_methods,
157 uint32_t num_resolved_methods,
158 ArtField** resolved_fields,
159 uint32_t num_resolved_fields,
Narayan Kamath25352fc2016-08-03 12:46:58 +0100160 MethodTypeDexCacheType* resolved_method_types,
161 uint32_t num_resolved_method_types,
Andreas Gampe542451c2016-07-26 09:02:02 -0700162 PointerSize pointer_size) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800163 CHECK(dex_file != nullptr);
164 CHECK(location != nullptr);
Vladimir Marko05792b92015-08-03 11:56:49 +0100165 CHECK_EQ(num_strings != 0u, strings != nullptr);
166 CHECK_EQ(num_resolved_types != 0u, resolved_types != nullptr);
167 CHECK_EQ(num_resolved_methods != 0u, resolved_methods != nullptr);
168 CHECK_EQ(num_resolved_fields != 0u, resolved_fields != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100169 CHECK_EQ(num_resolved_method_types != 0u, resolved_method_types != nullptr);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700170
Mathieu Chartierc7853442015-03-27 14:35:38 -0700171 SetDexFile(dex_file);
Mathieu Chartier76172162016-01-26 14:54:06 -0800172 SetLocation(location);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800173 SetStrings(strings);
174 SetResolvedTypes(resolved_types);
175 SetResolvedMethods(resolved_methods);
176 SetResolvedFields(resolved_fields);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100177 SetResolvedMethodTypes(resolved_method_types);
Vladimir Marko05792b92015-08-03 11:56:49 +0100178 SetField32<false>(NumStringsOffset(), num_strings);
179 SetField32<false>(NumResolvedTypesOffset(), num_resolved_types);
180 SetField32<false>(NumResolvedMethodsOffset(), num_resolved_methods);
181 SetField32<false>(NumResolvedFieldsOffset(), num_resolved_fields);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100182 SetField32<false>(NumResolvedMethodTypesOffset(), num_resolved_method_types);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 Runtime* const runtime = Runtime::Current();
Ian Rogers19846512012-02-24 11:42:47 -0800185 if (runtime->HasResolutionMethod()) {
186 // Initialize the resolve methods array to contain trampolines for resolution.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 Fixup(runtime->GetResolutionMethod(), pointer_size);
Ian Rogers19846512012-02-24 11:42:47 -0800188 }
189}
190
Andreas Gampe542451c2016-07-26 09:02:02 -0700191void DexCache::Fixup(ArtMethod* trampoline, PointerSize pointer_size) {
Ian Rogers19846512012-02-24 11:42:47 -0800192 // Fixup the resolve methods array to contain trampoline for resolution.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800193 CHECK(trampoline != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 CHECK(trampoline->IsRuntimeMethod());
195 auto* resolved_methods = GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100196 for (size_t i = 0, length = NumResolvedMethods(); i < length; i++) {
197 if (GetElementPtrSize<ArtMethod*>(resolved_methods, i, pointer_size) == nullptr) {
198 SetElementPtrSize(resolved_methods, i, trampoline, pointer_size);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700199 }
200 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700201}
202
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700203void DexCache::SetLocation(ObjPtr<mirror::String> location) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800204 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_), location);
205}
206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700208} // namespace art