blob: 0ed26d37c0c55549e5a3de6a2c040d8d762fdea3 [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001/*
2 * Copyright (C) 2012 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
17#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19
20#include "entrypoint_utils.h"
21
Andreas Gampec1d4cd72017-04-21 13:24:52 -070022#include "art_field-inl.h"
Matthew Gharrity465ecc82016-07-19 21:32:52 +000023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "class_linker-inl.h"
26#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file.h"
David Sehr8c0961f2018-01-23 16:11:38 -080028#include "dex/invoke_type.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010029#include "entrypoints/quick/callee_save_frame.h"
30#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070031#include "imt_conflict_table.h"
32#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033#include "indirect_reference_table.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010034#include "jni/jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035#include "mirror/array.h"
36#include "mirror/class-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010039#include "nth_caller_visitor.h"
40#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010041#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070042#include "thread.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070043#include "well_known_classes.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070044
45namespace art {
46
Mathieu Chartiere401d142015-04-22 13:56:20 -070047inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070048 const MethodInfo& method_info,
David Srbecky93bd3612018-07-02 19:30:18 +010049 const BitTableRange<InlineInfo>& inline_infos)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070050 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +010051 DCHECK(!outer_method->IsObsolete());
52
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010053 // This method is being used by artQuickResolutionTrampoline, before it sets up
54 // the passed parameters in a GC friendly way. Therefore we must never be
55 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070056 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010057
David Srbecky93bd3612018-07-02 19:30:18 +010058 {
59 InlineInfo inline_info = inline_infos.back();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000060
David Srbecky93bd3612018-07-02 19:30:18 +010061 if (inline_info.EncodesArtMethod()) {
62 return inline_info.GetArtMethod();
63 }
64
65 uint32_t method_index = inline_info.GetMethodIndex(method_info);
66 if (inline_info.GetDexPc() == static_cast<uint32_t>(-1)) {
67 // "charAt" special case. It is the only non-leaf method we inline across dex files.
68 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
69 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
70 return inlined_method;
71 }
Mathieu Chartier45bf2502016-03-31 11:07:09 -070072 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010073
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000074 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000075 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010076 ArtMethod* method = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +010077 for (InlineInfo inline_info : inline_infos) {
David Srbecky6e69e522018-06-03 12:00:14 +010078 DCHECK(!inline_info.EncodesArtMethod());
79 DCHECK_NE(inline_info.GetDexPc(), static_cast<uint32_t>(-1));
David Srbecky93bd3612018-07-02 19:30:18 +010080 uint32_t method_index = inline_info.GetMethodIndex(method_info);
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010081 ArtMethod* inlined_method = class_linker->LookupResolvedMethod(method_index,
82 method->GetDexCache(),
83 method->GetClassLoader());
84 if (UNLIKELY(inlined_method == nullptr)) {
85 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
86 << method->GetDexFile()->PrettyMethod(method_index) << " . "
87 << "This must be due to duplicate classes or playing wrongly with class loaders";
88 UNREACHABLE();
89 }
90 DCHECK(!inlined_method->IsRuntimeMethod());
91 if (UNLIKELY(inlined_method->GetDexFile() != method->GetDexFile())) {
92 // TODO: We could permit inlining within a multi-dex oat file and the boot image,
93 // even going back from boot image methods to the same oat file. However, this is
94 // not currently implemented in the compiler. Therefore crossing dex file boundary
95 // indicates that the inlined definition is not the same as the one used at runtime.
96 LOG(FATAL) << "Inlined method resolution crossed dex file boundary: from "
97 << method->PrettyMethod()
98 << " in " << method->GetDexFile()->GetLocation() << "/"
99 << static_cast<const void*>(method->GetDexFile())
100 << " to " << inlined_method->PrettyMethod()
101 << " in " << inlined_method->GetDexFile()->GetLocation() << "/"
102 << static_cast<const void*>(inlined_method->GetDexFile()) << ". "
103 << "This must be due to duplicate classes or playing wrongly with class loaders";
104 UNREACHABLE();
105 }
106 method = inlined_method;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000107 }
108
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100109 return method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100110}
111
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000112ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
113 Thread* self,
114 bool* slow_path)
115 REQUIRES_SHARED(Locks::mutator_lock_)
116 REQUIRES(!Roles::uninterruptible_) {
117 if (UNLIKELY(!klass->IsInstantiable())) {
118 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000120 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700121 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000122 if (UNLIKELY(klass->IsClassClass())) {
123 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
124 klass->PrettyDescriptor().c_str());
125 *slow_path = true;
126 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127 }
128 if (UNLIKELY(!klass->IsInitialized())) {
129 StackHandleScope<1> hs(self);
130 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
131 // EnsureInitialized (the class initializer) might cause a GC.
132 // may cause us to suspend meaning that another thread may try to
133 // change the allocator while we are stuck in the entrypoints of
134 // an old allocator. Also, the class initialization may fail. To
135 // handle these cases we mark the slow path boolean as true so
136 // that the caller knows to check the allocator type to see if it
137 // has changed and to null-check the return value in case the
138 // initialization fails.
139 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700140 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700141 DCHECK(self->IsExceptionPending());
142 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700143 } else {
144 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700145 }
146 return h_klass.Get();
147 }
148 return klass;
149}
150
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700151ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800152inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
153 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000154 bool* slow_path)
155 REQUIRES_SHARED(Locks::mutator_lock_)
156 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700157 if (UNLIKELY(!klass->IsInitialized())) {
158 StackHandleScope<1> hs(self);
159 Handle<mirror::Class> h_class(hs.NewHandle(klass));
160 // EnsureInitialized (the class initializer) might cause a GC.
161 // may cause us to suspend meaning that another thread may try to
162 // change the allocator while we are stuck in the entrypoints of
163 // an old allocator. Also, the class initialization may fail. To
164 // handle these cases we mark the slow path boolean as true so
165 // that the caller knows to check the allocator type to see if it
166 // has changed and to null-check the return value in case the
167 // initialization fails.
168 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700169 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700170 DCHECK(self->IsExceptionPending());
171 return nullptr; // Failure
172 }
173 return h_class.Get();
174 }
175 return klass;
176}
177
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000178// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
179// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
180template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700181ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000182inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800183 Thread* self,
184 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700185 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000186 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187 if (UNLIKELY(slow_path)) {
188 if (klass == nullptr) {
189 return nullptr;
190 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800191 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
192 return klass->Alloc</*kInstrumented*/true>(
193 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700194 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700195 }
196 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700197 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700198}
199
200// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700201template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700202ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800203inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
204 Thread* self,
205 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700206 DCHECK(klass != nullptr);
207 bool slow_path = false;
208 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
209 if (UNLIKELY(slow_path)) {
210 if (klass == nullptr) {
211 return nullptr;
212 }
213 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000214 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800215 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
216 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700217 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700218 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000219 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700220 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221}
222
223// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700224template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700225ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800226inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
227 Thread* self,
228 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000230 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700231 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232}
233
234
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700236ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800237inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800238 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800240 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241 if (UNLIKELY(component_count < 0)) {
242 ThrowNegativeArraySizeException(component_count);
243 *slow_path = true;
244 return nullptr; // Failure
245 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000246 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000248 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100249 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250 *slow_path = true;
251 if (klass == nullptr) { // Error
252 DCHECK(Thread::Current()->IsExceptionPending());
253 return nullptr; // Failure
254 }
David Sehr709b0702016-10-13 09:12:37 -0700255 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 }
257 if (kAccessCheck) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100258 ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700259 if (UNLIKELY(!referrer->CanAccess(klass))) {
260 ThrowIllegalAccessErrorClass(referrer, klass);
261 *slow_path = true;
262 return nullptr; // Failure
263 }
264 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000265 return klass.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266}
267
268// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
269// it cannot be resolved, throw an error. If it can, use it to create an array.
270// When verification/compiler hasn't been able to verify access, optionally perform an access
271// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700272template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700273ALWAYS_INLINE
Vladimir Markobcf17522018-06-01 13:14:32 +0100274inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
275 int32_t component_count,
276 ArtMethod* method,
277 Thread* self,
278 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700279 bool slow_path = false;
Vladimir Markobcf17522018-06-01 13:14:32 +0100280 ObjPtr<mirror::Class> klass =
281 CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282 if (UNLIKELY(slow_path)) {
283 if (klass == nullptr) {
284 return nullptr;
285 }
286 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800287 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
288 return mirror::Array::Alloc</*kInstrumented*/true>(self,
289 klass,
290 component_count,
291 klass->GetComponentSizeShift(),
292 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700293 }
294 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700295 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700296}
297
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000298template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700299ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800300inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800301 int32_t component_count,
302 Thread* self,
303 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700304 DCHECK(klass != nullptr);
305 if (UNLIKELY(component_count < 0)) {
306 ThrowNegativeArraySizeException(component_count);
307 return nullptr; // Failure
308 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700309 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
310 // suspension.
311 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Vladimir Markobcf17522018-06-01 13:14:32 +0100312 klass->GetComponentSizeShift(), allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313}
314
315template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800316inline ArtField* FindFieldFromCode(uint32_t field_idx,
317 ArtMethod* referrer,
318 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700319 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320 bool is_primitive;
321 bool is_set;
322 bool is_static;
323 switch (type) {
324 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
325 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
326 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
327 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
328 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
329 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
330 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
331 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
332 default: is_primitive = true; is_set = true; is_static = true; break;
333 }
334 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800335
336 ArtField* resolved_field;
337 if (access_check) {
338 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
339 // qualifying type of a field and the resolved run-time qualifying type of a field differed
340 // in their static-ness.
341 //
342 // In particular, don't assume the dex instruction already correctly knows if the
343 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700344 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800345
346 StackHandleScope<2> hs(self);
347 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
348 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
349
Vladimir Markoe11dd502017-12-08 14:09:45 +0000350 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800351 h_dex_cache,
352 h_class_loader);
353 } else {
354 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
355 // be executing here if there was a static/non-static mismatch.
356 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
357 }
358
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700359 if (UNLIKELY(resolved_field == nullptr)) {
360 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
361 return nullptr; // Failure.
362 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700363 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700364 if (access_check) {
365 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
366 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
367 return nullptr;
368 }
Vladimir Markod93e3742018-07-18 10:58:13 +0100369 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700370 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
371 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100372 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700373 field_idx))) {
374 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
375 return nullptr; // Failure.
376 }
377 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
378 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
379 return nullptr; // Failure.
380 } else {
381 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
382 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000383 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700384 "Attempted read of %zd-bit %s on field '%s'",
385 expected_size * (32 / sizeof(int32_t)),
386 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700387 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700388 return nullptr; // Failure.
389 }
390 }
391 }
392 if (!is_static) {
393 // instance fields must be being accessed on an initialized class
394 return resolved_field;
395 } else {
396 // If the class is initialized we're done.
397 if (LIKELY(fields_class->IsInitialized())) {
398 return resolved_field;
399 } else {
400 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700401 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700402 // Otherwise let's ensure the class is initialized before resolving the field.
403 return resolved_field;
404 }
405 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
406 return nullptr; // Failure.
407 }
408 }
409}
410
411// Explicit template declarations of FindFieldFromCode for all field access types.
412#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700413template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700414ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 ArtMethod* referrer, \
416 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700417
418#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
419 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
420 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
421
422EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
423EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
424EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
425EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
427EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
428EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
429EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
430
431#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
432#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
433
434template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700435inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700436 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700437 ArtMethod* referrer,
438 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700439 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100440 constexpr ClassLinker::ResolveMode resolve_mode =
441 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
442 : ClassLinker::ResolveMode::kNoChecks;
443 ArtMethod* resolved_method;
444 if (type == kStatic) {
445 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
446 } else {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700447 StackHandleScope<1> hs(self);
Vladimir Markoba118822017-06-12 15:41:56 +0100448 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800449 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700450 }
451 if (UNLIKELY(resolved_method == nullptr)) {
452 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
453 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700454 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700455 // Next, null pointer check.
456 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
457 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
458 resolved_method->IsConstructor())) {
459 // Hack for String init:
460 //
461 // We assume that the input of String.<init> in verified code is always
462 // an unitialized reference. If it is a null constant, it must have been
463 // optimized out by the compiler. Do not throw NullPointerException.
464 } else {
465 // Maintain interpreter-like semantics where NullPointerException is thrown
466 // after potential NoSuchMethodError from class linker.
467 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
468 return nullptr; // Failure.
469 }
470 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700471 switch (type) {
472 case kStatic:
473 case kDirect:
474 return resolved_method;
475 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100476 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700477 uint16_t vtable_index = resolved_method->GetMethodIndex();
478 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700479 (!klass->HasVTable() ||
480 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700481 // Behavior to agree with that of the verifier.
482 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
483 resolved_method->GetName(), resolved_method->GetSignature());
484 return nullptr; // Failure.
485 }
David Sehr709b0702016-10-13 09:12:37 -0700486 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700488 }
489 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700490 // TODO This lookup is quite slow.
491 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
492 // that will actually not be what we want in some cases where there are miranda methods or
493 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
494 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700495 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700496 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700497 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800498 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800499 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000500 ObjPtr<mirror::Class> method_reference_class =
501 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700502 if (UNLIKELY(method_reference_class == nullptr)) {
503 // Bad type idx.
504 CHECK(self->IsExceptionPending());
505 return nullptr;
506 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700507 // It is not an interface. If the referring class is in the class hierarchy of the
508 // referenced class in the bytecode, we use its super class. Otherwise, we throw
509 // a NoSuchMethodError.
Vladimir Marko302f69c2017-07-25 15:27:15 +0100510 ObjPtr<mirror::Class> super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700511 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
512 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700513 }
Alex Light705ad492015-09-21 11:36:30 -0700514 uint16_t vtable_index = resolved_method->GetMethodIndex();
515 if (access_check) {
516 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700517 if (super_class == nullptr ||
518 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700519 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
520 // Behavior to agree with that of the verifier.
521 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
522 resolved_method->GetName(), resolved_method->GetSignature());
523 return nullptr; // Failure.
524 }
525 }
526 DCHECK(super_class != nullptr);
527 DCHECK(super_class->HasVTable());
528 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
529 } else {
530 // It is an interface.
531 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700532 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700533 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
534 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700535 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700536 referrer);
537 return nullptr; // Failure.
538 }
539 }
540 // TODO We can do better than this for a (compiled) fastpath.
541 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
542 resolved_method, class_linker->GetImagePointerSize());
543 // Throw an NSME if nullptr;
544 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700545 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
546 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700547 }
Alex Light705ad492015-09-21 11:36:30 -0700548 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700549 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700550 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700551 }
552 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700553 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700554 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100555 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
556 ArtMethod* imt_method = klass->GetImt(pointer_size)->Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000557 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700558 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700559 ArtMethod* method = klass->FindVirtualMethodForInterface(
560 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700561 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
562 << imt_method->PrettyMethod() << " / "
563 << ArtMethod::PrettyMethod(method) << " / "
564 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700565 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700566 return imt_method;
567 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100568 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700569 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700570 if (UNLIKELY(interface_method == nullptr)) {
571 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700572 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700573 return nullptr; // Failure.
574 }
575 return interface_method;
576 }
577 }
578 default:
579 LOG(FATAL) << "Unknown invoke type " << type;
580 return nullptr; // Failure.
581 }
582}
583
584// Explicit template declarations of FindMethodFromCode for all invoke types.
585#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700586 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700587 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700588 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700589 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700591#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
592 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
593 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
594
595EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
596EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
597EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
598EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
599EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
600
601#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
602#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
603
604// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700605inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
606 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700607 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700608 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800609 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700610 if (UNLIKELY(resolved_field == nullptr)) {
611 return nullptr;
612 }
613 // Check for incompatible class change.
614 bool is_primitive;
615 bool is_set;
616 bool is_static;
617 switch (type) {
618 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
619 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
620 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
621 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
622 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
623 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
624 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
625 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
626 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700627 LOG(FATAL) << "UNREACHABLE";
628 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700629 }
630 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
631 // Incompatible class change.
632 return nullptr;
633 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700634 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700635 if (is_static) {
636 // Check class is initialized else fail so that we can contend to initialize the class with
637 // other threads that may be racing to do this.
638 if (UNLIKELY(!fields_class->IsInitialized())) {
639 return nullptr;
640 }
641 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100642 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700643 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700645 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
646 // Illegal access.
647 return nullptr;
648 }
649 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
650 resolved_field->FieldSize() != expected_size)) {
651 return nullptr;
652 }
653 return resolved_field;
654}
655
656// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100657template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700658inline ArtMethod* FindMethodFast(uint32_t method_idx,
659 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100660 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700661 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700662 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
663 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700664 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100665 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
666 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100667 constexpr ClassLinker::ResolveMode resolve_mode = access_check
668 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
669 : ClassLinker::ResolveMode::kNoChecks;
670 ClassLinker* linker = Runtime::Current()->GetClassLinker();
671 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700672 if (UNLIKELY(resolved_method == nullptr)) {
673 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700676 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
677 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700678 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700679 return resolved_method;
680 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700681 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000682 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000683 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000684 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700685 if (method_reference_class == nullptr) {
686 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000687 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700688 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700689 // It is not an interface. If the referring class is in the class hierarchy of the
690 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
691 // resolve the method.
692 if (!method_reference_class->IsAssignableFrom(referring_class)) {
693 return nullptr;
694 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100695 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700696 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
697 // The super class does not have the method.
698 return nullptr;
699 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700700 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700701 } else {
702 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700703 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000704 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700705 } else {
706 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700708 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700709 }
710}
711
Vladimir Marko28e012a2017-12-07 11:22:59 +0000712inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
713 ArtMethod* referrer,
714 Thread* self,
715 bool can_run_clinit,
716 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700717 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000718 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700719 if (UNLIKELY(klass == nullptr)) {
720 CHECK(self->IsExceptionPending());
721 return nullptr; // Failure - Indicate to caller to deliver exception
722 }
723 // Perform access check if necessary.
Vladimir Markod93e3742018-07-18 10:58:13 +0100724 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700725 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
726 ThrowIllegalAccessErrorClass(referring_class, klass);
727 return nullptr; // Failure - Indicate to caller to deliver exception
728 }
729 // If we're just implementing const-class, we shouldn't call <clinit>.
730 if (!can_run_clinit) {
731 return klass;
732 }
733 // If we are the <clinit> of this class, just return our storage.
734 //
735 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
736 // running.
737 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
738 return klass;
739 }
740 StackHandleScope<1> hs(self);
741 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700742 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700743 CHECK(self->IsExceptionPending());
744 return nullptr; // Failure - Indicate to caller to deliver exception
745 }
746 return h_class.Get();
747}
748
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800749inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700750 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700751 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700752 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000753 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700754 self->ClearException();
755 }
756 // Decode locked object and unlock, before popping local references.
757 self->DecodeJObject(locked)->MonitorExit(self);
758 if (UNLIKELY(self->IsExceptionPending())) {
759 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
760 << saved_exception->Dump()
761 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000762 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700763 }
764 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700765 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000766 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700767 }
768}
769
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700770template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800771inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700772 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
773 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
774 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
775 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
776 if (LIKELY(f > kMinIntAsFloat)) {
777 if (LIKELY(f < kMaxIntAsFloat)) {
778 return static_cast<INT_TYPE>(f);
779 } else {
780 return kMaxInt;
781 }
782 } else {
783 return (f != f) ? 0 : kMinInt; // f != f implies NaN
784 }
785}
786
787} // namespace art
788
789#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_