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

JNI AttachCurrentThread returns JNI_OK but *env NULL

XMLWordPrintable

    • x86_64
    • linux

      ADDITIONAL SYSTEM INFORMATION :
      Linux XXX 5.4.0-2-amd64 #1 SMP Debian 5.4.8-1 (2020-01-05) x86_64 GNU/Linux
      openjdk version "11.0.6-ea" 2020-01-14
      OpenJDK Runtime Environment (build 11.0.6-ea+7-post-Debian-1)
      OpenJDK 64-Bit Server VM (build 11.0.6-ea+7-post-Debian-1, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      setting a signal handler and raise the signal SIGQUIT with kill(getpid(), SIGQUIT) will cause inside the signalhandler
      (*jvm)->AttachCurrentThread(jvm, (void**) &env, NULL) return JNI_OK, but *env is NULL.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and run the source code of main.c

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      *env not NULL
      ACTUAL -
      *env is NULL

      ---------- BEGIN SOURCE ----------
      /*
       * File: main.c
       *
       * Created on 9. Januar 2020, 09:09
       */
      #define _GNU_SOURCE

      #include <signal.h>
      #include <unistd.h>

      #include <jni.h>
      #include <stdlib.h>

      static JavaVM *jvm;

      #define SIG SIGQUIT

      int signalHandler(int sig) {
          fprintf(stdout, "Signal received\n");
          JNIEnv *env;
          if ((*jvm)->AttachCurrentThread(jvm, (void**) &env, NULL) == JNI_OK) {
              if (*env == NULL) {
                  //We should never reach this point here.
                  fprintf(stderr, "Sucess, but env is NULL - This should never happen!\n");
                  exit(1);
              } else {
                  fprintf(stdout, "Sucess got env\n");
                  exit(0);
              }
          } else {
              fprintf(stderr, "got no env\n");
              exit(2);
          }
      }

      int main() {
          fprintf(stdout, "Starting\n");
          jint res;
          JNIEnv *env;
          JavaVMInitArgs vm_args;
          vm_args.version = JNI_VERSION_10;
          vm_args.options = NULL;
          vm_args.nOptions = 0;
          vm_args.ignoreUnrecognized = JNI_TRUE;
          res = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
          if (res != JNI_OK) {
              fprintf(stderr, "Can*t create the Java VM\n");
              exit(-1);
          }
          
          signal(SIG, (__sighandler_t) & signalHandler);
          fprintf(stdout, "Will raise signal\n");
          kill(getpid(), SIG);
      // raise(SIG); //Why does raise is differnt from kill here - the signal is not delivered -POSIX?
          fprintf(stdout, "Signal raised\n");
          sleep(2);
          exit(3);
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: