2017-07-06 09:05:05 +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/SMTChecker.h>
|
|
|
|
|
2018-04-20 14:56:10 +00:00
|
|
|
#include <libsolidity/formal/SMTPortfolio.h>
|
2017-09-28 13:24:24 +00:00
|
|
|
#include <libsolidity/formal/VariableUsage.h>
|
2018-10-15 15:32:17 +00:00
|
|
|
#include <libsolidity/formal/SymbolicTypes.h>
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2018-11-22 00:02:25 +00:00
|
|
|
#include <libdevcore/StringUtils.h>
|
|
|
|
|
2017-07-14 09:49:27 +00:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
2017-09-29 14:53:26 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2017-07-06 09:05:05 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
2018-11-14 16:11:55 +00:00
|
|
|
using namespace langutil;
|
2017-07-06 09:05:05 +00:00
|
|
|
using namespace dev::solidity;
|
|
|
|
|
2017-10-13 13:19:53 +00:00
|
|
|
SMTChecker::SMTChecker(ErrorReporter& _errorReporter, map<h256, string> const& _smtlib2Responses):
|
|
|
|
m_interface(make_shared<smt::SMTPortfolio>(_smtlib2Responses)),
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter(_errorReporter)
|
2017-07-06 09:05:05 +00:00
|
|
|
{
|
2018-08-14 10:34:53 +00:00
|
|
|
#if defined (HAVE_Z3) || defined (HAVE_CVC4)
|
|
|
|
if (!_smtlib2Responses.empty())
|
|
|
|
m_errorReporter.warning(
|
|
|
|
"SMT-LIB2 query responses were given in the auxiliary input, "
|
|
|
|
"but this Solidity binary uses an SMT solver (Z3/CVC4) directly."
|
|
|
|
"These responses will be ignored."
|
|
|
|
"Consider disabling Z3/CVC4 at compilation time in order to use SMT-LIB2 responses."
|
|
|
|
);
|
|
|
|
#endif
|
2017-07-06 09:05:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 11:21:25 +00:00
|
|
|
void SMTChecker::analyze(SourceUnit const& _source, shared_ptr<Scanner> const& _scanner)
|
2017-07-06 09:05:05 +00:00
|
|
|
{
|
2017-09-28 13:24:24 +00:00
|
|
|
m_variableUsage = make_shared<VariableUsage>(_source);
|
2018-11-12 11:21:25 +00:00
|
|
|
m_scanner = _scanner;
|
2017-08-11 16:54:56 +00:00
|
|
|
if (_source.annotation().experimentalFeatures.count(ExperimentalFeature::SMTChecker))
|
2017-07-11 11:26:43 +00:00
|
|
|
_source.accept(*this);
|
|
|
|
}
|
|
|
|
|
2018-04-30 14:30:41 +00:00
|
|
|
bool SMTChecker::visit(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
for (auto _var : _contract.stateVariables())
|
2018-11-09 16:06:30 +00:00
|
|
|
createVariable(*_var);
|
2018-04-30 14:30:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(ContractDefinition const&)
|
|
|
|
{
|
2018-06-05 09:46:21 +00:00
|
|
|
m_variables.clear();
|
2018-04-30 14:30:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
void SMTChecker::endVisit(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
2017-09-28 13:24:24 +00:00
|
|
|
if (_varDecl.isLocalVariable() && _varDecl.type()->isValueType() &&_varDecl.value())
|
2017-10-05 13:23:25 +00:00
|
|
|
assignment(_varDecl, *_varDecl.value(), _varDecl.location());
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SMTChecker::visit(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
if (!_function.modifiers().empty() || _function.isConstructor())
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_function.location(),
|
|
|
|
"Assertion checker does not yet support constructors and functions with modifiers."
|
|
|
|
);
|
2018-10-10 12:31:49 +00:00
|
|
|
m_functionPath.push_back(&_function);
|
|
|
|
// Not visited by a function call
|
|
|
|
if (isRootFunction())
|
|
|
|
{
|
|
|
|
m_interface->reset();
|
|
|
|
m_pathConditions.clear();
|
|
|
|
m_expressions.clear();
|
2018-12-10 16:23:36 +00:00
|
|
|
m_globalContext.clear();
|
2018-10-25 14:00:09 +00:00
|
|
|
m_uninterpretedTerms.clear();
|
2018-10-10 12:31:49 +00:00
|
|
|
resetStateVariables();
|
|
|
|
initializeLocalVariables(_function);
|
2018-12-14 11:21:43 +00:00
|
|
|
m_loopExecutionHappened = false;
|
|
|
|
m_arrayAssignmentHappened = false;
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(FunctionDefinition const&)
|
|
|
|
{
|
2018-10-10 12:31:49 +00:00
|
|
|
// If _function was visited from a function call we don't remove
|
|
|
|
// the local variables just yet, since we might need them for
|
|
|
|
// future calls.
|
|
|
|
// Otherwise we remove any local variables from the context and
|
|
|
|
// keep the state variables.
|
|
|
|
if (isRootFunction())
|
|
|
|
removeLocalVariables();
|
|
|
|
m_functionPath.pop_back();
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 09:49:27 +00:00
|
|
|
bool SMTChecker::visit(IfStatement const& _node)
|
|
|
|
{
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
// We ignore called functions here because they have
|
|
|
|
// specific input values.
|
|
|
|
if (isRootFunction())
|
|
|
|
checkBooleanNotConstant(_node.condition(), "Condition is always $VALUE.");
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
auto indicesEndTrue = visitBranch(_node.trueStatement(), expr(_node.condition()));
|
2018-06-12 08:58:24 +00:00
|
|
|
vector<VariableDeclaration const*> touchedVariables = m_variableUsage->touchedVariables(_node.trueStatement());
|
2018-10-17 09:32:01 +00:00
|
|
|
decltype(indicesEndTrue) indicesEndFalse;
|
2017-07-14 09:49:27 +00:00
|
|
|
if (_node.falseStatement())
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
indicesEndFalse = visitBranch(*_node.falseStatement(), !expr(_node.condition()));
|
2017-09-28 13:24:24 +00:00
|
|
|
touchedVariables += m_variableUsage->touchedVariables(*_node.falseStatement());
|
|
|
|
}
|
2018-01-17 20:02:23 +00:00
|
|
|
else
|
2018-10-17 09:32:01 +00:00
|
|
|
indicesEndFalse = copyVariableIndices();
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
mergeVariables(touchedVariables, expr(_node.condition()), indicesEndTrue, indicesEndFalse);
|
2017-07-14 09:49:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
// Here we consider the execution of two branches:
|
|
|
|
// Branch 1 assumes the loop condition to be true and executes the loop once,
|
|
|
|
// after resetting touched variables.
|
|
|
|
// Branch 2 assumes the loop condition to be false and skips the loop after
|
|
|
|
// visiting the condition (it might contain side-effects, they need to be considered)
|
|
|
|
// and does not erase knowledge.
|
|
|
|
// If the loop is a do-while, condition side-effects are lost since the body,
|
|
|
|
// executed once before the condition, might reassign variables.
|
|
|
|
// Variables touched by the loop are merged with Branch 2.
|
2017-07-14 09:49:27 +00:00
|
|
|
bool SMTChecker::visit(WhileStatement const& _node)
|
|
|
|
{
|
2018-11-09 17:50:06 +00:00
|
|
|
auto indicesBeforeLoop = copyVariableIndices();
|
2017-09-28 13:24:24 +00:00
|
|
|
auto touchedVariables = m_variableUsage->touchedVariables(_node);
|
|
|
|
resetVariables(touchedVariables);
|
2018-11-09 17:50:06 +00:00
|
|
|
decltype(indicesBeforeLoop) indicesAfterLoop;
|
2017-09-28 11:44:56 +00:00
|
|
|
if (_node.isDoWhile())
|
|
|
|
{
|
2018-11-09 17:50:06 +00:00
|
|
|
indicesAfterLoop = visitBranch(_node.body());
|
2017-09-28 13:24:24 +00:00
|
|
|
// TODO the assertions generated in the body should still be active in the condition
|
2017-09-28 11:44:56 +00:00
|
|
|
_node.condition().accept(*this);
|
2018-10-10 12:31:49 +00:00
|
|
|
if (isRootFunction())
|
|
|
|
checkBooleanNotConstant(_node.condition(), "Do-while loop condition is always $VALUE.");
|
2017-09-28 11:44:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_node.condition().accept(*this);
|
2018-10-10 12:31:49 +00:00
|
|
|
if (isRootFunction())
|
|
|
|
checkBooleanNotConstant(_node.condition(), "While loop condition is always $VALUE.");
|
2017-09-29 14:53:26 +00:00
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
indicesAfterLoop = visitBranch(_node.body(), expr(_node.condition()));
|
2017-09-28 11:44:56 +00:00
|
|
|
}
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
// We reset the execution to before the loop
|
|
|
|
// and visit the condition in case it's not a do-while.
|
|
|
|
// A do-while's body might have non-precise information
|
|
|
|
// in its first run about variables that are touched.
|
|
|
|
resetVariableIndices(indicesBeforeLoop);
|
|
|
|
if (!_node.isDoWhile())
|
|
|
|
_node.condition().accept(*this);
|
|
|
|
|
|
|
|
mergeVariables(touchedVariables, expr(_node.condition()), indicesAfterLoop, copyVariableIndices());
|
|
|
|
|
|
|
|
m_loopExecutionHappened = true;
|
2017-09-28 11:44:56 +00:00
|
|
|
return false;
|
2017-07-14 09:49:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
// Here we consider the execution of two branches similar to WhileStatement.
|
2017-10-04 19:45:45 +00:00
|
|
|
bool SMTChecker::visit(ForStatement const& _node)
|
|
|
|
{
|
|
|
|
if (_node.initializationExpression())
|
|
|
|
_node.initializationExpression()->accept(*this);
|
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
auto indicesBeforeLoop = copyVariableIndices();
|
|
|
|
|
2017-10-04 19:45:45 +00:00
|
|
|
// Do not reset the init expression part.
|
|
|
|
auto touchedVariables =
|
|
|
|
m_variableUsage->touchedVariables(_node.body());
|
|
|
|
if (_node.condition())
|
|
|
|
touchedVariables += m_variableUsage->touchedVariables(*_node.condition());
|
|
|
|
if (_node.loopExpression())
|
|
|
|
touchedVariables += m_variableUsage->touchedVariables(*_node.loopExpression());
|
|
|
|
// Remove duplicates
|
|
|
|
std::sort(touchedVariables.begin(), touchedVariables.end());
|
|
|
|
touchedVariables.erase(std::unique(touchedVariables.begin(), touchedVariables.end()), touchedVariables.end());
|
|
|
|
|
|
|
|
resetVariables(touchedVariables);
|
|
|
|
|
|
|
|
if (_node.condition())
|
|
|
|
{
|
|
|
|
_node.condition()->accept(*this);
|
2018-10-10 12:31:49 +00:00
|
|
|
if (isRootFunction())
|
|
|
|
checkBooleanNotConstant(*_node.condition(), "For loop condition is always $VALUE.");
|
2017-10-04 19:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_interface->push();
|
|
|
|
if (_node.condition())
|
|
|
|
m_interface->addAssertion(expr(*_node.condition()));
|
|
|
|
_node.body().accept(*this);
|
|
|
|
if (_node.loopExpression())
|
|
|
|
_node.loopExpression()->accept(*this);
|
|
|
|
m_interface->pop();
|
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
auto indicesAfterLoop = copyVariableIndices();
|
|
|
|
// We reset the execution to before the loop
|
|
|
|
// and visit the condition.
|
|
|
|
resetVariableIndices(indicesBeforeLoop);
|
|
|
|
if (_node.condition())
|
|
|
|
_node.condition()->accept(*this);
|
2017-10-04 19:45:45 +00:00
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
auto forCondition = _node.condition() ? expr(*_node.condition()) : smt::Expression(true);
|
|
|
|
mergeVariables(touchedVariables, forCondition, indicesAfterLoop, copyVariableIndices());
|
2017-10-04 19:45:45 +00:00
|
|
|
|
2018-11-09 17:50:06 +00:00
|
|
|
m_loopExecutionHappened = true;
|
2017-10-04 19:45:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
void SMTChecker::endVisit(VariableDeclarationStatement const& _varDecl)
|
|
|
|
{
|
|
|
|
if (_varDecl.declarations().size() != 1)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_varDecl.location(),
|
|
|
|
"Assertion checker does not yet support such variable declarations."
|
|
|
|
);
|
2017-07-14 09:49:27 +00:00
|
|
|
else if (knownVariable(*_varDecl.declarations()[0]))
|
|
|
|
{
|
|
|
|
if (_varDecl.initialValue())
|
2017-10-05 13:23:25 +00:00
|
|
|
assignment(*_varDecl.declarations()[0], *_varDecl.initialValue(), _varDecl.location());
|
2017-07-14 09:49:27 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_varDecl.location(),
|
|
|
|
"Assertion checker does not yet implement such variable declarations."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(Assignment const& _assignment)
|
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
if (_assignment.assignmentOperator() != Token::Assign)
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
_assignment.location(),
|
|
|
|
"Assertion checker does not yet implement compound assignment."
|
|
|
|
);
|
2018-10-15 15:32:17 +00:00
|
|
|
else if (!isSupportedType(_assignment.annotation().type->category()))
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
_assignment.location(),
|
|
|
|
"Assertion checker does not yet implement type " + _assignment.annotation().type->toString()
|
|
|
|
);
|
|
|
|
else if (Identifier const* identifier = dynamic_cast<Identifier const*>(&_assignment.leftHandSide()))
|
|
|
|
{
|
2018-06-12 08:58:24 +00:00
|
|
|
VariableDeclaration const& decl = dynamic_cast<VariableDeclaration const&>(*identifier->annotation().referencedDeclaration);
|
2018-12-10 16:23:36 +00:00
|
|
|
solAssert(knownVariable(decl), "");
|
|
|
|
assignment(decl, _assignment.rightHandSide(), _assignment.location());
|
|
|
|
defineExpr(_assignment, expr(_assignment.rightHandSide()));
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
2018-11-09 16:06:30 +00:00
|
|
|
else if (dynamic_cast<IndexAccess const*>(&_assignment.leftHandSide()))
|
|
|
|
{
|
2018-12-14 11:21:43 +00:00
|
|
|
arrayIndexAssignment(_assignment);
|
2018-11-09 16:06:30 +00:00
|
|
|
defineExpr(_assignment, expr(_assignment.rightHandSide()));
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_assignment.location(),
|
|
|
|
"Assertion checker does not yet implement such assignments."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(TupleExpression const& _tuple)
|
|
|
|
{
|
2018-12-10 16:23:36 +00:00
|
|
|
if (
|
|
|
|
_tuple.isInlineArray() ||
|
|
|
|
_tuple.components().size() != 1 ||
|
|
|
|
!isSupportedType(_tuple.components()[0]->annotation().type->category())
|
|
|
|
)
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
_tuple.location(),
|
2018-10-10 12:31:49 +00:00
|
|
|
"Assertion checker does not yet implement tuples and inline arrays."
|
2017-07-11 11:26:43 +00:00
|
|
|
);
|
|
|
|
else
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_tuple, expr(*_tuple.components()[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::checkUnderOverflow(smt::Expression _value, IntegerType const& _type, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
checkCondition(
|
2018-10-17 13:56:44 +00:00
|
|
|
_value < minValue(_type),
|
2017-10-05 13:23:25 +00:00
|
|
|
_location,
|
2018-11-22 00:02:25 +00:00
|
|
|
"Underflow (resulting value less than " + formatNumberReadable(_type.minValue()) + ")",
|
2018-08-01 21:27:11 +00:00
|
|
|
"<result>",
|
2017-10-05 13:23:25 +00:00
|
|
|
&_value
|
|
|
|
);
|
|
|
|
checkCondition(
|
2018-10-17 13:56:44 +00:00
|
|
|
_value > maxValue(_type),
|
2017-10-05 13:23:25 +00:00
|
|
|
_location,
|
2018-11-22 00:02:25 +00:00
|
|
|
"Overflow (resulting value larger than " + formatNumberReadable(_type.maxValue()) + ")",
|
2018-08-01 21:27:11 +00:00
|
|
|
"<result>",
|
2017-10-05 13:23:25 +00:00
|
|
|
&_value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(UnaryOperation const& _op)
|
|
|
|
{
|
|
|
|
switch (_op.getOperator())
|
|
|
|
{
|
|
|
|
case Token::Not: // !
|
|
|
|
{
|
2018-10-15 15:32:17 +00:00
|
|
|
solAssert(isBool(_op.annotation().type->category()), "");
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, !expr(_op.subExpression()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::Inc: // ++ (pre- or postfix)
|
|
|
|
case Token::Dec: // -- (pre- or postfix)
|
|
|
|
{
|
2018-03-09 15:19:03 +00:00
|
|
|
|
2018-10-15 15:32:17 +00:00
|
|
|
solAssert(isInteger(_op.annotation().type->category()), "");
|
2017-10-05 13:23:25 +00:00
|
|
|
solAssert(_op.subExpression().annotation().lValueRequested, "");
|
|
|
|
if (Identifier const* identifier = dynamic_cast<Identifier const*>(&_op.subExpression()))
|
|
|
|
{
|
2018-06-12 08:58:24 +00:00
|
|
|
VariableDeclaration const& decl = dynamic_cast<VariableDeclaration const&>(*identifier->annotation().referencedDeclaration);
|
|
|
|
if (knownVariable(decl))
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
2018-06-12 08:58:24 +00:00
|
|
|
auto innerValue = currentValue(decl);
|
2017-10-05 13:23:25 +00:00
|
|
|
auto newValue = _op.getOperator() == Token::Inc ? innerValue + 1 : innerValue - 1;
|
2018-06-12 08:58:24 +00:00
|
|
|
assignment(decl, newValue, _op.location());
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, _op.isPrefixOperation() ? newValue : innerValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement such assignments."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement such increments / decrements."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::Sub: // -
|
|
|
|
{
|
|
|
|
defineExpr(_op, 0 - expr(_op.subExpression()));
|
|
|
|
if (auto intType = dynamic_cast<IntegerType const*>(_op.annotation().type.get()))
|
|
|
|
checkUnderOverflow(expr(_op), *intType, _op.location());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator."
|
|
|
|
);
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(BinaryOperation const& _op)
|
|
|
|
{
|
2018-10-22 14:48:21 +00:00
|
|
|
if (TokenTraits::isArithmeticOp(_op.getOperator()))
|
2017-07-11 11:26:43 +00:00
|
|
|
arithmeticOperation(_op);
|
2018-10-22 14:48:21 +00:00
|
|
|
else if (TokenTraits::isCompareOp(_op.getOperator()))
|
2017-07-11 11:26:43 +00:00
|
|
|
compareOperation(_op);
|
2018-10-22 14:48:21 +00:00
|
|
|
else if (TokenTraits::isBooleanOp(_op.getOperator()))
|
2017-07-11 11:26:43 +00:00
|
|
|
booleanOperation(_op);
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::endVisit(FunctionCall const& _funCall)
|
|
|
|
{
|
2017-10-04 11:18:40 +00:00
|
|
|
solAssert(_funCall.annotation().kind != FunctionCallKind::Unset, "");
|
|
|
|
if (_funCall.annotation().kind != FunctionCallKind::FunctionCall)
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not yet implement this expression."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
FunctionType const& funType = dynamic_cast<FunctionType const&>(*_funCall.expression().annotation().type);
|
|
|
|
|
|
|
|
std::vector<ASTPointer<Expression const>> const args = _funCall.arguments();
|
2018-12-10 16:23:36 +00:00
|
|
|
switch (funType.kind())
|
|
|
|
{
|
|
|
|
case FunctionType::Kind::Assert:
|
2018-10-10 12:31:49 +00:00
|
|
|
visitAssert(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Require:
|
2018-10-10 12:31:49 +00:00
|
|
|
visitRequire(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
|
|
|
case FunctionType::Kind::GasLeft:
|
2018-10-18 13:03:52 +00:00
|
|
|
visitGasLeft(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
|
|
|
case FunctionType::Kind::Internal:
|
2018-10-10 12:31:49 +00:00
|
|
|
inlineFunctionCall(_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
break;
|
|
|
|
case FunctionType::Kind::KECCAK256:
|
|
|
|
case FunctionType::Kind::ECRecover:
|
|
|
|
case FunctionType::Kind::SHA256:
|
|
|
|
case FunctionType::Kind::RIPEMD160:
|
|
|
|
case FunctionType::Kind::BlockHash:
|
|
|
|
case FunctionType::Kind::AddMod:
|
|
|
|
case FunctionType::Kind::MulMod:
|
|
|
|
abstractFunctionCall(_funCall);
|
|
|
|
break;
|
|
|
|
default:
|
2018-10-10 12:31:49 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not yet implement this type of function call."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::visitAssert(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
|
|
|
solAssert(args.size() == 1, "");
|
|
|
|
solAssert(args[0]->annotation().type->category() == Type::Category::Bool, "");
|
|
|
|
checkCondition(!(expr(*args[0])), _funCall.location(), "Assertion violation");
|
|
|
|
addPathImpliedExpression(expr(*args[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::visitRequire(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
auto const& args = _funCall.arguments();
|
|
|
|
solAssert(args.size() == 1, "");
|
|
|
|
solAssert(args[0]->annotation().type->category() == Type::Category::Bool, "");
|
|
|
|
if (isRootFunction())
|
2017-10-05 13:23:25 +00:00
|
|
|
checkBooleanNotConstant(*args[0], "Condition is always $VALUE.");
|
2018-10-10 12:31:49 +00:00
|
|
|
addPathImpliedExpression(expr(*args[0]));
|
|
|
|
}
|
|
|
|
|
2018-10-18 13:03:52 +00:00
|
|
|
void SMTChecker::visitGasLeft(FunctionCall const& _funCall)
|
|
|
|
{
|
2018-10-22 16:19:11 +00:00
|
|
|
string gasLeft = "gasleft()";
|
2018-10-18 13:03:52 +00:00
|
|
|
// We increase the variable index since gasleft changes
|
|
|
|
// inside a tx.
|
2018-12-10 16:23:36 +00:00
|
|
|
defineGlobalVariable(gasLeft, _funCall, true);
|
|
|
|
auto const& symbolicVar = m_globalContext.at(gasLeft);
|
2018-10-22 16:19:11 +00:00
|
|
|
unsigned index = symbolicVar->index();
|
|
|
|
// We set the current value to unknown anyway to add type constraints.
|
2018-11-22 13:48:31 +00:00
|
|
|
setUnknownValue(*symbolicVar);
|
2018-10-22 16:19:11 +00:00
|
|
|
if (index > 0)
|
|
|
|
m_interface->addAssertion(symbolicVar->currentValue() <= symbolicVar->valueAtIndex(index - 1));
|
2018-10-18 13:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 11:21:43 +00:00
|
|
|
void SMTChecker::eraseArrayKnowledge()
|
|
|
|
{
|
|
|
|
for (auto const& var: m_variables)
|
|
|
|
if (var.first->annotation().type->category() == Type::Category::Mapping)
|
|
|
|
newValue(*var.first);
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
void SMTChecker::inlineFunctionCall(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
FunctionDefinition const* _funDef = nullptr;
|
|
|
|
Expression const* _calledExpr = &_funCall.expression();
|
|
|
|
|
|
|
|
if (TupleExpression const* _fun = dynamic_cast<TupleExpression const*>(&_funCall.expression()))
|
|
|
|
{
|
|
|
|
solAssert(_fun->components().size() == 1, "");
|
|
|
|
_calledExpr = _fun->components().at(0).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Identifier const* _fun = dynamic_cast<Identifier const*>(_calledExpr))
|
|
|
|
_funDef = dynamic_cast<FunctionDefinition const*>(_fun->annotation().referencedDeclaration);
|
|
|
|
else if (MemberAccess const* _fun = dynamic_cast<MemberAccess const*>(_calledExpr))
|
|
|
|
_funDef = dynamic_cast<FunctionDefinition const*>(_fun->annotation().referencedDeclaration);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not yet implement this type of function call."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
solAssert(_funDef, "");
|
|
|
|
|
|
|
|
if (visitedFunction(_funDef))
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not support recursive function calls.",
|
|
|
|
SecondarySourceLocation().append("Starting from function:", _funDef->location())
|
|
|
|
);
|
|
|
|
else if (_funDef && _funDef->isImplemented())
|
|
|
|
{
|
|
|
|
vector<smt::Expression> funArgs;
|
2018-10-25 15:50:31 +00:00
|
|
|
auto const& funType = dynamic_cast<FunctionType const*>(_calledExpr->annotation().type.get());
|
|
|
|
solAssert(funType, "");
|
|
|
|
if (funType->bound())
|
|
|
|
{
|
|
|
|
auto const& boundFunction = dynamic_cast<MemberAccess const*>(_calledExpr);
|
|
|
|
solAssert(boundFunction, "");
|
|
|
|
funArgs.push_back(expr(boundFunction->expression()));
|
|
|
|
}
|
2018-10-10 12:31:49 +00:00
|
|
|
for (auto arg: _funCall.arguments())
|
|
|
|
funArgs.push_back(expr(*arg));
|
|
|
|
initializeFunctionCallParameters(*_funDef, funArgs);
|
|
|
|
_funDef->accept(*this);
|
|
|
|
auto const& returnParams = _funDef->returnParameters();
|
|
|
|
if (_funDef->returnParameters().size())
|
|
|
|
{
|
|
|
|
if (returnParams.size() > 1)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not yet support calls to functions that return more than one value."
|
|
|
|
);
|
|
|
|
else
|
|
|
|
defineExpr(_funCall, currentValue(*returnParams[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_funCall.location(),
|
|
|
|
"Assertion checker does not support calls to functions without implementation."
|
|
|
|
);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
void SMTChecker::abstractFunctionCall(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
vector<smt::Expression> smtArguments;
|
|
|
|
for (auto const& arg: _funCall.arguments())
|
|
|
|
smtArguments.push_back(expr(*arg));
|
|
|
|
defineExpr(_funCall, (*m_expressions.at(&_funCall.expression()))(smtArguments));
|
2018-11-09 16:06:30 +00:00
|
|
|
m_uninterpretedTerms.insert(&_funCall);
|
2018-12-10 16:23:36 +00:00
|
|
|
setSymbolicUnknownValue(expr(_funCall), _funCall.annotation().type, *m_interface);
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
void SMTChecker::endVisit(Identifier const& _identifier)
|
|
|
|
{
|
2017-09-28 13:24:24 +00:00
|
|
|
if (_identifier.annotation().lValueRequested)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2017-09-28 13:24:24 +00:00
|
|
|
// Will be translated as part of the node that requested the lvalue.
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
2018-12-10 16:23:36 +00:00
|
|
|
else if (dynamic_cast<FunctionType const*>(_identifier.annotation().type.get()))
|
2018-10-17 16:00:42 +00:00
|
|
|
{
|
2018-12-10 16:23:36 +00:00
|
|
|
visitFunctionIdentifier(_identifier);
|
2018-10-17 16:00:42 +00:00
|
|
|
}
|
2018-10-15 15:32:17 +00:00
|
|
|
else if (isSupportedType(_identifier.annotation().type->category()))
|
2018-06-12 08:58:24 +00:00
|
|
|
{
|
2018-08-07 19:43:20 +00:00
|
|
|
if (VariableDeclaration const* decl = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
|
|
|
defineExpr(_identifier, currentValue(*decl));
|
2018-10-18 13:03:52 +00:00
|
|
|
else if (_identifier.name() == "now")
|
2018-12-10 16:23:36 +00:00
|
|
|
defineGlobalVariable(_identifier.name(), _identifier);
|
2018-08-07 19:43:20 +00:00
|
|
|
else
|
|
|
|
// TODO: handle MagicVariableDeclaration here
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_identifier.location(),
|
|
|
|
"Assertion checker does not yet support the type of this variable."
|
|
|
|
);
|
2018-06-12 08:58:24 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
void SMTChecker::visitFunctionIdentifier(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
auto const& fType = dynamic_cast<FunctionType const&>(*_identifier.annotation().type);
|
|
|
|
if (fType.returnParameterTypes().size() > 1)
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_identifier.location(),
|
|
|
|
"Assertion checker does not yet support functions with more than one return parameter."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
defineGlobalFunction(fType.richIdentifier(), _identifier);
|
|
|
|
m_expressions.emplace(&_identifier, m_globalContext.at(fType.richIdentifier()));
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
void SMTChecker::endVisit(Literal const& _literal)
|
|
|
|
{
|
|
|
|
Type const& type = *_literal.annotation().type;
|
2018-10-17 16:00:42 +00:00
|
|
|
if (isNumber(type.category()))
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_literal, smt::Expression(type.literalValue(&_literal)));
|
2018-10-17 16:00:42 +00:00
|
|
|
else if (isBool(type.category()))
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_literal, smt::Expression(_literal.token() == Token::TrueLiteral ? true : false));
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_literal.location(),
|
2017-09-28 13:24:24 +00:00
|
|
|
"Assertion checker does not yet support the type of this literal (" +
|
2017-07-11 11:26:43 +00:00
|
|
|
_literal.annotation().type->toString() +
|
|
|
|
")."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
void SMTChecker::endVisit(Return const& _return)
|
|
|
|
{
|
2018-10-18 13:03:52 +00:00
|
|
|
if (knownExpr(*_return.expression()))
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
|
|
|
auto returnParams = m_functionPath.back()->returnParameters();
|
|
|
|
if (returnParams.size() > 1)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_return.location(),
|
|
|
|
"Assertion checker does not yet support more than one return value."
|
|
|
|
);
|
|
|
|
else if (returnParams.size() == 1)
|
|
|
|
m_interface->addAssertion(expr(*_return.expression()) == newValue(*returnParams[0]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 13:03:52 +00:00
|
|
|
bool SMTChecker::visit(MemberAccess const& _memberAccess)
|
|
|
|
{
|
2018-10-25 15:50:31 +00:00
|
|
|
auto const& accessType = _memberAccess.annotation().type;
|
|
|
|
if (accessType->category() == Type::Category::Function)
|
|
|
|
return true;
|
|
|
|
|
2018-10-18 13:03:52 +00:00
|
|
|
auto const& exprType = _memberAccess.expression().annotation().type;
|
|
|
|
solAssert(exprType, "");
|
|
|
|
if (exprType->category() == Type::Category::Magic)
|
|
|
|
{
|
2018-10-22 16:19:11 +00:00
|
|
|
auto identifier = dynamic_cast<Identifier const*>(&_memberAccess.expression());
|
|
|
|
string accessedName;
|
|
|
|
if (identifier)
|
|
|
|
accessedName = identifier->name();
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_memberAccess.location(),
|
|
|
|
"Assertion checker does not yet support this expression."
|
|
|
|
);
|
2018-12-10 16:23:36 +00:00
|
|
|
defineGlobalVariable(accessedName + "." + _memberAccess.memberName(), _memberAccess);
|
2018-10-18 13:03:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_memberAccess.location(),
|
|
|
|
"Assertion checker does not yet support this expression."
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:06:30 +00:00
|
|
|
void SMTChecker::endVisit(IndexAccess const& _indexAccess)
|
|
|
|
{
|
|
|
|
shared_ptr<SymbolicVariable> array;
|
|
|
|
if (auto const& id = dynamic_cast<Identifier const*>(&_indexAccess.baseExpression()))
|
|
|
|
{
|
|
|
|
auto const& varDecl = dynamic_cast<VariableDeclaration const&>(*id->annotation().referencedDeclaration);
|
|
|
|
solAssert(knownVariable(varDecl), "");
|
|
|
|
array = m_variables[&varDecl];
|
|
|
|
}
|
|
|
|
else if (auto const& innerAccess = dynamic_cast<IndexAccess const*>(&_indexAccess.baseExpression()))
|
|
|
|
{
|
|
|
|
solAssert(knownExpr(*innerAccess), "");
|
|
|
|
array = m_expressions[innerAccess];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_indexAccess.location(),
|
|
|
|
"Assertion checker does not yet implement this expression."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
solAssert(array, "");
|
|
|
|
defineExpr(_indexAccess, smt::Expression::select(
|
|
|
|
array->currentValue(),
|
|
|
|
expr(*_indexAccess.indexExpression())
|
|
|
|
));
|
|
|
|
setSymbolicUnknownValue(
|
|
|
|
expr(_indexAccess),
|
|
|
|
_indexAccess.annotation().type,
|
|
|
|
*m_interface
|
|
|
|
);
|
|
|
|
m_uninterpretedTerms.insert(&_indexAccess);
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:21:43 +00:00
|
|
|
void SMTChecker::arrayAssignment()
|
|
|
|
{
|
|
|
|
m_arrayAssignmentHappened = true;
|
|
|
|
eraseArrayKnowledge();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::arrayIndexAssignment(Assignment const& _assignment)
|
2018-11-09 16:06:30 +00:00
|
|
|
{
|
|
|
|
auto const& indexAccess = dynamic_cast<IndexAccess const&>(_assignment.leftHandSide());
|
|
|
|
if (auto const& id = dynamic_cast<Identifier const*>(&indexAccess.baseExpression()))
|
|
|
|
{
|
|
|
|
auto const& varDecl = dynamic_cast<VariableDeclaration const&>(*id->annotation().referencedDeclaration);
|
|
|
|
solAssert(knownVariable(varDecl), "");
|
|
|
|
smt::Expression store = smt::Expression::store(
|
|
|
|
m_variables[&varDecl]->currentValue(),
|
|
|
|
expr(*indexAccess.indexExpression()),
|
|
|
|
expr(_assignment.rightHandSide())
|
|
|
|
);
|
|
|
|
m_interface->addAssertion(newValue(varDecl) == store);
|
|
|
|
}
|
|
|
|
else if (dynamic_cast<IndexAccess const*>(&indexAccess.baseExpression()))
|
|
|
|
m_errorReporter.warning(
|
|
|
|
indexAccess.location(),
|
|
|
|
"Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays."
|
|
|
|
);
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_assignment.location(),
|
|
|
|
"Assertion checker does not yet implement this expression."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
void SMTChecker::defineGlobalVariable(string const& _name, Expression const& _expr, bool _increaseIndex)
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2018-12-10 16:23:36 +00:00
|
|
|
if (!knownGlobalSymbol(_name))
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
|
|
|
auto result = newSymbolicVariable(*_expr.annotation().type, _name, *m_interface);
|
2018-12-10 16:23:36 +00:00
|
|
|
m_globalContext.emplace(_name, result.second);
|
2018-11-22 13:48:31 +00:00
|
|
|
setUnknownValue(*result.second);
|
2018-10-18 13:03:52 +00:00
|
|
|
if (result.first)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_expr.location(),
|
2018-12-10 16:23:36 +00:00
|
|
|
"Assertion checker does not yet support this global variable."
|
2018-10-18 13:03:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (_increaseIndex)
|
2018-12-10 16:23:36 +00:00
|
|
|
m_globalContext.at(_name)->increaseIndex();
|
2018-10-18 13:03:52 +00:00
|
|
|
// The default behavior is not to increase the index since
|
2018-12-10 16:23:36 +00:00
|
|
|
// most of the global values stay the same throughout a tx.
|
|
|
|
if (isSupportedType(_expr.annotation().type->category()))
|
|
|
|
defineExpr(_expr, m_globalContext.at(_name)->currentValue());
|
2018-10-18 13:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
void SMTChecker::defineGlobalFunction(string const& _name, Expression const& _expr)
|
2018-10-25 14:00:09 +00:00
|
|
|
{
|
2018-12-10 16:23:36 +00:00
|
|
|
if (!knownGlobalSymbol(_name))
|
|
|
|
{
|
|
|
|
auto result = newSymbolicVariable(*_expr.annotation().type, _name, *m_interface);
|
|
|
|
m_globalContext.emplace(_name, result.second);
|
|
|
|
if (result.first)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_expr.location(),
|
|
|
|
"Assertion checker does not yet support the type of this function."
|
|
|
|
);
|
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
}
|
2018-10-18 13:03:52 +00:00
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
void SMTChecker::arithmeticOperation(BinaryOperation const& _op)
|
|
|
|
{
|
|
|
|
switch (_op.getOperator())
|
|
|
|
{
|
|
|
|
case Token::Add:
|
|
|
|
case Token::Sub:
|
|
|
|
case Token::Mul:
|
2017-10-05 13:23:25 +00:00
|
|
|
case Token::Div:
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
|
|
|
solAssert(_op.annotation().commonType, "");
|
2018-07-24 22:23:54 +00:00
|
|
|
if (_op.annotation().commonType->category() != Type::Category::Integer)
|
|
|
|
{
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator on non-integer types."
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2017-10-05 17:20:46 +00:00
|
|
|
auto const& intType = dynamic_cast<IntegerType const&>(*_op.annotation().commonType);
|
2017-07-11 11:26:43 +00:00
|
|
|
smt::Expression left(expr(_op.leftExpression()));
|
|
|
|
smt::Expression right(expr(_op.rightExpression()));
|
2018-10-22 14:48:21 +00:00
|
|
|
Token op = _op.getOperator();
|
2017-07-11 11:26:43 +00:00
|
|
|
smt::Expression value(
|
|
|
|
op == Token::Add ? left + right :
|
|
|
|
op == Token::Sub ? left - right :
|
2017-10-05 17:20:46 +00:00
|
|
|
op == Token::Div ? division(left, right, intType) :
|
2017-07-11 11:26:43 +00:00
|
|
|
/*op == Token::Mul*/ left * right
|
|
|
|
);
|
|
|
|
|
2017-10-05 13:23:25 +00:00
|
|
|
if (_op.getOperator() == Token::Div)
|
|
|
|
{
|
2018-08-01 21:27:11 +00:00
|
|
|
checkCondition(right == 0, _op.location(), "Division by zero", "<result>", &right);
|
2017-10-05 13:23:25 +00:00
|
|
|
m_interface->addAssertion(right != 0);
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:20:46 +00:00
|
|
|
checkUnderOverflow(value, intType, _op.location());
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, value);
|
2017-07-11 11:26:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement this operator."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::compareOperation(BinaryOperation const& _op)
|
|
|
|
{
|
|
|
|
solAssert(_op.annotation().commonType, "");
|
2018-10-15 15:32:17 +00:00
|
|
|
if (isSupportedType(_op.annotation().commonType->category()))
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
|
|
|
smt::Expression left(expr(_op.leftExpression()));
|
|
|
|
smt::Expression right(expr(_op.rightExpression()));
|
2018-10-22 14:48:21 +00:00
|
|
|
Token op = _op.getOperator();
|
2018-03-09 15:19:03 +00:00
|
|
|
shared_ptr<smt::Expression> value;
|
2018-10-17 13:56:44 +00:00
|
|
|
if (isNumber(_op.annotation().commonType->category()))
|
2018-03-09 15:19:03 +00:00
|
|
|
{
|
|
|
|
value = make_shared<smt::Expression>(
|
|
|
|
op == Token::Equal ? (left == right) :
|
|
|
|
op == Token::NotEqual ? (left != right) :
|
|
|
|
op == Token::LessThan ? (left < right) :
|
|
|
|
op == Token::LessThanOrEqual ? (left <= right) :
|
|
|
|
op == Token::GreaterThan ? (left > right) :
|
|
|
|
/*op == Token::GreaterThanOrEqual*/ (left >= right)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else // Bool
|
|
|
|
{
|
2018-10-15 15:32:17 +00:00
|
|
|
solUnimplementedAssert(isBool(_op.annotation().commonType->category()), "Operation not yet supported");
|
2018-03-09 15:19:03 +00:00
|
|
|
value = make_shared<smt::Expression>(
|
|
|
|
op == Token::Equal ? (left == right) :
|
2018-04-27 09:35:58 +00:00
|
|
|
/*op == Token::NotEqual*/ (left != right)
|
2018-03-09 15:19:03 +00:00
|
|
|
);
|
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
// TODO: check that other values for op are not possible.
|
2018-03-09 15:19:03 +00:00
|
|
|
defineExpr(_op, *value);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement the type " + _op.annotation().commonType->toString() + " for comparisons"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::booleanOperation(BinaryOperation const& _op)
|
|
|
|
{
|
|
|
|
solAssert(_op.getOperator() == Token::And || _op.getOperator() == Token::Or, "");
|
|
|
|
solAssert(_op.annotation().commonType, "");
|
|
|
|
if (_op.annotation().commonType->category() == Type::Category::Bool)
|
|
|
|
{
|
2017-09-28 11:44:56 +00:00
|
|
|
// @TODO check that both of them are not constant
|
2017-07-11 11:26:43 +00:00
|
|
|
if (_op.getOperator() == Token::And)
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, expr(_op.leftExpression()) && expr(_op.rightExpression()));
|
2017-07-11 11:26:43 +00:00
|
|
|
else
|
2017-10-05 13:23:25 +00:00
|
|
|
defineExpr(_op, expr(_op.leftExpression()) || expr(_op.rightExpression()));
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_op.location(),
|
|
|
|
"Assertion checker does not yet implement the type " + _op.annotation().commonType->toString() + " for boolean operations"
|
2017-09-28 11:44:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:20:46 +00:00
|
|
|
smt::Expression SMTChecker::division(smt::Expression _left, smt::Expression _right, IntegerType const& _type)
|
|
|
|
{
|
|
|
|
// Signed division in SMTLIB2 rounds differently for negative division.
|
|
|
|
if (_type.isSigned())
|
|
|
|
return (smt::Expression::ite(
|
|
|
|
_left >= 0,
|
|
|
|
smt::Expression::ite(_right >= 0, _left / _right, 0 - (_left / (0 - _right))),
|
|
|
|
smt::Expression::ite(_right >= 0, 0 - ((0 - _left) / _right), (0 - _left) / (0 - _right))
|
|
|
|
));
|
|
|
|
else
|
|
|
|
return _left / _right;
|
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
void SMTChecker::assignment(VariableDeclaration const& _variable, Expression const& _value, SourceLocation const& _location)
|
2017-09-28 11:44:56 +00:00
|
|
|
{
|
2017-10-05 13:23:25 +00:00
|
|
|
assignment(_variable, expr(_value), _location);
|
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
void SMTChecker::assignment(VariableDeclaration const& _variable, smt::Expression const& _value, SourceLocation const& _location)
|
2017-10-05 13:23:25 +00:00
|
|
|
{
|
|
|
|
TypePointer type = _variable.type();
|
|
|
|
if (auto const* intType = dynamic_cast<IntegerType const*>(type.get()))
|
|
|
|
checkUnderOverflow(_value, *intType, _location);
|
2018-09-03 15:45:58 +00:00
|
|
|
else if (dynamic_cast<AddressType const*>(type.get()))
|
|
|
|
checkUnderOverflow(_value, IntegerType(160), _location);
|
2018-12-14 11:21:43 +00:00
|
|
|
else if (dynamic_cast<MappingType const*>(type.get()))
|
|
|
|
arrayAssignment();
|
2017-10-05 13:23:25 +00:00
|
|
|
m_interface->addAssertion(newValue(_variable) == _value);
|
2017-09-28 11:44:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
SMTChecker::VariableIndices SMTChecker::visitBranch(Statement const& _statement, smt::Expression _condition)
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2017-12-18 18:43:15 +00:00
|
|
|
return visitBranch(_statement, &_condition);
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
SMTChecker::VariableIndices SMTChecker::visitBranch(Statement const& _statement, smt::Expression const* _condition)
|
2017-09-28 11:44:56 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
auto indicesBeforeBranch = copyVariableIndices();
|
2017-09-28 11:44:56 +00:00
|
|
|
if (_condition)
|
2017-12-11 19:09:17 +00:00
|
|
|
pushPathCondition(*_condition);
|
2017-09-28 11:44:56 +00:00
|
|
|
_statement.accept(*this);
|
2017-12-11 19:09:17 +00:00
|
|
|
if (_condition)
|
|
|
|
popPathCondition();
|
2018-10-17 09:32:01 +00:00
|
|
|
auto indicesAfterBranch = copyVariableIndices();
|
|
|
|
resetVariableIndices(indicesBeforeBranch);
|
|
|
|
return indicesAfterBranch;
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::checkCondition(
|
|
|
|
smt::Expression _condition,
|
|
|
|
SourceLocation const& _location,
|
|
|
|
string const& _description,
|
|
|
|
string const& _additionalValueName,
|
|
|
|
smt::Expression* _additionalValue
|
|
|
|
)
|
|
|
|
{
|
2017-07-13 16:22:51 +00:00
|
|
|
m_interface->push();
|
2017-12-13 16:59:36 +00:00
|
|
|
addPathConjoinedExpression(_condition);
|
2017-07-11 11:26:43 +00:00
|
|
|
|
|
|
|
vector<smt::Expression> expressionsToEvaluate;
|
2017-07-13 20:07:01 +00:00
|
|
|
vector<string> expressionNames;
|
2018-10-10 12:31:49 +00:00
|
|
|
if (m_functionPath.size())
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2018-11-12 11:21:25 +00:00
|
|
|
solAssert(m_scanner, "");
|
2017-07-11 11:26:43 +00:00
|
|
|
if (_additionalValue)
|
2017-07-13 20:07:01 +00:00
|
|
|
{
|
2017-07-11 11:26:43 +00:00
|
|
|
expressionsToEvaluate.emplace_back(*_additionalValue);
|
2017-07-13 20:07:01 +00:00
|
|
|
expressionNames.push_back(_additionalValueName);
|
|
|
|
}
|
2018-06-05 09:46:21 +00:00
|
|
|
for (auto const& var: m_variables)
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2018-11-09 16:06:30 +00:00
|
|
|
if (var.first->type()->isValueType())
|
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(currentValue(*var.first));
|
|
|
|
expressionNames.push_back(var.first->name());
|
|
|
|
}
|
2018-10-18 13:03:52 +00:00
|
|
|
}
|
2018-12-10 16:23:36 +00:00
|
|
|
for (auto const& var: m_globalContext)
|
2018-10-18 13:03:52 +00:00
|
|
|
{
|
2018-11-09 16:06:30 +00:00
|
|
|
auto const& type = var.second->type();
|
|
|
|
if (
|
|
|
|
type->isValueType() &&
|
|
|
|
smtKind(type->category()) != smt::Kind::Function
|
|
|
|
)
|
2018-12-10 16:23:36 +00:00
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(var.second->currentValue());
|
|
|
|
expressionNames.push_back(var.first);
|
|
|
|
}
|
2018-10-18 13:03:52 +00:00
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
for (auto const& uf: m_uninterpretedTerms)
|
|
|
|
{
|
2018-11-09 16:06:30 +00:00
|
|
|
if (uf->annotation().type->isValueType())
|
|
|
|
{
|
|
|
|
expressionsToEvaluate.emplace_back(expr(*uf));
|
|
|
|
expressionNames.push_back(m_scanner->sourceAt(uf->location()));
|
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
smt::CheckResult result;
|
|
|
|
vector<string> values;
|
2017-12-18 16:31:27 +00:00
|
|
|
tie(result, values) = checkSatisfiableAndGenerateModel(expressionsToEvaluate);
|
2017-07-13 19:06:29 +00:00
|
|
|
|
2018-04-19 07:28:44 +00:00
|
|
|
string loopComment;
|
|
|
|
if (m_loopExecutionHappened)
|
|
|
|
loopComment =
|
|
|
|
"\nNote that some information is erased after the execution of loops.\n"
|
2017-09-28 11:44:56 +00:00
|
|
|
"You can re-introduce information using require().";
|
2018-12-14 11:21:43 +00:00
|
|
|
if (m_arrayAssignmentHappened)
|
|
|
|
loopComment +=
|
|
|
|
"\nNote that array aliasing is not supported,"
|
|
|
|
" therefore all mapping information is erased after"
|
|
|
|
" a mapping local variable/parameter is assigned.\n"
|
|
|
|
"You can re-introduce information using require().";
|
2018-11-09 17:50:06 +00:00
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
switch (result)
|
|
|
|
{
|
2017-08-21 15:02:47 +00:00
|
|
|
case smt::CheckResult::SATISFIABLE:
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
|
|
|
std::ostringstream message;
|
|
|
|
message << _description << " happens here";
|
2018-10-10 12:31:49 +00:00
|
|
|
if (m_functionPath.size())
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2018-08-01 21:27:46 +00:00
|
|
|
std::ostringstream modelMessage;
|
|
|
|
modelMessage << " for:\n";
|
2017-07-13 20:07:01 +00:00
|
|
|
solAssert(values.size() == expressionNames.size(), "");
|
2018-08-01 21:27:46 +00:00
|
|
|
map<string, string> sortedModel;
|
2017-07-13 20:07:01 +00:00
|
|
|
for (size_t i = 0; i < values.size(); ++i)
|
2018-04-18 21:14:45 +00:00
|
|
|
if (expressionsToEvaluate.at(i).name != values.at(i))
|
2018-08-01 21:27:46 +00:00
|
|
|
sortedModel[expressionNames.at(i)] = values.at(i);
|
|
|
|
|
|
|
|
for (auto const& eval: sortedModel)
|
|
|
|
modelMessage << " " << eval.first << " = " << eval.second << "\n";
|
2018-11-09 17:50:06 +00:00
|
|
|
m_errorReporter.warning(_location, message.str(), SecondarySourceLocation().append(modelMessage.str(), SourceLocation()).append(loopComment, SourceLocation()));
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
else
|
2018-08-01 21:27:46 +00:00
|
|
|
{
|
2017-07-11 11:26:43 +00:00
|
|
|
message << ".";
|
2018-11-09 17:50:06 +00:00
|
|
|
m_errorReporter.warning(_location, message.str(), SecondarySourceLocation().append(loopComment, SourceLocation()));
|
2018-08-01 21:27:46 +00:00
|
|
|
}
|
2017-07-11 11:26:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-08-21 15:02:47 +00:00
|
|
|
case smt::CheckResult::UNSATISFIABLE:
|
2017-07-11 11:26:43 +00:00
|
|
|
break;
|
|
|
|
case smt::CheckResult::UNKNOWN:
|
2018-11-09 17:50:06 +00:00
|
|
|
m_errorReporter.warning(_location, _description + " might happen here.", SecondarySourceLocation().append(loopComment, SourceLocation()));
|
2017-07-11 11:26:43 +00:00
|
|
|
break;
|
2018-06-26 10:41:26 +00:00
|
|
|
case smt::CheckResult::CONFLICTING:
|
|
|
|
m_errorReporter.warning(_location, "At least two SMT solvers provided conflicting answers. Results might not be sound.");
|
|
|
|
break;
|
2017-07-11 11:26:43 +00:00
|
|
|
case smt::CheckResult::ERROR:
|
|
|
|
m_errorReporter.warning(_location, "Error trying to invoke SMT solver.");
|
|
|
|
break;
|
|
|
|
}
|
2017-07-13 16:22:51 +00:00
|
|
|
m_interface->pop();
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 14:53:26 +00:00
|
|
|
void SMTChecker::checkBooleanNotConstant(Expression const& _condition, string const& _description)
|
|
|
|
{
|
|
|
|
// Do not check for const-ness if this is a constant.
|
|
|
|
if (dynamic_cast<Literal const*>(&_condition))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_interface->push();
|
2017-12-13 16:59:36 +00:00
|
|
|
addPathConjoinedExpression(expr(_condition));
|
2017-12-18 16:31:27 +00:00
|
|
|
auto positiveResult = checkSatisfiable();
|
2017-09-29 14:53:26 +00:00
|
|
|
m_interface->pop();
|
|
|
|
|
|
|
|
m_interface->push();
|
2017-12-13 16:59:36 +00:00
|
|
|
addPathConjoinedExpression(!expr(_condition));
|
2017-12-18 16:31:27 +00:00
|
|
|
auto negatedResult = checkSatisfiable();
|
2017-09-29 14:53:26 +00:00
|
|
|
m_interface->pop();
|
|
|
|
|
|
|
|
if (positiveResult == smt::CheckResult::ERROR || negatedResult == smt::CheckResult::ERROR)
|
|
|
|
m_errorReporter.warning(_condition.location(), "Error trying to invoke SMT solver.");
|
2018-06-26 10:41:26 +00:00
|
|
|
else if (positiveResult == smt::CheckResult::CONFLICTING || negatedResult == smt::CheckResult::CONFLICTING)
|
|
|
|
m_errorReporter.warning(_condition.location(), "At least two SMT solvers provided conflicting answers. Results might not be sound.");
|
2017-09-29 14:53:26 +00:00
|
|
|
else if (positiveResult == smt::CheckResult::SATISFIABLE && negatedResult == smt::CheckResult::SATISFIABLE)
|
|
|
|
{
|
|
|
|
// everything fine.
|
|
|
|
}
|
2018-11-26 10:55:53 +00:00
|
|
|
else if (positiveResult == smt::CheckResult::UNKNOWN || negatedResult == smt::CheckResult::UNKNOWN)
|
|
|
|
{
|
|
|
|
// can't do anything.
|
|
|
|
}
|
2017-09-29 14:53:26 +00:00
|
|
|
else if (positiveResult == smt::CheckResult::UNSATISFIABLE && negatedResult == smt::CheckResult::UNSATISFIABLE)
|
|
|
|
m_errorReporter.warning(_condition.location(), "Condition unreachable.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string value;
|
|
|
|
if (positiveResult == smt::CheckResult::SATISFIABLE)
|
|
|
|
{
|
|
|
|
solAssert(negatedResult == smt::CheckResult::UNSATISFIABLE, "");
|
|
|
|
value = "true";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(positiveResult == smt::CheckResult::UNSATISFIABLE, "");
|
|
|
|
solAssert(negatedResult == smt::CheckResult::SATISFIABLE, "");
|
|
|
|
value = "false";
|
|
|
|
}
|
|
|
|
m_errorReporter.warning(_condition.location(), boost::algorithm::replace_all_copy(_description, "$VALUE", value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pair<smt::CheckResult, vector<string>>
|
2017-12-18 16:31:27 +00:00
|
|
|
SMTChecker::checkSatisfiableAndGenerateModel(vector<smt::Expression> const& _expressionsToEvaluate)
|
2017-09-29 14:53:26 +00:00
|
|
|
{
|
|
|
|
smt::CheckResult result;
|
|
|
|
vector<string> values;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
tie(result, values) = m_interface->check(_expressionsToEvaluate);
|
|
|
|
}
|
|
|
|
catch (smt::SolverError const& _e)
|
|
|
|
{
|
|
|
|
string description("Error querying SMT solver");
|
|
|
|
if (_e.comment())
|
|
|
|
description += ": " + *_e.comment();
|
|
|
|
m_errorReporter.warning(description);
|
|
|
|
result = smt::CheckResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (string& value: values)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse and re-format nicely
|
2018-11-22 00:02:25 +00:00
|
|
|
value = formatNumberReadable(bigint(value));
|
2017-09-29 14:53:26 +00:00
|
|
|
}
|
|
|
|
catch (...) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_pair(result, values);
|
|
|
|
}
|
|
|
|
|
2017-12-18 16:31:27 +00:00
|
|
|
smt::CheckResult SMTChecker::checkSatisfiable()
|
2017-09-29 14:53:26 +00:00
|
|
|
{
|
2017-12-18 16:31:27 +00:00
|
|
|
return checkSatisfiableAndGenerateModel({}).first;
|
2017-09-29 14:53:26 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
void SMTChecker::initializeFunctionCallParameters(FunctionDefinition const& _function, vector<smt::Expression> const& _callArgs)
|
|
|
|
{
|
|
|
|
auto const& funParams = _function.parameters();
|
|
|
|
solAssert(funParams.size() == _callArgs.size(), "");
|
|
|
|
for (unsigned i = 0; i < funParams.size(); ++i)
|
|
|
|
if (createVariable(*funParams[i]))
|
2018-12-14 11:21:43 +00:00
|
|
|
{
|
2018-10-10 12:31:49 +00:00
|
|
|
m_interface->addAssertion(_callArgs[i] == newValue(*funParams[i]));
|
2018-12-14 11:21:43 +00:00
|
|
|
if (funParams[i]->annotation().type->category() == Type::Category::Mapping)
|
|
|
|
m_arrayAssignmentHappened = true;
|
|
|
|
}
|
2018-10-10 12:31:49 +00:00
|
|
|
|
|
|
|
for (auto const& variable: _function.localVariables())
|
|
|
|
if (createVariable(*variable))
|
|
|
|
{
|
|
|
|
newValue(*variable);
|
|
|
|
setZeroValue(*variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_function.returnParameterList())
|
|
|
|
for (auto const& retParam: _function.returnParameters())
|
|
|
|
if (createVariable(*retParam))
|
|
|
|
{
|
|
|
|
newValue(*retParam);
|
|
|
|
setZeroValue(*retParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
void SMTChecker::initializeLocalVariables(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
for (auto const& variable: _function.localVariables())
|
2017-10-05 17:31:17 +00:00
|
|
|
if (createVariable(*variable))
|
|
|
|
setZeroValue(*variable);
|
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
for (auto const& param: _function.parameters())
|
2017-10-05 17:31:17 +00:00
|
|
|
if (createVariable(*param))
|
|
|
|
setUnknownValue(*param);
|
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
if (_function.returnParameterList())
|
|
|
|
for (auto const& retParam: _function.returnParameters())
|
2017-10-05 17:31:17 +00:00
|
|
|
if (createVariable(*retParam))
|
|
|
|
setZeroValue(*retParam);
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
void SMTChecker::removeLocalVariables()
|
|
|
|
{
|
|
|
|
for (auto it = m_variables.begin(); it != m_variables.end(); )
|
|
|
|
{
|
|
|
|
if (it->first->isLocalVariable())
|
|
|
|
it = m_variables.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:14:45 +00:00
|
|
|
void SMTChecker::resetStateVariables()
|
|
|
|
{
|
2018-06-05 09:46:21 +00:00
|
|
|
for (auto const& variable: m_variables)
|
2018-04-18 21:14:45 +00:00
|
|
|
{
|
2018-06-12 08:58:24 +00:00
|
|
|
if (variable.first->isStateVariable())
|
2018-06-05 09:46:21 +00:00
|
|
|
{
|
|
|
|
newValue(*variable.first);
|
|
|
|
setUnknownValue(*variable.first);
|
|
|
|
}
|
2018-04-18 21:14:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
void SMTChecker::resetVariables(vector<VariableDeclaration const*> _variables)
|
2017-09-28 11:44:56 +00:00
|
|
|
{
|
|
|
|
for (auto const* decl: _variables)
|
|
|
|
{
|
|
|
|
newValue(*decl);
|
2017-09-28 13:24:24 +00:00
|
|
|
setUnknownValue(*decl);
|
2017-09-28 11:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
void SMTChecker::mergeVariables(vector<VariableDeclaration const*> const& _variables, smt::Expression const& _condition, VariableIndices const& _indicesEndTrue, VariableIndices const& _indicesEndFalse)
|
2017-12-18 18:43:15 +00:00
|
|
|
{
|
2018-06-12 08:58:24 +00:00
|
|
|
set<VariableDeclaration const*> uniqueVars(_variables.begin(), _variables.end());
|
2017-12-18 18:43:15 +00:00
|
|
|
for (auto const* decl: uniqueVars)
|
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
solAssert(_indicesEndTrue.count(decl) && _indicesEndFalse.count(decl), "");
|
|
|
|
int trueIndex = _indicesEndTrue.at(decl);
|
|
|
|
int falseIndex = _indicesEndFalse.at(decl);
|
|
|
|
solAssert(trueIndex != falseIndex, "");
|
2017-12-18 18:43:15 +00:00
|
|
|
m_interface->addAssertion(newValue(*decl) == smt::Expression::ite(
|
|
|
|
_condition,
|
2018-10-17 09:32:01 +00:00
|
|
|
valueAtIndex(*decl, trueIndex),
|
|
|
|
valueAtIndex(*decl, falseIndex))
|
2017-12-18 18:43:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 17:31:17 +00:00
|
|
|
bool SMTChecker::createVariable(VariableDeclaration const& _varDecl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2018-10-10 12:31:49 +00:00
|
|
|
// This might be the case for multiple calls to the same function.
|
2018-10-18 13:03:52 +00:00
|
|
|
if (knownVariable(_varDecl))
|
2018-10-10 12:31:49 +00:00
|
|
|
return true;
|
2018-10-15 15:32:17 +00:00
|
|
|
auto const& type = _varDecl.type();
|
2018-10-17 16:00:42 +00:00
|
|
|
solAssert(m_variables.count(&_varDecl) == 0, "");
|
|
|
|
auto result = newSymbolicVariable(*type, _varDecl.name() + "_" + to_string(_varDecl.id()), *m_interface);
|
|
|
|
m_variables.emplace(&_varDecl, result.second);
|
|
|
|
if (result.first)
|
2017-10-05 17:31:17 +00:00
|
|
|
{
|
2017-07-11 11:26:43 +00:00
|
|
|
m_errorReporter.warning(
|
|
|
|
_varDecl.location(),
|
|
|
|
"Assertion checker does not yet support the type of this variable."
|
|
|
|
);
|
2017-10-05 17:31:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-17 16:00:42 +00:00
|
|
|
return true;
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
bool SMTChecker::knownVariable(VariableDeclaration const& _decl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
return m_variables.count(&_decl);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
smt::Expression SMTChecker::currentValue(VariableDeclaration const& _decl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
solAssert(knownVariable(_decl), "");
|
2018-10-17 09:32:01 +00:00
|
|
|
return m_variables.at(&_decl)->currentValue();
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
smt::Expression SMTChecker::valueAtIndex(VariableDeclaration const& _decl, int _index)
|
2017-07-14 09:49:27 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
solAssert(knownVariable(_decl), "");
|
2018-10-17 09:32:01 +00:00
|
|
|
return m_variables.at(&_decl)->valueAtIndex(_index);
|
2017-07-14 09:49:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
smt::Expression SMTChecker::newValue(VariableDeclaration const& _decl)
|
2017-07-11 11:26:43 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
solAssert(knownVariable(_decl), "");
|
2018-10-17 09:32:01 +00:00
|
|
|
return m_variables.at(&_decl)->increaseIndex();
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
void SMTChecker::setZeroValue(VariableDeclaration const& _decl)
|
2017-07-14 09:49:27 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
solAssert(knownVariable(_decl), "");
|
2018-11-22 13:48:31 +00:00
|
|
|
setZeroValue(*m_variables.at(&_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::setZeroValue(SymbolicVariable& _variable)
|
|
|
|
{
|
|
|
|
smt::setSymbolicZeroValue(_variable, *m_interface);
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
2017-07-14 09:49:27 +00:00
|
|
|
|
2018-06-12 08:58:24 +00:00
|
|
|
void SMTChecker::setUnknownValue(VariableDeclaration const& _decl)
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2018-01-17 20:02:23 +00:00
|
|
|
solAssert(knownVariable(_decl), "");
|
2018-11-22 13:48:31 +00:00
|
|
|
setUnknownValue(*m_variables.at(&_decl));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::setUnknownValue(SymbolicVariable& _variable)
|
|
|
|
{
|
|
|
|
smt::setSymbolicUnknownValue(_variable, *m_interface);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
smt::Expression SMTChecker::expr(Expression const& _e)
|
|
|
|
{
|
2018-10-18 13:03:52 +00:00
|
|
|
if (!knownExpr(_e))
|
2017-10-05 17:39:29 +00:00
|
|
|
{
|
|
|
|
m_errorReporter.warning(_e.location(), "Internal error: Expression undefined for SMT solver." );
|
|
|
|
createExpr(_e);
|
|
|
|
}
|
2018-10-17 16:00:42 +00:00
|
|
|
return m_expressions.at(&_e)->currentValue();
|
2018-10-10 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:03:52 +00:00
|
|
|
bool SMTChecker::knownExpr(Expression const& _e) const
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
|
|
|
return m_expressions.count(&_e);
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
bool SMTChecker::knownGlobalSymbol(string const& _var) const
|
2018-10-10 12:31:49 +00:00
|
|
|
{
|
2018-12-10 16:23:36 +00:00
|
|
|
return m_globalContext.count(_var);
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::createExpr(Expression const& _e)
|
|
|
|
{
|
2018-10-10 12:31:49 +00:00
|
|
|
solAssert(_e.annotation().type, "");
|
2018-10-18 13:03:52 +00:00
|
|
|
if (knownExpr(_e))
|
2018-10-17 16:00:42 +00:00
|
|
|
m_expressions.at(&_e)->increaseIndex();
|
2018-10-17 17:00:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto result = newSymbolicVariable(*_e.annotation().type, "expr_" + to_string(_e.id()), *m_interface);
|
|
|
|
m_expressions.emplace(&_e, result.second);
|
|
|
|
if (result.first)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
_e.location(),
|
|
|
|
"Assertion checker does not yet implement this type."
|
|
|
|
);
|
|
|
|
}
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::defineExpr(Expression const& _e, smt::Expression _value)
|
|
|
|
{
|
|
|
|
createExpr(_e);
|
2018-12-10 16:23:36 +00:00
|
|
|
solAssert(isSupportedType(*_e.annotation().type), "Equality operator applied to type that is not fully supported");
|
2017-10-05 13:23:25 +00:00
|
|
|
m_interface->addAssertion(expr(_e) == _value);
|
2017-07-11 11:26:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:09:17 +00:00
|
|
|
void SMTChecker::popPathCondition()
|
|
|
|
{
|
|
|
|
solAssert(m_pathConditions.size() > 0, "Cannot pop path condition, empty.");
|
|
|
|
m_pathConditions.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::pushPathCondition(smt::Expression const& _e)
|
|
|
|
{
|
|
|
|
m_pathConditions.push_back(currentPathConditions() && _e);
|
|
|
|
}
|
|
|
|
|
|
|
|
smt::Expression SMTChecker::currentPathConditions()
|
|
|
|
{
|
2018-10-09 03:29:37 +00:00
|
|
|
if (m_pathConditions.empty())
|
2017-12-11 19:09:17 +00:00
|
|
|
return smt::Expression(true);
|
|
|
|
return m_pathConditions.back();
|
|
|
|
}
|
2017-12-13 16:59:36 +00:00
|
|
|
|
|
|
|
void SMTChecker::addPathConjoinedExpression(smt::Expression const& _e)
|
|
|
|
{
|
|
|
|
m_interface->addAssertion(currentPathConditions() && _e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SMTChecker::addPathImpliedExpression(smt::Expression const& _e)
|
|
|
|
{
|
|
|
|
m_interface->addAssertion(smt::Expression::implies(currentPathConditions(), _e));
|
|
|
|
}
|
2018-06-05 09:46:21 +00:00
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
bool SMTChecker::isRootFunction()
|
2018-06-05 09:46:21 +00:00
|
|
|
{
|
2018-10-10 12:31:49 +00:00
|
|
|
return m_functionPath.size() == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SMTChecker::visitedFunction(FunctionDefinition const* _funDef)
|
|
|
|
{
|
|
|
|
return contains(m_functionPath, _funDef);
|
2018-06-05 09:46:21 +00:00
|
|
|
}
|
2018-10-15 15:32:17 +00:00
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
SMTChecker::VariableIndices SMTChecker::copyVariableIndices()
|
2018-10-15 15:32:17 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
VariableIndices indices;
|
|
|
|
for (auto const& var: m_variables)
|
|
|
|
indices.emplace(var.first, var.second->index());
|
|
|
|
return indices;
|
2018-10-15 15:32:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 09:32:01 +00:00
|
|
|
void SMTChecker::resetVariableIndices(VariableIndices const& _indices)
|
2018-10-15 15:32:17 +00:00
|
|
|
{
|
2018-10-17 09:32:01 +00:00
|
|
|
for (auto const& var: _indices)
|
2018-10-15 15:32:17 +00:00
|
|
|
m_variables.at(var.first)->index() = var.second;
|
|
|
|
}
|