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

JVM TI GetCurrentContendedMonitor is implemented incorrectly

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 23
    • hotspot
    • None
    • behavioral
    • low
    • Hide
      The fix is correcting the JVMTI GetCurrentContendedMonitor behavior existed for a long time (from the original release of the JVMTI). This JVMTI function is intended for debugging and we don't know about the JVMTI agents except the JDWP agent that are using it. Also, the implementations of the JDWP `ThreadReference.CurrentContendedMonitor` command and JDI `ThreadReference.currentContendedMonitor` method are based and depend on this JVMTI function. The JDWP command and the JDI method were specified incorrectly and had incorrect behavior. The fix corrects both the JDWP+JDI specs and the JDWP+JDI implementations. The JetBrains Debugger is using this functionality for informational purposes and does not depend on the incorrect behavior. Additionally, this can potentially impact the IBM J9 implementation. There is one JCK vm/jdwp test which depend on this incorrect behavior and need to be fixed.
      Show
      The fix is correcting the JVMTI GetCurrentContendedMonitor behavior existed for a long time (from the original release of the JVMTI). This JVMTI function is intended for debugging and we don't know about the JVMTI agents except the JDWP agent that are using it. Also, the implementations of the JDWP `ThreadReference.CurrentContendedMonitor` command and JDI `ThreadReference.currentContendedMonitor` method are based and depend on this JVMTI function. The JDWP command and the JDI method were specified incorrectly and had incorrect behavior. The fix corrects both the JDWP+JDI specs and the JDWP+JDI implementations. The JetBrains Debugger is using this functionality for informational purposes and does not depend on the incorrect behavior. Additionally, this can potentially impact the IBM J9 implementation. There is one JCK vm/jdwp test which depend on this incorrect behavior and need to be fixed.
    • Java API
    • SE

      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:

      https://docs.oracle.com/en/java/javase/21/docs/specs/jdwp/jdwp-protocol.html#JDWP_ThreadReference_CurrentContendedMonitor

      The JDI ThreadReference.currentContendedMonitor method spec can be found here:

      https://docs.oracle.com/en/java/javase/21/docs/api/jdk.jdi/com/sun/jdi/ThreadReference.html#currentContendedMonitor()

            sspitsyn Serguei Spitsyn
            dholmes David Holmes
            Alan Bateman, Daniel Daugherty
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: