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

Clarify invalid method and field ID in JNI specification



    • b20


      In Java Native Interface Specification (https://docs.oracle.com/en/java/javase/16/docs/specs/jni/design.html), it has:

      "Accessing Fields and Methods"

      A field or method ID does not prevent the VM from unloading the class from which the ID has been derived. After the class is unloaded, the method or field ID becomes invalid. The native code, therefore, must make sure to:

      keep a live reference to the underlying class, or
      recompute the method or field ID
      if it intends to use a method or field ID for an extended period of time.

      The JNI does not impose any restrictions on how field and method IDs are implemented internally.

      "Reporting Programming Errors"

      The programmer must not pass illegal pointers or arguments of the wrong type to JNI functions. Doing so could result in arbitrary consequences, including a corrupted system state or VM crash.

      Clarifying the text in "Accessing Fields and Methods" section with stronger language to disallow the usages of invalid method and field ID after class unloading would reflect "Reporting Programming Errors" section more clearly.

      JDK-8268088 describes an issue associated with the use of stale method ID in JVMTI agent. Tightening up the specification helps discourage the use of stale method ID. It will enable the fix for the memory leak issue described in JDK-8268088 in the future.

      Please see more discussion in https://github.com/openjdk/jdk/pull/4383 comments.


        Issue Links



              dholmes David Holmes
              jiangli Jiangli Zhou
              0 Vote for this issue
              6 Start watching this issue