2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "native_bridge_art_interface.h"
21 #include "nativebridge/native_bridge.h"
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"
33 static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
34 ScopedObjectAccess soa(env);
35 ArtMethod* m = soa.DecodeMethod(mid);
36 return m->GetShorty();
39 static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
40 if (clazz == nullptr) {
44 ScopedObjectAccess soa(env);
45 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
47 uint32_t native_method_count = 0;
48 for (auto& m : c->GetMethods(sizeof(void*))) {
49 native_method_count += m.IsNative() ? 1u : 0u;
51 return native_method_count;
54 static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
55 uint32_t method_count) {
56 if ((clazz == nullptr) || (methods == nullptr)) {
59 ScopedObjectAccess soa(env);
60 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
63 for (auto& m : c->GetMethods(sizeof(void*))) {
65 if (count < method_count) {
66 methods[count].name = m.GetName();
67 methods[count].signature = m.GetShorty();
68 methods[count].fnPtr = m.GetEntryPointFromJni();
71 LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(&m);
78 // Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
80 // The interface is expected to expose the following methods:
81 // getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
82 // native bridge calls back to VM for the shorty of the method so that it can prepare based on
83 // host calling convention.
84 // getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
85 // native bridge can call back to get all native methods of specified class so that all
86 // corresponding trampolines can be destroyed.
87 static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
88 GetMethodShorty, GetNativeMethodCount, GetNativeMethods
91 bool LoadNativeBridge(std::string& native_bridge_library_filename) {
92 VLOG(startup) << "Runtime::Setup native bridge library: "
93 << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
94 return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
95 &native_bridge_art_callbacks_);
98 void PreInitializeNativeBridge(std::string dir) {
99 VLOG(startup) << "Runtime::Pre-initialize native bridge";
100 #ifndef __APPLE__ // Mac OS does not support CLONE_NEWNS.
101 if (unshare(CLONE_NEWNS) == -1) {
102 LOG(WARNING) << "Could not create mount namespace.";
104 android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA));
110 void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
111 if (android::InitializeNativeBridge(env, instruction_set)) {
112 if (android::NativeBridgeGetVersion() >= 2U) {
113 #ifdef _NSIG // Undefined on Apple, but we don't support running on Mac, anyways.
114 // Managed signal handling support added in version 2.
115 for (int signal = 0; signal < _NSIG; ++signal) {
116 android::NativeBridgeSignalHandlerFn fn = android::NativeBridgeGetSignalHandler(signal);
118 SetSpecialSignalHandlerFn(signal, fn);
126 void UnloadNativeBridge() {
127 android::UnloadNativeBridge();