jni libjvm sigsegv

Joined
May 25, 2011
Messages
2
Reaction score
0
Hello all.
I'm calling java code from C.
I'm getting lots of sigsegv inside libjvm.
I'm afraid it can depend on something wrong I'm doing before invoking the actual java method.
The java class I'm calling here is using a remote RMI object to perform a passphrase decryption and should return clear text to the caller.

Here is the invocation code:

Code:
int do_p_stuff_1p( char * encdec_client_path,  char *classPath,  char *libraryPath,  char *crypt_pwd, char *dariempire) {

     // jni per interfacciare EDS
    JNIEnv          *env;
    JavaVM          *jvm;
    JavaVMInitArgs  vm_args;
    JavaVMOption    options[3];
    int             nbOptions;
    jint            res;
    jclass          myJavaClass;

        /** Start the Java Virtual Machine **/
    nbOptions=0;
    options[0].optionString = classPath;   nbOptions++;
    options[1].optionString = libraryPath; nbOptions++;
    vm_args.version  = JNI_VERSION_1_2;                   /* Specifies the JNI
                                                             version used */
    vm_args.options  = options;
    vm_args.nOptions = nbOptions;
    vm_args.ignoreUnrecognized = JNI_TRUE;                 /* JNI won't
                                                              complain about unrecognized options */
    res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
    /*
     * Create an instance of the Class
     */
    myJavaClass   = (*env)->FindClass(env, encdec_client_path);
    if ((*env)->ExceptionOccurred(env)) {
      (*env)->ExceptionDescribe(env);
      return JAVAEXCEPTION;
    }
    jmethodID mid;
    jstring jstr, jstrReturn;
    jclass stringClass;
    jobjectArray args;
    const char * msg;
    mid = (*env)->GetStaticMethodID(env, myJavaClass, "execute",
                                    "([Ljava/lang/String;)Ljava/lang/String;");

    if (mid == NULL) {
      if ((*env)->ExceptionOccurred(env)) {
        (*env)->ExceptionDescribe(env);
        return JAVAEXCEPTION;
      }

    }
    jstr = (*env)->NewStringUTF(env, "DC");
    if (jstr == NULL) {
      if ((*env)->ExceptionOccurred(env)) {
        (*env)->ExceptionDescribe(env);
        return JAVAEXCEPTION;
      }

    }
    stringClass = (*env)->FindClass(env, "java/lang/String");
    args = (*env)->NewObjectArray(env, 3, stringClass, jstr);
    if (args == NULL) {
      if ((*env)->ExceptionOccurred(env)) {
        (*env)->ExceptionDescribe(env);
        return JAVAEXCEPTION;
      }
    }

    jstr = (*env)->NewStringUTF(env, crypt_pwd);
    (*env)->SetObjectArrayElement(env, args, 2, jstr);

    if ((*env)->ExceptionOccurred(env)) {
      (*env)->ExceptionDescribe(env);
      return JAVAEXCEPTION;
    }
   
   // THIS IS CAUSING PROBLEMS 
   jstrReturn = (*env)->CallStaticObjectMethod(env, myJavaClass, mid, args);

    msg = (const char *)(*env)->GetStringUTFChars(env, jstrReturn,NULL);

    if ((*env)->ExceptionOccurred(env)) {
      (*env)->ExceptionDescribe(env);
        return JAVAEXCEPTION;
    }

    if(jvm!=NULL) {
      if((*jvm)->DestroyJavaVM(jvm)!=0) {
        return JAVAEXCEPTION;
      }
    }

    if(strstr(msg,"ERROR")!=NULL || strstr(msg,"\n")!=NULL) {
      return EDERROR;
    }
    sprintf(dariempire,"%s", msg);
    return 0;
}

I've carefully checked the incoming params.
They are correctly allocated null terminated strings.

I can post the Java method as well, unless someone at a first glance will see the wrong stuff in what am I doing here.

Oh, I forgot to mention that sometimes the code works properly!!!

Thanks in advance and apologize if a silly question.

:adore:

\c
 
Last edited:
Joined
May 25, 2011
Messages
2
Reaction score
0
solved

Hi guys (if any).

Just in case someone couldn't sleep at night.

Trivial solution: I forgot to test the result string not being null.
...
Code:
    jstrReturn = (*env)->CallStaticObjectMethod(env, myJavaClass, mid, args);
    if(jstrReturn==NULL){
      if(jvm!=NULL) {
        if((*jvm)->DestroyJavaVM(jvm)!=0) {
          return JAVAEXCEPTION;
        }
      }
      return JAVAEXCEPTION;
    }
...

\c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top