2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
// See bug 5094825 / ClassLoadDeadlock.sh
|
|
25 |
|
|
26 |
import java.net.*;
|
|
27 |
|
|
28 |
import java.security.*;
|
|
29 |
|
|
30 |
public class ClassLoaderDeadlock {
|
|
31 |
|
|
32 |
public static void main(String[] args) throws Exception {
|
|
33 |
// create a new classloader
|
|
34 |
URL url = new URL("file:provider/");
|
|
35 |
final DelayClassLoader cl = new DelayClassLoader(url);
|
|
36 |
|
|
37 |
// install a provider on the custom class loader as the most prefered provider
|
|
38 |
Class clazz = cl.loadClass("HashProvider");
|
|
39 |
Provider p = (Provider)clazz.newInstance();
|
|
40 |
Security.insertProviderAt(p, 1);
|
|
41 |
|
|
42 |
// add a delay in class loading for reproducability
|
|
43 |
cl.delay = 1000;
|
|
44 |
|
|
45 |
new Thread() {
|
|
46 |
public void run() {
|
|
47 |
try {
|
|
48 |
// load a random system class
|
|
49 |
// this locks the DelayClassLoader and after the delay the system class loader
|
|
50 |
Class c1 = cl.loadClass("java.lang.String");
|
|
51 |
System.out.println(c1);
|
|
52 |
} catch (Exception e) {
|
|
53 |
e.printStackTrace();
|
|
54 |
}
|
|
55 |
}
|
|
56 |
}.start();
|
|
57 |
|
|
58 |
// make sure the other thread got a chance to run and grab the lock
|
|
59 |
Thread.sleep(200);
|
|
60 |
|
|
61 |
// load a random class from a signed JAR file to trigger JAR signature verification
|
|
62 |
// locks system class loader first and then tries to lock DelayClassLoader to load the hashes
|
|
63 |
// DEADLOCK HAPPENS HERE
|
|
64 |
Class c2 = Class.forName("com.abc.Tst1");
|
|
65 |
|
|
66 |
// NOT REACHED
|
|
67 |
System.out.println(c2);
|
|
68 |
|
|
69 |
System.out.println("OK");
|
|
70 |
}
|
|
71 |
|
|
72 |
static class DelayClassLoader extends URLClassLoader {
|
|
73 |
|
|
74 |
volatile int delay;
|
|
75 |
|
|
76 |
DelayClassLoader(URL url) {
|
|
77 |
super(new URL[] {url});
|
|
78 |
}
|
|
79 |
|
|
80 |
protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
|
81 |
System.out.println("-loadClass(" + name + "," + resolve + ")");
|
|
82 |
try {
|
|
83 |
// delay so that we hold the lock for a longer period
|
|
84 |
// makes it much easier to reproduce
|
|
85 |
Thread.sleep(delay);
|
|
86 |
} catch (InterruptedException e) { e.printStackTrace(); }
|
|
87 |
return super.loadClass(name, resolve);
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
}
|