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