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