8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use"
authormli
Wed, 11 Jan 2017 19:36:11 -0800
changeset 43066 bc2234c64fbd
parent 43065 23e5bc13ec20
child 43067 3f011a470ce2
child 43530 f444d3f438dd
8172314: java/rmi/registry/altSecurityManager/AltSecurityManager.java fails with "port in use" Reviewed-by: rriggs
jdk/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java
jdk/test/java/rmi/registry/altSecurityManager/TestSecurityManager.java
jdk/test/java/rmi/registry/altSecurityManager/registry.security.policy
jdk/test/java/rmi/registry/altSecurityManager/rmid.security.policy
jdk/test/java/rmi/registry/reexport/Reexport.java
jdk/test/java/rmi/testlibrary/JavaVM.java
jdk/test/java/rmi/testlibrary/REGISTRY.java
jdk/test/java/rmi/testlibrary/RMID.java
jdk/test/java/rmi/testlibrary/RMIRegistryRunner.java
jdk/test/java/rmi/testlibrary/RegistryRunner.java
jdk/test/java/rmi/testlibrary/RegistryVM.java
jdk/test/java/rmi/testlibrary/TestParams.java
jdk/test/java/rmi/transport/dgcDeadLock/DGCDeadLock.java
jdk/test/java/rmi/transport/dgcDeadLock/registry.security.policy
jdk/test/sun/rmi/transport/tcp/DeadCachedConnection.java
--- a/jdk/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/registry/altSecurityManager/AltSecurityManager.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,9 @@
  *          java.rmi/sun.rmi.server
  *          java.rmi/sun.rmi.transport
  *          java.rmi/sun.rmi.transport.tcp
- * @build TestLibrary JavaVM RMID TestSecurityManager
+ *          java.base/sun.nio.ch
+ * @build TestLibrary RMID RMIDSelectorProvider RegistryVM RMIRegistryRunner
+ *        TestSecurityManager
  * @run main/othervm AltSecurityManager
  */
 
@@ -44,7 +46,6 @@
  * if registry and rmid take too long to exit.
  */
 public class AltSecurityManager implements Runnable {
-    private final int regPort;
     // variable to hold registry and rmid children
     static JavaVM vm = null;
 
@@ -57,31 +58,34 @@
     private static final long TIME_OUT =
             (long)(15000 * TestLibrary.getTimeoutFactor());
 
-    public AltSecurityManager(int port) {
-        if (port <= 0) {
-            TestLibrary.bomb("Port must be greater than 0.");
-        }
-
-        this.regPort = port;
-    }
-
     public void run() {
         try {
             if (utilityToStart.equals(REGISTRY_IMPL)) {
-                vm = new JavaVM(utilityToStart,
-                        " -Djava.security.manager=TestSecurityManager",
-                        Integer.toString(regPort));
+                vm = RegistryVM.createRegistryVMWithRunner(
+                        "RMIRegistryRunner",
+                        "-Djava.security.manager=TestSecurityManager");
             } else if (utilityToStart.contains(ACTIVATION)) {
-                vm = new JavaVM(utilityToStart,
-                        " -Djava.security.manager=TestSecurityManager",
-                        "-port " + Integer.toString(regPort));
+                vm = RMID.createRMIDOnEphemeralPortWithOptions(
+                        "-Djava.security.manager=TestSecurityManager");
             } else {
                 TestLibrary.bomb("Utility to start must be " + REGISTRY_IMPL +
                         " or " + ACTIVATION);
             }
 
             System.err.println("starting " + utilityToStart);
-            vm.execute();
+            try {
+                vm.start();
+                throw new RuntimeException("Expected exception did not occur!");
+            } catch (Exception expected) {
+                int exit = vm.waitFor();
+                if (exit != TestSecurityManager.EXIT_VALUE) {
+                    throw new RuntimeException(utilityToStart
+                            + " exit with an unexpected value "
+                            + exit + ".");
+                }
+                System.err.format("Success: starting %s exited with status %d%n",
+                                  utilityToStart, TestSecurityManager.EXIT_VALUE);
+            }
 
         } catch (Exception e) {
             TestLibrary.bomb(e);
@@ -96,8 +100,7 @@
         utilityToStart = utility;
 
         try {
-            int port = TestLibrary.getUnusedRandomPort();
-            Thread thread = new Thread(new AltSecurityManager(port));
+            Thread thread = new Thread(new AltSecurityManager());
             System.err.println("expecting RuntimeException for " +
                                "checkListen in child process");
             long start = System.currentTimeMillis();
@@ -116,7 +119,7 @@
                                    " terminated on time");
             }
         } finally {
-            vm.destroy();
+            vm.cleanup();
             vm = null;
         }
     }
--- a/jdk/test/java/rmi/registry/altSecurityManager/TestSecurityManager.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/registry/altSecurityManager/TestSecurityManager.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,8 @@
 /**/
 
 public class TestSecurityManager extends SecurityManager {
+    public static final int EXIT_VALUE = 123;
+
     public TestSecurityManager() {
     }
 
@@ -36,7 +38,7 @@
         // by the main test process to detect that the proper security
         // manager has been installed in the relevant VMs.
         //
-        System.exit(1);
+        System.exit(EXIT_VALUE);
     }
 
     public void checkExit(int status) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/rmi/registry/altSecurityManager/registry.security.policy	Wed Jan 11 19:36:11 2017 -0800
@@ -0,0 +1,8 @@
+grant {
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.rmi.registry";
+  permission java.util.PropertyPermission "env.class.path", "read";
+  permission java.io.FilePermission ".", "read";
+  permission java.util.PropertyPermission "user.dir", "read";
+  permission java.lang.RuntimePermission "createClassLoader";
+  permission java.lang.RuntimePermission "setContextClassLoader";
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/rmi/registry/altSecurityManager/rmid.security.policy	Wed Jan 11 19:36:11 2017 -0800
@@ -0,0 +1,7 @@
+grant {
+  permission java.lang.RuntimePermission "selectorProvider";
+  permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";
+  permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.port", "read";
+  permission java.util.PropertyPermission "test.java.rmi.testlibrary.RMIDSelectorProvider.timeout", "read";
+  permission java.net.SocketPermission "*:1024-", "listen,resolve,connect,accept";
+};
--- a/jdk/test/java/rmi/registry/reexport/Reexport.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/registry/reexport/Reexport.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,7 +29,7 @@
  *          java.rmi/sun.rmi.server
  *          java.rmi/sun.rmi.transport
  *          java.rmi/sun.rmi.transport.tcp
- * @build TestLibrary REGISTRY RegistryRunner
+ * @build TestLibrary RegistryVM RegistryRunner
  * @run main/othervm Reexport
  */
 
@@ -114,7 +114,7 @@
 
     public static void makeRegistry() {
         try {
-            subreg = REGISTRY.createREGISTRY();
+            subreg = RegistryVM.createRegistryVM();
             subreg.start();
             port = subreg.getPort();
             System.out.println("Starting registry on port " + port);
@@ -125,12 +125,12 @@
         }
     }
 
-    private static REGISTRY subreg = null;
+    private static RegistryVM subreg = null;
     private static int port = -1;
 
     public static void killRegistry() {
         if (subreg != null) {
-            subreg.shutdown();
+            subreg.cleanup();
             subreg = null;
         }
     }
--- a/jdk/test/java/rmi/testlibrary/JavaVM.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/testlibrary/JavaVM.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -230,6 +230,22 @@
     }
 
     /**
+     * Return exit value for vm process.
+     * @return exit value for vm process
+     * @throws IllegalThreadStateException if the vm process has not yet terminated
+     */
+    public int exitValue() {
+        return vm.exitValue();
+    }
+
+    /**
+     * Destroy the vm process, and do necessary cleanup.
+     */
+    public void cleanup() {
+        destroy();
+    }
+
+    /**
      * Destroys the VM, waits for it to terminate, and returns
      * its exit status.
      *
--- a/jdk/test/java/rmi/testlibrary/REGISTRY.java	Wed Jan 11 08:53:59 2017 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,119 +0,0 @@
-/*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-import java.io.OutputStream;
-import java.io.IOException;
-
-/**
- * Class to run and control rmiregistry in a sub-process.
- *
- * We can't kill a registry if we have too-close control
- * over it.  We must make it in a subprocess, and then kill the
- * subprocess when it has served our needs.
- */
-public class REGISTRY extends JavaVM {
-
-    private static final double START_TIMEOUT =
-            20_000 * TestLibrary.getTimeoutFactor();
-    private static final String DEFAULT_RUNNER = "RegistryRunner";
-
-    private int port = -1;
-
-    private REGISTRY(String runner, OutputStream out, OutputStream err,
-                    String options, int port) {
-        super(runner, options, Integer.toString(port), out, err);
-        try {
-            Class runnerClass = Class.forName(runner);
-            if (!RegistryRunner.class.isAssignableFrom(runnerClass)) {
-                throw new RuntimeException("runner class must be RegistryRunner"
-                        + " or its sub class");
-            }
-        } catch (ClassNotFoundException ex) {
-            throw new RuntimeException(ex);
-        }
-        this.port = port;
-    }
-
-    public static REGISTRY createREGISTRY() {
-        return createREGISTRYWithRunner(DEFAULT_RUNNER, System.out, System.err, "", 0);
-    }
-
-    public static REGISTRY createREGISTRY(OutputStream out, OutputStream err,
-                                    String options, int port) {
-        return createREGISTRYWithRunner(DEFAULT_RUNNER, out, err, options, port);
-    }
-
-    public static REGISTRY createREGISTRYWithRunner(String runner, String options) {
-        return createREGISTRYWithRunner(runner, System.out, System.err, options, 0);
-    }
-
-    public static REGISTRY createREGISTRYWithRunner(String runner, OutputStream out,
-                                        OutputStream err, String options, int port) {
-        options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED"
-                + " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED"
-                + " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED"
-                + " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED";
-       REGISTRY reg = new REGISTRY(runner, out, err, options, port);
-       return reg;
-    }
-
-    /**
-     * Starts the registry in a sub-process and waits up to
-     * the given timeout period to confirm that it's running,
-     * and get the port where it's running.
-     */
-    public void start() throws IOException {
-        super.start();
-        long startTime = System.currentTimeMillis();
-        long deadline = TestLibrary.computeDeadline(startTime, (long)START_TIMEOUT);
-        while (true) {
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException ignore) { }
-
-            String output = outputStream.ba.toString();
-            port = RegistryRunner.getRegistryPort(output);
-            if (port != -1) {
-                break;
-            }
-            if (System.currentTimeMillis() > deadline) {
-                TestLibrary.bomb("Failed to start registry, giving up after " +
-                    (System.currentTimeMillis() - startTime) + "ms.", null);
-            }
-        }
-    }
-
-    /**
-     * Shuts down the registry.
-     */
-    public void shutdown() {
-        RegistryRunner.requestExit(port);
-    }
-
-    /**
-     * Gets the port where the registry is serving.
-     */
-    public int getPort() {
-        return port;
-    }
-}
--- a/jdk/test/java/rmi/testlibrary/RMID.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/testlibrary/RMID.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -140,18 +140,6 @@
     }
 
     private static String makeArgs(boolean includePortArg, int port) {
-        String propagateManager = null;
-
-        // rmid will run with a security manager set, but no policy
-        // file - it should not need one.
-        if (System.getSecurityManager() == null) {
-            propagateManager = MANAGER_OPTION +
-                TestParams.defaultSecurityManager;
-        } else {
-            propagateManager = MANAGER_OPTION +
-                System.getSecurityManager().getClass().getName();
-        }
-
         // getAbsolutePath requires permission to read user.dir
         String args =
             " -log " + (new File(LOGDIR, log)).getAbsolutePath();
@@ -210,7 +198,30 @@
                                   boolean debugExec, boolean includePortArg,
                                   int port)
     {
+        return createRMIDWithOptions(out, err, debugExec, includePortArg, port, "");
+    }
+
+    /**
+     * Create a RMID on a specified port capturing stdout and stderr
+     * with additional command line options and whether to print out
+     * debugging information that is used for spawning activation groups.
+     *
+     * @param out the OutputStream where the normal output of the
+     *            rmid subprocess goes
+     * @param err the OutputStream where the error output of the
+     *            rmid subprocess goes
+     * @param debugExec whether to print out debugging information
+     * @param includePortArg whether to include port argument
+     * @param port the port on which rmid accepts requests
+     * @param additionalOptions additional command line options
+     * @return a RMID instance
+     */
+    public static RMID createRMIDWithOptions(OutputStream out, OutputStream err,
+                                  boolean debugExec, boolean includePortArg,
+                                  int port, String additionalOptions)
+    {
         String options = makeOptions(port, debugExec, false);
+        options += " " + additionalOptions;
         String args = makeArgs(includePortArg, port);
         RMID rmid = new RMID("sun.rmi.server.Activation", options, args,
                              out, err, port);
@@ -223,6 +234,19 @@
         return createRMID(System.out, System.err, true, false, 0);
     }
 
