-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple solver backends #142
Draft
Boyan-MILANOV
wants to merge
6
commits into
dev-next
Choose a base branch
from
dev-multiple-solvers
base: dev-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5921058
Support exporting constraints to smtlibv2
Boyan-MILANOV bb56dd4
Merge branch 'dev-next' into dev-multiple-solvers
Boyan-MILANOV 5111d37
Add boolector support in cmake
Boyan-MILANOV 327d6da
Add class for boolector solver
Boyan-MILANOV 1edf640
WIP boolector support
Boyan-MILANOV e6ec979
WIP boolector support
Boyan-MILANOV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -81,6 +81,12 @@ if(maat_USE_Z3) | |||||
target_compile_definitions(maat_python PRIVATE MAAT_Z3_BACKEND=1 MAAT_HAS_SOLVER_BACKEND=1) | ||||||
endif() | ||||||
|
||||||
if(maat_USE_BOOLECTOR) | ||||||
find_package(Boolector) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably also here?
Suggested change
|
||||||
target_link_libraries(maat_python PRIVATE Boolector::boolector) | ||||||
target_compile_definitions(maat_python PUBLIC MAAT_BOOLECTOR_BACKEND=1) | ||||||
endif() | ||||||
|
||||||
if(maat_USE_LIEF) | ||||||
target_link_libraries(maat_python PRIVATE LIEF::LIEF) | ||||||
target_compile_definitions(maat_python PRIVATE MAAT_LIEF_BACKEND=1 MAAT_HAS_LOADER_BACKEND=1) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
#ifdef MAAT_BOOLECTOR_BACKEND | ||
|
||
#include "maat/solver.hpp" | ||
#include "maat/stats.hpp" | ||
#include "maat/exception.hpp" | ||
#include <fstream> | ||
|
||
namespace maat | ||
{ | ||
namespace solver | ||
{ | ||
|
||
SolverBtor::SolverBtor(): | ||
Solver(), | ||
has_model(false), | ||
btor(nullptr), | ||
model_file(nullptr) | ||
{ | ||
reset_btor(); | ||
} | ||
|
||
SolverBtor::~SolverBtor() | ||
{ | ||
boolector_delete(btor); | ||
btor = nullptr; | ||
} | ||
|
||
void SolverBtor::reset_btor() | ||
{ | ||
if (btor != nullptr) | ||
{ | ||
// TODO(boyan) | ||
// DEBUG: are these calls useful ? | ||
boolector_reset_assumptions(btor); | ||
boolector_release_all(btor); | ||
boolector_delete(btor); | ||
} | ||
btor = boolector_new(); | ||
boolector_set_opt (btor, BTOR_OPT_MODEL_GEN, 1); | ||
boolector_set_opt (btor, BTOR_OPT_INCREMENTAL, 1); | ||
} | ||
|
||
void SolverBtor::reset() | ||
{ | ||
constraints.clear(); | ||
has_model = false; | ||
_did_time_out = false; | ||
reset_btor(); | ||
} | ||
|
||
void SolverBtor::add(const Constraint& constr) | ||
{ | ||
constraints.push_back(constr); | ||
has_model = false; | ||
} | ||
|
||
void SolverBtor::pop() | ||
{ | ||
constraints.pop_back(); | ||
} | ||
|
||
bool SolverBtor::check() | ||
{ | ||
// If already has model, don't recompute it | ||
if (has_model) | ||
return true; | ||
|
||
reset_btor(); | ||
|
||
// Statistics | ||
MaatStats::instance().start_solving(); | ||
|
||
// Dump constraints to temporary file | ||
// TODO(boyan): use mkstemp instead of tmpnam | ||
const char *smt_file = tmpnam(NULL); // Get temp name | ||
if (smt_file == nullptr) | ||
throw solver_exception("SolverBtor::check(): couldn't create temporary filename"); | ||
std::ofstream f(smt_file); | ||
if (not f.good()) | ||
throw solver_exception("SolverBtor::check(): couldn't create temporary SMT file"); | ||
std::string smt_string = constraints_to_smt2(constraints); | ||
f << smt_string; | ||
f.close(); | ||
|
||
std::cout << "DEBUG SMT QUERY iN " << smt_file << "\n"; | ||
|
||
// Load SMT in boolector | ||
// TODO(boyan): don't reopen smt_file | ||
// TODO(boyan): don't hardcode dev/null but make it dependent on | ||
// the platform, and find a solution for Windows | ||
FILE* infile = fopen(smt_file, "r"); | ||
FILE* outfile = fopen("/dev/null", "w"); | ||
char* error_msg; | ||
int _status; | ||
int status = boolector_parse_smt2( | ||
btor, | ||
infile, // infile | ||
smt_file, // infile_name | ||
outfile, // outfile | ||
&error_msg, // error_msg | ||
&_status // status | ||
); | ||
fclose(outfile); | ||
fclose(infile); | ||
|
||
// DEBUG | ||
// if (status == BOOLECTOR_PARSE_UNKNOWN) | ||
// status = boolector_sat(btor); | ||
if (status == BOOLECTOR_PARSE_UNKNOWN) | ||
throw solver_exception( | ||
Fmt() << "SolverBtor::check(): error solving SMT file: " | ||
<< smt_file >> Fmt::to_str | ||
); | ||
|
||
has_model = (status == BOOLECTOR_SAT); | ||
|
||
// Remove temporary file | ||
// DEBUG remove(smt_file); | ||
|
||
// Statistics | ||
MaatStats::instance().done_solving(); | ||
|
||
return has_model; | ||
} | ||
|
||
std::shared_ptr<VarContext> SolverBtor::get_model() | ||
{ | ||
return std::shared_ptr<VarContext>(_get_model_raw()); | ||
} | ||
|
||
VarContext* SolverBtor::_get_model_raw() | ||
{ | ||
if (not has_model) | ||
return nullptr; | ||
|
||
// TODO(boyan): don't hardcode | ||
if (model_file == nullptr) | ||
{ | ||
model_file = tmpnam(NULL); // Get temp name | ||
if (model_file == nullptr) | ||
throw solver_exception("SolverBtor::_get_model_raw(): couldn't create temporary filename"); | ||
} | ||
FILE* f = fopen(model_file, "w"); | ||
boolector_print_model(btor, "smt2", f); | ||
fclose(f); | ||
|
||
std::ifstream in(model_file, std::ios::binary); | ||
std::vector<char> data( | ||
(std::istreambuf_iterator<char>(in)), | ||
(std::istreambuf_iterator<char>()) | ||
); | ||
std::vector<char> clean_data{'(', 'm', 'o', 'd', 'e', 'l', '\n'}; | ||
for (int i = 2; i < data.size(); i++) | ||
{ | ||
// DEBUG remove false?? | ||
if ( | ||
false && i+2 < data.size() && data[i] == '\n' | ||
&& data[i+1] == ' ' && data[i+2] == ' ' | ||
){ | ||
clean_data.push_back('\n'); | ||
i += 2; | ||
} | ||
else | ||
clean_data.push_back(data[i]); | ||
} | ||
remove(model_file); | ||
ctx_from_smt2(clean_data.data()); | ||
} | ||
|
||
} // namespace solver | ||
} // namespace maat | ||
#endif // #ifdef MAAT_BOOLECTOR_BACKEND |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree :)