solidity/libsmtutil/SolverInterface.h

556 lines
17 KiB
C
Raw Permalink Normal View History

2017-07-13 16:22:51 +00:00
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2017-07-13 16:22:51 +00:00
#pragma once
2020-05-19 12:52:31 +00:00
#include <libsmtutil/Exceptions.h>
2020-05-18 15:42:24 +00:00
#include <libsmtutil/Sorts.h>
2020-03-24 19:51:52 +00:00
#include <libsolutil/Common.h>
2021-09-16 14:33:28 +00:00
#include <libsolutil/Numeric.h>
#include <libsolutil/CommonData.h>
2017-07-13 16:22:51 +00:00
#include <range/v3/algorithm/all_of.hpp>
2021-05-19 15:35:19 +00:00
#include <range/v3/view.hpp>
2018-12-17 17:26:10 +00:00
#include <cstdio>
2017-07-13 16:22:51 +00:00
#include <map>
2020-04-14 09:09:38 +00:00
#include <memory>
#include <optional>
2021-05-19 15:35:19 +00:00
#include <set>
2017-07-13 16:22:51 +00:00
#include <string>
#include <vector>
2020-05-19 12:14:46 +00:00
namespace solidity::smtutil
2017-07-13 16:22:51 +00:00
{
struct SMTSolverChoice
{
bool cvc4 = false;
bool eld = false;
2021-05-19 15:35:19 +00:00
bool smtlib2 = false;
bool z3 = false;
static constexpr SMTSolverChoice All() noexcept { return {true, true, true, true}; }
static constexpr SMTSolverChoice CVC4() noexcept { return {true, false, false, false}; }
static constexpr SMTSolverChoice ELD() noexcept { return {false, true, false, false}; }
static constexpr SMTSolverChoice SMTLIB2() noexcept { return {false, false, true, false}; }
static constexpr SMTSolverChoice Z3() noexcept { return {false, false, false, true}; }
static constexpr SMTSolverChoice None() noexcept { return {false, false, false, false}; }
2021-05-19 15:35:19 +00:00
static std::optional<SMTSolverChoice> fromString(std::string const& _solvers)
{
SMTSolverChoice solvers;
for (auto&& s: _solvers | ranges::views::split(',') | ranges::to<std::vector<std::string>>())
if (!solvers.setSolver(s))
return {};
2021-05-19 15:35:19 +00:00
return solvers;
}
SMTSolverChoice& operator&=(SMTSolverChoice const& _other)
2021-05-19 15:35:19 +00:00
{
cvc4 &= _other.cvc4;
eld &= _other.eld;
2021-05-19 15:35:19 +00:00
smtlib2 &= _other.smtlib2;
z3 &= _other.z3;
return *this;
}
SMTSolverChoice operator&(SMTSolverChoice _other) const noexcept
2021-05-19 15:35:19 +00:00
{
_other &= *this;
return _other;
2021-05-19 15:35:19 +00:00
}
bool operator!=(SMTSolverChoice const& _other) const noexcept { return !(*this == _other); }
bool operator==(SMTSolverChoice const& _other) const noexcept
{
return cvc4 == _other.cvc4 &&
eld == _other.eld &&
2021-05-19 15:35:19 +00:00
smtlib2 == _other.smtlib2 &&
z3 == _other.z3;
}
bool setSolver(std::string const& _solver)
{
static std::set<std::string> const solvers{"cvc4", "eld", "smtlib2", "z3"};
2021-05-19 15:35:19 +00:00
if (!solvers.count(_solver))
return false;
if (_solver == "cvc4")
cvc4 = true;
if (_solver == "eld")
eld = true;
2021-05-19 15:35:19 +00:00
else if (_solver == "smtlib2")
smtlib2 = true;
else if (_solver == "z3")
z3 = true;
return true;
}
bool none() const noexcept { return !some(); }
bool some() const noexcept { return cvc4 || eld || smtlib2 || z3; }
bool all() const noexcept { return cvc4 && eld && smtlib2 && z3; }
};
2017-07-13 16:22:51 +00:00
enum class CheckResult
{
SATISFIABLE, UNSATISFIABLE, UNKNOWN, CONFLICTING, ERROR
2017-07-13 16:22:51 +00:00
};
/// C++ representation of an SMTLIB2 expression.
class Expression
{
friend class SolverInterface;
public:
explicit Expression(bool _v): Expression(_v ? "true" : "false", Kind::Bool) {}
2020-05-19 12:23:41 +00:00
explicit Expression(std::shared_ptr<SortSort> _sort, std::string _name = ""): Expression(std::move(_name), {}, _sort) {}
explicit Expression(std::string _name, std::vector<Expression> _arguments, SortPointer _sort):
name(std::move(_name)), arguments(std::move(_arguments)), sort(std::move(_sort)) {}
Expression(size_t _number): Expression(std::to_string(_number), {}, SortProvider::uintSort) {}
Expression(u256 const& _number): Expression(_number.str(), {}, SortProvider::sintSort) {}
2021-05-20 16:07:40 +00:00
Expression(s256 const& _number): Expression(
_number >= 0 ? _number.str() : "-",
_number >= 0 ?
std::vector<Expression>{} :
std::vector<Expression>{Expression(size_t(0)), bigint(-_number)},
SortProvider::sintSort
) {}
Expression(bigint const& _number): Expression(
_number >= 0 ? _number.str() : "-",
_number >= 0 ?
std::vector<Expression>{} :
std::vector<Expression>{Expression(size_t(0)), bigint(-_number)},
SortProvider::sintSort
) {}
2017-07-13 16:22:51 +00:00
Expression(Expression const&) = default;
Expression(Expression&&) = default;
Expression& operator=(Expression const&) = default;
Expression& operator=(Expression&&) = default;
2017-07-13 16:22:51 +00:00
2018-04-06 16:01:40 +00:00
bool hasCorrectArity() const
{
2020-04-14 09:09:38 +00:00
if (name == "tuple_constructor")
{
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(sort);
2020-05-19 12:52:31 +00:00
smtAssert(tupleSort, "");
2020-04-14 09:09:38 +00:00
return arguments.size() == tupleSort->components.size();
}
2018-04-06 16:01:40 +00:00
static std::map<std::string, unsigned> const operatorsArity{
{"ite", 3},
{"not", 1},
{"and", 2},
{"or", 2},
2021-05-18 17:15:08 +00:00
{"=>", 2},
2018-04-06 16:01:40 +00:00
{"=", 2},
{"<", 2},
{"<=", 2},
{">", 2},
{">=", 2},
{"+", 2},
{"-", 2},
{"*", 2},
{"div", 2},
2019-01-22 13:34:29 +00:00
{"mod", 2},
{"bvnot", 1},
2020-05-13 11:08:48 +00:00
{"bvand", 2},
{"bvor", 2},
{"bvxor", 2},
2020-09-04 14:55:45 +00:00
{"bvshl", 2},
{"bvlshr", 2},
{"bvashr", 2},
2020-05-13 11:08:48 +00:00
{"int2bv", 2},
{"bv2int", 1},
{"select", 2},
2019-08-22 13:44:30 +00:00
{"store", 3},
2020-04-03 13:10:16 +00:00
{"const_array", 2},
{"tuple_get", 2}
2018-04-06 16:01:40 +00:00
};
return operatorsArity.count(name) && operatorsArity.at(name) == arguments.size();
}
2017-07-14 09:49:27 +00:00
static Expression ite(Expression _condition, Expression _trueValue, Expression _falseValue)
{
if (_trueValue.sort->kind == Kind::Int)
smtAssert(_trueValue.sort->kind == _falseValue.sort->kind, "");
else
smtAssert(*_trueValue.sort == *_falseValue.sort, "");
2018-11-29 14:32:38 +00:00
SortPointer sort = _trueValue.sort;
2017-07-14 09:49:27 +00:00
return Expression("ite", std::vector<Expression>{
std::move(_condition), std::move(_trueValue), std::move(_falseValue)
2018-11-29 14:32:38 +00:00
}, std::move(sort));
2017-07-14 09:49:27 +00:00
}
static Expression implies(Expression _a, Expression _b)
{
return Expression(
2021-05-18 17:15:08 +00:00
"=>",
std::move(_a),
std::move(_b),
Kind::Bool
);
}
/// select is the SMT representation of an array index access.
static Expression select(Expression _array, Expression _index)
{
2020-05-19 12:52:31 +00:00
smtAssert(_array.sort->kind == Kind::Array, "");
2018-11-09 16:06:30 +00:00
std::shared_ptr<ArraySort> arraySort = std::dynamic_pointer_cast<ArraySort>(_array.sort);
2020-05-19 12:52:31 +00:00
smtAssert(arraySort, "");
smtAssert(_index.sort, "");
if (arraySort->domain->kind == Kind::Int)
smtAssert(arraySort->domain->kind == _index.sort->kind, "");
else
smtAssert(*arraySort->domain == *_index.sort, "");
return Expression(
"select",
std::vector<Expression>{std::move(_array), std::move(_index)},
arraySort->range
);
}
/// store is the SMT representation of an assignment to array index.
/// The function is pure and returns the modified array.
static Expression store(Expression _array, Expression _index, Expression _element)
{
2020-04-14 09:09:38 +00:00
auto arraySort = std::dynamic_pointer_cast<ArraySort>(_array.sort);
2020-05-19 12:52:31 +00:00
smtAssert(arraySort, "");
smtAssert(_index.sort, "");
smtAssert(_element.sort, "");
smtAssert(*arraySort->domain == *_index.sort, "");
if (arraySort->domain->kind == Kind::Int)
smtAssert(arraySort->range->kind == _element.sort->kind, "");
else
smtAssert(*arraySort->range == *_element.sort, "");
return Expression(
"store",
std::vector<Expression>{std::move(_array), std::move(_index), std::move(_element)},
2018-11-09 16:06:30 +00:00
arraySort
);
}
2019-08-22 13:44:30 +00:00
static Expression const_array(Expression _sort, Expression _value)
{
2020-05-19 12:52:31 +00:00
smtAssert(_sort.sort->kind == Kind::Sort, "");
2019-08-22 13:44:30 +00:00
auto sortSort = std::dynamic_pointer_cast<SortSort>(_sort.sort);
auto arraySort = std::dynamic_pointer_cast<ArraySort>(sortSort->inner);
2020-05-19 12:52:31 +00:00
smtAssert(sortSort && arraySort, "");
smtAssert(_value.sort, "");
if (arraySort->domain->kind == Kind::Int)
smtAssert(arraySort->range->kind == _value.sort->kind, "");
else
smtAssert(*arraySort->range == *_value.sort, "");
2019-08-22 13:44:30 +00:00
return Expression(
"const_array",
std::vector<Expression>{std::move(_sort), std::move(_value)},
arraySort
);
}
2020-04-03 13:10:16 +00:00
static Expression tuple_get(Expression _tuple, size_t _index)
{
2020-05-19 12:52:31 +00:00
smtAssert(_tuple.sort->kind == Kind::Tuple, "");
2020-04-03 13:10:16 +00:00
std::shared_ptr<TupleSort> tupleSort = std::dynamic_pointer_cast<TupleSort>(_tuple.sort);
2020-05-19 12:52:31 +00:00
smtAssert(tupleSort, "");
smtAssert(_index < tupleSort->components.size(), "");
2020-04-03 13:10:16 +00:00
return Expression(
"tuple_get",
std::vector<Expression>{std::move(_tuple), Expression(_index)},
tupleSort->components.at(_index)
);
}
2020-04-14 09:09:38 +00:00
static Expression tuple_constructor(Expression _tuple, std::vector<Expression> _arguments)
{
2020-05-19 12:52:31 +00:00
smtAssert(_tuple.sort->kind == Kind::Sort, "");
2020-04-14 09:09:38 +00:00
auto sortSort = std::dynamic_pointer_cast<SortSort>(_tuple.sort);
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(sortSort->inner);
2020-05-19 12:52:31 +00:00
smtAssert(tupleSort, "");
smtAssert(_arguments.size() == tupleSort->components.size(), "");
2020-04-14 09:09:38 +00:00
return Expression(
"tuple_constructor",
std::move(_arguments),
tupleSort
);
}
2020-05-13 11:08:48 +00:00
static Expression int2bv(Expression _n, size_t _size)
{
smtAssert(_n.sort->kind == Kind::Int, "");
std::shared_ptr<IntSort> intSort = std::dynamic_pointer_cast<IntSort>(_n.sort);
smtAssert(intSort, "");
smtAssert(_size <= 256, "");
return Expression(
"int2bv",
std::vector<Expression>{std::move(_n), Expression(_size)},
std::make_shared<BitVectorSort>(_size)
);
}
static Expression bv2int(Expression _bv, bool _signed = false)
{
smtAssert(_bv.sort->kind == Kind::BitVector, "");
std::shared_ptr<BitVectorSort> bvSort = std::dynamic_pointer_cast<BitVectorSort>(_bv.sort);
smtAssert(bvSort, "");
smtAssert(bvSort->size <= 256, "");
return Expression(
"bv2int",
std::vector<Expression>{std::move(_bv)},
SortProvider::intSort(_signed)
);
}
static bool sameSort(std::vector<Expression> const& _args)
{
if (_args.empty())
return true;
auto sort = _args.front().sort;
return ranges::all_of(
_args,
[&](auto const& _expr){ return _expr.sort->kind == sort->kind; }
);
}
static Expression mkAnd(std::vector<Expression> _args)
{
smtAssert(!_args.empty(), "");
smtAssert(sameSort(_args), "");
auto sort = _args.front().sort;
if (sort->kind == Kind::BitVector)
return Expression("bvand", std::move(_args), sort);
smtAssert(sort->kind == Kind::Bool, "");
return Expression("and", std::move(_args), Kind::Bool);
}
static Expression mkOr(std::vector<Expression> _args)
{
smtAssert(!_args.empty(), "");
smtAssert(sameSort(_args), "");
auto sort = _args.front().sort;
if (sort->kind == Kind::BitVector)
return Expression("bvor", std::move(_args), sort);
smtAssert(sort->kind == Kind::Bool, "");
return Expression("or", std::move(_args), Kind::Bool);
}
static Expression mkPlus(std::vector<Expression> _args)
{
smtAssert(!_args.empty(), "");
smtAssert(sameSort(_args), "");
auto sort = _args.front().sort;
smtAssert(sort->kind == Kind::BitVector || sort->kind == Kind::Int, "");
return Expression("+", std::move(_args), sort);
}
static Expression mkMul(std::vector<Expression> _args)
{
smtAssert(!_args.empty(), "");
smtAssert(sameSort(_args), "");
auto sort = _args.front().sort;
smtAssert(sort->kind == Kind::BitVector || sort->kind == Kind::Int, "");
return Expression("*", std::move(_args), sort);
}
2017-07-13 16:22:51 +00:00
friend Expression operator!(Expression _a)
{
if (_a.sort->kind == Kind::BitVector)
return ~_a;
return Expression("not", std::move(_a), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator&&(Expression _a, Expression _b)
{
if (_a.sort->kind == Kind::BitVector)
{
smtAssert(_b.sort->kind == Kind::BitVector, "");
return _a & _b;
}
return Expression("and", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator||(Expression _a, Expression _b)
{
if (_a.sort->kind == Kind::BitVector)
{
smtAssert(_b.sort->kind == Kind::BitVector, "");
return _a | _b;
}
return Expression("or", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator==(Expression _a, Expression _b)
{
smtAssert(_a.sort->kind == _b.sort->kind, "Trying to create an 'equal' expression with different sorts");
return Expression("=", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator!=(Expression _a, Expression _b)
{
return !(std::move(_a) == std::move(_b));
}
friend Expression operator<(Expression _a, Expression _b)
{
return Expression("<", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator<=(Expression _a, Expression _b)
{
return Expression("<=", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator>(Expression _a, Expression _b)
{
return Expression(">", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator>=(Expression _a, Expression _b)
{
return Expression(">=", std::move(_a), std::move(_b), Kind::Bool);
2017-07-13 16:22:51 +00:00
}
friend Expression operator+(Expression _a, Expression _b)
{
auto intSort = _a.sort;
return Expression("+", {std::move(_a), std::move(_b)}, intSort);
2017-07-13 16:22:51 +00:00
}
friend Expression operator-(Expression _a, Expression _b)
{
auto intSort = _a.sort;
return Expression("-", {std::move(_a), std::move(_b)}, intSort);
2017-07-13 16:22:51 +00:00
}
friend Expression operator*(Expression _a, Expression _b)
{
auto intSort = _a.sort;
return Expression("*", {std::move(_a), std::move(_b)}, intSort);
2017-10-05 13:23:25 +00:00
}
friend Expression operator/(Expression _a, Expression _b)
{
auto intSort = _a.sort;
return Expression("div", {std::move(_a), std::move(_b)}, intSort);
2017-07-13 16:22:51 +00:00
}
2019-01-22 13:34:29 +00:00
friend Expression operator%(Expression _a, Expression _b)
{
auto intSort = _a.sort;
return Expression("mod", {std::move(_a), std::move(_b)}, intSort);
2019-01-22 13:34:29 +00:00
}
friend Expression operator~(Expression _a)
{
auto bvSort = _a.sort;
return Expression("bvnot", {std::move(_a)}, bvSort);
}
2020-05-13 11:08:48 +00:00
friend Expression operator&(Expression _a, Expression _b)
{
auto bvSort = _a.sort;
return Expression("bvand", {std::move(_a), std::move(_b)}, bvSort);
}
2020-09-04 14:55:45 +00:00
friend Expression operator|(Expression _a, Expression _b)
{
auto bvSort = _a.sort;
return Expression("bvor", {std::move(_a), std::move(_b)}, bvSort);
}
friend Expression operator^(Expression _a, Expression _b)
{
auto bvSort = _a.sort;
return Expression("bvxor", {std::move(_a), std::move(_b)}, bvSort);
}
2020-09-04 14:55:45 +00:00
friend Expression operator<<(Expression _a, Expression _b)
{
auto bvSort = _a.sort;
2020-09-04 14:55:45 +00:00
return Expression("bvshl", {std::move(_a), std::move(_b)}, bvSort);
}
friend Expression operator>>(Expression _a, Expression _b)
{
auto bvSort = _a.sort;
return Expression("bvlshr", {std::move(_a), std::move(_b)}, bvSort);
}
static Expression ashr(Expression _a, Expression _b)
{
auto bvSort = _a.sort;
return Expression("bvashr", {std::move(_a), std::move(_b)}, bvSort);
}
Expression operator()(std::vector<Expression> _arguments) const
2017-07-13 16:22:51 +00:00
{
2020-05-19 12:52:31 +00:00
smtAssert(
sort->kind == Kind::Function,
2017-11-22 16:12:22 +00:00
"Attempted function application to non-function."
);
auto fSort = dynamic_cast<FunctionSort const*>(sort.get());
2020-05-19 12:52:31 +00:00
smtAssert(fSort, "");
return Expression(name, std::move(_arguments), fSort->codomain);
2017-07-13 16:22:51 +00:00
}
std::string name;
std::vector<Expression> arguments;
SortPointer sort;
2017-07-13 16:22:51 +00:00
private:
/// Manual constructors, should only be used by SolverInterface and this class itself.
Expression(std::string _name, std::vector<Expression> _arguments, Kind _kind):
Expression(std::move(_name), std::move(_arguments), std::make_shared<Sort>(_kind)) {}
explicit Expression(std::string _name, Kind _kind):
Expression(std::move(_name), std::vector<Expression>{}, _kind) {}
Expression(std::string _name, Expression _arg, Kind _kind):
Expression(std::move(_name), std::vector<Expression>{std::move(_arg)}, _kind) {}
Expression(std::string _name, Expression _arg1, Expression _arg2, Kind _kind):
Expression(std::move(_name), std::vector<Expression>{std::move(_arg1), std::move(_arg2)}, _kind) {}
2017-07-13 16:22:51 +00:00
};
2017-07-13 19:06:29 +00:00
DEV_SIMPLE_EXCEPTION(SolverError);
2017-07-13 16:22:51 +00:00
class SolverInterface
{
public:
2020-11-02 20:20:20 +00:00
SolverInterface(std::optional<unsigned> _queryTimeout = {}): m_queryTimeout(_queryTimeout) {}
virtual ~SolverInterface() = default;
2017-07-13 16:22:51 +00:00
virtual void reset() = 0;
virtual void push() = 0;
virtual void pop() = 0;
2019-09-24 15:35:31 +00:00
virtual void declareVariable(std::string const& _name, SortPointer const& _sort) = 0;
Expression newVariable(std::string _name, SortPointer const& _sort)
2017-07-13 16:22:51 +00:00
{
// Subclasses should do something here
2020-05-19 12:52:31 +00:00
smtAssert(_sort, "");
2019-09-24 15:35:31 +00:00
declareVariable(_name, _sort);
return Expression(std::move(_name), {}, _sort);
2017-07-13 16:22:51 +00:00
}
virtual void addAssertion(Expression const& _expr) = 0;
2017-07-13 19:06:29 +00:00
/// Checks for satisfiability, evaluates the expressions if a model
/// is available. Throws SMTSolverError on error.
2017-07-13 16:22:51 +00:00
virtual std::pair<CheckResult, std::vector<std::string>>
check(std::vector<Expression> const& _expressionsToEvaluate) = 0;
/// @returns a list of queries that the system was not able to respond to.
virtual std::vector<std::string> unhandledQueries() { return {}; }
/// @returns how many SMT solvers this interface has.
2020-07-13 20:40:33 +00:00
virtual size_t solvers() { return 1; }
2020-11-02 20:20:20 +00:00
protected:
std::optional<unsigned> m_queryTimeout;
2018-07-27 07:14:50 +00:00
};
2017-07-13 16:22:51 +00:00
}