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