1 /*
   2  * Copyright (c) 2016, 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @summary class p1.c1 defined in m1x tries to access p2.c2 defined in unnamed module.
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  * @modules java.base/jdk.internal.module
  32  * @compile myloaders/MySameClassLoader.java
  33  * @compile p2/c2.java
  34  * @compile p1/c1.java
  35  * @compile p1/c1ReadEdge.java
  36  * @compile p1/c1Loose.java
  37  * @run main/othervm -Xbootclasspath/a:. Umod
  38  */
  39 
  40 import static jdk.test.lib.Asserts.*;
  41 
  42 import java.lang.module.Configuration;
  43 import java.lang.module.ModuleDescriptor;
  44 import java.lang.module.ModuleFinder;
  45 import java.util.HashMap;
  46 import java.util.Map;
  47 import java.util.Set;
  48 import myloaders.MySameClassLoader;
  49 
  50 //
  51 // ClassLoader1 --> defines m1x --> packages p1
  52 //                  package p1 in m1x is exported unqualifiedly
  53 //
  54 // class p1.c1 defined in m1x tries to access p2.c2 defined in
  55 // in unnamed module.
  56 //
  57 // Three access attempts occur in this test:
  58 //   1. The first access is not allowed because a strict module
  59 //      cannot read an unnamed module.
  60 //   2. In this scenario a strict module establishes readability
  61 //      to the particular unnamed module it is trying to access.
  62 //      Access is allowed.
  63 //   3. Module m1x in the test_looseModuleLayer() method
  64 //      is transitioned to a loose module, access
  65 //      to all unnamed modules is allowed.
  66 //
  67 public class Umod {
  68 
  69  // Create layers over the boot layer to test different
  70  // accessing scenarios of a named module to an unnamed module.
  71 
  72  // Module m1x is a strict module and has not established
  73  // readability to an unnamed module that p2.c2 is defined in.
  74  public void test_strictModuleLayer() throws Throwable {
  75 
  76      // Define module:     m1x
  77      // Can read:          java.base
  78      // Packages:          p1
  79      // Packages exported: p1 is exported unqualifiedly
  80      ModuleDescriptor descriptor_m1x =
  81              ModuleDescriptor.newModule("m1x")
  82                      .requires("java.base")
  83                      .exports("p1")
  84                      .build();
  85 
  86      // Set up a ModuleFinder containing all modules for this layer.
  87      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
  88 
  89      // Resolves "m1x"
  90      Configuration cf = ModuleLayer.boot()
  91              .configuration()
  92              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
  93 
  94      // map module m1x to class loader.
  95      // class c2 will be loaded in an unnamed module/loader.
  96      MySameClassLoader loader = new MySameClassLoader();
  97      Map<String, ClassLoader> map = new HashMap<>();
  98      map.put("m1x", loader);
  99 
 100      // Create layer that contains m1x
 101      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
 102 
 103      assertTrue(layer.findLoader("m1x") == loader);
 104      assertTrue(layer.findLoader("java.base") == null);
 105 
 106      // now use the same loader to load class p1.c1
 107      Class p1_c1_class = loader.loadClass("p1.c1");
 108 
 109      // Attempt access
 110      try {
 111          p1_c1_class.newInstance();
 112          throw new RuntimeException("Test Failed, strict module m1x, type p1.c1, should not be able " +
 113                                     "to access public type p2.c2 defined in unnamed module");
 114      } catch (IllegalAccessError e) {
 115      }
 116  }
 117 
 118  // Module m1x is a strict module and has established
 119  // readability to an unnamed module that p2.c2 is defined in.
 120  public void test_strictModuleUnnamedReadableLayer() throws Throwable {
 121 
 122      // Define module:     m1x
 123      // Can read:          java.base
 124      // Packages:          p1
 125      // Packages exported: p1 is exported unqualifiedly
 126      ModuleDescriptor descriptor_m1x =
 127              ModuleDescriptor.newModule("m1x")
 128                      .requires("java.base")
 129                      .exports("p1")
 130                      .build();
 131 
 132      // Set up a ModuleFinder containing all modules for this layer.
 133      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
 134 
 135      // Resolves "m1x"
 136      Configuration cf = ModuleLayer.boot()
 137              .configuration()
 138              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
 139 
 140      MySameClassLoader loader = new MySameClassLoader();
 141      // map module m1x to class loader.
 142      // class c2 will be loaded in an unnamed module/loader.
 143      Map<String, ClassLoader> map = new HashMap<>();
 144      map.put("m1x", loader);
 145 
 146      // Create layer that contains m1x
 147      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
 148 
 149      assertTrue(layer.findLoader("m1x") == loader);
 150      assertTrue(layer.findLoader("java.base") == null);
 151 
 152      // now use the same loader to load class p1.c1ReadEdge
 153      Class p1_c1_class = loader.loadClass("p1.c1ReadEdge");
 154 
 155      try {
 156        // Read edge between m1x and the unnamed module that loads p2.c2 is established in
 157        // c1ReadEdge's ctor before attempting access.
 158        p1_c1_class.newInstance();
 159      } catch (IllegalAccessError e) {
 160          throw new RuntimeException("Test Failed, strict module m1x, type p1.c1ReadEdge, should be able to acccess public type " +
 161                                     "p2.c2 defined in unnamed module: " + e.getMessage());
 162      }
 163 }
 164 
 165  // Module m1x is a loose module and thus can read all unnamed modules.
 166  public void test_looseModuleLayer() throws Throwable {
 167 
 168      // Define module:     m1x
 169      // Can read:          java.base
 170      // Packages:          p1
 171      // Packages exported: p1 is exported unqualifiedly
 172      ModuleDescriptor descriptor_m1x =
 173              ModuleDescriptor.newModule("m1x")
 174                      .requires("java.base")
 175                      .exports("p1")
 176                      .build();
 177 
 178      // Set up a ModuleFinder containing all modules for this layer.
 179      ModuleFinder finder = ModuleLibrary.of(descriptor_m1x);
 180 
 181      // Resolves "m1x"
 182      Configuration cf = ModuleLayer.boot()
 183              .configuration()
 184              .resolve(finder, ModuleFinder.of(), Set.of("m1x"));
 185 
 186      MySameClassLoader loader = new MySameClassLoader();
 187      // map module m1x to class loader.
 188      // class c2 will be loaded in an unnamed module/loader.
 189      Map<String, ClassLoader> map = new HashMap<>();
 190      map.put("m1x", loader);
 191 
 192      // Create layer that contains m1x
 193      ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
 194 
 195      assertTrue(layer.findLoader("m1x") == loader);
 196      assertTrue(layer.findLoader("java.base") == null);
 197 
 198      // now use the same loader to load class p1.c1Loose
 199      Class p1_c1_class = loader.loadClass("p1.c1Loose");
 200 
 201      // change m1x to read all unnamed modules
 202      Module m1x = layer.findModule("m1x").get();
 203      jdk.internal.module.Modules.addReadsAllUnnamed(m1x);
 204 
 205      try {
 206          p1_c1_class.newInstance();
 207      } catch (IllegalAccessError e) {
 208          throw new RuntimeException("Test Failed, strict module m1x, type p1.c1Loose, should be able to acccess public type " +
 209                                     "p2.c2 defined in unnamed module: " + e.getMessage());
 210      }
 211  }
 212 
 213  public static void main(String args[]) throws Throwable {
 214    Umod test = new Umod();
 215    test.test_strictModuleLayer();                // access denied
 216    test.test_strictModuleUnnamedReadableLayer(); // access allowed
 217    test.test_looseModuleLayer();                 // access allowed
 218  }
 219 }