blob: 0b005e0851fb217b69fdf769f9865b111d24534d [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"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070035#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070036#include "mirror/class-alloc-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037#include "mirror/class-inl.h"
38#include "mirror/object-inl.h"
39#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010040#include "nth_caller_visitor.h"
41#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010042#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070043#include "thread.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070044#include "well_known_classes.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070045
46namespace art {
47
Mathieu Chartiere401d142015-04-22 13:56:20 -070048inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
David Srbecky8cd54542018-07-15 23:58:44 +010049 const CodeInfo& code_info,
David Srbecky93bd3612018-07-02 19:30:18 +010050 const BitTableRange<InlineInfo>& inline_infos)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +010052 DCHECK(!outer_method->IsObsolete());
53
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010054 // This method is being used by artQuickResolutionTrampoline, before it sets up
55 // the passed parameters in a GC friendly way. Therefore we must never be
56 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070057 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010058
David Srbecky93bd3612018-07-02 19:30:18 +010059 {
60 InlineInfo inline_info = inline_infos.back();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000061
David Srbecky93bd3612018-07-02 19:30:18 +010062 if (inline_info.EncodesArtMethod()) {
63 return inline_info.GetArtMethod();
64 }
65
David Srbecky8cd54542018-07-15 23:58:44 +010066 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
David Srbecky93bd3612018-07-02 19:30:18 +010067 if (inline_info.GetDexPc() == static_cast<uint32_t>(-1)) {
68 // "charAt" special case. It is the only non-leaf method we inline across dex files.
69 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
70 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
71 return inlined_method;
72 }
Mathieu Chartier45bf2502016-03-31 11:07:09 -070073 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010074
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000075 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000076 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010077 ArtMethod* method = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +010078 for (InlineInfo inline_info : inline_infos) {
David Srbecky6e69e522018-06-03 12:00:14 +010079 DCHECK(!inline_info.EncodesArtMethod());
80 DCHECK_NE(inline_info.GetDexPc(), static_cast<uint32_t>(-1));
David Srbecky8cd54542018-07-15 23:58:44 +010081 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
Vladimir Marko63a9f3e2018-04-26 09:18:10 +010082 ArtMethod* inlined_method = class_linker->LookupResolvedMethod(method_index,
83 method->GetDexCache(),
84 method->GetClassLoader());
85 if (UNLIKELY(inlined_method == nullptr)) {
86 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
87 << method->GetDexFile()->PrettyMethod(method_index) << " . "
88 << "This must be due to duplicate classes or playing wrongly with class loaders";
89 UNREACHABLE();
90 }
91 DCHECK(!inlined_method->IsRuntimeMethod());
92 if (UNLIKELY(inlined_method->GetDexFile() != method->GetDexFile())) {
93 // TODO: We could permit inlining within a multi-dex oat file and the boot image,
94 // even going back from boot image methods to the same oat file. However, this is
95 // not currently implemented in the compiler. Therefore crossing dex file boundary
96 // indicates that the inlined definition is not the same as the one used at runtime.
David Brazdil494b12c2018-09-07 15:03:57 +010097 bool target_sdk_pre_p = Runtime::Current()->GetTargetSdkVersion() < 28;
98 LOG(target_sdk_pre_p ? WARNING : FATAL)
99 << "Inlined method resolution crossed dex file boundary: from "
100 << method->PrettyMethod()
101 << " in " << method->GetDexFile()->GetLocation() << "/"
102 << static_cast<const void*>(method->GetDexFile())
103 << " to " << inlined_method->PrettyMethod()
104 << " in " << inlined_method->GetDexFile()->GetLocation() << "/"
105 << static_cast<const void*>(inlined_method->GetDexFile()) << ". "
106 << "This must be due to duplicate classes or playing wrongly with class loaders. "
107 << "The runtime is in an unsafe state.";
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100108 }
109 method = inlined_method;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000110 }
111
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100112 return method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100113}
114
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000115ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
116 Thread* self,
117 bool* slow_path)
118 REQUIRES_SHARED(Locks::mutator_lock_)
119 REQUIRES(!Roles::uninterruptible_) {
120 if (UNLIKELY(!klass->IsInstantiable())) {
121 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700122 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000123 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700124 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000125 if (UNLIKELY(klass->IsClassClass())) {
126 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
127 klass->PrettyDescriptor().c_str());
128 *slow_path = true;
129 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130 }
131 if (UNLIKELY(!klass->IsInitialized())) {
132 StackHandleScope<1> hs(self);
133 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
134 // EnsureInitialized (the class initializer) might cause a GC.
135 // may cause us to suspend meaning that another thread may try to
136 // change the allocator while we are stuck in the entrypoints of
137 // an old allocator. Also, the class initialization may fail. To
138 // handle these cases we mark the slow path boolean as true so
139 // that the caller knows to check the allocator type to see if it
140 // has changed and to null-check the return value in case the
141 // initialization fails.
142 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700143 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700144 DCHECK(self->IsExceptionPending());
145 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700146 } else {
147 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700148 }
149 return h_klass.Get();
150 }
151 return klass;
152}
153
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700154ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800155inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
156 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000157 bool* slow_path)
158 REQUIRES_SHARED(Locks::mutator_lock_)
159 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700160 if (UNLIKELY(!klass->IsInitialized())) {
161 StackHandleScope<1> hs(self);
162 Handle<mirror::Class> h_class(hs.NewHandle(klass));
163 // EnsureInitialized (the class initializer) might cause a GC.
164 // may cause us to suspend meaning that another thread may try to
165 // change the allocator while we are stuck in the entrypoints of
166 // an old allocator. Also, the class initialization may fail. To
167 // handle these cases we mark the slow path boolean as true so
168 // that the caller knows to check the allocator type to see if it
169 // has changed and to null-check the return value in case the
170 // initialization fails.
171 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700172 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700173 DCHECK(self->IsExceptionPending());
174 return nullptr; // Failure
175 }
176 return h_class.Get();
177 }
178 return klass;
179}
180
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000181// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
182// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
183template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700184ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000185inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800186 Thread* self,
187 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700188 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000189 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700190 if (UNLIKELY(slow_path)) {
191 if (klass == nullptr) {
192 return nullptr;
193 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800194 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700195 return klass->Alloc</*kInstrumented=*/true>(
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800196 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700197 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700198 }
199 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700200 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700201}
202
203// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700204template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700205ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800206inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
207 Thread* self,
208 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700209 DCHECK(klass != nullptr);
210 bool slow_path = false;
211 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
212 if (UNLIKELY(slow_path)) {
213 if (klass == nullptr) {
214 return nullptr;
215 }
216 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000217 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800218 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
219 // instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700220 return klass->Alloc</*kInstrumented=*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000222 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700223 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700224}
225
226// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700227template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700228ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800229inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
230 Thread* self,
231 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000233 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700234 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235}
236
237
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700238template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700239ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800240inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800241 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800243 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 if (UNLIKELY(component_count < 0)) {
245 ThrowNegativeArraySizeException(component_count);
246 *slow_path = true;
247 return nullptr; // Failure
248 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000249 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000251 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100252 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 *slow_path = true;
254 if (klass == nullptr) { // Error
255 DCHECK(Thread::Current()->IsExceptionPending());
256 return nullptr; // Failure
257 }
David Sehr709b0702016-10-13 09:12:37 -0700258 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700259 }
260 if (kAccessCheck) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100261 ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 if (UNLIKELY(!referrer->CanAccess(klass))) {
263 ThrowIllegalAccessErrorClass(referrer, klass);
264 *slow_path = true;
265 return nullptr; // Failure
266 }
267 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000268 return klass.Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700269}
270
271// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
272// it cannot be resolved, throw an error. If it can, use it to create an array.
273// When verification/compiler hasn't been able to verify access, optionally perform an access
274// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700275template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700276ALWAYS_INLINE
Vladimir Markobcf17522018-06-01 13:14:32 +0100277inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
278 int32_t component_count,
279 ArtMethod* method,
280 Thread* self,
281 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282 bool slow_path = false;
Vladimir Markobcf17522018-06-01 13:14:32 +0100283 ObjPtr<mirror::Class> klass =
284 CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700285 if (UNLIKELY(slow_path)) {
286 if (klass == nullptr) {
287 return nullptr;
288 }
289 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800290 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700291 return mirror::Array::Alloc</*kInstrumented=*/true>(self,
292 klass,
293 component_count,
294 klass->GetComponentSizeShift(),
295 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700296 }
297 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700298 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700299}
300
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000301template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700302ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800303inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800304 int32_t component_count,
305 Thread* self,
306 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307 DCHECK(klass != nullptr);
308 if (UNLIKELY(component_count < 0)) {
309 ThrowNegativeArraySizeException(component_count);
310 return nullptr; // Failure
311 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700312 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
313 // suspension.
314 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Vladimir Markobcf17522018-06-01 13:14:32 +0100315 klass->GetComponentSizeShift(), allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700316}
317
318template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800319inline ArtField* FindFieldFromCode(uint32_t field_idx,
320 ArtMethod* referrer,
321 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700322 size_t expected_size) {
David Srbeckyce32c102018-08-31 07:21:07 +0100323 constexpr bool is_primitive = (type & FindFieldFlags::PrimitiveBit) != 0;
324 constexpr bool is_set = (type & FindFieldFlags::WriteBit) != 0;
325 constexpr bool is_static = (type & FindFieldFlags::StaticBit) != 0;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700326 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800327
328 ArtField* resolved_field;
329 if (access_check) {
330 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
331 // qualifying type of a field and the resolved run-time qualifying type of a field differed
332 // in their static-ness.
333 //
334 // In particular, don't assume the dex instruction already correctly knows if the
335 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700336 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800337
338 StackHandleScope<2> hs(self);
339 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
340 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
341
Vladimir Markoe11dd502017-12-08 14:09:45 +0000342 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800343 h_dex_cache,
344 h_class_loader);
345 } else {
346 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
347 // be executing here if there was a static/non-static mismatch.
348 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
349 }
350
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700351 if (UNLIKELY(resolved_field == nullptr)) {
352 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
353 return nullptr; // Failure.
354 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700355 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700356 if (access_check) {
357 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
358 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
359 return nullptr;
360 }
Vladimir Markod93e3742018-07-18 10:58:13 +0100361 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700362 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
363 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100364 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 field_idx))) {
366 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
367 return nullptr; // Failure.
368 }
369 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
370 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
371 return nullptr; // Failure.
372 } else {
373 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
374 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000375 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700376 "Attempted read of %zd-bit %s on field '%s'",
377 expected_size * (32 / sizeof(int32_t)),
378 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700379 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700380 return nullptr; // Failure.
381 }
382 }
383 }
384 if (!is_static) {
385 // instance fields must be being accessed on an initialized class
386 return resolved_field;
387 } else {
388 // If the class is initialized we're done.
389 if (LIKELY(fields_class->IsInitialized())) {
390 return resolved_field;
391 } else {
392 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700393 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700394 // Otherwise let's ensure the class is initialized before resolving the field.
395 return resolved_field;
396 }
397 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
398 return nullptr; // Failure.
399 }
400 }
401}
402
403// Explicit template declarations of FindFieldFromCode for all field access types.
404#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700405template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700406ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 ArtMethod* referrer, \
408 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700409
410#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
411 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
412 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
413
414EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
415EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
416EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
417EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
418EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
419EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
420EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
421EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
422
423#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
424#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
425
David Srbeckyc853eed2018-09-13 14:33:22 +0100426// Follow virtual/interface indirections if applicable.
427// Will throw null-pointer exception the if the object is null.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700428template<InvokeType type, bool access_check>
David Srbeckyc853eed2018-09-13 14:33:22 +0100429ALWAYS_INLINE ArtMethod* FindMethodToCall(uint32_t method_idx,
430 ArtMethod* resolved_method,
431 ObjPtr<mirror::Object>* this_object,
432 ArtMethod* referrer,
433 Thread* self)
434 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700435 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
David Srbeckyc853eed2018-09-13 14:33:22 +0100436 // Null pointer check.
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700437 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
438 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
439 resolved_method->IsConstructor())) {
440 // Hack for String init:
441 //
442 // We assume that the input of String.<init> in verified code is always
443 // an unitialized reference. If it is a null constant, it must have been
444 // optimized out by the compiler. Do not throw NullPointerException.
445 } else {
446 // Maintain interpreter-like semantics where NullPointerException is thrown
447 // after potential NoSuchMethodError from class linker.
448 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
449 return nullptr; // Failure.
450 }
451 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700452 switch (type) {
453 case kStatic:
454 case kDirect:
455 return resolved_method;
456 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100457 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700458 uint16_t vtable_index = resolved_method->GetMethodIndex();
459 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700460 (!klass->HasVTable() ||
461 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700462 // Behavior to agree with that of the verifier.
463 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
464 resolved_method->GetName(), resolved_method->GetSignature());
465 return nullptr; // Failure.
466 }
David Sehr709b0702016-10-13 09:12:37 -0700467 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700468 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700469 }
470 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700471 // TODO This lookup is quite slow.
472 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
473 // that will actually not be what we want in some cases where there are miranda methods or
474 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
475 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700476 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700477 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700478 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800479 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800480 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000481 ObjPtr<mirror::Class> method_reference_class =
482 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700483 if (UNLIKELY(method_reference_class == nullptr)) {
484 // Bad type idx.
485 CHECK(self->IsExceptionPending());
486 return nullptr;
487 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700488 // It is not an interface. If the referring class is in the class hierarchy of the
489 // referenced class in the bytecode, we use its super class. Otherwise, we throw
490 // a NoSuchMethodError.
Vladimir Marko302f69c2017-07-25 15:27:15 +0100491 ObjPtr<mirror::Class> super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700492 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
493 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700494 }
Alex Light705ad492015-09-21 11:36:30 -0700495 uint16_t vtable_index = resolved_method->GetMethodIndex();
496 if (access_check) {
497 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700498 if (super_class == nullptr ||
499 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700500 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
501 // Behavior to agree with that of the verifier.
502 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
503 resolved_method->GetName(), resolved_method->GetSignature());
504 return nullptr; // Failure.
505 }
506 }
507 DCHECK(super_class != nullptr);
508 DCHECK(super_class->HasVTable());
509 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
510 } else {
511 // It is an interface.
512 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700513 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700514 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
515 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700516 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700517 referrer);
518 return nullptr; // Failure.
519 }
520 }
521 // TODO We can do better than this for a (compiled) fastpath.
522 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
523 resolved_method, class_linker->GetImagePointerSize());
524 // Throw an NSME if nullptr;
525 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700526 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
527 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700528 }
Alex Light705ad492015-09-21 11:36:30 -0700529 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700530 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700531 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700532 }
533 case kInterface: {
David Srbecky6c2b86e2018-10-15 12:57:20 +0100534 size_t imt_index;
535 InterpreterCache* tls_cache = self->GetInterpreterCache();
536 if (UNLIKELY(!tls_cache->Get(resolved_method, &imt_index))) {
537 imt_index = ImTable::GetImtIndex(resolved_method);
538 tls_cache->Set(resolved_method, imt_index);
539 }
540 DCHECK_EQ(imt_index, ImTable::GetImtIndex(resolved_method));
Andreas Gampe542451c2016-07-26 09:02:02 -0700541 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100542 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
543 ArtMethod* imt_method = klass->GetImt(pointer_size)->Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000544 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700545 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546 ArtMethod* method = klass->FindVirtualMethodForInterface(
547 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700548 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
549 << imt_method->PrettyMethod() << " / "
550 << ArtMethod::PrettyMethod(method) << " / "
551 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700552 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700553 return imt_method;
554 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100555 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700556 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700557 if (UNLIKELY(interface_method == nullptr)) {
558 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700559 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700560 return nullptr; // Failure.
561 }
562 return interface_method;
563 }
564 }
565 default:
566 LOG(FATAL) << "Unknown invoke type " << type;
567 return nullptr; // Failure.
568 }
569}
570
David Srbeckyc853eed2018-09-13 14:33:22 +0100571template<InvokeType type, bool access_check>
572inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
573 ObjPtr<mirror::Object>* this_object,
574 ArtMethod* referrer,
575 Thread* self) {
576 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
577 constexpr ClassLinker::ResolveMode resolve_mode =
578 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
579 : ClassLinker::ResolveMode::kNoChecks;
580 ArtMethod* resolved_method;
581 if (type == kStatic) {
582 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
583 } else {
584 StackHandleScope<1> hs(self);
585 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
586 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
587 }
588 if (UNLIKELY(resolved_method == nullptr)) {
589 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
590 return nullptr; // Failure.
591 }
592 return FindMethodToCall<type, access_check>(
593 method_idx, resolved_method, this_object, referrer, self);
594}
595
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700596// Explicit template declarations of FindMethodFromCode for all invoke types.
597#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700598 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700600 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700601 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700603#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
604 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
605 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
606
607EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
608EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
609EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
610EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
611EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
612
613#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
614#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
615
616// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
618 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700619 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700620 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800621 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700622 if (UNLIKELY(resolved_field == nullptr)) {
623 return nullptr;
624 }
625 // Check for incompatible class change.
David Srbeckyce32c102018-08-31 07:21:07 +0100626 const bool is_primitive = (type & FindFieldFlags::PrimitiveBit) != 0;
627 const bool is_set = (type & FindFieldFlags::WriteBit) != 0;
628 const bool is_static = (type & FindFieldFlags::StaticBit) != 0;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700629 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
630 // Incompatible class change.
631 return nullptr;
632 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700633 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 if (is_static) {
635 // Check class is initialized else fail so that we can contend to initialize the class with
636 // other threads that may be racing to do this.
637 if (UNLIKELY(!fields_class->IsInitialized())) {
638 return nullptr;
639 }
640 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100641 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700642 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700644 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
645 // Illegal access.
646 return nullptr;
647 }
648 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
649 resolved_field->FieldSize() != expected_size)) {
650 return nullptr;
651 }
652 return resolved_field;
653}
654
655// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100656template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700657inline ArtMethod* FindMethodFast(uint32_t method_idx,
658 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100659 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700660 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700661 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
662 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700663 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100664 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
665 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100666 constexpr ClassLinker::ResolveMode resolve_mode = access_check
667 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
668 : ClassLinker::ResolveMode::kNoChecks;
669 ClassLinker* linker = Runtime::Current()->GetClassLinker();
670 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700671 if (UNLIKELY(resolved_method == nullptr)) {
672 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700673 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700675 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
676 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700677 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 return resolved_method;
679 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700680 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000681 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000682 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000683 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700684 if (method_reference_class == nullptr) {
685 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000686 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700687 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700688 // It is not an interface. If the referring class is in the class hierarchy of the
689 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
690 // resolve the method.
691 if (!method_reference_class->IsAssignableFrom(referring_class)) {
692 return nullptr;
693 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100694 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700695 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
696 // The super class does not have the method.
697 return nullptr;
698 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700699 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700700 } else {
701 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700702 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000703 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700704 } else {
705 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700706 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700707 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700708 }
709}
710
Vladimir Marko28e012a2017-12-07 11:22:59 +0000711inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
712 ArtMethod* referrer,
713 Thread* self,
714 bool can_run_clinit,
715 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700716 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000717 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700718 if (UNLIKELY(klass == nullptr)) {
719 CHECK(self->IsExceptionPending());
720 return nullptr; // Failure - Indicate to caller to deliver exception
721 }
722 // Perform access check if necessary.
Vladimir Markod93e3742018-07-18 10:58:13 +0100723 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700724 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
725 ThrowIllegalAccessErrorClass(referring_class, klass);
726 return nullptr; // Failure - Indicate to caller to deliver exception
727 }
728 // If we're just implementing const-class, we shouldn't call <clinit>.
729 if (!can_run_clinit) {
730 return klass;
731 }
732 // If we are the <clinit> of this class, just return our storage.
733 //
734 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
735 // running.
736 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
737 return klass;
738 }
739 StackHandleScope<1> hs(self);
740 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700741 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700742 CHECK(self->IsExceptionPending());
743 return nullptr; // Failure - Indicate to caller to deliver exception
744 }
745 return h_class.Get();
746}
747
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800748inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700749 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700750 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700751 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000752 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700753 self->ClearException();
754 }
755 // Decode locked object and unlock, before popping local references.
756 self->DecodeJObject(locked)->MonitorExit(self);
757 if (UNLIKELY(self->IsExceptionPending())) {
758 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
759 << saved_exception->Dump()
760 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000761 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700762 }
763 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700764 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000765 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700766 }
767}
768
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700769template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800770inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700771 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
772 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
773 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
774 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
775 if (LIKELY(f > kMinIntAsFloat)) {
776 if (LIKELY(f < kMaxIntAsFloat)) {
777 return static_cast<INT_TYPE>(f);
778 } else {
779 return kMaxInt;
780 }
781 } else {
782 return (f != f) ? 0 : kMinInt; // f != f implies NaN
783 }
784}
785
786} // namespace art
787
788#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_