1 /*
   2  * Copyright (c) 2020, 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  * @summary Test the ability to safely unload a class that has an error
  27  *          with its designated nest host. The nest host class must resolve
  28  *          successfully but fail validation. This tests a specific, otherwise
  29  *          untested, code path in ResolutionErrorTable::free_entry.
  30  *
  31  * @library /runtime/testlibrary
  32  * @compile TestNestHostErrorWithClassUnload.java
  33  *          Helper.java
  34  *          PackagedNestHost.java
  35  *          PackagedNestHost2.java
  36  * @compile PackagedNestHost2Member.jcod
  37  *
  38  * @run main/othervm -Xlog:class+unload=trace TestNestHostErrorWithClassUnload
  39  */
  40 
  41 // Test setup:
  42 //   PackagedNestHost.java defines P1.PackagedNestHost, which is referenced
  43 //     by P2.PackageNestHost2.Member
  44 //   PackagedNestHost2.java defines P2.PackagedNestHost2 and its nested Member
  45 //     class.
  46 //   PackagedNestHost2Member.jcod changes P2.PackagedNestHost.Member to claim
  47 //     it is in the nest of P1.PackagedNestHost.
  48 //   Helper.java is a helper class to run the test under the other classloader.
  49 //     A separate class is used to avoid confusion because if a public nested
  50 //     class were to be used, then the main test class would be loaded a second
  51 //     time in the other loader, and also subsequently unloaded.
  52 //
  53 // We load all classes into a new classloader and then try to call
  54 // P2.PackagedNestHost.Member.m() which will be private at runtime. That will
  55 // trigger nest host resolution and validation and the invocation will fail
  56 // with IllegalAccessError. We then drop the classloader and invoke GC to force
  57 // the unloading of the classes. Not all GC configurations are guaranteed to
  58 // result in unloading, but that isn't essential for the test to succeed - we
  59 // know that when unloading does occur we have tested the desired code path.
  60 
  61 import java.lang.invoke.MethodHandle;
  62 import java.lang.invoke.MethodHandles;
  63 import java.lang.invoke.MethodType;
  64 
  65 public class TestNestHostErrorWithClassUnload {
  66 
  67     static final MethodType INVOKE_T = MethodType.methodType(void.class);
  68 
  69     public static void main(String[] args) throws Throwable {
  70         ClassLoader cl = ClassUnloadCommon.newClassLoader();
  71         Class<?> c = cl.loadClass("Helper");
  72         MethodHandle mh = MethodHandles.lookup().findStatic(c, "test", INVOKE_T);
  73         mh.invoke();
  74         // now drop all references so we can trigger unloading
  75         mh = null;
  76         c = null;
  77         cl = null;
  78         ClassUnloadCommon.triggerUnloading();
  79     }
  80 }