Search in sources :

Example 1 with SmtCheckSatQuery

use of org.evosuite.symbolic.solver.smt.SmtCheckSatQuery in project evosuite by EvoSuite.

the class Z3Str2Solver method solve.

@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, IOException, SolverParseException, SolverEmptyQueryException, SolverErrorException {
    SmtCheckSatQuery smtCheckSatQuery = buildSmtQuerty(constraints);
    if (smtCheckSatQuery.getConstantDeclarations().isEmpty()) {
        logger.debug("Z3-str2 input has no variables");
        throw new SolverEmptyQueryException("Z3-str2 input has no variables");
    }
    if (smtCheckSatQuery.getAssertions().isEmpty()) {
        Map<String, Object> emptySolution = new HashMap<String, Object>();
        SolverResult emptySAT = SolverResult.newSAT(emptySolution);
        return emptySAT;
    }
    Z3Str2QueryPrinter printer = new Z3Str2QueryPrinter();
    String smtQueryStr = printer.print(smtCheckSatQuery);
    logger.debug("Z3-str2 input:");
    logger.debug(smtQueryStr);
    int timeout = (int) Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
    File tempDir = createNewTmpDir();
    String z3TempFileName = tempDir.getAbsolutePath() + File.separatorChar + EVOSUITE_Z3_STR_FILENAME;
    if (Properties.Z3_STR2_PATH == null) {
        String errMsg = "Property Z3_STR_PATH should be setted in order to use the Z3StrSolver!";
        logger.error(errMsg);
        throw new IllegalStateException(errMsg);
    }
    try {
        FileIOUtils.writeFile(smtQueryStr, z3TempFileName);
        String z3Cmd = Properties.Z3_STR2_PATH + " -f " + z3TempFileName;
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        launchNewProcess(z3Cmd, smtQueryStr, timeout, stdout);
        String z3str2ResultStr = stdout.toString("UTF-8");
        Z3Str2ResultParser parser = new Z3Str2ResultParser();
        Set<Variable<?>> variables = getVariables(constraints);
        Map<String, Object> initialValues = getConcreteValues(variables);
        SolverResult solverResult;
        if (addMissingVariables()) {
            solverResult = parser.parse(z3str2ResultStr, initialValues);
        } else {
            solverResult = parser.parse(z3str2ResultStr);
        }
        if (solverResult.isSAT()) {
            // check if solution is correct, otherwise return UNSAT
            boolean check = checkSAT(constraints, solverResult);
            if (!check) {
                logger.debug("Z3-str2 solution does not solve the constraint system!");
                SolverResult unsatResult = SolverResult.newUNSAT();
                return unsatResult;
            }
        }
        return solverResult;
    } catch (UnsupportedEncodingException e) {
        throw new EvosuiteError("UTF-8 should not cause this exception!");
    } finally {
        File tempFile = new File(z3TempFileName);
        if (tempFile.exists()) {
            tempFile.delete();
        }
    }
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) SmtIntVariable(org.evosuite.symbolic.solver.smt.SmtIntVariable) SmtRealVariable(org.evosuite.symbolic.solver.smt.SmtRealVariable) SmtVariable(org.evosuite.symbolic.solver.smt.SmtVariable) SmtStringVariable(org.evosuite.symbolic.solver.smt.SmtStringVariable) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) HashMap(java.util.HashMap) SolverResult(org.evosuite.symbolic.solver.SolverResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Constraint(org.evosuite.symbolic.expr.Constraint) SolverEmptyQueryException(org.evosuite.symbolic.solver.SolverEmptyQueryException) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) File(java.io.File)

Example 2 with SmtCheckSatQuery

use of org.evosuite.symbolic.solver.smt.SmtCheckSatQuery in project evosuite by EvoSuite.

the class CVC4Solver method buildSmtCheckSatQuery.

