1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   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  *
  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.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8151486
  27  * @summary Call Class.forName() on the system classloader from a class loaded
  28  *          from a custom classloader, using the current class's protection domain.
  29  * @library /test/jdk/lib/testlibrary
  30  * @build jdk.testlibrary.Utils JarUtils
  31  * @build ClassForName ProtectionDomainCacheTest
  32  * @run main/othervm/policy=test.policy -XX:+UnlockDiagnosticVMOptions -XX:VerifySubSet=dictionary -XX:+VerifyAfterGC -Xlog:gc+verify=debug,protectiondomain=trace,class+unload:gc.log -Djava.security.manager ProtectionDomainCacheTest
  33  */
  34 
  35 import java.net.URL;
  36 import java.net.URLClassLoader;
  37 import java.nio.file.FileSystems;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.List;
  42 import jdk.testlibrary.Utils;
  43 
  44 /*
  45  * Create .jar, load ClassForName from .jar using a URLClassLoader
  46  */
  47 public class ProtectionDomainCacheTest {
  48     private static final long TIMEOUT = (long)(5000.0 * Utils.TIMEOUT_FACTOR);
  49     private static final String TESTCLASSES = System.getProperty("test.classes", ".");
  50     private static final String CLASSFILENAME = "ClassForName.class";
  51 
  52     // Use a new classloader to load the ClassForName class.
  53     public static void loadAndRun(Path jarFilePath)
  54             throws Exception {
  55         ClassLoader classLoader = new URLClassLoader(
  56                 new URL[]{jarFilePath.toUri().toURL()}) {
  57             @Override public String toString() { return "LeakedClassLoader"; }
  58         };
  59 
  60         Class<?> loadClass = Class.forName("ClassForName", true, classLoader);
  61         loadClass.newInstance();
  62 
  63         System.out.println("returning : " + classLoader);
  64     }
  65 
  66     public static void main(final String[] args) throws Exception {
  67         // Create a temporary .jar file containing ClassForName.class
  68         Path testClassesDir = Paths.get(TESTCLASSES);
  69         Path jarFilePath = Files.createTempFile("cfn", ".jar");
  70         JarUtils.createJarFile(jarFilePath, testClassesDir, CLASSFILENAME);
  71         jarFilePath.toFile().deleteOnExit();
  72 
  73         // Remove the ClassForName.class file that jtreg built, to make sure
  74         // we're loading from the tmp .jar
  75         Path classFile = FileSystems.getDefault().getPath(TESTCLASSES,
  76                                                           CLASSFILENAME);
  77         Files.delete(classFile);
  78 
  79         loadAndRun(jarFilePath);
  80 
  81         // Give the GC a chance to unload protection domains
  82         for (int i = 0; i < 100; i++) {
  83             System.gc();
  84         }
  85         System.out.println("All Classloaders and protection domain cache entries successfully unloaded");
  86     }
  87 }