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