+    /**
+     * Create a RMID on an ephemeral port capturing stdout and stderr
+     * with additional command line options.
+     *
+     * @param additionalOptions additional command line options
+     * @return a RMID instance
+     */
+    public static RMID createRMIDOnEphemeralPortWithOptions(
+                                            String additionalOptions) {
+        return createRMIDWithOptions(System.out, System.err,
+                                     true, false, 0, additionalOptions);
+    }
+
     public static RMID createRMIDOnEphemeralPort(OutputStream out,
                                                  OutputStream err,
                                                  boolean debugExec)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/rmi/testlibrary/RMIRegistryRunner.java	Wed Jan 11 19:36:11 2017 -0800
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**/
+
+import java.rmi.*;
+import java.rmi.registry.*;
+import java.rmi.server.*;
+
+/**
+ * Class to run a rmiregistry whose VM can be told to exit remotely;
+ * Difference between this class and RegistryRunner is that this class
+ * simulate rmiregistry closer than RegistryRunner.
+ */
+public class RMIRegistryRunner extends RegistryRunner
+{
+    public RMIRegistryRunner() throws RemoteException {
+    }
+
+    /**
+     * port 0 means to use ephemeral port to start registry.
+     *
+     * @param args command line arguments passed in from main
+     * @return the port number on which registry accepts requests
+     */
+    protected static int init(String[] args) {
+        try {
+            if (args.length == 0) {
+                System.err.println("Usage: <port>");
+                System.exit(0);
+            }
+            int port = -1;
+            port = Integer.parseInt(args[0]);
+
+            // call RegistryImpl.createRegistry to simulate rmiregistry.
+            registry = sun.rmi.registry.RegistryImpl.createRegistry(port);
+            if (port == 0) {
+                port = TestLibrary.getRegistryPort(registry);
+            }
+
+            // create a remote object to tell this VM to exit
+            exiter = new RMIRegistryRunner();
+            Naming.rebind("rmi://localhost:" + port +
+                          "/RemoteExiter", exiter);
+
+            return port;
+        } catch (Exception e) {
+            System.err.println(e.getMessage());
+            e.printStackTrace();
+            System.exit(1);
+        }
+        return -1;
+    }
+
+    public static void main(String[] args) {
+        int port = init(args);
+        notify(port);
+    }
+}
--- a/jdk/test/java/rmi/testlibrary/RegistryRunner.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/testlibrary/RegistryRunner.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,7 +29,7 @@
 
 /**
  * Class to run a registry whose VM can be told to exit remotely; using
- * the rmiregistry in this fashion makes tests more robust under
+ * a registry (in a sub-process) in this fashion makes tests more robust under
  * windows where Process.destroy() seems not to be 100% reliable.
  */
 public class RegistryRunner extends UnicastRemoteObject
