aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/formal/SMTLib2Interface.cpp
blob: a6c1f87c7f63f12e6e0ca7e0e17762d0317bdf77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <libsolidity/formal/SMTLib2Interface.h>

#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/interface/ReadFile.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/filesystem/operations.hpp>

#include <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>

using namespace std;
using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::smt;

SMTLib2Interface::SMTLib2Interface(ReadCallback::Callback const& _queryCallback):
    m_queryCallback(_queryCallback)
{
    reset();
}

void SMTLib2Interface::reset()
{
    m_accumulatedOutput.clear();
    m_accumulatedOutput.emplace_back();
    m_constants.clear();
    m_functions.clear();
    write("(set-option :produce-models true)");
    write("(set-logic QF_UFLIA)");
}

void SMTLib2Interface::push()
{
    m_accumulatedOutput.emplace_back();
}

void SMTLib2Interface::pop()
{
    solAssert(!m_accumulatedOutput.empty(), "");
    m_accumulatedOutput.pop_back();
}

void SMTLib2Interface::declareFunction(string _name, Sort _domain, Sort _codomain)
{
    // TODO Use domain and codomain as key as well
    if (!m_functions.count(_name))
    {
        m_functions.insert(_name);
        write(
            "(declare-fun |" +
            _name +
            "| (" +
            (_domain == Sort::Int ? "Int" : "Bool") +
            ") " +
            (_codomain == Sort::Int ? "Int" : "Bool") +
            ")"
        );
    }
}

void SMTLib2Interface::declareInteger(string _name)
{
    if (!m_constants.count(_name))
    {
        m_constants.insert(_name);
        write("(declare-const |" + _name + "| Int)");
    }
}

void SMTLib2Interface::declareBool(string _name)
{
    if (!m_constants.count(_name))
    {
        m_constants.insert(_name);
        write("(declare-const |" + _name + "| Bool)");
    }
}

void SMTLib2Interface::addAssertion(Expression const& _expr)
{
    write("(assert " + toSExpr(_expr) + ")");
}

pair<CheckResult, vector<string>> SMTLib2Interface::check(vector<Expression> const& _expressionsToEvaluate)
{
    string response = querySolver(
        boost::algorithm::join(m_accumulatedOutput, "\n") +
        checkSatAndGetValuesCommand(_expressionsToEvaluate)
    );

    CheckResult result;
    // TODO proper parsing
    if (boost::starts_with(response, "sat\n"))
        result = CheckResult::SATISFIABLE;
    else if (boost::starts_with(response, "unsat\n"))
        result = CheckResult::UNSATISFIABLE;
    else if (boost::starts_with(response, "unknown\n"))
        result = CheckResult::UNKNOWN;
    else
        result = CheckResult::ERROR;

    vector<string> values;
    if (result == CheckResult::SATISFIABLE && result != CheckResult::ERROR)
        values = parseValues(find(response.cbegin(), response.cend(), '\n'), response.cend());
    return make_pair(result, values);
}

string SMTLib2Interface::toSExpr(Expression const& _expr)
{
    if (_expr.arguments.empty())
        return _expr.name;
    std::string sexpr = "(" + _expr.name;
    for (auto const& arg: _expr.arguments)
        sexpr += " " + toSExpr(arg);
    sexpr += ")";
    return sexpr;
}

void SMTLib2Interface::write(string _data)
{
    solAssert(!m_accumulatedOutput.empty(), "");
    m_accumulatedOutput.back() += move(_data) + "\n";
}

string SMTLib2Interface::checkSatAndGetValuesCommand(vector<Expression> const& _expressionsToEvaluate)
{
    string command;
    if (_expressionsToEvaluate.empty())
        command = "(check-sat)\n";
    else
    {
        // TODO make sure these are unique
        for (size_t i = 0; i < _expressionsToEvaluate.size(); i++)
        {
            auto const& e = _expressionsToEvaluate.at(i);
            solAssert(e.sort == Sort::Int || e.sort == Sort::Bool, "Invalid sort for expression to evaluate.");
            command += "(declare-const |EVALEXPR_" + to_string(i) + "| " + (e.sort == Sort::Int ? "Int" : "Bool") + ")\n";
            command += "(assert (= |EVALEXPR_" + to_string(i) + "| " + toSExpr(e) + "))\n";
        }
        command += "(check-sat)\n";
        command += "(get-value (";
        for (size_t i = 0; i < _expressionsToEvaluate.size(); i++)
            command += "|EVALEXPR_" + to_string(i) + "| ";
        command += "))\n";
    }

    return command;
}

vector<string> SMTLib2Interface::parseValues(string::const_iterator _start, string::const_iterator _end)
{
    vector<string> values;
    while (_start < _end)
    {
        auto valStart = find(_start, _end, ' ');
        if (valStart < _end)
            ++valStart;
        auto valEnd = find(valStart, _end, ')');
        values.emplace_back(valStart, valEnd);
        _start = find(valEnd, _end, '(');
    }

    return values;
}

string SMTLib2Interface::querySolver(string const& _input)
{
    if (!m_queryCallback)
        BOOST_THROW_EXCEPTION(SolverError() << errinfo_comment("No SMT solver available."));

    ReadCallback::Result queryResult = m_queryCallback(_input);
    if (!queryResult.success)
        BOOST_THROW_EXCEPTION(SolverError() << errinfo_comment(queryResult.responseOrErrorMessage));
    return queryResult.responseOrErrorMessage;
}