/src/solidity/libsmtutil/SMTLib2Interface.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <libsmtutil/SolverInterface.h> |
22 | | |
23 | | #include <libsolidity/interface/ReadFile.h> |
24 | | |
25 | | #include <libsolutil/Common.h> |
26 | | #include <libsolutil/FixedHash.h> |
27 | | |
28 | | #include <cstdio> |
29 | | #include <map> |
30 | | #include <set> |
31 | | #include <string> |
32 | | #include <vector> |
33 | | |
34 | | namespace solidity::smtutil |
35 | | { |
36 | | |
37 | | class SMTLib2Interface: public SolverInterface |
38 | | { |
39 | | public: |
40 | | /// Noncopyable. |
41 | | SMTLib2Interface(SMTLib2Interface const&) = delete; |
42 | | SMTLib2Interface& operator=(SMTLib2Interface const&) = delete; |
43 | | |
44 | | explicit SMTLib2Interface( |
45 | | std::map<util::h256, std::string> _queryResponses = {}, |
46 | | frontend::ReadCallback::Callback _smtCallback = {}, |
47 | | std::optional<unsigned> _queryTimeout = {} |
48 | | ); |
49 | | |
50 | | void reset() override; |
51 | | |
52 | | void push() override; |
53 | | void pop() override; |
54 | | |
55 | | void declareVariable(std::string const&, SortPointer const&) override; |
56 | | |
57 | | void addAssertion(Expression const& _expr) override; |
58 | | std::pair<CheckResult, std::vector<std::string>> check(std::vector<Expression> const& _expressionsToEvaluate) override; |
59 | | |
60 | 0 | std::vector<std::string> unhandledQueries() override { return m_unhandledQueries; } |
61 | | |
62 | | // Used by CHCSmtLib2Interface |
63 | | std::string toSExpr(Expression const& _expr); |
64 | | std::string toSmtLibSort(Sort const& _sort); |
65 | | std::string toSmtLibSort(std::vector<SortPointer> const& _sort); |
66 | | |
67 | 0 | std::map<std::string, SortPointer> variables() { return m_variables; } |
68 | | |
69 | 0 | std::vector<std::pair<std::string, std::string>> const& userSorts() const { return m_userSorts; } |
70 | | |
71 | | private: |
72 | | void declareFunction(std::string const& _name, SortPointer const& _sort); |
73 | | |
74 | | void write(std::string _data); |
75 | | |
76 | | std::string checkSatAndGetValuesCommand(std::vector<Expression> const& _expressionsToEvaluate); |
77 | | std::vector<std::string> parseValues(std::string::const_iterator _start, std::string::const_iterator _end); |
78 | | |
79 | | /// Communicates with the solver via the callback. Throws SMTSolverError on error. |
80 | | std::string querySolver(std::string const& _input); |
81 | | |
82 | | std::vector<std::string> m_accumulatedOutput; |
83 | | std::map<std::string, SortPointer> m_variables; |
84 | | |
85 | | /// Each pair in this vector represents an SMTChecker created |
86 | | /// sort (a user sort), and the smtlib2 declaration of that sort. |
87 | | /// It needs to be a vector so that the declaration order is kept, |
88 | | /// otherwise solvers cannot parse the queries. |
89 | | std::vector<std::pair<std::string, std::string>> m_userSorts; |
90 | | |
91 | | std::map<util::h256, std::string> m_queryResponses; |
92 | | std::vector<std::string> m_unhandledQueries; |
93 | | |
94 | | frontend::ReadCallback::Callback m_smtCallback; |
95 | | }; |
96 | | |
97 | | } |