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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/interface/Exceptions.h>
|
|
|
|
#include <libsolidity/interface/ReadFile.h>
|
|
|
|
|
|
|
|
#include <libdevcore/Common.h>
|
2017-07-13 19:06:29 +00:00
|
|
|
#include <libdevcore/Exceptions.h>
|
2017-07-13 16:22:51 +00:00
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace smt
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class CheckResult
|
|
|
|
{
|
2018-06-26 10:41:26 +00:00
|
|
|
SATISFIABLE, UNSATISFIABLE, UNKNOWN, CONFLICTING, ERROR
|
2017-07-13 16:22:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class Sort
|
|
|
|
{
|
2017-11-22 16:12:22 +00:00
|
|
|
Int,
|
|
|
|
Bool,
|
2018-01-21 12:58:56 +00:00
|
|
|
IntIntFun, // Function of one Int returning a single Int
|
|
|
|
IntBoolFun // Function of one Int returning a single Bool
|
2017-07-13 16:22:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// C++ representation of an SMTLIB2 expression.
|
|
|
|
class Expression
|
|
|
|
{
|
|
|
|
friend class SolverInterface;
|
|
|
|
public:
|
2017-11-22 14:20:26 +00:00
|
|
|
explicit Expression(bool _v): name(_v ? "true" : "false"), sort(Sort::Bool) {}
|
|
|
|
Expression(size_t _number): name(std::to_string(_number)), sort(Sort::Int) {}
|
|
|
|
Expression(u256 const& _number): name(_number.str()), sort(Sort::Int) {}
|
|
|
|
Expression(bigint const& _number): name(_number.str()), sort(Sort::Int) {}
|
2017-07-13 16:22:51 +00:00
|
|
|
|
2017-08-31 10:16:41 +00:00
|
|
|
Expression(Expression const&) = default;
|
|
|
|
Expression(Expression&&) = default;
|
2018-10-17 14:46:18 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
static std::map<std::string, unsigned> const operatorsArity{
|
|
|
|
{"ite", 3},
|
|
|
|
{"not", 1},
|
|
|
|
{"and", 2},
|
|
|
|
{"or", 2},
|
|
|
|
{"=", 2},
|
|
|
|
{"<", 2},
|
|
|
|
{"<=", 2},
|
|
|
|
{">", 2},
|
|
|
|
{">=", 2},
|
|
|
|
{"+", 2},
|
|
|
|
{"-", 2},
|
|
|
|
{"*", 2},
|
|
|
|
{"/", 2}
|
|
|
|
};
|
|
|
|
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)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
solAssert(_trueValue.sort == _falseValue.sort, "");
|
2017-07-14 09:49:27 +00:00
|
|
|
return Expression("ite", std::vector<Expression>{
|
|
|
|
std::move(_condition), std::move(_trueValue), std::move(_falseValue)
|
2017-11-22 14:20:26 +00:00
|
|
|
}, _trueValue.sort);
|
2017-07-14 09:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:09:17 +00:00
|
|
|
static Expression implies(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
return !std::move(_a) || std::move(_b);
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:22:51 +00:00
|
|
|
friend Expression operator!(Expression _a)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("not", std::move(_a), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator&&(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("and", std::move(_a), std::move(_b), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator||(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("or", std::move(_a), std::move(_b), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator==(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("=", std::move(_a), std::move(_b), Sort::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)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("<", std::move(_a), std::move(_b), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator<=(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("<=", std::move(_a), std::move(_b), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator>(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression(">", std::move(_a), std::move(_b), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator>=(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression(">=", std::move(_a), std::move(_b), Sort::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator+(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("+", std::move(_a), std::move(_b), Sort::Int);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator-(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("-", std::move(_a), std::move(_b), Sort::Int);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator*(Expression _a, Expression _b)
|
|
|
|
{
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression("*", std::move(_a), std::move(_b), Sort::Int);
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
friend Expression operator/(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
return Expression("/", std::move(_a), std::move(_b), Sort::Int);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
Expression operator()(Expression _a) const
|
|
|
|
{
|
2017-11-22 16:12:22 +00:00
|
|
|
solAssert(
|
2018-03-09 15:19:03 +00:00
|
|
|
arguments.empty(),
|
2017-11-22 16:12:22 +00:00
|
|
|
"Attempted function application to non-function."
|
|
|
|
);
|
2018-03-09 15:19:03 +00:00
|
|
|
switch (sort)
|
|
|
|
{
|
|
|
|
case Sort::IntIntFun:
|
2018-01-21 12:58:56 +00:00
|
|
|
return Expression(name, _a, Sort::Int);
|
2018-03-09 15:19:03 +00:00
|
|
|
case Sort::IntBoolFun:
|
|
|
|
return Expression(name, _a, Sort::Bool);
|
|
|
|
default:
|
|
|
|
solAssert(
|
|
|
|
false,
|
|
|
|
"Attempted function application to invalid type."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 14:46:18 +00:00
|
|
|
std::string name;
|
|
|
|
std::vector<Expression> arguments;
|
2017-11-22 14:20:26 +00:00
|
|
|
Sort sort;
|
2017-07-13 16:22:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Manual constructor, should only be used by SolverInterface and this class itself.
|
2017-11-22 14:20:26 +00:00
|
|
|
Expression(std::string _name, std::vector<Expression> _arguments, Sort _sort):
|
|
|
|
name(std::move(_name)), arguments(std::move(_arguments)), sort(_sort) {}
|
|
|
|
|
|
|
|
explicit Expression(std::string _name, Sort _sort):
|
|
|
|
Expression(std::move(_name), std::vector<Expression>{}, _sort) {}
|
|
|
|
Expression(std::string _name, Expression _arg, Sort _sort):
|
|
|
|
Expression(std::move(_name), std::vector<Expression>{std::move(_arg)}, _sort) {}
|
|
|
|
Expression(std::string _name, Expression _arg1, Expression _arg2, Sort _sort):
|
|
|
|
Expression(std::move(_name), std::vector<Expression>{std::move(_arg1), std::move(_arg2)}, _sort) {}
|
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:
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~SolverInterface() = default;
|
2017-07-13 16:22:51 +00:00
|
|
|
virtual void reset() = 0;
|
|
|
|
|
|
|
|
virtual void push() = 0;
|
|
|
|
virtual void pop() = 0;
|
|
|
|
|
2018-04-20 14:56:10 +00:00
|
|
|
virtual void declareFunction(std::string _name, Sort _domain, Sort _codomain) = 0;
|
|
|
|
Expression newFunction(std::string _name, Sort _domain, Sort _codomain)
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
2018-04-20 14:56:10 +00:00
|
|
|
declareFunction(_name, _domain, _codomain);
|
2018-03-09 15:19:03 +00:00
|
|
|
solAssert(_domain == Sort::Int, "Function sort not supported.");
|
2017-07-13 16:22:51 +00:00
|
|
|
// Subclasses should do something here
|
2018-03-09 15:19:03 +00:00
|
|
|
switch (_codomain)
|
|
|
|
{
|
|
|
|
case Sort::Int:
|
2018-01-21 12:58:56 +00:00
|
|
|
return Expression(std::move(_name), {}, Sort::IntIntFun);
|
2018-03-09 15:19:03 +00:00
|
|
|
case Sort::Bool:
|
|
|
|
return Expression(std::move(_name), {}, Sort::IntBoolFun);
|
|
|
|
default:
|
|
|
|
solAssert(false, "Function sort not supported.");
|
|
|
|
break;
|
|
|
|
}
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
2018-04-20 14:56:10 +00:00
|
|
|
virtual void declareInteger(std::string _name) = 0;
|
|
|
|
Expression newInteger(std::string _name)
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
|
|
|
// Subclasses should do something here
|
2018-04-20 14:56:10 +00:00
|
|
|
declareInteger(_name);
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression(std::move(_name), {}, Sort::Int);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
2018-04-20 14:56:10 +00:00
|
|
|
virtual void declareBool(std::string _name) = 0;
|
|
|
|
Expression newBool(std::string _name)
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
|
|
|
// Subclasses should do something here
|
2018-04-20 14:56:10 +00:00
|
|
|
declareBool(_name);
|
2017-11-22 14:20:26 +00:00
|
|
|
return Expression(std::move(_name), {}, Sort::Bool);
|
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;
|
|
|
|
|
2018-07-27 07:14:50 +00:00
|
|
|
protected:
|
|
|
|
// SMT query timeout in milliseconds.
|
|
|
|
static int const queryTimeout = 10000;
|
|
|
|
};
|
2017-07-13 16:22:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|