blob: 01fb4ddde76469eeea5ea7fecb91002a133e345a [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 Marko8e110652019-07-30 10:14:41 +0100117ALWAYS_INLINE
118inline ObjPtr<mirror::Class> CheckClassInitializedForObjectAlloc(ObjPtr<mirror::Class> klass,
119 Thread* self,
120 bool* slow_path)
121 REQUIRES_SHARED(Locks::mutator_lock_)
122 REQUIRES(!Roles::uninterruptible_) {
123 if (UNLIKELY(!klass->IsVisiblyInitialized())) {
124 StackHandleScope<1> hs(self);
125 Handle<mirror::Class> h_class(hs.NewHandle(klass));
126 // EnsureInitialized (the class initializer) might cause a GC.
127 // may cause us to suspend meaning that another thread may try to
128 // change the allocator while we are stuck in the entrypoints of
129 // an old allocator. Also, the class initialization may fail. To
130 // handle these cases we mark the slow path boolean as true so
131 // that the caller knows to check the allocator type to see if it
132 // has changed and to null-check the return value in case the
133 // initialization fails.
134 *slow_path = true;
135 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
136 DCHECK(self->IsExceptionPending());
137 return nullptr; // Failure
138 } else {
139 DCHECK(!self->IsExceptionPending());
140 }
141 return h_class.Get();
142 }
143 return klass;
144}
145
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000146ALWAYS_INLINE inline ObjPtr<mirror::Class> CheckObjectAlloc(ObjPtr<mirror::Class> klass,
147 Thread* self,
148 bool* slow_path)
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000149 REQUIRES_SHARED(Locks::mutator_lock_)
150 REQUIRES(!Roles::uninterruptible_) {
151 if (UNLIKELY(!klass->IsInstantiable())) {
152 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700153 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000154 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700155 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000156 if (UNLIKELY(klass->IsClassClass())) {
157 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
158 klass->PrettyDescriptor().c_str());
159 *slow_path = true;
160 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700161 }
Vladimir Marko8e110652019-07-30 10:14:41 +0100162 return CheckClassInitializedForObjectAlloc(klass, self, slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700163}
164
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000165// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
166// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
167template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700168ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000169inline ObjPtr<mirror::Object> AllocObjectFromCode(ObjPtr<mirror::Class> klass,
170 Thread* self,
171 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700172 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000173 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700174 if (UNLIKELY(slow_path)) {
175 if (klass == nullptr) {
176 return nullptr;
177 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800178 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700179 return klass->Alloc</*kInstrumented=*/true>(
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800180 self,
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000181 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700182 }
183 DCHECK(klass != nullptr);
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000184 return klass->Alloc<kInstrumented>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700185}
186
187// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700188template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700189ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000190inline ObjPtr<mirror::Object> AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,
191 Thread* self,
192 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700193 DCHECK(klass != nullptr);
194 bool slow_path = false;
195 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
196 if (UNLIKELY(slow_path)) {
197 if (klass == nullptr) {
198 return nullptr;
199 }
200 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000201 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800202 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
203 // instrumented.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100204 return klass->Alloc</*kInstrumented=*/true, /*kCheckAddFinalizer=*/false>(
205 self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700206 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000207 // Pass in false since the object cannot be finalizable.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100208 return klass->Alloc<kInstrumented, /*kCheckAddFinalizer=*/false>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700209}
210
211// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700212template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700213ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000214inline ObjPtr<mirror::Object> AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,
215 Thread* self,
216 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700217 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000218 // Pass in false since the object cannot be finalizable.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100219 return klass->Alloc<kInstrumented, /*kCheckAddFinalizer=*/false>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220}
221
222
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700224ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000225inline ObjPtr<mirror::Class> CheckArrayAlloc(dex::TypeIndex type_idx,
226 int32_t component_count,
227 ArtMethod* method,
228 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229 if (UNLIKELY(component_count < 0)) {
230 ThrowNegativeArraySizeException(component_count);
231 *slow_path = true;
232 return nullptr; // Failure
233 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000234 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000236 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100237 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700238 *slow_path = true;
239 if (klass == nullptr) { // Error
240 DCHECK(Thread::Current()->IsExceptionPending());
241 return nullptr; // Failure
242 }
David Sehr709b0702016-10-13 09:12:37 -0700243 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 }
245 if (kAccessCheck) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100246 ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 if (UNLIKELY(!referrer->CanAccess(klass))) {
248 ThrowIllegalAccessErrorClass(referrer, klass);
249 *slow_path = true;
250 return nullptr; // Failure
251 }
252 }
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000253 return klass;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254}
255
256// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
257// it cannot be resolved, throw an error. If it can, use it to create an array.
258// When verification/compiler hasn't been able to verify access, optionally perform an access
259// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700260template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700261ALWAYS_INLINE
Vladimir Markobcf17522018-06-01 13:14:32 +0100262inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
263 int32_t component_count,
264 ArtMethod* method,
265 Thread* self,
266 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 bool slow_path = false;
Vladimir Markobcf17522018-06-01 13:14:32 +0100268 ObjPtr<mirror::Class> klass =
269 CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700270 if (UNLIKELY(slow_path)) {
271 if (klass == nullptr) {
272 return nullptr;
273 }
274 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800275 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700276 return mirror::Array::Alloc</*kInstrumented=*/true>(self,
277 klass,
278 component_count,
279 klass->GetComponentSizeShift(),
280 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700281 }
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100282 return mirror::Array::Alloc<kInstrumented>(self,
283 klass,
284 component_count,
285 klass->GetComponentSizeShift(),
286 allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700287}
288
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000289template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700290ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000291inline ObjPtr<mirror::Array> AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,
292 int32_t component_count,
293 Thread* self,
294 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700295 DCHECK(klass != nullptr);
296 if (UNLIKELY(component_count < 0)) {
297 ThrowNegativeArraySizeException(component_count);
298 return nullptr; // Failure
299 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700300 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
301 // suspension.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100302 return mirror::Array::Alloc<kInstrumented>(self,
303 klass,
304 component_count,
305 klass->GetComponentSizeShift(),
306 allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307}
308
309template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800310inline ArtField* FindFieldFromCode(uint32_t field_idx,
311 ArtMethod* referrer,
312 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700313 size_t expected_size) {
David Srbeckyce32c102018-08-31 07:21:07 +0100314 constexpr bool is_primitive = (type & FindFieldFlags::PrimitiveBit) != 0;
315 constexpr bool is_set = (type & FindFieldFlags::WriteBit) != 0;
316 constexpr bool is_static = (type & FindFieldFlags::StaticBit) != 0;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800318
319 ArtField* resolved_field;
320 if (access_check) {
321 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
322 // qualifying type of a field and the resolved run-time qualifying type of a field differed
323 // in their static-ness.
324 //
325 // In particular, don't assume the dex instruction already correctly knows if the
326 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700327 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800328
329 StackHandleScope<2> hs(self);
330 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
331 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
332
Vladimir Markoe11dd502017-12-08 14:09:45 +0000333 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800334 h_dex_cache,
335 h_class_loader);
336 } else {
337 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
338 // be executing here if there was a static/non-static mismatch.
339 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
340 }
341
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700342 if (UNLIKELY(resolved_field == nullptr)) {
343 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
344 return nullptr; // Failure.
345 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700346 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700347 if (access_check) {
348 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
349 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
350 return nullptr;
351 }
Vladimir Markod93e3742018-07-18 10:58:13 +0100352 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700353 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
354 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100355 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700356 field_idx))) {
357 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
358 return nullptr; // Failure.
359 }
360 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
361 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
362 return nullptr; // Failure.
363 } else {
364 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
365 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000366 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 "Attempted read of %zd-bit %s on field '%s'",
368 expected_size * (32 / sizeof(int32_t)),
369 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700370 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700371 return nullptr; // Failure.
372 }
373 }
374 }
375 if (!is_static) {
376 // instance fields must be being accessed on an initialized class
377 return resolved_field;
378 } else {
379 // If the class is initialized we're done.
380 if (LIKELY(fields_class->IsInitialized())) {
381 return resolved_field;
382 } else {
383 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700384 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700385 // Otherwise let's ensure the class is initialized before resolving the field.
386 return resolved_field;
387 }
388 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
389 return nullptr; // Failure.
390 }
391 }
392}
393
394// Explicit template declarations of FindFieldFromCode for all field access types.
395#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700396template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700397ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 ArtMethod* referrer, \
399 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700400
401#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
402 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
403 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
404
405EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
406EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
407EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
408EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
409EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
410EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
411EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
412EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
413
414#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
415#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
416
David Srbeckyc853eed2018-09-13 14:33:22 +0100417// Follow virtual/interface indirections if applicable.
418// Will throw null-pointer exception the if the object is null.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700419template<InvokeType type, bool access_check>
David Srbeckyc853eed2018-09-13 14:33:22 +0100420ALWAYS_INLINE ArtMethod* FindMethodToCall(uint32_t method_idx,
421 ArtMethod* resolved_method,
422 ObjPtr<mirror::Object>* this_object,
423 ArtMethod* referrer,
424 Thread* self)
425 REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700426 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
David Srbeckyc853eed2018-09-13 14:33:22 +0100427 // Null pointer check.
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700428 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
429 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
430 resolved_method->IsConstructor())) {
431 // Hack for String init:
432 //
433 // We assume that the input of String.<init> in verified code is always
434 // an unitialized reference. If it is a null constant, it must have been
435 // optimized out by the compiler. Do not throw NullPointerException.
436 } else {
437 // Maintain interpreter-like semantics where NullPointerException is thrown
438 // after potential NoSuchMethodError from class linker.
439 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
440 return nullptr; // Failure.
441 }
442 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700443 switch (type) {
444 case kStatic:
445 case kDirect:
446 return resolved_method;
447 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100448 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700449 uint16_t vtable_index = resolved_method->GetMethodIndex();
450 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700451 (!klass->HasVTable() ||
452 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700453 // Behavior to agree with that of the verifier.
454 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
455 resolved_method->GetName(), resolved_method->GetSignature());
456 return nullptr; // Failure.
457 }
David Sehr709b0702016-10-13 09:12:37 -0700458 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700459 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 }
461 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700462 // TODO This lookup is quite slow.
463 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
464 // that will actually not be what we want in some cases where there are miranda methods or
465 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
466 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700467 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700468 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700469 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800470 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800471 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000472 ObjPtr<mirror::Class> method_reference_class =
473 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700474 if (UNLIKELY(method_reference_class == nullptr)) {
475 // Bad type idx.
476 CHECK(self->IsExceptionPending());
477 return nullptr;
478 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700479 // It is not an interface. If the referring class is in the class hierarchy of the
480 // referenced class in the bytecode, we use its super class. Otherwise, we throw
481 // a NoSuchMethodError.
Vladimir Marko302f69c2017-07-25 15:27:15 +0100482 ObjPtr<mirror::Class> super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700483 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
484 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700485 }
Alex Light705ad492015-09-21 11:36:30 -0700486 uint16_t vtable_index = resolved_method->GetMethodIndex();
487 if (access_check) {
488 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700489 if (super_class == nullptr ||
490 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700491 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
492 // Behavior to agree with that of the verifier.
493 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
494 resolved_method->GetName(), resolved_method->GetSignature());
495 return nullptr; // Failure.
496 }
497 }
498 DCHECK(super_class != nullptr);
499 DCHECK(super_class->HasVTable());
500 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
501 } else {
502 // It is an interface.
503 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700504 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700505 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
506 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700507 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700508 referrer);
509 return nullptr; // Failure.
510 }
511 }
512 // TODO We can do better than this for a (compiled) fastpath.
513 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
514 resolved_method, class_linker->GetImagePointerSize());
515 // Throw an NSME if nullptr;
516 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700517 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
518 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700519 }
Alex Light705ad492015-09-21 11:36:30 -0700520 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700521 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700522 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700523 }
524 case kInterface: {
David Srbeckye36e7f22018-11-14 14:21:23 +0000525 size_t imt_index = resolved_method->GetImtIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -0700526 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100527 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
528 ArtMethod* imt_method = klass->GetImt(pointer_size)->Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000529 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700530 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 ArtMethod* method = klass->FindVirtualMethodForInterface(
532 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700533 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
534 << imt_method->PrettyMethod() << " / "
535 << ArtMethod::PrettyMethod(method) << " / "
536 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700537 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700538 return imt_method;
539 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100540 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700542 if (UNLIKELY(interface_method == nullptr)) {
543 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700544 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700545 return nullptr; // Failure.
546 }
547 return interface_method;
548 }
549 }
550 default:
551 LOG(FATAL) << "Unknown invoke type " << type;
552 return nullptr; // Failure.
553 }
554}
555
David Srbeckyc853eed2018-09-13 14:33:22 +0100556template<InvokeType type, bool access_check>
557inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
558 ObjPtr<mirror::Object>* this_object,
559 ArtMethod* referrer,
560 Thread* self) {
561 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
562 constexpr ClassLinker::ResolveMode resolve_mode =
563 access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE
564 : ClassLinker::ResolveMode::kNoChecks;
565 ArtMethod* resolved_method;
566 if (type == kStatic) {
567 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
568 } else {
569 StackHandleScope<1> hs(self);
570 HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
571 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
572 }
573 if (UNLIKELY(resolved_method == nullptr)) {
574 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
575 return nullptr; // Failure.
576 }
577 return FindMethodToCall<type, access_check>(
578 method_idx, resolved_method, this_object, referrer, self);
579}
580
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700581// Explicit template declarations of FindMethodFromCode for all invoke types.
582#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700583 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700584 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700585 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700586 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700587 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700588#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
589 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
590 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
591
592EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
593EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
594EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
595EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
596EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
597
598#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
599#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
600
601// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
603 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700604 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700605 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800606 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700607 if (UNLIKELY(resolved_field == nullptr)) {
608 return nullptr;
609 }
610 // Check for incompatible class change.
David Srbeckyce32c102018-08-31 07:21:07 +0100611 const bool is_primitive = (type & FindFieldFlags::PrimitiveBit) != 0;
612 const bool is_set = (type & FindFieldFlags::WriteBit) != 0;
613 const bool is_static = (type & FindFieldFlags::StaticBit) != 0;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700614 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
615 // Incompatible class change.
616 return nullptr;
617 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700618 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700619 if (is_static) {
620 // Check class is initialized else fail so that we can contend to initialize the class with
621 // other threads that may be racing to do this.
622 if (UNLIKELY(!fields_class->IsInitialized())) {
623 return nullptr;
624 }
625 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100626 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700627 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700628 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700629 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
630 // Illegal access.
631 return nullptr;
632 }
633 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
634 resolved_field->FieldSize() != expected_size)) {
635 return nullptr;
636 }
637 return resolved_field;
638}
639
640// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100641template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700642inline ArtMethod* FindMethodFast(uint32_t method_idx,
643 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100644 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700645 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700646 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
647 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700648 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100649 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
650 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100651 constexpr ClassLinker::ResolveMode resolve_mode = access_check
652 ? ClassLinker::ResolveMode::kCheckICCEAndIAE
653 : ClassLinker::ResolveMode::kNoChecks;
654 ClassLinker* linker = Runtime::Current()->GetClassLinker();
655 ArtMethod* resolved_method = linker->GetResolvedMethod<type, resolve_mode>(method_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700656 if (UNLIKELY(resolved_method == nullptr)) {
657 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700658 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700659 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700660 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
661 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700662 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700663 return resolved_method;
664 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700665 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000666 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000667 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000668 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700669 if (method_reference_class == nullptr) {
670 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000671 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700672 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700673 // It is not an interface. If the referring class is in the class hierarchy of the
674 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
675 // resolve the method.
676 if (!method_reference_class->IsAssignableFrom(referring_class)) {
677 return nullptr;
678 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100679 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700680 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
681 // The super class does not have the method.
682 return nullptr;
683 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700684 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700685 } else {
686 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700687 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000688 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700689 } else {
690 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700692 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700693 }
694}
695
Vladimir Marko28e012a2017-12-07 11:22:59 +0000696inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
697 ArtMethod* referrer,
698 Thread* self,
699 bool can_run_clinit,
700 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700701 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000702 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700703 if (UNLIKELY(klass == nullptr)) {
704 CHECK(self->IsExceptionPending());
705 return nullptr; // Failure - Indicate to caller to deliver exception
706 }
707 // Perform access check if necessary.
Vladimir Markod93e3742018-07-18 10:58:13 +0100708 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700709 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
710 ThrowIllegalAccessErrorClass(referring_class, klass);
711 return nullptr; // Failure - Indicate to caller to deliver exception
712 }
713 // If we're just implementing const-class, we shouldn't call <clinit>.
714 if (!can_run_clinit) {
715 return klass;
716 }
717 // If we are the <clinit> of this class, just return our storage.
718 //
719 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
720 // running.
721 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
722 return klass;
723 }
724 StackHandleScope<1> hs(self);
725 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700726 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700727 CHECK(self->IsExceptionPending());
728 return nullptr; // Failure - Indicate to caller to deliver exception
729 }
730 return h_class.Get();
731}
732
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800733inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700734 // Save any pending exception over monitor exit call.
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000735 ObjPtr<mirror::Throwable> saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700736 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000737 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700738 self->ClearException();
739 }
740 // Decode locked object and unlock, before popping local references.
741 self->DecodeJObject(locked)->MonitorExit(self);
742 if (UNLIKELY(self->IsExceptionPending())) {
743 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
744 << saved_exception->Dump()
745 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000746 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700747 }
748 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700749 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000750 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700751 }
752}
753
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700754template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800755inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700756 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
757 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
758 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
759 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
760 if (LIKELY(f > kMinIntAsFloat)) {
761 if (LIKELY(f < kMaxIntAsFloat)) {
762 return static_cast<INT_TYPE>(f);
763 } else {
764 return kMaxInt;
765 }
766 } else {
767 return (f != f) ? 0 : kMinInt; // f != f implies NaN
768 }
769}
770
771} // namespace art
772
773#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_