blob: 68c954f02f728b3067c82a9a53e3421c6179669e [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "class_linker-inl.h"
24#include "common_throws.h"
25#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010026#include "entrypoints/quick/callee_save_frame.h"
27#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "indirect_reference_table.h"
29#include "invoke_type.h"
30#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/array.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010035#include "nth_caller_visitor.h"
36#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010037#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038#include "thread.h"
39
40namespace art {
41
Mathieu Chartiere401d142015-04-22 13:56:20 -070042inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010043 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000044 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010045 uint8_t inlining_depth)
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010046 SHARED_REQUIRES(Locks::mutator_lock_) {
47 // This method is being used by artQuickResolutionTrampoline, before it sets up
48 // the passed parameters in a GC friendly way. Therefore we must never be
49 // suspended while executing it.
50 ScopedAssertNoThreadSuspension sants(Thread::Current(), __FUNCTION__);
51
David Srbecky61b28a12016-02-25 21:55:03 +000052 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010053 InvokeType invoke_type = static_cast<InvokeType>(
David Srbecky61b28a12016-02-25 21:55:03 +000054 inline_info.GetInvokeTypeAtDepth(encoding, inlining_depth));
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010055 ArtMethod* inlined_method = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
56 if (!inlined_method->IsRuntimeMethod()) {
57 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070058 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010059
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010060 // The method in the dex cache is the runtime method responsible for invoking
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010061 // the stub that will then update the dex cache. Therefore, we need to do the
62 // resolution ourselves.
Nicolas Geoffray3976e5e2015-06-15 08:58:03 +010063
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010064 // We first find the dex cache of our caller. If it is the outer method, we can directly
65 // use its dex cache. Otherwise, we also need to resolve our caller.
66 ArtMethod* caller = outer_method;
67 if (inlining_depth != 0) {
68 caller = GetResolvedMethod(outer_method,
69 inline_info,
70 encoding,
71 inlining_depth - 1);
72 }
73 DCHECK_EQ(caller->GetDexCache(), outer_method->GetDexCache())
74 << "Compiler only supports inlining calls within the same dex cache";
75 const DexFile* dex_file = outer_method->GetDexFile();
76 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
77
78 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
79 // "charAt" special case. It is the only non-leaf method we inline across dex files.
80 if (kIsDebugBuild) {
81 const char* name = dex_file->StringDataByIdx(method_id.name_idx_);
82 DCHECK_EQ(std::string(name), "charAt");
83 DCHECK_EQ(std::string(dex_file->GetMethodShorty(method_id)), "CI")
84 << std::string(dex_file->GetMethodShorty(method_id));
85 DCHECK_EQ(std::string(dex_file->StringByTypeIdx(method_id.class_idx_)), "Ljava/lang/String;")
86 << std::string(dex_file->StringByTypeIdx(method_id.class_idx_));
87 }
88 mirror::Class* cls =
89 Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kJavaLangString);
90 // Update the dex cache for future lookups.
91 caller->GetDexCache()->SetResolvedType(method_id.class_idx_, cls);
92 inlined_method = cls->FindVirtualMethod("charAt", "(I)C", sizeof(void*));
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010093 } else {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010094 mirror::Class* klass = caller->GetDexCache()->GetResolvedType(method_id.class_idx_);
95 DCHECK_EQ(klass->GetDexCache(), caller->GetDexCache())
96 << "Compiler only supports inlining calls within the same dex cache";
97 switch (invoke_type) {
98 case kDirect:
99 case kStatic:
100 inlined_method =
101 klass->FindDirectMethod(klass->GetDexCache(), method_index, sizeof(void*));
102 break;
103 case kSuper:
104 case kVirtual:
105 inlined_method =
106 klass->FindVirtualMethod(klass->GetDexCache(), method_index, sizeof(void*));
107 break;
108 default:
109 LOG(FATAL) << "Unimplemented inlined invocation type: " << invoke_type;
110 UNREACHABLE();
111 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100112 }
113
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100114 // Update the dex cache for future lookups. Note that for static methods, this is safe
115 // when the class is being initialized, as the entrypoint for the ArtMethod is at
116 // this point still the resolution trampoline.
117 outer_method->SetDexCacheResolvedMethod(method_index, inlined_method, sizeof(void*));
118 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100119}
120
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100123 return GetCalleeSaveMethodCaller(
124 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
125}
126
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700128ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800129inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800131 Thread* self, bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
133 size_t pointer_size = class_linker->GetImagePointerSize();
134 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 DCHECK(self->IsExceptionPending());
140 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700141 } else {
142 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 }
144 }
145 if (kAccessCheck) {
146 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000147 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700148 *slow_path = true;
149 return nullptr; // Failure
150 }
Aart Bikdb698f12016-07-25 17:52:22 -0700151 if (UNLIKELY(klass->IsClassClass())) {
152 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible", PrettyDescriptor(klass).c_str());
153 *slow_path = true;
154 return nullptr; // Failure
155 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700156 mirror::Class* referrer = method->GetDeclaringClass();
157 if (UNLIKELY(!referrer->CanAccess(klass))) {
158 ThrowIllegalAccessErrorClass(referrer, klass);
159 *slow_path = true;
160 return nullptr; // Failure
161 }
162 }
163 if (UNLIKELY(!klass->IsInitialized())) {
164 StackHandleScope<1> hs(self);
165 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
166 // EnsureInitialized (the class initializer) might cause a GC.
167 // may cause us to suspend meaning that another thread may try to
168 // change the allocator while we are stuck in the entrypoints of
169 // an old allocator. Also, the class initialization may fail. To
170 // handle these cases we mark the slow path boolean as true so
171 // that the caller knows to check the allocator type to see if it
172 // has changed and to null-check the return value in case the
173 // initialization fails.
174 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700175 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700176 DCHECK(self->IsExceptionPending());
177 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700178 } else {
179 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700180 }
181 return h_klass.Get();
182 }
183 return klass;
184}
185
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700186ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800187inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
188 Thread* self,
189 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700190 if (UNLIKELY(!klass->IsInitialized())) {
191 StackHandleScope<1> hs(self);
192 Handle<mirror::Class> h_class(hs.NewHandle(klass));
193 // EnsureInitialized (the class initializer) might cause a GC.
194 // may cause us to suspend meaning that another thread may try to
195 // change the allocator while we are stuck in the entrypoints of
196 // an old allocator. Also, the class initialization may fail. To
197 // handle these cases we mark the slow path boolean as true so
198 // that the caller knows to check the allocator type to see if it
199 // has changed and to null-check the return value in case the
200 // initialization fails.
201 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700202 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203 DCHECK(self->IsExceptionPending());
204 return nullptr; // Failure
205 }
206 return h_class.Get();
207 }
208 return klass;
209}
210
211// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
212// cannot be resolved, throw an error. If it can, use it to create an instance.
213// When verification/compiler hasn't been able to verify access, optionally perform an access
214// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700215template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700216ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800217inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800219 Thread* self,
220 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221 bool slow_path = false;
222 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
223 if (UNLIKELY(slow_path)) {
224 if (klass == nullptr) {
225 return nullptr;
226 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800227 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
228 return klass->Alloc</*kInstrumented*/true>(
229 self,
230 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231 }
232 DCHECK(klass != nullptr);
233 return klass->Alloc<kInstrumented>(self, allocator_type);
234}
235
236// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700238ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800239inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
240 Thread* self,
241 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 DCHECK(klass != nullptr);
243 bool slow_path = false;
244 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
245 if (UNLIKELY(slow_path)) {
246 if (klass == nullptr) {
247 return nullptr;
248 }
249 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000250 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800251 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
252 // instrumented.
253 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000255 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 return klass->Alloc<kInstrumented, false>(self, allocator_type);
257}
258
259// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700260template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700261ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800262inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
263 Thread* self,
264 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700265 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000266 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 return klass->Alloc<kInstrumented, false>(self, allocator_type);
268}
269
270
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700271template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700272ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800273inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800274 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800276 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277 if (UNLIKELY(component_count < 0)) {
278 ThrowNegativeArraySizeException(component_count);
279 *slow_path = true;
280 return nullptr; // Failure
281 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 size_t pointer_size = class_linker->GetImagePointerSize();
284 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700285 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100286 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700287 *slow_path = true;
288 if (klass == nullptr) { // Error
289 DCHECK(Thread::Current()->IsExceptionPending());
290 return nullptr; // Failure
291 }
292 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
293 }
294 if (kAccessCheck) {
295 mirror::Class* referrer = method->GetDeclaringClass();
296 if (UNLIKELY(!referrer->CanAccess(klass))) {
297 ThrowIllegalAccessErrorClass(referrer, klass);
298 *slow_path = true;
299 return nullptr; // Failure
300 }
301 }
302 return klass;
303}
304
305// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
306// it cannot be resolved, throw an error. If it can, use it to create an array.
307// When verification/compiler hasn't been able to verify access, optionally perform an access
308// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700309template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700310ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800311inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800312 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800314 Thread* self,
315 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700316 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800317 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700318 &slow_path);
319 if (UNLIKELY(slow_path)) {
320 if (klass == nullptr) {
321 return nullptr;
322 }
323 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800324 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
325 return mirror::Array::Alloc</*kInstrumented*/true>(self,
326 klass,
327 component_count,
328 klass->GetComponentSizeShift(),
329 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700330 }
331 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700332 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700333}
334
335template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700336ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800337inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800338 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800340 Thread* self,
341 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700342 DCHECK(klass != nullptr);
343 if (UNLIKELY(component_count < 0)) {
344 ThrowNegativeArraySizeException(component_count);
345 return nullptr; // Failure
346 }
347 if (kAccessCheck) {
348 mirror::Class* referrer = method->GetDeclaringClass();
349 if (UNLIKELY(!referrer->CanAccess(klass))) {
350 ThrowIllegalAccessErrorClass(referrer, klass);
351 return nullptr; // Failure
352 }
353 }
354 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
355 // suspension.
356 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700357 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700358}
359
360template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800361inline ArtField* FindFieldFromCode(uint32_t field_idx,
362 ArtMethod* referrer,
363 Thread* self,
364 size_t expected_size) REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 bool is_primitive;
366 bool is_set;
367 bool is_static;
368 switch (type) {
369 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
370 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
371 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
372 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
373 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
374 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
375 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
376 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
377 default: is_primitive = true; is_set = true; is_static = true; break;
378 }
379 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800380
381 ArtField* resolved_field;
382 if (access_check) {
383 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
384 // qualifying type of a field and the resolved run-time qualifying type of a field differed
385 // in their static-ness.
386 //
387 // In particular, don't assume the dex instruction already correctly knows if the
388 // real field is static or not. The resolution must not be aware of this.
389 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(sizeof(void*));
390
391 StackHandleScope<2> hs(self);
392 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
393 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
394
395 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
396 field_idx,
397 h_dex_cache,
398 h_class_loader);
399 } else {
400 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
401 // be executing here if there was a static/non-static mismatch.
402 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
403 }
404
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700405 if (UNLIKELY(resolved_field == nullptr)) {
406 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
407 return nullptr; // Failure.
408 }
409 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
410 if (access_check) {
411 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
412 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
413 return nullptr;
414 }
415 mirror::Class* referring_class = referrer->GetDeclaringClass();
416 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
417 field_idx))) {
418 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
419 return nullptr; // Failure.
420 }
421 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
422 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
423 return nullptr; // Failure.
424 } else {
425 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
426 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000427 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700428 "Attempted read of %zd-bit %s on field '%s'",
429 expected_size * (32 / sizeof(int32_t)),
430 is_primitive ? "primitive" : "non-primitive",
431 PrettyField(resolved_field, true).c_str());
432 return nullptr; // Failure.
433 }
434 }
435 }
436 if (!is_static) {
437 // instance fields must be being accessed on an initialized class
438 return resolved_field;
439 } else {
440 // If the class is initialized we're done.
441 if (LIKELY(fields_class->IsInitialized())) {
442 return resolved_field;
443 } else {
444 StackHandleScope<1> hs(self);
445 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700446 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700447 // Otherwise let's ensure the class is initialized before resolving the field.
448 return resolved_field;
449 }
450 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
451 return nullptr; // Failure.
452 }
453 }
454}
455
456// Explicit template declarations of FindFieldFromCode for all field access types.
457#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700458template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700459ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460 ArtMethod* referrer, \
461 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700462
463#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
464 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
465 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
466
467EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
468EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
469EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
470EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
471EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
472EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
473EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
474EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
475
476#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
477#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
478
479template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700480inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
Andreas Gampe3a357142015-08-07 17:20:11 -0700481 ArtMethod* referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700482 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700483 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700484 if (resolved_method == nullptr) {
485 StackHandleScope<1> hs(self);
486 mirror::Object* null_this = nullptr;
487 HandleWrapper<mirror::Object> h_this(
488 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800489 constexpr ClassLinker::ResolveMode resolve_mode =
490 access_check ? ClassLinker::kForceICCECheck
491 : ClassLinker::kNoICCECheckForCache;
492 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700493 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700494 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700495 if (UNLIKELY(resolved_method == nullptr)) {
496 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
497 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700498 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700499 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700501 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
502 resolved_method,
503 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 if (UNLIKELY(!can_access_resolved_method)) {
505 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
506 return nullptr; // Failure.
507 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100508 // Incompatible class change should have been handled in resolve method.
509 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
510 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
511 referrer);
512 return nullptr; // Failure.
513 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700514 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700515 // Next, null pointer check.
516 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
517 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
518 resolved_method->IsConstructor())) {
519 // Hack for String init:
520 //
521 // We assume that the input of String.<init> in verified code is always
522 // an unitialized reference. If it is a null constant, it must have been
523 // optimized out by the compiler. Do not throw NullPointerException.
524 } else {
525 // Maintain interpreter-like semantics where NullPointerException is thrown
526 // after potential NoSuchMethodError from class linker.
527 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
528 return nullptr; // Failure.
529 }
530 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700531 switch (type) {
532 case kStatic:
533 case kDirect:
534 return resolved_method;
535 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700536 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700537 uint16_t vtable_index = resolved_method->GetMethodIndex();
538 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700539 (!klass->HasVTable() ||
540 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700541 // Behavior to agree with that of the verifier.
542 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
543 resolved_method->GetName(), resolved_method->GetSignature());
544 return nullptr; // Failure.
545 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700546 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700547 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700548 }
549 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700550 // TODO This lookup is quite slow.
551 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
552 // that will actually not be what we want in some cases where there are miranda methods or
553 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
554 // this method is coming from.
555 mirror::Class* referring_class = referrer->GetDeclaringClass();
556 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
557 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
558 if (UNLIKELY(method_reference_class == nullptr)) {
559 // Bad type idx.
560 CHECK(self->IsExceptionPending());
561 return nullptr;
562 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700563 // It is not an interface. If the referring class is in the class hierarchy of the
564 // referenced class in the bytecode, we use its super class. Otherwise, we throw
565 // a NoSuchMethodError.
566 mirror::Class* super_class = nullptr;
567 if (method_reference_class->IsAssignableFrom(referring_class)) {
568 super_class = referring_class->GetSuperClass();
569 }
Alex Light705ad492015-09-21 11:36:30 -0700570 uint16_t vtable_index = resolved_method->GetMethodIndex();
571 if (access_check) {
572 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700573 if (super_class == nullptr ||
574 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700575 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
576 // Behavior to agree with that of the verifier.
577 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
578 resolved_method->GetName(), resolved_method->GetSignature());
579 return nullptr; // Failure.
580 }
581 }
582 DCHECK(super_class != nullptr);
583 DCHECK(super_class->HasVTable());
584 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
585 } else {
586 // It is an interface.
587 if (access_check) {
588 if (!method_reference_class->IsAssignableFrom((*this_object)->GetClass())) {
589 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
590 method_reference_class,
591 *this_object,
592 referrer);
593 return nullptr; // Failure.
594 }
595 }
596 // TODO We can do better than this for a (compiled) fastpath.
597 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
598 resolved_method, class_linker->GetImagePointerSize());
599 // Throw an NSME if nullptr;
600 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700601 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
602 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700603 }
Alex Light705ad492015-09-21 11:36:30 -0700604 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700605 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700606 }
607 case kInterface: {
Matthew Gharrity465ecc82016-07-19 21:32:52 +0000608 uint32_t imt_index = resolved_method->GetImtIndex();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000609 size_t pointer_size = class_linker->GetImagePointerSize();
610 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
611 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000612 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700613 if (kIsDebugBuild) {
614 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700615 ArtMethod* method = klass->FindVirtualMethodForInterface(
616 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700617 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
618 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
619 PrettyClass(klass);
620 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700621 return imt_method;
622 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700623 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
624 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700625 if (UNLIKELY(interface_method == nullptr)) {
626 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700627 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700628 return nullptr; // Failure.
629 }
630 return interface_method;
631 }
632 }
633 default:
634 LOG(FATAL) << "Unknown invoke type " << type;
635 return nullptr; // Failure.
636 }
637}
638
639// Explicit template declarations of FindMethodFromCode for all invoke types.
640#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700641 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
643 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700644 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700646#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
647 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
648 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
649
650EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
651EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
652EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
653EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
654EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
655
656#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
657#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
658
659// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700660inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
661 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700662 ArtField* resolved_field =
663 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700664 if (UNLIKELY(resolved_field == nullptr)) {
665 return nullptr;
666 }
667 // Check for incompatible class change.
668 bool is_primitive;
669 bool is_set;
670 bool is_static;
671 switch (type) {
672 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
673 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
674 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
675 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
676 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
677 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
678 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
679 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
680 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700681 LOG(FATAL) << "UNREACHABLE";
682 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700683 }
684 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
685 // Incompatible class change.
686 return nullptr;
687 }
688 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
689 if (is_static) {
690 // Check class is initialized else fail so that we can contend to initialize the class with
691 // other threads that may be racing to do this.
692 if (UNLIKELY(!fields_class->IsInitialized())) {
693 return nullptr;
694 }
695 }
696 mirror::Class* referring_class = referrer->GetDeclaringClass();
697 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700699 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
700 // Illegal access.
701 return nullptr;
702 }
703 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
704 resolved_field->FieldSize() != expected_size)) {
705 return nullptr;
706 }
707 return resolved_field;
708}
709
710// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700711inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
712 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700713 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
714 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700715 }
Alex Light705ad492015-09-21 11:36:30 -0700716 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700717 ArtMethod* resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700718 referring_class->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700719 if (UNLIKELY(resolved_method == nullptr)) {
720 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700721 }
722 if (access_check) {
723 // Check for incompatible class change errors and access.
724 bool icce = resolved_method->CheckIncompatibleClassChange(type);
725 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700726 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700727 }
728 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700729 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
730 !referring_class->CanAccessMember(methods_class,
731 resolved_method->GetAccessFlags()))) {
732 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700733 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700734 }
735 }
736 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700737 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700738 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700739 return resolved_method;
740 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700741 // TODO This lookup is rather slow.
742 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
743 mirror::Class* method_reference_class =
744 referring_class->GetDexCache()->GetResolvedType(method_type_idx);
745 if (method_reference_class == nullptr) {
746 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000747 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700748 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700749 // It is not an interface. If the referring class is in the class hierarchy of the
750 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
751 // resolve the method.
752 if (!method_reference_class->IsAssignableFrom(referring_class)) {
753 return nullptr;
754 }
755 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700756 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
757 // The super class does not have the method.
758 return nullptr;
759 }
760 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), sizeof(void*));
761 } else {
762 return method_reference_class->FindVirtualMethodForInterfaceSuper(
763 resolved_method, sizeof(void*));
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000764 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700765 } else {
766 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700767 return this_object->GetClass()->GetVTableEntry(
768 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700769 }
770}
771
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
773 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700774 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
775 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
776 if (UNLIKELY(klass == nullptr)) {
777 CHECK(self->IsExceptionPending());
778 return nullptr; // Failure - Indicate to caller to deliver exception
779 }
780 // Perform access check if necessary.
781 mirror::Class* referring_class = referrer->GetDeclaringClass();
782 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
783 ThrowIllegalAccessErrorClass(referring_class, klass);
784 return nullptr; // Failure - Indicate to caller to deliver exception
785 }
786 // If we're just implementing const-class, we shouldn't call <clinit>.
787 if (!can_run_clinit) {
788 return klass;
789 }
790 // If we are the <clinit> of this class, just return our storage.
791 //
792 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
793 // running.
794 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
795 return klass;
796 }
797 StackHandleScope<1> hs(self);
798 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700799 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700800 CHECK(self->IsExceptionPending());
801 return nullptr; // Failure - Indicate to caller to deliver exception
802 }
803 return h_class.Get();
804}
805
Mathieu Chartiere401d142015-04-22 13:56:20 -0700806inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700807 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
808 return class_linker->ResolveString(string_idx, referrer);
809}
810
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800811inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700812 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700813 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700814 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000815 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700816 self->ClearException();
817 }
818 // Decode locked object and unlock, before popping local references.
819 self->DecodeJObject(locked)->MonitorExit(self);
820 if (UNLIKELY(self->IsExceptionPending())) {
821 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
822 << saved_exception->Dump()
823 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000824 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700825 }
826 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700827 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000828 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700829 }
830}
831
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700832template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800833inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700834 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
835 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
836 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
837 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
838 if (LIKELY(f > kMinIntAsFloat)) {
839 if (LIKELY(f < kMaxIntAsFloat)) {
840 return static_cast<INT_TYPE>(f);
841 } else {
842 return kMaxInt;
843 }
844 } else {
845 return (f != f) ? 0 : kMinInt; // f != f implies NaN
846 }
847}
848
849} // namespace art
850
851#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_