JDK-8308816 : JNI_GetCreatedJavaVMs returns a partially initialized JVM
  • Type: CSR
  • Component: hotspot
  • Sub-Component: runtime
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 21
  • Submitted: 2023-05-25
  • Updated: 2024-10-25
  • Resolved: 2023-05-26
Related Reports
CSR :  
Relates :  
Description
Summary
-------

`JNI_GetCreatedJavaVMs` will now only return a VM that has been successfully initialized.

Problem
-------

`JNI_GetCreatedJavaVMs` will currently return a VM that has only just started to be created by `JNI_CreateJavaVM`. Consequently, if code then tries to attach to that VM it can crash in various ways, as the VM may not yet be ready for attaching.

Solution
--------

`JNI_GetCreatedJavaVMs` will now only return a VM that has been successfully initialized.

Specification
-------------

There are no changes to the JNI specification. The specification for `JNI_GetCreatedJavaVMs` already states:

> Returns all Java VMs that have been created.

and it is reasonable to expect that "created" means fully created, not "in the process of being created".

Comments
Thanks [~darcy]. I will consider a release note just to clarify they programmer must check for 1 vm being created.
26-05-2023

> The scenario I am thinking about is if the code does not check the actual count of VMs but instead relies on JNI_OK to indicate there is a VM (which is not what it means). Indeed, and the CSR prompted me to re-read the spec for JNI_GetCreatedJavaVMs. Given the statement "Creation of multiple VMs in a single process is not supported" then maybe some future change expand the description of nVMs to say that its value is 0 or 1. For the issue here, the important thing is that it might be 0, can't assume vmBufs[0] is a valid JavaVM.
26-05-2023

Moving to Approved. Out of an abundance of caution, is a release note warranted here?
26-05-2023

Thanks for the review Alan. The scenario I am thinking about is if the code does not check the actual count of VMs but instead relies on JNI_OK to indicate there is a VM (which is not what it means). If that code today then attaches to the VM and it is, by the nature of that code, long enough after the creation started that the attach succeeds, then such code will potentially crash going forward as it will now be using an uninitialized JavaVM pointer.
26-05-2023

I don't think there is any compatibility concerns as the scenario in the bug report is inherently racy, it might return 0, 1, or crash.
25-05-2023