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