2019-04-17 13:55:46 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/formal/EncodingContext.h>
|
|
|
|
|
|
|
|
#include <libsolidity/formal/SymbolicTypes.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity::smt;
|
|
|
|
|
2019-06-21 11:20:48 +00:00
|
|
|
EncodingContext::EncodingContext(std::shared_ptr<SolverInterface> _solver):
|
|
|
|
m_thisAddress(make_unique<SymbolicAddressVariable>("this", *_solver)),
|
|
|
|
m_solver(_solver)
|
2019-04-17 13:55:46 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
auto sort = make_shared<ArraySort>(
|
|
|
|
make_shared<Sort>(Kind::Int),
|
|
|
|
make_shared<Sort>(Kind::Int)
|
2019-04-11 16:14:48 +00:00
|
|
|
);
|
2019-06-21 11:20:48 +00:00
|
|
|
m_balances = make_unique<SymbolicVariable>(sort, "balances", *m_solver);
|
2019-04-17 13:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::reset()
|
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
resetAllVariables();
|
2019-05-13 14:53:38 +00:00
|
|
|
m_expressions.clear();
|
2019-05-14 16:57:34 +00:00
|
|
|
m_globalContext.clear();
|
2019-04-17 13:55:46 +00:00
|
|
|
m_thisAddress->increaseIndex();
|
2019-04-11 16:14:48 +00:00
|
|
|
m_balances->increaseIndex();
|
2019-06-21 11:20:48 +00:00
|
|
|
m_assertions.clear();
|
2019-04-17 13:55:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
/// Variables.
|
|
|
|
|
|
|
|
shared_ptr<SymbolicVariable> EncodingContext::variable(solidity::VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
solAssert(knownVariable(_varDecl), "");
|
|
|
|
return m_variables[&_varDecl];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EncodingContext::createVariable(solidity::VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
solAssert(!knownVariable(_varDecl), "");
|
|
|
|
auto const& type = _varDecl.type();
|
2019-06-21 11:20:48 +00:00
|
|
|
auto result = newSymbolicVariable(*type, _varDecl.name() + "_" + to_string(_varDecl.id()), *m_solver);
|
2019-05-10 14:28:29 +00:00
|
|
|
m_variables.emplace(&_varDecl, result.second);
|
|
|
|
return result.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EncodingContext::knownVariable(solidity::VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
return m_variables.count(&_varDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::resetVariable(solidity::VariableDeclaration const& _variable)
|
|
|
|
{
|
|
|
|
newValue(_variable);
|
|
|
|
setUnknownValue(_variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::resetVariables(set<solidity::VariableDeclaration const*> const& _variables)
|
|
|
|
{
|
|
|
|
for (auto const* decl: _variables)
|
|
|
|
resetVariable(*decl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::resetVariables(function<bool(solidity::VariableDeclaration const&)> const& _filter)
|
|
|
|
{
|
|
|
|
for_each(begin(m_variables), end(m_variables), [&](auto _variable)
|
|
|
|
{
|
|
|
|
if (_filter(*_variable.first))
|
|
|
|
this->resetVariable(*_variable.first);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::resetAllVariables()
|
|
|
|
{
|
|
|
|
resetVariables([&](solidity::VariableDeclaration const&) { return true; });
|
|
|
|
}
|
|
|
|
|
|
|
|
Expression EncodingContext::newValue(solidity::VariableDeclaration const& _decl)
|
|
|
|
{
|
|
|
|
solAssert(knownVariable(_decl), "");
|
|
|
|
return m_variables.at(&_decl)->increaseIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::setZeroValue(solidity::VariableDeclaration const& _decl)
|
|
|
|
{
|
|
|
|
solAssert(knownVariable(_decl), "");
|
|
|
|
setZeroValue(*m_variables.at(&_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::setZeroValue(SymbolicVariable& _variable)
|
|
|
|
{
|
2019-06-21 11:20:48 +00:00
|
|
|
setSymbolicZeroValue(_variable, *m_solver);
|
2019-05-10 14:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::setUnknownValue(solidity::VariableDeclaration const& _decl)
|
|
|
|
{
|
|
|
|
solAssert(knownVariable(_decl), "");
|
|
|
|
setUnknownValue(*m_variables.at(&_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::setUnknownValue(SymbolicVariable& _variable)
|
|
|
|
{
|
2019-06-21 11:20:48 +00:00
|
|
|
setSymbolicUnknownValue(_variable, *m_solver);
|
2019-05-10 14:28:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 14:53:38 +00:00
|
|
|
/// Expressions
|
|
|
|
|
|
|
|
shared_ptr<SymbolicVariable> EncodingContext::expression(solidity::Expression const& _e)
|
|
|
|
{
|
|
|
|
if (!knownExpression(_e))
|
|
|
|
createExpression(_e);
|
|
|
|
return m_expressions.at(&_e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EncodingContext::createExpression(solidity::Expression const& _e, shared_ptr<SymbolicVariable> _symbVar)
|
|
|
|
{
|
|
|
|
solAssert(_e.annotation().type, "");
|
|
|
|
if (knownExpression(_e))
|
|
|
|
{
|
|
|
|
expression(_e)->increaseIndex();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (_symbVar)
|
|
|
|
{
|
|
|
|
m_expressions.emplace(&_e, _symbVar);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-21 11:20:48 +00:00
|
|
|
auto result = newSymbolicVariable(*_e.annotation().type, "expr_" + to_string(_e.id()), *m_solver);
|
2019-05-13 14:53:38 +00:00
|
|
|
m_expressions.emplace(&_e, result.second);
|
|
|
|
return result.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EncodingContext::knownExpression(solidity::Expression const& _e) const
|
|
|
|
{
|
|
|
|
return m_expressions.count(&_e);
|
|
|
|
}
|
|
|
|
|
2019-05-14 16:57:34 +00:00
|
|
|
/// Global variables and functions.
|
|
|
|
|
|
|
|
shared_ptr<SymbolicVariable> EncodingContext::globalSymbol(string const& _name)
|
|
|
|
{
|
|
|
|
solAssert(knownGlobalSymbol(_name), "");
|
|
|
|
return m_globalContext.at(_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EncodingContext::createGlobalSymbol(string const& _name, solidity::Expression const& _expr)
|
|
|
|
{
|
|
|
|
solAssert(!knownGlobalSymbol(_name), "");
|
2019-06-21 11:20:48 +00:00
|
|
|
auto result = newSymbolicVariable(*_expr.annotation().type, _name, *m_solver);
|
2019-05-14 16:57:34 +00:00
|
|
|
m_globalContext.emplace(_name, result.second);
|
|
|
|
setUnknownValue(*result.second);
|
|
|
|
return result.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EncodingContext::knownGlobalSymbol(string const& _var) const
|
|
|
|
{
|
|
|
|
return m_globalContext.count(_var);
|
|
|
|
}
|
|
|
|
|
2019-05-13 14:53:38 +00:00
|
|
|
// Blockchain
|
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
Expression EncodingContext::thisAddress()
|
2019-04-17 13:55:46 +00:00
|
|
|
{
|
|
|
|
return m_thisAddress->currentValue();
|
|
|
|
}
|
2019-04-11 16:14:48 +00:00
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
Expression EncodingContext::balance()
|
2019-04-11 16:14:48 +00:00
|
|
|
{
|
|
|
|
return balance(m_thisAddress->currentValue());
|
|
|
|
}
|
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
Expression EncodingContext::balance(Expression _address)
|
2019-04-11 16:14:48 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
return Expression::select(m_balances->currentValue(), move(_address));
|
2019-04-11 16:14:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
void EncodingContext::transfer(Expression _from, Expression _to, Expression _value)
|
2019-04-11 16:14:48 +00:00
|
|
|
{
|
|
|
|
unsigned indexBefore = m_balances->index();
|
|
|
|
addBalance(_from, 0 - _value);
|
|
|
|
addBalance(_to, move(_value));
|
|
|
|
unsigned indexAfter = m_balances->index();
|
|
|
|
solAssert(indexAfter > indexBefore, "");
|
|
|
|
m_balances->increaseIndex();
|
|
|
|
/// Do not apply the transfer operation if _from == _to.
|
2019-05-10 14:28:29 +00:00
|
|
|
auto newBalances = Expression::ite(
|
2019-04-11 16:14:48 +00:00
|
|
|
move(_from) == move(_to),
|
|
|
|
m_balances->valueAtIndex(indexBefore),
|
|
|
|
m_balances->valueAtIndex(indexAfter)
|
|
|
|
);
|
2019-06-21 11:20:48 +00:00
|
|
|
m_solver->addAssertion(m_balances->currentValue() == newBalances);
|
2019-04-11 16:14:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 11:20:48 +00:00
|
|
|
/// Solver.
|
|
|
|
|
|
|
|
Expression EncodingContext::assertions()
|
|
|
|
{
|
|
|
|
if (m_assertions.empty())
|
|
|
|
return Expression(true);
|
|
|
|
|
|
|
|
return m_assertions.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::pushSolver()
|
|
|
|
{
|
|
|
|
m_assertions.push_back(assertions());
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::popSolver()
|
|
|
|
{
|
|
|
|
solAssert(!m_assertions.empty(), "");
|
|
|
|
m_assertions.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodingContext::addAssertion(Expression const& _expr)
|
|
|
|
{
|
|
|
|
if (m_assertions.empty())
|
|
|
|
m_assertions.push_back(_expr);
|
|
|
|
else
|
|
|
|
m_assertions.back() = _expr && move(m_assertions.back());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Private helpers.
|
|
|
|
|
2019-05-10 14:28:29 +00:00
|
|
|
void EncodingContext::addBalance(Expression _address, Expression _value)
|
2019-04-11 16:14:48 +00:00
|
|
|
{
|
2019-05-10 14:28:29 +00:00
|
|
|
auto newBalances = Expression::store(
|
2019-04-11 16:14:48 +00:00
|
|
|
m_balances->currentValue(),
|
|
|
|
_address,
|
|
|
|
balance(_address) + move(_value)
|
|
|
|
);
|
|
|
|
m_balances->increaseIndex();
|
2019-06-21 11:20:48 +00:00
|
|
|
m_solver->addAssertion(newBalances == m_balances->currentValue());
|
2019-04-11 16:14:48 +00:00
|
|
|
}
|