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 an unnamed package 29 * and an unnamed module. 30 * @modules java.base/jdk.internal.misc 31 * @library /testlibrary /test/lib 32 * @compile myloaders/MyDiffClassLoader.java 33 * @compile c4.java 34 * @compile p3/c3.jcod 35 * @compile p3/c3ReadEdgeDiffLoader.jcod 36 * @build DiffCL_UmodUpkg 37 * @run main/othervm -Xbootclasspath/a:. DiffCL_UmodUpkg 38 */ 39 40 import static jdk.test.lib.Asserts.*; 41 42 import java.lang.reflect.Layer; 43 import java.lang.module.Configuration; 44 import java.lang.module.ModuleDescriptor; 45 import java.lang.module.ModuleFinder; 46 import java.util.HashMap; 47 import java.util.Map; 48 import java.util.Set; 49 import myloaders.MyDiffClassLoader; 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 DiffCL_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 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); 93 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); 94 95 // map module m1 to class loader. 96 // class c2 will be loaded in an unnamed module/loader2 97 // to achieve differing class loaders. 98 Map<String, ClassLoader> map = new HashMap<>(); 99 map.put("m1", MyDiffClassLoader.loader1); 100 101 // Create Layer that contains m1 102 Layer layer = Layer.boot().defineModules(cf, map::get); 103 104 assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1); 105 assertTrue(layer.findLoader("java.base") == null); 106 107 // now use the same loader to load class p3.c3 108 Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3"); 109 110 // Attempt access 111 try { 112 p3_c3_class.newInstance(); 113 throw new RuntimeException("Test Failed, strict module m1 should not be able to access " + 114 "public type c4 defined in unnamed module"); 115 } catch (IllegalAccessError e) { 116 } 117 } 118 119 // Module m1 is a strict module and has established 120 // readability to an unnamed module that c4 is defined in. 121 public void test_strictModuleUnnamedReadableLayer() throws Throwable { 122 123 // Define module: m1 124 // Can read: java.base 125 // Packages: p3 126 // Packages exported: p3 is exported unqualifiedly 127 ModuleDescriptor descriptor_m1 = 128 new ModuleDescriptor.Builder("m1") 129 .requires("java.base") 130 .exports("p3") 131 .build(); 132 133 // Set up a ModuleFinder containing all modules for this layer. 134 ModuleFinder finder = ModuleLibrary.of(descriptor_m1); 135 136 // Resolves "m1" 137 Configuration cf = Layer.boot() 138 .configuration() 139 .resolveRequires(finder, ModuleFinder.of(), Set.of("m1")); 140 141 MyDiffClassLoader.loader1 = new MyDiffClassLoader(); 142 MyDiffClassLoader.loader2 = new MyDiffClassLoader(); 143 144 // map module m1 to class loader. 145 // class c2 will be loaded in an unnamed module/loader2 146 // to achieve differing class loaders. 147 Map<String, ClassLoader> map = new HashMap<>(); 148 map.put("m1", MyDiffClassLoader.loader1); 149 150 // Create Layer that contains m1 151 Layer layer = Layer.boot().defineModules(cf, map::get); 152 153 assertTrue(layer.findLoader("m1") == MyDiffClassLoader.loader1); 154 assertTrue(layer.findLoader("java.base") == null); 155 156 // now use the same loader to load class p3.c3ReadEdgeDiffLoader 157 Class p3_c3_class = MyDiffClassLoader.loader1.loadClass("p3.c3ReadEdgeDiffLoader"); 158 159 try { 160 // Read edge between m1 and the unnamed module that loads c4 is established in 161 // C3ReadEdgeDiffLoader's ctor before attempting access. 162 p3_c3_class.newInstance(); 163 } catch (IllegalAccessError e) { 164 throw new RuntimeException("Test Failed, module m1 has established readability to " + 165 "c4 loader's unnamed module, access should be allowed: " + e.getMessage()); 166 } 167 } 168 169 public static void main(String args[]) throws Throwable { 170 DiffCL_UmodUpkg test = new DiffCL_UmodUpkg(); 171 test.test_strictModuleLayer(); // access denied 172 test.test_strictModuleUnnamedReadableLayer(); // access allowed 173 } 174 }