package org.apache.bcel.verifier.structurals;
import org.apache.bcel.Const;
import org.apache.bcel.Repository;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantClass;
import org.apache.bcel.classfile.ConstantDouble;
import org.apache.bcel.classfile.ConstantFieldref;
import org.apache.bcel.classfile.ConstantFloat;
import org.apache.bcel.classfile.ConstantInteger;
import org.apache.bcel.classfile.ConstantLong;
import org.apache.bcel.classfile.ConstantString;
import org.apache.bcel.classfile.Field;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.*;
import org.apache.bcel.verifier.VerificationResult;
import org.apache.bcel.verifier.Verifier;
import org.apache.bcel.verifier.VerifierFactory;
import org.apache.bcel.verifier.exc.AssertionViolatedException;
import org.apache.bcel.verifier.exc.StructuralCodeConstraintException;
public class InstConstraintVisitor extends EmptyVisitor{
private static final ObjectType GENERIC_ARRAY = ObjectType.getInstance(GenericArray.class.getName());
public InstConstraintVisitor() {}
private Frame frame = null;
private ConstantPoolGen cpg = null;
private MethodGen mg = null;
private OperandStack stack() {
return frame.getStack();
}
private LocalVariables locals() {
return frame.getLocals();
}
private void constraintViolated(final Instruction violator, final String description) {
final String fq_classname = violator.getClass().getName();
throw new StructuralCodeConstraintException(
"Instruction "+ fq_classname.substring(fq_classname.lastIndexOf('.')+1) +" constraint violated: " + description);
}
public void setFrame(final Frame f) {
this.frame = f;
}
public void setConstantPoolGen(final ConstantPoolGen cpg) {
this.cpg = cpg;
}
public void setMethodGen(final MethodGen mg) {
this.mg = mg;
}
private void indexOfInt(final Instruction o, final Type index) {
if (! index.equals(Type.INT)) {
constraintViolated(o, "The 'index' is not of type int but of type "+index+".");
}
}
private void referenceTypeIsInitialized(final Instruction o, final ReferenceType r) {
if (r instanceof UninitializedObjectType) {
constraintViolated(o, "Working on an uninitialized object '"+r+"'.");
}
}
private void valueOfInt(final Instruction o, final Type value) {
if (! value.equals(Type.INT)) {
constraintViolated(o, "The 'value' is not of type int but of type "+value+".");
}
}
private boolean arrayrefOfArrayType(final Instruction o, final Type arrayref) {
if (! ((arrayref instanceof ArrayType) || arrayref.equals(Type.NULL)) ) {
constraintViolated(o, "The 'arrayref' does not refer to an array but is of type "+arrayref+".");
}
return arrayref instanceof ArrayType;
}
private void _visitStackAccessor(final Instruction o) {
final int consume = o.consumeStack(cpg);
if (consume > stack().slotsUsed()) {
constraintViolated(o,
"Cannot consume "+consume+" stack slots: only "+stack().slotsUsed()+" slot(s) left on stack!\nStack:\n"+stack());
}
final int produce = o.produceStack(cpg) - o.consumeStack(cpg);
if ( produce + stack().slotsUsed() > stack().maxStack() ) {
constraintViolated(o, "Cannot produce "+produce+" stack slots: only "+(stack().maxStack()-stack().slotsUsed())+
" free stack slot(s) left.\nStack:\n"+stack());
}
}
@Override
public void visitLoadClass(final LoadClass o) {
final ObjectType t = o.getLoadClassType(cpg);
if (t != null) {
final Verifier v = VerifierFactory.getVerifier(t.getClassName());
final VerificationResult vr = v.doPass2();
if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
constraintViolated((Instruction) o, "Class '"+o.getLoadClassType(cpg).getClassName()+
"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
}
}
}
@Override
public void visitStackConsumer(final StackConsumer o) {
_visitStackAccessor((Instruction) o);
}
@Override
public void visitStackProducer(final StackProducer o) {
_visitStackAccessor((Instruction) o);
}
@Override
public void visitCPInstruction(final CPInstruction o) {
final int idx = o.getIndex();
if ((idx < 0) || (idx >= cpg.getSize())) {
throw new AssertionViolatedException(
"Huh?! Constant pool index of instruction '"+o+"' illegal? Pass 3a should have checked this!");
}
}
@Override
public void visitFieldInstruction(final FieldInstruction o) {
final Constant c = cpg.getConstant(o.getIndex());
if (!(c instanceof ConstantFieldref)) {
constraintViolated(o,
"Index '"+o.getIndex()+"' should refer to a CONSTANT_Fieldref_info structure, but refers to '"+c+"'.");
}
final Type t = o.getType(cpg);
if (t instanceof ObjectType) {
final String name = ((ObjectType)t).getClassName();
final Verifier v = VerifierFactory.getVerifier( name );
final VerificationResult vr = v.doPass2();
if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
constraintViolated(o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
}
}
}
@Override
public void visitInvokeInstruction(final InvokeInstruction o) {
}
@Override
public void visitStackInstruction(final StackInstruction o) {
_visitStackAccessor(o);
}
@Override
public void visitLocalVariableInstruction(final LocalVariableInstruction o) {
if (locals().maxLocals() <= (o.getType(cpg).getSize()==1? o.getIndex() : o.getIndex()+1) ) {
constraintViolated(o, "The 'index' is not a valid index into the local variable array.");
}
}
@Override
public void visitLoadInstruction(final LoadInstruction o) {
if (locals().get(o.getIndex()) == Type.UNKNOWN) {
constraintViolated(o, "Read-Access on local variable "+o.getIndex()+" with unknown content.");
}
if (o.getType(cpg).getSize() == 2) {
if (locals().get(o.getIndex()+1) != Type.UNKNOWN) {
constraintViolated(o,
"Reading a two-locals value from local variables "+o.getIndex()+
" and "+(o.getIndex()+1)+" where the latter one is destroyed.");
}
}
if (!(o instanceof ALOAD)) {
if (locals().get(o.getIndex()) != o.getType(cpg) ) {
constraintViolated(o,"Local Variable type and LOADing Instruction type mismatch: Local Variable: '"+
locals().get(o.getIndex())+"'; Instruction type: '"+o.getType(cpg)+"'.");
}
}
else{
if (!(locals().get(o.getIndex()) instanceof ReferenceType)) {
constraintViolated(o, "Local Variable type and LOADing Instruction type mismatch: Local Variable: '"+
locals().get(o.getIndex())+"'; Instruction expects a ReferenceType.");
}
}
if ((stack().maxStack() - stack().slotsUsed()) < o.getType(cpg).getSize()) {
constraintViolated(o, "Not enough free stack slots to load a '"+o.getType(cpg)+"' onto the OperandStack.");
}
}
@Override
public void visitStoreInstruction(final StoreInstruction o) {
if (stack().isEmpty()) {
constraintViolated(o, "Cannot STORE: Stack to read from is empty.");
}
if ( !(o instanceof ASTORE) ) {
if (! (stack().peek() == o.getType(cpg)) ) {
constraintViolated(o, "Stack top type and STOREing Instruction type mismatch: Stack top: '"+stack().peek()+
"'; Instruction type: '"+o.getType(cpg)+"'.");
}
}
else{
final Type stacktop = stack().peek();
if ( (!(stacktop instanceof ReferenceType)) && (!(stacktop instanceof ReturnaddressType)) ) {
constraintViolated(o, "Stack top type and STOREing Instruction type mismatch: Stack top: '"+stack().peek()+
"'; Instruction expects a ReferenceType or a ReturnadressType.");
}
}
}
@Override
public void visitReturnInstruction(final ReturnInstruction o) {
Type method_type = mg.getType();
if (method_type == Type.BOOLEAN ||
method_type == Type.BYTE ||
method_type == Type.SHORT ||
method_type == Type.CHAR) {
method_type = Type.INT;
}
if (o instanceof RETURN) {
if (method_type != Type.VOID) {
constraintViolated(o, "RETURN instruction in non-void method.");
}
else{
return;
}
}
if (o instanceof ARETURN) {
if (method_type == Type.VOID) {
constraintViolated(o, "ARETURN instruction in void method.");
}
if (stack().peek() == Type.NULL) {
return;
}
if (! (stack().peek() instanceof ReferenceType)) {
constraintViolated(o, "Reference type expected on top of stack, but is: '"+stack().peek()+"'.");
}
referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()));
}
else{
if (! ( method_type.equals( stack().peek() ))) {
constraintViolated(o, "Current method has return type of '"+mg.getType()+"' expecting a '"+method_type+
"' on top of the stack. But stack top is a '"+stack().peek()+"'.");
}
}
}
@Override
public void visitAALOAD(final AALOAD o) {
final Type arrayref = stack().peek(1);
final Type index = stack().peek(0);
indexOfInt(o, index);
if (arrayrefOfArrayType(o, arrayref)) {
if (! (((ArrayType) arrayref).getElementType() instanceof ReferenceType)) {
constraintViolated(o,
"The 'arrayref' does not refer to an array with elements of a ReferenceType but to an array of "+
((ArrayType) arrayref).getElementType()+".");
}
}
}
@Override
public void visitAASTORE(final AASTORE o) {
final Type arrayref = stack().peek(2);
final Type index = stack().peek(1);
final Type value = stack().peek(0);
indexOfInt(o, index);
if (!(value instanceof ReferenceType)) {
constraintViolated(o, "The 'value' is not of a ReferenceType but of type "+value+".");
}else{
}
if (arrayrefOfArrayType(o, arrayref)) {
if (! (((ArrayType) arrayref).getElementType() instanceof ReferenceType)) {
constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a ReferenceType but to an array of "+
((ArrayType) arrayref).getElementType()+".");
}
}
}
@Override
public void visitACONST_NULL(final ACONST_NULL o) {
}
@Override
public void visitALOAD(final ALOAD o) {
}
@Override
public void visitANEWARRAY(final ANEWARRAY o) {
if (!stack().peek().equals(Type.INT)) {
constraintViolated(o, "The 'count' at the stack top is not of type '"+Type.INT+"' but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitARETURN(final ARETURN o) {
if (! (stack().peek() instanceof ReferenceType) ) {
constraintViolated(o, "The 'objectref' at the stack top is not of a ReferenceType but of type '"+stack().peek()+"'.");
}
final ReferenceType objectref = (ReferenceType) (stack().peek());
referenceTypeIsInitialized(o, objectref);
}
@Override
public void visitARRAYLENGTH(final ARRAYLENGTH o) {
final Type arrayref = stack().peek(0);
arrayrefOfArrayType(o, arrayref);
}
@Override
public void visitASTORE(final ASTORE o) {
if (! ( (stack().peek() instanceof ReferenceType) || (stack().peek() instanceof ReturnaddressType) ) ) {
constraintViolated(o, "The 'objectref' is not of a ReferenceType or of ReturnaddressType but of "+stack().peek()+".");
}
}
@Override
public void visitATHROW(final ATHROW o) {
try {
if (! ((stack().peek() instanceof ObjectType) || (stack().peek().equals(Type.NULL))) ) {
constraintViolated(o, "The 'objectref' is not of an (initialized) ObjectType but of type "+stack().peek()+".");
}
if (stack().peek().equals(Type.NULL)) {
return;
}
final ObjectType exc = (ObjectType) (stack().peek());
final ObjectType throwable = (ObjectType) (Type.getType("Ljava/lang/Throwable;"));
if ( (! (exc.subclassOf(throwable)) ) && (! (exc.equals(throwable))) ) {
constraintViolated(o,
"The 'objectref' is not of class Throwable or of a subclass of Throwable, but of '"+stack().peek()+"'.");
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitBALOAD(final BALOAD o) {
final Type arrayref = stack().peek(1);
final Type index = stack().peek(0);
indexOfInt(o, index);
if (arrayrefOfArrayType(o, arrayref)) {
if (! ( (((ArrayType) arrayref).getElementType().equals(Type.BOOLEAN)) ||
(((ArrayType) arrayref).getElementType().equals(Type.BYTE)) ) ) {
constraintViolated(o,
"The 'arrayref' does not refer to an array with elements of a Type.BYTE or Type.BOOLEAN but to an array of '"+
((ArrayType) arrayref).getElementType()+"'.");
}
}
}
@Override
public void visitBASTORE(final BASTORE o) {
final Type arrayref = stack().peek(2);
final Type index = stack().peek(1);
final Type value = stack().peek(0);
indexOfInt(o, index);
valueOfInt(o, value);
if (arrayrefOfArrayType(o, arrayref)) {
if (! ( (((ArrayType) arrayref).getElementType().equals(Type.BOOLEAN)) ||
(((ArrayType) arrayref).getElementType().equals(Type.BYTE)) ) ) {
constraintViolated(o,
"The 'arrayref' does not refer to an array with elements of a Type.BYTE or Type.BOOLEAN but to an array of '"+
((ArrayType) arrayref).getElementType()+"'.");
}
}
}
@Override
public void visitBIPUSH(final BIPUSH o) {
}
@Override
public void visitBREAKPOINT(final BREAKPOINT o) {
throw new AssertionViolatedException(
"In this JustIce verification pass there should not occur an illegal instruction such as BREAKPOINT.");
}
@Override
public void visitCALOAD(final CALOAD o) {
final Type arrayref = stack().peek(1);
final Type index = stack().peek(0);
indexOfInt(o, index);
arrayrefOfArrayType(o, arrayref);
}
@Override
public void visitCASTORE(final CASTORE o) {
final Type arrayref = stack().peek(2);
final Type index = stack().peek(1);
final Type value = stack().peek(0);
indexOfInt(o, index);
valueOfInt(o, value);
if (arrayrefOfArrayType(o, arrayref)) {
if (! ((ArrayType) arrayref).getElementType().equals(Type.CHAR) ) {
constraintViolated(o, "The 'arrayref' does not refer to an array with elements of type char but to an array of type "+
((ArrayType) arrayref).getElementType()+".");
}
}
}
@Override
public void visitCHECKCAST(final CHECKCAST o) {
final Type objectref = stack().peek(0);
if (!(objectref instanceof ReferenceType)) {
constraintViolated(o, "The 'objectref' is not of a ReferenceType but of type "+objectref+".");
}
final Constant c = cpg.getConstant(o.getIndex());
if (! (c instanceof ConstantClass)) {
constraintViolated(o, "The Constant at 'index' is not a ConstantClass, but '"+c+"'.");
}
}
@Override
public void visitD2F(final D2F o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitD2I(final D2I o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitD2L(final D2L o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitDADD(final DADD o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDALOAD(final DALOAD o) {
indexOfInt(o, stack().peek());
if (stack().peek(1) == Type.NULL) {
return;
}
if (! (stack().peek(1) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-top must be of type double[] but is '"+stack().peek(1)+"'.");
}
final Type t = ((ArrayType) (stack().peek(1))).getBasicType();
if (t != Type.DOUBLE) {
constraintViolated(o, "Stack next-to-top must be of type double[] but is '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDASTORE(final DASTORE o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
indexOfInt(o, stack().peek(1));
if (stack().peek(2) == Type.NULL) {
return;
}
if (! (stack().peek(2) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-next-to-top must be of type double[] but is '"+stack().peek(2)+"'.");
}
final Type t = ((ArrayType) (stack().peek(2))).getBasicType();
if (t != Type.DOUBLE) {
constraintViolated(o, "Stack next-to-next-to-top must be of type double[] but is '"+stack().peek(2)+"'.");
}
}
@Override
public void visitDCMPG(final DCMPG o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDCMPL(final DCMPL o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDCONST(final DCONST o) {
}
@Override
public void visitDDIV(final DDIV o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDLOAD(final DLOAD o) {
}
@Override
public void visitDMUL(final DMUL o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDNEG(final DNEG o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitDREM(final DREM o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDRETURN(final DRETURN o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitDSTORE(final DSTORE o) {
}
@Override
public void visitDSUB(final DSUB o) {
if (stack().peek() != Type.DOUBLE) {
constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.DOUBLE) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitDUP(final DUP o) {
if (stack().peek().getSize() != 1) {
constraintViolated(o, "Won't DUP type on stack top '"+stack().peek()+
"' because it must occupy exactly one slot, not '"+stack().peek().getSize()+"'.");
}
}
@Override
public void visitDUP_X1(final DUP_X1 o) {
if (stack().peek().getSize() != 1) {
constraintViolated(o,
"Type on stack top '"+stack().peek()+"' should occupy exactly one slot, not '"+stack().peek().getSize()+"'.");
}
if (stack().peek(1).getSize() != 1) {
constraintViolated(o,
"Type on stack next-to-top '"+stack().peek(1)+
"' should occupy exactly one slot, not '"+stack().peek(1).getSize()+"'.");
}
}
@Override
public void visitDUP_X2(final DUP_X2 o) {
if (stack().peek().getSize() != 1) {
constraintViolated(o,
"Stack top type must be of size 1, but is '"+stack().peek()+"' of size '"+stack().peek().getSize()+"'.");
}
if (stack().peek(1).getSize() == 2) {
return;
}
if (stack().peek(2).getSize() != 1) {
constraintViolated(o,
"If stack top's size is 1 and stack next-to-top's size is 1,"+
" stack next-to-next-to-top's size must also be 1, but is: '"+
stack().peek(2)+"' of size '"+stack().peek(2).getSize()+"'.");
}
}
@Override
public void visitDUP2(final DUP2 o) {
if (stack().peek().getSize() == 2) {
return;
}
if (stack().peek(1).getSize() != 1) {
constraintViolated(o,
"If stack top's size is 1, then stack next-to-top's size must also be 1. But it is '"+stack().peek(1)+
"' of size '"+stack().peek(1).getSize()+"'.");
}
}
@Override
public void visitDUP2_X1(final DUP2_X1 o) {
if (stack().peek().getSize() == 2) {
if (stack().peek(1).getSize() != 1) {
constraintViolated(o, "If stack top's size is 2, then stack next-to-top's size must be 1. But it is '"+
stack().peek(1)+"' of size '"+stack().peek(1).getSize()+"'.");
}
else{
return;
}
}
else{
if ( stack().peek(1).getSize() != 1 ) {
constraintViolated(o, "If stack top's size is 1, then stack next-to-top's size must also be 1. But it is '"+
stack().peek(1)+"' of size '"+stack().peek(1).getSize()+"'.");
}
if ( stack().peek(2).getSize() != 1 ) {
constraintViolated(o, "If stack top's size is 1, then stack next-to-next-to-top's size must also be 1. But it is '"+
stack().peek(2)+"' of size '"+stack().peek(2).getSize()+"'.");
}
}
}
@Override
public void visitDUP2_X2(final DUP2_X2 o) {
if (stack().peek(0).getSize() == 2) {
if (stack().peek(1).getSize() == 2) {
return;
}
if ( stack().peek(2).getSize() != 1 ) {
constraintViolated(o, "If stack top's size is 2 and stack-next-to-top's size is 1,"+
" then stack next-to-next-to-top's size must also be 1. But it is '"+stack().peek(2)+
"' of size '"+stack().peek(2).getSize()+"'.");
}
else{
return;
}
}
else{
if (stack().peek(1).getSize() == 1) {
if ( stack().peek(2).getSize() == 2 ) {
return;
}
if ( stack().peek(3).getSize() == 1) {
return;
}
}
}
constraintViolated(o, "The operand sizes on the stack do not match any of the four forms of usage of this instruction.");
}
@Override
public void visitF2D(final F2D o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitF2I(final F2I o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitF2L(final F2L o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitFADD(final FADD o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFALOAD(final FALOAD o) {
indexOfInt(o, stack().peek());
if (stack().peek(1) == Type.NULL) {
return;
}
if (! (stack().peek(1) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-top must be of type float[] but is '"+stack().peek(1)+"'.");
}
final Type t = ((ArrayType) (stack().peek(1))).getBasicType();
if (t != Type.FLOAT) {
constraintViolated(o, "Stack next-to-top must be of type float[] but is '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFASTORE(final FASTORE o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
indexOfInt(o, stack().peek(1));
if (stack().peek(2) == Type.NULL) {
return;
}
if (! (stack().peek(2) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-next-to-top must be of type float[] but is '"+stack().peek(2)+"'.");
}
final Type t = ((ArrayType) (stack().peek(2))).getBasicType();
if (t != Type.FLOAT) {
constraintViolated(o, "Stack next-to-next-to-top must be of type float[] but is '"+stack().peek(2)+"'.");
}
}
@Override
public void visitFCMPG(final FCMPG o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFCMPL(final FCMPL o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFCONST(final FCONST o) {
}
@Override
public void visitFDIV(final FDIV o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFLOAD(final FLOAD o) {
}
@Override
public void visitFMUL(final FMUL o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFNEG(final FNEG o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitFREM(final FREM o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitFRETURN(final FRETURN o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitFSTORE(final FSTORE o) {
}
@Override
public void visitFSUB(final FSUB o) {
if (stack().peek() != Type.FLOAT) {
constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.FLOAT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
}
}
private ObjectType getObjectType(final FieldInstruction o) {
final ReferenceType rt = o.getReferenceType(cpg);
if(rt instanceof ObjectType) {
return (ObjectType)rt;
}
constraintViolated(o, "expecting ObjectType but got "+rt);
return null;
}
@Override
public void visitGETFIELD(final GETFIELD o) {
try {
final Type objectref = stack().peek();
if (! ( (objectref instanceof ObjectType) || (objectref == Type.NULL) ) ) {
constraintViolated(o, "Stack top should be an object reference that's not an array reference, but is '"+objectref+"'.");
}
final String field_name = o.getFieldName(cpg);
final JavaClass jc = Repository.lookupClass(getObjectType(o).getClassName());
Field[] fields = jc.getFields();
Field f = null;
for (final Field field : fields) {
if (field.getName().equals(field_name)) {
final Type f_type = Type.getType(field.getSignature());
final Type o_type = o.getType(cpg);
if (f_type.equals(o_type)) {
f = field;
break;
}
}
}
if (f == null) {
final JavaClass[] superclasses = jc.getSuperClasses();
outer:
for (final JavaClass superclass : superclasses) {
fields = superclass.getFields();
for (final Field field : fields) {
if (field.getName().equals(field_name)) {
final Type f_type = Type.getType(field.getSignature());
final Type o_type = o.getType(cpg);
if (f_type.equals(o_type)) {
f = field;
if ((f.getAccessFlags() & (Const.ACC_PUBLIC | Const.ACC_PROTECTED)) == 0) {
f = null;
}
break outer;
}
}
}
}
if (f == null) {
throw new AssertionViolatedException("Field '" + field_name + "' not found in " + jc.getClassName());
}
}
if (f.isProtected()) {
final ObjectType classtype = getObjectType(o);
final ObjectType curr = ObjectType.getInstance(mg.getClassName());
if ( classtype.equals(curr) ||
curr.subclassOf(classtype) ) {
final Type t = stack().peek();
if (t == Type.NULL) {
return;
}
if (! (t instanceof ObjectType) ) {
constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '"+t+"'.");
}
final ObjectType objreftype = (ObjectType) t;
if (! ( objreftype.equals(curr) ||
objreftype.subclassOf(curr) ) ) {
}
}
}
if (f.isStatic()) {
constraintViolated(o, "Referenced field '"+f+"' is static which it shouldn't be.");
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitGETSTATIC(final GETSTATIC o) {
}
@Override
public void visitGOTO(final GOTO o) {
}
@Override
public void visitGOTO_W(final GOTO_W o) {
}
@Override
public void visitI2B(final I2B o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitI2C(final I2C o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitI2D(final I2D o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitI2F(final I2F o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitI2L(final I2L o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitI2S(final I2S o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIADD(final IADD o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIALOAD(final IALOAD o) {
indexOfInt(o, stack().peek());
if (stack().peek(1) == Type.NULL) {
return;
}
if (! (stack().peek(1) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-top must be of type int[] but is '"+stack().peek(1)+"'.");
}
final Type t = ((ArrayType) (stack().peek(1))).getBasicType();
if (t != Type.INT) {
constraintViolated(o, "Stack next-to-top must be of type int[] but is '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIAND(final IAND o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIASTORE(final IASTORE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
indexOfInt(o, stack().peek(1));
if (stack().peek(2) == Type.NULL) {
return;
}
if (! (stack().peek(2) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-next-to-top must be of type int[] but is '"+stack().peek(2)+"'.");
}
final Type t = ((ArrayType) (stack().peek(2))).getBasicType();
if (t != Type.INT) {
constraintViolated(o, "Stack next-to-next-to-top must be of type int[] but is '"+stack().peek(2)+"'.");
}
}
@Override
public void visitICONST(final ICONST o) {
}
@Override
public void visitIDIV(final IDIV o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ACMPEQ(final IF_ACMPEQ o) {
if (!(stack().peek() instanceof ReferenceType)) {
constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
}
if (!(stack().peek(1) instanceof ReferenceType)) {
constraintViolated(o, "The value at the stack next-to-top is not of a ReferenceType, but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ACMPNE(final IF_ACMPNE o) {
if (!(stack().peek() instanceof ReferenceType)) {
constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
}
if (!(stack().peek(1) instanceof ReferenceType)) {
constraintViolated(o, "The value at the stack next-to-top is not of a ReferenceType, but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ICMPEQ(final IF_ICMPEQ o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ICMPGE(final IF_ICMPGE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ICMPGT(final IF_ICMPGT o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ICMPLE(final IF_ICMPLE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ICMPLT(final IF_ICMPLT o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIF_ICMPNE(final IF_ICMPNE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIFEQ(final IFEQ o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIFGE(final IFGE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIFGT(final IFGT o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIFLE(final IFLE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIFLT(final IFLT o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIFNE(final IFNE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitIFNONNULL(final IFNONNULL o) {
if (!(stack().peek() instanceof ReferenceType)) {
constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
}
referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
}
@Override
public void visitIFNULL(final IFNULL o) {
if (!(stack().peek() instanceof ReferenceType)) {
constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
}
referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
}
@Override
public void visitIINC(final IINC o) {
if (locals().maxLocals() <= (o.getType(cpg).getSize()==1? o.getIndex() : o.getIndex()+1) ) {
constraintViolated(o, "The 'index' is not a valid index into the local variable array.");
}
indexOfInt(o, locals().get(o.getIndex()));
}
@Override
public void visitILOAD(final ILOAD o) {
}
@Override
public void visitIMPDEP1(final IMPDEP1 o) {
throw new AssertionViolatedException(
"In this JustIce verification pass there should not occur an illegal instruction such as IMPDEP1.");
}
@Override
public void visitIMPDEP2(final IMPDEP2 o) {
throw new AssertionViolatedException(
"In this JustIce verification pass there should not occur an illegal instruction such as IMPDEP2.");
}
@Override
public void visitIMUL(final IMUL o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitINEG(final INEG o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitINSTANCEOF(final INSTANCEOF o) {
final Type objectref = stack().peek(0);
if (!(objectref instanceof ReferenceType)) {
constraintViolated(o, "The 'objectref' is not of a ReferenceType but of type "+objectref+".");
}
final Constant c = cpg.getConstant(o.getIndex());
if (! (c instanceof ConstantClass)) {
constraintViolated(o, "The Constant at 'index' is not a ConstantClass, but '"+c+"'.");
}
}
@Override
public void visitINVOKEDYNAMIC(final INVOKEDYNAMIC o) {
throw new RuntimeException("INVOKEDYNAMIC instruction is not supported at this time");
}
@Override
public void visitINVOKEINTERFACE(final INVOKEINTERFACE o) {
final int count = o.getCount();
if (count == 0) {
constraintViolated(o, "The 'count' argument must not be 0.");
}
final Type t = o.getType(cpg);
if (t instanceof ObjectType) {
final String name = ((ObjectType)t).getClassName();
final Verifier v = VerifierFactory.getVerifier( name );
final VerificationResult vr = v.doPass2();
if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
constraintViolated(o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
}
}
final Type[] argtypes = o.getArgumentTypes(cpg);
final int nargs = argtypes.length;
for (int i=nargs-1; i>=0; i--) {
final Type fromStack = stack().peek( (nargs-1) - i );
Type fromDesc = argtypes[i];
if (fromDesc == Type.BOOLEAN ||
fromDesc == Type.BYTE ||
fromDesc == Type.CHAR ||
fromDesc == Type.SHORT) {
fromDesc = Type.INT;
}
if (! fromStack.equals(fromDesc)) {
if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType) {
final ReferenceType rFromStack = (ReferenceType) fromStack;
referenceTypeIsInitialized(o, rFromStack);
}
else{
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
}
}
}
Type objref = stack().peek(nargs);
if (objref == Type.NULL) {
return;
}
if (! (objref instanceof ReferenceType) ) {
constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '"+objref+"'.");
}
referenceTypeIsInitialized(o, (ReferenceType) objref);
if (!(objref instanceof ObjectType)) {
if (!(objref instanceof ArrayType)) {
constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'.");
}
else{
objref = GENERIC_ARRAY;
}
}
int counted_count = 1;
for (int i=0; i<nargs; i++) {
counted_count += argtypes[i].getSize();
}
if (count != counted_count) {
constraintViolated(o, "The 'count' argument should probably read '"+counted_count+"' but is '"+count+"'.");
}
}
@Override
public void visitINVOKESPECIAL(final INVOKESPECIAL o) {
try {
if ( (o.getMethodName(cpg).equals(Const.CONSTRUCTOR_NAME)) &&
(!(stack().peek(o.getArgumentTypes(cpg).length) instanceof UninitializedObjectType)) ) {
constraintViolated(o, "Possibly initializing object twice."+
" A valid instruction sequence must not have an uninitialized object on the operand stack or in a local variable"+
" during a backwards branch, or in a local variable in code protected by an exception handler."+
" Please see The Java Virtual Machine Specification, Second Edition, 4.9.4 (pages 147 and 148) for details.");
}
final Type t = o.getType(cpg);
if (t instanceof ObjectType) {
final String name = ((ObjectType)t).getClassName();
final Verifier v = VerifierFactory.getVerifier( name );
final VerificationResult vr = v.doPass2();
if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
constraintViolated(o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
}
}
final Type[] argtypes = o.getArgumentTypes(cpg);
final int nargs = argtypes.length;
for (int i=nargs-1; i>=0; i--) {
final Type fromStack = stack().peek( (nargs-1) - i );
Type fromDesc = argtypes[i];
if (fromDesc == Type.BOOLEAN ||
fromDesc == Type.BYTE ||
fromDesc == Type.CHAR ||
fromDesc == Type.SHORT) {
fromDesc = Type.INT;
}
if (! fromStack.equals(fromDesc)) {
if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType) {
final ReferenceType rFromStack = (ReferenceType) fromStack;
final ReferenceType rFromDesc = (ReferenceType) fromDesc;
if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ) {
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+
"' on the stack (which is not assignment compatible).");
}
referenceTypeIsInitialized(o, rFromStack);
}
else{
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
}
}
}
Type objref = stack().peek(nargs);
if (objref == Type.NULL) {
return;
}
if (! (objref instanceof ReferenceType) ) {
constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '"+objref+"'.");
}
String objref_classname = null;
if ( !(o.getMethodName(cpg).equals(Const.CONSTRUCTOR_NAME))) {
referenceTypeIsInitialized(o, (ReferenceType) objref);
if (!(objref instanceof ObjectType)) {
if (!(objref instanceof ArrayType)) {
constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'.");
}
else{
objref = GENERIC_ARRAY;
}
}
objref_classname = ((ObjectType) objref).getClassName();
}
else{
if (!(objref instanceof UninitializedObjectType)) {
constraintViolated(o, "Expecting an UninitializedObjectType as 'objectref' on the stack, not a '"+objref+
"'. Otherwise, you couldn't invoke a method since an array has no methods (not to speak of a return address).");
}
objref_classname = ((UninitializedObjectType) objref).getInitialized().getClassName();
}
final String theClass = o.getClassName(cpg);
if ( ! Repository.instanceOf(objref_classname, theClass) ) {
constraintViolated(o, "The 'objref' item '"+objref+"' does not implement '"+theClass+"' as expected.");
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitINVOKESTATIC(final INVOKESTATIC o) {
try {
final Type t = o.getType(cpg);
if (t instanceof ObjectType) {
final String name = ((ObjectType)t).getClassName();
final Verifier v = VerifierFactory.getVerifier( name );
final VerificationResult vr = v.doPass2();
if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
constraintViolated(o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
}
}
final Type[] argtypes = o.getArgumentTypes(cpg);
final int nargs = argtypes.length;
for (int i=nargs-1; i>=0; i--) {
final Type fromStack = stack().peek( (nargs-1) - i );
Type fromDesc = argtypes[i];
if (fromDesc == Type.BOOLEAN ||
fromDesc == Type.BYTE ||
fromDesc == Type.CHAR ||
fromDesc == Type.SHORT) {
fromDesc = Type.INT;
}
if (! fromStack.equals(fromDesc)) {
if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType) {
final ReferenceType rFromStack = (ReferenceType) fromStack;
final ReferenceType rFromDesc = (ReferenceType) fromDesc;
if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ) {
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+
"' on the stack (which is not assignment compatible).");
}
referenceTypeIsInitialized(o, rFromStack);
}
else{
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
}
}
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) {
try {
final Type t = o.getType(cpg);
if (t instanceof ObjectType) {
final String name = ((ObjectType)t).getClassName();
final Verifier v = VerifierFactory.getVerifier( name );
final VerificationResult vr = v.doPass2();
if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
constraintViolated(o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
}
}
final Type[] argtypes = o.getArgumentTypes(cpg);
final int nargs = argtypes.length;
for (int i=nargs-1; i>=0; i--) {
final Type fromStack = stack().peek( (nargs-1) - i );
Type fromDesc = argtypes[i];
if (fromDesc == Type.BOOLEAN ||
fromDesc == Type.BYTE ||
fromDesc == Type.CHAR ||
fromDesc == Type.SHORT) {
fromDesc = Type.INT;
}
if (! fromStack.equals(fromDesc)) {
if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType) {
final ReferenceType rFromStack = (ReferenceType) fromStack;
final ReferenceType rFromDesc = (ReferenceType) fromDesc;
if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ) {
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+
"' on the stack (which is not assignment compatible).");
}
referenceTypeIsInitialized(o, rFromStack);
}
else{
constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
}
}
}
Type objref = stack().peek(nargs);
if (objref == Type.NULL) {
return;
}
if (! (objref instanceof ReferenceType) ) {
constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '"+objref+"'.");
}
referenceTypeIsInitialized(o, (ReferenceType) objref);
if (!(objref instanceof ObjectType)) {
if (!(objref instanceof ArrayType)) {
constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'.");
}
else{
objref = GENERIC_ARRAY;
}
}
final String objref_classname = ((ObjectType) objref).getClassName();
final String theClass = o.getClassName(cpg);
if ( ! Repository.instanceOf(objref_classname, theClass) ) {
constraintViolated(o, "The 'objref' item '"+objref+"' does not implement '"+theClass+"' as expected.");
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitIOR(final IOR o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIREM(final IREM o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIRETURN(final IRETURN o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitISHL(final ISHL o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitISHR(final ISHR o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitISTORE(final ISTORE o) {
}
@Override
public void visitISUB(final ISUB o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIUSHR(final IUSHR o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitIXOR(final IXOR o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.INT) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitJSR(final JSR o) {
}
@Override
public void visitJSR_W(final JSR_W o) {
}
@Override
public void visitL2D(final L2D o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitL2F(final L2F o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitL2I(final L2I o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitLADD(final LADD o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLALOAD(final LALOAD o) {
indexOfInt(o, stack().peek());
if (stack().peek(1) == Type.NULL) {
return;
}
if (! (stack().peek(1) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-top must be of type long[] but is '"+stack().peek(1)+"'.");
}
final Type t = ((ArrayType) (stack().peek(1))).getBasicType();
if (t != Type.LONG) {
constraintViolated(o, "Stack next-to-top must be of type long[] but is '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLAND(final LAND o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLASTORE(final LASTORE o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
indexOfInt(o, stack().peek(1));
if (stack().peek(2) == Type.NULL) {
return;
}
if (! (stack().peek(2) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-next-to-top must be of type long[] but is '"+stack().peek(2)+"'.");
}
final Type t = ((ArrayType) (stack().peek(2))).getBasicType();
if (t != Type.LONG) {
constraintViolated(o, "Stack next-to-next-to-top must be of type long[] but is '"+stack().peek(2)+"'.");
}
}
@Override
public void visitLCMP(final LCMP o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLCONST(final LCONST o) {
}
@Override
public void visitLDC(final LDC o) {
final Constant c = cpg.getConstant(o.getIndex());
if (! ( ( c instanceof ConstantInteger) ||
( c instanceof ConstantFloat ) ||
( c instanceof ConstantString ) ||
( c instanceof ConstantClass ) ) ) {
constraintViolated(o,
"Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '"+
c + "'.");
}
}
public void visitLDC_W(final LDC_W o) {
final Constant c = cpg.getConstant(o.getIndex());
if (! ( ( c instanceof ConstantInteger) ||
( c instanceof ConstantFloat ) ||
( c instanceof ConstantString ) ||
( c instanceof ConstantClass ) ) ) {
constraintViolated(o,
"Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '"+
c + "'.");
}
}
@Override
public void visitLDC2_W(final LDC2_W o) {
final Constant c = cpg.getConstant(o.getIndex());
if (! ( ( c instanceof ConstantLong) ||
( c instanceof ConstantDouble ) ) ) {
constraintViolated(o,
"Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
}
}
@Override
public void visitLDIV(final LDIV o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLLOAD(final LLOAD o) {
}
@Override
public void visitLMUL(final LMUL o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLNEG(final LNEG o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitLOOKUPSWITCH(final LOOKUPSWITCH o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitLOR(final LOR o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLREM(final LREM o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLRETURN(final LRETURN o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitLSHL(final LSHL o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLSHR(final LSHR o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLSTORE(final LSTORE o) {
}
@Override
public void visitLSUB(final LSUB o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLUSHR(final LUSHR o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitLXOR(final LXOR o) {
if (stack().peek() != Type.LONG) {
constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
}
if (stack().peek(1) != Type.LONG) {
constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
}
}
@Override
public void visitMONITORENTER(final MONITORENTER o) {
if (! ((stack().peek()) instanceof ReferenceType)) {
constraintViolated(o, "The stack top should be of a ReferenceType, but is '"+stack().peek()+"'.");
}
}
@Override
public void visitMONITOREXIT(final MONITOREXIT o) {
if (! ((stack().peek()) instanceof ReferenceType)) {
constraintViolated(o, "The stack top should be of a ReferenceType, but is '"+stack().peek()+"'.");
}
}
@Override
public void visitMULTIANEWARRAY(final MULTIANEWARRAY o) {
final int dimensions = o.getDimensions();
for (int i=0; i<dimensions; i++) {
if (stack().peek(i) != Type.INT) {
constraintViolated(o, "The '"+dimensions+"' upper stack types should be 'int' but aren't.");
}
}
}
@Override
public void visitNEW(final NEW o) {
final Type t = o.getType(cpg);
if (! (t instanceof ReferenceType)) {
throw new AssertionViolatedException("NEW.getType() returning a non-reference type?!");
}
if (! (t instanceof ObjectType)) {
constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '"+t+"'.");
}
final ObjectType obj = (ObjectType) t;
try {
if (! obj.referencesClassExact()) {
constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '"+obj+"'.");
}
} catch (final ClassNotFoundException e) {
constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '"+obj+"'." + " which threw " + e);
}
}
@Override
public void visitNEWARRAY(final NEWARRAY o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
}
@Override
public void visitNOP(final NOP o) {
}
@Override
public void visitPOP(final POP o) {
if (stack().peek().getSize() != 1) {
constraintViolated(o,
"Stack top size should be 1 but stack top is '"+stack().peek()+"' of size '"+stack().peek().getSize()+"'.");
}
}
@Override
public void visitPOP2(final POP2 o) {
if (stack().peek().getSize() != 2) {
constraintViolated(o,
"Stack top size should be 2 but stack top is '"+stack().peek()+"' of size '"+stack().peek().getSize()+"'.");
}
}
@Override
public void visitPUTFIELD(final PUTFIELD o) {
try {
final Type objectref = stack().peek(1);
if (! ( (objectref instanceof ObjectType) || (objectref == Type.NULL) ) ) {
constraintViolated(o,
"Stack next-to-top should be an object reference that's not an array reference, but is '"+objectref+"'.");
}
final String field_name = o.getFieldName(cpg);
final JavaClass jc = Repository.lookupClass(getObjectType(o).getClassName());
final Field[] fields = jc.getFields();
Field f = null;
for (final Field field : fields) {
if (field.getName().equals(field_name)) {
final Type f_type = Type.getType(field.getSignature());
final Type o_type = o.getType(cpg);
if (f_type.equals(o_type)) {
f = field;
break;
}
}
}
if (f == null) {
throw new AssertionViolatedException("Field '" + field_name + "' not found in " + jc.getClassName());
}
final Type value = stack().peek();
final Type t = Type.getType(f.getSignature());
Type shouldbe = t;
if (shouldbe == Type.BOOLEAN ||
shouldbe == Type.BYTE ||
shouldbe == Type.CHAR ||
shouldbe == Type.SHORT) {
shouldbe = Type.INT;
}
if (t instanceof ReferenceType) {
ReferenceType rvalue = null;
if (value instanceof ReferenceType) {
rvalue = (ReferenceType) value;
referenceTypeIsInitialized(o, rvalue);
}
else{
constraintViolated(o, "The stack top type '"+value+"' is not of a reference type as expected.");
}
if (!(rvalue.isAssignmentCompatibleWith(shouldbe))) {
constraintViolated(o, "The stack top type '"+value+"' is not assignment compatible with '"+shouldbe+"'.");
}
}
else{
if (shouldbe != value) {
constraintViolated(o, "The stack top type '"+value+"' is not of type '"+shouldbe+"' as expected.");
}
}
if (f.isProtected()) {
final ObjectType classtype = getObjectType(o);
final ObjectType curr = ObjectType.getInstance(mg.getClassName());
if ( classtype.equals(curr) ||
curr.subclassOf(classtype) ) {
final Type tp = stack().peek(1);
if (tp == Type.NULL) {
return;
}
if (! (tp instanceof ObjectType) ) {
constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '"+tp+"'.");
}
final ObjectType objreftype = (ObjectType) tp;
if (! ( objreftype.equals(curr) ||
objreftype.subclassOf(curr) ) ) {
constraintViolated(o,
"The referenced field has the ACC_PROTECTED modifier, and it's a member of the current class or"+
" a superclass of the current class. However, the referenced object type '"+stack().peek()+
"' is not the current class or a subclass of the current class.");
}
}
}
if (f.isStatic()) {
constraintViolated(o, "Referenced field '"+f+"' is static which it shouldn't be.");
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitPUTSTATIC(final PUTSTATIC o) {
try {
final String field_name = o.getFieldName(cpg);
final JavaClass jc = Repository.lookupClass(getObjectType(o).getClassName());
final Field[] fields = jc.getFields();
Field f = null;
for (final Field field : fields) {
if (field.getName().equals(field_name)) {
final Type f_type = Type.getType(field.getSignature());
final Type o_type = o.getType(cpg);
if (f_type.equals(o_type)) {
f = field;
break;
}
}
}
if (f == null) {
throw new AssertionViolatedException("Field '" + field_name + "' not found in " + jc.getClassName());
}
final Type value = stack().peek();
final Type t = Type.getType(f.getSignature());
Type shouldbe = t;
if (shouldbe == Type.BOOLEAN ||
shouldbe == Type.BYTE ||
shouldbe == Type.CHAR ||
shouldbe == Type.SHORT) {
shouldbe = Type.INT;
}
if (t instanceof ReferenceType) {
ReferenceType rvalue = null;
if (value instanceof ReferenceType) {
rvalue = (ReferenceType) value;
referenceTypeIsInitialized(o, rvalue);
}
else{
constraintViolated(o, "The stack top type '"+value+"' is not of a reference type as expected.");
}
if (!(rvalue.isAssignmentCompatibleWith(shouldbe))) {
constraintViolated(o, "The stack top type '"+value+"' is not assignment compatible with '"+shouldbe+"'.");
}
}
else{
if (shouldbe != value) {
constraintViolated(o, "The stack top type '"+value+"' is not of type '"+shouldbe+"' as expected.");
}
}
} catch (final ClassNotFoundException e) {
throw new AssertionViolatedException("Missing class: " + e, e);
}
}
@Override
public void visitRET(final RET o) {
if (! (locals().get(o.getIndex()) instanceof ReturnaddressType)) {
constraintViolated(o, "Expecting a ReturnaddressType in local variable "+o.getIndex()+".");
}
if (locals().get(o.getIndex()) == ReturnaddressType.NO_TARGET) {
throw new AssertionViolatedException("Oops: RET expecting a target!");
}
}
@Override
public void visitRETURN(final RETURN o) {
if (mg.getName().equals(Const.CONSTRUCTOR_NAME)) {
if ((Frame.getThis() != null) && (!(mg.getClassName().equals(Type.OBJECT.getClassName()))) ) {
constraintViolated(o, "Leaving a constructor that itself did not call a constructor.");
}
}
}
@Override
public void visitSALOAD(final SALOAD o) {
indexOfInt(o, stack().peek());
if (stack().peek(1) == Type.NULL) {
return;
}
if (! (stack().peek(1) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-top must be of type short[] but is '"+stack().peek(1)+"'.");
}
final Type t = ((ArrayType) (stack().peek(1))).getBasicType();
if (t != Type.SHORT) {
constraintViolated(o, "Stack next-to-top must be of type short[] but is '"+stack().peek(1)+"'.");
}
}
@Override
public void visitSASTORE(final SASTORE o) {
if (stack().peek() != Type.INT) {
constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
}
indexOfInt(o, stack().peek(1));
if (stack().peek(2) == Type.NULL) {
return;
}
if (! (stack().peek(2) instanceof ArrayType)) {
constraintViolated(o, "Stack next-to-next-to-top must be of type short[] but is '"+stack().peek(2)+"'.");
}
final Type t = ((ArrayType) (stack().peek(2))).getBasicType();
if (t != Type.SHORT) {
constraintViolated(o, "Stack next-to-next-to-top must be of type short[] but is '"+stack().peek(2)+"'.");
}
}
@Override
public void visitSIPUSH(final SIPUSH o) {
}
@Override
public void visitSWAP(final SWAP o) {
if (stack().peek().getSize() != 1) {
constraintViolated(o, "The value at the stack top is not of size '1', but of size '"+stack().peek().getSize()+"'.");
}
if (stack().peek(1).getSize() != 1) {
constraintViolated(o,
"The value at the stack next-to-top is not of size '1', but of size '"+stack().peek(1).getSize()+"'.");
}
}
@Override
public void visitTABLESWITCH(final TABLESWITCH o) {
indexOfInt(o, stack().peek());
}
}