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