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