blob: 204ba463fb5382fb95a609b3d469fbd337eed7bc [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "indirect_reference_table.h"
30#include "invoke_type.h"
31#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "mirror/array.h"
33#include "mirror/class-inl.h"
34#include "mirror/object-inl.h"
35#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010036#include "nth_caller_visitor.h"
37#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010038#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070039#include "thread.h"
40
41namespace art {
42
Mathieu Chartiere401d142015-04-22 13:56:20 -070043inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010044 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000045 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010046 uint8_t inlining_depth)
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010047 SHARED_REQUIRES(Locks::mutator_lock_) {
48 // This method is being used by artQuickResolutionTrampoline, before it sets up
49 // the passed parameters in a GC friendly way. Therefore we must never be
50 // suspended while executing it.
51 ScopedAssertNoThreadSuspension sants(Thread::Current(), __FUNCTION__);
52
David Srbecky61b28a12016-02-25 21:55:03 +000053 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010054 InvokeType invoke_type = static_cast<InvokeType>(
David Srbecky61b28a12016-02-25 21:55:03 +000055 inline_info.GetInvokeTypeAtDepth(encoding, inlining_depth));
Andreas Gampe542451c2016-07-26 09:02:02 -070056 ArtMethod* inlined_method = outer_method->GetDexCacheResolvedMethod(method_index,
57 kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010058 if (!inlined_method->IsRuntimeMethod()) {
59 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070060 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010061
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010062 // The method in the dex cache is the runtime method responsible for invoking
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010063 // the stub that will then update the dex cache. Therefore, we need to do the
64 // resolution ourselves.
Nicolas Geoffray3976e5e2015-06-15 08:58:03 +010065
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010066 // We first find the dex cache of our caller. If it is the outer method, we can directly
67 // use its dex cache. Otherwise, we also need to resolve our caller.
68 ArtMethod* caller = outer_method;
69 if (inlining_depth != 0) {
70 caller = GetResolvedMethod(outer_method,
71 inline_info,
72 encoding,
73 inlining_depth - 1);
74 }
75 DCHECK_EQ(caller->GetDexCache(), outer_method->GetDexCache())
76 << "Compiler only supports inlining calls within the same dex cache";
77 const DexFile* dex_file = outer_method->GetDexFile();
78 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
79
80 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
81 // "charAt" special case. It is the only non-leaf method we inline across dex files.
82 if (kIsDebugBuild) {
83 const char* name = dex_file->StringDataByIdx(method_id.name_idx_);
84 DCHECK_EQ(std::string(name), "charAt");
85 DCHECK_EQ(std::string(dex_file->GetMethodShorty(method_id)), "CI")
86 << std::string(dex_file->GetMethodShorty(method_id));
87 DCHECK_EQ(std::string(dex_file->StringByTypeIdx(method_id.class_idx_)), "Ljava/lang/String;")
88 << std::string(dex_file->StringByTypeIdx(method_id.class_idx_));
89 }
90 mirror::Class* cls =
91 Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kJavaLangString);
92 // Update the dex cache for future lookups.
93 caller->GetDexCache()->SetResolvedType(method_id.class_idx_, cls);
Andreas Gampe542451c2016-07-26 09:02:02 -070094 inlined_method = cls->FindVirtualMethod("charAt", "(I)C", kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010095 } else {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010096 mirror::Class* klass = caller->GetDexCache()->GetResolvedType(method_id.class_idx_);
97 DCHECK_EQ(klass->GetDexCache(), caller->GetDexCache())
98 << "Compiler only supports inlining calls within the same dex cache";
99 switch (invoke_type) {
100 case kDirect:
101 case kStatic:
102 inlined_method =
Andreas Gampe542451c2016-07-26 09:02:02 -0700103 klass->FindDirectMethod(klass->GetDexCache(), method_index, kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100104 break;
105 case kSuper:
106 case kVirtual:
107 inlined_method =
Andreas Gampe542451c2016-07-26 09:02:02 -0700108 klass->FindVirtualMethod(klass->GetDexCache(), method_index, kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100109 break;
110 default:
111 LOG(FATAL) << "Unimplemented inlined invocation type: " << invoke_type;
112 UNREACHABLE();
113 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100114 }
115
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100116 // Update the dex cache for future lookups. Note that for static methods, this is safe
117 // when the class is being initialized, as the entrypoint for the ArtMethod is at
118 // this point still the resolution trampoline.
Andreas Gampe542451c2016-07-26 09:02:02 -0700119 outer_method->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100120 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100121}
122
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100125 return GetCalleeSaveMethodCaller(
126 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
127}
128
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700130ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800131inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800133 Thread* self, bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700135 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100138 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700141 DCHECK(self->IsExceptionPending());
142 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700143 } else {
144 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700145 }
146 }
147 if (kAccessCheck) {
148 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000149 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150 *slow_path = true;
151 return nullptr; // Failure
152 }
153 mirror::Class* referrer = method->GetDeclaringClass();
154 if (UNLIKELY(!referrer->CanAccess(klass))) {
155 ThrowIllegalAccessErrorClass(referrer, klass);
156 *slow_path = true;
157 return nullptr; // Failure
158 }
159 }
160 if (UNLIKELY(!klass->IsInitialized())) {
161 StackHandleScope<1> hs(self);
162 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
163 // EnsureInitialized (the class initializer) might cause a GC.
164 // may cause us to suspend meaning that another thread may try to
165 // change the allocator while we are stuck in the entrypoints of
166 // an old allocator. Also, the class initialization may fail. To
167 // handle these cases we mark the slow path boolean as true so
168 // that the caller knows to check the allocator type to see if it
169 // has changed and to null-check the return value in case the
170 // initialization fails.
171 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700172 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700173 DCHECK(self->IsExceptionPending());
174 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700175 } else {
176 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700177 }
178 return h_klass.Get();
179 }
180 return klass;
181}
182
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700183ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800184inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
185 Thread* self,
186 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187 if (UNLIKELY(!klass->IsInitialized())) {
188 StackHandleScope<1> hs(self);
189 Handle<mirror::Class> h_class(hs.NewHandle(klass));
190 // EnsureInitialized (the class initializer) might cause a GC.
191 // may cause us to suspend meaning that another thread may try to
192 // change the allocator while we are stuck in the entrypoints of
193 // an old allocator. Also, the class initialization may fail. To
194 // handle these cases we mark the slow path boolean as true so
195 // that the caller knows to check the allocator type to see if it
196 // has changed and to null-check the return value in case the
197 // initialization fails.
198 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700199 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700200 DCHECK(self->IsExceptionPending());
201 return nullptr; // Failure
202 }
203 return h_class.Get();
204 }
205 return klass;
206}
207
208// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
209// cannot be resolved, throw an error. If it can, use it to create an instance.
210// When verification/compiler hasn't been able to verify access, optionally perform an access
211// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700212template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700213ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800214inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800216 Thread* self,
217 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700218 bool slow_path = false;
219 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
220 if (UNLIKELY(slow_path)) {
221 if (klass == nullptr) {
222 return nullptr;
223 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800224 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
225 return klass->Alloc</*kInstrumented*/true>(
226 self,
227 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700228 }
229 DCHECK(klass != nullptr);
230 return klass->Alloc<kInstrumented>(self, allocator_type);
231}
232
233// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700235ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800236inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
237 Thread* self,
238 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239 DCHECK(klass != nullptr);
240 bool slow_path = false;
241 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
242 if (UNLIKELY(slow_path)) {
243 if (klass == nullptr) {
244 return nullptr;
245 }
246 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000247 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800248 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
249 // instrumented.
250 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700251 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000252 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 return klass->Alloc<kInstrumented, false>(self, allocator_type);
254}
255
256// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700258ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800259inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
260 Thread* self,
261 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000263 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264 return klass->Alloc<kInstrumented, false>(self, allocator_type);
265}
266
267
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700268template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700269ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800270inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800271 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800273 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700274 if (UNLIKELY(component_count < 0)) {
275 ThrowNegativeArraySizeException(component_count);
276 *slow_path = true;
277 return nullptr; // Failure
278 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100279 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700280 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100281 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100283 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700284 *slow_path = true;
285 if (klass == nullptr) { // Error
286 DCHECK(Thread::Current()->IsExceptionPending());
287 return nullptr; // Failure
288 }
289 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
290 }
291 if (kAccessCheck) {
292 mirror::Class* referrer = method->GetDeclaringClass();
293 if (UNLIKELY(!referrer->CanAccess(klass))) {
294 ThrowIllegalAccessErrorClass(referrer, klass);
295 *slow_path = true;
296 return nullptr; // Failure
297 }
298 }
299 return klass;
300}
301
302// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
303// it cannot be resolved, throw an error. If it can, use it to create an array.
304// When verification/compiler hasn't been able to verify access, optionally perform an access
305// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700306template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700307ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800308inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800309 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800311 Thread* self,
312 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800314 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700315 &slow_path);
316 if (UNLIKELY(slow_path)) {
317 if (klass == nullptr) {
318 return nullptr;
319 }
320 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800321 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
322 return mirror::Array::Alloc</*kInstrumented*/true>(self,
323 klass,
324 component_count,
325 klass->GetComponentSizeShift(),
326 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700327 }
328 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700329 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700330}
331
332template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700333ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800334inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800335 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800337 Thread* self,
338 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700339 DCHECK(klass != nullptr);
340 if (UNLIKELY(component_count < 0)) {
341 ThrowNegativeArraySizeException(component_count);
342 return nullptr; // Failure
343 }
344 if (kAccessCheck) {
345 mirror::Class* referrer = method->GetDeclaringClass();
346 if (UNLIKELY(!referrer->CanAccess(klass))) {
347 ThrowIllegalAccessErrorClass(referrer, klass);
348 return nullptr; // Failure
349 }
350 }
351 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
352 // suspension.
353 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700354 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700355}
356
357template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800358inline ArtField* FindFieldFromCode(uint32_t field_idx,
359 ArtMethod* referrer,
360 Thread* self,
361 size_t expected_size) REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700362 bool is_primitive;
363 bool is_set;
364 bool is_static;
365 switch (type) {
366 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
367 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
368 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
369 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
370 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
371 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
372 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
373 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
374 default: is_primitive = true; is_set = true; is_static = true; break;
375 }
376 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800377
378 ArtField* resolved_field;
379 if (access_check) {
380 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
381 // qualifying type of a field and the resolved run-time qualifying type of a field differed
382 // in their static-ness.
383 //
384 // In particular, don't assume the dex instruction already correctly knows if the
385 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700386 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800387
388 StackHandleScope<2> hs(self);
389 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
390 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
391
392 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
393 field_idx,
394 h_dex_cache,
395 h_class_loader);
396 } else {
397 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
398 // be executing here if there was a static/non-static mismatch.
399 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
400 }
401
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700402 if (UNLIKELY(resolved_field == nullptr)) {
403 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
404 return nullptr; // Failure.
405 }
406 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
407 if (access_check) {
408 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
409 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
410 return nullptr;
411 }
412 mirror::Class* referring_class = referrer->GetDeclaringClass();
413 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
414 field_idx))) {
415 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
416 return nullptr; // Failure.
417 }
418 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
419 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
420 return nullptr; // Failure.
421 } else {
422 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
423 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000424 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700425 "Attempted read of %zd-bit %s on field '%s'",
426 expected_size * (32 / sizeof(int32_t)),
427 is_primitive ? "primitive" : "non-primitive",
428 PrettyField(resolved_field, true).c_str());
429 return nullptr; // Failure.
430 }
431 }
432 }
433 if (!is_static) {
434 // instance fields must be being accessed on an initialized class
435 return resolved_field;
436 } else {
437 // If the class is initialized we're done.
438 if (LIKELY(fields_class->IsInitialized())) {
439 return resolved_field;
440 } else {
441 StackHandleScope<1> hs(self);
442 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700443 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700444 // Otherwise let's ensure the class is initialized before resolving the field.
445 return resolved_field;
446 }
447 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
448 return nullptr; // Failure.
449 }
450 }
451}
452
453// Explicit template declarations of FindFieldFromCode for all field access types.
454#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700455template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700456ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700457 ArtMethod* referrer, \
458 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700459
460#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
461 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
462 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
463
464EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
465EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
466EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
467EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
468EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
469EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
470EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
471EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
472
473#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
474#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
475
476template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700477inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
Andreas Gampe3a357142015-08-07 17:20:11 -0700478 ArtMethod* referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700479 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700480 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700481 if (resolved_method == nullptr) {
482 StackHandleScope<1> hs(self);
483 mirror::Object* null_this = nullptr;
484 HandleWrapper<mirror::Object> h_this(
485 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800486 constexpr ClassLinker::ResolveMode resolve_mode =
487 access_check ? ClassLinker::kForceICCECheck
488 : ClassLinker::kNoICCECheckForCache;
489 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700490 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700491 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700492 if (UNLIKELY(resolved_method == nullptr)) {
493 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
494 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700495 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700496 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700497 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700498 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
499 resolved_method,
500 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700501 if (UNLIKELY(!can_access_resolved_method)) {
502 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
503 return nullptr; // Failure.
504 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100505 // Incompatible class change should have been handled in resolve method.
506 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
507 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
508 referrer);
509 return nullptr; // Failure.
510 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700511 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700512 // Next, null pointer check.
513 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
514 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
515 resolved_method->IsConstructor())) {
516 // Hack for String init:
517 //
518 // We assume that the input of String.<init> in verified code is always
519 // an unitialized reference. If it is a null constant, it must have been
520 // optimized out by the compiler. Do not throw NullPointerException.
521 } else {
522 // Maintain interpreter-like semantics where NullPointerException is thrown
523 // after potential NoSuchMethodError from class linker.
524 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
525 return nullptr; // Failure.
526 }
527 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700528 switch (type) {
529 case kStatic:
530 case kDirect:
531 return resolved_method;
532 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700533 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700534 uint16_t vtable_index = resolved_method->GetMethodIndex();
535 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700536 (!klass->HasVTable() ||
537 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700538 // Behavior to agree with that of the verifier.
539 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
540 resolved_method->GetName(), resolved_method->GetSignature());
541 return nullptr; // Failure.
542 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700543 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700545 }
546 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700547 // TODO This lookup is quite slow.
548 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
549 // that will actually not be what we want in some cases where there are miranda methods or
550 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
551 // this method is coming from.
552 mirror::Class* referring_class = referrer->GetDeclaringClass();
553 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
554 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
555 if (UNLIKELY(method_reference_class == nullptr)) {
556 // Bad type idx.
557 CHECK(self->IsExceptionPending());
558 return nullptr;
559 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700560 // It is not an interface. If the referring class is in the class hierarchy of the
561 // referenced class in the bytecode, we use its super class. Otherwise, we throw
562 // a NoSuchMethodError.
563 mirror::Class* super_class = nullptr;
564 if (method_reference_class->IsAssignableFrom(referring_class)) {
565 super_class = referring_class->GetSuperClass();
566 }
Alex Light705ad492015-09-21 11:36:30 -0700567 uint16_t vtable_index = resolved_method->GetMethodIndex();
568 if (access_check) {
569 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700570 if (super_class == nullptr ||
571 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700572 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
573 // Behavior to agree with that of the verifier.
574 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
575 resolved_method->GetName(), resolved_method->GetSignature());
576 return nullptr; // Failure.
577 }
578 }
579 DCHECK(super_class != nullptr);
580 DCHECK(super_class->HasVTable());
581 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
582 } else {
583 // It is an interface.
584 if (access_check) {
585 if (!method_reference_class->IsAssignableFrom((*this_object)->GetClass())) {
586 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
587 method_reference_class,
588 *this_object,
589 referrer);
590 return nullptr; // Failure.
591 }
592 }
593 // TODO We can do better than this for a (compiled) fastpath.
594 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
595 resolved_method, class_linker->GetImagePointerSize());
596 // Throw an NSME if nullptr;
597 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700598 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
599 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700600 }
Alex Light705ad492015-09-21 11:36:30 -0700601 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700602 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700603 }
604 case kInterface: {
Matthew Gharrity465ecc82016-07-19 21:32:52 +0000605 uint32_t imt_index = resolved_method->GetImtIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -0700606 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000607 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
608 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000609 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700610 if (kIsDebugBuild) {
611 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700612 ArtMethod* method = klass->FindVirtualMethodForInterface(
613 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700614 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
615 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
616 PrettyClass(klass);
617 }
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) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700638 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700639 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
640 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 Chartierc7853442015-03-27 14:35:38 -0700659 ArtField* resolved_field =
Andreas Gampe542451c2016-07-26 09:02:02 -0700660 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx,
661 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 }
686 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
687 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 Chartiere401d142015-04-22 13:56:20 -0700709inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
710 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700711 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
712 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700713 }
Alex Light705ad492015-09-21 11:36:30 -0700714 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700715 ArtMethod* resolved_method =
Andreas Gampe542451c2016-07-26 09:02:02 -0700716 referring_class->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700717 if (UNLIKELY(resolved_method == nullptr)) {
718 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700719 }
720 if (access_check) {
721 // Check for incompatible class change errors and access.
722 bool icce = resolved_method->CheckIncompatibleClassChange(type);
723 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700724 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700725 }
726 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700727 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
728 !referring_class->CanAccessMember(methods_class,
729 resolved_method->GetAccessFlags()))) {
730 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700731 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700732 }
733 }
734 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700735 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
736 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700737 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700738 return resolved_method;
739 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700740 // TODO This lookup is rather slow.
741 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
742 mirror::Class* method_reference_class =
743 referring_class->GetDexCache()->GetResolvedType(method_type_idx);
744 if (method_reference_class == nullptr) {
745 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000746 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700747 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700748 // It is not an interface. If the referring class is in the class hierarchy of the
749 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
750 // resolve the method.
751 if (!method_reference_class->IsAssignableFrom(referring_class)) {
752 return nullptr;
753 }
754 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700755 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
756 // The super class does not have the method.
757 return nullptr;
758 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700759 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700760 } else {
761 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700762 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000763 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700764 } else {
765 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700767 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700768 }
769}
770
Mathieu Chartiere401d142015-04-22 13:56:20 -0700771inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
772 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700773 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
774 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
775 if (UNLIKELY(klass == nullptr)) {
776 CHECK(self->IsExceptionPending());
777 return nullptr; // Failure - Indicate to caller to deliver exception
778 }
779 // Perform access check if necessary.
780 mirror::Class* referring_class = referrer->GetDeclaringClass();
781 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
782 ThrowIllegalAccessErrorClass(referring_class, klass);
783 return nullptr; // Failure - Indicate to caller to deliver exception
784 }
785 // If we're just implementing const-class, we shouldn't call <clinit>.
786 if (!can_run_clinit) {
787 return klass;
788 }
789 // If we are the <clinit> of this class, just return our storage.
790 //
791 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
792 // running.
793 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
794 return klass;
795 }
796 StackHandleScope<1> hs(self);
797 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700798 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700799 CHECK(self->IsExceptionPending());
800 return nullptr; // Failure - Indicate to caller to deliver exception
801 }
802 return h_class.Get();
803}
804
Mathieu Chartiere401d142015-04-22 13:56:20 -0700805inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700806 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
807 return class_linker->ResolveString(string_idx, referrer);
808}
809
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800810inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700811 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700812 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700813 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000814 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700815 self->ClearException();
816 }
817 // Decode locked object and unlock, before popping local references.
818 self->DecodeJObject(locked)->MonitorExit(self);
819 if (UNLIKELY(self->IsExceptionPending())) {
820 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
821 << saved_exception->Dump()
822 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000823 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700824 }
825 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700826 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000827 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700828 }
829}
830
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700831template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800832inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700833 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
834 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
835 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
836 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
837 if (LIKELY(f > kMinIntAsFloat)) {
838 if (LIKELY(f < kMaxIntAsFloat)) {
839 return static_cast<INT_TYPE>(f);
840 } else {
841 return kMaxInt;
842 }
843 } else {
844 return (f != f) ? 0 : kMinInt; // f != f implies NaN
845 }
846}
847
848} // namespace art
849
850#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_