CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
smt2_dec.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "smt2_dec.h"
10
11#include <util/invariant.h>
12#include <util/message.h>
13#include <util/run.h>
14#include <util/tempfile.h>
15
16#include "smt2irep.h"
17
18#include <fstream>
19
21{
22 // clang-format off
23 return "SMT2 " + logic + (use_FPA_theory ? " (with FPA)" : "") + " using " +
24 (solver==solvert::GENERIC?"Generic":
25 solver==solvert::BITWUZLA?"Bitwuzla":
26 solver==solvert::BOOLECTOR?"Boolector":
27 solver==solvert::CPROVER_SMT2?"CPROVER SMT2":
28 solver==solvert::CVC3?"CVC3":
29 solver==solvert::CVC4?"CVC4":
30 solver==solvert::CVC5?"CVC5":
31 solver==solvert::MATHSAT?"MathSAT":
32 solver==solvert::YICES?"Yices":
33 solver==solvert::Z3?"Z3":
34 "(unknown)");
35 // clang-format on
36}
37
39{
41
42 temporary_filet temp_file_problem("smt2_dec_problem_", ""),
43 temp_file_stdout("smt2_dec_stdout_", ""),
44 temp_file_stderr("smt2_dec_stderr_", "");
45
46 const auto write_problem_to_file = [&](std::ofstream problem_out) {
47 if(assumption.is_not_nil())
48 assumptions.push_back(convert(assumption));
49
51 stringstream.str(std::string{});
52
54
55 if(assumption.is_not_nil())
56 assumptions.pop_back();
57
58 problem_out << cached_output.str() << stringstream.str();
59 stringstream.str(std::string{});
60 };
61
62 write_problem_to_file(std::ofstream(
63 temp_file_problem(), std::ios_base::out | std::ios_base::trunc));
64
65 std::vector<std::string> argv;
66 std::string stdin_filename;
67
68 switch(solver)
69 {
71 argv = {"bitwuzla", temp_file_problem()};
72 break;
73
75 argv = {"boolector", "--smt2", temp_file_problem(), "-m"};
76 break;
77
79 argv = {"smt2_solver"};
81 break;
82
83 case solvert::CVC3:
84 argv = {"cvc3",
85 "+model",
86 "-lang",
87 "smtlib",
88 "-output-lang",
89 "smtlib",
91 break;
92
93 case solvert::CVC4:
94 // The flags --bitblast=eager --bv-div-zero-const help but only
95 // work for pure bit-vector formulas.
96 argv = {"cvc4", "-L", "smt2", temp_file_problem()};
97 break;
98
99 case solvert::CVC5:
100 argv = {"cvc5", "--lang", "smtlib", temp_file_problem()};
101 break;
102
103 case solvert::MATHSAT:
104 // The options below were recommended by Alberto Griggio
105 // on 10 July 2013
106
107 argv = {"mathsat",
108 "-input=smt2",
109 "-preprocessor.toplevel_propagation=true",
110 "-preprocessor.simplification=7",
111 "-dpll.branching_random_frequency=0.01",
112 "-dpll.branching_random_invalidate_phase_cache=true",
113 "-dpll.restart_strategy=3",
114 "-dpll.glucose_var_activity=true",
115 "-dpll.glucose_learnt_minimization=true",
116 "-theory.bv.eager=true",
117 "-theory.bv.bit_blast_mode=1",
118 "-theory.bv.delay_propagated_eqs=true",
119 "-theory.fp.mode=1",
120 "-theory.fp.bit_blast_mode=2",
121 "-theory.arr.mode=1"};
122
124 break;
125
126 case solvert::YICES:
127 // command = "yices -smt -e " // Calling convention for older versions
128 // Convention for 2.2.1
129 argv = {"yices-smt2", temp_file_problem()};
130 break;
131
132 case solvert::Z3:
133 argv = {"z3", "-smt2", temp_file_problem()};
134 break;
135
136 case solvert::GENERIC:
138 }
139
140 int res =
142
143 if(res<0)
144 {
146 log.error() << "error running SMT2 solver" << messaget::eom;
148 }
149
150 std::ifstream in(temp_file_stdout());
151 return read_result(in);
152}
153
154static std::string drop_quotes(std::string src)
155{
156 if(src.size() >= 2 && src.front() == '|' && src.back() == '|')
157 return std::string(src, 1, src.size() - 2);
158 else
159 return src;
160}
161
163{
164 std::string line;
166
169
170 typedef std::unordered_map<irep_idt, irept> valuest;
171 valuest parsed_values;
172
173 while(in)
174 {
176
177 if(!parsed_opt.has_value())
178 break;
179
180 const auto &parsed = parsed_opt.value();
181
182 if(parsed.id()=="sat")
184 else if(parsed.id()=="unsat")
186 else if(parsed.id() == "unknown")
187 {
189 log.error() << "SMT2 solver returned \"unknown\"" << messaget::eom;
191 }
192 else if(
193 parsed.id().empty() && parsed.get_sub().size() == 1 &&
194 parsed.get_sub().front().get_sub().size() == 2)
195 {
196 const irept &s0=parsed.get_sub().front().get_sub()[0];
197 const irept &s1=parsed.get_sub().front().get_sub()[1];
198
199 // Examples:
200 // ( (B0 true) )
201 // ( (|__CPROVER_pipe_count#1| (_ bv0 32)) )
202 // ( (|some_integer| 0) )
203 // ( (|some_integer| (- 10)) )
204
205 parsed_values[s0.id()] = s1;
206 }
207 else if(
208 parsed.id().empty() && parsed.get_sub().size() == 2 &&
209 parsed.get_sub().front().id() == "error")
210 {
211 // We ignore errors after UNSAT because get-value after check-sat
212 // returns unsat will give an error.
214 {
215 const auto &message = id2string(parsed.get_sub()[1].id());
217 log.error() << "SMT2 solver returned error message:\n"
218 << "\t\"" << message << "\"" << messaget::eom;
220 }
221 }
222 }
223
224 // If the result is not satisfiable don't bother updating the assignments and
225 // values (since we didn't get any), just return.
227 return res;
228
229 for(auto &assignment : identifier_map)
230 {
231 std::string conv_id = drop_quotes(convert_identifier(assignment.first));
232 const irept &value = parsed_values[conv_id];
233 assignment.second.value = parse_rec(value, assignment.second.type);
234 }
235
236 // Booleans
237 for(unsigned v=0; v<no_boolean_variables; v++)
238 {
239 const std::string boolean_identifier =
240 convert_identifier("B" + std::to_string(v));
241 const auto found_parsed_value =
244 {
245 const irept &value = found_parsed_value->second;
246
247 if(value.id() != ID_true && value.id() != ID_false)
248 {
250 log.error() << "SMT2 solver returned non-constant value for variable "
253 }
254 boolean_assignment[v] = value.id() == ID_true;
255 }
256 else
257 {
258 // Work out the value based on what set_to was called with.
260 if(found_set_value != set_values.end())
262 else
263 {
264 // Old code used the computation
265 // const irept &value=values["B"+std::to_string(v)];
266 // boolean_assignment[v]=(value.id()==ID_true);
267 const irept &value = parsed_values[boolean_identifier];
268
269 if(value.id() != ID_true && value.id() != ID_false)
270 {
272 log.error()
273 << "SMT2 solver returned non-Boolean value for variable "
276 }
277 boolean_assignment[v] = value.id() == ID_true;
278 }
279 }
280 }
281
282 return res;
283}
int8_t s1
virtual void clear()
Reset the abstract state.
Definition ai.h:265
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
resultt
Result of running the decision procedure.
Base class for all expressions.
Definition expr.h:56
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition irep.h:364
bool is_not_nil() const
Definition irep.h:372
const irep_idt & id() const
Definition irep.h:388
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
static eomt eom
Definition message.h:289
std::size_t number_of_solver_calls
Definition smt2_conv.h:111
void write_footer()
Writes the end of the SMT file to the smt_convt::out stream.
bool use_FPA_theory
Definition smt2_conv.h:67
std::string logic
Definition smt2_conv.h:102
static std::string convert_identifier(const irep_idt &identifier)
std::unordered_map< irep_idt, bool > set_values
The values which boolean identifiers have been smt2_convt::set_to or in other words those which are a...
Definition smt2_conv.h:285
exprt parse_rec(const irept &s, const typet &type)
std::vector< bool > boolean_assignment
Definition smt2_conv.h:294
std::vector< literalt > assumptions
Definition smt2_conv.h:108
solvert solver
Definition smt2_conv.h:103
identifier_mapt identifier_map
Definition smt2_conv.h:265
std::size_t no_boolean_variables
Definition smt2_conv.h:293
literalt convert(const exprt &expr)
message_handlert & message_handler
Definition smt2_dec.h:43
std::string decision_procedure_text() const override
Return a textual description of the decision procedure.
Definition smt2_dec.cpp:20
resultt dec_solve(const exprt &) override
Implementation of the decision procedure.
Definition smt2_dec.cpp:38
resultt read_result(std::istream &in)
Definition smt2_dec.cpp:162
std::stringstream cached_output
Everything except the footer is cached, so that output files can be rewritten with varying footers.
Definition smt2_dec.h:48
std::stringstream stringstream
Definition smt2_dec.h:20
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
return read_result
Definition java.io.c:7
double log(double x)
Definition math.c:2449
int run(const std::string &what, const std::vector< std::string > &argv)
Definition run.cpp:48
static std::string drop_quotes(std::string src)
Definition smt2_dec.cpp:154
std::optional< irept > smt2irep(std::istream &in, message_handlert &message_handler)
returns an irep for an SMT-LIB2 expression read from a given stream returns {} when EOF is encountere...
Definition smt2irep.cpp:92
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525