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

jvm crashes on jni call to c++ standard library "cout"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • hotspot
    • sparc
    • solaris_2.6, solaris_7



      Name: boT120536 Date: 11/30/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
      Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

      This is a very annoying problem when trying to use JNI with j2se1.3 jvm. The
      C++ compiler is Sun Workshop 6. Basically this is a code which uses cout to
      print "Hello World" to standard output from native code. Here is the code, and
      the Makefile used to build it:
      Main.java:
      #######
      //
      // Load a native library. Create an object
      // and invoke a native method.
      //

      public class Main {
          public static void main(String[] args) {
              System.loadLibrary("Native");
              AClassWithNativeMethods
                  c = new AClassWithNativeMethods();
              c.theNativeMethod();
          }
      }
      #######
      AClassWithNativeMethods.java:
      #######
      // File: AClassWithNativeMethods.java
      // A really simple example of a class containing
      // a native method.
      public class AClassWithNativeMethods {
          public native void theNativeMethod();
      }
      ######
      jniAClassWithNativeMethods.cpp
      ######
      /* File: theNativeMethod.c
       * Implements AClassWithNativeMethods.theNativeMethod
       */
      #ifndef MAKEDEPEND
      #include <iostream>
      #include <cstdio>
      #endif
      #include "AClassWithNativeMethods.h"
      using namespace std;

      JNIEXPORT void JNICALL Java_AClassWithNativeMethods_theNativeMethod
              (JNIEnv* env, jobject thisObj) {
                  cout << "Hello world!" << endl;
      }
      ########

      The header file AClassWithNativeMethods.h is generated by the command:
      "javah -jni AClassWithNativeMethods"

      The Makefile is as follows:
      ######
      #
      # Makefile for example code
      #

      JNI_LIB = libNative.so

      CPP_SOURCES = jniAClassWithNativeMethods.cpp

      SOURCES_JAVA = Main.java
      SOURCES_NATIVE = AClassWithNativeMethods.java

      JAVA_SOURCES = $(SOURCES_JAVA) $(SOURCES_NATIVE)

      #
      # Sources.h defines $(SOURCES)
      #
      CLASSES = $(JAVA_SOURCES:.java=.class)
      HEADERS_NATIVE = $(SOURCES_NATIVE:%.java=%.h)
      OBJECTS = $(CPP_SOURCES:.cpp=.o)
      JDK_HOME = $(JAVA_HOME)

      #for sun
      CCFLAGS = -g -KPIC
      LDLIBS = -lm -lCrun -lCstd
      LDFLAGS = -G

      INCLUDES = -I$(JDK_HOME)/include -I$(JDK_HOME)/include/solaris

      .SUFFIXES: .cpp
      .cpp.o:
              @echo "# Compiling $@ because of $?:"
              $(COMPILE.cc) $(INCLUDES) $<

      .java.class:
              @echo "# Compiling $@ because of $?:"
              $(JDK_HOME)/bin/javac -g $<

      %.h:%.class
              @echo "# Creating $@ for class $* because of $?:"
              $(JDK_HOME)/bin/javah -jni $*

      default:
              cat Makefile > makefile0
              makedepend -DMAKEDEPEND $(INCLUDES) -fmakefile0 $(CPP_SOURCES)
              make -f makefile0 classes native $(JNI_LIB)

      classes: $(CLASSES)

      native: $(HEADERS_NATIVE)

      $(JNI_LIB): $(OBJECTS)
              @echo "# Linking $@ because of $?."
              $(LINK.cc) $(INCLUDES) $(OBJECTS) -o $@ $(LDLIBS)

      clean:
              @echo "# Cleaning up objects:"
              - /bin/rm -f $(CLASSES)
              - /bin/rm -f $(HEADERS_NATIVE)
              - /bin/rm -f $(JNI_LIB)
              - /bin/rm -f $(OBJECTS)

      # DO NOT DELETE THIS LINE -- make depend depends on it.
      ########
      I am using /usr/ccs/bin/make.

      The output of the code is:
      jal@ducati:127>java Main
      # # An unexpected exception has been detected in native code outside the VM.#
        Program counter=0xfd2bb22c
      #
      # Problematic Thread: prio=5 tid=0x280e0 nid=0x1 runnable
      #
      Abort (core dumped)


      The dbx call stack is:


      current thread: t@1
        [1] std::basic_ostream<char,std::char_traits<char>
      >::sentry::~sentry(0xffbee2d4, 0x0, 0xfd2ebaf4, 0xfd2b8b28, 0xffbee2d4,
      0xff0000), at 0xfd2bb22c
        [2] std::operator<<(0xfd2ec668, 0xfd2ec680, 0xfd2ec670, 0xfd2ebaf4, 0x0, 0xc),
      at 0xfd2ba0b8
      =>[3] Java_AClassWithNativeMethods_theNativeMethod(env = 0x28164, thisObj =
      0xffbee42c), line 14 in "jniAClassWithNativeMethods.cpp"
        [4] 0x6fb10(0xf4c34530, 0xffbee518, 0x280e0, 0xfe773b44, 0xb6, 0xf8cb6548), at
      0x6fb0f
        [5] 0x6cc94(0x0, 0x1, 0xfe77fd78, 0x75d18, 0x1e, 0xe), at 0x6cc93
        [6] 0xfe7a4994(0xffbee538, 0xffbee738, 0xa, 0xf8cb63c8, 0x6ecdc, 0xffbee66c),
      at 0xfe7a4993
        [7] JavaCalls::call_helper(0xffbee730, 0xfe773b44, 0xffbee664, 0x280e0,
      0x6ecdc, 0xffbee738), at 0xfe549d3c
        [8] JavaCalls::call(0xffbee730, 0xffbee644, 0xffbee664, 0x280e0, 0xfe773b44,
      0xffbee5dc), at 0xfe5499e8
        [9] jni_invoke(0x1, 0x280e0, 0x0, 0x0, 0xe6198, 0xffbee714), at 0xfe551aec
        [10] jni_CallStaticVoidMethod(0x280e0, 0xfe773b44, 0xe6198, 0x28164,
      0xffbee798, 0x0), at 0xfe55e15c
        [11] main(0x28164, 0x0, 0x28164, 0xffbef1ef, 0x0, 0x25508), at 0x1212c


      So you see it dies in the cout call.
      I don't know if this is a jvm problem or a Workshop 6 problem. It doesn't die
      when I use the GNU g++ compiler or the KAI KCC compiler for the native code. It
      also doesn't die when I use an earlier jvm like java 1.2.
      (Review ID: 112266)
      ======================================================================

      Name: yyT116575 Date: 01/23/2001


      /app/java/jre/1.3.0_us/bin/java -version

      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
      Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

      Using JNI, we call a C code which is used as an interface to a C++ code. The
      C++ code contains cout instructions. With the preceeding version of the
      JVM:
      /app/java/jre/1.2.2_05a_us/bin/java -version

      java version "1.2.2"
      Solaris VM (build Solaris_JDK_1.2.2_05a, native threads, sunwjit)

      we saw the prints on the console, while with JVM 1.3, all what we've got is

      # # An unexpected exception has been detected in native code outside the VM.
      # Program counter=0xe01560e0
      #
      # Problematic Thread: prio=6 tid=0xf3c68 nid=0xb runnable
      #
      Abort
      (Review ID: 115604)
      ======================================================================

            jmcilreesunw James Mcilree (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: