forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContractLevelChecker.cpp
570 lines (505 loc) · 19 KB
/
ContractLevelChecker.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
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/ContractLevelChecker.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/TypeProvider.h>
#include <libsolidity/analysis/TypeChecker.h>
#include <libsolutil/FunctionSelector.h>
#include <liblangutil/ErrorReporter.h>
#include <fmt/format.h>
#include <range/v3/view/reverse.hpp>
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::frontend;
namespace
{
template <class T, class B>
bool hasEqualExternalCallableParameters(T const& _a, B const& _b)
{
return FunctionType(_a).asExternallyCallableFunction(false)->hasEqualParameterTypes(
*FunctionType(_b).asExternallyCallableFunction(false)
);
}
template<typename T>
std::map<ASTString, std::vector<T const*>> filterDeclarations(
std::map<ASTString, std::vector<Declaration const*>> const& _declarations)
{
std::map<ASTString, std::vector<T const*>> filteredDeclarations;
for (auto const& [name, overloads]: _declarations)
for (auto const* declaration: overloads)
if (auto typedDeclaration = dynamic_cast<T const*>(declaration))
filteredDeclarations[name].push_back(typedDeclaration);
return filteredDeclarations;
}
}
bool ContractLevelChecker::check(SourceUnit const& _sourceUnit)
{
bool noErrors = true;
findDuplicateDefinitions(
filterDeclarations<FunctionDefinition>(*_sourceUnit.annotation().exportedSymbols)
);
// This check flags duplicate free events when free events become
// a Solidity feature
findDuplicateDefinitions(
filterDeclarations<EventDefinition>(*_sourceUnit.annotation().exportedSymbols)
);
if (Error::containsErrors(m_errorReporter.errors()))
noErrors = false;
for (ASTPointer<ASTNode> const& node: _sourceUnit.nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
if (!check(*contract))
noErrors = false;
return noErrors;
}
bool ContractLevelChecker::check(ContractDefinition const& _contract)
{
_contract.annotation().unimplementedDeclarations = std::vector<Declaration const*>();
checkDuplicateFunctions(_contract);
checkDuplicateEvents(_contract);
checkReceiveFunction(_contract);
m_overrideChecker.check(_contract);
checkBaseConstructorArguments(_contract);
checkAbstractDefinitions(_contract);
checkExternalTypeClashes(_contract);
checkHashCollisions(_contract);
checkLibraryRequirements(_contract);
checkBaseABICompatibility(_contract);
checkPayableFallbackWithoutReceive(_contract);
checkStorageSize(_contract);
return !Error::containsErrors(m_errorReporter.errors());
}
void ContractLevelChecker::checkDuplicateFunctions(ContractDefinition const& _contract)
{
/// Checks that two functions with the same name defined in this contract have different
/// argument types and that there is at most one constructor.
std::map<std::string, std::vector<FunctionDefinition const*>> functions;
FunctionDefinition const* constructor = nullptr;
FunctionDefinition const* fallback = nullptr;
FunctionDefinition const* receive = nullptr;
for (FunctionDefinition const* function: _contract.definedFunctions())
if (function->isConstructor())
{
if (constructor)
m_errorReporter.declarationError(
7997_error,
function->location(),
SecondarySourceLocation().append("Another declaration is here:", constructor->location()),
"More than one constructor defined."
);
constructor = function;
}
else if (function->isFallback())
{
if (fallback)
m_errorReporter.declarationError(
7301_error,
function->location(),
SecondarySourceLocation().append("Another declaration is here:", fallback->location()),
"Only one fallback function is allowed."
);
fallback = function;
}
else if (function->isReceive())
{
if (receive)
m_errorReporter.declarationError(
4046_error,
function->location(),
SecondarySourceLocation().append("Another declaration is here:", receive->location()),
"Only one receive function is allowed."
);
receive = function;
}
else
{
solAssert(!function->name().empty(), "");
functions[function->name()].push_back(function);
}
findDuplicateDefinitions(functions);
}
void ContractLevelChecker::checkDuplicateEvents(ContractDefinition const& _contract)
{
/// Checks that two events with the same name defined in this contract have different
/// argument types
std::map<std::string, std::vector<EventDefinition const*>> events;
for (auto const* contract: _contract.annotation().linearizedBaseContracts)
for (EventDefinition const* event: contract->events())
events[event->name()].push_back(event);
findDuplicateDefinitions(events);
}
void ContractLevelChecker::checkReceiveFunction(ContractDefinition const& _contract)
{
for (FunctionDefinition const* function: _contract.definedFunctions())
{
solAssert(function, "");
if (function->isReceive())
{
if (function->libraryFunction())
m_errorReporter.declarationError(4549_error, function->location(), "Libraries cannot have receive ether functions.");
if (function->stateMutability() != StateMutability::Payable)
m_errorReporter.declarationError(
7793_error,
function->location(),
"Receive ether function must be payable, but is \"" +
stateMutabilityToString(function->stateMutability()) +
"\"."
);
if (function->visibility() != Visibility::External)
m_errorReporter.declarationError(4095_error, function->location(), "Receive ether function must be defined as \"external\".");
if (!function->returnParameters().empty())
m_errorReporter.fatalDeclarationError(6899_error, function->returnParameterList()->location(), "Receive ether function cannot return values.");
if (!function->parameters().empty())
m_errorReporter.fatalDeclarationError(6857_error, function->parameterList().location(), "Receive ether function cannot take parameters.");
}
}
}
template <class T>
void ContractLevelChecker::findDuplicateDefinitions(std::map<std::string, std::vector<T>> const& _definitions)
{
for (auto const& it: _definitions)
{
std::vector<T> const& overloads = it.second;
std::set<size_t> reported;
for (size_t i = 0; i < overloads.size() && !reported.count(i); ++i)
{
SecondarySourceLocation ssl;
for (size_t j = i + 1; j < overloads.size(); ++j)
if (hasEqualExternalCallableParameters(*overloads[i], *overloads[j]))
{
solAssert(
(
dynamic_cast<ContractDefinition const*>(overloads[i]->scope()) &&
dynamic_cast<ContractDefinition const*>(overloads[j]->scope()) &&
overloads[i]->name() == overloads[j]->name()
) ||
(
dynamic_cast<SourceUnit const*>(overloads[i]->scope()) &&
dynamic_cast<SourceUnit const*>(overloads[j]->scope())
),
"Override is neither a namesake function/event in contract scope nor "
"a free function/event (alias)."
);
ssl.append("Other declaration is here:", overloads[j]->location());
reported.insert(j);
}
if (ssl.infos.size() > 0)
{
ErrorId error;
std::string message;
if constexpr (std::is_same_v<T, FunctionDefinition const*>)
{
error = 1686_error;
message = "Function with same name and parameter types defined twice.";
}
else
{
static_assert(std::is_same_v<T, EventDefinition const*>, "Expected \"FunctionDefinition const*\" or \"EventDefinition const*\"");
error = 5883_error;
message = "Event with same name and parameter types defined twice.";
}
ssl.limitSize(message);
m_errorReporter.declarationError(
error,
overloads[i]->location(),
ssl,
message
);
}
}
}
}
void ContractLevelChecker::checkAbstractDefinitions(ContractDefinition const& _contract)
{
// Collects functions, static variable getters and modifiers. If they
// override (unimplemented) base class ones, they are replaced.
std::set<OverrideProxy, OverrideProxy::CompareBySignature> proxies;
auto registerProxy = [&proxies](OverrideProxy const& _overrideProxy)
{
// Overwrite an existing proxy, if it exists.
if (!_overrideProxy.unimplemented())
proxies.erase(_overrideProxy);
proxies.insert(_overrideProxy);
};
// Search from base to derived, collect all functions and modifiers and
// update proxies.
for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts | ranges::views::reverse)
{
for (VariableDeclaration const* v: contract->stateVariables())
if (v->isPartOfExternalInterface())
registerProxy(OverrideProxy(v));
for (FunctionDefinition const* function: contract->definedFunctions())
if (!function->isConstructor())
registerProxy(OverrideProxy(function));
for (ModifierDefinition const* modifier: contract->functionModifiers())
registerProxy(OverrideProxy(modifier));
}
// Set to not fully implemented if at least one flag is false.
for (auto const& proxy: proxies)
if (proxy.unimplemented())
_contract.annotation().unimplementedDeclarations->push_back(proxy.declaration());
if (_contract.abstract())
{
if (_contract.contractKind() == ContractKind::Interface)
m_errorReporter.typeError(9348_error, _contract.location(), "Interfaces do not need the \"abstract\" keyword, they are abstract implicitly.");
else if (_contract.contractKind() == ContractKind::Library)
m_errorReporter.typeError(9571_error, _contract.location(), "Libraries cannot be abstract.");
else
solAssert(_contract.contractKind() == ContractKind::Contract, "");
}
// For libraries, we emit errors on function-level, so this is fine as long as we do
// not have inheritance for libraries.
if (
_contract.contractKind() == ContractKind::Contract &&
!_contract.abstract() &&
!_contract.annotation().unimplementedDeclarations->empty()
)
{
SecondarySourceLocation ssl;
for (auto declaration: *_contract.annotation().unimplementedDeclarations)
ssl.append("Missing implementation: ", declaration->location());
m_errorReporter.typeError(
3656_error,
_contract.location(),
ssl,
"Contract \"" + *_contract.annotation().canonicalName + "\" should be marked as abstract."
);
}
}
void ContractLevelChecker::checkBaseConstructorArguments(ContractDefinition const& _contract)
{
std::vector<ContractDefinition const*> const& bases = _contract.annotation().linearizedBaseContracts;
// Determine the arguments that are used for the base constructors.
for (ContractDefinition const* contract: bases)
{
if (FunctionDefinition const* constructor = contract->constructor())
for (auto const& modifier: constructor->modifiers())
if (auto baseContract = dynamic_cast<ContractDefinition const*>(
modifier->name().annotation().referencedDeclaration
))
{
if (modifier->arguments())
{
if (baseContract->constructor())
annotateBaseConstructorArguments(_contract, baseContract->constructor(), modifier.get());
}
else
m_errorReporter.declarationError(
1563_error,
modifier->location(),
"Modifier-style base constructor call without arguments."
);
}
for (ASTPointer<InheritanceSpecifier> const& base: contract->baseContracts())
{
ContractDefinition const* baseContract = dynamic_cast<ContractDefinition const*>(
base->name().annotation().referencedDeclaration
);
solAssert(baseContract, "");
if (baseContract->constructor() && base->arguments() && !base->arguments()->empty())
annotateBaseConstructorArguments(_contract, baseContract->constructor(), base.get());
}
}
// check that we get arguments for all base constructors that need it.
// If not mark the contract as abstract (not fully implemented)
if (_contract.contractKind() == ContractKind::Contract && !_contract.abstract())
for (ContractDefinition const* baseContract: bases)
if (FunctionDefinition const* baseConstructor = baseContract->constructor())
if (
baseContract != &_contract &&
!baseConstructor->parameters().empty() &&
_contract.annotation().baseConstructorArguments.count(baseConstructor) == 0
)
m_errorReporter.typeError(
3415_error,
_contract.location(),
SecondarySourceLocation{}.append(
"Base constructor parameters:",
baseConstructor->parameterList().location()
),
fmt::format(
"No arguments passed to the base constructor. "
"Specify the arguments or mark \"{}\" as abstract.",
*_contract.annotation().canonicalName
)
);
}
void ContractLevelChecker::annotateBaseConstructorArguments(
ContractDefinition const& _currentContract,
FunctionDefinition const* _baseConstructor,
ASTNode const* _argumentNode
)
{
solAssert(_baseConstructor, "");
solAssert(_argumentNode, "");
auto insertionResult = _currentContract.annotation().baseConstructorArguments.insert(
std::make_pair(_baseConstructor, _argumentNode)
);
if (!insertionResult.second)
{
ASTNode const* previousNode = insertionResult.first->second;
SourceLocation const* mainLocation = nullptr;
SecondarySourceLocation ssl;
if (
_currentContract.location().contains(previousNode->location()) ||
_currentContract.location().contains(_argumentNode->location())
)
{
mainLocation = &previousNode->location();
ssl.append("Second constructor call is here:", _argumentNode->location());
}
else
{
mainLocation = &_currentContract.location();
ssl.append("First constructor call is here:", _argumentNode->location());
ssl.append("Second constructor call is here:", previousNode->location());
}
m_errorReporter.declarationError(
3364_error,
*mainLocation,
ssl,
"Base constructor arguments given twice."
);
}
}
void ContractLevelChecker::checkExternalTypeClashes(ContractDefinition const& _contract)
{
std::map<std::string, std::vector<std::pair<Declaration const*, FunctionTypePointer>>> externalDeclarations;
for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts)
{
for (FunctionDefinition const* f: contract->definedFunctions())
if (f->isPartOfExternalInterface())
{
auto functionType = TypeProvider::function(*f);
// under non error circumstances this should be true
if (functionType->interfaceFunctionType())
externalDeclarations[functionType->externalSignature()].emplace_back(
f, functionType->asExternallyCallableFunction(false)
);
}
for (VariableDeclaration const* v: contract->stateVariables())
if (v->isPartOfExternalInterface())
{
auto functionType = TypeProvider::function(*v);
// under non error circumstances this should be true
if (functionType->interfaceFunctionType())
externalDeclarations[functionType->externalSignature()].emplace_back(
v, functionType->asExternallyCallableFunction(false)
);
}
}
for (auto const& it: externalDeclarations)
for (size_t i = 0; i < it.second.size(); ++i)
for (size_t j = i + 1; j < it.second.size(); ++j)
if (!it.second[i].second->hasEqualParameterTypes(*it.second[j].second))
m_errorReporter.typeError(
9914_error,
it.second[j].first->location(),
"Function overload clash during conversion to external types for arguments."
);
}
void ContractLevelChecker::checkHashCollisions(ContractDefinition const& _contract)
{
std::set<util::FixedHash<4>> hashes;
for (auto const& it: _contract.interfaceFunctionList())
{
util::FixedHash<4> const& hash = it.first;
if (hashes.count(hash))
m_errorReporter.fatalTypeError(
1860_error,
_contract.location(),
std::string("Function signature hash collision for ") + it.second->externalSignature()
);
hashes.insert(hash);
}
}
void ContractLevelChecker::checkLibraryRequirements(ContractDefinition const& _contract)
{
if (!_contract.isLibrary())
return;
if (!_contract.baseContracts().empty())
m_errorReporter.typeError(9469_error, _contract.location(), "Library is not allowed to inherit.");
for (auto const& var: _contract.stateVariables())
if (!var->isConstant())
m_errorReporter.typeError(9957_error, var->location(), "Library cannot have non-constant state variables");
}
void ContractLevelChecker::checkBaseABICompatibility(ContractDefinition const& _contract)
{
if (*_contract.sourceUnit().annotation().useABICoderV2)
return;
if (_contract.isLibrary())
{
solAssert(
_contract.baseContracts().empty() || m_errorReporter.hasErrors(),
"Library is not allowed to inherit"
);
return;
}
SecondarySourceLocation errors;
// interfaceFunctionList contains all inherited functions as well
for (auto const& func: _contract.interfaceFunctionList())
{
solAssert(func.second->hasDeclaration(), "Function has no declaration?!");
if (!*func.second->declaration().sourceUnit().annotation().useABICoderV2)
continue;
auto const& currentLoc = func.second->declaration().location();
for (Type const* paramType: func.second->parameterTypes() + func.second->returnParameterTypes())
if (!TypeChecker::typeSupportedByOldABIEncoder(*paramType, false))
{
errors.append("Type only supported by ABIEncoderV2", currentLoc);
break;
}
}
if (!errors.infos.empty())
m_errorReporter.fatalTypeError(
6594_error,
_contract.location(),
errors,
std::string("Contract \"") +
_contract.name() +
"\" does not use ABI coder v2 but wants to inherit from a contract " +
"which uses types that require it. " +
"Use \"pragma abicoder v2;\" for the inheriting contract as well to enable the feature."
);
}
void ContractLevelChecker::checkPayableFallbackWithoutReceive(ContractDefinition const& _contract)
{
if (auto const* fallback = _contract.fallbackFunction())
if (fallback->isPayable() && !_contract.interfaceFunctionList().empty() && !_contract.receiveFunction())
m_errorReporter.warning(
3628_error,
_contract.location(),
"This contract has a payable fallback function, but no receive ether function. Consider adding a receive ether function.",
SecondarySourceLocation{}.append("The payable fallback function is defined here.", fallback->location())
);
}
void ContractLevelChecker::checkStorageSize(ContractDefinition const& _contract)
{
bigint size = 0;
for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts | ranges::views::reverse)
for (VariableDeclaration const* variable: contract->stateVariables())
if (!(variable->isConstant() || variable->immutable()))
{
size += variable->annotation().type->storageSizeUpperBound();
if (size >= bigint(1) << 256)
{
m_errorReporter.typeError(7676_error, _contract.location(), "Contract requires too much storage.");
break;
}
}
}