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