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