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