blob: c95d92e34bc68a6bf0f98bbf73b09032e8e40fca [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) {
Vladimir Markof44d36c2017-03-14 14:18:46 +000055 static_assert(ArenaAllocator::kAlignment == 8, "Expecting arena alignment of 8.");
56 DCHECK(layout.Alignment() == 8u || layout.Alignment() == 16u);
Andreas Gampecc1b5352016-12-01 16:58:38 -080057 // Zero-initialized.
Vladimir Markof44d36c2017-03-14 14:18:46 +000058 raw_arrays = (layout.Alignment() == 16u)
59 ? reinterpret_cast<uint8_t*>(linear_alloc->AllocAlign16(self, layout.Size()))
60 : reinterpret_cast<uint8_t*>(linear_alloc->Alloc(self, layout.Size()));
Andreas Gampecc1b5352016-12-01 16:58:38 -080061 }
62
63 mirror::StringDexCacheType* strings = (dex_file->NumStringIds() == 0u) ? nullptr :
64 reinterpret_cast<mirror::StringDexCacheType*>(raw_arrays + layout.StringsOffset());
Vladimir Marko8d6768d2017-03-14 10:13:21 +000065 mirror::TypeDexCacheType* types = (dex_file->NumTypeIds() == 0u) ? nullptr :
66 reinterpret_cast<mirror::TypeDexCacheType*>(raw_arrays + layout.TypesOffset());
Andreas Gampecc1b5352016-12-01 16:58:38 -080067 ArtMethod** methods = (dex_file->NumMethodIds() == 0u) ? nullptr :
68 reinterpret_cast<ArtMethod**>(raw_arrays + layout.MethodsOffset());
Vladimir Markof44d36c2017-03-14 14:18:46 +000069 mirror::FieldDexCacheType* fields = (dex_file->NumFieldIds() == 0u) ? nullptr :
70 reinterpret_cast<mirror::FieldDexCacheType*>(raw_arrays + layout.FieldsOffset());
Andreas Gampecc1b5352016-12-01 16:58:38 -080071
Vladimir Markof44d36c2017-03-14 14:18:46 +000072 size_t num_strings = kDexCacheStringCacheSize;
Andreas Gampecc1b5352016-12-01 16:58:38 -080073 if (dex_file->NumStringIds() < num_strings) {
74 num_strings = dex_file->NumStringIds();
75 }
Vladimir Markof44d36c2017-03-14 14:18:46 +000076 size_t num_types = kDexCacheTypeCacheSize;
Vladimir Marko8d6768d2017-03-14 10:13:21 +000077 if (dex_file->NumTypeIds() < num_types) {
78 num_types = dex_file->NumTypeIds();
79 }
Vladimir Markof44d36c2017-03-14 14:18:46 +000080 size_t num_fields = kDexCacheFieldCacheSize;
81 if (dex_file->NumFieldIds() < num_fields) {
82 num_fields = dex_file->NumFieldIds();
83 }
Andreas Gampecc1b5352016-12-01 16:58:38 -080084
85 // Note that we allocate the method type dex caches regardless of this flag,
86 // and we make sure here that they're not used by the runtime. This is in the
87 // interest of simplicity and to avoid extensive compiler and layout class changes.
88 //
89 // If this needs to be mitigated in a production system running this code,
90 // DexCache::kDexCacheMethodTypeCacheSize can be set to zero.
Vladimir Markof44d36c2017-03-14 14:18:46 +000091 MethodTypeDexCacheType* method_types = nullptr;
Andreas Gampecc1b5352016-12-01 16:58:38 -080092 size_t num_method_types = 0;
93
Vladimir Markof44d36c2017-03-14 14:18:46 +000094 if (dex_file->NumProtoIds() < kDexCacheMethodTypeCacheSize) {
Andreas Gampecc1b5352016-12-01 16:58:38 -080095 num_method_types = dex_file->NumProtoIds();
96 } else {
Vladimir Markof44d36c2017-03-14 14:18:46 +000097 num_method_types = kDexCacheMethodTypeCacheSize;
Andreas Gampecc1b5352016-12-01 16:58:38 -080098 }
99
100 if (num_method_types > 0) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000101 method_types = reinterpret_cast<MethodTypeDexCacheType*>(
Andreas Gampecc1b5352016-12-01 16:58:38 -0800102 raw_arrays + layout.MethodTypesOffset());
103 }
104
Orion Hodsonc069a302017-01-18 09:23:12 +0000105 GcRoot<mirror::CallSite>* call_sites = (dex_file->NumCallSiteIds() == 0)
106 ? nullptr
107 : reinterpret_cast<GcRoot<mirror::CallSite>*>(raw_arrays + layout.CallSitesOffset());
108
Vladimir Markof44d36c2017-03-14 14:18:46 +0000109 DCHECK_ALIGNED(raw_arrays, alignof(StringDexCacheType)) <<
Andreas Gampecc1b5352016-12-01 16:58:38 -0800110 "Expected raw_arrays to align to StringDexCacheType.";
Vladimir Markof44d36c2017-03-14 14:18:46 +0000111 DCHECK_ALIGNED(layout.StringsOffset(), alignof(StringDexCacheType)) <<
Andreas Gampecc1b5352016-12-01 16:58:38 -0800112 "Expected StringsOffset() to align to StringDexCacheType.";
Vladimir Markof44d36c2017-03-14 14:18:46 +0000113 DCHECK_ALIGNED(strings, alignof(StringDexCacheType)) <<
Andreas Gampecc1b5352016-12-01 16:58:38 -0800114 "Expected strings to align to StringDexCacheType.";
Vladimir Markof44d36c2017-03-14 14:18:46 +0000115 static_assert(alignof(StringDexCacheType) == 8u,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800116 "Expected StringDexCacheType to have align of 8.");
117 if (kIsDebugBuild) {
118 // Sanity check to make sure all the dex cache arrays are empty. b/28992179
119 for (size_t i = 0; i < num_strings; ++i) {
120 CHECK_EQ(strings[i].load(std::memory_order_relaxed).index, 0u);
121 CHECK(strings[i].load(std::memory_order_relaxed).object.IsNull());
122 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000123 for (size_t i = 0; i < num_types; ++i) {
124 CHECK_EQ(types[i].load(std::memory_order_relaxed).index, 0u);
125 CHECK(types[i].load(std::memory_order_relaxed).object.IsNull());
Andreas Gampecc1b5352016-12-01 16:58:38 -0800126 }
127 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000128 CHECK(GetElementPtrSize(methods, i, image_pointer_size) == nullptr);
Andreas Gampecc1b5352016-12-01 16:58:38 -0800129 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000130 for (size_t i = 0; i < num_fields; ++i) {
131 CHECK_EQ(GetNativePairPtrSize(fields, i, image_pointer_size).index, 0u);
132 CHECK(GetNativePairPtrSize(fields, i, image_pointer_size).object == nullptr);
Andreas Gampecc1b5352016-12-01 16:58:38 -0800133 }
134 for (size_t i = 0; i < num_method_types; ++i) {
135 CHECK_EQ(method_types[i].load(std::memory_order_relaxed).index, 0u);
136 CHECK(method_types[i].load(std::memory_order_relaxed).object.IsNull());
137 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000138 for (size_t i = 0; i < dex_file->NumCallSiteIds(); ++i) {
139 CHECK(call_sites[i].IsNull());
140 }
Andreas Gampecc1b5352016-12-01 16:58:38 -0800141 }
142 if (strings != nullptr) {
143 mirror::StringDexCachePair::Initialize(strings);
144 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000145 if (types != nullptr) {
146 mirror::TypeDexCachePair::Initialize(types);
147 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000148 if (fields != nullptr) {
149 mirror::FieldDexCachePair::Initialize(fields, image_pointer_size);
150 }
Andreas Gampecc1b5352016-12-01 16:58:38 -0800151 if (method_types != nullptr) {
152 mirror::MethodTypeDexCachePair::Initialize(method_types);
153 }
154 dex_cache->Init(dex_file,
155 location,
156 strings,
157 num_strings,
158 types,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000159 num_types,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800160 methods,
161 dex_file->NumMethodIds(),
162 fields,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000163 num_fields,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800164 method_types,
165 num_method_types,
Orion Hodsonc069a302017-01-18 09:23:12 +0000166 call_sites,
167 dex_file->NumCallSiteIds(),
Andreas Gampecc1b5352016-12-01 16:58:38 -0800168 image_pointer_size);
169}
170
Vladimir Marko05792b92015-08-03 11:56:49 +0100171void DexCache::Init(const DexFile* dex_file,
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700172 ObjPtr<String> location,
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700173 StringDexCacheType* strings,
Vladimir Marko05792b92015-08-03 11:56:49 +0100174 uint32_t num_strings,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000175 TypeDexCacheType* resolved_types,
Vladimir Marko05792b92015-08-03 11:56:49 +0100176 uint32_t num_resolved_types,
177 ArtMethod** resolved_methods,
178 uint32_t num_resolved_methods,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000179 FieldDexCacheType* resolved_fields,
Vladimir Marko05792b92015-08-03 11:56:49 +0100180 uint32_t num_resolved_fields,
Narayan Kamath25352fc2016-08-03 12:46:58 +0100181 MethodTypeDexCacheType* resolved_method_types,
182 uint32_t num_resolved_method_types,
Orion Hodsonc069a302017-01-18 09:23:12 +0000183 GcRoot<CallSite>* resolved_call_sites,
184 uint32_t num_resolved_call_sites,
Andreas Gampe542451c2016-07-26 09:02:02 -0700185 PointerSize pointer_size) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800186 CHECK(dex_file != nullptr);
187 CHECK(location != nullptr);
Vladimir Marko05792b92015-08-03 11:56:49 +0100188 CHECK_EQ(num_strings != 0u, strings != nullptr);
189 CHECK_EQ(num_resolved_types != 0u, resolved_types != nullptr);
190 CHECK_EQ(num_resolved_methods != 0u, resolved_methods != nullptr);
191 CHECK_EQ(num_resolved_fields != 0u, resolved_fields != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100192 CHECK_EQ(num_resolved_method_types != 0u, resolved_method_types != nullptr);
Orion Hodsonc069a302017-01-18 09:23:12 +0000193 CHECK_EQ(num_resolved_call_sites != 0u, resolved_call_sites != nullptr);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700194
Mathieu Chartierc7853442015-03-27 14:35:38 -0700195 SetDexFile(dex_file);
Mathieu Chartier76172162016-01-26 14:54:06 -0800196 SetLocation(location);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800197 SetStrings(strings);
198 SetResolvedTypes(resolved_types);
199 SetResolvedMethods(resolved_methods);
200 SetResolvedFields(resolved_fields);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100201 SetResolvedMethodTypes(resolved_method_types);
Orion Hodsonc069a302017-01-18 09:23:12 +0000202 SetResolvedCallSites(resolved_call_sites);
Vladimir Marko05792b92015-08-03 11:56:49 +0100203 SetField32<false>(NumStringsOffset(), num_strings);
204 SetField32<false>(NumResolvedTypesOffset(), num_resolved_types);
205 SetField32<false>(NumResolvedMethodsOffset(), num_resolved_methods);
206 SetField32<false>(NumResolvedFieldsOffset(), num_resolved_fields);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100207 SetField32<false>(NumResolvedMethodTypesOffset(), num_resolved_method_types);
Orion Hodsonc069a302017-01-18 09:23:12 +0000208 SetField32<false>(NumResolvedCallSitesOffset(), num_resolved_call_sites);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700209
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 Runtime* const runtime = Runtime::Current();
Ian Rogers19846512012-02-24 11:42:47 -0800211 if (runtime->HasResolutionMethod()) {
212 // Initialize the resolve methods array to contain trampolines for resolution.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 Fixup(runtime->GetResolutionMethod(), pointer_size);
Ian Rogers19846512012-02-24 11:42:47 -0800214 }
215}
216
Andreas Gampe542451c2016-07-26 09:02:02 -0700217void DexCache::Fixup(ArtMethod* trampoline, PointerSize pointer_size) {
Ian Rogers19846512012-02-24 11:42:47 -0800218 // Fixup the resolve methods array to contain trampoline for resolution.
Ian Rogers5ddb4102014-01-07 08:58:46 -0800219 CHECK(trampoline != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220 CHECK(trampoline->IsRuntimeMethod());
221 auto* resolved_methods = GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +0100222 for (size_t i = 0, length = NumResolvedMethods(); i < length; i++) {
223 if (GetElementPtrSize<ArtMethod*>(resolved_methods, i, pointer_size) == nullptr) {
224 SetElementPtrSize(resolved_methods, i, trampoline, pointer_size);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700225 }
226 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700227}
228
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700229void DexCache::SetLocation(ObjPtr<mirror::String> location) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800230 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_), location);
231}
232
Vladimir Markof44d36c2017-03-14 14:18:46 +0000233#if !defined(__aarch64__) && !defined(__x86_64__)
234static pthread_mutex_t dex_cache_slow_atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
235
236DexCache::ConversionPair64 DexCache::AtomicLoadRelaxed16B(std::atomic<ConversionPair64>* target) {
237 pthread_mutex_lock(&dex_cache_slow_atomic_mutex);
238 DexCache::ConversionPair64 value = *reinterpret_cast<ConversionPair64*>(target);
239 pthread_mutex_unlock(&dex_cache_slow_atomic_mutex);
240 return value;
241}
242
243void DexCache::AtomicStoreRelease16B(std::atomic<ConversionPair64>* target,
244 ConversionPair64 value) {
245 pthread_mutex_lock(&dex_cache_slow_atomic_mutex);
246 *reinterpret_cast<ConversionPair64*>(target) = value;
247 pthread_mutex_unlock(&dex_cache_slow_atomic_mutex);
248}
249#endif
250
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700252} // namespace art