Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4171442

call to java_main when libjava.so is loaded dynamically dumps core

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_2.5.1

      Description



      Name: diC59631 Date: 09/04/98


      Background:
      ----------

      At Oracle, we have a product which invokes the appletviewer from a C program, with valid HTML file, which in-turn invokes an Applet.

      The C program creates a new thread. This thread loads the java library libjava.so (javai.dll on Win32), locates the function java_main. It then executes a call to this function with the following arguments (name testjni.html chosen for simplicity).

      'sun.applet.AppletViewer testjni.html'

      testjni.html is a generated HTML file which has the information to invoke a custom applet. This applet communicates with the C program using JNI to run the application.


      This works fine in win32 environment.


      Problem:
      -------

       On Solaris, this does not work. The appletviewer exits with a segmentation violation. However, if the java library is linked with the C program it invokes the appletviewer properly and the client applet runs as part of the program.

       Our requirement is to load the JDK dynamically.


      Note:
      ----

        1. JAVAHOME environment variable is set to the local JDK installation, which in my case is '/local/java/solaris/jdk/1_1_4'

        2. I am using green-threads by default

        3. I am not using threads in this example for simplicity

        4. Please forward any work around and alternatives directly to me. Thanks



      Information to repeat the bug:
      -----------------------------

       I have a created a simple test case with a C program (test.c), Java file (TestJNI.java) and HTML file (testjni.html). These are attached with this bug description.

       Steps to produce the bug:

        1. compile the C file with the following command line

         solaris> cc test.c -DLOAD_DYNAMIC -ldl -o testjni

        2. copy the TestJNI.java file in to the same directory

         solaris> javac TestJNI.java

        3. copy the jnicall.html to the same directory

        4. Execute the C programe

          solaris> testjni

        Result: notice that appletviewer exits with a core dump.


       Files: Total 3
       --------------

       1. test.c
       2. TestJNI.java
       3. jnicall.html

      -------------------test.c---------------------------------
       #include <stdio.h>
      #include <dlfcn.h>

      #define LIBPATH ((const char*)"LD_LIBRARY_PATH")
      #define JAVALIB ((const char*)"libjava.so")
      #define NUMARGS (3)

      int main(int argc, char **argv)
      {
       int ldpathlen;
       void *handle;
       void (*javamain)(int , char **);
       char *libpath, *javahome, *ldpath;
       char **jargv;

       printf("Fetching %s...\n", LIBPATH);
       libpath = getenv(LIBPATH);
       printf("%s : %s\n", LIBPATH, libpath);

       printf("Fetching JAVAHOME...\n");
       javahome = getenv("JAVAHOME");
       printf("JAVAHOME : %s\n", javahome);

       ldpathlen = strlen(LIBPATH) + strlen(javahome) + 3 +
                   strlen("/lib/sparc/green_threads/") + strlen(":") + strlen(libpath);
       ldpath = (char *)malloc(ldpathlen);
       sprintf(ldpath, "%s=%s:%s/lib/sparc/green_threads", LIBPATH, libpath, javahome);
       if ( putenv(ldpath) != 0 )
       {
        printf("Error in setting %s\n", LIBPATH);
       }
       printf("Setting %s : %s\n", LIBPATH, getenv(LIBPATH));

      #ifdef LOAD_DYNAMIC
       printf("Loading the %s...\n", JAVALIB);
       handle = dlopen("libjava.so", RTLD_NOW);
       if ( handle )
       {
        printf("Loaded the libjava.so...\n");
       }
       else
       {
        printf("Error in loading libjava.so...\n");
        printf("Error : %s\n", dlerror());
        free(ldpath);
        return -1;
       }

       printf("Finding the java_main...\n");
       javamain = dlsym( handle, "java_main");
       if ( javamain )
       {
        printf("Found the java_main...\n");
       }
       else
       {
        printf("Error in finding java_main...\n");
        free(ldpath);
        dlclose(handle);
        return -1;
       }
      #endif

       printf("Calling java_main...\n");
       jargv = (char **) malloc( sizeof(char *) * NUMARGS);
       jargv[0] = (char *)malloc( strlen("java") + 1);
       strcpy( jargv[0], "java");
       jargv[1] = (char *)malloc( strlen("sun.applet.AppletViewer") + 1);
       strcpy( jargv[1], "sun.applet.AppletViewer");
       jargv[2] = (char *)malloc( strlen("jnicall.html") + 1);
       strcpy( jargv[2], "jnicall.html");

      #ifdef LOAD_DYNAMIC
       (*javamain)(NUMARGS, jargv);
      #endif

      #ifndef LOAD_DYNAMIC
       java_main(NUMARGS, jargv);
      #endif

       printf("Returned from java_main...\n");

      #ifdef LOAD_DYNAMIC
       free(ldpath);
       dlclose(handle);
      #endif
      }
       --------------------------------------------------------

       -------------------TestJNI.java---------------------------
      import java.applet.Applet;
      import java.awt.*;

      public class TestJNI extends Applet
      {
       public void init()
       {
          System.out.println("Init in the applet");
       }

       public void paint(Graphics g)
       {
         g.drawString("Hello world", 20, 20);
       }
      }
       ------------------------------------------------------

       --------------------jnicall.html---------------------------
      <applet code=TestJNI width = 200 height = 200>
      </applet>
       -----------------------------------------------------------

      Error message:
      SIGSEGV 11* segmentation violation
          si_signo [11]: SIGSEGV 11* segmentation violation
          si_errno [0]: Error 0
          si_code [1]: SEGV_MAPERR [addr: 0x0]
       
              stackbase=EFFF9858, stackpointer=EFFF9598
       
      Full thread dump:
          "Finalizer thread" (TID:0xee300220, sys_thread_t:0xef3a0de0, state:R) prio=1
          "Async Garbage Collector" (TID:0xee3001d8, sys_thread_t:0xef3d0de0, state:R)
       prio=1
          "Idle thread" (TID:0xee300190, sys_thread_t:0xef400de0, state:R) prio=0
          "Clock" (TID:0xee3000d0, sys_thread_t:0xef430de0, state:CW) prio=12
          "main" (TID:0xee3000a8, sys_thread_t:0x379f0, state:R) prio=5 *current threa
      d*
              java.io.FileInputStream.<init>(FileInputStream.java)
              java.io.FileInputStream.<init>(FileInputStream.java)
              sun.applet.AppletViewer.init(AppletViewer.java:785)
              sun.applet.AppletViewer.mainInit(AppletViewer.java:1026)
              sun.applet.AppletViewer.main(AppletViewer.java:1039)
      Monitor Cache Dump:
      Registered Monitor Dump:
          Thread queue lock: <unowned>
          Name and type hash table lock: <unowned>
          String intern lock: <unowned>
          JNI pinning lock: <unowned>
          JNI global reference lock: <unowned>
          BinClass lock: <unowned>
          Class loading lock: <unowned>
          Java stack lock: <unowned>
          Code rewrite lock: <unowned>
          Heap lock: <unowned>
          Has finalization queue lock: <unowned>
          Finalize me queue lock: <unowned>
          Monitor IO lock: owner "main" (0x379f0, 1 entry)
          Child death monitor: <unowned>
          Event monitor: <unowned>
          I/O monitor: <unowned>
          Alarm monitor: <unowned>
              Waiting to be notified:
                  "Clock"
          Sbrk lock: <unowned>
          Monitor cache expansion lock: <unowned>
          Monitor registry: owner "main" (0x379f0, 1 entry)
      Thread Alarm Q:
      Abort (core dumped)
      (Review ID: 38061)
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            apalanissunw Anand Palaniswamy (Inactive)
            Reporter:
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: