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