2017-09-28 13:24:24 +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
|
2017-09-28 13:24:24 +00:00
|
|
|
|
|
|
|
#include <libsolidity/formal/VariableUsage.h>
|
|
|
|
|
2019-06-25 10:46:17 +00:00
|
|
|
#include <libsolidity/formal/BMC.h>
|
|
|
|
#include <libsolidity/formal/SMTEncoder.h>
|
2019-03-25 14:01:07 +00:00
|
|
|
|
2021-03-11 14:52:10 +00:00
|
|
|
#include <range/v3/view.hpp>
|
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
#include <algorithm>
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::smt;
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2023-08-14 20:19:11 +00:00
|
|
|
std::set<VariableDeclaration const*> VariableUsage::touchedVariables(ASTNode const& _node, std::vector<CallableDeclaration const*> const& _outerCallstack)
|
2019-05-09 14:06:13 +00:00
|
|
|
{
|
|
|
|
m_touchedVariables.clear();
|
|
|
|
m_callStack.clear();
|
|
|
|
m_callStack += _outerCallstack;
|
2020-05-28 10:17:53 +00:00
|
|
|
if (!m_callStack.empty())
|
|
|
|
m_lastCall = m_callStack.back();
|
2019-05-09 14:06:13 +00:00
|
|
|
_node.accept(*this);
|
|
|
|
return m_touchedVariables;
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
void VariableUsage::endVisit(Identifier const& _identifier)
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2020-04-09 10:48:57 +00:00
|
|
|
if (_identifier.annotation().willBeWrittenTo)
|
2019-05-09 14:06:13 +00:00
|
|
|
checkIdentifier(_identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VariableUsage::endVisit(IndexAccess const& _indexAccess)
|
|
|
|
{
|
2020-04-09 10:48:57 +00:00
|
|
|
if (_indexAccess.annotation().willBeWrittenTo)
|
2019-05-09 14:06:13 +00:00
|
|
|
{
|
2020-04-09 10:48:57 +00:00
|
|
|
/// identifier.annotation().willBeWrittenTo == false, that's why we
|
2019-05-09 14:06:13 +00:00
|
|
|
/// need to check that before.
|
2019-06-25 10:46:17 +00:00
|
|
|
auto identifier = dynamic_cast<Identifier const*>(SMTEncoder::leftmostBase(_indexAccess));
|
2019-05-09 14:06:13 +00:00
|
|
|
if (identifier)
|
|
|
|
checkIdentifier(*identifier);
|
|
|
|
}
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
void VariableUsage::endVisit(FunctionCall const& _funCall)
|
2017-09-28 13:24:24 +00:00
|
|
|
{
|
2021-03-11 14:52:10 +00:00
|
|
|
auto scopeContract = currentScopeContract();
|
2021-01-25 17:17:56 +00:00
|
|
|
if (m_inlineFunctionCalls(_funCall, scopeContract, m_currentContract))
|
|
|
|
if (auto funDef = SMTEncoder::functionCallToDefinition(_funCall, scopeContract, m_currentContract))
|
2019-07-08 09:15:15 +00:00
|
|
|
if (find(m_callStack.begin(), m_callStack.end(), funDef) == m_callStack.end())
|
|
|
|
funDef->accept(*this);
|
2019-04-01 09:10:28 +00:00
|
|
|
}
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
bool VariableUsage::visit(FunctionDefinition const& _function)
|
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
m_callStack.push_back(&_function);
|
2019-04-01 09:10:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
void VariableUsage::endVisit(FunctionDefinition const&)
|
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
solAssert(!m_callStack.empty(), "");
|
|
|
|
m_callStack.pop_back();
|
2019-04-01 09:10:28 +00:00
|
|
|
}
|
2019-03-25 14:01:07 +00:00
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
void VariableUsage::endVisit(ModifierInvocation const& _modifierInv)
|
|
|
|
{
|
2020-08-11 09:18:22 +00:00
|
|
|
auto const& modifierDef = dynamic_cast<ModifierDefinition const*>(_modifierInv.name().annotation().referencedDeclaration);
|
2019-04-01 09:10:28 +00:00
|
|
|
if (modifierDef)
|
|
|
|
modifierDef->accept(*this);
|
|
|
|
}
|
2019-03-25 14:01:07 +00:00
|
|
|
|
2019-04-01 09:10:28 +00:00
|
|
|
void VariableUsage::endVisit(PlaceholderStatement const&)
|
|
|
|
{
|
2019-05-06 18:03:11 +00:00
|
|
|
solAssert(!m_callStack.empty(), "");
|
|
|
|
FunctionDefinition const* funDef = nullptr;
|
|
|
|
for (auto it = m_callStack.rbegin(); it != m_callStack.rend() && !funDef; ++it)
|
|
|
|
funDef = dynamic_cast<FunctionDefinition const*>(*it);
|
|
|
|
solAssert(funDef, "");
|
|
|
|
if (funDef->isImplemented())
|
|
|
|
funDef->body().accept(*this);
|
2019-04-01 09:10:28 +00:00
|
|
|
}
|
2017-09-28 13:24:24 +00:00
|
|
|
|
2019-05-09 14:06:13 +00:00
|
|
|
void VariableUsage::checkIdentifier(Identifier const& _identifier)
|
2019-04-01 09:10:28 +00:00
|
|
|
{
|
2019-05-09 14:06:13 +00:00
|
|
|
Declaration const* declaration = _identifier.annotation().referencedDeclaration;
|
|
|
|
solAssert(declaration, "");
|
|
|
|
if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(declaration))
|
|
|
|
{
|
2020-05-29 16:20:15 +00:00
|
|
|
if (!varDecl->isLocalVariable() || (m_lastCall && varDecl->functionOrModifierDefinition() == m_lastCall))
|
2019-05-09 14:06:13 +00:00
|
|
|
m_touchedVariables.insert(varDecl);
|
|
|
|
}
|
2017-09-28 13:24:24 +00:00
|
|
|
}
|
2021-03-11 14:52:10 +00:00
|
|
|
|
|
|
|
ContractDefinition const* VariableUsage::currentScopeContract()
|
|
|
|
{
|
|
|
|
for (auto&& f: m_callStack | ranges::views::reverse)
|
|
|
|
if (auto fun = dynamic_cast<FunctionDefinition const*>(f))
|
|
|
|
return fun->annotation().contract;
|
|
|
|
return nullptr;
|
|
|
|
}
|