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  * @modules java.base/jdk.internal.misc
  27  * @library /test/lib
  28  * @build jdk.test.lib.JDKToolLauncher
  29  *        jdk.test.lib.process.ProcessTools
  30  *        jdk.test.lib.Utils
  31  * @run main/othervm -Xverify:remote DefineNonFindableClass
  32  */
  33 
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.File;
  36 import java.io.FileInputStream;
  37 import java.lang.invoke.MethodHandles;
  38 import java.lang.invoke.MethodHandles.Lookup;
  39 import static java.lang.invoke.MethodHandles.Lookup.ClassProperty.*;
  40 import static java.lang.invoke.MethodHandles.Lookup.PRIVATE;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import jdk.internal.misc.Unsafe;
  44 
  45 import jdk.test.lib.JDKToolLauncher;
  46 import jdk.test.lib.process.ProcessTools;
  47 import jdk.test.lib.Utils;
  48 
  49 /* package-private */ interface Test {
  50   void test();
  51 }
  52 
  53 
  54 public class DefineNonFindableClass {
  55 
  56     static final Class<?> klass = DefineNonFindableClass.class;
  57     static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "nonFindable");
  58     static final Path CLASSES_DIR = Paths.get(Utils.TEST_CLASSES, "nonFindable");
  59 
  60     static void compileSources(String sourceFile) throws Throwable {
  61         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("javac");
  62         launcher.addToolArg("-cp")
  63                 .addToolArg(Utils.TEST_CLASSES.toString())
  64                 .addToolArg("-d")
  65                 .addToolArg(CLASSES_DIR.toString())
  66             .addToolArg(Paths.get(SRC_DIR.toString(), sourceFile).toString());
  67 
  68         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
  69                                    .getExitValue();
  70         if (exitCode != 0) {
  71             throw new RuntimeException("Compilation of the test failed. "
  72                     + "Unexpected exit code: " + exitCode);
  73         }
  74     }
  75 
  76     static byte[] readClassFile(String classFileName) throws Exception {
  77         File classFile = new File(CLASSES_DIR + File.separator + classFileName);
  78         try (FileInputStream in = new FileInputStream(classFile);
  79              ByteArrayOutputStream out = new ByteArrayOutputStream())
  80         {
  81             int b;
  82             while ((b = in.read()) != -1) {
  83                 out.write(b);
  84             }
  85             return out.toByteArray();
  86         }
  87     }
  88 
  89     public static void main(String[] args) throws Throwable {
  90         compileSources("NonFindable.java");
  91         Lookup lookup = MethodHandles.lookup();
  92         byte[] bytes = readClassFile("NonFindable.class");
  93         Class<?> c = lookup.defineClass(bytes, NESTMATE, HIDDEN);
  94         Test t = (Test) c.newInstance();
  95         t.test();
  96 
  97 
  98         // Test that a nonFindable class cannot use its own name in a field
  99         // signature.
 100         compileSources("NonFindableField.java");
 101         Lookup lookup2 = MethodHandles.lookup();
 102         byte[] bytes2 = readClassFile("NonFindableField.class");
 103         try {
 104             Class<?> c2 = lookup2.defineClass(bytes2, NESTMATE, HIDDEN);
 105             throw new RuntimeException(
 106                 "Expected NoClassDefFoundError exception not thrown");
 107         } catch (java.lang.NoClassDefFoundError e) {
 108             if (!e.getMessage().contains("NonFindableField")) {
 109                 throw new RuntimeException(
 110                     "Unexpected NoClassDefFoundError: " + e.getMessage());
 111             }
 112         }
 113 
 114         // Test that a nonFindable class cannot use its own name in a method
 115         // signature.
 116         compileSources("NonFindableMethod.java");
 117         Lookup lookup3 = MethodHandles.lookup();
 118         byte[] bytes3 = readClassFile("NonFindableMethod.class");
 119         try {
 120             Class<?> c3 = lookup3.defineClass(bytes3, NESTMATE, HIDDEN);
 121             throw new RuntimeException(
 122                 "Expected NoClassDefFoundError exception not thrown");
 123         } catch (java.lang.NoClassDefFoundError e) {
 124             if (!e.getMessage().contains("NonFindableMethod")) {
 125                 throw new RuntimeException(
 126                     "Unexpected NoClassDefFoundError: " + e.getMessage());
 127             }
 128         }
 129     }
 130 
 131 }