blob: 99b8805099c8015764fe9a6858692151a5b282cd [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
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700125inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100126 return GetCalleeSaveMethodCaller(
127 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
128}
129
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700131ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800132inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 ArtMethod* method,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700134 Thread* self,
135 bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700137 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100138 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700139 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100140 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700141 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700142 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 DCHECK(self->IsExceptionPending());
144 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700145 } else {
146 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700147 }
148 }
149 if (kAccessCheck) {
150 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000151 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700152 *slow_path = true;
153 return nullptr; // Failure
154 }
Aart Bikdb698f12016-07-25 17:52:22 -0700155 if (UNLIKELY(klass->IsClassClass())) {
156 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible", PrettyDescriptor(klass).c_str());
157 *slow_path = true;
158 return nullptr; // Failure
159 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700160 mirror::Class* referrer = method->GetDeclaringClass();
161 if (UNLIKELY(!referrer->CanAccess(klass))) {
162 ThrowIllegalAccessErrorClass(referrer, klass);
163 *slow_path = true;
164 return nullptr; // Failure
165 }
166 }
167 if (UNLIKELY(!klass->IsInitialized())) {
168 StackHandleScope<1> hs(self);
169 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
170 // EnsureInitialized (the class initializer) might cause a GC.
171 // may cause us to suspend meaning that another thread may try to
172 // change the allocator while we are stuck in the entrypoints of
173 // an old allocator. Also, the class initialization may fail. To
174 // handle these cases we mark the slow path boolean as true so
175 // that the caller knows to check the allocator type to see if it
176 // has changed and to null-check the return value in case the
177 // initialization fails.
178 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700179 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700180 DCHECK(self->IsExceptionPending());
181 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700182 } else {
183 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700184 }
185 return h_klass.Get();
186 }
187 return klass;
188}
189
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700190ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800191inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
192 Thread* self,
193 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700194 if (UNLIKELY(!klass->IsInitialized())) {
195 StackHandleScope<1> hs(self);
196 Handle<mirror::Class> h_class(hs.NewHandle(klass));
197 // EnsureInitialized (the class initializer) might cause a GC.
198 // may cause us to suspend meaning that another thread may try to
199 // change the allocator while we are stuck in the entrypoints of
200 // an old allocator. Also, the class initialization may fail. To
201 // handle these cases we mark the slow path boolean as true so
202 // that the caller knows to check the allocator type to see if it
203 // has changed and to null-check the return value in case the
204 // initialization fails.
205 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700206 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700207 DCHECK(self->IsExceptionPending());
208 return nullptr; // Failure
209 }
210 return h_class.Get();
211 }
212 return klass;
213}
214
215// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
216// cannot be resolved, throw an error. If it can, use it to create an instance.
217// When verification/compiler hasn't been able to verify access, optionally perform an access
218// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700219template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700220ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800221inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800223 Thread* self,
224 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700225 bool slow_path = false;
226 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
227 if (UNLIKELY(slow_path)) {
228 if (klass == nullptr) {
229 return nullptr;
230 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800231 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
232 return klass->Alloc</*kInstrumented*/true>(
233 self,
234 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 }
236 DCHECK(klass != nullptr);
237 return klass->Alloc<kInstrumented>(self, allocator_type);
238}
239
240// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700242ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800243inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
244 Thread* self,
245 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 DCHECK(klass != nullptr);
247 bool slow_path = false;
248 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
249 if (UNLIKELY(slow_path)) {
250 if (klass == nullptr) {
251 return nullptr;
252 }
253 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000254 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800255 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
256 // instrumented.
257 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000259 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700260 return klass->Alloc<kInstrumented, false>(self, allocator_type);
261}
262
263// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700265ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800266inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
267 Thread* self,
268 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700269 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000270 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700271 return klass->Alloc<kInstrumented, false>(self, allocator_type);
272}
273
274
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700275template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700276ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800277inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800278 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800280 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700281 if (UNLIKELY(component_count < 0)) {
282 ThrowNegativeArraySizeException(component_count);
283 *slow_path = true;
284 return nullptr; // Failure
285 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100286 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700287 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100288 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700289 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100290 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700291 *slow_path = true;
292 if (klass == nullptr) { // Error
293 DCHECK(Thread::Current()->IsExceptionPending());
294 return nullptr; // Failure
295 }
296 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
297 }
298 if (kAccessCheck) {
299 mirror::Class* referrer = method->GetDeclaringClass();
300 if (UNLIKELY(!referrer->CanAccess(klass))) {
301 ThrowIllegalAccessErrorClass(referrer, klass);
302 *slow_path = true;
303 return nullptr; // Failure
304 }
305 }
306 return klass;
307}
308
309// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
310// it cannot be resolved, throw an error. If it can, use it to create an array.
311// When verification/compiler hasn't been able to verify access, optionally perform an access
312// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700314ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800315inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800316 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800318 Thread* self,
319 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800321 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700322 &slow_path);
323 if (UNLIKELY(slow_path)) {
324 if (klass == nullptr) {
325 return nullptr;
326 }
327 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800328 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
329 return mirror::Array::Alloc</*kInstrumented*/true>(self,
330 klass,
331 component_count,
332 klass->GetComponentSizeShift(),
333 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700334 }
335 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700336 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700337}
338
339template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700340ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800341inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800342 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800344 Thread* self,
345 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700346 DCHECK(klass != nullptr);
347 if (UNLIKELY(component_count < 0)) {
348 ThrowNegativeArraySizeException(component_count);
349 return nullptr; // Failure
350 }
351 if (kAccessCheck) {
352 mirror::Class* referrer = method->GetDeclaringClass();
353 if (UNLIKELY(!referrer->CanAccess(klass))) {
354 ThrowIllegalAccessErrorClass(referrer, klass);
355 return nullptr; // Failure
356 }
357 }
358 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
359 // suspension.
360 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700361 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700362}
363
364template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800365inline ArtField* FindFieldFromCode(uint32_t field_idx,
366 ArtMethod* referrer,
367 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700368 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700369 bool is_primitive;
370 bool is_set;
371 bool is_static;
372 switch (type) {
373 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
374 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
375 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
376 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
377 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
378 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
379 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
380 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
381 default: is_primitive = true; is_set = true; is_static = true; break;
382 }
383 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800384
385 ArtField* resolved_field;
386 if (access_check) {
387 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
388 // qualifying type of a field and the resolved run-time qualifying type of a field differed
389 // in their static-ness.
390 //
391 // In particular, don't assume the dex instruction already correctly knows if the
392 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700393 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800394
395 StackHandleScope<2> hs(self);
396 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
397 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
398
399 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
400 field_idx,
401 h_dex_cache,
402 h_class_loader);
403 } else {
404 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
405 // be executing here if there was a static/non-static mismatch.
406 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
407 }
408
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700409 if (UNLIKELY(resolved_field == nullptr)) {
410 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
411 return nullptr; // Failure.
412 }
413 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
414 if (access_check) {
415 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
416 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
417 return nullptr;
418 }
419 mirror::Class* referring_class = referrer->GetDeclaringClass();
420 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
421 field_idx))) {
422 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
423 return nullptr; // Failure.
424 }
425 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
426 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
427 return nullptr; // Failure.
428 } else {
429 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
430 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000431 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700432 "Attempted read of %zd-bit %s on field '%s'",
433 expected_size * (32 / sizeof(int32_t)),
434 is_primitive ? "primitive" : "non-primitive",
435 PrettyField(resolved_field, true).c_str());
436 return nullptr; // Failure.
437 }
438 }
439 }
440 if (!is_static) {
441 // instance fields must be being accessed on an initialized class
442 return resolved_field;
443 } else {
444 // If the class is initialized we're done.
445 if (LIKELY(fields_class->IsInitialized())) {
446 return resolved_field;
447 } else {
448 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700449 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700450 // Otherwise let's ensure the class is initialized before resolving the field.
451 return resolved_field;
452 }
453 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
454 return nullptr; // Failure.
455 }
456 }
457}
458
459// Explicit template declarations of FindFieldFromCode for all field access types.
460#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700461template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700462ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 ArtMethod* referrer, \
464 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700465
466#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
467 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
468 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
469
470EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
471EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
472EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
473EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
474EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
475EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
476EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
477EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
478
479#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
480#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
481
482template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700483inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
484 mirror::Object** this_object,
485 ArtMethod* referrer,
486 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700487 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700488 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489 if (resolved_method == nullptr) {
490 StackHandleScope<1> hs(self);
491 mirror::Object* null_this = nullptr;
492 HandleWrapper<mirror::Object> h_this(
493 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800494 constexpr ClassLinker::ResolveMode resolve_mode =
495 access_check ? ClassLinker::kForceICCECheck
496 : ClassLinker::kNoICCECheckForCache;
497 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700498 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700499 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 if (UNLIKELY(resolved_method == nullptr)) {
501 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
502 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700503 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700505 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700506 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
507 resolved_method,
508 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700509 if (UNLIKELY(!can_access_resolved_method)) {
510 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
511 return nullptr; // Failure.
512 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100513 // Incompatible class change should have been handled in resolve method.
514 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
515 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
516 referrer);
517 return nullptr; // Failure.
518 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700519 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700520 // Next, null pointer check.
521 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
522 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
523 resolved_method->IsConstructor())) {
524 // Hack for String init:
525 //
526 // We assume that the input of String.<init> in verified code is always
527 // an unitialized reference. If it is a null constant, it must have been
528 // optimized out by the compiler. Do not throw NullPointerException.
529 } else {
530 // Maintain interpreter-like semantics where NullPointerException is thrown
531 // after potential NoSuchMethodError from class linker.
532 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
533 return nullptr; // Failure.
534 }
535 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700536 switch (type) {
537 case kStatic:
538 case kDirect:
539 return resolved_method;
540 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700541 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700542 uint16_t vtable_index = resolved_method->GetMethodIndex();
543 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700544 (!klass->HasVTable() ||
545 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700546 // Behavior to agree with that of the verifier.
547 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
548 resolved_method->GetName(), resolved_method->GetSignature());
549 return nullptr; // Failure.
550 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700551 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700552 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700553 }
554 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700555 // TODO This lookup is quite slow.
556 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
557 // that will actually not be what we want in some cases where there are miranda methods or
558 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
559 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700560 StackHandleScope<2> hs2(self);
561 HandleWrapper<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
562 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
563 const uint16_t method_type_idx =
564 h_referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700565 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
566 if (UNLIKELY(method_reference_class == nullptr)) {
567 // Bad type idx.
568 CHECK(self->IsExceptionPending());
569 return nullptr;
570 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700571 // It is not an interface. If the referring class is in the class hierarchy of the
572 // referenced class in the bytecode, we use its super class. Otherwise, we throw
573 // a NoSuchMethodError.
574 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700575 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
576 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700577 }
Alex Light705ad492015-09-21 11:36:30 -0700578 uint16_t vtable_index = resolved_method->GetMethodIndex();
579 if (access_check) {
580 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700581 if (super_class == nullptr ||
582 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700583 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
584 // Behavior to agree with that of the verifier.
585 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
586 resolved_method->GetName(), resolved_method->GetSignature());
587 return nullptr; // Failure.
588 }
589 }
590 DCHECK(super_class != nullptr);
591 DCHECK(super_class->HasVTable());
592 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
593 } else {
594 // It is an interface.
595 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700596 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700597 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
598 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700599 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700600 referrer);
601 return nullptr; // Failure.
602 }
603 }
604 // TODO We can do better than this for a (compiled) fastpath.
605 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
606 resolved_method, class_linker->GetImagePointerSize());
607 // Throw an NSME if nullptr;
608 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700609 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
610 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700611 }
Alex Light705ad492015-09-21 11:36:30 -0700612 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700613 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700614 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700615 }
616 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700617 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700618 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000619 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
620 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000621 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700622 if (kIsDebugBuild) {
623 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700624 ArtMethod* method = klass->FindVirtualMethodForInterface(
625 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700626 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
627 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
628 PrettyClass(klass);
629 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 return imt_method;
631 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700632 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
633 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 if (UNLIKELY(interface_method == nullptr)) {
635 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700636 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700637 return nullptr; // Failure.
638 }
639 return interface_method;
640 }
641 }
642 default:
643 LOG(FATAL) << "Unknown invoke type " << type;
644 return nullptr; // Failure.
645 }
646}
647
648// Explicit template declarations of FindMethodFromCode for all invoke types.
649#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700650 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
652 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700653 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700654 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700655#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
656 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
657 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
658
659EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
660EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
661EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
662EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
663EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
664
665#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
666#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
667
668// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700669inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
670 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700671 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700672 ArtField* resolved_field =
Andreas Gampe542451c2016-07-26 09:02:02 -0700673 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx,
674 kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 if (UNLIKELY(resolved_field == nullptr)) {
676 return nullptr;
677 }
678 // Check for incompatible class change.
679 bool is_primitive;
680 bool is_set;
681 bool is_static;
682 switch (type) {
683 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
684 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
685 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
686 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
687 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
688 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
689 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
690 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
691 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700692 LOG(FATAL) << "UNREACHABLE";
693 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700694 }
695 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
696 // Incompatible class change.
697 return nullptr;
698 }
699 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
700 if (is_static) {
701 // Check class is initialized else fail so that we can contend to initialize the class with
702 // other threads that may be racing to do this.
703 if (UNLIKELY(!fields_class->IsInitialized())) {
704 return nullptr;
705 }
706 }
707 mirror::Class* referring_class = referrer->GetDeclaringClass();
708 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700709 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700710 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
711 // Illegal access.
712 return nullptr;
713 }
714 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
715 resolved_field->FieldSize() != expected_size)) {
716 return nullptr;
717 }
718 return resolved_field;
719}
720
721// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700722inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
723 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700724 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700725 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
726 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700727 }
Alex Light705ad492015-09-21 11:36:30 -0700728 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729 ArtMethod* resolved_method =
Andreas Gampe542451c2016-07-26 09:02:02 -0700730 referring_class->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700731 if (UNLIKELY(resolved_method == nullptr)) {
732 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700733 }
734 if (access_check) {
735 // Check for incompatible class change errors and access.
736 bool icce = resolved_method->CheckIncompatibleClassChange(type);
737 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700738 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700739 }
740 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700741 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
742 !referring_class->CanAccessMember(methods_class,
743 resolved_method->GetAccessFlags()))) {
744 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700745 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 }
747 }
748 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700749 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
750 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700751 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700752 return resolved_method;
753 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700754 // TODO This lookup is rather slow.
755 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
756 mirror::Class* method_reference_class =
757 referring_class->GetDexCache()->GetResolvedType(method_type_idx);
758 if (method_reference_class == nullptr) {
759 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000760 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700761 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700762 // It is not an interface. If the referring class is in the class hierarchy of the
763 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
764 // resolve the method.
765 if (!method_reference_class->IsAssignableFrom(referring_class)) {
766 return nullptr;
767 }
768 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700769 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
770 // The super class does not have the method.
771 return nullptr;
772 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700773 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700774 } else {
775 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700776 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000777 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700778 } else {
779 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700780 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700781 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700782 }
783}
784
Mathieu Chartiere401d142015-04-22 13:56:20 -0700785inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
786 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700787 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
788 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
789 if (UNLIKELY(klass == nullptr)) {
790 CHECK(self->IsExceptionPending());
791 return nullptr; // Failure - Indicate to caller to deliver exception
792 }
793 // Perform access check if necessary.
794 mirror::Class* referring_class = referrer->GetDeclaringClass();
795 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
796 ThrowIllegalAccessErrorClass(referring_class, klass);
797 return nullptr; // Failure - Indicate to caller to deliver exception
798 }
799 // If we're just implementing const-class, we shouldn't call <clinit>.
800 if (!can_run_clinit) {
801 return klass;
802 }
803 // If we are the <clinit> of this class, just return our storage.
804 //
805 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
806 // running.
807 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
808 return klass;
809 }
810 StackHandleScope<1> hs(self);
811 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700812 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700813 CHECK(self->IsExceptionPending());
814 return nullptr; // Failure - Indicate to caller to deliver exception
815 }
816 return h_class.Get();
817}
818
Mathieu Chartiere401d142015-04-22 13:56:20 -0700819inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700820 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
821 return class_linker->ResolveString(string_idx, referrer);
822}
823
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800824inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700825 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700826 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700827 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000828 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700829 self->ClearException();
830 }
831 // Decode locked object and unlock, before popping local references.
832 self->DecodeJObject(locked)->MonitorExit(self);
833 if (UNLIKELY(self->IsExceptionPending())) {
834 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
835 << saved_exception->Dump()
836 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000837 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700838 }
839 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700840 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000841 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700842 }
843}
844
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700845template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800846inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700847 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
848 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
849 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
850 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
851 if (LIKELY(f > kMinIntAsFloat)) {
852 if (LIKELY(f < kMaxIntAsFloat)) {
853 return static_cast<INT_TYPE>(f);
854 } else {
855 return kMaxInt;
856 }
857 } else {
858 return (f != f) ? 0 : kMinInt; // f != f implies NaN
859 }
860}
861
862} // namespace art
863
864#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_