private static SmtCheckSatQuery buildSmtCheckSatQuery(Collection<Constraint<?>> constraints) {
    ConstraintToCVC4Visitor v = new ConstraintToCVC4Visitor(true);
    SmtVariableCollector varCollector = new SmtVariableCollector();
    SmtOperatorCollector funCollector = new SmtOperatorCollector();
    List<SmtAssertion> smtAssertions = new LinkedList<SmtAssertion>();
    for (Constraint<?> c : constraints) {
        SmtExpr smtExpr = c.accept(v, null);
        if (smtExpr != null) {
            SmtAssertion smtAssertion = new SmtAssertion(smtExpr);
            smtAssertions.add(smtAssertion);
            smtExpr.accept(varCollector, null);
            smtExpr.accept(funCollector, null);
        }
    }
    Set<SmtVariable> variables = varCollector.getSmtVariables();
    if (variables.isEmpty()) {
        // no variables, constraint system is trivial
        return null;
    }
    List<SmtFunctionDefinition> functionDefinitions = new LinkedList<SmtFunctionDefinition>();
    final boolean addCharToInt = funCollector.getOperators().contains(Operator.CHAR_TO_INT);
    if (addCharToInt) {
        String charToIntFunction = buildCharToIntFunction();
        SmtFunctionDefinition funcDefinition = new SmtFunctionDefinition(charToIntFunction);
        functionDefinitions.add(funcDefinition);
    }
    final boolean addIntToChar = funCollector.getOperators().contains(Operator.INT_TO_CHAR);
    if (addIntToChar) {
        String intToCharFunction = buildIntToCharFunction();
        SmtFunctionDefinition funcDefinition = new SmtFunctionDefinition(intToCharFunction);
        functionDefinitions.add(funcDefinition);
    }
    List<SmtFunctionDeclaration> functionDeclarations = new LinkedList<SmtFunctionDeclaration>();
    for (SmtVariable var : variables) {
        String varName = var.getName();
        if (var instanceof SmtIntVariable) {
            SmtFunctionDeclaration intVar = SmtExprBuilder.mkIntFunctionDeclaration(varName);
            functionDeclarations.add(intVar);
        } else if (var instanceof SmtRealVariable) {
            SmtFunctionDeclaration realVar = SmtExprBuilder.mkRealFunctionDeclaration(varName);
            functionDeclarations.add(realVar);
        } else if (var instanceof SmtStringVariable) {
            SmtFunctionDeclaration stringVar = SmtExprBuilder.mkStringFunctionDeclaration(varName);
            functionDeclarations.add(stringVar);
        } else {
            throw new RuntimeException("Unknown variable type " + var.getClass().getCanonicalName());
        }
    }
    SmtCheckSatQuery smtQuery = new SmtCheckSatQuery(new LinkedList<SmtConstantDeclaration>(), functionDeclarations, functionDefinitions, smtAssertions);
    return smtQuery;
}
Also used : SmtFunctionDefinition(org.evosuite.symbolic.solver.smt.SmtFunctionDefinition) SmtAssertion(org.evosuite.symbolic.solver.smt.SmtAssertion) SmtVariableCollector(org.evosuite.symbolic.solver.smt.SmtVariableCollector) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) SmtStringVariable(org.evosuite.symbolic.solver.smt.SmtStringVariable) SmtOperatorCollector(org.evosuite.symbolic.solver.smt.SmtOperatorCollector) SmtIntVariable(org.evosuite.symbolic.solver.smt.SmtIntVariable) SmtFunctionDeclaration(org.evosuite.symbolic.solver.smt.SmtFunctionDeclaration) SmtConstantDeclaration(org.evosuite.symbolic.solver.smt.SmtConstantDeclaration) LinkedList(java.util.LinkedList) SmtRealVariable(org.evosuite.symbolic.solver.smt.SmtRealVariable) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) SmtVariable(org.evosuite.symbolic.solver.smt.SmtVariable)

Example 3 with SmtCheckSatQuery

use of org.evosuite.symbolic.solver.smt.SmtCheckSatQuery in project evosuite by EvoSuite.

the class Z3Solver method solve.

