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 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 20 | #include <atomic> |
| 21 | |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 22 | #include "android-base/thread_annotations.h" |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 23 | #include "art_field-inl.h" |
| 24 | #include "art_method-inl.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 25 | #include "base/mutex.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "class_linker.h" |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 27 | #include "dex/dex_file.h" |
| 28 | #include "dex/dex_file_structs.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include "gc_root-inl.h" |
| 30 | #include "handle_scope-inl.h" |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 31 | #include "jni/jni_internal.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 32 | #include "mirror/class_loader.h" |
Mathieu Chartier | bc56fc3 | 2014-06-03 15:37:03 -0700 | [diff] [blame] | 33 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "mirror/iftable.h" |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 35 | #include "mirror/object_array-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "obj_ptr-inl.h" |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change-inl.h" |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 38 | #include "well_known_classes.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 42 | inline ObjPtr<mirror::Class> ClassLinker::FindArrayClass(Thread* self, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 43 | ObjPtr<mirror::Class> element_class) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 44 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 45 | // 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] | 46 | ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 47 | if (array_class != nullptr && array_class->GetComponentType() == element_class) { |
| 48 | return array_class; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 51 | std::string descriptor = "["; |
| 52 | std::string temp; |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 53 | descriptor += element_class->GetDescriptor(&temp); |
| 54 | StackHandleScope<1> hs(Thread::Current()); |
| 55 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(element_class->GetClassLoader())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 56 | ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader); |
Nicolas Geoffray | 9638b64 | 2015-06-23 18:16:46 +0100 | [diff] [blame] | 57 | if (array_class != nullptr) { |
| 58 | // Benign races in storing array class and incrementing index. |
| 59 | size_t victim_index = find_array_class_cache_next_victim_; |
| 60 | find_array_class_cache_[victim_index] = GcRoot<mirror::Class>(array_class); |
| 61 | find_array_class_cache_next_victim_ = (victim_index + 1) % kFindArrayCacheSize; |
| 62 | } else { |
| 63 | // We should have a NoClassDefFoundError. |
| 64 | self->AssertPendingException(); |
| 65 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 66 | return array_class; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 69 | inline ObjPtr<mirror::String> ClassLinker::ResolveString(dex::StringIndex string_idx, |
| 70 | ArtField* referrer) { |
| 71 | Thread::PoisonObjectPointersIfDebug(); |
| 72 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 73 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 74 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 75 | ObjPtr<mirror::String> resolved = |
| 76 | referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedString(string_idx); |
| 77 | if (resolved == nullptr) { |
| 78 | resolved = DoResolveString(string_idx, referrer->GetDexCache()); |
| 79 | } |
| 80 | return resolved; |
| 81 | } |
| 82 | |
| 83 | inline ObjPtr<mirror::String> ClassLinker::ResolveString(dex::StringIndex string_idx, |
| 84 | ArtMethod* referrer) { |
| 85 | Thread::PoisonObjectPointersIfDebug(); |
| 86 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 87 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 88 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 89 | ObjPtr<mirror::String> resolved = |
| 90 | referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedString(string_idx); |
| 91 | if (resolved == nullptr) { |
| 92 | resolved = DoResolveString(string_idx, referrer->GetDexCache()); |
| 93 | } |
| 94 | return resolved; |
| 95 | } |
| 96 | |
| 97 | inline ObjPtr<mirror::String> ClassLinker::ResolveString(dex::StringIndex string_idx, |
| 98 | Handle<mirror::DexCache> dex_cache) { |
| 99 | Thread::PoisonObjectPointersIfDebug(); |
| 100 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 101 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
| 102 | if (resolved == nullptr) { |
| 103 | resolved = DoResolveString(string_idx, dex_cache); |
| 104 | } |
| 105 | return resolved; |
| 106 | } |
| 107 | |
| 108 | inline ObjPtr<mirror::String> ClassLinker::LookupString(dex::StringIndex string_idx, |
| 109 | ObjPtr<mirror::DexCache> dex_cache) { |
| 110 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
| 111 | if (resolved == nullptr) { |
| 112 | resolved = DoLookupString(string_idx, dex_cache); |
| 113 | } |
| 114 | return resolved; |
| 115 | } |
| 116 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 117 | inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx, |
| 118 | ObjPtr<mirror::Class> referrer) { |
| 119 | if (kObjPtrPoisoning) { |
| 120 | StackHandleScope<1> hs(Thread::Current()); |
| 121 | HandleWrapperObjPtr<mirror::Class> referrer_wrapper = hs.NewHandleWrapper(&referrer); |
| 122 | Thread::Current()->PoisonObjectPointers(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 123 | } |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 124 | DCHECK(!Thread::Current()->IsExceptionPending()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 125 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 126 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 127 | ObjPtr<mirror::Class> resolved_type = |
| 128 | referrer->GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetResolvedType(type_idx); |
| 129 | if (resolved_type == nullptr) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 130 | resolved_type = DoResolveType(type_idx, referrer); |
| 131 | } |
| 132 | return resolved_type; |
| 133 | } |
| 134 | |
| 135 | inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx, |
| 136 | ArtField* referrer) { |
| 137 | Thread::PoisonObjectPointersIfDebug(); |
| 138 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 139 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 140 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 141 | ObjPtr<mirror::Class> resolved_type = |
| 142 | referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx); |
| 143 | if (UNLIKELY(resolved_type == nullptr)) { |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 144 | resolved_type = DoResolveType(type_idx, referrer); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 145 | } |
| 146 | return resolved_type; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 149 | inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx, |
| 150 | ArtMethod* referrer) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 151 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 152 | DCHECK(!Thread::Current()->IsExceptionPending()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 153 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 154 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 155 | ObjPtr<mirror::Class> resolved_type = |
| 156 | referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 157 | if (UNLIKELY(resolved_type == nullptr)) { |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 158 | resolved_type = DoResolveType(type_idx, referrer); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 159 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 160 | return resolved_type; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 163 | inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx, |
| 164 | Handle<mirror::DexCache> dex_cache, |
| 165 | Handle<mirror::ClassLoader> class_loader) { |
| 166 | DCHECK(dex_cache != nullptr); |
| 167 | Thread::PoisonObjectPointersIfDebug(); |
| 168 | ObjPtr<mirror::Class> resolved = dex_cache->GetResolvedType(type_idx); |
| 169 | if (resolved == nullptr) { |
| 170 | resolved = DoResolveType(type_idx, dex_cache, class_loader); |
| 171 | } |
| 172 | return resolved; |
| 173 | } |
| 174 | |
| 175 | inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(dex::TypeIndex type_idx, |
| 176 | ObjPtr<mirror::Class> referrer) { |
| 177 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 178 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 179 | ObjPtr<mirror::Class> type = |
| 180 | referrer->GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetResolvedType(type_idx); |
| 181 | if (type == nullptr) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 182 | type = DoLookupResolvedType(type_idx, referrer); |
| 183 | } |
| 184 | return type; |
| 185 | } |
| 186 | |
| 187 | inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(dex::TypeIndex type_idx, |
| 188 | ArtField* referrer) { |
| 189 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 190 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 191 | ObjPtr<mirror::Class> type = |
| 192 | referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx); |
| 193 | if (type == nullptr) { |
| 194 | type = DoLookupResolvedType(type_idx, referrer->GetDeclaringClass()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 195 | } |
| 196 | return type; |
| 197 | } |
| 198 | |
| 199 | inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(dex::TypeIndex type_idx, |
| 200 | ArtMethod* referrer) { |
| 201 | // We do not need the read barrier for getting the DexCache for the initial resolved type |
| 202 | // lookup as both from-space and to-space copies point to the same native resolved types array. |
| 203 | ObjPtr<mirror::Class> type = |
| 204 | referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx); |
| 205 | if (type == nullptr) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 206 | type = DoLookupResolvedType(type_idx, referrer->GetDeclaringClass()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 207 | } |
| 208 | return type; |
| 209 | } |
| 210 | |
| 211 | inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType( |
| 212 | dex::TypeIndex type_idx, |
| 213 | ObjPtr<mirror::DexCache> dex_cache, |
| 214 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 215 | ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx); |
| 216 | if (type == nullptr) { |
| 217 | type = DoLookupResolvedType(type_idx, dex_cache, class_loader); |
| 218 | } |
| 219 | return type; |
| 220 | } |
| 221 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 222 | template <bool kThrowOnError, typename ClassGetter> |
| 223 | inline bool ClassLinker::CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache, |
| 224 | InvokeType type, |
| 225 | ClassGetter class_getter) { |
| 226 | switch (type) { |
| 227 | case kStatic: |
| 228 | case kSuper: |
| 229 | break; |
| 230 | case kInterface: { |
| 231 | // We have to check whether the method id really belongs to an interface (dex static bytecode |
| 232 | // constraints A15, A16). Otherwise you must not invoke-interface on it. |
| 233 | ObjPtr<mirror::Class> klass = class_getter(); |
| 234 | if (UNLIKELY(!klass->IsInterface())) { |
| 235 | if (kThrowOnError) { |
| 236 | ThrowIncompatibleClassChangeError(klass, |
| 237 | "Found class %s, but interface was expected", |
| 238 | klass->PrettyDescriptor().c_str()); |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | case kDirect: |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 245 | if (dex_cache->GetDexFile()->SupportsDefaultMethods()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | FALLTHROUGH_INTENDED; |
| 249 | case kVirtual: { |
| 250 | // Similarly, invoke-virtual (and invoke-direct without default methods) must reference |
| 251 | // a non-interface class (dex static bytecode constraint A24, A25). |
| 252 | ObjPtr<mirror::Class> klass = class_getter(); |
| 253 | if (UNLIKELY(klass->IsInterface())) { |
| 254 | if (kThrowOnError) { |
| 255 | ThrowIncompatibleClassChangeError(klass, |
| 256 | "Found interface %s, but class was expected", |
| 257 | klass->PrettyDescriptor().c_str()); |
| 258 | } |
| 259 | return true; |
| 260 | } |
| 261 | break; |
| 262 | } |
| 263 | default: |
| 264 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
| 265 | UNREACHABLE(); |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | template <bool kThrow> |
| 271 | inline bool ClassLinker::CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache, |
| 272 | InvokeType type, |
| 273 | uint32_t method_idx, |
| 274 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 275 | return CheckInvokeClassMismatch<kThrow>( |
| 276 | dex_cache, |
| 277 | type, |
| 278 | [this, dex_cache, method_idx, class_loader]() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 279 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 280 | ObjPtr<mirror::Class> klass = |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 281 | LookupResolvedType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 282 | DCHECK(klass != nullptr); |
| 283 | return klass; |
| 284 | }); |
| 285 | } |
| 286 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 287 | inline ArtMethod* ClassLinker::LookupResolvedMethod(uint32_t method_idx, |
| 288 | ObjPtr<mirror::DexCache> dex_cache, |
| 289 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 290 | PointerSize pointer_size = image_pointer_size_; |
| 291 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size); |
| 292 | if (resolved == nullptr) { |
| 293 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 294 | const dex::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 295 | ObjPtr<mirror::Class> klass = LookupResolvedType(method_id.class_idx_, dex_cache, class_loader); |
| 296 | if (klass != nullptr) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 297 | resolved = FindResolvedMethod(klass, dex_cache, class_loader, method_idx); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | return resolved; |
| 301 | } |
| 302 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 303 | template <InvokeType type, ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 304 | inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 305 | DCHECK(referrer != nullptr); |
| 306 | // Note: The referrer can be a Proxy constructor. In that case, we need to do the |
| 307 | // lookup in the context of the original method from where it steals the code. |
| 308 | // However, we delay the GetInterfaceMethodIfProxy() until needed. |
| 309 | DCHECK(!referrer->IsProxyMethod() || referrer->IsConstructor()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 310 | // We do not need the read barrier for getting the DexCache for the initial resolved method |
| 311 | // lookup as both from-space and to-space copies point to the same native resolved methods array. |
Vladimir Marko | 5122e6b | 2017-08-17 16:10:09 +0100 | [diff] [blame] | 312 | ArtMethod* resolved_method = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedMethod( |
| 313 | method_idx, image_pointer_size_); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 314 | if (resolved_method == nullptr) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 315 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 316 | } |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 317 | DCHECK(!resolved_method->IsRuntimeMethod()); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 318 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE) { |
| 319 | referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 320 | // Check if the invoke type matches the class type. |
| 321 | ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache(); |
| 322 | ObjPtr<mirror::ClassLoader> class_loader = referrer->GetClassLoader(); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 323 | if (CheckInvokeClassMismatch</* kThrow= */ false>(dex_cache, type, method_idx, class_loader)) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 324 | return nullptr; |
| 325 | } |
| 326 | // Check access. |
| 327 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 328 | if (!referring_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(), |
| 329 | resolved_method, |
| 330 | dex_cache, |
| 331 | method_idx)) { |
| 332 | return nullptr; |
| 333 | } |
| 334 | // Check if the invoke type matches the method type. |
| 335 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
| 336 | return nullptr; |
| 337 | } |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 338 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 339 | return resolved_method; |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 342 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 343 | inline ArtMethod* ClassLinker::ResolveMethod(Thread* self, |
| 344 | uint32_t method_idx, |
| 345 | ArtMethod* referrer, |
| 346 | InvokeType type) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 347 | DCHECK(referrer != nullptr); |
| 348 | // Note: The referrer can be a Proxy constructor. In that case, we need to do the |
| 349 | // lookup in the context of the original method from where it steals the code. |
| 350 | // However, we delay the GetInterfaceMethodIfProxy() until needed. |
| 351 | DCHECK(!referrer->IsProxyMethod() || referrer->IsConstructor()); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 352 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 353 | // We do not need the read barrier for getting the DexCache for the initial resolved method |
| 354 | // lookup as both from-space and to-space copies point to the same native resolved methods array. |
Vladimir Marko | 5122e6b | 2017-08-17 16:10:09 +0100 | [diff] [blame] | 355 | ArtMethod* resolved_method = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedMethod( |
| 356 | method_idx, image_pointer_size_); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 357 | DCHECK(resolved_method == nullptr || !resolved_method->IsRuntimeMethod()); |
| 358 | if (UNLIKELY(resolved_method == nullptr)) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 359 | referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 360 | ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 361 | StackHandleScope<2> hs(self); |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 362 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(referrer->GetDexCache())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 363 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 364 | resolved_method = ResolveMethod<kResolveMode>(method_idx, |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 365 | h_dex_cache, |
| 366 | h_class_loader, |
| 367 | referrer, |
| 368 | type); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 369 | } else if (kResolveMode == ResolveMode::kCheckICCEAndIAE) { |
| 370 | referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 371 | // Check if the invoke type matches the class type. |
| 372 | ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache(); |
| 373 | ObjPtr<mirror::ClassLoader> class_loader = referrer->GetClassLoader(); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 374 | if (CheckInvokeClassMismatch</* kThrow= */ true>(dex_cache, type, method_idx, class_loader)) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 375 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 376 | return nullptr; |
| 377 | } |
| 378 | // Check access. |
| 379 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 380 | if (!referring_class->CheckResolvedMethodAccess(resolved_method->GetDeclaringClass(), |
| 381 | resolved_method, |
| 382 | dex_cache, |
| 383 | method_idx, |
| 384 | type)) { |
| 385 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 386 | return nullptr; |
| 387 | } |
| 388 | // Check if the invoke type matches the method type. |
| 389 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
| 390 | ThrowIncompatibleClassChangeError(type, |
| 391 | resolved_method->GetInvokeType(), |
| 392 | resolved_method, |
| 393 | referrer); |
| 394 | return nullptr; |
| 395 | } |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 396 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 397 | // Note: We cannot check here to see whether we added the method to the cache. It |
| 398 | // 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] | 399 | return resolved_method; |
| 400 | } |
| 401 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 402 | inline ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 403 | ArtMethod* referrer, |
| 404 | bool is_static) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 405 | // We do not need the read barrier for getting the DexCache for the initial resolved field |
| 406 | // lookup as both from-space and to-space copies point to the same native resolved fields array. |
| 407 | ArtField* field = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedField( |
| 408 | field_idx, image_pointer_size_); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 409 | if (field == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 410 | ObjPtr<mirror::ClassLoader> class_loader = referrer->GetDeclaringClass()->GetClassLoader(); |
| 411 | field = LookupResolvedField(field_idx, referrer->GetDexCache(), class_loader, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 412 | } |
| 413 | return field; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 416 | inline ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
| 417 | ArtMethod* referrer, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 418 | bool is_static) { |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 419 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 420 | // We do not need the read barrier for getting the DexCache for the initial resolved field |
| 421 | // lookup as both from-space and to-space copies point to the same native resolved fields array. |
| 422 | ArtField* resolved_field = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedField( |
| 423 | field_idx, image_pointer_size_); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 424 | if (UNLIKELY(resolved_field == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 425 | StackHandleScope<2> hs(Thread::Current()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 426 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 427 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 428 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referring_class->GetClassLoader())); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 429 | resolved_field = ResolveField(field_idx, dex_cache, class_loader, is_static); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 430 | // Note: We cannot check here to see whether we added the field to the cache. The type |
| 431 | // 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] | 432 | } |
| 433 | return resolved_field; |
| 434 | } |
| 435 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 436 | template <class Visitor> |
| 437 | inline void ClassLinker::VisitClassTables(const Visitor& visitor) { |
| 438 | Thread* const self = Thread::Current(); |
| 439 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 440 | for (const ClassLoaderData& data : class_loaders_) { |
| 441 | if (data.class_table != nullptr) { |
| 442 | visitor(data.class_table); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 447 | template <ReadBarrierOption kReadBarrierOption> |
| 448 | inline ObjPtr<mirror::ObjectArray<mirror::Class>> ClassLinker::GetClassRoots() { |
| 449 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = |
| 450 | class_roots_.Read<kReadBarrierOption>(); |
| 451 | DCHECK(class_roots != nullptr); |
| 452 | return class_roots; |
| 453 | } |
| 454 | |
Alex Light | abd8f05 | 2019-12-06 10:49:17 -0800 | [diff] [blame] | 455 | template <typename Visitor> |
| 456 | void ClassLinker::VisitKnownDexFiles(Thread* self, Visitor visitor) { |
| 457 | ReaderMutexLock rmu(self, *Locks::dex_lock_); |
| 458 | std::for_each(dex_caches_.begin(), |
| 459 | dex_caches_.end(), |
| 460 | [&](DexCacheData& dcd) REQUIRES(Locks::mutator_lock_) { |
| 461 | if (dcd.IsValid()) { |
| 462 | visitor(dcd.dex_file); |
| 463 | } |
| 464 | }); |
| 465 | } |
| 466 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 467 | } // namespace art |
| 468 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 469 | #endif // ART_RUNTIME_CLASS_LINKER_INL_H_ |