Search in sources :

Example 1 with Expression

use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.

the class SymbolicObserver method readStaticField.

private ReferenceExpressionPair readStaticField(Field field) {
    String owner = field.getDeclaringClass().getName().replace('.', '/');
    String name = field.getName();
    Class<?> fieldClazz = field.getType();
    try {
        if (fieldClazz.equals(int.class)) {
            int conc_value = field.getInt(null);
            IntegerValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
            return new ReferenceExpressionPair(newIntegerRef, expr);
        } else if (fieldClazz.equals(char.class)) {
            char conc_value = field.getChar(null);
            IntegerValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
            return new ReferenceExpressionPair(newCharacterRef, expr);
        } else if (fieldClazz.equals(long.class)) {
            long conc_value = field.getLong(null);
            IntegerValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newLongRef = newLongReference(conc_value, expr);
            return new ReferenceExpressionPair(newLongRef, expr);
        } else if (fieldClazz.equals(short.class)) {
            short conc_value = field.getShort(null);
            IntegerValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newShortRef = newShortReference(conc_value, expr);
            return new ReferenceExpressionPair(newShortRef, expr);
        } else if (fieldClazz.equals(byte.class)) {
            byte conc_value = field.getByte(null);
            IntegerValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newByteRef = newByteReference(conc_value, expr);
            return new ReferenceExpressionPair(newByteRef, expr);
        } else if (fieldClazz.equals(boolean.class)) {
            boolean conc_value = field.getBoolean(null);
            IntegerValue expr = env.heap.getStaticField(owner, name, conc_value ? 1 : 0);
            ReferenceConstant newBooleanRef = newBooleanReference(conc_value, expr);
            return new ReferenceExpressionPair(newBooleanRef, expr);
        } else if (fieldClazz.equals(float.class)) {
            float conc_value = field.getFloat(null);
            RealValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
            return new ReferenceExpressionPair(newFloatRef, expr);
        } else if (fieldClazz.equals(double.class)) {
            double conc_value = field.getDouble(null);
            RealValue expr = env.heap.getStaticField(owner, name, conc_value);
            ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
            return new ReferenceExpressionPair(newDoubleRef, expr);
        } else {
            Object conc_value = field.get(null);
            if (conc_value instanceof String) {
                String string = (String) conc_value;
                StringValue expr = env.heap.getStaticField(owner, name, string);
                ReferenceConstant newStringRef = newStringReference(string, expr);
                return new ReferenceExpressionPair(newStringRef, expr);
            } else {
                ReferenceExpression ref = env.heap.getReference(conc_value);
                if (conc_value != null && isWrapper(conc_value)) {
                    ReferenceConstant nonNullRef = (ReferenceConstant) ref;
                    Expression<?> expr = findOrCreate(conc_value, nonNullRef);
                    return new ReferenceExpressionPair(ref, expr);
                } else {
                    return new ReferenceExpressionPair(ref, null);
                }
            }
        }
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 2 with Expression

use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.

the class SymbolicObserver method pushParameterList.

private void pushParameterList(List<VariableReference> parameters, Scope scope, String desc) {
    Type[] argTypes = Type.getArgumentTypes(desc);
    for (int i = 0; i < parameters.size(); i++) {
        VariableReference varRef = parameters.get(i);
        Type argType = argTypes[i];
        ReferenceExpressionPair readResult = this.read(varRef, scope);
        Expression<?> symb_expr = readResult.getExpression();
        ReferenceExpression symb_ref = readResult.getReference();
        if (isValue(argType)) {
            if (symb_expr instanceof RealValue) {
                RealValue realExpr = (RealValue) symb_expr;
                if (isFp32(argType)) {
                    env.topFrame().operandStack.pushFp32(realExpr);
                } else if (isFp64(argType)) {
                    env.topFrame().operandStack.pushFp64(realExpr);
                } else if (isBv32(argType)) {
                    int concV = realExpr.getConcreteValue().intValue();
                    RealToIntegerCast castExpr = new RealToIntegerCast(realExpr, (long) concV);
                    env.topFrame().operandStack.pushBv32(castExpr);
                } else if (isBv64(argType)) {
                    long concV = realExpr.getConcreteValue().longValue();
                    RealToIntegerCast castExpr = new RealToIntegerCast(realExpr, concV);
                    env.topFrame().operandStack.pushBv64(castExpr);
                } else {
                /* unreachable code */
                }
            } else if (symb_expr instanceof IntegerValue) {
                IntegerValue integerExpr = (IntegerValue) symb_expr;
                if (isBv32(argType)) {
                    env.topFrame().operandStack.pushBv32(integerExpr);
                } else if (isBv64(argType)) {
                    env.topFrame().operandStack.pushBv64(integerExpr);
                } else if (isFp32(argType)) {
                    float concV = integerExpr.getConcreteValue().floatValue();
                    IntegerToRealCast castExpr = new IntegerToRealCast(integerExpr, (double) concV);
                    env.topFrame().operandStack.pushFp32(castExpr);
                } else if (isFp64(argType)) {
                    double concV = integerExpr.getConcreteValue().doubleValue();
                    IntegerToRealCast castExpr = new IntegerToRealCast(integerExpr, concV);
                    env.topFrame().operandStack.pushFp64(castExpr);
                } else {
                /* unreachable code */
                }
            } else {
                if (symb_ref.getConcreteValue() == null) {
                    // although this will lead in the JVM to a NPE, we push
                    // a dummy
                    // value to prevent the DSE VM from crashing
                    pushDummyValue(argType);
                } else {
                    // auto unboxing reference
                    ReferenceConstant non_null_symb_ref = (ReferenceConstant) symb_ref;
                    Object conc_object = scope.getObject(varRef);
                    Expression<?> unboxed_expr = unboxReference(argType, conc_object, non_null_symb_ref);
                    pushValue(argType, unboxed_expr);
                }
            }
        } else {
            ReferenceExpression ref = readResult.getReference();
            env.topFrame().operandStack.pushRef(ref);
        }
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) VariableReference(org.evosuite.testcase.variable.VariableReference) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) RealToIntegerCast(org.evosuite.symbolic.expr.bv.RealToIntegerCast) ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) Type(org.objectweb.asm.Type) IntegerToRealCast(org.evosuite.symbolic.expr.fp.IntegerToRealCast) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression)

