From 65af20b1aaa2b23abaae3e4a21d7b6cdcb156889 Mon Sep 17 00:00:00 2001 From: Alex Light Date: Thu, 20 Apr 2017 09:15:08 -0700 Subject: Agent libraries need to be searched for JNI functions This makes agent libraries the option of last resort for native method implementations. This will allow one to not need to manually link all native methods in an agent library. Bug: 37522517 Bug: 37432636 Test: ./test.py --host -j40 Change-Id: I5ad78a15e7e2799d2a877c5d603342899e2a1bd1 --- runtime/java_vm_ext.cc | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'runtime/java_vm_ext.cc') diff --git a/runtime/java_vm_ext.cc b/runtime/java_vm_ext.cc index b93b8f2a97..c8dc2f2d20 100644 --- a/runtime/java_vm_ext.cc +++ b/runtime/java_vm_ext.cc @@ -40,6 +40,7 @@ #include "ScopedLocalRef.h" #include "scoped_thread_state_change-inl.h" #include "sigchain.h" +#include "ti/agent.h" #include "thread-inl.h" #include "thread_list.h" @@ -268,7 +269,6 @@ class Libraries { detail += "No implementation found for "; detail += m->PrettyMethod(); detail += " (tried " + jni_short_name + " and " + jni_long_name + ")"; - LOG(ERROR) << detail; return nullptr; } @@ -929,6 +929,26 @@ bool JavaVMExt::LoadNativeLibrary(JNIEnv* env, return was_successful; } +static void* FindCodeForNativeMethodInAgents(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) { + std::string jni_short_name(m->JniShortName()); + std::string jni_long_name(m->JniLongName()); + for (const ti::Agent& agent : Runtime::Current()->GetAgents()) { + void* fn = agent.FindSymbol(jni_short_name); + if (fn != nullptr) { + VLOG(jni) << "Found implementation for " << m->PrettyMethod() + << " (symbol: " << jni_short_name << ") in " << agent; + return fn; + } + fn = agent.FindSymbol(jni_long_name); + if (fn != nullptr) { + VLOG(jni) << "Found implementation for " << m->PrettyMethod() + << " (symbol: " << jni_long_name << ") in " << agent; + return fn; + } + } + return nullptr; +} + void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) { CHECK(m->IsNative()); mirror::Class* c = m->GetDeclaringClass(); @@ -941,8 +961,14 @@ void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) { MutexLock mu(self, *Locks::jni_libraries_lock_); native_method = libraries_->FindNativeMethod(m, detail); } + if (native_method == nullptr) { + // Lookup JNI native methods from native TI Agent libraries. See runtime/ti/agent.h for more + // information. Agent libraries are searched for native methods after all jni libraries. + native_method = FindCodeForNativeMethodInAgents(m); + } // Throwing can cause libraries_lock to be reacquired. if (native_method == nullptr) { + LOG(ERROR) << detail; self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str()); } return native_method; -- cgit v1.2.3-59-g8ed1b