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