2018-04-06 16:01:40 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-04-06 16:01:40 +00:00
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/CVC4Interface.h>
|
2018-04-06 16:01:40 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
2018-04-06 16:01:40 +00:00
|
|
|
|
2020-05-13 11:08:48 +00:00
|
|
|
#include <cvc4/util/bitvector.h>
|
|
|
|
|
2018-04-06 16:01:40 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
2020-05-19 12:14:46 +00:00
|
|
|
using namespace solidity::smtutil;
|
2018-04-06 16:01:40 +00:00
|
|
|
|
|
|
|
CVC4Interface::CVC4Interface():
|
|
|
|
m_solver(&m_context)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVC4Interface::reset()
|
|
|
|
{
|
2018-11-21 15:57:02 +00:00
|
|
|
m_variables.clear();
|
2018-04-06 16:01:40 +00:00
|
|
|
m_solver.reset();
|
|
|
|
m_solver.setOption("produce-models", true);
|
2019-12-03 00:11:21 +00:00
|
|
|
m_solver.setResourceLimit(resourceLimit);
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVC4Interface::push()
|
|
|
|
{
|
|
|
|
m_solver.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVC4Interface::pop()
|
|
|
|
{
|
|
|
|
m_solver.pop();
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:35:31 +00:00
|
|
|
void CVC4Interface::declareVariable(string const& _name, SortPointer const& _sort)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort, "");
|
2019-09-24 15:35:31 +00:00
|
|
|
m_variables[_name] = m_context.mkVar(_name.c_str(), cvc4Sort(*_sort));
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVC4Interface::addAssertion(Expression const& _expr)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_solver.assertFormula(toCVC4Expr(_expr));
|
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
catch (CVC4::TypeCheckingException const& _e)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
catch (CVC4::LogicException const& _e)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
catch (CVC4::UnsafeInterruptException const& _e)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2019-06-05 09:51:43 +00:00
|
|
|
}
|
|
|
|
catch (CVC4::Exception const& _e)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pair<CheckResult, vector<string>> CVC4Interface::check(vector<Expression> const& _expressionsToEvaluate)
|
|
|
|
{
|
|
|
|
CheckResult result;
|
|
|
|
vector<string> values;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
switch (m_solver.checkSat().isSat())
|
|
|
|
{
|
|
|
|
case CVC4::Result::SAT:
|
|
|
|
result = CheckResult::SATISFIABLE;
|
|
|
|
break;
|
|
|
|
case CVC4::Result::UNSAT:
|
|
|
|
result = CheckResult::UNSATISFIABLE;
|
|
|
|
break;
|
|
|
|
case CVC4::Result::SAT_UNKNOWN:
|
|
|
|
result = CheckResult::UNKNOWN;
|
|
|
|
break;
|
|
|
|
default:
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 12:13:22 +00:00
|
|
|
if (result == CheckResult::SATISFIABLE && !_expressionsToEvaluate.empty())
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
|
|
|
for (Expression const& e: _expressionsToEvaluate)
|
|
|
|
values.push_back(toString(m_solver.getValue(toCVC4Expr(e))));
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 12:57:21 +00:00
|
|
|
catch (CVC4::Exception const&)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
|
|
|
result = CheckResult::ERROR;
|
|
|
|
values.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_pair(result, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
CVC4::Expr CVC4Interface::toCVC4Expr(Expression const& _expr)
|
|
|
|
{
|
2018-11-21 15:57:02 +00:00
|
|
|
// Variable
|
|
|
|
if (_expr.arguments.empty() && m_variables.count(_expr.name))
|
|
|
|
return m_variables.at(_expr.name);
|
|
|
|
|
2018-04-06 16:01:40 +00:00
|
|
|
vector<CVC4::Expr> arguments;
|
|
|
|
for (auto const& arg: _expr.arguments)
|
|
|
|
arguments.push_back(toCVC4Expr(arg));
|
|
|
|
|
2019-06-05 09:51:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
string const& n = _expr.name;
|
|
|
|
// Function application
|
|
|
|
if (!arguments.empty() && m_variables.count(_expr.name))
|
|
|
|
return m_context.mkExpr(CVC4::kind::APPLY_UF, m_variables.at(n), arguments);
|
|
|
|
// Literal
|
|
|
|
else if (arguments.empty())
|
|
|
|
{
|
|
|
|
if (n == "true")
|
|
|
|
return m_context.mkConst(true);
|
|
|
|
else if (n == "false")
|
|
|
|
return m_context.mkConst(false);
|
2019-08-22 13:44:30 +00:00
|
|
|
else if (auto sortSort = dynamic_pointer_cast<SortSort>(_expr.sort))
|
|
|
|
return m_context.mkVar(n, cvc4Sort(*sortSort->inner));
|
2019-06-05 09:51:43 +00:00
|
|
|
else
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return m_context.mkConst(CVC4::Rational(n));
|
|
|
|
}
|
|
|
|
catch (CVC4::TypeCheckingException const& _e)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2019-06-05 09:51:43 +00:00
|
|
|
}
|
|
|
|
catch (CVC4::Exception const& _e)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2019-06-05 09:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_expr.hasCorrectArity(), "");
|
2019-06-05 09:51:43 +00:00
|
|
|
if (n == "ite")
|
|
|
|
return arguments[0].iteExpr(arguments[1], arguments[2]);
|
|
|
|
else if (n == "not")
|
|
|
|
return arguments[0].notExpr();
|
|
|
|
else if (n == "and")
|
|
|
|
return arguments[0].andExpr(arguments[1]);
|
|
|
|
else if (n == "or")
|
|
|
|
return arguments[0].orExpr(arguments[1]);
|
|
|
|
else if (n == "implies")
|
|
|
|
return m_context.mkExpr(CVC4::kind::IMPLIES, arguments[0], arguments[1]);
|
|
|
|
else if (n == "=")
|
|
|
|
return m_context.mkExpr(CVC4::kind::EQUAL, arguments[0], arguments[1]);
|
|
|
|
else if (n == "<")
|
|
|
|
return m_context.mkExpr(CVC4::kind::LT, arguments[0], arguments[1]);
|
|
|
|
else if (n == "<=")
|
|
|
|
return m_context.mkExpr(CVC4::kind::LEQ, arguments[0], arguments[1]);
|
|
|
|
else if (n == ">")
|
|
|
|
return m_context.mkExpr(CVC4::kind::GT, arguments[0], arguments[1]);
|
|
|
|
else if (n == ">=")
|
|
|
|
return m_context.mkExpr(CVC4::kind::GEQ, arguments[0], arguments[1]);
|
|
|
|
else if (n == "+")
|
|
|
|
return m_context.mkExpr(CVC4::kind::PLUS, arguments[0], arguments[1]);
|
|
|
|
else if (n == "-")
|
|
|
|
return m_context.mkExpr(CVC4::kind::MINUS, arguments[0], arguments[1]);
|
|
|
|
else if (n == "*")
|
|
|
|
return m_context.mkExpr(CVC4::kind::MULT, arguments[0], arguments[1]);
|
|
|
|
else if (n == "/")
|
|
|
|
return m_context.mkExpr(CVC4::kind::INTS_DIVISION_TOTAL, arguments[0], arguments[1]);
|
|
|
|
else if (n == "mod")
|
|
|
|
return m_context.mkExpr(CVC4::kind::INTS_MODULUS, arguments[0], arguments[1]);
|
2020-08-23 19:21:45 +00:00
|
|
|
else if (n == "bvnot")
|
|
|
|
return m_context.mkExpr(CVC4::kind::BITVECTOR_NOT, arguments[0]);
|
2020-05-13 11:08:48 +00:00
|
|
|
else if (n == "bvand")
|
|
|
|
return m_context.mkExpr(CVC4::kind::BITVECTOR_AND, arguments[0], arguments[1]);
|
2020-08-23 19:21:45 +00:00
|
|
|
else if (n == "bvor")
|
|
|
|
return m_context.mkExpr(CVC4::kind::BITVECTOR_OR, arguments[0], arguments[1]);
|
|
|
|
else if (n == "bvxor")
|
|
|
|
return m_context.mkExpr(CVC4::kind::BITVECTOR_XOR, arguments[0], arguments[1]);
|
2020-05-13 11:08:48 +00:00
|
|
|
else if (n == "int2bv")
|
|
|
|
{
|
2020-06-15 10:51:58 +00:00
|
|
|
size_t size = std::stoul(_expr.arguments[1].name);
|
2020-05-13 11:08:48 +00:00
|
|
|
auto i2bvOp = m_context.mkConst(CVC4::IntToBitVector(size));
|
|
|
|
// CVC4 treats all BVs as unsigned, so we need to manually apply 2's complement if needed.
|
|
|
|
return m_context.mkExpr(
|
|
|
|
CVC4::kind::ITE,
|
|
|
|
m_context.mkExpr(CVC4::kind::GEQ, arguments[0], m_context.mkConst(CVC4::Rational(0))),
|
|
|
|
m_context.mkExpr(CVC4::kind::INT_TO_BITVECTOR, i2bvOp, arguments[0]),
|
|
|
|
m_context.mkExpr(
|
|
|
|
CVC4::kind::BITVECTOR_NEG,
|
|
|
|
m_context.mkExpr(CVC4::kind::INT_TO_BITVECTOR, i2bvOp, m_context.mkExpr(CVC4::kind::UMINUS, arguments[0]))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (n == "bv2int")
|
|
|
|
{
|
|
|
|
auto intSort = dynamic_pointer_cast<IntSort>(_expr.sort);
|
|
|
|
smtAssert(intSort, "");
|
|
|
|
auto nat = m_context.mkExpr(CVC4::kind::BITVECTOR_TO_NAT, arguments[0]);
|
|
|
|
if (!intSort->isSigned)
|
|
|
|
return nat;
|
|
|
|
|
|
|
|
auto type = arguments[0].getType();
|
|
|
|
smtAssert(type.isBitVector(), "");
|
|
|
|
auto size = CVC4::BitVectorType(type).getSize();
|
|
|
|
// CVC4 treats all BVs as unsigned, so we need to manually apply 2's complement if needed.
|
|
|
|
auto extractOp = m_context.mkConst(CVC4::BitVectorExtract(size - 1, size - 1));
|
|
|
|
return m_context.mkExpr(CVC4::kind::ITE,
|
|
|
|
m_context.mkExpr(
|
|
|
|
CVC4::kind::EQUAL,
|
|
|
|
m_context.mkExpr(CVC4::kind::BITVECTOR_EXTRACT, extractOp, arguments[0]),
|
|
|
|
m_context.mkConst(CVC4::BitVector(1, size_t(0)))
|
|
|
|
),
|
|
|
|
nat,
|
|
|
|
m_context.mkExpr(
|
|
|
|
CVC4::kind::UMINUS,
|
|
|
|
m_context.mkExpr(CVC4::kind::BITVECTOR_TO_NAT, m_context.mkExpr(CVC4::kind::BITVECTOR_NEG, arguments[0]))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
else if (n == "select")
|
|
|
|
return m_context.mkExpr(CVC4::kind::SELECT, arguments[0], arguments[1]);
|
|
|
|
else if (n == "store")
|
|
|
|
return m_context.mkExpr(CVC4::kind::STORE, arguments[0], arguments[1], arguments[2]);
|
2019-08-22 13:44:30 +00:00
|
|
|
else if (n == "const_array")
|
|
|
|
{
|
|
|
|
shared_ptr<SortSort> sortSort = std::dynamic_pointer_cast<SortSort>(_expr.arguments[0].sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(sortSort, "");
|
2019-08-22 13:44:30 +00:00
|
|
|
return m_context.mkConst(CVC4::ArrayStoreAll(cvc4Sort(*sortSort->inner), arguments[1]));
|
|
|
|
}
|
2020-04-03 13:10:16 +00:00
|
|
|
else if (n == "tuple_get")
|
|
|
|
{
|
|
|
|
shared_ptr<TupleSort> tupleSort = std::dynamic_pointer_cast<TupleSort>(_expr.arguments[0].sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort, "");
|
2020-04-03 13:10:16 +00:00
|
|
|
CVC4::DatatypeType tt = m_context.mkTupleType(cvc4Sort(tupleSort->components));
|
|
|
|
CVC4::Datatype const& dt = tt.getDatatype();
|
2020-06-02 13:37:45 +00:00
|
|
|
size_t index = std::stoul(_expr.arguments[1].name);
|
2020-04-03 13:10:16 +00:00
|
|
|
CVC4::Expr s = dt[0][index].getSelector();
|
|
|
|
return m_context.mkExpr(CVC4::kind::APPLY_SELECTOR, s, arguments[0]);
|
|
|
|
}
|
2020-04-14 09:09:38 +00:00
|
|
|
else if (n == "tuple_constructor")
|
|
|
|
{
|
|
|
|
shared_ptr<TupleSort> tupleSort = std::dynamic_pointer_cast<TupleSort>(_expr.sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort, "");
|
2020-04-14 09:09:38 +00:00
|
|
|
CVC4::DatatypeType tt = m_context.mkTupleType(cvc4Sort(tupleSort->components));
|
|
|
|
CVC4::Datatype const& dt = tt.getDatatype();
|
|
|
|
CVC4::Expr c = dt[0].getConstructor();
|
|
|
|
return m_context.mkExpr(CVC4::kind::APPLY_CONSTRUCTOR, c, arguments);
|
|
|
|
}
|
2019-06-05 09:51:43 +00:00
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2019-06-05 09:51:43 +00:00
|
|
|
}
|
|
|
|
catch (CVC4::TypeCheckingException const& _e)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2019-06-05 09:51:43 +00:00
|
|
|
}
|
|
|
|
catch (CVC4::Exception const& _e)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, _e.what());
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2018-04-06 16:01:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 14:13:50 +00:00
|
|
|
CVC4::Type CVC4Interface::cvc4Sort(Sort const& _sort)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
switch (_sort.kind)
|
2018-04-06 16:01:40 +00:00
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
case Kind::Bool:
|
2018-04-06 16:01:40 +00:00
|
|
|
return m_context.booleanType();
|
2018-11-21 14:13:50 +00:00
|
|
|
case Kind::Int:
|
2018-04-06 16:01:40 +00:00
|
|
|
return m_context.integerType();
|
2018-11-21 15:57:02 +00:00
|
|
|
case Kind::Function:
|
|
|
|
{
|
|
|
|
FunctionSort const& fSort = dynamic_cast<FunctionSort const&>(_sort);
|
|
|
|
return m_context.mkFunctionType(cvc4Sort(fSort.domain), cvc4Sort(*fSort.codomain));
|
|
|
|
}
|
2018-11-22 10:24:12 +00:00
|
|
|
case Kind::Array:
|
|
|
|
{
|
|
|
|
auto const& arraySort = dynamic_cast<ArraySort const&>(_sort);
|
|
|
|
return m_context.mkArrayType(cvc4Sort(*arraySort.domain), cvc4Sort(*arraySort.range));
|
|
|
|
}
|
2020-04-03 13:10:16 +00:00
|
|
|
case Kind::Tuple:
|
|
|
|
{
|
|
|
|
auto const& tupleSort = dynamic_cast<TupleSort const&>(_sort);
|
|
|
|
return m_context.mkTupleType(cvc4Sort(tupleSort.components));
|
|
|
|
}
|
2018-04-06 16:01:40 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(false, "");
|
2018-04-06 16:01:40 +00:00
|
|
|
// Cannot be reached.
|
|
|
|
return m_context.integerType();
|
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
|
2018-11-21 14:13:50 +00:00
|
|
|
vector<CVC4::Type> CVC4Interface::cvc4Sort(vector<SortPointer> const& _sorts)
|
2018-10-25 14:00:09 +00:00
|
|
|
{
|
|
|
|
vector<CVC4::Type> cvc4Sorts;
|
|
|
|
for (auto const& _sort: _sorts)
|
2018-11-21 14:13:50 +00:00
|
|
|
cvc4Sorts.push_back(cvc4Sort(*_sort));
|
2018-10-25 14:00:09 +00:00
|
|
|
return cvc4Sorts;
|
|
|
|
}
|