CBMC
Loading...
Searching...
No Matches
solver_factory.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Solver Factory
4
5Author: Daniel Kroening, Peter Schrammel
6
7\*******************************************************************/
8
11
12#include "solver_factory.h"
13
14#include <util/cmdline.h>
16#include <util/exit_codes.h>
17#include <util/message.h>
18#include <util/options.h>
19#include <util/unicode.h>
20#include <util/version.h>
21
24#include <solvers/prop/prop.h>
33
34#include <iostream>
35
37 const optionst &_options,
38 const namespacet &_ns,
41 : options(_options),
42 ns(_ns),
43 message_handler(_message_handler),
44 output_xml_in_refinement(_output_xml_in_refinement)
45{
46}
47
48solver_factoryt::solvert::solvert(std::unique_ptr<stack_decision_proceduret> p)
49 : decision_procedure_ptr(std::move(p))
50{
51}
52
54 std::unique_ptr<stack_decision_proceduret> p1,
55 std::unique_ptr<propt> p2)
56 : prop_ptr(std::move(p2)), decision_procedure_ptr(std::move(p1))
57{
58}
59
61 std::unique_ptr<stack_decision_proceduret> p1,
62 std::unique_ptr<std::ofstream> p2)
63 : ofstream_ptr(std::move(p2)), decision_procedure_ptr(std::move(p1))
64{
65}
66
68 std::unique_ptr<boolbvt> p1,
69 std::unique_ptr<propt> p2)
70 : prop_ptr(std::move(p2)), decision_procedure_is_boolbvt_ptr(std::move(p1))
71{
72}
73
75{
77 (decision_procedure_ptr != nullptr) !=
78 (decision_procedure_is_boolbvt_ptr != nullptr));
79 if(decision_procedure_ptr)
80 return *decision_procedure_ptr;
81 else
82 return *decision_procedure_is_boolbvt_ptr;
83}
84
86{
87 PRECONDITION(decision_procedure_is_boolbvt_ptr != nullptr);
88 return *decision_procedure_is_boolbvt_ptr;
89}
90
92 solver_resource_limitst &decision_procedure)
93{
94 const int timeout_seconds =
95 options.get_signed_int_option("solver-time-limit");
96
97 if(timeout_seconds > 0)
98 decision_procedure.set_time_limit_seconds(timeout_seconds);
99}
100
101std::unique_ptr<solver_factoryt::solvert> solver_factoryt::get_solver()
102{
103 if(options.get_bool_option("dimacs"))
104 return get_dimacs();
105 if(options.is_set("external-sat-solver"))
106 return get_external_sat();
107 if(
108 options.get_bool_option("refine") &&
109 !options.get_bool_option("refine-strings"))
110 {
111 return get_bv_refinement();
112 }
113 else if(options.get_bool_option("refine-strings"))
114 return get_string_refinement();
115 const auto incremental_smt2_solver =
116 options.get_option("incremental-smt2-solver");
117 if(!incremental_smt2_solver.empty())
119 if(options.get_bool_option("smt2"))
121 return get_default();
122}
123
127{
128 // we shouldn't get here if this option isn't set
129 PRECONDITION(options.get_bool_option("smt2"));
130
132
133 if(options.get_bool_option("bitwuzla"))
135 else if(options.get_bool_option("boolector"))
137 else if(options.get_bool_option("cprover-smt2"))
139 else if(options.get_bool_option("mathsat"))
141 else if(options.get_bool_option("cvc3"))
143 else if(options.get_bool_option("cvc4"))
145 else if(options.get_bool_option("cvc5"))
147 else if(options.get_bool_option("yices"))
149 else if(options.get_bool_option("z3"))
151 else if(options.get_bool_option("generic"))
153
154 return s;
155}
156
160 const std::string &solver)
161{
163 log.warning() << "The specified solver, '" << solver
164 << "', is not available. "
165 << "The default solver will be used instead." << messaget::eom;
166}
167
168template <typename SatcheckT>
169static typename std::enable_if<
170 !std::is_base_of<hardness_collectort, SatcheckT>::value,
171 std::unique_ptr<SatcheckT>>::type
173{
174 auto satcheck = std::make_unique<SatcheckT>(message_handler);
175 if(options.is_set("write-solver-stats-to"))
176 {
178 log.warning()
179 << "Configured solver does not support --write-solver-stats-to. "
180 << "Solver stats will not be written." << messaget::eom;
181 }
182 return satcheck;
183}
184
185template <typename SatcheckT>
186static typename std::enable_if<
187 std::is_base_of<hardness_collectort, SatcheckT>::value,
188 std::unique_ptr<SatcheckT>>::type
190{
191 auto satcheck = std::make_unique<SatcheckT>(message_handler);
192 if(options.is_set("write-solver-stats-to"))
193 {
194 std::unique_ptr<solver_hardnesst> solver_hardness =
195 std::make_unique<solver_hardnesst>();
196 solver_hardness->set_outfile(options.get_option("write-solver-stats-to"));
197 satcheck->solver_hardness = std::move(solver_hardness);
198 }
199 return satcheck;
200}
201
202static std::unique_ptr<propt>
204{
205 const bool no_simplifier = options.get_bool_option("beautify") ||
206 !options.get_bool_option("sat-preprocessor") ||
207 options.get_bool_option("refine-arithmetic") ||
208 options.get_bool_option("refine-strings");
209
210 if(options.is_set("sat-solver"))
211 {
212 const std::string &solver_option = options.get_option("sat-solver");
213 if(solver_option == "zchaff")
214 {
215#if defined SATCHECK_ZCHAFF
217#else
219#endif
220 }
221 else if(solver_option == "booleforce")
222 {
223#if defined SATCHECK_BOOLERFORCE
225#else
227#endif
228 }
229 else if(solver_option == "minisat1")
230 {
231#if defined SATCHECK_MINISAT1
233#else
235#endif
236 }
237 else if(solver_option == "minisat2")
238 {
239#if defined SATCHECK_MINISAT2
240 if(no_simplifier)
241 {
242 // simplifier won't work with beautification
245 }
246 else // with simplifier
247 {
250 }
251#else
253#endif
254 }
255 else if(solver_option == "ipasir")
256 {
257#if defined SATCHECK_IPASIR
259#else
261#endif
262 }
263 else if(solver_option == "picosat")
264 {
265#if defined SATCHECK_PICOSAT
267#else
269#endif
270 }
271 else if(solver_option == "lingeling")
272 {
273#if defined SATCHECK_LINGELING
275#else
277#endif
278 }
279 else if(solver_option == "glucose")
280 {
281#if defined SATCHECK_GLUCOSE
282 if(no_simplifier)
283 {
284 // simplifier won't work with beautification
287 }
288 else // with simplifier
289 {
292 }
293#else
295#endif
296 }
297 else if(solver_option == "cadical")
298 {
299#if defined SATCHECK_CADICAL
302#else
304#endif
305 }
306 else
307 {
309 log.error() << "unknown solver '" << solver_option << "'"
310 << messaget::eom;
312 }
313 }
314
315 // default solver
316 if(no_simplifier)
317 {
318 // simplifier won't work with beautification
321 }
322 else // with simplifier
323 {
325 }
326}
327
328std::unique_ptr<solver_factoryt::solvert> solver_factoryt::get_default()
329{
331
332 bool get_array_constraints =
333 options.get_bool_option("show-array-constraints");
334 auto bv_pointers = std::make_unique<bv_pointerst>(
335 ns, *sat_solver, message_handler, get_array_constraints);
336
337 if(options.get_option("arrays-uf") == "never")
339 else if(options.get_option("arrays-uf") == "always")
341
343
344 std::unique_ptr<boolbvt> boolbv = std::move(bv_pointers);
345 return std::make_unique<solvert>(std::move(boolbv), std::move(sat_solver));
346}
347
348std::unique_ptr<solver_factoryt::solvert> solver_factoryt::get_dimacs()
349{
352
353 auto prop = std::make_unique<dimacs_cnft>(message_handler);
354
355 std::string filename = options.get_option("outfile");
356
357 std::unique_ptr<boolbvt> bv_dimacs =
358 std::make_unique<bv_dimacst>(ns, *prop, message_handler, filename);
359
360 return std::make_unique<solvert>(std::move(bv_dimacs), std::move(prop));
361}
362
363std::unique_ptr<solver_factoryt::solvert> solver_factoryt::get_external_sat()
364{
367
368 std::string external_sat_solver = options.get_option("external-sat-solver");
369 auto prop =
370 std::make_unique<external_satt>(message_handler, external_sat_solver);
371
372 std::unique_ptr<boolbvt> bv_pointers =
373 std::make_unique<bv_pointerst>(ns, *prop, message_handler);
374
375 return std::make_unique<solvert>(std::move(bv_pointers), std::move(prop));
376}
377
378std::unique_ptr<solver_factoryt::solvert> solver_factoryt::get_bv_refinement()
379{
380 std::unique_ptr<propt> prop = get_sat_solver(message_handler, options);
381
383 info.ns = &ns;
384 info.prop = prop.get();
386
387 // we allow setting some parameters
388 if(options.get_bool_option("max-node-refinement"))
389 info.max_node_refinement =
390 options.get_unsigned_int_option("max-node-refinement");
391
392 info.refine_arrays = options.get_bool_option("refine-arrays");
393 info.refine_arithmetic = options.get_bool_option("refine-arithmetic");
395
396 std::unique_ptr<boolbvt> decision_procedure =
397 std::make_unique<bv_refinementt>(info);
398 set_decision_procedure_time_limit(*decision_procedure);
399 return std::make_unique<solvert>(
400 std::move(decision_procedure), std::move(prop));
401}
402
406std::unique_ptr<solver_factoryt::solvert>
408{
410 info.ns = &ns;
412 info.prop = prop.get();
413 info.refinement_bound = DEFAULT_MAX_NB_REFINEMENT;
415 if(options.get_bool_option("max-node-refinement"))
416 info.max_node_refinement =
417 options.get_unsigned_int_option("max-node-refinement");
418 info.refine_arrays = options.get_bool_option("refine-arrays");
419 info.refine_arithmetic = options.get_bool_option("refine-arithmetic");
421
422 std::unique_ptr<boolbvt> decision_procedure =
423 std::make_unique<string_refinementt>(info);
424 set_decision_procedure_time_limit(*decision_procedure);
425 return std::make_unique<solvert>(
426 std::move(decision_procedure), std::move(prop));
427}
428
429std::unique_ptr<std::ofstream> open_outfile_and_check(
430 const std::string &filename,
432 const std::string &arg_name)
433{
434 if(filename.empty())
435 return nullptr;
436
437 auto out = std::make_unique<std::ofstream>(widen_if_needed(filename));
438
439 if(!*out)
440 {
442 "failed to open file: " + filename, arg_name);
443 }
444
446 log.status() << "Outputting SMTLib formula to file: " << filename
447 << messaget::eom;
448 return out;
449}
450
451std::unique_ptr<solver_factoryt::solvert>
453{
455
456 const std::string outfile_arg = options.get_option("outfile");
457 const std::string dump_smt_formula = options.get_option("dump-smt-formula");
458
459 if(!outfile_arg.empty() && !dump_smt_formula.empty())
460 {
462 "Argument --outfile is incompatible with --dump-smt-formula. ",
463 "--outfile");
464 }
465
466 std::unique_ptr<smt_base_solver_processt> solver_process = nullptr;
467
468 if(!outfile_arg.empty())
469 {
470 bool on_std_out = outfile_arg == "-";
471 std::unique_ptr<std::ostream> outfile =
473 ? nullptr
475 solver_process = std::make_unique<smt_incremental_dry_run_solvert>(
476 message_handler, on_std_out ? std::cout : *outfile, std::move(outfile));
477 }
478 else
479 {
480 const auto out_filename = options.get_option("dump-smt-formula");
481
482 // If no out_filename is provided `open_outfile_and_check` will return
483 // `nullptr`, and the solver will work normally without any logging.
484 solver_process = std::make_unique<smt_piped_solver_processt>(
485 std::move(solver_command),
488 out_filename, message_handler, "--dump-smt-formula"));
489 }
490
491 return std::make_unique<solvert>(
492 std::make_unique<smt2_incremental_decision_proceduret>(
493 ns, std::move(solver_process), message_handler));
494}
495
496std::unique_ptr<solver_factoryt::solvert>
498{
500
501 const std::string &filename = options.get_option("outfile");
502
503 if(filename.empty())
504 {
506 {
508 "required filename not provided",
509 "--outfile",
510 "provide a filename with --outfile");
511 }
512
513 auto smt2_dec = std::make_unique<smt2_dect>(
514 ns,
515 "cbmc",
516 std::string("Generated by CBMC ") + CBMC_VERSION,
517 "QF_AUFBV",
518 solver,
520
521 if(options.get_bool_option("fpa"))
522 smt2_dec->use_FPA_theory = true;
523
524 return std::make_unique<solvert>(std::move(smt2_dec));
525 }
526 else if(filename == "-")
527 {
528 auto smt2_conv = std::make_unique<smt2_convt>(
529 ns,
530 "cbmc",
531 std::string("Generated by CBMC ") + CBMC_VERSION,
532 "QF_AUFBV",
533 solver,
534 std::cout);
535
536 if(options.get_bool_option("fpa"))
537 smt2_conv->use_FPA_theory = true;
538
539 return std::make_unique<solvert>(std::move(smt2_conv));
540 }
541 else
542 {
543 auto out = open_outfile_and_check(filename, message_handler, "--outfile");
544
545 auto smt2_conv = std::make_unique<smt2_convt>(
546 ns,
547 "cbmc",
548 std::string("Generated by CBMC ") + CBMC_VERSION,
549 "QF_AUFBV",
550 solver,
551 *out);
552
553 if(options.get_bool_option("fpa"))
554 smt2_conv->use_FPA_theory = true;
555
556 return std::make_unique<solvert>(std::move(smt2_conv), std::move(out));
557 }
558}
559
561{
562 if(options.get_bool_option("beautify"))
563 {
565 "the chosen solver does not support beautification", "--beautify");
566 }
567}
568
570{
571 const bool all_properties = options.get_bool_option("all-properties");
572 const bool cover = options.is_set("cover");
573 const bool incremental_loop = options.is_set("incremental-loop");
574
576 {
578 "the chosen solver does not support incremental solving",
579 "--all_properties");
580 }
581 else if(cover)
582 {
584 "the chosen solver does not support incremental solving", "--cover");
585 }
586 else if(incremental_loop)
587 {
589 "the chosen solver does not support incremental solving",
590 "--incremental-loop");
591 }
592}
593
594static void parse_sat_options(const cmdlinet &cmdline, optionst &options)
595{
596 if(cmdline.isset("external-sat-solver"))
597 {
598 options.set_option(
599 "external-sat-solver", cmdline.get_value("external-sat-solver"));
600 }
601
602 options.set_option("sat-preprocessor", !cmdline.isset("no-sat-preprocessor"));
603
604 if(cmdline.isset("dimacs"))
605 options.set_option("dimacs", true);
606
607 if(cmdline.isset("sat-solver"))
608 options.set_option("sat-solver", cmdline.get_value("sat-solver"));
609}
610
611static void parse_smt2_options(const cmdlinet &cmdline, optionst &options)
612{
613 if(cmdline.isset("smt2"))
614 options.set_option("smt2", true);
615
616 if(cmdline.isset("fpa"))
617 options.set_option("fpa", true);
618
619 bool solver_set = false;
620
621 if(cmdline.isset("bitwuzla"))
622 {
623 options.set_option("bitwuzla", true), solver_set = true;
624 options.set_option("smt2", true);
625 }
626
627 if(cmdline.isset("boolector"))
628 {
629 options.set_option("boolector", true), solver_set = true;
630 options.set_option("smt2", true);
631 }
632
633 if(cmdline.isset("cprover-smt2"))
634 {
635 options.set_option("cprover-smt2", true), solver_set = true;
636 options.set_option("smt2", true);
637 }
638
639 if(cmdline.isset("mathsat"))
640 {
641 options.set_option("mathsat", true), solver_set = true;
642 options.set_option("smt2", true);
643 }
644
645 if(cmdline.isset("cvc4"))
646 {
647 options.set_option("cvc4", true), solver_set = true;
648 options.set_option("smt2", true);
649 }
650
651 if(cmdline.isset("cvc5"))
652 {
653 options.set_option("cvc5", true), solver_set = true;
654 options.set_option("smt2", true);
655 }
656
657 if(cmdline.isset("incremental-smt2-solver"))
658 {
659 options.set_option(
660 "incremental-smt2-solver", cmdline.get_value("incremental-smt2-solver")),
661 solver_set = true;
662 }
663
664 if(cmdline.isset("yices"))
665 {
666 options.set_option("yices", true), solver_set = true;
667 options.set_option("smt2", true);
668 }
669
670 if(cmdline.isset("z3"))
671 {
672 options.set_option("z3", true), solver_set = true;
673 options.set_option("smt2", true);
674 }
675
676 if(cmdline.isset("smt2") && !solver_set)
677 {
678 if(cmdline.isset("outfile"))
679 {
680 // outfile and no solver should give standard compliant SMT-LIB
681 options.set_option("generic", true);
682 }
683 else
684 {
685 // the default smt2 solver
686 options.set_option("z3", true);
687 }
688 }
689}
690
692{
693 parse_sat_options(cmdline, options);
694 parse_smt2_options(cmdline, options);
695
696 if(cmdline.isset("outfile"))
697 options.set_option("outfile", cmdline.get_value("outfile"));
698
699 if(cmdline.isset("dump-smt-formula"))
700 options.set_option(
701 "dump-smt-formula", cmdline.get_value("dump-smt-formula"));
702
703 if(cmdline.isset("write-solver-stats-to"))
704 {
705 options.set_option(
706 "write-solver-stats-to", cmdline.get_value("write-solver-stats-to"));
707 }
708
709 if(cmdline.isset("beautify"))
710 options.set_option("beautify", true);
711
712 if(cmdline.isset("refine-arrays"))
713 {
714 options.set_option("refine", true);
715 options.set_option("refine-arrays", true);
716 }
717
718 if(cmdline.isset("refine-arithmetic"))
719 {
720 options.set_option("refine", true);
721 options.set_option("refine-arithmetic", true);
722 }
723
724 if(cmdline.isset("refine"))
725 {
726 options.set_option("refine", true);
727 options.set_option("refine-arrays", true);
728 options.set_option("refine-arithmetic", true);
729 }
730
731 if(cmdline.isset("max-node-refinement"))
732 {
733 options.set_option(
734 "max-node-refinement", cmdline.get_value("max-node-refinement"));
735 }
736}
Writing DIMACS Files.
Abstraction Refinement Loop.
message_handlert & message_handler
Definition ai.h:521
virtual xmlt output_xml(const namespacet &ns, const irep_idt &function_id, const goto_programt &goto_program) const
Output the abstract states for a single function as XML.
Definition ai.cpp:136
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
std::string get_value(char option) const
Definition cmdline.cpp:48
virtual bool isset(char option) const
Definition cmdline.cpp:30
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
static eomt eom
Definition message.h:289
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
boolbvt & boolbv_decision_procedure() const
solvert(std::unique_ptr< stack_decision_proceduret > p)
stack_decision_proceduret & decision_procedure() const
message_handlert & message_handler
const optionst & options
const namespacet & ns
std::unique_ptr< solvert > get_external_sat()
std::unique_ptr< solvert > get_default()
solver_factoryt(const optionst &_options, const namespacet &_ns, message_handlert &_message_handler, bool _output_xml_in_refinement)
Note: The solver returned will hold a reference to the namespace ns.
void set_decision_procedure_time_limit(solver_resource_limitst &decision_procedure)
Sets the timeout of decision_procedure if the solver-time-limit option has a positive value (in secon...
std::unique_ptr< solvert > get_dimacs()
std::unique_ptr< solvert > get_string_refinement()
the string refinement adds to the bit vector refinement specifications for functions from the Java st...
std::unique_ptr< solvert > get_smt2(smt2_dect::solvert solver)
const bool output_xml_in_refinement
smt2_dect::solvert get_smt2_solver_type() const
Uses the options to pick an SMT 2.0 solver.
virtual std::unique_ptr< solvert > get_solver()
Returns a solvert object.
std::unique_ptr< solvert > get_incremental_smt2(std::string solver_command)
std::unique_ptr< solvert > get_bv_refinement()
virtual void set_time_limit_seconds(uint32_t)=0
Set the limit for the solver to time out in seconds.
Document and give macros for the exit codes of CPROVER binaries.
#define CPROVER_EXIT_USAGE_ERROR
A usage error is returned when the command line is invalid or conflicting.
Definition exit_codes.h:33
Allows calling an external SAT solver to allow faster integration of newer SAT solvers.
double log(double x)
Definition math.c:2449
STL namespace.
Options.
Decision procedure with incremental SMT2 solving.
void solver(std::vector< framet > &frames, const std::unordered_set< symbol_exprt, irep_hash > &address_taken, const solver_optionst &solver_options, const namespacet &ns, std::vector< propertyt > &properties, std::size_t property_index)
Definition solver.cpp:44
static std::enable_if<!std::is_base_of< hardness_collectort, SatcheckT >::value, std::unique_ptr< SatcheckT > >::type make_satcheck_prop(message_handlert &message_handler, const optionst &options)
static void parse_sat_options(const cmdlinet &cmdline, optionst &options)
static void parse_smt2_options(const cmdlinet &cmdline, optionst &options)
std::unique_ptr< std::ofstream > open_outfile_and_check(const std::string &filename, message_handlert &message_handler, const std::string &arg_name)
static std::unique_ptr< propt > get_sat_solver(message_handlert &message_handler, const optionst &options)
static void emit_solver_warning(message_handlert &message_handler, const std::string &solver)
Emit a warning for non-existent solver solver via message_handler.
Solver Factory.
void parse_solver_options(const cmdlinet &cmdline, optionst &options)
Parse solver-related command-line parameters in cmdline and set corresponding values in options.
Solver capability to set resource limits.
#define PRECONDITION(CONDITION)
Definition invariant.h:463
void exit(int status)
Definition stdlib.c:102
String support via creating string constraints and progressively instantiating the universal constrai...
#define DEFAULT_MAX_NB_REFINEMENT
string_refinementt constructor arguments
#define widen_if_needed(s)
Definition unicode.h:28
const char * CBMC_VERSION