1 /*
   2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4118056
  26  *
  27  * @summary synopsis: Distributed Garbage Collection Deadlock
  28  * @author Laird Dornin
  29  *
  30  * @library ../../testlibrary
  31  * @build TestLibrary Test TestImpl TestImpl_Stub
  32  * @run main/othervm/policy=security.policy/timeout=360 DGCDeadLock
  33  */
  34 
  35 /* This test attempts to cause a deadlock between the rmi leaseChecker
  36  * thread and a thread that is servicing a dgc clean call. Before the
  37  * fix for this bug was implemented, deadlock could occur when the
  38  * leaseChecker held the lock on the lease table and the clean thread
  39  * held the lock on a target x. The clean thread would attempt to get
  40  * the lock on the leaseTable to do DGCImpl.unregisterTarget and the
  41  * leaseChecker would attempt to get the lock on x to do
  42  * Target.vmidDead.  Each thread held a resource that the other thread
  43  * was attempting to lock.
  44  *
  45  * This test causes the above conditions to occur and waits to see if
  46  * a given set of remote calls finishes "quickly enough."
  47  */
  48 
  49 import java.rmi.*;
  50 import java.io.*;
  51 
  52 public class DGCDeadLock implements Runnable {
  53     private static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
  54     final static public int HOLD_TARGET_TIME = 25000;
  55     public static int TEST_FAIL_TIME = HOLD_TARGET_TIME + 30000;
  56     public static boolean finished = false;
  57     static DGCDeadLock test = new DGCDeadLock();
  58 
  59     static {
  60         System.setProperty("sun.rmi.transport.cleanInterval", "50");
  61     }
  62 
  63     static public void main(String[] args) {
  64 
  65         JavaVM testImplVM = null;
  66 
  67         System.err.println("\nregression test for 4118056\n");
  68         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  69 
  70         try {
  71             String options = " -Djava.security.policy=" +
  72                 TestParams.defaultPolicy +
  73                 " -Djava.rmi.dgc.leaseValue=500000" +
  74                 "  -Dsun.rmi.dgc.checkInterval=" +
  75                 (HOLD_TARGET_TIME - 5000) +
  76                 "   -Drmi.registry.port=" + REGISTRY_PORT +
  77                 "" ;
  78 
  79             testImplVM = new JavaVM("TestImpl", options, "");
  80             testImplVM.start();
  81 
  82             synchronized (test) {
  83                 Thread t = new Thread(test);
  84                 t.setDaemon(true);
  85                 t.start();
  86 
  87                 // wait for the remote calls to take place
  88                 test.wait(TEST_FAIL_TIME);
  89             }
  90 
  91             if (!finished) {
  92                 TestLibrary.bomb("Test failed, had exception or exercise" +
  93                                            " routines took too long to " +
  94                                            "execute");
  95             }
  96             System.err.println("Test passed, exercises " +
  97                                "finished in time.");
  98 
  99         } catch (Exception e) {
 100             testImplVM = null;
 101             TestLibrary.bomb("test failed", e);
 102         }
 103     }
 104 
 105     public void run() {
 106         try {
 107             String echo = null;
 108 
 109             // give the test remote object time to initialize.
 110             Thread.currentThread().sleep(8000);
 111 
 112             // create a test client
 113             Test foo = (Test) Naming.lookup("rmi://:" +
 114                                             REGISTRY_PORT +
 115                                             "/Foo");
 116             echo = foo.echo("Hello world");
 117             System.err.println("Test object created.");
 118 
 119             /* give TestImpl time to lock the target in the
 120              * object table and any dirtys finish.
 121              */
 122             Thread.currentThread().sleep(5000);
 123 
 124             //unreference "Foo"
 125             foo = null;
 126 
 127             //garbage collect and finalize foo
 128             Runtime.getRuntime().gc();
 129             Runtime.getRuntime().runFinalization();
 130 
 131             //import "Bar"
 132             Test bar = (Test) Naming.lookup("rmi://:" +
 133                                             REGISTRY_PORT +
 134                                             "/Bar");
 135 
 136             /* infinite loop to show the liveness of Client,
 137              * if we have deadlock remote call will not return
 138              */
 139             try {
 140                 for (int i = 0; i < 500; i++) {
 141                     echo = bar.echo("Remote call" + i);
 142                     Thread.sleep(10);
 143                 }
 144 
 145                 // flag exercises finished
 146                 finished = true;
 147 
 148             } catch (RemoteException e) {
 149             }
 150 
 151         } catch (Exception e) {
 152             TestLibrary.bomb("test failed", e);
 153         } finally {
 154         }
 155     }
 156 }