Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | #include "class_linker-inl.h" |
| 23 | #include "common_throws.h" |
| 24 | #include "dex_file.h" |
| 25 | #include "indirect_reference_table.h" |
| 26 | #include "invoke_type.h" |
| 27 | #include "jni_internal.h" |
| 28 | #include "mirror/art_method.h" |
| 29 | #include "mirror/array.h" |
| 30 | #include "mirror/class-inl.h" |
| 31 | #include "mirror/object-inl.h" |
| 32 | #include "mirror/throwable.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 33 | #include "handle_scope-inl.h" |
| 34 | #include "thread.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 38 | template <const bool kAccessCheck> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 39 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 40 | inline mirror::Class* CheckObjectAlloc(uint32_t type_idx, |
| 41 | mirror::ArtMethod* method, |
| 42 | Thread* self, bool* slow_path) { |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 43 | mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 44 | if (UNLIKELY(klass == NULL)) { |
| 45 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 46 | *slow_path = true; |
| 47 | if (klass == NULL) { |
| 48 | DCHECK(self->IsExceptionPending()); |
| 49 | return nullptr; // Failure |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 50 | } else { |
| 51 | DCHECK(!self->IsExceptionPending()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | if (kAccessCheck) { |
| 55 | if (UNLIKELY(!klass->IsInstantiable())) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 56 | self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 57 | *slow_path = true; |
| 58 | return nullptr; // Failure |
| 59 | } |
| 60 | mirror::Class* referrer = method->GetDeclaringClass(); |
| 61 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 62 | ThrowIllegalAccessErrorClass(referrer, klass); |
| 63 | *slow_path = true; |
| 64 | return nullptr; // Failure |
| 65 | } |
| 66 | } |
| 67 | if (UNLIKELY(!klass->IsInitialized())) { |
| 68 | StackHandleScope<1> hs(self); |
| 69 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 70 | // EnsureInitialized (the class initializer) might cause a GC. |
| 71 | // may cause us to suspend meaning that another thread may try to |
| 72 | // change the allocator while we are stuck in the entrypoints of |
| 73 | // an old allocator. Also, the class initialization may fail. To |
| 74 | // handle these cases we mark the slow path boolean as true so |
| 75 | // that the caller knows to check the allocator type to see if it |
| 76 | // has changed and to null-check the return value in case the |
| 77 | // initialization fails. |
| 78 | *slow_path = true; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 79 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 80 | DCHECK(self->IsExceptionPending()); |
| 81 | return nullptr; // Failure |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 82 | } else { |
| 83 | DCHECK(!self->IsExceptionPending()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 84 | } |
| 85 | return h_klass.Get(); |
| 86 | } |
| 87 | return klass; |
| 88 | } |
| 89 | |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 90 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 91 | inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass, |
| 92 | Thread* self, |
| 93 | bool* slow_path) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 94 | if (UNLIKELY(!klass->IsInitialized())) { |
| 95 | StackHandleScope<1> hs(self); |
| 96 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 97 | // EnsureInitialized (the class initializer) might cause a GC. |
| 98 | // may cause us to suspend meaning that another thread may try to |
| 99 | // change the allocator while we are stuck in the entrypoints of |
| 100 | // an old allocator. Also, the class initialization may fail. To |
| 101 | // handle these cases we mark the slow path boolean as true so |
| 102 | // that the caller knows to check the allocator type to see if it |
| 103 | // has changed and to null-check the return value in case the |
| 104 | // initialization fails. |
| 105 | *slow_path = true; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 106 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 107 | DCHECK(self->IsExceptionPending()); |
| 108 | return nullptr; // Failure |
| 109 | } |
| 110 | return h_class.Get(); |
| 111 | } |
| 112 | return klass; |
| 113 | } |
| 114 | |
| 115 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 116 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
| 117 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 118 | // check. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 119 | template <bool kAccessCheck, bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 120 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 121 | inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, |
| 122 | mirror::ArtMethod* method, |
| 123 | Thread* self, |
| 124 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 125 | bool slow_path = false; |
| 126 | mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path); |
| 127 | if (UNLIKELY(slow_path)) { |
| 128 | if (klass == nullptr) { |
| 129 | return nullptr; |
| 130 | } |
| 131 | return klass->Alloc<kInstrumented>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 132 | } |
| 133 | DCHECK(klass != nullptr); |
| 134 | return klass->Alloc<kInstrumented>(self, allocator_type); |
| 135 | } |
| 136 | |
| 137 | // Given the context of a calling Method and a resolved class, create an instance. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 138 | template <bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 139 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 140 | inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass, |
| 141 | Thread* self, |
| 142 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 143 | DCHECK(klass != nullptr); |
| 144 | bool slow_path = false; |
| 145 | klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path); |
| 146 | if (UNLIKELY(slow_path)) { |
| 147 | if (klass == nullptr) { |
| 148 | return nullptr; |
| 149 | } |
| 150 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 151 | // Pass in false since the object can not be finalizable. |
| 152 | return klass->Alloc<kInstrumented, false>(self, heap->GetCurrentAllocator()); |
| 153 | } |
| 154 | // Pass in false since the object can not be finalizable. |
| 155 | return klass->Alloc<kInstrumented, false>(self, allocator_type); |
| 156 | } |
| 157 | |
| 158 | // Given the context of a calling Method and an initialized class, create an instance. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 159 | template <bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 160 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 161 | inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass, |
| 162 | Thread* self, |
| 163 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 164 | DCHECK(klass != nullptr); |
| 165 | // Pass in false since the object can not be finalizable. |
| 166 | return klass->Alloc<kInstrumented, false>(self, allocator_type); |
| 167 | } |
| 168 | |
| 169 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 170 | template <bool kAccessCheck> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 171 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 172 | inline mirror::Class* CheckArrayAlloc(uint32_t type_idx, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 173 | int32_t component_count, |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 174 | mirror::ArtMethod* method, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 175 | bool* slow_path) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 176 | if (UNLIKELY(component_count < 0)) { |
| 177 | ThrowNegativeArraySizeException(component_count); |
| 178 | *slow_path = true; |
| 179 | return nullptr; // Failure |
| 180 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 181 | mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 182 | if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve |
| 183 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 184 | *slow_path = true; |
| 185 | if (klass == nullptr) { // Error |
| 186 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 187 | return nullptr; // Failure |
| 188 | } |
| 189 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 190 | } |
| 191 | if (kAccessCheck) { |
| 192 | mirror::Class* referrer = method->GetDeclaringClass(); |
| 193 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 194 | ThrowIllegalAccessErrorClass(referrer, klass); |
| 195 | *slow_path = true; |
| 196 | return nullptr; // Failure |
| 197 | } |
| 198 | } |
| 199 | return klass; |
| 200 | } |
| 201 | |
| 202 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 203 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 204 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 205 | // check. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 206 | template <bool kAccessCheck, bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 207 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 208 | inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 209 | int32_t component_count, |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 210 | mirror::ArtMethod* method, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 211 | Thread* self, |
| 212 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 213 | bool slow_path = false; |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 214 | mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 215 | &slow_path); |
| 216 | if (UNLIKELY(slow_path)) { |
| 217 | if (klass == nullptr) { |
| 218 | return nullptr; |
| 219 | } |
| 220 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 221 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 222 | klass->GetComponentSizeShift(), |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 223 | heap->GetCurrentAllocator()); |
| 224 | } |
| 225 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 226 | klass->GetComponentSizeShift(), allocator_type); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | template <bool kAccessCheck, bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 230 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 231 | inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 232 | int32_t component_count, |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 233 | mirror::ArtMethod* method, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 234 | Thread* self, |
| 235 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 236 | DCHECK(klass != nullptr); |
| 237 | if (UNLIKELY(component_count < 0)) { |
| 238 | ThrowNegativeArraySizeException(component_count); |
| 239 | return nullptr; // Failure |
| 240 | } |
| 241 | if (kAccessCheck) { |
| 242 | mirror::Class* referrer = method->GetDeclaringClass(); |
| 243 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 244 | ThrowIllegalAccessErrorClass(referrer, klass); |
| 245 | return nullptr; // Failure |
| 246 | } |
| 247 | } |
| 248 | // No need to retry a slow-path allocation as the above code won't cause a GC or thread |
| 249 | // suspension. |
| 250 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 251 | klass->GetComponentSizeShift(), allocator_type); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | template<FindFieldType type, bool access_check> |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame^] | 255 | inline ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 256 | Thread* self, size_t expected_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 257 | bool is_primitive; |
| 258 | bool is_set; |
| 259 | bool is_static; |
| 260 | switch (type) { |
| 261 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 262 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 263 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 264 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 265 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 266 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 267 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 268 | case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through. |
| 269 | default: is_primitive = true; is_set = true; is_static = true; break; |
| 270 | } |
| 271 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame^] | 272 | ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 273 | if (UNLIKELY(resolved_field == nullptr)) { |
| 274 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 275 | return nullptr; // Failure. |
| 276 | } |
| 277 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 278 | if (access_check) { |
| 279 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 280 | ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer); |
| 281 | return nullptr; |
| 282 | } |
| 283 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 284 | if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field, |
| 285 | field_idx))) { |
| 286 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 287 | return nullptr; // Failure. |
| 288 | } |
| 289 | if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { |
| 290 | ThrowIllegalAccessErrorFinalField(referrer, resolved_field); |
| 291 | return nullptr; // Failure. |
| 292 | } else { |
| 293 | if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive || |
| 294 | resolved_field->FieldSize() != expected_size)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 295 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 296 | "Attempted read of %zd-bit %s on field '%s'", |
| 297 | expected_size * (32 / sizeof(int32_t)), |
| 298 | is_primitive ? "primitive" : "non-primitive", |
| 299 | PrettyField(resolved_field, true).c_str()); |
| 300 | return nullptr; // Failure. |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | if (!is_static) { |
| 305 | // instance fields must be being accessed on an initialized class |
| 306 | return resolved_field; |
| 307 | } else { |
| 308 | // If the class is initialized we're done. |
| 309 | if (LIKELY(fields_class->IsInitialized())) { |
| 310 | return resolved_field; |
| 311 | } else { |
| 312 | StackHandleScope<1> hs(self); |
| 313 | Handle<mirror::Class> h_class(hs.NewHandle(fields_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 314 | if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 315 | // Otherwise let's ensure the class is initialized before resolving the field. |
| 316 | return resolved_field; |
| 317 | } |
| 318 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind |
| 319 | return nullptr; // Failure. |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Explicit template declarations of FindFieldFromCode for all field access types. |
| 325 | #define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \ |
| 326 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \ |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame^] | 327 | ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 328 | mirror::ArtMethod* referrer, \ |
| 329 | Thread* self, size_t expected_size) \ |
| 330 | |
| 331 | #define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \ |
| 332 | EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \ |
| 333 | EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true) |
| 334 | |
| 335 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead); |
| 336 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite); |
| 337 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead); |
| 338 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite); |
| 339 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead); |
| 340 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite); |
| 341 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead); |
| 342 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite); |
| 343 | |
| 344 | #undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL |
| 345 | #undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL |
| 346 | |
| 347 | template<InvokeType type, bool access_check> |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 348 | inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx, |
| 349 | mirror::Object** this_object, |
| 350 | mirror::ArtMethod** referrer, Thread* self) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 351 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 352 | mirror::ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, *referrer); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 353 | if (resolved_method == nullptr) { |
| 354 | StackHandleScope<1> hs(self); |
| 355 | mirror::Object* null_this = nullptr; |
| 356 | HandleWrapper<mirror::Object> h_this( |
| 357 | hs.NewHandleWrapper(type == kStatic ? &null_this : this_object)); |
| 358 | resolved_method = class_linker->ResolveMethod(self, method_idx, referrer, type); |
| 359 | } |
| 360 | if (UNLIKELY(resolved_method == nullptr)) { |
| 361 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 362 | return nullptr; // Failure. |
| 363 | } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) { |
| 364 | // Maintain interpreter-like semantics where NullPointerException is thrown |
| 365 | // after potential NoSuchMethodError from class linker. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 366 | ThrowNullPointerExceptionForMethodAccess(method_idx, type); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 367 | return nullptr; // Failure. |
| 368 | } else if (access_check) { |
| 369 | // Incompatible class change should have been handled in resolve method. |
| 370 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
| 371 | ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method, |
| 372 | *referrer); |
| 373 | return nullptr; // Failure. |
| 374 | } |
| 375 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
| 376 | mirror::Class* referring_class = (*referrer)->GetDeclaringClass(); |
| 377 | bool can_access_resolved_method = |
| 378 | referring_class->CheckResolvedMethodAccess<type>(methods_class, resolved_method, |
| 379 | method_idx); |
| 380 | if (UNLIKELY(!can_access_resolved_method)) { |
| 381 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 382 | return nullptr; // Failure. |
| 383 | } |
| 384 | } |
| 385 | switch (type) { |
| 386 | case kStatic: |
| 387 | case kDirect: |
| 388 | return resolved_method; |
| 389 | case kVirtual: { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 390 | mirror::Class* klass = (*this_object)->GetClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 391 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 392 | if (access_check && |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 393 | (!klass->HasVTable() || |
| 394 | vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 395 | // Behavior to agree with that of the verifier. |
| 396 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), |
| 397 | resolved_method->GetName(), resolved_method->GetSignature()); |
| 398 | return nullptr; // Failure. |
| 399 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 400 | DCHECK(klass->HasVTable()) << PrettyClass(klass); |
| 401 | return klass->GetVTableEntry(vtable_index); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 402 | } |
| 403 | case kSuper: { |
| 404 | mirror::Class* super_class = (*referrer)->GetDeclaringClass()->GetSuperClass(); |
| 405 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 406 | if (access_check) { |
| 407 | // Check existence of super class. |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 408 | if (super_class == nullptr || !super_class->HasVTable() || |
| 409 | vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 410 | // Behavior to agree with that of the verifier. |
| 411 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), |
| 412 | resolved_method->GetName(), resolved_method->GetSignature()); |
| 413 | return nullptr; // Failure. |
| 414 | } |
| 415 | } else { |
| 416 | // Super class must exist. |
| 417 | DCHECK(super_class != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 418 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 419 | DCHECK(super_class->HasVTable()); |
| 420 | return super_class->GetVTableEntry(vtable_index); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 421 | } |
| 422 | case kInterface: { |
| 423 | uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
| 424 | mirror::ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(imt_index); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 425 | if (!imt_method->IsImtConflictMethod() && !imt_method->IsImtUnimplementedMethod()) { |
| 426 | if (kIsDebugBuild) { |
| 427 | mirror::Class* klass = (*this_object)->GetClass(); |
| 428 | mirror::ArtMethod* method = klass->FindVirtualMethodForInterface(resolved_method); |
| 429 | CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " << |
| 430 | PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " << |
| 431 | PrettyClass(klass); |
| 432 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 433 | return imt_method; |
| 434 | } else { |
| 435 | mirror::ArtMethod* interface_method = |
| 436 | (*this_object)->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 437 | if (UNLIKELY(interface_method == nullptr)) { |
| 438 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, |
| 439 | *this_object, *referrer); |
| 440 | return nullptr; // Failure. |
| 441 | } |
| 442 | return interface_method; |
| 443 | } |
| 444 | } |
| 445 | default: |
| 446 | LOG(FATAL) << "Unknown invoke type " << type; |
| 447 | return nullptr; // Failure. |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // Explicit template declarations of FindMethodFromCode for all invoke types. |
| 452 | #define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \ |
| 453 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \ |
| 454 | mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \ |
| 455 | mirror::Object** this_object, \ |
| 456 | mirror::ArtMethod** referrer, \ |
| 457 | Thread* self) |
| 458 | #define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \ |
| 459 | EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \ |
| 460 | EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true) |
| 461 | |
| 462 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic); |
| 463 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect); |
| 464 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual); |
| 465 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper); |
| 466 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface); |
| 467 | |
| 468 | #undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL |
| 469 | #undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL |
| 470 | |
| 471 | // Fast path field resolution that can't initialize classes or throw exceptions. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame^] | 472 | inline ArtField* FindFieldFast(uint32_t field_idx, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 473 | mirror::ArtMethod* referrer, |
| 474 | FindFieldType type, size_t expected_size) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame^] | 475 | ArtField* resolved_field = |
| 476 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 477 | if (UNLIKELY(resolved_field == nullptr)) { |
| 478 | return nullptr; |
| 479 | } |
| 480 | // Check for incompatible class change. |
| 481 | bool is_primitive; |
| 482 | bool is_set; |
| 483 | bool is_static; |
| 484 | switch (type) { |
| 485 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 486 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 487 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 488 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 489 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 490 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 491 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 492 | case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break; |
| 493 | default: |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 494 | LOG(FATAL) << "UNREACHABLE"; |
| 495 | UNREACHABLE(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 496 | } |
| 497 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 498 | // Incompatible class change. |
| 499 | return nullptr; |
| 500 | } |
| 501 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 502 | if (is_static) { |
| 503 | // Check class is initialized else fail so that we can contend to initialize the class with |
| 504 | // other threads that may be racing to do this. |
| 505 | if (UNLIKELY(!fields_class->IsInitialized())) { |
| 506 | return nullptr; |
| 507 | } |
| 508 | } |
| 509 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 510 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 511 | !referring_class->CanAccessMember(fields_class, |
| 512 | resolved_field->GetAccessFlags()) || |
| 513 | (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) { |
| 514 | // Illegal access. |
| 515 | return nullptr; |
| 516 | } |
| 517 | if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive || |
| 518 | resolved_field->FieldSize() != expected_size)) { |
| 519 | return nullptr; |
| 520 | } |
| 521 | return resolved_field; |
| 522 | } |
| 523 | |
| 524 | // Fast path method resolution that can't throw exceptions. |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 525 | inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx, |
| 526 | mirror::Object* this_object, |
| 527 | mirror::ArtMethod* referrer, |
| 528 | bool access_check, InvokeType type) { |
Jeff Hao | 207a37d | 2014-10-29 17:24:25 -0700 | [diff] [blame] | 529 | if (UNLIKELY(this_object == NULL && type != kStatic)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 530 | return NULL; |
| 531 | } |
| 532 | mirror::ArtMethod* resolved_method = |
| 533 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx); |
| 534 | if (UNLIKELY(resolved_method == NULL)) { |
| 535 | return NULL; |
| 536 | } |
| 537 | if (access_check) { |
| 538 | // Check for incompatible class change errors and access. |
| 539 | bool icce = resolved_method->CheckIncompatibleClassChange(type); |
| 540 | if (UNLIKELY(icce)) { |
| 541 | return NULL; |
| 542 | } |
| 543 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
| 544 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 545 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 546 | !referring_class->CanAccessMember(methods_class, |
| 547 | resolved_method->GetAccessFlags()))) { |
| 548 | // Potential illegal access, may need to refine the method's class. |
| 549 | return NULL; |
| 550 | } |
| 551 | } |
| 552 | if (type == kInterface) { // Most common form of slow path dispatch. |
| 553 | return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
Jeff Hao | 207a37d | 2014-10-29 17:24:25 -0700 | [diff] [blame] | 554 | } else if (type == kStatic || type == kDirect) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 555 | return resolved_method; |
| 556 | } else if (type == kSuper) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 557 | return referrer->GetDeclaringClass()->GetSuperClass() |
| 558 | ->GetVTableEntry(resolved_method->GetMethodIndex()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 559 | } else { |
| 560 | DCHECK(type == kVirtual); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 561 | return this_object->GetClass()->GetVTableEntry(resolved_method->GetMethodIndex()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 565 | inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 566 | mirror::ArtMethod* referrer, |
| 567 | Thread* self, bool can_run_clinit, |
Ian Rogers | e5877a1 | 2014-07-16 12:06:35 -0700 | [diff] [blame] | 568 | bool verify_access) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 569 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 570 | mirror::Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 571 | if (UNLIKELY(klass == nullptr)) { |
| 572 | CHECK(self->IsExceptionPending()); |
| 573 | return nullptr; // Failure - Indicate to caller to deliver exception |
| 574 | } |
| 575 | // Perform access check if necessary. |
| 576 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 577 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
| 578 | ThrowIllegalAccessErrorClass(referring_class, klass); |
| 579 | return nullptr; // Failure - Indicate to caller to deliver exception |
| 580 | } |
| 581 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 582 | if (!can_run_clinit) { |
| 583 | return klass; |
| 584 | } |
| 585 | // If we are the <clinit> of this class, just return our storage. |
| 586 | // |
| 587 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 588 | // running. |
| 589 | if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) { |
| 590 | return klass; |
| 591 | } |
| 592 | StackHandleScope<1> hs(self); |
| 593 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 594 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 595 | CHECK(self->IsExceptionPending()); |
| 596 | return nullptr; // Failure - Indicate to caller to deliver exception |
| 597 | } |
| 598 | return h_class.Get(); |
| 599 | } |
| 600 | |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 601 | inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer, |
| 602 | uint32_t string_idx) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 603 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 604 | return class_linker->ResolveString(string_idx, referrer); |
| 605 | } |
| 606 | |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 607 | inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 608 | // Save any pending exception over monitor exit call. |
| 609 | mirror::Throwable* saved_exception = NULL; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 610 | if (UNLIKELY(self->IsExceptionPending())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 611 | saved_exception = self->GetException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 612 | self->ClearException(); |
| 613 | } |
| 614 | // Decode locked object and unlock, before popping local references. |
| 615 | self->DecodeJObject(locked)->MonitorExit(self); |
| 616 | if (UNLIKELY(self->IsExceptionPending())) { |
| 617 | LOG(FATAL) << "Synchronized JNI code returning with an exception:\n" |
| 618 | << saved_exception->Dump() |
| 619 | << "\nEncountered second exception during implicit MonitorExit:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 620 | << self->GetException()->Dump(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 621 | } |
| 622 | // Restore pending exception. |
| 623 | if (saved_exception != NULL) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 624 | self->SetException(saved_exception); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 628 | template <typename INT_TYPE, typename FLOAT_TYPE> |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 629 | inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 630 | const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max()); |
| 631 | const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min()); |
| 632 | const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt); |
| 633 | const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt); |
| 634 | if (LIKELY(f > kMinIntAsFloat)) { |
| 635 | if (LIKELY(f < kMaxIntAsFloat)) { |
| 636 | return static_cast<INT_TYPE>(f); |
| 637 | } else { |
| 638 | return kMaxInt; |
| 639 | } |
| 640 | } else { |
| 641 | return (f != f) ? 0 : kMinInt; // f != f implies NaN |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | } // namespace art |
| 646 | |
| 647 | #endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_ |