xref: /aosp_15_r20/art/runtime/entrypoints/entrypoint_utils-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "entrypoint_utils.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <sstream>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/sdk_version.h"
28*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "common_throws.h"
30*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
31*795d594fSAndroid Build Coastguard Worker #include "dex/invoke_type.h"
32*795d594fSAndroid Build Coastguard Worker #include "entrypoints/quick/callee_save_frame.h"
33*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "imt_conflict_table.h"
35*795d594fSAndroid Build Coastguard Worker #include "imtable-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "indirect_reference_table.h"
37*795d594fSAndroid Build Coastguard Worker #include "mirror/array-alloc-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "mirror/class-alloc-inl.h"
39*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
40*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
41*795d594fSAndroid Build Coastguard Worker #include "mirror/throwable.h"
42*795d594fSAndroid Build Coastguard Worker #include "nth_caller_visitor.h"
43*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file.h"
44*795d594fSAndroid Build Coastguard Worker #include "oat/stack_map.h"
45*795d594fSAndroid Build Coastguard Worker #include "reflective_handle_scope-inl.h"
46*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
47*795d594fSAndroid Build Coastguard Worker #include "thread.h"
48*795d594fSAndroid Build Coastguard Worker #include "well_known_classes.h"
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
51*795d594fSAndroid Build Coastguard Worker 
GetResolvedMethodErrorString(ClassLinker * class_linker,ArtMethod * inlined_method,ArtMethod * parent_method,ArtMethod * outer_method,ObjPtr<mirror::DexCache> dex_cache,MethodInfo method_info)52*795d594fSAndroid Build Coastguard Worker inline std::string GetResolvedMethodErrorString(ClassLinker* class_linker,
53*795d594fSAndroid Build Coastguard Worker                                                 ArtMethod* inlined_method,
54*795d594fSAndroid Build Coastguard Worker                                                 ArtMethod* parent_method,
55*795d594fSAndroid Build Coastguard Worker                                                 ArtMethod* outer_method,
56*795d594fSAndroid Build Coastguard Worker                                                 ObjPtr<mirror::DexCache> dex_cache,
57*795d594fSAndroid Build Coastguard Worker                                                 MethodInfo method_info)
58*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
59*795d594fSAndroid Build Coastguard Worker   const uint32_t method_index = method_info.GetMethodIndex();
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   std::stringstream error_ss;
62*795d594fSAndroid Build Coastguard Worker   std::string separator = "";
63*795d594fSAndroid Build Coastguard Worker   error_ss << "BCP vector {";
64*795d594fSAndroid Build Coastguard Worker   for (const DexFile* df : class_linker->GetBootClassPath()) {
65*795d594fSAndroid Build Coastguard Worker     error_ss << separator << df << "(" << df->GetLocation() << ")";
66*795d594fSAndroid Build Coastguard Worker     separator = ", ";
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker   error_ss << "}. oat_dex_files vector: {";
69*795d594fSAndroid Build Coastguard Worker   separator = "";
70*795d594fSAndroid Build Coastguard Worker   for (const OatDexFile* odf_value :
71*795d594fSAndroid Build Coastguard Worker        parent_method->GetDexFile()->GetOatDexFile()->GetOatFile()->GetOatDexFiles()) {
72*795d594fSAndroid Build Coastguard Worker     error_ss << separator << odf_value << "(" << odf_value->GetDexFileLocation() << ")";
73*795d594fSAndroid Build Coastguard Worker     separator = ", ";
74*795d594fSAndroid Build Coastguard Worker   }
75*795d594fSAndroid Build Coastguard Worker   error_ss << "}. ";
76*795d594fSAndroid Build Coastguard Worker   if (inlined_method != nullptr) {
77*795d594fSAndroid Build Coastguard Worker     error_ss << "Inlined method: " << inlined_method->PrettyMethod() << " ("
78*795d594fSAndroid Build Coastguard Worker              << inlined_method->GetDexFile()->GetLocation() << "/"
79*795d594fSAndroid Build Coastguard Worker              << static_cast<const void*>(inlined_method->GetDexFile()) << "). ";
80*795d594fSAndroid Build Coastguard Worker   } else if (dex_cache != nullptr) {
81*795d594fSAndroid Build Coastguard Worker     error_ss << "Could not find an inlined method from an .oat file, using dex_cache to print the "
82*795d594fSAndroid Build Coastguard Worker                 "inlined method: "
83*795d594fSAndroid Build Coastguard Worker              << dex_cache->GetDexFile()->PrettyMethod(method_index) << " ("
84*795d594fSAndroid Build Coastguard Worker              << dex_cache->GetDexFile()->GetLocation() << "/"
85*795d594fSAndroid Build Coastguard Worker              << static_cast<const void*>(dex_cache->GetDexFile()) << "). ";
86*795d594fSAndroid Build Coastguard Worker   } else {
87*795d594fSAndroid Build Coastguard Worker     error_ss << "Both inlined_method and dex_cache are null. This means that we had an OOB access "
88*795d594fSAndroid Build Coastguard Worker              << "to either bcp_dex_files or oat_dex_files. ";
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker   error_ss << "The outer method is: " << parent_method->PrettyMethod() << " ("
91*795d594fSAndroid Build Coastguard Worker            << parent_method->GetDexFile()->GetLocation() << "/"
92*795d594fSAndroid Build Coastguard Worker            << static_cast<const void*>(parent_method->GetDexFile())
93*795d594fSAndroid Build Coastguard Worker            << "). The outermost method in the chain is: " << outer_method->PrettyMethod() << " ("
94*795d594fSAndroid Build Coastguard Worker            << outer_method->GetDexFile()->GetLocation() << "/"
95*795d594fSAndroid Build Coastguard Worker            << static_cast<const void*>(outer_method->GetDexFile())
96*795d594fSAndroid Build Coastguard Worker            << "). MethodInfo: method_index=" << std::dec << method_index
97*795d594fSAndroid Build Coastguard Worker            << ", is_in_bootclasspath=" << std::boolalpha
98*795d594fSAndroid Build Coastguard Worker            << (method_info.GetDexFileIndexKind() == MethodInfo::kKindBCP) << std::noboolalpha
99*795d594fSAndroid Build Coastguard Worker            << ", dex_file_index=" << std::dec << method_info.GetDexFileIndex() << ".";
100*795d594fSAndroid Build Coastguard Worker   return error_ss.str();
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker 
GetResolvedMethod(ArtMethod * outer_method,const CodeInfo & code_info,const BitTableRange<InlineInfo> & inline_infos)103*795d594fSAndroid Build Coastguard Worker inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
104*795d594fSAndroid Build Coastguard Worker                                     const CodeInfo& code_info,
105*795d594fSAndroid Build Coastguard Worker                                     const BitTableRange<InlineInfo>& inline_infos)
106*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
107*795d594fSAndroid Build Coastguard Worker   DCHECK(!outer_method->IsObsolete());
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   // This method is being used by artQuickResolutionTrampoline, before it sets up
110*795d594fSAndroid Build Coastguard Worker   // the passed parameters in a GC friendly way. Therefore we must never be
111*795d594fSAndroid Build Coastguard Worker   // suspended while executing it.
112*795d594fSAndroid Build Coastguard Worker   ScopedAssertNoThreadSuspension sants(__FUNCTION__);
113*795d594fSAndroid Build Coastguard Worker 
114*795d594fSAndroid Build Coastguard Worker   {
115*795d594fSAndroid Build Coastguard Worker     InlineInfo inline_info = inline_infos.back();
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker     if (inline_info.EncodesArtMethod()) {
118*795d594fSAndroid Build Coastguard Worker       return inline_info.GetArtMethod();
119*795d594fSAndroid Build Coastguard Worker     }
120*795d594fSAndroid Build Coastguard Worker 
121*795d594fSAndroid Build Coastguard Worker     uint32_t method_index = code_info.GetMethodIndexOf(inline_info);
122*795d594fSAndroid Build Coastguard Worker     if (inline_info.GetDexPc() == static_cast<uint32_t>(-1)) {
123*795d594fSAndroid Build Coastguard Worker       // "charAt" special case. It is the only non-leaf method we inline across dex files.
124*795d594fSAndroid Build Coastguard Worker       ArtMethod* inlined_method = WellKnownClasses::java_lang_String_charAt;
125*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(inlined_method->GetDexMethodIndex(), method_index);
126*795d594fSAndroid Build Coastguard Worker       return inlined_method;
127*795d594fSAndroid Build Coastguard Worker     }
128*795d594fSAndroid Build Coastguard Worker   }
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   // Find which method did the call in the inlining hierarchy.
131*795d594fSAndroid Build Coastguard Worker   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
132*795d594fSAndroid Build Coastguard Worker   ArtMethod* method = outer_method;
133*795d594fSAndroid Build Coastguard Worker   for (InlineInfo inline_info : inline_infos) {
134*795d594fSAndroid Build Coastguard Worker     DCHECK(!inline_info.EncodesArtMethod());
135*795d594fSAndroid Build Coastguard Worker     DCHECK_NE(inline_info.GetDexPc(), static_cast<uint32_t>(-1));
136*795d594fSAndroid Build Coastguard Worker     MethodInfo method_info = code_info.GetMethodInfoOf(inline_info);
137*795d594fSAndroid Build Coastguard Worker     uint32_t method_index = method_info.GetMethodIndex();
138*795d594fSAndroid Build Coastguard Worker     const uint32_t dex_file_index = method_info.GetDexFileIndex();
139*795d594fSAndroid Build Coastguard Worker     ArtMethod* inlined_method = nullptr;
140*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::DexCache> dex_cache = nullptr;
141*795d594fSAndroid Build Coastguard Worker     if (method_info.HasDexFileIndex()) {
142*795d594fSAndroid Build Coastguard Worker       if (method_info.GetDexFileIndexKind() == MethodInfo::kKindBCP) {
143*795d594fSAndroid Build Coastguard Worker         ArrayRef<const DexFile* const> bcp_dex_files(class_linker->GetBootClassPath());
144*795d594fSAndroid Build Coastguard Worker         DCHECK_LT(dex_file_index, bcp_dex_files.size())
145*795d594fSAndroid Build Coastguard Worker             << "OOB access to bcp_dex_files. Dumping info: "
146*795d594fSAndroid Build Coastguard Worker             << GetResolvedMethodErrorString(
147*795d594fSAndroid Build Coastguard Worker                    class_linker, inlined_method, method, outer_method, dex_cache, method_info);
148*795d594fSAndroid Build Coastguard Worker         const DexFile* dex_file = bcp_dex_files[dex_file_index];
149*795d594fSAndroid Build Coastguard Worker         DCHECK_NE(dex_file, nullptr);
150*795d594fSAndroid Build Coastguard Worker         dex_cache = class_linker->FindDexCache(Thread::Current(), *dex_file);
151*795d594fSAndroid Build Coastguard Worker       } else {
152*795d594fSAndroid Build Coastguard Worker         ArrayRef<const OatDexFile* const> oat_dex_files(
153*795d594fSAndroid Build Coastguard Worker             outer_method->GetDexFile()->GetOatDexFile()->GetOatFile()->GetOatDexFiles());
154*795d594fSAndroid Build Coastguard Worker         DCHECK_LT(dex_file_index, oat_dex_files.size())
155*795d594fSAndroid Build Coastguard Worker             << "OOB access to oat_dex_files. Dumping info: "
156*795d594fSAndroid Build Coastguard Worker             << GetResolvedMethodErrorString(
157*795d594fSAndroid Build Coastguard Worker                    class_linker, inlined_method, method, outer_method, dex_cache, method_info);
158*795d594fSAndroid Build Coastguard Worker         const OatDexFile* odf = oat_dex_files[dex_file_index];
159*795d594fSAndroid Build Coastguard Worker         DCHECK_NE(odf, nullptr);
160*795d594fSAndroid Build Coastguard Worker         dex_cache = class_linker->FindDexCache(Thread::Current(), *odf);
161*795d594fSAndroid Build Coastguard Worker       }
162*795d594fSAndroid Build Coastguard Worker     } else {
163*795d594fSAndroid Build Coastguard Worker       dex_cache = outer_method->GetDexCache();
164*795d594fSAndroid Build Coastguard Worker     }
165*795d594fSAndroid Build Coastguard Worker     inlined_method =
166*795d594fSAndroid Build Coastguard Worker         class_linker->LookupResolvedMethod(method_index, dex_cache, dex_cache->GetClassLoader());
167*795d594fSAndroid Build Coastguard Worker 
168*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(inlined_method == nullptr)) {
169*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << GetResolvedMethodErrorString(
170*795d594fSAndroid Build Coastguard Worker           class_linker, inlined_method, method, outer_method, dex_cache, method_info);
171*795d594fSAndroid Build Coastguard Worker       UNREACHABLE();
172*795d594fSAndroid Build Coastguard Worker     }
173*795d594fSAndroid Build Coastguard Worker     DCHECK(!inlined_method->IsRuntimeMethod());
174*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(inlined_method->GetDexFile() == outer_method->GetDexFile(),
175*795d594fSAndroid Build Coastguard Worker               dex_file_index == MethodInfo::kSameDexFile)
176*795d594fSAndroid Build Coastguard Worker         << GetResolvedMethodErrorString(
177*795d594fSAndroid Build Coastguard Worker                class_linker, inlined_method, method, outer_method, dex_cache, method_info);
178*795d594fSAndroid Build Coastguard Worker     method = inlined_method;
179*795d594fSAndroid Build Coastguard Worker   }
180*795d594fSAndroid Build Coastguard Worker 
181*795d594fSAndroid Build Coastguard Worker   return method;
182*795d594fSAndroid Build Coastguard Worker }
183*795d594fSAndroid Build Coastguard Worker 
184*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
CheckClassInitializedForObjectAlloc(ObjPtr<mirror::Class> klass,Thread * self,bool * slow_path)185*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Class> CheckClassInitializedForObjectAlloc(ObjPtr<mirror::Class> klass,
186*795d594fSAndroid Build Coastguard Worker                                                                  Thread* self,
187*795d594fSAndroid Build Coastguard Worker                                                                  bool* slow_path)
188*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_)
189*795d594fSAndroid Build Coastguard Worker     REQUIRES(!Roles::uninterruptible_) {
190*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(!klass->IsVisiblyInitialized())) {
191*795d594fSAndroid Build Coastguard Worker     StackHandleScope<1> hs(self);
192*795d594fSAndroid Build Coastguard Worker     Handle<mirror::Class> h_class(hs.NewHandle(klass));
193*795d594fSAndroid Build Coastguard Worker     // EnsureInitialized (the class initializer) might cause a GC.
194*795d594fSAndroid Build Coastguard Worker     // may cause us to suspend meaning that another thread may try to
195*795d594fSAndroid Build Coastguard Worker     // change the allocator while we are stuck in the entrypoints of
196*795d594fSAndroid Build Coastguard Worker     // an old allocator. Also, the class initialization may fail. To
197*795d594fSAndroid Build Coastguard Worker     // handle these cases we mark the slow path boolean as true so
198*795d594fSAndroid Build Coastguard Worker     // that the caller knows to check the allocator type to see if it
199*795d594fSAndroid Build Coastguard Worker     // has changed and to null-check the return value in case the
200*795d594fSAndroid Build Coastguard Worker     // initialization fails.
201*795d594fSAndroid Build Coastguard Worker     *slow_path = true;
202*795d594fSAndroid Build Coastguard Worker     if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
203*795d594fSAndroid Build Coastguard Worker       DCHECK(self->IsExceptionPending());
204*795d594fSAndroid Build Coastguard Worker       return nullptr;  // Failure
205*795d594fSAndroid Build Coastguard Worker     } else {
206*795d594fSAndroid Build Coastguard Worker       DCHECK(!self->IsExceptionPending());
207*795d594fSAndroid Build Coastguard Worker     }
208*795d594fSAndroid Build Coastguard Worker     return h_class.Get();
209*795d594fSAndroid Build Coastguard Worker   }
210*795d594fSAndroid Build Coastguard Worker   return klass;
211*795d594fSAndroid Build Coastguard Worker }
212*795d594fSAndroid Build Coastguard Worker 
CheckObjectAlloc(ObjPtr<mirror::Class> klass,Thread * self,bool * slow_path)213*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE inline ObjPtr<mirror::Class> CheckObjectAlloc(ObjPtr<mirror::Class> klass,
214*795d594fSAndroid Build Coastguard Worker                                                             Thread* self,
215*795d594fSAndroid Build Coastguard Worker                                                             bool* slow_path)
216*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_)
217*795d594fSAndroid Build Coastguard Worker     REQUIRES(!Roles::uninterruptible_) {
218*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(!klass->IsInstantiable())) {
219*795d594fSAndroid Build Coastguard Worker     self->ThrowNewException("Ljava/lang/InstantiationError;", klass->PrettyDescriptor().c_str());
220*795d594fSAndroid Build Coastguard Worker     *slow_path = true;
221*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure
222*795d594fSAndroid Build Coastguard Worker   }
223*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(klass->IsClassClass())) {
224*795d594fSAndroid Build Coastguard Worker     ThrowIllegalAccessError(nullptr, "Class %s is inaccessible",
225*795d594fSAndroid Build Coastguard Worker                             klass->PrettyDescriptor().c_str());
226*795d594fSAndroid Build Coastguard Worker     *slow_path = true;
227*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure
228*795d594fSAndroid Build Coastguard Worker   }
229*795d594fSAndroid Build Coastguard Worker   return CheckClassInitializedForObjectAlloc(klass, self, slow_path);
230*795d594fSAndroid Build Coastguard Worker }
231*795d594fSAndroid Build Coastguard Worker 
232*795d594fSAndroid Build Coastguard Worker // Allocate an instance of klass. Throws InstantationError if klass is not instantiable,
233*795d594fSAndroid Build Coastguard Worker // or IllegalAccessError if klass is j.l.Class. Performs a clinit check too.
234*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented>
235*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
AllocObjectFromCode(ObjPtr<mirror::Class> klass,Thread * self,gc::AllocatorType allocator_type)236*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Object> AllocObjectFromCode(ObjPtr<mirror::Class> klass,
237*795d594fSAndroid Build Coastguard Worker                                                   Thread* self,
238*795d594fSAndroid Build Coastguard Worker                                                   gc::AllocatorType allocator_type) {
239*795d594fSAndroid Build Coastguard Worker   bool slow_path = false;
240*795d594fSAndroid Build Coastguard Worker   klass = CheckObjectAlloc(klass, self, &slow_path);
241*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(slow_path)) {
242*795d594fSAndroid Build Coastguard Worker     if (klass == nullptr) {
243*795d594fSAndroid Build Coastguard Worker       return nullptr;
244*795d594fSAndroid Build Coastguard Worker     }
245*795d594fSAndroid Build Coastguard Worker     // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
246*795d594fSAndroid Build Coastguard Worker     return klass->Alloc</*kInstrumented=*/true>(
247*795d594fSAndroid Build Coastguard Worker         self,
248*795d594fSAndroid Build Coastguard Worker         Runtime::Current()->GetHeap()->GetCurrentAllocator());
249*795d594fSAndroid Build Coastguard Worker   }
250*795d594fSAndroid Build Coastguard Worker   DCHECK(klass != nullptr);
251*795d594fSAndroid Build Coastguard Worker   return klass->Alloc<kInstrumented>(self, allocator_type);
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker 
254*795d594fSAndroid Build Coastguard Worker // Given the context of a calling Method and a resolved class, create an instance.
255*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented>
256*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,Thread * self,gc::AllocatorType allocator_type)257*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Object> AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass,
258*795d594fSAndroid Build Coastguard Worker                                                           Thread* self,
259*795d594fSAndroid Build Coastguard Worker                                                           gc::AllocatorType allocator_type) {
260*795d594fSAndroid Build Coastguard Worker   DCHECK(klass != nullptr);
261*795d594fSAndroid Build Coastguard Worker   bool slow_path = false;
262*795d594fSAndroid Build Coastguard Worker   klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
263*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(slow_path)) {
264*795d594fSAndroid Build Coastguard Worker     if (klass == nullptr) {
265*795d594fSAndroid Build Coastguard Worker       return nullptr;
266*795d594fSAndroid Build Coastguard Worker     }
267*795d594fSAndroid Build Coastguard Worker     gc::Heap* heap = Runtime::Current()->GetHeap();
268*795d594fSAndroid Build Coastguard Worker     // Pass in kNoAddFinalizer since the object cannot be finalizable.
269*795d594fSAndroid Build Coastguard Worker     // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
270*795d594fSAndroid Build Coastguard Worker     // instrumented.
271*795d594fSAndroid Build Coastguard Worker     return klass->Alloc</*kInstrumented=*/true, mirror::Class::AddFinalizer::kNoAddFinalizer>(
272*795d594fSAndroid Build Coastguard Worker         self, heap->GetCurrentAllocator());
273*795d594fSAndroid Build Coastguard Worker   }
274*795d594fSAndroid Build Coastguard Worker   // Pass in kNoAddFinalizer since the object cannot be finalizable.
275*795d594fSAndroid Build Coastguard Worker   return klass->Alloc<kInstrumented,
276*795d594fSAndroid Build Coastguard Worker                       mirror::Class::AddFinalizer::kNoAddFinalizer>(self, allocator_type);
277*795d594fSAndroid Build Coastguard Worker }
278*795d594fSAndroid Build Coastguard Worker 
279*795d594fSAndroid Build Coastguard Worker // Given the context of a calling Method and an initialized class, create an instance.
280*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented>
281*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,Thread * self,gc::AllocatorType allocator_type)282*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Object> AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass,
283*795d594fSAndroid Build Coastguard Worker                                                              Thread* self,
284*795d594fSAndroid Build Coastguard Worker                                                              gc::AllocatorType allocator_type) {
285*795d594fSAndroid Build Coastguard Worker   DCHECK(klass != nullptr);
286*795d594fSAndroid Build Coastguard Worker   // Pass in kNoAddFinalizer since the object cannot be finalizable.
287*795d594fSAndroid Build Coastguard Worker   return klass->Alloc<kInstrumented,
288*795d594fSAndroid Build Coastguard Worker                       mirror::Class::AddFinalizer::kNoAddFinalizer>(self, allocator_type);
289*795d594fSAndroid Build Coastguard Worker }
290*795d594fSAndroid Build Coastguard Worker 
291*795d594fSAndroid Build Coastguard Worker 
292*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
CheckArrayAlloc(dex::TypeIndex type_idx,int32_t component_count,ArtMethod * method,bool * slow_path)293*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Class> CheckArrayAlloc(dex::TypeIndex type_idx,
294*795d594fSAndroid Build Coastguard Worker                                              int32_t component_count,
295*795d594fSAndroid Build Coastguard Worker                                              ArtMethod* method,
296*795d594fSAndroid Build Coastguard Worker                                              bool* slow_path) {
297*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(component_count < 0)) {
298*795d594fSAndroid Build Coastguard Worker     ThrowNegativeArraySizeException(component_count);
299*795d594fSAndroid Build Coastguard Worker     *slow_path = true;
300*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure
301*795d594fSAndroid Build Coastguard Worker   }
302*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> klass = method->GetDexCache()->GetResolvedType(type_idx);
303*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(klass == nullptr)) {  // Not in dex cache so try to resolve
304*795d594fSAndroid Build Coastguard Worker     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
305*795d594fSAndroid Build Coastguard Worker     klass = class_linker->ResolveType(type_idx, method);
306*795d594fSAndroid Build Coastguard Worker     *slow_path = true;
307*795d594fSAndroid Build Coastguard Worker     if (klass == nullptr) {  // Error
308*795d594fSAndroid Build Coastguard Worker       DCHECK(Thread::Current()->IsExceptionPending());
309*795d594fSAndroid Build Coastguard Worker       return nullptr;  // Failure
310*795d594fSAndroid Build Coastguard Worker     }
311*795d594fSAndroid Build Coastguard Worker     CHECK(klass->IsArrayClass()) << klass->PrettyClass();
312*795d594fSAndroid Build Coastguard Worker   }
313*795d594fSAndroid Build Coastguard Worker   if (!method->SkipAccessChecks()) {
314*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Class> referrer = method->GetDeclaringClass();
315*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(!referrer->CanAccess(klass))) {
316*795d594fSAndroid Build Coastguard Worker       ThrowIllegalAccessErrorClass(referrer, klass);
317*795d594fSAndroid Build Coastguard Worker       *slow_path = true;
318*795d594fSAndroid Build Coastguard Worker       return nullptr;  // Failure
319*795d594fSAndroid Build Coastguard Worker     }
320*795d594fSAndroid Build Coastguard Worker   }
321*795d594fSAndroid Build Coastguard Worker   return klass;
322*795d594fSAndroid Build Coastguard Worker }
323*795d594fSAndroid Build Coastguard Worker 
324*795d594fSAndroid Build Coastguard Worker // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
325*795d594fSAndroid Build Coastguard Worker // it cannot be resolved, throw an error. If it can, use it to create an array.
326*795d594fSAndroid Build Coastguard Worker // When verification/compiler hasn't been able to verify access, optionally perform an access
327*795d594fSAndroid Build Coastguard Worker // check.
328*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented>
329*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
AllocArrayFromCode(dex::TypeIndex type_idx,int32_t component_count,ArtMethod * method,Thread * self,gc::AllocatorType allocator_type)330*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
331*795d594fSAndroid Build Coastguard Worker                                                 int32_t component_count,
332*795d594fSAndroid Build Coastguard Worker                                                 ArtMethod* method,
333*795d594fSAndroid Build Coastguard Worker                                                 Thread* self,
334*795d594fSAndroid Build Coastguard Worker                                                 gc::AllocatorType allocator_type) {
335*795d594fSAndroid Build Coastguard Worker   bool slow_path = false;
336*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> klass = CheckArrayAlloc(type_idx, component_count, method, &slow_path);
337*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(slow_path)) {
338*795d594fSAndroid Build Coastguard Worker     if (klass == nullptr) {
339*795d594fSAndroid Build Coastguard Worker       return nullptr;
340*795d594fSAndroid Build Coastguard Worker     }
341*795d594fSAndroid Build Coastguard Worker     gc::Heap* heap = Runtime::Current()->GetHeap();
342*795d594fSAndroid Build Coastguard Worker     // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
343*795d594fSAndroid Build Coastguard Worker     return mirror::Array::Alloc</*kInstrumented=*/true>(self,
344*795d594fSAndroid Build Coastguard Worker                                                         klass,
345*795d594fSAndroid Build Coastguard Worker                                                         component_count,
346*795d594fSAndroid Build Coastguard Worker                                                         klass->GetComponentSizeShift(),
347*795d594fSAndroid Build Coastguard Worker                                                         heap->GetCurrentAllocator());
348*795d594fSAndroid Build Coastguard Worker   }
349*795d594fSAndroid Build Coastguard Worker   return mirror::Array::Alloc<kInstrumented>(self,
350*795d594fSAndroid Build Coastguard Worker                                              klass,
351*795d594fSAndroid Build Coastguard Worker                                              component_count,
352*795d594fSAndroid Build Coastguard Worker                                              klass->GetComponentSizeShift(),
353*795d594fSAndroid Build Coastguard Worker                                              allocator_type);
354*795d594fSAndroid Build Coastguard Worker }
355*795d594fSAndroid Build Coastguard Worker 
356*795d594fSAndroid Build Coastguard Worker template <bool kInstrumented>
357*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE
AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,int32_t component_count,Thread * self,gc::AllocatorType allocator_type)358*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Array> AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass,
359*795d594fSAndroid Build Coastguard Worker                                                         int32_t component_count,
360*795d594fSAndroid Build Coastguard Worker                                                         Thread* self,
361*795d594fSAndroid Build Coastguard Worker                                                         gc::AllocatorType allocator_type) {
362*795d594fSAndroid Build Coastguard Worker   DCHECK(klass != nullptr);
363*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(component_count < 0)) {
364*795d594fSAndroid Build Coastguard Worker     ThrowNegativeArraySizeException(component_count);
365*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure
366*795d594fSAndroid Build Coastguard Worker   }
367*795d594fSAndroid Build Coastguard Worker   // No need to retry a slow-path allocation as the above code won't cause a GC or thread
368*795d594fSAndroid Build Coastguard Worker   // suspension.
369*795d594fSAndroid Build Coastguard Worker   return mirror::Array::Alloc<kInstrumented>(self,
370*795d594fSAndroid Build Coastguard Worker                                              klass,
371*795d594fSAndroid Build Coastguard Worker                                              component_count,
372*795d594fSAndroid Build Coastguard Worker                                              klass->GetComponentSizeShift(),
373*795d594fSAndroid Build Coastguard Worker                                              allocator_type);
374*795d594fSAndroid Build Coastguard Worker }
375*795d594fSAndroid Build Coastguard Worker 
376*795d594fSAndroid Build Coastguard Worker FLATTEN
ResolveFieldWithAccessChecks(Thread * self,ClassLinker * class_linker,uint16_t field_index,ArtMethod * caller,bool is_static,bool is_put,size_t resolve_field_type)377*795d594fSAndroid Build Coastguard Worker inline ArtField* ResolveFieldWithAccessChecks(Thread* self,
378*795d594fSAndroid Build Coastguard Worker                                               ClassLinker* class_linker,
379*795d594fSAndroid Build Coastguard Worker                                               uint16_t field_index,
380*795d594fSAndroid Build Coastguard Worker                                               ArtMethod* caller,
381*795d594fSAndroid Build Coastguard Worker                                               bool is_static,
382*795d594fSAndroid Build Coastguard Worker                                               bool is_put,
383*795d594fSAndroid Build Coastguard Worker                                               size_t resolve_field_type)  // Resolve if not zero
384*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
385*795d594fSAndroid Build Coastguard Worker   if (caller->SkipAccessChecks()) {
386*795d594fSAndroid Build Coastguard Worker     return class_linker->ResolveField(field_index, caller, is_static);
387*795d594fSAndroid Build Coastguard Worker   }
388*795d594fSAndroid Build Coastguard Worker 
389*795d594fSAndroid Build Coastguard Worker   caller = caller->GetInterfaceMethodIfProxy(class_linker->GetImagePointerSize());
390*795d594fSAndroid Build Coastguard Worker   ArtField* resolved_field = caller->GetDexCache()->GetResolvedField(field_index);
391*795d594fSAndroid Build Coastguard Worker   if (resolved_field == nullptr) {
392*795d594fSAndroid Build Coastguard Worker     StackHandleScope<2> hs(self);
393*795d594fSAndroid Build Coastguard Worker     Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(caller->GetDexCache()));
394*795d594fSAndroid Build Coastguard Worker     Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(caller->GetClassLoader()));
395*795d594fSAndroid Build Coastguard Worker     resolved_field = class_linker->ResolveFieldJLS(field_index, h_dex_cache, h_class_loader);
396*795d594fSAndroid Build Coastguard Worker     if (resolved_field == nullptr) {
397*795d594fSAndroid Build Coastguard Worker       return nullptr;
398*795d594fSAndroid Build Coastguard Worker     }
399*795d594fSAndroid Build Coastguard Worker   }
400*795d594fSAndroid Build Coastguard Worker 
401*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
402*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
403*795d594fSAndroid Build Coastguard Worker     ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, caller);
404*795d594fSAndroid Build Coastguard Worker     return nullptr;
405*795d594fSAndroid Build Coastguard Worker   }
406*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> referring_class = caller->GetDeclaringClass();
407*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class,
408*795d594fSAndroid Build Coastguard Worker                                                           resolved_field,
409*795d594fSAndroid Build Coastguard Worker                                                           caller->GetDexCache(),
410*795d594fSAndroid Build Coastguard Worker                                                           field_index))) {
411*795d594fSAndroid Build Coastguard Worker     DCHECK(self->IsExceptionPending());
412*795d594fSAndroid Build Coastguard Worker     return nullptr;
413*795d594fSAndroid Build Coastguard Worker   }
414*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_put && !resolved_field->CanBeChangedBy(caller))) {
415*795d594fSAndroid Build Coastguard Worker     ThrowIllegalAccessErrorFinalField(caller, resolved_field);
416*795d594fSAndroid Build Coastguard Worker     return nullptr;
417*795d594fSAndroid Build Coastguard Worker   }
418*795d594fSAndroid Build Coastguard Worker 
419*795d594fSAndroid Build Coastguard Worker   if (resolve_field_type != 0u) {
420*795d594fSAndroid Build Coastguard Worker     StackArtFieldHandleScope<1> rhs(self);
421*795d594fSAndroid Build Coastguard Worker     ReflectiveHandle<ArtField> field_handle(rhs.NewHandle(resolved_field));
422*795d594fSAndroid Build Coastguard Worker     if (resolved_field->ResolveType().IsNull()) {
423*795d594fSAndroid Build Coastguard Worker       DCHECK(self->IsExceptionPending());
424*795d594fSAndroid Build Coastguard Worker       return nullptr;
425*795d594fSAndroid Build Coastguard Worker     }
426*795d594fSAndroid Build Coastguard Worker     resolved_field = field_handle.Get();
427*795d594fSAndroid Build Coastguard Worker   }
428*795d594fSAndroid Build Coastguard Worker   return resolved_field;
429*795d594fSAndroid Build Coastguard Worker }
430*795d594fSAndroid Build Coastguard Worker 
431*795d594fSAndroid Build Coastguard Worker template<FindFieldType type>
432*795d594fSAndroid Build Coastguard Worker inline ArtField* FindFieldFromCode(uint32_t field_idx,
433*795d594fSAndroid Build Coastguard Worker                                    ArtMethod* referrer,
434*795d594fSAndroid Build Coastguard Worker                                    Thread* self,
435*795d594fSAndroid Build Coastguard Worker                                    bool should_resolve_type = false) {
436*795d594fSAndroid Build Coastguard Worker   constexpr bool is_set = (type & FindFieldFlags::WriteBit) != 0;
437*795d594fSAndroid Build Coastguard Worker   constexpr bool is_static = (type & FindFieldFlags::StaticBit) != 0;
438*795d594fSAndroid Build Coastguard Worker   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
439*795d594fSAndroid Build Coastguard Worker   ArtField* resolved_field = ResolveFieldWithAccessChecks(
440*795d594fSAndroid Build Coastguard Worker       self, class_linker, field_idx, referrer, is_static, is_set, should_resolve_type ? 1u : 0u);
441*795d594fSAndroid Build Coastguard Worker   if (!is_static || resolved_field == nullptr) {
442*795d594fSAndroid Build Coastguard Worker     // instance fields must be being accessed on an initialized class
443*795d594fSAndroid Build Coastguard Worker     return resolved_field;
444*795d594fSAndroid Build Coastguard Worker   } else {
445*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Class> fields_class = resolved_field->GetDeclaringClass();
446*795d594fSAndroid Build Coastguard Worker     // If the class is initialized we're done.
447*795d594fSAndroid Build Coastguard Worker     if (LIKELY(fields_class->IsVisiblyInitialized())) {
448*795d594fSAndroid Build Coastguard Worker       return resolved_field;
449*795d594fSAndroid Build Coastguard Worker     } else {
450*795d594fSAndroid Build Coastguard Worker       StackHandleScope<1> hs(self);
451*795d594fSAndroid Build Coastguard Worker       StackArtFieldHandleScope<1> rhs(self);
452*795d594fSAndroid Build Coastguard Worker       ReflectiveHandle<ArtField> resolved_field_handle(rhs.NewHandle(resolved_field));
453*795d594fSAndroid Build Coastguard Worker       if (LIKELY(class_linker->EnsureInitialized(self, hs.NewHandle(fields_class), true, true))) {
454*795d594fSAndroid Build Coastguard Worker         // Otherwise let's ensure the class is initialized before resolving the field.
455*795d594fSAndroid Build Coastguard Worker         return resolved_field_handle.Get();
456*795d594fSAndroid Build Coastguard Worker       }
457*795d594fSAndroid Build Coastguard Worker       DCHECK(self->IsExceptionPending());  // Throw exception and unwind
458*795d594fSAndroid Build Coastguard Worker       return nullptr;  // Failure.
459*795d594fSAndroid Build Coastguard Worker     }
460*795d594fSAndroid Build Coastguard Worker   }
461*795d594fSAndroid Build Coastguard Worker }
462*795d594fSAndroid Build Coastguard Worker 
463*795d594fSAndroid Build Coastguard Worker // NOLINTBEGIN(bugprone-macro-parentheses)
464*795d594fSAndroid Build Coastguard Worker // Explicit template declarations of FindFieldFromCode for all field access types.
465*795d594fSAndroid Build Coastguard Worker #define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type) \
466*795d594fSAndroid Build Coastguard Worker template REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE \
467*795d594fSAndroid Build Coastguard Worker ArtField* FindFieldFromCode<_type>(uint32_t field_idx, \
468*795d594fSAndroid Build Coastguard Worker                                    ArtMethod* referrer, \
469*795d594fSAndroid Build Coastguard Worker                                    Thread* self, \
470*795d594fSAndroid Build Coastguard Worker                                    bool should_resolve_type = false) \
471*795d594fSAndroid Build Coastguard Worker 
472*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstanceObjectRead);
473*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstanceObjectWrite);
474*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstancePrimitiveRead);
475*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(InstancePrimitiveWrite);
476*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticObjectRead);
477*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticObjectWrite);
478*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticPrimitiveRead);
479*795d594fSAndroid Build Coastguard Worker EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(StaticPrimitiveWrite);
480*795d594fSAndroid Build Coastguard Worker 
481*795d594fSAndroid Build Coastguard Worker #undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
482*795d594fSAndroid Build Coastguard Worker // NOLINTEND(bugprone-macro-parentheses)
483*795d594fSAndroid Build Coastguard Worker 
IsStringInit(const DexFile * dex_file,uint32_t method_idx)484*795d594fSAndroid Build Coastguard Worker static inline bool IsStringInit(const DexFile* dex_file, uint32_t method_idx)
485*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
486*795d594fSAndroid Build Coastguard Worker   const dex::MethodId& method_id = dex_file->GetMethodId(method_idx);
487*795d594fSAndroid Build Coastguard Worker   const std::string_view class_name = dex_file->GetTypeDescriptorView(method_id.class_idx_);
488*795d594fSAndroid Build Coastguard Worker   const std::string_view method_name = dex_file->GetMethodNameView(method_id);
489*795d594fSAndroid Build Coastguard Worker   // Instead of calling ResolveMethod() which has suspend point and can trigger
490*795d594fSAndroid Build Coastguard Worker   // GC, look up the method symbolically.
491*795d594fSAndroid Build Coastguard Worker   // Compare method's class name and method name against string init.
492*795d594fSAndroid Build Coastguard Worker   // It's ok since it's not allowed to create your own java/lang/String.
493*795d594fSAndroid Build Coastguard Worker   // TODO: verify that assumption.
494*795d594fSAndroid Build Coastguard Worker   if (class_name == "Ljava/lang/String;" && method_name == "<init>") {
495*795d594fSAndroid Build Coastguard Worker     return true;
496*795d594fSAndroid Build Coastguard Worker   }
497*795d594fSAndroid Build Coastguard Worker   return false;
498*795d594fSAndroid Build Coastguard Worker }
499*795d594fSAndroid Build Coastguard Worker 
IsStringInit(const Instruction & instr,ArtMethod * caller)500*795d594fSAndroid Build Coastguard Worker static inline bool IsStringInit(const Instruction& instr, ArtMethod* caller)
501*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
502*795d594fSAndroid Build Coastguard Worker   if (instr.Opcode() == Instruction::INVOKE_DIRECT ||
503*795d594fSAndroid Build Coastguard Worker       instr.Opcode() == Instruction::INVOKE_DIRECT_RANGE) {
504*795d594fSAndroid Build Coastguard Worker     uint16_t callee_method_idx = (instr.Opcode() == Instruction::INVOKE_DIRECT_RANGE) ?
505*795d594fSAndroid Build Coastguard Worker         instr.VRegB_3rc() : instr.VRegB_35c();
506*795d594fSAndroid Build Coastguard Worker     return IsStringInit(caller->GetDexFile(), callee_method_idx);
507*795d594fSAndroid Build Coastguard Worker   }
508*795d594fSAndroid Build Coastguard Worker   return false;
509*795d594fSAndroid Build Coastguard Worker }
510*795d594fSAndroid Build Coastguard Worker 
511*795d594fSAndroid Build Coastguard Worker LIBART_PROTECTED
512*795d594fSAndroid Build Coastguard Worker extern "C" size_t NterpGetMethod(Thread* self, ArtMethod* caller, const uint16_t* dex_pc_ptr);
513*795d594fSAndroid Build Coastguard Worker 
514*795d594fSAndroid Build Coastguard Worker template <InvokeType type>
FindMethodToCall(Thread * self,ArtMethod * caller,ObjPtr<mirror::Object> * this_object,const Instruction & inst,bool only_lookup_tls_cache,bool * string_init)515*795d594fSAndroid Build Coastguard Worker ArtMethod* FindMethodToCall(Thread* self,
516*795d594fSAndroid Build Coastguard Worker                             ArtMethod* caller,
517*795d594fSAndroid Build Coastguard Worker                             ObjPtr<mirror::Object>* this_object,
518*795d594fSAndroid Build Coastguard Worker                             const Instruction& inst,
519*795d594fSAndroid Build Coastguard Worker                             bool only_lookup_tls_cache,
520*795d594fSAndroid Build Coastguard Worker                             /*out*/ bool* string_init)
521*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
522*795d594fSAndroid Build Coastguard Worker   PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
523*795d594fSAndroid Build Coastguard Worker 
524*795d594fSAndroid Build Coastguard Worker   // Try to find the method in thread-local cache.
525*795d594fSAndroid Build Coastguard Worker   size_t tls_value = 0u;
526*795d594fSAndroid Build Coastguard Worker   if (!self->GetInterpreterCache()->Get(self, &inst, &tls_value)) {
527*795d594fSAndroid Build Coastguard Worker     if (only_lookup_tls_cache) {
528*795d594fSAndroid Build Coastguard Worker       return nullptr;
529*795d594fSAndroid Build Coastguard Worker     }
530*795d594fSAndroid Build Coastguard Worker     DCHECK(!self->IsExceptionPending());
531*795d594fSAndroid Build Coastguard Worker     // NterpGetMethod can suspend, so save this_object.
532*795d594fSAndroid Build Coastguard Worker     StackHandleScope<1> hs(self);
533*795d594fSAndroid Build Coastguard Worker     HandleWrapperObjPtr<mirror::Object> h_this(hs.NewHandleWrapper(this_object));
534*795d594fSAndroid Build Coastguard Worker     tls_value = NterpGetMethod(self, caller, reinterpret_cast<const uint16_t*>(&inst));
535*795d594fSAndroid Build Coastguard Worker     if (self->IsExceptionPending()) {
536*795d594fSAndroid Build Coastguard Worker       return nullptr;
537*795d594fSAndroid Build Coastguard Worker     }
538*795d594fSAndroid Build Coastguard Worker   }
539*795d594fSAndroid Build Coastguard Worker 
540*795d594fSAndroid Build Coastguard Worker   if (type != kStatic && UNLIKELY((*this_object) == nullptr)) {
541*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(IsStringInit(inst, caller))) {
542*795d594fSAndroid Build Coastguard Worker       // Hack for String init:
543*795d594fSAndroid Build Coastguard Worker       //
544*795d594fSAndroid Build Coastguard Worker       // We assume that the input of String.<init> in verified code is always
545*795d594fSAndroid Build Coastguard Worker       // an uninitialized reference. If it is a null constant, it must have been
546*795d594fSAndroid Build Coastguard Worker       // optimized out by the compiler and we arrive here after deoptimization.
547*795d594fSAndroid Build Coastguard Worker       // Do not throw NullPointerException.
548*795d594fSAndroid Build Coastguard Worker     } else {
549*795d594fSAndroid Build Coastguard Worker       // Maintain interpreter-like semantics where NullPointerException is thrown
550*795d594fSAndroid Build Coastguard Worker       // after potential NoSuchMethodError from class linker.
551*795d594fSAndroid Build Coastguard Worker       const uint32_t method_idx = inst.VRegB();
552*795d594fSAndroid Build Coastguard Worker       ThrowNullPointerExceptionForMethodAccess(method_idx, type);
553*795d594fSAndroid Build Coastguard Worker       return nullptr;
554*795d594fSAndroid Build Coastguard Worker     }
555*795d594fSAndroid Build Coastguard Worker   }
556*795d594fSAndroid Build Coastguard Worker 
557*795d594fSAndroid Build Coastguard Worker   static constexpr size_t kStringInitMethodFlag = 0b1;
558*795d594fSAndroid Build Coastguard Worker   static constexpr size_t kInvokeInterfaceOnObjectMethodFlag = 0b1;
559*795d594fSAndroid Build Coastguard Worker   static constexpr size_t kMethodMask = ~0b11;
560*795d594fSAndroid Build Coastguard Worker 
561*795d594fSAndroid Build Coastguard Worker   ArtMethod* called_method = nullptr;
562*795d594fSAndroid Build Coastguard Worker   switch (type) {
563*795d594fSAndroid Build Coastguard Worker     case kDirect:
564*795d594fSAndroid Build Coastguard Worker     case kSuper:
565*795d594fSAndroid Build Coastguard Worker     case kStatic:
566*795d594fSAndroid Build Coastguard Worker       // Note: for the interpreter, the String.<init> special casing for invocation is handled
567*795d594fSAndroid Build Coastguard Worker       // in DoCallCommon.
568*795d594fSAndroid Build Coastguard Worker       *string_init = ((tls_value & kStringInitMethodFlag) != 0);
569*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(*string_init, IsStringInit(inst, caller));
570*795d594fSAndroid Build Coastguard Worker       called_method = reinterpret_cast<ArtMethod*>(tls_value & kMethodMask);
571*795d594fSAndroid Build Coastguard Worker       break;
572*795d594fSAndroid Build Coastguard Worker     case kInterface:
573*795d594fSAndroid Build Coastguard Worker       if ((tls_value & kInvokeInterfaceOnObjectMethodFlag) != 0) {
574*795d594fSAndroid Build Coastguard Worker         // invokeinterface on a j.l.Object method.
575*795d594fSAndroid Build Coastguard Worker         uint16_t method_index = tls_value >> 16;
576*795d594fSAndroid Build Coastguard Worker         called_method = (*this_object)->GetClass()->GetVTableEntry(method_index, pointer_size);
577*795d594fSAndroid Build Coastguard Worker       } else {
578*795d594fSAndroid Build Coastguard Worker         ArtMethod* interface_method = reinterpret_cast<ArtMethod*>(tls_value & kMethodMask);
579*795d594fSAndroid Build Coastguard Worker         called_method = (*this_object)->GetClass()->GetImt(pointer_size)->Get(
580*795d594fSAndroid Build Coastguard Worker             interface_method->GetImtIndex(), pointer_size);
581*795d594fSAndroid Build Coastguard Worker         if (called_method->IsRuntimeMethod()) {
582*795d594fSAndroid Build Coastguard Worker           called_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
583*795d594fSAndroid Build Coastguard Worker               interface_method, pointer_size);
584*795d594fSAndroid Build Coastguard Worker           if (UNLIKELY(called_method == nullptr)) {
585*795d594fSAndroid Build Coastguard Worker             ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
586*795d594fSAndroid Build Coastguard Worker                 interface_method, *this_object, caller);
587*795d594fSAndroid Build Coastguard Worker             return nullptr;
588*795d594fSAndroid Build Coastguard Worker           }
589*795d594fSAndroid Build Coastguard Worker         }
590*795d594fSAndroid Build Coastguard Worker       }
591*795d594fSAndroid Build Coastguard Worker       break;
592*795d594fSAndroid Build Coastguard Worker     case kVirtual:
593*795d594fSAndroid Build Coastguard Worker       called_method = (*this_object)->GetClass()->GetVTableEntry(tls_value, pointer_size);
594*795d594fSAndroid Build Coastguard Worker       break;
595*795d594fSAndroid Build Coastguard Worker   }
596*795d594fSAndroid Build Coastguard Worker 
597*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(!called_method->IsInvokable())) {
598*795d594fSAndroid Build Coastguard Worker     called_method->ThrowInvocationTimeError((type == kStatic) ? nullptr : *this_object);
599*795d594fSAndroid Build Coastguard Worker     return nullptr;
600*795d594fSAndroid Build Coastguard Worker   }
601*795d594fSAndroid Build Coastguard Worker   DCHECK(!called_method->IsRuntimeMethod()) << called_method->PrettyMethod();
602*795d594fSAndroid Build Coastguard Worker   return called_method;
603*795d594fSAndroid Build Coastguard Worker }
604*795d594fSAndroid Build Coastguard Worker 
605*795d594fSAndroid Build Coastguard Worker template<bool access_check>
FindSuperMethodToCall(uint32_t method_idx,ArtMethod * resolved_method,ArtMethod * referrer,Thread * self)606*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE ArtMethod* FindSuperMethodToCall(uint32_t method_idx,
607*795d594fSAndroid Build Coastguard Worker                                               ArtMethod* resolved_method,
608*795d594fSAndroid Build Coastguard Worker                                               ArtMethod* referrer,
609*795d594fSAndroid Build Coastguard Worker                                               Thread* self)
610*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
611*795d594fSAndroid Build Coastguard Worker   // TODO This lookup is quite slow.
612*795d594fSAndroid Build Coastguard Worker   // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
613*795d594fSAndroid Build Coastguard Worker   //    that will actually not be what we want in some cases where there are miranda methods or
614*795d594fSAndroid Build Coastguard Worker   //    defaults. What we actually need is a GetContainingClass that says which classes virtuals
615*795d594fSAndroid Build Coastguard Worker   //    this method is coming from.
616*795d594fSAndroid Build Coastguard Worker   ClassLinker* linker = Runtime::Current()->GetClassLinker();
617*795d594fSAndroid Build Coastguard Worker   dex::TypeIndex type_idx = referrer->GetDexFile()->GetMethodId(method_idx).class_idx_;
618*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> referenced_class = linker->ResolveType(type_idx, referrer);
619*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(referenced_class == nullptr)) {
620*795d594fSAndroid Build Coastguard Worker     DCHECK(self->IsExceptionPending());
621*795d594fSAndroid Build Coastguard Worker     return nullptr;
622*795d594fSAndroid Build Coastguard Worker   }
623*795d594fSAndroid Build Coastguard Worker 
624*795d594fSAndroid Build Coastguard Worker   if (access_check) {
625*795d594fSAndroid Build Coastguard Worker     if (!referenced_class->IsAssignableFrom(referrer->GetDeclaringClass())) {
626*795d594fSAndroid Build Coastguard Worker       ThrowNoSuchMethodError(kSuper,
627*795d594fSAndroid Build Coastguard Worker                              resolved_method->GetDeclaringClass(),
628*795d594fSAndroid Build Coastguard Worker                              resolved_method->GetName(),
629*795d594fSAndroid Build Coastguard Worker                              resolved_method->GetSignature());
630*795d594fSAndroid Build Coastguard Worker       return nullptr;
631*795d594fSAndroid Build Coastguard Worker     }
632*795d594fSAndroid Build Coastguard Worker   }
633*795d594fSAndroid Build Coastguard Worker 
634*795d594fSAndroid Build Coastguard Worker   if (referenced_class->IsInterface()) {
635*795d594fSAndroid Build Coastguard Worker     // TODO We can do better than this for a (compiled) fastpath.
636*795d594fSAndroid Build Coastguard Worker     ArtMethod* found_method = referenced_class->FindVirtualMethodForInterfaceSuper(
637*795d594fSAndroid Build Coastguard Worker         resolved_method, linker->GetImagePointerSize());
638*795d594fSAndroid Build Coastguard Worker     DCHECK(found_method != nullptr);
639*795d594fSAndroid Build Coastguard Worker     return found_method;
640*795d594fSAndroid Build Coastguard Worker   }
641*795d594fSAndroid Build Coastguard Worker 
642*795d594fSAndroid Build Coastguard Worker   DCHECK(resolved_method->IsCopied() ||
643*795d594fSAndroid Build Coastguard Worker          !resolved_method->GetDeclaringClass()->IsInterface());
644*795d594fSAndroid Build Coastguard Worker 
645*795d594fSAndroid Build Coastguard Worker   uint16_t vtable_index = resolved_method->GetMethodIndex();
646*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> super_class = referrer->GetDeclaringClass()->GetSuperClass();
647*795d594fSAndroid Build Coastguard Worker   if (access_check) {
648*795d594fSAndroid Build Coastguard Worker     DCHECK(super_class == nullptr || super_class->HasVTable());
649*795d594fSAndroid Build Coastguard Worker     // Check existence of super class.
650*795d594fSAndroid Build Coastguard Worker     if (super_class == nullptr ||
651*795d594fSAndroid Build Coastguard Worker         vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
652*795d594fSAndroid Build Coastguard Worker       // Behavior to agree with that of the verifier.
653*795d594fSAndroid Build Coastguard Worker       ThrowNoSuchMethodError(kSuper,
654*795d594fSAndroid Build Coastguard Worker                              resolved_method->GetDeclaringClass(),
655*795d594fSAndroid Build Coastguard Worker                              resolved_method->GetName(),
656*795d594fSAndroid Build Coastguard Worker                              resolved_method->GetSignature());
657*795d594fSAndroid Build Coastguard Worker       return nullptr;  // Failure.
658*795d594fSAndroid Build Coastguard Worker     }
659*795d594fSAndroid Build Coastguard Worker   }
660*795d594fSAndroid Build Coastguard Worker   DCHECK(super_class != nullptr);
661*795d594fSAndroid Build Coastguard Worker   DCHECK(super_class->HasVTable());
662*795d594fSAndroid Build Coastguard Worker   return super_class->GetVTableEntry(vtable_index, linker->GetImagePointerSize());
663*795d594fSAndroid Build Coastguard Worker }
664*795d594fSAndroid Build Coastguard Worker 
ResolveVerifyAndClinit(dex::TypeIndex type_idx,ArtMethod * referrer,Thread * self,bool can_run_clinit,bool verify_access)665*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
666*795d594fSAndroid Build Coastguard Worker                                                     ArtMethod* referrer,
667*795d594fSAndroid Build Coastguard Worker                                                     Thread* self,
668*795d594fSAndroid Build Coastguard Worker                                                     bool can_run_clinit,
669*795d594fSAndroid Build Coastguard Worker                                                     bool verify_access) {
670*795d594fSAndroid Build Coastguard Worker   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
671*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> klass = class_linker->ResolveType(type_idx, referrer);
672*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(klass == nullptr)) {
673*795d594fSAndroid Build Coastguard Worker     CHECK(self->IsExceptionPending());
674*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure - Indicate to caller to deliver exception
675*795d594fSAndroid Build Coastguard Worker   }
676*795d594fSAndroid Build Coastguard Worker   // Perform access check if necessary.
677*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass();
678*795d594fSAndroid Build Coastguard Worker   if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
679*795d594fSAndroid Build Coastguard Worker     ThrowIllegalAccessErrorClass(referring_class, klass);
680*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure - Indicate to caller to deliver exception
681*795d594fSAndroid Build Coastguard Worker   }
682*795d594fSAndroid Build Coastguard Worker   // If we're just implementing const-class, we shouldn't call <clinit>.
683*795d594fSAndroid Build Coastguard Worker   if (!can_run_clinit) {
684*795d594fSAndroid Build Coastguard Worker     return klass;
685*795d594fSAndroid Build Coastguard Worker   }
686*795d594fSAndroid Build Coastguard Worker   // If we are the <clinit> of this class, just return our storage.
687*795d594fSAndroid Build Coastguard Worker   //
688*795d594fSAndroid Build Coastguard Worker   // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
689*795d594fSAndroid Build Coastguard Worker   // running.
690*795d594fSAndroid Build Coastguard Worker   if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
691*795d594fSAndroid Build Coastguard Worker     return klass;
692*795d594fSAndroid Build Coastguard Worker   }
693*795d594fSAndroid Build Coastguard Worker   StackHandleScope<1> hs(self);
694*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> h_class(hs.NewHandle(klass));
695*795d594fSAndroid Build Coastguard Worker   if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
696*795d594fSAndroid Build Coastguard Worker     CHECK(self->IsExceptionPending());
697*795d594fSAndroid Build Coastguard Worker     return nullptr;  // Failure - Indicate to caller to deliver exception
698*795d594fSAndroid Build Coastguard Worker   }
699*795d594fSAndroid Build Coastguard Worker   return h_class.Get();
700*795d594fSAndroid Build Coastguard Worker }
701*795d594fSAndroid Build Coastguard Worker 
702*795d594fSAndroid Build Coastguard Worker template <typename INT_TYPE, typename FLOAT_TYPE>
art_float_to_integral(FLOAT_TYPE f)703*795d594fSAndroid Build Coastguard Worker inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
704*795d594fSAndroid Build Coastguard Worker   const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
705*795d594fSAndroid Build Coastguard Worker   const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
706*795d594fSAndroid Build Coastguard Worker   const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
707*795d594fSAndroid Build Coastguard Worker   const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
708*795d594fSAndroid Build Coastguard Worker   if (LIKELY(f > kMinIntAsFloat)) {
709*795d594fSAndroid Build Coastguard Worker      if (LIKELY(f < kMaxIntAsFloat)) {
710*795d594fSAndroid Build Coastguard Worker        return static_cast<INT_TYPE>(f);
711*795d594fSAndroid Build Coastguard Worker      } else {
712*795d594fSAndroid Build Coastguard Worker        return kMaxInt;
713*795d594fSAndroid Build Coastguard Worker      }
714*795d594fSAndroid Build Coastguard Worker   } else {
715*795d594fSAndroid Build Coastguard Worker     return (f != f) ? 0 : kMinInt;  // f != f implies NaN
716*795d594fSAndroid Build Coastguard Worker   }
717*795d594fSAndroid Build Coastguard Worker }
718*795d594fSAndroid Build Coastguard Worker 
GetGenericJniSynchronizationObject(Thread * self,ArtMethod * called)719*795d594fSAndroid Build Coastguard Worker inline ObjPtr<mirror::Object> GetGenericJniSynchronizationObject(Thread* self, ArtMethod* called)
720*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
721*795d594fSAndroid Build Coastguard Worker   DCHECK(!called->IsCriticalNative());
722*795d594fSAndroid Build Coastguard Worker   DCHECK(!called->IsFastNative());
723*795d594fSAndroid Build Coastguard Worker   DCHECK(self->GetManagedStack()->GetTopQuickFrame() != nullptr);
724*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(*self->GetManagedStack()->GetTopQuickFrame(), called);
725*795d594fSAndroid Build Coastguard Worker   // We do not need read barriers here.
726*795d594fSAndroid Build Coastguard Worker   // On method entry, all reference arguments are to-space references and we mark the
727*795d594fSAndroid Build Coastguard Worker   // declaring class of a static native method if needed. When visiting thread roots at
728*795d594fSAndroid Build Coastguard Worker   // the start of a GC, we visit all these references to ensure they point to the to-space.
729*795d594fSAndroid Build Coastguard Worker   if (called->IsStatic()) {
730*795d594fSAndroid Build Coastguard Worker     // Static methods synchronize on the declaring class object.
731*795d594fSAndroid Build Coastguard Worker     return called->GetDeclaringClass<kWithoutReadBarrier>();
732*795d594fSAndroid Build Coastguard Worker   } else {
733*795d594fSAndroid Build Coastguard Worker     // Instance methods synchronize on the `this` object.
734*795d594fSAndroid Build Coastguard Worker     // The `this` reference is stored in the first out vreg in the caller's frame.
735*795d594fSAndroid Build Coastguard Worker     uint8_t* sp = reinterpret_cast<uint8_t*>(self->GetManagedStack()->GetTopQuickFrame());
736*795d594fSAndroid Build Coastguard Worker     size_t frame_size = RuntimeCalleeSaveFrame::GetFrameSize(CalleeSaveType::kSaveRefsAndArgs);
737*795d594fSAndroid Build Coastguard Worker     StackReference<mirror::Object>* this_ref = reinterpret_cast<StackReference<mirror::Object>*>(
738*795d594fSAndroid Build Coastguard Worker         sp + frame_size + static_cast<size_t>(kRuntimePointerSize));
739*795d594fSAndroid Build Coastguard Worker     return this_ref->AsMirrorPtr();
740*795d594fSAndroid Build Coastguard Worker   }
741*795d594fSAndroid Build Coastguard Worker }
742*795d594fSAndroid Build Coastguard Worker 
743*795d594fSAndroid Build Coastguard Worker }  // namespace art
744*795d594fSAndroid Build Coastguard Worker 
745*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
746