Search in sources :

Example 1 with SolverErrorException

use of org.evosuite.symbolic.solver.SolverErrorException in project evosuite by EvoSuite.

the class Z3Str2ResultParser method parse.

public SolverResult parse(String z3Str2Result, Map<String, Object> initialValues) throws SolverErrorException {
    if (z3Str2Result.contains("unknown sort")) {
        logger.debug("Z3_str2 output was " + z3Str2Result);
        String errMsg = "Z3_str2 found an unknown";
        throw new SolverErrorException(errMsg);
    }
    if (z3Str2Result.contains("unknown constant")) {
        logger.debug("Z3_str2 output was " + z3Str2Result);
        String errMsg = "Z3_str2 found an unknown constant";
        throw new SolverErrorException(errMsg);
    }
    if (z3Str2Result.contains("invalid expression")) {
        logger.debug("Z3_str2 output was " + z3Str2Result);
        String errMsg = "Z3_str2 found an invalid expression";
        throw new SolverErrorException(errMsg);
    }
    if (z3Str2Result.contains("unexpected input")) {
        logger.debug("Z3_str2 output was " + z3Str2Result);
        String errMsg = "Z3_str2 found an unexpected input";
        throw new SolverErrorException(errMsg);
    }
    if (z3Str2Result.contains("(error")) {
        throw new SolverErrorException("An error occurred in z3str2: " + z3Str2Result);
    }
    if (z3Str2Result.contains("> Error:")) {
        throw new SolverErrorException("An error occurred in z3str2: " + z3Str2Result);
    }
    if (!z3Str2Result.contains(">> SAT")) {
        SolverResult unsatResult = SolverResult.newUNSAT();
        return unsatResult;
    }
    SolverResult solverResult = parseSAT(z3Str2Result, initialValues);
    return solverResult;
}
Also used : SolverErrorException(org.evosuite.symbolic.solver.SolverErrorException) SolverResult(org.evosuite.symbolic.solver.SolverResult)

Example 2 with SolverErrorException

use of org.evosuite.symbolic.solver.SolverErrorException in project evosuite by EvoSuite.

the class CVC4ResultParser method parse.

