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