blob: b36c5c3c96582d2e0ee0334d4e2cc19310bea0da [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001/*
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 Solanes4b8ea5d2021-11-19 10:14:54 +000022#include <sstream>
23
Andreas Gampec1d4cd72017-04-21 13:24:52 -070024#include "art_field-inl.h"
Matthew Gharrity465ecc82016-07-19 21:32:52 +000025#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000027#include "base/sdk_version.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "class_linker-inl.h"
29#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file.h"
David Sehr8c0961f2018-01-23 16:11:38 -080031#include "dex/invoke_type.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010032#include "entrypoints/quick/callee_save_frame.h"
33#include "handle_scope-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070034#include "imt_conflict_table.h"
35#include "imtable-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070036#include "indirect_reference_table.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010037#include "jni/jni_internal.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070038#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070039#include "mirror/class-alloc-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040#include "mirror/class-inl.h"
41#include "mirror/object-inl.h"
42#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010043#include "nth_caller_visitor.h"
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +010044#include "oat_file.h"
Alex Light55eccdf2019-10-07 13:51:13 +000045#include "reflective_handle_scope-inl.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010046#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010047#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070048#include "thread.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070049#include "well_known_classes.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070050
51namespace art {
52
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +000053inline 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 Solanes05ed65a2021-11-30 10:36:13 +000099 << (method_info.GetDexFileIndexKind() == MethodInfo::kKindBCP) << std::noboolalpha
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +0000100 << ", dex_file_index=" << std::dec << method_info.GetDexFileIndex() << ".";
101 return error_ss.str();
102}
103
Mathieu Chartiere401d142015-04-22 13:56:20 -0700104inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
David Srbecky8cd54542018-07-15 23:58:44 +0100105 const CodeInfo& code_info,
David Srbecky93bd3612018-07-02 19:30:18 +0100106 const BitTableRange<InlineInfo>& inline_infos)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700107 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoba118822017-06-12 15:41:56 +0100108 DCHECK(!outer_method->IsObsolete());
109
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100110 // 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 Chartier268764d2016-09-13 12:09:38 -0700113 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100114
David Srbecky93bd3612018-07-02 19:30:18 +0100115 {
116 InlineInfo inline_info = inline_infos.back();
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000117
David Srbecky93bd3612018-07-02 19:30:18 +0100118 if (inline_info.EncodesArtMethod()) {
119 return inline_info.GetArtMethod();
120 }
121
David Srbecky8cd54542018-07-15 23:58:44 +0100122 uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
David Srbecky93bd3612018-07-02 19:30:18 +0100123 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 Chartier45bf2502016-03-31 11:07:09 -0700129 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100130
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000131 // Find which method did the call in the inlining hierarchy.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100133 ArtMethod* method = outer_method;
David Srbecky93bd3612018-07-02 19:30:18 +0100134 for (InlineInfo inline_info : inline_infos) {
David Srbecky6e69e522018-06-03 12:00:14 +0100135 DCHECK(!inline_info.EncodesArtMethod());
136 DCHECK_NE(inline_info.GetDexPc(), static_cast<uint32_t>(-1));
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000137 MethodInfo method_info = code_info.GetMethodInfoOf(inline_info);
138 uint32_t method_index = method_info.GetMethodIndex();
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +0000139 const uint32_t dex_file_index = method_info.GetDexFileIndex();
140 ArtMethod* inlined_method = nullptr;
141 ObjPtr<mirror::DexCache> dex_cache = nullptr;
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000142 if (method_info.HasDexFileIndex()) {
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100143 if (method_info.GetDexFileIndexKind() == MethodInfo::kKindBCP) {
Santiago Aboy Solanes4b8ea5d2021-11-19 10:14:54 +0000144 ArrayRef<const DexFile* const> bcp_dex_files(class_linker->GetBootClassPath());
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +0000145 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 Solanes970ba212021-10-21 10:52:47 +0100151 dex_cache = class_linker->FindDexCache(Thread::Current(), *dex_file);
152 } else {
Santiago Aboy Solanes4b8ea5d2021-11-19 10:14:54 +0000153 ArrayRef<const OatDexFile* const> oat_dex_files(
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +0000154 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 Solanes4b8ea5d2021-11-19 10:14:54 +0000161 dex_cache = class_linker->FindDexCache(Thread::Current(), *odf);
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100162 }
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000163 } else {
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100164 dex_cache = outer_method->GetDexCache();
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000165 }
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100166 inlined_method =
167 class_linker->LookupResolvedMethod(method_index, dex_cache, dex_cache->GetClassLoader());
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +0000168
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100169 if (UNLIKELY(inlined_method == nullptr)) {
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +0000170 LOG(FATAL) << GetResolvedMethodErrorString(
171 class_linker, inlined_method, method, outer_method, dex_cache, method_info);
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100172 UNREACHABLE();
173 }
174 DCHECK(!inlined_method->IsRuntimeMethod());
Santiago Aboy Solanes970ba212021-10-21 10:52:47 +0100175 DCHECK_EQ(inlined_method->GetDexFile() == outer_method->GetDexFile(),
Santiago Aboy Solanesc3e004d2021-11-24 10:29:38 +0000176 dex_file_index == MethodInfo::kSameDexFile)
177 << GetResolvedMethodErrorString(
178 class_linker, inlined_method, method, outer_method, dex_cache, method_info);
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100179 method = inlined_method;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000180 }
181
Vladimir Marko63a9f3e2018-04-26 09:18:10 +0100182 return method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100183}
184
Vladimir Marko8e110652019-07-30 10:14:41 +0100185ALWAYS_INLINE
186inline 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 Marko4bb2af52019-03-22 11:09:19 +0000214ALWAYS_INLINE inline ObjPtr<mirror::Class> CheckObjectAlloc(ObjPtr<mirror::Class> klass,
215 Thread* self,
216 bool* slow_path)
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000217 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 Yang98d1cc82014-05-15 17:02:16 -0700221 *slow_path = true;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000222 return nullptr; // Failure
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223 }
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000224 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 Yang98d1cc82014-05-15 17:02:16 -0700229 }
Vladimir Marko8e110652019-07-30 10:14:41 +0100230 return CheckClassInitializedForObjectAlloc(klass, self, slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231}
232
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000233// 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.
235template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700236ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000237inline ObjPtr<mirror::Object> AllocObjectFromCode(ObjPtr<mirror::Class> klass,
238 Thread* self,
239 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240 bool slow_path = false;
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +0000241 klass = CheckObjectAlloc(klass, self, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 if (UNLIKELY(slow_path)) {
243 if (klass == nullptr) {
244 return nullptr;
245 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800246 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700247 return klass->Alloc</*kInstrumented=*/true>(
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800248 self,
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000249 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250 }
251 DCHECK(klass != nullptr);
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000252 return klass->Alloc<kInstrumented>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253}
254
255// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700257ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000258inline ObjPtr<mirror::Object> AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,
259 Thread* self,
260 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700261 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 Light2f494282020-01-07 22:53:09 +0000269 // Pass in kNoAddFinalizer since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800270 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
271 // instrumented.
Alex Light2f494282020-01-07 22:53:09 +0000272 return klass->Alloc</*kInstrumented=*/true, mirror::Class::AddFinalizer::kNoAddFinalizer>(
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100273 self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700274 }
Alex Light2f494282020-01-07 22:53:09 +0000275 // Pass in kNoAddFinalizer since the object cannot be finalizable.
276 return klass->Alloc<kInstrumented,
277 mirror::Class::AddFinalizer::kNoAddFinalizer>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700278}
279
280// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700281template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700282ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000283inline ObjPtr<mirror::Object> AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,
284 Thread* self,
285 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700286 DCHECK(klass != nullptr);
Alex Light2f494282020-01-07 22:53:09 +0000287 // Pass in kNoAddFinalizer since the object cannot be finalizable.
288 return klass->Alloc<kInstrumented,
289 mirror::Class::AddFinalizer::kNoAddFinalizer>(self, allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700290}
291
292
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700293template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700294ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000295inline ObjPtr<mirror::Class> CheckArrayAlloc(dex::TypeIndex type_idx,
296 int32_t component_count,
297 ArtMethod* method,
298 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700299 if (UNLIKELY(component_count < 0)) {
300 ThrowNegativeArraySizeException(component_count);
301 *slow_path = true;
302 return nullptr; // Failure
303 }
Vladimir Marko28e012a2017-12-07 11:22:59 +0000304 ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700305 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko942fd312017-01-16 20:52:19 +0000306 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko05792b92015-08-03 11:56:49 +0100307 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700308 *slow_path = true;
309 if (klass == nullptr) { // Error
310 DCHECK(Thread::Current()->IsExceptionPending());
311 return nullptr; // Failure
312 }
David Sehr709b0702016-10-13 09:12:37 -0700313 CHECK(klass->IsArrayClass()) << klass->PrettyClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700314 }
315 if (kAccessCheck) {
Vladimir Markod93e3742018-07-18 10:58:13 +0100316 ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317 if (UNLIKELY(!referrer->CanAccess(klass))) {
318 ThrowIllegalAccessErrorClass(referrer, klass);
319 *slow_path = true;
320 return nullptr; // Failure
321 }
322 }
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000323 return klass;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700324}
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 Yang98d1cc82014-05-15 17:02:16 -0700330template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700331ALWAYS_INLINE
Vladimir Markobcf17522018-06-01 13:14:32 +0100332inline 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 Yang98d1cc82014-05-15 17:02:16 -0700337 bool slow_path = false;
Vladimir Markobcf17522018-06-01 13:14:32 +0100338 ObjPtr<mirror::Class> klass =
339 CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method, &slow_path);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700340 if (UNLIKELY(slow_path)) {
341 if (klass == nullptr) {
342 return nullptr;
343 }
344 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800345 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700346 return mirror::Array::Alloc</*kInstrumented=*/true>(self,
347 klass,
348 component_count,
349 klass->GetComponentSizeShift(),
350 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700351 }
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100352 return mirror::Array::Alloc<kInstrumented>(self,
353 klass,
354 component_count,
355 klass->GetComponentSizeShift(),
356 allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700357}
358
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000359template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700360ALWAYS_INLINE
Vladimir Marko4bb2af52019-03-22 11:09:19 +0000361inline ObjPtr<mirror::Array> AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,
362 int32_t component_count,
363 Thread* self,
364 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 DCHECK(klass != nullptr);
366 if (UNLIKELY(component_count < 0)) {
367 ThrowNegativeArraySizeException(component_count);
368 return nullptr; // Failure
369 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700370 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
371 // suspension.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100372 return mirror::Array::Alloc<kInstrumented>(self,
373 klass,
374 component_count,
375 klass->GetComponentSizeShift(),
376 allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700377}
378
379template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800380inline ArtField* FindFieldFromCode(uint32_t field_idx,
381 ArtMethod* referrer,
382 Thread* self,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700383 size_t expected_size) {
David Srbeckyce32c102018-08-31 07:21:07 +0100384 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 Yang98d1cc82014-05-15 17:02:16 -0700387 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800388
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 Gampe542451c2016-07-26 09:02:02 -0700397 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800398
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 Markoe11dd502017-12-08 14:09:45 +0000403 resolved_field = class_linker->ResolveFieldJLS(field_idx,
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800404 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 Yang98d1cc82014-05-15 17:02:16 -0700412 if (UNLIKELY(resolved_field == nullptr)) {
413 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
414 return nullptr; // Failure.
415 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700416 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700417 if (access_check) {
418 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
419 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
420 return nullptr;
421 }
Vladimir Markod93e3742018-07-18 10:58:13 +0100422 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700423 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
424 resolved_field,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100425 referrer->GetDexCache(),
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700426 field_idx))) {
427 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
428 return nullptr; // Failure.
429 }
Alex Lightc9af14e2019-12-12 09:51:38 -0800430 if (UNLIKELY(is_set && !resolved_field->CanBeChangedBy(referrer))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700431 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 Geoffray0aa50ce2015-03-10 11:03:29 +0000436 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700437 "Attempted read of %zd-bit %s on field '%s'",
438 expected_size * (32 / sizeof(int32_t)),
439 is_primitive ? "primitive" : "non-primitive",
David Sehr709b0702016-10-13 09:12:37 -0700440 resolved_field->PrettyField(true).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700441 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 Markobaa81b52019-08-02 10:14:04 +0100450 if (LIKELY(fields_class->IsVisiblyInitialized())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700451 return resolved_field;
452 } else {
453 StackHandleScope<1> hs(self);
Alex Light55eccdf2019-10-07 13:51:13 +0000454 StackArtFieldHandleScope<1> rhs(self);
455 ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700456 if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700457 // Otherwise let's ensure the class is initialized before resolving the field.
Alex Light55eccdf2019-10-07 13:51:13 +0000458 return resolved_field_handle.Get();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700459 }
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 Gampebdf7f1c2016-08-30 16:38:47 -0700468template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700469ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 ArtMethod* referrer, \
471 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700472
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
477EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
478EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
479EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
480EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
481EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
482EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
483EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
484EXPLICIT_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 Srbeckyc853eed2018-09-13 14:33:22 +0100489// Follow virtual/interface indirections if applicable.
490// Will throw null-pointer exception the if the object is null.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700491template<InvokeType type, bool access_check>
David Srbeckyc853eed2018-09-13 14:33:22 +0100492ALWAYS_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 Yang98d1cc82014-05-15 17:02:16 -0700498 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
David Srbeckyc853eed2018-09-13 14:33:22 +0100499 // Null pointer check.
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700500 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 Yang98d1cc82014-05-15 17:02:16 -0700515 switch (type) {
516 case kStatic:
517 case kDirect:
518 return resolved_method;
519 case kVirtual: {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100520 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700521 uint16_t vtable_index = resolved_method->GetMethodIndex();
522 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700523 (!klass->HasVTable() ||
524 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700525 // 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 Sehr709b0702016-10-13 09:12:37 -0700530 DCHECK(klass->HasVTable()) << klass->PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700532 }
533 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700534 // 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 Chartierbe08cf52016-09-13 13:41:24 -0700539 StackHandleScope<2> hs2(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700540 HandleWrapperObjPtr<mirror::Object> h_this(hs2.NewHandleWrapper(this_object));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700541 Handle<mirror::Class> h_referring_class(hs2.NewHandle(referrer->GetDeclaringClass()));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800542 const dex::TypeIndex method_type_idx =
Alex Lightdba61482016-12-21 08:20:29 -0800543 referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko28e012a2017-12-07 11:22:59 +0000544 ObjPtr<mirror::Class> method_reference_class =
545 class_linker->ResolveType(method_type_idx, referrer);
Alex Light705ad492015-09-21 11:36:30 -0700546 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 Bikf663e342016-04-04 17:28:59 -0700551 // 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 Marko302f69c2017-07-25 15:27:15 +0100554 ObjPtr<mirror::Class> super_class = nullptr;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700555 if (method_reference_class->IsAssignableFrom(h_referring_class.Get())) {
556 super_class = h_referring_class->GetSuperClass();
Aart Bikf663e342016-04-04 17:28:59 -0700557 }
Alex Light705ad492015-09-21 11:36:30 -0700558 uint16_t vtable_index = resolved_method->GetMethodIndex();
559 if (access_check) {
560 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700561 if (super_class == nullptr ||
562 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700563 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 Chartierbe08cf52016-09-13 13:41:24 -0700576 if (!method_reference_class->IsAssignableFrom(h_this->GetClass())) {
Alex Light705ad492015-09-21 11:36:30 -0700577 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
578 method_reference_class,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700579 h_this.Get(),
Alex Light705ad492015-09-21 11:36:30 -0700580 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 Yang98d1cc82014-05-15 17:02:16 -0700589 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
590 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700591 }
Alex Light705ad492015-09-21 11:36:30 -0700592 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700593 }
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700594 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700595 }
596 case kInterface: {
David Srbeckye36e7f22018-11-14 14:21:23 +0000597 size_t imt_index = resolved_method->GetImtIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -0700598 PointerSize pointer_size = class_linker->GetImagePointerSize();
Vladimir Marko302f69c2017-07-25 15:27:15 +0100599 ObjPtr<mirror::Class> klass = (*this_object)->GetClass();
600 ArtMethod* imt_method = klass->GetImt(pointer_size)->Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000601 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700602 if (kIsDebugBuild) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700603 ArtMethod* method = klass->FindVirtualMethodForInterface(
604 resolved_method, class_linker->GetImagePointerSize());
David Sehr709b0702016-10-13 09:12:37 -0700605 CHECK_EQ(imt_method, method) << ArtMethod::PrettyMethod(resolved_method) << " / "
606 << imt_method->PrettyMethod() << " / "
607 << ArtMethod::PrettyMethod(method) << " / "
608 << klass->PrettyClass();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700609 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700610 return imt_method;
611 } else {
Vladimir Marko302f69c2017-07-25 15:27:15 +0100612 ArtMethod* interface_method = klass->FindVirtualMethodForInterface(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700613 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700614 if (UNLIKELY(interface_method == nullptr)) {
615 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700616 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700617 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 Srbeckyc853eed2018-09-13 14:33:22 +0100628template<InvokeType type, bool access_check>
629inline 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 Yang98d1cc82014-05-15 17:02:16 -0700653// Explicit template declarations of FindMethodFromCode for all invoke types.
654#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700655 template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
Mathieu Chartieref41db72016-10-25 15:08:01 -0700657 ObjPtr<mirror::Object>* this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700658 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700659 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700660#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
664EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
665EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
666EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
667EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
668EXPLICIT_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 Chartiere401d142015-04-22 13:56:20 -0700674inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
675 size_t expected_size) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700676 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
David Srbecky5de5efe2021-02-15 21:23:00 +0000677 ArtField* resolved_field = referrer->GetDexCache()->GetResolvedField(field_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 if (UNLIKELY(resolved_field == nullptr)) {
679 return nullptr;
680 }
681 // Check for incompatible class change.
David Srbeckyce32c102018-08-31 07:21:07 +0100682 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 Yang98d1cc82014-05-15 17:02:16 -0700685 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
686 // Incompatible class change.
687 return nullptr;
688 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700689 ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700690 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 Markobaa81b52019-08-02 10:14:04 +0100693 if (UNLIKELY(!fields_class->IsVisiblyInitialized())) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700694 return nullptr;
695 }
696 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100697 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700698 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Alex Lightc9af14e2019-12-12 09:51:38 -0800700 (is_set && !resolved_field->CanBeChangedBy(referrer)))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700701 // 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 Markof79aa7f2017-07-04 16:58:55 +0100712template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700713inline ArtMethod* FindMethodFast(uint32_t method_idx,
714 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100715 ArtMethod* referrer) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700716 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700717 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
718 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700719 }
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100720 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
721 ObjPtr<mirror::DexCache> dex_cache = referrer->GetDexCache();
Vladimir Markoba118822017-06-12 15:41:56 +0100722 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 Chartier2cebb242015-04-21 16:50:40 -0700727 if (UNLIKELY(resolved_method == nullptr)) {
728 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700729 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700730 if (type == kInterface) { // Most common form of slow path dispatch.
Andreas Gampe542451c2016-07-26 09:02:02 -0700731 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method,
732 kRuntimePointerSize);
Jeff Hao207a37d2014-10-29 17:24:25 -0700733 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700734 return resolved_method;
735 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700736 // TODO This lookup is rather slow.
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000737 dex::TypeIndex method_type_idx = dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000738 ObjPtr<mirror::Class> method_reference_class = linker->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000739 method_type_idx, dex_cache, referrer->GetClassLoader());
Alex Light705ad492015-09-21 11:36:30 -0700740 if (method_reference_class == nullptr) {
741 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000742 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700743 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700744 // 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 Markof79aa7f2017-07-04 16:58:55 +0100750 ObjPtr<mirror::Class> super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700751 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
752 // The super class does not have the method.
753 return nullptr;
754 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700755 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), kRuntimePointerSize);
Alex Light705ad492015-09-21 11:36:30 -0700756 } else {
757 return method_reference_class->FindVirtualMethodForInterfaceSuper(
Andreas Gampe542451c2016-07-26 09:02:02 -0700758 resolved_method, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000759 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700760 } else {
761 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700762 return this_object->GetClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700763 resolved_method->GetMethodIndex(), kRuntimePointerSize);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700764 }
765}
766
Vladimir Marko28e012a2017-12-07 11:22:59 +0000767inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
768 ArtMethod* referrer,
769 Thread* self,
770 bool can_run_clinit,
771 bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700772 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko28e012a2017-12-07 11:22:59 +0000773 ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700774 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 Markod93e3742018-07-18 10:58:13 +0100779 ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700780 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 Rogers7b078e82014-09-10 14:44:24 -0700797 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700798 CHECK(self->IsExceptionPending());
799 return nullptr; // Failure - Indicate to caller to deliver exception
800 }
801 return h_class.Get();
802}
803
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700804template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800805inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700806 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 Marko5115a4d2019-10-17 14:56:47 +0100821inline 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 Marko4d527152021-11-23 12:07:04 +0000828inline ObjPtr<mirror::Object> GetGenericJniSynchronizationObject(Thread* self, ArtMethod* called)
Vladimir Markocedec9d2021-02-08 16:16:13 +0000829 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 Marko4d527152021-11-23 12:07:04 +0000834 // 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 Markocedec9d2021-02-08 16:16:13 +0000838 if (called->IsStatic()) {
Vladimir Marko654f01c2021-05-26 16:40:20 +0100839 // Static methods synchronize on the declaring class object.
Vladimir Marko4d527152021-11-23 12:07:04 +0000840 return called->GetDeclaringClass<kWithoutReadBarrier>();
Vladimir Markocedec9d2021-02-08 16:16:13 +0000841 } else {
Vladimir Marko654f01c2021-05-26 16:40:20 +0100842 // Instance methods synchronize on the `this` object.
Vladimir Markocedec9d2021-02-08 16:16:13 +0000843 // 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 Marko4d527152021-11-23 12:07:04 +0000846 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 Markocedec9d2021-02-08 16:16:13 +0000849 }
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000850}
851
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700852} // namespace art
853
854#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_