Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame^] | 19 | #include <signal.h> |
| 20 | |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 21 | #include "nativebridge/native_bridge.h" |
| 22 | |
| 23 | #include "base/logging.h" |
Andreas Gampe | d77ac7e | 2014-11-04 00:42:32 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 26 | #include "mirror/art_method-inl.h" |
| 27 | #include "mirror/class-inl.h" |
| 28 | #include "scoped_thread_state_change.h" |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame^] | 29 | #include "sigchain.h" |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 33 | static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) { |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 34 | ScopedObjectAccess soa(env); |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 35 | mirror::ArtMethod* m = soa.DecodeMethod(mid); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 36 | return m->GetShorty(); |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 39 | static uint32_t GetNativeMethodCount(JNIEnv* env, jclass clazz) { |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 40 | if (clazz == nullptr) |
| 41 | return 0; |
| 42 | |
| 43 | ScopedObjectAccess soa(env); |
| 44 | mirror::Class* c = soa.Decode<mirror::Class*>(clazz); |
| 45 | |
| 46 | uint32_t native_method_count = 0; |
| 47 | for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 48 | mirror::ArtMethod* m = c->GetDirectMethod(i); |
| 49 | if (m->IsNative()) { |
| 50 | native_method_count++; |
| 51 | } |
| 52 | } |
| 53 | for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 54 | mirror::ArtMethod* m = c->GetVirtualMethod(i); |
| 55 | if (m->IsNative()) { |
| 56 | native_method_count++; |
| 57 | } |
| 58 | } |
| 59 | return native_method_count; |
| 60 | } |
| 61 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 62 | static uint32_t GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods, |
| 63 | uint32_t method_count) { |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 64 | if ((clazz == nullptr) || (methods == nullptr)) { |
| 65 | return 0; |
| 66 | } |
| 67 | ScopedObjectAccess soa(env); |
| 68 | mirror::Class* c = soa.Decode<mirror::Class*>(clazz); |
| 69 | |
| 70 | uint32_t count = 0; |
| 71 | for (uint32_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 72 | mirror::ArtMethod* m = c->GetDirectMethod(i); |
| 73 | if (m->IsNative()) { |
| 74 | if (count < method_count) { |
| 75 | methods[count].name = m->GetName(); |
| 76 | methods[count].signature = m->GetShorty(); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 77 | methods[count].fnPtr = m->GetEntryPointFromJni(); |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 78 | count++; |
| 79 | } else { |
| 80 | LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | for (uint32_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 85 | mirror::ArtMethod* m = c->GetVirtualMethod(i); |
| 86 | if (m->IsNative()) { |
| 87 | if (count < method_count) { |
| 88 | methods[count].name = m->GetName(); |
| 89 | methods[count].signature = m->GetShorty(); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 90 | methods[count].fnPtr = m->GetEntryPointFromJni(); |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 91 | count++; |
| 92 | } else { |
| 93 | LOG(WARNING) << "Output native method array too small. Skipping " << PrettyMethod(m); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return count; |
| 98 | } |
| 99 | |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 100 | // Native bridge library runtime callbacks. They represent the runtime interface to native bridge. |
| 101 | // |
| 102 | // The interface is expected to expose the following methods: |
| 103 | // getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(), |
| 104 | // native bridge calls back to VM for the shorty of the method so that it can prepare based on |
| 105 | // host calling convention. |
| 106 | // getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(), |
| 107 | // native bridge can call back to get all native methods of specified class so that all |
| 108 | // corresponding trampolines can be destroyed. |
| 109 | static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ { |
| 110 | GetMethodShorty, GetNativeMethodCount, GetNativeMethods |
| 111 | }; |
| 112 | |
Calin Juravle | 07d83c7 | 2014-10-22 21:02:23 +0100 | [diff] [blame] | 113 | bool LoadNativeBridge(std::string& native_bridge_library_filename) { |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 114 | VLOG(startup) << "Runtime::Setup native bridge library: " |
| 115 | << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename); |
Calin Juravle | 07d83c7 | 2014-10-22 21:02:23 +0100 | [diff] [blame] | 116 | return android::LoadNativeBridge(native_bridge_library_filename.c_str(), |
| 117 | &native_bridge_art_callbacks_); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void PreInitializeNativeBridge(std::string dir) { |
| 121 | VLOG(startup) << "Runtime::Pre-initialize native bridge"; |
Andreas Gampe | 7a53653 | 2014-09-25 23:13:47 -0700 | [diff] [blame] | 122 | #ifndef __APPLE__ // Mac OS does not support CLONE_NEWNS. |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 123 | if (unshare(CLONE_NEWNS) == -1) { |
| 124 | LOG(WARNING) << "Could not create mount namespace."; |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 125 | } |
| 126 | android::PreInitializeNativeBridge(dir.c_str(), GetInstructionSetString(kRuntimeISA)); |
Andreas Gampe | d77ac7e | 2014-11-04 00:42:32 -0800 | [diff] [blame] | 127 | #else |
| 128 | UNUSED(dir); |
Andreas Gampe | 7a53653 | 2014-09-25 23:13:47 -0700 | [diff] [blame] | 129 | #endif |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void InitializeNativeBridge(JNIEnv* env, const char* instruction_set) { |
Andreas Gampe | 03c2cc8 | 2015-05-22 18:31:50 -0700 | [diff] [blame^] | 133 | if (android::InitializeNativeBridge(env, instruction_set)) { |
| 134 | if (android::NativeBridgeGetVersion() >= 2U) { |
| 135 | #ifdef _NSIG // Undefined on Apple, but we don't support running on Mac, anyways. |
| 136 | // Managed signal handling support added in version 2. |
| 137 | for (int signal = 0; signal < _NSIG; ++signal) { |
| 138 | android::NativeBridgeSignalHandlerFn fn = android::NativeBridgeGetSignalHandler(signal); |
| 139 | if (fn != nullptr) { |
| 140 | SetSpecialSignalHandlerFn(signal, fn); |
| 141 | } |
| 142 | } |
| 143 | #endif |
| 144 | } |
| 145 | } |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void UnloadNativeBridge() { |
| 149 | android::UnloadNativeBridge(); |
| 150 | } |
| 151 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 152 | } // namespace art |