blob: 1775468688ef87bfd6c46b1814c3858a7b436c91 [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);
Calin Juravlec8423522014-08-12 20:55:20 +010031 mirror::ArtMethod* m = soa.DecodeMethod(mid);
Ian Rogerse94652f2014-12-02 11:13:19 -080032 return m->GetShorty();
Calin Juravlec8423522014-08-12 20:55:20 +010033}
34
Andreas Gampe277ccbd2014-11-03 21:36:10 -080035static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) {
Calin Juravlec8423522014-08-12 20:55:20 +010036 if (clazz == nullptr)
37 return 0;
38
39 ScopedObjectAccess soa(env);
40 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
41
42 uint32_t native_method_count = 0;
43 for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
44 mirror::ArtMethod* m = c->GetDirectMethod(i);
45 if (m->IsNative()) {
46 native_method_count++;
47 }
48 }
49 for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
50 mirror::ArtMethod* m = c->GetVirtualMethod(i);
51 if (m->IsNative()) {
52 native_method_count++;
53 }
54 }
55 return native_method_count;
56}
57
Andreas Gampe277ccbd2014-11-03 21:36:10 -080058static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
59 uint32_t method_count) {
Calin Juravlec8423522014-08-12 20:55:20 +010060 if ((clazz == nullptr) || (methods == nullptr)) {
61 return 0;
62 }
63 ScopedObjectAccess soa(env);
64 mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
65
66 uint32_t count = 0;
67 for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) {
68 mirror::ArtMethod* m = c->GetDirectMethod(i);
69 if (m->IsNative()) {
70 if (count < method_count) {
71 methods[count].name = m->GetName();
72 methods[count].signature = m->GetShorty();
Mathieu Chartier2d721012014-11-10 11:08:06 -080073 methods[count].fnPtr = m->GetEntryPointFromJni();
Calin Juravlec8423522014-08-12 20:55:20 +010074 count++;
75 } else {
76 LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m);
77 }
78 }
79 }
80 for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) {
81 mirror::ArtMethod* m = c->GetVirtualMethod(i);
82 if (m->IsNative()) {
83 if (count < method_count) {
84 methods[count].name = m->GetName();
85 methods[count].signature = m->GetShorty();
Mathieu Chartier2d721012014-11-10 11:08:06 -080086 methods[count].fnPtr = m->GetEntryPointFromJni();
Calin Juravlec8423522014-08-12 20:55:20 +010087 count++;
88 } else {
89 LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m);
90 }
91 }
92 }
93 return count;
94}
95
jgu21a6da74e2014-09-10 06:57:17 -040096// Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
97//
98// The interface is expected to expose the following methods:
99// getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
100// native bridge calls back to VM for the shorty of the method so that it can prepare based on
101// host calling convention.
102// getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
103// native bridge can call back to get all native methods of specified class so that all
104// corresponding trampolines can be destroyed.
105static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
106 GetMethodShorty, GetNativeMethodCount, GetNativeMethods
107};
108
Calin Juravle07d83c72014-10-22 21:02:23 +0100109bool LoadNativeBridge(std::string& native_bridge_library_filename) {
jgu21a6da74e2014-09-10 06:57:17 -0400110 VLOG(startup) << "Runtime::Setup native bridge library: "
111 << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
Calin Juravle07d83c72014-10-22 21:02:23 +0100112 return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
113 &native_bridge_art_callbacks_);
jgu21a6da74e2014-09-10 06:57:17 -0400114}
115
116void PreInitializeNativeBridge(std::string dir) {
117 VLOG(startup) << "Runtime::Pre-initialize native bridge";
Andreas Gampe7a536532014-09-25 23:13:47 -0700118#ifndef __APPLE__ // Mac OS does not support CLONE_NEWNS.
jgu21a6da74e2014-09-10 06:57:17 -0400119 if (unshare(CLONE_NEWNS) == -1) {
120 LOG(WARNING) << "Could not create mount namespace.";
jgu21a6da74e2014-09-10 06:57:17 -0400121 }
122 android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA));
Andreas Gamped77ac7e2014-11-04 00:42:32 -0800123#else
124 UNUSED(dir);
Andreas Gampe7a536532014-09-25 23:13:47 -0700125#endif
jgu21a6da74e2014-09-10 06:57:17 -0400126}
127
128void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) {
129 android::InitializeNativeBridge(env, instruction_set);
130}
131
132void UnloadNativeBridge() {
133 android::UnloadNativeBridge();
134}
135
Andreas Gampec8ccf682014-09-29 20:07:43 -0700136} // namespace art