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