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