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

Error message is not shown and a problematic JNI program executes successfully.

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux_ubuntu

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Linux c5832eeef939 4.15.0-88-generic #88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      I have compared the behaviors of two different versions of Hotspot by running my JNI program and found discrepancies.
      The error message is properly shown on Hotspot for java 11.
      However, no error message is shown on Hotspot for java 8.

      The behavior of Hotspot for Java 8 is misleading because not only it does not show proper error messages but the problematic JNI program executes normally.

      No error message is shown in the following version of Hotspot:
      java version "1.8.0_241"
      Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

      The error message is properly shown in the following version of Hotspot:
      java version "11.0.6" 2020-01-14 LTS
      Java(TM) SE Runtime Environment 18.9 (build 11.0.6+8-LTS)
      Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.6+8-LTS, mixed mode)
      ------------------------------------------------------------------
      The following error message is shown on Hotspot for java 11
      double free or corruption (out)
      Aborted (core dumped)

      The problematic JNI program is below:
      JNIEXPORT int JNICALL Java_HelloJNI_sayHello (JNIEnv *env, jobject thisObj){
      jchar *s1;
      jstring str1, str2;
      str1 = (*env)->NewString(env, "test3", 5);
      s1 = (*env)->GetStringCritical(env,str1,NULL);
      str2 = (*env)->NewStringUTF(env, "test5");
      (*env)->ReleaseStringCritical(env,str2,s1); //pasing str2 causes discrepancies
      return 0;
      }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. compile HelloJNI.c file
      gcc -w /<location>/HelloJNI.c -o /<location>/libHello.so -shared \-I/opt/java/openjdk/include/ \-I/opt/java/openjdk/include/linux -fPIC

      2. compile HelloJNI.java file
      javac HelloJNI.java

      3. run java file with libHello.so shared library.
      java HelloJNI


      ---------- BEGIN SOURCE ----------
      HelloJNI.c:
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <stdbool.h>
      #include "HelloJNI.h"
      #include <stdarg.h>
      #include <assert.h>

      JNIEXPORT int JNICALL Java_HelloJNI_sayHello (JNIEnv *env, jobject thisObj){
      jchar *s1;
      jstring str1, str2;
      str1 = (*env)->NewString(env, "test3", 5);
      s1 = (*env)->GetStringCritical(env,str1,NULL);
      str2 = (*env)->NewStringUTF(env, "test5");
      (*env)->ReleaseStringCritical(env,str2,s1);
      return 0;
      }

      HelloJNI.h:
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class HelloJNI */

      #ifndef _Included_HelloJNI
      #define _Included_HelloJNI
      #ifdef __cplusplus
      extern "C" {
      #endif
      /*
       * Class: HelloJNI
       * Method: sayHello
       * Signature: ()I
       */
      JNIEXPORT jint JNICALL Java_HelloJNI_sayHello
        (JNIEnv *, jobject);

      #ifdef __cplusplus
      }
      #endif
      #endif

      HelloJNI.java:
      import java.lang.Exception;

      public class HelloJNI {
       static {System.loadLibrary("Hello");}
       private native int sayHello();
       public static void main(String[] args){
         try{
              HelloJNI jni = new HelloJNI();
              int tmp = jni.sayHello();
              System.out.println("java_result:"+tmp);
         }
         catch(Throwable t){ t.printStackTrace();}
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

        1. HelloJNI.c
          0.6 kB
        2. HelloJNI.java
          0.8 kB

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: