jdk/test/com/sun/jdi/ExclusiveBind.java
author jbachorik
Mon, 10 Feb 2014 14:01:16 +0100
changeset 22947 c7d9e7b465f9
parent 20201 50cc2d25a60b
child 24509 5d02a6d25f78
permissions -rw-r--r--
6791551: ExclusiveBind.java has a race condition Reviewed-by: sla, sjiang
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4531526
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that more than one debuggee cannot bind to same port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          at the same time.
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    28
 * @library /lib/testlibrary
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    30
 * @build jdk.testlibrary.ProcessTools jdk.testlibrary.JDKToolLauncher jdk.testlibrary.Utils
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @build VMConnection ExclusiveBind HelloWorld
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ExclusiveBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.connect.AttachingConnector;
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    39
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    40
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Iterator;
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    44
import java.util.concurrent.TimeUnit;
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    45
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    46
import jdk.testlibrary.ProcessTools;
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    47
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class ExclusiveBind {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Find a connector by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Launch (in server mode) a debuggee with the given address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * suspend mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    69
    private static ProcessBuilder prepareLauncher(String address, boolean suspend, String class_name) throws Exception {
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    70
        List<String> args = new ArrayList<>();
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    71
        for(String dbgOption : VMConnection.getDebuggeeVMOptions().split(" ")) {
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    72
            args.add(dbgOption);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    74
        String lib = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=";
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    75
        if (suspend) {
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    76
            lib += "y";
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    77
        } else {
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    78
            lib += "n";
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    79
        }
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    80
        lib += ",address=" + address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    82
        args.add(lib);
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    83
        args.add(class_name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
    85
        return ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * - pick a TCP port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * - Launch a debuggee in server=y,suspend=y,address=${port}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * - Launch a second debuggee in server=y,suspend=n with the same port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * - Second debuggee should fail with an error (address already in use)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * - For clean-up we attach to the first debuggee and resume it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // find a free port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String address = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // launch the first debuggee
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   104
        ProcessBuilder process1 = prepareLauncher(address, true, "HelloWorld");
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   105
        // start the debuggee and wait for the "ready" message
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   106
        Process p = ProcessTools.startProcess(
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   107
                "process1",
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   108
                process1,
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   109
                line -> line.equals("Listening for transport dt_socket at address: " + address),
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   110
                Math.round(5000 * Utils.TIMEOUT_FACTOR),
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   111
                TimeUnit.MILLISECONDS
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   112
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // launch a second debuggee with the same address
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   115
        ProcessBuilder process2 = prepareLauncher(address, false, "HelloWorld");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // get exit status from second debuggee
22947
c7d9e7b465f9 6791551: ExclusiveBind.java has a race condition
jbachorik
parents: 20201
diff changeset
   118
        int exitCode = ProcessTools.startProcess("process2", process2).waitFor();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // clean-up - attach to first debuggee and resume it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Map conn_args = conn.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Connector.IntegerArgument port_arg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            (Connector.IntegerArgument)conn_args.get("port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        port_arg.setValue(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        VirtualMachine vm = conn.attach(conn_args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // if the second debuggee ran to completion then we've got a problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (exitCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.println("Test passed - second debuggee correctly failed to bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}