Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when the storage layout base is near the end of storage (2^64 slots or less) #15912

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions libsolidity/analysis/PostTypeContractLevelChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <libsolutil/FunctionSelector.h>
#include <liblangutil/ErrorReporter.h>

#include <range/v3/action/reverse.hpp>

#include <limits>

using namespace solidity;
Expand Down Expand Up @@ -76,6 +78,8 @@ bool PostTypeContractLevelChecker::check(ContractDefinition const& _contract)
if (_contract.storageLayoutSpecifier())
checkStorageLayoutSpecifier(_contract);

warnStorageLayoutBaseNearStorageEnd(_contract);

return !Error::containsErrors(m_errorReporter.errors());
}

Expand Down Expand Up @@ -145,3 +149,60 @@ void PostTypeContractLevelChecker::checkStorageLayoutSpecifier(ContractDefinitio
"Contract extends past the end of storage when this base slot value is specified."
);
}

namespace
{

VariableDeclaration const* findLastStorageVariable(ContractDefinition const& _contract)
{
for (ContractDefinition const* baseContract: ranges::actions::reverse(_contract.annotation().linearizedBaseContracts))
for (VariableDeclaration const* stateVariable: ranges::actions::reverse(baseContract->stateVariables()))
if (stateVariable->referenceLocation() == VariableDeclaration::Location::Unspecified)
return stateVariable;

return nullptr;
}

}

void PostTypeContractLevelChecker::warnStorageLayoutBaseNearStorageEnd(ContractDefinition const& _contract)
{
// In case of most errors the warning is pointless. E.g. if we're already past storage end.
// If the errors were in the layout specifier, we may not even be able to get values to validate.
if (Error::containsErrors(m_errorReporter.errors()))
return;

bigint storageSize = contractStorageSizeUpperBound(_contract, VariableDeclaration::Location::Unspecified);
u256 baseSlot = layoutBaseForInheritanceHierarchy(_contract, DataLocation::Storage);
solAssert(baseSlot + storageSize <= std::numeric_limits<u256>::max());

if (
u256 slotsLeft = std::numeric_limits<u256>::max() - baseSlot - u256(storageSize);
slotsLeft <= u256(1) << 64
)
{
auto const& location = _contract.storageLayoutSpecifier() ?
_contract.storageLayoutSpecifier()->location() :
_contract.location();

VariableDeclaration const* lastStorageVariable = findLastStorageVariable(_contract);

auto errorID = 3495_error;
std::string errorMsg = "This contract is very close to the end of storage. This limits its future upgradability.";
if (lastStorageVariable)
m_errorReporter.warning(
errorID,
location,
errorMsg,
SecondarySourceLocation{}.append(
fmt::format(
"There are {} storage slots between this state variable and the end of storage.",
formatNumberReadable(slotsLeft)
),
lastStorageVariable->location()
)
);
else
m_errorReporter.warning(errorID, location, errorMsg);
}
}
2 changes: 2 additions & 0 deletions libsolidity/analysis/PostTypeContractLevelChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PostTypeContractLevelChecker

void checkStorageLayoutSpecifier(ContractDefinition const& _contract);

void warnStorageLayoutBaseNearStorageEnd(ContractDefinition const& _contract);

langutil::ErrorReporter& m_errorReporter;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ contract C {
// ====
// EVMVersion: >=cancun
// ----
// Warning 3495: (0-60): This contract is very close to the end of storage. This limits its future upgradability.
// Warning 7325: (17-33): Type uint256[115792089237316195423570985008687907853269984665640564039457584007913129639935] covers a large part of storage and thus makes collisions likely. Either use mappings or dynamic arrays and allow their size to be increased only in small quantities per transaction.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
contract C layout at 2**256 - 1 {
}
// ----
// Warning 3495: (11-31): This contract is very close to the end of storage. This limits its future upgradability.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ contract C layout at 2**256 - 1 {
// ====
// EVMVersion: >=cancun
// ----
// Warning 3495: (11-31): This contract is very close to the end of storage. This limits its future upgradability.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
contract A layout at (2**256 + 1) * 2 - 2**256 - 3 {}
contract B layout at (2**2 - 2**3) * (2**5 - 2**8) {}
// ----
// Warning 3495: (11-51): This contract is very close to the end of storage. This limits its future upgradability.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
contract C layout at 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF {}
// ----
// Warning 3495: (11-87): This contract is very close to the end of storage. This limits its future upgradability.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract A layout at 2**256 - 2**64 {}
contract C layout at 2**256 - 2**65 {
uint[2**63] x;
uint[2**63] y;
}
// ----
// Warning 3495: (11-35): This contract is very close to the end of storage. This limits its future upgradability.
// Warning 3495: (50-74): This contract is very close to the end of storage. This limits its future upgradability.