blob: dbd24a276f1cd16f67a1499f15f5b78f998c0fce [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "class_linker-inl.h"
24#include "common_throws.h"
25#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010026#include "entrypoints/quick/callee_save_frame.h"
27#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "indirect_reference_table.h"
29#include "invoke_type.h"
30#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/array.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010035#include "nth_caller_visitor.h"
36#include "runtime.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037#include "thread.h"
38
39namespace art {
40
Mathieu Chartiere401d142015-04-22 13:56:20 -070041inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
42 uint32_t method_index,
43 InvokeType invoke_type)
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010044 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070045 ArtMethod* caller = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010046 if (!caller->IsRuntimeMethod()) {
47 return caller;
48 }
49
50 // The method in the dex cache can be the runtime method responsible for invoking
51 // the stub that will then update the dex cache. Therefore, we need to do the
52 // resolution ourselves.
53
Mathieu Chartiere401d142015-04-22 13:56:20 -070054 StackHandleScope<2> hs(Thread::Current());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010055 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(outer_method->GetClassLoader()));
57 Handle<mirror::DexCache> dex_cache(hs.NewHandle(outer_method->GetDexCache()));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010058 return class_linker->ResolveMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 *outer_method->GetDexFile(), method_index, dex_cache, class_loader, nullptr, invoke_type);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010060}
61
Mathieu Chartiere401d142015-04-22 13:56:20 -070062inline ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
63 Runtime::CalleeSaveType type,
64 bool do_caller_check = false)
Vladimir Marko5ea536a2015-04-20 20:11:30 +010065 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
Vladimir Marko5ea536a2015-04-20 20:11:30 +010067
68 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 auto** caller_sp = reinterpret_cast<ArtMethod**>(
70 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
71 ArtMethod* outer_method = *caller_sp;
72 ArtMethod* caller = outer_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010073
74 if ((outer_method != nullptr) && outer_method->IsOptimized(sizeof(void*))) {
75 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
76 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
77 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
78 uintptr_t native_pc_offset = outer_method->NativeQuickPcOffset(caller_pc);
79 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
80 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
81 DCHECK(stack_map.IsValid());
82 if (stack_map.HasInlineInfo(code_info)) {
83 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map);
84 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inline_info.GetDepth() - 1);
85 InvokeType invoke_type = static_cast<InvokeType>(
86 inline_info.GetInvokeTypeAtDepth(inline_info.GetDepth() - 1));
87 caller = GetResolvedMethod(outer_method, method_index, invoke_type);
88 }
89 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +010090
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010091 if (kIsDebugBuild && do_caller_check) {
92 // Note that do_caller_check is optional, as this method can be called by
93 // stubs, and tests without a proper call stack.
94 NthCallerVisitor visitor(Thread::Current(), 1, true);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010095 visitor.WalkStack();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010096 CHECK_EQ(caller, visitor.caller);
Vladimir Marko5ea536a2015-04-20 20:11:30 +010097 }
98
99 return caller;
100}
101
Mathieu Chartiere401d142015-04-22 13:56:20 -0700102inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
104 return GetCalleeSaveMethodCaller(
105 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
106}
107
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700108template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700109ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800110inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800112 Thread* self, bool* slow_path) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700113 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700114 if (UNLIKELY(klass == nullptr)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700115 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
116 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118 DCHECK(self->IsExceptionPending());
119 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700120 } else {
121 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700122 }
123 }
124 if (kAccessCheck) {
125 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000126 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127 *slow_path = true;
128 return nullptr; // Failure
129 }
130 mirror::Class* referrer = method->GetDeclaringClass();
131 if (UNLIKELY(!referrer->CanAccess(klass))) {
132 ThrowIllegalAccessErrorClass(referrer, klass);
133 *slow_path = true;
134 return nullptr; // Failure
135 }
136 }
137 if (UNLIKELY(!klass->IsInitialized())) {
138 StackHandleScope<1> hs(self);
139 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
140 // EnsureInitialized (the class initializer) might cause a GC.
141 // may cause us to suspend meaning that another thread may try to
142 // change the allocator while we are stuck in the entrypoints of
143 // an old allocator. Also, the class initialization may fail. To
144 // handle these cases we mark the slow path boolean as true so
145 // that the caller knows to check the allocator type to see if it
146 // has changed and to null-check the return value in case the
147 // initialization fails.
148 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700149 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150 DCHECK(self->IsExceptionPending());
151 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700152 } else {
153 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700154 }
155 return h_klass.Get();
156 }
157 return klass;
158}
159
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700160ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800161inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
162 Thread* self,
163 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700164 if (UNLIKELY(!klass->IsInitialized())) {
165 StackHandleScope<1> hs(self);
166 Handle<mirror::Class> h_class(hs.NewHandle(klass));
167 // EnsureInitialized (the class initializer) might cause a GC.
168 // may cause us to suspend meaning that another thread may try to
169 // change the allocator while we are stuck in the entrypoints of
170 // an old allocator. Also, the class initialization may fail. To
171 // handle these cases we mark the slow path boolean as true so
172 // that the caller knows to check the allocator type to see if it
173 // has changed and to null-check the return value in case the
174 // initialization fails.
175 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700176 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700177 DCHECK(self->IsExceptionPending());
178 return nullptr; // Failure
179 }
180 return h_class.Get();
181 }
182 return klass;
183}
184
185// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
186// cannot be resolved, throw an error. If it can, use it to create an instance.
187// When verification/compiler hasn't been able to verify access, optionally perform an access
188// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700189template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700190ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800191inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800193 Thread* self,
194 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700195 bool slow_path = false;
196 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
197 if (UNLIKELY(slow_path)) {
198 if (klass == nullptr) {
199 return nullptr;
200 }
201 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
202 }
203 DCHECK(klass != nullptr);
204 return klass->Alloc<kInstrumented>(self, allocator_type);
205}
206
207// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700209ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800210inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
211 Thread* self,
212 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700213 DCHECK(klass != nullptr);
214 bool slow_path = false;
215 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
216 if (UNLIKELY(slow_path)) {
217 if (klass == nullptr) {
218 return nullptr;
219 }
220 gc::Heap* heap = Runtime::Current()->GetHeap();
221 // Pass in false since the object can not be finalizable.
222 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
223 }
224 // Pass in false since the object can not be finalizable.
225 return klass->Alloc<kInstrumented, false>(self, allocator_type);
226}
227
228// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700230ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800231inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
232 Thread* self,
233 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 DCHECK(klass != nullptr);
235 // Pass in false since the object can not be finalizable.
236 return klass->Alloc<kInstrumented, false>(self, allocator_type);
237}
238
239
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700241ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800242inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800243 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800245 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 if (UNLIKELY(component_count < 0)) {
247 ThrowNegativeArraySizeException(component_count);
248 *slow_path = true;
249 return nullptr; // Failure
250 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700251 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
253 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
254 *slow_path = true;
255 if (klass == nullptr) { // Error
256 DCHECK(Thread::Current()->IsExceptionPending());
257 return nullptr; // Failure
258 }
259 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
260 }
261 if (kAccessCheck) {
262 mirror::Class* referrer = method->GetDeclaringClass();
263 if (UNLIKELY(!referrer->CanAccess(klass))) {
264 ThrowIllegalAccessErrorClass(referrer, klass);
265 *slow_path = true;
266 return nullptr; // Failure
267 }
268 }
269 return klass;
270}
271
272// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
273// it cannot be resolved, throw an error. If it can, use it to create an array.
274// When verification/compiler hasn't been able to verify access, optionally perform an access
275// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700276template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700277ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800278inline mirror::Array* AllocArrayFromCode(uint32_t 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 Thread* self,
282 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700283 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800284 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700285 &slow_path);
286 if (UNLIKELY(slow_path)) {
287 if (klass == nullptr) {
288 return nullptr;
289 }
290 gc::Heap* heap = Runtime::Current()->GetHeap();
291 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700292 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700293 heap->GetCurrentAllocator());
294 }
295 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700296 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700297}
298
299template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700300ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800301inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800302 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800304 Thread* self,
305 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700306 DCHECK(klass != nullptr);
307 if (UNLIKELY(component_count < 0)) {
308 ThrowNegativeArraySizeException(component_count);
309 return nullptr; // Failure
310 }
311 if (kAccessCheck) {
312 mirror::Class* referrer = method->GetDeclaringClass();
313 if (UNLIKELY(!referrer->CanAccess(klass))) {
314 ThrowIllegalAccessErrorClass(referrer, klass);
315 return nullptr; // Failure
316 }
317 }
318 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
319 // suspension.
320 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700321 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700322}
323
324template<FindFieldType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325inline ArtField* FindFieldFromCode(uint32_t field_idx, ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800326 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700327 bool is_primitive;
328 bool is_set;
329 bool is_static;
330 switch (type) {
331 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
332 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
333 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
334 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
335 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
336 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
337 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
338 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
339 default: is_primitive = true; is_set = true; is_static = true; break;
340 }
341 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700342 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700343 if (UNLIKELY(resolved_field == nullptr)) {
344 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
345 return nullptr; // Failure.
346 }
347 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
348 if (access_check) {
349 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
350 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
351 return nullptr;
352 }
353 mirror::Class* referring_class = referrer->GetDeclaringClass();
354 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
355 field_idx))) {
356 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
357 return nullptr; // Failure.
358 }
359 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
360 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
361 return nullptr; // Failure.
362 } else {
363 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
364 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000365 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700366 "Attempted read of %zd-bit %s on field '%s'",
367 expected_size * (32 / sizeof(int32_t)),
368 is_primitive ? "primitive" : "non-primitive",
369 PrettyField(resolved_field, true).c_str());
370 return nullptr; // Failure.
371 }
372 }
373 }
374 if (!is_static) {
375 // instance fields must be being accessed on an initialized class
376 return resolved_field;
377 } else {
378 // If the class is initialized we're done.
379 if (LIKELY(fields_class->IsInitialized())) {
380 return resolved_field;
381 } else {
382 StackHandleScope<1> hs(self);
383 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700384 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700385 // Otherwise let's ensure the class is initialized before resolving the field.
386 return resolved_field;
387 }
388 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
389 return nullptr; // Failure.
390 }
391 }
392}
393
394// Explicit template declarations of FindFieldFromCode for all field access types.
395#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
396template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700397ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 ArtMethod* referrer, \
399 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700400
401#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
402 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
403 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
404
405EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
406EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
407EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
408EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
409EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
410EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
411EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
412EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
413
414#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
415#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
416
417template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
419 ArtMethod** referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700420 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700421 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700422 if (resolved_method == nullptr) {
423 StackHandleScope<1> hs(self);
424 mirror::Object* null_this = nullptr;
425 HandleWrapper<mirror::Object> h_this(
426 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 resolved_method = class_linker->ResolveMethod(self, method_idx, *referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700428 }
429 if (UNLIKELY(resolved_method == nullptr)) {
430 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
431 return nullptr; // Failure.
432 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
433 // Maintain interpreter-like semantics where NullPointerException is thrown
434 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000435 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700436 return nullptr; // Failure.
437 } else if (access_check) {
438 // Incompatible class change should have been handled in resolve method.
439 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
440 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
441 *referrer);
442 return nullptr; // Failure.
443 }
444 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
445 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
446 bool can_access_resolved_method =
447 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
448 method_idx);
449 if (UNLIKELY(!can_access_resolved_method)) {
450 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
451 return nullptr; // Failure.
452 }
453 }
454 switch (type) {
455 case kStatic:
456 case kDirect:
457 return resolved_method;
458 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700459 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 uint16_t vtable_index = resolved_method->GetMethodIndex();
461 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700462 (!klass->HasVTable() ||
463 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700464 // Behavior to agree with that of the verifier.
465 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
466 resolved_method->GetName(), resolved_method->GetSignature());
467 return nullptr; // Failure.
468 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700469 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700471 }
472 case kSuper: {
473 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
474 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700475 if (access_check) {
476 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700477 if (super_class == nullptr || !super_class->HasVTable() ||
478 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700479 // Behavior to agree with that of the verifier.
480 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
481 resolved_method->GetName(), resolved_method->GetSignature());
482 return nullptr; // Failure.
483 }
484 } else {
485 // Super class must exist.
486 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700487 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700488 DCHECK(super_class->HasVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700489 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700490 }
491 case kInterface: {
492 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700493 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
494 imt_index, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700495 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
496 if (kIsDebugBuild) {
497 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700498 ArtMethod* method = klass->FindVirtualMethodForInterface(
499 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700500 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
501 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
502 PrettyClass(klass);
503 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 return imt_method;
505 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
507 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700508 if (UNLIKELY(interface_method == nullptr)) {
509 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
510 *this_object, *referrer);
511 return nullptr; // Failure.
512 }
513 return interface_method;
514 }
515 }
516 default:
517 LOG(FATAL) << "Unknown invoke type " << type;
518 return nullptr; // Failure.
519 }
520}
521
522// Explicit template declarations of FindMethodFromCode for all invoke types.
523#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
524 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700525 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
526 mirror::Object** this_object, \
527 ArtMethod** referrer, \
528 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700529#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
530 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
531 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
532
533EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
534EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
535EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
536EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
537EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
538
539#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
540#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
541
542// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700543inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
544 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700545 ArtField* resolved_field =
546 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700547 if (UNLIKELY(resolved_field == nullptr)) {
548 return nullptr;
549 }
550 // Check for incompatible class change.
551 bool is_primitive;
552 bool is_set;
553 bool is_static;
554 switch (type) {
555 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
556 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
557 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
558 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
559 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
560 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
561 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
562 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
563 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700564 LOG(FATAL) << "UNREACHABLE";
565 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700566 }
567 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
568 // Incompatible class change.
569 return nullptr;
570 }
571 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
572 if (is_static) {
573 // Check class is initialized else fail so that we can contend to initialize the class with
574 // other threads that may be racing to do this.
575 if (UNLIKELY(!fields_class->IsInitialized())) {
576 return nullptr;
577 }
578 }
579 mirror::Class* referring_class = referrer->GetDeclaringClass();
580 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700581 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700582 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
583 // Illegal access.
584 return nullptr;
585 }
586 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
587 resolved_field->FieldSize() != expected_size)) {
588 return nullptr;
589 }
590 return resolved_field;
591}
592
593// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
595 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700596 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
597 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700598 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 ArtMethod* resolved_method =
600 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700601 if (UNLIKELY(resolved_method == nullptr)) {
602 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700603 }
604 if (access_check) {
605 // Check for incompatible class change errors and access.
606 bool icce = resolved_method->CheckIncompatibleClassChange(type);
607 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700608 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700609 }
610 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
611 mirror::Class* referring_class = referrer->GetDeclaringClass();
612 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
613 !referring_class->CanAccessMember(methods_class,
614 resolved_method->GetAccessFlags()))) {
615 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700616 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617 }
618 }
619 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700621 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700622 return resolved_method;
623 } else if (type == kSuper) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700624 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
625 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700626 } else {
627 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700628 return this_object->GetClass()->GetVTableEntry(
629 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 }
631}
632
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
634 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700635 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
636 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
637 if (UNLIKELY(klass == nullptr)) {
638 CHECK(self->IsExceptionPending());
639 return nullptr; // Failure - Indicate to caller to deliver exception
640 }
641 // Perform access check if necessary.
642 mirror::Class* referring_class = referrer->GetDeclaringClass();
643 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
644 ThrowIllegalAccessErrorClass(referring_class, klass);
645 return nullptr; // Failure - Indicate to caller to deliver exception
646 }
647 // If we're just implementing const-class, we shouldn't call <clinit>.
648 if (!can_run_clinit) {
649 return klass;
650 }
651 // If we are the <clinit> of this class, just return our storage.
652 //
653 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
654 // running.
655 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
656 return klass;
657 }
658 StackHandleScope<1> hs(self);
659 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700660 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700661 CHECK(self->IsExceptionPending());
662 return nullptr; // Failure - Indicate to caller to deliver exception
663 }
664 return h_class.Get();
665}
666
Mathieu Chartiere401d142015-04-22 13:56:20 -0700667inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700668 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
669 return class_linker->ResolveString(string_idx, referrer);
670}
671
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800672inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700673 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700674 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000676 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700677 self->ClearException();
678 }
679 // Decode locked object and unlock, before popping local references.
680 self->DecodeJObject(locked)->MonitorExit(self);
681 if (UNLIKELY(self->IsExceptionPending())) {
682 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
683 << saved_exception->Dump()
684 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000685 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700686 }
687 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000689 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700690 }
691}
692
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700693template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800694inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700695 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
696 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
697 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
698 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
699 if (LIKELY(f > kMinIntAsFloat)) {
700 if (LIKELY(f < kMaxIntAsFloat)) {
701 return static_cast<INT_TYPE>(f);
702 } else {
703 return kMaxInt;
704 }
705 } else {
706 return (f != f) ? 0 : kMinInt; // f != f implies NaN
707 }
708}
709
710} // namespace art
711
712#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_