1 /* 2 * Copyright (c) 2019, 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 * @library /test/lib 27 * @build jdk.test.lib.Utils 28 * jdk.test.lib.compiler.CompilerUtils 29 * SelfReferenceDescriptor 30 * @run main/othervm -Xverify:remote SelfReferenceDescriptor 31 * @summary Test that a hidden class cannot be referenced in descriptor 32 */ 33 34 import java.io.ByteArrayOutputStream; 35 import java.io.File; 36 import java.io.FileInputStream; 37 import java.io.IOException; 38 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; 39 import static java.lang.invoke.MethodHandles.lookup; 40 41 import java.nio.file.Files; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.Arrays; 45 import java.util.stream.Stream; 46 47 import jdk.test.lib.compiler.CompilerUtils; 48 49 import jdk.test.lib.Utils; 50 51 /* package-private */ interface Test { 52 void test(); 53 } 54 55 public class SelfReferenceDescriptor { 56 57 private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC); 58 private static final Path CLASSES_DIR = Paths.get("classes"); 59 60 static void compileSources(Path sourceFile, String... options) throws IOException { 61 Stream<String> ops = Stream.of("-cp", Utils.TEST_CLASSES + File.pathSeparator + CLASSES_DIR); 62 if (options != null && options.length > 0) { 63 ops = Stream.concat(ops, Arrays.stream(options)); 64 } 65 if (!CompilerUtils.compile(sourceFile, CLASSES_DIR, ops.toArray(String[]::new))) { 66 throw new RuntimeException("Compilation of the test failed: " + sourceFile); 67 } 68 } 69 70 // Test that a hidden class cannot use its own name in a field 71 // signature. 72 public static void hiddenClassInFieldDescriptor() throws Exception { 73 compileSources(SRC_DIR.resolve("SelfRefField.java")); 74 Path path = CLASSES_DIR.resolve("SelfRefField.class"); 75 byte[] bytes = Files.readAllBytes(path); 76 try { 77 lookup().defineHiddenClass(bytes, false, NESTMATE); 78 throw new RuntimeException("expected NCDFE in defining SelfRefField hidden class"); 79 } catch (NoClassDefFoundError e) { 80 if (!e.getMessage().contains("SelfRefField")) throw e; 81 } 82 } 83 84 // Test that a hidden class cannot use its own name in a method 85 // signature. 86 public static void hiddenClassInMethodDescriptor() throws Exception { 87 compileSources(SRC_DIR.resolve("SelfRefMethod.java")); 88 Path path = CLASSES_DIR.resolve("SelfRefMethod.class"); 89 byte[] bytes = Files.readAllBytes(path); 90 try { 91 lookup().defineHiddenClass(bytes, false, NESTMATE); 92 throw new RuntimeException("expected NCDFE in defining SelfRefMethod hidden class"); 93 } catch (NoClassDefFoundError e) { 94 if (!e.getMessage().contains("SelfRefMethod")) throw e; 95 } 96 } 97 98 public static void main(String... args) throws Exception { 99 hiddenClassInMethodDescriptor(); 100 hiddenClassInFieldDescriptor(); 101 } 102 }