1 /*
2  * Copyright (C) 2014 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 "native_bridge_art_interface.h"
18 
19 #include <signal.h>
20 
21 #include "nativebridge/native_bridge.h"
22 
23 #include "art_method-inl.h"
24 #include "base/logging.h"
25 #include "base/macros.h"
26 #include "dex_file-inl.h"
27 #include "mirror/class-inl.h"
28 #include "scoped_thread_state_change.h"
29 #include "sigchain.h"
30 
31 namespace art {
32 
GetMethodShorty(JNIEnv * env,jmethodID mid)33 static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
34   ScopedObjectAccess soa(env);
35   ArtMethod* m = soa.DecodeMethod(mid);
36   return m->GetShorty();
37 }
38 
GetNativeMethodCount(JNIEnv * env,jclass clazz)39 static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
40   if (clazz == nullptr) {
41     return 0;
42   }
43 
44   ScopedObjectAccess soa(env);
45   mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
46 
47   uint32_t native_method_count = 0;
48   for (auto& m : c->GetDirectMethods(sizeof(void*))) {
49     native_method_count += m.IsNative() ? 1u : 0u;
50   }
51   for (auto& m : c->GetVirtualMethods(sizeof(void*))) {
52     native_method_count += m.IsNative() ? 1u : 0u;
53   }
54   return native_method_count;
55 }
56 
GetNativeMethods(JNIEnv * env,jclass clazz,JNINativeMethod * methods,uint32_t method_count)57 static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
58                                  uint32_t method_count) {
59   if ((clazz == nullptr) || (methods == nullptr)) {
60     return 0;
61   }
62   ScopedObjectAccess soa(env);
63   mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
64 
65   uint32_t count = 0;
66   for (auto& m : c->GetDirectMethods(sizeof(void*))) {
67     if (m.IsNative()) {
68       if (count < method_count) {
69         methods[count].name = m.GetName();
70         methods[count].signature = m.GetShorty();
71         methods[count].fnPtr = m.GetEntryPointFromJni();
72         count++;
73       } else {
74         LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(&m);
75       }
76     }
77   }
78   for (auto& m : c->GetVirtualMethods(sizeof(void*))) {
79     if (m.IsNative()) {
80       if (count < method_count) {
81         methods[count].name = m.GetName();
82         methods[count].signature = m.GetShorty();
83         methods[count].fnPtr = m.GetEntryPointFromJni();
84         count++;
85       } else {
86         LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(&m);
87       }
88     }
89   }
90   return count;
91 }
92 
93 // Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
94 //
95 // The interface is expected to expose the following methods:
96 // getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
97 //   native bridge calls back to VM for the shorty of the method so that it can prepare based on
98 //   host calling convention.
99 // getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
100 //   native bridge can call back to get all native methods of specified class so that all
101 //   corresponding trampolines can be destroyed.
102 static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
103   GetMethodShorty, GetNativeMethodCount, GetNativeMethods
104 };
105 
LoadNativeBridge(std::string & native_bridge_library_filename)106 bool LoadNativeBridge(std::string& native_bridge_library_filename) {
107   VLOG(startup) << "Runtime::Setup native bridge library: "
108       << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
109   return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
110                                    &native_bridge_art_callbacks_);
111 }
112 
PreInitializeNativeBridge(std::string dir)113 void PreInitializeNativeBridge(std::string dir) {
114   VLOG(startup) << "Runtime::Pre-initialize native bridge";
115 #ifndef __APPLE__  // Mac OS does not support CLONE_NEWNS.
116   if (unshare(CLONE_NEWNS) == -1) {
117     LOG(WARNING) << "Could not create mount namespace.";
118   }
119   android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA));
120 #else
121   UNUSED(dir);
122 #endif
123 }
124 
InitializeNativeBridge(JNIEnv * env,const char * instruction_set)125 void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
126   if (android::InitializeNativeBridge(env, instruction_set)) {
127     if (android::NativeBridgeGetVersion() >= 2U) {
128 #ifdef _NSIG  // Undefined on Apple, but we don't support running on Mac, anyways.
129       // Managed signal handling support added in version 2.
130       for (int signal = 0; signal < _NSIG; ++signal) {
131         android::NativeBridgeSignalHandlerFn fn = android::NativeBridgeGetSignalHandler(signal);
132         if (fn != nullptr) {
133           SetSpecialSignalHandlerFn(signal, fn);
134         }
135       }
136 #endif
137     }
138   }
139 }
140 
UnloadNativeBridge()141 void UnloadNativeBridge() {
142   android::UnloadNativeBridge();
143 }
144 
145 }  // namespace art
146