1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8087342
* @summary Test linkresolver search static, instance and overpass duplicates
* @modules java.base/jdk.internal.org.objectweb.asm
* @run main/othervm -Xverify:none TestStaticandInstance
*/
import java.util.*;
import jdk.internal.org.objectweb.asm.*;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
public class TestStaticandInstance {
static final String stringC = "C";
static final String stringD = "D";
static final String stringI = "I";
public static void main(String args[]) throws Throwable {
ClassLoader cl = new ClassLoader() {
public Class<?> loadClass(String name) throws ClassNotFoundException {
Class retClass;
if ((retClass = findLoadedClass(name)) != null) {
return retClass;
}
if (stringC.equals(name)) {
byte[] classFile=dumpC();
return defineClass(stringC, classFile, 0, classFile.length);
}
if (stringD.equals(name)) {
byte[] classFile=dumpD();
return defineClass(stringD, classFile, 0, classFile.length);
}
if (stringI.equals(name)) {
byte[] classFile=dumpI();
return defineClass(stringI, classFile, 0, classFile.length);
}
return super.loadClass(name);
}
};
Class classC = cl.loadClass(stringC);
Class classI = cl.loadClass(stringI);
try {
int staticret = (Integer)cl.loadClass(stringD).getDeclaredMethod("CallStatic").invoke(null);
if (staticret != 1) {
throw new RuntimeException("invokestatic failed to call correct method");
}
System.out.println("staticret: " + staticret); // should be 1
int invokeinterfaceret = (Integer)cl.loadClass(stringD).getDeclaredMethod("CallInterface").invoke(null);
if (invokeinterfaceret != 0) {
throw new RuntimeException(String.format("Expected java.lang.AbstractMethodError, got %d", invokeinterfaceret));
}
System.out.println("invokeinterfaceret: AbstractMethodError");
int invokevirtualret = (Integer)cl.loadClass(stringD).getDeclaredMethod("CallVirtual").invoke(null);
if (invokevirtualret != 0) {
throw new RuntimeException(String.format("Expected java.lang.IncompatibleClassChangeError, got %d", invokevirtualret));
}
System.out.println("invokevirtualret: IncompatibleClassChangeError");
} catch (java.lang.Throwable e) {
throw new RuntimeException("Unexpected exception: " + e.getMessage());
}
}
/*
interface I {
public int m(); // abstract
default int q() { return 3; } // trigger defmeth processing: C gets AME overpass
}
// C gets static, private and AME overpass m()I with -Xverify:none
class C implements I {
static int m() { return 1;} // javac with "n()" and patch to "m()"
private int m() { return 2;} // javac with public and patch to private
}
public class D {
public static int CallStatic() {
int staticret = C.m(); // javac with "C.n" and patch to "C.m"
return staticret;
}
public static int CallInterface() throws AbstractMethodError{
try {
I myI = new C();
return myI.m();
} catch (java.lang.AbstractMethodError e) {
return 0; // for success
}
}
public static int CallVirtual() {
try {
C myC = new C();
return myC.m();
} catch (java.lang.IncompatibleClassChangeError e) {
return 0; // for success
}
}
}
*/
public static byte[] dumpC() {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(52, ACC_SUPER, "C", null, "java/lang/Object", new String[] { "I" });
{
mv = cw.visitMethod(0, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_STATIC, "m", "()I", null, null);
mv.visitCode();
mv.visitInsn(ICONST_1);
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 0);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PRIVATE, "m", "()I", null, null);
mv.visitCode();
mv.visitInsn(ICONST_2);
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
public static byte[] dumpD () {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(52, ACC_PUBLIC + ACC_SUPER, "D", null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "CallStatic", "()I", null, null);
mv.visitCode();
mv.visitMethodInsn(INVOKESTATIC, "C", "m", "()I", false);
mv.visitVarInsn(ISTORE, 0);
mv.visitVarInsn(ILOAD, 0);
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "CallInterface", "()I", null, new String[] { "java/lang/AbstractMethodError" });
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/AbstractMethodError");
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, "C");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEINTERFACE, "I", "m", "()I", true);
mv.visitLabel(l1);
mv.visitInsn(IRETURN);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/AbstractMethodError"});
mv.visitVarInsn(ASTORE, 0);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "CallVirtual", "()I", null, null);
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/IncompatibleClassChangeError");
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, "C");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V", false);
mv.visitVarInsn(ASTORE, 0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, "C", "m", "()I", false);
mv.visitLabel(l1);
mv.visitInsn(IRETURN);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/IncompatibleClassChangeError"});
mv.visitVarInsn(ASTORE, 0);
mv.visitInsn(ICONST_0);
mv.visitInsn(IRETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
public static byte[] dumpI() {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(52, ACC_ABSTRACT + ACC_INTERFACE, "I", null, "java/lang/Object", null);
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "q", "()I", null, null);
mv.visitCode();
mv.visitInsn(ICONST_3);
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}
|