jni libjvm sigsegv

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

  1. oviv

    oviv

    Joined:
    May 25, 2011
    Messages:
    2
    Likes Received:
    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 (Text):

    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. Advertisements

  2. oviv

    oviv

    Joined:
    May 25, 2011
    Messages:
    2
    Likes Received:
    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 (Text):

        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. Advertisements

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. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    1,092
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    1,183
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    3,231
    vasanth
    Jan 25, 2005
  4. kiran.dpatil@gmail.com
    Replies:
    0
    Views:
    687
    kiran.dpatil@gmail.com
    Apr 22, 2005
  5. Naresh Agarwal
    Replies:
    0
    Views:
    991
    Naresh Agarwal
    Jun 30, 2005
  6. rafael@enq.ufrgs.br

    JNI SIGSEGV on AMD64

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

    Python,SWIG and libjvm

    sapsi, Sep 21, 2007, in forum: Python
    Replies:
    3
    Views:
    908
    Graham Dumpleton
    Sep 21, 2007
  8. bgabrhelik
    Replies:
    0
    Views:
    1,149
    bgabrhelik
    Sep 29, 2009
Loading...