blob: 8b94b5abe12e2ecd0812d8939e110a335a4c05e7 [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 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100139 DCHECK(klass != nullptr);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800140 return klass->Alloc<kInstrumented>(self, allocator_type);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700141}
142
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800143// Given the context of a calling Method and a resolved class, create an instance.
144// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
145template <bool kInstrumented>
146ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
147 mirror::ArtMethod* method,
148 Thread* self,
149 gc::AllocatorType allocator_type)
150 NO_THREAD_SAFETY_ANALYSIS {
151 DCHECK(klass != nullptr);
152 bool slow_path = false;
153 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
154 if (UNLIKELY(slow_path)) {
155 if (klass == nullptr) {
156 return nullptr;
157 }
158 gc::Heap* heap = Runtime::Current()->GetHeap();
159 return klass->Alloc<kInstrumented>(self, heap->GetCurrentAllocator());
160 }
161 return klass->Alloc<kInstrumented>(self, allocator_type);
162}
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);
173 return klass->Alloc<kInstrumented>(self, allocator_type);
174}
175
176
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800177// TODO: Fix no thread safety analysis when GCC can handle template specialization.
178template <bool kAccessCheck>
179ALWAYS_INLINE static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
180 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800181 int32_t component_count,
182 bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800183 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700184 if (UNLIKELY(component_count < 0)) {
185 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800186 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800187 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700188 }
189 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800190 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700191 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800192 *slow_path = true;
193 if (klass == nullptr) { // Error
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700194 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800195 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700196 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700197 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700198 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800199 if (kAccessCheck) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700201 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700202 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800203 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800204 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700205 }
206 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800207 return klass;
Ian Rogers57b86d42012-03-27 16:05:41 -0700208}
209
210// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
211// it cannot be resolved, throw an error. If it can, use it to create an array.
212// When verification/compiler hasn't been able to verify access, optionally perform an access
213// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800214// TODO: Fix no thread safety analysis when GCC can handle template specialization.
215template <bool kAccessCheck, bool kInstrumented>
216ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
217 mirror::ArtMethod* method,
218 int32_t component_count,
219 Thread* self,
220 gc::AllocatorType allocator_type)
221 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800222 bool slow_path = false;
223 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, method, component_count,
224 &slow_path);
225 if (UNLIKELY(slow_path)) {
226 if (klass == nullptr) {
227 return nullptr;
228 }
229 gc::Heap* heap = Runtime::Current()->GetHeap();
230 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
231 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700232 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800233 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, allocator_type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700234}
235
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800236template <bool kAccessCheck, bool kInstrumented>
237ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
238 mirror::ArtMethod* method,
239 int32_t component_count,
240 Thread* self,
241 gc::AllocatorType allocator_type)
242 NO_THREAD_SAFETY_ANALYSIS {
243 DCHECK(klass != nullptr);
244 if (UNLIKELY(component_count < 0)) {
245 ThrowNegativeArraySizeException(component_count);
246 return nullptr; // Failure
247 }
248 if (kAccessCheck) {
249 mirror::Class* referrer = method->GetDeclaringClass();
250 if (UNLIKELY(!referrer->CanAccess(klass))) {
251 ThrowIllegalAccessErrorClass(referrer, klass);
252 return nullptr; // Failure
253 }
254 }
255 // No need to retry a slow-path allocation as the above code won't
256 // cause a GC or thread suspension.
257 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, allocator_type);
258}
259
Brian Carlstromea46f952013-07-30 01:26:50 -0700260extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800261 int32_t component_count, Thread* self,
262 bool access_check,
263 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700264 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700265
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800266extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
267 mirror::ArtMethod* method,
268 int32_t component_count, Thread* self,
269 bool access_check,
270 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700271 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
272
Ian Rogers08f753d2012-08-24 14:35:25 -0700273// Type of find field operation for fast and slow case.
274enum FindFieldType {
275 InstanceObjectRead,
276 InstanceObjectWrite,
277 InstancePrimitiveRead,
278 InstancePrimitiveWrite,
279 StaticObjectRead,
280 StaticObjectWrite,
281 StaticPrimitiveRead,
282 StaticPrimitiveWrite,
283};
284
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200285template<FindFieldType type, bool access_check>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200287 Thread* self, size_t expected_size) {
288 bool is_primitive;
289 bool is_set;
290 bool is_static;
291 switch (type) {
292 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
293 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
294 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
295 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
296 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
297 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
298 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
299 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
300 default: is_primitive = true; is_set = true; is_static = true; break;
301 }
302 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
303 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
304 if (UNLIKELY(resolved_field == nullptr)) {
305 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
306 return nullptr; // Failure.
307 }
308 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
309 if (access_check) {
310 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
311 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
312 return nullptr;
313 }
314 mirror::Class* referring_class = referrer->GetDeclaringClass();
Vladimir Marko89786432014-01-31 15:03:55 +0000315 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
316 field_idx))) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000317 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
318 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200319 }
320 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
321 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Vladimir Marko23a28212014-01-09 19:24:37 +0000322 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200323 } else {
324 FieldHelper fh(resolved_field);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800325 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || fh.FieldSize() != expected_size)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200326 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
327 DCHECK(throw_location.GetMethod() == referrer);
328 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
329 "Attempted read of %zd-bit %s on field '%s'",
330 expected_size * (32 / sizeof(int32_t)),
331 is_primitive ? "primitive" : "non-primitive",
332 PrettyField(resolved_field, true).c_str());
Vladimir Marko23a28212014-01-09 19:24:37 +0000333 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200334 }
335 }
336 }
337 if (!is_static) {
338 // instance fields must be being accessed on an initialized class
339 return resolved_field;
340 } else {
341 // If the class is initialized we're done.
342 if (LIKELY(fields_class->IsInitialized())) {
343 return resolved_field;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200344 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800345 SirtRef<mirror::Class> sirt_class(self, fields_class);
346 if (LIKELY(class_linker->EnsureInitialized(sirt_class, true, true))) {
347 // Otherwise let's ensure the class is initialized before resolving the field.
348 return resolved_field;
349 } else {
350 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Vladimir Marko23a28212014-01-09 19:24:37 +0000351 return nullptr; // Failure.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800352 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200353 }
354 }
355}
356
357// Explicit template declarations of FindFieldFromCode for all field access types.
358#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
359template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100360mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Ian Rogersef7d42f2014-01-06 12:55:46 -0800361 mirror::ArtMethod* referrer, \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100362 Thread* self, size_t expected_size) \
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200363
364#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
365 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
366 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
367
368EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
369EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
370EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
371EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
372EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
373EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
374EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
375EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
376
377#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
378#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
379
380template<InvokeType type, bool access_check>
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800381static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
382 mirror::Object* this_object,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200383 mirror::ArtMethod* referrer, Thread* self) {
384 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800385 SirtRef<mirror::Object> sirt_this(self, type == kStatic ? nullptr : this_object);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200386 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
387 if (UNLIKELY(resolved_method == nullptr)) {
388 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
389 return nullptr; // Failure.
Mathieu Chartier37a98762014-02-05 12:14:39 -0800390 } else if (UNLIKELY(sirt_this.get() == nullptr && type != kStatic)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200391 // Maintain interpreter-like semantics where NullPointerException is thrown
392 // after potential NoSuchMethodError from class linker.
393 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
394 DCHECK(referrer == throw_location.GetMethod());
395 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
396 return nullptr; // Failure.
397 } else if (access_check) {
398 // Incompatible class change should have been handled in resolve method.
399 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
400 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
401 referrer);
402 return nullptr; // Failure.
403 }
404 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
405 mirror::Class* referring_class = referrer->GetDeclaringClass();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800406 bool can_access_resolved_method =
Vladimir Marko89786432014-01-31 15:03:55 +0000407 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
408 method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800409 if (UNLIKELY(!can_access_resolved_method)) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000410 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
411 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200412 }
413 }
414 switch (type) {
415 case kStatic:
416 case kDirect:
417 return resolved_method;
418 case kVirtual: {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800419 mirror::ObjectArray<mirror::ArtMethod>* vtable = sirt_this->GetClass()->GetVTable();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200420 uint16_t vtable_index = resolved_method->GetMethodIndex();
421 if (access_check &&
422 (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) {
423 // Behavior to agree with that of the verifier.
424 MethodHelper mh(resolved_method);
425 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
426 mh.GetSignature());
427 return nullptr; // Failure.
428 }
429 DCHECK(vtable != nullptr);
430 return vtable->GetWithoutChecks(vtable_index);
431 }
432 case kSuper: {
433 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
434 uint16_t vtable_index = resolved_method->GetMethodIndex();
435 mirror::ObjectArray<mirror::ArtMethod>* vtable;
436 if (access_check) {
437 // Check existence of super class.
438 vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr;
439 if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) {
440 // Behavior to agree with that of the verifier.
441 MethodHelper mh(resolved_method);
442 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
443 mh.GetSignature());
444 return nullptr; // Failure.
445 }
446 } else {
447 // Super class must exist.
448 DCHECK(super_class != nullptr);
449 vtable = super_class->GetVTable();
450 }
451 DCHECK(vtable != nullptr);
452 return vtable->GetWithoutChecks(vtable_index);
453 }
454 case kInterface: {
Jeff Hao88474b42013-10-23 16:24:40 -0700455 uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize;
Mathieu Chartier37a98762014-02-05 12:14:39 -0800456 mirror::ObjectArray<mirror::ArtMethod>* imt_table = sirt_this->GetClass()->GetImTable();
Jeff Hao88474b42013-10-23 16:24:40 -0700457 mirror::ArtMethod* imt_method = imt_table->Get(imt_index);
458 if (!imt_method->IsImtConflictMethod()) {
459 return imt_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200460 } else {
Jeff Hao88474b42013-10-23 16:24:40 -0700461 mirror::ArtMethod* interface_method =
Mathieu Chartier37a98762014-02-05 12:14:39 -0800462 sirt_this->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao88474b42013-10-23 16:24:40 -0700463 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800464 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
465 sirt_this.get(), referrer);
Jeff Hao88474b42013-10-23 16:24:40 -0700466 return nullptr; // Failure.
467 } else {
468 return interface_method;
469 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200470 }
471 }
472 default:
473 LOG(FATAL) << "Unknown invoke type " << type;
474 return nullptr; // Failure.
475 }
476}
477
478// Explicit template declarations of FindMethodFromCode for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100479#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
480 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
481 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
482 mirror::Object* this_object, \
483 mirror::ArtMethod* referrer, \
484 Thread* self)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200485#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
486 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
487 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
488
489EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
490EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
491EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
492EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
493EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
494
495#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
496#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
Ian Rogers57b86d42012-03-27 16:05:41 -0700497
Ian Rogers08f753d2012-08-24 14:35:25 -0700498// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700499static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800500 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700501 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700503 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800504 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700505 if (UNLIKELY(resolved_field == NULL)) {
506 return NULL;
507 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700509 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700510 if (UNLIKELY(!fields_class->IsInitializing())) {
511 return NULL;
512 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700513 // Check for incompatible class change.
514 bool is_primitive;
515 bool is_set;
516 bool is_static;
517 switch (type) {
518 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
519 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
520 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
521 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
522 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
523 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
524 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
525 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700526 default:
527 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
528 is_primitive = true;
529 is_set = true;
530 is_static = true;
531 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700532 }
533 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
534 // Incompatible class change.
535 return NULL;
536 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700538 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
539 !referring_class->CanAccessMember(fields_class,
540 resolved_field->GetAccessFlags()) ||
541 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700542 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700543 return NULL;
544 }
545 FieldHelper fh(resolved_field);
546 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
547 fh.FieldSize() != expected_size)) {
548 return NULL;
549 }
550 return resolved_field;
551}
552
Ian Rogers08f753d2012-08-24 14:35:25 -0700553// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700554static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
555 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800556 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700557 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700558 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700559 bool is_direct = type == kStatic || type == kDirect;
560 if (UNLIKELY(this_object == NULL && !is_direct)) {
561 return NULL;
562 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700563 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700564 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
565 if (UNLIKELY(resolved_method == NULL)) {
566 return NULL;
567 }
568 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700569 // Check for incompatible class change errors and access.
570 bool icce = resolved_method->CheckIncompatibleClassChange(type);
571 if (UNLIKELY(icce)) {
572 return NULL;
573 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
575 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700576 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
577 !referring_class->CanAccessMember(methods_class,
578 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700579 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700580 return NULL;
581 }
582 }
583 if (type == kInterface) { // Most common form of slow path dispatch.
584 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
585 } else if (is_direct) {
586 return resolved_method;
587 } else if (type == kSuper) {
588 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
589 Get(resolved_method->GetMethodIndex());
590 } else {
591 DCHECK(type == kVirtual);
592 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
593 }
594}
595
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700596static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800597 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700598 Thread* self, bool can_run_clinit,
599 bool verify_access)
600 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
601 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
602 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800603 if (UNLIKELY(klass == nullptr)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700604 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800605 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700606 }
607 // Perform access check if necessary.
608 mirror::Class* referring_class = referrer->GetDeclaringClass();
609 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
610 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800611 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700612 }
613 // If we're just implementing const-class, we shouldn't call <clinit>.
614 if (!can_run_clinit) {
615 return klass;
616 }
617 // If we are the <clinit> of this class, just return our storage.
618 //
619 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
620 // running.
Ian Rogers241b5de2013-10-09 17:58:57 -0700621 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700622 return klass;
623 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800624 SirtRef<mirror::Class> sirt_class(self, klass);
625 if (!class_linker->EnsureInitialized(sirt_class, true, true)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700626 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800627 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700628 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800629 return sirt_class.get();
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700630}
Ian Rogers57b86d42012-03-27 16:05:41 -0700631
jeffhaod7521322012-11-21 15:38:24 -0800632extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
633
Ian Rogersef7d42f2014-01-06 12:55:46 -0800634static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700636 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700637 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
638 return class_linker->ResolveString(string_idx, referrer);
639}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700640
TDYa1273d71d802012-08-15 03:47:03 -0700641static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700642 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700643 UNLOCK_FUNCTION(monitor_lock_) {
644 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800645 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800646 ThrowLocation saved_throw_location;
TDYa1273d71d802012-08-15 03:47:03 -0700647 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800648 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700649 self->ClearException();
650 }
651 // Decode locked object and unlock, before popping local references.
652 self->DecodeJObject(locked)->MonitorExit(self);
653 if (UNLIKELY(self->IsExceptionPending())) {
654 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
655 << saved_exception->Dump()
656 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800657 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700658 }
659 // Restore pending exception.
660 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800661 self->SetException(saved_throw_location, saved_exception);
TDYa1273d71d802012-08-15 03:47:03 -0700662 }
663}
664
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800665static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700666 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700667 if (o == NULL) {
668 return;
669 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700670 mirror::ArtMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700671 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800672 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700673 }
674 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers62d6c772013-02-27 08:32:07 -0800675 mirror::Class* return_type = MethodHelper(m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700676
677 if (!o->InstanceOf(return_type)) {
678 JniAbortF(NULL, "attempt to return an instance of %s from %s",
679 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
680 }
681}
682
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800683static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800684 for (;;) {
685 if (thread->ReadFlag(kCheckpointRequest)) {
686 thread->RunCheckpointFunction();
jeffhao373c52f2012-11-20 16:11:52 -0800687 } else if (thread->ReadFlag(kSuspendRequest)) {
688 thread->FullSuspendCheck();
689 } else {
690 break;
691 }
692 }
693}
694
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800695JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800697 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700698 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800699
Jeff Hao58df3272013-04-22 15:28:53 -0700700// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700701extern "C" void art_quick_deoptimize();
702static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700703 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
704}
705
706// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700707extern "C" void art_quick_instrumentation_entry(void*);
708static inline void* GetQuickInstrumentationEntryPoint() {
709 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700710}
711
712// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700713extern "C" void art_quick_instrumentation_exit();
714static inline uintptr_t GetQuickInstrumentationExitPc() {
715 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
716}
717
Brian Carlstromea46f952013-07-30 01:26:50 -0700718extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700719static inline const void* GetPortableToInterpreterBridge() {
720 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
721}
722
Ian Rogersef7d42f2014-01-06 12:55:46 -0800723static inline const void* GetPortableToQuickBridge() {
724 // TODO: portable to quick bridge. Bug: 8196384
725 return GetPortableToInterpreterBridge();
726}
727
Brian Carlstromea46f952013-07-30 01:26:50 -0700728extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700729static inline const void* GetQuickToInterpreterBridge() {
730 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700731}
732
Ian Rogersef7d42f2014-01-06 12:55:46 -0800733static inline const void* GetQuickToPortableBridge() {
734 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700735 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700736}
737
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700738static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
739 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700740}
741
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700742static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
743 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700744}
745
Jeff Hao88474b42013-10-23 16:24:40 -0700746static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) {
747 return class_linker->GetPortableImtConflictTrampoline();
748}
749
750static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) {
751 return class_linker->GetQuickImtConflictTrampoline();
752}
753
Ian Rogers848871b2013-08-05 10:56:33 -0700754extern "C" void art_portable_proxy_invoke_handler();
755static inline const void* GetPortableProxyInvokeHandler() {
756 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700757}
758
Ian Rogers848871b2013-08-05 10:56:33 -0700759extern "C" void art_quick_proxy_invoke_handler();
760static inline const void* GetQuickProxyInvokeHandler() {
761 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700762}
763
Ian Rogers848871b2013-08-05 10:56:33 -0700764extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700765static inline void* GetJniDlsymLookupStub() {
766 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
767}
Jeff Hao58df3272013-04-22 15:28:53 -0700768
Ian Rogers450dcb52013-09-20 17:36:02 -0700769template <typename INT_TYPE, typename FLOAT_TYPE>
770static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
771 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
772 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
773 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
774 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
775 if (LIKELY(f > kMinIntAsFloat)) {
776 if (LIKELY(f < kMaxIntAsFloat)) {
777 return static_cast<INT_TYPE>(f);
778 } else {
779 return kMaxInt;
780 }
781 } else {
782 return (f != f) ? 0 : kMinInt; // f != f implies NaN
783 }
784}
785
Shih-wei Liao2d831012011-09-28 22:06:53 -0700786} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700787
Ian Rogers7655f292013-07-29 11:07:13 -0700788#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_