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