1 /*
  2  * Copyright (c) 2015, 2021, 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 /*
 26  * @test
 27  * @summary Check that during dumping, the classes for BOOT/EXT/APP loaders are segregated from the
 28  *          custom loader classes.
 29  * @requires vm.cds
 30  * @requires vm.cds.custom.loaders
 31  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 32  * @compile test-classes/LoaderSegregation.java
 33  *          test-classes/CustomLoadee.java test-classes/CustomLoadee2.java
 34  *          test-classes/CustomInterface2_ia.java test-classes/CustomInterface2_ib.java
 35  *          test-classes/CustomLoadee3.java test-classes/CustomLoadee3Child.java
 36  *          test-classes/OnlyBuiltin.java
 37  *          test-classes/OnlyUnregistered.java
 38  *          ../test-classes/Util.java
 39  * @build sun.hotspot.WhiteBox
 40  * @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
 41  * @run driver LoaderSegregationTest
 42  */
 43 
 44 import jdk.test.lib.process.OutputAnalyzer;
 45 import sun.hotspot.WhiteBox;
 46 
 47 /**
 48  * See "Handling of the classes in the AppCDS archive" at the top of
 49  * systemDicrionatyShared.hpp.
 50  *
 51  * This test ensure that the 2 types of archived classes (BUILTIN and UNREGISTERED)
 52  * are segregated at both dump-time and run time:
 53  *
 54  * [A] An archived BUILTIN class cannot be a subclass of a non-BUILTIN class.
 55  * [B] An archived BUILTIN class cannot implement a non-BUILTIN interface.
 56  * [C] BUILTIN and UNREGISTERED classes can be loaded only by their corresponding
 57  *     type of loaders.
 58  *
 59  */
 60 public class LoaderSegregationTest {
 61     public static void main(String[] args) throws Exception {
 62         String wbJar = JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox");
 63         String use_whitebox_jar = "-Xbootclasspath/a:" + wbJar;
 64 
 65         String appJar = JarBuilder.build("LoaderSegregation_app", "LoaderSegregation", "LoaderSegregation$1",
 66                                          "CustomLoadee", "CustomLoadee2", "CustomLoadee3Child", "CustomInterface2_ia",
 67                                          "OnlyBuiltin", "Util");
 68 
 69         String app2Jar = JarBuilder.build("LoaderSegregation_app2", "CustomLoadee3", "CustomInterface2_ib");
 70 
 71         String customJarPath = JarBuilder.build("LoaderSegregation_custom", "CustomLoadee",
 72                                                 "CustomLoadee2", "CustomInterface2_ia", "CustomInterface2_ib",
 73                                                 "CustomLoadee3", "CustomLoadee3Child",
 74                                                 "OnlyBuiltin", "OnlyUnregistered");
 75 
 76         // Dump the archive
 77         String classlist[] = new String[] {
 78             "LoaderSegregation",
 79             "java/lang/Object id: 1",
 80             "java/lang/IdentityObject id: 2",
 81 
 82             // These are the UNREGISTERED classes: they have "source:"
 83             // but they don't have "loader:".
 84             "CustomLoadee id: 3 super: 1 interfaces: 2 source: " + customJarPath,
 85 
 86             "CustomInterface2_ia id: 4 super: 1 source: " + customJarPath,
 87             "CustomInterface2_ib id: 5 super: 1 source: " + customJarPath,
 88             "CustomLoadee2 id: 6 super: 1 interfaces: 2 4 5 source: " + customJarPath,
 89 
 90             "CustomLoadee3 id: 7 super: 1 interfaces: 2 source: " + customJarPath,
 91             "CustomLoadee3Child id: 8 super: 7 source: " + customJarPath,
 92 
 93             // At dump time, the following BUILTIN classes are loaded after the UNREGISTERED
 94             // classes from above. However, at dump time, they cannot use the UNREGISTERED classes are their
 95             // super or interface.
 96             "CustomLoadee",          // can be loaded at dump time
 97             "CustomLoadee2",         // cannot be loaded at dump time (interface missing)
 98             "CustomLoadee3Child",    // cannot be loaded at dump time (super missing)
 99 
100             // Check that BUILTIN and UNREGISTERED classes can be loaded only by their
101             // corresponding type of loaders.
102             "OnlyBuiltin",
103             "OnlyUnregistered id: 10 super: 1 interfaces: 2 source: " + customJarPath,
104         };
105 
106         OutputAnalyzer output;
107         TestCommon.testDump(appJar, classlist,
108                             // command-line arguments ...
109                             use_whitebox_jar);
110 
111         output = TestCommon.exec(TestCommon.concatPaths(appJar, app2Jar),
112                                  // command-line arguments ...
113                                  use_whitebox_jar,
114                                  "-XX:+UnlockDiagnosticVMOptions",
115                                  "-XX:+WhiteBoxAPI",
116                                  "LoaderSegregation", customJarPath);
117         TestCommon.checkExec(output);
118     }
119 }