jni libjvm sigsegv

Discussion in 'Java' started by oviv, May 25, 2011.

  1. oviv

    oviv

    Joined:
    May 25, 2011
    Messages:
    2
    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: May 25, 2011
    oviv, May 25, 2011
    #1
    1. Advertising

  2. oviv

    oviv

    Joined:
    May 25, 2011
    Messages:
    2
    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
    oviv, May 30, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    0
    Views:
    2,094
    Frank
    Aug 5, 2003
  2. kiran.dpatil@gmail.com
    Replies:
    0
    Views:
    554
    kiran.dpatil@gmail.com
    Apr 22, 2005
  3. Naresh Agarwal
    Replies:
    0
    Views:
    816
    Naresh Agarwal
    Jun 30, 2005
  4. rafael@enq.ufrgs.br

    JNI SIGSEGV on AMD64

    rafael@enq.ufrgs.br, Aug 7, 2006, in forum: Java
    Replies:
    2
    Views:
    803
    rafael@enq.ufrgs.br
    Aug 8, 2006
  5. sapsi

    Python,SWIG and libjvm

    sapsi, Sep 21, 2007, in forum: Python
    Replies:
    3
    Views:
    726
    Graham Dumpleton
    Sep 21, 2007
Loading...

Share This Page