• 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,
73                               MethodReference(method->GetDexFile(), method_idx),
74                               GetCalleeSaveOuterMethod(self, CalleeSaveType::kSaveRefsAndArgs));
75 
76     // These calls do not have an explicit class initialization check, so do the check now.
77     // (When going through the stub or GenericJNI, the check was already done.)
78     DCHECK(target_method->NeedsClinitCheckBeforeCall());
79     ObjPtr<mirror::Class> declaring_class = target_method->GetDeclaringClass();
80     if (UNLIKELY(!declaring_class->IsVisiblyInitialized())) {
81       StackHandleScope<1> hs(self);
82       Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
83       if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
84         DCHECK(self->IsExceptionPending()) << method->PrettyMethod();
85         return nullptr;
86       }
87     }
88 
89     // Replace the runtime method on the stack with the target method.
90     DCHECK(!self->GetManagedStack()->GetTopQuickFrameGenericJniTag());
91     ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrameKnownNotTagged();
92     DCHECK(*sp == Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
93     *sp = target_method;
94     self->SetTopOfStackGenericJniTagged(sp);  // Fake GenericJNI frame.
95 
96     // Continue with the target method.
97     method = target_method;
98   }
99   DCHECK(method == self->GetCurrentMethod(/*dex_pc=*/ nullptr));
100 
101   // Check whether we already have a registered native code.
102   // For @CriticalNative it may not be stored in the ArtMethod as a JNI entrypoint if the class
103   // was not visibly initialized yet. Do this check also for @FastNative and normal native for
104   // consistency; though success would mean that another thread raced to do this lookup.
105   const void* native_code = class_linker->GetRegisteredNative(self, method);
106   if (native_code != nullptr) {
107     return native_code;
108   }
109 
110   // Lookup symbol address for method, on failure we'll return null with an exception set,
111   // otherwise we return the address of the method we found.
112   JavaVMExt* vm = down_cast<JNIEnvExt*>(self->GetJniEnv())->GetVm();
113   std::string error_msg;
114   native_code = vm->FindCodeForNativeMethod(method, &error_msg, /*can_suspend=*/ true);
115   if (native_code == nullptr) {
116     LOG(ERROR) << error_msg;
117     self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", error_msg.c_str());
118     return nullptr;
119   }
120 
121   // Register the code. This usually prevents future calls from coming to this function again.
122   // We can still come here if the ClassLinker cannot set the entrypoint in the ArtMethod,
123   // i.e. for @CriticalNative methods with the declaring class not visibly initialized.
124   return class_linker->RegisterNative(self, method, native_code);
125 }
126 
127 // Used by the JNI dlsym stub to find the native method to invoke if none is registered.
artFindNativeMethod(Thread * self)128 extern "C" const void* artFindNativeMethod(Thread* self) {
129   DCHECK_EQ(self, Thread::Current());
130   Locks::mutator_lock_->AssertNotHeld(self);  // We come here as Native.
131   ScopedObjectAccess soa(self);
132   return artFindNativeMethodRunnable(self);
133 }
134 
artCriticalNativeFrameSize(ArtMethod * method,uintptr_t caller_pc)135 extern "C" size_t artCriticalNativeFrameSize(ArtMethod* method, uintptr_t caller_pc)
136     REQUIRES_SHARED(Locks::mutator_lock_)  {
137   if (method->IsNative()) {
138     // Get the method's shorty.
139     DCHECK(method->IsCriticalNative());
140     uint32_t shorty_len;
141     const char* shorty = method->GetShorty(&shorty_len);
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, shorty_len);
148       case InstructionSet::kArm64:
149         return arm64::GetCriticalNativeStubFrameSize(shorty, shorty_len);
150       case InstructionSet::kX86:
151         return x86::GetCriticalNativeStubFrameSize(shorty, shorty_len);
152       case InstructionSet::kX86_64:
153         return x86_64::GetCriticalNativeStubFrameSize(shorty, shorty_len);
154       default:
155         UNIMPLEMENTED(FATAL) << kRuntimeISA;
156         UNREACHABLE();
157     }
158   } else {
159     // We're coming from compiled managed code and the `method` we see here is the compiled
160     // method that made the call. Get the actual caller (may be inlined) and dex pc.
161     const OatQuickMethodHeader* current_code = method->GetOatQuickMethodHeader(caller_pc);
162     DCHECK(current_code != nullptr);
163     DCHECK(current_code->IsOptimized());
164     uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
165     CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(current_code);
166     StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
167     DCHECK(stack_map.IsValid());
168     BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
169     ArtMethod* caller =
170         inline_infos.empty() ? method : GetResolvedMethod(method, code_info, inline_infos);
171     uint32_t dex_pc = inline_infos.empty() ? stack_map.GetDexPc() : inline_infos.back().GetDexPc();
172 
173     // Get the callee shorty.
174     const DexFile* dex_file = caller->GetDexFile();
175     uint32_t method_idx = GetInvokeStaticMethodIndex(caller, dex_pc);
176     uint32_t shorty_len;
177     const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
178 
179     // Return the platform-dependent direct call frame size.
180     switch (kRuntimeISA) {
181       case InstructionSet::kArm:
182       case InstructionSet::kThumb2:
183         return arm::GetCriticalNativeDirectCallFrameSize(shorty, shorty_len);
184       case InstructionSet::kArm64:
185         return arm64::GetCriticalNativeDirectCallFrameSize(shorty, shorty_len);
186       case InstructionSet::kX86:
187         return x86::GetCriticalNativeDirectCallFrameSize(shorty, shorty_len);
188       case InstructionSet::kX86_64:
189         return x86_64::GetCriticalNativeDirectCallFrameSize(shorty, shorty_len);
190       default:
191         UNIMPLEMENTED(FATAL) << kRuntimeISA;
192         UNREACHABLE();
193     }
194   }
195 }
196 
197 }  // namespace art
198