blob: 2ced942240ec142370b38d26abdcf74bd0634be6 [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"
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,
Ian Rogers6fac4472014-02-25 17:01:10 -0800231 klass->GetComponentSize(),
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800232 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700233 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800234 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
235 klass->GetComponentSize(), allocator_type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700236}
237
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800238template <bool kAccessCheck, bool kInstrumented>
239ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
240 mirror::ArtMethod* method,
241 int32_t component_count,
242 Thread* self,
243 gc::AllocatorType allocator_type)
244 NO_THREAD_SAFETY_ANALYSIS {
245 DCHECK(klass != nullptr);
246 if (UNLIKELY(component_count < 0)) {
247 ThrowNegativeArraySizeException(component_count);
248 return nullptr; // Failure
249 }
250 if (kAccessCheck) {
251 mirror::Class* referrer = method->GetDeclaringClass();
252 if (UNLIKELY(!referrer->CanAccess(klass))) {
253 ThrowIllegalAccessErrorClass(referrer, klass);
254 return nullptr; // Failure
255 }
256 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800257 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
258 // suspension.
259 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
260 klass->GetComponentSize(), allocator_type);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800261}
262
Brian Carlstromea46f952013-07-30 01:26:50 -0700263extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800264 int32_t component_count, Thread* self,
265 bool access_check,
266 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700268
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800269extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
270 mirror::ArtMethod* method,
271 int32_t component_count, Thread* self,
272 bool access_check,
273 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
275
Ian Rogers08f753d2012-08-24 14:35:25 -0700276// Type of find field operation for fast and slow case.
277enum FindFieldType {
278 InstanceObjectRead,
279 InstanceObjectWrite,
280 InstancePrimitiveRead,
281 InstancePrimitiveWrite,
282 StaticObjectRead,
283 StaticObjectWrite,
284 StaticPrimitiveRead,
285 StaticPrimitiveWrite,
286};
287
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200288template<FindFieldType type, bool access_check>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800289static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200290 Thread* self, size_t expected_size) {
291 bool is_primitive;
292 bool is_set;
293 bool is_static;
294 switch (type) {
295 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
296 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
297 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
298 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
299 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
300 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
301 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
302 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
303 default: is_primitive = true; is_set = true; is_static = true; break;
304 }
305 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
306 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
307 if (UNLIKELY(resolved_field == nullptr)) {
308 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
309 return nullptr; // Failure.
310 }
311 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
312 if (access_check) {
313 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
314 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
315 return nullptr;
316 }
317 mirror::Class* referring_class = referrer->GetDeclaringClass();
Vladimir Marko89786432014-01-31 15:03:55 +0000318 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
319 field_idx))) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000320 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
321 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200322 }
323 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
324 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Vladimir Marko23a28212014-01-09 19:24:37 +0000325 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200326 } else {
327 FieldHelper fh(resolved_field);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || fh.FieldSize() != expected_size)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200329 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
330 DCHECK(throw_location.GetMethod() == referrer);
331 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
332 "Attempted read of %zd-bit %s on field '%s'",
333 expected_size * (32 / sizeof(int32_t)),
334 is_primitive ? "primitive" : "non-primitive",
335 PrettyField(resolved_field, true).c_str());
Vladimir Marko23a28212014-01-09 19:24:37 +0000336 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200337 }
338 }
339 }
340 if (!is_static) {
341 // instance fields must be being accessed on an initialized class
342 return resolved_field;
343 } else {
344 // If the class is initialized we're done.
345 if (LIKELY(fields_class->IsInitialized())) {
346 return resolved_field;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200347 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800348 SirtRef<mirror::Class> sirt_class(self, fields_class);
349 if (LIKELY(class_linker->EnsureInitialized(sirt_class, true, true))) {
350 // Otherwise let's ensure the class is initialized before resolving the field.
351 return resolved_field;
352 } else {
353 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
Vladimir Marko23a28212014-01-09 19:24:37 +0000354 return nullptr; // Failure.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800355 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200356 }
357 }
358}
359
360// Explicit template declarations of FindFieldFromCode for all field access types.
361#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
362template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100363mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364 mirror::ArtMethod* referrer, \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100365 Thread* self, size_t expected_size) \
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200366
367#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
368 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
369 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
370
371EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
372EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
373EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
374EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
375EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
376EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
377EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
378EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
379
380#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
381#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
382
383template<InvokeType type, bool access_check>
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800384static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
385 mirror::Object* this_object,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200386 mirror::ArtMethod* referrer, Thread* self) {
387 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800388 SirtRef<mirror::Object> sirt_this(self, type == kStatic ? nullptr : this_object);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200389 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
390 if (UNLIKELY(resolved_method == nullptr)) {
391 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
392 return nullptr; // Failure.
Mathieu Chartier37a98762014-02-05 12:14:39 -0800393 } else if (UNLIKELY(sirt_this.get() == nullptr && type != kStatic)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200394 // Maintain interpreter-like semantics where NullPointerException is thrown
395 // after potential NoSuchMethodError from class linker.
396 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
397 DCHECK(referrer == throw_location.GetMethod());
398 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
399 return nullptr; // Failure.
400 } else if (access_check) {
401 // Incompatible class change should have been handled in resolve method.
402 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
403 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
404 referrer);
405 return nullptr; // Failure.
406 }
407 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
408 mirror::Class* referring_class = referrer->GetDeclaringClass();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800409 bool can_access_resolved_method =
Vladimir Marko89786432014-01-31 15:03:55 +0000410 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
411 method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800412 if (UNLIKELY(!can_access_resolved_method)) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000413 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
414 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200415 }
416 }
417 switch (type) {
418 case kStatic:
419 case kDirect:
420 return resolved_method;
421 case kVirtual: {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800422 mirror::ObjectArray<mirror::ArtMethod>* vtable = sirt_this->GetClass()->GetVTable();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200423 uint16_t vtable_index = resolved_method->GetMethodIndex();
424 if (access_check &&
425 (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) {
426 // Behavior to agree with that of the verifier.
427 MethodHelper mh(resolved_method);
428 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
429 mh.GetSignature());
430 return nullptr; // Failure.
431 }
432 DCHECK(vtable != nullptr);
433 return vtable->GetWithoutChecks(vtable_index);
434 }
435 case kSuper: {
436 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
437 uint16_t vtable_index = resolved_method->GetMethodIndex();
438 mirror::ObjectArray<mirror::ArtMethod>* vtable;
439 if (access_check) {
440 // Check existence of super class.
441 vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr;
442 if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) {
443 // Behavior to agree with that of the verifier.
444 MethodHelper mh(resolved_method);
445 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
446 mh.GetSignature());
447 return nullptr; // Failure.
448 }
449 } else {
450 // Super class must exist.
451 DCHECK(super_class != nullptr);
452 vtable = super_class->GetVTable();
453 }
454 DCHECK(vtable != nullptr);
455 return vtable->GetWithoutChecks(vtable_index);
456 }
457 case kInterface: {
Jeff Hao88474b42013-10-23 16:24:40 -0700458 uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize;
Mathieu Chartier37a98762014-02-05 12:14:39 -0800459 mirror::ObjectArray<mirror::ArtMethod>* imt_table = sirt_this->GetClass()->GetImTable();
Jeff Hao88474b42013-10-23 16:24:40 -0700460 mirror::ArtMethod* imt_method = imt_table->Get(imt_index);
461 if (!imt_method->IsImtConflictMethod()) {
462 return imt_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200463 } else {
Jeff Hao88474b42013-10-23 16:24:40 -0700464 mirror::ArtMethod* interface_method =
Mathieu Chartier37a98762014-02-05 12:14:39 -0800465 sirt_this->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao88474b42013-10-23 16:24:40 -0700466 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800467 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
468 sirt_this.get(), referrer);
Jeff Hao88474b42013-10-23 16:24:40 -0700469 return nullptr; // Failure.
470 } else {
471 return interface_method;
472 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200473 }
474 }
475 default:
476 LOG(FATAL) << "Unknown invoke type " << type;
477 return nullptr; // Failure.
478 }
479}
480
481// Explicit template declarations of FindMethodFromCode for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100482#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
483 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
484 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
485 mirror::Object* this_object, \
486 mirror::ArtMethod* referrer, \
487 Thread* self)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200488#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
489 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
490 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
491
492EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
493EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
494EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
495EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
496EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
497
498#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
499#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
Ian Rogers57b86d42012-03-27 16:05:41 -0700500
Ian Rogers08f753d2012-08-24 14:35:25 -0700501// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700502static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800503 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700504 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700506 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700508 if (UNLIKELY(resolved_field == NULL)) {
509 return NULL;
510 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700512 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700513 if (UNLIKELY(!fields_class->IsInitializing())) {
514 return NULL;
515 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700516 // Check for incompatible class change.
517 bool is_primitive;
518 bool is_set;
519 bool is_static;
520 switch (type) {
521 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
522 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
523 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
524 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
525 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
526 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
527 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
528 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700529 default:
530 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
531 is_primitive = true;
532 is_set = true;
533 is_static = true;
534 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700535 }
536 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
537 // Incompatible class change.
538 return NULL;
539 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700541 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
542 !referring_class->CanAccessMember(fields_class,
543 resolved_field->GetAccessFlags()) ||
544 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700545 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700546 return NULL;
547 }
548 FieldHelper fh(resolved_field);
549 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
550 fh.FieldSize() != expected_size)) {
551 return NULL;
552 }
553 return resolved_field;
554}
555
Ian Rogers08f753d2012-08-24 14:35:25 -0700556// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700557static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
558 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800559 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700560 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700562 bool is_direct = type == kStatic || type == kDirect;
563 if (UNLIKELY(this_object == NULL && !is_direct)) {
564 return NULL;
565 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700567 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
568 if (UNLIKELY(resolved_method == NULL)) {
569 return NULL;
570 }
571 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700572 // Check for incompatible class change errors and access.
573 bool icce = resolved_method->CheckIncompatibleClassChange(type);
574 if (UNLIKELY(icce)) {
575 return NULL;
576 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
578 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700579 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
580 !referring_class->CanAccessMember(methods_class,
581 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700582 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700583 return NULL;
584 }
585 }
586 if (type == kInterface) { // Most common form of slow path dispatch.
587 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
588 } else if (is_direct) {
589 return resolved_method;
590 } else if (type == kSuper) {
591 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
592 Get(resolved_method->GetMethodIndex());
593 } else {
594 DCHECK(type == kVirtual);
595 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
596 }
597}
598
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700599static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800600 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700601 Thread* self, bool can_run_clinit,
602 bool verify_access)
603 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
604 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
605 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800606 if (UNLIKELY(klass == nullptr)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700607 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800608 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700609 }
610 // Perform access check if necessary.
611 mirror::Class* referring_class = referrer->GetDeclaringClass();
612 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
613 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700615 }
616 // If we're just implementing const-class, we shouldn't call <clinit>.
617 if (!can_run_clinit) {
618 return klass;
619 }
620 // If we are the <clinit> of this class, just return our storage.
621 //
622 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
623 // running.
Ian Rogers241b5de2013-10-09 17:58:57 -0700624 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700625 return klass;
626 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800627 SirtRef<mirror::Class> sirt_class(self, klass);
628 if (!class_linker->EnsureInitialized(sirt_class, true, true)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700629 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800630 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700631 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800632 return sirt_class.get();
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700633}
Ian Rogers57b86d42012-03-27 16:05:41 -0700634
jeffhaod7521322012-11-21 15:38:24 -0800635extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
636
Ian Rogersef7d42f2014-01-06 12:55:46 -0800637static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700639 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700640 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
641 return class_linker->ResolveString(string_idx, referrer);
642}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700643
TDYa1273d71d802012-08-15 03:47:03 -0700644static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700645 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
TDYa1273d71d802012-08-15 03:47:03 -0700646 UNLOCK_FUNCTION(monitor_lock_) {
647 // 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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800736static inline const void* GetQuickToPortableBridge() {
737 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700738 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700739}
740
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700741static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
742 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700743}
744
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700745static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
746 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700747}
748
Jeff Hao88474b42013-10-23 16:24:40 -0700749static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) {
750 return class_linker->GetPortableImtConflictTrampoline();
751}
752
753static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) {
754 return class_linker->GetQuickImtConflictTrampoline();
755}
756
Ian Rogers848871b2013-08-05 10:56:33 -0700757extern "C" void art_portable_proxy_invoke_handler();
758static inline const void* GetPortableProxyInvokeHandler() {
759 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700760}
761
Ian Rogers848871b2013-08-05 10:56:33 -0700762extern "C" void art_quick_proxy_invoke_handler();
763static inline const void* GetQuickProxyInvokeHandler() {
764 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700765}
766
Ian Rogers848871b2013-08-05 10:56:33 -0700767extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700768static inline void* GetJniDlsymLookupStub() {
769 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
770}
Jeff Hao58df3272013-04-22 15:28:53 -0700771
Ian Rogers450dcb52013-09-20 17:36:02 -0700772template <typename INT_TYPE, typename FLOAT_TYPE>
773static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
774 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
775 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
776 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
777 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
778 if (LIKELY(f > kMinIntAsFloat)) {
779 if (LIKELY(f < kMaxIntAsFloat)) {
780 return static_cast<INT_TYPE>(f);
781 } else {
782 return kMaxInt;
783 }
784 } else {
785 return (f != f) ? 0 : kMinInt; // f != f implies NaN
786 }
787}
788
Shih-wei Liao2d831012011-09-28 22:06:53 -0700789} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700790
Ian Rogers7655f292013-07-29 11:07:13 -0700791#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_