blob: 6998e2157297689d1b41d3a5556cb61942844d25 [file] [log] [blame]
Elliott Hughes0f3c5532012-03-30 14:51:51 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
buzbee54330722011-08-23 16:46:55 -070016
Ian Rogers7655f292013-07-29 11:07:13 -070017#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
Ian Rogers450dcb52013-09-20 17:36:02 -070019
20#include "base/macros.h"
Ian Rogers98379392014-02-24 16:53:16 -080021#include "class_linker-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070022#include "common_throws.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070023#include "dex_file.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070024#include "indirect_reference_table.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070025#include "invoke_type.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070026#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/array.h"
Ian Rogers693ff612013-02-01 10:56:12 -080029#include "mirror/class-inl.h"
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080030#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/throwable.h"
Ian Rogers450dcb52013-09-20 17:36:02 -070032#include "object_utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070033#include "handle_scope-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070034#include "thread.h"
35
Shih-wei Liao2d831012011-09-28 22:06:53 -070036namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070037
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038namespace mirror {
Ian Rogers848871b2013-08-05 10:56:33 -070039 class Class;
Brian Carlstromea46f952013-07-30 01:26:50 -070040 class ArtField;
Ian Rogers848871b2013-08-05 10:56:33 -070041 class Object;
42} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070043
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080044// TODO: Fix no thread safety analysis when GCC can handle template specialization.
45template <const bool kAccessCheck>
46ALWAYS_INLINE static inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
47 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080048 Thread* self, bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080049 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070050 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070051 if (UNLIKELY(klass == NULL)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080052 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080053 *slow_path = true;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070054 if (klass == NULL) {
55 DCHECK(self->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080056 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070057 }
58 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080059 if (kAccessCheck) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070060 if (UNLIKELY(!klass->IsInstantiable())) {
61 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
62 self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;",
63 PrettyDescriptor(klass).c_str());
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080064 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080065 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070066 }
67 mirror::Class* referrer = method->GetDeclaringClass();
68 if (UNLIKELY(!referrer->CanAccess(klass))) {
69 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080070 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080071 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070072 }
73 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080074 if (UNLIKELY(!klass->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070075 StackHandleScope<1> hs(self);
76 Handle<mirror::Class> h_klass(hs.NewHandle(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 Chartiereb8167a2014-05-07 15:43:14 -070086 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_klass, true, true)) {
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080087 DCHECK(self->IsExceptionPending());
88 return nullptr; // Failure
89 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070090 return h_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())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 StackHandleScope<1> hs(self);
101 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800102 // EnsureInitialized (the class initializer) might cause a GC.
103 // may cause us to suspend meaning that another thread may try to
104 // change the allocator while we are stuck in the entrypoints of
105 // an old allocator. Also, the class initialization may fail. To
106 // handle these cases we mark the slow path boolean as true so
107 // that the caller knows to check the allocator type to see if it
108 // has changed and to null-check the return value in case the
109 // initialization fails.
110 *slow_path = true;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800112 DCHECK(self->IsExceptionPending());
113 return nullptr; // Failure
114 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 return h_class.Get();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800116 }
117 return klass;
118}
119
Ian Rogers57b86d42012-03-27 16:05:41 -0700120// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
121// cannot be resolved, throw an error. If it can, use it to create an instance.
122// When verification/compiler hasn't been able to verify access, optionally perform an access
123// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800124// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
125template <bool kAccessCheck, bool kInstrumented>
126ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
127 mirror::ArtMethod* method,
128 Thread* self,
129 gc::AllocatorType allocator_type)
130 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800131 bool slow_path = false;
132 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
133 if (UNLIKELY(slow_path)) {
134 if (klass == nullptr) {
135 return nullptr;
136 }
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700137 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->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();
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700159 // Pass in false since the object can not be finalizable.
160 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800161 }
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700162 // Pass in false since the object can not be finalizable.
163 return klass->Alloc<kInstrumented, false>(self, allocator_type);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800164}
165
166// Given the context of a calling Method and an initialized class, create an instance.
167// TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter.
168template <bool kInstrumented>
169ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
170 mirror::ArtMethod* method,
171 Thread* self,
172 gc::AllocatorType allocator_type)
173 NO_THREAD_SAFETY_ANALYSIS {
174 DCHECK(klass != nullptr);
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700175 // Pass in false since the object can not be finalizable.
176 return klass->Alloc<kInstrumented, false>(self, allocator_type);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800177}
178
179
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800180// TODO: Fix no thread safety analysis when GCC can handle template specialization.
181template <bool kAccessCheck>
182ALWAYS_INLINE static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
183 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800184 int32_t component_count,
185 bool* slow_path)
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800186 NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700187 if (UNLIKELY(component_count < 0)) {
188 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800189 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800190 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700191 }
192 mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800193 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700194 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800195 *slow_path = true;
196 if (klass == nullptr) { // Error
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700197 DCHECK(Thread::Current()->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800198 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700199 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700200 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
Ian Rogers57b86d42012-03-27 16:05:41 -0700201 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800202 if (kAccessCheck) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 mirror::Class* referrer = method->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700204 if (UNLIKELY(!referrer->CanAccess(klass))) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700205 ThrowIllegalAccessErrorClass(referrer, klass);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800206 *slow_path = true;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800207 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -0700208 }
209 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800210 return klass;
Ian Rogers57b86d42012-03-27 16:05:41 -0700211}
212
213// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
214// it cannot be resolved, throw an error. If it can, use it to create an array.
215// When verification/compiler hasn't been able to verify access, optionally perform an access
216// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800217// TODO: Fix no thread safety analysis when GCC can handle template specialization.
218template <bool kAccessCheck, bool kInstrumented>
219ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
220 mirror::ArtMethod* method,
221 int32_t component_count,
222 Thread* self,
223 gc::AllocatorType allocator_type)
224 NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800225 bool slow_path = false;
226 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, method, component_count,
227 &slow_path);
228 if (UNLIKELY(slow_path)) {
229 if (klass == nullptr) {
230 return nullptr;
231 }
232 gc::Heap* heap = Runtime::Current()->GetHeap();
233 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Ian Rogers6fac4472014-02-25 17:01:10 -0800234 klass->GetComponentSize(),
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800235 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700236 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800237 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
238 klass->GetComponentSize(), allocator_type);
Ian Rogers57b86d42012-03-27 16:05:41 -0700239}
240
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800241template <bool kAccessCheck, bool kInstrumented>
242ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
243 mirror::ArtMethod* method,
244 int32_t component_count,
245 Thread* self,
246 gc::AllocatorType allocator_type)
247 NO_THREAD_SAFETY_ANALYSIS {
248 DCHECK(klass != nullptr);
249 if (UNLIKELY(component_count < 0)) {
250 ThrowNegativeArraySizeException(component_count);
251 return nullptr; // Failure
252 }
253 if (kAccessCheck) {
254 mirror::Class* referrer = method->GetDeclaringClass();
255 if (UNLIKELY(!referrer->CanAccess(klass))) {
256 ThrowIllegalAccessErrorClass(referrer, klass);
257 return nullptr; // Failure
258 }
259 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800260 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
261 // suspension.
262 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
263 klass->GetComponentSize(), allocator_type);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800264}
265
Brian Carlstromea46f952013-07-30 01:26:50 -0700266extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800267 int32_t component_count, Thread* self,
268 bool access_check,
269 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700271
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800272extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
273 mirror::ArtMethod* method,
274 int32_t component_count, Thread* self,
275 bool access_check,
276 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700277 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
278
Ian Rogers08f753d2012-08-24 14:35:25 -0700279// Type of find field operation for fast and slow case.
280enum FindFieldType {
281 InstanceObjectRead,
282 InstanceObjectWrite,
283 InstancePrimitiveRead,
284 InstancePrimitiveWrite,
285 StaticObjectRead,
286 StaticObjectWrite,
287 StaticPrimitiveRead,
288 StaticPrimitiveWrite,
289};
290
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200291template<FindFieldType type, bool access_check>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200293 Thread* self, size_t expected_size) {
294 bool is_primitive;
295 bool is_set;
296 bool is_static;
297 switch (type) {
298 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
299 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
300 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
301 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
302 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
303 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
304 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
305 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
306 default: is_primitive = true; is_set = true; is_static = true; break;
307 }
308 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
309 mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
310 if (UNLIKELY(resolved_field == nullptr)) {
311 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
312 return nullptr; // Failure.
313 }
314 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
315 if (access_check) {
316 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
317 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
318 return nullptr;
319 }
320 mirror::Class* referring_class = referrer->GetDeclaringClass();
Vladimir Marko89786432014-01-31 15:03:55 +0000321 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
322 field_idx))) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000323 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
324 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200325 }
326 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
327 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
Vladimir Marko23a28212014-01-09 19:24:37 +0000328 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200329 } else {
330 FieldHelper fh(resolved_field);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800331 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || fh.FieldSize() != expected_size)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200332 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
333 DCHECK(throw_location.GetMethod() == referrer);
334 self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
335 "Attempted read of %zd-bit %s on field '%s'",
336 expected_size * (32 / sizeof(int32_t)),
337 is_primitive ? "primitive" : "non-primitive",
338 PrettyField(resolved_field, true).c_str());
Vladimir Marko23a28212014-01-09 19:24:37 +0000339 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200340 }
341 }
342 }
343 if (!is_static) {
344 // instance fields must be being accessed on an initialized class
345 return resolved_field;
346 } else {
347 // If the class is initialized we're done.
348 if (LIKELY(fields_class->IsInitialized())) {
349 return resolved_field;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200350 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700351 StackHandleScope<1> hs(self);
352 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
353 if (LIKELY(class_linker->EnsureInitialized(h_class, true, true))) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800354 // Otherwise let's ensure the class is initialized before resolving the field.
355 return resolved_field;
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800356 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700357 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
358 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200359 }
360 }
361}
362
363// Explicit template declarations of FindFieldFromCode for all field access types.
364#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
365template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100366mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 mirror::ArtMethod* referrer, \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100368 Thread* self, size_t expected_size) \
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200369
370#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
371 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
372 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
373
374EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
375EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
376EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
377EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
378EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
379EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
380EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
381EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
382
383#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
384#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
385
386template<InvokeType type, bool access_check>
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800387static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
388 mirror::Object* this_object,
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200389 mirror::ArtMethod* referrer, Thread* self) {
390 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700391 StackHandleScope<1> hs(self);
392 Handle<mirror::Object> handle_scope_this(hs.NewHandle(type == kStatic ? nullptr : this_object));
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200393 mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
394 if (UNLIKELY(resolved_method == nullptr)) {
395 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
396 return nullptr; // Failure.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700397 } else if (UNLIKELY(handle_scope_this.Get() == nullptr && type != kStatic)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200398 // Maintain interpreter-like semantics where NullPointerException is thrown
399 // after potential NoSuchMethodError from class linker.
400 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
401 DCHECK(referrer == throw_location.GetMethod());
402 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
403 return nullptr; // Failure.
404 } else if (access_check) {
405 // Incompatible class change should have been handled in resolve method.
406 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
407 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
408 referrer);
409 return nullptr; // Failure.
410 }
411 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
412 mirror::Class* referring_class = referrer->GetDeclaringClass();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800413 bool can_access_resolved_method =
Vladimir Marko89786432014-01-31 15:03:55 +0000414 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
415 method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800416 if (UNLIKELY(!can_access_resolved_method)) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000417 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
418 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200419 }
420 }
421 switch (type) {
422 case kStatic:
423 case kDirect:
424 return resolved_method;
425 case kVirtual: {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700426 mirror::ObjectArray<mirror::ArtMethod>* vtable = handle_scope_this->GetClass()->GetVTable();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200427 uint16_t vtable_index = resolved_method->GetMethodIndex();
428 if (access_check &&
429 (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) {
430 // Behavior to agree with that of the verifier.
431 MethodHelper mh(resolved_method);
432 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
433 mh.GetSignature());
434 return nullptr; // Failure.
435 }
436 DCHECK(vtable != nullptr);
437 return vtable->GetWithoutChecks(vtable_index);
438 }
439 case kSuper: {
440 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
441 uint16_t vtable_index = resolved_method->GetMethodIndex();
442 mirror::ObjectArray<mirror::ArtMethod>* vtable;
443 if (access_check) {
444 // Check existence of super class.
445 vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr;
446 if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) {
447 // Behavior to agree with that of the verifier.
448 MethodHelper mh(resolved_method);
449 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
450 mh.GetSignature());
451 return nullptr; // Failure.
452 }
453 } else {
454 // Super class must exist.
455 DCHECK(super_class != nullptr);
456 vtable = super_class->GetVTable();
457 }
458 DCHECK(vtable != nullptr);
459 return vtable->GetWithoutChecks(vtable_index);
460 }
461 case kInterface: {
Jeff Hao88474b42013-10-23 16:24:40 -0700462 uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700463 mirror::ObjectArray<mirror::ArtMethod>* imt_table = handle_scope_this->GetClass()->GetImTable();
Jeff Hao88474b42013-10-23 16:24:40 -0700464 mirror::ArtMethod* imt_method = imt_table->Get(imt_index);
465 if (!imt_method->IsImtConflictMethod()) {
466 return imt_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200467 } else {
Jeff Hao88474b42013-10-23 16:24:40 -0700468 mirror::ArtMethod* interface_method =
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700469 handle_scope_this->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao88474b42013-10-23 16:24:40 -0700470 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800471 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700472 handle_scope_this.Get(), referrer);
Jeff Hao88474b42013-10-23 16:24:40 -0700473 return nullptr; // Failure.
474 } else {
475 return interface_method;
476 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200477 }
478 }
479 default:
480 LOG(FATAL) << "Unknown invoke type " << type;
481 return nullptr; // Failure.
482 }
483}
484
485// Explicit template declarations of FindMethodFromCode for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100486#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
487 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
488 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
489 mirror::Object* this_object, \
490 mirror::ArtMethod* referrer, \
491 Thread* self)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200492#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
493 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
494 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
495
496EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
497EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
498EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
499EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
500EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
501
502#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
503#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
Ian Rogers57b86d42012-03-27 16:05:41 -0700504
Ian Rogers08f753d2012-08-24 14:35:25 -0700505// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700506static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800507 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700508 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700509 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700510 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers57b86d42012-03-27 16:05:41 -0700512 if (UNLIKELY(resolved_field == NULL)) {
513 return NULL;
514 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
Ian Rogers08f753d2012-08-24 14:35:25 -0700516 // Check class is initiliazed or initializing.
Ian Rogers57b86d42012-03-27 16:05:41 -0700517 if (UNLIKELY(!fields_class->IsInitializing())) {
518 return NULL;
519 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700520 // Check for incompatible class change.
521 bool is_primitive;
522 bool is_set;
523 bool is_static;
524 switch (type) {
525 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
526 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
527 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
528 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
529 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
530 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
531 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
532 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700533 default:
534 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
535 is_primitive = true;
536 is_set = true;
537 is_static = true;
538 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700539 }
540 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
541 // Incompatible class change.
542 return NULL;
543 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700545 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
546 !referring_class->CanAccessMember(fields_class,
547 resolved_field->GetAccessFlags()) ||
548 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700549 // Illegal access.
Ian Rogers57b86d42012-03-27 16:05:41 -0700550 return NULL;
551 }
552 FieldHelper fh(resolved_field);
553 if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
554 fh.FieldSize() != expected_size)) {
555 return NULL;
556 }
557 return resolved_field;
558}
559
Ian Rogers08f753d2012-08-24 14:35:25 -0700560// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700561static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
562 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800563 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700566 bool is_direct = type == kStatic || type == kDirect;
567 if (UNLIKELY(this_object == NULL && !is_direct)) {
568 return NULL;
569 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700571 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
572 if (UNLIKELY(resolved_method == NULL)) {
573 return NULL;
574 }
575 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700576 // Check for incompatible class change errors and access.
577 bool icce = resolved_method->CheckIncompatibleClassChange(type);
578 if (UNLIKELY(icce)) {
579 return NULL;
580 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
582 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700583 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
584 !referring_class->CanAccessMember(methods_class,
585 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700586 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700587 return NULL;
588 }
589 }
590 if (type == kInterface) { // Most common form of slow path dispatch.
591 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
592 } else if (is_direct) {
593 return resolved_method;
594 } else if (type == kSuper) {
595 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
596 Get(resolved_method->GetMethodIndex());
597 } else {
598 DCHECK(type == kVirtual);
599 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
600 }
601}
602
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700603static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800604 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700605 Thread* self, bool can_run_clinit,
606 bool verify_access)
607 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
608 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
609 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800610 if (UNLIKELY(klass == nullptr)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700611 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800612 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700613 }
614 // Perform access check if necessary.
615 mirror::Class* referring_class = referrer->GetDeclaringClass();
616 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
617 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800618 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700619 }
620 // If we're just implementing const-class, we shouldn't call <clinit>.
621 if (!can_run_clinit) {
622 return klass;
623 }
624 // If we are the <clinit> of this class, just return our storage.
625 //
626 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
627 // running.
Ian Rogers241b5de2013-10-09 17:58:57 -0700628 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700629 return klass;
630 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700631 StackHandleScope<1> hs(self);
632 Handle<mirror::Class> h_class(hs.NewHandle(klass));
633 if (!class_linker->EnsureInitialized(h_class, true, true)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700634 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800635 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700636 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700637 return h_class.Get();
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700638}
Ian Rogers57b86d42012-03-27 16:05:41 -0700639
jeffhaod7521322012-11-21 15:38:24 -0800640extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
641
Ian Rogersef7d42f2014-01-06 12:55:46 -0800642static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800643 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700645 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
646 return class_linker->ResolveString(string_idx, referrer);
647}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700648
TDYa1273d71d802012-08-15 03:47:03 -0700649static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogers719d1a32014-03-06 12:13:39 -0800650 NO_THREAD_SAFETY_ANALYSIS /* SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) */ {
TDYa1273d71d802012-08-15 03:47:03 -0700651 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800653 ThrowLocation saved_throw_location;
TDYa1273d71d802012-08-15 03:47:03 -0700654 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800655 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700656 self->ClearException();
657 }
658 // Decode locked object and unlock, before popping local references.
659 self->DecodeJObject(locked)->MonitorExit(self);
660 if (UNLIKELY(self->IsExceptionPending())) {
661 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
662 << saved_exception->Dump()
663 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800664 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700665 }
666 // Restore pending exception.
667 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800668 self->SetException(saved_throw_location, saved_exception);
TDYa1273d71d802012-08-15 03:47:03 -0700669 }
670}
671
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700674 if (o == NULL) {
675 return;
676 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700677 mirror::ArtMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700678 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800679 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700680 }
681 // Make sure that the result is an instance of the type this method was expected to return.
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 mirror::Class* return_type = MethodHelper(m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700683
684 if (!o->InstanceOf(return_type)) {
685 JniAbortF(NULL, "attempt to return an instance of %s from %s",
686 PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str());
687 }
688}
689
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800690static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800691 for (;;) {
692 if (thread->ReadFlag(kCheckpointRequest)) {
693 thread->RunCheckpointFunction();
jeffhao373c52f2012-11-20 16:11:52 -0800694 } else if (thread->ReadFlag(kSuspendRequest)) {
695 thread->FullSuspendCheck();
696 } else {
697 break;
698 }
699 }
700}
701
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800702JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700703 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800704 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700705 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800706
Jeff Hao58df3272013-04-22 15:28:53 -0700707// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700708extern "C" void art_quick_deoptimize();
709static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700710 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
711}
712
713// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700714extern "C" void art_quick_instrumentation_entry(void*);
715static inline void* GetQuickInstrumentationEntryPoint() {
716 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700717}
718
719// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700720extern "C" void art_quick_instrumentation_exit();
721static inline uintptr_t GetQuickInstrumentationExitPc() {
722 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
723}
724
Brian Carlstromea46f952013-07-30 01:26:50 -0700725extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700726static inline const void* GetPortableToInterpreterBridge() {
727 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
728}
729
Ian Rogersef7d42f2014-01-06 12:55:46 -0800730static inline const void* GetPortableToQuickBridge() {
731 // TODO: portable to quick bridge. Bug: 8196384
732 return GetPortableToInterpreterBridge();
733}
734
Brian Carlstromea46f952013-07-30 01:26:50 -0700735extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700736static inline const void* GetQuickToInterpreterBridge() {
737 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700738}
739
Andreas Gampe2da88232014-02-27 12:26:20 -0800740extern "C" void art_quick_generic_jni_trampoline(mirror::ArtMethod*);
741static inline const void* GetQuickGenericJniTrampoline() {
742 return reinterpret_cast<void*>(art_quick_generic_jni_trampoline);
743}
744
Ian Rogersef7d42f2014-01-06 12:55:46 -0800745static inline const void* GetQuickToPortableBridge() {
746 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700747 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700748}
749
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700750static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
751 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700752}
753
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700754static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
755 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700756}
757
Jeff Hao88474b42013-10-23 16:24:40 -0700758static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) {
759 return class_linker->GetPortableImtConflictTrampoline();
760}
761
762static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) {
763 return class_linker->GetQuickImtConflictTrampoline();
764}
765
Andreas Gampe2da88232014-02-27 12:26:20 -0800766static inline const void* GetQuickGenericJniTrampoline(ClassLinker* class_linker) {
767 return class_linker->GetQuickGenericJniTrampoline();
768}
769
Vladimir Marko8a630572014-04-09 18:45:35 +0100770static inline const void* GetQuickToInterpreterBridgeTrampoline(ClassLinker* class_linker) {
771 return class_linker->GetQuickToInterpreterBridgeTrampoline();
772}
773
Ian Rogers848871b2013-08-05 10:56:33 -0700774extern "C" void art_portable_proxy_invoke_handler();
775static inline const void* GetPortableProxyInvokeHandler() {
776 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700777}
778
Ian Rogers848871b2013-08-05 10:56:33 -0700779extern "C" void art_quick_proxy_invoke_handler();
780static inline const void* GetQuickProxyInvokeHandler() {
781 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700782}
783
Ian Rogers848871b2013-08-05 10:56:33 -0700784extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700785static inline void* GetJniDlsymLookupStub() {
786 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
787}
Jeff Hao58df3272013-04-22 15:28:53 -0700788
Ian Rogers450dcb52013-09-20 17:36:02 -0700789template <typename INT_TYPE, typename FLOAT_TYPE>
790static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
791 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
792 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
793 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
794 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
795 if (LIKELY(f > kMinIntAsFloat)) {
796 if (LIKELY(f < kMaxIntAsFloat)) {
797 return static_cast<INT_TYPE>(f);
798 } else {
799 return kMaxInt;
800 }
801 } else {
802 return (f != f) ? 0 : kMinInt; // f != f implies NaN
803 }
804}
805
Shih-wei Liao2d831012011-09-28 22:06:53 -0700806} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700807
Ian Rogers7655f292013-07-29 11:07:13 -0700808#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_