blob: 155c008e85545cd63253d3ee2dd83aae136060eb [file] [log] [blame]
Calin Juravlec8423522014-08-12 20:55:20 +01001/*
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
Andreas Gampe03c2cc82015-05-22 18:31:50 -070019#include <signal.h>
20
jgu21a6da74e2014-09-10 06:57:17 -040021#include "nativebridge/native_bridge.h"
22
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
jgu21a6da74e2014-09-10 06:57:17 -040025#include "base/logging.h"
Andreas Gamped77ac7e2014-11-04 00:42:32 -080026#include "base/macros.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080027#include "dex_file-inl.h"
Calin Juravlec8423522014-08-12 20:55:20 +010028#include "mirror/class-inl.h"
29#include "scoped_thread_state_change.h"
Andreas Gampe03c2cc82015-05-22 18:31:50 -070030#include "sigchain.h"
Calin Juravlec8423522014-08-12 20:55:20 +010031
32namespace art {
33
Andreas Gampe277ccbd2014-11-03 21:36:10 -080034static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
Calin Juravlec8423522014-08-12 20:55:20 +010035 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -070036 ArtMethod* m = soa.DecodeMethod(mid);
Ian Rogerse94652f2014-12-02 11:13:19 -080037 return m->GetShorty();
Calin Juravlec8423522014-08-12 20:55:20 +010038}
39
Andreas Gampe277ccbd2014-11-03 21:36:10 -080040static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070041 if (clazz == nullptr) {
Calin Juravlec8423522014-08-12 20:55:20 +010042 return 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 }
Calin Juravlec8423522014-08-12 20:55:20 +010044
45 ScopedObjectAccess soa(env);
46 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
47
48 uint32_t native_method_count = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -070049 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070050 native_method_count += m.IsNative() ? 1u : 0u;
Calin Juravlec8423522014-08-12 20:55:20 +010051 }
52 return native_method_count;
53}
54
Andreas Gampe277ccbd2014-11-03 21:36:10 -080055static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
56 uint32_t method_count) {
Calin Juravlec8423522014-08-12 20:55:20 +010057 if ((clazz == nullptr) || (methods == nullptr)) {
58 return 0;
59 }
60 ScopedObjectAccess soa(env);
61 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
62
63 uint32_t count = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -070064 for (auto& m : c->GetMethods(kRuntimePointerSize)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 if (m.IsNative()) {
Calin Juravlec8423522014-08-12 20:55:20 +010066 if (count < method_count) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070067 methods[count].name = m.GetName();
68 methods[count].signature = m.GetShorty();
69 methods[count].fnPtr = m.GetEntryPointFromJni();
Calin Juravlec8423522014-08-12 20:55:20 +010070 count++;
71 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -070072 LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(&m);
Calin Juravlec8423522014-08-12 20:55:20 +010073 }
74 }
75 }
76 return count;
77}
78
jgu21a6da74e2014-09-10 06:57:17 -040079// Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
80//
81// The interface is expected to expose the following methods:
82// getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
83// native bridge calls back to VM for the shorty of the method so that it can prepare based on
84// host calling convention.
85// getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
86// native bridge can call back to get all native methods of specified class so that all
87// corresponding trampolines can be destroyed.
88static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
89 GetMethodShorty, GetNativeMethodCount, GetNativeMethods
90};
91
Calin Juravle07d83c72014-10-22 21:02:23 +010092bool LoadNativeBridge(std::string& native_bridge_library_filename) {
jgu21a6da74e2014-09-10 06:57:17 -040093 VLOG(startup) << "Runtime::Setup native bridge library: "
94 << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
Calin Juravle07d83c72014-10-22 21:02:23 +010095 return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
96 &native_bridge_art_callbacks_);
jgu21a6da74e2014-09-10 06:57:17 -040097}
98
99void PreInitializeNativeBridge(std::string dir) {
100 VLOG(startup) << "Runtime::Pre-initialize native bridge";
Andreas Gampe7a536532014-09-25 23:13:47 -0700101#ifndef __APPLE__ // Mac OS does not support CLONE_NEWNS.
jgu21a6da74e2014-09-10 06:57:17 -0400102 if (unshare(CLONE_NEWNS) == -1) {
103 LOG(WARNING) << "Could not create mount namespace.";
jgu21a6da74e2014-09-10 06:57:17 -0400104 }
105 android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA));
Andreas Gamped77ac7e2014-11-04 00:42:32 -0800106#else
107 UNUSED(dir);
Andreas Gampe7a536532014-09-25 23:13:47 -0700108#endif
jgu21a6da74e2014-09-10 06:57:17 -0400109}
110
111void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
Andreas Gampe03c2cc82015-05-22 18:31:50 -0700112 if (android::InitializeNativeBridge(env, instruction_set)) {
113 if (android::NativeBridgeGetVersion() >= 2U) {
114#ifdef _NSIG // Undefined on Apple, but we don't support running on Mac, anyways.
115 // Managed signal handling support added in version 2.
116 for (int signal = 0; signal < _NSIG; ++signal) {
117 android::NativeBridgeSignalHandlerFn fn = android::NativeBridgeGetSignalHandler(signal);
118 if (fn != nullptr) {
119 SetSpecialSignalHandlerFn(signal, fn);
120 }
121 }
122#endif
123 }
124 }
jgu21a6da74e2014-09-10 06:57:17 -0400125}
126
127void UnloadNativeBridge() {
128 android::UnloadNativeBridge();
129}
130
Andreas Gampec8ccf682014-09-29 20:07:43 -0700131} // namespace art