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