1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
* Copyright (c) 2018, 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.
*/
/*
* @test
* @bug 4087516
* @summary Incorrect locking leads to deadlock in monitorCacheMaybeExpand.
* @author Anand Palaniswamy
* @build MonitorCacheMaybeExpand_DeadLock
* @run main/othervm MonitorCacheMaybeExpand_DeadLock
*/
/**
* Background on the bug:
*
* The thread local monitor cache had a locking bug (till
* 1.2beta1) where two threads trying to expand the monitor cache
* at the same time would cause deadlock. The code paths that the
* two threads must be executing for this to happen is described
* in the bug report.
*
* Caveat and red-flag:
*
* Since deadlocks are very timing dependent, there is a good
* chance this test case will not catch the bug most of the time
* -- on your machine and setting, it is _possible_ that the two
* threads might not try a monitorCacheExpand at the same
* time. But in practice, on Solaris native threads, this program
* deadlocks the VM in about 2 seconds pretty consistently,
* whether MP or not.
*
* The rationale for running this test despite this rather large
* caveat is that at worst, it can do no harm.
*
* The idea:
*
* Is to create two monitor hungry threads.
*
* Originally Tom Rodriguez and I suspected that this weird state
* of two threads trying to expand monitor cache can happen only
* if:
*
* Thread 1: Is in the middle of a monitorCacheMaybeExpand.
* Thread 2: Runs GC and tries to freeClasses(). This causes
* sysFree() to be invoked, which in turn needs a
* mutex_lock -- and oops, we end up deadlocking
* with 1 on green_threads.
*
* Which is why this test tries to cause class GC at regular
* intervals.
*
* Turns out that the GC is not required. Two instances of the
* monitor hungry threads deadlock the VM pretty quick. :-) Infact
* the static initializer in the forName'd classes running
* alongside one of the hungry threads is sufficient to
* deadlock. Still keep the GC stuff just-in-case (and also
* because I wrote it :-).
*
*/
public class MonitorCacheMaybeExpand_DeadLock {
/**
* A monitor-hungry thread.
*/
static class LotsaMonitors extends Thread {
/** How many recursions? Could cause Java stack overflow. */
static final int MAX_DEPTH = 800;
/** What is our depth? */
int depth = 0;
/** Thread ID */
int tid;
/** So output will have thread number. */
public LotsaMonitors(int tid, int depth) {
super("LotsaMonitors #" + new Integer(tid).toString());
this.tid = tid;
this.depth = depth;
}
/** Start a recursion that grabs monitors. */
public void run() {
System.out.println(">>>Starting " + this.toString() + " ...");
Thread.currentThread().yield();
this.recurse();
System.out.println("<<<Finished " + this.toString());
}
/** Every call to this method grabs an extra monitor. */
synchronized void recurse() {
if (this.depth > 0) {
new LotsaMonitors(tid, depth-1).recurse();
}
}
}
/**
* The test.
*/
public static void main(String[] args) {
/* Start the two of these crazy threads. */
new LotsaMonitors(1, LotsaMonitors.MAX_DEPTH).start();
new LotsaMonitors(2, LotsaMonitors.MAX_DEPTH).start();
/* And sit there and GC for good measure. */
for (int i = 0; i < MAX_GC_ITERATIONS; i++) {
new LotsaMonitors(i+3, LotsaMonitors.MAX_DEPTH).start();
System.out.println(">>>Loading 10 classes and gc'ing ...");
Class[] classes = new Class[10];
fillClasses(classes);
classes = null;
System.gc();
Thread.currentThread().yield();
System.out.println("<<<Finished loading 10 classes and gc'ing");
}
}
/** How many times to GC? */
static final int MAX_GC_ITERATIONS = 10;
/** Load some classes into the array. */
static void fillClasses(Class[] classes) {
for (int i = 0; i < classes.length; i++) {
try {
classes[i] = Class.forName(classnames[i]);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
/** Some random classes to load. */
private static String[] classnames = {
"java.text.DecimalFormat",
"java.text.MessageFormat",
"java.util.GregorianCalendar",
"java.util.ResourceBundle",
"java.text.Collator",
"java.util.Date",
"java.io.Reader",
"java.io.Writer",
"java.lang.IllegalAccessException",
"java.lang.InstantiationException",
"java.lang.ClassNotFoundException",
"java.lang.CloneNotSupportedException",
"java.lang.InterruptedException",
"java.lang.NoSuchFieldException",
"java.lang.NoSuchMethodException",
"java.lang.RuntimeException",
"java.lang.ArithmeticException",
"java.lang.ArrayStoreException",
"java.lang.ClassCastException",
"java.lang.StringIndexOutOfBoundsException",
"java.lang.NegativeArraySizeException",
"java.lang.IllegalStateException",
"java.lang.IllegalArgumentException",
"java.lang.NumberFormatException",
"java.lang.IllegalThreadStateException",
"java.lang.IllegalMonitorStateException",
"java.lang.SecurityException",
"java.lang.ExceptionInInitializerError"
};
}
|