1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 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 #include "art_method.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <algorithm>
20*795d594fSAndroid Build Coastguard Worker #include <cstddef>
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include "arch/context.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/stl_util.h"
28*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "debugger.h"
31*795d594fSAndroid Build Coastguard Worker #include "dex/class_accessor-inl.h"
32*795d594fSAndroid Build Coastguard Worker #include "dex/descriptors_names.h"
33*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_exception_helpers.h"
35*795d594fSAndroid Build Coastguard Worker #include "dex/dex_instruction.h"
36*795d594fSAndroid Build Coastguard Worker #include "dex/signature-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "entrypoints/runtime_asm_entrypoints.h"
38*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/card_table-inl.h"
39*795d594fSAndroid Build Coastguard Worker #include "hidden_api.h"
40*795d594fSAndroid Build Coastguard Worker #include "interpreter/interpreter.h"
41*795d594fSAndroid Build Coastguard Worker #include "intrinsics_enum.h"
42*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
43*795d594fSAndroid Build Coastguard Worker #include "jit/jit_code_cache.h"
44*795d594fSAndroid Build Coastguard Worker #include "jit/profiling_info.h"
45*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
46*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
47*795d594fSAndroid Build Coastguard Worker #include "mirror/class_ext-inl.h"
48*795d594fSAndroid Build Coastguard Worker #include "mirror/executable.h"
49*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
50*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-inl.h"
51*795d594fSAndroid Build Coastguard Worker #include "mirror/string.h"
52*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file-inl.h"
53*795d594fSAndroid Build Coastguard Worker #include "runtime_callbacks.h"
54*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
55*795d594fSAndroid Build Coastguard Worker #include "vdex_file.h"
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
60*795d594fSAndroid Build Coastguard Worker
61*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
62*795d594fSAndroid Build Coastguard Worker const char*);
63*795d594fSAndroid Build Coastguard Worker extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
64*795d594fSAndroid Build Coastguard Worker const char*);
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker // Enforce that we have the right index for runtime methods.
67*795d594fSAndroid Build Coastguard Worker static_assert(ArtMethod::kRuntimeMethodDexMethodIndex == dex::kDexNoIndex,
68*795d594fSAndroid Build Coastguard Worker "Wrong runtime-method dex method index");
69*795d594fSAndroid Build Coastguard Worker
GetCanonicalMethod(PointerSize pointer_size)70*795d594fSAndroid Build Coastguard Worker ArtMethod* ArtMethod::GetCanonicalMethod(PointerSize pointer_size) {
71*795d594fSAndroid Build Coastguard Worker if (LIKELY(!IsCopied())) {
72*795d594fSAndroid Build Coastguard Worker return this;
73*795d594fSAndroid Build Coastguard Worker } else {
74*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
75*795d594fSAndroid Build Coastguard Worker DCHECK(declaring_class->IsInterface());
76*795d594fSAndroid Build Coastguard Worker ArtMethod* ret = declaring_class->FindInterfaceMethod(GetDexCache(),
77*795d594fSAndroid Build Coastguard Worker GetDexMethodIndex(),
78*795d594fSAndroid Build Coastguard Worker pointer_size);
79*795d594fSAndroid Build Coastguard Worker DCHECK(ret != nullptr);
80*795d594fSAndroid Build Coastguard Worker return ret;
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker
GetNonObsoleteMethod()84*795d594fSAndroid Build Coastguard Worker ArtMethod* ArtMethod::GetNonObsoleteMethod() {
85*795d594fSAndroid Build Coastguard Worker if (LIKELY(!IsObsolete())) {
86*795d594fSAndroid Build Coastguard Worker return this;
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
89*795d594fSAndroid Build Coastguard Worker if (IsDirect()) {
90*795d594fSAndroid Build Coastguard Worker return &GetDeclaringClass()->GetDirectMethodsSlice(kRuntimePointerSize)[GetMethodIndex()];
91*795d594fSAndroid Build Coastguard Worker } else {
92*795d594fSAndroid Build Coastguard Worker return GetDeclaringClass()->GetVTableEntry(GetMethodIndex(), kRuntimePointerSize);
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker }
95*795d594fSAndroid Build Coastguard Worker
GetSingleImplementation(PointerSize pointer_size)96*795d594fSAndroid Build Coastguard Worker ArtMethod* ArtMethod::GetSingleImplementation(PointerSize pointer_size) {
97*795d594fSAndroid Build Coastguard Worker if (IsInvokable()) {
98*795d594fSAndroid Build Coastguard Worker // An invokable method single implementation is itself.
99*795d594fSAndroid Build Coastguard Worker return this;
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker DCHECK(!IsDefaultConflicting());
102*795d594fSAndroid Build Coastguard Worker ArtMethod* m = reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
103*795d594fSAndroid Build Coastguard Worker CHECK(m == nullptr || !m->IsDefaultConflicting());
104*795d594fSAndroid Build Coastguard Worker return m;
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable & soa,jobject jlr_method)107*795d594fSAndroid Build Coastguard Worker ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
108*795d594fSAndroid Build Coastguard Worker jobject jlr_method) {
109*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Executable> executable = soa.Decode<mirror::Executable>(jlr_method);
110*795d594fSAndroid Build Coastguard Worker DCHECK(executable != nullptr);
111*795d594fSAndroid Build Coastguard Worker return executable->GetArtMethod();
112*795d594fSAndroid Build Coastguard Worker }
113*795d594fSAndroid Build Coastguard Worker
114*795d594fSAndroid Build Coastguard Worker template <ReadBarrierOption kReadBarrierOption>
GetObsoleteDexCache()115*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::DexCache> ArtMethod::GetObsoleteDexCache() {
116*795d594fSAndroid Build Coastguard Worker // Note: The class redefinition happens with GC disabled, so at the point where we
117*795d594fSAndroid Build Coastguard Worker // create obsolete methods, the `ClassExt` and its obsolete methods and dex caches
118*795d594fSAndroid Build Coastguard Worker // members are reachable without a read barrier. If we start a GC later, and we
119*795d594fSAndroid Build Coastguard Worker // look at these objects without read barriers (`kWithoutReadBarrier`), the method
120*795d594fSAndroid Build Coastguard Worker // pointers shall be the same in from-space array as in to-space array (if these
121*795d594fSAndroid Build Coastguard Worker // arrays are different) and the dex cache array entry can point to from-space or
122*795d594fSAndroid Build Coastguard Worker // to-space `DexCache` but either is a valid result for `kWithoutReadBarrier`.
123*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants(__FUNCTION__);
124*795d594fSAndroid Build Coastguard Worker std::optional<ScopedDebugDisallowReadBarriers> sddrb(std::nullopt);
125*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && kReadBarrierOption == kWithoutReadBarrier) {
126*795d594fSAndroid Build Coastguard Worker sddrb.emplace(Thread::Current());
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker PointerSize pointer_size = kRuntimePointerSize;
129*795d594fSAndroid Build Coastguard Worker DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
130*795d594fSAndroid Build Coastguard Worker DCHECK(IsObsolete());
131*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = GetDeclaringClass<kReadBarrierOption>();
132*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ClassExt> ext =
133*795d594fSAndroid Build Coastguard Worker declaring_class->GetExtData<kDefaultVerifyFlags, kReadBarrierOption>();
134*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::PointerArray> obsolete_methods(
135*795d594fSAndroid Build Coastguard Worker ext.IsNull() ? nullptr : ext->GetObsoleteMethods<kDefaultVerifyFlags, kReadBarrierOption>());
136*795d594fSAndroid Build Coastguard Worker int32_t len = 0;
137*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::DexCache>> obsolete_dex_caches = nullptr;
138*795d594fSAndroid Build Coastguard Worker if (!obsolete_methods.IsNull()) {
139*795d594fSAndroid Build Coastguard Worker len = obsolete_methods->GetLength();
140*795d594fSAndroid Build Coastguard Worker obsolete_dex_caches = ext->GetObsoleteDexCaches<kDefaultVerifyFlags, kReadBarrierOption>();
141*795d594fSAndroid Build Coastguard Worker // FIXME: `ClassExt::SetObsoleteArrays()` is not atomic, so one of the arrays we see here
142*795d594fSAndroid Build Coastguard Worker // could be extended for a new class redefinition while the other may be shorter.
143*795d594fSAndroid Build Coastguard Worker // Furthermore, there is no synchronization to ensure that copied contents of an old
144*795d594fSAndroid Build Coastguard Worker // obsolete array are visible to a thread reading the new array.
145*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(len, obsolete_dex_caches->GetLength())
146*795d594fSAndroid Build Coastguard Worker << " ext->GetObsoleteDexCaches()=" << obsolete_dex_caches;
147*795d594fSAndroid Build Coastguard Worker }
148*795d594fSAndroid Build Coastguard Worker // Using kRuntimePointerSize (instead of using the image's pointer size) is fine since images
149*795d594fSAndroid Build Coastguard Worker // should never have obsolete methods in them so they should always be the same.
150*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(pointer_size, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
151*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i < len; i++) {
152*795d594fSAndroid Build Coastguard Worker if (this == obsolete_methods->GetElementPtrSize<ArtMethod*>(i, pointer_size)) {
153*795d594fSAndroid Build Coastguard Worker return obsolete_dex_caches->GetWithoutChecks<kDefaultVerifyFlags, kReadBarrierOption>(i);
154*795d594fSAndroid Build Coastguard Worker }
155*795d594fSAndroid Build Coastguard Worker }
156*795d594fSAndroid Build Coastguard Worker CHECK(declaring_class->IsObsoleteObject())
157*795d594fSAndroid Build Coastguard Worker << "This non-structurally obsolete method does not appear in the obsolete map of its class: "
158*795d594fSAndroid Build Coastguard Worker << declaring_class->PrettyClass() << " Searched " << len << " caches.";
159*795d594fSAndroid Build Coastguard Worker CHECK_EQ(this,
160*795d594fSAndroid Build Coastguard Worker std::clamp(this,
161*795d594fSAndroid Build Coastguard Worker &(*declaring_class->GetMethods(pointer_size).begin()),
162*795d594fSAndroid Build Coastguard Worker &(*declaring_class->GetMethods(pointer_size).end())))
163*795d594fSAndroid Build Coastguard Worker << "class is marked as structurally obsolete method but not found in normal obsolete-map "
164*795d594fSAndroid Build Coastguard Worker << "despite not being the original method pointer for " << GetDeclaringClass()->PrettyClass();
165*795d594fSAndroid Build Coastguard Worker return declaring_class->template GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker
168*795d594fSAndroid Build Coastguard Worker template ObjPtr<mirror::DexCache> ArtMethod::GetObsoleteDexCache<kWithReadBarrier>();
169*795d594fSAndroid Build Coastguard Worker template ObjPtr<mirror::DexCache> ArtMethod::GetObsoleteDexCache<kWithoutReadBarrier>();
170*795d594fSAndroid Build Coastguard Worker
FindObsoleteDexClassDefIndex()171*795d594fSAndroid Build Coastguard Worker uint16_t ArtMethod::FindObsoleteDexClassDefIndex() {
172*795d594fSAndroid Build Coastguard Worker DCHECK(!Runtime::Current()->IsAotCompiler()) << PrettyMethod();
173*795d594fSAndroid Build Coastguard Worker DCHECK(IsObsolete());
174*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = GetDexFile();
175*795d594fSAndroid Build Coastguard Worker const dex::TypeIndex declaring_class_type = dex_file->GetMethodId(GetDexMethodIndex()).class_idx_;
176*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* class_def = dex_file->FindClassDef(declaring_class_type);
177*795d594fSAndroid Build Coastguard Worker CHECK(class_def != nullptr);
178*795d594fSAndroid Build Coastguard Worker return dex_file->GetIndexForClassDef(*class_def);
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker
ThrowInvocationTimeError(ObjPtr<mirror::Object> receiver)181*795d594fSAndroid Build Coastguard Worker void ArtMethod::ThrowInvocationTimeError(ObjPtr<mirror::Object> receiver) {
182*795d594fSAndroid Build Coastguard Worker DCHECK(!IsInvokable());
183*795d594fSAndroid Build Coastguard Worker if (IsDefaultConflicting()) {
184*795d594fSAndroid Build Coastguard Worker ThrowIncompatibleClassChangeErrorForMethodConflict(this);
185*795d594fSAndroid Build Coastguard Worker } else if (GetDeclaringClass()->IsInterface() && receiver != nullptr) {
186*795d594fSAndroid Build Coastguard Worker // If this was an interface call, check whether there is a method in the
187*795d594fSAndroid Build Coastguard Worker // superclass chain that isn't public. In this situation, we should throw an
188*795d594fSAndroid Build Coastguard Worker // IllegalAccessError.
189*795d594fSAndroid Build Coastguard Worker DCHECK(IsAbstract());
190*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> current = receiver->GetClass();
191*795d594fSAndroid Build Coastguard Worker std::string_view name = GetNameView();
192*795d594fSAndroid Build Coastguard Worker Signature signature = GetSignature();
193*795d594fSAndroid Build Coastguard Worker while (current != nullptr) {
194*795d594fSAndroid Build Coastguard Worker for (ArtMethod& method : current->GetDeclaredMethodsSlice(kRuntimePointerSize)) {
195*795d594fSAndroid Build Coastguard Worker ArtMethod* np_method = method.GetInterfaceMethodIfProxy(kRuntimePointerSize);
196*795d594fSAndroid Build Coastguard Worker if (!np_method->IsStatic() &&
197*795d594fSAndroid Build Coastguard Worker np_method->GetNameView() == name &&
198*795d594fSAndroid Build Coastguard Worker np_method->GetSignature() == signature) {
199*795d594fSAndroid Build Coastguard Worker if (!np_method->IsPublic()) {
200*795d594fSAndroid Build Coastguard Worker ThrowIllegalAccessErrorForImplementingMethod(receiver->GetClass(), np_method, this);
201*795d594fSAndroid Build Coastguard Worker return;
202*795d594fSAndroid Build Coastguard Worker } else if (np_method->IsAbstract()) {
203*795d594fSAndroid Build Coastguard Worker ThrowAbstractMethodError(this);
204*795d594fSAndroid Build Coastguard Worker return;
205*795d594fSAndroid Build Coastguard Worker }
206*795d594fSAndroid Build Coastguard Worker }
207*795d594fSAndroid Build Coastguard Worker }
208*795d594fSAndroid Build Coastguard Worker current = current->GetSuperClass();
209*795d594fSAndroid Build Coastguard Worker }
210*795d594fSAndroid Build Coastguard Worker ThrowAbstractMethodError(this);
211*795d594fSAndroid Build Coastguard Worker } else {
212*795d594fSAndroid Build Coastguard Worker DCHECK(IsAbstract());
213*795d594fSAndroid Build Coastguard Worker ThrowAbstractMethodError(this);
214*795d594fSAndroid Build Coastguard Worker }
215*795d594fSAndroid Build Coastguard Worker }
216*795d594fSAndroid Build Coastguard Worker
GetInvokeType()217*795d594fSAndroid Build Coastguard Worker InvokeType ArtMethod::GetInvokeType() {
218*795d594fSAndroid Build Coastguard Worker // TODO: kSuper?
219*795d594fSAndroid Build Coastguard Worker if (IsStatic()) {
220*795d594fSAndroid Build Coastguard Worker return kStatic;
221*795d594fSAndroid Build Coastguard Worker } else if (GetDeclaringClass()->IsInterface()) {
222*795d594fSAndroid Build Coastguard Worker return kInterface;
223*795d594fSAndroid Build Coastguard Worker } else if (IsDirect()) {
224*795d594fSAndroid Build Coastguard Worker return kDirect;
225*795d594fSAndroid Build Coastguard Worker } else if (IsSignaturePolymorphic()) {
226*795d594fSAndroid Build Coastguard Worker return kPolymorphic;
227*795d594fSAndroid Build Coastguard Worker } else {
228*795d594fSAndroid Build Coastguard Worker return kVirtual;
229*795d594fSAndroid Build Coastguard Worker }
230*795d594fSAndroid Build Coastguard Worker }
231*795d594fSAndroid Build Coastguard Worker
NumArgRegisters(std::string_view shorty)232*795d594fSAndroid Build Coastguard Worker size_t ArtMethod::NumArgRegisters(std::string_view shorty) {
233*795d594fSAndroid Build Coastguard Worker CHECK(!shorty.empty());
234*795d594fSAndroid Build Coastguard Worker uint32_t num_registers = 0;
235*795d594fSAndroid Build Coastguard Worker for (char c : shorty.substr(1u)) {
236*795d594fSAndroid Build Coastguard Worker if (c == 'D' || c == 'J') {
237*795d594fSAndroid Build Coastguard Worker num_registers += 2;
238*795d594fSAndroid Build Coastguard Worker } else {
239*795d594fSAndroid Build Coastguard Worker num_registers += 1;
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker return num_registers;
243*795d594fSAndroid Build Coastguard Worker }
244*795d594fSAndroid Build Coastguard Worker
HasSameNameAndSignature(ArtMethod * other)245*795d594fSAndroid Build Coastguard Worker bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) {
246*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants("HasSameNameAndSignature");
247*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = GetDexFile();
248*795d594fSAndroid Build Coastguard Worker const dex::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex());
249*795d594fSAndroid Build Coastguard Worker if (GetDexCache() == other->GetDexCache()) {
250*795d594fSAndroid Build Coastguard Worker const dex::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex());
251*795d594fSAndroid Build Coastguard Worker return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
252*795d594fSAndroid Build Coastguard Worker }
253*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file2 = other->GetDexFile();
254*795d594fSAndroid Build Coastguard Worker const dex::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex());
255*795d594fSAndroid Build Coastguard Worker if (!DexFile::StringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
256*795d594fSAndroid Build Coastguard Worker return false; // Name mismatch.
257*795d594fSAndroid Build Coastguard Worker }
258*795d594fSAndroid Build Coastguard Worker return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
259*795d594fSAndroid Build Coastguard Worker }
260*795d594fSAndroid Build Coastguard Worker
FindOverriddenMethod(PointerSize pointer_size)261*795d594fSAndroid Build Coastguard Worker ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) {
262*795d594fSAndroid Build Coastguard Worker if (IsStatic()) {
263*795d594fSAndroid Build Coastguard Worker return nullptr;
264*795d594fSAndroid Build Coastguard Worker }
265*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
266*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> super_class = declaring_class->GetSuperClass();
267*795d594fSAndroid Build Coastguard Worker uint16_t method_index = GetMethodIndex();
268*795d594fSAndroid Build Coastguard Worker ArtMethod* result = nullptr;
269*795d594fSAndroid Build Coastguard Worker // Did this method override a super class method? If so load the result from the super class'
270*795d594fSAndroid Build Coastguard Worker // vtable
271*795d594fSAndroid Build Coastguard Worker if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
272*795d594fSAndroid Build Coastguard Worker result = super_class->GetVTableEntry(method_index, pointer_size);
273*795d594fSAndroid Build Coastguard Worker } else {
274*795d594fSAndroid Build Coastguard Worker // Method didn't override superclass method so search interfaces
275*795d594fSAndroid Build Coastguard Worker if (IsProxyMethod()) {
276*795d594fSAndroid Build Coastguard Worker result = GetInterfaceMethodIfProxy(pointer_size);
277*795d594fSAndroid Build Coastguard Worker DCHECK(result != nullptr);
278*795d594fSAndroid Build Coastguard Worker } else {
279*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::IfTable> iftable = GetDeclaringClass()->GetIfTable();
280*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
281*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> interface = iftable->GetInterface(i);
282*795d594fSAndroid Build Coastguard Worker for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) {
283*795d594fSAndroid Build Coastguard Worker if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) {
284*795d594fSAndroid Build Coastguard Worker result = &interface_method;
285*795d594fSAndroid Build Coastguard Worker break;
286*795d594fSAndroid Build Coastguard Worker }
287*795d594fSAndroid Build Coastguard Worker }
288*795d594fSAndroid Build Coastguard Worker }
289*795d594fSAndroid Build Coastguard Worker }
290*795d594fSAndroid Build Coastguard Worker }
291*795d594fSAndroid Build Coastguard Worker DCHECK(result == nullptr ||
292*795d594fSAndroid Build Coastguard Worker GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature(
293*795d594fSAndroid Build Coastguard Worker result->GetInterfaceMethodIfProxy(pointer_size)));
294*795d594fSAndroid Build Coastguard Worker return result;
295*795d594fSAndroid Build Coastguard Worker }
296*795d594fSAndroid Build Coastguard Worker
FindDexMethodIndexInOtherDexFile(const DexFile & other_dexfile,uint32_t name_and_signature_idx)297*795d594fSAndroid Build Coastguard Worker uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
298*795d594fSAndroid Build Coastguard Worker uint32_t name_and_signature_idx) {
299*795d594fSAndroid Build Coastguard Worker const DexFile* dexfile = GetDexFile();
300*795d594fSAndroid Build Coastguard Worker const uint32_t dex_method_idx = GetDexMethodIndex();
301*795d594fSAndroid Build Coastguard Worker const dex::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
302*795d594fSAndroid Build Coastguard Worker const dex::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
303*795d594fSAndroid Build Coastguard Worker DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
304*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
305*795d594fSAndroid Build Coastguard Worker if (dexfile == &other_dexfile) {
306*795d594fSAndroid Build Coastguard Worker return dex_method_idx;
307*795d594fSAndroid Build Coastguard Worker }
308*795d594fSAndroid Build Coastguard Worker std::string_view mid_declaring_class_descriptor = dexfile->GetTypeDescriptorView(mid.class_idx_);
309*795d594fSAndroid Build Coastguard Worker const dex::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor);
310*795d594fSAndroid Build Coastguard Worker if (other_type_id != nullptr) {
311*795d594fSAndroid Build Coastguard Worker const dex::MethodId* other_mid = other_dexfile.FindMethodId(
312*795d594fSAndroid Build Coastguard Worker *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
313*795d594fSAndroid Build Coastguard Worker other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
314*795d594fSAndroid Build Coastguard Worker if (other_mid != nullptr) {
315*795d594fSAndroid Build Coastguard Worker return other_dexfile.GetIndexForMethodId(*other_mid);
316*795d594fSAndroid Build Coastguard Worker }
317*795d594fSAndroid Build Coastguard Worker }
318*795d594fSAndroid Build Coastguard Worker return dex::kDexNoIndex;
319*795d594fSAndroid Build Coastguard Worker }
320*795d594fSAndroid Build Coastguard Worker
FindCatchBlock(Handle<mirror::Class> exception_type,uint32_t dex_pc,bool * has_no_move_exception)321*795d594fSAndroid Build Coastguard Worker uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
322*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc, bool* has_no_move_exception) {
323*795d594fSAndroid Build Coastguard Worker // Set aside the exception while we resolve its type.
324*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
325*795d594fSAndroid Build Coastguard Worker StackHandleScope<1> hs(self);
326*795d594fSAndroid Build Coastguard Worker Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
327*795d594fSAndroid Build Coastguard Worker self->ClearException();
328*795d594fSAndroid Build Coastguard Worker // Default to handler not found.
329*795d594fSAndroid Build Coastguard Worker uint32_t found_dex_pc = dex::kDexNoIndex;
330*795d594fSAndroid Build Coastguard Worker // Iterate over the catch handlers associated with dex_pc.
331*795d594fSAndroid Build Coastguard Worker CodeItemDataAccessor accessor(DexInstructionData());
332*795d594fSAndroid Build Coastguard Worker for (CatchHandlerIterator it(accessor, dex_pc); it.HasNext(); it.Next()) {
333*795d594fSAndroid Build Coastguard Worker dex::TypeIndex iter_type_idx = it.GetHandlerTypeIndex();
334*795d594fSAndroid Build Coastguard Worker // Catch all case
335*795d594fSAndroid Build Coastguard Worker if (!iter_type_idx.IsValid()) {
336*795d594fSAndroid Build Coastguard Worker found_dex_pc = it.GetHandlerAddress();
337*795d594fSAndroid Build Coastguard Worker break;
338*795d594fSAndroid Build Coastguard Worker }
339*795d594fSAndroid Build Coastguard Worker // Does this catch exception type apply?
340*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> iter_exception_type = ResolveClassFromTypeIndex(iter_type_idx);
341*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(iter_exception_type == nullptr)) {
342*795d594fSAndroid Build Coastguard Worker // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
343*795d594fSAndroid Build Coastguard Worker // removed by a pro-guard like tool.
344*795d594fSAndroid Build Coastguard Worker // Note: this is not RI behavior. RI would have failed when loading the class.
345*795d594fSAndroid Build Coastguard Worker self->ClearException();
346*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Unresolved exception class when finding catch block: "
347*795d594fSAndroid Build Coastguard Worker << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
348*795d594fSAndroid Build Coastguard Worker } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
349*795d594fSAndroid Build Coastguard Worker found_dex_pc = it.GetHandlerAddress();
350*795d594fSAndroid Build Coastguard Worker break;
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker }
353*795d594fSAndroid Build Coastguard Worker if (found_dex_pc != dex::kDexNoIndex) {
354*795d594fSAndroid Build Coastguard Worker const Instruction& first_catch_instr = accessor.InstructionAt(found_dex_pc);
355*795d594fSAndroid Build Coastguard Worker *has_no_move_exception = (first_catch_instr.Opcode() != Instruction::MOVE_EXCEPTION);
356*795d594fSAndroid Build Coastguard Worker }
357*795d594fSAndroid Build Coastguard Worker // Put the exception back.
358*795d594fSAndroid Build Coastguard Worker if (exception != nullptr) {
359*795d594fSAndroid Build Coastguard Worker self->SetException(exception.Get());
360*795d594fSAndroid Build Coastguard Worker }
361*795d594fSAndroid Build Coastguard Worker return found_dex_pc;
362*795d594fSAndroid Build Coastguard Worker }
363*795d594fSAndroid Build Coastguard Worker
364*795d594fSAndroid Build Coastguard Worker NO_STACK_PROTECTOR
Invoke(Thread * self,uint32_t * args,uint32_t args_size,JValue * result,const char * shorty)365*795d594fSAndroid Build Coastguard Worker void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
366*795d594fSAndroid Build Coastguard Worker const char* shorty) {
367*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd<kNativeStackType>())) {
368*795d594fSAndroid Build Coastguard Worker ThrowStackOverflowError<kNativeStackType>(self);
369*795d594fSAndroid Build Coastguard Worker return;
370*795d594fSAndroid Build Coastguard Worker }
371*795d594fSAndroid Build Coastguard Worker
372*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
373*795d594fSAndroid Build Coastguard Worker self->AssertThreadSuspensionIsAllowable();
374*795d594fSAndroid Build Coastguard Worker CHECK_EQ(ThreadState::kRunnable, self->GetState());
375*795d594fSAndroid Build Coastguard Worker CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty);
376*795d594fSAndroid Build Coastguard Worker }
377*795d594fSAndroid Build Coastguard Worker
378*795d594fSAndroid Build Coastguard Worker // Push a transition back into managed code onto the linked list in thread.
379*795d594fSAndroid Build Coastguard Worker ManagedStack fragment;
380*795d594fSAndroid Build Coastguard Worker self->PushManagedStackFragment(&fragment);
381*795d594fSAndroid Build Coastguard Worker
382*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
383*795d594fSAndroid Build Coastguard Worker // Call the invoke stub, passing everything as arguments.
384*795d594fSAndroid Build Coastguard Worker // If the runtime is not yet started or it is required by the debugger, then perform the
385*795d594fSAndroid Build Coastguard Worker // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent
386*795d594fSAndroid Build Coastguard Worker // cycling around the various JIT/Interpreter methods that handle method invocation.
387*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(!runtime->IsStarted() ||
388*795d594fSAndroid Build Coastguard Worker (self->IsForceInterpreter() && !IsNative() && !IsProxyMethod() && IsInvokable()))) {
389*795d594fSAndroid Build Coastguard Worker if (IsStatic()) {
390*795d594fSAndroid Build Coastguard Worker art::interpreter::EnterInterpreterFromInvoke(
391*795d594fSAndroid Build Coastguard Worker self, this, nullptr, args, result, /*stay_in_interpreter=*/ true);
392*795d594fSAndroid Build Coastguard Worker } else {
393*795d594fSAndroid Build Coastguard Worker mirror::Object* receiver =
394*795d594fSAndroid Build Coastguard Worker reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
395*795d594fSAndroid Build Coastguard Worker art::interpreter::EnterInterpreterFromInvoke(
396*795d594fSAndroid Build Coastguard Worker self, this, receiver, args + 1, result, /*stay_in_interpreter=*/ true);
397*795d594fSAndroid Build Coastguard Worker }
398*795d594fSAndroid Build Coastguard Worker } else {
399*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
400*795d594fSAndroid Build Coastguard Worker
401*795d594fSAndroid Build Coastguard Worker constexpr bool kLogInvocationStartAndReturn = false;
402*795d594fSAndroid Build Coastguard Worker bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
403*795d594fSAndroid Build Coastguard Worker if (LIKELY(have_quick_code)) {
404*795d594fSAndroid Build Coastguard Worker if (kLogInvocationStartAndReturn) {
405*795d594fSAndroid Build Coastguard Worker LOG(INFO) << StringPrintf(
406*795d594fSAndroid Build Coastguard Worker "Invoking '%s' quick code=%p static=%d", PrettyMethod().c_str(),
407*795d594fSAndroid Build Coastguard Worker GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
408*795d594fSAndroid Build Coastguard Worker }
409*795d594fSAndroid Build Coastguard Worker
410*795d594fSAndroid Build Coastguard Worker // Ensure that we won't be accidentally calling quick compiled code when -Xint.
411*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
412*795d594fSAndroid Build Coastguard Worker CHECK(!runtime->UseJitCompilation());
413*795d594fSAndroid Build Coastguard Worker const void* oat_quick_code =
414*795d594fSAndroid Build Coastguard Worker (IsNative() || !IsInvokable() || IsProxyMethod() || IsObsolete())
415*795d594fSAndroid Build Coastguard Worker ? nullptr
416*795d594fSAndroid Build Coastguard Worker : GetOatMethodQuickCode(runtime->GetClassLinker()->GetImagePointerSize());
417*795d594fSAndroid Build Coastguard Worker CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
418*795d594fSAndroid Build Coastguard Worker << "Don't call compiled code when -Xint " << PrettyMethod();
419*795d594fSAndroid Build Coastguard Worker }
420*795d594fSAndroid Build Coastguard Worker
421*795d594fSAndroid Build Coastguard Worker if (!IsStatic()) {
422*795d594fSAndroid Build Coastguard Worker (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
423*795d594fSAndroid Build Coastguard Worker } else {
424*795d594fSAndroid Build Coastguard Worker (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
425*795d594fSAndroid Build Coastguard Worker }
426*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
427*795d594fSAndroid Build Coastguard Worker // Unusual case where we were running generated code and an
428*795d594fSAndroid Build Coastguard Worker // exception was thrown to force the activations to be removed from the
429*795d594fSAndroid Build Coastguard Worker // stack. Continue execution in the interpreter.
430*795d594fSAndroid Build Coastguard Worker self->DeoptimizeWithDeoptimizationException(result);
431*795d594fSAndroid Build Coastguard Worker }
432*795d594fSAndroid Build Coastguard Worker if (kLogInvocationStartAndReturn) {
433*795d594fSAndroid Build Coastguard Worker LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod().c_str(),
434*795d594fSAndroid Build Coastguard Worker GetEntryPointFromQuickCompiledCode());
435*795d594fSAndroid Build Coastguard Worker }
436*795d594fSAndroid Build Coastguard Worker } else {
437*795d594fSAndroid Build Coastguard Worker LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null";
438*795d594fSAndroid Build Coastguard Worker if (result != nullptr) {
439*795d594fSAndroid Build Coastguard Worker result->SetJ(0);
440*795d594fSAndroid Build Coastguard Worker }
441*795d594fSAndroid Build Coastguard Worker }
442*795d594fSAndroid Build Coastguard Worker }
443*795d594fSAndroid Build Coastguard Worker
444*795d594fSAndroid Build Coastguard Worker // Pop transition.
445*795d594fSAndroid Build Coastguard Worker self->PopManagedStackFragment(fragment);
446*795d594fSAndroid Build Coastguard Worker }
447*795d594fSAndroid Build Coastguard Worker
IsSignaturePolymorphic()448*795d594fSAndroid Build Coastguard Worker bool ArtMethod::IsSignaturePolymorphic() {
449*795d594fSAndroid Build Coastguard Worker // Methods with a polymorphic signature have constraints that they
450*795d594fSAndroid Build Coastguard Worker // are native and varargs and belong to either MethodHandle or VarHandle.
451*795d594fSAndroid Build Coastguard Worker if (!IsNative() || !IsVarargs()) {
452*795d594fSAndroid Build Coastguard Worker return false;
453*795d594fSAndroid Build Coastguard Worker }
454*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots =
455*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetClassLinker()->GetClassRoots();
456*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> cls = GetDeclaringClass();
457*795d594fSAndroid Build Coastguard Worker return (cls == GetClassRoot<mirror::MethodHandle>(class_roots) ||
458*795d594fSAndroid Build Coastguard Worker cls == GetClassRoot<mirror::VarHandle>(class_roots));
459*795d594fSAndroid Build Coastguard Worker }
460*795d594fSAndroid Build Coastguard Worker
GetOatMethodIndexFromMethodIndex(const DexFile & dex_file,uint16_t class_def_idx,uint32_t method_idx)461*795d594fSAndroid Build Coastguard Worker static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file,
462*795d594fSAndroid Build Coastguard Worker uint16_t class_def_idx,
463*795d594fSAndroid Build Coastguard Worker uint32_t method_idx) {
464*795d594fSAndroid Build Coastguard Worker ClassAccessor accessor(dex_file, class_def_idx);
465*795d594fSAndroid Build Coastguard Worker uint32_t class_def_method_index = 0u;
466*795d594fSAndroid Build Coastguard Worker for (const ClassAccessor::Method& method : accessor.GetMethods()) {
467*795d594fSAndroid Build Coastguard Worker if (method.GetIndex() == method_idx) {
468*795d594fSAndroid Build Coastguard Worker return class_def_method_index;
469*795d594fSAndroid Build Coastguard Worker }
470*795d594fSAndroid Build Coastguard Worker class_def_method_index++;
471*795d594fSAndroid Build Coastguard Worker }
472*795d594fSAndroid Build Coastguard Worker LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation();
473*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
474*795d594fSAndroid Build Coastguard Worker }
475*795d594fSAndroid Build Coastguard Worker
476*795d594fSAndroid Build Coastguard Worker // We use the method's DexFile and declaring class name to find the OatMethod for an obsolete
477*795d594fSAndroid Build Coastguard Worker // method. This is extremely slow but we need it if we want to be able to have obsolete native
478*795d594fSAndroid Build Coastguard Worker // methods since we need this to find the size of its stack frames.
479*795d594fSAndroid Build Coastguard Worker //
480*795d594fSAndroid Build Coastguard Worker // NB We could (potentially) do this differently and rely on the way the transformation is applied
481*795d594fSAndroid Build Coastguard Worker // in order to use the entrypoint to find this information. However, for debugging reasons (most
482*795d594fSAndroid Build Coastguard Worker // notably making sure that new invokes of obsolete methods fail) we choose to instead get the data
483*795d594fSAndroid Build Coastguard Worker // directly from the dex file.
FindOatMethodFromDexFileFor(ArtMethod * method,bool * found)484*795d594fSAndroid Build Coastguard Worker static const OatFile::OatMethod FindOatMethodFromDexFileFor(ArtMethod* method, bool* found)
485*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
486*795d594fSAndroid Build Coastguard Worker DCHECK(method->IsObsolete() && method->IsNative());
487*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = method->GetDexFile();
488*795d594fSAndroid Build Coastguard Worker
489*795d594fSAndroid Build Coastguard Worker // recreate the class_def_index from the descriptor.
490*795d594fSAndroid Build Coastguard Worker const dex::TypeId* declaring_class_type_id =
491*795d594fSAndroid Build Coastguard Worker dex_file->FindTypeId(method->GetDeclaringClassDescriptorView());
492*795d594fSAndroid Build Coastguard Worker CHECK(declaring_class_type_id != nullptr);
493*795d594fSAndroid Build Coastguard Worker dex::TypeIndex declaring_class_type_index = dex_file->GetIndexForTypeId(*declaring_class_type_id);
494*795d594fSAndroid Build Coastguard Worker const dex::ClassDef* declaring_class_type_def =
495*795d594fSAndroid Build Coastguard Worker dex_file->FindClassDef(declaring_class_type_index);
496*795d594fSAndroid Build Coastguard Worker CHECK(declaring_class_type_def != nullptr);
497*795d594fSAndroid Build Coastguard Worker uint16_t declaring_class_def_index = dex_file->GetIndexForClassDef(*declaring_class_type_def);
498*795d594fSAndroid Build Coastguard Worker
499*795d594fSAndroid Build Coastguard Worker size_t oat_method_index = GetOatMethodIndexFromMethodIndex(*dex_file,
500*795d594fSAndroid Build Coastguard Worker declaring_class_def_index,
501*795d594fSAndroid Build Coastguard Worker method->GetDexMethodIndex());
502*795d594fSAndroid Build Coastguard Worker
503*795d594fSAndroid Build Coastguard Worker OatFile::OatClass oat_class = OatFile::FindOatClass(*dex_file,
504*795d594fSAndroid Build Coastguard Worker declaring_class_def_index,
505*795d594fSAndroid Build Coastguard Worker found);
506*795d594fSAndroid Build Coastguard Worker if (!(*found)) {
507*795d594fSAndroid Build Coastguard Worker return OatFile::OatMethod::Invalid();
508*795d594fSAndroid Build Coastguard Worker }
509*795d594fSAndroid Build Coastguard Worker return oat_class.GetOatMethod(oat_method_index);
510*795d594fSAndroid Build Coastguard Worker }
511*795d594fSAndroid Build Coastguard Worker
FindOatMethodFor(ArtMethod * method,PointerSize pointer_size,bool * found)512*795d594fSAndroid Build Coastguard Worker static const OatFile::OatMethod FindOatMethodFor(ArtMethod* method,
513*795d594fSAndroid Build Coastguard Worker PointerSize pointer_size,
514*795d594fSAndroid Build Coastguard Worker bool* found)
515*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
516*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(method->IsObsolete())) {
517*795d594fSAndroid Build Coastguard Worker // We shouldn't be calling this with obsolete methods except for native obsolete methods for
518*795d594fSAndroid Build Coastguard Worker // which we need to use the oat method to figure out how large the quick frame is.
519*795d594fSAndroid Build Coastguard Worker DCHECK(method->IsNative()) << "We should only be finding the OatMethod of obsolete methods in "
520*795d594fSAndroid Build Coastguard Worker << "order to allow stack walking. Other obsolete methods should "
521*795d594fSAndroid Build Coastguard Worker << "never need to access this information.";
522*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(pointer_size, kRuntimePointerSize) << "Obsolete method in compiler!";
523*795d594fSAndroid Build Coastguard Worker return FindOatMethodFromDexFileFor(method, found);
524*795d594fSAndroid Build Coastguard Worker }
525*795d594fSAndroid Build Coastguard Worker // Although we overwrite the trampoline of non-static methods, we may get here via the resolution
526*795d594fSAndroid Build Coastguard Worker // method for direct methods (or virtual methods made direct).
527*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = method->GetDeclaringClass();
528*795d594fSAndroid Build Coastguard Worker size_t oat_method_index;
529*795d594fSAndroid Build Coastguard Worker if (method->IsStatic() || method->IsDirect()) {
530*795d594fSAndroid Build Coastguard Worker // Simple case where the oat method index was stashed at load time.
531*795d594fSAndroid Build Coastguard Worker oat_method_index = method->GetMethodIndex();
532*795d594fSAndroid Build Coastguard Worker } else {
533*795d594fSAndroid Build Coastguard Worker // Compute the oat_method_index by search for its position in the declared virtual methods.
534*795d594fSAndroid Build Coastguard Worker oat_method_index = declaring_class->NumDirectMethods();
535*795d594fSAndroid Build Coastguard Worker bool found_virtual = false;
536*795d594fSAndroid Build Coastguard Worker for (ArtMethod& art_method : declaring_class->GetVirtualMethods(pointer_size)) {
537*795d594fSAndroid Build Coastguard Worker // Check method index instead of identity in case of duplicate method definitions.
538*795d594fSAndroid Build Coastguard Worker if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) {
539*795d594fSAndroid Build Coastguard Worker found_virtual = true;
540*795d594fSAndroid Build Coastguard Worker break;
541*795d594fSAndroid Build Coastguard Worker }
542*795d594fSAndroid Build Coastguard Worker oat_method_index++;
543*795d594fSAndroid Build Coastguard Worker }
544*795d594fSAndroid Build Coastguard Worker CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
545*795d594fSAndroid Build Coastguard Worker << method->PrettyMethod();
546*795d594fSAndroid Build Coastguard Worker }
547*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(oat_method_index,
548*795d594fSAndroid Build Coastguard Worker GetOatMethodIndexFromMethodIndex(declaring_class->GetDexFile(),
549*795d594fSAndroid Build Coastguard Worker method->GetDeclaringClass()->GetDexClassDefIndex(),
550*795d594fSAndroid Build Coastguard Worker method->GetDexMethodIndex()));
551*795d594fSAndroid Build Coastguard Worker OatFile::OatClass oat_class = OatFile::FindOatClass(declaring_class->GetDexFile(),
552*795d594fSAndroid Build Coastguard Worker declaring_class->GetDexClassDefIndex(),
553*795d594fSAndroid Build Coastguard Worker found);
554*795d594fSAndroid Build Coastguard Worker if (!(*found)) {
555*795d594fSAndroid Build Coastguard Worker return OatFile::OatMethod::Invalid();
556*795d594fSAndroid Build Coastguard Worker }
557*795d594fSAndroid Build Coastguard Worker return oat_class.GetOatMethod(oat_method_index);
558*795d594fSAndroid Build Coastguard Worker }
559*795d594fSAndroid Build Coastguard Worker
EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params)560*795d594fSAndroid Build Coastguard Worker bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
561*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = GetDexFile();
562*795d594fSAndroid Build Coastguard Worker const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
563*795d594fSAndroid Build Coastguard Worker const auto& proto_id = dex_file->GetMethodPrototype(method_id);
564*795d594fSAndroid Build Coastguard Worker const dex::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
565*795d594fSAndroid Build Coastguard Worker auto count = proto_params != nullptr ? proto_params->Size() : 0u;
566*795d594fSAndroid Build Coastguard Worker auto param_len = params != nullptr ? params->GetLength() : 0u;
567*795d594fSAndroid Build Coastguard Worker if (param_len != count) {
568*795d594fSAndroid Build Coastguard Worker return false;
569*795d594fSAndroid Build Coastguard Worker }
570*795d594fSAndroid Build Coastguard Worker auto* cl = Runtime::Current()->GetClassLinker();
571*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < count; ++i) {
572*795d594fSAndroid Build Coastguard Worker dex::TypeIndex type_idx = proto_params->GetTypeItem(i).type_idx_;
573*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> type = cl->ResolveType(type_idx, this);
574*795d594fSAndroid Build Coastguard Worker if (type == nullptr) {
575*795d594fSAndroid Build Coastguard Worker Thread::Current()->AssertPendingException();
576*795d594fSAndroid Build Coastguard Worker return false;
577*795d594fSAndroid Build Coastguard Worker }
578*795d594fSAndroid Build Coastguard Worker if (type != params->GetWithoutChecks(i)) {
579*795d594fSAndroid Build Coastguard Worker return false;
580*795d594fSAndroid Build Coastguard Worker }
581*795d594fSAndroid Build Coastguard Worker }
582*795d594fSAndroid Build Coastguard Worker return true;
583*795d594fSAndroid Build Coastguard Worker }
584*795d594fSAndroid Build Coastguard Worker
GetOatQuickMethodHeader(uintptr_t pc)585*795d594fSAndroid Build Coastguard Worker const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
586*795d594fSAndroid Build Coastguard Worker if (IsRuntimeMethod()) {
587*795d594fSAndroid Build Coastguard Worker return nullptr;
588*795d594fSAndroid Build Coastguard Worker }
589*795d594fSAndroid Build Coastguard Worker
590*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
591*795d594fSAndroid Build Coastguard Worker const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
592*795d594fSAndroid Build Coastguard Worker CHECK(existing_entry_point != nullptr) << PrettyMethod() << "@" << this;
593*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = runtime->GetClassLinker();
594*795d594fSAndroid Build Coastguard Worker
595*795d594fSAndroid Build Coastguard Worker if (existing_entry_point == GetQuickProxyInvokeHandler()) {
596*795d594fSAndroid Build Coastguard Worker DCHECK(IsProxyMethod() && !IsConstructor());
597*795d594fSAndroid Build Coastguard Worker // The proxy entry point does not have any method header.
598*795d594fSAndroid Build Coastguard Worker return nullptr;
599*795d594fSAndroid Build Coastguard Worker }
600*795d594fSAndroid Build Coastguard Worker
601*795d594fSAndroid Build Coastguard Worker // We should not reach here with a pc of 0. pc can be 0 for downcalls when walking the stack.
602*795d594fSAndroid Build Coastguard Worker // For native methods this case is handled by the caller by checking the quick frame tag. See
603*795d594fSAndroid Build Coastguard Worker // StackVisitor::WalkStack for more details. For non-native methods pc can be 0 only for runtime
604*795d594fSAndroid Build Coastguard Worker // methods or proxy invoke handlers which are handled earlier.
605*795d594fSAndroid Build Coastguard Worker DCHECK_NE(pc, 0u) << "PC 0 for " << PrettyMethod();
606*795d594fSAndroid Build Coastguard Worker
607*795d594fSAndroid Build Coastguard Worker // Check whether the current entry point contains this pc. We need to manually
608*795d594fSAndroid Build Coastguard Worker // check some entrypoints in case they are trampolines in the oat file.
609*795d594fSAndroid Build Coastguard Worker if (!class_linker->IsQuickGenericJniStub(existing_entry_point) &&
610*795d594fSAndroid Build Coastguard Worker !class_linker->IsQuickResolutionStub(existing_entry_point) &&
611*795d594fSAndroid Build Coastguard Worker !class_linker->IsQuickToInterpreterBridge(existing_entry_point) &&
612*795d594fSAndroid Build Coastguard Worker !OatQuickMethodHeader::IsStub(
613*795d594fSAndroid Build Coastguard Worker reinterpret_cast<const uint8_t*>(existing_entry_point)).value_or(true)) {
614*795d594fSAndroid Build Coastguard Worker OatQuickMethodHeader* method_header =
615*795d594fSAndroid Build Coastguard Worker OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
616*795d594fSAndroid Build Coastguard Worker
617*795d594fSAndroid Build Coastguard Worker if (method_header->Contains(pc)) {
618*795d594fSAndroid Build Coastguard Worker return method_header;
619*795d594fSAndroid Build Coastguard Worker }
620*795d594fSAndroid Build Coastguard Worker }
621*795d594fSAndroid Build Coastguard Worker
622*795d594fSAndroid Build Coastguard Worker if (OatQuickMethodHeader::IsNterpPc(pc)) {
623*795d594fSAndroid Build Coastguard Worker return OatQuickMethodHeader::NterpMethodHeader;
624*795d594fSAndroid Build Coastguard Worker }
625*795d594fSAndroid Build Coastguard Worker
626*795d594fSAndroid Build Coastguard Worker // Check whether the pc is in the JIT code cache.
627*795d594fSAndroid Build Coastguard Worker jit::Jit* jit = runtime->GetJit();
628*795d594fSAndroid Build Coastguard Worker if (jit != nullptr) {
629*795d594fSAndroid Build Coastguard Worker jit::JitCodeCache* code_cache = jit->GetCodeCache();
630*795d594fSAndroid Build Coastguard Worker OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
631*795d594fSAndroid Build Coastguard Worker if (method_header != nullptr) {
632*795d594fSAndroid Build Coastguard Worker DCHECK(method_header->Contains(pc));
633*795d594fSAndroid Build Coastguard Worker return method_header;
634*795d594fSAndroid Build Coastguard Worker } else {
635*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && code_cache->ContainsPc(reinterpret_cast<const void*>(pc))) {
636*795d594fSAndroid Build Coastguard Worker code_cache->DumpAllCompiledMethods(LOG_STREAM(FATAL_WITHOUT_ABORT));
637*795d594fSAndroid Build Coastguard Worker LOG(FATAL)
638*795d594fSAndroid Build Coastguard Worker << PrettyMethod()
639*795d594fSAndroid Build Coastguard Worker << ", pc=" << std::hex << pc
640*795d594fSAndroid Build Coastguard Worker << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
641*795d594fSAndroid Build Coastguard Worker << ", copy=" << std::boolalpha << IsCopied()
642*795d594fSAndroid Build Coastguard Worker << ", proxy=" << std::boolalpha << IsProxyMethod()
643*795d594fSAndroid Build Coastguard Worker << ", is_native=" << std::boolalpha << IsNative();
644*795d594fSAndroid Build Coastguard Worker }
645*795d594fSAndroid Build Coastguard Worker }
646*795d594fSAndroid Build Coastguard Worker }
647*795d594fSAndroid Build Coastguard Worker
648*795d594fSAndroid Build Coastguard Worker // The code has to be in an oat file.
649*795d594fSAndroid Build Coastguard Worker bool found;
650*795d594fSAndroid Build Coastguard Worker OatFile::OatMethod oat_method =
651*795d594fSAndroid Build Coastguard Worker FindOatMethodFor(this, class_linker->GetImagePointerSize(), &found);
652*795d594fSAndroid Build Coastguard Worker if (!found) {
653*795d594fSAndroid Build Coastguard Worker if (!IsNative()) {
654*795d594fSAndroid Build Coastguard Worker PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
655*795d594fSAndroid Build Coastguard Worker MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), /* terse= */ true);
656*795d594fSAndroid Build Coastguard Worker LOG(FATAL)
657*795d594fSAndroid Build Coastguard Worker << PrettyMethod()
658*795d594fSAndroid Build Coastguard Worker << " pc=" << pc
659*795d594fSAndroid Build Coastguard Worker << ", entrypoint= " << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
660*795d594fSAndroid Build Coastguard Worker << ", jit= " << jit;
661*795d594fSAndroid Build Coastguard Worker }
662*795d594fSAndroid Build Coastguard Worker // We are running the GenericJNI stub. The entrypoint may point
663*795d594fSAndroid Build Coastguard Worker // to different entrypoints, to a JIT-compiled JNI stub, or to a shared boot
664*795d594fSAndroid Build Coastguard Worker // image stub.
665*795d594fSAndroid Build Coastguard Worker DCHECK(class_linker->IsQuickGenericJniStub(existing_entry_point) ||
666*795d594fSAndroid Build Coastguard Worker class_linker->IsQuickResolutionStub(existing_entry_point) ||
667*795d594fSAndroid Build Coastguard Worker (jit != nullptr && jit->GetCodeCache()->ContainsPc(existing_entry_point)) ||
668*795d594fSAndroid Build Coastguard Worker (class_linker->FindBootJniStub(this) != nullptr))
669*795d594fSAndroid Build Coastguard Worker << " method: " << PrettyMethod()
670*795d594fSAndroid Build Coastguard Worker << " entrypoint: " << existing_entry_point
671*795d594fSAndroid Build Coastguard Worker << " size: " << OatQuickMethodHeader::FromEntryPoint(existing_entry_point)->GetCodeSize()
672*795d594fSAndroid Build Coastguard Worker << " pc: " << reinterpret_cast<const void*>(pc);
673*795d594fSAndroid Build Coastguard Worker return nullptr;
674*795d594fSAndroid Build Coastguard Worker }
675*795d594fSAndroid Build Coastguard Worker const void* oat_entry_point = oat_method.GetQuickCode();
676*795d594fSAndroid Build Coastguard Worker if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
677*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && !IsNative()) {
678*795d594fSAndroid Build Coastguard Worker PrintFileToLog("/proc/self/maps", LogSeverity::FATAL_WITHOUT_ABORT);
679*795d594fSAndroid Build Coastguard Worker MemMap::DumpMaps(LOG_STREAM(FATAL_WITHOUT_ABORT), /* terse= */ true);
680*795d594fSAndroid Build Coastguard Worker LOG(FATAL)
681*795d594fSAndroid Build Coastguard Worker << PrettyMethod()
682*795d594fSAndroid Build Coastguard Worker << std::hex
683*795d594fSAndroid Build Coastguard Worker << " pc=" << pc
684*795d594fSAndroid Build Coastguard Worker << ", entrypoint= " << reinterpret_cast<uintptr_t>(existing_entry_point)
685*795d594fSAndroid Build Coastguard Worker << ", jit= " << jit
686*795d594fSAndroid Build Coastguard Worker << ", nterp_start= "
687*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<uintptr_t>(OatQuickMethodHeader::NterpImpl.data())
688*795d594fSAndroid Build Coastguard Worker << ", nterp_end= "
689*795d594fSAndroid Build Coastguard Worker << reinterpret_cast<uintptr_t>(
690*795d594fSAndroid Build Coastguard Worker OatQuickMethodHeader::NterpImpl.data() + OatQuickMethodHeader::NterpImpl.size());
691*795d594fSAndroid Build Coastguard Worker }
692*795d594fSAndroid Build Coastguard Worker return nullptr;
693*795d594fSAndroid Build Coastguard Worker }
694*795d594fSAndroid Build Coastguard Worker
695*795d594fSAndroid Build Coastguard Worker OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
696*795d594fSAndroid Build Coastguard Worker // We could have existing Oat code for native methods but we may not use it if the runtime is java
697*795d594fSAndroid Build Coastguard Worker // debuggable or when profiling boot class path. There is no easy way to check if the pc
698*795d594fSAndroid Build Coastguard Worker // corresponds to QuickGenericJniStub. Since we have eliminated all the other cases, if the pc
699*795d594fSAndroid Build Coastguard Worker // doesn't correspond to the AOT code then we must be running QuickGenericJniStub.
700*795d594fSAndroid Build Coastguard Worker if (IsNative() && !method_header->Contains(pc)) {
701*795d594fSAndroid Build Coastguard Worker DCHECK_NE(pc, 0u) << "PC 0 for " << PrettyMethod();
702*795d594fSAndroid Build Coastguard Worker return nullptr;
703*795d594fSAndroid Build Coastguard Worker }
704*795d594fSAndroid Build Coastguard Worker
705*795d594fSAndroid Build Coastguard Worker DCHECK(method_header->Contains(pc))
706*795d594fSAndroid Build Coastguard Worker << PrettyMethod()
707*795d594fSAndroid Build Coastguard Worker << " " << std::hex << pc << " " << oat_entry_point
708*795d594fSAndroid Build Coastguard Worker << " " << (uintptr_t)(method_header->GetCode() + method_header->GetCodeSize());
709*795d594fSAndroid Build Coastguard Worker return method_header;
710*795d594fSAndroid Build Coastguard Worker }
711*795d594fSAndroid Build Coastguard Worker
GetOatMethodQuickCode(PointerSize pointer_size)712*795d594fSAndroid Build Coastguard Worker const void* ArtMethod::GetOatMethodQuickCode(PointerSize pointer_size) {
713*795d594fSAndroid Build Coastguard Worker bool found;
714*795d594fSAndroid Build Coastguard Worker OatFile::OatMethod oat_method = FindOatMethodFor(this, pointer_size, &found);
715*795d594fSAndroid Build Coastguard Worker if (found) {
716*795d594fSAndroid Build Coastguard Worker return oat_method.GetQuickCode();
717*795d594fSAndroid Build Coastguard Worker }
718*795d594fSAndroid Build Coastguard Worker return nullptr;
719*795d594fSAndroid Build Coastguard Worker }
720*795d594fSAndroid Build Coastguard Worker
SetIntrinsic(Intrinsics intrinsic)721*795d594fSAndroid Build Coastguard Worker void ArtMethod::SetIntrinsic(Intrinsics intrinsic) {
722*795d594fSAndroid Build Coastguard Worker // Currently we only do intrinsics for static/final methods or methods of final
723*795d594fSAndroid Build Coastguard Worker // classes. We don't set kHasSingleImplementation for those methods.
724*795d594fSAndroid Build Coastguard Worker DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
725*795d594fSAndroid Build Coastguard Worker "Potential conflict with kAccSingleImplementation";
726*795d594fSAndroid Build Coastguard Worker static constexpr int kAccFlagsShift = CTZ(kAccIntrinsicBits);
727*795d594fSAndroid Build Coastguard Worker uint32_t intrinsic_u32 = enum_cast<uint32_t>(intrinsic);
728*795d594fSAndroid Build Coastguard Worker DCHECK_LE(intrinsic_u32, kAccIntrinsicBits >> kAccFlagsShift);
729*795d594fSAndroid Build Coastguard Worker uint32_t intrinsic_bits = intrinsic_u32 << kAccFlagsShift;
730*795d594fSAndroid Build Coastguard Worker uint32_t new_value = (GetAccessFlags() & ~kAccIntrinsicBits) | kAccIntrinsic | intrinsic_bits;
731*795d594fSAndroid Build Coastguard Worker
732*795d594fSAndroid Build Coastguard Worker // These flags shouldn't be overridden by setting the intrinsic.
733*795d594fSAndroid Build Coastguard Worker uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
734*795d594fSAndroid Build Coastguard Worker bool is_constructor = IsConstructor();
735*795d594fSAndroid Build Coastguard Worker bool is_synchronized = IsSynchronized();
736*795d594fSAndroid Build Coastguard Worker bool skip_access_checks = SkipAccessChecks();
737*795d594fSAndroid Build Coastguard Worker bool is_fast_native = IsFastNative();
738*795d594fSAndroid Build Coastguard Worker bool is_critical_native = IsCriticalNative();
739*795d594fSAndroid Build Coastguard Worker bool is_copied = IsCopied();
740*795d594fSAndroid Build Coastguard Worker bool is_miranda = IsMiranda();
741*795d594fSAndroid Build Coastguard Worker bool is_default = IsDefault();
742*795d594fSAndroid Build Coastguard Worker bool is_default_conflict = IsDefaultConflicting();
743*795d594fSAndroid Build Coastguard Worker bool is_compilable = IsCompilable();
744*795d594fSAndroid Build Coastguard Worker bool must_count_locks = MustCountLocks();
745*795d594fSAndroid Build Coastguard Worker
746*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
747*795d594fSAndroid Build Coastguard Worker // Recompute flags instead of getting them from the current access flags because
748*795d594fSAndroid Build Coastguard Worker // access flags may have been changed to deduplicate warning messages (b/129063331).
749*795d594fSAndroid Build Coastguard Worker // For host builds, the flags from the api list (i.e. hiddenapi::CreateRuntimeFlags) might not
750*795d594fSAndroid Build Coastguard Worker // have the right value.
751*795d594fSAndroid Build Coastguard Worker uint32_t hiddenapi_flags = hiddenapi::CreateRuntimeFlags(this);
752*795d594fSAndroid Build Coastguard Worker #endif
753*795d594fSAndroid Build Coastguard Worker
754*795d594fSAndroid Build Coastguard Worker SetAccessFlags(new_value);
755*795d594fSAndroid Build Coastguard Worker // Intrinsics are considered hot from the first call.
756*795d594fSAndroid Build Coastguard Worker SetHotCounter();
757*795d594fSAndroid Build Coastguard Worker
758*795d594fSAndroid Build Coastguard Worker // DCHECK that the flags weren't overridden.
759*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
760*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_constructor, IsConstructor());
761*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_synchronized, IsSynchronized());
762*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(skip_access_checks, SkipAccessChecks());
763*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_fast_native, IsFastNative());
764*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_critical_native, IsCriticalNative());
765*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_copied, IsCopied());
766*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_miranda, IsMiranda());
767*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_default, IsDefault());
768*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
769*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(is_compilable, IsCompilable());
770*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(must_count_locks, MustCountLocks());
771*795d594fSAndroid Build Coastguard Worker
772*795d594fSAndroid Build Coastguard Worker #ifdef ART_TARGET_ANDROID
773*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(hiddenapi_flags, hiddenapi::GetRuntimeFlags(this)) << PrettyMethod();
774*795d594fSAndroid Build Coastguard Worker #endif
775*795d594fSAndroid Build Coastguard Worker }
776*795d594fSAndroid Build Coastguard Worker
SetNotIntrinsic()777*795d594fSAndroid Build Coastguard Worker void ArtMethod::SetNotIntrinsic() {
778*795d594fSAndroid Build Coastguard Worker if (!IsIntrinsic()) {
779*795d594fSAndroid Build Coastguard Worker return;
780*795d594fSAndroid Build Coastguard Worker }
781*795d594fSAndroid Build Coastguard Worker
782*795d594fSAndroid Build Coastguard Worker // Read the existing hiddenapi flags.
783*795d594fSAndroid Build Coastguard Worker uint32_t hiddenapi_runtime_flags = hiddenapi::GetRuntimeFlags(this);
784*795d594fSAndroid Build Coastguard Worker
785*795d594fSAndroid Build Coastguard Worker // Clear intrinsic-related access flags.
786*795d594fSAndroid Build Coastguard Worker ClearAccessFlags(kAccIntrinsic | kAccIntrinsicBits);
787*795d594fSAndroid Build Coastguard Worker
788*795d594fSAndroid Build Coastguard Worker // Re-apply hidden API access flags now that the method is not an intrinsic.
789*795d594fSAndroid Build Coastguard Worker SetAccessFlags(GetAccessFlags() | hiddenapi_runtime_flags);
790*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(hiddenapi_runtime_flags, hiddenapi::GetRuntimeFlags(this));
791*795d594fSAndroid Build Coastguard Worker }
792*795d594fSAndroid Build Coastguard Worker
CopyFrom(ArtMethod * src,PointerSize image_pointer_size)793*795d594fSAndroid Build Coastguard Worker void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
794*795d594fSAndroid Build Coastguard Worker memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
795*795d594fSAndroid Build Coastguard Worker Size(image_pointer_size));
796*795d594fSAndroid Build Coastguard Worker declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
797*795d594fSAndroid Build Coastguard Worker
798*795d594fSAndroid Build Coastguard Worker // If the entry point of the method we are copying from is from JIT code, we just
799*795d594fSAndroid Build Coastguard Worker // put the entry point of the new method to interpreter or GenericJNI. We could set
800*795d594fSAndroid Build Coastguard Worker // the entry point to the JIT code, but this would require taking the JIT code cache
801*795d594fSAndroid Build Coastguard Worker // lock to notify it, which we do not want at this level.
802*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
803*795d594fSAndroid Build Coastguard Worker const void* entry_point = GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size);
804*795d594fSAndroid Build Coastguard Worker if (runtime->UseJitCompilation()) {
805*795d594fSAndroid Build Coastguard Worker if (runtime->GetJit()->GetCodeCache()->ContainsPc(entry_point)) {
806*795d594fSAndroid Build Coastguard Worker SetNativePointer(EntryPointFromQuickCompiledCodeOffset(image_pointer_size),
807*795d594fSAndroid Build Coastguard Worker src->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge(),
808*795d594fSAndroid Build Coastguard Worker image_pointer_size);
809*795d594fSAndroid Build Coastguard Worker }
810*795d594fSAndroid Build Coastguard Worker }
811*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
812*795d594fSAndroid Build Coastguard Worker if (interpreter::IsNterpSupported() && class_linker->IsNterpEntryPoint(entry_point)) {
813*795d594fSAndroid Build Coastguard Worker // If the entrypoint is nterp, it's too early to check if the new method
814*795d594fSAndroid Build Coastguard Worker // will support it. So for simplicity, use the interpreter bridge.
815*795d594fSAndroid Build Coastguard Worker SetNativePointer(EntryPointFromQuickCompiledCodeOffset(image_pointer_size),
816*795d594fSAndroid Build Coastguard Worker GetQuickToInterpreterBridge(),
817*795d594fSAndroid Build Coastguard Worker image_pointer_size);
818*795d594fSAndroid Build Coastguard Worker }
819*795d594fSAndroid Build Coastguard Worker
820*795d594fSAndroid Build Coastguard Worker // Clear the data pointer, it will be set if needed by the caller.
821*795d594fSAndroid Build Coastguard Worker if (!src->HasCodeItem() && !src->IsNative()) {
822*795d594fSAndroid Build Coastguard Worker SetDataPtrSize(nullptr, image_pointer_size);
823*795d594fSAndroid Build Coastguard Worker }
824*795d594fSAndroid Build Coastguard Worker // Clear hotness to let the JIT properly decide when to compile this method.
825*795d594fSAndroid Build Coastguard Worker ResetCounter(runtime->GetJITOptions()->GetWarmupThreshold());
826*795d594fSAndroid Build Coastguard Worker }
827*795d594fSAndroid Build Coastguard Worker
IsImagePointerSize(PointerSize pointer_size)828*795d594fSAndroid Build Coastguard Worker bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
829*795d594fSAndroid Build Coastguard Worker // Hijack this function to get access to PtrSizedFieldsOffset.
830*795d594fSAndroid Build Coastguard Worker //
831*795d594fSAndroid Build Coastguard Worker // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
832*795d594fSAndroid Build Coastguard Worker // 64-bit builds.
833*795d594fSAndroid Build Coastguard Worker static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
834*795d594fSAndroid Build Coastguard Worker static_assert(
835*795d594fSAndroid Build Coastguard Worker (sizeof(void*) != 4) ||
836*795d594fSAndroid Build Coastguard Worker (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
837*795d594fSAndroid Build Coastguard Worker "Unexpected 32-bit class layout.");
838*795d594fSAndroid Build Coastguard Worker static_assert(
839*795d594fSAndroid Build Coastguard Worker (sizeof(void*) != 8) ||
840*795d594fSAndroid Build Coastguard Worker (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
841*795d594fSAndroid Build Coastguard Worker "Unexpected 64-bit class layout.");
842*795d594fSAndroid Build Coastguard Worker
843*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
844*795d594fSAndroid Build Coastguard Worker if (runtime == nullptr) {
845*795d594fSAndroid Build Coastguard Worker return true;
846*795d594fSAndroid Build Coastguard Worker }
847*795d594fSAndroid Build Coastguard Worker return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
848*795d594fSAndroid Build Coastguard Worker }
849*795d594fSAndroid Build Coastguard Worker
PrettyMethod(ArtMethod * m,bool with_signature)850*795d594fSAndroid Build Coastguard Worker std::string ArtMethod::PrettyMethod(ArtMethod* m, bool with_signature) {
851*795d594fSAndroid Build Coastguard Worker if (m == nullptr) {
852*795d594fSAndroid Build Coastguard Worker return "null";
853*795d594fSAndroid Build Coastguard Worker }
854*795d594fSAndroid Build Coastguard Worker return m->PrettyMethod(with_signature);
855*795d594fSAndroid Build Coastguard Worker }
856*795d594fSAndroid Build Coastguard Worker
PrettyMethod(bool with_signature)857*795d594fSAndroid Build Coastguard Worker std::string ArtMethod::PrettyMethod(bool with_signature) {
858*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(IsRuntimeMethod())) {
859*795d594fSAndroid Build Coastguard Worker std::string result = "<runtime method>.";
860*795d594fSAndroid Build Coastguard Worker result += GetName();
861*795d594fSAndroid Build Coastguard Worker // Do not add "<no signature>" even if `with_signature` is true.
862*795d594fSAndroid Build Coastguard Worker return result;
863*795d594fSAndroid Build Coastguard Worker }
864*795d594fSAndroid Build Coastguard Worker ArtMethod* m =
865*795d594fSAndroid Build Coastguard Worker GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize());
866*795d594fSAndroid Build Coastguard Worker std::string res(m->GetDexFile()->PrettyMethod(m->GetDexMethodIndex(), with_signature));
867*795d594fSAndroid Build Coastguard Worker if (with_signature && m->IsObsolete()) {
868*795d594fSAndroid Build Coastguard Worker return "<OBSOLETE> " + res;
869*795d594fSAndroid Build Coastguard Worker } else {
870*795d594fSAndroid Build Coastguard Worker return res;
871*795d594fSAndroid Build Coastguard Worker }
872*795d594fSAndroid Build Coastguard Worker }
873*795d594fSAndroid Build Coastguard Worker
JniShortName()874*795d594fSAndroid Build Coastguard Worker std::string ArtMethod::JniShortName() {
875*795d594fSAndroid Build Coastguard Worker return GetJniShortName(GetDeclaringClassDescriptor(), GetName());
876*795d594fSAndroid Build Coastguard Worker }
877*795d594fSAndroid Build Coastguard Worker
JniLongName()878*795d594fSAndroid Build Coastguard Worker std::string ArtMethod::JniLongName() {
879*795d594fSAndroid Build Coastguard Worker std::string long_name;
880*795d594fSAndroid Build Coastguard Worker long_name += JniShortName();
881*795d594fSAndroid Build Coastguard Worker long_name += "__";
882*795d594fSAndroid Build Coastguard Worker
883*795d594fSAndroid Build Coastguard Worker std::string signature(GetSignature().ToString());
884*795d594fSAndroid Build Coastguard Worker signature.erase(0, 1);
885*795d594fSAndroid Build Coastguard Worker signature.erase(signature.begin() + signature.find(')'), signature.end());
886*795d594fSAndroid Build Coastguard Worker
887*795d594fSAndroid Build Coastguard Worker long_name += MangleForJni(signature);
888*795d594fSAndroid Build Coastguard Worker
889*795d594fSAndroid Build Coastguard Worker return long_name;
890*795d594fSAndroid Build Coastguard Worker }
891*795d594fSAndroid Build Coastguard Worker
GetRuntimeMethodName()892*795d594fSAndroid Build Coastguard Worker const char* ArtMethod::GetRuntimeMethodName() {
893*795d594fSAndroid Build Coastguard Worker Runtime* const runtime = Runtime::Current();
894*795d594fSAndroid Build Coastguard Worker if (this == runtime->GetResolutionMethod()) {
895*795d594fSAndroid Build Coastguard Worker return "<runtime internal resolution method>";
896*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetImtConflictMethod()) {
897*795d594fSAndroid Build Coastguard Worker return "<runtime internal imt conflict method>";
898*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
899*795d594fSAndroid Build Coastguard Worker return "<runtime internal callee-save all registers method>";
900*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
901*795d594fSAndroid Build Coastguard Worker return "<runtime internal callee-save reference registers method>";
902*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
903*795d594fSAndroid Build Coastguard Worker return "<runtime internal callee-save reference and argument registers method>";
904*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
905*795d594fSAndroid Build Coastguard Worker return "<runtime internal save-every-register method>";
906*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
907*795d594fSAndroid Build Coastguard Worker return "<runtime internal save-every-register method for clinit>";
908*795d594fSAndroid Build Coastguard Worker } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
909*795d594fSAndroid Build Coastguard Worker return "<runtime internal save-every-register method for suspend check>";
910*795d594fSAndroid Build Coastguard Worker } else {
911*795d594fSAndroid Build Coastguard Worker return "<unknown runtime internal method>";
912*795d594fSAndroid Build Coastguard Worker }
913*795d594fSAndroid Build Coastguard Worker }
914*795d594fSAndroid Build Coastguard Worker
SetCodeItem(const dex::CodeItem * code_item,bool is_compact_dex_code_item)915*795d594fSAndroid Build Coastguard Worker void ArtMethod::SetCodeItem(const dex::CodeItem* code_item, bool is_compact_dex_code_item) {
916*795d594fSAndroid Build Coastguard Worker DCHECK(HasCodeItem());
917*795d594fSAndroid Build Coastguard Worker // We mark the lowest bit for the interpreter to know whether it's executing a
918*795d594fSAndroid Build Coastguard Worker // method in a compact or standard dex file.
919*795d594fSAndroid Build Coastguard Worker uintptr_t data =
920*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(code_item) | (is_compact_dex_code_item ? 1 : 0);
921*795d594fSAndroid Build Coastguard Worker SetDataPtrSize(reinterpret_cast<void*>(data), kRuntimePointerSize);
922*795d594fSAndroid Build Coastguard Worker }
923*795d594fSAndroid Build Coastguard Worker
924*795d594fSAndroid Build Coastguard Worker // AssertSharedHeld doesn't work in GetAccessFlags, so use a NO_THREAD_SAFETY_ANALYSIS helper.
925*795d594fSAndroid Build Coastguard Worker // TODO: Figure out why ASSERT_SHARED_CAPABILITY doesn't work.
926*795d594fSAndroid Build Coastguard Worker template <ReadBarrierOption kReadBarrierOption>
DoGetAccessFlagsHelper(ArtMethod * method)927*795d594fSAndroid Build Coastguard Worker ALWAYS_INLINE static inline void DoGetAccessFlagsHelper(ArtMethod* method)
928*795d594fSAndroid Build Coastguard Worker NO_THREAD_SAFETY_ANALYSIS {
929*795d594fSAndroid Build Coastguard Worker CHECK(method->IsRuntimeMethod() ||
930*795d594fSAndroid Build Coastguard Worker method->GetDeclaringClass<kReadBarrierOption>()->IsIdxLoaded() ||
931*795d594fSAndroid Build Coastguard Worker method->GetDeclaringClass<kReadBarrierOption>()->IsErroneous());
932*795d594fSAndroid Build Coastguard Worker }
933*795d594fSAndroid Build Coastguard Worker
934*795d594fSAndroid Build Coastguard Worker } // namespace art
935