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