1 /*
   2  * Copyright (c) 2016, 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  * @summary class p3.c3 defined in module m1x tries to access c4 defined in an unnamed package
  27  *          and an unnamed module.
  28  * @modules java.base/jdk.internal.misc
  29  * @library /test/lib
  30  * @compile myloaders/MyDiffClassLoader.java
  31  * @compile c4.java
  32  * @compile p3/c3.jcod
  33  * @compile p3/c3ReadEdgeDiffLoader.jcod
  34  * @run main/othervm -Xbootclasspath/a:. DiffCL_UmodUpkg
  35  */
  36 
  37 import static jdk.test.lib.Asserts.*;
  38 
  39 import java.lang.module.Configuration;
  40 import java.lang.module.ModuleDescriptor;
  41 import java.lang.module.ModuleFinder;
  42 import java.util.HashMap;
  43 import java.util.Map;
  44 import java.util.Set;
  45 import myloaders.MyDiffClassLoader;
  46 
  47 //
  48 // ClassLoader1 --> defines m1x --> packages p3
  49 //                  package p3 in m1x is exported unqualifiedly
  50 //
  51 // class p3.c3 defined in m1x tries to access c4 defined in
  52 // in unnamed module.
  53 //
  54 // Two access attempts occur in this test:
  55 //   1. The first access is not allowed because a strict module
  56 //      cannot read an unnamed module.
  57 //   2. In this scenario a strict module establishes readability
  58 //      to the particular unnamed module it is trying to access.
  59 //      Access is allowed.
  60 //
  61 public class DiffCL_UmodUpkg {
  62 
  63  // Create layers over the boot layer to test different
  64  // accessing scenarios of a named module to an unnamed module.
  65 
  66  // Module m1x is a strict module and has not established
  67  // readability to an unnamed module that c4 is defined in.
  68  public void test_strictModuleLayer() throws Throwable {
  69 
  70      // Define module:     m1x
  71      // Can read:          java.base
  72      // Packages:          p3
  73      // Packages exported: p3 is exported unqualifiedly
  74      ModuleDescriptor descriptor_m1x =
  75              ModuleDescriptor.newModule("m1x")
  76                      .requires("java.base")
  77                      .exports("p3")
  78                      .build();
  79 
  80      // Set up a ModuleFinder containing all modules for this layer.
  81      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
  82 
  83      // Resolves "m1x"
  84      Configuration cf = ModuleLayer.boot()
  85              .configuration()
  86              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
  87 
  88      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
  89      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
  90 
  91      // map module m1x to class loader.
  92      // class c2 will be loaded in an unnamed module/loader2
  93      // to achieve differing class loaders.
  94      Map<String, ClassLoader> map = new HashMap<>();
  95      map.put("m1x", MyDiffClassLoader.loader1);
  96 
  97      // Create layer that contains m1x
  98      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
  99 
 100      assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
 101      assertTrue(layer.findLoader("java.base") == null);
 102 
 103      // now use the same loader to load class p3.c3
 104      Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3");
 105 
 106      // Attempt access
 107      try {
 108          p3_c3_class.newInstance();
 109          throw new RuntimeException("Test Failed, strict module m1x should not be able to access " +
 110                                     "public type c4 defined in unnamed module");
 111      } catch (IllegalAccessError e) {
 112      }
 113 }
 114 
 115  // Module m1x is a strict module and has established
 116  // readability to an unnamed module that c4 is defined in.
 117  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
 118 
 119      // Define module:     m1x
 120      // Can read:          java.base
 121      // Packages:          p3
 122      // Packages exported: p3 is exported unqualifiedly
 123      ModuleDescriptor descriptor_m1x =
 124              ModuleDescriptor.newModule("m1x")
 125                      .requires("java.base")
 126                      .exports("p3")
 127                      .build();
 128 
 129      // Set up a ModuleFinder containing all modules for this layer.
 130      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
 131 
 132      // Resolves "m1x"
 133      Configuration cf = ModuleLayer.boot()
 134              .configuration()
 135              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
 136 
 137      MyDiffClassLoader.loader1 = new MyDiffClassLoader();
 138      MyDiffClassLoader.loader2 = new MyDiffClassLoader();
 139 
 140      // map module m1x to class loader.
 141      // class c2 will be loaded in an unnamed module/loader2
 142      // to achieve differing class loaders.
 143      Map<String, ClassLoader> map = new HashMap<>();
 144      map.put("m1x", MyDiffClassLoader.loader1);
 145 
 146      // Create layer that contains m1x
 147      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
 148 
 149      assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1);
 150      assertTrue(layer.findLoader("java.base") == null);
 151 
 152      // now use the same loader to load class p3.c3ReadEdgeDiffLoader
 153      Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader");
 154 
 155      try {
 156         // Read edge between m1x and the unnamed module that loads c4 is established in
 157         // C3ReadEdgeDiffLoader's ctor before attempting access.
 158         p3_c3_class.newInstance();
 159      } catch (IllegalAccessError e) {
 160          throw new RuntimeException("Test Failed, module m1x has established readability to " +
 161                                     "c4 loader's unnamed module, access should be allowed: " + e.getMessage());
 162      }
 163  }
 164 
 165  public static void main(String args[]) throws Throwable {
 166    DiffCL_UmodUpkg test = new DiffCL_UmodUpkg();
 167    test.test_strictModuleLayer();                // access denied
 168    test.test_strictModuleUnnamedReadableLayer(); // access allowed
 169  }
 170 }