Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missmatch between the method signature and actual call

When using the java native interface on android I made two silly mistakes that cost me a lot of time.

Having this method id:

jmethodID myMethod_methodID = env->GetMethodID(hello_Cls, "myMethod", "(ILjava/lang/String;Ljava/lang/String;I)Z");

My first mistake was calling it using

env->CallVoidMethod

and my second mistake was calling it like this

jboolean rv = jenv->CallBooleanMethod(hello_obj, myMethod_methodID, myfirst_jstring, mysecond_jstring, 1);

which was obviously missing a jint argument between myMethod_methodID and myfirst_jstring.

It took me a long time to track these errors down because there was no relevant output in logcat and the only behavior was not doing anything (it didn't even crash).

So, the question is: How do I get more meaningful errors for these kind of mistakes?

like image 315
João Portela Avatar asked Apr 21 '11 11:04

João Portela


1 Answers

This page might be useful: http://www.netmite.com/android/mydroid/dalvik/docs/jni-tips.html , specially:

JNI does very little error checking. Calling SetFieldInt on an Object field will succeed. The goal is to minimize the overhead on the assumption that, if you've written it in native code, you probably did it for performance reasons.

Some VMs support extended checking with the "-Xcheck:jni" flag. If the flag is set, the VM puts a different table of functions into the JavaVM and JNIEnv pointers. These functions do an extended series of checks before calling the standard implementation.

Some things that may be verified:

  • Check for null pointers where not allowed.
  • Verify argument type correctness (jclass is a class object, jfieldID points to field data, jstring is a java.lang.String).
  • Field type correctness, e.g. don't store a HashMap in a String field.
  • Check to see if an exception is pending on calls where pending exceptions are not legal.
  • Check for calls to inappropriate functions between Critical get/release calls.
  • Check that JNIEnv structs aren't being shared between threads.
  • Make sure local references aren't used outside their allowed lifespan.
  • UTF-8 strings contain valid "modified UTF-8" data.

Accessibility of methods and fields (i.e. public vs. private) is not checked.

Following the link: http://www.netmite.com/android/mydroid/dalvik/docs/embedded-vm-control.html

You can set up adb as follows:

adb shell setprop dalvik.vm.checkjni true
adb shell setprop dalvik.vm.execution-mode int:debug
like image 170
Aleadam Avatar answered Sep 21 '22 16:09

Aleadam