Summary
The JVMTI GetCurrentContendedMonitor function implementation can sometimes return an incorrect information about the contended monitor. Such a behavior does not match the function spec. The JDWP ThreadReference.CurrentContendedMonitor command implementation depends on the JVMTI function, and so, is also incorrect. Also, the JDWP CurrentContendedMonitor command spec does not match the JVMTI GetCurrentContendedMonitor spec.
Problem
The JVMTI GetCurrentContendedMonitor function spec has this statement:
Get the object, if any, whose monitor the specified thread is waiting
to enter or waiting to regain through java.lang.Object.wait.
The spec is clear that the object is returned only when the thread is waiting to enter or waiting to re-enter its monitor through java.lang.Object.wait. However, the JVMTI implementation is also returning the object when the specified thread is waiting on its monitor to be notified.
The JDWP ThreadReference.CurrentContendedMonitor command spec has the following statements:
Returns the object, if any, for which this thread is waiting.
The thread may be waiting to enter a monitor, or it may be waiting, via the
java.lang.Object.wait method, for another thread to invoke the notify method.
The last part does not match the JVMTI GetCurrentContendedMonitor spec:
... it may be waiting, via the java.lang.Object.wait method,
for another thread to invoke the notify method.
Instead, it must say about waiting to re-enter the monitor.
The JDI ThreadReference.currentContendedMonitor method spec has the following statements:
The thread can be waiting for a monitor through entry into a synchronized method,
the synchronized statement, or Object.wait(). The status() method can be used
to differentiate between the first two cases and the third.
The last part is wrong and does not match the JVMTI GetCurrentContendedMonitor spec:
... or Object.wait(). The status() method can be used
to differentiate between the first two cases and the third.
Solution
The solution on JVMTI side is to align the implementation with the JVMTI GetCurrentContendedMonitor spec, so the monitor is returned only when the specified thread is waiting to enter or re-enter the monitor and the monitor is not returned when the specified thread is waiting in the java.lang.Object.wait to be notified.
Update the JDWP ThreadReference.CurrentContendedMonitor command spec to match the JVMTI GetCurrentContendedMonitor spec.
Replace the statement:
The thread may be waiting to enter a monitor, or it may be waiting, via the
java.lang.Object.wait method, for another thread to invoke the notify method.
with:
The thread may be waiting to enter the object's monitor, or in java.lang.Object.wait
waiting to re-enter the monitor after being notified, interrupted, or timed-out.
Update the JDI ThreadReference.currentContendedMonitor method spec to match the JVMTI GetCurrentContendedMonitor spec.
Replace the statement:
The thread can be waiting for a monitor through entry into a synchronized method,
the synchronized statement, or Object.wait(). The status() method can be used
to differentiate between the first two cases and the third.
with:
The thread can be waiting for a monitor through entry into a synchronized method,
the synchronized statement, or Object.wait() waiting to re-enter the monitor after
being notified, interrupted, or timed-out.
The bug needs a Release Note.
The following JCK vm/jdwp test has to be fixed to adopt to the modified behavior:
vm/jdwp/ThreadReference/CurrentContendedMonitor/curcontmonitor001/curcontmonitor001.html
Specification
The JVMTI GetCurrentContendedMonitor spec is not being changed and can be found here:
https://docs.oracle.com/en/java/javase/21/docs/specs/jvmti.html#GetCurrentContendedMonitor
The JDWP ThreadReference.CurrentContendedMonitor command spec can be found here:
The JDI ThreadReference.currentContendedMonitor method spec can be found here:
- csr of
-
JDK-8256314 JVM TI GetCurrentContendedMonitor is implemented incorrectly
-
- Resolved
-