blob: 3e6b4537f9fc8ff62603a05eff610f5a27ce173b [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -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 */
16
17#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19
20#include "entrypoint_utils.h"
21
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "class_linker-inl.h"
24#include "common_throws.h"
25#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010026#include "entrypoints/quick/callee_save_frame.h"
27#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "indirect_reference_table.h"
29#include "invoke_type.h"
30#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/array.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010035#include "nth_caller_visitor.h"
36#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010037#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038#include "thread.h"
39
40namespace art {
41
Mathieu Chartiere401d142015-04-22 13:56:20 -070042inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010043 const InlineInfo& inline_info,
44 uint8_t inlining_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -070045 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010046 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inlining_depth);
47 InvokeType invoke_type = static_cast<InvokeType>(
48 inline_info.GetInvokeTypeAtDepth(inlining_depth));
Mathieu Chartiere401d142015-04-22 13:56:20 -070049 ArtMethod* caller = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010050 if (!caller->IsRuntimeMethod()) {
51 return caller;
52 }
53
54 // The method in the dex cache can be the runtime method responsible for invoking
55 // the stub that will then update the dex cache. Therefore, we need to do the
56 // resolution ourselves.
Nicolas Geoffray3976e5e2015-06-15 08:58:03 +010057
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010058 // We first find the class loader of our caller. If it is the outer method, we can directly
59 // use its class loader. Otherwise, we also need to resolve our caller.
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 StackHandleScope<2> hs(Thread::Current());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010062 MutableHandle<mirror::ClassLoader> class_loader(hs.NewHandle<mirror::Class>(nullptr));
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 Handle<mirror::DexCache> dex_cache(hs.NewHandle(outer_method->GetDexCache()));
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010064 if (inlining_depth == 0) {
65 class_loader.Assign(outer_method->GetClassLoader());
66 } else {
67 caller = GetResolvedMethod(outer_method, inline_info, inlining_depth - 1);
68 class_loader.Assign(caller->GetClassLoader());
69 }
70
Andreas Gampe42ef8ab2015-12-03 17:27:32 -080071 return class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Mathieu Chartiere401d142015-04-22 13:56:20 -070072 *outer_method->GetDexFile(), method_index, dex_cache, class_loader, nullptr, invoke_type);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010073}
74
Mathieu Chartiere401d142015-04-22 13:56:20 -070075inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Mathieu Chartier90443472015-07-16 20:32:27 -070076 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +010077 return GetCalleeSaveMethodCaller(
78 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
79}
80
Mingyao Yang98d1cc82014-05-15 17:02:16 -070081template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -070082ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -080083inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080085 Thread* self, bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +010086 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
87 size_t pointer_size = class_linker->GetImagePointerSize();
88 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +010090 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070091 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070092 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070093 DCHECK(self->IsExceptionPending());
94 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -070095 } else {
96 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -070097 }
98 }
99 if (kAccessCheck) {
100 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000101 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700102 *slow_path = true;
103 return nullptr; // Failure
104 }
105 mirror::Class* referrer = method->GetDeclaringClass();
106 if (UNLIKELY(!referrer->CanAccess(klass))) {
107 ThrowIllegalAccessErrorClass(referrer, klass);
108 *slow_path = true;
109 return nullptr; // Failure
110 }
111 }
112 if (UNLIKELY(!klass->IsInitialized())) {
113 StackHandleScope<1> hs(self);
114 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
115 // EnsureInitialized (the class initializer) might cause a GC.
116 // may cause us to suspend meaning that another thread may try to
117 // change the allocator while we are stuck in the entrypoints of
118 // an old allocator. Also, the class initialization may fail. To
119 // handle these cases we mark the slow path boolean as true so
120 // that the caller knows to check the allocator type to see if it
121 // has changed and to null-check the return value in case the
122 // initialization fails.
123 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700124 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125 DCHECK(self->IsExceptionPending());
126 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700127 } else {
128 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129 }
130 return h_klass.Get();
131 }
132 return klass;
133}
134
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700135ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800136inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
137 Thread* self,
138 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 if (UNLIKELY(!klass->IsInitialized())) {
140 StackHandleScope<1> hs(self);
141 Handle<mirror::Class> h_class(hs.NewHandle(klass));
142 // EnsureInitialized (the class initializer) might cause a GC.
143 // may cause us to suspend meaning that another thread may try to
144 // change the allocator while we are stuck in the entrypoints of
145 // an old allocator. Also, the class initialization may fail. To
146 // handle these cases we mark the slow path boolean as true so
147 // that the caller knows to check the allocator type to see if it
148 // has changed and to null-check the return value in case the
149 // initialization fails.
150 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700151 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700152 DCHECK(self->IsExceptionPending());
153 return nullptr; // Failure
154 }
155 return h_class.Get();
156 }
157 return klass;
158}
159
160// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
161// cannot be resolved, throw an error. If it can, use it to create an instance.
162// When verification/compiler hasn't been able to verify access, optionally perform an access
163// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700164template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700165ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800166inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800168 Thread* self,
169 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700170 bool slow_path = false;
171 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
172 if (UNLIKELY(slow_path)) {
173 if (klass == nullptr) {
174 return nullptr;
175 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800176 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
177 return klass->Alloc</*kInstrumented*/true>(
178 self,
179 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700180 }
181 DCHECK(klass != nullptr);
182 return klass->Alloc<kInstrumented>(self, allocator_type);
183}
184
185// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700186template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700187ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800188inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
189 Thread* self,
190 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700191 DCHECK(klass != nullptr);
192 bool slow_path = false;
193 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
194 if (UNLIKELY(slow_path)) {
195 if (klass == nullptr) {
196 return nullptr;
197 }
198 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000199 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800200 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
201 // instrumented.
202 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000204 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700205 return klass->Alloc<kInstrumented, false>(self, allocator_type);
206}
207
208// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700209template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700210ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800211inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
212 Thread* self,
213 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700214 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000215 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700216 return klass->Alloc<kInstrumented, false>(self, allocator_type);
217}
218
219
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700221ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800222inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800223 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800225 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226 if (UNLIKELY(component_count < 0)) {
227 ThrowNegativeArraySizeException(component_count);
228 *slow_path = true;
229 return nullptr; // Failure
230 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100231 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
232 size_t pointer_size = class_linker->GetImagePointerSize();
233 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100235 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236 *slow_path = true;
237 if (klass == nullptr) { // Error
238 DCHECK(Thread::Current()->IsExceptionPending());
239 return nullptr; // Failure
240 }
241 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
242 }
243 if (kAccessCheck) {
244 mirror::Class* referrer = method->GetDeclaringClass();
245 if (UNLIKELY(!referrer->CanAccess(klass))) {
246 ThrowIllegalAccessErrorClass(referrer, klass);
247 *slow_path = true;
248 return nullptr; // Failure
249 }
250 }
251 return klass;
252}
253
254// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
255// it cannot be resolved, throw an error. If it can, use it to create an array.
256// When verification/compiler hasn't been able to verify access, optionally perform an access
257// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700259ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800260inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800261 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800263 Thread* self,
264 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700265 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800266 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 &slow_path);
268 if (UNLIKELY(slow_path)) {
269 if (klass == nullptr) {
270 return nullptr;
271 }
272 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800273 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
274 return mirror::Array::Alloc</*kInstrumented*/true>(self,
275 klass,
276 component_count,
277 klass->GetComponentSizeShift(),
278 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700279 }
280 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700281 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282}
283
284template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700285ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800286inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800287 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800289 Thread* self,
290 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700291 DCHECK(klass != nullptr);
292 if (UNLIKELY(component_count < 0)) {
293 ThrowNegativeArraySizeException(component_count);
294 return nullptr; // Failure
295 }
296 if (kAccessCheck) {
297 mirror::Class* referrer = method->GetDeclaringClass();
298 if (UNLIKELY(!referrer->CanAccess(klass))) {
299 ThrowIllegalAccessErrorClass(referrer, klass);
300 return nullptr; // Failure
301 }
302 }
303 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
304 // suspension.
305 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700306 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307}
308
309template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800310inline ArtField* FindFieldFromCode(uint32_t field_idx,
311 ArtMethod* referrer,
312 Thread* self,
313 size_t expected_size) REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314 bool is_primitive;
315 bool is_set;
316 bool is_static;
317 switch (type) {
318 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
319 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
320 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
321 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
322 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
323 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
324 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
325 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
326 default: is_primitive = true; is_set = true; is_static = true; break;
327 }
328 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800329
330 ArtField* resolved_field;
331 if (access_check) {
332 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
333 // qualifying type of a field and the resolved run-time qualifying type of a field differed
334 // in their static-ness.
335 //
336 // In particular, don't assume the dex instruction already correctly knows if the
337 // real field is static or not. The resolution must not be aware of this.
338 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(sizeof(void*));
339
340 StackHandleScope<2> hs(self);
341 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
342 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
343
344 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
345 field_idx,
346 h_dex_cache,
347 h_class_loader);
348 } else {
349 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
350 // be executing here if there was a static/non-static mismatch.
351 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
352 }
353
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700354 if (UNLIKELY(resolved_field == nullptr)) {
355 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
356 return nullptr; // Failure.
357 }
358 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
359 if (access_check) {
360 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
361 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
362 return nullptr;
363 }
364 mirror::Class* referring_class = referrer->GetDeclaringClass();
365 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
366 field_idx))) {
367 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
368 return nullptr; // Failure.
369 }
370 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
371 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
372 return nullptr; // Failure.
373 } else {
374 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
375 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000376 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700377 "Attempted read of %zd-bit %s on field '%s'",
378 expected_size * (32 / sizeof(int32_t)),
379 is_primitive ? "primitive" : "non-primitive",
380 PrettyField(resolved_field, true).c_str());
381 return nullptr; // Failure.
382 }
383 }
384 }
385 if (!is_static) {
386 // instance fields must be being accessed on an initialized class
387 return resolved_field;
388 } else {
389 // If the class is initialized we're done.
390 if (LIKELY(fields_class->IsInitialized())) {
391 return resolved_field;
392 } else {
393 StackHandleScope<1> hs(self);
394 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700396 // Otherwise let's ensure the class is initialized before resolving the field.
397 return resolved_field;
398 }
399 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
400 return nullptr; // Failure.
401 }
402 }
403}
404
405// Explicit template declarations of FindFieldFromCode for all field access types.
406#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700407template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700408ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700409 ArtMethod* referrer, \
410 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700411
412#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
413 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
414 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
415
416EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
417EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
418EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
419EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
420EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
421EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
422EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
423EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
424
425#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
426#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
427
428template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
Andreas Gampe3a357142015-08-07 17:20:11 -0700430 ArtMethod* referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700431 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700432 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700433 if (resolved_method == nullptr) {
434 StackHandleScope<1> hs(self);
435 mirror::Object* null_this = nullptr;
436 HandleWrapper<mirror::Object> h_this(
437 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800438 constexpr ClassLinker::ResolveMode resolve_mode =
439 access_check ? ClassLinker::kForceICCECheck
440 : ClassLinker::kNoICCECheckForCache;
441 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700442 }
443 if (UNLIKELY(resolved_method == nullptr)) {
444 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
445 return nullptr; // Failure.
446 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
David Brazdil65902e82016-01-15 09:35:13 +0000447 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
448 resolved_method->IsConstructor())) {
449 // Hack for String init:
450 //
451 // We assume that the input of String.<init> in verified code is always
452 // an unitialized reference. If it is a null constant, it must have been
453 // optimized out by the compiler. Do not throw NullPointerException.
454 } else {
455 // Maintain interpreter-like semantics where NullPointerException is thrown
456 // after potential NoSuchMethodError from class linker.
457 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
458 return nullptr; // Failure.
459 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700461 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700462 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700463 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
464 resolved_method,
465 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700466 if (UNLIKELY(!can_access_resolved_method)) {
467 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
468 return nullptr; // Failure.
469 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100470 // Incompatible class change should have been handled in resolve method.
471 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
472 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
473 referrer);
474 return nullptr; // Failure.
475 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700476 }
477 switch (type) {
478 case kStatic:
479 case kDirect:
480 return resolved_method;
481 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700482 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700483 uint16_t vtable_index = resolved_method->GetMethodIndex();
484 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700485 (!klass->HasVTable() ||
486 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700487 // Behavior to agree with that of the verifier.
488 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
489 resolved_method->GetName(), resolved_method->GetSignature());
490 return nullptr; // Failure.
491 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700492 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700493 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700494 }
495 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700496 // TODO This lookup is quite slow.
497 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
498 // that will actually not be what we want in some cases where there are miranda methods or
499 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
500 // this method is coming from.
501 mirror::Class* referring_class = referrer->GetDeclaringClass();
502 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
503 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
504 if (UNLIKELY(method_reference_class == nullptr)) {
505 // Bad type idx.
506 CHECK(self->IsExceptionPending());
507 return nullptr;
508 } else if (!method_reference_class->IsInterface()) {
509 // It is not an interface.
510 mirror::Class* super_class = referring_class->GetSuperClass();
511 uint16_t vtable_index = resolved_method->GetMethodIndex();
512 if (access_check) {
513 // Check existence of super class.
514 if (super_class == nullptr || !super_class->HasVTable() ||
515 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
516 // Behavior to agree with that of the verifier.
517 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
518 resolved_method->GetName(), resolved_method->GetSignature());
519 return nullptr; // Failure.
520 }
521 }
522 DCHECK(super_class != nullptr);
523 DCHECK(super_class->HasVTable());
524 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
525 } else {
526 // It is an interface.
527 if (access_check) {
528 if (!method_reference_class->IsAssignableFrom((*this_object)->GetClass())) {
529 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
530 method_reference_class,
531 *this_object,
532 referrer);
533 return nullptr; // Failure.
534 }
535 }
536 // TODO We can do better than this for a (compiled) fastpath.
537 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
538 resolved_method, class_linker->GetImagePointerSize());
539 // Throw an NSME if nullptr;
540 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700541 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
542 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700543 }
Alex Light705ad492015-09-21 11:36:30 -0700544 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700545 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700546 }
547 case kInterface: {
548 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
550 imt_index, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700551 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
552 if (kIsDebugBuild) {
553 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554 ArtMethod* method = klass->FindVirtualMethodForInterface(
555 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700556 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
557 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
558 PrettyClass(klass);
559 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700560 return imt_method;
561 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700562 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
563 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700564 if (UNLIKELY(interface_method == nullptr)) {
565 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700566 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700567 return nullptr; // Failure.
568 }
569 return interface_method;
570 }
571 }
572 default:
573 LOG(FATAL) << "Unknown invoke type " << type;
574 return nullptr; // Failure.
575 }
576}
577
578// Explicit template declarations of FindMethodFromCode for all invoke types.
579#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700580 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700581 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
582 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700583 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700584 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700585#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
586 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
587 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
588
589EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
590EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
591EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
592EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
593EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
594
595#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
596#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
597
598// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
600 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700601 ArtField* resolved_field =
602 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700603 if (UNLIKELY(resolved_field == nullptr)) {
604 return nullptr;
605 }
606 // Check for incompatible class change.
607 bool is_primitive;
608 bool is_set;
609 bool is_static;
610 switch (type) {
611 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
612 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
613 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
614 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
615 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
616 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
617 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
618 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
619 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700620 LOG(FATAL) << "UNREACHABLE";
621 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700622 }
623 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
624 // Incompatible class change.
625 return nullptr;
626 }
627 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
628 if (is_static) {
629 // Check class is initialized else fail so that we can contend to initialize the class with
630 // other threads that may be racing to do this.
631 if (UNLIKELY(!fields_class->IsInitialized())) {
632 return nullptr;
633 }
634 }
635 mirror::Class* referring_class = referrer->GetDeclaringClass();
636 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700638 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
639 // Illegal access.
640 return nullptr;
641 }
642 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
643 resolved_field->FieldSize() != expected_size)) {
644 return nullptr;
645 }
646 return resolved_field;
647}
648
649// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700650inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
651 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700652 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
653 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700654 }
Alex Light705ad492015-09-21 11:36:30 -0700655 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 ArtMethod* resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700657 referring_class->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700658 if (UNLIKELY(resolved_method == nullptr)) {
659 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700660 }
661 if (access_check) {
662 // Check for incompatible class change errors and access.
663 bool icce = resolved_method->CheckIncompatibleClassChange(type);
664 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700665 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700666 }
667 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700668 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
669 !referring_class->CanAccessMember(methods_class,
670 resolved_method->GetAccessFlags()))) {
671 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700672 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700673 }
674 }
675 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700676 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700677 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 return resolved_method;
679 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700680 // TODO This lookup is rather slow.
681 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
682 mirror::Class* method_reference_class =
683 referring_class->GetDexCache()->GetResolvedType(method_type_idx);
684 if (method_reference_class == nullptr) {
685 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000686 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700687 } else if (!method_reference_class->IsInterface()) {
688 // It is not an interface.
689 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
690 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
691 // The super class does not have the method.
692 return nullptr;
693 }
694 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), sizeof(void*));
695 } else {
696 return method_reference_class->FindVirtualMethodForInterfaceSuper(
697 resolved_method, sizeof(void*));
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000698 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700699 } else {
700 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700701 return this_object->GetClass()->GetVTableEntry(
702 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700703 }
704}
705
Mathieu Chartiere401d142015-04-22 13:56:20 -0700706inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
707 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700708 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
709 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
710 if (UNLIKELY(klass == nullptr)) {
711 CHECK(self->IsExceptionPending());
712 return nullptr; // Failure - Indicate to caller to deliver exception
713 }
714 // Perform access check if necessary.
715 mirror::Class* referring_class = referrer->GetDeclaringClass();
716 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
717 ThrowIllegalAccessErrorClass(referring_class, klass);
718 return nullptr; // Failure - Indicate to caller to deliver exception
719 }
720 // If we're just implementing const-class, we shouldn't call <clinit>.
721 if (!can_run_clinit) {
722 return klass;
723 }
724 // If we are the <clinit> of this class, just return our storage.
725 //
726 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
727 // running.
728 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
729 return klass;
730 }
731 StackHandleScope<1> hs(self);
732 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700733 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700734 CHECK(self->IsExceptionPending());
735 return nullptr; // Failure - Indicate to caller to deliver exception
736 }
737 return h_class.Get();
738}
739
Mathieu Chartiere401d142015-04-22 13:56:20 -0700740inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700741 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
742 return class_linker->ResolveString(string_idx, referrer);
743}
744
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800745inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700747 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700748 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000749 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700750 self->ClearException();
751 }
752 // Decode locked object and unlock, before popping local references.
753 self->DecodeJObject(locked)->MonitorExit(self);
754 if (UNLIKELY(self->IsExceptionPending())) {
755 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
756 << saved_exception->Dump()
757 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000758 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700759 }
760 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700761 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000762 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700763 }
764}
765
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700766template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800767inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700768 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
769 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
770 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
771 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
772 if (LIKELY(f > kMinIntAsFloat)) {
773 if (LIKELY(f < kMaxIntAsFloat)) {
774 return static_cast<INT_TYPE>(f);
775 } else {
776 return kMaxInt;
777 }
778 } else {
779 return (f != f) ? 0 : kMinInt; // f != f implies NaN
780 }
781}
782
783} // namespace art
784
785#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_