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  * @key intermittent
  27  *
  28  * @summary synopsis: Distributed Garbage Collection Deadlock
  29  * @author Laird Dornin
  30  *
  31  * @library ../../testlibrary
  32  * @modules java.rmi/sun.rmi.registry
  33  *          java.rmi/sun.rmi.server
  34  *          java.rmi/sun.rmi.transport
  35  *          java.rmi/sun.rmi.transport.tcp
  36  * @build TestLibrary Test TestImpl TestImpl_Stub
  37  * @run main/othervm/policy=security.policy/timeout=360 DGCDeadLock
  38  */
  39 
  40 /* This test attempts to cause a deadlock between the rmi leaseChecker
  41  * thread and a thread that is servicing a dgc clean call. Before the
  42  * fix for this bug was implemented, deadlock could occur when the
  43  * leaseChecker held the lock on the lease table and the clean thread
  44  * held the lock on a target x. The clean thread would attempt to get
  45  * the lock on the leaseTable to do DGCImpl.unregisterTarget and the
  46  * leaseChecker would attempt to get the lock on x to do
  47  * Target.vmidDead.  Each thread held a resource that the other thread
  48  * was attempting to lock.
  49  *
  50  * This test causes the above conditions to occur and waits to see if
  51  * a given set of remote calls finishes "quickly enough."
  52  */
  53 
  54 import java.rmi.*;
  55 import java.io.*;
  56 
  57 public class DGCDeadLock implements Runnable {
  58     private static final int REGISTRY_PORT = TestLibrary.getUnusedRandomPort();
  59     final static public int HOLD_TARGET_TIME = 25000;
  60     public static int TEST_FAIL_TIME = HOLD_TARGET_TIME + 30000;
  61     public static boolean finished = false;
  62     static DGCDeadLock test = new DGCDeadLock();
  63 
  64     static {
  65         System.setProperty("sun.rmi.transport.cleanInterval", "50");
  66     }
  67 
  68     static public void main(String[] args) {
  69 
  70         JavaVM testImplVM = null;
  71 
  72         System.err.println("\nregression test for 4118056\n");
  73         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  74 
  75         try {
  76             String options = " -Djava.security.policy=" +
  77                 TestParams.defaultPolicy +
  78                 " --add-exports java.rmi/sun.rmi.registry=ALL-UNNAMED" +
  79                 " --add-exports java.rmi/sun.rmi.server=ALL-UNNAMED" +
  80                 " --add-exports java.rmi/sun.rmi.transport=ALL-UNNAMED" +
  81                 " --add-exports java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED" +
  82                 " -Djava.rmi.dgc.leaseValue=500000" +
  83                 "  -Dsun.rmi.dgc.checkInterval=" +
  84                 (HOLD_TARGET_TIME - 5000) +
  85                 "   -Drmi.registry.port=" + REGISTRY_PORT +
  86                 "" ;
  87 
  88             testImplVM = new JavaVM("TestImpl", options, "");
  89             testImplVM.start();
  90 
  91             synchronized (test) {
  92                 Thread t = new Thread(test);
  93                 t.setDaemon(true);
  94                 t.start();
  95 
  96                 // wait for the remote calls to take place
  97                 test.wait(TEST_FAIL_TIME);
  98             }
  99 
 100             if (!finished) {
 101                 TestLibrary.bomb("Test failed, had exception or exercise" +
 102                                            " routines took too long to " +
 103                                            "execute");
 104             }
 105             System.err.println("Test passed, exercises " +
 106                                "finished in time.");
 107 
 108         } catch (Exception e) {
 109             testImplVM = null;
 110             TestLibrary.bomb("test failed", e);
 111         }
 112     }
 113 
 114     public void run() {
 115         try {
 116             String echo = null;
 117 
 118             // give the test remote object time to initialize.
 119             Thread.currentThread().sleep(8000);
 120 
 121             // create a test client
 122             Test foo = (Test) Naming.lookup("rmi://:" +
 123                                             REGISTRY_PORT +
 124                                             "/Foo");
 125             echo = foo.echo("Hello world");
 126             System.err.println("Test object created.");
 127 
 128             /* give TestImpl time to lock the target in the
 129              * object table and any dirtys finish.
 130              */
 131             Thread.currentThread().sleep(5000);
 132 
 133             //unreference "Foo"
 134             foo = null;
 135 
 136             //garbage collect and finalize foo
 137             Runtime.getRuntime().gc();
 138             Runtime.getRuntime().runFinalization();
 139 
 140             //import "Bar"
 141             Test bar = (Test) Naming.lookup("rmi://:" +
 142                                             REGISTRY_PORT +
 143                                             "/Bar");
 144 
 145             /* infinite loop to show the liveness of Client,
 146              * if we have deadlock remote call will not return
 147              */
 148             try {
 149                 for (int i = 0; i < 500; i++) {
 150                     echo = bar.echo("Remote call" + i);
 151                     Thread.sleep(10);
 152                 }
 153 
 154                 // flag exercises finished
 155                 finished = true;
 156 
 157             } catch (RemoteException e) {
 158             }
 159 
 160         } catch (Exception e) {
 161             TestLibrary.bomb("test failed", e);
 162         } finally {
 163         }
 164     }
 165 }