1 /* 2 * Copyright (c) 2019, 2020, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @modules jdk.compiler 27 * @library /test/lib 28 * @build jdk.test.lib.Utils 29 * jdk.test.lib.compiler.CompilerUtils 30 * SelfReferenceDescriptor 31 * @run main/othervm -Xverify:remote SelfReferenceDescriptor 32 * @summary Test that a hidden class cannot be referenced in descriptor 33 */ 34 35 import java.io.ByteArrayOutputStream; 36 import java.io.File; 37 import java.io.FileInputStream; 38 import java.io.IOException; 39 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; 40 import static java.lang.invoke.MethodHandles.lookup; 41 42 import java.nio.file.Files; 43 import java.nio.file.Path; 44 import java.nio.file.Paths; 45 import java.util.Arrays; 46 import java.util.stream.Stream; 47 48 import jdk.test.lib.compiler.CompilerUtils; 49 50 import jdk.test.lib.Utils; 51 52 /* package-private */ interface Test { 53 void test(); 54 } 55 56 public class SelfReferenceDescriptor { 57 58 private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC); 59 private static final Path CLASSES_DIR = Paths.get("classes"); 60 61 static void compileSources(Path sourceFile, String... options) throws IOException { 62 Stream<String> ops = Stream.of("-cp", Utils.TEST_CLASSES + File.pathSeparator + CLASSES_DIR); 63 if (options != null && options.length > 0) { 64 ops = Stream.concat(ops, Arrays.stream(options)); 65 } 66 if (!CompilerUtils.compile(sourceFile, CLASSES_DIR, ops.toArray(String[]::new))) { 67 throw new RuntimeException("Compilation of the test failed: " + sourceFile); 68 } 69 } 70 71 // Test that a hidden class cannot use its own name in a field 72 // signature. 73 public static void hiddenClassInFieldDescriptor() throws Exception { 74 compileSources(SRC_DIR.resolve("SelfRefField.java")); 75 Path path = CLASSES_DIR.resolve("SelfRefField.class"); 76 byte[] bytes = Files.readAllBytes(path); 77 try { 78 lookup().defineHiddenClass(bytes, false, NESTMATE); 79 throw new RuntimeException("expected NCDFE in defining SelfRefField hidden class"); 80 } catch (NoClassDefFoundError e) { 81 if (!e.getMessage().contains("SelfRefField")) throw e; 82 } 83 } 84 85 // Test that a hidden class cannot use its own name in a method 86 // signature. 87 public static void hiddenClassInMethodDescriptor() throws Exception { 88 compileSources(SRC_DIR.resolve("SelfRefMethod.java")); 89 Path path = CLASSES_DIR.resolve("SelfRefMethod.class"); 90 byte[] bytes = Files.readAllBytes(path); 91 try { 92 lookup().defineHiddenClass(bytes, false, NESTMATE); 93 throw new RuntimeException("expected NCDFE in defining SelfRefMethod hidden class"); 94 } catch (NoClassDefFoundError e) { 95 if (!e.getMessage().contains("SelfRefMethod")) throw e; 96 } 97 } 98 99 public static void main(String... args) throws Exception { 100 hiddenClassInMethodDescriptor(); 101 hiddenClassInFieldDescriptor(); 102 } 103 }