@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, IOException, SolverParseException, SolverEmptyQueryException, SolverErrorException {
    long hard_timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
    Set<Variable<?>> variables = new HashSet<Variable<?>>();
    for (Constraint<?> c : constraints) {
        Set<Variable<?>> c_variables = c.getVariables();
        variables.addAll(c_variables);
    }
    SmtCheckSatQuery smtCheckSatQuery = buildSmtQuery(constraints, variables);
    if (smtCheckSatQuery.getConstantDeclarations().isEmpty()) {
        logger.debug("Z3 SMT query has no variables");
        throw new SolverEmptyQueryException("Z3 SMT query has no variables");
    }
    if (smtCheckSatQuery.getAssertions().isEmpty()) {
        Map<String, Object> emptySolution = new HashMap<String, Object>();
        SolverResult emptySAT = SolverResult.newSAT(emptySolution);
        return emptySAT;
    }
    Z3QueryPrinter printer = new Z3QueryPrinter();
    String smtQueryStr = printer.print(smtCheckSatQuery, hard_timeout);
    logger.debug("Z3 Query:");
    logger.debug(smtQueryStr);
    if (Properties.Z3_PATH == null) {
        String errMsg = "Property Z3_PATH should be setted in order to use the Z3 Solver!";
        logger.error(errMsg);
        throw new IllegalStateException(errMsg);
    }
    String z3Cmd = Properties.Z3_PATH + " -smt2 -in";
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    launchNewProcess(z3Cmd, smtQueryStr, (int) hard_timeout, stdout);
    String z3ResultStr = stdout.toString("UTF-8");
    Map<String, Object> initialValues = getConcreteValues(variables);
    Z3ResultParser resultParser;
    if (this.addMissingVariables()) {
        resultParser = new Z3ResultParser(initialValues);
    } else {
        resultParser = new Z3ResultParser();
    }
    SolverResult result = resultParser.parseResult(z3ResultStr);
    return result;
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) HashMap(java.util.HashMap) SolverResult(org.evosuite.symbolic.solver.SolverResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolverEmptyQueryException(org.evosuite.symbolic.solver.SolverEmptyQueryException) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) HashSet(java.util.HashSet)

Example 4 with SmtCheckSatQuery

use of org.evosuite.symbolic.solver.smt.SmtCheckSatQuery in project evosuite by EvoSuite.

the class Z3Solver method buildSmtQuery.

private static SmtCheckSatQuery buildSmtQuery(Collection<Constraint<?>> constraints, Set<Variable<?>> variables) {
    List<SmtConstantDeclaration> constantDeclarations = new LinkedList<SmtConstantDeclaration>();
    for (Variable<?> v : variables) {
        String varName = v.getName();
        if (v instanceof IntegerVariable) {
            SmtConstantDeclaration intVar = SmtExprBuilder.mkIntConstantDeclaration(varName);
            constantDeclarations.add(intVar);
        } else if (v instanceof RealVariable) {
            SmtConstantDeclaration realVar = SmtExprBuilder.mkRealConstantDeclaration(varName);
            constantDeclarations.add(realVar);
        } else if (v instanceof StringVariable) {
        // ignore string variables
        } else {
            throw new RuntimeException("Unknown variable type " + v.getClass().getCanonicalName());
        }
    }
    List<SmtAssertion> assertions = new LinkedList<SmtAssertion>();
    for (Constraint<?> c : constraints) {
        ConstraintToZ3Visitor v = new ConstraintToZ3Visitor();
        SmtExpr bool_expr = c.accept(v, null);
        if (bool_expr != null && bool_expr.isSymbolic()) {
            SmtAssertion newAssertion = new SmtAssertion(bool_expr);
            assertions.add(newAssertion);
        }
    }
    SmtCheckSatQuery smtCheckSatQuery = new SmtCheckSatQuery(constantDeclarations, assertions);
    return smtCheckSatQuery;
}
Also used : SmtAssertion(org.evosuite.symbolic.solver.smt.SmtAssertion) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) SmtConstantDeclaration(org.evosuite.symbolic.solver.smt.SmtConstantDeclaration) LinkedList(java.util.LinkedList) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)

Example 5 with SmtCheckSatQuery

use of org.evosuite.symbolic.solver.smt.SmtCheckSatQuery in project evosuite by EvoSuite.

the class Z3Str2Solver method buildSmtQuerty.

