solidity/libsolidity/formal/SMTChecker.h

162 lines
6.6 KiB
C
Raw Normal View History

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/>.
*/
#pragma once
2017-09-28 13:24:24 +00:00
2017-07-13 16:22:51 +00:00
#include <libsolidity/formal/SolverInterface.h>
2017-09-28 13:24:24 +00:00
#include <libsolidity/ast/ASTVisitor.h>
2017-07-11 11:26:43 +00:00
#include <libsolidity/interface/ReadFile.h>
2017-07-06 09:05:05 +00:00
#include <map>
#include <string>
namespace dev
{
namespace solidity
{
2017-09-28 13:24:24 +00:00
class VariableUsage;
2017-07-06 09:05:05 +00:00
class ErrorReporter;
2017-07-11 11:26:43 +00:00
class SMTChecker: private ASTConstVisitor
2017-07-06 09:05:05 +00:00
{
public:
2017-07-13 19:06:04 +00:00
SMTChecker(ErrorReporter& _errorReporter, ReadCallback::Callback const& _readCallback);
2017-07-06 09:05:05 +00:00
void analyze(SourceUnit const& _sources);
private:
2017-07-11 11:26:43 +00:00
// TODO: Check that we do not have concurrent reads and writes to a variable,
// because the order of expression evaluation is undefined
// TODO: or just force a certain order, but people might have a different idea about that.
virtual void endVisit(VariableDeclaration const& _node) override;
virtual bool visit(FunctionDefinition const& _node) override;
virtual void endVisit(FunctionDefinition const& _node) override;
2017-07-14 09:49:27 +00:00
virtual bool visit(IfStatement const& _node) override;
virtual bool visit(WhileStatement const& _node) override;
2017-10-04 19:45:45 +00:00
virtual bool visit(ForStatement const& _node) override;
2017-07-11 11:26:43 +00:00
virtual void endVisit(VariableDeclarationStatement const& _node) override;
virtual void endVisit(ExpressionStatement const& _node) override;
virtual void endVisit(Assignment const& _node) override;
virtual void endVisit(TupleExpression const& _node) override;
2017-10-05 13:23:25 +00:00
virtual void endVisit(UnaryOperation const& _node) override;
2017-07-11 11:26:43 +00:00
virtual void endVisit(BinaryOperation const& _node) override;
virtual void endVisit(FunctionCall const& _node) override;
virtual void endVisit(Identifier const& _node) override;
virtual void endVisit(Literal const& _node) override;
void arithmeticOperation(BinaryOperation const& _op);
void compareOperation(BinaryOperation const& _op);
void booleanOperation(BinaryOperation const& _op);
2017-10-05 17:20:46 +00:00
/// Division expression in the given type. Requires special treatment because
/// of rounding for signed division.
smt::Expression division(smt::Expression _left, smt::Expression _right, IntegerType const& _type);
2017-10-05 13:23:25 +00:00
void assignment(Declaration const& _variable, Expression const& _value, SourceLocation const& _location);
void assignment(Declaration const& _variable, smt::Expression const& _value, SourceLocation const& _location);
2017-09-28 13:24:24 +00:00
2017-09-28 11:44:56 +00:00
// Visits the branch given by the statement, pushes and pops the SMT checker.
// @param _condition if present, asserts that this condition is true within the branch.
2017-09-28 13:24:24 +00:00
void visitBranch(Statement const& _statement, smt::Expression const* _condition = nullptr);
void visitBranch(Statement const& _statement, smt::Expression _condition);
2017-09-28 11:44:56 +00:00
2017-09-29 14:53:26 +00:00
/// Check that a condition can be satisfied.
2017-07-11 11:26:43 +00:00
void checkCondition(
smt::Expression _condition,
SourceLocation const& _location,
std::string const& _description,
std::string const& _additionalValueName = "",
smt::Expression* _additionalValue = nullptr
);
2017-09-29 14:53:26 +00:00
/// Checks that a boolean condition is not constant. Do not warn if the expression
/// is a literal constant.
/// @param _description the warning string, $VALUE will be replaced by the constant value.
void checkBooleanNotConstant(
Expression const& _condition,
std::string const& _description
);
2017-10-05 13:23:25 +00:00
/// Checks that the value is in the range given by the type.
void checkUnderOverflow(smt::Expression _value, IntegerType const& _Type, SourceLocation const& _location);
2017-09-29 14:53:26 +00:00
std::pair<smt::CheckResult, std::vector<std::string>>
checkSatisifableAndGenerateModel(std::vector<smt::Expression> const& _expressionsToEvaluate);
smt::CheckResult checkSatisifable();
2017-07-11 11:26:43 +00:00
2017-09-28 13:24:24 +00:00
void initializeLocalVariables(FunctionDefinition const& _function);
void resetVariables(std::vector<Declaration const*> _variables);
/// Tries to create an uninitialized variable and returns true on success.
/// This fails if the type is not supported.
bool createVariable(VariableDeclaration const& _varDecl);
2017-07-11 11:26:43 +00:00
2017-08-21 15:02:47 +00:00
static std::string uniqueSymbol(Declaration const& _decl);
static std::string uniqueSymbol(Expression const& _expr);
2017-07-14 09:49:27 +00:00
/// @returns true if _delc is a variable that is known at the current point, i.e.
/// has a valid sequence number
2017-07-11 11:26:43 +00:00
bool knownVariable(Declaration const& _decl);
2017-07-14 09:49:27 +00:00
/// @returns an expression denoting the value of the variable declared in @a _decl
/// at the current point.
2017-07-11 11:26:43 +00:00
smt::Expression currentValue(Declaration const& _decl);
2017-07-14 09:49:27 +00:00
/// @returns an expression denoting the value of the variable declared in @a _decl
/// at the given sequence point. Does not ensure that this sequence point exists.
smt::Expression valueAtSequence(Declaration const& _decl, int _sequence);
/// Allocates a new sequence number for the declaration, updates the current
/// sequence number to this value and returns the expression.
2017-07-11 11:26:43 +00:00
smt::Expression newValue(Declaration const& _decl);
2017-09-28 13:24:24 +00:00
/// Sets the value of the declaration to zero.
void setZeroValue(Declaration const& _decl);
/// Resets the variable to an unknown value (in its range).
void setUnknownValue(Declaration const& decl);
2017-07-14 09:49:27 +00:00
2017-08-21 15:02:47 +00:00
static smt::Expression minValue(IntegerType const& _t);
static smt::Expression maxValue(IntegerType const& _t);
2017-07-11 11:26:43 +00:00
2017-09-28 11:44:56 +00:00
using VariableSequenceCounters = std::map<Declaration const*, int>;
2017-10-05 13:23:25 +00:00
/// Returns the expression corresponding to the AST node. Throws if the expression does not exist.
2017-07-11 11:26:43 +00:00
smt::Expression expr(Expression const& _e);
2017-10-05 13:23:25 +00:00
/// Creates the expression (value can be arbitrary)
void createExpr(Expression const& _e);
/// Creates the expression and sets its value.
void defineExpr(Expression const& _e, smt::Expression _value);
2017-07-11 11:26:43 +00:00
/// Returns the function declaration corresponding to the given variable.
/// The function takes one argument which is the "sequence number".
smt::Expression var(Declaration const& _decl);
2017-07-13 16:22:51 +00:00
std::shared_ptr<smt::SolverInterface> m_interface;
2017-09-28 13:24:24 +00:00
std::shared_ptr<VariableUsage> m_variableUsage;
2017-09-28 11:44:56 +00:00
bool m_conditionalExecutionHappened = false;
2017-07-11 11:26:43 +00:00
std::map<Declaration const*, int> m_currentSequenceCounter;
2017-07-14 09:49:27 +00:00
std::map<Declaration const*, int> m_nextFreeSequenceCounter;
2017-09-28 09:29:04 +00:00
std::map<Expression const*, smt::Expression> m_expressions;
std::map<Declaration const*, smt::Expression> m_variables;
2017-07-11 11:26:43 +00:00
ErrorReporter& m_errorReporter;
FunctionDefinition const* m_currentFunction = nullptr;
2017-07-06 09:05:05 +00:00
};
}
}