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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-07-04 12:44:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
2021-01-19 11:56:22 +00:00
|
|
|
#include <libsolidity/formal/ModelCheckerSettings.h>
|
2020-08-13 12:00:33 +00:00
|
|
|
#include <libsolidity/formal/Predicate.h>
|
2019-07-04 12:44:10 +00:00
|
|
|
#include <libsolidity/formal/SMTEncoder.h>
|
|
|
|
|
2019-09-17 14:06:43 +00:00
|
|
|
#include <libsolidity/interface/ReadFile.h>
|
|
|
|
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/CHCSolverInterface.h>
|
|
|
|
|
2021-07-02 12:42:53 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
2021-08-27 09:40:20 +00:00
|
|
|
#include <liblangutil/UniqueErrorReporter.h>
|
2021-07-02 12:42:53 +00:00
|
|
|
|
2020-08-20 08:43:50 +00:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
|
2020-07-10 08:28:49 +00:00
|
|
|
#include <map>
|
2020-07-13 19:10:44 +00:00
|
|
|
#include <optional>
|
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,
|
2021-08-27 09:40:20 +00:00
|
|
|
langutil::UniqueErrorReporter& _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,
|
2022-05-15 15:45:43 +00:00
|
|
|
ModelCheckerSettings _settings,
|
2021-07-01 15:28:06 +00:00
|
|
|
langutil::CharStreamProvider const& _charStreamProvider
|
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
|
|
|
|
2021-07-02 12:42:53 +00:00
|
|
|
struct ReportTargetInfo
|
|
|
|
{
|
|
|
|
langutil::ErrorId error;
|
|
|
|
langutil::SourceLocation location;
|
|
|
|
std::string message;
|
|
|
|
};
|
|
|
|
std::map<ASTNode const*, std::set<VerificationTargetType>, smt::EncodingContext::IdCompare> const& safeTargets() const { return m_safeTargets; }
|
|
|
|
std::map<ASTNode const*, std::map<VerificationTargetType, ReportTargetInfo>, smt::EncodingContext::IdCompare> const& unsafeTargets() const { return m_unsafeTargets; }
|
2019-07-04 12:44:10 +00:00
|
|
|
|
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;
|
|
|
|
|
2021-04-27 11:30:19 +00:00
|
|
|
enum class CHCNatspecOption
|
|
|
|
{
|
|
|
|
AbstractFunctionNondet
|
|
|
|
};
|
|
|
|
|
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;
|
2021-01-21 18:04:34 +00:00
|
|
|
bool visit(Block const& _block) override;
|
|
|
|
void endVisit(Block const& _block) override;
|
2019-07-04 12:44:10 +00:00
|
|
|
bool visit(IfStatement const& _node) override;
|
2019-07-17 16:01:14 +00:00
|
|
|
bool visit(WhileStatement const&) override;
|
|
|
|
bool visit(ForStatement const&) override;
|
2021-01-21 18:04:34 +00:00
|
|
|
void endVisit(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;
|
2020-09-24 17:24:33 +00:00
|
|
|
void endVisit(IndexRangeAccess const& _node) override;
|
2020-07-27 17:39:17 +00:00
|
|
|
void endVisit(Return const& _node) override;
|
2021-01-21 18:04:34 +00:00
|
|
|
bool visit(TryCatchClause const&) override;
|
|
|
|
void endVisit(TryCatchClause const&) override;
|
2021-01-04 18:33:07 +00:00
|
|
|
bool visit(TryStatement const& _node) override;
|
2020-07-27 17:39:17 +00:00
|
|
|
|
|
|
|
void pushInlineFrame(CallableDeclaration const& _callable) override;
|
|
|
|
void popInlineFrame(CallableDeclaration const& _callable) override;
|
2019-07-04 12:44:10 +00:00
|
|
|
|
|
|
|
void visitAssert(FunctionCall const& _funCall);
|
2020-09-28 10:09:40 +00:00
|
|
|
void visitAddMulMod(FunctionCall const& _funCall) override;
|
2020-02-12 02:21:42 +00:00
|
|
|
void internalFunctionCall(FunctionCall const& _funCall);
|
2020-05-14 18:09:52 +00:00
|
|
|
void externalFunctionCall(FunctionCall const& _funCall);
|
2020-11-09 13:50:41 +00:00
|
|
|
void externalFunctionCallToTrustedCode(FunctionCall const& _funCall);
|
2019-07-17 16:01:14 +00:00
|
|
|
void unknownFunctionCall(FunctionCall const& _funCall);
|
2020-05-17 21:21:08 +00:00
|
|
|
void makeArrayPopVerificationTarget(FunctionCall const& _arrayPop) override;
|
2021-03-23 18:15:14 +00:00
|
|
|
void makeOutOfBoundsVerificationTarget(IndexAccess const& _access) override;
|
2020-08-11 10:01:45 +00:00
|
|
|
/// Creates underflow/overflow verification targets.
|
|
|
|
std::pair<smtutil::Expression, smtutil::Expression> arithmeticOperation(
|
|
|
|
Token _op,
|
|
|
|
smtutil::Expression const& _left,
|
|
|
|
smtutil::Expression const& _right,
|
2021-03-22 16:12:05 +00:00
|
|
|
Type const* _commonType,
|
2020-08-11 10:01:45 +00:00
|
|
|
Expression const& _expression
|
|
|
|
) override;
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Helpers.
|
|
|
|
//@{
|
2020-02-12 01:52:35 +00:00
|
|
|
void resetSourceAnalysis();
|
|
|
|
void resetContractAnalysis();
|
2019-07-17 16:01:14 +00:00
|
|
|
void eraseKnowledge();
|
2020-02-12 01:12:42 +00:00
|
|
|
void clearIndices(ContractDefinition const* _contract, FunctionDefinition const* _function = nullptr) override;
|
2020-09-16 09:47:54 +00:00
|
|
|
void setCurrentBlock(Predicate const& _block);
|
2020-10-27 08:24:32 +00:00
|
|
|
std::set<unsigned> transactionVerificationTargetsIds(ASTNode const* _txRoot);
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
2021-04-27 11:30:19 +00:00
|
|
|
/// SMT Natspec and abstraction helpers.
|
|
|
|
//@{
|
|
|
|
/// @returns a CHCNatspecOption enum if _option is a valid SMTChecker Natspec value
|
|
|
|
/// or nullopt otherwise.
|
|
|
|
static std::optional<CHCNatspecOption> natspecOptionFromString(std::string const& _option);
|
|
|
|
/// @returns which SMTChecker options are enabled by @a _function's Natspec via
|
|
|
|
/// `@custom:smtchecker <option>` or nullopt if none is used.
|
|
|
|
std::set<CHCNatspecOption> smtNatspecTags(FunctionDefinition const& _function);
|
|
|
|
/// @returns true if _function is Natspec annotated to be abstracted by
|
|
|
|
/// nondeterministic values.
|
|
|
|
bool abstractAsNondet(FunctionDefinition const& _function);
|
|
|
|
//@}
|
|
|
|
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Sort helpers.
|
|
|
|
//@{
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::SortPointer sort(FunctionDefinition const& _function);
|
|
|
|
smtutil::SortPointer sort(ASTNode const* _block);
|
2019-07-09 14:39:30 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Predicate helpers.
|
|
|
|
//@{
|
|
|
|
/// @returns a new block of given _sort and _name.
|
2021-01-22 10:42:40 +00:00
|
|
|
Predicate const* createSymbolicBlock(smtutil::SortPointer _sort, std::string const& _name, PredicateType _predType, ASTNode const* _node = nullptr, ContractDefinition const* _contractContext = nullptr);
|
2019-07-09 14:39:30 +00:00
|
|
|
|
2020-02-12 01:52:35 +00:00
|
|
|
/// Creates summary predicates for all functions of all contracts
|
|
|
|
/// in a given _source.
|
|
|
|
void defineInterfacesAndSummaries(SourceUnit const& _source);
|
|
|
|
|
2021-08-20 13:49:36 +00:00
|
|
|
/// Creates the rule
|
|
|
|
/// summary_function \land transaction_entry_constraints => external_summary_function
|
|
|
|
/// This is needed to add these transaction entry constraints which include
|
|
|
|
/// potential balance increase by external means, for example.
|
|
|
|
void defineExternalFunctionInterface(FunctionDefinition const& _function, ContractDefinition const& _contract);
|
|
|
|
|
2020-11-09 15:37:08 +00:00
|
|
|
/// Creates a CHC system that, for a given contract,
|
|
|
|
/// - initializes its state variables (as 0 or given value, if any).
|
|
|
|
/// - "calls" the explicit constructor function of the contract, if any.
|
2021-03-03 19:24:05 +00:00
|
|
|
void defineContractInitializer(ContractDefinition const& _contract, ContractDefinition const& _contractContext);
|
2020-11-09 15:37:08 +00:00
|
|
|
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Interface predicate over current variables.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression interface();
|
|
|
|
smtutil::Expression interface(ContractDefinition const& _contract);
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Error predicate over current variables.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression error();
|
|
|
|
smtutil::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.
|
2020-09-16 09:47:54 +00:00
|
|
|
Predicate const* createBlock(ASTNode const* _node, PredicateType _predType, std::string const& _prefix = "");
|
2020-02-12 01:52:35 +00:00
|
|
|
/// Creates a call block for the given function _function from contract _contract.
|
|
|
|
/// The contract is needed here because of inheritance.
|
2021-01-06 15:34:16 +00:00
|
|
|
/// There are different types of summaries, where the most common is FunctionSummary,
|
|
|
|
/// but other summaries are also used for internal and external function calls.
|
|
|
|
Predicate const* createSummaryBlock(
|
|
|
|
FunctionDefinition const& _function,
|
|
|
|
ContractDefinition const& _contract,
|
|
|
|
PredicateType _type = PredicateType::FunctionSummary
|
|
|
|
);
|
2019-08-20 13:03:45 +00:00
|
|
|
|
2020-11-09 15:37:08 +00:00
|
|
|
/// @returns a block related to @a _contract's constructor.
|
|
|
|
Predicate const* createConstructorBlock(ContractDefinition const& _contract, 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();
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
void connectBlocks(smtutil::Expression const& _from, smtutil::Expression const& _to, smtutil::Expression const& _constraints = smtutil::Expression(true));
|
2019-07-17 16:01:14 +00:00
|
|
|
|
2020-11-09 15:37:08 +00:00
|
|
|
/// @returns The initial constraints that set up the beginning of a function.
|
|
|
|
smtutil::Expression initialConstraints(ContractDefinition const& _contract, FunctionDefinition const* _function = nullptr);
|
|
|
|
|
2020-02-12 01:52:35 +00:00
|
|
|
/// @returns the symbolic values of the state variables at the beginning
|
|
|
|
/// of the current transaction.
|
2020-05-19 12:14:46 +00:00
|
|
|
std::vector<smtutil::Expression> initialStateVariables();
|
2020-07-08 09:47:03 +00:00
|
|
|
std::vector<smtutil::Expression> stateVariablesAtIndex(unsigned _index);
|
|
|
|
std::vector<smtutil::Expression> stateVariablesAtIndex(unsigned _index, ContractDefinition const& _contract);
|
2019-09-26 14:12:27 +00:00
|
|
|
/// @returns the current symbolic values of the current state variables.
|
2020-05-19 12:14:46 +00:00
|
|
|
std::vector<smtutil::Expression> currentStateVariables();
|
2020-05-14 18:09:52 +00:00
|
|
|
std::vector<smtutil::Expression> currentStateVariables(ContractDefinition const& _contract);
|
2019-09-26 14:12:27 +00:00
|
|
|
|
2021-04-27 11:30:19 +00:00
|
|
|
/// @returns \bigwedge currentValue(_vars[i]) == initialState(_var[i])
|
|
|
|
smtutil::Expression currentEqualInitialVarsConstraints(std::vector<VariableDeclaration const*> const& _vars) const;
|
|
|
|
|
2019-08-20 13:03:45 +00:00
|
|
|
/// @returns the predicate name for a given node.
|
2020-02-12 01:52:35 +00:00
|
|
|
std::string predicateName(ASTNode const* _node, ContractDefinition const* _contract = nullptr);
|
2020-09-16 09:47:54 +00:00
|
|
|
/// @returns a predicate application after checking the predicate's type.
|
2020-08-13 12:00:33 +00:00
|
|
|
smtutil::Expression predicate(Predicate const& _block);
|
2020-02-12 02:21:42 +00:00
|
|
|
/// @returns the summary predicate for the called function.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression predicate(FunctionCall const& _funCall);
|
2021-03-03 19:24:05 +00:00
|
|
|
/// @returns a predicate that defines a contract initializer for _contract in the context of _contractContext.
|
|
|
|
smtutil::Expression initializer(ContractDefinition const& _contract, ContractDefinition const& _contractContext);
|
2020-02-12 01:52:35 +00:00
|
|
|
/// @returns a predicate that defines a constructor summary.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression summary(ContractDefinition const& _contract);
|
2020-02-12 01:52:35 +00:00
|
|
|
/// @returns a predicate that defines a function summary.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression summary(FunctionDefinition const& _function);
|
2020-05-14 18:09:52 +00:00
|
|
|
smtutil::Expression summary(FunctionDefinition const& _function, ContractDefinition const& _contract);
|
2021-08-20 13:49:36 +00:00
|
|
|
/// @returns a predicate that applies a function summary
|
|
|
|
/// over the constrained variables.
|
|
|
|
smtutil::Expression summaryCall(FunctionDefinition const& _function);
|
|
|
|
smtutil::Expression summaryCall(FunctionDefinition const& _function, ContractDefinition const& _contract);
|
|
|
|
/// @returns a predicate that defines an external function summary.
|
|
|
|
smtutil::Expression externalSummary(FunctionDefinition const& _function);
|
|
|
|
smtutil::Expression externalSummary(FunctionDefinition const& _function, ContractDefinition const& _contract);
|
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.
|
2020-05-19 12:14:46 +00:00
|
|
|
void addRule(smtutil::Expression const& _rule, std::string const& _ruleName);
|
2021-10-06 09:50:41 +00:00
|
|
|
/// @returns <true, invariant, empty> if query is unsatisfiable (safe).
|
|
|
|
/// @returns <false, Expression(true), model> otherwise.
|
|
|
|
std::tuple<smtutil::CheckResult, smtutil::Expression, smtutil::CHCSolverInterface::CexGraph> query(smtutil::Expression const& _query, langutil::SourceLocation const& _location);
|
2020-02-12 01:52:35 +00:00
|
|
|
|
2021-01-19 11:56:22 +00:00
|
|
|
void verificationTargetEncountered(ASTNode const* const _errorNode, VerificationTargetType _type, smtutil::Expression const& _errorCondition);
|
2020-07-10 08:28:49 +00:00
|
|
|
|
|
|
|
void checkVerificationTargets();
|
2021-10-20 14:21:34 +00:00
|
|
|
// Forward declarations. Definitions are below.
|
2020-07-10 08:28:49 +00:00
|
|
|
struct CHCVerificationTarget;
|
2021-10-20 14:21:34 +00:00
|
|
|
struct CHCQueryPlaceholder;
|
2020-07-10 08:28:49 +00:00
|
|
|
void checkAssertTarget(ASTNode const* _scope, CHCVerificationTarget const& _target);
|
|
|
|
void checkAndReportTarget(
|
|
|
|
CHCVerificationTarget const& _target,
|
2021-10-20 14:21:34 +00:00
|
|
|
std::vector<CHCQueryPlaceholder> const& _placeholders,
|
2020-07-13 19:10:44 +00:00
|
|
|
langutil::ErrorId _errorReporterId,
|
2020-07-10 08:28:49 +00:00
|
|
|
std::string _satMsg,
|
2020-07-13 19:10:44 +00:00
|
|
|
std::string _unknownMsg = ""
|
2020-07-10 08:28:49 +00:00
|
|
|
);
|
2020-07-13 19:10:44 +00:00
|
|
|
|
|
|
|
std::optional<std::string> generateCounterexample(smtutil::CHCSolverInterface::CexGraph const& _graph, std::string const& _root);
|
2020-08-20 08:43:50 +00:00
|
|
|
|
2021-01-06 10:55:42 +00:00
|
|
|
/// @returns a call graph for function summaries in the counterexample graph.
|
|
|
|
/// The returned map also contains a key _root, whose value are the
|
|
|
|
/// summaries called by _root.
|
|
|
|
std::map<unsigned, std::vector<unsigned>> summaryCalls(
|
|
|
|
smtutil::CHCSolverInterface::CexGraph const& _graph,
|
|
|
|
unsigned _root
|
|
|
|
);
|
|
|
|
|
2020-08-20 08:43:50 +00:00
|
|
|
/// @returns a set of pairs _var = _value separated by _separator.
|
|
|
|
template <typename T>
|
2020-10-19 11:21:05 +00:00
|
|
|
std::string formatVariableModel(std::vector<T> const& _variables, std::vector<std::optional<std::string>> const& _values, std::string const& _separator) const
|
2020-08-20 08:43:50 +00:00
|
|
|
{
|
|
|
|
solAssert(_variables.size() == _values.size(), "");
|
|
|
|
|
|
|
|
std::vector<std::string> assignments;
|
|
|
|
for (unsigned i = 0; i < _values.size(); ++i)
|
|
|
|
{
|
|
|
|
auto var = _variables.at(i);
|
2020-10-19 15:13:21 +00:00
|
|
|
if (var && _values.at(i))
|
2020-10-19 11:21:05 +00:00
|
|
|
assignments.emplace_back(var->name() + " = " + *_values.at(i));
|
2020-08-20 08:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return boost::algorithm::join(assignments, _separator);
|
|
|
|
}
|
2020-07-17 10:26:55 +00:00
|
|
|
|
|
|
|
/// @returns a DAG in the dot format.
|
|
|
|
/// Used for debugging purposes.
|
|
|
|
std::string cex2dot(smtutil::CHCSolverInterface::CexGraph const& _graph);
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
2019-09-16 18:14:27 +00:00
|
|
|
/// Misc.
|
|
|
|
//@{
|
2020-09-08 14:52:58 +00:00
|
|
|
/// @returns a prefix to be used in a new unique block name
|
2019-09-16 18:14:27 +00:00
|
|
|
/// and increases the block counter.
|
|
|
|
std::string uniquePrefix();
|
2020-07-10 08:28:49 +00:00
|
|
|
|
2020-09-08 14:52:58 +00:00
|
|
|
/// @returns a suffix to be used by contract related predicates.
|
|
|
|
std::string contractSuffix(ContractDefinition const& _contract);
|
|
|
|
|
2020-07-10 08:28:49 +00:00
|
|
|
/// @returns a new unique error id associated with _expr and stores
|
|
|
|
/// it into m_errorIds.
|
2020-10-27 08:24:32 +00:00
|
|
|
unsigned newErrorId();
|
2020-09-15 17:17:18 +00:00
|
|
|
|
|
|
|
smt::SymbolicIntVariable& errorFlag();
|
2019-09-16 18:14:27 +00:00
|
|
|
//@}
|
|
|
|
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Predicates.
|
|
|
|
//@{
|
|
|
|
/// Artificial Interface predicate.
|
|
|
|
/// Single entry block for all functions.
|
2020-08-13 12:00:33 +00:00
|
|
|
std::map<ContractDefinition const*, Predicate const*> m_interfaces;
|
2019-07-09 14:39:30 +00:00
|
|
|
|
2020-05-14 18:09:52 +00:00
|
|
|
/// Nondeterministic interfaces.
|
|
|
|
/// These are used when the analyzed contract makes external calls to unknown code,
|
|
|
|
/// which means that the analyzed contract can potentially be called
|
|
|
|
/// nondeterministically.
|
2020-08-13 12:00:33 +00:00
|
|
|
std::map<ContractDefinition const*, Predicate const*> m_nondetInterfaces;
|
2020-05-14 18:09:52 +00:00
|
|
|
|
2020-11-09 15:37:08 +00:00
|
|
|
std::map<ContractDefinition const*, Predicate const*> m_constructorSummaries;
|
2021-03-03 19:24:05 +00:00
|
|
|
std::map<ContractDefinition const*, std::map<ContractDefinition const*, Predicate const*>> m_contractInitializers;
|
2020-11-09 15:37:08 +00:00
|
|
|
|
2019-07-09 14:39:30 +00:00
|
|
|
/// Artificial Error predicate.
|
|
|
|
/// Single error block for all assertions.
|
2020-08-13 12:00:33 +00:00
|
|
|
Predicate const* m_errorPredicate = nullptr;
|
2020-02-12 01:52:35 +00:00
|
|
|
|
|
|
|
/// Function predicates.
|
2020-08-13 12:00:33 +00:00
|
|
|
std::map<ContractDefinition const*, std::map<FunctionDefinition const*, Predicate const*>> m_summaries;
|
2021-08-20 13:49:36 +00:00
|
|
|
|
|
|
|
/// External function predicates.
|
|
|
|
std::map<ContractDefinition const*, std::map<FunctionDefinition const*, Predicate const*>> m_externalSummaries;
|
2019-07-09 14:39:30 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Variables.
|
|
|
|
//@{
|
|
|
|
/// State variables.
|
|
|
|
/// Used to create all predicates.
|
|
|
|
std::vector<VariableDeclaration const*> m_stateVariables;
|
|
|
|
//@}
|
|
|
|
|
2019-07-04 12:44:10 +00:00
|
|
|
/// Verification targets.
|
|
|
|
//@{
|
2020-02-12 01:52:35 +00:00
|
|
|
struct CHCVerificationTarget: VerificationTarget
|
|
|
|
{
|
2020-10-27 08:24:32 +00:00
|
|
|
unsigned const errorId;
|
|
|
|
ASTNode const* const errorNode;
|
2020-02-12 01:52:35 +00:00
|
|
|
};
|
|
|
|
|
2020-10-27 08:24:32 +00:00
|
|
|
/// Query placeholder stores information necessary to create the final query edge in the CHC system.
|
|
|
|
/// It is combined with the unique error id (and error type) to create a complete Verification Target.
|
|
|
|
struct CHCQueryPlaceholder
|
|
|
|
{
|
|
|
|
smtutil::Expression const constraints;
|
|
|
|
smtutil::Expression const errorExpression;
|
|
|
|
smtutil::Expression const fromPredicate;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Query placeholders for constructors, if the key has type ContractDefinition*,
|
|
|
|
/// or external functions, if the key has type FunctionDefinition*.
|
|
|
|
/// A placeholder is created for each possible context of a function (e.g. multiple contracts in contract inheritance hierarchy).
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<ASTNode const*, std::vector<CHCQueryPlaceholder>, smt::EncodingContext::IdCompare> m_queryPlaceholders;
|
2020-10-27 08:24:32 +00:00
|
|
|
|
|
|
|
/// Records verification conditions IDs per function encountered during an analysis of that function.
|
|
|
|
/// The key is the ASTNode of the function where the verification condition has been encountered,
|
|
|
|
/// or the ASTNode of the contract if the verification condition happens inside an implicit constructor.
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<ASTNode const*, std::vector<unsigned>, smt::EncodingContext::IdCompare> m_functionTargetIds;
|
2020-10-27 08:24:32 +00:00
|
|
|
/// Helper mapping unique IDs to actual verification targets.
|
|
|
|
std::map<unsigned, CHCVerificationTarget> m_verificationTargets;
|
2019-07-04 12:44:10 +00:00
|
|
|
|
2021-07-02 12:42:53 +00:00
|
|
|
/// Targets proved safe.
|
|
|
|
std::map<ASTNode const*, std::set<VerificationTargetType>, smt::EncodingContext::IdCompare> m_safeTargets;
|
|
|
|
/// Targets proved unsafe.
|
|
|
|
std::map<ASTNode const*, std::map<VerificationTargetType, ReportTargetInfo>, smt::EncodingContext::IdCompare> m_unsafeTargets;
|
|
|
|
/// Targets not proved.
|
|
|
|
std::map<ASTNode const*, std::map<VerificationTargetType, ReportTargetInfo>, smt::EncodingContext::IdCompare> m_unprovedTargets;
|
2021-10-06 09:50:41 +00:00
|
|
|
|
|
|
|
/// Inferred invariants.
|
|
|
|
std::map<Predicate const*, std::set<std::string>, PredicateCompare> m_invariants;
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// Control-flow.
|
|
|
|
//@{
|
|
|
|
FunctionDefinition const* m_currentFunction = nullptr;
|
2019-07-17 16:01:14 +00:00
|
|
|
|
2020-11-06 11:47:31 +00:00
|
|
|
std::map<ASTNode const*, std::set<ASTNode const*, smt::EncodingContext::IdCompare>, smt::EncodingContext::IdCompare> m_callGraph;
|
2020-02-12 01:52:35 +00:00
|
|
|
|
2019-09-16 18:14:27 +00:00
|
|
|
/// The current block.
|
2020-05-19 12:14:46 +00:00
|
|
|
smtutil::Expression m_currentBlock = smtutil::Expression(true);
|
2019-09-16 18:14:27 +00:00
|
|
|
|
|
|
|
/// 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.
|
2020-11-09 15:37:08 +00:00
|
|
|
Predicate const* m_breakDest = nullptr;
|
2019-08-20 13:03:45 +00:00
|
|
|
/// Block where a loop continue should go to.
|
2020-11-09 15:37:08 +00:00
|
|
|
Predicate const* m_continueDest = nullptr;
|
|
|
|
|
|
|
|
/// Block where an error condition should go to.
|
|
|
|
/// This can be:
|
|
|
|
/// 1) Constructor initializer summary, if error happens while evaluating initial values of state variables.
|
|
|
|
/// 2) Constructor summary, if error happens while evaluating base constructor arguments.
|
|
|
|
/// 3) Function summary, if error happens inside a function.
|
|
|
|
Predicate const* m_errorDest = nullptr;
|
2020-07-27 17:39:17 +00:00
|
|
|
|
|
|
|
/// Represents the stack of destinations where a `return` should go.
|
|
|
|
/// This is different from `m_errorDest` above:
|
|
|
|
/// - Constructor initializers and constructor summaries will never be `return` targets because they are artificial.
|
|
|
|
/// - Modifiers also have their own `return` target blocks, whereas they do not have their own error destination.
|
|
|
|
std::vector<Predicate const*> m_returnDests;
|
2019-07-04 12:44:10 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// CHC solver.
|
2020-05-19 12:14:46 +00:00
|
|
|
std::unique_ptr<smtutil::CHCSolverInterface> m_interface;
|
2022-05-10 17:04:37 +00:00
|
|
|
|
|
|
|
std::map<util::h256, std::string> const& m_smtlib2Responses;
|
|
|
|
ReadCallback::Callback const& m_smtCallback;
|
2019-07-04 12:44:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|