Path: blob/master/test/hotspot/jtreg/runtime/Dictionary/ProtectionDomainCacheTest.java
40942 views
/*1* Copyright (c) 2018, 2021, 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 8151486 821826626* @summary Call Class.forName() on the system classloader from a class loaded27* from a custom classloader, using the current class's protection domain.28* @library /test/lib29* @modules java.base/jdk.internal.misc30* @build jdk.test.lib.Utils31* jdk.test.lib.util.JarUtils32* @build ClassForName ProtectionDomainCacheTest33* @run driver ProtectionDomainCacheTest34*/3536import java.net.URL;37import java.net.URLClassLoader;38import java.nio.file.FileSystems;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.util.List;43import jdk.test.lib.Utils;44import jdk.test.lib.util.JarUtils;45import java.io.File;4647import jdk.test.lib.process.OutputAnalyzer;48import jdk.test.lib.process.ProcessTools;4950/*51* Create .jar, load ClassForName from .jar using a URLClassLoader52*/53public class ProtectionDomainCacheTest {54static class Test {55private static final long TIMEOUT = (long)(5000.0 * Utils.TIMEOUT_FACTOR);56private static final String TESTCLASSES = System.getProperty("test.classes", ".");57private static final String CLASSFILENAME = "ClassForName.class";5859// Use a new classloader to load the ClassForName class.60public static void loadAndRun(Path jarFilePath)61throws Exception {62ClassLoader classLoader = new URLClassLoader(63new URL[]{jarFilePath.toUri().toURL()}) {64@Override public String toString() { return "LeakedClassLoader"; }65};6667Class<?> loadClass = Class.forName("ClassForName", true, classLoader);68loadClass.newInstance();6970System.out.println("returning : " + classLoader);71}7273public static void main(final String[] args) throws Exception {74// Create a temporary .jar file containing ClassForName.class75Path testClassesDir = Paths.get(TESTCLASSES);76Path jarFilePath = Files.createTempFile("cfn", ".jar");77JarUtils.createJarFile(jarFilePath, testClassesDir, CLASSFILENAME);78jarFilePath.toFile().deleteOnExit();7980// Remove the ClassForName.class file that jtreg built, to make sure81// we're loading from the tmp .jar82Path classFile = FileSystems.getDefault().getPath(TESTCLASSES,83CLASSFILENAME);84Files.delete(classFile);8586for (int i = 0; i < 20; i++) {87loadAndRun(jarFilePath);88}8990// Give the GC a chance to unload protection domains91for (int i = 0; i < 100; i++) {92System.gc();93}94System.out.println("All Classloaders and protection domain cache entries successfully unloaded");95}96}9798public static void main(String args[]) throws Exception {99ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(100"-Djava.security.policy==" + System.getProperty("test.src") + File.separator + "test.policy",101"-Dtest.classes=" + System.getProperty("test.classes", "."),102"-XX:+UnlockDiagnosticVMOptions",103"-XX:VerifySubSet=dictionary",104"-XX:+VerifyAfterGC",105"-Xlog:gc+verify,protectiondomain=trace",106"-Djava.security.manager",107Test.class.getName());108OutputAnalyzer output = new OutputAnalyzer(pb.start());109output.shouldContain("PD in set is not alive");110output.shouldContain("HandshakeForPD::do_thread");111output.shouldHaveExitValue(0);112}113}114115116