Integrate ART with NativeBridge interfaces
Native-bridge will provide the following interfaces to ART:
struct NativeBridgeCallbacks {
bool (*initialize )(NativeBridgeArtCallbacks* vm_itf);
void* (*loadLibrary )(const char* libpath, int flag);
void* (*getTrampoline)(void* handle, const char* name, const char* shorty,
uint32_t len);
bool (*isSupported )(const char* libpath);
};
Native-bridge will expose a symbol NativeBridgeItf with the
type of NativeBridgeCallbacks to ART.
And ART will provide the interfaces below to native-bridge:
struct NativeBridgeArtCallbacks {
int (*logger )(int prio, const char* tag, const char* fmt, ...);
const char* (*getMethodShorty)(JNIEnv* env, jmethodID mid);
int (*getNativeMethodCount )(JNIEnv* env, jclass clazz);
int (*getNativeMethods )(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
uint32_t method_count);
};
Based on the interfaces, if an ART call to dlopen fails to open a native library,
it queries the native bridge by using NativeBridgeCallbacks::isSupported(). If the
native library is supported by native-bridge, ART can load the native library
using NativeBridgeCallbacks::loadLibrary() and get a trampoline for a specific
native method using NativeBridgeCallbacks::getTrampoline(). ART can then call
the native method using the normal signature and the address of the trampoline.
On the other side, in the case of a native method calling JNI native function
CallXXXXMethodY(), native-bridge calls back to Art for the shorty of the method
using NativeBridgeArtCallbacks::getMethodShorty() so that it can prepare based
on host calling convention.
In case of JNI function RegisterNatives()/UnregisterNatives(), native bridge can
call back to NativeBridgeArtCallbacks::getNativeMethodCount() and NativeBridgeArtCallbacks
::getNativeMethods() to get all native methods of specified class so that all
corresponding trampolines can be prepared/destroyed.
Class NativeBridge is created to encapsulate the function pointers of
NativeBridgeCallbacks and provides better abstraction to ART.
Note: functionality is turned off in native_bridge.cc at the moment.
Change-Id: I652755044957a7960254648652b538cce70dd011
diff --git a/runtime/jni_internal.cc b/runtime/jni_internal.cc
index f9c7ec6..64cca3d 100644
--- a/runtime/jni_internal.cc
+++ b/runtime/jni_internal.cc
@@ -41,6 +41,7 @@
#include "mirror/object_array-inl.h"
#include "mirror/string-inl.h"
#include "mirror/throwable.h"
+#include "native_bridge.h"
#include "parsed_options.h"
#include "reflection.h"
#include "runtime.h"
@@ -362,6 +363,7 @@
SharedLibrary(const std::string& path, void* handle, mirror::Object* class_loader)
: path_(path),
handle_(handle),
+ needs_native_bridge_(false),
class_loader_(class_loader),
jni_on_load_lock_("JNI_OnLoad lock"),
jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
@@ -422,10 +424,30 @@
jni_on_load_cond_.Broadcast(self);
}
+ void SetNeedsNativeBridge() {
+ needs_native_bridge_ = true;
+ }
+
+ bool NeedsNativeBridge() const {
+ return needs_native_bridge_;
+ }
+
void* FindSymbol(const std::string& symbol_name) {
return dlsym(handle_, symbol_name.c_str());
}
+ void* FindSymbolWithNativeBridge(const std::string& symbol_name, mirror::ArtMethod* m)
+ SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+ CHECK(NeedsNativeBridge());
+
+ uint32_t len = 0;
+ const char* shorty = nullptr;
+ if (m != nullptr) {
+ shorty = m->GetShorty(&len);
+ }
+ return NativeBridge::GetTrampoline(handle_, symbol_name.c_str(), shorty, len);
+ }
+
void VisitRoots(RootCallback* visitor, void* arg) {
if (class_loader_ != nullptr) {
visitor(&class_loader_, arg, 0, kRootVMInternal);
@@ -445,6 +467,9 @@
// The void* returned by dlopen(3).
void* handle_;
+ // True if a native bridge is required.
+ bool needs_native_bridge_;
+
// The ClassLoader this library is associated with.
mirror::Object* class_loader_;
@@ -505,9 +530,17 @@
continue;
}
// Try the short name then the long name...
- void* fn = library->FindSymbol(jni_short_name);
- if (fn == nullptr) {
- fn = library->FindSymbol(jni_long_name);
+ void* fn = nullptr;
+ if (UNLIKELY(library->NeedsNativeBridge())) {
+ fn = library->FindSymbolWithNativeBridge(jni_short_name, m);
+ if (fn == nullptr) {
+ fn = library->FindSymbolWithNativeBridge(jni_long_name, m);
+ }
+ } else {
+ fn = library->FindSymbol(jni_short_name);
+ if (fn == nullptr) {
+ fn = library->FindSymbol(jni_long_name);
+ }
}
if (fn != nullptr) {
VLOG(jni) << "[Found native code for " << PrettyMethod(m)
@@ -3267,7 +3300,15 @@
// This can execute slowly for a large library on a busy system, so we
// want to switch from kRunnable while it executes. This allows the GC to ignore us.
self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
- void* handle = dlopen(path.empty() ? nullptr : path.c_str(), RTLD_LAZY);
+ const char* path_str = path.empty() ? nullptr : path.c_str();
+ void* handle = dlopen(path_str, RTLD_LAZY);
+ bool needs_native_bridge = false;
+ if (handle == nullptr) {
+ if (NativeBridge::IsSupported(path_str)) {
+ handle = NativeBridge::LoadLibrary(path_str, RTLD_LAZY);
+ needs_native_bridge = true;
+ }
+ }
self->TransitionFromSuspendedToRunnable();
VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
@@ -3300,7 +3341,14 @@
<< "]";
bool was_successful = false;
- void* sym = dlsym(handle, "JNI_OnLoad");
+ void* sym = nullptr;
+ if (UNLIKELY(needs_native_bridge)) {
+ library->SetNeedsNativeBridge();
+ sym = library->FindSymbolWithNativeBridge("JNI_OnLoad", nullptr);
+ } else {
+ sym = dlsym(handle, "JNI_OnLoad");
+ }
+
if (sym == nullptr) {
VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
was_successful = true;