blob: a30eb2301426cc6c953504f45da7699da284267e [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"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000025#include "base/sdk_version.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "class_linker-inl.h"
27#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file.h"
David Sehr8c0961f2018-01-23 16:11:38 -080029#include "dex/invoke_type.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010030#include "entrypoints/quick/callee_save_frame.h"
31#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070032#include "imt_conflict_table.h"
33#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "indirect_reference_table.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010035#include "jni/jni_internal.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070036#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070037#include "mirror/class-alloc-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038#include "mirror/class-inl.h"
39#include "mirror/object-inl.h"
40#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010041#include "nth_caller_visitor.h"
42#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010043#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070044#include "thread.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070045#include "well_known_classes.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070046
47namespace art {
48
Mathieu Chartiere401d142015-04-22 13:56:20 -070049inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
David Srbecky8cd54542018-07-15 23:58:44 +010050 const CodeInfo& code_info,
David Srbecky93bd3612018-07-02 19:30:18 +010051 const BitTableRange<InlineInfo>& inline_infos)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070052 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +010053 DCHECK(!outer_method->IsObsolete());
54
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010055 // This method is being used by artQuickResolutionTrampoline, before it sets up
56 // the passed parameters in a GC friendly way. Therefore we must never be
57 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070058 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010059
David Srbecky93bd3612018-07-02 19:30:18 +010060 {
61 InlineInfo inline_info = inline_infos.back();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000062
David Srbecky93bd3612018-07-02 19:30:18 +010063 if (inline_info.EncodesArtMethod()) {
64 return inline_info.GetArtMethod();
65 }
66
David Srbecky8cd54542018-07-15 23:58:44 +010067 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
David Srbecky93bd3612018-07-02 19:30:18 +010068 if (inline_info.GetDexPc() == static_cast<uint32_t>(-1)) {
69 // "charAt" special case. It is the only non-leaf method we inline across dex files.
70 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
71 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
72 return inlined_method;
73 }
Mathieu Chartier45bf2502016-03-31 11:07:09 -070074 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010075
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000076 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000077 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010078 ArtMethod* method = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +010079 for (InlineInfo inline_info : inline_infos) {
David Srbecky6e69e522018-06-03 12:00:14 +010080 DCHECK(!inline_info.EncodesArtMethod());
81 DCHECK_NE(inline_info.GetDexPc(), static_cast<uint32_t>(-1));
David Srbecky8cd54542018-07-15 23:58:44 +010082 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010083 ArtMethod* inlined_method = class_linker->LookupResolvedMethod(method_index,
84 method->GetDexCache(),
85 method->GetClassLoader());
86 if (UNLIKELY(inlined_method == nullptr)) {
87 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
88 << method->GetDexFile()->PrettyMethod(method_index) << " . "
89 << "This must be due to duplicate classes or playing wrongly with class loaders";
90 UNREACHABLE();
91 }
92 DCHECK(!inlined_method->IsRuntimeMethod());
93 if (UNLIKELY(inlined_method->GetDexFile() != method->GetDexFile())) {
94 // TODO: We could permit inlining within a multi-dex oat file and the boot image,
95 // even going back from boot image methods to the same oat file. However, this is
96 // not currently implemented in the compiler. Therefore crossing dex file boundary
97 // indicates that the inlined definition is not the same as the one used at runtime.
David Brazdil2bb2fbd2018-11-13 18:24:26 +000098 bool target_sdk_at_least_p =
99 IsSdkVersionSetAndAtLeast(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kP);
100 LOG(target_sdk_at_least_p ? FATAL : WARNING)
David Brazdil494b12c2018-09-07 15:03:57 +0100101 << "Inlined method resolution crossed dex file boundary: from "
102 << method->PrettyMethod()
103 << " in " << method->GetDexFile()->GetLocation() << "/"
104 << static_cast<const void*>(method->GetDexFile())
105 << " to " << inlined_method->PrettyMethod()
106 << " in " << inlined_method->GetDexFile()->GetLocation() << "/"
107 << static_cast<const void*>(inlined_method->GetDexFile()) << ". "
108 << "This must be due to duplicate classes or playing wrongly with class loaders. "
109 << "The runtime is in an unsafe state.";
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100110 }
111 method = inlined_method;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000112 }
113
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100114 return method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100115}
116
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000117ALWAYS_INLINE inline ObjPtr<mirror::Class> CheckObjectAlloc(ObjPtr<mirror::Class> klass,
118 Thread* self,
119 bool* slow_path)
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000120 REQUIRES_SHARED(Locks::mutator_lock_)
121 REQUIRES(!Roles::uninterruptible_) {
122 if (UNLIKELY(!klass->IsInstantiable())) {
123 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700124 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000125 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000127 if (UNLIKELY(klass->IsClassClass())) {
128 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
129 klass->PrettyDescriptor().c_str());
130 *slow_path = true;
131 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 }
133 if (UNLIKELY(!klass->IsInitialized())) {
134 StackHandleScope<1> hs(self);
135 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
136 // EnsureInitialized (the class initializer) might cause a GC.
137 // may cause us to suspend meaning that another thread may try to
138 // change the allocator while we are stuck in the entrypoints of
139 // an old allocator. Also, the class initialization may fail. To
140 // handle these cases we mark the slow path boolean as true so
141 // that the caller knows to check the allocator type to see if it
142 // has changed and to null-check the return value in case the
143 // initialization fails.
144 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700146 DCHECK(self->IsExceptionPending());
147 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700148 } else {
149 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150 }
151 return h_klass.Get();
152 }
153 return klass;
154}
155
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700156ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000157inline ObjPtr<mirror::Class> CheckClassInitializedForObjectAlloc(ObjPtr<mirror::Class> klass,
158 Thread* self,
159 bool* slow_path)
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000160 REQUIRES_SHARED(Locks::mutator_lock_)
161 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700162 if (UNLIKELY(!klass->IsInitialized())) {
163 StackHandleScope<1> hs(self);
164 Handle<mirror::Class> h_class(hs.NewHandle(klass));
165 // EnsureInitialized (the class initializer) might cause a GC.
166 // may cause us to suspend meaning that another thread may try to
167 // change the allocator while we are stuck in the entrypoints of
168 // an old allocator. Also, the class initialization may fail. To
169 // handle these cases we mark the slow path boolean as true so
170 // that the caller knows to check the allocator type to see if it
171 // has changed and to null-check the return value in case the
172 // initialization fails.
173 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700174 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700175 DCHECK(self->IsExceptionPending());
176 return nullptr; // Failure
177 }
178 return h_class.Get();
179 }
180 return klass;
181}
182
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000183// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
184// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
185template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700186ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000187inline ObjPtr<mirror::Object> AllocObjectFromCode(ObjPtr<mirror::Class> klass,
188 Thread* self,
189 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700190 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000191 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700192 if (UNLIKELY(slow_path)) {
193 if (klass == nullptr) {
194 return nullptr;
195 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800196 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700197 return klass->Alloc</*kInstrumented=*/true>(
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800198 self,
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000199 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700200 }
201 DCHECK(klass != nullptr);
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000202 return klass->Alloc<kInstrumented>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203}
204
205// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700206template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700207ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000208inline ObjPtr<mirror::Object> AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,
209 Thread* self,
210 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700211 DCHECK(klass != nullptr);
212 bool slow_path = false;
213 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
214 if (UNLIKELY(slow_path)) {
215 if (klass == nullptr) {
216 return nullptr;
217 }
218 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000219 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800220 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
221 // instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700222 return klass->Alloc</*kInstrumented=*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000224 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700225 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226}
227
228// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700230ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000231inline ObjPtr<mirror::Object> AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,
232 Thread* self,
233 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000235 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700236 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237}
238
239
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700241ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000242inline ObjPtr<mirror::Class> CheckArrayAlloc(dex::TypeIndex type_idx,
243 int32_t component_count,
244 ArtMethod* method,
245 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 if (UNLIKELY(component_count < 0)) {
247 ThrowNegativeArraySizeException(component_count);
248 *slow_path = true;
249 return nullptr; // Failure
250 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000251 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000253 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100254 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255 *slow_path = true;
256 if (klass == nullptr) { // Error
257 DCHECK(Thread::Current()->IsExceptionPending());
258 return nullptr; // Failure
259 }
David Sehr709b0702016-10-13 09:12:37 -0700260 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700261 }
262 if (kAccessCheck) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100263 ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264 if (UNLIKELY(!referrer->CanAccess(klass))) {
265 ThrowIllegalAccessErrorClass(referrer, klass);
266 *slow_path = true;
267 return nullptr; // Failure
268 }
269 }
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000270 return klass;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700271}
272
273// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
274// it cannot be resolved, throw an error. If it can, use it to create an array.
275// When verification/compiler hasn't been able to verify access, optionally perform an access
276// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700278ALWAYS_INLINE
Vladimir Markobcf17522018-06-01 13:14:32 +0100279inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
280 int32_t component_count,
281 ArtMethod* method,
282 Thread* self,
283 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700284 bool slow_path = false;
Vladimir Markobcf17522018-06-01 13:14:32 +0100285 ObjPtr<mirror::Class> klass =
286 CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700287 if (UNLIKELY(slow_path)) {
288 if (klass == nullptr) {
289 return nullptr;
290 }
291 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800292 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700293 return mirror::Array::Alloc</*kInstrumented=*/true>(self,
294 klass,
295 component_count,
296 klass->GetComponentSizeShift(),
297 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700298 }
299 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700300 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700301}
302
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000303template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700304ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000305inline ObjPtr<mirror::Array> AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,
306 int32_t component_count,
307 Thread* self,
308 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700309 DCHECK(klass != nullptr);
310 if (UNLIKELY(component_count < 0)) {
311 ThrowNegativeArraySizeException(component_count);
312 return nullptr; // Failure
313 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
315 // suspension.
316 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Vladimir Markobcf17522018-06-01 13:14:32 +0100317 klass->GetComponentSizeShift(), allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700318}
319
320template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800321inline ArtField* FindFieldFromCode(uint32_t field_idx,
322 ArtMethod* referrer,
323 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700324 size_t expected_size) {
David Srbeckyce32c102018-08-31 07:21:07 +0100325 constexpr bool is_primitive = (type & FindFieldFlags::PrimitiveBit) != 0;
326 constexpr bool is_set = (type & FindFieldFlags::WriteBit) != 0;
327 constexpr bool is_static = (type & FindFieldFlags::StaticBit) != 0;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700328 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800329
330 ArtField* resolved_field;
331 if (access_check) {
332 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
333 // qualifying type of a field and the resolved run-time qualifying type of a field differed
334 // in their static-ness.
335 //
336 // In particular, don't assume the dex instruction already correctly knows if the
337 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700338 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800339
340 StackHandleScope<2> hs(self);
341 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
342 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
343
Vladimir Markoe11dd502017-12-08 14:09:45 +0000344 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800345 h_dex_cache,
346 h_class_loader);
347 } else {
348 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
349 // be executing here if there was a static/non-static mismatch.
350 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
351 }
352
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700353 if (UNLIKELY(resolved_field == nullptr)) {
354 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
355 return nullptr; // Failure.
356 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700357 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700358 if (access_check) {
359 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
360 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
361 return nullptr;
362 }
Vladimir Markod93e3742018-07-18 10:58:13 +0100363 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700364 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
365 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100366 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 field_idx))) {
368 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
369 return nullptr; // Failure.
370 }
371 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
372 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
373 return nullptr; // Failure.
374 } else {
375 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
376 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700378 "Attempted read of %zd-bit %s on field '%s'",
379 expected_size * (32 / sizeof(int32_t)),
380 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700381 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700382 return nullptr; // Failure.
383 }
384 }
385 }
386 if (!is_static) {
387 // instance fields must be being accessed on an initialized class
388 return resolved_field;
389 } else {
390 // If the class is initialized we're done.
391 if (LIKELY(fields_class->IsInitialized())) {
392 return resolved_field;
393 } else {
394 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700395 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700396 // Otherwise let's ensure the class is initialized before resolving the field.
397 return resolved_field;
398 }
399 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
400 return nullptr; // Failure.
401 }
402 }
403}
404
405// Explicit template declarations of FindFieldFromCode for all field access types.
406#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700407template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700408ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700409 ArtMethod* referrer, \
410 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700411
412#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
413 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
414 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
415
416EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
417EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
418EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
419EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
420EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
421EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
422EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
423EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
424
425#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
426#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
427
David Srbeckyc853eed2018-09-13 14:33:22 +0100428// Follow virtual/interface indirections if applicable.
429// Will throw null-pointer exception the if the object is null.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700430template<InvokeType type, bool access_check>
David Srbeckyc853eed2018-09-13 14:33:22 +0100431ALWAYS_INLINE ArtMethod* FindMethodToCall(uint32_t method_idx,
432 ArtMethod* resolved_method,
433 ObjPtr<mirror::Object>* this_object,
434 ArtMethod* referrer,
435 Thread* self)
436 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700437 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
David Srbeckyc853eed2018-09-13 14:33:22 +0100438 // Null pointer check.
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700439 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
440 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
441 resolved_method->IsConstructor())) {
442 // Hack for String init:
443 //
444 // We assume that the input of String.<init> in verified code is always
445 // an unitialized reference. If it is a null constant, it must have been
446 // optimized out by the compiler. Do not throw NullPointerException.
447 } else {
448 // Maintain interpreter-like semantics where NullPointerException is thrown
449 // after potential NoSuchMethodError from class linker.
450 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
451 return nullptr; // Failure.
452 }
453 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700454 switch (type) {
455 case kStatic:
456 case kDirect:
457 return resolved_method;
458 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100459 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 uint16_t vtable_index = resolved_method->GetMethodIndex();
461 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700462 (!klass->HasVTable() ||
463 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700464 // Behavior to agree with that of the verifier.
465 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
466 resolved_method->GetName(), resolved_method->GetSignature());
467 return nullptr; // Failure.
468 }
David Sehr709b0702016-10-13 09:12:37 -0700469 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700471 }
472 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700473 // TODO This lookup is quite slow.
474 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
475 // that will actually not be what we want in some cases where there are miranda methods or
476 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
477 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700478 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700479 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700480 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800481 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800482 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000483 ObjPtr<mirror::Class> method_reference_class =
484 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700485 if (UNLIKELY(method_reference_class == nullptr)) {
486 // Bad type idx.
487 CHECK(self->IsExceptionPending());
488 return nullptr;
489 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700490 // It is not an interface. If the referring class is in the class hierarchy of the
491 // referenced class in the bytecode, we use its super class. Otherwise, we throw
492 // a NoSuchMethodError.
Vladimir Marko302f69c2017-07-25 15:27:15 +0100493 ObjPtr<mirror::Class> super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700494 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
495 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700496 }
Alex Light705ad492015-09-21 11:36:30 -0700497 uint16_t vtable_index = resolved_method->GetMethodIndex();
498 if (access_check) {
499 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700500 if (super_class == nullptr ||
501 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700502 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
503 // Behavior to agree with that of the verifier.
504 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
505 resolved_method->GetName(), resolved_method->GetSignature());
506 return nullptr; // Failure.
507 }
508 }
509 DCHECK(super_class != nullptr);
510 DCHECK(super_class->HasVTable());
511 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
512 } else {
513 // It is an interface.
514 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700515 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700516 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
517 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700518 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700519 referrer);
520 return nullptr; // Failure.
521 }
522 }
523 // TODO We can do better than this for a (compiled) fastpath.
524 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
525 resolved_method, class_linker->GetImagePointerSize());
526 // Throw an NSME if nullptr;
527 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700528 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
529 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700530 }
Alex Light705ad492015-09-21 11:36:30 -0700531 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700532 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700533 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700534 }
535 case kInterface: {
David Srbeckye36e7f22018-11-14 14:21:23 +0000536 size_t imt_index = resolved_method->GetImtIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -0700537 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100538 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
539 ArtMethod* imt_method = klass->GetImt(pointer_size)->Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000540 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700541 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 ArtMethod* method = klass->FindVirtualMethodForInterface(
543 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700544 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
545 << imt_method->PrettyMethod() << " / "
546 << ArtMethod::PrettyMethod(method) << " / "
547 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700548 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700549 return imt_method;
550 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100551 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700552 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700553 if (UNLIKELY(interface_method == nullptr)) {
554 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700555 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700556 return nullptr; // Failure.
557 }
558 return interface_method;
559 }
560 }
561 default:
562 LOG(FATAL) << "Unknown invoke type " << type;
563 return nullptr; // Failure.
564 }
565}
566
David Srbeckyc853eed2018-09-13 14:33:22 +0100567template<InvokeType type, bool access_check>
568inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
569 ObjPtr<mirror::Object>* this_object,
570 ArtMethod* referrer,
571 Thread* self) {
572 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
573 constexpr ClassLinker::ResolveMode resolve_mode =
574 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
575 : ClassLinker::ResolveMode::kNoChecks;
576 ArtMethod* resolved_method;
577 if (type == kStatic) {
578 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
579 } else {
580 StackHandleScope<1> hs(self);
581 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
582 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
583 }
584 if (UNLIKELY(resolved_method == nullptr)) {
585 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
586 return nullptr; // Failure.
587 }
588 return FindMethodToCall<type, access_check>(
589 method_idx, resolved_method, this_object, referrer, self);
590}
591
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700592// Explicit template declarations of FindMethodFromCode for all invoke types.
593#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700594 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700596 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700597 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700599#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
600 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
601 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
602
603EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
604EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
605EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
606EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
607EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
608
609#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
610#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
611
612// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700613inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
614 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700615 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700616 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800617 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700618 if (UNLIKELY(resolved_field == nullptr)) {
619 return nullptr;
620 }
621 // Check for incompatible class change.
David Srbeckyce32c102018-08-31 07:21:07 +0100622 const bool is_primitive = (type & FindFieldFlags::PrimitiveBit) != 0;
623 const bool is_set = (type & FindFieldFlags::WriteBit) != 0;
624 const bool is_static = (type & FindFieldFlags::StaticBit) != 0;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700625 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
626 // Incompatible class change.
627 return nullptr;
628 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700629 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 if (is_static) {
631 // Check class is initialized else fail so that we can contend to initialize the class with
632 // other threads that may be racing to do this.
633 if (UNLIKELY(!fields_class->IsInitialized())) {
634 return nullptr;
635 }
636 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100637 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700638 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
641 // Illegal access.
642 return nullptr;
643 }
644 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
645 resolved_field->FieldSize() != expected_size)) {
646 return nullptr;
647 }
648 return resolved_field;
649}
650
651// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100652template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700653inline ArtMethod* FindMethodFast(uint32_t method_idx,
654 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100655 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700656 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700657 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
658 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700659 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100660 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
661 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100662 constexpr ClassLinker::ResolveMode resolve_mode = access_check
663 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
664 : ClassLinker::ResolveMode::kNoChecks;
665 ClassLinker* linker = Runtime::Current()->GetClassLinker();
666 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700667 if (UNLIKELY(resolved_method == nullptr)) {
668 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700669 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700670 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700671 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
672 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700673 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 return resolved_method;
675 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700676 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000677 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000678 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000679 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700680 if (method_reference_class == nullptr) {
681 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000682 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700683 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700684 // It is not an interface. If the referring class is in the class hierarchy of the
685 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
686 // resolve the method.
687 if (!method_reference_class->IsAssignableFrom(referring_class)) {
688 return nullptr;
689 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100690 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700691 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
692 // The super class does not have the method.
693 return nullptr;
694 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700695 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700696 } else {
697 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700698 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000699 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700700 } else {
701 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700703 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700704 }
705}
706
Vladimir Marko28e012a2017-12-07 11:22:59 +0000707inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
708 ArtMethod* referrer,
709 Thread* self,
710 bool can_run_clinit,
711 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700712 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000713 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700714 if (UNLIKELY(klass == nullptr)) {
715 CHECK(self->IsExceptionPending());
716 return nullptr; // Failure - Indicate to caller to deliver exception
717 }
718 // Perform access check if necessary.
Vladimir Markod93e3742018-07-18 10:58:13 +0100719 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
721 ThrowIllegalAccessErrorClass(referring_class, klass);
722 return nullptr; // Failure - Indicate to caller to deliver exception
723 }
724 // If we're just implementing const-class, we shouldn't call <clinit>.
725 if (!can_run_clinit) {
726 return klass;
727 }
728 // If we are the <clinit> of this class, just return our storage.
729 //
730 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
731 // running.
732 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
733 return klass;
734 }
735 StackHandleScope<1> hs(self);
736 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700737 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700738 CHECK(self->IsExceptionPending());
739 return nullptr; // Failure - Indicate to caller to deliver exception
740 }
741 return h_class.Get();
742}
743
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800744inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 // Save any pending exception over monitor exit call.
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000746 ObjPtr<mirror::Throwable> saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700747 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000748 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700749 self->ClearException();
750 }
751 // Decode locked object and unlock, before popping local references.
752 self->DecodeJObject(locked)->MonitorExit(self);
753 if (UNLIKELY(self->IsExceptionPending())) {
754 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
755 << saved_exception->Dump()
756 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000757 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700758 }
759 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700760 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000761 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700762 }
763}
764
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700765template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800766inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700767 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
768 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
769 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
770 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
771 if (LIKELY(f > kMinIntAsFloat)) {
772 if (LIKELY(f < kMaxIntAsFloat)) {
773 return static_cast<INT_TYPE>(f);
774 } else {
775 return kMaxInt;
776 }
777 } else {
778 return (f != f) ? 0 : kMinInt; // f != f implies NaN
779 }
780}
781
782} // namespace art
783
784#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_