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

JNI: launching a function called debug() across .so files will fail

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Fix
    • P4
    • None
    • 1.4.1_03, 1.4.2
    • docs
    • generic
    • generic

    Description

      You can't define your own method called debug() in your JNI code, because
      the debug() function is already defined in libjvm.so.

      jvm source code at:
      {hotspot_ws}/src/share/vm/utilities/debug.cpp

      This is not documented anywhere and could lead to a crash,
      when you are trying to call your function called "debug()"
      across c libraries.


      How to reproduce:
      =================
      HelloJWorld.java
      ----------------
      public class HelloJWorld
      {
        private native void Hi();

        public static void main(String[] args) {
          new HelloJWorld().Hi();
        }

        static {
          System.loadLibrary("HelloCWorld");
        }
      }

      HelloJWorld.h (you can create it with javah HelloJWorld)
      -------------
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class HelloJWorld */

      #ifndef _Included_HelloJWorld
      #define _Included_HelloJWorld
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
       * Class: HelloJWorld
       * Method: debug
       * Signature: ()V
       */
      JNIEXPORT void JNICALL Java_HelloJWorld_debug
        (JNIEnv *, jobject);

      #ifdef __cplusplus
      }
      #endif
      #endif


      HelloCWorld.c
      -------------
      #include <jni.h>
      #include <stdio.h>
      #include "HelloJWorld.h"

      JNIEXPORT void JNICALL
      Java_HelloJWorld_Hi(JNIEnv *env, jobject obj)
      {
              debug(4, "Hello debug World!\n");
              return;
      }


      util.c (in ./util folder)
      -------------------------
      #include <stdio.h>
      #include <sys/types.h>

      int debug( int level, char *fmt, char *var)
      {

              fprintf(stderr, "DEBUG:%d - %s %s\n", level, fmt, var);

      }

      Complete sourcecodes and Makefiles are attached to this bugreport.
      If you compile them (with gcc and Java 1.4.2),
      HelloJWorld.java file gets compiled to HelloJWorld.class file
      HelloCWorld.c file gets compiled to libHelloCWorld.so library file
      util.c file gets compiled to libutil.so library file

      When you run above java program it crashes with the following log:
      An unexpected exception has been detected in native code outside the VM.
      Unexpected Signal : 10 occurred at PC=0xFF3C2BF0
      Function=elf_rtbndr+0x20
      Library=/usr/lib/ld.so.1

      Current Java thread:
              at HelloJWorld.Hi(Native Method)
              at HelloJWorld.main(HelloJWorld.java:6)

      Dynamic libraries:
      0x10000 java
      0xff370000 /usr/lib/libthread.so.1
      0xff3a0000 /usr/lib/libdl.so.1
      0xff280000 /usr/lib/libc.so.1
      0xff350000 /usr/platform/SUNW,Ultra-60/lib/libc_psr.so.1
      0xfec00000 /opt/java/j2sdk/j2sdk1.4.2/jre/lib/sparc/client/libjvm.so
      0xff230000 /usr/lib/libCrun.so.1
      0xff210000 /usr/lib/libsocket.so.1
      0xff100000 /usr/lib/libnsl.so.1
      0xff1d0000 /usr/lib/libm.so.1
      0xff0e0000 /usr/lib/libsched.so.1
      0xff260000 /usr/lib/libw.so.1
      0xff0c0000 /usr/lib/libmp.so.2
      0xff0a0000 /usr/lib/librt.so.1
      0xff080000 /usr/lib/libaio.so.1
      0xff060000 /usr/lib/libmd5.so.1
      0xff030000 /usr/platform/SUNW,Ultra-60/lib/libmd5_psr.so.1
      0xfebd0000 /opt/java/j2sdk/j2sdk1.4.2/jre/lib/sparc/native_threads/libhpi.so
      0xfeb80000 /opt/java/j2sdk/j2sdk1.4.2/jre/lib/sparc/libverify.so
      0xfeb30000 /opt/java/j2sdk/j2sdk1.4.2/jre/lib/sparc/libjava.so
      0xfeb10000 /opt/java/j2sdk/j2sdk1.4.2/jre/lib/sparc/libzip.so
      0xfe860000 /usr/lib/locale/en_US.ISO8859-1/en_US.ISO8859-1.so.2
      0xfe3e0000 /net/cores/cores/CA_63639521/ori/jni/mydebug/libHelloCWorld.so

      Heap at VM Abort:
      Heap
       def new generation total 2112K, used 112K [0xf1800000, 0xf1a20000, 0xf1f10000)
        eden space 2048K, 5% used [0xf1800000, 0xf181c2a0, 0xf1a00000)
        from space 64K, 0% used [0xf1a00000, 0xf1a00000, 0xf1a10000)
        to space 64K, 0% used [0xf1a10000, 0xf1a10000, 0xf1a20000)
       tenured generation total 1408K, used 0K [0xf1f10000, 0xf2070000, 0xf5800000)
         the space 1408K, 0% used [0xf1f10000, 0xf1f10000, 0xf1f10200, 0xf2070000)
       compacting perm gen total 4096K, used 960K [0xf5800000, 0xf5c00000, 0xf9800000)
         the space 4096K, 23% used [0xf5800000, 0xf58f0220, 0xf58f0400, 0xf5c00000)

      Local Time = Tue Aug 12 14:15:09 2003
      Elapsed Time = 0
      #
      # The exception above was detected in native code outside the VM
      #
      # Java VM: Java HotSpot(TM) Client VM (1.4.2-b28 mixed mode)
      #
      # An error report file has been saved as hs_err_pid971.log.
      # Please refer to the file for further information.
      #
      Abort


      The same progam works fine if you rename the function name 'debug()' to 'debug1()' for example. Here is the output of the program with funtion name modified to 'debug1()':


      #./run
      DEBUG:4 -
      Hello debug World!
      Have a nice day!

      Attachments

        Issue Links

          Activity

            People

              lmartinsunw Louis Martin (Inactive)
              jloefflm Johann Löfflmann (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: