-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathAsmAnalysis.cpp
953 lines (865 loc) · 30.9 KB
/
AsmAnalysis.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
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
/**
* Analyzer part of inline assembly.
*/
#include <libyul/AsmAnalysis.h>
#include <libyul/AST.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/Utilities.h>
#include <libyul/Exceptions.h>
#include <libyul/Object.h>
#include <libyul/Scope.h>
#include <libyul/ScopeFiller.h>
#include <liblangutil/ErrorReporter.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/StringUtils.h>
#include <libsolutil/Visitor.h>
#include <libevmasm/Instruction.h>
#include <boost/algorithm/string.hpp>
#include <fmt/format.h>
#include <functional>
using namespace std::string_literals;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::util;
using namespace solidity::langutil;
namespace
{
inline std::string to_string(LiteralKind _kind)
{
switch (_kind)
{
case LiteralKind::Number: return "number";
case LiteralKind::Boolean: return "boolean";
case LiteralKind::String: return "string";
default: yulAssert(false, "");
}
}
}
bool AsmAnalyzer::analyze(Block const& _block)
{
auto watcher = m_errorReporter.errorWatcher();
try
{
// FIXME: Pass location of the object name. Now it's a location of first code section in yul
validateObjectStructure(nativeLocationOf(_block));
if (!(ScopeFiller(m_info, m_errorReporter))(_block))
return false;
(*this)(_block);
}
catch (FatalError const&)
{
// NOTE: There's a cap on the number of reported errors, but watcher.ok() will work fine even if
// we exceed it because the reporter keeps counting (it just stops adding errors to the list).
// Note also that fact of exceeding the cap triggers a FatalError so one can get thrown even
// if we don't make any of our errors fatal.
if (watcher.ok())
{
std::cerr << "Unreported fatal error:" << std::endl;
std::cerr << boost::current_exception_diagnostic_information() << std::endl;
yulAssert(false, "Unreported fatal error.");
}
}
return watcher.ok();
}
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Object const& _object)
{
yulAssert(_object.dialect());
return analyzeStrictAssertCorrect(*_object.dialect(), *_object.code(), _object.summarizeStructure());
}
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(
Dialect const& _dialect,
AST const& _ast,
Object::Structure _objectStructure
)
{
return analyzeStrictAssertCorrect(_dialect, _ast.root(), std::move(_objectStructure));
}
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(
Dialect const& _dialect,
Block const& _astRoot,
Object::Structure _objectStructure
)
{
ErrorList errorList;
langutil::ErrorReporter errors(errorList);
AsmAnalysisInfo analysisInfo;
bool success = AsmAnalyzer(
analysisInfo,
errors,
_dialect,
{},
std::move(_objectStructure)
).analyze(_astRoot);
if (!success)
{
auto formatErrors = [](ErrorList const& _errorList) {
std::vector<std::string> formattedErrors;
for (std::shared_ptr<Error const> const& error: _errorList)
{
yulAssert(error->comment());
formattedErrors.push_back(fmt::format(
// Intentionally not showing source locations because we don't have the original
// source here and it's unlikely they match the pretty-printed version.
// They may not even match the original source if the AST was modified by the optimizer.
"- {} {}: {}",
Error::formatErrorType(error->type()),
error->errorId().error,
*error->comment()
));
}
return joinHumanReadable(formattedErrors, "\n");
};
yulAssert(errors.hasErrors(), "Yul analysis failed but did not report any errors.");
yulAssert(false, fmt::format(
"{}\n\nExpected valid Yul, but errors were reported during analysis:\n{}",
AsmPrinter{_dialect}(_astRoot),
formatErrors(errorList)
));
}
yulAssert(!errors.hasErrors());
return analysisInfo;
}
size_t AsmAnalyzer::operator()(Literal const& _literal)
{
bool erroneousLiteralValue = false;
if (_literal.kind == LiteralKind::String && !_literal.value.unlimited() && _literal.value.hint() && _literal.value.hint()->size() > 32)
{
erroneousLiteralValue = true;
m_errorReporter.typeError(
3069_error,
nativeLocationOf(_literal),
fmt::format("String literal too long ({} > 32)", formatLiteral(_literal, false /* _validated */ ).size())
);
}
else if (_literal.kind == LiteralKind::Number && _literal.value.hint() && bigint(*_literal.value.hint()) > u256(-1))
{
erroneousLiteralValue = true;
m_errorReporter.typeError(6708_error, nativeLocationOf(_literal), "Number literal too large (> 256 bits)");
}
yulAssert(erroneousLiteralValue ^ validLiteral(_literal), "Invalid literal after validating it through AsmAnalyzer.");
return 1;
}
size_t AsmAnalyzer::operator()(Identifier const& _identifier)
{
yulAssert(!_identifier.name.empty());
auto watcher = m_errorReporter.errorWatcher();
if (m_currentScope->lookup(_identifier.name, GenericVisitor{
[&](Scope::Variable const& _var)
{
if (!m_activeVariables.count(&_var))
m_errorReporter.declarationError(
4990_error,
nativeLocationOf(_identifier),
fmt::format("Variable {} used before it was declared.", _identifier.name.str())
);
},
[&](Scope::Function const&)
{
m_errorReporter.typeError(
6041_error,
nativeLocationOf(_identifier),
fmt::format("Function {} used without being called.", _identifier.name.str())
);
}
}))
{
if (m_resolver)
// We found a local reference, make sure there is no external reference.
// Used for side effects, e.g., error reporting in TypeChecker, hence ignoring return value
std::ignore = m_resolver(
_identifier,
yul::IdentifierContext::NonExternal,
m_currentScope->insideFunction()
);
}
else
{
bool found = m_resolver && m_resolver(
_identifier,
yul::IdentifierContext::RValue,
m_currentScope->insideFunction()
);
if (!found && watcher.ok())
// Only add an error message if the callback did not do it.
m_errorReporter.declarationError(
8198_error,
nativeLocationOf(_identifier),
fmt::format("Identifier \"{}\" not found.", _identifier.name.str())
);
}
return 1;
}
void AsmAnalyzer::operator()(ExpressionStatement const& _statement)
{
auto watcher = m_errorReporter.errorWatcher();
size_t numReturns = std::visit(*this, _statement.expression);
if (watcher.ok() && numReturns > 0)
m_errorReporter.typeError(
3083_error,
nativeLocationOf(_statement),
fmt::format(
"Top-level expressions are not supposed to return values (this expression returns {} value{}). "
"Use ``pop()`` or assign them.",
numReturns,
numReturns == 1 ? "" : "s"
)
);
}
void AsmAnalyzer::operator()(Assignment const& _assignment)
{
yulAssert(_assignment.value, "");
size_t const numVariables = _assignment.variableNames.size();
yulAssert(numVariables >= 1, "");
std::set<YulName> variables;
for (auto const& _variableName: _assignment.variableNames)
if (!variables.insert(_variableName.name).second)
m_errorReporter.declarationError(
9005_error,
nativeLocationOf(_assignment),
fmt::format(
"Variable {} occurs multiple times on the left-hand side of the assignment.",
_variableName.name.str()
)
);
size_t numRhsValues = std::visit(*this, *_assignment.value);
if (numRhsValues != numVariables)
m_errorReporter.declarationError(
8678_error,
nativeLocationOf(_assignment),
fmt::format(
"Variable count for assignment to \"{}\" does not match number of values ({} vs. {})",
joinHumanReadable(applyMap(_assignment.variableNames, [](auto const& _identifier){ return _identifier.name.str(); })),
numVariables,
numRhsValues
)
);
for (size_t i = 0; i < numVariables; ++i)
checkAssignment(_assignment.variableNames[i]);
}
void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
{
size_t const numVariables = _varDecl.variables.size();
if (m_resolver)
for (auto const& variable: _varDecl.variables)
// Call the resolver for variable declarations to allow it to raise errors on shadowing.
m_resolver(
yul::Identifier{variable.debugData, variable.name},
yul::IdentifierContext::VariableDeclaration,
m_currentScope->insideFunction()
);
for (auto const& variable: _varDecl.variables)
{
expectValidIdentifier(variable.name, nativeLocationOf(variable));
}
if (_varDecl.value)
{
size_t numValues = std::visit(*this, *_varDecl.value);
if (numValues != numVariables)
m_errorReporter.declarationError(
3812_error,
nativeLocationOf(_varDecl),
fmt::format(
"Variable count mismatch for declaration of \"{}\": {} variables and {} values.",
joinHumanReadable(applyMap(_varDecl.variables, [](auto const& _identifier){ return _identifier.name.str(); })),
numVariables,
numValues
)
);
}
for (NameWithDebugData const& variable: _varDecl.variables)
m_activeVariables.insert(&std::get<Scope::Variable>(
m_currentScope->identifiers.at(variable.name))
);
}
void AsmAnalyzer::operator()(FunctionDefinition const& _funDef)
{
yulAssert(!_funDef.name.empty());
expectValidIdentifier(_funDef.name, nativeLocationOf(_funDef));
Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get();
yulAssert(virtualBlock, "");
Scope& varScope = scope(virtualBlock);
for (auto const& var: _funDef.parameters + _funDef.returnVariables)
{
expectValidIdentifier(var.name, nativeLocationOf(var));
m_activeVariables.insert(&std::get<Scope::Variable>(varScope.identifiers.at(var.name)));
}
if (m_eofVersion.has_value())
{
if (_funDef.parameters.size() >= 0x80)
m_errorReporter.typeError(
8534_error,
nativeLocationOf(_funDef),
"Too many function parameters. At most 127 parameters allowed for EOF"
);
if (_funDef.returnVariables.size() >= 0x80)
m_errorReporter.typeError(
2101_error,
nativeLocationOf(_funDef),
"Too many function return variables. At most 127 return variables allowed for EOF"
);
}
(*this)(_funDef.body);
}
size_t AsmAnalyzer::operator()(FunctionCall const& _funCall)
{
auto watcher = m_errorReporter.errorWatcher();
std::optional<size_t> numParameters;
std::optional<size_t> numReturns;
std::vector<std::optional<LiteralKind>> const* literalArguments = nullptr;
std::visit(
GenericVisitor {
[&](BuiltinName const& _builtinName)
{
BuiltinFunction const& builtin = m_dialect.builtin(_builtinName.handle);
if (builtin.name == "selfdestruct")
m_errorReporter.warning(
1699_error,
nativeLocationOf(_builtinName),
"\"selfdestruct\" has been deprecated. "
"Note that, starting from the Cancun hard fork, the underlying opcode no longer deletes the code and "
"data associated with an account and only transfers its Ether to the beneficiary, "
"unless executed in the same transaction in which the contract was created (see EIP-6780). "
"Any use in newly deployed contracts is strongly discouraged even if the new behavior is taken into account. "
"Future changes to the EVM might further reduce the functionality of the opcode."
);
else if (
builtin.name == "tstore" &&
!m_errorReporter.hasError({2394})
)
{
yulAssert(m_evmVersion.supportsTransientStorage(), "tstore is only a builtin on EVMs that support transient storage");
m_errorReporter.warning(
2394_error,
nativeLocationOf(_builtinName),
"Transient storage as defined by EIP-1153 can break the composability of smart contracts: "
"Since transient storage is cleared only at the end of the transaction and not at the end of the outermost call frame to the contract within a transaction, "
"your contract may unintentionally misbehave when invoked multiple times in a complex transaction. "
"To avoid this, be sure to clear all transient storage at the end of any call to your contract. "
"The use of transient storage for reentrancy guards that are cleared at the end of the call is safe."
);
}
numParameters = builtin.numParameters;
numReturns = builtin.numReturns;
if (!builtin.literalArguments.empty())
literalArguments = &builtin.literalArguments;
validateInstructions(_funCall);
m_sideEffects += builtin.sideEffects;
},
[&](Identifier const& _identifier)
{
bool const identifierInCurrentScope = m_currentScope->lookup(_identifier.name, GenericVisitor{
[&](Scope::Variable const&)
{
m_errorReporter.typeError(
4202_error,
nativeLocationOf(_identifier),
"Attempt to call variable instead of function."
);
},
[&](Scope::Function const& _fun)
{
numParameters = _fun.numArguments;
numReturns = _fun.numReturns;
}
});
if (identifierInCurrentScope)
{
if (m_resolver)
// We found a local reference, make sure there is no external reference.
// Used for side effects, e.g., error reporting in TypeChecker, hence ignoring return value
std::ignore = m_resolver(
_identifier,
yul::IdentifierContext::NonExternal,
m_currentScope->insideFunction()
);
}
else
{
if (!validateInstructions(_funCall))
m_errorReporter.declarationError(
4619_error,
nativeLocationOf(_identifier),
fmt::format("Function \"{}\" not found.", _identifier.name.str())
);
yulAssert(!watcher.ok(), "Expected a reported error.");
}
}
},
_funCall.functionName
);
if (numParameters && _funCall.arguments.size() != *numParameters)
m_errorReporter.typeError(
7000_error,
nativeLocationOf(_funCall.functionName),
fmt::format(
"Function \"{}\" expects {} arguments but got {}.",
resolveFunctionName(_funCall.functionName, m_dialect),
*numParameters,
_funCall.arguments.size()
)
);
for (size_t i = _funCall.arguments.size(); i > 0; i--)
{
Expression const& arg = _funCall.arguments[i - 1];
if (
auto literalArgumentKind = (literalArguments && i <= literalArguments->size()) ?
literalArguments->at(i - 1) :
std::nullopt
)
{
if (!std::holds_alternative<Literal>(arg))
m_errorReporter.typeError(
9114_error,
nativeLocationOf(_funCall.functionName),
"Function expects direct literals as arguments."
);
else if (*literalArgumentKind != std::get<Literal>(arg).kind)
m_errorReporter.typeError(
5859_error,
nativeLocationOf(arg),
fmt::format("Function expects {} literal.", to_string(*literalArgumentKind))
);
else if (*literalArgumentKind == LiteralKind::String)
{
std::string_view functionName = resolveFunctionName(_funCall.functionName, m_dialect);
if (functionName == "datasize" || functionName == "dataoffset")
{
auto const& argumentAsLiteral = std::get<Literal>(arg);
if (!m_objectStructure.contains(formatLiteral(argumentAsLiteral)))
m_errorReporter.typeError(
3517_error,
nativeLocationOf(arg),
fmt::format("Unknown data object \"{}\".", formatLiteral(argumentAsLiteral))
);
}
else if (functionName.substr(0, "verbatim_"s.size()) == "verbatim_")
{
auto const& literalValue = std::get<Literal>(arg).value;
yulAssert(literalValue.unlimited()); // verbatim literals are always unlimited
if (literalValue.builtinStringLiteralValue().empty())
m_errorReporter.typeError(
1844_error,
nativeLocationOf(arg),
"The \"verbatim_*\" builtins cannot be used with empty bytecode."
);
}
else if (functionName == "eofcreate" || functionName == "returncontract")
{
auto const& argumentAsLiteral = std::get<Literal>(arg);
auto const formattedLiteral = formatLiteral(argumentAsLiteral);
if (util::contains(formattedLiteral, '.'))
m_errorReporter.typeError(
2186_error,
nativeLocationOf(arg),
fmt::format("Name required but path given as \"{}\" argument.", functionName)
);
if (!m_objectStructure.topLevelSubObjectNames().count(formattedLiteral))
{
if (m_objectStructure.containsData(formattedLiteral))
m_errorReporter.typeError(
7575_error,
nativeLocationOf(arg),
fmt::format(
"Data name \"{}\" cannot be used as an argument of eofcreate/returncontract. "
"Only an object name is acceptable.",
formattedLiteral
)
);
else
m_errorReporter.typeError(
8970_error,
nativeLocationOf(arg),
fmt::format("Unknown object \"{}\".", formattedLiteral)
);
}
}
expectUnlimitedStringLiteral(std::get<Literal>(arg));
continue;
}
else if (*literalArgumentKind == LiteralKind::Number)
{
std::string_view functionName = resolveFunctionName(_funCall.functionName, m_dialect);
if (functionName == "auxdataloadn")
{
auto const& argumentAsLiteral = std::get<Literal>(arg);
if (argumentAsLiteral.value.value() > std::numeric_limits<uint16_t>::max())
m_errorReporter.typeError(
5202_error,
nativeLocationOf(arg),
"Invalid auxdataloadn argument value. Offset must be in range 0...0xFFFF"
);
}
}
}
expectExpression(arg);
}
if (watcher.ok())
{
yulAssert(numParameters && numParameters == _funCall.arguments.size());
yulAssert(numReturns);
return *numReturns;
}
else if (numReturns)
return *numReturns;
else
return {};
}
void AsmAnalyzer::operator()(If const& _if)
{
expectExpression(*_if.condition);
(*this)(_if.body);
}
void AsmAnalyzer::operator()(Switch const& _switch)
{
yulAssert(_switch.expression, "");
if (_switch.cases.size() == 1 && !_switch.cases[0].value)
m_errorReporter.warning(
9592_error,
nativeLocationOf(_switch),
"\"switch\" statement with only a default case."
);
expectExpression(*_switch.expression);
std::set<u256> cases;
for (auto const& _case: _switch.cases)
{
if (_case.value)
{
auto watcher = m_errorReporter.errorWatcher();
// We cannot use "expectExpression" here because *_case.value is not an
// Expression and would be converted to an Expression otherwise.
(*this)(*_case.value);
/// Note: the parser ensures there is only one default case
if (watcher.ok() && !cases.insert(_case.value->value.value()).second)
m_errorReporter.declarationError(
6792_error,
nativeLocationOf(_case),
fmt::format("Duplicate case \"{}\" defined.", formatLiteral(*_case.value))
);
}
(*this)(_case.body);
}
}
void AsmAnalyzer::operator()(ForLoop const& _for)
{
yulAssert(_for.condition, "");
Scope* outerScope = m_currentScope;
(*this)(_for.pre);
// The block was closed already, but we re-open it again and stuff the
// condition, the body and the post part inside.
m_currentScope = &scope(&_for.pre);
expectExpression(*_for.condition);
// backup outer for-loop & create new state
auto outerForLoop = m_currentForLoop;
m_currentForLoop = &_for;
(*this)(_for.body);
(*this)(_for.post);
m_currentScope = outerScope;
m_currentForLoop = outerForLoop;
}
void AsmAnalyzer::operator()(Block const& _block)
{
auto previousScope = m_currentScope;
m_currentScope = &scope(&_block);
for (auto const& s: _block.statements)
std::visit(*this, s);
m_currentScope = previousScope;
}
void AsmAnalyzer::expectExpression(Expression const& _expr)
{
size_t numValues = std::visit(*this, _expr);
if (numValues != 1)
m_errorReporter.typeError(
3950_error,
nativeLocationOf(_expr),
fmt::format(
"Expected expression to evaluate to one value, but got {} values instead.",
numValues
)
);
}
void AsmAnalyzer::expectUnlimitedStringLiteral(Literal const& _literal)
{
yulAssert(_literal.kind == LiteralKind::String);
yulAssert(_literal.value.unlimited());
}
void AsmAnalyzer::checkAssignment(Identifier const& _variable)
{
yulAssert(!_variable.name.empty());
auto watcher = m_errorReporter.errorWatcher();
bool hasVariable = false;
bool found = false;
if (Scope::Identifier const* var = m_currentScope->lookup(_variable.name))
{
if (m_resolver)
// We found a local reference, make sure there is no external reference.
// Used for side effects, e.g., error reporting in TypeChecker, hence ignoring return value
std::ignore = m_resolver(
_variable,
yul::IdentifierContext::NonExternal,
m_currentScope->insideFunction()
);
if (!std::holds_alternative<Scope::Variable>(*var))
m_errorReporter.typeError(2657_error, nativeLocationOf(_variable), "Assignment requires variable.");
else if (!m_activeVariables.count(&std::get<Scope::Variable>(*var)))
m_errorReporter.declarationError(
1133_error,
nativeLocationOf(_variable),
fmt::format("Variable {} used before it was declared.", _variable.name.str())
);
else
hasVariable = true;
found = true;
}
else if (m_resolver)
{
bool insideFunction = m_currentScope->insideFunction();
if (m_resolver(_variable, yul::IdentifierContext::LValue, insideFunction))
{
found = true;
hasVariable = true;
}
}
if (!found && watcher.ok())
// Only add message if the callback did not.
m_errorReporter.declarationError(4634_error, nativeLocationOf(_variable), "Variable not found or variable not lvalue.");
yulAssert(!watcher.ok() || hasVariable, "");
}
Scope& AsmAnalyzer::scope(Block const* _block)
{
yulAssert(m_info.scopes.count(_block) == 1, "Scope requested but not present.");
auto scopePtr = m_info.scopes.at(_block);
yulAssert(scopePtr, "Scope requested but not present.");
return *scopePtr;
}
void AsmAnalyzer::expectValidIdentifier(YulName _identifier, SourceLocation const& _location)
{
std::string_view const label = _identifier.str();
// NOTE: the leading dot case is handled by the parser not allowing it.
if (label.ends_with('.'))
m_errorReporter.syntaxError(
3384_error,
_location,
fmt::format("\"{}\" is not a valid identifier (ends with a dot).", label)
);
if (label.find("..") != std::string::npos)
m_errorReporter.syntaxError(
7771_error,
_location,
fmt::format("\"{}\" is not a valid identifier (contains consecutive dots).", label)
);
if (m_dialect.reservedIdentifier(label))
m_errorReporter.declarationError(
5017_error,
_location,
fmt::format("The identifier \"{}\" is reserved and can not be used.", label)
);
}
bool AsmAnalyzer::validateInstructions(std::string_view _instructionIdentifier, langutil::SourceLocation const& _location)
{
// NOTE: This function uses the default EVM version instead of the currently selected one.
auto const& defaultEVMDialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{}, std::nullopt);
auto const builtinHandle = defaultEVMDialect.findBuiltin(_instructionIdentifier);
if (builtinHandle && defaultEVMDialect.builtin(*builtinHandle).instruction.has_value())
return validateInstructions(*defaultEVMDialect.builtin(*builtinHandle).instruction, _location);
solAssert(!m_eofVersion.has_value() || *m_eofVersion == 1);
auto const& eofDialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::firstWithEOF(), 1);
auto const eofBuiltinHandle = eofDialect.findBuiltin(_instructionIdentifier);
if (eofBuiltinHandle)
{
auto const builtin = eofDialect.builtin(*eofBuiltinHandle);
if (builtin.instruction.has_value())
return validateInstructions(*builtin.instruction, _location);
// If builtin is available in EOF but not available in legacy (and we build to legacy) generate custom error.
else if (!m_eofVersion.has_value() && !builtinHandle)
{
m_errorReporter.declarationError(
7223_error,
_location,
fmt::format(
"Builtin function \"{function}\" is only available in EOF.",
fmt::arg("function", _instructionIdentifier)
)
);
return true;
}
}
return false;
}
bool AsmAnalyzer::validateInstructions(evmasm::Instruction _instr, SourceLocation const& _location)
{
// We assume that returndatacopy, returndatasize and staticcall are either all available
// or all not available.
yulAssert(m_evmVersion.supportsReturndata() == m_evmVersion.hasStaticCall(), "");
// Similarly we assume bitwise shifting and create2 go together.
yulAssert(m_evmVersion.hasBitwiseShifting() == m_evmVersion.hasCreate2(), "");
// These instructions are disabled in the dialect.
yulAssert(
_instr != evmasm::Instruction::JUMP &&
_instr != evmasm::Instruction::JUMPI &&
_instr != evmasm::Instruction::JUMPDEST &&
_instr != evmasm::Instruction::DATALOADN &&
_instr != evmasm::Instruction::EOFCREATE &&
_instr != evmasm::Instruction::RETURNCONTRACT &&
_instr != evmasm::Instruction::RJUMP &&
_instr != evmasm::Instruction::RJUMPI &&
_instr != evmasm::Instruction::CALLF &&
_instr != evmasm::Instruction::JUMPF &&
_instr != evmasm::Instruction::RETF &&
_instr != evmasm::Instruction::DUPN &&
_instr != evmasm::Instruction::SWAPN
);
auto errorForVM = [&](ErrorId _errorId, std::string const& vmKindMessage) {
m_errorReporter.typeError(
_errorId,
_location,
fmt::format(
"The \"{instruction}\" instruction is {kind} VMs (you are currently compiling for \"{version}\").",
fmt::arg("instruction", boost::to_lower_copy(instructionInfo(_instr, m_evmVersion).name)),
fmt::arg("kind", vmKindMessage),
fmt::arg("version", m_evmVersion.name())
)
);
};
// The errors below are meant to be issued when processing an undeclared identifier matching a builtin name
// present on the default EVM version but not on the currently selected one,
// since the other `validateInstructions()` overload uses the default EVM version.
if (_instr == evmasm::Instruction::RETURNDATACOPY && !m_evmVersion.supportsReturndata())
errorForVM(7756_error, "only available for Byzantium-compatible");
else if (_instr == evmasm::Instruction::RETURNDATASIZE && !m_evmVersion.supportsReturndata())
errorForVM(4778_error, "only available for Byzantium-compatible");
else if (_instr == evmasm::Instruction::STATICCALL && !m_evmVersion.hasStaticCall())
errorForVM(1503_error, "only available for Byzantium-compatible");
else if (_instr == evmasm::Instruction::SHL && !m_evmVersion.hasBitwiseShifting())
errorForVM(6612_error, "only available for Constantinople-compatible");
else if (_instr == evmasm::Instruction::SHR && !m_evmVersion.hasBitwiseShifting())
errorForVM(7458_error, "only available for Constantinople-compatible");
else if (_instr == evmasm::Instruction::SAR && !m_evmVersion.hasBitwiseShifting())
errorForVM(2054_error, "only available for Constantinople-compatible");
else if (_instr == evmasm::Instruction::CREATE2 && !m_evmVersion.hasCreate2())
errorForVM(6166_error, "only available for Constantinople-compatible");
else if (_instr == evmasm::Instruction::EXTCODEHASH && !m_evmVersion.hasExtCodeHash())
errorForVM(7110_error, "only available for Constantinople-compatible");
else if (_instr == evmasm::Instruction::CHAINID && !m_evmVersion.hasChainID())
errorForVM(1561_error, "only available for Istanbul-compatible");
else if (_instr == evmasm::Instruction::SELFBALANCE && !m_evmVersion.hasSelfBalance())
errorForVM(7721_error, "only available for Istanbul-compatible");
else if (_instr == evmasm::Instruction::BASEFEE && !m_evmVersion.hasBaseFee())
errorForVM(5430_error, "only available for London-compatible");
else if (_instr == evmasm::Instruction::BLOBBASEFEE && !m_evmVersion.hasBlobBaseFee())
errorForVM(6679_error, "only available for Cancun-compatible");
else if (_instr == evmasm::Instruction::BLOBHASH && !m_evmVersion.hasBlobHash())
errorForVM(8314_error, "only available for Cancun-compatible");
else if (_instr == evmasm::Instruction::MCOPY && !m_evmVersion.hasMcopy())
errorForVM(7755_error, "only available for Cancun-compatible");
else if ((_instr == evmasm::Instruction::TSTORE || _instr == evmasm::Instruction::TLOAD) && !m_evmVersion.supportsTransientStorage())
errorForVM(6243_error, "only available for Cancun-compatible");
else if (_instr == evmasm::Instruction::PC)
m_errorReporter.error(
2450_error,
Error::Type::SyntaxError,
_location,
"PC instruction is a low-level EVM feature. "
"Because of that PC is disallowed in strict assembly."
);
else if (!m_eofVersion.has_value() && (
_instr == evmasm::Instruction::EXTCALL ||
_instr == evmasm::Instruction::EXTDELEGATECALL ||
_instr == evmasm::Instruction::EXTSTATICCALL
))
{
m_errorReporter.typeError(
4328_error,
_location,
fmt::format(
"The \"{instruction}\" instruction is only available in EOF.",
fmt::arg("instruction", boost::to_lower_copy(instructionInfo(_instr, m_evmVersion).name))
)
);
}
else if (m_eofVersion.has_value() && (
_instr == evmasm::Instruction::CALL ||
_instr == evmasm::Instruction::CALLCODE ||
_instr == evmasm::Instruction::DELEGATECALL ||
_instr == evmasm::Instruction::STATICCALL ||
_instr == evmasm::Instruction::SELFDESTRUCT ||
_instr == evmasm::Instruction::JUMP ||
_instr == evmasm::Instruction::JUMPI ||
_instr == evmasm::Instruction::PC ||
_instr == evmasm::Instruction::CREATE ||
_instr == evmasm::Instruction::CREATE2 ||
_instr == evmasm::Instruction::CODESIZE ||
_instr == evmasm::Instruction::CODECOPY ||
_instr == evmasm::Instruction::EXTCODESIZE ||
_instr == evmasm::Instruction::EXTCODECOPY ||
_instr == evmasm::Instruction::EXTCODEHASH ||
_instr == evmasm::Instruction::GAS
))
{
m_errorReporter.typeError(
9132_error,
_location,
fmt::format(
"The \"{instruction}\" instruction is {kind} VMs (you are currently compiling to EOF).",
fmt::arg("instruction", boost::to_lower_copy(instructionInfo(_instr, m_evmVersion).name)),
fmt::arg("kind", "only available in legacy bytecode")
)
);
}
else
{
// Sanity check
solAssert(m_evmVersion.hasOpcode(_instr, m_eofVersion));
return false;
}
// Sanity check
// PC is not available in strict assembly but it is always valid opcode in legacy evm.
solAssert(_instr == evmasm::Instruction::PC || !m_evmVersion.hasOpcode(_instr, m_eofVersion));
return true;
}
bool AsmAnalyzer::validateInstructions(FunctionCall const& _functionCall)
{
return validateInstructions(
resolveFunctionName(_functionCall.functionName, m_dialect),
nativeLocationOf(_functionCall.functionName)
);
}
void AsmAnalyzer::validateObjectStructure(langutil::SourceLocation const& _astRootLocation)
{
if (m_eofVersion.has_value())
{
if (util::contains(m_objectStructure.objectName, '.')) // No dots in object name for EOF
m_errorReporter.syntaxError(
9822_error,
_astRootLocation,
fmt::format(
"The object name \"{objectName}\" is invalid in EOF context. Object names must not contain 'dot' character.",
fmt::arg("objectName", m_objectStructure.objectName)
)
);
else if (m_objectStructure.topLevelSubObjectNames().size() > 256)
{
m_errorReporter.syntaxError(
1305_error,
_astRootLocation,
fmt::format(
"Too many subobjects in \"{objectName}\". At most 256 subobjects allowed when compiling to EOF",
fmt::arg("objectName", m_objectStructure.objectName)
)
);
}
}
}