Java Code Examples for org.apache.bcel.verifier.VerificationResult#VERIFIED_OK

The following examples show how to use org.apache.bcel.verifier.VerificationResult#VERIFIED_OK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures the general preconditions of a FieldInstruction instance.
 */
 @Override
public void visitFieldInstruction(final FieldInstruction o) {
     // visitLoadClass(o) has been called before: Every FieldOrMethod
     // implements LoadClass.
     // visitCPInstruction(o) has been called before.
    // A FieldInstruction may be: GETFIELD, GETSTATIC, PUTFIELD, PUTSTATIC
        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+"'.");
        }
        // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
        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+"'.");
            }
        }
 }
 
Example 2
Source File: Pass3aVerifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Assures the generic preconditions of a LoadClass instance.
 * The referenced class is loaded and pass2-verified.
 */
@Override
public void visitLoadClass(final LoadClass loadClass) {
    final ObjectType t = loadClass.getLoadClassType(constantPoolGen);
    if (t != null) {// null means "no class is loaded"
        final Verifier v = VerifierFactory.getVerifier(t.getClassName());
        final VerificationResult vr = v.doPass1();
        if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
            constraintViolated((Instruction) loadClass,
                "Class '"+loadClass.getLoadClassType(constantPoolGen).getClassName()+"' is referenced, but cannot be loaded: '"+vr+"'.");
        }
    }
}
 
Example 3
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Assures the generic preconditions of a LoadClass instance.
 * The referenced class is loaded and pass2-verified.
 */
@Override
public void visitLoadClass(final LoadClass o) {
    final ObjectType t = o.getLoadClassType(cpg);
    if (t != null) {// null means "no class is loaded"
        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+"'.");
        }
    }
}
 
Example 4
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
@Override
public void visitINVOKEINTERFACE(final INVOKEINTERFACE o) {
    // Method is not native, otherwise pass 3 would not happen.

    final int count = o.getCount();
    if (count == 0) {
        constraintViolated(o, "The 'count' argument must not be 0.");
    }
    // It is a ConstantInterfaceMethodref, Pass 3a made it sure.
    // TODO: Do we want to do anything with it?
    //ConstantInterfaceMethodref cimr = (ConstantInterfaceMethodref) (cpg.getConstant(o.getIndex()));

    // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).

    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 );    // 0 to nargs-1
        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;
                //ReferenceType rFromDesc = (ReferenceType) fromDesc;
                // TODO: This can only be checked when using Staerk-et-al's "set of object types"
                // instead of a "wider cast object type" created during verification.
                //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)) { // could be a ReturnaddressType
            constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'.");
        }
        else{
            objref = GENERIC_ARRAY;
        }
    }

    // String objref_classname = ((ObjectType) objref).getClassName();
    // String theInterface = o.getClassName(cpg);
    // TODO: This can only be checked if we're using Staerk-et-al's "set of object types"
    //       instead of "wider cast object types" generated during verification.
    //if ( ! Repository.implementationOf(objref_classname, theInterface) ) {
    //    constraintViolated(o, "The 'objref' item '"+objref+"' does not implement '"+theInterface+"' as expected.");
    //}

    int counted_count = 1; // 1 for the objectref
    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+"'.");
    }
}
 
Example 5
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
@Override
public void visitINVOKESPECIAL(final INVOKESPECIAL o) {
    try {
    // Don't init an object twice.
    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.");
    }

    // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).

    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 );    // 0 to nargs-1
        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;
                // TODO: This can only be checked using Staerk-et-al's "set of object types", not
                // using a "wider cast object type".
                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)) { // could be a ReturnaddressType
                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) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}
 
Example 6
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
@Override
public void visitINVOKESTATIC(final INVOKESTATIC o) {
    try {
    // Method is not native, otherwise pass 3 would not happen.

    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 );    // 0 to nargs-1
        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;
                // TODO: This check can possibly only be done using Staerk-et-al's "set of object types"
                // instead of a "wider cast object type" created during verification.
                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) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}
 
Example 7
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
@Override
public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) {
    try {
    // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).

    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 );    // 0 to nargs-1
        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;
                // TODO: This can possibly only be checked when using Staerk-et-al's "set of object types" instead
                // of a single "wider cast object type" created during verification.
                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)) { // could be a ReturnaddressType
            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) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}