blob: ffadfc61a78ff3562e009483230a8bd43ff5fc3f [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
jgu21a6da74e2014-09-10 06:57:17 -040019#include "nativebridge/native_bridge.h"
20
21#include "base/logging.h"
Andreas Gamped77ac7e2014-11-04 00:42:32 -080022#include "base/macros.h"
Calin Juravlec8423522014-08-12 20:55:20 +010023#include "mirror/art_method-inl.h"
24#include "mirror/class-inl.h"
25#include "scoped_thread_state_change.h"
26
27namespace art {
28
Andreas Gampe277ccbd2014-11-03 21:36:10 -080029static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
Calin Juravlec8423522014-08-12 20:55:20 +010030 ScopedObjectAccess soa(env);
31 StackHandleScope<1> scope(soa.Self());
32 mirror::ArtMethod* m = soa.DecodeMethod(mid);
33 MethodHelper mh(scope.NewHandle(m));
34 return mh.GetShorty();
35}
36
Andreas Gampe277ccbd2014-11-03 21:36:10 -080037static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
Calin Juravlec8423522014-08-12 20:55:20 +010038 if (clazz == nullptr)
39 return 0;
40
41 ScopedObjectAccess soa(env);
42 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
43
44 uint32_t native_method_count = 0;
45 for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
46 mirror::ArtMethod* m = c->GetDirectMethod(i);
47 if (m->IsNative()) {
48 native_method_count++;
49 }
50 }
51 for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
52 mirror::ArtMethod* m = c->GetVirtualMethod(i);
53 if (m->IsNative()) {
54 native_method_count++;
55 }
56 }
57 return native_method_count;
58}
59
Andreas Gampe277ccbd2014-11-03 21:36:10 -080060static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
61 uint32_t method_count) {
Calin Juravlec8423522014-08-12 20:55:20 +010062 if ((clazz == nullptr) || (methods == nullptr)) {
63 return 0;
64 }
65 ScopedObjectAccess soa(env);
66 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
67
68 uint32_t count = 0;
69 for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
70 mirror::ArtMethod* m = c->GetDirectMethod(i);
71 if (m->IsNative()) {
72 if (count < method_count) {
73 methods[count].name = m->GetName();
74 methods[count].signature = m->GetShorty();
Mathieu Chartier2d721012014-11-10 11:08:06 -080075 methods[count].fnPtr = m->GetEntryPointFromJni();
Calin Juravlec8423522014-08-12 20:55:20 +010076 count++;
77 } else {
78 LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m);
79 }
80 }
81 }
82 for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
83 mirror::ArtMethod* m = c->GetVirtualMethod(i);
84 if (m->IsNative()) {
85 if (count < method_count) {
86 methods[count].name = m->GetName();
87 methods[count].signature = m->GetShorty();
Mathieu Chartier2d721012014-11-10 11:08:06 -080088 methods[count].fnPtr = m->GetEntryPointFromJni();
Calin Juravlec8423522014-08-12 20:55:20 +010089 count++;
90 } else {
91 LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m);
92 }
93 }
94 }
95 return count;
96}
97
jgu21a6da74e2014-09-10 06:57:17 -040098// Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
99//
100// The interface is expected to expose the following methods:
101// getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
102// native bridge calls back to VM for the shorty of the method so that it can prepare based on
103// host calling convention.
104// getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
105// native bridge can call back to get all native methods of specified class so that all
106// corresponding trampolines can be destroyed.
107static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
108 GetMethodShorty, GetNativeMethodCount, GetNativeMethods
109};
110
Calin Juravle07d83c72014-10-22 21:02:23 +0100111bool LoadNativeBridge(std::string& native_bridge_library_filename) {
jgu21a6da74e2014-09-10 06:57:17 -0400112 VLOG(startup) << "Runtime::Setup native bridge library: "
113 << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
Calin Juravle07d83c72014-10-22 21:02:23 +0100114 return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
115 &native_bridge_art_callbacks_);
jgu21a6da74e2014-09-10 06:57:17 -0400116}
117
118void PreInitializeNativeBridge(std::string dir) {
119 VLOG(startup) << "Runtime::Pre-initialize native bridge";
Andreas Gampe7a536532014-09-25 23:13:47 -0700120#ifndef __APPLE__ // Mac OS does not support CLONE_NEWNS.
jgu21a6da74e2014-09-10 06:57:17 -0400121 if (unshare(CLONE_NEWNS) == -1) {
122 LOG(WARNING) << "Could not create mount namespace.";
jgu21a6da74e2014-09-10 06:57:17 -0400123 }
124 android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA));
Andreas Gamped77ac7e2014-11-04 00:42:32 -0800125#else
126 UNUSED(dir);
Andreas Gampe7a536532014-09-25 23:13:47 -0700127#endif
jgu21a6da74e2014-09-10 06:57:17 -0400128}
129
130void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
131 android::InitializeNativeBridge(env, instruction_set);
132}
133
134void UnloadNativeBridge() {
135 android::UnloadNativeBridge();
136}
137
Andreas Gampec8ccf682014-09-29 20:07:43 -0700138} // namespace art