Search in sources :

Example 1 with Variable

use of org.evosuite.symbolic.expr.Variable 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 Variable

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

the class DeprecatedTestSuiteDSE method reduce.

/**
 * Apply cone of influence reduction to constraints with respect to the last
 * constraint in the list
 *
 * @param constraints
 * @return
 */
private List<Constraint<?>> reduce(List<Constraint<?>> constraints) {
    Constraint<?> target = constraints.get(constraints.size() - 1);
    Set<Variable<?>> dependencies = getVariables(target);
    LinkedList<Constraint<?>> coi = new LinkedList<Constraint<?>>();
    if (dependencies.size() <= 0)
        return coi;
    coi.add(target);
    for (int i = constraints.size() - 2; i >= 0; i--) {
        Constraint<?> constraint = constraints.get(i);
        Set<Variable<?>> variables = getVariables(constraint);
        for (Variable<?> var : dependencies) {
            if (variables.contains(var)) {
                dependencies.addAll(variables);
                coi.addFirst(constraint);
                break;
            }
        }
    }
    return coi;
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) Constraint(org.evosuite.symbolic.expr.Constraint) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint)

Example 3 with Variable

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

the class ConcolicExecutionTest method testCase94.

@Test
public void testCase94() throws SecurityException, NoSuchMethodException {
    DefaultTestCase tc = buildTestCase94();
    List<BranchCondition> branch_conditions = executeTest(tc);
    Set<Variable<?>> variables = new HashSet<Variable<?>>();
    for (BranchCondition branchCondition : branch_conditions) {
        variables.addAll(branchCondition.getConstraint().getVariables());
    }
    assertEquals(2, variables.size());
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Variable

use of org.evosuite.symbolic.expr.Variable 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 5 with Variable

use of org.evosuite.symbolic.expr.Variable 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

Variable (org.evosuite.symbolic.expr.Variable)9 Constraint (org.evosuite.symbolic.expr.Constraint)5 HashSet (java.util.HashSet)4 SolverResult (org.evosuite.symbolic.solver.SolverResult)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SmtCheckSatQuery (org.evosuite.symbolic.solver.smt.SmtCheckSatQuery)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 SmtIntVariable (org.evosuite.symbolic.solver.smt.SmtIntVariable)2 SmtRealVariable (org.evosuite.symbolic.solver.smt.SmtRealVariable)2 SmtStringVariable (org.evosuite.symbolic.solver.smt.SmtStringVariable)2 SmtVariable (org.evosuite.symbolic.solver.smt.SmtVariable)2 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SolverErrorException (org.evosuite.symbolic.solver.SolverErrorException)1