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