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 <android-base/logging.h>
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "arch/arm/jni_frame_arm.h"
20*795d594fSAndroid Build Coastguard Worker #include "arch/arm64/jni_frame_arm64.h"
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
22*795d594fSAndroid Build Coastguard Worker #include "arch/riscv64/jni_frame_riscv64.h"
23*795d594fSAndroid Build Coastguard Worker #include "arch/x86/jni_frame_x86.h"
24*795d594fSAndroid Build Coastguard Worker #include "arch/x86_64/jni_frame_x86_64.h"
25*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
26*795d594fSAndroid Build Coastguard Worker #include "dex/dex_instruction-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 "jni/java_vm_ext.h"
30*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "oat/oat_quick_method_header.h"
32*795d594fSAndroid Build Coastguard Worker #include "oat/stack_map.h"
33*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
34*795d594fSAndroid Build Coastguard Worker #include "thread.h"
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
37*795d594fSAndroid Build Coastguard Worker
GetInvokeStaticMethodIndex(ArtMethod * caller,uint32_t dex_pc)38*795d594fSAndroid Build Coastguard Worker static inline uint32_t GetInvokeStaticMethodIndex(ArtMethod* caller, uint32_t dex_pc)
39*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
40*795d594fSAndroid Build Coastguard Worker // Get the DexFile and method index.
41*795d594fSAndroid Build Coastguard Worker const Instruction& instruction = caller->DexInstructions().InstructionAt(dex_pc);
42*795d594fSAndroid Build Coastguard Worker DCHECK(instruction.Opcode() == Instruction::INVOKE_STATIC ||
43*795d594fSAndroid Build Coastguard Worker instruction.Opcode() == Instruction::INVOKE_STATIC_RANGE);
44*795d594fSAndroid Build Coastguard Worker uint32_t method_idx = (instruction.Opcode() == Instruction::INVOKE_STATIC)
45*795d594fSAndroid Build Coastguard Worker ? instruction.VRegB_35c()
46*795d594fSAndroid Build Coastguard Worker : instruction.VRegB_3rc();
47*795d594fSAndroid Build Coastguard Worker return method_idx;
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker // Used by the JNI dlsym stub to find the native method to invoke if none is registered.
artFindNativeMethodRunnable(Thread * self)51*795d594fSAndroid Build Coastguard Worker extern "C" const void* artFindNativeMethodRunnable(Thread* self)
52*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
53*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertSharedHeld(self); // We come here as Runnable.
54*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc;
55*795d594fSAndroid Build Coastguard Worker ArtMethod* method = self->GetCurrentMethod(&dex_pc);
56*795d594fSAndroid Build Coastguard Worker DCHECK(method != nullptr);
57*795d594fSAndroid Build Coastguard Worker ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Worker if (!method->IsNative()) {
60*795d594fSAndroid Build Coastguard Worker // We're coming from compiled managed code and the `method` we see here is the caller.
61*795d594fSAndroid Build Coastguard Worker // Resolve target @CriticalNative method for a direct call from compiled managed code.
62*795d594fSAndroid Build Coastguard Worker uint32_t method_idx = GetInvokeStaticMethodIndex(method, dex_pc);
63*795d594fSAndroid Build Coastguard Worker ArtMethod* target_method = class_linker->ResolveMethodId(method_idx, method);
64*795d594fSAndroid Build Coastguard Worker if (target_method == nullptr) {
65*795d594fSAndroid Build Coastguard Worker self->AssertPendingException();
66*795d594fSAndroid Build Coastguard Worker return nullptr;
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker DCHECK(target_method->IsCriticalNative());
69*795d594fSAndroid Build Coastguard Worker // Note that the BSS also contains entries used for super calls. Given we
70*795d594fSAndroid Build Coastguard Worker // only deal with invokestatic in this code path, we don't need to adjust
71*795d594fSAndroid Build Coastguard Worker // the method index.
72*795d594fSAndroid Build Coastguard Worker MaybeUpdateBssMethodEntry(target_method,
73*795d594fSAndroid Build Coastguard Worker MethodReference(method->GetDexFile(), method_idx),
74*795d594fSAndroid Build Coastguard Worker GetCalleeSaveOuterMethod(self, CalleeSaveType::kSaveRefsAndArgs));
75*795d594fSAndroid Build Coastguard Worker
76*795d594fSAndroid Build Coastguard Worker // These calls do not have an explicit class initialization check, so do the check now.
77*795d594fSAndroid Build Coastguard Worker // (When going through the stub or GenericJNI, the check was already done.)
78*795d594fSAndroid Build Coastguard Worker DCHECK(target_method->NeedsClinitCheckBeforeCall());
79*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
80*795d594fSAndroid Build Coastguard Worker if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) {
81*795d594fSAndroid Build Coastguard Worker StackHandleScope<1> hs(self);
82*795d594fSAndroid Build Coastguard Worker Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
83*795d594fSAndroid Build Coastguard Worker if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
84*795d594fSAndroid Build Coastguard Worker DCHECK(self->IsExceptionPending()) << method->PrettyMethod();
85*795d594fSAndroid Build Coastguard Worker return nullptr;
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker
89*795d594fSAndroid Build Coastguard Worker // Replace the runtime method on the stack with the target method.
90*795d594fSAndroid Build Coastguard Worker DCHECK(!self->GetManagedStack()->GetTopQuickFrameGenericJniTag());
91*795d594fSAndroid Build Coastguard Worker ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrameKnownNotTagged();
92*795d594fSAndroid Build Coastguard Worker DCHECK(*sp == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
93*795d594fSAndroid Build Coastguard Worker *sp = target_method;
94*795d594fSAndroid Build Coastguard Worker self->SetTopOfStackGenericJniTagged(sp); // Fake GenericJNI frame.
95*795d594fSAndroid Build Coastguard Worker
96*795d594fSAndroid Build Coastguard Worker // Continue with the target method.
97*795d594fSAndroid Build Coastguard Worker method = target_method;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker DCHECK(method == self->GetCurrentMethod(/*dex_pc=*/ nullptr));
100*795d594fSAndroid Build Coastguard Worker
101*795d594fSAndroid Build Coastguard Worker // Check whether we already have a registered native code.
102*795d594fSAndroid Build Coastguard Worker // For @CriticalNative it may not be stored in the ArtMethod as a JNI entrypoint if the class
103*795d594fSAndroid Build Coastguard Worker // was not visibly initialized yet. Do this check also for @FastNative and normal native for
104*795d594fSAndroid Build Coastguard Worker // consistency; though success would mean that another thread raced to do this lookup.
105*795d594fSAndroid Build Coastguard Worker const void* native_code = class_linker->GetRegisteredNative(self, method);
106*795d594fSAndroid Build Coastguard Worker if (native_code != nullptr) {
107*795d594fSAndroid Build Coastguard Worker return native_code;
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker
110*795d594fSAndroid Build Coastguard Worker // Lookup symbol address for method, on failure we'll return null with an exception set,
111*795d594fSAndroid Build Coastguard Worker // otherwise we return the address of the method we found.
112*795d594fSAndroid Build Coastguard Worker JavaVMExt* vm = down_cast<JNIEnvExt*>(self->GetJniEnv())->GetVm();
113*795d594fSAndroid Build Coastguard Worker std::string error_msg;
114*795d594fSAndroid Build Coastguard Worker native_code = vm->FindCodeForNativeMethod(method, &error_msg, /*can_suspend=*/ true);
115*795d594fSAndroid Build Coastguard Worker if (native_code == nullptr) {
116*795d594fSAndroid Build Coastguard Worker LOG(ERROR) << error_msg;
117*795d594fSAndroid Build Coastguard Worker self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", error_msg.c_str());
118*795d594fSAndroid Build Coastguard Worker return nullptr;
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker
121*795d594fSAndroid Build Coastguard Worker // Register the code. This usually prevents future calls from coming to this function again.
122*795d594fSAndroid Build Coastguard Worker // We can still come here if the ClassLinker cannot set the entrypoint in the ArtMethod,
123*795d594fSAndroid Build Coastguard Worker // i.e. for @CriticalNative methods with the declaring class not visibly initialized.
124*795d594fSAndroid Build Coastguard Worker return class_linker->RegisterNative(self, method, native_code);
125*795d594fSAndroid Build Coastguard Worker }
126*795d594fSAndroid Build Coastguard Worker
127*795d594fSAndroid Build Coastguard Worker // Used by the JNI dlsym stub to find the native method to invoke if none is registered.
artFindNativeMethod(Thread * self)128*795d594fSAndroid Build Coastguard Worker extern "C" const void* artFindNativeMethod(Thread* self) {
129*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(self, Thread::Current());
130*795d594fSAndroid Build Coastguard Worker Locks::mutator_lock_->AssertNotHeld(self); // We come here as Native.
131*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(self);
132*795d594fSAndroid Build Coastguard Worker return artFindNativeMethodRunnable(self);
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker
artCriticalNativeFrameSize(ArtMethod * method,uintptr_t caller_pc)135*795d594fSAndroid Build Coastguard Worker extern "C" size_t artCriticalNativeFrameSize(ArtMethod* method, uintptr_t caller_pc)
136*795d594fSAndroid Build Coastguard Worker REQUIRES_SHARED(Locks::mutator_lock_) {
137*795d594fSAndroid Build Coastguard Worker if (method->IsNative()) {
138*795d594fSAndroid Build Coastguard Worker // Get the method's shorty.
139*795d594fSAndroid Build Coastguard Worker DCHECK(method->IsCriticalNative());
140*795d594fSAndroid Build Coastguard Worker std::string_view shorty = method->GetShortyView();
141*795d594fSAndroid Build Coastguard Worker
142*795d594fSAndroid Build Coastguard Worker // Return the platform-dependent stub frame size.
143*795d594fSAndroid Build Coastguard Worker switch (kRuntimeISA) {
144*795d594fSAndroid Build Coastguard Worker case InstructionSet::kArm:
145*795d594fSAndroid Build Coastguard Worker case InstructionSet::kThumb2:
146*795d594fSAndroid Build Coastguard Worker return arm::GetCriticalNativeStubFrameSize(shorty);
147*795d594fSAndroid Build Coastguard Worker case InstructionSet::kArm64:
148*795d594fSAndroid Build Coastguard Worker return arm64::GetCriticalNativeStubFrameSize(shorty);
149*795d594fSAndroid Build Coastguard Worker case InstructionSet::kRiscv64:
150*795d594fSAndroid Build Coastguard Worker return riscv64::GetCriticalNativeStubFrameSize(shorty);
151*795d594fSAndroid Build Coastguard Worker case InstructionSet::kX86:
152*795d594fSAndroid Build Coastguard Worker return x86::GetCriticalNativeStubFrameSize(shorty);
153*795d594fSAndroid Build Coastguard Worker case InstructionSet::kX86_64:
154*795d594fSAndroid Build Coastguard Worker return x86_64::GetCriticalNativeStubFrameSize(shorty);
155*795d594fSAndroid Build Coastguard Worker default:
156*795d594fSAndroid Build Coastguard Worker UNIMPLEMENTED(FATAL) << kRuntimeISA;
157*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker } else {
160*795d594fSAndroid Build Coastguard Worker // We're coming from compiled managed code and the `method` we see here is the compiled
161*795d594fSAndroid Build Coastguard Worker // method that made the call. Get the actual caller (may be inlined) and dex pc.
162*795d594fSAndroid Build Coastguard Worker const OatQuickMethodHeader* current_code = method->GetOatQuickMethodHeader(caller_pc);
163*795d594fSAndroid Build Coastguard Worker DCHECK(current_code != nullptr);
164*795d594fSAndroid Build Coastguard Worker DCHECK(current_code->IsOptimized());
165*795d594fSAndroid Build Coastguard Worker uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
166*795d594fSAndroid Build Coastguard Worker CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(current_code);
167*795d594fSAndroid Build Coastguard Worker StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
168*795d594fSAndroid Build Coastguard Worker DCHECK(stack_map.IsValid());
169*795d594fSAndroid Build Coastguard Worker BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
170*795d594fSAndroid Build Coastguard Worker ArtMethod* caller =
171*795d594fSAndroid Build Coastguard Worker inline_infos.empty() ? method : GetResolvedMethod(method, code_info, inline_infos);
172*795d594fSAndroid Build Coastguard Worker uint32_t dex_pc = inline_infos.empty() ? stack_map.GetDexPc() : inline_infos.back().GetDexPc();
173*795d594fSAndroid Build Coastguard Worker
174*795d594fSAndroid Build Coastguard Worker // Get the callee shorty.
175*795d594fSAndroid Build Coastguard Worker const DexFile* dex_file = caller->GetDexFile();
176*795d594fSAndroid Build Coastguard Worker uint32_t method_idx = GetInvokeStaticMethodIndex(caller, dex_pc);
177*795d594fSAndroid Build Coastguard Worker std::string_view shorty = dex_file->GetMethodShortyView(method_idx);
178*795d594fSAndroid Build Coastguard Worker
179*795d594fSAndroid Build Coastguard Worker // Return the platform-dependent direct call frame size.
180*795d594fSAndroid Build Coastguard Worker switch (kRuntimeISA) {
181*795d594fSAndroid Build Coastguard Worker case InstructionSet::kArm:
182*795d594fSAndroid Build Coastguard Worker case InstructionSet::kThumb2:
183*795d594fSAndroid Build Coastguard Worker return arm::GetCriticalNativeDirectCallFrameSize(shorty);
184*795d594fSAndroid Build Coastguard Worker case InstructionSet::kArm64:
185*795d594fSAndroid Build Coastguard Worker return arm64::GetCriticalNativeDirectCallFrameSize(shorty);
186*795d594fSAndroid Build Coastguard Worker case InstructionSet::kRiscv64:
187*795d594fSAndroid Build Coastguard Worker return riscv64::GetCriticalNativeDirectCallFrameSize(shorty);
188*795d594fSAndroid Build Coastguard Worker case InstructionSet::kX86:
189*795d594fSAndroid Build Coastguard Worker return x86::GetCriticalNativeDirectCallFrameSize(shorty);
190*795d594fSAndroid Build Coastguard Worker case InstructionSet::kX86_64:
191*795d594fSAndroid Build Coastguard Worker return x86_64::GetCriticalNativeDirectCallFrameSize(shorty);
192*795d594fSAndroid Build Coastguard Worker default:
193*795d594fSAndroid Build Coastguard Worker UNIMPLEMENTED(FATAL) << kRuntimeISA;
194*795d594fSAndroid Build Coastguard Worker UNREACHABLE();
195*795d594fSAndroid Build Coastguard Worker }
196*795d594fSAndroid Build Coastguard Worker }
197*795d594fSAndroid Build Coastguard Worker }
198*795d594fSAndroid Build Coastguard Worker
199*795d594fSAndroid Build Coastguard Worker } // namespace art
200