Search in sources :

Example 26 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException 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 27 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class SymbolicObserver method readInstanceField.

private ReferenceExpressionPair readInstanceField(VariableReference source, Field field, Scope scope) {
    String owner = field.getDeclaringClass().getName().replace('.', '/');
    String name = field.getName();
    Class<?> fieldClazz = field.getType();
    String source_name = source.getName();
    ReferenceConstant symb_receiver = (ReferenceConstant) symb_references.get(source_name);
    try {
        Object conc_receiver = source.getObject(scope);
        if (fieldClazz.equals(int.class)) {
            int conc_value = field.getInt(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
            return new ReferenceExpressionPair(newIntegerRef, expr);
        } else if (fieldClazz.equals(char.class)) {
            char conc_value = field.getChar(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
            return new ReferenceExpressionPair(newCharacterRef, expr);
        } else if (fieldClazz.equals(long.class)) {
            long conc_value = field.getLong(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newLongRef = newLongReference(conc_value, expr);
            return new ReferenceExpressionPair(newLongRef, expr);
        } else if (fieldClazz.equals(short.class)) {
            short conc_value = field.getShort(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newShortRef = newShortReference(conc_value, expr);
            return new ReferenceExpressionPair(newShortRef, expr);
        } else if (fieldClazz.equals(byte.class)) {
            byte conc_value = field.getByte(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newByteRef = newByteReference(conc_value, expr);
            return new ReferenceExpressionPair(newByteRef, expr);
        } else if (fieldClazz.equals(boolean.class)) {
            boolean conc_value = field.getBoolean(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, 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(conc_receiver);
            RealValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
            return new ReferenceExpressionPair(newFloatRef, expr);
        } else if (fieldClazz.equals(double.class)) {
            double conc_value = field.getDouble(conc_receiver);
            RealValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
            return new ReferenceExpressionPair(newDoubleRef, expr);
        } else {
            Object conc_value = field.get(conc_receiver);
            if (conc_value instanceof String) {
                String string = (String) conc_value;
                StringValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, 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);
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) 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 28 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(LongPrimitiveStatement statement, Scope scope) {
    long valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Long.MIN_VALUE, Long.MAX_VALUE);
    symb_expressions.put(varRefName, integerVariable);
    Long long_instance;
    try {
        long_instance = (Long) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant longRef = newLongReference(long_instance, integerVariable);
    symb_references.put(varRefName, longRef);
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 29 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class SymbolicObserver method before.

private void before(MethodStatement statement, Scope scope) {
    Method method = statement.getMethod().getMethod();
    String owner = method.getDeclaringClass().getName().replace('.', '/');
    String name = method.getName();
    String desc = Type.getMethodDescriptor(method);
    boolean needThis = statement.getCallee() != null;
    if (needThis) {
        VariableReference callee = statement.getCallee();
        ReferenceExpressionPair refExprPair = read(callee, scope);
        ReferenceExpression ref = refExprPair.getReference();
        this.env.topFrame().operandStack.pushRef(ref);
    }
    List<VariableReference> parameters = statement.getParameterReferences();
    pushParameterList(parameters, scope, desc);
    if (needThis) {
        VariableReference callee = statement.getCallee();
        Object receiver;
        try {
            receiver = callee.getObject(scope);
        } catch (CodeUnderTestException e) {
            throw new RuntimeException(e);
        }
        Class<?> ownerClass = env.ensurePrepared(owner);
        if (ownerClass.isInterface()) {
            VM.INVOKEINTERFACE(receiver, owner, name, desc);
        } else {
            VM.INVOKEVIRTUAL(receiver, owner, name, desc);
        }
    } else {
        VM.INVOKESTATIC(owner, name, desc);
    }
    call_vm_caller_stack_params(needThis, parameters, scope, desc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) Method(java.lang.reflect.Method) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 30 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(CharPrimitiveStatement statement, Scope scope) {
    char valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Character.MIN_VALUE, Character.MAX_VALUE);
    symb_expressions.put(varRefName, integerVariable);
    Character character0;
    try {
        character0 = (Character) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant charRef = newCharacterReference(character0, integerVariable);
    symb_references.put(varRefName, charRef);
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Aggregations

CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)33 VariableReference (org.evosuite.testcase.variable.VariableReference)25 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)16 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)14 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)6 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 Type (org.objectweb.asm.Type)5 StringValue (org.evosuite.symbolic.expr.str.StringValue)4 UncompilableCodeException (org.evosuite.testcase.execution.UncompilableCodeException)4 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 LinkedHashSet (java.util.LinkedHashSet)3 Expression (org.evosuite.symbolic.expr.Expression)3 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)3 RealValue (org.evosuite.symbolic.expr.fp.RealValue)3 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)3 ArrayReference (org.evosuite.testcase.variable.ArrayReference)3 ConstantValue (org.evosuite.testcase.variable.ConstantValue)3 Field (java.lang.reflect.Field)2