public SolverResult parse(String cvc4ResultStr) throws SolverParseException, SolverErrorException, SolverTimeoutException {
    if (cvc4ResultStr.startsWith("sat")) {
        logger.debug("CVC4 outcome was SAT");
        SolverResult satResult = parseModel(cvc4ResultStr);
        return satResult;
    } else if (cvc4ResultStr.startsWith("unsat")) {
        logger.debug("CVC4 outcome was UNSAT");
        SolverResult unsatResult = SolverResult.newUNSAT();
        return unsatResult;
    } else if (cvc4ResultStr.startsWith("unknown")) {
        logger.debug("CVC4 outcome was UNKNOWN (probably due to timeout)");
        throw new SolverTimeoutException();
    } else if (cvc4ResultStr.startsWith("(error")) {
        logger.debug("CVC4 output was the following " + cvc4ResultStr);
        throw new SolverErrorException("An error (probably an invalid input) occurred while executing CVC4");
    } else {
        logger.debug("The following CVC4 output could not be parsed " + cvc4ResultStr);
        throw new SolverParseException("CVC4 output is unknown. We are unable to parse it to a proper solution!", cvc4ResultStr);
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) SolverErrorException(org.evosuite.symbolic.solver.SolverErrorException) SolverParseException(org.evosuite.symbolic.solver.SolverParseException) SolverResult(org.evosuite.symbolic.solver.SolverResult)

Example 3 with SolverErrorException

use of org.evosuite.symbolic.solver.SolverErrorException in project evosuite by EvoSuite.

the class CVC4Solver method solve.

@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, SolverEmptyQueryException, SolverErrorException, SolverParseException, IOException {
    if (Properties.CVC4_PATH == null) {
        String errMsg = "Property CVC4_PATH should be setted in order to use the CVC4 Solver!";
        logger.error(errMsg);
        throw new IllegalStateException(errMsg);
    }
    // In fact, it cannot even produce models for non-linear theories
    if (!reWriteNonLinearConstraints && hasNonLinearConstraints(constraints)) {
        logger.debug("Skipping query due to (unsupported) non-linear constraints");
        throw new SolverEmptyQueryException("Skipping query due to (unsupported) non-linear constraints");
    }
    long cvcTimeout = 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 smtQuery = buildSmtCheckSatQuery(constraints);
    if (smtQuery == null) {
        logger.debug("No variables found during the creation of the SMT query.");
        throw new SolverEmptyQueryException("No variables found during the creation of the SMT query.");
    }
    if (smtQuery.getAssertions().isEmpty()) {
        Map<String, Object> emptySolution = new HashMap<String, Object>();
        SolverResult emptySAT = SolverResult.newSAT(emptySolution);
        return emptySAT;
    }
    CVC4QueryPrinter printer = new CVC4QueryPrinter();
    String smtQueryStr = printer.print(smtQuery);
    if (smtQueryStr == null) {
        logger.debug("No variables found during constraint solving.");
        throw new SolverEmptyQueryException("No variables found during constraint solving.");
    }
    logger.debug("CVC4 Query:");
    logger.debug(smtQueryStr);
    String cvc4Cmd = buildCVC4cmd(cvcTimeout);
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    try {
        launchNewProcess(cvc4Cmd, smtQueryStr, (int) cvcTimeout, stdout);
        String cvc4ResultStr = stdout.toString("UTF-8");
        if (cvc4ResultStr.startsWith("unsat") && cvc4ResultStr.contains("(error \"Cannot get the current model unless immediately preceded by SAT/INVALID or UNKNOWN response.\")")) {
            // UNSAT
            SolverResult unsatResult = SolverResult.newUNSAT();
            return unsatResult;
        }
        if (cvc4ResultStr.contains("error")) {
            String errMsg = "An error occurred while executing CVC4!";
            logger.error(errMsg);
            throw new SolverErrorException(errMsg);
        }
        // parse solution
        Map<String, Object> initialValues = getConcreteValues(variables);
        CVC4ResultParser resultParser;
        if (addMissingVariables()) {
            resultParser = new CVC4ResultParser(initialValues);
        } else {
            resultParser = new CVC4ResultParser();
        }
        SolverResult solverResult = resultParser.parse(cvc4ResultStr);
        if (solverResult.isSAT()) {
            // check if the found solution is useful
            boolean check = checkSAT(constraints, solverResult);
            if (!check) {
                logger.debug("CVC4 solution does not solve the original constraint system. ");
                SolverResult unsatResult = SolverResult.newUNSAT();
                return unsatResult;
            }
        }
        return solverResult;
    } catch (IOException e) {
        if (e.getMessage().contains("Permission denied")) {
            logger.error("No permissions for running CVC4 binary");
        } else {
            logger.error("IO Exception during launching of CVC4 command");
        }
        throw e;
    }
}
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) HashMap(java.util.HashMap) SolverResult(org.evosuite.symbolic.solver.SolverResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SolverEmptyQueryException(org.evosuite.symbolic.solver.SolverEmptyQueryException) SmtCheckSatQuery(org.evosuite.symbolic.solver.smt.SmtCheckSatQuery) SolverErrorException(org.evosuite.symbolic.solver.SolverErrorException) HashSet(java.util.HashSet)

Aggregations

SolverErrorException (org.evosuite.symbolic.solver.SolverErrorException)3 SolverResult (org.evosuite.symbolic.solver.SolverResult)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Variable (org.evosuite.symbolic.expr.Variable)1 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)1 SolverParseException (org.evosuite.symbolic.solver.SolverParseException)1 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)1 SmtCheckSatQuery (org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)1 SmtIntVariable (org.evosuite.symbolic.solver.smt.SmtIntVariable)1 SmtRealVariable (org.evosuite.symbolic.solver.smt.SmtRealVariable)1 SmtStringVariable (org.evosuite.symbolic.solver.smt.SmtStringVariable)1 SmtVariable (org.evosuite.symbolic.solver.smt.SmtVariable)1