blob: 14c9c21356c4749c58717c4806f8faad3eeccf6e [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 Gampea5b09a62016-11-17 15:21:22 -0800132inline mirror::Class* CheckObjectAlloc(dex::TypeIndex 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())) {
David Sehr709b0702016-10-13 09:12:37 -0700151 self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().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())) {
David Sehr709b0702016-10-13 09:12:37 -0700156 ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
157 klass->PrettyDescriptor().c_str());
Aart Bikdb698f12016-07-25 17:52:22 -0700158 *slow_path = true;
159 return nullptr; // Failure
160 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700161 mirror::Class* referrer = method->GetDeclaringClass();
162 if (UNLIKELY(!referrer->CanAccess(klass))) {
163 ThrowIllegalAccessErrorClass(referrer, klass);
164 *slow_path = true;
165 return nullptr; // Failure
166 }
167 }
168 if (UNLIKELY(!klass->IsInitialized())) {
169 StackHandleScope<1> hs(self);
170 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
171 // EnsureInitialized (the class initializer) might cause a GC.
172 // may cause us to suspend meaning that another thread may try to
173 // change the allocator while we are stuck in the entrypoints of
174 // an old allocator. Also, the class initialization may fail. To
175 // handle these cases we mark the slow path boolean as true so
176 // that the caller knows to check the allocator type to see if it
177 // has changed and to null-check the return value in case the
178 // initialization fails.
179 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700180 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700181 DCHECK(self->IsExceptionPending());
182 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700183 } else {
184 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700185 }
186 return h_klass.Get();
187 }
188 return klass;
189}
190
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700191ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800192inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
193 Thread* self,
194 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700195 if (UNLIKELY(!klass->IsInitialized())) {
196 StackHandleScope<1> hs(self);
197 Handle<mirror::Class> h_class(hs.NewHandle(klass));
198 // EnsureInitialized (the class initializer) might cause a GC.
199 // may cause us to suspend meaning that another thread may try to
200 // change the allocator while we are stuck in the entrypoints of
201 // an old allocator. Also, the class initialization may fail. To
202 // handle these cases we mark the slow path boolean as true so
203 // that the caller knows to check the allocator type to see if it
204 // has changed and to null-check the return value in case the
205 // initialization fails.
206 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700207 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208 DCHECK(self->IsExceptionPending());
209 return nullptr; // Failure
210 }
211 return h_class.Get();
212 }
213 return klass;
214}
215
216// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
217// cannot be resolved, throw an error. If it can, use it to create an instance.
218// When verification/compiler hasn't been able to verify access, optionally perform an access
219// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700221ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800222inline mirror::Object* AllocObjectFromCode(dex::TypeIndex type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800224 Thread* self,
225 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226 bool slow_path = false;
227 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
228 if (UNLIKELY(slow_path)) {
229 if (klass == nullptr) {
230 return nullptr;
231 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800232 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
233 return klass->Alloc</*kInstrumented*/true>(
234 self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700235 Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236 }
237 DCHECK(klass != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700238 return klass->Alloc<kInstrumented>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239}
240
241// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700243ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800244inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
245 Thread* self,
246 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 DCHECK(klass != nullptr);
248 bool slow_path = false;
249 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
250 if (UNLIKELY(slow_path)) {
251 if (klass == nullptr) {
252 return nullptr;
253 }
254 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000255 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800256 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
257 // instrumented.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700258 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700259 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000260 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700261 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262}
263
264// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700265template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700266ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800267inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
268 Thread* self,
269 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700270 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000271 // Pass in false since the object cannot be finalizable.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700272 return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700273}
274
275
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700276template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700277ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800278inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800279 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800281 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282 if (UNLIKELY(component_count < 0)) {
283 ThrowNegativeArraySizeException(component_count);
284 *slow_path = true;
285 return nullptr; // Failure
286 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100287 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe542451c2016-07-26 09:02:02 -0700288 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko05792b92015-08-03 11:56:49 +0100289 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700290 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100291 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700292 *slow_path = true;
293 if (klass == nullptr) { // Error
294 DCHECK(Thread::Current()->IsExceptionPending());
295 return nullptr; // Failure
296 }
David Sehr709b0702016-10-13 09:12:37 -0700297 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700298 }
299 if (kAccessCheck) {
300 mirror::Class* referrer = method->GetDeclaringClass();
301 if (UNLIKELY(!referrer->CanAccess(klass))) {
302 ThrowIllegalAccessErrorClass(referrer, klass);
303 *slow_path = true;
304 return nullptr; // Failure
305 }
306 }
307 return klass;
308}
309
310// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
311// it cannot be resolved, throw an error. If it can, use it to create an array.
312// When verification/compiler hasn't been able to verify access, optionally perform an access
313// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700315ALWAYS_INLINE
Andreas Gampea5b09a62016-11-17 15:21:22 -0800316inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800317 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800319 Thread* self,
320 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700321 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800322 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700323 &slow_path);
324 if (UNLIKELY(slow_path)) {
325 if (klass == nullptr) {
326 return nullptr;
327 }
328 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800329 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
330 return mirror::Array::Alloc</*kInstrumented*/true>(self,
331 klass,
332 component_count,
333 klass->GetComponentSizeShift(),
334 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700335 }
336 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700337 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700338}
339
340template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700341ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800342inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800343 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800345 Thread* self,
346 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700347 DCHECK(klass != nullptr);
348 if (UNLIKELY(component_count < 0)) {
349 ThrowNegativeArraySizeException(component_count);
350 return nullptr; // Failure
351 }
352 if (kAccessCheck) {
353 mirror::Class* referrer = method->GetDeclaringClass();
354 if (UNLIKELY(!referrer->CanAccess(klass))) {
355 ThrowIllegalAccessErrorClass(referrer, klass);
356 return nullptr; // Failure
357 }
358 }
359 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
360 // suspension.
361 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700362 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700363}
364
365template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800366inline ArtField* FindFieldFromCode(uint32_t field_idx,
367 ArtMethod* referrer,
368 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700369 size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700370 bool is_primitive;
371 bool is_set;
372 bool is_static;
373 switch (type) {
374 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
375 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
376 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
377 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
378 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
379 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
380 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
381 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
382 default: is_primitive = true; is_set = true; is_static = true; break;
383 }
384 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800385
386 ArtField* resolved_field;
387 if (access_check) {
388 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
389 // qualifying type of a field and the resolved run-time qualifying type of a field differed
390 // in their static-ness.
391 //
392 // In particular, don't assume the dex instruction already correctly knows if the
393 // real field is static or not. The resolution must not be aware of this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700394 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800395
396 StackHandleScope<2> hs(self);
397 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
398 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
399
400 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
401 field_idx,
402 h_dex_cache,
403 h_class_loader);
404 } else {
405 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
406 // be executing here if there was a static/non-static mismatch.
407 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
408 }
409
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700410 if (UNLIKELY(resolved_field == nullptr)) {
411 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
412 return nullptr; // Failure.
413 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700414 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700415 if (access_check) {
416 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
417 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
418 return nullptr;
419 }
420 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700421 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
422 resolved_field,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700423 field_idx))) {
424 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
425 return nullptr; // Failure.
426 }
427 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
428 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
429 return nullptr; // Failure.
430 } else {
431 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
432 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000433 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700434 "Attempted read of %zd-bit %s on field '%s'",
435 expected_size * (32 / sizeof(int32_t)),
436 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700437 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700438 return nullptr; // Failure.
439 }
440 }
441 }
442 if (!is_static) {
443 // instance fields must be being accessed on an initialized class
444 return resolved_field;
445 } else {
446 // If the class is initialized we're done.
447 if (LIKELY(fields_class->IsInitialized())) {
448 return resolved_field;
449 } else {
450 StackHandleScope<1> hs(self);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700451 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700452 // Otherwise let's ensure the class is initialized before resolving the field.
453 return resolved_field;
454 }
455 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
456 return nullptr; // Failure.
457 }
458 }
459}
460
461// Explicit template declarations of FindFieldFromCode for all field access types.
462#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700463template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700464ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 ArtMethod* referrer, \
466 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700467
468#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
469 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
470 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
471
472EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
473EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
474EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
475EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
476EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
477EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
478EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
479EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
480
481#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
482#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
483
484template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700485inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700486 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700487 ArtMethod* referrer,
488 Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700490 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700491 if (resolved_method == nullptr) {
492 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700493 ObjPtr<mirror::Object> null_this = nullptr;
494 HandleWrapperObjPtr<mirror::Object> h_this(
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700495 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800496 constexpr ClassLinker::ResolveMode resolve_mode =
497 access_check ? ClassLinker::kForceICCECheck
498 : ClassLinker::kNoICCECheckForCache;
499 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700501 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700502 if (UNLIKELY(resolved_method == nullptr)) {
503 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
504 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700505 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700506 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700507 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700508 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
509 resolved_method,
510 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700511 if (UNLIKELY(!can_access_resolved_method)) {
512 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
513 return nullptr; // Failure.
514 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100515 // Incompatible class change should have been handled in resolve method.
516 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
517 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
518 referrer);
519 return nullptr; // Failure.
520 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700521 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700522 // Next, null pointer check.
523 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
524 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
525 resolved_method->IsConstructor())) {
526 // Hack for String init:
527 //
528 // We assume that the input of String.<init> in verified code is always
529 // an unitialized reference. If it is a null constant, it must have been
530 // optimized out by the compiler. Do not throw NullPointerException.
531 } else {
532 // Maintain interpreter-like semantics where NullPointerException is thrown
533 // after potential NoSuchMethodError from class linker.
534 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
535 return nullptr; // Failure.
536 }
537 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700538 switch (type) {
539 case kStatic:
540 case kDirect:
541 return resolved_method;
542 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700543 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700544 uint16_t vtable_index = resolved_method->GetMethodIndex();
545 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700546 (!klass->HasVTable() ||
547 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700548 // Behavior to agree with that of the verifier.
549 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
550 resolved_method->GetName(), resolved_method->GetSignature());
551 return nullptr; // Failure.
552 }
David Sehr709b0702016-10-13 09:12:37 -0700553 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700555 }
556 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700557 // TODO This lookup is quite slow.
558 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
559 // that will actually not be what we want in some cases where there are miranda methods or
560 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
561 // this method is coming from.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700562 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700563 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700564 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800565 const dex::TypeIndex method_type_idx =
Alex Lightce77fc02016-12-15 01:05:52 +0000566 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700567 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
568 if (UNLIKELY(method_reference_class == nullptr)) {
569 // Bad type idx.
570 CHECK(self->IsExceptionPending());
571 return nullptr;
572 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700573 // It is not an interface. If the referring class is in the class hierarchy of the
574 // referenced class in the bytecode, we use its super class. Otherwise, we throw
575 // a NoSuchMethodError.
576 mirror::Class* super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700577 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
578 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700579 }
Alex Light705ad492015-09-21 11:36:30 -0700580 uint16_t vtable_index = resolved_method->GetMethodIndex();
581 if (access_check) {
582 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700583 if (super_class == nullptr ||
584 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700585 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
586 // Behavior to agree with that of the verifier.
587 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
588 resolved_method->GetName(), resolved_method->GetSignature());
589 return nullptr; // Failure.
590 }
591 }
592 DCHECK(super_class != nullptr);
593 DCHECK(super_class->HasVTable());
594 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
595 } else {
596 // It is an interface.
597 if (access_check) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700598 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700599 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
600 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700601 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700602 referrer);
603 return nullptr; // Failure.
604 }
605 }
606 // TODO We can do better than this for a (compiled) fastpath.
607 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
608 resolved_method, class_linker->GetImagePointerSize());
609 // Throw an NSME if nullptr;
610 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700611 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
612 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700613 }
Alex Light705ad492015-09-21 11:36:30 -0700614 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700615 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700616 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617 }
618 case kInterface: {
Andreas Gampe75a7db62016-09-26 12:04:26 -0700619 uint32_t imt_index = ImTable::GetImtIndex(resolved_method);
Andreas Gampe542451c2016-07-26 09:02:02 -0700620 PointerSize pointer_size = class_linker->GetImagePointerSize();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000621 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
622 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000623 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700624 if (kIsDebugBuild) {
625 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 ArtMethod* method = klass->FindVirtualMethodForInterface(
627 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700628 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
629 << imt_method->PrettyMethod() << " / "
630 << ArtMethod::PrettyMethod(method) << " / "
631 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700632 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700633 return imt_method;
634 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
636 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700637 if (UNLIKELY(interface_method == nullptr)) {
638 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700639 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640 return nullptr; // Failure.
641 }
642 return interface_method;
643 }
644 }
645 default:
646 LOG(FATAL) << "Unknown invoke type " << type;
647 return nullptr; // Failure.
648 }
649}
650
651// Explicit template declarations of FindMethodFromCode for all invoke types.
652#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700653 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700654 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700655 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700656 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700657 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700658#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
659 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
660 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
661
662EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
663EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
664EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
665EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
666EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
667
668#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
669#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
670
671// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700672inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
673 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700674 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700675 ArtField* resolved_field =
Alex Lightce77fc02016-12-15 01:05:52 +0000676 referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700677 if (UNLIKELY(resolved_field == nullptr)) {
678 return nullptr;
679 }
680 // Check for incompatible class change.
681 bool is_primitive;
682 bool is_set;
683 bool is_static;
684 switch (type) {
685 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
686 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
687 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
688 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
689 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
690 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
691 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
692 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
693 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700694 LOG(FATAL) << "UNREACHABLE";
695 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700696 }
697 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
698 // Incompatible class change.
699 return nullptr;
700 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700701 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700702 if (is_static) {
703 // Check class is initialized else fail so that we can contend to initialize the class with
704 // other threads that may be racing to do this.
705 if (UNLIKELY(!fields_class->IsInitialized())) {
706 return nullptr;
707 }
708 }
709 mirror::Class* referring_class = referrer->GetDeclaringClass();
710 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700711 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700712 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
713 // Illegal access.
714 return nullptr;
715 }
716 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
717 resolved_field->FieldSize() != expected_size)) {
718 return nullptr;
719 }
720 return resolved_field;
721}
722
723// Fast path method resolution that can't throw exceptions.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700724inline ArtMethod* FindMethodFast(uint32_t method_idx,
725 ObjPtr<mirror::Object> this_object,
726 ArtMethod* referrer,
727 bool access_check,
728 InvokeType type) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700729 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700730 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
731 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700732 }
Alex Light705ad492015-09-21 11:36:30 -0700733 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700734 ArtMethod* resolved_method =
Alex Lightce77fc02016-12-15 01:05:52 +0000735 referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700736 if (UNLIKELY(resolved_method == nullptr)) {
737 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700738 }
739 if (access_check) {
740 // Check for incompatible class change errors and access.
741 bool icce = resolved_method->CheckIncompatibleClassChange(type);
742 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700743 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700744 }
745 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
747 !referring_class->CanAccessMember(methods_class,
748 resolved_method->GetAccessFlags()))) {
749 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700750 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700751 }
752 }
753 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700754 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
755 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700756 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700757 return resolved_method;
758 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700759 // TODO This lookup is rather slow.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800760 dex::TypeIndex method_type_idx =
Alex Lightce77fc02016-12-15 01:05:52 +0000761 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Alex Light705ad492015-09-21 11:36:30 -0700762 mirror::Class* method_reference_class =
Alex Lightce77fc02016-12-15 01:05:52 +0000763 referrer->GetDexCache()->GetResolvedType(method_type_idx);
Alex Light705ad492015-09-21 11:36:30 -0700764 if (method_reference_class == nullptr) {
765 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000766 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700767 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700768 // It is not an interface. If the referring class is in the class hierarchy of the
769 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
770 // resolve the method.
771 if (!method_reference_class->IsAssignableFrom(referring_class)) {
772 return nullptr;
773 }
774 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700775 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
776 // The super class does not have the method.
777 return nullptr;
778 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700779 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700780 } else {
781 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700782 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000783 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700784 } else {
785 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700786 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700787 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700788 }
789}
790
Andreas Gampea5b09a62016-11-17 15:21:22 -0800791inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx,
792 ArtMethod* referrer,
793 Thread* self,
794 bool can_run_clinit,
795 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700796 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
797 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
798 if (UNLIKELY(klass == nullptr)) {
799 CHECK(self->IsExceptionPending());
800 return nullptr; // Failure - Indicate to caller to deliver exception
801 }
802 // Perform access check if necessary.
803 mirror::Class* referring_class = referrer->GetDeclaringClass();
804 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
805 ThrowIllegalAccessErrorClass(referring_class, klass);
806 return nullptr; // Failure - Indicate to caller to deliver exception
807 }
808 // If we're just implementing const-class, we shouldn't call <clinit>.
809 if (!can_run_clinit) {
810 return klass;
811 }
812 // If we are the <clinit> of this class, just return our storage.
813 //
814 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
815 // running.
816 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
817 return klass;
818 }
819 StackHandleScope<1> hs(self);
820 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700821 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700822 CHECK(self->IsExceptionPending());
823 return nullptr; // Failure - Indicate to caller to deliver exception
824 }
825 return h_class.Get();
826}
827
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800828inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700829 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
830 return class_linker->ResolveString(string_idx, referrer);
831}
832
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800833inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700834 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700835 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700836 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000837 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700838 self->ClearException();
839 }
840 // Decode locked object and unlock, before popping local references.
841 self->DecodeJObject(locked)->MonitorExit(self);
842 if (UNLIKELY(self->IsExceptionPending())) {
843 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
844 << saved_exception->Dump()
845 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000846 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700847 }
848 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700849 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000850 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700851 }
852}
853
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700854template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800855inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700856 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
857 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
858 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
859 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
860 if (LIKELY(f > kMinIntAsFloat)) {
861 if (LIKELY(f < kMaxIntAsFloat)) {
862 return static_cast<INT_TYPE>(f);
863 } else {
864 return kMaxInt;
865 }
866 } else {
867 return (f != f) ? 0 : kMinInt; // f != f implies NaN
868 }
869}
870
871} // namespace art
872
873#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_