Path: blob/master/test/jdk/java/security/Security/ClassLoaderDeadlock/ClassLoaderDeadlock.java
41152 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223// See bug 5094825 / ClassLoadDeadlock.sh2425import java.net.*;2627import java.security.*;2829public class ClassLoaderDeadlock {3031public static void main(String[] args) throws Exception {32// create a new classloader33URL url = new URL("file:provider/");34final DelayClassLoader cl = new DelayClassLoader(url);3536// install a provider on the custom class loader as the most prefered provider37Class clazz = cl.loadClass("HashProvider");38Provider p = (Provider)clazz.newInstance();39Security.insertProviderAt(p, 1);4041// add a delay in class loading for reproducability42cl.delay = 1000;4344new Thread() {45public void run() {46try {47// load a random system class48// this locks the DelayClassLoader and after the delay the system class loader49Class c1 = cl.loadClass("java.lang.String");50System.out.println(c1);51} catch (Exception e) {52e.printStackTrace();53}54}55}.start();5657// make sure the other thread got a chance to run and grab the lock58Thread.sleep(200);5960// load a random class from a signed JAR file to trigger JAR signature verification61// locks system class loader first and then tries to lock DelayClassLoader to load the hashes62// DEADLOCK HAPPENS HERE63Class c2 = Class.forName("com.abc.Tst1");6465// NOT REACHED66System.out.println(c2);6768System.out.println("OK");69}7071static class DelayClassLoader extends URLClassLoader {7273volatile int delay;7475DelayClassLoader(URL url) {76super(new URL[] {url});77}7879protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {80System.out.println("-loadClass(" + name + "," + resolve + ")");81try {82// delay so that we hold the lock for a longer period83// makes it much easier to reproduce84Thread.sleep(delay);85} catch (InterruptedException e) { e.printStackTrace(); }86return super.loadClass(name, resolve);87}88}8990}919293