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 "entrypoints/entrypoint_utils.h"
26 #include "jni/java_vm_ext.h"
27 #include "mirror/object-inl.h"
28 #include "scoped_thread_state_change-inl.h"
29 #include "thread.h"
30 
31 namespace art {
32 
33 // Used by the JNI dlsym stub to find the native method to invoke if none is registered.
artFindNativeMethodRunnable(Thread * self)34 extern "C" const void* artFindNativeMethodRunnable(Thread* self)
35     REQUIRES_SHARED(Locks::mutator_lock_) {
36   Locks::mutator_lock_->AssertSharedHeld(self);  // We come here as Runnable.
37   ArtMethod* method = self->GetCurrentMethod(nullptr);
38   DCHECK(method != nullptr);
39 
40   // Lookup symbol address for method, on failure we'll return null with an exception set,
41   // otherwise we return the address of the method we found.
42   JavaVMExt* vm = down_cast<JNIEnvExt*>(self->GetJniEnv())->GetVm();
43   void* native_code = vm->FindCodeForNativeMethod(method);
44   if (native_code == nullptr) {
45     self->AssertPendingException();
46     return nullptr;
47   }
48   // Register so that future calls don't come here
49   return method->RegisterNative(native_code);
50 }
51 
52 // Used by the JNI dlsym stub to find the native method to invoke if none is registered.
artFindNativeMethod(Thread * self)53 extern "C" const void* artFindNativeMethod(Thread* self) {
54   DCHECK_EQ(self, Thread::Current());
55   Locks::mutator_lock_->AssertNotHeld(self);  // We come here as Native.
56   ScopedObjectAccess soa(self);
57   return artFindNativeMethodRunnable(self);
58 }
59 
artCriticalNativeOutArgsSize(ArtMethod * method)60 extern "C" size_t artCriticalNativeOutArgsSize(ArtMethod* method)
61     REQUIRES_SHARED(Locks::mutator_lock_)  {
62   uint32_t shorty_len;
63   const char* shorty = method->GetShorty(&shorty_len);
64   switch (kRuntimeISA) {
65     case InstructionSet::kArm:
66     case InstructionSet::kThumb2:
67       return arm::GetCriticalNativeOutArgsSize(shorty, shorty_len);
68     case InstructionSet::kArm64:
69       return arm64::GetCriticalNativeOutArgsSize(shorty, shorty_len);
70     case InstructionSet::kX86:
71       return x86::GetCriticalNativeOutArgsSize(shorty, shorty_len);
72     case InstructionSet::kX86_64:
73       return x86_64::GetCriticalNativeOutArgsSize(shorty, shorty_len);
74     default:
75       UNIMPLEMENTED(FATAL) << kRuntimeISA;
76       UNREACHABLE();
77   }
78 }
79 
80 }  // namespace art
81