jdk/test/com/sun/jdi/NullThreadGroupNameTest.java
author egahlin
Wed, 23 Oct 2013 10:50:34 +0200
changeset 21343 2a4c97049dc6
child 24973 8c4bc3fa4c4e
permissions -rw-r--r--
7105883: JDWP: agent crash if there exists a ThreadGroup with null name Reviewed-by: sla, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21343
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     1
/*
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     4
 *
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     8
 *
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    13
 * accompanied this code).
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    14
 *
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    18
 *
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    21
 * questions.
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    22
 */
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    23
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    24
/**
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    25
 *  @test
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    26
 *  @bug 7105883
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    27
 *  @summary Ensure that JDWP doesn't crash with a null thread group name
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    28
 *
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    29
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    30
 *  @run main NullThreadGroupNameTest
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    31
 */
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    32
import com.sun.jdi.*;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    33
import com.sun.jdi.connect.*;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    34
import com.sun.jdi.event.*;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    35
import com.sun.jdi.request.*;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    36
import com.sun.jdi.VMDisconnectedException;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    37
import java.util.concurrent.CountDownLatch;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    38
import java.util.*;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    39
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    40
class DebugTarget {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    41
    public final static String DEBUG_THREAD_NAME = "DebugThread";
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    42
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    44
        DebugThread thread = new DebugThread();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    45
        thread.start();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    46
        thread.runningLatch.await();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    47
        breakpointHere();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    48
        thread.breakpointLatch.countDown();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    49
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    50
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    51
    public static void breakpointHere() {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    52
        System.out.println("Breakpoint finished!");
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    53
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    54
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    55
    static class DebugThread extends Thread {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    56
        final CountDownLatch runningLatch = new CountDownLatch(1);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    57
        final CountDownLatch breakpointLatch = new CountDownLatch(1);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    58
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    59
        public DebugThread() {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    60
            super(new ThreadGroup(null), DEBUG_THREAD_NAME);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    61
        }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    62
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    63
        public void run() {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    64
            runningLatch.countDown();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    65
            try {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    66
                breakpointLatch.await();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    67
            } catch (InterruptedException ie) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    68
                ie.printStackTrace();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    69
            }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    70
        }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    71
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    72
}
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    73
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    74
public class NullThreadGroupNameTest extends TestScaffold {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    75
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    76
    NullThreadGroupNameTest(String args[]) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    77
        super(args);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    78
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    79
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    80
    public static void main(String[] args) throws Exception {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    81
        new NullThreadGroupNameTest(args).startTests();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    82
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    83
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    84
    protected void runTests() throws Exception {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    85
        startTo("DebugTarget", "breakpointHere", "()V");
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    86
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    87
        ThreadReference thread = findThread(DebugTarget.DEBUG_THREAD_NAME);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    88
        assertThreadGroupName(thread.threadGroup(), "");
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    89
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    90
        listenUntilVMDisconnect();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    91
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    92
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    93
    private ThreadReference findThread(String name) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    94
        for (ThreadReference thread : vm().allThreads()) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    95
            if (name.equals(thread.name())) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    96
                return thread;
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    97
            }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    98
        }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
    99
        throw new NoSuchElementException("Couldn't find " + name);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   100
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   101
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   102
    private void assertThreadGroupName(ThreadGroupReference threadGroup, String expectedName) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   103
        try {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   104
            String name = threadGroup.name();
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   105
            if (!expectedName.equals(name)) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   106
                throw new AssertionError("Unexpected thread group name '" + name + "'");
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   107
            }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   108
        } catch (VMDisconnectedException vmde) {
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   109
            throw new AssertionError("Likely JVM crash with null thread group name", vmde);
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   110
        }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   111
    }
2a4c97049dc6 7105883: JDWP: agent crash if there exists a ThreadGroup with null name
egahlin
parents:
diff changeset
   112
}