|
| 1 | +var commandExistsSync = require('command-exists').sync; |
| 2 | +var execSync = require('child_process').execSync; |
| 3 | +var fs = require('fs-extra'); |
| 4 | +var tmp = require('tmp'); |
| 5 | + |
| 6 | +var potentialSolvers = [ |
| 7 | + { |
| 8 | + name: 'z3', |
| 9 | + params: '' |
| 10 | + }, |
| 11 | + { |
| 12 | + name: 'cvc4', |
| 13 | + params: '--lang=smt2' |
| 14 | + } |
| 15 | +]; |
| 16 | +var solvers = potentialSolvers.filter(solver => commandExistsSync(solver.name)); |
| 17 | + |
| 18 | +function solve (query) { |
| 19 | + var tmpFile = tmp.fileSync(); |
| 20 | + fs.writeFileSync(tmpFile.name, query); |
| 21 | + // TODO For now only the first SMT solver found is used. |
| 22 | + // At some point a computation similar to the one done in |
| 23 | + // SMTPortfolio::check should be performed, where the results |
| 24 | + // given by different solvers are compared and an error is |
| 25 | + // reported if solvers disagree (i.e. SAT vs UNSAT). |
| 26 | + var solverOutput = execSync(solvers[0].name + ' ' + solvers[0].params + ' ' + tmpFile.name); |
| 27 | + // Trigger early manual cleanup |
| 28 | + tmpFile.removeCallback(); |
| 29 | + return solverOutput.toString(); |
| 30 | +} |
| 31 | + |
| 32 | +// This function checks the standard JSON output for auxiliaryInputRequested, |
| 33 | +// where smtlib2queries represent the queries created by the SMTChecker. |
| 34 | +// The function runs an SMT solver on each query and adjusts the input for |
| 35 | +// another run. |
| 36 | +// Returns null if no solving is requested. |
| 37 | +function handleSMTQueries (inputJSON, outputJSON) { |
| 38 | + var auxInputReq = outputJSON.auxiliaryInputRequested; |
| 39 | + if (!auxInputReq) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + var queries = auxInputReq.smtlib2queries; |
| 44 | + if (!queries || Object.keys(queries) === 0) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + if (solvers.length === 0) { |
| 49 | + throw new Error('No SMT solver available. Assertion checking will not be performed.'); |
| 50 | + } |
| 51 | + |
| 52 | + var responses = {}; |
| 53 | + for (var query in queries) { |
| 54 | + responses[query] = solve(queries[query]); |
| 55 | + } |
| 56 | + |
| 57 | + // Note: all existing solved queries are replaced. |
| 58 | + // This assumes that all neccessary queries are quested above. |
| 59 | + inputJSON.auxiliaryInput = { smtlib2responses: responses }; |
| 60 | + return inputJSON; |
| 61 | +} |
| 62 | + |
| 63 | +module.exports = { |
| 64 | + handleSMTQueries: handleSMTQueries |
| 65 | +}; |
0 commit comments