blob: cc3eefed34c25e79f4d5746de2f97f75e0db9373 [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037#include "thread.h"
38
39namespace art {
40
Mathieu Chartiere401d142015-04-22 13:56:20 -070041inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010042 const InlineInfo& inline_info,
43 uint8_t inlining_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -070044 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010045 uint32_t method_index = inline_info.GetMethodIndexAtDepth(inlining_depth);
46 InvokeType invoke_type = static_cast<InvokeType>(
47 inline_info.GetInvokeTypeAtDepth(inlining_depth));
Mathieu Chartiere401d142015-04-22 13:56:20 -070048 ArtMethod* caller = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010049 if (!caller->IsRuntimeMethod()) {
50 return caller;
51 }
52
53 // The method in the dex cache can be the runtime method responsible for invoking
54 // the stub that will then update the dex cache. Therefore, we need to do the
55 // resolution ourselves.
Nicolas Geoffray3976e5e2015-06-15 08:58:03 +010056
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010057 // We first find the class loader of our caller. If it is the outer method, we can directly
58 // use its class loader. Otherwise, we also need to resolve our caller.
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 StackHandleScope<2> hs(Thread::Current());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010061 MutableHandle<mirror::ClassLoader> class_loader(hs.NewHandle<mirror::Class>(nullptr));
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 Handle<mirror::DexCache> dex_cache(hs.NewHandle(outer_method->GetDexCache()));
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010063 if (inlining_depth == 0) {
64 class_loader.Assign(outer_method->GetClassLoader());
65 } else {
66 caller = GetResolvedMethod(outer_method, inline_info, inlining_depth - 1);
67 class_loader.Assign(caller->GetClassLoader());
68 }
69
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010070 return class_linker->ResolveMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 *outer_method->GetDexFile(), method_index, dex_cache, class_loader, nullptr, invoke_type);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010072}
73
Mathieu Chartiere401d142015-04-22 13:56:20 -070074inline ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
75 Runtime::CalleeSaveType type,
76 bool do_caller_check = false)
Mathieu Chartier90443472015-07-16 20:32:27 -070077 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
Vladimir Marko5ea536a2015-04-20 20:11:30 +010079
80 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
Mathieu Chartiere401d142015-04-22 13:56:20 -070081 auto** caller_sp = reinterpret_cast<ArtMethod**>(
82 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
83 ArtMethod* outer_method = *caller_sp;
84 ArtMethod* caller = outer_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010085
86 if ((outer_method != nullptr) && outer_method->IsOptimized(sizeof(void*))) {
87 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
88 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
89 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
90 uintptr_t native_pc_offset = outer_method->NativeQuickPcOffset(caller_pc);
91 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +010092 StackMapEncoding encoding = code_info.ExtractEncoding();
93 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010094 DCHECK(stack_map.IsValid());
David Brazdilf677ebf2015-05-29 16:29:43 +010095 if (stack_map.HasInlineInfo(encoding)) {
96 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010097 caller = GetResolvedMethod(outer_method, inline_info, inline_info.GetDepth() - 1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010098 }
99 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100100
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100101 if (kIsDebugBuild && do_caller_check) {
102 // Note that do_caller_check is optional, as this method can be called by
103 // stubs, and tests without a proper call stack.
104 NthCallerVisitor visitor(Thread::Current(), 1, true);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100105 visitor.WalkStack();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100106 CHECK_EQ(caller, visitor.caller);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100107 }
108
109 return caller;
110}
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700113 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100114 return GetCalleeSaveMethodCaller(
115 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
116}
117
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700119ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800120inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800122 Thread* self, bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100123 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
124 size_t pointer_size = class_linker->GetImagePointerSize();
125 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100127 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700128 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130 DCHECK(self->IsExceptionPending());
131 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700132 } else {
133 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700134 }
135 }
136 if (kAccessCheck) {
137 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000138 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 *slow_path = true;
140 return nullptr; // Failure
141 }
142 mirror::Class* referrer = method->GetDeclaringClass();
143 if (UNLIKELY(!referrer->CanAccess(klass))) {
144 ThrowIllegalAccessErrorClass(referrer, klass);
145 *slow_path = true;
146 return nullptr; // Failure
147 }
148 }
149 if (UNLIKELY(!klass->IsInitialized())) {
150 StackHandleScope<1> hs(self);
151 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
152 // EnsureInitialized (the class initializer) might cause a GC.
153 // may cause us to suspend meaning that another thread may try to
154 // change the allocator while we are stuck in the entrypoints of
155 // an old allocator. Also, the class initialization may fail. To
156 // handle these cases we mark the slow path boolean as true so
157 // that the caller knows to check the allocator type to see if it
158 // has changed and to null-check the return value in case the
159 // initialization fails.
160 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700161 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700162 DCHECK(self->IsExceptionPending());
163 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700164 } else {
165 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700166 }
167 return h_klass.Get();
168 }
169 return klass;
170}
171
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700172ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800173inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
174 Thread* self,
175 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700176 if (UNLIKELY(!klass->IsInitialized())) {
177 StackHandleScope<1> hs(self);
178 Handle<mirror::Class> h_class(hs.NewHandle(klass));
179 // EnsureInitialized (the class initializer) might cause a GC.
180 // may cause us to suspend meaning that another thread may try to
181 // change the allocator while we are stuck in the entrypoints of
182 // an old allocator. Also, the class initialization may fail. To
183 // handle these cases we mark the slow path boolean as true so
184 // that the caller knows to check the allocator type to see if it
185 // has changed and to null-check the return value in case the
186 // initialization fails.
187 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700189 DCHECK(self->IsExceptionPending());
190 return nullptr; // Failure
191 }
192 return h_class.Get();
193 }
194 return klass;
195}
196
197// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
198// cannot be resolved, throw an error. If it can, use it to create an instance.
199// When verification/compiler hasn't been able to verify access, optionally perform an access
200// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700201template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700202ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800203inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800205 Thread* self,
206 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700207 bool slow_path = false;
208 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
209 if (UNLIKELY(slow_path)) {
210 if (klass == nullptr) {
211 return nullptr;
212 }
213 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
214 }
215 DCHECK(klass != nullptr);
216 return klass->Alloc<kInstrumented>(self, allocator_type);
217}
218
219// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700221ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800222inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
223 Thread* self,
224 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700225 DCHECK(klass != nullptr);
226 bool slow_path = false;
227 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
228 if (UNLIKELY(slow_path)) {
229 if (klass == nullptr) {
230 return nullptr;
231 }
232 gc::Heap* heap = Runtime::Current()->GetHeap();
233 // Pass in false since the object can not be finalizable.
234 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
235 }
236 // Pass in false since the object can not be finalizable.
237 return klass->Alloc<kInstrumented, false>(self, allocator_type);
238}
239
240// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700242ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800243inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
244 Thread* self,
245 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700246 DCHECK(klass != nullptr);
247 // Pass in false since the object can not be finalizable.
248 return klass->Alloc<kInstrumented, false>(self, allocator_type);
249}
250
251
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700253ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800254inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800255 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800257 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258 if (UNLIKELY(component_count < 0)) {
259 ThrowNegativeArraySizeException(component_count);
260 *slow_path = true;
261 return nullptr; // Failure
262 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100263 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
264 size_t pointer_size = class_linker->GetImagePointerSize();
265 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100267 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700268 *slow_path = true;
269 if (klass == nullptr) { // Error
270 DCHECK(Thread::Current()->IsExceptionPending());
271 return nullptr; // Failure
272 }
273 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
274 }
275 if (kAccessCheck) {
276 mirror::Class* referrer = method->GetDeclaringClass();
277 if (UNLIKELY(!referrer->CanAccess(klass))) {
278 ThrowIllegalAccessErrorClass(referrer, klass);
279 *slow_path = true;
280 return nullptr; // Failure
281 }
282 }
283 return klass;
284}
285
286// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
287// it cannot be resolved, throw an error. If it can, use it to create an array.
288// When verification/compiler hasn't been able to verify access, optionally perform an access
289// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700290template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700291ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800292inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800293 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800295 Thread* self,
296 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700297 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800298 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700299 &slow_path);
300 if (UNLIKELY(slow_path)) {
301 if (klass == nullptr) {
302 return nullptr;
303 }
304 gc::Heap* heap = Runtime::Current()->GetHeap();
305 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700306 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700307 heap->GetCurrentAllocator());
308 }
309 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700310 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700311}
312
313template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700314ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800315inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800316 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800318 Thread* self,
319 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320 DCHECK(klass != nullptr);
321 if (UNLIKELY(component_count < 0)) {
322 ThrowNegativeArraySizeException(component_count);
323 return nullptr; // Failure
324 }
325 if (kAccessCheck) {
326 mirror::Class* referrer = method->GetDeclaringClass();
327 if (UNLIKELY(!referrer->CanAccess(klass))) {
328 ThrowIllegalAccessErrorClass(referrer, klass);
329 return nullptr; // Failure
330 }
331 }
332 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
333 // suspension.
334 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700335 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700336}
337
338template<FindFieldType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339inline ArtField* FindFieldFromCode(uint32_t field_idx, ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800340 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700341 bool is_primitive;
342 bool is_set;
343 bool is_static;
344 switch (type) {
345 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
346 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
347 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
348 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
349 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
350 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
351 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
352 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
353 default: is_primitive = true; is_set = true; is_static = true; break;
354 }
355 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700356 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700357 if (UNLIKELY(resolved_field == nullptr)) {
358 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
359 return nullptr; // Failure.
360 }
361 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
362 if (access_check) {
363 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
364 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
365 return nullptr;
366 }
367 mirror::Class* referring_class = referrer->GetDeclaringClass();
368 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
369 field_idx))) {
370 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
371 return nullptr; // Failure.
372 }
373 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
374 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
375 return nullptr; // Failure.
376 } else {
377 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
378 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000379 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700380 "Attempted read of %zd-bit %s on field '%s'",
381 expected_size * (32 / sizeof(int32_t)),
382 is_primitive ? "primitive" : "non-primitive",
383 PrettyField(resolved_field, true).c_str());
384 return nullptr; // Failure.
385 }
386 }
387 }
388 if (!is_static) {
389 // instance fields must be being accessed on an initialized class
390 return resolved_field;
391 } else {
392 // If the class is initialized we're done.
393 if (LIKELY(fields_class->IsInitialized())) {
394 return resolved_field;
395 } else {
396 StackHandleScope<1> hs(self);
397 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700399 // Otherwise let's ensure the class is initialized before resolving the field.
400 return resolved_field;
401 }
402 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
403 return nullptr; // Failure.
404 }
405 }
406}
407
408// Explicit template declarations of FindFieldFromCode for all field access types.
409#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700410template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700411ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 ArtMethod* referrer, \
413 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700414
415#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
416 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
417 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
418
419EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
420EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
421EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
422EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
423EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
424EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
425EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
426EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
427
428#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
429#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
430
431template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
Andreas Gampe3a357142015-08-07 17:20:11 -0700433 ArtMethod* referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700434 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700435 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700436 if (resolved_method == nullptr) {
437 StackHandleScope<1> hs(self);
438 mirror::Object* null_this = nullptr;
439 HandleWrapper<mirror::Object> h_this(
440 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe3a357142015-08-07 17:20:11 -0700441 resolved_method = class_linker->ResolveMethod(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)) {
447 // Maintain interpreter-like semantics where NullPointerException is thrown
448 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000449 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700450 return nullptr; // Failure.
451 } else if (access_check) {
452 // Incompatible class change should have been handled in resolve method.
453 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
454 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700455 referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700456 return nullptr; // Failure.
457 }
458 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Andreas Gampe3a357142015-08-07 17:20:11 -0700459 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700460 bool can_access_resolved_method =
461 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
462 method_idx);
463 if (UNLIKELY(!can_access_resolved_method)) {
464 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
465 return nullptr; // Failure.
466 }
467 }
468 switch (type) {
469 case kStatic:
470 case kDirect:
471 return resolved_method;
472 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700473 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700474 uint16_t vtable_index = resolved_method->GetMethodIndex();
475 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700476 (!klass->HasVTable() ||
477 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700478 // Behavior to agree with that of the verifier.
479 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
480 resolved_method->GetName(), resolved_method->GetSignature());
481 return nullptr; // Failure.
482 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700483 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700485 }
486 case kSuper: {
Andreas Gampe3a357142015-08-07 17:20:11 -0700487 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700488 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489 if (access_check) {
490 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700491 if (super_class == nullptr || !super_class->HasVTable() ||
492 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700493 // Behavior to agree with that of the verifier.
494 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
495 resolved_method->GetName(), resolved_method->GetSignature());
496 return nullptr; // Failure.
497 }
498 } else {
499 // Super class must exist.
500 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700501 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700502 DCHECK(super_class->HasVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700503 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 }
505 case kInterface: {
506 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
508 imt_index, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700509 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
510 if (kIsDebugBuild) {
511 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 ArtMethod* method = klass->FindVirtualMethodForInterface(
513 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700514 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
515 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
516 PrettyClass(klass);
517 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700518 return imt_method;
519 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
521 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700522 if (UNLIKELY(interface_method == nullptr)) {
523 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700524 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700525 return nullptr; // Failure.
526 }
527 return interface_method;
528 }
529 }
530 default:
531 LOG(FATAL) << "Unknown invoke type " << type;
532 return nullptr; // Failure.
533 }
534}
535
536// Explicit template declarations of FindMethodFromCode for all invoke types.
537#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700538 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
540 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700541 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700543#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
544 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
545 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
546
547EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
548EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
549EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
550EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
551EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
552
553#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
554#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
555
556// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700557inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
558 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700559 ArtField* resolved_field =
560 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700561 if (UNLIKELY(resolved_field == nullptr)) {
562 return nullptr;
563 }
564 // Check for incompatible class change.
565 bool is_primitive;
566 bool is_set;
567 bool is_static;
568 switch (type) {
569 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
570 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
571 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
572 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
573 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
574 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
575 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
576 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
577 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700578 LOG(FATAL) << "UNREACHABLE";
579 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700580 }
581 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
582 // Incompatible class change.
583 return nullptr;
584 }
585 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
586 if (is_static) {
587 // Check class is initialized else fail so that we can contend to initialize the class with
588 // other threads that may be racing to do this.
589 if (UNLIKELY(!fields_class->IsInitialized())) {
590 return nullptr;
591 }
592 }
593 mirror::Class* referring_class = referrer->GetDeclaringClass();
594 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700596 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
597 // Illegal access.
598 return nullptr;
599 }
600 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
601 resolved_field->FieldSize() != expected_size)) {
602 return nullptr;
603 }
604 return resolved_field;
605}
606
607// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700608inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
609 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700610 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
611 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700613 ArtMethod* resolved_method =
614 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700615 if (UNLIKELY(resolved_method == nullptr)) {
616 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617 }
618 if (access_check) {
619 // Check for incompatible class change errors and access.
620 bool icce = resolved_method->CheckIncompatibleClassChange(type);
621 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700622 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700623 }
624 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
625 mirror::Class* referring_class = referrer->GetDeclaringClass();
626 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
627 !referring_class->CanAccessMember(methods_class,
628 resolved_method->GetAccessFlags()))) {
629 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700630 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700631 }
632 }
633 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700634 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700635 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700636 return resolved_method;
637 } else if (type == kSuper) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
639 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700640 } else {
641 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700642 return this_object->GetClass()->GetVTableEntry(
643 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700644 }
645}
646
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
648 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700649 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
650 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
651 if (UNLIKELY(klass == nullptr)) {
652 CHECK(self->IsExceptionPending());
653 return nullptr; // Failure - Indicate to caller to deliver exception
654 }
655 // Perform access check if necessary.
656 mirror::Class* referring_class = referrer->GetDeclaringClass();
657 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
658 ThrowIllegalAccessErrorClass(referring_class, klass);
659 return nullptr; // Failure - Indicate to caller to deliver exception
660 }
661 // If we're just implementing const-class, we shouldn't call <clinit>.
662 if (!can_run_clinit) {
663 return klass;
664 }
665 // If we are the <clinit> of this class, just return our storage.
666 //
667 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
668 // running.
669 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
670 return klass;
671 }
672 StackHandleScope<1> hs(self);
673 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700674 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 CHECK(self->IsExceptionPending());
676 return nullptr; // Failure - Indicate to caller to deliver exception
677 }
678 return h_class.Get();
679}
680
Mathieu Chartiere401d142015-04-22 13:56:20 -0700681inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700682 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
683 return class_linker->ResolveString(string_idx, referrer);
684}
685
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800686inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700687 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700689 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000690 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700691 self->ClearException();
692 }
693 // Decode locked object and unlock, before popping local references.
694 self->DecodeJObject(locked)->MonitorExit(self);
695 if (UNLIKELY(self->IsExceptionPending())) {
696 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
697 << saved_exception->Dump()
698 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000699 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700700 }
701 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700702 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000703 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700704 }
705}
706
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700707template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800708inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700709 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
710 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
711 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
712 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
713 if (LIKELY(f > kMinIntAsFloat)) {
714 if (LIKELY(f < kMaxIntAsFloat)) {
715 return static_cast<INT_TYPE>(f);
716 } else {
717 return kMaxInt;
718 }
719 } else {
720 return (f != f) ? 0 : kMinInt; // f != f implies NaN
721 }
722}
723
724} // namespace art
725
726#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_