Search in sources :

Example 36 with IntegerConstant

use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.

the class JumpVM method LOOKUPSWITCH.

/**
 * <b>switch</b> statement whose cases may not be numbered consecutively.
 * I.e., there may be holes (missing targets) between the lowest and highest
 * target.
 *
 * <p>
 * Very similar to {@link #TABLESWITCH}. The main difference is that here we
 * are given a list of explicit goals. Tableswitch defines its goals
 * implicitly, between min and max.
 *
 * http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
 * doc8.html#lookupswitch
 */
@Override
public void LOOKUPSWITCH(String className, String methName, int branchIndex, int goalConcrete, int[] targetsConcrete) {
    // TODO: target array remains constant. Do we really need to create and
    // pass
    // this array every time as a paremeter?
    final IntegerValue goal = env.topFrame().operandStack.popBv32();
    Vector<IntegerConstraint> constraints = new Vector<IntegerConstraint>();
    for (int targetConcrete : targetsConcrete) {
        IntegerConstant integerConstant = ExpressionFactory.buildNewIntegerConstant(targetConcrete);
        IntegerConstraint constraint;
        if (goalConcrete == targetConcrete) {
            constraint = ConstraintFactory.eq(goal, integerConstant);
            constraints.add(constraint);
            break;
        } else {
            constraint = ConstraintFactory.neq(goal, integerConstant);
            constraints.add(constraint);
        }
    }
    for (int i = 0; i < constraints.size() - 1; i++) {
        IntegerConstraint cnstrnt = constraints.get(i);
        if (cnstrnt.getLeftOperand().containsSymbolicVariable() || cnstrnt.getRightOperand().containsSymbolicVariable())
            pc.addSupportingConstraint(cnstrnt);
    }
    // add branch condition iif local constraint is concrete
    IntegerConstraint cnstr = constraints.get(constraints.size() - 1);
    if (cnstr.getLeftOperand().containsSymbolicVariable() || cnstr.getRightOperand().containsSymbolicVariable()) {
        pc.addBranchCondition(className, methName, branchIndex, cnstr);
    }
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) Vector(java.util.Vector) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 37 with IntegerConstant

use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.

the class LocalsVM method BIPUSH.

/**
 * Bytecode instruction stream: ... ,0x10, byte, ...
 *
 * <p>
 * Push the byte value that immediately follows this bytecode instruction
 * (0x10) in the bytecode stream. The byte value is sign-extended to an int.
 *
 * http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
 * doc1.html#bipush
 */
@Override
public void BIPUSH(int value) {
    IntegerConstant intConstant = ExpressionFactory.buildNewIntegerConstant(value);
    env.topFrame().operandStack.pushBv32(intConstant);
}
Also used : IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 38 with IntegerConstant

