Search in sources :

Example 6 with SolverResult

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

the class TestCVC4ResultParser method parseSingleStringSolution.

@Test
public void parseSingleStringSolution() throws SolverParseException, SolverErrorException, SolverTimeoutException {
    StringBuffer buff = new StringBuffer();
    buff.append("sat\n");
    buff.append("(model\n");
    buff.append("(define-fun var8 () String \"Hello\")\n");
    buff.append(")\n");
    String result_str = buff.toString();
    CVC4ResultParser parser = new CVC4ResultParser();
    SolverResult solution = parser.parse(result_str);
    assertTrue(solution.isSAT());
    assertEquals("Hello", solution.getValue("var8"));
}
Also used : SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 7 with SolverResult

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

the class Z3ResultParser method parseResult.

public SolverResult parseResult(String z3ResultStr) throws SolverParseException {
    if (z3ResultStr.startsWith("sat")) {
        logger.debug("Z3 outcome was SAT");
        // parse solution
        Map<String, Object> solution = parseModel(z3ResultStr);
        // return a SAT
        SolverResult satResult = SolverResult.newSAT(solution);
        return satResult;
    } else if (z3ResultStr.startsWith("unsat")) {
        logger.debug("Z3 outcome was UNSAT");
        // return an UNSAT
        SolverResult unsatResult = SolverResult.newUNSAT();
        return unsatResult;
    } else {
        logger.debug("Z3 output was " + z3ResultStr);
        throw new SolverParseException("Z3 output is unknown. We are unable to parse it to a proper solution!", z3ResultStr);
    }
}
Also used : SolverParseException(org.evosuite.symbolic.solver.SolverParseException) SolverResult(org.evosuite.symbolic.solver.SolverResult)

Example 8 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult 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 9 with SolverResult

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

the class Z3Str2ResultParser method parseSAT.

private static SolverResult parseSAT(String z3str2Result, Map<String, Object> initialValues) {
    Map<String, Object> solution = new HashMap<String, Object>();
    String[] lines = z3str2Result.split("\n");
    for (String line : lines) {
        if (line.trim().equals(""))
            continue;
        if (line.startsWith("_t_"))
            continue;
        if (line.startsWith("unique-value!"))
            continue;
        if (line.startsWith("**************") || line.startsWith(">>") || line.startsWith("--------------"))
            continue;
        if (line.contains(" -> ")) {
            String[] fields = line.split(" -> ");
            String[] varSec = fields[0].split(":");
            String varName = varSec[0].trim();
            String varType = varSec[1].trim();
            String value = fields[1].trim();
            if (varName.startsWith("$$_len_")) {
                // ignore $$_len_... string length variables
                continue;
            }
            if (varName.startsWith("$$_val_")) {
                // ignore $$_val_... string val variables
                continue;
            }
            if (varName.startsWith("$$_str")) {
                // ignore $$_str... string str variables
                continue;
            }
            if (varName.startsWith("$$_bol")) {
                // ignore $$_bol... bool variables
                continue;
            }
            if (varName.startsWith("$$_int_")) {
                // ignore $$_int_... int variables
                continue;
            }
            if (varName.startsWith("$$_xor_")) {
                // ignore $$_xor_... int variables
                continue;
            }
            if (varType.equals("string")) {
                String noQuotationMarks = value.substring(1, value.length() - 1);
                String valueStr = removeSlashX(noQuotationMarks);
                solution.put(varName, valueStr);
            } else if (varType.equals("real")) {
                Double doubleVal;
                if (value.contains("/")) {
                    String[] fraction = value.split("/");
                    String numeratorStr = fraction[0];
                    String denominatorStr = fraction[1];
                    doubleVal = parseRational(false, numeratorStr, denominatorStr);
                } else {
                    doubleVal = Double.valueOf(value);
                }
                solution.put(varName, doubleVal);
            } else if (varType.equals("int")) {
                Long longVal = Long.valueOf(value);
                solution.put(varName, longVal);
            }
        }
    }
    if (initialValues != null) {
        addMissingValues(initialValues, solution);
    }
    SolverResult satResult = SolverResult.newSAT(solution);
    return satResult;
}
Also used : HashMap(java.util.HashMap) SolverResult(org.evosuite.symbolic.solver.SolverResult)

Example 10 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult 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)

Aggregations

SolverResult (org.evosuite.symbolic.solver.SolverResult)50 Test (org.junit.Test)37 Constraint (org.evosuite.symbolic.expr.Constraint)36 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)34 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)31 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)27 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)26 ArrayList (java.util.ArrayList)24 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)14 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)7 HashMap (java.util.HashMap)6 Variable (org.evosuite.symbolic.expr.Variable)4 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Solver (org.evosuite.symbolic.solver.Solver)3 SolverCache (org.evosuite.symbolic.solver.SolverCache)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SolverErrorException (org.evosuite.symbolic.solver.SolverErrorException)3