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