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