blob: c3deba5ae9df061dfa161cb0e5b43e4c6f4f2e57 [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"
Shih-wei Liao2d831012011-09-28 22:06:53 -070021#include "class_linker.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"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080032#include "locks.h"
Ian Rogers450dcb52013-09-20 17:36:02 -070033#include "object_utils.h"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080034#include "sirt_ref.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070035#include "thread.h"
36
Shih-wei Liao2d831012011-09-28 22:06:53 -070037namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070038
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039namespace mirror {
Ian Rogers848871b2013-08-05 10:56:33 -070040 class Class;
Brian Carlstromea46f952013-07-30 01:26:50 -070041 class ArtField;
Ian Rogers848871b2013-08-05 10:56:33 -070042 class Object;
43} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070044
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080045// TODO: Fix no thread safety analysis when GCC can handle template specialization.
46template <const bool kAccessCheck>
47ALWAYS_INLINE static inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
48 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080049 Thread* self, bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080050 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070051 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070052 if (UNLIKELY(klass == NULL)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080053 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080054 *slow_path = true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070055 if (klass == NULL) {
56 DCHECK(self->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080057 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070058 }
59 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080060 if (kAccessCheck) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070061 if (UNLIKELY(!klass->IsInstantiable())) {
62 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
63 self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;",
64 PrettyDescriptor(klass).c_str());
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080065 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080066 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070067 }
68 mirror::Class* referrer = method->GetDeclaringClass();
69 if (UNLIKELY(!referrer->CanAccess(klass))) {
70 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080071 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080072 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070073 }
74 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080075 if (UNLIKELY(!klass->IsInitialized())) {
76 SirtRef<mirror::Class> sirt_klass(self, klass);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080077 // EnsureInitialized (the class initializer) might cause a GC.
78 // may cause us to suspend meaning that another thread may try to
79 // change the allocator while we are stuck in the entrypoints of
80 // an old allocator. Also, the class initialization may fail. To
81 // handle these cases we mark the slow path boolean as true so
82 // that the caller knows to check the allocator type to see if it
83 // has changed and to null-check the return value in case the
84 // initialization fails.
85 *slow_path = true;
Mathieu Chartierc528dba2013-11-26 12:00:11 -080086 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080087 DCHECK(self->IsExceptionPending());
88 return nullptr; // Failure
89 }
90 return sirt_klass.get();
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070091 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080092 return klass;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070093}
94
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080095// TODO: Fix no thread safety analysis when annotalysis is smarter.
96ALWAYS_INLINE static inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
97 Thread* self, bool* slow_path)
98 NO_THREAD_SAFETY_ANALYSIS {
99 if (UNLIKELY(!klass->IsInitialized())) {
100 SirtRef<mirror::Class> sirt_class(self, klass);
101 // EnsureInitialized (the class initializer) might cause a GC.
102 // may cause us to suspend meaning that another thread may try to
103 // change the allocator while we are stuck in the entrypoints of
104 // an old allocator. Also, the class initialization may fail. To
105 // handle these cases we mark the slow path boolean as true so
106 // that the caller knows to check the allocator type to see if it
107 // has changed and to null-check the return value in case the
108 // initialization fails.
109 *slow_path = true;
110 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_class, true, true)) {
111 DCHECK(self->IsExceptionPending());
112 return nullptr; // Failure
113 }
114 return sirt_class.get();
115 }
116 return klass;
117}
118
Ian Rogers57b86d42012-03-27 16:05:41 -0700119// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
120// cannot be resolved, throw an error. If it can, use it to create an instance.
121// When verification/compiler hasn't been able to verify access, optionally perform an access
122// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800123// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
124template <bool kAccessCheck, bool kInstrumented>
125ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
126 mirror::ArtMethod* method,
127 Thread* self,
128 gc::AllocatorType allocator_type)
129 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800130 bool slow_path = false;
131 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
132 if (UNLIKELY(slow_path)) {
133 if (klass == nullptr) {
134 return nullptr;
135 }
136 gc::Heap* heap = Runtime::Current()->GetHeap();
137 return klass->Alloc<kInstrumented>(self, heap->GetCurrentAllocator());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700138 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800139 return klass->Alloc<kInstrumented>(self, allocator_type);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700140}
141
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800142// Given the context of a calling Method and a resolved class, create an instance.
143// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
144template <bool kInstrumented>
145ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
146 mirror::ArtMethod* method,
147 Thread* self,
148 gc::AllocatorType allocator_type)
149 NO_THREAD_SAFETY_ANALYSIS {
150 DCHECK(klass != nullptr);
151 bool slow_path = false;
152 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
153 if (UNLIKELY(slow_path)) {
154 if (klass == nullptr) {
155 return nullptr;
156 }
157 gc::Heap* heap = Runtime::Current()->GetHeap();
158 return klass->Alloc<kInstrumented>(self, heap->GetCurrentAllocator());
159 }
160 return klass->Alloc<kInstrumented>(self, allocator_type);
161}
162
163// Given the context of a calling Method and an initialized class, create an instance.
164// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
165template <bool kInstrumented>
166ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
167 mirror::ArtMethod* method,
168 Thread* self,
169 gc::AllocatorType allocator_type)
170 NO_THREAD_SAFETY_ANALYSIS {
171 DCHECK(klass != nullptr);
172 return klass->Alloc<kInstrumented>(self, allocator_type);
173}
174
175
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800176// TODO: Fix no thread safety analysis when GCC can handle template specialization.
177template <bool kAccessCheck>
178ALWAYS_INLINE static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
179 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800180 int32_t component_count,
181 bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800182 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700183 if (UNLIKELY(component_count < 0)) {
184 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800185 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800186 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700187 }
188 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800189 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700190 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800191 *slow_path = true;
192 if (klass == nullptr) { // Error
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700193 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800194 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700195 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700196 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700197 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800198 if (kAccessCheck) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700200 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700201 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800202 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800203 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700204 }
205 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800206 return klass;
Ian Rogers57b86d42012-03-27 16:05:41 -0700207}
208
209// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
210// it cannot be resolved, throw an error. If it can, use it to create an array.
211// When verification/compiler hasn't been able to verify access, optionally perform an access
212// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800213// TODO: Fix no thread safety analysis when GCC can handle template specialization.
214template <bool kAccessCheck, bool kInstrumented>
215ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
216 mirror::ArtMethod* method,
217 int32_t component_count,
218 Thread* self,
219 gc::AllocatorType allocator_type)
220 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800221 bool slow_path = false;
222 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, method, component_count,
223 &slow_path);
224 if (UNLIKELY(slow_path)) {
225 if (klass == nullptr) {
226 return nullptr;
227 }
228 gc::Heap* heap = Runtime::Current()->GetHeap();
229 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
230 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700231 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800232 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, allocator_type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700233}
234
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800235template <bool kAccessCheck, bool kInstrumented>
236ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
237 mirror::ArtMethod* method,
238 int32_t component_count,
239 Thread* self,
240 gc::AllocatorType allocator_type)
241 NO_THREAD_SAFETY_ANALYSIS {
242 DCHECK(klass != nullptr);
243 if (UNLIKELY(component_count < 0)) {
244 ThrowNegativeArraySizeException(component_count);
245 return nullptr; // Failure
246 }
247 if (kAccessCheck) {
248 mirror::Class* referrer = method->GetDeclaringClass();
249 if (UNLIKELY(!referrer->CanAccess(klass))) {
250 ThrowIllegalAccessErrorClass(referrer, klass);
251 return nullptr; // Failure
252 }
253 }
254 // No need to retry a slow-path allocation as the above code won't
255 // cause a GC or thread suspension.
256 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, allocator_type);
257}
258
Brian Carlstromea46f952013-07-30 01:26:50 -0700259extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800260 int32_t component_count, Thread* self,
261 bool access_check,
262 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700264
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800265extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
266 mirror::ArtMethod* method,
267 int32_t component_count, Thread* self,
268 bool access_check,
269 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
271
Ian Rogers08f753d2012-08-24 14:35:25 -0700272// Type of find field operation for fast and slow case.
273enum FindFieldType {
274 InstanceObjectRead,
275 InstanceObjectWrite,
276 InstancePrimitiveRead,
277 InstancePrimitiveWrite,
278 StaticObjectRead,
279 StaticObjectWrite,
280 StaticPrimitiveRead,
281 StaticPrimitiveWrite,
282};
283
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200284template<FindFieldType type, bool access_check>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800285static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200286 Thread* self, size_t expected_size) {
287 bool is_primitive;
288 bool is_set;
289 bool is_static;
290 switch (type) {
291 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
292 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
293 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
294 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
295 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
296 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
297 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
298 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
299 default: is_primitive = true; is_set = true; is_static = true; break;
300 }
301 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
302 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
303 if (UNLIKELY(resolved_field == nullptr)) {
304 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
305 return nullptr; // Failure.
306 }
307 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
308 if (access_check) {
309 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
310 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
311 return nullptr;
312 }
313 mirror::Class* referring_class = referrer->GetDeclaringClass();
Vladimir Marko89786432014-01-31 15:03:55 +0000314 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
315 field_idx))) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000316 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
317 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200318 }
319 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
320 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Vladimir Marko23a28212014-01-09 19:24:37 +0000321 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200322 } else {
323 FieldHelper fh(resolved_field);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800324 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || fh.FieldSize() != expected_size)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200325 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
326 DCHECK(throw_location.GetMethod() == referrer);
327 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
328 "Attempted read of %zd-bit %s on field '%s'",
329 expected_size * (32 / sizeof(int32_t)),
330 is_primitive ? "primitive" : "non-primitive",
331 PrettyField(resolved_field, true).c_str());
Vladimir Marko23a28212014-01-09 19:24:37 +0000332 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200333 }
334 }
335 }
336 if (!is_static) {
337 // instance fields must be being accessed on an initialized class
338 return resolved_field;
339 } else {
340 // If the class is initialized we're done.
341 if (LIKELY(fields_class->IsInitialized())) {
342 return resolved_field;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200343 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800344 SirtRef<mirror::Class> sirt_class(self, fields_class);
345 if (LIKELY(class_linker->EnsureInitialized(sirt_class, true, true))) {
346 // Otherwise let's ensure the class is initialized before resolving the field.
347 return resolved_field;
348 } else {
349 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Vladimir Marko23a28212014-01-09 19:24:37 +0000350 return nullptr; // Failure.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800351 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200352 }
353 }
354}
355
356// Explicit template declarations of FindFieldFromCode for all field access types.
357#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
358template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100359mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Ian Rogersef7d42f2014-01-06 12:55:46 -0800360 mirror::ArtMethod* referrer, \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100361 Thread* self, size_t expected_size) \
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200362
363#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
364 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
365 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
366
367EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
368EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
369EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
370EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
371EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
372EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
373EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
374EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
375
376#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
377#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
378
379template<InvokeType type, bool access_check>
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800380static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
381 mirror::Object* this_object,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200382 mirror::ArtMethod* referrer, Thread* self) {
383 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800384 SirtRef<mirror::Object> sirt_this(self, type == kStatic ? nullptr : this_object);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200385 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
386 if (UNLIKELY(resolved_method == nullptr)) {
387 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
388 return nullptr; // Failure.
Mathieu Chartier37a98762014-02-05 12:14:39 -0800389 } else if (UNLIKELY(sirt_this.get() == nullptr && type != kStatic)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200390 // Maintain interpreter-like semantics where NullPointerException is thrown
391 // after potential NoSuchMethodError from class linker.
392 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
393 DCHECK(referrer == throw_location.GetMethod());
394 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
395 return nullptr; // Failure.
396 } else if (access_check) {
397 // Incompatible class change should have been handled in resolve method.
398 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
399 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
400 referrer);
401 return nullptr; // Failure.
402 }
403 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
404 mirror::Class* referring_class = referrer->GetDeclaringClass();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800405 bool can_access_resolved_method =
Vladimir Marko89786432014-01-31 15:03:55 +0000406 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
407 method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800408 if (UNLIKELY(!can_access_resolved_method)) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000409 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
410 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200411 }
412 }
413 switch (type) {
414 case kStatic:
415 case kDirect:
416 return resolved_method;
417 case kVirtual: {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800418 mirror::ObjectArray<mirror::ArtMethod>* vtable = sirt_this->GetClass()->GetVTable();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200419 uint16_t vtable_index = resolved_method->GetMethodIndex();
420 if (access_check &&
421 (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) {
422 // Behavior to agree with that of the verifier.
423 MethodHelper mh(resolved_method);
424 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
425 mh.GetSignature());
426 return nullptr; // Failure.
427 }
428 DCHECK(vtable != nullptr);
429 return vtable->GetWithoutChecks(vtable_index);
430 }
431 case kSuper: {
432 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
433 uint16_t vtable_index = resolved_method->GetMethodIndex();
434 mirror::ObjectArray<mirror::ArtMethod>* vtable;
435 if (access_check) {
436 // Check existence of super class.
437 vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr;
438 if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) {
439 // Behavior to agree with that of the verifier.
440 MethodHelper mh(resolved_method);
441 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
442 mh.GetSignature());
443 return nullptr; // Failure.
444 }
445 } else {
446 // Super class must exist.
447 DCHECK(super_class != nullptr);
448 vtable = super_class->GetVTable();
449 }
450 DCHECK(vtable != nullptr);
451 return vtable->GetWithoutChecks(vtable_index);
452 }
453 case kInterface: {
Jeff Hao88474b42013-10-23 16:24:40 -0700454 uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize;
Mathieu Chartier37a98762014-02-05 12:14:39 -0800455 mirror::ObjectArray<mirror::ArtMethod>* imt_table = sirt_this->GetClass()->GetImTable();
Jeff Hao88474b42013-10-23 16:24:40 -0700456 mirror::ArtMethod* imt_method = imt_table->Get(imt_index);
457 if (!imt_method->IsImtConflictMethod()) {
458 return imt_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200459 } else {
Jeff Hao88474b42013-10-23 16:24:40 -0700460 mirror::ArtMethod* interface_method =
Mathieu Chartier37a98762014-02-05 12:14:39 -0800461 sirt_this->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao88474b42013-10-23 16:24:40 -0700462 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800463 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
464 sirt_this.get(), referrer);
Jeff Hao88474b42013-10-23 16:24:40 -0700465 return nullptr; // Failure.
466 } else {
467 return interface_method;
468 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200469 }
470 }
471 default:
472 LOG(FATAL) << "Unknown invoke type " << type;
473 return nullptr; // Failure.
474 }
475}
476
477// Explicit template declarations of FindMethodFromCode for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100478#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
479 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
480 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
481 mirror::Object* this_object, \
482 mirror::ArtMethod* referrer, \
483 Thread* self)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200484#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
485 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
486 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
487
488EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
489EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
490EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
491EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
492EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
493
494#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
495#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
Ian Rogers57b86d42012-03-27 16:05:41 -0700496
Ian Rogers08f753d2012-08-24 14:35:25 -0700497// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700498static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800499 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700500 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700501 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700502 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700504 if (UNLIKELY(resolved_field == NULL)) {
505 return NULL;
506 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700508 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700509 if (UNLIKELY(!fields_class->IsInitializing())) {
510 return NULL;
511 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700512 // Check for incompatible class change.
513 bool is_primitive;
514 bool is_set;
515 bool is_static;
516 switch (type) {
517 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
518 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
519 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
520 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
521 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
522 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
523 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
524 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700525 default:
526 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
527 is_primitive = true;
528 is_set = true;
529 is_static = true;
530 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700531 }
532 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
533 // Incompatible class change.
534 return NULL;
535 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700537 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
538 !referring_class->CanAccessMember(fields_class,
539 resolved_field->GetAccessFlags()) ||
540 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700541 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700542 return NULL;
543 }
544 FieldHelper fh(resolved_field);
545 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
546 fh.FieldSize() != expected_size)) {
547 return NULL;
548 }
549 return resolved_field;
550}
551
Ian Rogers08f753d2012-08-24 14:35:25 -0700552// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700553static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
554 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800555 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700557 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700558 bool is_direct = type == kStatic || type == kDirect;
559 if (UNLIKELY(this_object == NULL && !is_direct)) {
560 return NULL;
561 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700562 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700563 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
564 if (UNLIKELY(resolved_method == NULL)) {
565 return NULL;
566 }
567 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700568 // Check for incompatible class change errors and access.
569 bool icce = resolved_method->CheckIncompatibleClassChange(type);
570 if (UNLIKELY(icce)) {
571 return NULL;
572 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
574 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700575 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
576 !referring_class->CanAccessMember(methods_class,
577 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700578 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700579 return NULL;
580 }
581 }
582 if (type == kInterface) { // Most common form of slow path dispatch.
583 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
584 } else if (is_direct) {
585 return resolved_method;
586 } else if (type == kSuper) {
587 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
588 Get(resolved_method->GetMethodIndex());
589 } else {
590 DCHECK(type == kVirtual);
591 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
592 }
593}
594
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700595static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800596 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700597 Thread* self, bool can_run_clinit,
598 bool verify_access)
599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
600 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
601 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800602 if (UNLIKELY(klass == nullptr)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700603 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800604 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700605 }
606 // Perform access check if necessary.
607 mirror::Class* referring_class = referrer->GetDeclaringClass();
608 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
609 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800610 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700611 }
612 // If we're just implementing const-class, we shouldn't call <clinit>.
613 if (!can_run_clinit) {
614 return klass;
615 }
616 // If we are the <clinit> of this class, just return our storage.
617 //
618 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
619 // running.
Ian Rogers241b5de2013-10-09 17:58:57 -0700620 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700621 return klass;
622 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800623 SirtRef<mirror::Class> sirt_class(self, klass);
624 if (!class_linker->EnsureInitialized(sirt_class, true, true)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700625 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800626 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700627 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800628 return sirt_class.get();
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700629}
Ian Rogers57b86d42012-03-27 16:05:41 -0700630
jeffhaod7521322012-11-21 15:38:24 -0800631extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
632
Ian Rogersef7d42f2014-01-06 12:55:46 -0800633static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700636 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
637 return class_linker->ResolveString(string_idx, referrer);
638}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700639
TDYa1273d71d802012-08-15 03:47:03 -0700640static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700641 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700642 UNLOCK_FUNCTION(monitor_lock_) {
643 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800645 ThrowLocation saved_throw_location;
TDYa1273d71d802012-08-15 03:47:03 -0700646 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800647 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700648 self->ClearException();
649 }
650 // Decode locked object and unlock, before popping local references.
651 self->DecodeJObject(locked)->MonitorExit(self);
652 if (UNLIKELY(self->IsExceptionPending())) {
653 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
654 << saved_exception->Dump()
655 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800656 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700657 }
658 // Restore pending exception.
659 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 self->SetException(saved_throw_location, saved_exception);
TDYa1273d71d802012-08-15 03:47:03 -0700661 }
662}
663
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700666 if (o == NULL) {
667 return;
668 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700669 mirror::ArtMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700670 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800671 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700672 }
673 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers62d6c772013-02-27 08:32:07 -0800674 mirror::Class* return_type = MethodHelper(m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700675
676 if (!o->InstanceOf(return_type)) {
677 JniAbortF(NULL, "attempt to return an instance of %s from %s",
678 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
679 }
680}
681
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800682static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800683 for (;;) {
684 if (thread->ReadFlag(kCheckpointRequest)) {
685 thread->RunCheckpointFunction();
jeffhao373c52f2012-11-20 16:11:52 -0800686 } else if (thread->ReadFlag(kSuspendRequest)) {
687 thread->FullSuspendCheck();
688 } else {
689 break;
690 }
691 }
692}
693
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800694JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700695 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800696 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700697 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800698
Jeff Hao58df3272013-04-22 15:28:53 -0700699// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700700extern "C" void art_quick_deoptimize();
701static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700702 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
703}
704
705// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700706extern "C" void art_quick_instrumentation_entry(void*);
707static inline void* GetQuickInstrumentationEntryPoint() {
708 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700709}
710
711// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700712extern "C" void art_quick_instrumentation_exit();
713static inline uintptr_t GetQuickInstrumentationExitPc() {
714 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
715}
716
Brian Carlstromea46f952013-07-30 01:26:50 -0700717extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700718static inline const void* GetPortableToInterpreterBridge() {
719 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
720}
721
Ian Rogersef7d42f2014-01-06 12:55:46 -0800722static inline const void* GetPortableToQuickBridge() {
723 // TODO: portable to quick bridge. Bug: 8196384
724 return GetPortableToInterpreterBridge();
725}
726
Brian Carlstromea46f952013-07-30 01:26:50 -0700727extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700728static inline const void* GetQuickToInterpreterBridge() {
729 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700730}
731
Ian Rogersef7d42f2014-01-06 12:55:46 -0800732static inline const void* GetQuickToPortableBridge() {
733 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700734 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700735}
736
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700737static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
738 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700739}
740
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700741static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
742 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700743}
744
Jeff Hao88474b42013-10-23 16:24:40 -0700745static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) {
746 return class_linker->GetPortableImtConflictTrampoline();
747}
748
749static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) {
750 return class_linker->GetQuickImtConflictTrampoline();
751}
752
Ian Rogers848871b2013-08-05 10:56:33 -0700753extern "C" void art_portable_proxy_invoke_handler();
754static inline const void* GetPortableProxyInvokeHandler() {
755 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700756}
757
Ian Rogers848871b2013-08-05 10:56:33 -0700758extern "C" void art_quick_proxy_invoke_handler();
759static inline const void* GetQuickProxyInvokeHandler() {
760 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700761}
762
Ian Rogers848871b2013-08-05 10:56:33 -0700763extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700764static inline void* GetJniDlsymLookupStub() {
765 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
766}
Jeff Hao58df3272013-04-22 15:28:53 -0700767
Ian Rogers450dcb52013-09-20 17:36:02 -0700768template <typename INT_TYPE, typename FLOAT_TYPE>
769static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
770 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
771 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
772 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
773 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
774 if (LIKELY(f > kMinIntAsFloat)) {
775 if (LIKELY(f < kMaxIntAsFloat)) {
776 return static_cast<INT_TYPE>(f);
777 } else {
778 return kMaxInt;
779 }
780 } else {
781 return (f != f) ? 0 : kMinInt; // f != f implies NaN
782 }
783}
784
Shih-wei Liao2d831012011-09-28 22:06:53 -0700785} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700786
Ian Rogers7655f292013-07-29 11:07:13 -0700787#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_