blob: 661f954ef551bbe109c29a9a12334d47fbd2bde8 [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"
David Sehr1979c642018-04-26 14:41:18 -070020#include "base/globals.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"
Andreas Gampecc1b5352016-12-01 16:58:38 -080024#include "linear_alloc.h"
Andreas Gampe2ff3b972017-06-05 18:14:53 -070025#include "oat_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "object-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "object.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#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
Vladimir Marko0f3c7002017-09-07 14:15:56 +010048 if (dex_file->NumStringIds() != 0u ||
49 dex_file->NumTypeIds() != 0u ||
50 dex_file->NumMethodIds() != 0u ||
51 dex_file->NumFieldIds() != 0u) {
Vladimir Markof44d36c2017-03-14 14:18:46 +000052 static_assert(ArenaAllocator::kAlignment == 8, "Expecting arena alignment of 8.");
53 DCHECK(layout.Alignment() == 8u || layout.Alignment() == 16u);
Andreas Gampecc1b5352016-12-01 16:58:38 -080054 // Zero-initialized.
Vladimir Markof44d36c2017-03-14 14:18:46 +000055 raw_arrays = (layout.Alignment() == 16u)
56 ? reinterpret_cast<uint8_t*>(linear_alloc->AllocAlign16(self, layout.Size()))
57 : reinterpret_cast<uint8_t*>(linear_alloc->Alloc(self, layout.Size()));
Andreas Gampecc1b5352016-12-01 16:58:38 -080058 }
59
Vladimir Marko07bfbac2017-07-06 14:55:02 +010060 StringDexCacheType* strings = (dex_file->NumStringIds() == 0u) ? nullptr :
61 reinterpret_cast<StringDexCacheType*>(raw_arrays + layout.StringsOffset());
62 TypeDexCacheType* types = (dex_file->NumTypeIds() == 0u) ? nullptr :
63 reinterpret_cast<TypeDexCacheType*>(raw_arrays + layout.TypesOffset());
64 MethodDexCacheType* methods = (dex_file->NumMethodIds() == 0u) ? nullptr :
65 reinterpret_cast<MethodDexCacheType*>(raw_arrays + layout.MethodsOffset());
66 FieldDexCacheType* fields = (dex_file->NumFieldIds() == 0u) ? nullptr :
67 reinterpret_cast<FieldDexCacheType*>(raw_arrays + layout.FieldsOffset());
Andreas Gampecc1b5352016-12-01 16:58:38 -080068
Vladimir Markof44d36c2017-03-14 14:18:46 +000069 size_t num_strings = kDexCacheStringCacheSize;
Andreas Gampecc1b5352016-12-01 16:58:38 -080070 if (dex_file->NumStringIds() < num_strings) {
71 num_strings = dex_file->NumStringIds();
72 }
Vladimir Markof44d36c2017-03-14 14:18:46 +000073 size_t num_types = kDexCacheTypeCacheSize;
Vladimir Marko8d6768d2017-03-14 10:13:21 +000074 if (dex_file->NumTypeIds() < num_types) {
75 num_types = dex_file->NumTypeIds();
76 }
Vladimir Markof44d36c2017-03-14 14:18:46 +000077 size_t num_fields = kDexCacheFieldCacheSize;
78 if (dex_file->NumFieldIds() < num_fields) {
79 num_fields = dex_file->NumFieldIds();
80 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +010081 size_t num_methods = kDexCacheMethodCacheSize;
82 if (dex_file->NumMethodIds() < num_methods) {
83 num_methods = dex_file->NumMethodIds();
84 }
Andreas Gampecc1b5352016-12-01 16:58:38 -080085
86 // Note that we allocate the method type dex caches regardless of this flag,
87 // and we make sure here that they're not used by the runtime. This is in the
88 // interest of simplicity and to avoid extensive compiler and layout class changes.
89 //
90 // If this needs to be mitigated in a production system running this code,
91 // DexCache::kDexCacheMethodTypeCacheSize can be set to zero.
Vladimir Markof44d36c2017-03-14 14:18:46 +000092 MethodTypeDexCacheType* method_types = nullptr;
Andreas Gampecc1b5352016-12-01 16:58:38 -080093 size_t num_method_types = 0;
94
Vladimir Markof44d36c2017-03-14 14:18:46 +000095 if (dex_file->NumProtoIds() < kDexCacheMethodTypeCacheSize) {
Andreas Gampecc1b5352016-12-01 16:58:38 -080096 num_method_types = dex_file->NumProtoIds();
97 } else {
Vladimir Markof44d36c2017-03-14 14:18:46 +000098 num_method_types = kDexCacheMethodTypeCacheSize;
Andreas Gampecc1b5352016-12-01 16:58:38 -080099 }
100
101 if (num_method_types > 0) {
Vladimir Markof44d36c2017-03-14 14:18:46 +0000102 method_types = reinterpret_cast<MethodTypeDexCacheType*>(
Andreas Gampecc1b5352016-12-01 16:58:38 -0800103 raw_arrays + layout.MethodTypesOffset());
104 }
105
Orion Hodsonc069a302017-01-18 09:23:12 +0000106 GcRoot<mirror::CallSite>* call_sites = (dex_file->NumCallSiteIds() == 0)
107 ? nullptr
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100108 : reinterpret_cast<GcRoot<CallSite>*>(raw_arrays + layout.CallSitesOffset());
Orion Hodsonc069a302017-01-18 09:23:12 +0000109
Vladimir Markof44d36c2017-03-14 14:18:46 +0000110 DCHECK_ALIGNED(raw_arrays, alignof(StringDexCacheType)) <<
Andreas Gampecc1b5352016-12-01 16:58:38 -0800111 "Expected raw_arrays to align to StringDexCacheType.";
Vladimir Markof44d36c2017-03-14 14:18:46 +0000112 DCHECK_ALIGNED(layout.StringsOffset(), alignof(StringDexCacheType)) <<
Andreas Gampecc1b5352016-12-01 16:58:38 -0800113 "Expected StringsOffset() to align to StringDexCacheType.";
Vladimir Markof44d36c2017-03-14 14:18:46 +0000114 DCHECK_ALIGNED(strings, alignof(StringDexCacheType)) <<
Andreas Gampecc1b5352016-12-01 16:58:38 -0800115 "Expected strings to align to StringDexCacheType.";
Vladimir Markof44d36c2017-03-14 14:18:46 +0000116 static_assert(alignof(StringDexCacheType) == 8u,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800117 "Expected StringDexCacheType to have align of 8.");
118 if (kIsDebugBuild) {
119 // Sanity check to make sure all the dex cache arrays are empty. b/28992179
120 for (size_t i = 0; i < num_strings; ++i) {
121 CHECK_EQ(strings[i].load(std::memory_order_relaxed).index, 0u);
122 CHECK(strings[i].load(std::memory_order_relaxed).object.IsNull());
123 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000124 for (size_t i = 0; i < num_types; ++i) {
125 CHECK_EQ(types[i].load(std::memory_order_relaxed).index, 0u);
126 CHECK(types[i].load(std::memory_order_relaxed).object.IsNull());
Andreas Gampecc1b5352016-12-01 16:58:38 -0800127 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100128 for (size_t i = 0; i < num_methods; ++i) {
129 CHECK_EQ(GetNativePairPtrSize(methods, i, image_pointer_size).index, 0u);
130 CHECK(GetNativePairPtrSize(methods, i, image_pointer_size).object == nullptr);
Andreas Gampecc1b5352016-12-01 16:58:38 -0800131 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000132 for (size_t i = 0; i < num_fields; ++i) {
133 CHECK_EQ(GetNativePairPtrSize(fields, i, image_pointer_size).index, 0u);
134 CHECK(GetNativePairPtrSize(fields, i, image_pointer_size).object == nullptr);
Andreas Gampecc1b5352016-12-01 16:58:38 -0800135 }
136 for (size_t i = 0; i < num_method_types; ++i) {
137 CHECK_EQ(method_types[i].load(std::memory_order_relaxed).index, 0u);
138 CHECK(method_types[i].load(std::memory_order_relaxed).object.IsNull());
139 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000140 for (size_t i = 0; i < dex_file->NumCallSiteIds(); ++i) {
141 CHECK(call_sites[i].IsNull());
142 }
Andreas Gampecc1b5352016-12-01 16:58:38 -0800143 }
144 if (strings != nullptr) {
145 mirror::StringDexCachePair::Initialize(strings);
146 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000147 if (types != nullptr) {
148 mirror::TypeDexCachePair::Initialize(types);
149 }
Vladimir Markof44d36c2017-03-14 14:18:46 +0000150 if (fields != nullptr) {
151 mirror::FieldDexCachePair::Initialize(fields, image_pointer_size);
152 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100153 if (methods != nullptr) {
154 mirror::MethodDexCachePair::Initialize(methods, image_pointer_size);
155 }
Andreas Gampecc1b5352016-12-01 16:58:38 -0800156 if (method_types != nullptr) {
157 mirror::MethodTypeDexCachePair::Initialize(method_types);
158 }
159 dex_cache->Init(dex_file,
160 location,
161 strings,
162 num_strings,
163 types,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000164 num_types,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800165 methods,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100166 num_methods,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800167 fields,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000168 num_fields,
Andreas Gampecc1b5352016-12-01 16:58:38 -0800169 method_types,
170 num_method_types,
Orion Hodsonc069a302017-01-18 09:23:12 +0000171 call_sites,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100172 dex_file->NumCallSiteIds());
Andreas Gampecc1b5352016-12-01 16:58:38 -0800173}
174
Vladimir Marko05792b92015-08-03 11:56:49 +0100175void DexCache::Init(const DexFile* dex_file,
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700176 ObjPtr<String> location,
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700177 StringDexCacheType* strings,
Vladimir Marko05792b92015-08-03 11:56:49 +0100178 uint32_t num_strings,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000179 TypeDexCacheType* resolved_types,
Vladimir Marko05792b92015-08-03 11:56:49 +0100180 uint32_t num_resolved_types,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100181 MethodDexCacheType* resolved_methods,
Vladimir Marko05792b92015-08-03 11:56:49 +0100182 uint32_t num_resolved_methods,
Vladimir Markof44d36c2017-03-14 14:18:46 +0000183 FieldDexCacheType* resolved_fields,
Vladimir Marko05792b92015-08-03 11:56:49 +0100184 uint32_t num_resolved_fields,
Narayan Kamath25352fc2016-08-03 12:46:58 +0100185 MethodTypeDexCacheType* resolved_method_types,
186 uint32_t num_resolved_method_types,
Orion Hodsonc069a302017-01-18 09:23:12 +0000187 GcRoot<CallSite>* resolved_call_sites,
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100188 uint32_t num_resolved_call_sites) {
Ian Rogers5ddb4102014-01-07 08:58:46 -0800189 CHECK(dex_file != nullptr);
190 CHECK(location != nullptr);
Vladimir Marko05792b92015-08-03 11:56:49 +0100191 CHECK_EQ(num_strings != 0u, strings != nullptr);
192 CHECK_EQ(num_resolved_types != 0u, resolved_types != nullptr);
193 CHECK_EQ(num_resolved_methods != 0u, resolved_methods != nullptr);
194 CHECK_EQ(num_resolved_fields != 0u, resolved_fields != nullptr);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100195 CHECK_EQ(num_resolved_method_types != 0u, resolved_method_types != nullptr);
Orion Hodsonc069a302017-01-18 09:23:12 +0000196 CHECK_EQ(num_resolved_call_sites != 0u, resolved_call_sites != nullptr);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700197
Mathieu Chartierc7853442015-03-27 14:35:38 -0700198 SetDexFile(dex_file);
Mathieu Chartier76172162016-01-26 14:54:06 -0800199 SetLocation(location);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800200 SetStrings(strings);
201 SetResolvedTypes(resolved_types);
202 SetResolvedMethods(resolved_methods);
203 SetResolvedFields(resolved_fields);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100204 SetResolvedMethodTypes(resolved_method_types);
Orion Hodsonc069a302017-01-18 09:23:12 +0000205 SetResolvedCallSites(resolved_call_sites);
Vladimir Marko05792b92015-08-03 11:56:49 +0100206 SetField32<false>(NumStringsOffset(), num_strings);
207 SetField32<false>(NumResolvedTypesOffset(), num_resolved_types);
208 SetField32<false>(NumResolvedMethodsOffset(), num_resolved_methods);
209 SetField32<false>(NumResolvedFieldsOffset(), num_resolved_fields);
Narayan Kamath25352fc2016-08-03 12:46:58 +0100210 SetField32<false>(NumResolvedMethodTypesOffset(), num_resolved_method_types);
Orion Hodsonc069a302017-01-18 09:23:12 +0000211 SetField32<false>(NumResolvedCallSitesOffset(), num_resolved_call_sites);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700212}
213
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700214void DexCache::SetLocation(ObjPtr<mirror::String> location) {
Mathieu Chartier76172162016-01-26 14:54:06 -0800215 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_), location);
216}
217
Alexey Frunze279cfba2017-07-22 00:24:43 -0700218#if !defined(__aarch64__) && !defined(__x86_64__) && !defined(__mips__)
Vladimir Markof44d36c2017-03-14 14:18:46 +0000219static pthread_mutex_t dex_cache_slow_atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
220
221DexCache::ConversionPair64 DexCache::AtomicLoadRelaxed16B(std::atomic<ConversionPair64>* target) {
222 pthread_mutex_lock(&dex_cache_slow_atomic_mutex);
223 DexCache::ConversionPair64 value = *reinterpret_cast<ConversionPair64*>(target);
224 pthread_mutex_unlock(&dex_cache_slow_atomic_mutex);
225 return value;
226}
227
228void DexCache::AtomicStoreRelease16B(std::atomic<ConversionPair64>* target,
229 ConversionPair64 value) {
230 pthread_mutex_lock(&dex_cache_slow_atomic_mutex);
231 *reinterpret_cast<ConversionPair64*>(target) = value;
232 pthread_mutex_unlock(&dex_cache_slow_atomic_mutex);
233}
234#endif
235
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236} // namespace mirror
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700237} // namespace art