Commit 41b865df authored by Eric Bruneton's avatar Eric Bruneton

Add a failing test case showing the regression.

parent 6e219625
......@@ -97,7 +97,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitCode() {
mv.visitCode();
super.visitCode();
if (constructor) {
stackFrame = new ArrayList<Object>();
branches = new HashMap<Label, List<Object>>();
......@@ -109,7 +109,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitLabel(final Label label) {
mv.visitLabel(label);
super.visitLabel(label);
if (constructor && branches != null) {
List<Object> frame = branches.get(label);
if (frame != null) {
......@@ -298,7 +298,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
break;
}
}
mv.visitInsn(opcode);
super.visitInsn(opcode);
}
@Override
......@@ -335,7 +335,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitFieldInsn(
final int opcode, final String owner, final String name, final String desc) {
mv.visitFieldInsn(opcode, owner, name, desc);
super.visitFieldInsn(opcode, owner, name, desc);
if (constructor) {
char c = desc.charAt(0);
boolean longOrDouble = c == 'J' || c == 'D';
......@@ -370,7 +370,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitIntInsn(final int opcode, final int operand) {
mv.visitIntInsn(opcode, operand);
super.visitIntInsn(opcode, operand);
if (constructor && opcode != NEWARRAY) {
pushValue(OTHER);
}
......@@ -378,7 +378,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitLdcInsn(final Object cst) {
mv.visitLdcInsn(cst);
super.visitLdcInsn(cst);
if (constructor) {
pushValue(OTHER);
if (cst instanceof Double || cst instanceof Long) {
......@@ -389,7 +389,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitMultiANewArrayInsn(final String desc, final int dims) {
mv.visitMultiANewArrayInsn(desc, dims);
super.visitMultiANewArrayInsn(desc, dims);
if (constructor) {
for (int i = 0; i < dims; i++) {
popValue();
......@@ -400,7 +400,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitTypeInsn(final int opcode, final String type) {
mv.visitTypeInsn(opcode, type);
super.visitTypeInsn(opcode, type);
// ANEWARRAY, CHECKCAST or INSTANCEOF don't change stack
if (constructor && opcode == NEW) {
pushValue(OTHER);
......@@ -474,7 +474,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
mv.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
super.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
if (constructor) {
Type[] types = Type.getArgumentTypes(desc);
for (int i = 0; i < types.length; i++) {
......@@ -496,7 +496,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitJumpInsn(final int opcode, final Label label) {
mv.visitJumpInsn(opcode, label);
super.visitJumpInsn(opcode, label);
if (constructor) {
switch (opcode) {
case IFEQ:
......@@ -530,7 +530,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
mv.visitLookupSwitchInsn(dflt, keys, labels);
super.visitLookupSwitchInsn(dflt, keys, labels);
if (constructor) {
popValue();
addBranches(dflt, labels);
......@@ -540,7 +540,7 @@ public abstract class AdviceAdapter extends GeneratorAdapter implements Opcodes
@Override
public void visitTableSwitchInsn(
final int min, final int max, final Label dflt, final Label... labels) {
mv.visitTableSwitchInsn(min, max, dflt, labels);
super.visitTableSwitchInsn(min, max, dflt, labels);
if (constructor) {
popValue();
addBranches(dflt, labels);
......
......@@ -197,9 +197,7 @@ public class AnalyzerAdapter extends MethodVisitor {
"ClassReader.accept() should be called with EXPAND_FRAMES flag");
}
if (mv != null) {
mv.visitFrame(type, nLocal, local, nStack, stack);
}
super.visitFrame(type, nLocal, local, nStack, stack);
if (this.locals != null) {
this.locals.clear();
......@@ -226,9 +224,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitInsn(final int opcode) {
if (mv != null) {
mv.visitInsn(opcode);
}
super.visitInsn(opcode);
execute(opcode, 0, null);
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
this.locals = null;
......@@ -238,17 +234,13 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitIntInsn(final int opcode, final int operand) {
if (mv != null) {
mv.visitIntInsn(opcode, operand);
}
super.visitIntInsn(opcode, operand);
execute(opcode, operand, null);
}
@Override
public void visitVarInsn(final int opcode, final int var) {
if (mv != null) {
mv.visitVarInsn(opcode, var);
}
super.visitVarInsn(opcode, var);
execute(opcode, var, null);
}
......@@ -267,18 +259,14 @@ public class AnalyzerAdapter extends MethodVisitor {
uninitializedTypes.put(labels.get(i), type);
}
}
if (mv != null) {
mv.visitTypeInsn(opcode, type);
}
super.visitTypeInsn(opcode, type);
execute(opcode, 0, type);
}
@Override
public void visitFieldInsn(
final int opcode, final String owner, final String name, final String desc) {
if (mv != null) {
mv.visitFieldInsn(opcode, owner, name, desc);
}
super.visitFieldInsn(opcode, owner, name, desc);
execute(opcode, 0, desc);
}
......@@ -344,9 +332,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
if (mv != null) {
mv.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
}
super.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
if (this.locals == null) {
labels = null;
return;
......@@ -358,9 +344,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitJumpInsn(final int opcode, final Label label) {
if (mv != null) {
mv.visitJumpInsn(opcode, label);
}
super.visitJumpInsn(opcode, label);
execute(opcode, 0, null);
if (opcode == Opcodes.GOTO) {
this.locals = null;
......@@ -370,9 +354,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitLabel(final Label label) {
if (mv != null) {
mv.visitLabel(label);
}
super.visitLabel(label);
if (labels == null) {
labels = new ArrayList<Label>(3);
}
......@@ -381,9 +363,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitLdcInsn(final Object cst) {
if (mv != null) {
mv.visitLdcInsn(cst);
}
super.visitLdcInsn(cst);
if (this.locals == null) {
labels = null;
return;
......@@ -419,18 +399,14 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitIincInsn(final int var, final int increment) {
if (mv != null) {
mv.visitIincInsn(var, increment);
}
super.visitIincInsn(var, increment);
execute(Opcodes.IINC, var, null);
}
@Override
public void visitTableSwitchInsn(
final int min, final int max, final Label dflt, final Label... labels) {
if (mv != null) {
mv.visitTableSwitchInsn(min, max, dflt, labels);
}
super.visitTableSwitchInsn(min, max, dflt, labels);
execute(Opcodes.TABLESWITCH, 0, null);
this.locals = null;
this.stack = null;
......@@ -438,9 +414,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
if (mv != null) {
mv.visitLookupSwitchInsn(dflt, keys, labels);
}
super.visitLookupSwitchInsn(dflt, keys, labels);
execute(Opcodes.LOOKUPSWITCH, 0, null);
this.locals = null;
this.stack = null;
......@@ -448,9 +422,7 @@ public class AnalyzerAdapter extends MethodVisitor {
@Override
public void visitMultiANewArrayInsn(final String desc, final int dims) {
if (mv != null) {
mv.visitMultiANewArrayInsn(desc, dims);
}
super.visitMultiANewArrayInsn(desc, dims);
execute(Opcodes.MULTIANEWARRAY, dims, desc);
}
......
......@@ -63,9 +63,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
public void visitInsn(final int opcode) {
minSize += 1;
maxSize += 1;
if (mv != null) {
mv.visitInsn(opcode);
}
super.visitInsn(opcode);
}
@Override
......@@ -77,9 +75,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
minSize += 2;
maxSize += 2;
}
if (mv != null) {
mv.visitIntInsn(opcode, operand);
}
super.visitIntInsn(opcode, operand);
}
@Override
......@@ -94,18 +90,14 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
minSize += 2;
maxSize += 2;
}
if (mv != null) {
mv.visitVarInsn(opcode, var);
}
super.visitVarInsn(opcode, var);
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
minSize += 3;
maxSize += 3;
if (mv != null) {
mv.visitTypeInsn(opcode, type);
}
super.visitTypeInsn(opcode, type);
}
@Override
......@@ -113,9 +105,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
final int opcode, final String owner, final String name, final String desc) {
minSize += 3;
maxSize += 3;
if (mv != null) {
mv.visitFieldInsn(opcode, owner, name, desc);
}
super.visitFieldInsn(opcode, owner, name, desc);
}
@Deprecated
......@@ -161,9 +151,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
minSize += 5;
maxSize += 5;
if (mv != null) {
mv.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
}
super.visitInvokeDynamicInsn(name, desc, bsm, bsmArgs);
}
@Override
......@@ -174,9 +162,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
} else {
maxSize += 8;
}
if (mv != null) {
mv.visitJumpInsn(opcode, label);
}
super.visitJumpInsn(opcode, label);
}
@Override
......@@ -188,9 +174,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
minSize += 2;
maxSize += 3;
}
if (mv != null) {
mv.visitLdcInsn(cst);
}
super.visitLdcInsn(cst);
}
@Override
......@@ -202,9 +186,7 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
minSize += 3;
maxSize += 3;
}
if (mv != null) {
mv.visitIincInsn(var, increment);
}
super.visitIincInsn(var, increment);
}
@Override
......@@ -212,26 +194,20 @@ public class CodeSizeEvaluator extends MethodVisitor implements Opcodes {
final int min, final int max, final Label dflt, final Label... labels) {
minSize += 13 + labels.length * 4;
maxSize += 16 + labels.length * 4;
if (mv != null) {
mv.visitTableSwitchInsn(min, max, dflt, labels);
}
super.visitTableSwitchInsn(min, max, dflt, labels);
}
@Override
public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
minSize += 9 + keys.length * 8;
maxSize += 12 + keys.length * 8;
if (mv != null) {
mv.visitLookupSwitchInsn(dflt, keys, labels);
}
super.visitLookupSwitchInsn(dflt, keys, labels);
}
@Override
public void visitMultiANewArrayInsn(final String desc, final int dims) {
minSize += 4;
maxSize += 4;
if (mv != null) {
mv.visitMultiANewArrayInsn(desc, dims);
}
super.visitMultiANewArrayInsn(desc, dims);
}
}
......@@ -121,6 +121,7 @@ public abstract class AsmTest {
JDK8_ALL_INSTRUCTIONS("jdk8.AllInstructions"),
JDK8_ALL_STRUCTURES("jdk8.AllStructures"),
JDK8_ANONYMOUS_INNER_CLASS("jdk8.AllStructures$1"),
JDK8_ARTIFICIAL_STRUCTURES("jdk8.ArtificialStructures"),
JDK8_INNER_CLASS("jdk8.AllStructures$InnerClass"),
JDK8_LARGE_METHOD("jdk8.LargeMethod"),
JDK9_MODULE("jdk9.module-info");
......
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package jdk8;
import java.io.FileOutputStream;
import java.io.IOException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.CodeComment;
import org.objectweb.asm.Comment;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
/**
* Generates a class with structures, instructions and patterns that cannot be produced by compiling
* a Java source file with the javac compiler. This includes LDC instructions with MethodType
* constants.
*
* <p>Ideally we should not use ASM to generate this class (which is later used to test ASM), but
* this would be hard to do.
*
* @author Eric Bruneton
*/
public class DumpArtificialStructures implements Opcodes {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("ArtificialStructures.class");
fileOutputStream.write(dump());
fileOutputStream.close();
}
private static byte[] dump() {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
MethodVisitor methodVisitor;
classWriter.visit(
V1_8, ACC_PUBLIC + ACC_SUPER, "jdk8/ArtificialStructures", null, "java/lang/Object", null);
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
methodVisitor.visitCode();
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
methodVisitor.visitInsn(RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
methodVisitor =
classWriter.visitMethod(
ACC_PUBLIC + ACC_STATIC, "methodType", "()Ljava/lang/invoke/MethodType;", null, null);
methodVisitor.visitCode();
methodVisitor.visitLdcInsn(Type.getMethodType("(I)I"));
methodVisitor.visitInsn(ARETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
classWriter.visitEnd();
return classWriter.toByteArray();
}
}
......@@ -643,6 +643,12 @@ public class MethodNode extends MethodVisitor {
if (isInterface != (insn.opcode == Opcodes.INVOKEINTERFACE)) {
throw new UnsupportedClassVersionException();
}
} else if (insn instanceof LdcInsnNode) {
Object value = ((LdcInsnNode) insn).cst;
if (value instanceof Handle
|| (value instanceof Type && ((Type) value).getSort() == Type.METHOD)) {
throw new UnsupportedClassVersionException();
}
}
}
if (visibleLocalVariableAnnotations != null && !visibleLocalVariableAnnotations.isEmpty()) {
......
......@@ -530,6 +530,11 @@ public abstract class MethodVisitor {
* constants, for classes whose version is 51.0.
*/
public void visitLdcInsn(final Object value) {
if (api < Opcodes.ASM5
&& (value instanceof Handle
|| (value instanceof Type && ((Type) value).getSort() == Type.METHOD))) {
throw new UnsupportedOperationException(REQUIRES_ASM5);
}
if (mv != null) {
mv.visitLdcInsn(value);
}
......
......@@ -221,9 +221,13 @@ public class ClassReaderTest extends AsmTest implements Opcodes {
final PrecompiledClass classParameter, final Api apiParameter) {
ClassReader classReader = new ClassReader(classParameter.getBytes());
ClassVisitor classVisitor = new EmptyClassVisitor(apiParameter.value());
// jdk8.ArtificialStructures contains structures which require ASM5, but only inside the method
// code. Here we skip the code, so this class can be read with ASM4.
assertThat(() -> classReader.accept(classVisitor, ClassReader.SKIP_CODE))
.succeedsOrThrows(RuntimeException.class)
.when(classParameter.isMoreRecentThan(apiParameter));
.when(
classParameter.isMoreRecentThan(apiParameter)
&& classParameter != PrecompiledClass.JDK8_ARTIFICIAL_STRUCTURES);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment