2020-03-10 17:15:50 +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
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
#include <libsolidity/analysis/ImmutableValidator.h>
|
|
|
|
|
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
using namespace solidity::frontend;
|
2020-04-18 01:00:22 +00:00
|
|
|
using namespace solidity::langutil;
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
void ImmutableValidator::analyze()
|
|
|
|
{
|
2021-08-05 15:06:19 +00:00
|
|
|
m_inCreationContext = true;
|
2020-03-10 17:15:50 +00:00
|
|
|
|
2022-01-12 17:58:17 +00:00
|
|
|
auto linearizedContracts = m_mostDerivedContract.annotation().linearizedBaseContracts | ranges::views::reverse;
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
for (ContractDefinition const* contract: linearizedContracts)
|
|
|
|
for (VariableDeclaration const* stateVar: contract->stateVariables())
|
|
|
|
if (stateVar->value())
|
2021-08-05 15:06:19 +00:00
|
|
|
stateVar->value()->accept(*this);
|
2020-08-27 15:25:13 +00:00
|
|
|
|
|
|
|
for (ContractDefinition const* contract: linearizedContracts)
|
2021-08-05 15:06:19 +00:00
|
|
|
for (std::shared_ptr<InheritanceSpecifier> const& inheritSpec: contract->baseContracts())
|
|
|
|
if (auto args = inheritSpec->arguments())
|
|
|
|
ASTNode::listAccept(*args, *this);
|
|
|
|
|
|
|
|
for (ContractDefinition const* contract: linearizedContracts)
|
|
|
|
{
|
2020-08-27 15:25:13 +00:00
|
|
|
for (VariableDeclaration const* stateVar: contract->stateVariables())
|
|
|
|
if (stateVar->value())
|
2021-08-05 15:06:19 +00:00
|
|
|
m_initializedStateVariables.emplace(stateVar);
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
if (contract->constructor())
|
|
|
|
visitCallableIfNew(*contract->constructor());
|
2021-08-05 15:06:19 +00:00
|
|
|
}
|
2020-03-10 17:15:50 +00:00
|
|
|
|
2021-08-05 15:06:19 +00:00
|
|
|
m_inCreationContext = false;
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
for (ContractDefinition const* contract: linearizedContracts)
|
|
|
|
{
|
|
|
|
for (auto funcDef: contract->definedFunctions())
|
|
|
|
visitCallableIfNew(*funcDef);
|
|
|
|
|
|
|
|
for (auto modDef: contract->functionModifiers())
|
|
|
|
visitCallableIfNew(*modDef);
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:58:17 +00:00
|
|
|
checkAllVariablesInitialized(m_mostDerivedContract.location());
|
2020-03-10 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 15:06:19 +00:00
|
|
|
bool ImmutableValidator::visit(Assignment const& _assignment)
|
|
|
|
{
|
|
|
|
// Need to visit values first (rhs) as they might access other immutables.
|
|
|
|
_assignment.rightHandSide().accept(*this);
|
|
|
|
_assignment.leftHandSide().accept(*this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-10 17:15:50 +00:00
|
|
|
bool ImmutableValidator::visit(FunctionDefinition const& _functionDefinition)
|
|
|
|
{
|
|
|
|
return analyseCallable(_functionDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImmutableValidator::visit(ModifierDefinition const& _modifierDefinition)
|
|
|
|
{
|
|
|
|
return analyseCallable(_modifierDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImmutableValidator::visit(MemberAccess const& _memberAccess)
|
|
|
|
{
|
|
|
|
_memberAccess.expression().accept(*this);
|
|
|
|
|
2020-04-02 16:26:33 +00:00
|
|
|
if (auto contractType = dynamic_cast<ContractType const*>(_memberAccess.expression().annotation().type))
|
|
|
|
if (!contractType->isSuper())
|
|
|
|
// external access, no analysis needed.
|
|
|
|
return false;
|
|
|
|
|
2020-03-10 17:15:50 +00:00
|
|
|
if (auto varDecl = dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration))
|
|
|
|
analyseVariableReference(*varDecl, _memberAccess);
|
|
|
|
else if (auto funcType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type))
|
|
|
|
if (funcType->kind() == FunctionType::Kind::Internal && funcType->hasDeclaration())
|
|
|
|
visitCallableIfNew(funcType->declaration());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImmutableValidator::visit(IfStatement const& _ifStatement)
|
|
|
|
{
|
|
|
|
bool prevInBranch = m_inBranch;
|
|
|
|
|
|
|
|
_ifStatement.condition().accept(*this);
|
|
|
|
|
|
|
|
m_inBranch = true;
|
|
|
|
_ifStatement.trueStatement().accept(*this);
|
|
|
|
|
|
|
|
if (auto falseStatement = _ifStatement.falseStatement())
|
|
|
|
falseStatement->accept(*this);
|
|
|
|
|
|
|
|
m_inBranch = prevInBranch;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImmutableValidator::visit(WhileStatement const& _whileStatement)
|
|
|
|
{
|
|
|
|
bool prevInLoop = m_inLoop;
|
|
|
|
m_inLoop = true;
|
|
|
|
|
|
|
|
_whileStatement.condition().accept(*this);
|
|
|
|
_whileStatement.body().accept(*this);
|
|
|
|
|
|
|
|
m_inLoop = prevInLoop;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-09 10:12:12 +00:00
|
|
|
bool ImmutableValidator::visit(TryStatement const& _tryStatement)
|
|
|
|
{
|
|
|
|
ScopedSaveAndRestore constructorGuard{m_inTryStatement, true};
|
|
|
|
|
|
|
|
_tryStatement.externalCall().accept(*this);
|
|
|
|
|
|
|
|
for (auto&& clause: _tryStatement.clauses())
|
|
|
|
if (clause)
|
|
|
|
clause->accept(*this);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:18:22 +00:00
|
|
|
void ImmutableValidator::endVisit(IdentifierPath const& _identifierPath)
|
|
|
|
{
|
|
|
|
if (auto const callableDef = dynamic_cast<CallableDeclaration const*>(_identifierPath.annotation().referencedDeclaration))
|
|
|
|
visitCallableIfNew(
|
|
|
|
*_identifierPath.annotation().requiredLookup == VirtualLookup::Virtual ?
|
2022-01-12 17:58:17 +00:00
|
|
|
callableDef->resolveVirtual(m_mostDerivedContract) :
|
2020-08-11 09:18:22 +00:00
|
|
|
*callableDef
|
|
|
|
);
|
|
|
|
|
|
|
|
solAssert(!dynamic_cast<VariableDeclaration const*>(_identifierPath.annotation().referencedDeclaration), "");
|
|
|
|
}
|
|
|
|
|
2020-03-10 17:15:50 +00:00
|
|
|
void ImmutableValidator::endVisit(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
if (auto const callableDef = dynamic_cast<CallableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
2022-01-12 17:58:17 +00:00
|
|
|
visitCallableIfNew(*_identifier.annotation().requiredLookup == VirtualLookup::Virtual ? callableDef->resolveVirtual(m_mostDerivedContract) : *callableDef);
|
2020-03-10 17:15:50 +00:00
|
|
|
if (auto const varDecl = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
|
|
|
|
analyseVariableReference(*varDecl, _identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImmutableValidator::endVisit(Return const& _return)
|
|
|
|
{
|
|
|
|
if (m_currentConstructor != nullptr)
|
|
|
|
checkAllVariablesInitialized(_return.location());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImmutableValidator::analyseCallable(CallableDeclaration const& _callableDeclaration)
|
|
|
|
{
|
2022-01-12 17:58:17 +00:00
|
|
|
ScopedSaveAndRestore constructorGuard{m_currentConstructor, {}};
|
|
|
|
ScopedSaveAndRestore constructorContractGuard{m_currentConstructorContract, {}};
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
if (FunctionDefinition const* funcDef = dynamic_cast<decltype(funcDef)>(&_callableDeclaration))
|
|
|
|
{
|
|
|
|
ASTNode::listAccept(funcDef->modifiers(), *this);
|
|
|
|
|
|
|
|
if (funcDef->isConstructor())
|
2022-01-12 17:58:17 +00:00
|
|
|
{
|
|
|
|
m_currentConstructorContract = funcDef->annotation().contract;
|
2020-03-10 17:15:50 +00:00
|
|
|
m_currentConstructor = funcDef;
|
2022-01-12 17:58:17 +00:00
|
|
|
}
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
if (funcDef->isImplemented())
|
|
|
|
funcDef->body().accept(*this);
|
|
|
|
}
|
|
|
|
else if (ModifierDefinition const* modDef = dynamic_cast<decltype(modDef)>(&_callableDeclaration))
|
2020-04-15 10:42:15 +00:00
|
|
|
if (modDef->isImplemented())
|
|
|
|
modDef->body().accept(*this);
|
2020-03-10 17:15:50 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImmutableValidator::analyseVariableReference(VariableDeclaration const& _variableReference, Expression const& _expression)
|
|
|
|
{
|
|
|
|
if (!_variableReference.isStateVariable() || !_variableReference.immutable())
|
|
|
|
return;
|
|
|
|
|
2020-08-04 11:18:41 +00:00
|
|
|
// If this is not an ordinary assignment, we write and read at the same time.
|
|
|
|
bool write = _expression.annotation().willBeWrittenTo;
|
2020-10-05 10:21:30 +00:00
|
|
|
bool read = !_expression.annotation().willBeWrittenTo || !_expression.annotation().lValueOfOrdinaryAssignment;
|
2020-08-04 11:18:41 +00:00
|
|
|
if (write)
|
2020-03-10 17:15:50 +00:00
|
|
|
{
|
|
|
|
if (!m_currentConstructor)
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
1581_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2020-08-04 11:18:41 +00:00
|
|
|
"Cannot write to immutable here: Immutable variables can only be initialized inline or assigned directly in the constructor."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
|
|
|
else if (m_currentConstructor->annotation().contract->id() != _variableReference.annotation().contract->id())
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
7484_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2020-08-04 11:18:41 +00:00
|
|
|
"Cannot write to immutable here: Immutable variables must be initialized in the constructor of the contract they are defined in."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
|
|
|
else if (m_inLoop)
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
6672_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2020-08-04 11:18:41 +00:00
|
|
|
"Cannot write to immutable here: Immutable variables cannot be initialized inside a loop."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
|
|
|
else if (m_inBranch)
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
4599_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2020-08-04 11:18:41 +00:00
|
|
|
"Cannot write to immutable here: Immutable variables cannot be initialized inside an if statement."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
2023-05-09 10:12:12 +00:00
|
|
|
else if (m_inTryStatement)
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
4130_error,
|
|
|
|
_expression.location(),
|
|
|
|
"Cannot write to immutable here: Immutable variables cannot be initialized inside a try/catch statement."
|
|
|
|
);
|
2020-08-04 11:18:41 +00:00
|
|
|
else if (m_initializedStateVariables.count(&_variableReference))
|
2021-08-05 15:06:19 +00:00
|
|
|
{
|
|
|
|
if (!read)
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
1574_error,
|
|
|
|
_expression.location(),
|
|
|
|
"Immutable state variable already initialized."
|
|
|
|
);
|
|
|
|
else
|
|
|
|
m_errorReporter.typeError(
|
|
|
|
2718_error,
|
|
|
|
_expression.location(),
|
|
|
|
"Immutable variables cannot be modified after initialization."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (read)
|
2020-03-10 17:15:50 +00:00
|
|
|
m_errorReporter.typeError(
|
2021-08-05 15:06:19 +00:00
|
|
|
3969_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2021-08-05 15:06:19 +00:00
|
|
|
"Immutable variables must be initialized using an assignment."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
2020-08-04 11:18:41 +00:00
|
|
|
m_initializedStateVariables.emplace(&_variableReference);
|
2020-03-10 17:15:50 +00:00
|
|
|
}
|
2021-08-05 15:06:19 +00:00
|
|
|
if (
|
|
|
|
read &&
|
|
|
|
m_inCreationContext &&
|
|
|
|
!m_initializedStateVariables.count(&_variableReference)
|
|
|
|
)
|
2020-03-10 17:15:50 +00:00
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
7733_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_expression.location(),
|
2021-08-05 15:06:19 +00:00
|
|
|
"Immutable variables cannot be read before they are initialized."
|
2020-03-10 17:15:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImmutableValidator::checkAllVariablesInitialized(solidity::langutil::SourceLocation const& _location)
|
|
|
|
{
|
2022-01-12 17:58:17 +00:00
|
|
|
for (ContractDefinition const* contract: m_mostDerivedContract.annotation().linearizedBaseContracts | ranges::views::reverse)
|
|
|
|
{
|
2020-03-10 17:15:50 +00:00
|
|
|
for (VariableDeclaration const* varDecl: contract->stateVariables())
|
|
|
|
if (varDecl->immutable())
|
|
|
|
if (!util::contains(m_initializedStateVariables, varDecl))
|
|
|
|
m_errorReporter.typeError(
|
2020-05-05 22:38:28 +00:00
|
|
|
2658_error,
|
2020-03-10 17:15:50 +00:00
|
|
|
_location,
|
|
|
|
solidity::langutil::SecondarySourceLocation().append("Not initialized: ", varDecl->location()),
|
|
|
|
"Construction control flow ends without initializing all immutable state variables."
|
|
|
|
);
|
2022-01-12 17:58:17 +00:00
|
|
|
|
|
|
|
// Don't check further than the current c'tors contract
|
|
|
|
if (contract == m_currentConstructorContract)
|
|
|
|
break;
|
|
|
|
}
|
2020-03-10 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImmutableValidator::visitCallableIfNew(Declaration const& _declaration)
|
|
|
|
{
|
|
|
|
CallableDeclaration const* _callable = dynamic_cast<CallableDeclaration const*>(&_declaration);
|
|
|
|
solAssert(_callable != nullptr, "");
|
|
|
|
|
|
|
|
if (m_visitedCallables.emplace(_callable).second)
|
|
|
|
_declaration.accept(*this);
|
|
|
|
}
|