blob: e52a8fbc3337e76b391964d68d070f71eee91864 [file] [log] [blame]
Elliott Hughes0f3c5532012-03-30 14:51:51 -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 */
buzbee54330722011-08-23 16:46:55 -070016
Ian Rogers7655f292013-07-29 11:07:13 -070017#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
Ian Rogers450dcb52013-09-20 17:36:02 -070019
20#include "base/macros.h"
Ian Rogers98379392014-02-24 16:53:16 -080021#include "class_linker-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070022#include "common_throws.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070023#include "dex_file.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070024#include "indirect_reference_table.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070025#include "invoke_type.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070026#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/array.h"
Ian Rogers693ff612013-02-01 10:56:12 -080029#include "mirror/class-inl.h"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080030#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/throwable.h"
Ian Rogers450dcb52013-09-20 17:36:02 -070032#include "object_utils.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080033#include "sirt_ref-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070034#include "thread.h"
35
Shih-wei Liao2d831012011-09-28 22:06:53 -070036namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070037
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038namespace mirror {
Ian Rogers848871b2013-08-05 10:56:33 -070039 class Class;
Brian Carlstromea46f952013-07-30 01:26:50 -070040 class ArtField;
Ian Rogers848871b2013-08-05 10:56:33 -070041 class Object;
42} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070043
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080044// TODO: Fix no thread safety analysis when GCC can handle template specialization.
45template <const bool kAccessCheck>
46ALWAYS_INLINE static inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
47 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080048 Thread* self, bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080049 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070050 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070051 if (UNLIKELY(klass == NULL)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080052 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080053 *slow_path = true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070054 if (klass == NULL) {
55 DCHECK(self->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080056 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070057 }
58 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080059 if (kAccessCheck) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070060 if (UNLIKELY(!klass->IsInstantiable())) {
61 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
62 self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;",
63 PrettyDescriptor(klass).c_str());
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080064 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080065 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070066 }
67 mirror::Class* referrer = method->GetDeclaringClass();
68 if (UNLIKELY(!referrer->CanAccess(klass))) {
69 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080070 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080071 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070072 }
73 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080074 if (UNLIKELY(!klass->IsInitialized())) {
75 SirtRef<mirror::Class> sirt_klass(self, klass);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080076 // EnsureInitialized (the class initializer) might cause a GC.
77 // may cause us to suspend meaning that another thread may try to
78 // change the allocator while we are stuck in the entrypoints of
79 // an old allocator. Also, the class initialization may fail. To
80 // handle these cases we mark the slow path boolean as true so
81 // that the caller knows to check the allocator type to see if it
82 // has changed and to null-check the return value in case the
83 // initialization fails.
84 *slow_path = true;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080085 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080086 DCHECK(self->IsExceptionPending());
87 return nullptr; // Failure
88 }
89 return sirt_klass.get();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070090 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080091 return klass;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070092}
93
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080094// TODO: Fix no thread safety analysis when annotalysis is smarter.
95ALWAYS_INLINE static inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
96 Thread* self, bool* slow_path)
97 NO_THREAD_SAFETY_ANALYSIS {
98 if (UNLIKELY(!klass->IsInitialized())) {
99 SirtRef<mirror::Class> sirt_class(self, klass);
100 // EnsureInitialized (the class initializer) might cause a GC.
101 // may cause us to suspend meaning that another thread may try to
102 // change the allocator while we are stuck in the entrypoints of
103 // an old allocator. Also, the class initialization may fail. To
104 // handle these cases we mark the slow path boolean as true so
105 // that the caller knows to check the allocator type to see if it
106 // has changed and to null-check the return value in case the
107 // initialization fails.
108 *slow_path = true;
109 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_class, true, true)) {
110 DCHECK(self->IsExceptionPending());
111 return nullptr; // Failure
112 }
113 return sirt_class.get();
114 }
115 return klass;
116}
117
Ian Rogers57b86d42012-03-27 16:05:41 -0700118// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
119// cannot be resolved, throw an error. If it can, use it to create an instance.
120// When verification/compiler hasn't been able to verify access, optionally perform an access
121// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800122// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
123template <bool kAccessCheck, bool kInstrumented>
124ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
125 mirror::ArtMethod* method,
126 Thread* self,
127 gc::AllocatorType allocator_type)
128 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800129 bool slow_path = false;
130 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
131 if (UNLIKELY(slow_path)) {
132 if (klass == nullptr) {
133 return nullptr;
134 }
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700135 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700136 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137 DCHECK(klass != nullptr);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800138 return klass->Alloc<kInstrumented>(self, allocator_type);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700139}
140
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800141// Given the context of a calling Method and a resolved class, create an instance.
142// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
143template <bool kInstrumented>
144ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
145 mirror::ArtMethod* method,
146 Thread* self,
147 gc::AllocatorType allocator_type)
148 NO_THREAD_SAFETY_ANALYSIS {
149 DCHECK(klass != nullptr);
150 bool slow_path = false;
151 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
152 if (UNLIKELY(slow_path)) {
153 if (klass == nullptr) {
154 return nullptr;
155 }
156 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700157 // Pass in false since the object can not be finalizable.
158 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800159 }
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700160 // Pass in false since the object can not be finalizable.
161 return klass->Alloc<kInstrumented, false>(self, allocator_type);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800162}
163
164// Given the context of a calling Method and an initialized class, create an instance.
165// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
166template <bool kInstrumented>
167ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
168 mirror::ArtMethod* method,
169 Thread* self,
170 gc::AllocatorType allocator_type)
171 NO_THREAD_SAFETY_ANALYSIS {
172 DCHECK(klass != nullptr);
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700173 // Pass in false since the object can not be finalizable.
174 return klass->Alloc<kInstrumented, false>(self, allocator_type);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800175}
176
177
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800178// TODO: Fix no thread safety analysis when GCC can handle template specialization.
179template <bool kAccessCheck>
180ALWAYS_INLINE static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
181 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800182 int32_t component_count,
183 bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800184 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700185 if (UNLIKELY(component_count < 0)) {
186 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800187 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800188 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700189 }
190 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800191 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700192 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800193 *slow_path = true;
194 if (klass == nullptr) { // Error
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700195 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800196 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700197 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700198 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700199 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800200 if (kAccessCheck) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700202 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700203 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800204 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800205 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700206 }
207 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800208 return klass;
Ian Rogers57b86d42012-03-27 16:05:41 -0700209}
210
211// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
212// it cannot be resolved, throw an error. If it can, use it to create an array.
213// When verification/compiler hasn't been able to verify access, optionally perform an access
214// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800215// TODO: Fix no thread safety analysis when GCC can handle template specialization.
216template <bool kAccessCheck, bool kInstrumented>
217ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
218 mirror::ArtMethod* method,
219 int32_t component_count,
220 Thread* self,
221 gc::AllocatorType allocator_type)
222 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800223 bool slow_path = false;
224 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, method, component_count,
225 &slow_path);
226 if (UNLIKELY(slow_path)) {
227 if (klass == nullptr) {
228 return nullptr;
229 }
230 gc::Heap* heap = Runtime::Current()->GetHeap();
231 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -0800232 klass->GetComponentSize(),
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800233 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700234 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800235 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
236 klass->GetComponentSize(), allocator_type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700237}
238
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800239template <bool kAccessCheck, bool kInstrumented>
240ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
241 mirror::ArtMethod* method,
242 int32_t component_count,
243 Thread* self,
244 gc::AllocatorType allocator_type)
245 NO_THREAD_SAFETY_ANALYSIS {
246 DCHECK(klass != nullptr);
247 if (UNLIKELY(component_count < 0)) {
248 ThrowNegativeArraySizeException(component_count);
249 return nullptr; // Failure
250 }
251 if (kAccessCheck) {
252 mirror::Class* referrer = method->GetDeclaringClass();
253 if (UNLIKELY(!referrer->CanAccess(klass))) {
254 ThrowIllegalAccessErrorClass(referrer, klass);
255 return nullptr; // Failure
256 }
257 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800258 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
259 // suspension.
260 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
261 klass->GetComponentSize(), allocator_type);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800262}
263
Brian Carlstromea46f952013-07-30 01:26:50 -0700264extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800265 int32_t component_count, Thread* self,
266 bool access_check,
267 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700269
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800270extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
271 mirror::ArtMethod* method,
272 int32_t component_count, Thread* self,
273 bool access_check,
274 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
276
Ian Rogers08f753d2012-08-24 14:35:25 -0700277// Type of find field operation for fast and slow case.
278enum FindFieldType {
279 InstanceObjectRead,
280 InstanceObjectWrite,
281 InstancePrimitiveRead,
282 InstancePrimitiveWrite,
283 StaticObjectRead,
284 StaticObjectWrite,
285 StaticPrimitiveRead,
286 StaticPrimitiveWrite,
287};
288
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200289template<FindFieldType type, bool access_check>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800290static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200291 Thread* self, size_t expected_size) {
292 bool is_primitive;
293 bool is_set;
294 bool is_static;
295 switch (type) {
296 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
297 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
298 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
299 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
300 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
301 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
302 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
303 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
304 default: is_primitive = true; is_set = true; is_static = true; break;
305 }
306 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
307 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
308 if (UNLIKELY(resolved_field == nullptr)) {
309 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
310 return nullptr; // Failure.
311 }
312 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
313 if (access_check) {
314 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
315 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
316 return nullptr;
317 }
318 mirror::Class* referring_class = referrer->GetDeclaringClass();
Vladimir Marko89786432014-01-31 15:03:55 +0000319 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
320 field_idx))) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000321 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
322 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200323 }
324 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
325 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Vladimir Marko23a28212014-01-09 19:24:37 +0000326 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200327 } else {
328 FieldHelper fh(resolved_field);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800329 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || fh.FieldSize() != expected_size)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200330 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
331 DCHECK(throw_location.GetMethod() == referrer);
332 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
333 "Attempted read of %zd-bit %s on field '%s'",
334 expected_size * (32 / sizeof(int32_t)),
335 is_primitive ? "primitive" : "non-primitive",
336 PrettyField(resolved_field, true).c_str());
Vladimir Marko23a28212014-01-09 19:24:37 +0000337 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200338 }
339 }
340 }
341 if (!is_static) {
342 // instance fields must be being accessed on an initialized class
343 return resolved_field;
344 } else {
345 // If the class is initialized we're done.
346 if (LIKELY(fields_class->IsInitialized())) {
347 return resolved_field;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200348 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800349 SirtRef<mirror::Class> sirt_class(self, fields_class);
350 if (LIKELY(class_linker->EnsureInitialized(sirt_class, true, true))) {
351 // Otherwise let's ensure the class is initialized before resolving the field.
352 return resolved_field;
353 } else {
354 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Vladimir Marko23a28212014-01-09 19:24:37 +0000355 return nullptr; // Failure.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800356 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200357 }
358 }
359}
360
361// Explicit template declarations of FindFieldFromCode for all field access types.
362#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
363template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100364mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Ian Rogersef7d42f2014-01-06 12:55:46 -0800365 mirror::ArtMethod* referrer, \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100366 Thread* self, size_t expected_size) \
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200367
368#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
369 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
370 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
371
372EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
373EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
374EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
375EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
376EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
377EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
378EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
379EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
380
381#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
382#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
383
384template<InvokeType type, bool access_check>
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800385static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
386 mirror::Object* this_object,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200387 mirror::ArtMethod* referrer, Thread* self) {
388 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800389 SirtRef<mirror::Object> sirt_this(self, type == kStatic ? nullptr : this_object);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200390 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
391 if (UNLIKELY(resolved_method == nullptr)) {
392 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
393 return nullptr; // Failure.
Mathieu Chartier37a98762014-02-05 12:14:39 -0800394 } else if (UNLIKELY(sirt_this.get() == nullptr && type != kStatic)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200395 // Maintain interpreter-like semantics where NullPointerException is thrown
396 // after potential NoSuchMethodError from class linker.
397 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
398 DCHECK(referrer == throw_location.GetMethod());
399 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
400 return nullptr; // Failure.
401 } else if (access_check) {
402 // Incompatible class change should have been handled in resolve method.
403 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
404 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
405 referrer);
406 return nullptr; // Failure.
407 }
408 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
409 mirror::Class* referring_class = referrer->GetDeclaringClass();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800410 bool can_access_resolved_method =
Vladimir Marko89786432014-01-31 15:03:55 +0000411 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
412 method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800413 if (UNLIKELY(!can_access_resolved_method)) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000414 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
415 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200416 }
417 }
418 switch (type) {
419 case kStatic:
420 case kDirect:
421 return resolved_method;
422 case kVirtual: {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800423 mirror::ObjectArray<mirror::ArtMethod>* vtable = sirt_this->GetClass()->GetVTable();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200424 uint16_t vtable_index = resolved_method->GetMethodIndex();
425 if (access_check &&
426 (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) {
427 // Behavior to agree with that of the verifier.
428 MethodHelper mh(resolved_method);
429 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
430 mh.GetSignature());
431 return nullptr; // Failure.
432 }
433 DCHECK(vtable != nullptr);
434 return vtable->GetWithoutChecks(vtable_index);
435 }
436 case kSuper: {
437 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
438 uint16_t vtable_index = resolved_method->GetMethodIndex();
439 mirror::ObjectArray<mirror::ArtMethod>* vtable;
440 if (access_check) {
441 // Check existence of super class.
442 vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr;
443 if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) {
444 // Behavior to agree with that of the verifier.
445 MethodHelper mh(resolved_method);
446 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
447 mh.GetSignature());
448 return nullptr; // Failure.
449 }
450 } else {
451 // Super class must exist.
452 DCHECK(super_class != nullptr);
453 vtable = super_class->GetVTable();
454 }
455 DCHECK(vtable != nullptr);
456 return vtable->GetWithoutChecks(vtable_index);
457 }
458 case kInterface: {
Jeff Hao88474b42013-10-23 16:24:40 -0700459 uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize;
Mathieu Chartier37a98762014-02-05 12:14:39 -0800460 mirror::ObjectArray<mirror::ArtMethod>* imt_table = sirt_this->GetClass()->GetImTable();
Jeff Hao88474b42013-10-23 16:24:40 -0700461 mirror::ArtMethod* imt_method = imt_table->Get(imt_index);
462 if (!imt_method->IsImtConflictMethod()) {
463 return imt_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200464 } else {
Jeff Hao88474b42013-10-23 16:24:40 -0700465 mirror::ArtMethod* interface_method =
Mathieu Chartier37a98762014-02-05 12:14:39 -0800466 sirt_this->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao88474b42013-10-23 16:24:40 -0700467 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800468 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
469 sirt_this.get(), referrer);
Jeff Hao88474b42013-10-23 16:24:40 -0700470 return nullptr; // Failure.
471 } else {
472 return interface_method;
473 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200474 }
475 }
476 default:
477 LOG(FATAL) << "Unknown invoke type " << type;
478 return nullptr; // Failure.
479 }
480}
481
482// Explicit template declarations of FindMethodFromCode for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100483#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
484 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
485 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
486 mirror::Object* this_object, \
487 mirror::ArtMethod* referrer, \
488 Thread* self)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200489#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
490 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
491 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
492
493EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
494EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
495EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
496EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
497EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
498
499#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
500#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
Ian Rogers57b86d42012-03-27 16:05:41 -0700501
Ian Rogers08f753d2012-08-24 14:35:25 -0700502// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700503static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800504 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700505 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700507 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700509 if (UNLIKELY(resolved_field == NULL)) {
510 return NULL;
511 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800512 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700513 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700514 if (UNLIKELY(!fields_class->IsInitializing())) {
515 return NULL;
516 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700517 // Check for incompatible class change.
518 bool is_primitive;
519 bool is_set;
520 bool is_static;
521 switch (type) {
522 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
523 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
524 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
525 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
526 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
527 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
528 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
529 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700530 default:
531 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
532 is_primitive = true;
533 is_set = true;
534 is_static = true;
535 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700536 }
537 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
538 // Incompatible class change.
539 return NULL;
540 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700542 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
543 !referring_class->CanAccessMember(fields_class,
544 resolved_field->GetAccessFlags()) ||
545 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700546 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700547 return NULL;
548 }
549 FieldHelper fh(resolved_field);
550 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
551 fh.FieldSize() != expected_size)) {
552 return NULL;
553 }
554 return resolved_field;
555}
556
Ian Rogers08f753d2012-08-24 14:35:25 -0700557// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700558static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
559 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800560 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700561 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700563 bool is_direct = type == kStatic || type == kDirect;
564 if (UNLIKELY(this_object == NULL && !is_direct)) {
565 return NULL;
566 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700567 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700568 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
569 if (UNLIKELY(resolved_method == NULL)) {
570 return NULL;
571 }
572 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700573 // Check for incompatible class change errors and access.
574 bool icce = resolved_method->CheckIncompatibleClassChange(type);
575 if (UNLIKELY(icce)) {
576 return NULL;
577 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
579 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700580 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
581 !referring_class->CanAccessMember(methods_class,
582 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700583 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700584 return NULL;
585 }
586 }
587 if (type == kInterface) { // Most common form of slow path dispatch.
588 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
589 } else if (is_direct) {
590 return resolved_method;
591 } else if (type == kSuper) {
592 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
593 Get(resolved_method->GetMethodIndex());
594 } else {
595 DCHECK(type == kVirtual);
596 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
597 }
598}
599
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700600static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800601 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700602 Thread* self, bool can_run_clinit,
603 bool verify_access)
604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
605 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
606 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800607 if (UNLIKELY(klass == nullptr)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700608 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800609 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700610 }
611 // Perform access check if necessary.
612 mirror::Class* referring_class = referrer->GetDeclaringClass();
613 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
614 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800615 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700616 }
617 // If we're just implementing const-class, we shouldn't call <clinit>.
618 if (!can_run_clinit) {
619 return klass;
620 }
621 // If we are the <clinit> of this class, just return our storage.
622 //
623 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
624 // running.
Ian Rogers241b5de2013-10-09 17:58:57 -0700625 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700626 return klass;
627 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800628 SirtRef<mirror::Class> sirt_class(self, klass);
629 if (!class_linker->EnsureInitialized(sirt_class, true, true)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700630 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800631 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700632 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800633 return sirt_class.get();
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700634}
Ian Rogers57b86d42012-03-27 16:05:41 -0700635
jeffhaod7521322012-11-21 15:38:24 -0800636extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
637
Ian Rogersef7d42f2014-01-06 12:55:46 -0800638static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700640 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700641 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
642 return class_linker->ResolveString(string_idx, referrer);
643}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700644
TDYa1273d71d802012-08-15 03:47:03 -0700645static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogers719d1a32014-03-06 12:13:39 -0800646 NO_THREAD_SAFETY_ANALYSIS /* SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) */ {
TDYa1273d71d802012-08-15 03:47:03 -0700647 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800648 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800649 ThrowLocation saved_throw_location;
TDYa1273d71d802012-08-15 03:47:03 -0700650 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700652 self->ClearException();
653 }
654 // Decode locked object and unlock, before popping local references.
655 self->DecodeJObject(locked)->MonitorExit(self);
656 if (UNLIKELY(self->IsExceptionPending())) {
657 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
658 << saved_exception->Dump()
659 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700661 }
662 // Restore pending exception.
663 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800664 self->SetException(saved_throw_location, saved_exception);
TDYa1273d71d802012-08-15 03:47:03 -0700665 }
666}
667
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700669 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700670 if (o == NULL) {
671 return;
672 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700673 mirror::ArtMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700674 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800675 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700676 }
677 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers62d6c772013-02-27 08:32:07 -0800678 mirror::Class* return_type = MethodHelper(m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700679
680 if (!o->InstanceOf(return_type)) {
681 JniAbortF(NULL, "attempt to return an instance of %s from %s",
682 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
683 }
684}
685
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800686static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800687 for (;;) {
688 if (thread->ReadFlag(kCheckpointRequest)) {
689 thread->RunCheckpointFunction();
jeffhao373c52f2012-11-20 16:11:52 -0800690 } else if (thread->ReadFlag(kSuspendRequest)) {
691 thread->FullSuspendCheck();
692 } else {
693 break;
694 }
695 }
696}
697
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800698JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700699 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800700 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700701 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800702
Jeff Hao58df3272013-04-22 15:28:53 -0700703// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700704extern "C" void art_quick_deoptimize();
705static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700706 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
707}
708
709// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700710extern "C" void art_quick_instrumentation_entry(void*);
711static inline void* GetQuickInstrumentationEntryPoint() {
712 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700713}
714
715// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700716extern "C" void art_quick_instrumentation_exit();
717static inline uintptr_t GetQuickInstrumentationExitPc() {
718 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
719}
720
Brian Carlstromea46f952013-07-30 01:26:50 -0700721extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700722static inline const void* GetPortableToInterpreterBridge() {
723 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
724}
725
Ian Rogersef7d42f2014-01-06 12:55:46 -0800726static inline const void* GetPortableToQuickBridge() {
727 // TODO: portable to quick bridge. Bug: 8196384
728 return GetPortableToInterpreterBridge();
729}
730
Brian Carlstromea46f952013-07-30 01:26:50 -0700731extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700732static inline const void* GetQuickToInterpreterBridge() {
733 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700734}
735
Andreas Gampe2da88232014-02-27 12:26:20 -0800736extern "C" void art_quick_generic_jni_trampoline(mirror::ArtMethod*);
737static inline const void* GetQuickGenericJniTrampoline() {
738 return reinterpret_cast<void*>(art_quick_generic_jni_trampoline);
739}
740
Ian Rogersef7d42f2014-01-06 12:55:46 -0800741static inline const void* GetQuickToPortableBridge() {
742 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700743 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700744}
745
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700746static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
747 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700748}
749
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700750static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
751 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700752}
753
Jeff Hao88474b42013-10-23 16:24:40 -0700754static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) {
755 return class_linker->GetPortableImtConflictTrampoline();
756}
757
758static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) {
759 return class_linker->GetQuickImtConflictTrampoline();
760}
761
Andreas Gampe2da88232014-02-27 12:26:20 -0800762static inline const void* GetQuickGenericJniTrampoline(ClassLinker* class_linker) {
763 return class_linker->GetQuickGenericJniTrampoline();
764}
765
Vladimir Marko8a630572014-04-09 18:45:35 +0100766static inline const void* GetQuickToInterpreterBridgeTrampoline(ClassLinker* class_linker) {
767 return class_linker->GetQuickToInterpreterBridgeTrampoline();
768}
769
Ian Rogers848871b2013-08-05 10:56:33 -0700770extern "C" void art_portable_proxy_invoke_handler();
771static inline const void* GetPortableProxyInvokeHandler() {
772 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700773}
774
Ian Rogers848871b2013-08-05 10:56:33 -0700775extern "C" void art_quick_proxy_invoke_handler();
776static inline const void* GetQuickProxyInvokeHandler() {
777 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700778}
779
Ian Rogers848871b2013-08-05 10:56:33 -0700780extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700781static inline void* GetJniDlsymLookupStub() {
782 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
783}
Jeff Hao58df3272013-04-22 15:28:53 -0700784
Ian Rogers450dcb52013-09-20 17:36:02 -0700785template <typename INT_TYPE, typename FLOAT_TYPE>
786static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
787 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
788 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
789 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
790 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
791 if (LIKELY(f > kMinIntAsFloat)) {
792 if (LIKELY(f < kMaxIntAsFloat)) {
793 return static_cast<INT_TYPE>(f);
794 } else {
795 return kMaxInt;
796 }
797 } else {
798 return (f != f) ? 0 : kMinInt; // f != f implies NaN
799 }
800}
801
Shih-wei Liao2d831012011-09-28 22:06:53 -0700802} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700803
Ian Rogers7655f292013-07-29 11:07:13 -0700804#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_