blob: 2732de56f7606c41084fa50fbe9e1bee9fab6b20 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_CLASS_LINKER_INL_H_
18#define ART_RUNTIME_CLASS_LINKER_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Vladimir Marko09c5ca42018-05-31 15:15:31 +010020#include <atomic>
21
Alex Lightabd8f052019-12-06 10:49:17 -080022#include "android-base/thread_annotations.h"
Vladimir Marko09c5ca42018-05-31 15:15:31 +010023#include "art_field-inl.h"
24#include "art_method-inl.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080025#include "base/mutex.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker.h"
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080027#include "dex/dex_file.h"
28#include "dex/dex_file_structs.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "gc_root-inl.h"
30#include "handle_scope-inl.h"
Alex Lightabd8f052019-12-06 10:49:17 -080031#include "jni/jni_internal.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070032#include "mirror/class_loader.h"
Mathieu Chartierbc56fc32014-06-03 15:37:03 -070033#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/iftable.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070035#include "mirror/object_array-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "obj_ptr-inl.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070037#include "scoped_thread_state_change-inl.h"
Alex Lightabd8f052019-12-06 10:49:17 -080038#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039
40namespace art {
41
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010042inline ObjPtr<mirror::Class> ClassLinker::FindArrayClass(Thread* self,
Vladimir Markobcf17522018-06-01 13:14:32 +010043 ObjPtr<mirror::Class> element_class) {
Ian Rogers98379392014-02-24 16:53:16 -080044 for (size_t i = 0; i < kFindArrayCacheSize; ++i) {
Ian Rogersa55cf412014-02-27 00:31:26 -080045 // Read the cached array class once to avoid races with other threads setting it.
Mathieu Chartier28357fa2016-10-18 16:27:40 -070046 ObjPtr<mirror::Class> array_class = find_array_class_cache_[i].Read();
Vladimir Markobcf17522018-06-01 13:14:32 +010047 if (array_class != nullptr && array_class->GetComponentType() == element_class) {
48 return array_class;
Ian Rogers98379392014-02-24 16:53:16 -080049 }
50 }
Ian Rogers1ff3c982014-08-12 02:30:58 -070051 std::string descriptor = "[";
52 std::string temp;
Vladimir Markobcf17522018-06-01 13:14:32 +010053 descriptor += element_class->GetDescriptor(&temp);
54 StackHandleScope<1> hs(Thread::Current());
55 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(element_class->GetClassLoader()));
Mathieu Chartier28357fa2016-10-18 16:27:40 -070056 ObjPtr<mirror::Class> array_class = FindClass(self, descriptor.c_str(), class_loader);
Nicolas Geoffray9638b642015-06-23 18:16:46 +010057 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 Markobcf17522018-06-01 13:14:32 +010066 return array_class;
Ian Rogers98379392014-02-24 16:53:16 -080067}
68
Vladimir Marko18090d12018-06-01 16:53:12 +010069inline ObjPtr<mirror::String> ClassLinker::ResolveString(dex::StringIndex string_idx,
70 ArtField* referrer) {
71 Thread::PoisonObjectPointersIfDebug();
72 DCHECK(!Thread::Current()->IsExceptionPending());
73 // We do not need the read barrier for getting the DexCache for the initial resolved type
74 // lookup as both from-space and to-space copies point to the same native resolved types array.
75 ObjPtr<mirror::String> resolved =
76 referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedString(string_idx);
77 if (resolved == nullptr) {
78 resolved = DoResolveString(string_idx, referrer->GetDexCache());
79 }
80 return resolved;
81}
82
83inline ObjPtr<mirror::String> ClassLinker::ResolveString(dex::StringIndex string_idx,
84 ArtMethod* referrer) {
85 Thread::PoisonObjectPointersIfDebug();
86 DCHECK(!Thread::Current()->IsExceptionPending());
87 // We do not need the read barrier for getting the DexCache for the initial resolved type
88 // lookup as both from-space and to-space copies point to the same native resolved types array.
89 ObjPtr<mirror::String> resolved =
90 referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedString(string_idx);
91 if (resolved == nullptr) {
92 resolved = DoResolveString(string_idx, referrer->GetDexCache());
93 }
94 return resolved;
95}
96
97inline ObjPtr<mirror::String> ClassLinker::ResolveString(dex::StringIndex string_idx,
98 Handle<mirror::DexCache> dex_cache) {
99 Thread::PoisonObjectPointersIfDebug();
100 DCHECK(!Thread::Current()->IsExceptionPending());
101 ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx);
102 if (resolved == nullptr) {
103 resolved = DoResolveString(string_idx, dex_cache);
104 }
105 return resolved;
106}
107
108inline ObjPtr<mirror::String> ClassLinker::LookupString(dex::StringIndex string_idx,
109 ObjPtr<mirror::DexCache> dex_cache) {
110 ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx);
111 if (resolved == nullptr) {
112 resolved = DoLookupString(string_idx, dex_cache);
113 }
114 return resolved;
115}
116
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000117inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx,
118 ObjPtr<mirror::Class> referrer) {
119 if (kObjPtrPoisoning) {
120 StackHandleScope<1> hs(Thread::Current());
121 HandleWrapperObjPtr<mirror::Class> referrer_wrapper = hs.NewHandleWrapper(&referrer);
122 Thread::Current()->PoisonObjectPointers();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000123 }
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100124 DCHECK(!Thread::Current()->IsExceptionPending());
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000125 // We do not need the read barrier for getting the DexCache for the initial resolved type
126 // lookup as both from-space and to-space copies point to the same native resolved types array.
127 ObjPtr<mirror::Class> resolved_type =
128 referrer->GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetResolvedType(type_idx);
129 if (resolved_type == nullptr) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100130 resolved_type = DoResolveType(type_idx, referrer);
131 }
132 return resolved_type;
133}
134
135inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx,
136 ArtField* referrer) {
137 Thread::PoisonObjectPointersIfDebug();
138 DCHECK(!Thread::Current()->IsExceptionPending());
139 // We do not need the read barrier for getting the DexCache for the initial resolved type
140 // lookup as both from-space and to-space copies point to the same native resolved types array.
141 ObjPtr<mirror::Class> resolved_type =
142 referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx);
143 if (UNLIKELY(resolved_type == nullptr)) {
Andreas Gampe4835d212018-11-21 14:55:10 -0800144 resolved_type = DoResolveType(type_idx, referrer);
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000145 }
146 return resolved_type;
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000147}
148
Vladimir Marko28e012a2017-12-07 11:22:59 +0000149inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx,
150 ArtMethod* referrer) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700151 Thread::PoisonObjectPointersIfDebug();
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100152 DCHECK(!Thread::Current()->IsExceptionPending());
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000153 // We do not need the read barrier for getting the DexCache for the initial resolved type
154 // lookup as both from-space and to-space copies point to the same native resolved types array.
155 ObjPtr<mirror::Class> resolved_type =
156 referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 if (UNLIKELY(resolved_type == nullptr)) {
Andreas Gampe4835d212018-11-21 14:55:10 -0800158 resolved_type = DoResolveType(type_idx, referrer);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000160 return resolved_type;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161}
162
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000163inline ObjPtr<mirror::Class> ClassLinker::ResolveType(dex::TypeIndex type_idx,
164 Handle<mirror::DexCache> dex_cache,
165 Handle<mirror::ClassLoader> class_loader) {
166 DCHECK(dex_cache != nullptr);
167 Thread::PoisonObjectPointersIfDebug();
168 ObjPtr<mirror::Class> resolved = dex_cache->GetResolvedType(type_idx);
169 if (resolved == nullptr) {
170 resolved = DoResolveType(type_idx, dex_cache, class_loader);
171 }
172 return resolved;
173}
174
175inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(dex::TypeIndex type_idx,
176 ObjPtr<mirror::Class> referrer) {
177 // We do not need the read barrier for getting the DexCache for the initial resolved type
178 // lookup as both from-space and to-space copies point to the same native resolved types array.
179 ObjPtr<mirror::Class> type =
180 referrer->GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetResolvedType(type_idx);
181 if (type == nullptr) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100182 type = DoLookupResolvedType(type_idx, referrer);
183 }
184 return type;
185}
186
187inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(dex::TypeIndex type_idx,
188 ArtField* referrer) {
189 // We do not need the read barrier for getting the DexCache for the initial resolved type
190 // lookup as both from-space and to-space copies point to the same native resolved types array.
191 ObjPtr<mirror::Class> type =
192 referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx);
193 if (type == nullptr) {
194 type = DoLookupResolvedType(type_idx, referrer->GetDeclaringClass());
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000195 }
196 return type;
197}
198
199inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(dex::TypeIndex type_idx,
200 ArtMethod* referrer) {
201 // We do not need the read barrier for getting the DexCache for the initial resolved type
202 // lookup as both from-space and to-space copies point to the same native resolved types array.
203 ObjPtr<mirror::Class> type =
204 referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedType(type_idx);
205 if (type == nullptr) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100206 type = DoLookupResolvedType(type_idx, referrer->GetDeclaringClass());
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000207 }
208 return type;
209}
210
211inline ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(
212 dex::TypeIndex type_idx,
213 ObjPtr<mirror::DexCache> dex_cache,
214 ObjPtr<mirror::ClassLoader> class_loader) {
215 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
216 if (type == nullptr) {
217 type = DoLookupResolvedType(type_idx, dex_cache, class_loader);
218 }
219 return type;
220}
221
Vladimir Markoba118822017-06-12 15:41:56 +0100222template <bool kThrowOnError, typename ClassGetter>
223inline bool ClassLinker::CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache,
224 InvokeType type,
225 ClassGetter class_getter) {
226 switch (type) {
227 case kStatic:
228 case kSuper:
229 break;
230 case kInterface: {
231 // We have to check whether the method id really belongs to an interface (dex static bytecode
232 // constraints A15, A16). Otherwise you must not invoke-interface on it.
233 ObjPtr<mirror::Class> klass = class_getter();
234 if (UNLIKELY(!klass->IsInterface())) {
235 if (kThrowOnError) {
236 ThrowIncompatibleClassChangeError(klass,
237 "Found class %s, but interface was expected",
238 klass->PrettyDescriptor().c_str());
239 }
240 return true;
241 }
242 break;
243 }
244 case kDirect:
Mathieu Chartierf6e31472017-12-28 13:32:08 -0800245 if (dex_cache->GetDexFile()->SupportsDefaultMethods()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100246 break;
247 }
248 FALLTHROUGH_INTENDED;
249 case kVirtual: {
250 // Similarly, invoke-virtual (and invoke-direct without default methods) must reference
251 // a non-interface class (dex static bytecode constraint A24, A25).
252 ObjPtr<mirror::Class> klass = class_getter();
253 if (UNLIKELY(klass->IsInterface())) {
254 if (kThrowOnError) {
255 ThrowIncompatibleClassChangeError(klass,
256 "Found interface %s, but class was expected",
257 klass->PrettyDescriptor().c_str());
258 }
259 return true;
260 }
261 break;
262 }
263 default:
264 LOG(FATAL) << "Unreachable - invocation type: " << type;
265 UNREACHABLE();
266 }
267 return false;
268}
269
270template <bool kThrow>
271inline bool ClassLinker::CheckInvokeClassMismatch(ObjPtr<mirror::DexCache> dex_cache,
272 InvokeType type,
273 uint32_t method_idx,
274 ObjPtr<mirror::ClassLoader> class_loader) {
275 return CheckInvokeClassMismatch<kThrow>(
276 dex_cache,
277 type,
278 [this, dex_cache, method_idx, class_loader]() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800279 const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +0100280 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000281 LookupResolvedType(method_id.class_idx_, dex_cache, class_loader);
Vladimir Markoba118822017-06-12 15:41:56 +0100282 DCHECK(klass != nullptr);
283 return klass;
284 });
285}
286
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100287inline ArtMethod* ClassLinker::LookupResolvedMethod(uint32_t method_idx,
288 ObjPtr<mirror::DexCache> dex_cache,
289 ObjPtr<mirror::ClassLoader> class_loader) {
290 PointerSize pointer_size = image_pointer_size_;
291 ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size);
292 if (resolved == nullptr) {
293 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800294 const dex::MethodId& method_id = dex_file.GetMethodId(method_idx);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100295 ObjPtr<mirror::Class> klass = LookupResolvedType(method_id.class_idx_, dex_cache, class_loader);
296 if (klass != nullptr) {
Nicolas Geoffrayea179f42018-02-08 22:30:18 +0000297 resolved = FindResolvedMethod(klass, dex_cache, class_loader, method_idx);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100298 }
299 }
300 return resolved;
301}
302
Vladimir Markoba118822017-06-12 15:41:56 +0100303template <InvokeType type, ClassLinker::ResolveMode kResolveMode>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304inline ArtMethod* ClassLinker::GetResolvedMethod(uint32_t method_idx, ArtMethod* referrer) {
Vladimir Markoba118822017-06-12 15:41:56 +0100305 DCHECK(referrer != nullptr);
306 // Note: The referrer can be a Proxy constructor. In that case, we need to do the
307 // lookup in the context of the original method from where it steals the code.
308 // However, we delay the GetInterfaceMethodIfProxy() until needed.
309 DCHECK(!referrer->IsProxyMethod() || referrer->IsConstructor());
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000310 // We do not need the read barrier for getting the DexCache for the initial resolved method
311 // lookup as both from-space and to-space copies point to the same native resolved methods array.
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100312 ArtMethod* resolved_method = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedMethod(
313 method_idx, image_pointer_size_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100314 if (resolved_method == nullptr) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700315 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100317 DCHECK(!resolved_method->IsRuntimeMethod());
Vladimir Markoba118822017-06-12 15:41:56 +0100318 if (kResolveMode == ResolveMode::kCheckICCEAndIAE) {
319 referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_);
320 // Check if the invoke type matches the class type.
321 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
322 ObjPtr<mirror::ClassLoader> class_loader = referrer->GetClassLoader();
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700323 if (CheckInvokeClassMismatch</* kThrow= */ false>(dex_cache, type, method_idx, class_loader)) {
Vladimir Markoba118822017-06-12 15:41:56 +0100324 return nullptr;
325 }
326 // Check access.
327 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
328 if (!referring_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
329 resolved_method,
330 dex_cache,
331 method_idx)) {
332 return nullptr;
333 }
334 // Check if the invoke type matches the method type.
335 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
336 return nullptr;
337 }
Alex Lightfedd91d2016-01-07 14:49:16 -0800338 }
Vladimir Markoba118822017-06-12 15:41:56 +0100339 return resolved_method;
Alex Lightfedd91d2016-01-07 14:49:16 -0800340}
341
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800342template <ClassLinker::ResolveMode kResolveMode>
Mathieu Chartierc77f3ab2015-09-03 19:41:50 -0700343inline ArtMethod* ClassLinker::ResolveMethod(Thread* self,
344 uint32_t method_idx,
345 ArtMethod* referrer,
346 InvokeType type) {
Vladimir Markoba118822017-06-12 15:41:56 +0100347 DCHECK(referrer != nullptr);
348 // Note: The referrer can be a Proxy constructor. In that case, we need to do the
349 // lookup in the context of the original method from where it steals the code.
350 // However, we delay the GetInterfaceMethodIfProxy() until needed.
351 DCHECK(!referrer->IsProxyMethod() || referrer->IsConstructor());
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700352 Thread::PoisonObjectPointersIfDebug();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000353 // We do not need the read barrier for getting the DexCache for the initial resolved method
354 // lookup as both from-space and to-space copies point to the same native resolved methods array.
Vladimir Marko5122e6b2017-08-17 16:10:09 +0100355 ArtMethod* resolved_method = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedMethod(
356 method_idx, image_pointer_size_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100357 DCHECK(resolved_method == nullptr || !resolved_method->IsRuntimeMethod());
358 if (UNLIKELY(resolved_method == nullptr)) {
Vladimir Markoba118822017-06-12 15:41:56 +0100359 referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_);
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700360 ObjPtr<mirror::Class> declaring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700361 StackHandleScope<2> hs(self);
Alex Light4ba388a2017-01-27 10:26:49 -0800362 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(referrer->GetDexCache()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Vladimir Marko89011192017-12-11 13:45:05 +0000364 resolved_method = ResolveMethod<kResolveMode>(method_idx,
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800365 h_dex_cache,
366 h_class_loader,
367 referrer,
368 type);
Vladimir Markoba118822017-06-12 15:41:56 +0100369 } else if (kResolveMode == ResolveMode::kCheckICCEAndIAE) {
370 referrer = referrer->GetInterfaceMethodIfProxy(image_pointer_size_);
371 // Check if the invoke type matches the class type.
372 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
373 ObjPtr<mirror::ClassLoader> class_loader = referrer->GetClassLoader();
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700374 if (CheckInvokeClassMismatch</* kThrow= */ true>(dex_cache, type, method_idx, class_loader)) {
Vladimir Markoba118822017-06-12 15:41:56 +0100375 DCHECK(Thread::Current()->IsExceptionPending());
376 return nullptr;
377 }
378 // Check access.
379 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
380 if (!referring_class->CheckResolvedMethodAccess(resolved_method->GetDeclaringClass(),
381 resolved_method,
382 dex_cache,
383 method_idx,
384 type)) {
385 DCHECK(Thread::Current()->IsExceptionPending());
386 return nullptr;
387 }
388 // Check if the invoke type matches the method type.
389 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
390 ThrowIncompatibleClassChangeError(type,
391 resolved_method->GetInvokeType(),
392 resolved_method,
393 referrer);
394 return nullptr;
395 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700396 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700397 // Note: We cannot check here to see whether we added the method to the cache. It
398 // might be an erroneous class, which results in it being hidden from us.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700399 return resolved_method;
400}
401
Vladimir Markof44d36c2017-03-14 14:18:46 +0000402inline ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx,
403 ArtMethod* referrer,
404 bool is_static) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000405 // We do not need the read barrier for getting the DexCache for the initial resolved field
406 // lookup as both from-space and to-space copies point to the same native resolved fields array.
407 ArtField* field = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedField(
408 field_idx, image_pointer_size_);
Vladimir Markof44d36c2017-03-14 14:18:46 +0000409 if (field == nullptr) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000410 ObjPtr<mirror::ClassLoader> class_loader = referrer->GetDeclaringClass()->GetClassLoader();
411 field = LookupResolvedField(field_idx, referrer->GetDexCache(), class_loader, is_static);
Vladimir Markof44d36c2017-03-14 14:18:46 +0000412 }
413 return field;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700414}
415
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700416inline ArtField* ClassLinker::ResolveField(uint32_t field_idx,
417 ArtMethod* referrer,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418 bool is_static) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -0700419 Thread::PoisonObjectPointersIfDebug();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000420 // We do not need the read barrier for getting the DexCache for the initial resolved field
421 // lookup as both from-space and to-space copies point to the same native resolved fields array.
422 ArtField* resolved_field = referrer->GetDexCache<kWithoutReadBarrier>()->GetResolvedField(
423 field_idx, image_pointer_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700424 if (UNLIKELY(resolved_field == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700425 StackHandleScope<2> hs(Thread::Current());
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000426 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Alex Lightdba61482016-12-21 08:20:29 -0800427 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000428 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referring_class->GetClassLoader()));
Vladimir Markoe11dd502017-12-08 14:09:45 +0000429 resolved_field = ResolveField(field_idx, dex_cache, class_loader, is_static);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700430 // Note: We cannot check here to see whether we added the field to the cache. The type
431 // might be an erroneous class, which results in it being hidden from us.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 }
433 return resolved_field;
434}
435
Mathieu Chartier72041a02017-07-14 18:23:25 -0700436template <class Visitor>
437inline void ClassLinker::VisitClassTables(const Visitor& visitor) {
438 Thread* const self = Thread::Current();
439 WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
440 for (const ClassLoaderData& data : class_loaders_) {
441 if (data.class_table != nullptr) {
442 visitor(data.class_table);
443 }
444 }
445}
446
Andreas Gampe88dbad32018-06-26 19:54:12 -0700447template <ReadBarrierOption kReadBarrierOption>
448inline ObjPtr<mirror::ObjectArray<mirror::Class>> ClassLinker::GetClassRoots() {
449 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
450 class_roots_.Read<kReadBarrierOption>();
451 DCHECK(class_roots != nullptr);
452 return class_roots;
453}
454
Alex Lightabd8f052019-12-06 10:49:17 -0800455template <typename Visitor>
456void ClassLinker::VisitKnownDexFiles(Thread* self, Visitor visitor) {
457 ReaderMutexLock rmu(self, *Locks::dex_lock_);
458 std::for_each(dex_caches_.begin(),
459 dex_caches_.end(),
460 [&](DexCacheData& dcd) REQUIRES(Locks::mutator_lock_) {
461 if (dcd.IsValid()) {
462 visitor(dcd.dex_file);
463 }
464 });
465}
466
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467} // namespace art
468
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700469#endif // ART_RUNTIME_CLASS_LINKER_INL_H_