1 /*
   2  * Copyright (c) 2005, 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 4486732
  26  * @summary When a remote stub contains a client socket factory and a
  27  * remote invocation is made using that stub, the factory should not
  28  * be held strongly reachable by the RMI implementation forever; in
  29  * particular, after the stub has become unreachable and all
  30  * connections to its endpoint have been closed, then the factory
  31  * should become unreachable too (through the RMI implementation).
  32  * @author Peter Jones
  33  *
  34  * @run main/othervm -Dsun.rmi.transport.connectionTimeout=2000
  35  *     PinClientSocketFactory
  36  */
  37 
  38 import java.io.IOException;
  39 import java.io.ObjectInputStream;
  40 import java.io.Serializable;
  41 import java.lang.ref.Reference;
  42 import java.lang.ref.WeakReference;
  43 import java.net.ServerSocket;
  44 import java.net.Socket;
  45 import java.rmi.RMISecurityManager;
  46 import java.rmi.Remote;
  47 import java.rmi.RemoteException;
  48 import java.rmi.registry.LocateRegistry;
  49 import java.rmi.registry.Registry;
  50 import java.rmi.server.RMIClientSocketFactory;
  51 import java.rmi.server.RMIServerSocketFactory;
  52 import java.rmi.server.RMISocketFactory;
  53 import java.rmi.server.UnicastRemoteObject;
  54 import java.util.ArrayList;
  55 import java.util.Collections;
  56 import java.util.List;
  57 import java.util.concurrent.atomic.AtomicInteger;
  58 
  59 public class PinClientSocketFactory {
  60 
  61     private static final int SESSIONS = 50;
  62 
  63     public interface Factory extends Remote {
  64         Session getSession() throws RemoteException;
  65     }
  66 
  67     public interface Session extends Remote {
  68         void ping() throws RemoteException;
  69     }
  70 
  71     private static class FactoryImpl implements Factory {
  72         FactoryImpl() { }
  73         public Session getSession() throws RemoteException {
  74             Session impl = new SessionImpl();
  75             UnicastRemoteObject.exportObject(impl, 0, new CSF(), new SSF());
  76             // return impl instead of stub to work around 4114579
  77             return impl;
  78         }
  79     }
  80 
  81     private static class SessionImpl implements Session {
  82         SessionImpl() { }
  83         public void ping() { }
  84     }
  85 
  86     public static void main(String[] args) throws Exception {
  87         System.err.println("\nRegression test for bug 4486732\n");
  88 
  89         // Delegate to capture the actual port being used by the server socket.
  90         DelegateRMISocketFactory delegateFactory = new DelegateRMISocketFactory();
  91         RMISocketFactory.setSocketFactory(delegateFactory);
  92 
  93         Factory factoryImpl = new FactoryImpl();
  94         Factory factoryStub =
  95             (Factory) UnicastRemoteObject.exportObject(factoryImpl, 0);
  96         for (int i = 0; i < SESSIONS; i++) {
  97             Session session = factoryStub.getSession();
  98             session.ping();
  99         }
 100         UnicastRemoteObject.unexportObject(factoryImpl, true);
 101         Registry registryImpl = LocateRegistry.createRegistry(DelegateRMISocketFactory.EPHEMERAL_PORT);
 102         int port = delegateFactory.getServerPort();
 103 
 104         CSF csf = new CSF();
 105         Reference<CSF> registryRef = new WeakReference<CSF>(csf);
 106         Registry registryStub = LocateRegistry.getRegistry("", port, csf);
 107         csf = null;
 108         registryStub.list();
 109         registryStub = null;
 110         UnicastRemoteObject.unexportObject(registryImpl, true);
 111 
 112         System.gc();
 113         // allow connections to time out
 114         Thread.sleep(3 * Long.getLong("sun.rmi.transport.connectionTimeout",
 115                                       15000));
 116         System.gc();
 117 
 118         if (CSF.deserializedInstances.size() != SESSIONS) {
 119             throw new Error("unexpected number of deserialized instances: " +
 120                             CSF.deserializedInstances.size());
 121         }
 122 
 123         int nonNullCount = 0;
 124         for (Reference<CSF> ref : CSF.deserializedInstances) {
 125             csf = ref.get();
 126             if (csf != null) {
 127                 System.err.println("non-null deserialized instance: " + csf);
 128                 nonNullCount++;
 129             }
 130         }
 131         if (nonNullCount > 0) {
 132             throw new Error("TEST FAILED: " +
 133                             nonNullCount + " non-null deserialized instances");
 134         }
 135 
 136         csf = registryRef.get();
 137         if (csf != null) {
 138             System.err.println("non-null registry instance: " + csf);
 139             throw new Error("TEST FAILED: non-null registry instance");
 140         }
 141 
 142         System.err.println("TEST PASSED");
 143     }
 144 
 145     private static class CSF implements RMIClientSocketFactory, Serializable {
 146         static final List<Reference<CSF>> deserializedInstances =
 147             Collections.synchronizedList(new ArrayList<Reference<CSF>>());
 148         private static final AtomicInteger count = new AtomicInteger(0);
 149         private int num = count.incrementAndGet();
 150         CSF() { }
 151         public Socket createSocket(String host, int port) throws IOException {
 152             return new Socket(host, port);
 153         }
 154         public int hashCode() {
 155             return num;
 156         }
 157         public boolean equals(Object obj) {
 158             return obj instanceof CSF && ((CSF) obj).num == num;
 159         }
 160         private void readObject(ObjectInputStream in)
 161             throws IOException, ClassNotFoundException
 162         {
 163             in.defaultReadObject();
 164             deserializedInstances.add(new WeakReference<CSF>(this));
 165         }
 166     }
 167 
 168     private static class SSF implements RMIServerSocketFactory {
 169         SSF() { }
 170         public ServerSocket createServerSocket(int port) throws IOException {
 171             return new ServerSocket(port);
 172         }
 173     }
 174 
 175     /** Delegate socket factory to support listening on an ephemeral port. */
 176     private static class DelegateRMISocketFactory extends RMISocketFactory {
 177         // Map requests for port 9999 to the empemeral port.
 178         public static final int EPHEMERAL_PORT = 9999;
 179 
 180         private static RMISocketFactory factory = RMISocketFactory.getDefaultSocketFactory();
 181         private int port;
 182 
 183         @Override
 184         public ServerSocket createServerSocket(int p) throws IOException {
 185             if (p == EPHEMERAL_PORT)
 186                 p = 0;
 187             ServerSocket ss =  new ServerSocket(p);
 188             port = ss.getLocalPort();
 189             return ss;
 190         }
 191 
 192         @Override
 193         public Socket createSocket(String host, int p) throws IOException {
 194             return factory.createSocket(host, p);
 195         }
 196 
 197         /** Returns the port of the most recently created server socket */
 198         public int getServerPort() {
 199             return port;
 200         }
 201     }
 202 }