Path: blob/master/test/jdk/java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java
41152 views
/*1* Copyright (c) 2016, 2018, 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/*24* @test25* @bug 815148626* @summary Call Class.forName() on the system classloader from a class loaded27* from a custom classloader.28* @library /test/lib29* @build jdk.test.lib.Utils30* jdk.test.lib.util.JarUtils31* @build ClassForName ClassForNameLeak32* @run main/othervm/policy=test.policy -Djava.security.manager ClassForNameLeak33*/3435import java.io.IOException;36import java.lang.ref.PhantomReference;37import java.lang.ref.Reference;38import java.lang.ref.ReferenceQueue;39import java.net.MalformedURLException;40import java.net.URL;41import java.net.URLClassLoader;42import java.nio.file.Path;43import java.nio.file.Paths;44import java.util.List;45import java.util.concurrent.Callable;46import java.util.concurrent.ExecutorService;47import java.util.concurrent.Executors;48import java.util.concurrent.Future;49import java.util.stream.Collectors;50import java.util.stream.Stream;5152import jdk.test.lib.Utils;53import jdk.test.lib.util.JarUtils;5455/*56* Create .jar, load ClassForName from .jar using a URLClassLoader57*/58public class ClassForNameLeak {59private static final long TIMEOUT = (long)(5000.0 * Utils.TIMEOUT_FACTOR);60private static final int THREADS = 10;61private static final Path jarFilePath = Paths.get("cfn.jar");62private static final ReferenceQueue<ClassLoader> rq = new ReferenceQueue<>();6364static class TestLoader {65private final PhantomReference<ClassLoader> ref;66TestLoader() {67this.ref = loadAndRun();68}6970// Use a new classloader to load the ClassForName class, then run its71// Runnable.72PhantomReference<ClassLoader> loadAndRun() {73try {74ClassLoader classLoader =75new URLClassLoader("LeakedClassLoader",76new URL[]{jarFilePath.toUri().toURL()},77ClassLoader.getPlatformClassLoader());7879Class<?> loadClass = Class.forName("ClassForName", true, classLoader);80((Runnable) loadClass.newInstance()).run();8182return new PhantomReference<>(classLoader, rq);83} catch (MalformedURLException|ReflectiveOperationException e) {84throw new RuntimeException(e);85}86}8788PhantomReference<ClassLoader> getRef() {89return ref;90}91}9293public static void main(String... args) throws Exception {94// create the JAR file95setup();9697// Make simultaneous calls to the test method, to stress things a bit98ExecutorService es = Executors.newFixedThreadPool(THREADS);99100List<Callable<TestLoader>> callables =101Stream.generate(() -> {102Callable<TestLoader> cprcl = TestLoader::new;103return cprcl;104}).limit(THREADS).collect(Collectors.toList());105106List<Future<TestLoader>> futures = es.invokeAll(callables);107108// Give the GC a chance to enqueue the PhantomReferences109for (int i = 0; i < 10; i++) {110System.gc();111}112113// Make sure all PhantomReferences to the leaked classloader are enqueued114for (int j = 0; j < futures.size(); j++) {115Reference rmRef = rq.remove(TIMEOUT);116if (rmRef == null) {117throw new RuntimeException("ClassLoader was never enqueued!");118} else {119System.out.println("Enqueued " + rmRef);120}121}122es.shutdown();123System.out.println("All ClassLoaders successfully enqueued");124}125126private static final String CLASSFILENAME = "ClassForName.class";127private static void setup() throws IOException {128String testclasses = System.getProperty("test.classes", ".");129130// Create a temporary .jar file containing ClassForName.class131Path testClassesDir = Paths.get(testclasses);132JarUtils.createJarFile(jarFilePath, testClassesDir, CLASSFILENAME);133}134}135136137