1 /*
   2  * Copyright (c) 1998, 2008, 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 4116437
  26  * @summary Distributed Garbage Collector Memory Leak
  27  *
  28  * @author Laird Dornin
  29  *
  30  * @library ../../testlibrary
  31  * @build CheckLeaseLeak CheckLeaseLeak_Stub LeaseLeakClient LeaseLeak
  32  * @run main/othervm/timeout=240 CheckLeaseLeak
  33  *
  34  */
  35 
  36 /**
  37  * A bug in sun.rmi.transport.DGCImp.checkLeases() results in memory
  38  * leak of LeaseInfo objects.
  39  *
  40  * In order to verify that this problem no longer exists, we create a
  41  * remote object and a serveral clients in different VMs. The clients
  42  * call a remote method on an exported object. This will cause the rmi
  43  * runtime to create several references (all with different vmids) to
  44  * the remote object.  Each vmid needs a seperate LeaseInfo object in
  45  * the object table target DGCImpl.leaseTable.  If the leak is fixed,
  46  * the leaseTable field will contain no objects.  We use reflection to
  47  * find the number of objects contained in this table.
  48  */
  49 
  50 import java.rmi.*;
  51 import java.rmi.server.*;
  52 import sun.rmi.transport.*;
  53 import sun.rmi.*;
  54 import java.util.Map;
  55 import java.io.*;
  56 import java.lang.reflect.*;
  57 import java.rmi.registry.*;
  58 
  59 public class CheckLeaseLeak extends UnicastRemoteObject implements LeaseLeak {
  60 
  61     public CheckLeaseLeak() throws RemoteException { }
  62     public void ping () throws RemoteException { }
  63 
  64     /**
  65      * Id to fake the DGC_ID, so we can later get a reference to the
  66      * DGCImpl in the object table.
  67      */
  68     private final static int DGC_ID = 2;
  69 
  70     private final static int ITERATIONS = 10;
  71     private final static int numberPingCalls = 0;
  72     private final static int CHECK_INTERVAL = 400;
  73     private final static int LEASE_VALUE = 20;
  74 
  75     public static void main (String[] args) {
  76         CheckLeaseLeak leakServer = null;
  77         int numLeft =0;
  78 
  79         /*
  80          * we want DGC to collect leases *quickly*
  81          * decrease the lease check interval
  82          */
  83         TestLibrary.setInteger("sun.rmi.dgc.checkInterval",
  84                                CHECK_INTERVAL);
  85         TestLibrary.setInteger("java.rmi.dgc.leaseValue",
  86                                LEASE_VALUE);
  87 
  88         try {
  89             Registry registry =
  90                 java.rmi.registry.LocateRegistry.
  91                     createRegistry(TestLibrary.REGISTRY_PORT);
  92 
  93             leakServer = new CheckLeaseLeak();
  94             registry.rebind("/LeaseLeak", leakServer);
  95 
  96             /* create a bunch of clients in a *different* vm */
  97             for (int i = 0 ; i < ITERATIONS ; i ++ ) {
  98                 System.err.println("Created client: " + i);
  99 
 100                 JavaVM jvm = new JavaVM("LeaseLeakClient",
 101                                         " -Djava.security.policy=" +
 102                                         TestParams.defaultPolicy, "");
 103                 jvm.start();
 104 
 105                 if (jvm.getVM().waitFor() == 1 ) {
 106                     TestLibrary.bomb("Client process failed");
 107                 }
 108             }
 109             numLeft = getDGCLeaseTableSize();
 110             Thread.sleep(3000);
 111 
 112         } catch(Exception e) {
 113             TestLibrary.bomb("CheckLeaseLeak Error: ", e);
 114         } finally {
 115             if (leakServer != null) {
 116                 TestLibrary.unexport(leakServer);
 117                 leakServer = null;
 118             }
 119         }
 120 
 121         /* numLeft should be 2 - if 11 there is a problem. */
 122         if (numLeft > 2) {
 123             TestLibrary.bomb("Too many objects in DGCImpl.leaseTable: "+
 124                             numLeft);
 125         } else {
 126             System.err.println("Check leaseInfo leak passed with " +
 127                                numLeft
 128                                    + " object(s) in the leaseTable");
 129         }
 130     }
 131 
 132     /**
 133      * Obtain a reference to the main DGCImpl via reflection.  Extract
 134      * the DGCImpl using the ObjectTable and the well known ID of the
 135      * DGCImpl.
 136      */
 137     private static int getDGCLeaseTableSize () {
 138         int numLeaseInfosLeft = 0;
 139 
 140         /**
 141          * Will eventually be set to point at the leaseTable inside
 142          * DGCImpl.
 143          */
 144         Map leaseTable = null;
 145         final Remote[] dgcImpl = new Remote[1];
 146         Field f;
 147 
 148         try {
 149             f = (Field) java.security.AccessController.doPrivileged
 150                 (new java.security.PrivilegedExceptionAction() {
 151                     public Object run() throws Exception {
 152 
 153                         ObjID dgcID = new ObjID(DGC_ID);
 154 
 155                         /*
 156                          * Construct an ObjectEndpoint containing DGC's
 157                          * ObjID.
 158                          */
 159                         Class oeClass =
 160                             Class.forName("sun.rmi.transport.ObjectEndpoint");
 161                         Class[] constrParams =
 162                             new Class[]{ ObjID.class, Transport.class };
 163                         Constructor oeConstructor =
 164                             oeClass.getDeclaredConstructor(constrParams);
 165                         oeConstructor.setAccessible(true);
 166                         Object oe =
 167                             oeConstructor.newInstance(
 168                                 new Object[]{ dgcID, null });
 169 
 170                         /*
 171                          * Get Target that contains DGCImpl in ObjectTable
 172                          */
 173                         Class objTableClass =
 174                             Class.forName("sun.rmi.transport.ObjectTable");
 175                         Class getTargetParams[] = new Class[] { oeClass };
 176                         Method objTableGetTarget =
 177                             objTableClass.getDeclaredMethod("getTarget",
 178                                                             getTargetParams);
 179                         objTableGetTarget.setAccessible(true);
 180                         Target dgcTarget = (Target)
 181                             objTableGetTarget.invoke(null, new Object[]{ oe });
 182 
 183                         /* get the DGCImpl from its Target */
 184                         Method targetGetImpl =
 185                             dgcTarget.getClass().getDeclaredMethod
 186                             ("getImpl", null);
 187                         targetGetImpl.setAccessible(true);
 188                         dgcImpl[0] =
 189                             (Remote) targetGetImpl.invoke(dgcTarget, null);
 190 
 191                         /* Get the lease table from the DGCImpl. */
 192                         Field reflectedLeaseTable =
 193                             dgcImpl[0].getClass().getDeclaredField
 194                             ("leaseTable");
 195                         reflectedLeaseTable.setAccessible(true);
 196 
 197                         return reflectedLeaseTable;
 198                     }
 199             });
 200 
 201             /**
 202              * This is the leaseTable that will fill up with LeaseInfo
 203              * objects if the LeaseInfo memory leak is not fixed.
 204              */
 205             leaseTable = (Map) f.get(dgcImpl[0]);
 206 
 207             numLeaseInfosLeft = leaseTable.size();
 208 
 209         } catch(Exception e) {
 210             if (e instanceof java.security.PrivilegedActionException)
 211                 e = ((java.security.PrivilegedActionException) e).
 212                     getException();
 213             TestLibrary.bomb(e);
 214         }
 215 
 216         return numLeaseInfosLeft;
 217     }
 218 }