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>
|
2018-10-22 08:29:03 +00:00
|
|
|
#include <libsolidity/formal/SymbolicVariables.h>
|
2018-01-17 20:02:23 +00:00
|
|
|
|
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>
|
2019-01-23 10:22:38 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Scanner.h>
|
2018-11-12 11:21:25 +00:00
|
|
|
|
2017-07-06 09:05:05 +00:00
|
|
|
#include <string>
|
2018-12-17 17:26:10 +00:00
|
|
|
#include <unordered_map>
|
2017-12-11 19:09:17 +00:00
|
|
|
#include <vector>
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
namespace langutil
|
|
|
|
{
|
|
|
|
class ErrorReporter;
|
|
|
|
struct SourceLocation;
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:05:05 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
class VariableUsage;
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2017-07-11 11:26:43 +00:00
|
|
|
class SMTChecker: private ASTConstVisitor
|
2017-07-06 09:05:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-10-13 13:19:53 +00:00
|
|
|
SMTChecker(langutil::ErrorReporter& _errorReporter, std::map<h256, std::string> const& _smtlib2Responses);
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
void analyze(SourceUnit const& _sources, std::shared_ptr<langutil::Scanner> const& _scanner);
|
2017-07-06 09:05:05 +00:00
|
|
|
|
2017-10-13 13:19:53 +00:00
|
|
|
/// This is used if the SMT solver is not directly linked into this binary.
|
|
|
|
/// @returns a list of inputs to the SMT solver that were not part of the argument to
|
|
|
|
/// the constructor.
|
|
|
|
std::vector<std::string> unhandledQueries() { return m_interface->unhandledQueries(); }
|
|
|
|
|
2017-07-06 09:05:05 +00:00
|
|
|
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.
|
|
|
|
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(ContractDefinition const& _node) override;
|
|
|
|
void endVisit(ContractDefinition const& _node) override;
|
|
|
|
void endVisit(VariableDeclaration const& _node) override;
|
|
|
|
bool visit(FunctionDefinition const& _node) override;
|
|
|
|
void endVisit(FunctionDefinition const& _node) override;
|
|
|
|
bool visit(IfStatement const& _node) override;
|
|
|
|
bool visit(WhileStatement const& _node) override;
|
|
|
|
bool visit(ForStatement const& _node) override;
|
|
|
|
void endVisit(VariableDeclarationStatement const& _node) override;
|
|
|
|
void endVisit(Assignment const& _node) override;
|
|
|
|
void endVisit(TupleExpression const& _node) override;
|
2019-03-11 10:56:08 +00:00
|
|
|
bool visit(UnaryOperation const& _node) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
void endVisit(UnaryOperation const& _node) override;
|
2019-03-11 10:56:08 +00:00
|
|
|
bool visit(BinaryOperation const& _node) override;
|
2018-11-16 01:09:04 +00:00
|
|
|
void endVisit(BinaryOperation const& _node) override;
|
|
|
|
void endVisit(FunctionCall const& _node) override;
|
|
|
|
void endVisit(Identifier const& _node) override;
|
|
|
|
void endVisit(Literal const& _node) override;
|
|
|
|
void endVisit(Return const& _node) override;
|
|
|
|
bool visit(MemberAccess const& _node) override;
|
2018-11-09 16:06:30 +00:00
|
|
|
void endVisit(IndexAccess const& _node) override;
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2019-03-11 10:56:08 +00:00
|
|
|
/// Do not visit subtree if node is a RationalNumber.
|
|
|
|
/// Symbolic _expr is the rational literal.
|
|
|
|
bool shortcutRationalNumber(Expression const& _expr);
|
2017-07-11 11:26:43 +00:00
|
|
|
void arithmeticOperation(BinaryOperation const& _op);
|
|
|
|
void compareOperation(BinaryOperation const& _op);
|
|
|
|
void booleanOperation(BinaryOperation const& _op);
|
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
void visitAssert(FunctionCall const& _funCall);
|
|
|
|
void visitRequire(FunctionCall const& _funCall);
|
|
|
|
void visitGasLeft(FunctionCall const& _funCall);
|
2018-12-20 12:20:07 +00:00
|
|
|
void visitTypeConversion(FunctionCall const& _funCall);
|
2018-10-10 12:31:49 +00:00
|
|
|
/// Visits the FunctionDefinition of the called function
|
|
|
|
/// if available and inlines the return value.
|
2018-12-10 16:23:36 +00:00
|
|
|
void inlineFunctionCall(FunctionCall const& _funCall);
|
|
|
|
/// Creates an uninterpreted function call.
|
|
|
|
void abstractFunctionCall(FunctionCall const& _funCall);
|
|
|
|
void visitFunctionIdentifier(Identifier const& _identifier);
|
2018-10-10 12:31:49 +00:00
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
void defineGlobalVariable(std::string const& _name, Expression const& _expr, bool _increaseIndex = false);
|
|
|
|
void defineGlobalFunction(std::string const& _name, Expression const& _expr);
|
2018-12-14 11:21:43 +00:00
|
|
|
/// Handles the side effects of assignment
|
|
|
|
/// to variable of some SMT array type
|
|
|
|
/// while aliasing is not supported.
|
|
|
|
void arrayAssignment();
|
|
|
|
/// Handles assignment to SMT array index.
|
|
|
|
void arrayIndexAssignment(Assignment const& _assignment);
|
2018-10-18 13:03:52 +00:00
|
|
|
|
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);
|
|
|
|
|
2018-11-14 16:11:55 +00:00
|
|
|
void assignment(VariableDeclaration const& _variable, Expression const& _value, langutil::SourceLocation const& _location);
|
|
|
|
void assignment(VariableDeclaration const& _variable, smt::Expression const& _value, langutil::SourceLocation const& _location);
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2017-12-18 18:43:15 +00:00
|
|
|
/// Maps a variable to an SSA index.
|
2018-10-17 09:32:01 +00:00
|
|
|
using VariableIndices = std::unordered_map<VariableDeclaration const*, int>;
|
2017-12-18 18:43:15 +00:00
|
|
|
|
|
|
|
/// Visits the branch given by the statement, pushes and pops the current path conditions.
|
|
|
|
/// @param _condition if present, asserts that this condition is true within the branch.
|
2018-10-17 09:32:01 +00:00
|
|
|
/// @returns the variable indices after visiting the branch.
|
|
|
|
VariableIndices visitBranch(Statement const& _statement, smt::Expression const* _condition = nullptr);
|
|
|
|
VariableIndices 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,
|
2018-11-14 16:11:55 +00:00
|
|
|
langutil::SourceLocation const& _location,
|
2017-07-11 11:26:43 +00:00
|
|
|
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
|
|
|
|
2019-02-06 10:12:02 +00:00
|
|
|
struct OverflowTarget
|
|
|
|
{
|
|
|
|
enum class Type { Underflow, Overflow, All } type;
|
|
|
|
TypePointer intType;
|
|
|
|
smt::Expression value;
|
|
|
|
smt::Expression path;
|
|
|
|
langutil::SourceLocation const& location;
|
|
|
|
|
|
|
|
OverflowTarget(Type _type, TypePointer _intType, smt::Expression _value, smt::Expression _path, langutil::SourceLocation const& _location):
|
|
|
|
type(_type),
|
|
|
|
intType(_intType),
|
|
|
|
value(_value),
|
|
|
|
path(_path),
|
|
|
|
location(_location)
|
|
|
|
{
|
|
|
|
solAssert(dynamic_cast<IntegerType const*>(intType.get()), "");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Checks that the value is in the range given by the type.
|
|
|
|
void checkUnderflow(OverflowTarget& _target);
|
|
|
|
void checkOverflow(OverflowTarget& _target);
|
|
|
|
/// Calls the functions above for all elements in m_overflowTargets accordingly.
|
|
|
|
void checkUnderOverflow();
|
|
|
|
/// Adds an overflow target for lazy check at the end of the function.
|
|
|
|
void addOverflowTarget(OverflowTarget::Type _type, TypePointer _intType, smt::Expression _value, langutil::SourceLocation const& _location);
|
2017-09-29 14:53:26 +00:00
|
|
|
|
|
|
|
std::pair<smt::CheckResult, std::vector<std::string>>
|
2017-12-18 16:31:27 +00:00
|
|
|
checkSatisfiableAndGenerateModel(std::vector<smt::Expression> const& _expressionsToEvaluate);
|
2017-09-29 14:53:26 +00:00
|
|
|
|
2017-12-18 16:31:27 +00:00
|
|
|
smt::CheckResult checkSatisfiable();
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
void initializeLocalVariables(FunctionDefinition const& _function);
|
2018-10-10 12:31:49 +00:00
|
|
|
void initializeFunctionCallParameters(FunctionDefinition const& _function, std::vector<smt::Expression> const& _callArgs);
|
2018-12-20 17:11:20 +00:00
|
|
|
void resetVariable(VariableDeclaration const& _variable);
|
2018-04-18 21:14:45 +00:00
|
|
|
void resetStateVariables();
|
2018-12-20 17:11:20 +00:00
|
|
|
void resetStorageReferences();
|
2018-06-12 08:58:24 +00:00
|
|
|
void resetVariables(std::vector<VariableDeclaration const*> _variables);
|
2018-12-20 17:11:20 +00:00
|
|
|
void resetVariables(std::function<bool(VariableDeclaration const&)> const& _filter);
|
2019-02-21 12:01:58 +00:00
|
|
|
/// @returns the type without storage pointer information if it has it.
|
|
|
|
TypePointer typeWithoutPointer(TypePointer const& _type);
|
|
|
|
|
2017-12-18 18:43:15 +00:00
|
|
|
/// Given two different branches and the touched variables,
|
|
|
|
/// merge the touched variables into after-branch ite variables
|
|
|
|
/// using the branch condition as guard.
|
2018-10-17 09:32:01 +00:00
|
|
|
void mergeVariables(std::vector<VariableDeclaration const*> const& _variables, smt::Expression const& _condition, VariableIndices const& _indicesEndTrue, VariableIndices const& _indicesEndFalse);
|
2017-10-05 17:31:17 +00:00
|
|
|
/// 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-07-14 09:49:27 +00:00
|
|
|
/// @returns true if _delc is a variable that is known at the current point, i.e.
|
2018-10-17 09:32:01 +00:00
|
|
|
/// has a valid index
|
2018-06-12 08:58:24 +00:00
|
|
|
bool knownVariable(VariableDeclaration 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.
|
2018-06-12 08:58:24 +00:00
|
|
|
smt::Expression currentValue(VariableDeclaration const& _decl);
|
2017-07-14 09:49:27 +00:00
|
|
|
/// @returns an expression denoting the value of the variable declared in @a _decl
|
2018-10-17 09:32:01 +00:00
|
|
|
/// at the given index. Does not ensure that this index exists.
|
|
|
|
smt::Expression valueAtIndex(VariableDeclaration const& _decl, int _index);
|
|
|
|
/// Allocates a new index for the declaration, updates the current
|
|
|
|
/// index to this value and returns the expression.
|
2018-06-12 08:58:24 +00:00
|
|
|
smt::Expression newValue(VariableDeclaration const& _decl);
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2017-09-28 13:24:24 +00:00
|
|
|
/// Sets the value of the declaration to zero.
|
2018-06-12 08:58:24 +00:00
|
|
|
void setZeroValue(VariableDeclaration const& _decl);
|
2018-11-22 13:48:31 +00:00
|
|
|
void setZeroValue(SymbolicVariable& _variable);
|
2017-09-28 13:24:24 +00:00
|
|
|
/// Resets the variable to an unknown value (in its range).
|
2018-06-12 08:58:24 +00:00
|
|
|
void setUnknownValue(VariableDeclaration const& decl);
|
2018-11-22 13:48:31 +00:00
|
|
|
void setUnknownValue(SymbolicVariable& _variable);
|
2017-07-14 09:49:27 +00:00
|
|
|
|
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);
|
2018-10-10 12:31:49 +00:00
|
|
|
/// Checks if expression was created
|
2018-10-18 13:03:52 +00:00
|
|
|
bool knownExpr(Expression const& _e) const;
|
2017-10-05 13:23:25 +00:00
|
|
|
/// Creates the expression and sets its value.
|
|
|
|
void defineExpr(Expression const& _e, smt::Expression _value);
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2018-12-10 16:23:36 +00:00
|
|
|
/// Checks if special variable or function was seen.
|
|
|
|
bool knownGlobalSymbol(std::string const& _var) const;
|
2018-10-18 13:03:52 +00:00
|
|
|
|
2017-12-11 19:09:17 +00:00
|
|
|
/// Adds a new path condition
|
|
|
|
void pushPathCondition(smt::Expression const& _e);
|
|
|
|
/// Remove the last path condition
|
|
|
|
void popPathCondition();
|
|
|
|
/// Returns the conjunction of all path conditions or True if empty
|
|
|
|
smt::Expression currentPathConditions();
|
2017-12-13 16:59:36 +00:00
|
|
|
/// Conjoin the current path conditions with the given parameter and add to the solver
|
|
|
|
void addPathConjoinedExpression(smt::Expression const& _e);
|
|
|
|
/// Add to the solver: the given expression implied by the current path conditions
|
|
|
|
void addPathImpliedExpression(smt::Expression const& _e);
|
2017-12-11 19:09:17 +00:00
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
/// Removes local variables from the context.
|
2018-06-06 10:11:58 +00:00
|
|
|
void removeLocalVariables();
|
2018-06-05 09:46:21 +00:00
|
|
|
|
2018-10-15 15:32:17 +00:00
|
|
|
/// Copy the SSA indices of m_variables.
|
2018-10-17 09:32:01 +00:00
|
|
|
VariableIndices copyVariableIndices();
|
|
|
|
/// Resets the variable indices.
|
|
|
|
void resetVariableIndices(VariableIndices const& _indices);
|
2018-10-15 15:32:17 +00:00
|
|
|
|
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;
|
2018-04-19 07:28:44 +00:00
|
|
|
bool m_loopExecutionHappened = false;
|
2018-12-14 11:21:43 +00:00
|
|
|
bool m_arrayAssignmentHappened = false;
|
2019-01-23 10:22:38 +00:00
|
|
|
// True if the "No SMT solver available" warning was already created.
|
|
|
|
bool m_noSolverWarning = false;
|
2018-10-10 12:31:49 +00:00
|
|
|
/// An Expression may have multiple smt::Expression due to
|
|
|
|
/// repeated calls to the same function.
|
2018-10-17 16:00:42 +00:00
|
|
|
std::unordered_map<Expression const*, std::shared_ptr<SymbolicVariable>> m_expressions;
|
2018-10-15 15:32:17 +00:00
|
|
|
std::unordered_map<VariableDeclaration const*, std::shared_ptr<SymbolicVariable>> m_variables;
|
2018-12-10 16:23:36 +00:00
|
|
|
std::unordered_map<std::string, std::shared_ptr<SymbolicVariable>> m_globalContext;
|
2018-10-25 14:00:09 +00:00
|
|
|
/// Stores the instances of an Uninterpreted Function applied to arguments.
|
2018-11-09 16:06:30 +00:00
|
|
|
/// These may be direct application of UFs or Array index access.
|
2018-10-25 14:00:09 +00:00
|
|
|
/// Used to retrieve models.
|
2018-11-09 16:06:30 +00:00
|
|
|
std::set<Expression const*> m_uninterpretedTerms;
|
2017-12-11 19:09:17 +00:00
|
|
|
std::vector<smt::Expression> m_pathConditions;
|
2019-01-23 10:22:38 +00:00
|
|
|
/// ErrorReporter that comes from CompilerStack.
|
|
|
|
langutil::ErrorReporter& m_errorReporterReference;
|
|
|
|
/// Local SMTChecker ErrorReporter.
|
|
|
|
/// This is necessary to show the "No SMT solver available"
|
|
|
|
/// warning before the others in case it's needed.
|
|
|
|
langutil::ErrorReporter m_errorReporter;
|
|
|
|
langutil::ErrorList m_smtErrors;
|
2018-11-14 16:11:55 +00:00
|
|
|
std::shared_ptr<langutil::Scanner> m_scanner;
|
2017-07-11 11:26:43 +00:00
|
|
|
|
2018-10-10 12:31:49 +00:00
|
|
|
/// Stores the current path of function calls.
|
|
|
|
std::vector<FunctionDefinition const*> m_functionPath;
|
|
|
|
/// Returns true if the current function was not visited by
|
|
|
|
/// a function call.
|
|
|
|
bool isRootFunction();
|
|
|
|
/// Returns true if _funDef was already visited.
|
|
|
|
bool visitedFunction(FunctionDefinition const* _funDef);
|
2019-02-06 10:12:02 +00:00
|
|
|
|
|
|
|
std::vector<OverflowTarget> m_overflowTargets;
|
2017-07-06 09:05:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|