1 /*
   2  * Copyright (c) 2015, 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 /*
  26  * @test
  27  * @bug 8087342
  28  * @summary Test linkresolver search static, instance and overpass duplicates
  29  * @run main/othervm -Xverify:none TestStaticandInstance
  30  */
  31 
  32 
  33 import java.util.*;
  34 import jdk.internal.org.objectweb.asm.*;
  35 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  36 
  37 public class TestStaticandInstance {
  38   static final String stringC = "C";
  39   static final String stringD = "D";
  40   static final String stringI = "I";
  41 
  42   public static void main(String args[]) throws Throwable {
  43     ClassLoader cl = new ClassLoader() {
  44       public Class<?> loadClass(String name) throws ClassNotFoundException {
  45         Class retClass;
  46         if ((retClass = findLoadedClass(name)) != null) {
  47            return retClass;
  48         }
  49         if (stringC.equals(name)) {
  50             byte[] classFile=dumpC();
  51             return defineClass(stringC, classFile, 0, classFile.length);
  52         }
  53         if (stringD.equals(name)) {
  54             byte[] classFile=dumpD();
  55             return defineClass(stringD, classFile, 0, classFile.length);
  56         }
  57         if (stringI.equals(name)) {
  58             byte[] classFile=dumpI();
  59             return defineClass(stringI, classFile, 0, classFile.length);
  60         }
  61         return super.loadClass(name);
  62       }
  63     };
  64 
  65     Class classC = cl.loadClass(stringC);
  66     Class classI = cl.loadClass(stringI);
  67 
  68     try {
  69       int staticret = (Integer)cl.loadClass(stringD).getDeclaredMethod("CallStatic").invoke(null);
  70       if (staticret != 1) {
  71         throw new RuntimeException("invokestatic failed to call correct method");
  72       }
  73       System.out.println("staticret: " + staticret); // should be 1
  74    
  75       int invokeinterfaceret = (Integer)cl.loadClass(stringD).getDeclaredMethod("CallInterface").invoke(null);
  76       if (invokeinterfaceret != 0) {
  77         throw new RuntimeException(String.format("Expected java.lang.AbstractMethodError, got %d", invokeinterfaceret));
  78       }
  79       System.out.println("invokeinterfaceret: AbstractMethodError");
  80 
  81       int invokevirtualret = (Integer)cl.loadClass(stringD).getDeclaredMethod("CallVirtual").invoke(null);
  82       if (invokevirtualret != 0) {
  83         throw new RuntimeException(String.format("Expected java.lang.IncompatibleClassChangeError, got %d", invokevirtualret));
  84       }
  85       System.out.println("invokevirtualret: IncompatibleClassChangeError");
  86     } catch (java.lang.Throwable e) {
  87       throw new RuntimeException("Unexpected exception: " + e.getMessage());
  88     }
  89   }
  90 
  91 /*
  92 interface I {
  93   public int m(); // abstract 
  94   default int q() { return 3; } // trigger defmeth processing: C gets AME overpass 
  95 }
  96 
  97 // C gets static, private and AME overpass m()I with -Xverify:none
  98 class C implements I {
  99   static int n() { return 1;} // patch to m
 100   public int m() { return 2;} // patch to private 
 101 }
 102 
 103 public class D {
 104   public static int CallStatic() {
 105     int staticret = C.n();    // patch to m
 106     return staticret;
 107   }
 108   public static int CallInterface() throws AbstractMethodError{
 109     try {
 110       I myI = new C();
 111       return myI.m();
 112     } catch (java.lang.AbstractMethodError e) {
 113       return 0; // for success
 114     }
 115   }
 116   public static int CallVirtual() {
 117     try {
 118       C myC = new C();
 119       return myC.m();
 120     } catch (java.lang.IncompatibleClassChangeError e) {
 121       return 0; // for success
 122     }
 123   }
 124 }
 125 */
 126 
 127   public static byte[] dumpC() {
 128 
 129     ClassWriter cw = new ClassWriter(0);
 130     FieldVisitor fv;
 131     MethodVisitor mv;
 132     AnnotationVisitor av0;
 133 
 134     cw.visit(52, ACC_SUPER, "C", null, "java/lang/Object", new String[] { "I" });
 135 
 136     {
 137       mv = cw.visitMethod(0, "<init>", "()V", null, null);
 138       mv.visitCode();
 139       mv.visitVarInsn(ALOAD, 0);
 140       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 141       mv.visitInsn(RETURN);
 142       mv.visitMaxs(1, 1);
 143       mv.visitEnd();
 144     }
 145     {
 146       mv = cw.visitMethod(ACC_STATIC, "m", "()I", null, null);
 147       mv.visitCode();
 148       mv.visitInsn(ICONST_1);
 149       mv.visitInsn(IRETURN);
 150       mv.visitMaxs(1, 0);
 151       mv.visitEnd();
 152     }
 153     {
 154       mv = cw.visitMethod(ACC_PRIVATE, "m", "()I", null, null);
 155       mv.visitCode();
 156       mv.visitInsn(ICONST_2);
 157       mv.visitInsn(IRETURN);
 158       mv.visitMaxs(1, 1);
 159       mv.visitEnd();
 160     }
 161     cw.visitEnd();
 162 
 163     return cw.toByteArray();
 164   }
 165 
 166   public static byte[] dumpD () {
 167 
 168     ClassWriter cw = new ClassWriter(0);
 169     FieldVisitor fv;
 170     MethodVisitor mv;
 171     AnnotationVisitor av0;
 172 
 173     cw.visit(52, ACC_PUBLIC + ACC_SUPER, "D", null, "java/lang/Object", null);
 174 
 175     {
 176       mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
 177       mv.visitCode();
 178       mv.visitVarInsn(ALOAD, 0);
 179       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 180       mv.visitInsn(RETURN);
 181       mv.visitMaxs(1, 1);
 182       mv.visitEnd();
 183     }
 184     {
 185       mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "CallStatic", "()I", null, null);
 186       mv.visitCode();
 187       mv.visitMethodInsn(INVOKESTATIC, "C", "m", "()I", false);
 188       mv.visitVarInsn(ISTORE, 0);
 189       mv.visitVarInsn(ILOAD, 0);
 190       mv.visitInsn(IRETURN);
 191       mv.visitMaxs(1, 1);
 192       mv.visitEnd();
 193     }
 194     {
 195       mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "CallInterface", "()I", null, new String[] { "java/lang/AbstractMethodError" });
 196       mv.visitCode();
 197       Label l0 = new Label();
 198       Label l1 = new Label();
 199       Label l2 = new Label();
 200       mv.visitTryCatchBlock(l0, l1, l2, "java/lang/AbstractMethodError");
 201       mv.visitLabel(l0);
 202       mv.visitTypeInsn(NEW, "C");
 203       mv.visitInsn(DUP);
 204       mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V", false);
 205       mv.visitVarInsn(ASTORE, 0);
 206       mv.visitVarInsn(ALOAD, 0);
 207       mv.visitMethodInsn(INVOKEINTERFACE, "I", "m", "()I", true);
 208       mv.visitLabel(l1);
 209       mv.visitInsn(IRETURN);
 210       mv.visitLabel(l2);
 211       mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/AbstractMethodError"});
 212       mv.visitVarInsn(ASTORE, 0);
 213       mv.visitInsn(ICONST_0);
 214       mv.visitInsn(IRETURN);
 215       mv.visitMaxs(2, 1);
 216       mv.visitEnd();
 217     }
 218     {
 219       mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "CallVirtual", "()I", null, null);
 220       mv.visitCode();
 221       Label l0 = new Label();
 222       Label l1 = new Label();
 223       Label l2 = new Label();
 224       mv.visitTryCatchBlock(l0, l1, l2, "java/lang/IncompatibleClassChangeError");
 225       mv.visitLabel(l0);
 226       mv.visitTypeInsn(NEW, "C");
 227       mv.visitInsn(DUP);
 228       mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V", false);
 229       mv.visitVarInsn(ASTORE, 0);
 230       mv.visitVarInsn(ALOAD, 0);
 231       mv.visitMethodInsn(INVOKEVIRTUAL, "C", "m", "()I", false);
 232       mv.visitLabel(l1);
 233       mv.visitInsn(IRETURN);
 234       mv.visitLabel(l2);
 235       mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/IncompatibleClassChangeError"});
 236       mv.visitVarInsn(ASTORE, 0);
 237       mv.visitInsn(ICONST_0);
 238       mv.visitInsn(IRETURN);
 239       mv.visitMaxs(2, 1);
 240       mv.visitEnd();
 241     }
 242     cw.visitEnd();
 243 
 244     return cw.toByteArray();
 245   }
 246 
 247   public static byte[] dumpI() {
 248 
 249     ClassWriter cw = new ClassWriter(0);
 250     FieldVisitor fv;
 251     MethodVisitor mv;
 252     AnnotationVisitor av0;
 253 
 254     cw.visit(52, ACC_ABSTRACT + ACC_INTERFACE, "I", null, "java/lang/Object", null);
 255 
 256     {
 257       mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null);
 258       mv.visitEnd();
 259     }
 260     {
 261       mv = cw.visitMethod(ACC_PUBLIC, "q", "()I", null, null);
 262       mv.visitCode();
 263       mv.visitInsn(ICONST_3);
 264       mv.visitInsn(IRETURN);
 265       mv.visitMaxs(1, 1);
 266       mv.visitEnd();
 267     }
 268     cw.visitEnd();
 269     
 270     return cw.toByteArray();
 271   }
 272 }