blob: 314cf5357d98c7f3537ea628ccd15d0d04c475ae [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
Nicolas Geoffray43855cc2015-09-29 15:43:06 +010074extern "C" void art_quick_instrumentation_exit();
75
Mathieu Chartiere401d142015-04-22 13:56:20 -070076inline ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
77 Runtime::CalleeSaveType type,
78 bool do_caller_check = false)
Mathieu Chartier90443472015-07-16 20:32:27 -070079 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
Vladimir Marko5ea536a2015-04-20 20:11:30 +010081
82 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
Mathieu Chartiere401d142015-04-22 13:56:20 -070083 auto** caller_sp = reinterpret_cast<ArtMethod**>(
84 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
85 ArtMethod* outer_method = *caller_sp;
86 ArtMethod* caller = outer_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010087
88 if ((outer_method != nullptr) && outer_method->IsOptimized(sizeof(void*))) {
89 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
90 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
91 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
Nicolas Geoffray43855cc2015-09-29 15:43:06 +010092 if (LIKELY(caller_pc != reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit))) {
93 uintptr_t native_pc_offset = outer_method->NativeQuickPcOffset(caller_pc);
94 CodeInfo code_info = outer_method->GetOptimizedCodeInfo();
95 StackMapEncoding encoding = code_info.ExtractEncoding();
96 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
97 DCHECK(stack_map.IsValid());
98 if (stack_map.HasInlineInfo(encoding)) {
99 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
100 caller = GetResolvedMethod(outer_method, inline_info, inline_info.GetDepth() - 1);
101 }
102 } else {
103 // We're instrumenting, just use the StackVisitor which knows how to
104 // handle instrumented frames.
105 NthCallerVisitor visitor(Thread::Current(), 1, true);
106 visitor.WalkStack();
107 caller = visitor.caller;
108 do_caller_check = false;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100109 }
110 }
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100111
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100112 if (kIsDebugBuild && do_caller_check) {
113 // Note that do_caller_check is optional, as this method can be called by
114 // stubs, and tests without a proper call stack.
115 NthCallerVisitor visitor(Thread::Current(), 1, true);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100116 visitor.WalkStack();
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100117 CHECK_EQ(caller, visitor.caller);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100118 }
119
120 return caller;
121}
122
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700124 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100125 return GetCalleeSaveMethodCaller(
126 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
127}
128
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700130ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800131inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700132 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800133 Thread* self, bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
135 size_t pointer_size = class_linker->GetImagePointerSize();
136 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700137 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100138 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700141 DCHECK(self->IsExceptionPending());
142 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700143 } else {
144 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700145 }
146 }
147 if (kAccessCheck) {
148 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000149 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150 *slow_path = true;
151 return nullptr; // Failure
152 }
153 mirror::Class* referrer = method->GetDeclaringClass();
154 if (UNLIKELY(!referrer->CanAccess(klass))) {
155 ThrowIllegalAccessErrorClass(referrer, klass);
156 *slow_path = true;
157 return nullptr; // Failure
158 }
159 }
160 if (UNLIKELY(!klass->IsInitialized())) {
161 StackHandleScope<1> hs(self);
162 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
163 // EnsureInitialized (the class initializer) might cause a GC.
164 // may cause us to suspend meaning that another thread may try to
165 // change the allocator while we are stuck in the entrypoints of
166 // an old allocator. Also, the class initialization may fail. To
167 // handle these cases we mark the slow path boolean as true so
168 // that the caller knows to check the allocator type to see if it
169 // has changed and to null-check the return value in case the
170 // initialization fails.
171 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700172 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700173 DCHECK(self->IsExceptionPending());
174 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700175 } else {
176 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700177 }
178 return h_klass.Get();
179 }
180 return klass;
181}
182
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700183ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800184inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
185 Thread* self,
186 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187 if (UNLIKELY(!klass->IsInitialized())) {
188 StackHandleScope<1> hs(self);
189 Handle<mirror::Class> h_class(hs.NewHandle(klass));
190 // EnsureInitialized (the class initializer) might cause a GC.
191 // may cause us to suspend meaning that another thread may try to
192 // change the allocator while we are stuck in the entrypoints of
193 // an old allocator. Also, the class initialization may fail. To
194 // handle these cases we mark the slow path boolean as true so
195 // that the caller knows to check the allocator type to see if it
196 // has changed and to null-check the return value in case the
197 // initialization fails.
198 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700199 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700200 DCHECK(self->IsExceptionPending());
201 return nullptr; // Failure
202 }
203 return h_class.Get();
204 }
205 return klass;
206}
207
208// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
209// cannot be resolved, throw an error. If it can, use it to create an instance.
210// When verification/compiler hasn't been able to verify access, optionally perform an access
211// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700212template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700213ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800214inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800216 Thread* self,
217 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700218 bool slow_path = false;
219 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
220 if (UNLIKELY(slow_path)) {
221 if (klass == nullptr) {
222 return nullptr;
223 }
224 return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
225 }
226 DCHECK(klass != nullptr);
227 return klass->Alloc<kInstrumented>(self, allocator_type);
228}
229
230// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700232ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800233inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
234 Thread* self,
235 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236 DCHECK(klass != nullptr);
237 bool slow_path = false;
238 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
239 if (UNLIKELY(slow_path)) {
240 if (klass == nullptr) {
241 return nullptr;
242 }
243 gc::Heap* heap = Runtime::Current()->GetHeap();
244 // Pass in false since the object can not be finalizable.
245 return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator());
246 }
247 // Pass in false since the object can not be finalizable.
248 return klass->Alloc<kInstrumented, false>(self, allocator_type);
249}
250
251// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700253ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800254inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
255 Thread* self,
256 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257 DCHECK(klass != nullptr);
258 // Pass in false since the object can not be finalizable.
259 return klass->Alloc<kInstrumented, false>(self, allocator_type);
260}
261
262
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700264ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800265inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800266 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800268 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700269 if (UNLIKELY(component_count < 0)) {
270 ThrowNegativeArraySizeException(component_count);
271 *slow_path = true;
272 return nullptr; // Failure
273 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
275 size_t pointer_size = class_linker->GetImagePointerSize();
276 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100278 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700279 *slow_path = true;
280 if (klass == nullptr) { // Error
281 DCHECK(Thread::Current()->IsExceptionPending());
282 return nullptr; // Failure
283 }
284 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
285 }
286 if (kAccessCheck) {
287 mirror::Class* referrer = method->GetDeclaringClass();
288 if (UNLIKELY(!referrer->CanAccess(klass))) {
289 ThrowIllegalAccessErrorClass(referrer, klass);
290 *slow_path = true;
291 return nullptr; // Failure
292 }
293 }
294 return klass;
295}
296
297// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
298// it cannot be resolved, throw an error. If it can, use it to create an array.
299// When verification/compiler hasn't been able to verify access, optionally perform an access
300// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700301template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700302ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800303inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800304 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800306 Thread* self,
307 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700308 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800309 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700310 &slow_path);
311 if (UNLIKELY(slow_path)) {
312 if (klass == nullptr) {
313 return nullptr;
314 }
315 gc::Heap* heap = Runtime::Current()->GetHeap();
316 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700317 klass->GetComponentSizeShift(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700318 heap->GetCurrentAllocator());
319 }
320 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700321 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700322}
323
324template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700325ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800326inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800327 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700328 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800329 Thread* self,
330 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700331 DCHECK(klass != nullptr);
332 if (UNLIKELY(component_count < 0)) {
333 ThrowNegativeArraySizeException(component_count);
334 return nullptr; // Failure
335 }
336 if (kAccessCheck) {
337 mirror::Class* referrer = method->GetDeclaringClass();
338 if (UNLIKELY(!referrer->CanAccess(klass))) {
339 ThrowIllegalAccessErrorClass(referrer, klass);
340 return nullptr; // Failure
341 }
342 }
343 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
344 // suspension.
345 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700346 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700347}
348
349template<FindFieldType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350inline ArtField* FindFieldFromCode(uint32_t field_idx, ArtMethod* referrer,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800351 Thread* self, size_t expected_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700352 bool is_primitive;
353 bool is_set;
354 bool is_static;
355 switch (type) {
356 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
357 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
358 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
359 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
360 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
361 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
362 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
363 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
364 default: is_primitive = true; is_set = true; is_static = true; break;
365 }
366 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700367 ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700368 if (UNLIKELY(resolved_field == nullptr)) {
369 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
370 return nullptr; // Failure.
371 }
372 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
373 if (access_check) {
374 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
375 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
376 return nullptr;
377 }
378 mirror::Class* referring_class = referrer->GetDeclaringClass();
379 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
380 field_idx))) {
381 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
382 return nullptr; // Failure.
383 }
384 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
385 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
386 return nullptr; // Failure.
387 } else {
388 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
389 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000390 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700391 "Attempted read of %zd-bit %s on field '%s'",
392 expected_size * (32 / sizeof(int32_t)),
393 is_primitive ? "primitive" : "non-primitive",
394 PrettyField(resolved_field, true).c_str());
395 return nullptr; // Failure.
396 }
397 }
398 }
399 if (!is_static) {
400 // instance fields must be being accessed on an initialized class
401 return resolved_field;
402 } else {
403 // If the class is initialized we're done.
404 if (LIKELY(fields_class->IsInitialized())) {
405 return resolved_field;
406 } else {
407 StackHandleScope<1> hs(self);
408 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700409 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700410 // Otherwise let's ensure the class is initialized before resolving the field.
411 return resolved_field;
412 }
413 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
414 return nullptr; // Failure.
415 }
416 }
417}
418
419// Explicit template declarations of FindFieldFromCode for all field access types.
420#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700421template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700422ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 ArtMethod* referrer, \
424 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700425
426#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
427 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
428 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
429
430EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
431EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
432EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
433EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
434EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
435EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
436EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
437EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
438
439#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
440#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
441
442template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
Andreas Gampe3a357142015-08-07 17:20:11 -0700444 ArtMethod* referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700445 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700446 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700447 if (resolved_method == nullptr) {
448 StackHandleScope<1> hs(self);
449 mirror::Object* null_this = nullptr;
450 HandleWrapper<mirror::Object> h_this(
451 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe3a357142015-08-07 17:20:11 -0700452 resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700453 }
454 if (UNLIKELY(resolved_method == nullptr)) {
455 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
456 return nullptr; // Failure.
457 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
458 // Maintain interpreter-like semantics where NullPointerException is thrown
459 // after potential NoSuchMethodError from class linker.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000460 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700461 return nullptr; // Failure.
462 } else if (access_check) {
463 // Incompatible class change should have been handled in resolve method.
464 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
465 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700466 referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700467 return nullptr; // Failure.
468 }
469 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Andreas Gampe3a357142015-08-07 17:20:11 -0700470 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700471 bool can_access_resolved_method =
472 referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method,
473 method_idx);
474 if (UNLIKELY(!can_access_resolved_method)) {
475 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
476 return nullptr; // Failure.
477 }
478 }
479 switch (type) {
480 case kStatic:
481 case kDirect:
482 return resolved_method;
483 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700484 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700485 uint16_t vtable_index = resolved_method->GetMethodIndex();
486 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700487 (!klass->HasVTable() ||
488 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700489 // Behavior to agree with that of the verifier.
490 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
491 resolved_method->GetName(), resolved_method->GetSignature());
492 return nullptr; // Failure.
493 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700494 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700495 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700496 }
497 case kSuper: {
Andreas Gampe3a357142015-08-07 17:20:11 -0700498 mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700499 uint16_t vtable_index = resolved_method->GetMethodIndex();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 if (access_check) {
501 // Check existence of super class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700502 if (super_class == nullptr || !super_class->HasVTable() ||
503 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 // Behavior to agree with that of the verifier.
505 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
506 resolved_method->GetName(), resolved_method->GetSignature());
507 return nullptr; // Failure.
508 }
509 } else {
510 // Super class must exist.
511 DCHECK(super_class != nullptr);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700512 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700513 DCHECK(super_class->HasVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700515 }
516 case kInterface: {
517 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
519 imt_index, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700520 if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) {
521 if (kIsDebugBuild) {
522 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700523 ArtMethod* method = klass->FindVirtualMethodForInterface(
524 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700525 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
526 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
527 PrettyClass(klass);
528 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700529 return imt_method;
530 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
532 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700533 if (UNLIKELY(interface_method == nullptr)) {
534 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700535 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700536 return nullptr; // Failure.
537 }
538 return interface_method;
539 }
540 }
541 default:
542 LOG(FATAL) << "Unknown invoke type " << type;
543 return nullptr; // Failure.
544 }
545}
546
547// Explicit template declarations of FindMethodFromCode for all invoke types.
548#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700549 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
551 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700552 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700554#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
555 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
556 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
557
558EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
559EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
560EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
561EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
562EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
563
564#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
565#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
566
567// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700568inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
569 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700570 ArtField* resolved_field =
571 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700572 if (UNLIKELY(resolved_field == nullptr)) {
573 return nullptr;
574 }
575 // Check for incompatible class change.
576 bool is_primitive;
577 bool is_set;
578 bool is_static;
579 switch (type) {
580 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
581 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
582 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
583 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
584 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
585 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
586 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
587 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
588 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700589 LOG(FATAL) << "UNREACHABLE";
590 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700591 }
592 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
593 // Incompatible class change.
594 return nullptr;
595 }
596 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
597 if (is_static) {
598 // Check class is initialized else fail so that we can contend to initialize the class with
599 // other threads that may be racing to do this.
600 if (UNLIKELY(!fields_class->IsInitialized())) {
601 return nullptr;
602 }
603 }
604 mirror::Class* referring_class = referrer->GetDeclaringClass();
605 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700607 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
608 // Illegal access.
609 return nullptr;
610 }
611 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
612 resolved_field->FieldSize() != expected_size)) {
613 return nullptr;
614 }
615 return resolved_field;
616}
617
618// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700619inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
620 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700621 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
622 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700623 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700624 ArtMethod* resolved_method =
625 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700626 if (UNLIKELY(resolved_method == nullptr)) {
627 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700628 }
629 if (access_check) {
630 // Check for incompatible class change errors and access.
631 bool icce = resolved_method->CheckIncompatibleClassChange(type);
632 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700633 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700634 }
635 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
636 mirror::Class* referring_class = referrer->GetDeclaringClass();
637 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
638 !referring_class->CanAccessMember(methods_class,
639 resolved_method->GetAccessFlags()))) {
640 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700641 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700642 }
643 }
644 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700646 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700647 return resolved_method;
648 } else if (type == kSuper) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700649 return referrer->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
650 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700651 } else {
652 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700653 return this_object->GetClass()->GetVTableEntry(
654 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700655 }
656}
657
Mathieu Chartiere401d142015-04-22 13:56:20 -0700658inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
659 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700660 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
661 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
662 if (UNLIKELY(klass == nullptr)) {
663 CHECK(self->IsExceptionPending());
664 return nullptr; // Failure - Indicate to caller to deliver exception
665 }
666 // Perform access check if necessary.
667 mirror::Class* referring_class = referrer->GetDeclaringClass();
668 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
669 ThrowIllegalAccessErrorClass(referring_class, klass);
670 return nullptr; // Failure - Indicate to caller to deliver exception
671 }
672 // If we're just implementing const-class, we shouldn't call <clinit>.
673 if (!can_run_clinit) {
674 return klass;
675 }
676 // If we are the <clinit> of this class, just return our storage.
677 //
678 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
679 // running.
680 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
681 return klass;
682 }
683 StackHandleScope<1> hs(self);
684 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700685 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700686 CHECK(self->IsExceptionPending());
687 return nullptr; // Failure - Indicate to caller to deliver exception
688 }
689 return h_class.Get();
690}
691
Mathieu Chartiere401d142015-04-22 13:56:20 -0700692inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700693 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
694 return class_linker->ResolveString(string_idx, referrer);
695}
696
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800697inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700698 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700699 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700700 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000701 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700702 self->ClearException();
703 }
704 // Decode locked object and unlock, before popping local references.
705 self->DecodeJObject(locked)->MonitorExit(self);
706 if (UNLIKELY(self->IsExceptionPending())) {
707 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
708 << saved_exception->Dump()
709 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000710 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700711 }
712 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700713 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000714 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700715 }
716}
717
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700718template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800719inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
721 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
722 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
723 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
724 if (LIKELY(f > kMinIntAsFloat)) {
725 if (LIKELY(f < kMaxIntAsFloat)) {
726 return static_cast<INT_TYPE>(f);
727 } else {
728 return kMaxInt;
729 }
730 } else {
731 return (f != f) ? 0 : kMinInt; // f != f implies NaN
732 }
733}
734
735} // namespace art
736
737#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_