use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(ArrayStatement s, Scope scope) {
    try {
        ArrayReference arrayRef = (ArrayReference) s.getReturnValue();
        Object conc_array;
        conc_array = arrayRef.getObject(scope);
        if (arrayRef.getArrayDimensions() == 1) {
            int length = arrayRef.getArrayLength();
            IntegerConstant lengthExpr = ExpressionFactory.buildNewIntegerConstant(length);
            Class<?> component_class = arrayRef.getComponentClass();
            env.topFrame().operandStack.pushBv32(lengthExpr);
            if (component_class.equals(int.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_INT);
            } else if (component_class.equals(char.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_CHAR);
            } else if (component_class.equals(short.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_SHORT);
            } else if (component_class.equals(byte.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_BYTE);
            } else if (component_class.equals(float.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_FLOAT);
            } else if (component_class.equals(long.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_LONG);
            } else if (component_class.equals(boolean.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_BOOLEAN);
            } else if (component_class.equals(double.class)) {
                VM.NEWARRAY(length, COMPONENT_TYPE_DOUBLE);
            } else {
                // push arguments
                String componentTypeName = component_class.getName().replace('.', '/');
                VM.ANEWARRAY(length, componentTypeName);
            }
        } else {
            // push dimensions
            List<Integer> dimensions = arrayRef.getLengths();
            for (int i = 0; i < arrayRef.getArrayDimensions(); i++) {
                int length = dimensions.get(i);
                IntegerConstant lengthExpr = ExpressionFactory.buildNewIntegerConstant(length);
                env.topFrame().operandStack.pushBv32(lengthExpr);
            }
            String arrayTypeDesc = Type.getDescriptor(conc_array.getClass());
            VM.MULTIANEWARRAY(arrayTypeDesc, arrayRef.getArrayDimensions());
        }
        ReferenceConstant symb_array = (ReferenceConstant) env.topFrame().operandStack.popRef();
        env.heap.initializeReference(conc_array, symb_array);
        String varRef_name = arrayRef.getName();
        symb_references.put(varRef_name, symb_array);
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ArrayReference(org.evosuite.testcase.variable.ArrayReference) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 39 with IntegerConstant

use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.

the class DistanceCalculator method getDistanceIndexOfCEqualsK.

private static long getDistanceIndexOfCEqualsK(IntegerConstraint n, long leftVal, long rightVal) {
    if (n.getLeftOperand() instanceof StringBinaryToIntegerExpression && n.getComparator() == Comparator.EQ && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringBinaryToIntegerExpression left_string_expr = (StringBinaryToIntegerExpression) n.getLeftOperand();
        if (left_string_expr.getOperator() == Operator.INDEXOFC) {
            Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
            Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
            Expression<?> theSymbolicIndex = right_constant;
            // check theString.lenght>0
            ExpressionExecutor exprExecutor = new ExpressionExecutor();
            String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
            Long theConcreteIndex = (Long) theSymbolicIndex.accept(exprExecutor, null);
            if (theConcreteIndex > theConcreteString.length() - 1) {
                // there is no char at the index to modify
                return Long.MAX_VALUE;
            } else if (theConcreteIndex != -1) {
                int theIndex = theConcreteIndex.intValue();
                char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
                char theCurrentChar = theConcreteString.charAt(theIndex);
                return Math.abs(theCurrentChar - theConcreteChar);
            }
        }
    }
    return -1;
}
Also used : StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 40 with IntegerConstant

use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.

the class DistanceCalculator method getDistanceIndexOfCFound.

private static long getDistanceIndexOfCFound(IntegerConstraint n, long leftVal, long rightVal) {
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    if (n.getLeftOperand() instanceof StringBinaryToIntegerExpression && n.getComparator() == Comparator.NE && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringBinaryToIntegerExpression left_string_expr = (StringBinaryToIntegerExpression) n.getLeftOperand();
        if (left_string_expr.getOperator() == Operator.INDEXOFC && right_constant.getConcreteValue() == -1L) {
            Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
            Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
            // check theString.lenght>0
            String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
            if (theConcreteString.length() == 0) {
                // no char can be modified to satisfy the constraint
                return Long.MAX_VALUE;
            } else {
                char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
                char[] charArray = theConcreteString.toCharArray();
                int min_distance_to_char = Integer.MAX_VALUE;
                for (char c : charArray) {
                    if (Math.abs(c - theConcreteChar) < min_distance_to_char) {
                        min_distance_to_char = Math.abs(c - theConcreteChar);
                    }
                }
                return min_distance_to_char;
            }
        }
    }
    return -1;
}
Also used : StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Aggregations

IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)65 Constraint (org.evosuite.symbolic.expr.Constraint)42 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)42 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)36 Test (org.junit.Test)36 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)35 ArrayList (java.util.ArrayList)34 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)29 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)26 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)23 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)17 SolverResult (org.evosuite.symbolic.solver.SolverResult)14 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)13 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)12 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)7 Expression (org.evosuite.symbolic.expr.Expression)6 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)5 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)5 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)5 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)3