Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java
38821 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 /lib/testlibrary29* @build jdk.testlibrary.Utils jdk.testlibrary.JarUtils30* @build ClassForName ClassForNameLeak31* @run main/othervm/policy=test.policy -Djava.security.manager ClassForNameLeak32*/3334import java.io.IOException;35import java.lang.ref.PhantomReference;36import java.lang.ref.Reference;37import java.lang.ref.ReferenceQueue;38import java.net.MalformedURLException;39import java.net.URL;40import java.net.URLClassLoader;41import java.nio.file.Path;42import java.nio.file.Paths;43import java.util.List;44import java.util.concurrent.Callable;45import java.util.concurrent.ExecutorService;46import java.util.concurrent.Executors;47import java.util.concurrent.Future;48import java.util.stream.Collectors;49import java.util.stream.Stream;50import jdk.testlibrary.Utils;51import jdk.testlibrary.JarUtils;5253/*54* Create .jar, load ClassForName from .jar using a URLClassLoader55*/56public class ClassForNameLeak {57private static final long TIMEOUT = (long)(5000.0 * Utils.TIMEOUT_FACTOR);58private static final int THREADS = 10;59private static final Path jarFilePath = Paths.get("cfn.jar");60private static final ReferenceQueue<ClassLoader> rq = new ReferenceQueue<>();6162static class TestLoader {63private final PhantomReference<ClassLoader> ref;6465TestLoader() {66this.ref = loadAndRun();67}6869// Use a new classloader to load the ClassForName class, then run its70// Runnable.71PhantomReference<ClassLoader> loadAndRun() {72try {73ClassLoader classLoader =74new URLClassLoader(new URL[]{jarFilePath.toUri().toURL()},75ClassLoader.getSystemClassLoader());7677Class<?> loadClass = Class.forName("ClassForName", true, classLoader);78((Runnable) loadClass.newInstance()).run();7980return new PhantomReference<>(classLoader, rq);81} catch (MalformedURLException | ReflectiveOperationException e) {82throw new RuntimeException(e);83}84}8586PhantomReference<ClassLoader> getRef() {87return ref;88}89}9091public static void main(String... args) throws Exception {92// create the JAR file93setup();9495// Make simultaneous calls to the test method, to stress things a bit96ExecutorService es = Executors.newFixedThreadPool(THREADS);9798List<Callable<TestLoader>> callables =99Stream.generate(() -> {100Callable<TestLoader> cprcl = TestLoader::new;101return cprcl;102}).limit(THREADS).collect(Collectors.toList());103104List<Future<TestLoader>> futures = es.invokeAll(callables);105106// Give the GC a chance to enqueue the PhantomReferences107for (int i = 0; i < 10; i++) {108System.gc();109}110111// Make sure all PhantomReferences to the leaked classloader are enqueued112for (int j = 0; j < futures.size(); j++) {113Reference rmRef = rq.remove(TIMEOUT);114if (rmRef == null) {115throw new RuntimeException("ClassLoader was never enqueued!");116} else {117System.out.println("Enqueued " + rmRef);118}119}120es.shutdown();121System.out.println("All ClassLoaders successfully enqueued");122}123124private static final String CLASSFILENAME = "ClassForName.class";125private static void setup() throws IOException {126String testclasses = System.getProperty("test.classes", ".");127128// Create a temporary .jar file containing ClassForName.class129Path testClassesDir = Paths.get(testclasses);130JarUtils.createJar(jarFilePath.toString(), testClassesDir.toString(), CLASSFILENAME);131}132}133134135