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 | |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 23 | #include "base/enums.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 24 | #include "class_linker-inl.h" |
| 25 | #include "common_throws.h" |
| 26 | #include "dex_file.h" |
Vladimir Marko | 5ea536a | 2015-04-20 20:11:30 +0100 | [diff] [blame] | 27 | #include "entrypoints/quick/callee_save_frame.h" |
| 28 | #include "handle_scope-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 29 | #include "imt_conflict_table.h" |
| 30 | #include "imtable-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 31 | #include "indirect_reference_table.h" |
| 32 | #include "invoke_type.h" |
| 33 | #include "jni_internal.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 34 | #include "mirror/array.h" |
| 35 | #include "mirror/class-inl.h" |
| 36 | #include "mirror/object-inl.h" |
| 37 | #include "mirror/throwable.h" |
Vladimir Marko | 5ea536a | 2015-04-20 20:11:30 +0100 | [diff] [blame] | 38 | #include "nth_caller_visitor.h" |
| 39 | #include "runtime.h" |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 40 | #include "stack_map.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 41 | #include "thread.h" |
| 42 | |
| 43 | namespace art { |
| 44 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 45 | inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method, |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 46 | const InlineInfo& inline_info, |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 47 | const InlineInfoEncoding& encoding, |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 48 | uint8_t inlining_depth) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 49 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 50 | // This method is being used by artQuickResolutionTrampoline, before it sets up |
| 51 | // the passed parameters in a GC friendly way. Therefore we must never be |
| 52 | // suspended while executing it. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 53 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 54 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 55 | if (inline_info.EncodesArtMethodAtDepth(encoding, inlining_depth)) { |
| 56 | return inline_info.GetArtMethodAtDepth(encoding, inlining_depth); |
| 57 | } |
| 58 | |
David Srbecky | 61b28a1 | 2016-02-25 21:55:03 +0000 | [diff] [blame] | 59 | uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 60 | if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) { |
| 61 | // "charAt" special case. It is the only non-leaf method we inline across dex files. |
| 62 | ArtMethod* inlined_method = jni::DecodeArtMethod(WellKnownClasses::java_lang_String_charAt); |
| 63 | DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index); |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 64 | return inlined_method; |
Mathieu Chartier | 45bf250 | 2016-03-31 11:07:09 -0700 | [diff] [blame] | 65 | } |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 66 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 67 | // Find which method did the call in the inlining hierarchy. |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 68 | ArtMethod* caller = outer_method; |
| 69 | if (inlining_depth != 0) { |
| 70 | caller = GetResolvedMethod(outer_method, |
| 71 | inline_info, |
| 72 | encoding, |
| 73 | inlining_depth - 1); |
| 74 | } |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 75 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 76 | // Lookup the declaring class of the inlined method. |
| 77 | const DexFile* dex_file = caller->GetDexFile(); |
| 78 | const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index); |
Mathieu Chartier | 20eb58e | 2017-02-01 15:50:54 -0800 | [diff] [blame^] | 79 | ArtMethod* inlined_method = caller->GetDexCacheResolvedMethod(method_index, kRuntimePointerSize); |
| 80 | if (inlined_method != nullptr && !inlined_method->IsRuntimeMethod()) { |
| 81 | return inlined_method; |
| 82 | } |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 83 | const char* descriptor = dex_file->StringByTypeIdx(method_id.class_idx_); |
| 84 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 85 | Thread* self = Thread::Current(); |
| 86 | mirror::ClassLoader* class_loader = caller->GetDeclaringClass()->GetClassLoader(); |
| 87 | mirror::Class* klass = class_linker->LookupClass(self, descriptor, class_loader); |
| 88 | if (klass == nullptr) { |
| 89 | LOG(FATAL) << "Could not find an inlined method from an .oat file: " |
| 90 | << "the class " << descriptor << " was not found in the class loader of " |
| 91 | << caller->PrettyMethod() << ". " |
| 92 | << "This must be due to playing wrongly with class loaders"; |
| 93 | } |
| 94 | |
| 95 | // Lookup the method. |
| 96 | const char* method_name = dex_file->GetMethodName(method_id); |
| 97 | const Signature signature = dex_file->GetMethodSignature(method_id); |
| 98 | |
Mathieu Chartier | 20eb58e | 2017-02-01 15:50:54 -0800 | [diff] [blame^] | 99 | inlined_method = klass->FindDeclaredDirectMethod(method_name, signature, kRuntimePointerSize); |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 100 | if (inlined_method == nullptr) { |
| 101 | inlined_method = klass->FindDeclaredVirtualMethod(method_name, signature, kRuntimePointerSize); |
| 102 | if (inlined_method == nullptr) { |
| 103 | LOG(FATAL) << "Could not find an inlined method from an .oat file: " |
| 104 | << "the class " << descriptor << " does not have " |
| 105 | << method_name << signature << " declared. " |
| 106 | << "This must be due to duplicate classes or playing wrongly with class loaders"; |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 107 | } |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 108 | } |
Mathieu Chartier | 20eb58e | 2017-02-01 15:50:54 -0800 | [diff] [blame^] | 109 | caller->SetDexCacheResolvedMethod(method_index, inlined_method, kRuntimePointerSize); |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 110 | |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 111 | return inlined_method; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 114 | ALWAYS_INLINE inline mirror::Class* CheckObjectAlloc(mirror::Class* klass, |
| 115 | Thread* self, |
| 116 | bool* slow_path) |
| 117 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 118 | REQUIRES(!Roles::uninterruptible_) { |
| 119 | if (UNLIKELY(!klass->IsInstantiable())) { |
| 120 | self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 121 | *slow_path = true; |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 122 | return nullptr; // Failure |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 123 | } |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 124 | if (UNLIKELY(klass->IsClassClass())) { |
| 125 | ThrowIllegalAccessError(nullptr, "Class %s is inaccessible", |
| 126 | klass->PrettyDescriptor().c_str()); |
| 127 | *slow_path = true; |
| 128 | return nullptr; // Failure |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 129 | } |
| 130 | if (UNLIKELY(!klass->IsInitialized())) { |
| 131 | StackHandleScope<1> hs(self); |
| 132 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 133 | // EnsureInitialized (the class initializer) might cause a GC. |
| 134 | // may cause us to suspend meaning that another thread may try to |
| 135 | // change the allocator while we are stuck in the entrypoints of |
| 136 | // an old allocator. Also, the class initialization may fail. To |
| 137 | // handle these cases we mark the slow path boolean as true so |
| 138 | // that the caller knows to check the allocator type to see if it |
| 139 | // has changed and to null-check the return value in case the |
| 140 | // initialization fails. |
| 141 | *slow_path = true; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 142 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 143 | DCHECK(self->IsExceptionPending()); |
| 144 | return nullptr; // Failure |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 145 | } else { |
| 146 | DCHECK(!self->IsExceptionPending()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 147 | } |
| 148 | return h_klass.Get(); |
| 149 | } |
| 150 | return klass; |
| 151 | } |
| 152 | |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 153 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 154 | inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass, |
| 155 | Thread* self, |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 156 | bool* slow_path) |
| 157 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 158 | REQUIRES(!Roles::uninterruptible_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 159 | if (UNLIKELY(!klass->IsInitialized())) { |
| 160 | StackHandleScope<1> hs(self); |
| 161 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 162 | // EnsureInitialized (the class initializer) might cause a GC. |
| 163 | // may cause us to suspend meaning that another thread may try to |
| 164 | // change the allocator while we are stuck in the entrypoints of |
| 165 | // an old allocator. Also, the class initialization may fail. To |
| 166 | // handle these cases we mark the slow path boolean as true so |
| 167 | // that the caller knows to check the allocator type to see if it |
| 168 | // has changed and to null-check the return value in case the |
| 169 | // initialization fails. |
| 170 | *slow_path = true; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 171 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 172 | DCHECK(self->IsExceptionPending()); |
| 173 | return nullptr; // Failure |
| 174 | } |
| 175 | return h_class.Get(); |
| 176 | } |
| 177 | return klass; |
| 178 | } |
| 179 | |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 180 | // Allocate an instance of klass. Throws InstantationError if klass is not instantiable, |
| 181 | // or IllegalAccessError if klass is j.l.Class. Performs a clinit check too. |
| 182 | template <bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 183 | ALWAYS_INLINE |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 184 | inline mirror::Object* AllocObjectFromCode(mirror::Class* klass, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 185 | Thread* self, |
| 186 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 187 | bool slow_path = false; |
Nicolas Geoffray | 0d3998b | 2017-01-12 15:35:12 +0000 | [diff] [blame] | 188 | klass = CheckObjectAlloc(klass, self, &slow_path); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 189 | if (UNLIKELY(slow_path)) { |
| 190 | if (klass == nullptr) { |
| 191 | return nullptr; |
| 192 | } |
Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 193 | // CheckObjectAlloc can cause thread suspension which means we may now be instrumented. |
| 194 | return klass->Alloc</*kInstrumented*/true>( |
| 195 | self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 196 | Runtime::Current()->GetHeap()->GetCurrentAllocator()).Ptr(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 197 | } |
| 198 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 199 | return klass->Alloc<kInstrumented>(self, allocator_type).Ptr(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // 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] | 203 | template <bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 204 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 205 | inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass, |
| 206 | Thread* self, |
| 207 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 208 | DCHECK(klass != nullptr); |
| 209 | bool slow_path = false; |
| 210 | klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path); |
| 211 | if (UNLIKELY(slow_path)) { |
| 212 | if (klass == nullptr) { |
| 213 | return nullptr; |
| 214 | } |
| 215 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 216 | // Pass in false since the object cannot be finalizable. |
Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 217 | // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be |
| 218 | // instrumented. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 219 | return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator()).Ptr(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 220 | } |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 221 | // Pass in false since the object cannot be finalizable. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 222 | return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // 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] | 226 | template <bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 227 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 228 | inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass, |
| 229 | Thread* self, |
| 230 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 231 | DCHECK(klass != nullptr); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 232 | // Pass in false since the object cannot be finalizable. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 233 | return klass->Alloc<kInstrumented, false>(self, allocator_type).Ptr(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 237 | template <bool kAccessCheck> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 238 | ALWAYS_INLINE |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 239 | inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 240 | int32_t component_count, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 241 | ArtMethod* method, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 242 | bool* slow_path) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 243 | if (UNLIKELY(component_count < 0)) { |
| 244 | ThrowNegativeArraySizeException(component_count); |
| 245 | *slow_path = true; |
| 246 | return nullptr; // Failure |
| 247 | } |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 248 | mirror::Class* klass = method->GetDexCache()->GetResolvedType(type_idx); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 249 | if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 250 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 251 | klass = class_linker->ResolveType(type_idx, method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 252 | *slow_path = true; |
| 253 | if (klass == nullptr) { // Error |
| 254 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 255 | return nullptr; // Failure |
| 256 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 257 | CHECK(klass->IsArrayClass()) << klass->PrettyClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 258 | } |
| 259 | if (kAccessCheck) { |
| 260 | mirror::Class* referrer = method->GetDeclaringClass(); |
| 261 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 262 | ThrowIllegalAccessErrorClass(referrer, klass); |
| 263 | *slow_path = true; |
| 264 | return nullptr; // Failure |
| 265 | } |
| 266 | } |
| 267 | return klass; |
| 268 | } |
| 269 | |
| 270 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 271 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 272 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 273 | // check. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 274 | template <bool kAccessCheck, bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 275 | ALWAYS_INLINE |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 276 | inline mirror::Array* AllocArrayFromCode(dex::TypeIndex type_idx, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 277 | int32_t component_count, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 278 | ArtMethod* method, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 279 | Thread* self, |
| 280 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 281 | bool slow_path = false; |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 282 | mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 283 | &slow_path); |
| 284 | if (UNLIKELY(slow_path)) { |
| 285 | if (klass == nullptr) { |
| 286 | return nullptr; |
| 287 | } |
| 288 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 14b0a5d | 2016-03-11 17:22:23 -0800 | [diff] [blame] | 289 | // CheckArrayAlloc can cause thread suspension which means we may now be instrumented. |
| 290 | return mirror::Array::Alloc</*kInstrumented*/true>(self, |
| 291 | klass, |
| 292 | component_count, |
| 293 | klass->GetComponentSizeShift(), |
| 294 | heap->GetCurrentAllocator()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 295 | } |
| 296 | return mirror::Array::Alloc<kInstrumented>(self, klass, component_count, |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 297 | klass->GetComponentSizeShift(), allocator_type); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 300 | template <bool kInstrumented> |
Hiroshi Yamauchi | eb1e929 | 2014-08-06 12:41:15 -0700 | [diff] [blame] | 301 | ALWAYS_INLINE |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 302 | inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass, |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 303 | int32_t component_count, |
| 304 | Thread* self, |
| 305 | gc::AllocatorType allocator_type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 306 | DCHECK(klass != nullptr); |
| 307 | if (UNLIKELY(component_count < 0)) { |
| 308 | ThrowNegativeArraySizeException(component_count); |
| 309 | return nullptr; // Failure |
| 310 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 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, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 321 | size_t expected_size) { |
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. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 346 | ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Igor Murashkin | f1b4c41 | 2016-02-01 17:40:19 -0800 | [diff] [blame] | 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 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 366 | ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 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(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 373 | if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, |
| 374 | resolved_field, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 375 | field_idx))) { |
| 376 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 377 | return nullptr; // Failure. |
| 378 | } |
| 379 | if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { |
| 380 | ThrowIllegalAccessErrorFinalField(referrer, resolved_field); |
| 381 | return nullptr; // Failure. |
| 382 | } else { |
| 383 | if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive || |
| 384 | resolved_field->FieldSize() != expected_size)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 385 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 386 | "Attempted read of %zd-bit %s on field '%s'", |
| 387 | expected_size * (32 / sizeof(int32_t)), |
| 388 | is_primitive ? "primitive" : "non-primitive", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 389 | resolved_field->PrettyField(true).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 390 | return nullptr; // Failure. |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | if (!is_static) { |
| 395 | // instance fields must be being accessed on an initialized class |
| 396 | return resolved_field; |
| 397 | } else { |
| 398 | // If the class is initialized we're done. |
| 399 | if (LIKELY(fields_class->IsInitialized())) { |
| 400 | return resolved_field; |
| 401 | } else { |
| 402 | StackHandleScope<1> hs(self); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 403 | if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_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) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 415 | template REQUIRES_SHARED(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 | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 437 | inline ArtMethod* FindMethodFromCode(uint32_t method_idx, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 438 | ObjPtr<mirror::Object>* this_object, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 439 | ArtMethod* referrer, |
| 440 | Thread* self) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 441 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 3a35714 | 2015-08-07 17:20:11 -0700 | [diff] [blame] | 442 | ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 443 | if (resolved_method == nullptr) { |
| 444 | StackHandleScope<1> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 445 | ObjPtr<mirror::Object> null_this = nullptr; |
| 446 | HandleWrapperObjPtr<mirror::Object> h_this( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 447 | hs.NewHandleWrapper(type == kStatic ? &null_this : this_object)); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 448 | constexpr ClassLinker::ResolveMode resolve_mode = |
| 449 | access_check ? ClassLinker::kForceICCECheck |
| 450 | : ClassLinker::kNoICCECheckForCache; |
| 451 | resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 452 | } |
Aart Bik | 2a1b7ac | 2016-06-29 14:54:26 -0700 | [diff] [blame] | 453 | // Resolution and access check. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 454 | if (UNLIKELY(resolved_method == nullptr)) { |
| 455 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 456 | return nullptr; // Failure. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 457 | } else if (access_check) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 458 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 459 | bool can_access_resolved_method = |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 460 | referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class, |
| 461 | resolved_method, |
| 462 | method_idx); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 463 | if (UNLIKELY(!can_access_resolved_method)) { |
| 464 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 465 | return nullptr; // Failure. |
| 466 | } |
Nicolas Geoffray | 470d54f | 2015-10-02 17:14:53 +0100 | [diff] [blame] | 467 | // Incompatible class change should have been handled in resolve method. |
| 468 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
| 469 | ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method, |
| 470 | referrer); |
| 471 | return nullptr; // Failure. |
| 472 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 473 | } |
Aart Bik | 2a1b7ac | 2016-06-29 14:54:26 -0700 | [diff] [blame] | 474 | // Next, null pointer check. |
| 475 | if (UNLIKELY(*this_object == nullptr && type != kStatic)) { |
| 476 | if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() && |
| 477 | resolved_method->IsConstructor())) { |
| 478 | // Hack for String init: |
| 479 | // |
| 480 | // We assume that the input of String.<init> in verified code is always |
| 481 | // an unitialized reference. If it is a null constant, it must have been |
| 482 | // optimized out by the compiler. Do not throw NullPointerException. |
| 483 | } else { |
| 484 | // Maintain interpreter-like semantics where NullPointerException is thrown |
| 485 | // after potential NoSuchMethodError from class linker. |
| 486 | ThrowNullPointerExceptionForMethodAccess(method_idx, type); |
| 487 | return nullptr; // Failure. |
| 488 | } |
| 489 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 490 | switch (type) { |
| 491 | case kStatic: |
| 492 | case kDirect: |
| 493 | return resolved_method; |
| 494 | case kVirtual: { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 495 | mirror::Class* klass = (*this_object)->GetClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 496 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 497 | if (access_check && |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 498 | (!klass->HasVTable() || |
| 499 | vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 500 | // Behavior to agree with that of the verifier. |
| 501 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), |
| 502 | resolved_method->GetName(), resolved_method->GetSignature()); |
| 503 | return nullptr; // Failure. |
| 504 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 505 | DCHECK(klass->HasVTable()) << klass->PrettyClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 506 | return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 507 | } |
| 508 | case kSuper: { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 509 | // TODO This lookup is quite slow. |
| 510 | // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since |
| 511 | // that will actually not be what we want in some cases where there are miranda methods or |
| 512 | // defaults. What we actually need is a GetContainingClass that says which classes virtuals |
| 513 | // this method is coming from. |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 514 | StackHandleScope<2> hs2(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 515 | HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object)); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 516 | Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass())); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 517 | const dex::TypeIndex method_type_idx = |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 518 | referrer->GetDexFile()->GetMethodId(method_idx).class_idx_; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 519 | mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer); |
| 520 | if (UNLIKELY(method_reference_class == nullptr)) { |
| 521 | // Bad type idx. |
| 522 | CHECK(self->IsExceptionPending()); |
| 523 | return nullptr; |
| 524 | } else if (!method_reference_class->IsInterface()) { |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 525 | // It is not an interface. If the referring class is in the class hierarchy of the |
| 526 | // referenced class in the bytecode, we use its super class. Otherwise, we throw |
| 527 | // a NoSuchMethodError. |
| 528 | mirror::Class* super_class = nullptr; |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 529 | if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) { |
| 530 | super_class = h_referring_class->GetSuperClass(); |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 531 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 532 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 533 | if (access_check) { |
| 534 | // Check existence of super class. |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 535 | if (super_class == nullptr || |
| 536 | !super_class->HasVTable() || |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 537 | vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) { |
| 538 | // Behavior to agree with that of the verifier. |
| 539 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), |
| 540 | resolved_method->GetName(), resolved_method->GetSignature()); |
| 541 | return nullptr; // Failure. |
| 542 | } |
| 543 | } |
| 544 | DCHECK(super_class != nullptr); |
| 545 | DCHECK(super_class->HasVTable()); |
| 546 | return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize()); |
| 547 | } else { |
| 548 | // It is an interface. |
| 549 | if (access_check) { |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 550 | if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 551 | ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method, |
| 552 | method_reference_class, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 553 | h_this.Get(), |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 554 | referrer); |
| 555 | return nullptr; // Failure. |
| 556 | } |
| 557 | } |
| 558 | // TODO We can do better than this for a (compiled) fastpath. |
| 559 | ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper( |
| 560 | resolved_method, class_linker->GetImagePointerSize()); |
| 561 | // Throw an NSME if nullptr; |
| 562 | if (result == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 563 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), |
| 564 | resolved_method->GetName(), resolved_method->GetSignature()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 565 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 566 | return result; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 567 | } |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 568 | UNREACHABLE(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 569 | } |
| 570 | case kInterface: { |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 571 | uint32_t imt_index = ImTable::GetImtIndex(resolved_method); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 572 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 573 | ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)-> |
| 574 | Get(imt_index, pointer_size); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 575 | if (!imt_method->IsRuntimeMethod()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 576 | if (kIsDebugBuild) { |
| 577 | mirror::Class* klass = (*this_object)->GetClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 578 | ArtMethod* method = klass->FindVirtualMethodForInterface( |
| 579 | resolved_method, class_linker->GetImagePointerSize()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 580 | CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / " |
| 581 | << imt_method->PrettyMethod() << " / " |
| 582 | << ArtMethod::PrettyMethod(method) << " / " |
| 583 | << klass->PrettyClass(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 584 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 585 | return imt_method; |
| 586 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 587 | ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface( |
| 588 | resolved_method, class_linker->GetImagePointerSize()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 589 | if (UNLIKELY(interface_method == nullptr)) { |
| 590 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, |
Andreas Gampe | 3a35714 | 2015-08-07 17:20:11 -0700 | [diff] [blame] | 591 | *this_object, referrer); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 592 | return nullptr; // Failure. |
| 593 | } |
| 594 | return interface_method; |
| 595 | } |
| 596 | } |
| 597 | default: |
| 598 | LOG(FATAL) << "Unknown invoke type " << type; |
| 599 | return nullptr; // Failure. |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Explicit template declarations of FindMethodFromCode for all invoke types. |
| 604 | #define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 605 | template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 606 | ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \ |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 607 | ObjPtr<mirror::Object>* this_object, \ |
Andreas Gampe | 3a35714 | 2015-08-07 17:20:11 -0700 | [diff] [blame] | 608 | ArtMethod* referrer, \ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 609 | Thread* self) |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 610 | #define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \ |
| 611 | EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \ |
| 612 | EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true) |
| 613 | |
| 614 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic); |
| 615 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect); |
| 616 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual); |
| 617 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper); |
| 618 | EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface); |
| 619 | |
| 620 | #undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL |
| 621 | #undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL |
| 622 | |
| 623 | // Fast path field resolution that can't initialize classes or throw exceptions. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 624 | inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type, |
| 625 | size_t expected_size) { |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 626 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 627 | ArtField* resolved_field = |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 628 | referrer->GetDexCache()->GetResolvedField(field_idx, kRuntimePointerSize); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 629 | if (UNLIKELY(resolved_field == nullptr)) { |
| 630 | return nullptr; |
| 631 | } |
| 632 | // Check for incompatible class change. |
| 633 | bool is_primitive; |
| 634 | bool is_set; |
| 635 | bool is_static; |
| 636 | switch (type) { |
| 637 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 638 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 639 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 640 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 641 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 642 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 643 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 644 | case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break; |
| 645 | default: |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 646 | LOG(FATAL) << "UNREACHABLE"; |
| 647 | UNREACHABLE(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 648 | } |
| 649 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 650 | // Incompatible class change. |
| 651 | return nullptr; |
| 652 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 653 | ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 654 | if (is_static) { |
| 655 | // Check class is initialized else fail so that we can contend to initialize the class with |
| 656 | // other threads that may be racing to do this. |
| 657 | if (UNLIKELY(!fields_class->IsInitialized())) { |
| 658 | return nullptr; |
| 659 | } |
| 660 | } |
| 661 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 662 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 663 | !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) || |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 664 | (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) { |
| 665 | // Illegal access. |
| 666 | return nullptr; |
| 667 | } |
| 668 | if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive || |
| 669 | resolved_field->FieldSize() != expected_size)) { |
| 670 | return nullptr; |
| 671 | } |
| 672 | return resolved_field; |
| 673 | } |
| 674 | |
| 675 | // Fast path method resolution that can't throw exceptions. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 676 | inline ArtMethod* FindMethodFast(uint32_t method_idx, |
| 677 | ObjPtr<mirror::Object> this_object, |
| 678 | ArtMethod* referrer, |
| 679 | bool access_check, |
| 680 | InvokeType type) { |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 681 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 682 | if (UNLIKELY(this_object == nullptr && type != kStatic)) { |
| 683 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 684 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 685 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 686 | ArtMethod* resolved_method = |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 687 | referrer->GetDexCache()->GetResolvedMethod(method_idx, kRuntimePointerSize); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 688 | if (UNLIKELY(resolved_method == nullptr)) { |
| 689 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 690 | } |
| 691 | if (access_check) { |
| 692 | // Check for incompatible class change errors and access. |
| 693 | bool icce = resolved_method->CheckIncompatibleClassChange(type); |
| 694 | if (UNLIKELY(icce)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 695 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 696 | } |
| 697 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 698 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 699 | !referring_class->CanAccessMember(methods_class, |
| 700 | resolved_method->GetAccessFlags()))) { |
| 701 | // Potential illegal access, may need to refine the method's class. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 702 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | if (type == kInterface) { // Most common form of slow path dispatch. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 706 | return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, |
| 707 | kRuntimePointerSize); |
Jeff Hao | 207a37d | 2014-10-29 17:24:25 -0700 | [diff] [blame] | 708 | } else if (type == kStatic || type == kDirect) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 709 | return resolved_method; |
| 710 | } else if (type == kSuper) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 711 | // TODO This lookup is rather slow. |
Vladimir Marko | d16363a | 2017-02-01 14:09:13 +0000 | [diff] [blame] | 712 | dex::TypeIndex method_type_idx = |
| 713 | referrer->GetDexFile()->GetMethodId(method_idx).class_idx_; |
| 714 | mirror::Class* method_reference_class = |
| 715 | referrer->GetDexCache()->GetResolvedType(method_type_idx); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 716 | if (method_reference_class == nullptr) { |
| 717 | // Need to do full type resolution... |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 718 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 719 | } else if (!method_reference_class->IsInterface()) { |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 720 | // It is not an interface. If the referring class is in the class hierarchy of the |
| 721 | // referenced class in the bytecode, we use its super class. Otherwise, we cannot |
| 722 | // resolve the method. |
| 723 | if (!method_reference_class->IsAssignableFrom(referring_class)) { |
| 724 | return nullptr; |
| 725 | } |
| 726 | mirror::Class* super_class = referring_class->GetSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 727 | if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) { |
| 728 | // The super class does not have the method. |
| 729 | return nullptr; |
| 730 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 731 | return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 732 | } else { |
| 733 | return method_reference_class->FindVirtualMethodForInterfaceSuper( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 734 | resolved_method, kRuntimePointerSize); |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 735 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 736 | } else { |
| 737 | DCHECK(type == kVirtual); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 738 | return this_object->GetClass()->GetVTableEntry( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 739 | resolved_method->GetMethodIndex(), kRuntimePointerSize); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 743 | inline mirror::Class* ResolveVerifyAndClinit(dex::TypeIndex type_idx, |
| 744 | ArtMethod* referrer, |
| 745 | Thread* self, |
| 746 | bool can_run_clinit, |
| 747 | bool verify_access) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 748 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 749 | mirror::Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 750 | if (UNLIKELY(klass == nullptr)) { |
| 751 | CHECK(self->IsExceptionPending()); |
| 752 | return nullptr; // Failure - Indicate to caller to deliver exception |
| 753 | } |
| 754 | // Perform access check if necessary. |
| 755 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 756 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
| 757 | ThrowIllegalAccessErrorClass(referring_class, klass); |
| 758 | return nullptr; // Failure - Indicate to caller to deliver exception |
| 759 | } |
| 760 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 761 | if (!can_run_clinit) { |
| 762 | return klass; |
| 763 | } |
| 764 | // If we are the <clinit> of this class, just return our storage. |
| 765 | // |
| 766 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 767 | // running. |
| 768 | if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) { |
| 769 | return klass; |
| 770 | } |
| 771 | StackHandleScope<1> hs(self); |
| 772 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 773 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 774 | CHECK(self->IsExceptionPending()); |
| 775 | return nullptr; // Failure - Indicate to caller to deliver exception |
| 776 | } |
| 777 | return h_class.Get(); |
| 778 | } |
| 779 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 780 | inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, dex::StringIndex string_idx) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 781 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 782 | return class_linker->ResolveString(string_idx, referrer); |
| 783 | } |
| 784 | |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 785 | inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 786 | // Save any pending exception over monitor exit call. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 787 | mirror::Throwable* saved_exception = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 788 | if (UNLIKELY(self->IsExceptionPending())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 789 | saved_exception = self->GetException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 790 | self->ClearException(); |
| 791 | } |
| 792 | // Decode locked object and unlock, before popping local references. |
| 793 | self->DecodeJObject(locked)->MonitorExit(self); |
| 794 | if (UNLIKELY(self->IsExceptionPending())) { |
| 795 | LOG(FATAL) << "Synchronized JNI code returning with an exception:\n" |
| 796 | << saved_exception->Dump() |
| 797 | << "\nEncountered second exception during implicit MonitorExit:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 798 | << self->GetException()->Dump(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 799 | } |
| 800 | // Restore pending exception. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 801 | if (saved_exception != nullptr) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 802 | self->SetException(saved_exception); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 806 | template <typename INT_TYPE, typename FLOAT_TYPE> |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 807 | inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 808 | const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max()); |
| 809 | const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min()); |
| 810 | const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt); |
| 811 | const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt); |
| 812 | if (LIKELY(f > kMinIntAsFloat)) { |
| 813 | if (LIKELY(f < kMaxIntAsFloat)) { |
| 814 | return static_cast<INT_TYPE>(f); |
| 815 | } else { |
| 816 | return kMaxInt; |
| 817 | } |
| 818 | } else { |
| 819 | return (f != f) ? 0 : kMinInt; // f != f implies NaN |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | } // namespace art |
| 824 | |
| 825 | #endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_ |