Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_CLASS_LINKER_INL_H_ |
| 18 | #define ART_RUNTIME_CLASS_LINKER_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 20 | #include "art_field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "class_linker.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 22 | #include "gc_root-inl.h" |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 23 | #include "gc/heap-inl.h" |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 24 | #include "obj_ptr-inl.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 25 | #include "mirror/class_loader.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 26 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/iftable.h" |
| 28 | #include "mirror/object_array.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 29 | #include "handle_scope-inl.h" |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 30 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 32 | #include <atomic> |
| 33 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 36 | inline mirror::Class* ClassLinker::FindSystemClass(Thread* self, const char* descriptor) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 37 | return FindClass(self, descriptor, ScopedNullHandle<mirror::ClassLoader>()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 40 | inline mirror::Class* ClassLinker::FindArrayClass(Thread* self, |
| 41 | ObjPtr<mirror::Class>* element_class) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 42 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 43 | // Read the cached array class once to avoid races with other threads setting it. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 44 | ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read(); |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 45 | if (array_class != nullptr && array_class->GetComponentType() == *element_class) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 46 | return array_class.Ptr(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 49 | std::string descriptor = "["; |
| 50 | std::string temp; |
| 51 | descriptor += (*element_class)->GetDescriptor(&temp); |
Mathieu Chartier | b74cd29 | 2014-05-29 14:31:33 -0700 | [diff] [blame] | 52 | StackHandleScope<2> hs(Thread::Current()); |
| 53 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle((*element_class)->GetClassLoader())); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 54 | HandleWrapperObjPtr<mirror::Class> h_element_class(hs.NewHandleWrapper(element_class)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 55 | ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader); |
Nicolas Geoffray | 9638b64 | 2015-06-23 18:16:46 +0100 | [diff] [blame] | 56 | if (array_class != nullptr) { |
| 57 | // Benign races in storing array class and incrementing index. |
| 58 | size_t victim_index = find_array_class_cache_next_victim_; |
| 59 | find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class); |
| 60 | find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize; |
| 61 | } else { |
| 62 | // We should have a NoClassDefFoundError. |
| 63 | self->AssertPendingException(); |
| 64 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 65 | return array_class.Ptr(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 68 | inline mirror::String* ClassLinker::ResolveString(uint32_t string_idx, ArtMethod* referrer) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 69 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 70 | ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 71 | // MethodVerifier refuses methods with string_idx out of bounds. |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame^] | 72 | DCHECK_LT(string_idx, declaring_class->GetDexFile().NumStringIds()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 73 | ObjPtr<mirror::String> string = |
Narayan Kamath | c38a6f8 | 2016-09-29 17:07:20 +0100 | [diff] [blame] | 74 | mirror::StringDexCachePair::Lookup(declaring_class->GetDexCacheStrings(), |
| 75 | string_idx, |
| 76 | mirror::DexCache::kDexCacheStringCacheSize).Read(); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 77 | if (UNLIKELY(string == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 78 | StackHandleScope<1> hs(Thread::Current()); |
| 79 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 81 | string = ResolveString(dex_file, string_idx, dex_cache); |
| 82 | if (string != nullptr) { |
| 83 | DCHECK_EQ(dex_cache->GetResolvedString(string_idx), string); |
Ian Rogers | eebe03a | 2014-05-02 11:09:57 -0700 | [diff] [blame] | 84 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 86 | return string.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 89 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtMethod* referrer) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 90 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 91 | ObjPtr<mirror::Class> resolved_type = |
| 92 | referrer->GetDexCacheResolvedType(type_idx, image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 93 | if (UNLIKELY(resolved_type == nullptr)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 94 | ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 95 | StackHandleScope<2> hs(Thread::Current()); |
| 96 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 97 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 99 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 100 | // Note: We cannot check here to see whether we added the type to the cache. The type |
| 101 | // might be an erroneous class, which results in it being hidden from us. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 102 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 103 | return resolved_type.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 106 | inline mirror::Class* ClassLinker::ResolveType(uint16_t type_idx, ArtField* referrer) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 107 | Thread::PoisonObjectPointersIfDebug(); |
| 108 | ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 109 | ObjPtr<mirror::DexCache> dex_cache_ptr = declaring_class->GetDexCache(); |
| 110 | ObjPtr<mirror::Class> resolved_type = dex_cache_ptr->GetResolvedType(type_idx); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 111 | if (UNLIKELY(resolved_type == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 112 | StackHandleScope<2> hs(Thread::Current()); |
| 113 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_cache_ptr)); |
| 114 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 116 | resolved_type = ResolveType(dex_file, type_idx, dex_cache, class_loader); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 117 | // Note: We cannot check here to see whether we added the type to the cache. The type |
| 118 | // might be an erroneous class, which results in it being hidden from us. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 120 | return resolved_type.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 123 | inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 124 | ArtMethod* resolved_method = referrer->GetDexCacheResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 125 | if (resolved_method == nullptr || resolved_method->IsRuntimeMethod()) { |
| 126 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 127 | } |
| 128 | return resolved_method; |
| 129 | } |
| 130 | |
Nicolas Geoffray | 393fdb8 | 2016-04-25 14:58:06 +0100 | [diff] [blame] | 131 | inline mirror::Class* ClassLinker::ResolveReferencedClassOfMethod( |
| 132 | uint32_t method_idx, |
| 133 | Handle<mirror::DexCache> dex_cache, |
| 134 | Handle<mirror::ClassLoader> class_loader) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 135 | // NB: We cannot simply use `GetResolvedMethod(method_idx, ...)->GetDeclaringClass()`. This is |
| 136 | // because if we did so than an invoke-super could be incorrectly dispatched in cases where |
| 137 | // GetMethodId(method_idx).class_idx_ refers to a non-interface, non-direct-superclass |
| 138 | // (super*-class?) of the referrer and the direct superclass of the referrer contains a concrete |
| 139 | // implementation of the method. If this class's implementation of the method is copied from an |
| 140 | // interface (either miranda, default or conflict) we would incorrectly assume that is what we |
| 141 | // want to invoke on, instead of the 'concrete' implementation that the direct superclass |
| 142 | // contains. |
Nicolas Geoffray | 393fdb8 | 2016-04-25 14:58:06 +0100 | [diff] [blame] | 143 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 144 | const DexFile::MethodId& method = dex_file->GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 145 | ObjPtr<mirror::Class> resolved_type = dex_cache->GetResolvedType(method.class_idx_); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 146 | if (UNLIKELY(resolved_type == nullptr)) { |
Nicolas Geoffray | 393fdb8 | 2016-04-25 14:58:06 +0100 | [diff] [blame] | 147 | resolved_type = ResolveType(*dex_file, method.class_idx_, dex_cache, class_loader); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 148 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 149 | return resolved_type.Ptr(); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 152 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 153 | inline ArtMethod* ClassLinker::ResolveMethod(Thread* self, |
| 154 | uint32_t method_idx, |
| 155 | ArtMethod* referrer, |
| 156 | InvokeType type) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 157 | ArtMethod* resolved_method = GetResolvedMethod(method_idx, referrer); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 158 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 159 | if (UNLIKELY(resolved_method == nullptr)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 160 | ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 161 | StackHandleScope<2> hs(self); |
| 162 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 163 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
| 164 | const DexFile* dex_file = h_dex_cache->GetDexFile(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 165 | resolved_method = ResolveMethod<kResolveMode>(*dex_file, |
| 166 | method_idx, |
| 167 | h_dex_cache, |
| 168 | h_class_loader, |
| 169 | referrer, |
| 170 | type); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 171 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 172 | // Note: We cannot check here to see whether we added the method to the cache. It |
| 173 | // might be an erroneous class, which results in it being hidden from us. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 174 | return resolved_method; |
| 175 | } |
| 176 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 177 | inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, |
| 178 | ObjPtr<mirror::DexCache> dex_cache) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 179 | return dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 182 | inline ArtField* ClassLinker::GetResolvedField(uint32_t field_idx, |
| 183 | ObjPtr<mirror::Class> field_declaring_class) { |
| 184 | return GetResolvedField(field_idx, MakeObjPtr(field_declaring_class->GetDexCache())); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 187 | inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
| 188 | ArtMethod* referrer, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 189 | bool is_static) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 190 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 191 | ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass(); |
| 192 | ArtField* resolved_field = GetResolvedField(field_idx, declaring_class); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 193 | if (UNLIKELY(resolved_field == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 194 | StackHandleScope<2> hs(Thread::Current()); |
| 195 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 196 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 198 | resolved_field = ResolveField(dex_file, field_idx, dex_cache, class_loader, is_static); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 199 | // Note: We cannot check here to see whether we added the field to the cache. The type |
| 200 | // might be an erroneous class, which results in it being hidden from us. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 201 | } |
| 202 | return resolved_field; |
| 203 | } |
| 204 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 205 | inline mirror::Object* ClassLinker::AllocObject(Thread* self) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 206 | return GetClassRoot(kJavaLangObject)->Alloc<true, false>( |
| 207 | self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 208 | Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr(); |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 211 | template <class T> |
| 212 | inline mirror::ObjectArray<T>* ClassLinker::AllocObjectArray(Thread* self, size_t length) { |
| 213 | return mirror::ObjectArray<T>::Alloc(self, GetClassRoot(kObjectArrayClass), length); |
| 214 | } |
| 215 | |
| 216 | inline mirror::ObjectArray<mirror::Class>* ClassLinker::AllocClassArray(Thread* self, |
| 217 | size_t length) { |
| 218 | return mirror::ObjectArray<mirror::Class>::Alloc(self, GetClassRoot(kClassArrayClass), length); |
| 219 | } |
| 220 | |
| 221 | inline mirror::ObjectArray<mirror::String>* ClassLinker::AllocStringArray(Thread* self, |
| 222 | size_t length) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 223 | return mirror::ObjectArray<mirror::String>::Alloc(self, |
| 224 | GetClassRoot(kJavaLangStringArrayClass), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | length); |
| 226 | } |
| 227 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 228 | inline mirror::IfTable* ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 229 | return down_cast<mirror::IfTable*>( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 230 | mirror::IfTable::Alloc(self, |
| 231 | GetClassRoot(kObjectArrayClass), |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 232 | ifcount * mirror::IfTable::kMax)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 235 | inline mirror::Class* ClassLinker::GetClassRoot(ClassRoot class_root) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 236 | DCHECK(!class_roots_.IsNull()); |
| 237 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 238 | ObjPtr<mirror::Class> klass = class_roots->Get(class_root); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 239 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 240 | return klass.Ptr(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Hiroshi Yamauchi | 7a62e67 | 2016-06-10 17:22:48 -0700 | [diff] [blame] | 243 | template<ReadBarrierOption kReadBarrierOption> |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 244 | ArtMethod* ClassLinker::FindMethodForProxy(ObjPtr<mirror::Class> proxy_class, |
| 245 | ArtMethod* proxy_method) { |
Hiroshi Yamauchi | 7a62e67 | 2016-06-10 17:22:48 -0700 | [diff] [blame] | 246 | DCHECK(proxy_class->IsProxyClass()); |
| 247 | DCHECK(proxy_method->IsProxyMethod<kReadBarrierOption>()); |
| 248 | { |
| 249 | Thread* const self = Thread::Current(); |
| 250 | ReaderMutexLock mu(self, dex_lock_); |
| 251 | // Locate the dex cache of the original interface/Object |
| 252 | for (const DexCacheData& data : dex_caches_) { |
| 253 | if (!self->IsJWeakCleared(data.weak_root) && |
| 254 | proxy_method->HasSameDexCacheResolvedTypes(data.resolved_types, |
| 255 | image_pointer_size_)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 256 | ObjPtr<mirror::DexCache> dex_cache = |
| 257 | ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)); |
Hiroshi Yamauchi | 7a62e67 | 2016-06-10 17:22:48 -0700 | [diff] [blame] | 258 | if (dex_cache != nullptr) { |
| 259 | ArtMethod* resolved_method = dex_cache->GetResolvedMethod( |
| 260 | proxy_method->GetDexMethodIndex(), image_pointer_size_); |
| 261 | CHECK(resolved_method != nullptr); |
| 262 | return resolved_method; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 267 | LOG(FATAL) << "Didn't find dex cache for " << proxy_class->PrettyClass() << " " |
| 268 | << proxy_method->PrettyMethod(); |
Hiroshi Yamauchi | 7a62e67 | 2016-06-10 17:22:48 -0700 | [diff] [blame] | 269 | UNREACHABLE(); |
| 270 | } |
| 271 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 272 | } // namespace art |
| 273 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 274 | #endif // ART_RUNTIME_CLASS_LINKER_INL_H_ |