2019-07-04 12:44:10 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model checker based on Constrained Horn Clauses.
|
|
|
|
*
|
|
|
|
* A Solidity contract's CFG is encoded into a system of Horn clauses where
|
|
|
|
* each block has a predicate and edges are rules.
|
|
|
|
*
|
|
|
|
* The entry block is the constructor which has no in-edges.
|
|
|
|
* The constructor has one out-edge to an artificial block named _Interface_
|
|
|
|
* which has in/out-edges from/to all public functions.
|
|
|
|
*
|
|
|
|
* Loop invariants for Interface -> Interface' are state invariants.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/formal/SMTEncoder.h>
|
|
|
|
|
|
|
|
#include <libsolidity/formal/CHCSolverInterface.h>
|
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
#include <libsolidity/interface/ReadFile.h>
|
|
|
|
|
2019-07-04 12:44:10 +00:00
|
|
|
#include <set>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2019-07-04 12:44:10 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class CHC: public SMTEncoder
|
|
|
|
{
|
|
|
|
public:
|
2019-09-24 15:35:31 +00:00
|
|
|
CHC(
|
|
|
|
smt::EncodingContext& _context,
|
|
|
|
langutil::ErrorReporter& _errorReporter,
|
2019-12-11 16:31:36 +00:00
|
|
|
std::map<util::h256, std::string> const& _smtlib2Responses,
|
2019-12-05 15:44:26 +00:00
|
|
|
ReadCallback::Callback const& _smtCallback,
|
2019-12-03 13:39:47 +00:00
|
|
|
smt::SMTSolverChoice _enabledSolvers
|
2019-09-24 15:35:31 +00:00
|
|
|
);
|
2019-07-04 12:44:10 +00:00
|
|
|
|
2019-08-06 14:02:11 +00:00
|
|
|
void analyze(SourceUnit const& _sources);
|
2019-07-04 12:44:10 +00:00
|
|
|
|
|
|
|
std::set<Expression const*> const& safeAssertions() const { return m_safeAssertions; }
|
|
|
|
|
2019-09-24 15:35:31 +00:00
|
|
|
/// This is used if the Horn solver is not directly linked into this binary.
|
|
|
|
/// @returns a list of inputs to the Horn solver that were not part of the argument to
|
|
|
|
/// the constructor.
|
|
|
|
std::vector<std::string> unhandledQueries() const;
|
|
|
|
|
2019-07-04 12:44:10 +00:00
|
|
|
private:
|
|
|
|
/// Visitor functions.
|
|
|
|
//@{
|
|
|
|
bool visit(ContractDefinition const& _node) override;
|
|
|
|
void endVisit(ContractDefinition const& _node) override;
|
|
|
|
bool visit(FunctionDefinition const& _node) override;
|
|
|
|
void endVisit(FunctionDefinition const& _node) override;
|
|
|
|
bool visit(IfStatement const& _node) override;
|
2019-07-17 16:01:14 +00:00
|
|
|
bool visit(WhileStatement const&) override;
|
|
|
|
bool visit(ForStatement const&) override;
|
2019-07-04 12:44:10 +00:00
|
|
|
void endVisit(FunctionCall const& _node) override;
|
2019-08-20 13:03:45 +00:00
|
|
|
void endVisit(Break const& _node) override;
|
|
|
|
void endVisit(Continue const& _node) override;
|
2019-07-04 12:44:10 +00:00
|
|
|
|
|
|
|
void visitAssert(FunctionCall const& _funCall);
|
2019-07-17 16:01:14 +00:00
|
|
|
void unknownFunctionCall(FunctionCall const& _funCall);
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Helpers.
|
|
|
|
//@{
|
|
|
|
void reset();
|
2019-07-17 16:01:14 +00:00
|
|
|
void eraseKnowledge();
|
2019-07-04 12:44:10 +00:00
|
|
|
bool shouldVisit(ContractDefinition const& _contract) const;
|
|
|
|
bool shouldVisit(FunctionDefinition const& _function) const;
|
2019-09-26 14:12:27 +00:00
|
|
|
void setCurrentBlock(smt::SymbolicFunctionVariable const& _block, std::vector<smt::Expression> const* _arguments = nullptr);
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Sort helpers.
|
|
|
|
//@{
|
|
|
|
smt::SortPointer constructorSort();
|
|
|
|
smt::SortPointer interfaceSort();
|
2019-07-17 16:01:14 +00:00
|
|
|
smt::SortPointer sort(FunctionDefinition const& _function);
|
2019-08-20 13:03:45 +00:00
|
|
|
smt::SortPointer sort(ASTNode const* _block);
|
2019-07-09 14:39:30 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Predicate helpers.
|
|
|
|
//@{
|
|
|
|
/// @returns a new block of given _sort and _name.
|
2019-08-20 13:03:45 +00:00
|
|
|
std::unique_ptr<smt::SymbolicFunctionVariable> createSymbolicBlock(smt::SortPointer _sort, std::string const& _name);
|
2019-07-09 14:39:30 +00:00
|
|
|
|
|
|
|
/// Interface predicate over current variables.
|
|
|
|
smt::Expression interface();
|
|
|
|
/// Error predicate over current variables.
|
|
|
|
smt::Expression error();
|
2019-08-20 13:03:45 +00:00
|
|
|
smt::Expression error(unsigned _idx);
|
2019-07-17 16:01:14 +00:00
|
|
|
|
2019-09-16 18:14:27 +00:00
|
|
|
/// Creates a block for the given _node.
|
|
|
|
std::unique_ptr<smt::SymbolicFunctionVariable> createBlock(ASTNode const* _node, std::string const& _prefix = "");
|
2019-08-20 13:03:45 +00:00
|
|
|
|
|
|
|
/// Creates a new error block to be used by an assertion.
|
|
|
|
/// Also registers the predicate.
|
|
|
|
void createErrorBlock();
|
|
|
|
|
|
|
|
void connectBlocks(smt::Expression const& _from, smt::Expression const& _to, smt::Expression const& _constraints = smt::Expression(true));
|
2019-07-17 16:01:14 +00:00
|
|
|
|
2019-09-26 14:12:27 +00:00
|
|
|
/// @returns the current symbolic values of the current state variables.
|
|
|
|
std::vector<smt::Expression> currentStateVariables();
|
|
|
|
|
2019-07-17 16:01:14 +00:00
|
|
|
/// @returns the current symbolic values of the current function's
|
|
|
|
/// input and output parameters.
|
|
|
|
std::vector<smt::Expression> currentFunctionVariables();
|
2019-09-26 14:12:27 +00:00
|
|
|
/// @returns the same as currentFunctionVariables plus
|
2019-07-17 16:01:14 +00:00
|
|
|
/// local variables.
|
|
|
|
std::vector<smt::Expression> currentBlockVariables();
|
|
|
|
|
2019-08-20 13:03:45 +00:00
|
|
|
/// @returns the predicate name for a given node.
|
|
|
|
std::string predicateName(ASTNode const* _node);
|
|
|
|
/// @returns a predicate application over the current scoped variables.
|
2019-09-16 18:14:27 +00:00
|
|
|
smt::Expression predicate(smt::SymbolicFunctionVariable const& _block);
|
|
|
|
/// @returns a predicate application over @param _arguments.
|
|
|
|
smt::Expression predicate(smt::SymbolicFunctionVariable const& _block, std::vector<smt::Expression> const& _arguments);
|
2019-07-09 14:39:30 +00:00
|
|
|
//@}
|
|
|
|
|
2019-07-04 12:44:10 +00:00
|
|
|
/// Solver related.
|
|
|
|
//@{
|
2019-08-20 13:03:45 +00:00
|
|
|
/// Adds Horn rule to the solver.
|
|
|
|
void addRule(smt::Expression const& _rule, std::string const& _ruleName);
|
2019-07-04 12:44:10 +00:00
|
|
|
/// @returns true if query is unsatisfiable (safe).
|
|
|
|
bool query(smt::Expression const& _query, langutil::SourceLocation const& _location);
|
|
|
|
//@}
|
|
|
|
|
2019-09-16 18:14:27 +00:00
|
|
|
/// Misc.
|
|
|
|
//@{
|
|
|
|
/// Returns a prefix to be used in a new unique block name
|
|
|
|
/// and increases the block counter.
|
|
|
|
std::string uniquePrefix();
|
|
|
|
//@}
|
|
|
|
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Predicates.
|
|
|
|
//@{
|
2019-09-26 14:12:27 +00:00
|
|
|
/// Genesis predicate.
|
|
|
|
std::unique_ptr<smt::SymbolicFunctionVariable> m_genesisPredicate;
|
|
|
|
|
|
|
|
/// Implicit constructor predicate.
|
|
|
|
/// Explicit constructors are handled as functions.
|
2019-11-10 17:58:29 +00:00
|
|
|
std::unique_ptr<smt::SymbolicFunctionVariable> m_constructorPredicate;
|
2019-07-09 14:39:30 +00:00
|
|
|
|
|
|
|
/// Artificial Interface predicate.
|
|
|
|
/// Single entry block for all functions.
|
2019-11-10 17:58:29 +00:00
|
|
|
std::unique_ptr<smt::SymbolicFunctionVariable> m_interfacePredicate;
|
2019-07-09 14:39:30 +00:00
|
|
|
|
|
|
|
/// Artificial Error predicate.
|
|
|
|
/// Single error block for all assertions.
|
2019-11-10 17:58:29 +00:00
|
|
|
std::unique_ptr<smt::SymbolicFunctionVariable> m_errorPredicate;
|
2019-07-09 14:39:30 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Variables.
|
|
|
|
//@{
|
|
|
|
/// State variables sorts.
|
|
|
|
/// Used by all predicates.
|
|
|
|
std::vector<smt::SortPointer> m_stateSorts;
|
|
|
|
/// State variables.
|
|
|
|
/// Used to create all predicates.
|
|
|
|
std::vector<VariableDeclaration const*> m_stateVariables;
|
|
|
|
//@}
|
|
|
|
|
2019-07-04 12:44:10 +00:00
|
|
|
/// Verification targets.
|
|
|
|
//@{
|
|
|
|
std::vector<Expression const*> m_verificationTargets;
|
|
|
|
|
|
|
|
/// Assertions proven safe.
|
|
|
|
std::set<Expression const*> m_safeAssertions;
|
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Control-flow.
|
|
|
|
//@{
|
|
|
|
FunctionDefinition const* m_currentFunction = nullptr;
|
2019-07-17 16:01:14 +00:00
|
|
|
|
2019-09-16 18:14:27 +00:00
|
|
|
/// The current block.
|
|
|
|
smt::Expression m_currentBlock = smt::Expression(true);
|
|
|
|
|
|
|
|
/// Counter to generate unique block names.
|
|
|
|
unsigned m_blockCounter = 0;
|
|
|
|
|
2019-07-17 16:01:14 +00:00
|
|
|
/// Whether a function call was seen in the current scope.
|
|
|
|
bool m_unknownFunctionCallSeen = false;
|
2019-08-20 13:03:45 +00:00
|
|
|
|
|
|
|
/// Block where a loop break should go to.
|
2019-09-16 18:14:27 +00:00
|
|
|
smt::SymbolicFunctionVariable const* m_breakDest = nullptr;
|
2019-08-20 13:03:45 +00:00
|
|
|
/// Block where a loop continue should go to.
|
2019-09-16 18:14:27 +00:00
|
|
|
smt::SymbolicFunctionVariable const* m_continueDest = nullptr;
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// CHC solver.
|
2020-01-04 20:45:21 +00:00
|
|
|
std::unique_ptr<smt::CHCSolverInterface> m_interface;
|
2019-07-04 12:44:10 +00:00
|
|
|
|
|
|
|
/// ErrorReporter that comes from CompilerStack.
|
|
|
|
langutil::ErrorReporter& m_outerErrorReporter;
|
2019-12-03 15:50:28 +00:00
|
|
|
|
|
|
|
/// SMT solvers that are chosen at runtime.
|
|
|
|
smt::SMTSolverChoice m_enabledSolvers;
|
2019-07-04 12:44:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|