2018-02-02 14:23:44 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
2018-06-06 04:43:58 +00:00
|
|
|
* Base class to perform data flow analysis during AST walks.
|
2018-02-02 14:23:44 +00:00
|
|
|
* Tracks assignments and is used as base class for both Rematerialiser and
|
|
|
|
* Common Subexpression Eliminator.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2022-11-14 09:52:02 +00:00
|
|
|
#include <libyul/optimiser/OptimizerUtilities.h>
|
2022-03-10 17:25:00 +00:00
|
|
|
#include <libyul/optimiser/KnowledgeBase.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-12-03 17:48:17 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2021-03-01 18:05:59 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2020-12-11 16:30:06 +00:00
|
|
|
#include <libsolutil/cxx20.h>
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::yul;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2020-12-03 17:48:17 +00:00
|
|
|
DataFlowAnalyzer::DataFlowAnalyzer(
|
|
|
|
Dialect const& _dialect,
|
2022-07-11 13:51:25 +00:00
|
|
|
MemoryAndStorage _analyzeStores,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<YulString, SideEffects> _functionSideEffects
|
2020-12-03 17:48:17 +00:00
|
|
|
):
|
2021-11-04 14:40:57 +00:00
|
|
|
m_dialect(_dialect),
|
|
|
|
m_functionSideEffects(std::move(_functionSideEffects)),
|
2022-11-10 10:49:40 +00:00
|
|
|
m_knowledgeBase([this](YulString _var) { return variableValue(_var); }),
|
2022-07-11 13:51:25 +00:00
|
|
|
m_analyzeStores(_analyzeStores == MemoryAndStorage::Analyze)
|
2020-12-03 17:48:17 +00:00
|
|
|
{
|
2022-07-11 13:51:25 +00:00
|
|
|
if (m_analyzeStores)
|
|
|
|
{
|
|
|
|
if (auto const* builtin = _dialect.memoryStoreFunction(YulString{}))
|
|
|
|
m_storeFunctionName[static_cast<unsigned>(StoreLoadLocation::Memory)] = builtin->name;
|
|
|
|
if (auto const* builtin = _dialect.memoryLoadFunction(YulString{}))
|
|
|
|
m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Memory)] = builtin->name;
|
|
|
|
if (auto const* builtin = _dialect.storageStoreFunction(YulString{}))
|
|
|
|
m_storeFunctionName[static_cast<unsigned>(StoreLoadLocation::Storage)] = builtin->name;
|
|
|
|
if (auto const* builtin = _dialect.storageLoadFunction(YulString{}))
|
|
|
|
m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Storage)] = builtin->name;
|
|
|
|
}
|
2020-12-03 17:48:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 13:52:15 +00:00
|
|
|
void DataFlowAnalyzer::operator()(ExpressionStatement& _statement)
|
|
|
|
{
|
2022-07-11 13:51:25 +00:00
|
|
|
if (m_analyzeStores)
|
2019-05-27 22:14:01 +00:00
|
|
|
{
|
2022-07-11 13:51:25 +00:00
|
|
|
if (auto vars = isSimpleStore(StoreLoadLocation::Storage, _statement))
|
|
|
|
{
|
|
|
|
ASTModifier::operator()(_statement);
|
2022-03-17 18:04:00 +00:00
|
|
|
cxx20::erase_if(m_state.environment.storage, mapTuple([&](auto&& key, auto&& value) {
|
2022-07-11 13:51:25 +00:00
|
|
|
return
|
|
|
|
!m_knowledgeBase.knownToBeDifferent(vars->first, key) &&
|
2022-11-10 10:49:40 +00:00
|
|
|
vars->second != value;
|
2022-07-11 13:51:25 +00:00
|
|
|
}));
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.storage[vars->first] = vars->second;
|
2022-07-11 13:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (auto vars = isSimpleStore(StoreLoadLocation::Memory, _statement))
|
|
|
|
{
|
|
|
|
ASTModifier::operator()(_statement);
|
2022-03-17 18:04:00 +00:00
|
|
|
cxx20::erase_if(m_state.environment.memory, mapTuple([&](auto&& key, auto&& /* value */) {
|
2022-07-11 13:51:25 +00:00
|
|
|
return !m_knowledgeBase.knownToBeDifferentByAtLeast32(vars->first, key);
|
|
|
|
}));
|
2022-11-14 09:52:02 +00:00
|
|
|
// TODO erase keccak knowledge, but in a more clever way
|
|
|
|
m_state.environment.keccak = {};
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.memory[vars->first] = vars->second;
|
2022-07-11 13:51:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2022-07-11 13:51:25 +00:00
|
|
|
clearKnowledgeIfInvalidated(_statement.expression);
|
|
|
|
ASTModifier::operator()(_statement);
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
void DataFlowAnalyzer::operator()(Assignment& _assignment)
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> names;
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& var: _assignment.variableNames)
|
2018-10-29 14:12:02 +00:00
|
|
|
names.emplace(var.name);
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(_assignment.value, OptimizerException, "");
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(*_assignment.value);
|
2018-02-02 14:23:44 +00:00
|
|
|
visit(*_assignment.value);
|
2020-11-26 17:07:12 +00:00
|
|
|
handleAssignment(names, _assignment.value.get(), false);
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> names;
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& var: _varDecl.variables)
|
2018-10-29 14:12:02 +00:00
|
|
|
names.emplace(var.name);
|
2018-02-05 17:02:32 +00:00
|
|
|
m_variableScopes.back().variables += names;
|
2019-01-09 13:05:03 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
if (_varDecl.value)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(*_varDecl.value);
|
2018-02-02 14:23:44 +00:00
|
|
|
visit(*_varDecl.value);
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2019-01-09 13:05:03 +00:00
|
|
|
|
2020-11-26 17:07:12 +00:00
|
|
|
handleAssignment(names, _varDecl.value.get(), true);
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(If& _if)
|
|
|
|
{
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(*_if.condition);
|
2022-03-17 18:04:00 +00:00
|
|
|
Environment preEnvironment = m_state.environment;
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
ASTModifier::operator()(_if);
|
2022-03-17 18:04:00 +00:00
|
|
|
joinKnowledge(preEnvironment);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2021-11-04 14:40:57 +00:00
|
|
|
clearValues(assignedVariableNames(_if.body));
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(Switch& _switch)
|
|
|
|
{
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(*_switch.expression);
|
2018-02-02 14:23:44 +00:00
|
|
|
visit(*_switch.expression);
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> assignedVariables;
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto& _case: _switch.cases)
|
|
|
|
{
|
2022-03-17 18:04:00 +00:00
|
|
|
Environment preEnvironment = m_state.environment;
|
2018-02-02 14:23:44 +00:00
|
|
|
(*this)(_case.body);
|
2022-03-17 18:04:00 +00:00
|
|
|
joinKnowledge(preEnvironment);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> variables = assignedVariableNames(_case.body);
|
2021-11-04 14:40:57 +00:00
|
|
|
assignedVariables += variables;
|
2018-02-02 14:23:44 +00:00
|
|
|
// This is a little too destructive, we could retain the old values.
|
2021-11-04 14:40:57 +00:00
|
|
|
clearValues(variables);
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(_case.body);
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
for (auto& _case: _switch.cases)
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(_case.body);
|
2018-02-02 14:23:44 +00:00
|
|
|
clearValues(assignedVariables);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
|
|
|
{
|
2018-10-28 11:57:13 +00:00
|
|
|
// Save all information. We might rather reinstantiate this class,
|
|
|
|
// but this could be difficult if it is subclassed.
|
2022-03-10 18:35:58 +00:00
|
|
|
ScopedSaveAndRestore stateResetter(m_state, {});
|
2021-04-13 11:20:59 +00:00
|
|
|
ScopedSaveAndRestore loopDepthResetter(m_loopDepth, 0u);
|
2018-10-18 12:45:11 +00:00
|
|
|
pushScope(true);
|
2018-10-28 11:57:13 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& parameter: _fun.parameters)
|
2018-10-29 14:12:02 +00:00
|
|
|
m_variableScopes.back().variables.emplace(parameter.name);
|
2018-02-02 14:23:44 +00:00
|
|
|
for (auto const& var: _fun.returnVariables)
|
2018-12-13 11:33:03 +00:00
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
m_variableScopes.back().variables.emplace(var.name);
|
2020-11-26 17:07:12 +00:00
|
|
|
handleAssignment({var.name}, nullptr, true);
|
2018-12-13 11:33:03 +00:00
|
|
|
}
|
2018-02-02 14:23:44 +00:00
|
|
|
ASTModifier::operator()(_fun);
|
2018-10-28 11:57:13 +00:00
|
|
|
|
2019-10-28 14:25:02 +00:00
|
|
|
// Note that the contents of return variables, storage and memory at this point
|
|
|
|
// might be incorrect due to the fact that the DataFlowAnalyzer ignores the ``leave``
|
|
|
|
// statement.
|
|
|
|
|
2018-10-18 12:45:11 +00:00
|
|
|
popScope();
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
|
|
|
{
|
2019-04-05 18:38:23 +00:00
|
|
|
// If the pre block was not empty,
|
|
|
|
// we would have to deal with more complicated scoping rules.
|
|
|
|
assertThrow(_for.pre.statements.empty(), OptimizerException, "");
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2019-11-27 09:44:40 +00:00
|
|
|
++m_loopDepth;
|
|
|
|
|
2019-03-11 15:06:43 +00:00
|
|
|
AssignmentsSinceContinue assignmentsSinceCont;
|
|
|
|
assignmentsSinceCont(_for.body);
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> assignedVariables =
|
2021-11-04 14:40:57 +00:00
|
|
|
assignedVariableNames(_for.body) + assignedVariableNames(_for.post);
|
|
|
|
clearValues(assignedVariables);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2019-05-21 13:52:15 +00:00
|
|
|
// break/continue are tricky for storage and thus we almost always clear here.
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(*_for.condition);
|
|
|
|
clearKnowledgeIfInvalidated(_for.post);
|
|
|
|
clearKnowledgeIfInvalidated(_for.body);
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
visit(*_for.condition);
|
|
|
|
(*this)(_for.body);
|
2019-03-11 15:06:43 +00:00
|
|
|
clearValues(assignmentsSinceCont.names());
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(_for.body);
|
2018-02-02 14:23:44 +00:00
|
|
|
(*this)(_for.post);
|
2021-11-04 14:40:57 +00:00
|
|
|
clearValues(assignedVariables);
|
2019-05-27 22:14:01 +00:00
|
|
|
clearKnowledgeIfInvalidated(*_for.condition);
|
|
|
|
clearKnowledgeIfInvalidated(_for.post);
|
|
|
|
clearKnowledgeIfInvalidated(_for.body);
|
2019-11-27 09:44:40 +00:00
|
|
|
|
|
|
|
--m_loopDepth;
|
2019-03-04 14:38:05 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
void DataFlowAnalyzer::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
size_t numScopes = m_variableScopes.size();
|
2018-10-18 12:45:11 +00:00
|
|
|
pushScope(false);
|
2018-02-02 14:23:44 +00:00
|
|
|
ASTModifier::operator()(_block);
|
2018-10-18 12:45:11 +00:00
|
|
|
popScope();
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(numScopes == m_variableScopes.size(), OptimizerException, "");
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<YulString> DataFlowAnalyzer::storageValue(YulString _key) const
|
2022-03-10 17:25:00 +00:00
|
|
|
{
|
2022-11-14 09:52:02 +00:00
|
|
|
if (YulString const* value = valueOrNullptr(m_state.environment.storage, _key))
|
2022-03-10 17:25:00 +00:00
|
|
|
return *value;
|
|
|
|
else
|
2023-08-23 02:03:44 +00:00
|
|
|
return std::nullopt;
|
2022-03-10 17:25:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<YulString> DataFlowAnalyzer::memoryValue(YulString _key) const
|
2022-03-10 17:25:00 +00:00
|
|
|
{
|
2022-11-14 09:52:02 +00:00
|
|
|
if (YulString const* value = valueOrNullptr(m_state.environment.memory, _key))
|
|
|
|
return *value;
|
|
|
|
else
|
2023-08-23 02:03:44 +00:00
|
|
|
return std::nullopt;
|
2022-11-14 09:52:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<YulString> DataFlowAnalyzer::keccakValue(YulString _start, YulString _length) const
|
2022-11-14 09:52:02 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
if (YulString const* value = valueOrNullptr(m_state.environment.keccak, std::make_pair(_start, _length)))
|
2022-03-10 17:25:00 +00:00
|
|
|
return *value;
|
|
|
|
else
|
2023-08-23 02:03:44 +00:00
|
|
|
return std::nullopt;
|
2022-03-10 17:25:00 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void DataFlowAnalyzer::handleAssignment(std::set<YulString> const& _variables, Expression* _value, bool _isDeclaration)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2020-11-26 17:07:12 +00:00
|
|
|
if (!_isDeclaration)
|
|
|
|
clearValues(_variables);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2019-08-13 16:38:11 +00:00
|
|
|
MovableChecker movableChecker{m_dialect, &m_functionSideEffects};
|
2018-02-02 14:23:44 +00:00
|
|
|
if (_value)
|
|
|
|
movableChecker.visit(*_value);
|
2018-12-11 23:49:36 +00:00
|
|
|
else
|
|
|
|
for (auto const& var: _variables)
|
2019-11-27 09:44:40 +00:00
|
|
|
assignValue(var, &m_zero);
|
2018-12-11 23:49:36 +00:00
|
|
|
|
|
|
|
if (_value && _variables.size() == 1)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2018-10-29 14:12:02 +00:00
|
|
|
YulString name = *_variables.begin();
|
2018-02-02 14:23:44 +00:00
|
|
|
// Expression has to be movable and cannot contain a reference
|
|
|
|
// to the variable that will be assigned to.
|
2018-12-11 23:49:36 +00:00
|
|
|
if (movableChecker.movable() && !movableChecker.referencedVariables().count(name))
|
2019-11-27 09:44:40 +00:00
|
|
|
assignValue(name, _value);
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto const& referencedVariables = movableChecker.referencedVariables();
|
|
|
|
for (auto const& name: _variables)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2022-03-10 18:35:58 +00:00
|
|
|
m_state.references[name] = referencedVariables;
|
2020-11-26 17:07:12 +00:00
|
|
|
if (!_isDeclaration)
|
|
|
|
{
|
|
|
|
// assignment to slot denoted by "name"
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.storage.erase(name);
|
2020-11-26 17:07:12 +00:00
|
|
|
// assignment to slot contents denoted by "name"
|
2022-03-17 18:04:00 +00:00
|
|
|
cxx20::erase_if(m_state.environment.storage, mapTuple([&name](auto&& /* key */, auto&& value) { return value == name; }));
|
2020-11-26 17:07:12 +00:00
|
|
|
// assignment to slot denoted by "name"
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.memory.erase(name);
|
2020-11-26 17:07:12 +00:00
|
|
|
// assignment to slot contents denoted by "name"
|
2022-11-14 09:52:02 +00:00
|
|
|
cxx20::erase_if(m_state.environment.keccak, [&name](auto&& _item) {
|
|
|
|
return _item.first.first == name || _item.first.second == name || _item.second == name;
|
|
|
|
});
|
2022-03-17 18:04:00 +00:00
|
|
|
cxx20::erase_if(m_state.environment.memory, mapTuple([&name](auto&& /* key */, auto&& value) { return value == name; }));
|
2020-11-26 17:07:12 +00:00
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2020-06-29 17:32:32 +00:00
|
|
|
|
|
|
|
if (_value && _variables.size() == 1)
|
|
|
|
{
|
|
|
|
YulString variable = *_variables.begin();
|
|
|
|
if (!movableChecker.referencedVariables().count(variable))
|
|
|
|
{
|
|
|
|
// This might erase additional knowledge about the slot.
|
|
|
|
// On the other hand, if we knew the value in the slot
|
|
|
|
// already, then the sload() / mload() would have been replaced by a variable anyway.
|
2020-12-03 17:48:17 +00:00
|
|
|
if (auto key = isSimpleLoad(StoreLoadLocation::Memory, *_value))
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.memory[*key] = variable;
|
2020-12-03 17:48:17 +00:00
|
|
|
else if (auto key = isSimpleLoad(StoreLoadLocation::Storage, *_value))
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.storage[*key] = variable;
|
2022-11-14 09:52:02 +00:00
|
|
|
else if (auto arguments = isKeccak(*_value))
|
|
|
|
m_state.environment.keccak[*arguments] = variable;
|
2020-06-29 17:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 12:45:11 +00:00
|
|
|
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
|
|
|
{
|
|
|
|
m_variableScopes.emplace_back(_functionScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::popScope()
|
|
|
|
{
|
2020-12-11 17:11:20 +00:00
|
|
|
for (auto const& name: m_variableScopes.back().variables)
|
|
|
|
{
|
2022-03-10 18:35:58 +00:00
|
|
|
m_state.value.erase(name);
|
|
|
|
m_state.references.erase(name);
|
2020-12-11 17:11:20 +00:00
|
|
|
}
|
2018-10-18 12:45:11 +00:00
|
|
|
m_variableScopes.pop_back();
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void DataFlowAnalyzer::clearValues(std::set<YulString> _variables)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
|
|
|
// All variables that reference variables to be cleared also have to be
|
|
|
|
// cleared, but not recursively, since only the value of the original
|
|
|
|
// variables changes. Example:
|
|
|
|
// let a := 1
|
|
|
|
// let b := a
|
|
|
|
// let c := b
|
2022-11-14 09:52:02 +00:00
|
|
|
// a := 2
|
2018-02-02 14:23:44 +00:00
|
|
|
// add(b, c)
|
|
|
|
// In the last line, we can replace c by b, but not b by a.
|
|
|
|
//
|
|
|
|
// This cannot be easily tested since the substitutions will be done
|
|
|
|
// one by one on the fly, and the last line will just be add(1, 1)
|
|
|
|
|
2019-05-21 13:52:15 +00:00
|
|
|
// First clear storage knowledge, because we do not have to clear
|
|
|
|
// storage knowledge of variables whose expression has changed,
|
|
|
|
// since the value is still unchanged.
|
2021-01-13 16:23:27 +00:00
|
|
|
auto eraseCondition = mapTuple([&_variables](auto&& key, auto&& value) {
|
|
|
|
return _variables.count(key) || _variables.count(value);
|
|
|
|
});
|
2022-03-17 18:04:00 +00:00
|
|
|
cxx20::erase_if(m_state.environment.storage, eraseCondition);
|
|
|
|
cxx20::erase_if(m_state.environment.memory, eraseCondition);
|
2022-11-14 09:52:02 +00:00
|
|
|
cxx20::erase_if(m_state.environment.keccak, [&_variables](auto&& _item) {
|
|
|
|
return
|
|
|
|
_variables.count(_item.first.first) ||
|
|
|
|
_variables.count(_item.first.second) ||
|
|
|
|
_variables.count(_item.second);
|
|
|
|
});
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
// Also clear variables that reference variables to be cleared.
|
2021-01-13 11:43:38 +00:00
|
|
|
for (auto const& variableToClear: _variables)
|
2022-03-10 18:35:58 +00:00
|
|
|
for (auto const& [ref, names]: m_state.references)
|
2021-01-13 11:43:38 +00:00
|
|
|
if (names.count(variableToClear))
|
2020-12-01 23:02:44 +00:00
|
|
|
_variables.emplace(ref);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
// Clear the value and update the reference relation.
|
2018-10-18 12:45:11 +00:00
|
|
|
for (auto const& name: _variables)
|
2020-12-11 16:30:06 +00:00
|
|
|
{
|
2022-03-10 18:35:58 +00:00
|
|
|
m_state.value.erase(name);
|
|
|
|
m_state.references.erase(name);
|
2020-12-11 16:30:06 +00:00
|
|
|
}
|
2018-02-02 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:44:40 +00:00
|
|
|
void DataFlowAnalyzer::assignValue(YulString _variable, Expression const* _value)
|
|
|
|
{
|
2022-03-10 18:35:58 +00:00
|
|
|
m_state.value[_variable] = {_value, m_loopDepth};
|
2019-11-27 09:44:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Block const& _block)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2022-07-11 13:51:25 +00:00
|
|
|
if (!m_analyzeStores)
|
|
|
|
return;
|
2019-09-02 12:22:53 +00:00
|
|
|
SideEffectsCollector sideEffects(m_dialect, _block, &m_functionSideEffects);
|
2019-05-27 22:14:01 +00:00
|
|
|
if (sideEffects.invalidatesStorage())
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.storage.clear();
|
2019-05-27 22:14:01 +00:00
|
|
|
if (sideEffects.invalidatesMemory())
|
2022-11-14 09:52:02 +00:00
|
|
|
{
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.memory.clear();
|
2022-11-14 09:52:02 +00:00
|
|
|
m_state.environment.keccak.clear();
|
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Expression const& _expr)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2022-07-11 13:51:25 +00:00
|
|
|
if (!m_analyzeStores)
|
|
|
|
return;
|
2019-09-02 12:22:53 +00:00
|
|
|
SideEffectsCollector sideEffects(m_dialect, _expr, &m_functionSideEffects);
|
2019-05-27 22:14:01 +00:00
|
|
|
if (sideEffects.invalidatesStorage())
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.storage.clear();
|
2019-05-27 22:14:01 +00:00
|
|
|
if (sideEffects.invalidatesMemory())
|
2022-11-14 09:52:02 +00:00
|
|
|
{
|
2022-03-17 18:04:00 +00:00
|
|
|
m_state.environment.memory.clear();
|
2022-11-14 09:52:02 +00:00
|
|
|
m_state.environment.keccak.clear();
|
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2021-03-31 18:03:04 +00:00
|
|
|
for (auto const& scope: m_variableScopes | ranges::views::reverse)
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2018-02-05 17:02:32 +00:00
|
|
|
if (scope.variables.count(_variableName))
|
2018-02-02 14:23:44 +00:00
|
|
|
return true;
|
2018-02-05 17:02:32 +00:00
|
|
|
if (scope.isFunction)
|
2018-02-02 14:23:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<u256> DataFlowAnalyzer::valueOfIdentifier(YulString const& _name) const
|
2021-03-01 18:05:59 +00:00
|
|
|
{
|
2022-03-10 17:25:00 +00:00
|
|
|
if (AssignedValue const* value = variableValue(_name))
|
2023-08-23 02:03:44 +00:00
|
|
|
if (Literal const* literal = std::get_if<Literal>(value->value))
|
2021-03-01 18:05:59 +00:00
|
|
|
return valueOfLiteral(*literal);
|
2023-08-23 02:03:44 +00:00
|
|
|
return std::nullopt;
|
2021-03-01 18:05:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<std::pair<YulString, YulString>> DataFlowAnalyzer::isSimpleStore(
|
2020-12-03 17:48:17 +00:00
|
|
|
StoreLoadLocation _location,
|
2019-05-21 13:52:15 +00:00
|
|
|
ExpressionStatement const& _statement
|
|
|
|
) const
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
if (FunctionCall const* funCall = std::get_if<FunctionCall>(&_statement.expression))
|
2020-12-03 17:48:17 +00:00
|
|
|
if (funCall->functionName.name == m_storeFunctionName[static_cast<unsigned>(_location)])
|
|
|
|
if (Identifier const* key = std::get_if<Identifier>(&funCall->arguments.front()))
|
|
|
|
if (Identifier const* value = std::get_if<Identifier>(&funCall->arguments.back()))
|
2023-08-23 02:03:44 +00:00
|
|
|
return std::make_pair(key->name, value->name);
|
2019-05-21 13:52:15 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:32:32 +00:00
|
|
|
std::optional<YulString> DataFlowAnalyzer::isSimpleLoad(
|
2020-12-03 17:48:17 +00:00
|
|
|
StoreLoadLocation _location,
|
2020-06-29 17:32:32 +00:00
|
|
|
Expression const& _expression
|
|
|
|
) const
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
if (FunctionCall const* funCall = std::get_if<FunctionCall>(&_expression))
|
2020-12-03 17:48:17 +00:00
|
|
|
if (funCall->functionName.name == m_loadFunctionName[static_cast<unsigned>(_location)])
|
|
|
|
if (Identifier const* key = std::get_if<Identifier>(&funCall->arguments.front()))
|
|
|
|
return key->name;
|
2020-06-29 17:32:32 +00:00
|
|
|
return {};
|
|
|
|
}
|
2022-03-17 18:04:00 +00:00
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<std::pair<YulString, YulString>> DataFlowAnalyzer::isKeccak(Expression const& _expression) const
|
2022-11-14 09:52:02 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
if (FunctionCall const* funCall = std::get_if<FunctionCall>(&_expression))
|
2022-11-14 09:52:02 +00:00
|
|
|
if (funCall->functionName.name == m_dialect.hashFunction({}))
|
|
|
|
if (Identifier const* start = std::get_if<Identifier>(&funCall->arguments.at(0)))
|
|
|
|
if (Identifier const* length = std::get_if<Identifier>(&funCall->arguments.at(1)))
|
2023-08-23 02:03:44 +00:00
|
|
|
return std::make_pair(start->name, length->name);
|
|
|
|
return std::nullopt;
|
2022-11-14 09:52:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 18:04:00 +00:00
|
|
|
void DataFlowAnalyzer::joinKnowledge(Environment const& _olderEnvironment)
|
|
|
|
{
|
|
|
|
if (!m_analyzeStores)
|
|
|
|
return;
|
|
|
|
joinKnowledgeHelper(m_state.environment.storage, _olderEnvironment.storage);
|
|
|
|
joinKnowledgeHelper(m_state.environment.memory, _olderEnvironment.memory);
|
2022-11-14 09:52:02 +00:00
|
|
|
cxx20::erase_if(m_state.environment.keccak, mapTuple([&_olderEnvironment](auto&& key, auto&& currentValue) {
|
|
|
|
YulString const* oldValue = valueOrNullptr(_olderEnvironment.keccak, key);
|
|
|
|
return !oldValue || *oldValue != currentValue;
|
|
|
|
}));
|
2022-03-17 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlowAnalyzer::joinKnowledgeHelper(
|
|
|
|
std::unordered_map<YulString, YulString>& _this,
|
|
|
|
std::unordered_map<YulString, YulString> const& _older
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// We clear if the key does not exist in the older map or if the value is different.
|
|
|
|
// This also works for memory because _older is an "older version"
|
|
|
|
// of m_state.environment.memory and thus any overlapping write would have cleared the keys
|
|
|
|
// that are not known to be different inside m_state.environment.memory already.
|
|
|
|
cxx20::erase_if(_this, mapTuple([&_older](auto&& key, auto&& currentValue){
|
2022-11-14 09:52:02 +00:00
|
|
|
YulString const* oldValue = valueOrNullptr(_older, key);
|
2022-03-17 18:04:00 +00:00
|
|
|
return !oldValue || *oldValue != currentValue;
|
|
|
|
}));
|
|
|
|
}
|