Example 3 with Expression

use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(FunctionalMockStatement statement, Scope scope) {
    Type returnType = Type.getType(statement.getReturnClass());
    VariableReference varRef = statement.getReturnValue();
    String varName = varRef.getName();
    try {
        if (varRef.getType().equals(void.class)) {
        } else if (returnType.equals(Type.INT_TYPE) || returnType.equals(Type.BOOLEAN_TYPE) || returnType.equals(Type.DOUBLE_TYPE) || returnType.equals(Type.FLOAT_TYPE) || returnType.equals(Type.LONG_TYPE) || returnType.equals(Type.SHORT_TYPE) || returnType.equals(Type.BYTE_TYPE) || returnType.equals(Type.CHAR_TYPE)) {
            throw new EvosuiteError("mocking of primitive types is not supported!");
        } else {
            Object res = varRef.getObject(scope);
            ReferenceExpression ref = env.heap.getReference(res);
            if (res != null && res instanceof String) {
                String string = (String) res;
                ReferenceConstant newStringRef = (ReferenceConstant) env.heap.getReference(string);
                StringValue str_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, string, newStringRef, string);
                symb_references.put(varName, newStringRef);
                symb_expressions.put(varName, str_expr);
            } else {
                symb_references.put(varName, ref);
                if (res != null && isWrapper(res)) {
                    ReferenceConstant nonNullRef = (ReferenceConstant) ref;
                    Expression<?> expr = findOrCreate(res, nonNullRef);
                    symb_expressions.put(varName, expr);
                }
            }
        }
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) Type(org.objectweb.asm.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 4 with Expression

use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.

the class SymbolicObserver method readArray.

private ReferenceExpressionPair readArray(ArrayIndex rhs, Scope scope) {
    ArrayReference arrayReference = rhs.getArray();
    ReferenceConstant symb_array = (ReferenceConstant) symb_references.get(arrayReference.getName());
    int conc_index = rhs.getArrayIndex();
    Class<?> componentClass = arrayReference.getComponentClass();
    try {
        Object conc_array = arrayReference.getObject(scope);
        if (componentClass.equals(int.class)) {
            int conc_value = Array.getInt(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
            return new ReferenceExpressionPair(newIntegerRef, expr);
        } else if (componentClass.equals(char.class)) {
            char conc_value = Array.getChar(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
            return new ReferenceExpressionPair(newCharacterRef, expr);
        } else if (componentClass.equals(boolean.class)) {
            boolean conc_value = Array.getBoolean(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value ? 1 : 0);
            ReferenceConstant newBooleanRef = newBooleanReference(conc_value, expr);
            return new ReferenceExpressionPair(newBooleanRef, expr);
        } else if (componentClass.equals(byte.class)) {
            byte conc_value = Array.getByte(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newByteRef = newByteReference(conc_value, expr);
            return new ReferenceExpressionPair(newByteRef, expr);
        } else if (componentClass.equals(short.class)) {
            short conc_value = Array.getShort(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newShortRef = newShortReference(conc_value, expr);
            return new ReferenceExpressionPair(newShortRef, expr);
        } else if (componentClass.equals(long.class)) {
            long conc_value = Array.getLong(conc_array, conc_index);
            IntegerValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newLongRef = newLongReference(conc_value, expr);
            return new ReferenceExpressionPair(newLongRef, expr);
        } else if (componentClass.equals(float.class)) {
            float conc_value = Array.getFloat(conc_array, conc_index);
            RealValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
            return new ReferenceExpressionPair(newFloatRef, expr);
        } else if (componentClass.equals(double.class)) {
            double conc_value = Array.getDouble(conc_array, conc_index);
            RealValue expr = env.heap.array_load(symb_array, conc_index, conc_value);
            ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
            return new ReferenceExpressionPair(newDoubleRef, expr);
        } else {
            Object conc_value = Array.get(conc_array, conc_index);
            if (conc_value instanceof String) {
                StringValue expr = env.heap.array_load(symb_array, conc_index, (String) conc_value);
                ReferenceConstant newStringRef = newStringReference((String) conc_value, expr);
                return new ReferenceExpressionPair(newStringRef, expr);
            } else {
                ReferenceExpression ref = env.heap.getReference(conc_value);
                if (conc_value != null && isWrapper(conc_value)) {
                    ReferenceConstant nonNullRef = (ReferenceConstant) ref;
                    Expression<?> expr = findOrCreate(conc_value, nonNullRef);
                    return new ReferenceExpressionPair(ref, expr);
                } else {
                    return new ReferenceExpressionPair(ref, null);
                }
            }
        }
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) ArrayReference(org.evosuite.testcase.variable.ArrayReference) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 5 with Expression

use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.

the class SymbolicHeap method getField.

/**
 * Returns a stored symbolic expression for an int field or created one
 *
 * @param owner
 * @param name
 * @param conc_receiver
 * @param symb_receiver
 * @param conc_value
 * @return
 */
public IntegerValue getField(String owner, String name, Object conc_receiver, ReferenceExpression symb_receiver, long conc_value) {
    Map<ReferenceExpression, Expression<?>> symb_field = getOrCreateSymbolicField(owner, name);
    IntegerValue symb_value = (IntegerValue) symb_field.get(symb_receiver);
    if (symb_value == null || ((Long) symb_value.getConcreteValue()).longValue() != conc_value) {
        symb_value = ExpressionFactory.buildNewIntegerConstant(conc_value);
        symb_field.remove(symb_receiver);
    }
    return symb_value;
}
Also used : Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression)

Aggregations

Expression (org.evosuite.symbolic.expr.Expression)24 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)13 StringValue (org.evosuite.symbolic.expr.str.StringValue)10 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)9 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)8 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)8 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)8 ArrayList (java.util.ArrayList)6 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)6 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)6 RealValue (org.evosuite.symbolic.expr.fp.RealValue)6 StringMultipleExpression (org.evosuite.symbolic.expr.str.StringMultipleExpression)6 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)5 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)5 LinkedList (java.util.LinkedList)4 Constraint (org.evosuite.symbolic.expr.Constraint)4 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)4 Operator (org.evosuite.symbolic.expr.Operator)4 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)4 IntegerUnaryExpression (org.evosuite.symbolic.expr.bv.IntegerUnaryExpression)4