< prev index next >

test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorInfo/GetOwnedMonitorInfoTest.java

Print this page
@@ -22,12 +22,14 @@
   */
  
  
  /**
   * @test
-  * @bug 8185164
-  * @summary Checks that a contended monitor does not show up in the list of owned monitors
+  * @bug 8185164 8320515
+  * @summary Checks that a contended monitor does not show up in the list of owned monitors.
+  *          8320515 piggy-backs on this test and injects an owned monitor with a dead object,
+             and checks that that monitor isn't exposed to GetOwnedMonitorInfo.
   * @requires vm.jvmti
   * @compile GetOwnedMonitorInfoTest.java
   * @run main/othervm/native -agentlib:GetOwnedMonitorInfoTest GetOwnedMonitorInfoTest
   */
  

@@ -44,23 +46,46 @@
                                 + System.getProperty("java.library.path"));
              throw ule;
          }
      }
  
+     private static native void jniMonitorEnter(Object obj);
      private static native int check();
      private static native boolean hasEventPosted();
  
+     private static void jniMonitorEnterAndLetObjectDie() {
+         // The monitor iterator used by GetOwnedMonitorInfo used to
+         // assert when an owned monitor with a dead object was found.
+         // Inject this situation into this test that performs other
+         // GetOwnedMonitorInfo testing.
+         Object obj = new Object() {};
+         jniMonitorEnter(obj);
+         if (!Thread.holdsLock(obj)) {
+             throw new RuntimeException("The object is not locked");
+         }
+         obj = null;
+         System.gc();
+     }
+ 
      public static void main(String[] args) throws Exception {
-         runTest(true);
-         runTest(false);
+         runTest(true, true);
+         runTest(true, false);
+         runTest(false, true);
+         runTest(false, false);
      }
  
-     public static void runTest(boolean isVirtual) throws Exception {
+     public static void runTest(boolean isVirtual, boolean jni) throws Exception {
          var threadFactory = isVirtual ? Thread.ofVirtual().factory() : Thread.ofPlatform().factory();
          final GetOwnedMonitorInfoTest lock = new GetOwnedMonitorInfoTest();
  
          Thread t1 = threadFactory.newThread(() -> {
+             Thread.currentThread().setName("Worker-Thread");
+ 
+             if (jni) {
+                 jniMonitorEnterAndLetObjectDie();
+             }
+ 
              synchronized (lock) {
                  System.out.println("Thread in sync section: "
                                     + Thread.currentThread().getName());
              }
          });
< prev index next >