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