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/Android.mk b/runtime/Android.mk
index 6fa34c4..17d3fce 100644
--- a/runtime/Android.mk
+++ b/runtime/Android.mk
@@ -104,6 +104,7 @@
   mirror/string.cc \
   mirror/throwable.cc \
   monitor.cc \
+  native_bridge.cc \
   native/dalvik_system_DexFile.cc \
   native/dalvik_system_VMDebug.cc \
   native/dalvik_system_VMRuntime.cc \
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;
diff --git a/runtime/native_bridge.cc b/runtime/native_bridge.cc
new file mode 100644
index 0000000..de04a99
--- /dev/null
+++ b/runtime/native_bridge.cc
@@ -0,0 +1,260 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "native_bridge.h"
+
+#include <dlfcn.h>
+#include <stdio.h>
+#include "jni.h"
+
+#include "base/mutex.h"
+#include "mirror/art_method-inl.h"
+#include "mirror/class-inl.h"
+#include "scoped_thread_state_change.h"
+#include "ScopedLocalRef.h"
+#include "thread.h"
+
+#ifdef HAVE_ANDROID_OS
+#include "cutils/properties.h"
+#endif
+
+
+namespace art {
+
+// Is native-bridge support enabled?
+static constexpr bool kNativeBridgeEnabled = false;
+
+// Default library name for native-bridge.
+static constexpr const char* kDefaultNativeBridge = "libnativebridge.so";
+
+#ifdef HAVE_ANDROID_OS
+// TODO: This will be removed once we have native-bridge command-line arguments.
+
+// Property that defines the library name of native-bridge.
+static constexpr const char* kPropNativeBridge = "persist.native.bridge";
+
+// Property that enables native-bridge.
+static constexpr const char* kPropEnableNativeBridge = "persist.enable.native.bridge";
+#endif
+
+// The symbol name exposed by native-bridge with the type of NativeBridgeCallbacks.
+static constexpr const char* kNativeBridgeInterfaceSymbol = "NativeBridgeItf";
+
+// ART interfaces to native-bridge.
+struct NativeBridgeArtCallbacks {
+  // Log utility, reserve unused.
+  int (*logger)(int prio, const char* tag, const char* fmt, ...);
+
+  // Get shorty of a Java method. The shorty is supposed to be persistent in memory.
+  //
+  // Parameters:
+  //   env [IN] pointer to JNIenv.
+  //   mid [IN] Java methodID.
+  // Returns:
+  //   short descriptor for method.
+  const char* (*getMethodShorty)(JNIEnv* env, jmethodID mid);
+
+  // Get number of native methods for specified class.
+  //
+  // Parameters:
+  //   env [IN] pointer to JNIenv.
+  //   clazz [IN] Java class object.
+  // Returns:
+  //   number of native methods.
+  int (*getNativeMethodCount)(JNIEnv* env, jclass clazz);
+
+  // Get at most 'method_count' native methods for specified class 'clazz'. Results are outputed
+  // via 'methods' [OUT]. The signature pointer in JNINativeMethod is reused as the method shorty.
+  //
+  // Parameters:
+  //   env [IN] pointer to JNIenv.
+  //   clazz [IN] Java class object.
+  //   methods [OUT] array of method with the name, shorty, and fnPtr.
+  //   method_count [IN] max number of elements in methods.
+  // Returns:
+  //   number of method it actually wrote to methods.
+  int (*getNativeMethods)(JNIEnv* env, jclass clazz, JNINativeMethod* methods, uint32_t method_count);
+};
+
+// Native-bridge interfaces to ART
+struct NativeBridgeCallbacks {
+  // Initialize native-bridge. Native-bridge's internal implementation must ensure MT safety and
+  // that the native-bridge is initialized only once. Thus it is OK to call this interface for an
+  // already initialized native-bridge.
+  //
+  // Parameters:
+  //   art_cbs [IN] the pointer to NativeBridgeArtCallbacks.
+  // Returns:
+  //   true iff initialization was successful.
+  bool (*initialize)(NativeBridgeArtCallbacks* art_cbs);
+
+  // Load a shared library that is supported by the native-bridge.
+  //
+  // Parameters:
+  //   libpath [IN] path to the shared library
+  //   flag [IN] the stardard RTLD_XXX defined in bionic dlfcn.h
+  // Returns:
+  //   The opaque handle of the shared library if sucessful, otherwise NULL
+  void* (*loadLibrary)(const char* libpath, int flag);
+
+  // Get a native-bridge trampoline for specified native method. The trampoline has same
+  // sigature as the native method.
+  //
+  // Parameters:
+  //   handle [IN] the handle returned from loadLibrary
+  //   shorty [IN] short descriptor of native method
+  //   len [IN] length of shorty
+  // Returns:
+  //   address of trampoline if successful, otherwise NULL
+  void* (*getTrampoline)(void* handle, const char* name, const char* shorty, uint32_t len);
+
+  // Check whether native library is valid and is for an ABI that is supported by native-bridge.
+  //
+  // Parameters:
+  //   libpath [IN] path to the shared library
+  // Returns:
+  //   TRUE if library is supported by native-bridge, FALSE otherwise
+  bool (*isSupported)(const char* libpath);
+};
+
+static const char* GetMethodShorty(JNIEnv* env, jmethodID mid) {
+  ScopedObjectAccess soa(env);
+  StackHandleScope<1> scope(soa.Self());
+  mirror::ArtMethod* m = soa.DecodeMethod(mid);
+  MethodHelper mh(scope.NewHandle(m));
+  return mh.GetShorty();
+}
+
+static int GetNativeMethodCount(JNIEnv* env, jclass clazz) {
+  if (clazz == nullptr)
+    return 0;
+
+  ScopedObjectAccess soa(env);
+  mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
+
+  size_t method_count = 0;
+  for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
+    mirror::ArtMethod* m = c->GetDirectMethod(i);
+    if (m->IsNative())
+      method_count++;
+  }
+  for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
+    mirror::ArtMethod* m = c->GetVirtualMethod(i);
+    if (m->IsNative())
+      method_count++;
+  }
+  return method_count;
+}
+
+static int GetNativeMethods(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
+                            uint32_t method_count) {
+  if ((clazz == nullptr) || (methods == nullptr))
+    return 0;
+
+  ScopedObjectAccess soa(env);
+  mirror::Class* c = soa.Decode<mirror::Class*>(clazz);
+
+  size_t count = 0;
+  for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
+    mirror::ArtMethod* m = c->GetDirectMethod(i);
+    if (m->IsNative() && count < method_count) {
+      methods[count].name = m->GetName();
+      methods[count].signature = m->GetShorty();
+      methods[count].fnPtr = const_cast<void*>(m->GetNativeMethod());
+      count++;
+    }
+  }
+  for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
+    mirror::ArtMethod* m = c->GetVirtualMethod(i);
+    if (m->IsNative() && count < method_count) {
+      methods[count].name = m->GetName();
+      methods[count].signature = m->GetShorty();
+      methods[count].fnPtr = const_cast<void*>(m->GetNativeMethod());
+      count++;
+    }
+  }
+  return count;
+}
+
+NativeBridgeArtCallbacks NativeBridgeArtItf = {
+  nullptr,
+  GetMethodShorty,
+  GetNativeMethodCount,
+  GetNativeMethods
+};
+
+bool NativeBridge::Init() {
+  if (!kNativeBridgeEnabled) {
+    return false;
+  }
+
+  MutexLock mu(Thread::Current(), lock_);
+
+  if (!initialized_) {
+    const char* libnb_path = kDefaultNativeBridge;
+#ifdef HAVE_ANDROID_OS
+    char prop_buf[PROP_VALUE_MAX];
+    property_get(kPropEnableNativeBridge, prop_buf, "false");
+    if (strcmp(prop_buf, "true") != 0)
+      return false;
+
+    // If prop persist.native.bridge set, overwrite the default name.
+    int name_len = property_get(kPropNativeBridge, prop_buf, kDefaultNativeBridge);
+    if (name_len > 0)
+      libnb_path = prop_buf;
+#endif
+    void* handle = dlopen(libnb_path, RTLD_LAZY);
+    if (handle == nullptr)
+      return false;
+
+    callbacks_ = reinterpret_cast<NativeBridgeCallbacks*>(dlsym(handle,
+                                                                kNativeBridgeInterfaceSymbol));
+    if (callbacks_ == nullptr) {
+      dlclose(handle);
+      return false;
+    }
+
+    callbacks_->initialize(&NativeBridgeArtItf);
+    initialized_ = true;
+  }
+
+  return initialized_;
+}
+
+void* NativeBridge::LoadLibrary(const char* libpath, int flag) {
+  if (Init())
+    return callbacks_->loadLibrary(libpath, flag);
+  return nullptr;
+}
+
+void* NativeBridge::GetTrampoline(void* handle, const char* name, const char* shorty,
+                                  uint32_t len) {
+  if (Init())
+    return callbacks_->getTrampoline(handle, name, shorty, len);
+  return nullptr;
+}
+
+bool NativeBridge::IsSupported(const char* libpath) {
+  if (Init())
+    return callbacks_->isSupported(libpath);
+  return false;
+}
+
+bool NativeBridge::initialized_ = false;
+Mutex NativeBridge::lock_("native bridge lock");
+NativeBridgeCallbacks* NativeBridge::callbacks_ = nullptr;
+
+};  // namespace art
diff --git a/runtime/native_bridge.h b/runtime/native_bridge.h
new file mode 100644
index 0000000..dd895d2
--- /dev/null
+++ b/runtime/native_bridge.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_RUNTIME_NATIVE_BRIDGE_H_
+#define ART_RUNTIME_NATIVE_BRIDGE_H_
+
+#include "base/mutex.h"
+
+namespace art {
+
+struct NativeBridgeCallbacks;
+
+class NativeBridge {
+ public:
+  // Load a shared library that is supported by the native-bridge.
+  static void* LoadLibrary(const char* libpath, int flag);
+  // Get a native-bridge trampoline for specified native method.
+  static void* GetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len);
+  // True if native library is valid and is for an ABI that is supported by native-bridge.
+  static bool  IsSupported(const char* libpath);
+
+ private:
+  static bool  Init();
+  static bool  initialized_ GUARDED_BY(lock_);
+  static Mutex lock_;
+  static NativeBridgeCallbacks* callbacks_;
+};
+
+};  // namespace art
+
+#endif  // ART_RUNTIME_NATIVE_BRIDGE_H_