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/MyDiffClassLoader.java 33 * @compile p2/c2.java 34 * @compile p1/c1.java 35 * @compile p1/c1ReadEdgeDiffLoader.java 36 * @compile p1/c1Loose.java 37 * @run main/othervm -Xbootclasspath/a:. DiffCL_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.MyDiffClassLoader; 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 DiffCL_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 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); 95 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); 96 97 // map module m1x to class loader. 98 // class c2 will be loaded in an unnamed module/loader2 99 // to achieve differing class loaders. 100 Map<String, ClassLoader> map = new HashMap<>(); 101 map.put("m1x", MyDiffClassLoader.loader1); 102 103 // Create layer that contains m1x 104 ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); 105 106 assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); 107 assertTrue(layer.findLoader("java.base") == null); 108 109 // now use the same loader to load class p1.c1 110 Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1"); 111 112 // Attempt access 113 try { 114 p1_c1_class.newInstance(); 115 throw new RuntimeException("Test Failed, strict module m1x should not be able " + 116 "to access public type p2.c2 defined in unnamed module"); 117 } catch (IllegalAccessError e) { 118 } 119 } 120 121 // Module m1x is a strict module and has established 122 // readability to an unnamed module that p2.c2 is defined in. 123 public void test_strictModuleUnnamedReadableLayer() throws Throwable { 124 125 // Define module: m1x 126 // Can read: java.base 127 // Packages: p1 128 // Packages exported: p1 is exported unqualifiedly 129 ModuleDescriptor descriptor_m1x = 130 ModuleDescriptor.newModule("m1x") 131 .requires("java.base") 132 .exports("p1") 133 .build(); 134 135 // Set up a ModuleFinder containing all modules for this layer. 136 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); 137 138 // Resolves "m1x" 139 Configuration cf = ModuleLayer.boot() 140 .configuration() 141 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); 142 143 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); 144 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); 145 146 // map module m1x to class loader. 147 // class c2 will be loaded in an unnamed module/loader2 148 // to achieve differing class loaders. 149 Map<String, ClassLoader> map = new HashMap<>(); 150 map.put("m1x", MyDiffClassLoader.loader1); 151 152 // Create layer that contains m1x 153 ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); 154 155 assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); 156 assertTrue(layer.findLoader("java.base") == null); 157 158 // now use the same loader to load class p1.c1ReadEdgeDiffLoader 159 Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1ReadEdgeDiffLoader"); 160 161 try { 162 // Read edge between m1x and the unnamed module that loads p2.c2 is established in 163 // c1ReadEdgeDiffLoader's ctor before attempting access. 164 p1_c1_class.newInstance(); 165 } catch (IllegalAccessError e) { 166 throw new RuntimeException("Test Failed, module m1x has established readability to p2/c2 loader's " + 167 "unnamed module, access should be allowed: " + e.getMessage()); 168 } 169 } 170 171 // Module m1x is a loose module and thus can read all unnamed modules. 172 public void test_looseModuleLayer() throws Throwable { 173 174 // Define module: m1x 175 // Can read: java.base 176 // Packages: p1 177 // Packages exported: p1 is exported unqualifiedly 178 ModuleDescriptor descriptor_m1x = 179 ModuleDescriptor.newModule("m1x") 180 .requires("java.base") 181 .exports("p1") 182 .build(); 183 184 // Set up a ModuleFinder containing all modules for this layer. 185 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x); 186 187 // Resolves "m1x" 188 Configuration cf = ModuleLayer.boot() 189 .configuration() 190 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); 191 192 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); 193 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); 194 195 // map module m1x to class loader. 196 // class c2 will be loaded in an unnamed module/loader2 197 // to achieve differing class loaders. 198 Map<String, ClassLoader> map = new HashMap<>(); 199 map.put("m1x", MyDiffClassLoader.loader1); 200 201 // Create layer that contains m1x 202 ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); 203 204 assertTrue(layer.findLoader("m1x") == MyDiffClassLoader.loader1); 205 assertTrue(layer.findLoader("java.base") == null); 206 207 // now use the same loader to load class p1.c1Loose 208 Class p1_c1_class = MyDiffClassLoader.loader1.loadClass("p1.c1Loose"); 209 210 // change m1x to read all unnamed modules 211 Module m1x = layer.findModule("m1x").get(); 212 jdk.internal.module.Modules.addReadsAllUnnamed(m1x); 213 214 try { 215 p1_c1_class.newInstance(); 216 } catch (IllegalAccessError e) { 217 throw new RuntimeException("Test Failed, loose module m1x should be able to access " + 218 "public type p2.c2 defined in unnamed module: " + e.getMessage()); 219 } 220 } 221 222 public static void main(String args[]) throws Throwable { 223 DiffCL_Umod test = new DiffCL_Umod(); 224 test.test_strictModuleLayer(); // access denied 225 test.test_strictModuleUnnamedReadableLayer(); // access allowed 226 test.test_looseModuleLayer(); // access allowed 227 } 228 }