blob: 72dad0cb323427a3eb5047e77ce93505fec69d9d [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
Matthew Gharrity465ecc82016-07-19 21:32:52 +000022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "class_linker-inl.h"
25#include "common_throws.h"
26#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010027#include "entrypoints/quick/callee_save_frame.h"
28#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070029#include "imt_conflict_table.h"
30#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "indirect_reference_table.h"
32#include "invoke_type.h"
33#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "mirror/array.h"
35#include "mirror/class-inl.h"
36#include "mirror/object-inl.h"
37#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010038#include "nth_caller_visitor.h"
39#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010040#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041#include "thread.h"
42
43namespace art {
44
Mathieu Chartiere401d142015-04-22 13:56:20 -070045inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010046 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000047 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010048 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010050 // This method is being used by artQuickResolutionTrampoline, before it sets up
51 // the passed parameters in a GC friendly way. Therefore we must never be
52 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070053 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010054
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000055 if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) {
56 return inline_info.GetArtMethodAtDepth(encoding, inlining_depth);
57 }
58
David Srbecky61b28a12016-02-25 21:55:03 +000059 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000060 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
61 // "charAt" special case. It is the only non-leaf method we inline across dex files.
62 ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt);
63 DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010064 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070065 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010066
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000067 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010068 ArtMethod* caller = outer_method;
69 if (inlining_depth != 0) {
70 caller = GetResolvedMethod(outer_method,
71 inline_info,
72 encoding,
73 inlining_depth - 1);
74 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010075
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000076 // Lookup the declaring class of the inlined method.
77 const DexFile* dex_file = caller->GetDexFile();
78 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
79 const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_);
80 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
81 Thread* self = Thread::Current();
82 mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader();
83 mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader);
84 if (klass == nullptr) {
85 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
86 << "the class " << descriptor << " was not found in the class loader of "
87 << caller->PrettyMethod() << ". "
88 << "This must be due to playing wrongly with class loaders";
89 }
90
91 // Lookup the method.
92 const char* method_name = dex_file->GetMethodName(method_id);
93 const Signature signature = dex_file->GetMethodSignature(method_id);
94
95 ArtMethod* inlined_method =
96 klass->FindDeclaredDirectMethod(method_name, signature, kRuntimePointerSize);
97 if (inlined_method == nullptr) {
98 inlined_method = klass->FindDeclaredVirtualMethod(method_name, signature, kRuntimePointerSize);
99 if (inlined_method == nullptr) {
100 LOG(FATAL) << "Could not find an inlined method from an .oat file: "
101 << "the class " << descriptor << " does not have "
102 << method_name << signature << " declared. "
103 << "This must be due to duplicate classes or playing wrongly with class loaders";
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100104 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100105 }
106
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100107 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100108}
109
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700110inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100111 return GetCalleeSaveMethodCaller(
112 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
113}
114
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000115template <const bool kAccessCheck>
116ALWAYS_INLINE
117inline mirror::Class* CheckObjectAlloc(dex::TypeIndex type_idx,
118 ArtMethod* method,
119 Thread* self,
120 bool* slow_path) {
121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
122 PointerSize pointer_size = class_linker->GetImagePointerSize();
123 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
124 if (UNLIKELY(klass == nullptr)) {
125 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126 *slow_path = true;
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000127 if (klass == nullptr) {
128 DCHECK(self->IsExceptionPending());
129 return nullptr; // Failure
130 } else {
131 DCHECK(!self->IsExceptionPending());
132 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700133 }
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000134 if (kAccessCheck) {
135 if (UNLIKELY(!klass->IsInstantiable())) {
136 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
137 *slow_path = true;
138 return nullptr; // Failure
139 }
140 if (UNLIKELY(klass->IsClassClass())) {
141 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
142 klass->PrettyDescriptor().c_str());
143 *slow_path = true;
144 return nullptr; // Failure
145 }
146 mirror::Class* referrer = method->GetDeclaringClass();
147 if (UNLIKELY(!referrer->CanAccess(klass))) {
148 ThrowIllegalAccessErrorClass(referrer, klass);
149 *slow_path = true;
150 return nullptr; // Failure
151 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700152 }
153 if (UNLIKELY(!klass->IsInitialized())) {
154 StackHandleScope<1> hs(self);
155 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
156 // EnsureInitialized (the class initializer) might cause a GC.
157 // may cause us to suspend meaning that another thread may try to
158 // change the allocator while we are stuck in the entrypoints of
159 // an old allocator. Also, the class initialization may fail. To
160 // handle these cases we mark the slow path boolean as true so
161 // that the caller knows to check the allocator type to see if it
162 // has changed and to null-check the return value in case the
163 // initialization fails.
164 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700165 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700166 DCHECK(self->IsExceptionPending());
167 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700168 } else {
169 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700170 }
171 return h_klass.Get();
172 }
173 return klass;
174}
175
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700176ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800177inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
178 Thread* self,
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000179 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700180 if (UNLIKELY(!klass->IsInitialized())) {
181 StackHandleScope<1> hs(self);
182 Handle<mirror::Class> h_class(hs.NewHandle(klass));
183 // EnsureInitialized (the class initializer) might cause a GC.
184 // may cause us to suspend meaning that another thread may try to
185 // change the allocator while we are stuck in the entrypoints of
186 // an old allocator. Also, the class initialization may fail. To
187 // handle these cases we mark the slow path boolean as true so
188 // that the caller knows to check the allocator type to see if it
189 // has changed and to null-check the return value in case the
190 // initialization fails.
191 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700192 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700193 DCHECK(self->IsExceptionPending());
194 return nullptr; // Failure
195 }
196 return h_class.Get();
197 }
198 return klass;
199}
200
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000201// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
202// cannot be resolved, throw an error. If it can, use it to create an instance.
203// When verification/compiler hasn't been able to verify access, optionally perform an access
204// check.
205template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700206ALWAYS_INLINE
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000207inline mirror::Object* AllocObjectFromCode(dex::TypeIndex type_idx,
208 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800209 Thread* self,
210 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700211 bool slow_path = false;
Hiroshi Yamauchif7aaacd2017-01-12 02:58:38 +0000212 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700213 if (UNLIKELY(slow_path)) {
214 if (klass == nullptr) {
215 return nullptr;
216 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800217 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
218 return klass->Alloc</*kInstrumented*/true>(
219 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700220 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221 }
222 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700223 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700224}
225
226// Given the context of a calling Method and a resolved 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* AllocObjectFromCodeResolved(mirror::Class* klass,
230 Thread* self,
231 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232 DCHECK(klass != nullptr);
233 bool slow_path = false;
234 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
235 if (UNLIKELY(slow_path)) {
236 if (klass == nullptr) {
237 return nullptr;
238 }
239 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000240 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800241 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
242 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700243 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000245 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700246 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247}
248
249// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700251ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800252inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
253 Thread* self,
254 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000256 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700257 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258}
259
260
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700261template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700262ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800263inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800264 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800266 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 if (UNLIKELY(component_count < 0)) {
268 ThrowNegativeArraySizeException(component_count);
269 *slow_path = true;
270 return nullptr; // Failure
271 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100272 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700273 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100274 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700275 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100276 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277 *slow_path = true;
278 if (klass == nullptr) { // Error
279 DCHECK(Thread::Current()->IsExceptionPending());
280 return nullptr; // Failure
281 }
David Sehr709b0702016-10-13 09:12:37 -0700282 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283 }
284 if (kAccessCheck) {
285 mirror::Class* referrer = method->GetDeclaringClass();
286 if (UNLIKELY(!referrer->CanAccess(klass))) {
287 ThrowIllegalAccessErrorClass(referrer, klass);
288 *slow_path = true;
289 return nullptr; // Failure
290 }
291 }
292 return klass;
293}
294
295// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
296// it cannot be resolved, throw an error. If it can, use it to create an array.
297// When verification/compiler hasn't been able to verify access, optionally perform an access
298// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700299template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700300ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800301inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800302 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800304 Thread* self,
305 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700306 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800307 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700308 &slow_path);
309 if (UNLIKELY(slow_path)) {
310 if (klass == nullptr) {
311 return nullptr;
312 }
313 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800314 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
315 return mirror::Array::Alloc</*kInstrumented*/true>(self,
316 klass,
317 component_count,
318 klass->GetComponentSizeShift(),
319 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320 }
321 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700322 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700323}
324
325template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700326ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800327inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800328 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800330 Thread* self,
331 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700332 DCHECK(klass != nullptr);
333 if (UNLIKELY(component_count < 0)) {
334 ThrowNegativeArraySizeException(component_count);
335 return nullptr; // Failure
336 }
337 if (kAccessCheck) {
338 mirror::Class* referrer = method->GetDeclaringClass();
339 if (UNLIKELY(!referrer->CanAccess(klass))) {
340 ThrowIllegalAccessErrorClass(referrer, klass);
341 return nullptr; // Failure
342 }
343 }
344 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
345 // suspension.
346 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700347 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700348}
349
350template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800351inline ArtField* FindFieldFromCode(uint32_t field_idx,
352 ArtMethod* referrer,
353 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700354 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700355 bool is_primitive;
356 bool is_set;
357 bool is_static;
358 switch (type) {
359 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
360 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
361 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
362 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
363 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
364 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
365 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
366 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
367 default: is_primitive = true; is_set = true; is_static = true; break;
368 }
369 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800370
371 ArtField* resolved_field;
372 if (access_check) {
373 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
374 // qualifying type of a field and the resolved run-time qualifying type of a field differed
375 // in their static-ness.
376 //
377 // In particular, don't assume the dex instruction already correctly knows if the
378 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700379 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800380
381 StackHandleScope<2> hs(self);
382 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
383 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
384
385 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
386 field_idx,
387 h_dex_cache,
388 h_class_loader);
389 } else {
390 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
391 // be executing here if there was a static/non-static mismatch.
392 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
393 }
394
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700395 if (UNLIKELY(resolved_field == nullptr)) {
396 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
397 return nullptr; // Failure.
398 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700399 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700400 if (access_check) {
401 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
402 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
403 return nullptr;
404 }
405 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700406 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
407 resolved_field,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700408 field_idx))) {
409 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
410 return nullptr; // Failure.
411 }
412 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
413 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
414 return nullptr; // Failure.
415 } else {
416 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
417 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000418 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700419 "Attempted read of %zd-bit %s on field '%s'",
420 expected_size * (32 / sizeof(int32_t)),
421 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700422 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700423 return nullptr; // Failure.
424 }
425 }
426 }
427 if (!is_static) {
428 // instance fields must be being accessed on an initialized class
429 return resolved_field;
430 } else {
431 // If the class is initialized we're done.
432 if (LIKELY(fields_class->IsInitialized())) {
433 return resolved_field;
434 } else {
435 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700436 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700437 // Otherwise let's ensure the class is initialized before resolving the field.
438 return resolved_field;
439 }
440 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
441 return nullptr; // Failure.
442 }
443 }
444}
445
446// Explicit template declarations of FindFieldFromCode for all field access types.
447#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700448template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700449ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 ArtMethod* referrer, \
451 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700452
453#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
454 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
455 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
456
457EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
458EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
459EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
460EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
461EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
462EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
463EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
464EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
465
466#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
467#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
468
469template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700470inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700471 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700472 ArtMethod* referrer,
473 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700474 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700475 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700476 if (resolved_method == nullptr) {
477 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700478 ObjPtr<mirror::Object> null_this = nullptr;
479 HandleWrapperObjPtr<mirror::Object> h_this(
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700480 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800481 constexpr ClassLinker::ResolveMode resolve_mode =
482 access_check ? ClassLinker::kForceICCECheck
483 : ClassLinker::kNoICCECheckForCache;
484 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700485 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700486 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700487 if (UNLIKELY(resolved_method == nullptr)) {
488 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
489 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700490 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700491 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700492 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700493 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
494 resolved_method,
495 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700496 if (UNLIKELY(!can_access_resolved_method)) {
497 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
498 return nullptr; // Failure.
499 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100500 // Incompatible class change should have been handled in resolve method.
501 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
502 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
503 referrer);
504 return nullptr; // Failure.
505 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700506 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700507 // Next, null pointer check.
508 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
509 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
510 resolved_method->IsConstructor())) {
511 // Hack for String init:
512 //
513 // We assume that the input of String.<init> in verified code is always
514 // an unitialized reference. If it is a null constant, it must have been
515 // optimized out by the compiler. Do not throw NullPointerException.
516 } else {
517 // Maintain interpreter-like semantics where NullPointerException is thrown
518 // after potential NoSuchMethodError from class linker.
519 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
520 return nullptr; // Failure.
521 }
522 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700523 switch (type) {
524 case kStatic:
525 case kDirect:
526 return resolved_method;
527 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700528 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700529 uint16_t vtable_index = resolved_method->GetMethodIndex();
530 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700531 (!klass->HasVTable() ||
532 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700533 // Behavior to agree with that of the verifier.
534 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
535 resolved_method->GetName(), resolved_method->GetSignature());
536 return nullptr; // Failure.
537 }
David Sehr709b0702016-10-13 09:12:37 -0700538 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700540 }
541 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700542 // TODO This lookup is quite slow.
543 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
544 // that will actually not be what we want in some cases where there are miranda methods or
545 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
546 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700547 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700548 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700549 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800550 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800551 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700552 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
553 if (UNLIKELY(method_reference_class == nullptr)) {
554 // Bad type idx.
555 CHECK(self->IsExceptionPending());
556 return nullptr;
557 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700558 // It is not an interface. If the referring class is in the class hierarchy of the
559 // referenced class in the bytecode, we use its super class. Otherwise, we throw
560 // a NoSuchMethodError.
561 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700562 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
563 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700564 }
Alex Light705ad492015-09-21 11:36:30 -0700565 uint16_t vtable_index = resolved_method->GetMethodIndex();
566 if (access_check) {
567 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700568 if (super_class == nullptr ||
569 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700570 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
571 // Behavior to agree with that of the verifier.
572 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
573 resolved_method->GetName(), resolved_method->GetSignature());
574 return nullptr; // Failure.
575 }
576 }
577 DCHECK(super_class != nullptr);
578 DCHECK(super_class->HasVTable());
579 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
580 } else {
581 // It is an interface.
582 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700583 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700584 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
585 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700586 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700587 referrer);
588 return nullptr; // Failure.
589 }
590 }
591 // TODO We can do better than this for a (compiled) fastpath.
592 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
593 resolved_method, class_linker->GetImagePointerSize());
594 // Throw an NSME if nullptr;
595 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700596 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
597 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700598 }
Alex Light705ad492015-09-21 11:36:30 -0700599 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700600 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700601 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700602 }
603 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700604 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700605 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000606 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
607 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000608 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700609 if (kIsDebugBuild) {
610 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 ArtMethod* method = klass->FindVirtualMethodForInterface(
612 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700613 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
614 << imt_method->PrettyMethod() << " / "
615 << ArtMethod::PrettyMethod(method) << " / "
616 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700617 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700618 return imt_method;
619 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
621 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700622 if (UNLIKELY(interface_method == nullptr)) {
623 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700624 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700625 return nullptr; // Failure.
626 }
627 return interface_method;
628 }
629 }
630 default:
631 LOG(FATAL) << "Unknown invoke type " << type;
632 return nullptr; // Failure.
633 }
634}
635
636// Explicit template declarations of FindMethodFromCode for all invoke types.
637#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700638 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700640 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700641 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700643#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
644 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
645 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
646
647EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
648EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
649EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
650EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
651EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
652
653#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
654#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
655
656// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700657inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
658 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700659 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700660 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800661 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700662 if (UNLIKELY(resolved_field == nullptr)) {
663 return nullptr;
664 }
665 // Check for incompatible class change.
666 bool is_primitive;
667 bool is_set;
668 bool is_static;
669 switch (type) {
670 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
671 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
672 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
673 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
674 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
675 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
676 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
677 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
678 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700679 LOG(FATAL) << "UNREACHABLE";
680 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700681 }
682 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
683 // Incompatible class change.
684 return nullptr;
685 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700686 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 if (is_static) {
688 // Check class is initialized else fail so that we can contend to initialize the class with
689 // other threads that may be racing to do this.
690 if (UNLIKELY(!fields_class->IsInitialized())) {
691 return nullptr;
692 }
693 }
694 mirror::Class* referring_class = referrer->GetDeclaringClass();
695 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700697 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
698 // Illegal access.
699 return nullptr;
700 }
701 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
702 resolved_field->FieldSize() != expected_size)) {
703 return nullptr;
704 }
705 return resolved_field;
706}
707
708// Fast path method resolution that can't throw exceptions.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700709inline ArtMethod* FindMethodFast(uint32_t method_idx,
710 ObjPtr<mirror::Object> this_object,
711 ArtMethod* referrer,
712 bool access_check,
713 InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700714 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700715 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
716 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700717 }
Alex Light705ad492015-09-21 11:36:30 -0700718 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700719 ArtMethod* resolved_method =
Alex Lightdba61482016-12-21 08:20:29 -0800720 referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700721 if (UNLIKELY(resolved_method == nullptr)) {
722 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700723 }
724 if (access_check) {
725 // Check for incompatible class change errors and access.
726 bool icce = resolved_method->CheckIncompatibleClassChange(type);
727 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700728 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700729 }
730 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700731 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
732 !referring_class->CanAccessMember(methods_class,
733 resolved_method->GetAccessFlags()))) {
734 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700735 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700736 }
737 }
738 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700739 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
740 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700741 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700742 return resolved_method;
743 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700744 // TODO This lookup is rather slow.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800745 dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800746 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700747 mirror::Class* method_reference_class =
Alex Lightdba61482016-12-21 08:20:29 -0800748 referrer->GetDexCache()->GetResolvedType(method_type_idx);
Alex Light705ad492015-09-21 11:36:30 -0700749 if (method_reference_class == nullptr) {
750 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000751 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700752 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700753 // It is not an interface. If the referring class is in the class hierarchy of the
754 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
755 // resolve the method.
756 if (!method_reference_class->IsAssignableFrom(referring_class)) {
757 return nullptr;
758 }
759 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700760 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
761 // The super class does not have the method.
762 return nullptr;
763 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700764 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700765 } else {
766 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700767 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000768 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700769 } else {
770 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700772 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700773 }
774}
775
Andreas Gampea5b09a62016-11-17 15:21:22 -0800776inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
777 ArtMethod* referrer,
778 Thread* self,
779 bool can_run_clinit,
780 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700781 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
782 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
783 if (UNLIKELY(klass == nullptr)) {
784 CHECK(self->IsExceptionPending());
785 return nullptr; // Failure - Indicate to caller to deliver exception
786 }
787 // Perform access check if necessary.
788 mirror::Class* referring_class = referrer->GetDeclaringClass();
789 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
790 ThrowIllegalAccessErrorClass(referring_class, klass);
791 return nullptr; // Failure - Indicate to caller to deliver exception
792 }
793 // If we're just implementing const-class, we shouldn't call <clinit>.
794 if (!can_run_clinit) {
795 return klass;
796 }
797 // If we are the <clinit> of this class, just return our storage.
798 //
799 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
800 // running.
801 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
802 return klass;
803 }
804 StackHandleScope<1> hs(self);
805 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700806 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700807 CHECK(self->IsExceptionPending());
808 return nullptr; // Failure - Indicate to caller to deliver exception
809 }
810 return h_class.Get();
811}
812
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800813inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700814 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
815 return class_linker->ResolveString(string_idx, referrer);
816}
817
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800818inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700820 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700821 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000822 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700823 self->ClearException();
824 }
825 // Decode locked object and unlock, before popping local references.
826 self->DecodeJObject(locked)->MonitorExit(self);
827 if (UNLIKELY(self->IsExceptionPending())) {
828 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
829 << saved_exception->Dump()
830 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000831 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700832 }
833 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700834 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000835 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700836 }
837}
838
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700839template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800840inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700841 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
842 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
843 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
844 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
845 if (LIKELY(f > kMinIntAsFloat)) {
846 if (LIKELY(f < kMaxIntAsFloat)) {
847 return static_cast<INT_TYPE>(f);
848 } else {
849 return kMaxInt;
850 }
851 } else {
852 return (f != f) ? 0 : kMinInt; // f != f implies NaN
853 }
854}
855
856} // namespace art
857
858#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_