JDK-8268416 : Clarify invalid method and field ID in JNI specification
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 18
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-06-08
  • Updated: 2021-10-26
  • Resolved: 2021-10-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 18
18 b20Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Description
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.
Comments
Hi David, thanks for the ping! Your earlier comment from 9/16/2021 missed my radar. The clarification looks good to me. Thanks!
12-10-2021

[~jiangli] Please respond. :)
12-10-2021

[~jiangli] is this the kind of clarification you were looking for: "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 +and may not be passed to any function taking such an ID.+" ?
17-09-2021

Moved to hotspot->runtime as we maintain the JNI specification
11-08-2021