1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "native_bridge_art_interface.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <signal.h>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "nativebridge/native_bridge.h"
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/logging.h" // For VLOG.
25*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
27*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file-inl.h"
28*795d594fSAndroid Build Coastguard Worker #include "jni/jni_internal.h"
29*795d594fSAndroid Build Coastguard Worker #include "mirror/class-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
31*795d594fSAndroid Build Coastguard Worker #include "sigchain.h"
32*795d594fSAndroid Build Coastguard Worker
33*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
34*795d594fSAndroid Build Coastguard Worker
GetMethodShorty(JNIEnv * env,jmethodID mid)35*795d594fSAndroid Build Coastguard Worker static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
36*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(env);
37*795d594fSAndroid Build Coastguard Worker ArtMethod* m = jni::DecodeArtMethod(mid);
38*795d594fSAndroid Build Coastguard Worker return m->GetShorty();
39*795d594fSAndroid Build Coastguard Worker }
40*795d594fSAndroid Build Coastguard Worker
GetNativeMethodCount(JNIEnv * env,jclass clazz)41*795d594fSAndroid Build Coastguard Worker static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
42*795d594fSAndroid Build Coastguard Worker if (clazz == nullptr) {
43*795d594fSAndroid Build Coastguard Worker return 0;
44*795d594fSAndroid Build Coastguard Worker }
45*795d594fSAndroid Build Coastguard Worker
46*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(env);
47*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(clazz);
48*795d594fSAndroid Build Coastguard Worker
49*795d594fSAndroid Build Coastguard Worker uint32_t native_method_count = 0;
50*795d594fSAndroid Build Coastguard Worker for (auto& m : c->GetMethods(kRuntimePointerSize)) {
51*795d594fSAndroid Build Coastguard Worker native_method_count += m.IsNative() ? 1u : 0u;
52*795d594fSAndroid Build Coastguard Worker }
53*795d594fSAndroid Build Coastguard Worker return native_method_count;
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker
GetNativeMethods(JNIEnv * env,jclass clazz,JNINativeMethod * methods,uint32_t method_count)56*795d594fSAndroid Build Coastguard Worker static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
57*795d594fSAndroid Build Coastguard Worker uint32_t method_count) {
58*795d594fSAndroid Build Coastguard Worker if ((clazz == nullptr) || (methods == nullptr)) {
59*795d594fSAndroid Build Coastguard Worker return 0;
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker ScopedObjectAccess soa(env);
62*795d594fSAndroid Build Coastguard Worker ObjPtr<mirror::Class> c = soa.Decode<mirror::Class>(clazz);
63*795d594fSAndroid Build Coastguard Worker
64*795d594fSAndroid Build Coastguard Worker uint32_t count = 0;
65*795d594fSAndroid Build Coastguard Worker for (auto& m : c->GetMethods(kRuntimePointerSize)) {
66*795d594fSAndroid Build Coastguard Worker if (m.IsNative()) {
67*795d594fSAndroid Build Coastguard Worker if (count < method_count) {
68*795d594fSAndroid Build Coastguard Worker methods[count].name = m.GetName();
69*795d594fSAndroid Build Coastguard Worker methods[count].signature = m.GetShorty();
70*795d594fSAndroid Build Coastguard Worker methods[count].fnPtr = m.GetEntryPointFromJni();
71*795d594fSAndroid Build Coastguard Worker count++;
72*795d594fSAndroid Build Coastguard Worker } else {
73*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Output native method array too small. Skipping "
74*795d594fSAndroid Build Coastguard Worker << m.PrettyMethod();
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker return count;
79*795d594fSAndroid Build Coastguard Worker }
80*795d594fSAndroid Build Coastguard Worker
81*795d594fSAndroid Build Coastguard Worker // Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
82*795d594fSAndroid Build Coastguard Worker //
83*795d594fSAndroid Build Coastguard Worker // The interface is expected to expose the following methods:
84*795d594fSAndroid Build Coastguard Worker // getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
85*795d594fSAndroid Build Coastguard Worker // native bridge calls back to VM for the shorty of the method so that it can prepare based on
86*795d594fSAndroid Build Coastguard Worker // host calling convention.
87*795d594fSAndroid Build Coastguard Worker // getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
88*795d594fSAndroid Build Coastguard Worker // native bridge can call back to get all native methods of specified class so that all
89*795d594fSAndroid Build Coastguard Worker // corresponding trampolines can be destroyed.
90*795d594fSAndroid Build Coastguard Worker static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
91*795d594fSAndroid Build Coastguard Worker GetMethodShorty, GetNativeMethodCount, GetNativeMethods
92*795d594fSAndroid Build Coastguard Worker };
93*795d594fSAndroid Build Coastguard Worker
LoadNativeBridge(const std::string & native_bridge_library_filename)94*795d594fSAndroid Build Coastguard Worker bool LoadNativeBridge(const std::string& native_bridge_library_filename) {
95*795d594fSAndroid Build Coastguard Worker VLOG(startup) << "Runtime::Setup native bridge library: "
96*795d594fSAndroid Build Coastguard Worker << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
97*795d594fSAndroid Build Coastguard Worker return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
98*795d594fSAndroid Build Coastguard Worker &native_bridge_art_callbacks_);
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker
PreInitializeNativeBridge(const std::string & dir)101*795d594fSAndroid Build Coastguard Worker void PreInitializeNativeBridge(const std::string& dir) {
102*795d594fSAndroid Build Coastguard Worker VLOG(startup) << "Runtime::Pre-initialize native bridge";
103*795d594fSAndroid Build Coastguard Worker #ifndef __APPLE__ // Mac OS does not support CLONE_NEWNS.
104*795d594fSAndroid Build Coastguard Worker if (unshare(CLONE_NEWNS) == -1) {
105*795d594fSAndroid Build Coastguard Worker LOG(WARNING) << "Could not create mount namespace.";
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA));
108*795d594fSAndroid Build Coastguard Worker #else
109*795d594fSAndroid Build Coastguard Worker UNUSED(dir);
110*795d594fSAndroid Build Coastguard Worker #endif
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker
PreZygoteForkNativeBridge()113*795d594fSAndroid Build Coastguard Worker void PreZygoteForkNativeBridge() {
114*795d594fSAndroid Build Coastguard Worker android::PreZygoteForkNativeBridge();
115*795d594fSAndroid Build Coastguard Worker }
116*795d594fSAndroid Build Coastguard Worker
InitializeNativeBridge(JNIEnv * env,const char * instruction_set)117*795d594fSAndroid Build Coastguard Worker void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
118*795d594fSAndroid Build Coastguard Worker if (android::NativeBridgeInitialized()) {
119*795d594fSAndroid Build Coastguard Worker // This happens in apps forked from app-zygote, since native bridge
120*795d594fSAndroid Build Coastguard Worker // is initialized in the zygote.
121*795d594fSAndroid Build Coastguard Worker return;
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker if (android::InitializeNativeBridge(env, instruction_set)) {
124*795d594fSAndroid Build Coastguard Worker if (android::NativeBridgeGetVersion() >= 2U) {
125*795d594fSAndroid Build Coastguard Worker #ifdef _NSIG // Undefined on Apple, but we don't support running on Mac, anyways.
126*795d594fSAndroid Build Coastguard Worker // Managed signal handling support added in version 2.
127*795d594fSAndroid Build Coastguard Worker for (int signal = 0; signal < _NSIG; ++signal) {
128*795d594fSAndroid Build Coastguard Worker android::NativeBridgeSignalHandlerFn fn = android::NativeBridgeGetSignalHandler(signal);
129*795d594fSAndroid Build Coastguard Worker if (fn != nullptr) {
130*795d594fSAndroid Build Coastguard Worker sigset_t mask;
131*795d594fSAndroid Build Coastguard Worker sigfillset(&mask);
132*795d594fSAndroid Build Coastguard Worker SigchainAction sa = {
133*795d594fSAndroid Build Coastguard Worker .sc_sigaction = fn,
134*795d594fSAndroid Build Coastguard Worker .sc_mask = mask,
135*795d594fSAndroid Build Coastguard Worker // The native bridge signal might not return back to sigchain's handler.
136*795d594fSAndroid Build Coastguard Worker .sc_flags = SIGCHAIN_ALLOW_NORETURN,
137*795d594fSAndroid Build Coastguard Worker };
138*795d594fSAndroid Build Coastguard Worker AddSpecialSignalHandlerFn(signal, &sa);
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker #endif
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker }
145*795d594fSAndroid Build Coastguard Worker
UnloadNativeBridge()146*795d594fSAndroid Build Coastguard Worker void UnloadNativeBridge() {
147*795d594fSAndroid Build Coastguard Worker android::UnloadNativeBridge();
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker
150*795d594fSAndroid Build Coastguard Worker } // namespace art
151