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