-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathPostTypeContractLevelChecker.cpp
138 lines (120 loc) · 4.32 KB
/
PostTypeContractLevelChecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Component that verifies overloads, abstract contracts, function clashes and others
* checks at contract or function level.
*/
#include <libsolidity/analysis/PostTypeContractLevelChecker.h>
#include <fmt/format.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/ASTUtils.h>
#include <libsolidity/ast/TypeProvider.h>
#include <libsolutil/FunctionSelector.h>
#include <liblangutil/ErrorReporter.h>
#include <limits>
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::util;
bool PostTypeContractLevelChecker::check(SourceUnit const& _sourceUnit)
{
bool noErrors = true;
for (auto* contract: ASTNode::filteredNodes<ContractDefinition>(_sourceUnit.nodes()))
if (!check(*contract))
noErrors = false;
return noErrors;
}
bool PostTypeContractLevelChecker::check(ContractDefinition const& _contract)
{
solAssert(
_contract.annotation().creationCallGraph.set() &&
_contract.annotation().deployedCallGraph.set(),
""
);
std::map<uint32_t, std::map<std::string, SourceLocation>> errorHashes;
for (ErrorDefinition const* error: _contract.interfaceErrors())
{
std::string signature = error->functionType(true)->externalSignature();
uint32_t hash = selectorFromSignatureU32(signature);
// Fail if there is a different signature for the same hash.
if (!errorHashes[hash].empty() && !errorHashes[hash].count(signature))
{
SourceLocation& otherLocation = errorHashes[hash].begin()->second;
m_errorReporter.typeError(
4883_error,
error->nameLocation(),
SecondarySourceLocation{}.append("This error has a different signature but the same hash: ", otherLocation),
"Error signature hash collision for " + error->functionType(true)->externalSignature()
);
}
else
errorHashes[hash][signature] = error->location();
}
if (auto const* layoutSpecifier = _contract.storageLayoutSpecifier())
checkStorageLayoutSpecifier(*layoutSpecifier);
return !Error::containsErrors(m_errorReporter.errors());
}
void PostTypeContractLevelChecker::checkStorageLayoutSpecifier(StorageLayoutSpecifier const& _storageLayoutSpecifier)
{
Expression const& baseSlotExpression = _storageLayoutSpecifier.baseSlotExpression();
if (!*baseSlotExpression.annotation().isPure)
{
// TODO: introduce and handle erc7201 as a builtin function
m_errorReporter.typeError(
1139_error,
baseSlotExpression.location(),
"The base slot of the storage layout must be a compile-time constant expression."
);
return;
}
auto const* baseSlotExpressionType = type(baseSlotExpression);
auto const* rationalType = dynamic_cast<RationalNumberType const*>(baseSlotExpressionType);
if (!rationalType)
{
m_errorReporter.typeError(
6396_error,
baseSlotExpression.location(),
"The base slot of the storage layout must evaluate to a rational number."
);
return;
}
if (rationalType->isFractional())
{
m_errorReporter.typeError(
1763_error,
baseSlotExpression.location(),
"The base slot of the storage layout must evaluate to an integer."
);
return;
}
solAssert(rationalType->value().denominator() == 1);
if (
rationalType->value().numerator() < 0 ||
rationalType->value().numerator() > std::numeric_limits<u256>::max()
)
{
m_errorReporter.typeError(
6753_error,
baseSlotExpression.location(),
fmt::format(
"The base slot of the storage layout evaluates to {}, which is outside the range of type uint256.",
formatNumberReadable(rationalType->value().numerator())
)
);
return;
}
solAssert(baseSlotExpressionType->isImplicitlyConvertibleTo(*TypeProvider::uint256()));
_storageLayoutSpecifier.annotation().baseSlot = u256(rationalType->value().numerator());
}