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 #include "entrypoints/entrypoint_utils.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "art_field-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/sdk_version.h"
24*795d594fSAndroid Build Coastguard Worker #include "class_linker-inl.h"
25*795d594fSAndroid Build Coastguard Worker #include "class_root-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
27*795d594fSAndroid Build Coastguard Worker #include "dex/method_reference.h"
28*795d594fSAndroid Build Coastguard Worker #include "entrypoints/entrypoint_utils-inl.h"
29*795d594fSAndroid Build Coastguard Worker #include "entrypoints/quick/callee_save_frame.h"
30*795d594fSAndroid Build Coastguard Worker #include "entrypoints/runtime_asm_entrypoints.h"
31*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/card_table-inl.h"
32*795d594fSAndroid Build Coastguard Worker #include "jni/java_vm_ext.h"
33*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "mirror/method.h"
35*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "mirror/object_array-alloc-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "nth_caller_visitor.h"
38*795d594fSAndroid Build Coastguard Worker #include "oat/index_bss_mapping.h"
39*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file-inl.h"
40*795d594fSAndroid Build Coastguard Worker #include "oat/oat_quick_method_header.h"
41*795d594fSAndroid Build Coastguard Worker #include "reflection.h"
42*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
43*795d594fSAndroid Build Coastguard Worker #include "well_known_classes-inl.h"
44*795d594fSAndroid Build Coastguard Worker
45*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
46*795d594fSAndroid Build Coastguard Worker
CheckReferenceResult(Handle<mirror::Object> o,Thread * self)47*795d594fSAndroid Build Coastguard Worker void CheckReferenceResult(Handle<mirror::Object> o, Thread* self) {
48*795d594fSAndroid Build Coastguard Worker if (o == nullptr) {
49*795d594fSAndroid Build Coastguard Worker return;
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker // Make sure that the result is an instance of the type this method was expected to return.
52*795d594fSAndroid Build Coastguard Worker ArtMethod* method = self->GetCurrentMethod(nullptr);
53*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> return_type = method->ResolveReturnType();
54*795d594fSAndroid Build Coastguard Worker
55*795d594fSAndroid Build Coastguard Worker if (!o->InstanceOf(return_type)) {
56*795d594fSAndroid Build Coastguard Worker Runtime::Current()->GetJavaVM()->JniAbortF(nullptr,
57*795d594fSAndroid Build Coastguard Worker "attempt to return an instance of %s from %s",
58*795d594fSAndroid Build Coastguard Worker o->PrettyTypeOf().c_str(),
59*795d594fSAndroid Build Coastguard Worker method->PrettyMethod().c_str());
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker
InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable & soa,const char * shorty,jobject rcvr_jobj,jobject interface_method_jobj,std::vector<jvalue> & args)63*795d594fSAndroid Build Coastguard Worker JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa,
64*795d594fSAndroid Build Coastguard Worker const char* shorty,
65*795d594fSAndroid Build Coastguard Worker jobject rcvr_jobj,
66*795d594fSAndroid Build Coastguard Worker jobject interface_method_jobj,
67*795d594fSAndroid Build Coastguard Worker std::vector<jvalue>& args) {
68*795d594fSAndroid Build Coastguard Worker StackHandleScope<4u> hs(soa.Self());
69*795d594fSAndroid Build Coastguard Worker DCHECK(rcvr_jobj != nullptr);
70*795d594fSAndroid Build Coastguard Worker Handle<mirror::Object> h_receiver = hs.NewHandle(soa.Decode<mirror::Object>(rcvr_jobj));
71*795d594fSAndroid Build Coastguard Worker DCHECK(h_receiver->InstanceOf(GetClassRoot(ClassRoot::kJavaLangReflectProxy)));
72*795d594fSAndroid Build Coastguard Worker Handle<mirror::Method> h_interface_method =
73*795d594fSAndroid Build Coastguard Worker hs.NewHandle(soa.Decode<mirror::Method>(interface_method_jobj));
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker // Build argument array possibly triggering GC.
76*795d594fSAndroid Build Coastguard Worker soa.Self()->AssertThreadSuspensionIsAllowable();
77*795d594fSAndroid Build Coastguard Worker auto h_args = hs.NewHandle<mirror::ObjectArray<mirror::Object>>(nullptr);
78*795d594fSAndroid Build Coastguard Worker const JValue zero;
79*795d594fSAndroid Build Coastguard Worker Runtime* runtime = Runtime::Current();
80*795d594fSAndroid Build Coastguard Worker uint32_t target_sdk_version = runtime->GetTargetSdkVersion();
81*795d594fSAndroid Build Coastguard Worker // Do not create empty arrays unless needed to maintain Dalvik bug compatibility.
82*795d594fSAndroid Build Coastguard Worker if (args.size() > 0 || IsSdkVersionSetAndAtMost(target_sdk_version, SdkVersion::kL)) {
83*795d594fSAndroid Build Coastguard Worker h_args.Assign(mirror::ObjectArray<mirror::Object>::Alloc(
84*795d594fSAndroid Build Coastguard Worker soa.Self(), GetClassRoot<mirror::ObjectArray<mirror::Object>>(), args.size()));
85*795d594fSAndroid Build Coastguard Worker if (h_args == nullptr) {
86*795d594fSAndroid Build Coastguard Worker CHECK(soa.Self()->IsExceptionPending());
87*795d594fSAndroid Build Coastguard Worker return zero;
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < args.size(); ++i) {
90*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> value;
91*795d594fSAndroid Build Coastguard Worker if (shorty[i + 1] == 'L') {
92*795d594fSAndroid Build Coastguard Worker value = soa.Decode<mirror::Object>(args[i].l);
93*795d594fSAndroid Build Coastguard Worker } else {
94*795d594fSAndroid Build Coastguard Worker JValue jv;
95*795d594fSAndroid Build Coastguard Worker jv.SetJ(args[i].j);
96*795d594fSAndroid Build Coastguard Worker value = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
97*795d594fSAndroid Build Coastguard Worker if (value == nullptr) {
98*795d594fSAndroid Build Coastguard Worker CHECK(soa.Self()->IsExceptionPending());
99*795d594fSAndroid Build Coastguard Worker return zero;
100*795d594fSAndroid Build Coastguard Worker }
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker // We do not support `Proxy.invoke()` in a transaction.
103*795d594fSAndroid Build Coastguard Worker h_args->SetWithoutChecks</*kActiveTransaction=*/ false>(i, value);
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker
107*795d594fSAndroid Build Coastguard Worker // Call Proxy.invoke(Proxy proxy, Method method, Object[] args).
108*795d594fSAndroid Build Coastguard Worker Handle<mirror::Object> h_result = hs.NewHandle(
109*795d594fSAndroid Build Coastguard Worker WellKnownClasses::java_lang_reflect_Proxy_invoke->InvokeStatic<'L', 'L', 'L', 'L'>(
110*795d594fSAndroid Build Coastguard Worker soa.Self(), h_receiver.Get(), h_interface_method.Get(), h_args.Get()));
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker // Unbox result and handle error conditions.
113*795d594fSAndroid Build Coastguard Worker if (LIKELY(!soa.Self()->IsExceptionPending())) {
114*795d594fSAndroid Build Coastguard Worker if (shorty[0] == 'V' || (shorty[0] == 'L' && h_result == nullptr)) {
115*795d594fSAndroid Build Coastguard Worker // Do nothing.
116*795d594fSAndroid Build Coastguard Worker return zero;
117*795d594fSAndroid Build Coastguard Worker } else {
118*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> result_type;
119*795d594fSAndroid Build Coastguard Worker if (shorty[0] == 'L') {
120*795d594fSAndroid Build Coastguard Worker // This can cause thread suspension.
121*795d594fSAndroid Build Coastguard Worker result_type = h_interface_method->GetArtMethod()->ResolveReturnType();
122*795d594fSAndroid Build Coastguard Worker if (result_type == nullptr) {
123*795d594fSAndroid Build Coastguard Worker DCHECK(soa.Self()->IsExceptionPending());
124*795d594fSAndroid Build Coastguard Worker return zero;
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker } else {
127*795d594fSAndroid Build Coastguard Worker result_type = runtime->GetClassLinker()->LookupPrimitiveClass(shorty[0]);
128*795d594fSAndroid Build Coastguard Worker DCHECK(result_type != nullptr);
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker JValue result_unboxed;
131*795d594fSAndroid Build Coastguard Worker if (!UnboxPrimitiveForResult(h_result.Get(), result_type, &result_unboxed)) {
132*795d594fSAndroid Build Coastguard Worker DCHECK(soa.Self()->IsExceptionPending());
133*795d594fSAndroid Build Coastguard Worker return zero;
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker return result_unboxed;
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker } else {
138*795d594fSAndroid Build Coastguard Worker // In the case of checked exceptions that aren't declared, the exception must be wrapped by
139*795d594fSAndroid Build Coastguard Worker // a UndeclaredThrowableException.
140*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Throwable> exception = soa.Self()->GetException();
141*795d594fSAndroid Build Coastguard Worker if (exception->IsCheckedException()) {
142*795d594fSAndroid Build Coastguard Worker bool declares_exception = false;
143*795d594fSAndroid Build Coastguard Worker {
144*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants(__FUNCTION__);
145*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Object> rcvr = soa.Decode<mirror::Object>(rcvr_jobj);
146*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> proxy_class = rcvr->GetClass();
147*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Method> interface_method = soa.Decode<mirror::Method>(interface_method_jobj);
148*795d594fSAndroid Build Coastguard Worker ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(
149*795d594fSAndroid Build Coastguard Worker interface_method->GetArtMethod(), kRuntimePointerSize);
150*795d594fSAndroid Build Coastguard Worker auto virtual_methods = proxy_class->GetVirtualMethodsSlice(kRuntimePointerSize);
151*795d594fSAndroid Build Coastguard Worker size_t num_virtuals = proxy_class->NumVirtualMethods();
152*795d594fSAndroid Build Coastguard Worker size_t method_size = ArtMethod::Size(kRuntimePointerSize);
153*795d594fSAndroid Build Coastguard Worker // Rely on the fact that the methods are contiguous to determine the index of the method in
154*795d594fSAndroid Build Coastguard Worker // the slice.
155*795d594fSAndroid Build Coastguard Worker int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) -
156*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(&virtual_methods[0])) / method_size;
157*795d594fSAndroid Build Coastguard Worker CHECK_LT(throws_index, static_cast<int>(num_virtuals));
158*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::ObjectArray<mirror::Class>> declared_exceptions =
159*795d594fSAndroid Build Coastguard Worker proxy_class->GetProxyThrows()->Get(throws_index);
160*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> exception_class = exception->GetClass();
161*795d594fSAndroid Build Coastguard Worker for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
162*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declared_exception = declared_exceptions->Get(i);
163*795d594fSAndroid Build Coastguard Worker declares_exception = declared_exception->IsAssignableFrom(exception_class);
164*795d594fSAndroid Build Coastguard Worker }
165*795d594fSAndroid Build Coastguard Worker }
166*795d594fSAndroid Build Coastguard Worker if (!declares_exception) {
167*795d594fSAndroid Build Coastguard Worker soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
168*795d594fSAndroid Build Coastguard Worker nullptr);
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker }
171*795d594fSAndroid Build Coastguard Worker return zero;
172*795d594fSAndroid Build Coastguard Worker }
173*795d594fSAndroid Build Coastguard Worker }
174*795d594fSAndroid Build Coastguard Worker
FillArrayData(ObjPtr<mirror::Object> obj,const Instruction::ArrayDataPayload * payload)175*795d594fSAndroid Build Coastguard Worker bool FillArrayData(ObjPtr<mirror::Object> obj, const Instruction::ArrayDataPayload* payload) {
176*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
177*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(obj == nullptr)) {
178*795d594fSAndroid Build Coastguard Worker ThrowNullPointerException("null array in FILL_ARRAY_DATA");
179*795d594fSAndroid Build Coastguard Worker return false;
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Array> array = obj->AsArray();
182*795d594fSAndroid Build Coastguard Worker DCHECK(!array->IsObjectArray());
183*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
184*795d594fSAndroid Build Coastguard Worker Thread* self = Thread::Current();
185*795d594fSAndroid Build Coastguard Worker self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
186*795d594fSAndroid Build Coastguard Worker "failed FILL_ARRAY_DATA; length=%d, index=%d",
187*795d594fSAndroid Build Coastguard Worker array->GetLength(), payload->element_count);
188*795d594fSAndroid Build Coastguard Worker return false;
189*795d594fSAndroid Build Coastguard Worker }
190*795d594fSAndroid Build Coastguard Worker // Copy data from dex file to memory assuming both are little endian.
191*795d594fSAndroid Build Coastguard Worker uint32_t size_in_bytes = payload->element_count * payload->element_width;
192*795d594fSAndroid Build Coastguard Worker memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes);
193*795d594fSAndroid Build Coastguard Worker return true;
194*795d594fSAndroid Build Coastguard Worker }
195*795d594fSAndroid Build Coastguard Worker
DoGetCalleeSaveMethodOuterCallerAndPc(ArtMethod ** sp,CalleeSaveType type)196*795d594fSAndroid Build Coastguard Worker static inline std::pair<ArtMethod*, uintptr_t> DoGetCalleeSaveMethodOuterCallerAndPc(
197*795d594fSAndroid Build Coastguard Worker ArtMethod** sp, CalleeSaveType type) REQUIRES_SHARED(Locks::mutator_lock_) {
198*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
199*795d594fSAndroid Build Coastguard Worker
200*795d594fSAndroid Build Coastguard Worker const size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
201*795d594fSAndroid Build Coastguard Worker auto** caller_sp = reinterpret_cast<ArtMethod**>(
202*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
203*795d594fSAndroid Build Coastguard Worker const size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
204*795d594fSAndroid Build Coastguard Worker uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
205*795d594fSAndroid Build Coastguard Worker (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
206*795d594fSAndroid Build Coastguard Worker ArtMethod* outer_method = *caller_sp;
207*795d594fSAndroid Build Coastguard Worker return std::make_pair(outer_method, caller_pc);
208*795d594fSAndroid Build Coastguard Worker }
209*795d594fSAndroid Build Coastguard Worker
DoGetCalleeSaveMethodCallerAndDexPc(ArtMethod ** sp,CalleeSaveType type,ArtMethod * outer_method,uintptr_t caller_pc,uint32_t * dex_pc,bool do_caller_check)210*795d594fSAndroid Build Coastguard Worker static inline ArtMethod* DoGetCalleeSaveMethodCallerAndDexPc(ArtMethod** sp,
211*795d594fSAndroid Build Coastguard Worker CalleeSaveType type,
212*795d594fSAndroid Build Coastguard Worker ArtMethod* outer_method,
213*795d594fSAndroid Build Coastguard Worker uintptr_t caller_pc,
214*795d594fSAndroid Build Coastguard Worker uint32_t* dex_pc,
215*795d594fSAndroid Build Coastguard Worker bool do_caller_check)
216*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
217*795d594fSAndroid Build Coastguard Worker ArtMethod* caller = outer_method;
218*795d594fSAndroid Build Coastguard Worker if (outer_method != nullptr) {
219*795d594fSAndroid Build Coastguard Worker const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
220*795d594fSAndroid Build Coastguard Worker DCHECK(current_code != nullptr);
221*795d594fSAndroid Build Coastguard Worker if (current_code->IsOptimized() &&
222*795d594fSAndroid Build Coastguard Worker CodeInfo::HasInlineInfo(current_code->GetOptimizedCodeInfoPtr())) {
223*795d594fSAndroid Build Coastguard Worker uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
224*795d594fSAndroid Build Coastguard Worker CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(current_code);
225*795d594fSAndroid Build Coastguard Worker StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
226*795d594fSAndroid Build Coastguard Worker DCHECK(stack_map.IsValid());
227*795d594fSAndroid Build Coastguard Worker BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
228*795d594fSAndroid Build Coastguard Worker if (!inline_infos.empty()) {
229*795d594fSAndroid Build Coastguard Worker caller = GetResolvedMethod(outer_method, code_info, inline_infos);
230*795d594fSAndroid Build Coastguard Worker *dex_pc = inline_infos.back().GetDexPc();
231*795d594fSAndroid Build Coastguard Worker } else {
232*795d594fSAndroid Build Coastguard Worker *dex_pc = stack_map.GetDexPc();
233*795d594fSAndroid Build Coastguard Worker }
234*795d594fSAndroid Build Coastguard Worker } else {
235*795d594fSAndroid Build Coastguard Worker size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
236*795d594fSAndroid Build Coastguard Worker ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
237*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
238*795d594fSAndroid Build Coastguard Worker *dex_pc = current_code->ToDexPc(caller_sp, caller_pc);
239*795d594fSAndroid Build Coastguard Worker }
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild && do_caller_check) {
242*795d594fSAndroid Build Coastguard Worker // Note that do_caller_check is optional, as this method can be called by
243*795d594fSAndroid Build Coastguard Worker // stubs, and tests without a proper call stack.
244*795d594fSAndroid Build Coastguard Worker NthCallerVisitor visitor(Thread::Current(), 1, true);
245*795d594fSAndroid Build Coastguard Worker visitor.WalkStack();
246*795d594fSAndroid Build Coastguard Worker CHECK_EQ(caller, visitor.caller);
247*795d594fSAndroid Build Coastguard Worker }
248*795d594fSAndroid Build Coastguard Worker return caller;
249*795d594fSAndroid Build Coastguard Worker }
250*795d594fSAndroid Build Coastguard Worker
GetCalleeSaveMethodCallerAndDexPc(ArtMethod ** sp,CalleeSaveType type,uint32_t * dex_pc,bool do_caller_check)251*795d594fSAndroid Build Coastguard Worker ArtMethod* GetCalleeSaveMethodCallerAndDexPc(ArtMethod** sp,
252*795d594fSAndroid Build Coastguard Worker CalleeSaveType type,
253*795d594fSAndroid Build Coastguard Worker uint32_t* dex_pc,
254*795d594fSAndroid Build Coastguard Worker bool do_caller_check)
255*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
256*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants(__FUNCTION__);
257*795d594fSAndroid Build Coastguard Worker auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type);
258*795d594fSAndroid Build Coastguard Worker ArtMethod* outer_method = outer_caller_and_pc.first;
259*795d594fSAndroid Build Coastguard Worker uintptr_t caller_pc = outer_caller_and_pc.second;
260*795d594fSAndroid Build Coastguard Worker ArtMethod* caller = DoGetCalleeSaveMethodCallerAndDexPc(sp,
261*795d594fSAndroid Build Coastguard Worker type,
262*795d594fSAndroid Build Coastguard Worker outer_method,
263*795d594fSAndroid Build Coastguard Worker caller_pc,
264*795d594fSAndroid Build Coastguard Worker dex_pc,
265*795d594fSAndroid Build Coastguard Worker do_caller_check);
266*795d594fSAndroid Build Coastguard Worker return caller;
267*795d594fSAndroid Build Coastguard Worker }
268*795d594fSAndroid Build Coastguard Worker
GetCalleeSaveMethodCallerAndOuterMethod(Thread * self,CalleeSaveType type)269*795d594fSAndroid Build Coastguard Worker CallerAndOuterMethod GetCalleeSaveMethodCallerAndOuterMethod(Thread* self, CalleeSaveType type) {
270*795d594fSAndroid Build Coastguard Worker CallerAndOuterMethod result;
271*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants(__FUNCTION__);
272*795d594fSAndroid Build Coastguard Worker ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrameKnownNotTagged();
273*795d594fSAndroid Build Coastguard Worker auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type);
274*795d594fSAndroid Build Coastguard Worker result.outer_method = outer_caller_and_pc.first;
275*795d594fSAndroid Build Coastguard Worker uintptr_t caller_pc = outer_caller_and_pc.second;
276*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc;
277*795d594fSAndroid Build Coastguard Worker result.caller = DoGetCalleeSaveMethodCallerAndDexPc(sp,
278*795d594fSAndroid Build Coastguard Worker type,
279*795d594fSAndroid Build Coastguard Worker result.outer_method,
280*795d594fSAndroid Build Coastguard Worker caller_pc,
281*795d594fSAndroid Build Coastguard Worker &dex_pc,
282*795d594fSAndroid Build Coastguard Worker /* do_caller_check= */ true);
283*795d594fSAndroid Build Coastguard Worker return result;
284*795d594fSAndroid Build Coastguard Worker }
285*795d594fSAndroid Build Coastguard Worker
GetCalleeSaveOuterMethod(Thread * self,CalleeSaveType type)286*795d594fSAndroid Build Coastguard Worker ArtMethod* GetCalleeSaveOuterMethod(Thread* self, CalleeSaveType type) {
287*795d594fSAndroid Build Coastguard Worker ScopedAssertNoThreadSuspension ants(__FUNCTION__);
288*795d594fSAndroid Build Coastguard Worker ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrameKnownNotTagged();
289*795d594fSAndroid Build Coastguard Worker return DoGetCalleeSaveMethodOuterCallerAndPc(sp, type).first;
290*795d594fSAndroid Build Coastguard Worker }
291*795d594fSAndroid Build Coastguard Worker
ResolveMethodHandleFromCode(ArtMethod * referrer,uint32_t method_handle_idx)292*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::MethodHandle> ResolveMethodHandleFromCode(ArtMethod* referrer,
293*795d594fSAndroid Build Coastguard Worker uint32_t method_handle_idx) {
294*795d594fSAndroid Build Coastguard Worker Thread::PoisonObjectPointersIfDebug();
295*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
296*795d594fSAndroid Build Coastguard Worker return class_linker->ResolveMethodHandle(Thread::Current(), method_handle_idx, referrer);
297*795d594fSAndroid Build Coastguard Worker }
298*795d594fSAndroid Build Coastguard Worker
ResolveMethodTypeFromCode(ArtMethod * referrer,dex::ProtoIndex proto_idx)299*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::MethodType> ResolveMethodTypeFromCode(ArtMethod* referrer,
300*795d594fSAndroid Build Coastguard Worker dex::ProtoIndex proto_idx) {
301*795d594fSAndroid Build Coastguard Worker Thread::PoisonObjectPointersIfDebug();
302*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::MethodType> method_type =
303*795d594fSAndroid Build Coastguard Worker referrer->GetDexCache()->GetResolvedMethodType(proto_idx);
304*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(method_type == nullptr)) {
305*795d594fSAndroid Build Coastguard Worker StackHandleScope<2> hs(Thread::Current());
306*795d594fSAndroid Build Coastguard Worker Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
307*795d594fSAndroid Build Coastguard Worker Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
308*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
309*795d594fSAndroid Build Coastguard Worker method_type = class_linker->ResolveMethodType(hs.Self(), proto_idx, dex_cache, class_loader);
310*795d594fSAndroid Build Coastguard Worker }
311*795d594fSAndroid Build Coastguard Worker return method_type;
312*795d594fSAndroid Build Coastguard Worker }
313*795d594fSAndroid Build Coastguard Worker
MaybeUpdateBssMethodEntry(ArtMethod * callee,MethodReference callee_reference,ArtMethod * outer_method)314*795d594fSAndroid Build Coastguard Worker void MaybeUpdateBssMethodEntry(ArtMethod* callee,
315*795d594fSAndroid Build Coastguard Worker MethodReference callee_reference,
316*795d594fSAndroid Build Coastguard Worker ArtMethod* outer_method) {
317*795d594fSAndroid Build Coastguard Worker DCHECK_NE(callee, nullptr);
318*795d594fSAndroid Build Coastguard Worker if (outer_method->GetDexFile()->GetOatDexFile() == nullptr ||
319*795d594fSAndroid Build Coastguard Worker outer_method->GetDexFile()->GetOatDexFile()->GetOatFile() == nullptr) {
320*795d594fSAndroid Build Coastguard Worker // No OatFile to update.
321*795d594fSAndroid Build Coastguard Worker return;
322*795d594fSAndroid Build Coastguard Worker }
323*795d594fSAndroid Build Coastguard Worker const OatFile* outer_oat_file = outer_method->GetDexFile()->GetOatDexFile()->GetOatFile();
324*795d594fSAndroid Build Coastguard Worker
325*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = callee_reference.dex_file;
326*795d594fSAndroid Build Coastguard Worker const OatDexFile* oat_dex_file = dex_file->GetOatDexFile();
327*795d594fSAndroid Build Coastguard Worker const IndexBssMapping* mapping = nullptr;
328*795d594fSAndroid Build Coastguard Worker if (oat_dex_file != nullptr && oat_dex_file->GetOatFile() == outer_oat_file) {
329*795d594fSAndroid Build Coastguard Worker // DexFiles compiled together to an oat file case.
330*795d594fSAndroid Build Coastguard Worker mapping = oat_dex_file->GetMethodBssMapping();
331*795d594fSAndroid Build Coastguard Worker } else {
332*795d594fSAndroid Build Coastguard Worker // Try to find the DexFile in the BCP of the outer_method.
333*795d594fSAndroid Build Coastguard Worker const OatFile::BssMappingInfo* mapping_info = outer_oat_file->FindBcpMappingInfo(dex_file);
334*795d594fSAndroid Build Coastguard Worker if (mapping_info != nullptr) {
335*795d594fSAndroid Build Coastguard Worker mapping = mapping_info->method_bss_mapping;
336*795d594fSAndroid Build Coastguard Worker }
337*795d594fSAndroid Build Coastguard Worker }
338*795d594fSAndroid Build Coastguard Worker
339*795d594fSAndroid Build Coastguard Worker // Perform the update if we found a mapping.
340*795d594fSAndroid Build Coastguard Worker if (mapping != nullptr) {
341*795d594fSAndroid Build Coastguard Worker size_t bss_offset =
342*795d594fSAndroid Build Coastguard Worker IndexBssMappingLookup::GetBssOffset(mapping,
343*795d594fSAndroid Build Coastguard Worker callee_reference.index,
344*795d594fSAndroid Build Coastguard Worker dex_file->NumMethodIds(),
345*795d594fSAndroid Build Coastguard Worker static_cast<size_t>(kRuntimePointerSize));
346*795d594fSAndroid Build Coastguard Worker if (bss_offset != IndexBssMappingLookup::npos) {
347*795d594fSAndroid Build Coastguard Worker DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
348*795d594fSAndroid Build Coastguard Worker DCHECK_NE(outer_oat_file, nullptr);
349*795d594fSAndroid Build Coastguard Worker ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(
350*795d594fSAndroid Build Coastguard Worker const_cast<uint8_t*>(outer_oat_file->BssBegin() + bss_offset));
351*795d594fSAndroid Build Coastguard Worker DCHECK_GE(method_entry, outer_oat_file->GetBssMethods().data());
352*795d594fSAndroid Build Coastguard Worker DCHECK_LT(method_entry,
353*795d594fSAndroid Build Coastguard Worker outer_oat_file->GetBssMethods().data() + outer_oat_file->GetBssMethods().size());
354*795d594fSAndroid Build Coastguard Worker std::atomic<ArtMethod*>* atomic_entry =
355*795d594fSAndroid Build Coastguard Worker reinterpret_cast<std::atomic<ArtMethod*>*>(method_entry);
356*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
357*795d594fSAndroid Build Coastguard Worker ArtMethod* existing = atomic_entry->load(std::memory_order_acquire);
358*795d594fSAndroid Build Coastguard Worker CHECK(existing->IsRuntimeMethod() || existing == callee);
359*795d594fSAndroid Build Coastguard Worker }
360*795d594fSAndroid Build Coastguard Worker static_assert(sizeof(*method_entry) == sizeof(*atomic_entry), "Size check.");
361*795d594fSAndroid Build Coastguard Worker atomic_entry->store(callee, std::memory_order_release);
362*795d594fSAndroid Build Coastguard Worker }
363*795d594fSAndroid Build Coastguard Worker }
364*795d594fSAndroid Build Coastguard Worker }
365*795d594fSAndroid Build Coastguard Worker
366*795d594fSAndroid Build Coastguard Worker } // namespace art
367