use of org.evosuite.symbolic.solver.SolverEmptyQueryException 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();
}
}
}
use of org.evosuite.symbolic.solver.SolverEmptyQueryException 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;
}
use of org.evosuite.symbolic.solver.SolverEmptyQueryException 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;
}
}
Aggregations