blob: ff836a4745035557d87071b6f5f5adcd943f932e [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 {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700330 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
331 resolved_field->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,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700388 mirror::Object** this_object,
389 mirror::ArtMethod** referrer, Thread* self) {
390 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
391 mirror::ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer, type);
392 if (resolved_method == nullptr) {
393 StackHandleScope<1> hs(self);
394 mirror::Object* null_this = nullptr;
395 HandleWrapper<mirror::Object> h_this(
396 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
397 resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type);
398 }
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200399 if (UNLIKELY(resolved_method == nullptr)) {
400 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
401 return nullptr; // Failure.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700402 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200403 // Maintain interpreter-like semantics where NullPointerException is thrown
404 // after potential NoSuchMethodError from class linker.
405 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700406 DCHECK_EQ(*referrer, throw_location.GetMethod());
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200407 ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
408 return nullptr; // Failure.
409 } else if (access_check) {
410 // Incompatible class change should have been handled in resolve method.
411 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
412 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700413 *referrer);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200414 return nullptr; // Failure.
415 }
416 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700417 mirror::Class* referring_class = (*referrer)->GetDeclaringClass();
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800418 bool can_access_resolved_method =
Vladimir Marko89786432014-01-31 15:03:55 +0000419 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
420 method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800421 if (UNLIKELY(!can_access_resolved_method)) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000422 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
423 return nullptr; // Failure.
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200424 }
425 }
426 switch (type) {
427 case kStatic:
428 case kDirect:
429 return resolved_method;
430 case kVirtual: {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700431 mirror::ObjectArray<mirror::ArtMethod>* vtable = (*this_object)->GetClass()->GetVTable();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200432 uint16_t vtable_index = resolved_method->GetMethodIndex();
433 if (access_check &&
434 (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) {
435 // Behavior to agree with that of the verifier.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700436 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
437 resolved_method->GetName(), resolved_method->GetSignature());
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200438 return nullptr; // Failure.
439 }
440 DCHECK(vtable != nullptr);
441 return vtable->GetWithoutChecks(vtable_index);
442 }
443 case kSuper: {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700444 mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass();
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200445 uint16_t vtable_index = resolved_method->GetMethodIndex();
446 mirror::ObjectArray<mirror::ArtMethod>* vtable;
447 if (access_check) {
448 // Check existence of super class.
449 vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr;
450 if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) {
451 // Behavior to agree with that of the verifier.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700452 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
453 resolved_method->GetName(), resolved_method->GetSignature());
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200454 return nullptr; // Failure.
455 }
456 } else {
457 // Super class must exist.
458 DCHECK(super_class != nullptr);
459 vtable = super_class->GetVTable();
460 }
461 DCHECK(vtable != nullptr);
462 return vtable->GetWithoutChecks(vtable_index);
463 }
464 case kInterface: {
Jeff Hao88474b42013-10-23 16:24:40 -0700465 uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize;
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700466 mirror::ObjectArray<mirror::ArtMethod>* imt_table = (*this_object)->GetClass()->GetImTable();
Jeff Hao88474b42013-10-23 16:24:40 -0700467 mirror::ArtMethod* imt_method = imt_table->Get(imt_index);
468 if (!imt_method->IsImtConflictMethod()) {
469 return imt_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200470 } else {
Jeff Hao88474b42013-10-23 16:24:40 -0700471 mirror::ArtMethod* interface_method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700472 (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method);
Jeff Hao88474b42013-10-23 16:24:40 -0700473 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier37a98762014-02-05 12:14:39 -0800474 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700475 *this_object, *referrer);
Jeff Hao88474b42013-10-23 16:24:40 -0700476 return nullptr; // Failure.
Jeff Hao88474b42013-10-23 16:24:40 -0700477 }
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700478 return interface_method;
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200479 }
480 }
481 default:
482 LOG(FATAL) << "Unknown invoke type " << type;
483 return nullptr; // Failure.
484 }
485}
486
487// Explicit template declarations of FindMethodFromCode for all invoke types.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100488#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
489 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \
490 mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700491 mirror::Object** this_object, \
492 mirror::ArtMethod** referrer, \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100493 Thread* self)
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200494#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
495 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
496 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
497
498EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
499EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
500EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
501EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
502EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
503
504#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
505#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
Ian Rogers57b86d42012-03-27 16:05:41 -0700506
Ian Rogers08f753d2012-08-24 14:35:25 -0700507// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700508static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700510 FindFieldType type, size_t expected_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 mirror::ArtField* resolved_field =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx);
Ian Rogers6c5cb212014-06-18 16:07:20 -0700514 if (UNLIKELY(resolved_field == nullptr)) {
515 return nullptr;
Ian Rogers57b86d42012-03-27 16:05:41 -0700516 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700517 // Check for incompatible class change.
518 bool is_primitive;
519 bool is_set;
520 bool is_static;
521 switch (type) {
522 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
523 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
524 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
525 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
526 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
527 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
528 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
529 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700530 default:
531 LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings.
532 is_primitive = true;
533 is_set = true;
534 is_static = true;
535 break;
Ian Rogers08f753d2012-08-24 14:35:25 -0700536 }
537 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
538 // Incompatible class change.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700539 return nullptr;
540 }
541 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
542 if (is_static) {
543 // Check class is initialized else fail so that we can contend to initialize the class with
544 // other threads that may be racing to do this.
545 if (UNLIKELY(!fields_class->IsInitialized())) {
546 return nullptr;
547 }
Ian Rogers08f753d2012-08-24 14:35:25 -0700548 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700550 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
551 !referring_class->CanAccessMember(fields_class,
552 resolved_field->GetAccessFlags()) ||
553 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700554 // Illegal access.
Ian Rogers6c5cb212014-06-18 16:07:20 -0700555 return nullptr;
Ian Rogers57b86d42012-03-27 16:05:41 -0700556 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700557 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
558 resolved_field->FieldSize() != expected_size)) {
Ian Rogers6c5cb212014-06-18 16:07:20 -0700559 return nullptr;
Ian Rogers57b86d42012-03-27 16:05:41 -0700560 }
561 return resolved_field;
562}
563
Ian Rogers08f753d2012-08-24 14:35:25 -0700564// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700565static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
566 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800567 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700568 bool access_check, InvokeType type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700569 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700570 bool is_direct = type == kStatic || type == kDirect;
571 if (UNLIKELY(this_object == NULL && !is_direct)) {
572 return NULL;
573 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 mirror::ArtMethod* resolved_method =
Ian Rogers57b86d42012-03-27 16:05:41 -0700575 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx);
576 if (UNLIKELY(resolved_method == NULL)) {
577 return NULL;
578 }
579 if (access_check) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700580 // Check for incompatible class change errors and access.
581 bool icce = resolved_method->CheckIncompatibleClassChange(type);
582 if (UNLIKELY(icce)) {
583 return NULL;
584 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800585 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
586 mirror::Class* referring_class = referrer->GetDeclaringClass();
Ian Rogers57b86d42012-03-27 16:05:41 -0700587 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
588 !referring_class->CanAccessMember(methods_class,
589 resolved_method->GetAccessFlags()))) {
Ian Rogers08f753d2012-08-24 14:35:25 -0700590 // Potential illegal access, may need to refine the method's class.
Ian Rogers57b86d42012-03-27 16:05:41 -0700591 return NULL;
592 }
593 }
594 if (type == kInterface) { // Most common form of slow path dispatch.
595 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
596 } else if (is_direct) {
597 return resolved_method;
598 } else if (type == kSuper) {
599 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()->
600 Get(resolved_method->GetMethodIndex());
601 } else {
602 DCHECK(type == kVirtual);
603 return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex());
604 }
605}
606
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700607static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800608 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700609 Thread* self, bool can_run_clinit,
610 bool verify_access)
611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
612 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
613 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800614 if (UNLIKELY(klass == nullptr)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700615 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800616 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700617 }
618 // Perform access check if necessary.
619 mirror::Class* referring_class = referrer->GetDeclaringClass();
620 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
621 ThrowIllegalAccessErrorClass(referring_class, klass);
Ian Rogers5ddb4102014-01-07 08:58:46 -0800622 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700623 }
624 // If we're just implementing const-class, we shouldn't call <clinit>.
625 if (!can_run_clinit) {
626 return klass;
627 }
628 // If we are the <clinit> of this class, just return our storage.
629 //
630 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
631 // running.
Ian Rogers241b5de2013-10-09 17:58:57 -0700632 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700633 return klass;
634 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700635 StackHandleScope<1> hs(self);
636 Handle<mirror::Class> h_class(hs.NewHandle(klass));
637 if (!class_linker->EnsureInitialized(h_class, true, true)) {
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700638 CHECK(self->IsExceptionPending());
Ian Rogers5ddb4102014-01-07 08:58:46 -0800639 return nullptr; // Failure - Indicate to caller to deliver exception
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700640 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700641 return h_class.Get();
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700642}
Ian Rogers57b86d42012-03-27 16:05:41 -0700643
jeffhaod7521322012-11-21 15:38:24 -0800644extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
645
Ian Rogersef7d42f2014-01-06 12:55:46 -0800646static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647 uint32_t string_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700648 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700649 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
650 return class_linker->ResolveString(string_idx, referrer);
651}
Shih-wei Liao2d831012011-09-28 22:06:53 -0700652
TDYa1273d71d802012-08-15 03:47:03 -0700653static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogers719d1a32014-03-06 12:13:39 -0800654 NO_THREAD_SAFETY_ANALYSIS /* SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) */ {
TDYa1273d71d802012-08-15 03:47:03 -0700655 // Save any pending exception over monitor exit call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656 mirror::Throwable* saved_exception = NULL;
Ian Rogers62d6c772013-02-27 08:32:07 -0800657 ThrowLocation saved_throw_location;
Sebastien Hertz9f102032014-05-23 08:59:42 +0200658 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
TDYa1273d71d802012-08-15 03:47:03 -0700659 if (UNLIKELY(self->IsExceptionPending())) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800660 saved_exception = self->GetException(&saved_throw_location);
TDYa1273d71d802012-08-15 03:47:03 -0700661 self->ClearException();
662 }
663 // Decode locked object and unlock, before popping local references.
664 self->DecodeJObject(locked)->MonitorExit(self);
665 if (UNLIKELY(self->IsExceptionPending())) {
666 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
667 << saved_exception->Dump()
668 << "\nEncountered second exception during implicit MonitorExit:\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800669 << self->GetException(NULL)->Dump();
TDYa1273d71d802012-08-15 03:47:03 -0700670 }
671 // Restore pending exception.
672 if (saved_exception != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800673 self->SetException(saved_throw_location, saved_exception);
Sebastien Hertz9f102032014-05-23 08:59:42 +0200674 self->SetExceptionReportedToInstrumentation(is_exception_reported);
TDYa1273d71d802012-08-15 03:47:03 -0700675 }
676}
677
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800678static inline void CheckReferenceResult(mirror::Object* o, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa1273d71d802012-08-15 03:47:03 -0700680 if (o == NULL) {
681 return;
682 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700683 mirror::ArtMethod* m = self->GetCurrentMethod(NULL);
TDYa1273d71d802012-08-15 03:47:03 -0700684 if (o == kInvalidIndirectRefObject) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700686 }
687 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700688 StackHandleScope<1> hs(self);
689 Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
690 mirror::Class* return_type = MethodHelper(h_m).GetReturnType();
TDYa1273d71d802012-08-15 03:47:03 -0700691
692 if (!o->InstanceOf(return_type)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700693 JniAbortF(NULL, "attempt to return an instance of %s from %s", PrettyTypeOf(o).c_str(),
694 PrettyMethod(h_m.Get()).c_str());
TDYa1273d71d802012-08-15 03:47:03 -0700695 }
696}
697
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800698static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
jeffhao373c52f2012-11-20 16:11:52 -0800699 for (;;) {
700 if (thread->ReadFlag(kCheckpointRequest)) {
701 thread->RunCheckpointFunction();
jeffhao373c52f2012-11-20 16:11:52 -0800702 } else if (thread->ReadFlag(kSuspendRequest)) {
703 thread->FullSuspendCheck();
704 } else {
705 break;
706 }
707 }
708}
709
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700710JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700711 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800712 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700713 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800714
Jeff Hao58df3272013-04-22 15:28:53 -0700715// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700716extern "C" void art_quick_deoptimize();
717static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700718 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
719}
720
721// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700722extern "C" void art_quick_instrumentation_entry(void*);
723static inline void* GetQuickInstrumentationEntryPoint() {
724 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700725}
726
727// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700728extern "C" void art_quick_instrumentation_exit();
729static inline uintptr_t GetQuickInstrumentationExitPc() {
730 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
731}
732
Brian Carlstromea46f952013-07-30 01:26:50 -0700733extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700734static inline const void* GetPortableToInterpreterBridge() {
735 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
736}
737
Ian Rogersef7d42f2014-01-06 12:55:46 -0800738static inline const void* GetPortableToQuickBridge() {
739 // TODO: portable to quick bridge. Bug: 8196384
740 return GetPortableToInterpreterBridge();
741}
742
Brian Carlstromea46f952013-07-30 01:26:50 -0700743extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700744static inline const void* GetQuickToInterpreterBridge() {
745 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700746}
747
Ian Rogersef7d42f2014-01-06 12:55:46 -0800748static inline const void* GetQuickToPortableBridge() {
749 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700750 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700751}
752
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700753static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) {
754 return class_linker->GetPortableResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700755}
756
Jeff Hao0aba0ba2013-06-03 14:49:28 -0700757static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) {
758 return class_linker->GetQuickResolutionTrampoline();
Jeff Hao58df3272013-04-22 15:28:53 -0700759}
760
Jeff Hao88474b42013-10-23 16:24:40 -0700761static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) {
762 return class_linker->GetPortableImtConflictTrampoline();
763}
764
765static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) {
766 return class_linker->GetQuickImtConflictTrampoline();
767}
768
Vladimir Marko8a630572014-04-09 18:45:35 +0100769static inline const void* GetQuickToInterpreterBridgeTrampoline(ClassLinker* class_linker) {
770 return class_linker->GetQuickToInterpreterBridgeTrampoline();
771}
772
Ian Rogers848871b2013-08-05 10:56:33 -0700773extern "C" void art_portable_proxy_invoke_handler();
774static inline const void* GetPortableProxyInvokeHandler() {
775 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700776}
777
Ian Rogers848871b2013-08-05 10:56:33 -0700778extern "C" void art_quick_proxy_invoke_handler();
779static inline const void* GetQuickProxyInvokeHandler() {
780 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700781}
782
Ian Rogers848871b2013-08-05 10:56:33 -0700783extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700784static inline void* GetJniDlsymLookupStub() {
785 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
786}
Jeff Hao58df3272013-04-22 15:28:53 -0700787
Ian Rogers450dcb52013-09-20 17:36:02 -0700788template <typename INT_TYPE, typename FLOAT_TYPE>
789static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
790 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
791 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
792 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
793 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
794 if (LIKELY(f > kMinIntAsFloat)) {
795 if (LIKELY(f < kMaxIntAsFloat)) {
796 return static_cast<INT_TYPE>(f);
797 } else {
798 return kMaxInt;
799 }
800 } else {
801 return (f != f) ? 0 : kMinInt; // f != f implies NaN
802 }
803}
804
Shih-wei Liao2d831012011-09-28 22:06:53 -0700805} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700806
Ian Rogers7655f292013-07-29 11:07:13 -0700807#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_