Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -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 | */ |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |
| 18 | #define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 19 | |
| 20 | #include "base/macros.h" |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 21 | #include "class_linker.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 22 | #include "common_throws.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 23 | #include "dex_file.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 24 | #include "indirect_reference_table.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 25 | #include "invoke_type.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 26 | #include "jni_internal.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 27 | #include "mirror/art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/array.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 29 | #include "mirror/class-inl.h" |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 30 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/throwable.h" |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 32 | #include "locks.h" |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 33 | #include "object_utils.h" |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 34 | #include "sirt_ref.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 35 | #include "thread.h" |
| 36 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 37 | namespace art { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 38 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | namespace mirror { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 40 | class Class; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 41 | class ArtField; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 42 | class Object; |
| 43 | } // namespace mirror |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 44 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 45 | // TODO: Fix no thread safety analysis when GCC can handle template specialization. |
| 46 | template <const bool kAccessCheck> |
| 47 | ALWAYS_INLINE static inline mirror::Class* CheckObjectAlloc(uint32_t type_idx, |
| 48 | mirror::ArtMethod* method, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 49 | Thread* self, bool* slow_path) |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 50 | NO_THREAD_SAFETY_ANALYSIS { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 51 | mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 52 | if (UNLIKELY(klass == NULL)) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 53 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 54 | *slow_path = true; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 55 | if (klass == NULL) { |
| 56 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 57 | return nullptr; // Failure |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 60 | if (kAccessCheck) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 61 | if (UNLIKELY(!klass->IsInstantiable())) { |
| 62 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 63 | self->ThrowNewException(throw_location, "Ljava/lang/InstantiationError;", |
| 64 | PrettyDescriptor(klass).c_str()); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 65 | *slow_path = true; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 66 | return nullptr; // Failure |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 67 | } |
| 68 | mirror::Class* referrer = method->GetDeclaringClass(); |
| 69 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 70 | ThrowIllegalAccessErrorClass(referrer, klass); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 71 | *slow_path = true; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 72 | return nullptr; // Failure |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 75 | if (UNLIKELY(!klass->IsInitialized())) { |
| 76 | SirtRef<mirror::Class> sirt_klass(self, klass); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 77 | // EnsureInitialized (the class initializer) might cause a GC. |
| 78 | // may cause us to suspend meaning that another thread may try to |
| 79 | // change the allocator while we are stuck in the entrypoints of |
| 80 | // an old allocator. Also, the class initialization may fail. To |
| 81 | // handle these cases we mark the slow path boolean as true so |
| 82 | // that the caller knows to check the allocator type to see if it |
| 83 | // has changed and to null-check the return value in case the |
| 84 | // initialization fails. |
| 85 | *slow_path = true; |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 86 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_klass, true, true)) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 87 | DCHECK(self->IsExceptionPending()); |
| 88 | return nullptr; // Failure |
| 89 | } |
| 90 | return sirt_klass.get(); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 91 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 92 | return klass; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 95 | // TODO: Fix no thread safety analysis when annotalysis is smarter. |
| 96 | ALWAYS_INLINE static inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass, |
| 97 | Thread* self, bool* slow_path) |
| 98 | NO_THREAD_SAFETY_ANALYSIS { |
| 99 | if (UNLIKELY(!klass->IsInitialized())) { |
| 100 | SirtRef<mirror::Class> sirt_class(self, 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; |
| 110 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_class, true, true)) { |
| 111 | DCHECK(self->IsExceptionPending()); |
| 112 | return nullptr; // Failure |
| 113 | } |
| 114 | return sirt_class.get(); |
| 115 | } |
| 116 | return klass; |
| 117 | } |
| 118 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 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. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 123 | // TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter. |
| 124 | template <bool kAccessCheck, bool kInstrumented> |
| 125 | ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx, |
| 126 | mirror::ArtMethod* method, |
| 127 | Thread* self, |
| 128 | gc::AllocatorType allocator_type) |
| 129 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [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 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 137 | return klass->Alloc<kInstrumented>(self, heap->GetCurrentAllocator()); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 138 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 139 | return klass->Alloc<kInstrumented>(self, allocator_type); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 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> |
| 145 | ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass, |
| 146 | mirror::ArtMethod* method, |
| 147 | Thread* self, |
| 148 | gc::AllocatorType allocator_type) |
| 149 | NO_THREAD_SAFETY_ANALYSIS { |
| 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 | return klass->Alloc<kInstrumented>(self, heap->GetCurrentAllocator()); |
| 159 | } |
| 160 | return klass->Alloc<kInstrumented>(self, allocator_type); |
| 161 | } |
| 162 | |
| 163 | // Given the context of a calling Method and an initialized class, create an instance. |
| 164 | // TODO: Fix NO_THREAD_SAFETY_ANALYSIS when GCC is smarter. |
| 165 | template <bool kInstrumented> |
| 166 | ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass, |
| 167 | mirror::ArtMethod* method, |
| 168 | Thread* self, |
| 169 | gc::AllocatorType allocator_type) |
| 170 | NO_THREAD_SAFETY_ANALYSIS { |
| 171 | DCHECK(klass != nullptr); |
| 172 | return klass->Alloc<kInstrumented>(self, allocator_type); |
| 173 | } |
| 174 | |
| 175 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 176 | // TODO: Fix no thread safety analysis when GCC can handle template specialization. |
| 177 | template <bool kAccessCheck> |
| 178 | ALWAYS_INLINE static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx, |
| 179 | mirror::ArtMethod* method, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 180 | int32_t component_count, |
| 181 | bool* slow_path) |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 182 | NO_THREAD_SAFETY_ANALYSIS { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 183 | if (UNLIKELY(component_count < 0)) { |
| 184 | ThrowNegativeArraySizeException(component_count); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 185 | *slow_path = true; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 186 | return nullptr; // Failure |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 187 | } |
| 188 | mirror::Class* klass = method->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 189 | if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 190 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 191 | *slow_path = true; |
| 192 | if (klass == nullptr) { // Error |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 193 | DCHECK(Thread::Current()->IsExceptionPending()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 194 | return nullptr; // Failure |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 195 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 196 | CHECK(klass->IsArrayClass()) << PrettyClass(klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 197 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 198 | if (kAccessCheck) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 199 | mirror::Class* referrer = method->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 200 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 201 | ThrowIllegalAccessErrorClass(referrer, klass); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 202 | *slow_path = true; |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 203 | return nullptr; // Failure |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 206 | return klass; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 210 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 211 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 212 | // check. |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 213 | // TODO: Fix no thread safety analysis when GCC can handle template specialization. |
| 214 | template <bool kAccessCheck, bool kInstrumented> |
| 215 | ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx, |
| 216 | mirror::ArtMethod* method, |
| 217 | int32_t component_count, |
| 218 | Thread* self, |
| 219 | gc::AllocatorType allocator_type) |
| 220 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 221 | bool slow_path = false; |
| 222 | mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, method, component_count, |
| 223 | &slow_path); |
| 224 | if (UNLIKELY(slow_path)) { |
| 225 | if (klass == nullptr) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 229 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, |
| 230 | heap->GetCurrentAllocator()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 231 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 232 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, allocator_type); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame^] | 235 | template <bool kAccessCheck, bool kInstrumented> |
| 236 | ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass, |
| 237 | mirror::ArtMethod* method, |
| 238 | int32_t component_count, |
| 239 | Thread* self, |
| 240 | gc::AllocatorType allocator_type) |
| 241 | NO_THREAD_SAFETY_ANALYSIS { |
| 242 | DCHECK(klass != nullptr); |
| 243 | if (UNLIKELY(component_count < 0)) { |
| 244 | ThrowNegativeArraySizeException(component_count); |
| 245 | return nullptr; // Failure |
| 246 | } |
| 247 | if (kAccessCheck) { |
| 248 | mirror::Class* referrer = method->GetDeclaringClass(); |
| 249 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 250 | ThrowIllegalAccessErrorClass(referrer, klass); |
| 251 | return nullptr; // Failure |
| 252 | } |
| 253 | } |
| 254 | // No need to retry a slow-path allocation as the above code won't |
| 255 | // cause a GC or thread suspension. |
| 256 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, allocator_type); |
| 257 | } |
| 258 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 259 | extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 260 | int32_t component_count, Thread* self, |
| 261 | bool access_check, |
| 262 | gc::AllocatorType allocator_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 263 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 264 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 265 | extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx, |
| 266 | mirror::ArtMethod* method, |
| 267 | int32_t component_count, Thread* self, |
| 268 | bool access_check, |
| 269 | gc::AllocatorType allocator_type) |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 270 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 271 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 272 | // Type of find field operation for fast and slow case. |
| 273 | enum FindFieldType { |
| 274 | InstanceObjectRead, |
| 275 | InstanceObjectWrite, |
| 276 | InstancePrimitiveRead, |
| 277 | InstancePrimitiveWrite, |
| 278 | StaticObjectRead, |
| 279 | StaticObjectWrite, |
| 280 | StaticPrimitiveRead, |
| 281 | StaticPrimitiveWrite, |
| 282 | }; |
| 283 | |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 284 | template<FindFieldType type, bool access_check> |
| 285 | static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, const mirror::ArtMethod* referrer, |
| 286 | Thread* self, size_t expected_size) { |
| 287 | bool is_primitive; |
| 288 | bool is_set; |
| 289 | bool is_static; |
| 290 | switch (type) { |
| 291 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 292 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 293 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 294 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 295 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 296 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 297 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 298 | case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through. |
| 299 | default: is_primitive = true; is_set = true; is_static = true; break; |
| 300 | } |
| 301 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 302 | mirror::ArtField* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); |
| 303 | if (UNLIKELY(resolved_field == nullptr)) { |
| 304 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 305 | return nullptr; // Failure. |
| 306 | } |
| 307 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 308 | if (access_check) { |
| 309 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 310 | ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer); |
| 311 | return nullptr; |
| 312 | } |
| 313 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 314 | if (UNLIKELY(!referring_class->CanAccessResolvedField<true>(fields_class, resolved_field, |
| 315 | field_idx))) { |
| 316 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 317 | return nullptr; // Failure. |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 318 | } |
| 319 | if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { |
| 320 | ThrowIllegalAccessErrorFinalField(referrer, resolved_field); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 321 | return nullptr; // Failure. |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 322 | } else { |
| 323 | FieldHelper fh(resolved_field); |
| 324 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 325 | fh.FieldSize() != expected_size)) { |
| 326 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 327 | DCHECK(throw_location.GetMethod() == referrer); |
| 328 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;", |
| 329 | "Attempted read of %zd-bit %s on field '%s'", |
| 330 | expected_size * (32 / sizeof(int32_t)), |
| 331 | is_primitive ? "primitive" : "non-primitive", |
| 332 | PrettyField(resolved_field, true).c_str()); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 333 | return nullptr; // Failure. |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
| 337 | if (!is_static) { |
| 338 | // instance fields must be being accessed on an initialized class |
| 339 | return resolved_field; |
| 340 | } else { |
| 341 | // If the class is initialized we're done. |
| 342 | if (LIKELY(fields_class->IsInitialized())) { |
| 343 | return resolved_field; |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 344 | } else { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 345 | SirtRef<mirror::Class> sirt_class(self, fields_class); |
| 346 | if (LIKELY(class_linker->EnsureInitialized(sirt_class, true, true))) { |
| 347 | // Otherwise let's ensure the class is initialized before resolving the field. |
| 348 | return resolved_field; |
| 349 | } else { |
| 350 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 351 | return nullptr; // Failure. |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 352 | } |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Explicit template declarations of FindFieldFromCode for all field access types. |
| 358 | #define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \ |
| 359 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 360 | mirror::ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \ |
| 361 | const mirror::ArtMethod* referrer, \ |
| 362 | Thread* self, size_t expected_size) \ |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 363 | |
| 364 | #define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \ |
| 365 | EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \ |
| 366 | EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true) |
| 367 | |
| 368 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead); |
| 369 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite); |
| 370 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead); |
| 371 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite); |
| 372 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead); |
| 373 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite); |
| 374 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead); |
| 375 | EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite); |
| 376 | |
| 377 | #undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL |
| 378 | #undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL |
| 379 | |
| 380 | template<InvokeType type, bool access_check> |
| 381 | static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object, |
| 382 | mirror::ArtMethod* referrer, Thread* self) { |
| 383 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 384 | mirror::ArtMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type); |
| 385 | if (UNLIKELY(resolved_method == nullptr)) { |
| 386 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 387 | return nullptr; // Failure. |
| 388 | } else if (UNLIKELY(this_object == nullptr && type != kStatic)) { |
| 389 | // Maintain interpreter-like semantics where NullPointerException is thrown |
| 390 | // after potential NoSuchMethodError from class linker. |
| 391 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 392 | DCHECK(referrer == throw_location.GetMethod()); |
| 393 | ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type); |
| 394 | return nullptr; // Failure. |
| 395 | } else if (access_check) { |
| 396 | // Incompatible class change should have been handled in resolve method. |
| 397 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
| 398 | ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method, |
| 399 | referrer); |
| 400 | return nullptr; // Failure. |
| 401 | } |
| 402 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
| 403 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 404 | bool can_access_resolved_method = |
| 405 | referring_class->CanAccessResolvedMethod<true, type>(methods_class, resolved_method, |
| 406 | method_idx); |
| 407 | if (UNLIKELY(!can_access_resolved_method)) { |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 408 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 409 | return nullptr; // Failure. |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | switch (type) { |
| 413 | case kStatic: |
| 414 | case kDirect: |
| 415 | return resolved_method; |
| 416 | case kVirtual: { |
| 417 | mirror::ObjectArray<mirror::ArtMethod>* vtable = this_object->GetClass()->GetVTable(); |
| 418 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 419 | if (access_check && |
| 420 | (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength()))) { |
| 421 | // Behavior to agree with that of the verifier. |
| 422 | MethodHelper mh(resolved_method); |
| 423 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(), |
| 424 | mh.GetSignature()); |
| 425 | return nullptr; // Failure. |
| 426 | } |
| 427 | DCHECK(vtable != nullptr); |
| 428 | return vtable->GetWithoutChecks(vtable_index); |
| 429 | } |
| 430 | case kSuper: { |
| 431 | mirror::Class* super_class = referrer->GetDeclaringClass()->GetSuperClass(); |
| 432 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 433 | mirror::ObjectArray<mirror::ArtMethod>* vtable; |
| 434 | if (access_check) { |
| 435 | // Check existence of super class. |
| 436 | vtable = (super_class != nullptr) ? super_class->GetVTable() : nullptr; |
| 437 | if (vtable == nullptr || vtable_index >= static_cast<uint32_t>(vtable->GetLength())) { |
| 438 | // Behavior to agree with that of the verifier. |
| 439 | MethodHelper mh(resolved_method); |
| 440 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(), |
| 441 | mh.GetSignature()); |
| 442 | return nullptr; // Failure. |
| 443 | } |
| 444 | } else { |
| 445 | // Super class must exist. |
| 446 | DCHECK(super_class != nullptr); |
| 447 | vtable = super_class->GetVTable(); |
| 448 | } |
| 449 | DCHECK(vtable != nullptr); |
| 450 | return vtable->GetWithoutChecks(vtable_index); |
| 451 | } |
| 452 | case kInterface: { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 453 | uint32_t imt_index = resolved_method->GetDexMethodIndex() % ClassLinker::kImtSize; |
| 454 | mirror::ObjectArray<mirror::ArtMethod>* imt_table = this_object->GetClass()->GetImTable(); |
| 455 | mirror::ArtMethod* imt_method = imt_table->Get(imt_index); |
| 456 | if (!imt_method->IsImtConflictMethod()) { |
| 457 | return imt_method; |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 458 | } else { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 459 | mirror::ArtMethod* interface_method = |
| 460 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 461 | if (UNLIKELY(interface_method == nullptr)) { |
| 462 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, |
| 463 | referrer); |
| 464 | return nullptr; // Failure. |
| 465 | } else { |
| 466 | return interface_method; |
| 467 | } |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | default: |
| 471 | LOG(FATAL) << "Unknown invoke type " << type; |
| 472 | return nullptr; // Failure. |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // Explicit template declarations of FindMethodFromCode for all invoke types. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 477 | #define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \ |
| 478 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE \ |
| 479 | mirror::ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \ |
| 480 | mirror::Object* this_object, \ |
| 481 | mirror::ArtMethod* referrer, \ |
| 482 | Thread* self) |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 483 | #define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \ |
| 484 | EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \ |
| 485 | EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true) |
| 486 | |
| 487 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic); |
| 488 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect); |
| 489 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual); |
| 490 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper); |
| 491 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface); |
| 492 | |
| 493 | #undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL |
| 494 | #undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 495 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 496 | // Fast path field resolution that can't initialize classes or throw exceptions. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 497 | static inline mirror::ArtField* FindFieldFast(uint32_t field_idx, |
| 498 | const mirror::ArtMethod* referrer, |
| 499 | FindFieldType type, size_t expected_size) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 500 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 501 | mirror::ArtField* resolved_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 502 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 503 | if (UNLIKELY(resolved_field == NULL)) { |
| 504 | return NULL; |
| 505 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 506 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 507 | // Check class is initiliazed or initializing. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 508 | if (UNLIKELY(!fields_class->IsInitializing())) { |
| 509 | return NULL; |
| 510 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 511 | // Check for incompatible class change. |
| 512 | bool is_primitive; |
| 513 | bool is_set; |
| 514 | bool is_static; |
| 515 | switch (type) { |
| 516 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 517 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 518 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 519 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 520 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 521 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 522 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 523 | case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break; |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 524 | default: |
| 525 | LOG(FATAL) << "UNREACHABLE"; // Assignment below to avoid GCC warnings. |
| 526 | is_primitive = true; |
| 527 | is_set = true; |
| 528 | is_static = true; |
| 529 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 530 | } |
| 531 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 532 | // Incompatible class change. |
| 533 | return NULL; |
| 534 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 535 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 536 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 537 | !referring_class->CanAccessMember(fields_class, |
| 538 | resolved_field->GetAccessFlags()) || |
| 539 | (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 540 | // Illegal access. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | FieldHelper fh(resolved_field); |
| 544 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 545 | fh.FieldSize() != expected_size)) { |
| 546 | return NULL; |
| 547 | } |
| 548 | return resolved_field; |
| 549 | } |
| 550 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 551 | // Fast path method resolution that can't throw exceptions. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 552 | static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx, |
| 553 | mirror::Object* this_object, |
| 554 | const mirror::ArtMethod* referrer, |
| 555 | bool access_check, InvokeType type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 556 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 557 | bool is_direct = type == kStatic || type == kDirect; |
| 558 | if (UNLIKELY(this_object == NULL && !is_direct)) { |
| 559 | return NULL; |
| 560 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 561 | mirror::ArtMethod* resolved_method = |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 562 | referrer->GetDeclaringClass()->GetDexCache()->GetResolvedMethod(method_idx); |
| 563 | if (UNLIKELY(resolved_method == NULL)) { |
| 564 | return NULL; |
| 565 | } |
| 566 | if (access_check) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 567 | // Check for incompatible class change errors and access. |
| 568 | bool icce = resolved_method->CheckIncompatibleClassChange(type); |
| 569 | if (UNLIKELY(icce)) { |
| 570 | return NULL; |
| 571 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 572 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
| 573 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 574 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 575 | !referring_class->CanAccessMember(methods_class, |
| 576 | resolved_method->GetAccessFlags()))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 577 | // Potential illegal access, may need to refine the method's class. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 578 | return NULL; |
| 579 | } |
| 580 | } |
| 581 | if (type == kInterface) { // Most common form of slow path dispatch. |
| 582 | return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 583 | } else if (is_direct) { |
| 584 | return resolved_method; |
| 585 | } else if (type == kSuper) { |
| 586 | return referrer->GetDeclaringClass()->GetSuperClass()->GetVTable()-> |
| 587 | Get(resolved_method->GetMethodIndex()); |
| 588 | } else { |
| 589 | DCHECK(type == kVirtual); |
| 590 | return this_object->GetClass()->GetVTable()->Get(resolved_method->GetMethodIndex()); |
| 591 | } |
| 592 | } |
| 593 | |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 594 | static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 595 | const mirror::ArtMethod* referrer, |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 596 | Thread* self, bool can_run_clinit, |
| 597 | bool verify_access) |
| 598 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 599 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 600 | mirror::Class* klass = class_linker->ResolveType(type_idx, referrer); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 601 | if (UNLIKELY(klass == nullptr)) { |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 602 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 603 | return nullptr; // Failure - Indicate to caller to deliver exception |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 604 | } |
| 605 | // Perform access check if necessary. |
| 606 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 607 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
| 608 | ThrowIllegalAccessErrorClass(referring_class, klass); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 609 | return nullptr; // Failure - Indicate to caller to deliver exception |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 610 | } |
| 611 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 612 | if (!can_run_clinit) { |
| 613 | return klass; |
| 614 | } |
| 615 | // If we are the <clinit> of this class, just return our storage. |
| 616 | // |
| 617 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 618 | // running. |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 619 | if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) { |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 620 | return klass; |
| 621 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 622 | SirtRef<mirror::Class> sirt_class(self, klass); |
| 623 | if (!class_linker->EnsureInitialized(sirt_class, true, true)) { |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 624 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 5ddb410 | 2014-01-07 08:58:46 -0800 | [diff] [blame] | 625 | return nullptr; // Failure - Indicate to caller to deliver exception |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 626 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 627 | return sirt_class.get(); |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 628 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 629 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 630 | extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 631 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 632 | static inline mirror::String* ResolveStringFromCode(const mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 633 | uint32_t string_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 634 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 635 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 636 | return class_linker->ResolveString(string_idx, referrer); |
| 637 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 638 | |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 639 | static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 640 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 641 | UNLOCK_FUNCTION(monitor_lock_) { |
| 642 | // Save any pending exception over monitor exit call. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 643 | mirror::Throwable* saved_exception = NULL; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 644 | ThrowLocation saved_throw_location; |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 645 | if (UNLIKELY(self->IsExceptionPending())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 646 | saved_exception = self->GetException(&saved_throw_location); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 647 | self->ClearException(); |
| 648 | } |
| 649 | // Decode locked object and unlock, before popping local references. |
| 650 | self->DecodeJObject(locked)->MonitorExit(self); |
| 651 | if (UNLIKELY(self->IsExceptionPending())) { |
| 652 | LOG(FATAL) << "Synchronized JNI code returning with an exception:\n" |
| 653 | << saved_exception->Dump() |
| 654 | << "\nEncountered second exception during implicit MonitorExit:\n" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 655 | << self->GetException(NULL)->Dump(); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 656 | } |
| 657 | // Restore pending exception. |
| 658 | if (saved_exception != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 659 | self->SetException(saved_throw_location, saved_exception); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 663 | static inline void CheckReferenceResult(mirror::Object* o, Thread* self) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 664 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 665 | if (o == NULL) { |
| 666 | return; |
| 667 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 668 | mirror::ArtMethod* m = self->GetCurrentMethod(NULL); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 669 | if (o == kInvalidIndirectRefObject) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 670 | JniAbortF(NULL, "invalid reference returned from %s", PrettyMethod(m).c_str()); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 671 | } |
| 672 | // Make sure that the result is an instance of the type this method was expected to return. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 673 | mirror::Class* return_type = MethodHelper(m).GetReturnType(); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 674 | |
| 675 | if (!o->InstanceOf(return_type)) { |
| 676 | JniAbortF(NULL, "attempt to return an instance of %s from %s", |
| 677 | PrettyTypeOf(o).c_str(), PrettyMethod(m).c_str()); |
| 678 | } |
| 679 | } |
| 680 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 681 | static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
jeffhao | 373c52f | 2012-11-20 16:11:52 -0800 | [diff] [blame] | 682 | for (;;) { |
| 683 | if (thread->ReadFlag(kCheckpointRequest)) { |
| 684 | thread->RunCheckpointFunction(); |
jeffhao | 373c52f | 2012-11-20 16:11:52 -0800 | [diff] [blame] | 685 | } else if (thread->ReadFlag(kSuspendRequest)) { |
| 686 | thread->FullSuspendCheck(); |
| 687 | } else { |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 693 | JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 694 | jobject rcvr_jobj, jobject interface_art_method_jobj, |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 695 | std::vector<jvalue>& args) |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 696 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 697 | |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 698 | // Entry point for deoptimization. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 699 | extern "C" void art_quick_deoptimize(); |
| 700 | static inline uintptr_t GetQuickDeoptimizationEntryPoint() { |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 701 | return reinterpret_cast<uintptr_t>(art_quick_deoptimize); |
| 702 | } |
| 703 | |
| 704 | // Return address of instrumentation stub. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 705 | extern "C" void art_quick_instrumentation_entry(void*); |
| 706 | static inline void* GetQuickInstrumentationEntryPoint() { |
| 707 | return reinterpret_cast<void*>(art_quick_instrumentation_entry); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | // The return_pc of instrumentation exit stub. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 711 | extern "C" void art_quick_instrumentation_exit(); |
| 712 | static inline uintptr_t GetQuickInstrumentationExitPc() { |
| 713 | return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit); |
| 714 | } |
| 715 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 716 | extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 717 | static inline const void* GetPortableToInterpreterBridge() { |
| 718 | return reinterpret_cast<void*>(art_portable_to_interpreter_bridge); |
| 719 | } |
| 720 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 721 | extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 722 | static inline const void* GetQuickToInterpreterBridge() { |
| 723 | return reinterpret_cast<void*>(art_quick_to_interpreter_bridge); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | // Return address of interpreter stub. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 727 | static inline const void* GetCompiledCodeToInterpreterBridge() { |
| 728 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 729 | return GetPortableToInterpreterBridge(); |
| 730 | #else |
| 731 | return GetQuickToInterpreterBridge(); |
| 732 | #endif |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 735 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 736 | static inline const void* GetPortableResolutionTrampoline(ClassLinker* class_linker) { |
| 737 | return class_linker->GetPortableResolutionTrampoline(); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 740 | static inline const void* GetQuickResolutionTrampoline(ClassLinker* class_linker) { |
| 741 | return class_linker->GetQuickResolutionTrampoline(); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // Return address of resolution trampoline stub for defined compiler. |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 745 | static inline const void* GetResolutionTrampoline(ClassLinker* class_linker) { |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 746 | #if defined(ART_USE_PORTABLE_COMPILER) |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 747 | return GetPortableResolutionTrampoline(class_linker); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 748 | #else |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 749 | return GetQuickResolutionTrampoline(class_linker); |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 750 | #endif |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 753 | static inline const void* GetPortableImtConflictTrampoline(ClassLinker* class_linker) { |
| 754 | return class_linker->GetPortableImtConflictTrampoline(); |
| 755 | } |
| 756 | |
| 757 | static inline const void* GetQuickImtConflictTrampoline(ClassLinker* class_linker) { |
| 758 | return class_linker->GetQuickImtConflictTrampoline(); |
| 759 | } |
| 760 | |
| 761 | // Return address of imt conflict trampoline stub for defined compiler. |
| 762 | static inline const void* GetImtConflictTrampoline(ClassLinker* class_linker) { |
| 763 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 764 | return GetPortableImtConflictTrampoline(class_linker); |
| 765 | #else |
| 766 | return GetQuickImtConflictTrampoline(class_linker); |
| 767 | #endif |
| 768 | } |
| 769 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 770 | extern "C" void art_portable_proxy_invoke_handler(); |
| 771 | static inline const void* GetPortableProxyInvokeHandler() { |
| 772 | return reinterpret_cast<void*>(art_portable_proxy_invoke_handler); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 775 | extern "C" void art_quick_proxy_invoke_handler(); |
| 776 | static inline const void* GetQuickProxyInvokeHandler() { |
| 777 | return reinterpret_cast<void*>(art_quick_proxy_invoke_handler); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 780 | static inline const void* GetProxyInvokeHandler() { |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 781 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 782 | return GetPortableProxyInvokeHandler(); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 783 | #else |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 784 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 785 | #endif |
| 786 | } |
| 787 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 788 | extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject); |
Jeff Hao | 79fe539 | 2013-04-24 18:41:58 -0700 | [diff] [blame] | 789 | static inline void* GetJniDlsymLookupStub() { |
| 790 | return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub); |
| 791 | } |
Jeff Hao | 58df327 | 2013-04-22 15:28:53 -0700 | [diff] [blame] | 792 | |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 793 | template <typename INT_TYPE, typename FLOAT_TYPE> |
| 794 | static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) { |
| 795 | const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max()); |
| 796 | const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min()); |
| 797 | const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt); |
| 798 | const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt); |
| 799 | if (LIKELY(f > kMinIntAsFloat)) { |
| 800 | if (LIKELY(f < kMaxIntAsFloat)) { |
| 801 | return static_cast<INT_TYPE>(f); |
| 802 | } else { |
| 803 | return kMaxInt; |
| 804 | } |
| 805 | } else { |
| 806 | return (f != f) ? 0 : kMinInt; // f != f implies NaN |
| 807 | } |
| 808 | } |
| 809 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 810 | } // namespace art |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 811 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 812 | #endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |