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 8202113
  27  * @summary Test the caller class loader is not kept strongly reachable
  28   *         by reflection API
  29  * @library /test/lib/
  30  * @build ReflectionCallerCacheTest Members jdk.test.lib.compiler.CompilerUtils
  31  * @run testng/othervm ReflectionCallerCacheTest
  32  */
  33 
  34 import java.io.IOException;
  35 import java.lang.ref.Cleaner;
  36 import java.lang.ref.WeakReference;
  37 import java.lang.reflect.*;
  38 import java.net.MalformedURLException;
  39 import java.net.URL;
  40 import java.net.URLClassLoader;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import java.util.concurrent.Callable;
  44 import java.util.concurrent.CountDownLatch;
  45 import java.util.concurrent.TimeUnit;
  46 import java.util.function.BooleanSupplier;
  47 
  48 import jdk.test.lib.compiler.CompilerUtils;
  49 import org.testng.annotations.BeforeTest;
  50 import org.testng.annotations.DataProvider;
  51 import org.testng.annotations.Test;
  52 
  53 public class ReflectionCallerCacheTest {
  54     private static final Path CLASSES = Paths.get("classes");
  55     private static final ReflectionCallerCacheTest TEST = new ReflectionCallerCacheTest();
  56 
  57     @BeforeTest
  58     public void setup() throws IOException {
  59         String src = System.getProperty("test.src", ".");
  60         String classpath = System.getProperty("test.classes", ".");
  61         boolean rc = CompilerUtils.compile(Paths.get(src, "AccessTest.java"), CLASSES, "-cp", classpath);
  62         if (!rc) {
  63             throw new RuntimeException("fail compilation");
  64         }
  65     }
  66     @DataProvider(name = "memberAccess")
  67     public Object[][] memberAccess() {
  68         return new Object[][] {
  69             { "AccessTest$PublicConstructor" },
  70             { "AccessTest$PublicMethod" },
  71             { "AccessTest$PublicField" },
  72             { "AccessTest$ProtectedMethod" },
  73             { "AccessTest$ProtectedField" },
  74             { "AccessTest$PrivateMethod" },
  75             { "AccessTest$PrivateField"}
  76         };
  77     }
  78 
  79     // Keep the root of the reflective objects strongly reachable
  80     private final Constructor<?> publicConstructor;
  81     private final Method publicMethod;
  82     private final Method protectedMethod;
  83     private final Method privateMethod;
  84     private final Field publicField;
  85     private final Field protectedField;
  86     private final Field privateField;
  87 
  88     ReflectionCallerCacheTest() {
  89         try {
  90             this.publicConstructor = Members.class.getConstructor();
  91             this.publicMethod = Members.class.getDeclaredMethod("publicMethod");
  92             this.publicField = Members.class.getDeclaredField("publicField");
  93             this.protectedMethod = Members.class.getDeclaredMethod("protectedMethod");
  94             this.protectedField = Members.class.getDeclaredField("protectedField");
  95             this.privateMethod = Members.class.getDeclaredMethod("privateMethod");
  96             this.privateField = Members.class.getDeclaredField("privateField");
  97         } catch (ReflectiveOperationException e) {
  98             throw new RuntimeException(e);
  99         }
 100     }
 101 
 102     @Test(dataProvider = "memberAccess")
 103     private void load(String classname) throws Exception {
 104         WeakReference<?> weakLoader = loadAndRunClass(classname);
 105 
 106         // Force garbage collection to trigger unloading of class loader
 107         new ForceGC().await(() -> weakLoader.get() == null);
 108 
 109         if (weakLoader.get() != null) {
 110             throw new RuntimeException("Class " + classname + " not unloaded!");
 111         }
 112     }
 113 
 114     private WeakReference<?> loadAndRunClass(String classname) throws Exception {
 115         try (TestLoader loader = new TestLoader()) {
 116             // Load member access class with custom class loader
 117             Class<?> c = Class.forName(classname, true, loader);
 118             // access the reflective member
 119             Callable callable = (Callable) c.newInstance();
 120             callable.call();
 121             return new WeakReference<>(loader);
 122         }
 123     }
 124 
 125     static class TestLoader extends URLClassLoader {
 126         static URL[] toURLs() {
 127             try {
 128                 return new URL[] { CLASSES.toUri().toURL() };
 129             } catch (MalformedURLException e) {
 130                 throw new Error(e);
 131             }
 132         }
 133 
 134         TestLoader() {
 135             super("testloader", toURLs(), ClassLoader.getSystemClassLoader());
 136         }
 137     }
 138 
 139     /**
 140      * Utility class to invoke System.gc()
 141      */
 142     static class ForceGC {
 143         private  final CountDownLatch cleanerInvoked = new CountDownLatch(1);
 144         private  final Cleaner cleaner = Cleaner.create();
 145 
 146         ForceGC() {
 147             cleaner.register(new Object(), () -> cleanerInvoked.countDown());
 148         }
 149 
 150         void doit() {
 151             try {
 152                 for (int i = 0; i < 10; i++) {
 153                     System.gc();
 154                     if (cleanerInvoked.await(1L, TimeUnit.SECONDS)) {
 155                         return;
 156                     }
 157                 }
 158             } catch (InterruptedException unexpected) {
 159                 throw new AssertionError("unexpected InterruptedException");
 160             }
 161         }
 162 
 163         void await(BooleanSupplier s) {
 164             for (int i = 0; i < 10; i++) {
 165                 if (s.getAsBoolean()) return;
 166                 doit();
 167             }
 168             throw new AssertionError("failed to satisfy condition");
 169         }
 170     }
 171 }