1 /* 2 * Copyright (c) 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 * @bug 8238550 27 * @summary Test that verifer assignability checks involving the 'this' pointer 28 * work ok for hidden classes. 29 * @compile FieldInSuperSub.jasm 30 * @run main/othervm -Xverify:remote FieldInSuper 31 */ 32 33 import java.io.ByteArrayOutputStream; 34 import java.io.File; 35 import java.io.FileInputStream; 36 import java.lang.invoke.MethodHandles; 37 import java.lang.invoke.MethodHandles.Lookup; 38 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; 39 import java.nio.file.Path; 40 import java.nio.file.Paths; 41 42 public class FieldInSuper { 43 44 static final Path CLASSES_DIR = Paths.get(System.getProperty("test.classes")); 45 String hello = "hello"; 46 47 static byte[] readClassFile(String classFileName) throws Exception { 48 File classFile = new File(CLASSES_DIR + File.separator + classFileName); 49 try (FileInputStream in = new FileInputStream(classFile); 50 ByteArrayOutputStream out = new ByteArrayOutputStream()) { 51 int b; 52 while ((b = in.read()) != -1) { 53 out.write(b); 54 } 55 return out.toByteArray(); 56 } 57 } 58 59 public static void main(String[] args) throws Throwable { 60 Lookup lookup = MethodHandles.lookup(); 61 byte[] bytes = readClassFile("FieldInSuperSub.class"); 62 63 // Define a hidden class that loads the 'this' pointer and then 64 // references field 'hello' using: 65 // getfield Field FieldInSuper.hello:"Ljava/lang/String;"; 66 // This will cause the verifier to check that the 'this' pointer for 67 // the hidden class is assignable to FieldInSuper. 68 Class<?> c = lookup.defineHiddenClass(bytes, true, NESTMATE).lookupClass(); 69 Object fss = c.newInstance(); 70 c.getMethod("printMe").invoke(fss); 71 } 72 73 }