blob: 5ff95b480f70b2f44d78bd6655ef6bb16d91a849 [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19
20#include "entrypoint_utils.h"
21
Matthew Gharrity465ecc82016-07-19 21:32:52 +000022#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "class_linker-inl.h"
25#include "common_throws.h"
26#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010027#include "entrypoints/quick/callee_save_frame.h"
28#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070029#include "imt_conflict_table.h"
30#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "indirect_reference_table.h"
32#include "invoke_type.h"
33#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "mirror/array.h"
35#include "mirror/class-inl.h"
36#include "mirror/object-inl.h"
37#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010038#include "nth_caller_visitor.h"
39#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010040#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070041#include "thread.h"
42
43namespace art {
44
Mathieu Chartiere401d142015-04-22 13:56:20 -070045inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010046 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000047 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010048 uint8_t inlining_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070049 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010050 // This method is being used by artQuickResolutionTrampoline, before it sets up
51 // the passed parameters in a GC friendly way. Therefore we must never be
52 // suspended while executing it.
Mathieu Chartier268764d2016-09-13 12:09:38 -070053 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010054
David Srbecky61b28a12016-02-25 21:55:03 +000055 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010056 InvokeType invoke_type = static_cast<InvokeType>(
David Srbecky61b28a12016-02-25 21:55:03 +000057 inline_info.GetInvokeTypeAtDepth(encoding, inlining_depth));
Andreas Gampe542451c2016-07-26 09:02:02 -070058 ArtMethod* inlined_method = outer_method->GetDexCacheResolvedMethod(method_index,
59 kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010060 if (!inlined_method->IsRuntimeMethod()) {
61 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070062 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010063
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010064 // The method in the dex cache is the runtime method responsible for invoking
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010065 // the stub that will then update the dex cache. Therefore, we need to do the
66 // resolution ourselves.
Nicolas Geoffray3976e5e2015-06-15 08:58:03 +010067
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010068 // We first find the dex cache of our caller. If it is the outer method, we can directly
69 // use its dex cache. Otherwise, we also need to resolve our caller.
70 ArtMethod* caller = outer_method;
71 if (inlining_depth != 0) {
72 caller = GetResolvedMethod(outer_method,
73 inline_info,
74 encoding,
75 inlining_depth - 1);
76 }
77 DCHECK_EQ(caller->GetDexCache(), outer_method->GetDexCache())
78 << "Compiler only supports inlining calls within the same dex cache";
79 const DexFile* dex_file = outer_method->GetDexFile();
80 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
81
82 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
83 // "charAt" special case. It is the only non-leaf method we inline across dex files.
84 if (kIsDebugBuild) {
85 const char* name = dex_file->StringDataByIdx(method_id.name_idx_);
86 DCHECK_EQ(std::string(name), "charAt");
87 DCHECK_EQ(std::string(dex_file->GetMethodShorty(method_id)), "CI")
88 << std::string(dex_file->GetMethodShorty(method_id));
89 DCHECK_EQ(std::string(dex_file->StringByTypeIdx(method_id.class_idx_)), "Ljava/lang/String;")
90 << std::string(dex_file->StringByTypeIdx(method_id.class_idx_));
91 }
92 mirror::Class* cls =
93 Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kJavaLangString);
94 // Update the dex cache for future lookups.
95 caller->GetDexCache()->SetResolvedType(method_id.class_idx_, cls);
Andreas Gampe542451c2016-07-26 09:02:02 -070096 inlined_method = cls->FindVirtualMethod("charAt", "(I)C", kRuntimePointerSize);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010097 } else {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010098 mirror::Class* klass = caller->GetDexCache()->GetResolvedType(method_id.class_idx_);
99 DCHECK_EQ(klass->GetDexCache(), caller->GetDexCache())
100 << "Compiler only supports inlining calls within the same dex cache";
101 switch (invoke_type) {
102 case kDirect:
103 case kStatic:
104 inlined_method =
Andreas Gampe542451c2016-07-26 09:02:02 -0700105 klass->FindDirectMethod(klass->GetDexCache(), method_index, kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100106 break;
107 case kSuper:
108 case kVirtual:
109 inlined_method =
Andreas Gampe542451c2016-07-26 09:02:02 -0700110 klass->FindVirtualMethod(klass->GetDexCache(), method_index, kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100111 break;
112 default:
113 LOG(FATAL) << "Unimplemented inlined invocation type: " << invoke_type;
114 UNREACHABLE();
115 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100116 }
117
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100118 // Update the dex cache for future lookups. Note that for static methods, this is safe
119 // when the class is being initialized, as the entrypoint for the ArtMethod is at
120 // this point still the resolution trampoline.
Andreas Gampe542451c2016-07-26 09:02:02 -0700121 outer_method->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100122 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100123}
124
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000125ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass,
126 Thread* self,
127 bool* slow_path)
128 REQUIRES_SHARED(Locks::mutator_lock_)
129 REQUIRES(!Roles::uninterruptible_) {
130 if (UNLIKELY(!klass->IsInstantiable())) {
131 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000133 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700134 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000135 if (UNLIKELY(klass->IsClassClass())) {
136 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
137 klass->PrettyDescriptor().c_str());
138 *slow_path = true;
139 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700140 }
141 if (UNLIKELY(!klass->IsInitialized())) {
142 StackHandleScope<1> hs(self);
143 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
144 // EnsureInitialized (the class initializer) might cause a GC.
145 // may cause us to suspend meaning that another thread may try to
146 // change the allocator while we are stuck in the entrypoints of
147 // an old allocator. Also, the class initialization may fail. To
148 // handle these cases we mark the slow path boolean as true so
149 // that the caller knows to check the allocator type to see if it
150 // has changed and to null-check the return value in case the
151 // initialization fails.
152 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700153 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700154 DCHECK(self->IsExceptionPending());
155 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700156 } else {
157 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700158 }
159 return h_klass.Get();
160 }
161 return klass;
162}
163
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700164ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800165inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
166 Thread* self,
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000167 bool* slow_path)
168 REQUIRES_SHARED(Locks::mutator_lock_)
169 REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700170 if (UNLIKELY(!klass->IsInitialized())) {
171 StackHandleScope<1> hs(self);
172 Handle<mirror::Class> h_class(hs.NewHandle(klass));
173 // EnsureInitialized (the class initializer) might cause a GC.
174 // may cause us to suspend meaning that another thread may try to
175 // change the allocator while we are stuck in the entrypoints of
176 // an old allocator. Also, the class initialization may fail. To
177 // handle these cases we mark the slow path boolean as true so
178 // that the caller knows to check the allocator type to see if it
179 // has changed and to null-check the return value in case the
180 // initialization fails.
181 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700182 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700183 DCHECK(self->IsExceptionPending());
184 return nullptr; // Failure
185 }
186 return h_class.Get();
187 }
188 return klass;
189}
190
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000191// Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
192// or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
193template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700194ALWAYS_INLINE
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000195inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800196 Thread* self,
197 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700198 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000199 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700200 if (UNLIKELY(slow_path)) {
201 if (klass == nullptr) {
202 return nullptr;
203 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800204 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
205 return klass->Alloc</*kInstrumented*/true>(
206 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700207 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208 }
209 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700210 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700211}
212
213// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700214template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700215ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800216inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
217 Thread* self,
218 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700219 DCHECK(klass != nullptr);
220 bool slow_path = false;
221 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
222 if (UNLIKELY(slow_path)) {
223 if (klass == nullptr) {
224 return nullptr;
225 }
226 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000227 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800228 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
229 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700230 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000232 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700233 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234}
235
236// Given the context of a calling Method and an initialized 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* AllocObjectFromCodeInitialized(mirror::Class* klass,
240 Thread* self,
241 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000243 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700244 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700245}
246
247
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700248template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700249ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800250inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800251 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800253 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254 if (UNLIKELY(component_count < 0)) {
255 ThrowNegativeArraySizeException(component_count);
256 *slow_path = true;
257 return nullptr; // Failure
258 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100259 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700260 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100261 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100263 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264 *slow_path = true;
265 if (klass == nullptr) { // Error
266 DCHECK(Thread::Current()->IsExceptionPending());
267 return nullptr; // Failure
268 }
David Sehr709b0702016-10-13 09:12:37 -0700269 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700270 }
271 if (kAccessCheck) {
272 mirror::Class* referrer = method->GetDeclaringClass();
273 if (UNLIKELY(!referrer->CanAccess(klass))) {
274 ThrowIllegalAccessErrorClass(referrer, klass);
275 *slow_path = true;
276 return nullptr; // Failure
277 }
278 }
279 return klass;
280}
281
282// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
283// it cannot be resolved, throw an error. If it can, use it to create an array.
284// When verification/compiler hasn't been able to verify access, optionally perform an access
285// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700286template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700287ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800288inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800289 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800291 Thread* self,
292 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700293 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800294 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700295 &slow_path);
296 if (UNLIKELY(slow_path)) {
297 if (klass == nullptr) {
298 return nullptr;
299 }
300 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800301 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
302 return mirror::Array::Alloc</*kInstrumented*/true>(self,
303 klass,
304 component_count,
305 klass->GetComponentSizeShift(),
306 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307 }
308 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700309 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700310}
311
312template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700313ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800314inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800315 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800317 Thread* self,
318 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700319 DCHECK(klass != nullptr);
320 if (UNLIKELY(component_count < 0)) {
321 ThrowNegativeArraySizeException(component_count);
322 return nullptr; // Failure
323 }
324 if (kAccessCheck) {
325 mirror::Class* referrer = method->GetDeclaringClass();
326 if (UNLIKELY(!referrer->CanAccess(klass))) {
327 ThrowIllegalAccessErrorClass(referrer, klass);
328 return nullptr; // Failure
329 }
330 }
331 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
332 // suspension.
333 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700334 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700335}
336
337template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800338inline ArtField* FindFieldFromCode(uint32_t field_idx,
339 ArtMethod* referrer,
340 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700341 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700342 bool is_primitive;
343 bool is_set;
344 bool is_static;
345 switch (type) {
346 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
347 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
348 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
349 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
350 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
351 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
352 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
353 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
354 default: is_primitive = true; is_set = true; is_static = true; break;
355 }
356 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800357
358 ArtField* resolved_field;
359 if (access_check) {
360 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
361 // qualifying type of a field and the resolved run-time qualifying type of a field differed
362 // in their static-ness.
363 //
364 // In particular, don't assume the dex instruction already correctly knows if the
365 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700366 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800367
368 StackHandleScope<2> hs(self);
369 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
370 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
371
372 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
373 field_idx,
374 h_dex_cache,
375 h_class_loader);
376 } else {
377 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
378 // be executing here if there was a static/non-static mismatch.
379 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
380 }
381
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700382 if (UNLIKELY(resolved_field == nullptr)) {
383 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
384 return nullptr; // Failure.
385 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700386 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700387 if (access_check) {
388 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
389 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
390 return nullptr;
391 }
392 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700393 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
394 resolved_field,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700395 field_idx))) {
396 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
397 return nullptr; // Failure.
398 }
399 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
400 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
401 return nullptr; // Failure.
402 } else {
403 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
404 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000405 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700406 "Attempted read of %zd-bit %s on field '%s'",
407 expected_size * (32 / sizeof(int32_t)),
408 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700409 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700410 return nullptr; // Failure.
411 }
412 }
413 }
414 if (!is_static) {
415 // instance fields must be being accessed on an initialized class
416 return resolved_field;
417 } else {
418 // If the class is initialized we're done.
419 if (LIKELY(fields_class->IsInitialized())) {
420 return resolved_field;
421 } else {
422 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700423 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700424 // Otherwise let's ensure the class is initialized before resolving the field.
425 return resolved_field;
426 }
427 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
428 return nullptr; // Failure.
429 }
430 }
431}
432
433// Explicit template declarations of FindFieldFromCode for all field access types.
434#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700435template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700436ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 ArtMethod* referrer, \
438 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700439
440#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
441 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
442 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
443
444EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
445EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
446EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
447EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
448EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
449EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
450EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
451EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
452
453#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
454#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
455
456template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700457inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700458 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700459 ArtMethod* referrer,
460 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700461 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700462 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700463 if (resolved_method == nullptr) {
464 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700465 ObjPtr<mirror::Object> null_this = nullptr;
466 HandleWrapperObjPtr<mirror::Object> h_this(
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700467 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800468 constexpr ClassLinker::ResolveMode resolve_mode =
469 access_check ? ClassLinker::kForceICCECheck
470 : ClassLinker::kNoICCECheckForCache;
471 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700472 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700473 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700474 if (UNLIKELY(resolved_method == nullptr)) {
475 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
476 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700477 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700478 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700479 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700480 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
481 resolved_method,
482 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700483 if (UNLIKELY(!can_access_resolved_method)) {
484 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
485 return nullptr; // Failure.
486 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100487 // Incompatible class change should have been handled in resolve method.
488 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
489 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
490 referrer);
491 return nullptr; // Failure.
492 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700493 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700494 // Next, null pointer check.
495 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
496 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
497 resolved_method->IsConstructor())) {
498 // Hack for String init:
499 //
500 // We assume that the input of String.<init> in verified code is always
501 // an unitialized reference. If it is a null constant, it must have been
502 // optimized out by the compiler. Do not throw NullPointerException.
503 } else {
504 // Maintain interpreter-like semantics where NullPointerException is thrown
505 // after potential NoSuchMethodError from class linker.
506 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
507 return nullptr; // Failure.
508 }
509 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700510 switch (type) {
511 case kStatic:
512 case kDirect:
513 return resolved_method;
514 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700515 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700516 uint16_t vtable_index = resolved_method->GetMethodIndex();
517 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700518 (!klass->HasVTable() ||
519 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700520 // Behavior to agree with that of the verifier.
521 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
522 resolved_method->GetName(), resolved_method->GetSignature());
523 return nullptr; // Failure.
524 }
David Sehr709b0702016-10-13 09:12:37 -0700525 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700526 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700527 }
528 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700529 // TODO This lookup is quite slow.
530 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
531 // that will actually not be what we want in some cases where there are miranda methods or
532 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
533 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700534 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700535 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700536 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800537 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800538 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700539 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
540 if (UNLIKELY(method_reference_class == nullptr)) {
541 // Bad type idx.
542 CHECK(self->IsExceptionPending());
543 return nullptr;
544 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700545 // It is not an interface. If the referring class is in the class hierarchy of the
546 // referenced class in the bytecode, we use its super class. Otherwise, we throw
547 // a NoSuchMethodError.
548 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700549 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
550 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700551 }
Alex Light705ad492015-09-21 11:36:30 -0700552 uint16_t vtable_index = resolved_method->GetMethodIndex();
553 if (access_check) {
554 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700555 if (super_class == nullptr ||
556 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700557 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
558 // Behavior to agree with that of the verifier.
559 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
560 resolved_method->GetName(), resolved_method->GetSignature());
561 return nullptr; // Failure.
562 }
563 }
564 DCHECK(super_class != nullptr);
565 DCHECK(super_class->HasVTable());
566 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
567 } else {
568 // It is an interface.
569 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700570 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700571 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
572 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700573 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700574 referrer);
575 return nullptr; // Failure.
576 }
577 }
578 // TODO We can do better than this for a (compiled) fastpath.
579 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
580 resolved_method, class_linker->GetImagePointerSize());
581 // Throw an NSME if nullptr;
582 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700583 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
584 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700585 }
Alex Light705ad492015-09-21 11:36:30 -0700586 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700587 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700588 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700589 }
590 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700591 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700592 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000593 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
594 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000595 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700596 if (kIsDebugBuild) {
597 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598 ArtMethod* method = klass->FindVirtualMethodForInterface(
599 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700600 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
601 << imt_method->PrettyMethod() << " / "
602 << ArtMethod::PrettyMethod(method) << " / "
603 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700604 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700605 return imt_method;
606 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700607 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
608 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700609 if (UNLIKELY(interface_method == nullptr)) {
610 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700611 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612 return nullptr; // Failure.
613 }
614 return interface_method;
615 }
616 }
617 default:
618 LOG(FATAL) << "Unknown invoke type " << type;
619 return nullptr; // Failure.
620 }
621}
622
623// Explicit template declarations of FindMethodFromCode for all invoke types.
624#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700625 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700627 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700628 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
631 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
632 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
633
634EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
635EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
636EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
637EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
638EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
639
640#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
641#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
642
643// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700644inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
645 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700646 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700647 ArtField* resolved_field =
Alex Lightdba61482016-12-21 08:20:29 -0800648 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700649 if (UNLIKELY(resolved_field == nullptr)) {
650 return nullptr;
651 }
652 // Check for incompatible class change.
653 bool is_primitive;
654 bool is_set;
655 bool is_static;
656 switch (type) {
657 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
658 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
659 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
660 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
661 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
662 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
663 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
664 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
665 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700666 LOG(FATAL) << "UNREACHABLE";
667 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700668 }
669 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
670 // Incompatible class change.
671 return nullptr;
672 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700673 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 if (is_static) {
675 // Check class is initialized else fail so that we can contend to initialize the class with
676 // other threads that may be racing to do this.
677 if (UNLIKELY(!fields_class->IsInitialized())) {
678 return nullptr;
679 }
680 }
681 mirror::Class* referring_class = referrer->GetDeclaringClass();
682 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700683 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700684 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
685 // Illegal access.
686 return nullptr;
687 }
688 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
689 resolved_field->FieldSize() != expected_size)) {
690 return nullptr;
691 }
692 return resolved_field;
693}
694
695// Fast path method resolution that can't throw exceptions.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700696inline ArtMethod* FindMethodFast(uint32_t method_idx,
697 ObjPtr<mirror::Object> this_object,
698 ArtMethod* referrer,
699 bool access_check,
700 InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700701 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700702 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
703 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700704 }
Alex Light705ad492015-09-21 11:36:30 -0700705 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700706 ArtMethod* resolved_method =
Alex Lightdba61482016-12-21 08:20:29 -0800707 referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700708 if (UNLIKELY(resolved_method == nullptr)) {
709 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700710 }
711 if (access_check) {
712 // Check for incompatible class change errors and access.
713 bool icce = resolved_method->CheckIncompatibleClassChange(type);
714 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700715 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700716 }
717 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700718 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
719 !referring_class->CanAccessMember(methods_class,
720 resolved_method->GetAccessFlags()))) {
721 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700722 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700723 }
724 }
725 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700726 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
727 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700728 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700729 return resolved_method;
730 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700731 // TODO This lookup is rather slow.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800732 dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800733 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700734 mirror::Class* method_reference_class =
Alex Lightdba61482016-12-21 08:20:29 -0800735 referrer->GetDexCache()->GetResolvedType(method_type_idx);
Alex Light705ad492015-09-21 11:36:30 -0700736 if (method_reference_class == nullptr) {
737 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000738 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700739 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700740 // It is not an interface. If the referring class is in the class hierarchy of the
741 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
742 // resolve the method.
743 if (!method_reference_class->IsAssignableFrom(referring_class)) {
744 return nullptr;
745 }
746 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700747 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
748 // The super class does not have the method.
749 return nullptr;
750 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700751 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700752 } else {
753 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700754 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000755 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700756 } else {
757 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700758 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700759 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700760 }
761}
762
Andreas Gampea5b09a62016-11-17 15:21:22 -0800763inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
764 ArtMethod* referrer,
765 Thread* self,
766 bool can_run_clinit,
767 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700768 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
769 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
770 if (UNLIKELY(klass == nullptr)) {
771 CHECK(self->IsExceptionPending());
772 return nullptr; // Failure - Indicate to caller to deliver exception
773 }
774 // Perform access check if necessary.
775 mirror::Class* referring_class = referrer->GetDeclaringClass();
776 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
777 ThrowIllegalAccessErrorClass(referring_class, klass);
778 return nullptr; // Failure - Indicate to caller to deliver exception
779 }
780 // If we're just implementing const-class, we shouldn't call <clinit>.
781 if (!can_run_clinit) {
782 return klass;
783 }
784 // If we are the <clinit> of this class, just return our storage.
785 //
786 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
787 // running.
788 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
789 return klass;
790 }
791 StackHandleScope<1> hs(self);
792 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700793 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700794 CHECK(self->IsExceptionPending());
795 return nullptr; // Failure - Indicate to caller to deliver exception
796 }
797 return h_class.Get();
798}
799
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800800inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700801 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
802 return class_linker->ResolveString(string_idx, referrer);
803}
804
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800805inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700806 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700807 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700808 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000809 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700810 self->ClearException();
811 }
812 // Decode locked object and unlock, before popping local references.
813 self->DecodeJObject(locked)->MonitorExit(self);
814 if (UNLIKELY(self->IsExceptionPending())) {
815 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
816 << saved_exception->Dump()
817 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000818 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819 }
820 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700821 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000822 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700823 }
824}
825
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700826template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800827inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700828 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
829 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
830 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
831 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
832 if (LIKELY(f > kMinIntAsFloat)) {
833 if (LIKELY(f < kMaxIntAsFloat)) {
834 return static_cast<INT_TYPE>(f);
835 } else {
836 return kMaxInt;
837 }
838 } else {
839 return (f != f) ? 0 : kMinInt; // f != f implies NaN
840 }
841}
842
843} // namespace art
844
845#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_