2018-05-04 13:58: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
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/ast/ASTVisitor.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2020-03-05 09:47:01 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2019-01-08 18:33:46 +00:00
|
|
|
#include <liblangutil/SourceLocation.h>
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <stack>
|
2020-04-01 03:04:29 +00:00
|
|
|
#include <utility>
|
2018-05-04 13:58:10 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::frontend
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
/**
|
|
|
|
* Occurrence of a variable in a block of control flow.
|
|
|
|
* Stores the declaration of the referenced variable, the
|
2020-03-05 09:47:01 +00:00
|
|
|
* kind of the occurrence and possibly the source location
|
|
|
|
* at which it occurred.
|
2019-02-13 15:56:46 +00:00
|
|
|
*/
|
2018-12-07 17:20:35 +00:00
|
|
|
class VariableOccurrence
|
2018-05-04 13:58:10 +00:00
|
|
|
{
|
2018-12-07 17:20:35 +00:00
|
|
|
public:
|
|
|
|
enum class Kind
|
|
|
|
{
|
|
|
|
Declaration,
|
|
|
|
Access,
|
|
|
|
Return,
|
|
|
|
Assignment,
|
|
|
|
InlineAssembly
|
|
|
|
};
|
2020-04-01 03:04:29 +00:00
|
|
|
VariableOccurrence(VariableDeclaration const& _declaration, Kind _kind, std::optional<langutil::SourceLocation> _occurrence = {}):
|
|
|
|
m_declaration(_declaration), m_occurrenceKind(_kind), m_occurrence(std::move(_occurrence))
|
2018-12-07 17:20:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Defines a deterministic order on variable occurrences.
|
|
|
|
bool operator<(VariableOccurrence const& _rhs) const
|
|
|
|
{
|
|
|
|
if (m_occurrence && _rhs.m_occurrence)
|
|
|
|
{
|
2020-03-05 09:47:01 +00:00
|
|
|
if (*m_occurrence < *_rhs.m_occurrence) return true;
|
|
|
|
if (*_rhs.m_occurrence < *m_occurrence) return false;
|
2018-12-07 17:20:35 +00:00
|
|
|
}
|
|
|
|
else if (_rhs.m_occurrence)
|
|
|
|
return true;
|
|
|
|
else if (m_occurrence)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
using KindCompareType = std::underlying_type<VariableOccurrence::Kind>::type;
|
|
|
|
return
|
|
|
|
std::make_pair(m_declaration.id(), static_cast<KindCompareType>(m_occurrenceKind)) <
|
2019-12-12 23:39:29 +00:00
|
|
|
std::make_pair(_rhs.m_declaration.id(), static_cast<KindCompareType>(_rhs.m_occurrenceKind));
|
2018-12-07 17:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VariableDeclaration const& declaration() const { return m_declaration; }
|
2020-11-11 21:55:38 +00:00
|
|
|
Kind kind() const { return m_occurrenceKind; }
|
2020-03-05 09:47:01 +00:00
|
|
|
std::optional<langutil::SourceLocation> const& occurrence() const { return m_occurrence; }
|
2018-12-07 17:20:35 +00:00
|
|
|
private:
|
|
|
|
/// Declaration of the occurring variable.
|
|
|
|
VariableDeclaration const& m_declaration;
|
|
|
|
/// Kind of occurrence.
|
|
|
|
Kind m_occurrenceKind = Kind::Access;
|
2020-03-05 09:47:01 +00:00
|
|
|
/// Source location at which the variable occurred, if available (may be nullptr).
|
|
|
|
std::optional<langutil::SourceLocation> m_occurrence;
|
2018-05-04 13:58:10 +00:00
|
|
|
};
|
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
/**
|
|
|
|
* Node of the Control Flow Graph.
|
|
|
|
* The control flow is a directed graph connecting control flow blocks.
|
|
|
|
* An arc between two nodes indicates that the control flow can possibly
|
|
|
|
* move from its start node to its end node during execution.
|
|
|
|
*/
|
2018-05-04 13:58:10 +00:00
|
|
|
struct CFGNode
|
|
|
|
{
|
|
|
|
/// Entry nodes. All CFG nodes from which control flow may move into this node.
|
|
|
|
std::vector<CFGNode*> entries;
|
|
|
|
/// Exit nodes. All CFG nodes to which control flow may continue after this node.
|
|
|
|
std::vector<CFGNode*> exits;
|
2021-12-30 17:16:29 +00:00
|
|
|
/// Function call done by this node
|
|
|
|
FunctionCall const* functionCall = nullptr;
|
2018-05-04 13:58:10 +00:00
|
|
|
|
2018-12-07 17:20:35 +00:00
|
|
|
/// Variable occurrences in the node.
|
|
|
|
std::vector<VariableOccurrence> variableOccurrences;
|
2019-01-08 18:33:46 +00:00
|
|
|
// Source location of this control flow block.
|
|
|
|
langutil::SourceLocation location;
|
2018-05-04 13:58:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Describes the control flow of a function. */
|
|
|
|
struct FunctionFlow
|
|
|
|
{
|
2018-12-12 13:51:22 +00:00
|
|
|
virtual ~FunctionFlow() = default;
|
|
|
|
|
2018-05-04 13:58:10 +00:00
|
|
|
/// Entry node. Control flow of the function starts here.
|
|
|
|
/// This node is empty and does not have any entries.
|
|
|
|
CFGNode* entry = nullptr;
|
|
|
|
/// Exit node. All non-reverting control flow of the function ends here.
|
|
|
|
/// This node is empty and does not have any exits, but may have multiple entries
|
|
|
|
/// (e.g. all return statements of the function).
|
|
|
|
CFGNode* exit = nullptr;
|
|
|
|
/// Revert node. Control flow of the function in case of revert.
|
2020-10-28 19:32:46 +00:00
|
|
|
/// This node is empty and does not have any exits, but may have multiple entries
|
2018-05-04 13:58:10 +00:00
|
|
|
/// (e.g. all assert, require, revert and throw statements).
|
|
|
|
CFGNode* revert = nullptr;
|
2020-03-05 09:47:01 +00:00
|
|
|
/// Transaction return node. Destination node for inline assembly "return" calls.
|
|
|
|
/// This node is empty and does not have any exits, but may have multiple entries
|
|
|
|
/// (e.g. all inline assembly return calls).
|
|
|
|
CFGNode* transactionReturn = nullptr;
|
2018-05-04 13:58:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CFG: private ASTConstVisitor
|
|
|
|
{
|
|
|
|
public:
|
2020-10-28 19:32:46 +00:00
|
|
|
struct FunctionContractTuple
|
|
|
|
{
|
|
|
|
ContractDefinition const* contract = nullptr;
|
|
|
|
FunctionDefinition const* function = nullptr;
|
|
|
|
|
|
|
|
// Use AST ids for comparison to keep a deterministic order in the
|
|
|
|
// containers using this struct
|
|
|
|
bool operator<(FunctionContractTuple const& _other) const
|
|
|
|
{
|
2021-06-08 17:37:48 +00:00
|
|
|
return
|
|
|
|
std::make_pair(contract ? contract->id() : -1, function->id()) <
|
|
|
|
std::make_pair(_other.contract ? _other.contract->id() : -1, _other.function->id());
|
2020-10-28 19:32:46 +00:00
|
|
|
}
|
|
|
|
};
|
2018-11-14 16:11:55 +00:00
|
|
|
explicit CFG(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) {}
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
bool constructFlow(ASTNode const& _astRoot);
|
|
|
|
|
2018-11-16 01:09:04 +00:00
|
|
|
bool visit(FunctionDefinition const& _function) override;
|
2020-10-28 19:32:46 +00:00
|
|
|
bool visit(ContractDefinition const& _contract) override;
|
|
|
|
|
|
|
|
/// Get the function flow for the given function, using `_contract` as the
|
|
|
|
/// most derived contract
|
|
|
|
/// @param _function function to find the function flow for
|
|
|
|
/// @param _contract most derived contract or nullptr for free functions
|
|
|
|
FunctionFlow const& functionFlow(FunctionDefinition const& _function, ContractDefinition const* _contract = nullptr) const;
|
2018-05-04 13:58:10 +00:00
|
|
|
|
2020-10-28 19:32:46 +00:00
|
|
|
std::map<FunctionContractTuple, std::unique_ptr<FunctionFlow>> const& allFunctionFlows() const
|
|
|
|
{
|
|
|
|
return m_functionControlFlow;
|
|
|
|
}
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
class NodeContainer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CFGNode* newNode();
|
|
|
|
private:
|
|
|
|
std::vector<std::unique_ptr<CFGNode>> m_nodes;
|
|
|
|
};
|
|
|
|
private:
|
2018-11-14 16:11:55 +00:00
|
|
|
langutil::ErrorReporter& m_errorReporter;
|
2018-05-04 13:58:10 +00:00
|
|
|
|
|
|
|
/// Node container.
|
|
|
|
/// All nodes allocated during the construction of the control flow graph
|
|
|
|
/// are owned by the CFG class and stored in this container.
|
|
|
|
NodeContainer m_nodeContainer;
|
|
|
|
|
2020-10-28 19:32:46 +00:00
|
|
|
std::map<FunctionContractTuple, std::unique_ptr<FunctionFlow>> m_functionControlFlow;
|
2018-05-04 13:58:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|