0

In my project i want to use a JNI-agent to intercept results of java-static-native methods.

My basic approach is, at binding time, to rebind all the static methods to my interceptor method. The interceptor method would accept a variadic number of arguments and, when called, would forward them to the according static function.

Is it an appropriate approach? Right now in my implementation i struggle to forward variadic arguments from interceptor function to the actual function...

(c++ forward function call)

Does anyone have an idea of a better approach for this? Or am I doing something in an overcomplicated way?

Community
  • 1
  • 1
Aksim Elnik
  • 425
  • 6
  • 27

1 Answers1

0

va_arg isn't going to give you the type information of the parameters. At best you will get the parameter count and if all of your functions have a different param count (barring the JNIEnv* and jclass) then you would be able to get it to work.

If you are intercepting specific methods then you can build a table of intercept methods. Just use JavaH to generate the stubs, use renames if you wish and then build your JNINativeMethod table.

static JNINativeMethod JVM_Methods[] =
{
   // grab function names and signatures from JavaH stubs
   {"OnDeepLink",    "(Ljava/lang/String;)V",    (void*)& JNI_OnDeepLink},
   ...
};
int JVM_Methods_Count = N;

// register your overrides
jint nRes = g_env->RegisterNatives(activity, JVM_Methods, JVM_Methods_Count);

Also, I have a little trick where after I register the C functions, I'll call back into the activity/class and set a boolean that they are linked. I'm dynamically linking, so I need to check if they are set before performing the call.

James Poag
  • 2,320
  • 1
  • 13
  • 20
  • My aim is to intercept all methods, so I am rebinding all the native methods to my interceptor method. My interceptor, when called, is able to lookup the jmethod id of the originally called method... It also can find the memory address of the originally method , however it cannot call it, because that needs to be done in a generic way, which i havent found solution for yet – Aksim Elnik May 15 '17 at 14:46