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