private static SmtCheckSatQuery buildSmtQuerty(Collection<Constraint<?>> constraints) {
    ConstraintToZ3Str2Visitor v = new ConstraintToZ3Str2Visitor();
    List<SmtAssertion> assertions = new LinkedList<SmtAssertion>();
    SmtVariableCollector varCollector = new SmtVariableCollector();
    SmtOperatorCollector opCollector = new SmtOperatorCollector();
    for (Constraint<?> c : constraints) {
        SmtExpr smtExpr = c.accept(v, null);
        if (smtExpr != null) {
            SmtAssertion newAssertion = new SmtAssertion(smtExpr);
            assertions.add(newAssertion);
            smtExpr.accept(varCollector, null);
            smtExpr.accept(opCollector, null);
        }
    }
    Set<SmtVariable> smtVariables = varCollector.getSmtVariables();
    Set<Operator> smtOperators = opCollector.getOperators();
    boolean addCharToIntFunction;
    if (smtOperators.contains(SmtOperation.Operator.CHAR_TO_INT)) {
        addCharToIntFunction = true;
    } else {
        addCharToIntFunction = false;
    }
    Set<SmtVariable> smtVariablesToDeclare = new HashSet<SmtVariable>(smtVariables);
    if (addCharToIntFunction) {
        Set<SmtStringVariable> charVariables = buildCharVariables();
        smtVariablesToDeclare.addAll(charVariables);
    }
    List<SmtConstantDeclaration> constantDeclarations = new LinkedList<SmtConstantDeclaration>();
    for (SmtVariable v1 : smtVariablesToDeclare) {
        String varName = v1.getName();
        if (v1 instanceof SmtIntVariable) {
            SmtConstantDeclaration constantDecl = SmtExprBuilder.mkIntConstantDeclaration(varName);
            constantDeclarations.add(constantDecl);
        } else if (v1 instanceof SmtRealVariable) {
            SmtConstantDeclaration constantDecl = SmtExprBuilder.mkRealConstantDeclaration(varName);
            constantDeclarations.add(constantDecl);
        } else if (v1 instanceof SmtStringVariable) {
            SmtConstantDeclaration constantDecl = SmtExprBuilder.mkStringConstantDeclaration(varName);
            constantDeclarations.add(constantDecl);
        } else {
            throw new RuntimeException("Unknown variable type " + v1.getClass().getCanonicalName());
        }
    }
    List<SmtFunctionDefinition> functionDefinitions = new LinkedList<SmtFunctionDefinition>();
    if (addCharToIntFunction) {
        String charToInt = buildCharToIntFunction();
        SmtFunctionDefinition newFunctionDef = new SmtFunctionDefinition(charToInt);
        functionDefinitions.add(newFunctionDef);
    }
    SmtCheckSatQuery smtCheckSatQuery = new SmtCheckSatQuery(constantDeclarations, functionDefinitions, assertions);
    return smtCheckSatQuery;
}
Also used : Operator(org.evosuite.symbolic.solver.smt.SmtOperation.Operator) SmtFunctionDefinition(org.evosuite.symbolic.solver.smt.SmtFunctionDefinition) SmtStringVariable(org.evosuite.symbolic.solver.smt.SmtStringVariable) SmtOperatorCollector(org.evosuite.symbolic.solver.smt.SmtOperatorCollector) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) SmtVariable(org.evosuite.symbolic.solver.smt.SmtVariable) HashSet(java.util.HashSet) SmtAssertion(org.evosuite.symbolic.solver.smt.SmtAssertion) SmtVariableCollector(org.evosuite.symbolic.solver.smt.SmtVariableCollector) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) SmtIntVariable(org.evosuite.symbolic.solver.smt.SmtIntVariable) SmtConstantDeclaration(org.evosuite.symbolic.solver.smt.SmtConstantDeclaration) LinkedList(java.util.LinkedList) SmtRealVariable(org.evosuite.symbolic.solver.smt.SmtRealVariable)

Aggregations

SmtCheckSatQuery (org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)6 SmtIntVariable (org.evosuite.symbolic.solver.smt.SmtIntVariable)4 SmtRealVariable (org.evosuite.symbolic.solver.smt.SmtRealVariable)4 SmtStringVariable (org.evosuite.symbolic.solver.smt.SmtStringVariable)4 SmtVariable (org.evosuite.symbolic.solver.smt.SmtVariable)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Variable (org.evosuite.symbolic.expr.Variable)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SolverResult (org.evosuite.symbolic.solver.SolverResult)3 SmtAssertion (org.evosuite.symbolic.solver.smt.SmtAssertion)3 SmtConstantDeclaration (org.evosuite.symbolic.solver.smt.SmtConstantDeclaration)3 SmtExpr (org.evosuite.symbolic.solver.smt.SmtExpr)3 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)2 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)2 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)2 SmtFunctionDefinition (org.evosuite.symbolic.solver.smt.SmtFunctionDefinition)2 SmtOperatorCollector (org.evosuite.symbolic.solver.smt.SmtOperatorCollector)2