| 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 | |
| Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 17 | #include "dex_cache-inl.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 18 | |
| Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
| Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 20 | #include "class_linker.h" |
| Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 21 | #include "gc/accounting/card_table-inl.h" |
| 22 | #include "gc/heap.h" |
| Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 23 | #include "linear_alloc.h" |
| Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 24 | #include "oat_file.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "object-inl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 26 | #include "object.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "object_array-inl.h" |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 28 | #include "reflective_value_visitor.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "runtime.h" |
| Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 30 | #include "runtime_globals.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "string.h" |
| Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 32 | #include "thread.h" |
| Alex Light | e61f16a | 2019-11-20 13:49:19 -0800 | [diff] [blame] | 33 | #include "write_barrier.h" |
| Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | namespace mirror { |
| Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 37 | |
| Orion Hodson | b9b7d91 | 2021-02-24 09:24:47 +0000 | [diff] [blame] | 38 | template<typename T> |
| 39 | static T* AllocArray(Thread* self, LinearAlloc* alloc, size_t num) { |
| 40 | if (num == 0) { |
| 41 | return nullptr; |
| 42 | } |
| 43 | return reinterpret_cast<T*>(alloc->AllocAlign16(self, RoundUp(num * sizeof(T), 16))); |
| 44 | } |
| 45 | |
| 46 | void DexCache::InitializeNativeFields(const DexFile* dex_file, LinearAlloc* linear_alloc) { |
| David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 47 | DCHECK(GetDexFile() == nullptr); |
| 48 | DCHECK(GetStrings() == nullptr); |
| 49 | DCHECK(GetResolvedTypes() == nullptr); |
| 50 | DCHECK(GetResolvedMethods() == nullptr); |
| 51 | DCHECK(GetResolvedFields() == nullptr); |
| 52 | DCHECK(GetResolvedMethodTypes() == nullptr); |
| 53 | DCHECK(GetResolvedCallSites() == nullptr); |
| 54 | |
| Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 55 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
| Orion Hodson | b9b7d91 | 2021-02-24 09:24:47 +0000 | [diff] [blame] | 56 | Thread* self = Thread::Current(); |
| Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 57 | |
| Orion Hodson | b9b7d91 | 2021-02-24 09:24:47 +0000 | [diff] [blame] | 58 | size_t num_strings = std::min<size_t>(kDexCacheStringCacheSize, dex_file->NumStringIds()); |
| 59 | size_t num_types = std::min<size_t>(kDexCacheTypeCacheSize, dex_file->NumTypeIds()); |
| 60 | size_t num_fields = std::min<size_t>(kDexCacheFieldCacheSize, dex_file->NumFieldIds()); |
| 61 | size_t num_methods = std::min<size_t>(kDexCacheMethodCacheSize, dex_file->NumMethodIds()); |
| 62 | size_t num_method_types = std::min<size_t>(kDexCacheMethodTypeCacheSize, dex_file->NumProtoIds()); |
| 63 | size_t num_call_sites = dex_file->NumCallSiteIds(); // Full size. |
| 64 | |
| 65 | static_assert(ArenaAllocator::kAlignment == 8, "Expecting arena alignment of 8."); |
| 66 | StringDexCacheType* strings = |
| 67 | AllocArray<StringDexCacheType>(self, linear_alloc, num_strings); |
| 68 | TypeDexCacheType* types = |
| 69 | AllocArray<TypeDexCacheType>(self, linear_alloc, num_types); |
| 70 | MethodDexCacheType* methods = |
| 71 | AllocArray<MethodDexCacheType>(self, linear_alloc, num_methods); |
| 72 | FieldDexCacheType* fields = |
| 73 | AllocArray<FieldDexCacheType>(self, linear_alloc, num_fields); |
| 74 | MethodTypeDexCacheType* method_types = |
| 75 | AllocArray<MethodTypeDexCacheType>(self, linear_alloc, num_method_types); |
| 76 | GcRoot<mirror::CallSite>* call_sites = |
| 77 | AllocArray<GcRoot<CallSite>>(self, linear_alloc, num_call_sites); |
| 78 | |
| 79 | DCHECK_ALIGNED(types, alignof(StringDexCacheType)) << |
| 80 | "Expected StringsOffset() to align to StringDexCacheType."; |
| 81 | DCHECK_ALIGNED(strings, alignof(StringDexCacheType)) << |
| 82 | "Expected strings to align to StringDexCacheType."; |
| 83 | static_assert(alignof(StringDexCacheType) == 8u, |
| 84 | "Expected StringDexCacheType to have align of 8."); |
| 85 | if (kIsDebugBuild) { |
| 86 | // Consistency check to make sure all the dex cache arrays are empty. b/28992179 |
| 87 | for (size_t i = 0; i < num_strings; ++i) { |
| 88 | CHECK_EQ(strings[i].load(std::memory_order_relaxed).index, 0u); |
| 89 | CHECK(strings[i].load(std::memory_order_relaxed).object.IsNull()); |
| 90 | } |
| 91 | for (size_t i = 0; i < num_types; ++i) { |
| 92 | CHECK_EQ(types[i].load(std::memory_order_relaxed).index, 0u); |
| 93 | CHECK(types[i].load(std::memory_order_relaxed).object.IsNull()); |
| 94 | } |
| 95 | for (size_t i = 0; i < num_methods; ++i) { |
| 96 | CHECK_EQ(GetNativePair(methods, i).index, 0u); |
| 97 | CHECK(GetNativePair(methods, i).object == nullptr); |
| 98 | } |
| 99 | for (size_t i = 0; i < num_fields; ++i) { |
| 100 | CHECK_EQ(GetNativePair(fields, i).index, 0u); |
| 101 | CHECK(GetNativePair(fields, i).object == nullptr); |
| 102 | } |
| 103 | for (size_t i = 0; i < num_method_types; ++i) { |
| 104 | CHECK_EQ(method_types[i].load(std::memory_order_relaxed).index, 0u); |
| 105 | CHECK(method_types[i].load(std::memory_order_relaxed).object.IsNull()); |
| 106 | } |
| 107 | for (size_t i = 0; i < dex_file->NumCallSiteIds(); ++i) { |
| 108 | CHECK(call_sites[i].IsNull()); |
| 109 | } |
| 110 | } |
| 111 | if (strings != nullptr) { |
| 112 | mirror::StringDexCachePair::Initialize(strings); |
| 113 | } |
| 114 | if (types != nullptr) { |
| 115 | mirror::TypeDexCachePair::Initialize(types); |
| 116 | } |
| 117 | if (fields != nullptr) { |
| 118 | mirror::FieldDexCachePair::Initialize(fields); |
| 119 | } |
| 120 | if (methods != nullptr) { |
| 121 | mirror::MethodDexCachePair::Initialize(methods); |
| 122 | } |
| 123 | if (method_types != nullptr) { |
| 124 | mirror::MethodTypeDexCachePair::Initialize(method_types); |
| 125 | } |
| David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 126 | SetDexFile(dex_file); |
| Orion Hodson | b9b7d91 | 2021-02-24 09:24:47 +0000 | [diff] [blame] | 127 | SetNativeArrays(strings, |
| 128 | num_strings, |
| 129 | types, |
| 130 | num_types, |
| 131 | methods, |
| 132 | num_methods, |
| 133 | fields, |
| 134 | num_fields, |
| 135 | method_types, |
| 136 | num_method_types, |
| 137 | call_sites, |
| 138 | num_call_sites); |
| 139 | } |
| 140 | |
| 141 | void DexCache::ResetNativeFields() { |
| 142 | SetDexFile(nullptr); |
| 143 | SetNativeArrays(nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0); |
| Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 146 | void DexCache::VisitReflectiveTargets(ReflectiveValueVisitor* visitor) { |
| Alex Light | e61f16a | 2019-11-20 13:49:19 -0800 | [diff] [blame] | 147 | bool wrote = false; |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 148 | for (size_t i = 0; i < NumResolvedFields(); i++) { |
| David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 149 | auto pair(GetNativePair(GetResolvedFields(), i)); |
| Alex Light | 28b6efe | 2019-11-06 12:49:41 -0800 | [diff] [blame] | 150 | if (pair.index == FieldDexCachePair::InvalidIndexForSlot(i)) { |
| 151 | continue; |
| 152 | } |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 153 | ArtField* new_val = visitor->VisitField( |
| 154 | pair.object, DexCacheSourceInfo(kSourceDexCacheResolvedField, pair.index, this)); |
| 155 | if (UNLIKELY(new_val != pair.object)) { |
| Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 156 | if (new_val == nullptr) { |
| 157 | pair = FieldDexCachePair(nullptr, FieldDexCachePair::InvalidIndexForSlot(i)); |
| 158 | } else { |
| 159 | pair.object = new_val; |
| 160 | } |
| David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 161 | SetNativePair(GetResolvedFields(), i, pair); |
| Alex Light | e61f16a | 2019-11-20 13:49:19 -0800 | [diff] [blame] | 162 | wrote = true; |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | for (size_t i = 0; i < NumResolvedMethods(); i++) { |
| David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 166 | auto pair(GetNativePair(GetResolvedMethods(), i)); |
| Alex Light | 28b6efe | 2019-11-06 12:49:41 -0800 | [diff] [blame] | 167 | if (pair.index == MethodDexCachePair::InvalidIndexForSlot(i)) { |
| 168 | continue; |
| 169 | } |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 170 | ArtMethod* new_val = visitor->VisitMethod( |
| 171 | pair.object, DexCacheSourceInfo(kSourceDexCacheResolvedMethod, pair.index, this)); |
| 172 | if (UNLIKELY(new_val != pair.object)) { |
| Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 173 | if (new_val == nullptr) { |
| 174 | pair = MethodDexCachePair(nullptr, MethodDexCachePair::InvalidIndexForSlot(i)); |
| 175 | } else { |
| 176 | pair.object = new_val; |
| 177 | } |
| David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 178 | SetNativePair(GetResolvedMethods(), i, pair); |
| Alex Light | e61f16a | 2019-11-20 13:49:19 -0800 | [diff] [blame] | 179 | wrote = true; |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| Alex Light | e61f16a | 2019-11-20 13:49:19 -0800 | [diff] [blame] | 182 | if (wrote) { |
| 183 | WriteBarrier::ForEveryFieldWrite(this); |
| 184 | } |
| Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| Orion Hodson | b9b7d91 | 2021-02-24 09:24:47 +0000 | [diff] [blame] | 187 | bool DexCache::AddPreResolvedStringsArray() { |
| 188 | DCHECK_EQ(NumPreResolvedStrings(), 0u); |
| 189 | Thread* const self = Thread::Current(); |
| 190 | LinearAlloc* linear_alloc = Runtime::Current()->GetLinearAlloc(); |
| 191 | const size_t num_strings = GetDexFile()->NumStringIds(); |
| 192 | if (num_strings != 0) { |
| 193 | GcRoot<mirror::String>* strings = |
| 194 | linear_alloc->AllocArray<GcRoot<mirror::String>>(self, num_strings); |
| 195 | if (strings == nullptr) { |
| 196 | // Failed to allocate pre-resolved string array (probably due to address fragmentation), bail. |
| 197 | return false; |
| 198 | } |
| 199 | SetField32<false>(NumPreResolvedStringsOffset(), num_strings); |
| 200 | |
| 201 | CHECK(strings != nullptr); |
| 202 | SetPreResolvedStrings(strings); |
| 203 | for (size_t i = 0; i < GetDexFile()->NumStringIds(); ++i) { |
| 204 | CHECK(GetPreResolvedStrings()[i].Read() == nullptr); |
| 205 | } |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | void DexCache::SetNativeArrays(StringDexCacheType* strings, |
| 211 | uint32_t num_strings, |
| 212 | TypeDexCacheType* resolved_types, |
| 213 | uint32_t num_resolved_types, |
| 214 | MethodDexCacheType* resolved_methods, |
| 215 | uint32_t num_resolved_methods, |
| 216 | FieldDexCacheType* resolved_fields, |
| 217 | uint32_t num_resolved_fields, |
| 218 | MethodTypeDexCacheType* resolved_method_types, |
| 219 | uint32_t num_resolved_method_types, |
| 220 | GcRoot<CallSite>* resolved_call_sites, |
| 221 | uint32_t num_resolved_call_sites) { |
| 222 | CHECK_EQ(num_strings != 0u, strings != nullptr); |
| 223 | CHECK_EQ(num_resolved_types != 0u, resolved_types != nullptr); |
| 224 | CHECK_EQ(num_resolved_methods != 0u, resolved_methods != nullptr); |
| 225 | CHECK_EQ(num_resolved_fields != 0u, resolved_fields != nullptr); |
| 226 | CHECK_EQ(num_resolved_method_types != 0u, resolved_method_types != nullptr); |
| 227 | CHECK_EQ(num_resolved_call_sites != 0u, resolved_call_sites != nullptr); |
| 228 | SetStrings(strings); |
| 229 | SetResolvedTypes(resolved_types); |
| 230 | SetResolvedMethods(resolved_methods); |
| 231 | SetResolvedFields(resolved_fields); |
| 232 | SetResolvedMethodTypes(resolved_method_types); |
| 233 | SetResolvedCallSites(resolved_call_sites); |
| 234 | SetField32<false>(NumStringsOffset(), num_strings); |
| 235 | SetField32<false>(NumResolvedTypesOffset(), num_resolved_types); |
| 236 | SetField32<false>(NumResolvedMethodsOffset(), num_resolved_methods); |
| 237 | SetField32<false>(NumResolvedFieldsOffset(), num_resolved_fields); |
| 238 | SetField32<false>(NumResolvedMethodTypesOffset(), num_resolved_method_types); |
| 239 | SetField32<false>(NumResolvedCallSitesOffset(), num_resolved_call_sites); |
| Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 242 | void DexCache::SetLocation(ObjPtr<mirror::String> location) { |
| Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 243 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_), location); |
| 244 | } |
| 245 | |
| Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 246 | void DexCache::SetClassLoader(ObjPtr<ClassLoader> class_loader) { |
| 247 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, class_loader_), class_loader); |
| 248 | } |
| 249 | |
| Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 250 | ObjPtr<ClassLoader> DexCache::GetClassLoader() { |
| 251 | return GetFieldObject<mirror::ClassLoader>(OFFSET_OF_OBJECT_MEMBER(DexCache, class_loader_)); |
| 252 | } |
| 253 | |
| Vladimir Marko | 41b605c | 2020-02-12 10:52:22 +0000 | [diff] [blame] | 254 | #if !defined(__aarch64__) && !defined(__x86_64__) |
| Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 255 | static pthread_mutex_t dex_cache_slow_atomic_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 256 | |
| 257 | DexCache::ConversionPair64 DexCache::AtomicLoadRelaxed16B(std::atomic<ConversionPair64>* target) { |
| 258 | pthread_mutex_lock(&dex_cache_slow_atomic_mutex); |
| 259 | DexCache::ConversionPair64 value = *reinterpret_cast<ConversionPair64*>(target); |
| 260 | pthread_mutex_unlock(&dex_cache_slow_atomic_mutex); |
| 261 | return value; |
| 262 | } |
| 263 | |
| 264 | void DexCache::AtomicStoreRelease16B(std::atomic<ConversionPair64>* target, |
| 265 | ConversionPair64 value) { |
| 266 | pthread_mutex_lock(&dex_cache_slow_atomic_mutex); |
| 267 | *reinterpret_cast<ConversionPair64*>(target) = value; |
| 268 | pthread_mutex_unlock(&dex_cache_slow_atomic_mutex); |
| 269 | } |
| 270 | #endif |
| 271 | |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 272 | } // namespace mirror |
| Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 273 | } // namespace art |