CBMC
Loading...
Searching...
No Matches
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::CVC5?"CVC5":
29 solver==solvert::MATHSAT?"MathSAT":
30 solver==solvert::YICES?"Yices":
31 solver==solvert::Z3?"Z3":
32 "(unknown)");
33 // clang-format on
34}
35
37{
39
40 temporary_filet temp_file_problem("smt2_dec_problem_", ""),
41 temp_file_stdout("smt2_dec_stdout_", ""),
42 temp_file_stderr("smt2_dec_stderr_", "");
43
44 const auto write_problem_to_file = [&](std::ofstream problem_out) {
45 if(assumption.is_not_nil())
46 assumptions.push_back(convert(assumption));
47
49 stringstream.str(std::string{});
50
52
53 if(assumption.is_not_nil())
54 assumptions.pop_back();
55
56 problem_out << cached_output.str() << stringstream.str();
57 stringstream.str(std::string{});
58 };
59
60 write_problem_to_file(std::ofstream(
61 temp_file_problem(), std::ios_base::out | std::ios_base::trunc));
62
63 std::vector<std::string> argv;
64 std::string stdin_filename;
65
66 auto solver_binary_name = [this](const std::string &solver_name)
67 {
68 if(solver_binary_or_empty.empty())
69 return solver_name;
70 else
72 };
73
74 switch(solver)
75 {
77 argv = {solver_binary_name("bitwuzla"), temp_file_problem()};
78 break;
79
81 argv = {
82 solver_binary_name("boolector"), "--smt2", temp_file_problem(), "-m"};
83 break;
84
86 argv = {solver_binary_name("smt2_solver")};
88 break;
89
90 case solvert::CVC5:
91 argv = {
92 solver_binary_name("cvc5"), "--lang", "smtlib", temp_file_problem()};
93 break;
94
96 // The options below were recommended by Alberto Griggio
97 // on 10 July 2013
98
99 argv = {
100 solver_binary_name("mathsat"),
101 "-input=smt2",
102 "-preprocessor.toplevel_propagation=true",
103 "-preprocessor.simplification=7",
104 "-dpll.branching_random_frequency=0.01",
105 "-dpll.branching_random_invalidate_phase_cache=true",
106 "-dpll.restart_strategy=3",
107 "-dpll.glucose_var_activity=true",
108 "-dpll.glucose_learnt_minimization=true",
109 "-theory.bv.eager=true",
110 "-theory.bv.bit_blast_mode=1",
111 "-theory.bv.delay_propagated_eqs=true",
112 "-theory.fp.mode=1",
113 "-theory.fp.bit_blast_mode=2",
114 "-theory.arr.mode=1"};
115
117 break;
118
119 case solvert::YICES:
120 // command = "yices -smt -e " // Calling convention for older versions
121 // Convention for 2.2.1
122 argv = {solver_binary_name("yices-smt2"), temp_file_problem()};
123 break;
124
125 case solvert::Z3:
126 argv = {solver_binary_name("z3"), "-smt2", temp_file_problem()};
127 break;
128
129 case solvert::GENERIC:
132 break;
133 }
134
135 int res =
137
138 if(res<0)
139 {
141 log.error() << "error running SMT2 solver" << messaget::eom;
143 }
144
145 std::ifstream in(temp_file_stdout());
146 return read_result(in);
147}
148
149static std::string drop_quotes(std::string src)
150{
151 if(src.size() >= 2 && src.front() == '|' && src.back() == '|')
152 return std::string(src, 1, src.size() - 2);
153 else
154 return src;
155}
156
158{
159 std::string line;
161
164
165 typedef std::unordered_map<irep_idt, irept> valuest;
166 valuest parsed_values;
167
168 while(in)
169 {
171
172 if(!parsed_opt.has_value())
173 break;
174
175 const auto &parsed = parsed_opt.value();
176
177 if(parsed.id()=="sat")
179 else if(parsed.id()=="unsat")
181 else if(parsed.id() == "unknown")
182 {
184 log.error() << "SMT2 solver returned \"unknown\"" << messaget::eom;
186 }
187 else if(
188 parsed.id().empty() && parsed.get_sub().size() == 1 &&
189 parsed.get_sub().front().get_sub().size() == 2)
190 {
191 const irept &s0=parsed.get_sub().front().get_sub()[0];
192 const irept &s1=parsed.get_sub().front().get_sub()[1];
193
194 // Examples:
195 // ( (B0 true) )
196 // ( (|__CPROVER_pipe_count#1| (_ bv0 32)) )
197 // ( (|some_integer| 0) )
198 // ( (|some_integer| (- 10)) )
199
200 parsed_values[s0.id()] = s1;
201 }
202 else if(
203 parsed.id().empty() && parsed.get_sub().size() == 2 &&
204 parsed.get_sub().front().id() == "error")
205 {
206 // We ignore errors after UNSAT because get-value after check-sat
207 // returns unsat will give an error.
209 {
210 const auto &message = id2string(parsed.get_sub()[1].id());
212 log.error() << "SMT2 solver returned error message:\n"
213 << "\t\"" << message << "\"" << messaget::eom;
215 }
216 }
217 }
218
219 // If the result is not satisfiable don't bother updating the assignments and
220 // values (since we didn't get any), just return.
222 return res;
223
224 for(auto &assignment : identifier_map)
225 {
226 std::string conv_id = drop_quotes(convert_identifier(assignment.first));
227 const irept &value = parsed_values[conv_id];
228 assignment.second.value = parse_rec(value, assignment.second.type);
229 }
230
231 // Booleans
232 for(unsigned v=0; v<no_boolean_variables; v++)
233 {
234 const std::string boolean_identifier =
235 convert_identifier("B" + std::to_string(v));
236 const auto found_parsed_value =
239 {
240 const irept &value = found_parsed_value->second;
241
242 if(value.id() != ID_true && value.id() != ID_false)
243 {
245 log.error() << "SMT2 solver returned non-constant value for variable "
248 }
249 boolean_assignment[v] = value.id() == ID_true;
250 }
251 else
252 {
253 // Work out the value based on what set_to was called with.
255 if(found_set_value != set_values.end())
257 else
258 {
259 // Old code used the computation
260 // const irept &value=values["B"+std::to_string(v)];
261 // boolean_assignment[v]=(value.id()==ID_true);
262 const irept &value = parsed_values[boolean_identifier];
263
264 if(value.id() != ID_true && value.id() != ID_false)
265 {
267 log.error()
268 << "SMT2 solver returned non-Boolean value for variable "
271 }
272 boolean_assignment[v] = value.id() == ID_true;
273 }
274 }
275 }
276
277 return res;
278}
int8_t s1
virtual void clear()
Reset the abstract state.
Definition ai.h:269
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
resultt
Result of running the decision procedure.
Base class for all expressions.
Definition expr.h:57
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:110
void write_footer()
Writes the end of the SMT file to the smt_convt::out stream.
bool use_FPA_theory
Definition smt2_conv.h:66
std::string logic
Definition smt2_conv.h:101
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:107
solvert solver
Definition smt2_conv.h:102
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:46
std::string decision_procedure_text() const override
Return a textual description of the decision procedure.
Definition smt2_dec.cpp:20
std::string solver_binary_or_empty
Definition smt2_dec.h:45
resultt dec_solve(const exprt &) override
Implementation of the decision procedure.
Definition smt2_dec.cpp:36
resultt read_result(std::istream &in)
Definition smt2_dec.cpp:157
std::stringstream cached_output
Everything except the footer is cached, so that output files can be rewritten with varying footers.
Definition smt2_dec.h:51
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:2416
int run(const std::string &what, const std::vector< std::string > &argv)
Definition run.cpp:49
static std::string drop_quotes(std::string src)
Definition smt2_dec.cpp:149
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 PRECONDITION(CONDITION)
Definition invariant.h:463