@@ -38,8 +38,8 @@
     private static final String PORT_LABEL_START = "RegistryRunner.port.start:";
     private static final String PORT_LABEL_END = ":RegistryRunner.port.end";
 
-    private static Registry registry = null;
-    private static RemoteExiter exiter = null;
+    protected static Registry registry = null;
+    protected static RemoteExiter exiter = null;
 
     public RegistryRunner() throws RemoteException {
     }
@@ -72,6 +72,7 @@
             } catch (RemoteException re) {
             }
             e = null;
+
         } catch (java.net.MalformedURLException mfue) {
             // will not happen
         } catch (NotBoundException nbe) {
@@ -97,6 +98,9 @@
 
     /**
      * port 0 means to use ephemeral port to start registry.
+     *
+     * @param args command line arguments passed in from main
+     * @return the port number on which registry accepts requests
      */
     protected static int init(String[] args) {
         try {
@@ -128,13 +132,15 @@
     }
 
     /**
-     * REGISTRY.start() will filter the output of registry subprocess,
-     * when valid port is detected, REGISTRY.start() returns.
+     * RegistryVM.start() will filter the output of registry subprocess,
+     * when valid port is detected, RegistryVM.start() returns.
      * So, for subclass, it's important to call this method after registry
      * is initialized and necessary remote objects have been bound.
+     *
+     * @param port the port on which registry accepts requests
      */
     protected static void notify(int port) {
-        // this output is important for REGISTRY to get the port
+        // this output is important for RegistryVM to get the port
         // where rmiregistry is serving
         System.out.println(PORT_LABEL_START + port + PORT_LABEL_END);
         System.out.flush();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/rmi/testlibrary/RegistryVM.java	Wed Jan 11 19:36:11 2017 -0800
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * Class to run and control registry/rmiregistry in a sub-process.
+ * The behaviour changes when use different runner, currently
+ * there are 2 built-in runners, RegistryRunner and RMIRegistryRunner.
+ *
+ * We can't kill a registry if we have too-close control
+ * over it.  We must make it in a subprocess, and then kill the
+ * subprocess when it has served our needs.
+ */
+public class RegistryVM extends JavaVM {
+
+    private static final double START_TIMEOUT =
+            20_000 * TestLibrary.getTimeoutFactor();
+    private static final String DEFAULT_RUNNER = "RegistryRunner";
+
+    private int port = -1;
+
+    private RegistryVM(String runner, OutputStream out, OutputStream err,
+                    String options, int port) {
+        super(runner, options, Integer.toString(port), out, err);
+        try {
+            Class runnerClass = Class.forName(runner);
+            if (!RegistryRunner.class.isAssignableFrom(runnerClass)) {
+                throw new RuntimeException("runner class must be RegistryRunner"
+                        + " or its sub class");
+            }
+        } catch (ClassNotFoundException ex) {
+            throw new RuntimeException(ex);
+        }
+        this.port = port;
+    }
+
+    /**
+     * Create a RegistryVM instance on an ephemeral port.
+     *
+     * @return a RegistryVM instance
+     */
+    public static RegistryVM createRegistryVM() {
+        return createRegistryVMWithRunner(DEFAULT_RUNNER, System.out, System.err, "", 0);
+    }
+
+    /**
+     * Create a RegistryVM instance on an ephemeral port with additional
+     * command line options.
+     *
+     * @param options command line options
+     * @return a RegistryVM instance
+     */
+    public static RegistryVM createRegistryVM(String options) {
+        return createRegistryVMWithRunner(
+                DEFAULT_RUNNER, System.out, System.err, options, 0);
+    }
+
+    /**
+     * Create a RegistryVM instance on a specified port capturing stdout and
+     * stderr with additional command line options.
+     *
+     * @param out the OutputStream where the normal output of the
+     *            registry subprocess goes
+     * @param err the OutputStream where the error output of the
+     *            registry subprocess goes
+     * @param options the command line options
+     * @param port the port on which Registry accepts requests
+     * @return a RegistryVM instance
+     */
+    public static RegistryVM createRegistryVM(OutputStream out, OutputStream err,
+                                              String options, int port) {
+        return createRegistryVMWithRunner(DEFAULT_RUNNER, out, err, options, port);
+    }
+
+    /**
+     * Create a RegistryVM instance on an ephemeral port with additional
+     * command line options and a specified runner.
+     *
+     * @param runner the runner class name
+     * @param options command line options
+     * @return a RegistryVM instance
+     */
+    public static RegistryVM createRegistryVMWithRunner(String runner, String options) {
+        return createRegistryVMWithRunner(runner, System.out, System.err, options, 0);
+    }
+
+    /**
+     * Create a RegistryVM instance on a specified port capturing stdout and
+     * stderr with additional command line options and a specified runner.
+     *
+     * @param runner the runner class name
+     * @param out the OutputStream where the normal output of the
+     *            registry subprocess goes
+     * @param err the OutputStream where the error output of the
+     *            registry subprocess goes
+     * @param options the command line options
+     * @param port the port on which Registry accepts requests
+     * @return a RegistryVM instance
+     */
+    public static RegistryVM createRegistryVMWithRunner(String runner, OutputStream out,
+                                        OutputStream err, String options, int port) {
+        options += " --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED"
+                + " --add-exports=java.rmi/sun.rmi.server=ALL-UNNAMED"
+                + " --add-exports=java.rmi/sun.rmi.transport=ALL-UNNAMED"
+                + " --add-exports=java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED";
+        RegistryVM reg = new RegistryVM(runner, out, err, options, port);
+        reg.setPolicyFile(TestParams.defaultRegistryPolicy);
+        return reg;
+    }
+
+    /**
+     * Starts the registry in a sub-process and waits up to
+     * the given timeout period to confirm that it's running,
+     * and get the port where it's running.
+     *
+     * @throws IOException if fails to start subprocess
+     */
+    public void start() throws IOException {
+        super.start();
+        long startTime = System.currentTimeMillis();
+        long deadline = TestLibrary.computeDeadline(startTime, (long)START_TIMEOUT);
+        while (true) {
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException ignore) { }
+
+            String output = outputStream.ba.toString();
+            port = RegistryRunner.getRegistryPort(output);
+            if (port != -1) {
+                break;
+            }
+            try {
+                int exit = vm.exitValue();
+                TestLibrary.bomb("[RegistryVM] registry sub-process exited with status "
+                        + exit + ".");
+            } catch (IllegalThreadStateException ignore) { }
+
+            if (System.currentTimeMillis() > deadline) {
+                TestLibrary.bomb("Failed to start registry, giving up after " +
+                    (System.currentTimeMillis() - startTime) + "ms.", null);
+            }
+        }
+    }
+
+    /**
+     * Shuts down the registry.
+     */
+    @Override
+    public void cleanup() {
+        RegistryRunner.requestExit(port);
+        super.destroy();
+    }
+
+    /**
+     * Gets the port where the registry is serving.
+     *
+     * @return the port where the registry is serving
+     */
+    public int getPort() {
+        return port;
+    }
+}
--- a/jdk/test/java/rmi/testlibrary/TestParams.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/testlibrary/TestParams.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,9 @@
     /** name of default security policy for RMID */
     public static final String defaultRmidPolicy;
 
+    /** name of default security policy for RegistryVM */
+    public static final String defaultRegistryPolicy;
+
     /** name of default security policy for activation groups */
     public static final String defaultGroupPolicy;
 
@@ -69,6 +72,9 @@
         defaultRmidPolicy =
             testSrc + File.separatorChar + "rmid.security.policy";
 
+        defaultRegistryPolicy =
+            testSrc + File.separatorChar + "registry.security.policy";
+
         defaultGroupPolicy =
             testSrc + File.separatorChar + "group.security.policy";
 
--- a/jdk/test/java/rmi/transport/dgcDeadLock/DGCDeadLock.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/java/rmi/transport/dgcDeadLock/DGCDeadLock.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,7 @@
  *          java.rmi/sun.rmi.server
  *          java.rmi/sun.rmi.transport
  *          java.rmi/sun.rmi.transport.tcp
- * @build TestLibrary Test TestImpl REGISTRY RegistryRunner
+ * @build TestLibrary Test TestImpl RegistryVM RegistryRunner
  * @run main/othervm/policy=security.policy/timeout=360 DGCDeadLock
  */
 
@@ -68,21 +68,18 @@
 
     static public void main(String[] args) {
 
-        REGISTRY testImplVM = null;
+        RegistryVM testImplVM = null;
 
         System.err.println("\nregression test for 4118056\n");
         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 
         try {
-            String options = " -Djava.security.policy=" +
-                TestParams.defaultPolicy +
-                " --add-opens java.rmi/sun.rmi.transport=ALL-UNNAMED" +
+            String options = " --add-opens java.rmi/sun.rmi.transport=ALL-UNNAMED" +
                 " -Djava.rmi.dgc.leaseValue=500000" +
                 " -Dsun.rmi.dgc.checkInterval=" +
-                (HOLD_TARGET_TIME - 5000) +
-                "" ;
+                (HOLD_TARGET_TIME - 5000);
 
-            testImplVM = REGISTRY.createREGISTRYWithRunner("TestImpl", options);
+            testImplVM = RegistryVM.createRegistryVMWithRunner("TestImpl", options);
             testImplVM.start();
             registryPort = testImplVM.getPort();
 
@@ -107,7 +104,7 @@
             TestLibrary.bomb("test failed in main()", e);
         } finally {
             if (testImplVM != null) {
-                testImplVM.shutdown();
+                testImplVM.cleanup();
                 testImplVM = null;
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/rmi/transport/dgcDeadLock/registry.security.policy	Wed Jan 11 19:36:11 2017 -0800
@@ -0,0 +1,27 @@
+/*
+ * security policy used by the registry sub-process
+ */
+
+grant {
+  // used by TestLibrary to determine extra commandline properties
+  permission java.io.FilePermission "..${/}..${/}test.props", "read";
+
+  // property specifically accessed by this test.
+  permission java.util.PropertyPermission "sun.rmi.transport.cleanInterval", "write";
+  permission java.util.PropertyPermission "package.restrict.access.sun", "read";
+  permission java.util.PropertyPermission "package.restrict.access.sun.rmi", "read";
+
+  // test needs to use java to exec an EchoImpl object
+  permission java.io.FilePermission "${java.home}${/}bin${/}java", "execute";
+
+  // used by TestLibrary to determine test environment 
+  permission java.util.PropertyPermission "test.*", "read";
+  permission java.util.PropertyPermission "user.dir", "read";
+  permission java.util.PropertyPermission "java.home", "read";
+
+  permission java.util.PropertyPermission "java.security.policy", "read";
+  permission java.util.PropertyPermission "java.security.manager", "read";
+
+  // test needs to export rmid and communicate with objects on arbitrary ports
+  permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
+};
--- a/jdk/test/sun/rmi/transport/tcp/DeadCachedConnection.java	Wed Jan 11 08:53:59 2017 -0800
+++ b/jdk/test/sun/rmi/transport/tcp/DeadCachedConnection.java	Wed Jan 11 19:36:11 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -29,7 +29,7 @@
  *          java.rmi/sun.rmi.server
  *          java.rmi/sun.rmi.transport
  *          java.rmi/sun.rmi.transport.tcp
- * @build TestLibrary REGISTRY RegistryRunner
+ * @build TestLibrary RegistryVM RegistryRunner
  * @run main/othervm DeadCachedConnection
  */
 
@@ -100,7 +100,7 @@
 
     public static int makeRegistry(int port) {
         try {
-            subreg = REGISTRY.createREGISTRY(System.out, System.err, "", port);
+            subreg = RegistryVM.createRegistryVM(System.out, System.err, "", port);
             subreg.start();
             int regPort = subreg.getPort();
             System.out.println("Starting registry on port " + regPort);
@@ -113,11 +113,11 @@
         return -1;
     }
 
-    private static REGISTRY subreg = null;
+    private static RegistryVM subreg = null;
 
     public static void killRegistry() throws InterruptedException {
         if (subreg != null) {
-            subreg.shutdown();
+            subreg.cleanup();
             subreg = null;
         }
     }