forked from argotorg/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderDependentStateDestroyer.cpp
More file actions
1734 lines (1392 loc) · 45.9 KB
/
OrderDependentStateDestroyer.cpp
File metadata and controls
1734 lines (1392 loc) · 45.9 KB
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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/>.
*/
/**
* Optimiser component that destroys order-dependent state accesses.
*/
#include <libyul/AsmPrinter.h>
#include "libsolutil/Common.h"
#include "libyul/Dialect.h"
#include "libyul/Utilities.h"
#include "libyul/YulString.h"
#include "libyul/backends/evm/EVMDialect.h"
#include "libyul/optimiser/ASTWalker.h"
#include <algorithm>
#include <boost/range/adaptor/reversed.hpp>
#include <cstddef>
#include <deque>
#include <iostream>
#include <iterator>
#include <libevmasm/Instruction.h>
#include <libyul/Exceptions.h>
#include <libsolutil/Assertions.h>
#include <libyul/AsmDataForward.h>
#include <libyul/optimiser/OrderDependentStateDestroyer.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/AsmData.h>
#include <libsolutil/CommonData.h>
#include <boost/range/algorithm_ext/erase.hpp>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <set>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
using namespace std;
using namespace solidity;
using namespace solidity::yul;
static const YulString MAIN("!!main");
static const YulString MEMORY("!!memory");
enum class BlockType
{
Root,
Function,
For,
If,
};
class TaintBlock
{
public:
TaintBlock() = delete;
explicit TaintBlock(BlockType bt);
BlockType type() const { return m_type; }
YulString const& name() const { return m_name; }
private:
static unsigned int s_block_count;
YulString m_name;
BlockType m_type;
std::vector<YulString> m_conditions;
};
unsigned int TaintBlock::s_block_count;
TaintBlock::TaintBlock(BlockType bt) : m_type(bt)
{
std::stringstream ss;
ss << "!!block_" << s_block_count;
m_name = YulString(ss.str());
s_block_count += 1;
}
struct CallSite
{
YulString callee;
std::vector<std::optional<YulString>> arguments;
std::vector<YulString> returns;
};
enum class Virtue { Clean = 0, Undecided, Tainted };
class Variable
{
public:
Variable(YulString _name, Virtue _virtue = Virtue::Undecided):
m_name(_name), m_virtue(_virtue), m_protected(false), m_constant(true) {}
Virtue virtue() const { return m_virtue; }
const char* virtue_str() const
{
switch (m_virtue)
{
case Virtue::Undecided:
return "Undecided";
case Virtue::Clean:
return "Clean";
case Virtue::Tainted:
return "Tainted";
default:
return "";
}
}
bool isProtected() const { return m_protected; }
void setProtected()
{
m_protected = true;
}
void setTainted()
{
m_virtue = Virtue::Tainted;
}
void setClean()
{
switch (m_virtue)
{
case Virtue::Clean:
case Virtue::Undecided:
m_virtue = Virtue::Clean;
break;
default:
assertThrow(false, OptimizerException, "conflict: already tainted");
break;
}
}
bool isConstant() const { return m_constant; }
std::optional<u256> value() const { return m_value; }
void removeConstant()
{
m_constant = false;
m_value.reset();
}
void value(u256 _v)
{
if (!m_constant) return;
if (m_value.has_value())
{
if (_v == *m_value)
{
return;
}
else
{
removeConstant();
}
}
m_value = _v;
}
private:
Variable() = delete;
YulString m_name;
Virtue m_virtue;
bool m_protected;
bool m_constant;
std::optional<u256> m_value;
};
class TaintMemory
{
public:
TaintMemory(): m_counter(0), m_bytes() {}
YulString write(u256 _addr, u256 _len);
YulString write(const Variable& _var, const Variable& _len);
YulString write(const Variable& _var, u256 _len);
std::set<YulString> read(u256 _addr, u256 _len);
std::set<YulString> read(Variable const& _var, u256 _len);
std::set<YulString> read(Variable const& _var, const Variable& _len);
void clear() { m_bytes.clear(); }
private:
unsigned int m_counter;
std::map<u256, YulString> m_bytes;
YulString newVariable();
};
YulString TaintMemory::newVariable()
{
unsigned int c = m_counter;
m_counter += 1;
std::stringstream ss;
ss << "!!m" << c;
return YulString(ss.str());
}
std::set<YulString> TaintMemory::read(Variable const& _addr, Variable const& _len)
{
if (_len.isConstant())
{
assertThrow(_len.value().has_value(), OptimizerException, "length constant has no value");
return read(_addr, *_len.value());
}
else
{
u256 addr = 0;
if (_addr.isConstant() && _addr.value().has_value())
{
addr = *_addr.value();
}
std::set<YulString> vars;
vars.insert(MEMORY);
for (auto const& kv: m_bytes)
{
if (kv.first >= addr)
{
vars.insert(kv.second);
}
}
return vars;
}
}
std::set<YulString> TaintMemory::read(Variable const& _addr, u256 _len)
{
if (_addr.isConstant())
{
assertThrow(_addr.value().has_value(), OptimizerException, "address constant has no value");
return read(*_addr.value(), _len);
}
else
{
std::set<YulString> vars;
vars.insert(MEMORY);
for (auto const& kv: m_bytes)
{
vars.insert(kv.second);
}
return vars;
}
}
std::set<YulString> TaintMemory::read(u256 _addr, u256 _len)
{
std::cout << "Memory Read at " << _addr << " (" << _len << " bytes)" << std::endl;
std::set<YulString> vars;
for (u256 ii = 0; ii < _len; ii++)
{
YulString name;
try
{
name = m_bytes.at(ii + _addr);
}
catch (std::out_of_range const&)
{
name = MEMORY;
}
vars.insert(name);
}
return vars;
}
YulString TaintMemory::write(const Variable& _addr, const Variable& _len)
{
u256 length;
if (_len.isConstant())
{
assertThrow(_len.value().has_value(), OptimizerException, "length constant has no value");
return write(_addr, *_len.value());
}
else
{
// TODO: This can technically only affect keys >= _addr
m_bytes.clear();
return MEMORY;
}
}
YulString TaintMemory::write(const Variable& _addr, u256 _len)
{
if (_addr.isConstant())
{
assertThrow(_addr.value().has_value(), OptimizerException, "address constant has no value");
return write(*_addr.value(), _len);
}
else
{
m_bytes.clear();
return MEMORY;
}
}
YulString TaintMemory::write(u256 _addr, u256 _len)
{
std::cout << "Memory write to " << _addr << " (" << _len << " bytes)" << std::endl;
YulString name(newVariable());
for (u256 ii = 0; ii < _len; ii++)
{
m_bytes[ii + _addr] = name;
}
return name;
}
class FunctionScope;
class State
{
public:
static std::shared_ptr<State> make_shared(Dialect const& _dialect);
State() = delete;
Dialect const& dialect() { return m_dialect; }
FunctionScope const& function(YulString const& _name) const { return m_functions.at(_name); }
FunctionScope& function(YulString const& _name) { return m_functions.at(_name); }
FunctionScope& addFunction(FunctionScope scope);
Variable const& variable(YulString const& _name) const { return m_variables.at(_name); }
Variable& variable(YulString const& _name) { return m_variables.at(_name); }
Variable& addVariable(YulString _name, bool _allowDuplicate = false);
void taintVariable(YulString const& _name);
void protectVariable(YulString const& _name);
void protectVariable(Expression const& _expr);
YulString duplicateVariable(YulString const& _name);
std::set<YulString> taintedVariables() const;
void enterAssignment(std::vector<YulString>);
void leaveAssignment(std::vector<YulString> const&);
std::vector<YulString> const& currentAssignment() const;
void dump() const;
void resolve();
std::optional<YulString> verify() const;
TaintBlock const& currentBlock() const { return m_blocks.back(); }
void enterBlock(BlockType type);
void leaveBlock(BlockType type);
TaintBlock const& findBlock(BlockType type);
TaintMemory const& memory() const { return m_memory; }
TaintMemory& memory() { return m_memory; }
private:
unsigned int m_rename;
Dialect const& m_dialect;
std::map<YulString, Variable> m_variables;
std::vector<YulString> m_current_assignment;
std::map<YulString, FunctionScope> m_functions;
std::vector<TaintBlock> m_blocks;
TaintMemory m_memory;
explicit State(Dialect const&);
};
State::State(Dialect const& _dialect): m_rename(0), m_dialect(_dialect)
{
enterBlock(BlockType::Root);
}
std::set<YulString> State::taintedVariables() const
{
std::set<YulString> tainted;
for (auto const& kv: m_variables)
{
if (kv.second.virtue() == Virtue::Tainted)
{
tainted.insert(kv.first);
}
}
return tainted;
}
TaintBlock const& State::findBlock(BlockType type)
{
for (auto it = m_blocks.rbegin(); it != m_blocks.rend(); it++)
{
TaintBlock const& block = *it;
if (block.type() == type)
{
return block;
}
}
assertThrow(false, OptimizerException, "no block found");
}
void State::enterBlock(BlockType _type)
{
m_blocks.emplace_back(_type);
addVariable(currentBlock().name());
}
void State::leaveBlock(BlockType _type)
{
assertThrow(m_blocks.back().type() == _type, OptimizerException, "mismatched block");
m_blocks.pop_back();
}
YulString State::duplicateVariable(YulString const& _name)
{
if (_name == MEMORY) return MEMORY;
std::stringstream ss;
ss << _name.str() << "_embed" << m_rename;
YulString new_name = YulString(ss.str());
m_rename += 1;
addVariable(new_name) = m_variables.at(_name);
return new_name;
}
void State::taintVariable(YulString const& _name)
{
std::cout << "Tainting " << _name.str() << std::endl;
m_variables.at(_name).setTainted();
}
void State::protectVariable(YulString const& _name)
{
m_variables.at(_name).setProtected();
}
void State::protectVariable(Expression const& _expr)
{
if (holds_alternative<Literal>(_expr)) return;
assertThrow(holds_alternative<Identifier>(_expr), OptimizerException, "untaintable non-ident");
Identifier const& ident = std::get<Identifier>(_expr);
protectVariable(ident.name);
}
Variable& State::addVariable(YulString _name, bool _allowDuplicate)
{
Variable var(_name);
auto result = m_variables.emplace(std::make_pair(_name, var));
assertThrow(_allowDuplicate || result.second, OptimizerException, "duplicate variable");
return m_variables.at(_name);
}
void State::enterAssignment(std::vector<YulString> _vars)
{
assertThrow(m_current_assignment.empty(), OptimizerException, "double assign");
m_current_assignment = _vars;
}
void State::leaveAssignment(std::vector<YulString> const& _vars)
{
assertThrow(!m_current_assignment.empty(), OptimizerException, "missing assign");
assertThrow(_vars.size() == m_current_assignment.size(), OptimizerException, "mismatched assign");
m_current_assignment.clear();
}
std::vector<YulString> const& State::currentAssignment() const
{
return m_current_assignment;
}
class FunctionScope: public ASTWalker
{
public:
explicit FunctionScope(std::weak_ptr<State> _state);
FunctionScope(std::weak_ptr<State> _state, FunctionDefinition const& _funDef);
YulString const& name() const { return m_name; }
using ASTWalker::operator();
void operator()(Assignment const& _assignment) override;
void operator()(VariableDeclaration const& _varDecl) override;
void operator()(FunctionCall const& _funCall) override;
void operator()(If const& _if) override;
void operator()(Switch const& _switch) override;
void operator()(ForLoop const& _for) override;
void operator()(Break const&) override;
void operator()(Continue const& _continue) override;
void dump_state() const;
bool isResolved() const { return m_unresolved_calls.empty(); }
bool resolve();
void propagateTaint();
std::vector<YulString> findPath(YulString const&, YulString const&) const;
private:
std::weak_ptr<State> m_state;
YulString m_name;
std::map<YulString, std::set<YulString>> m_data_flow;
std::vector<YulString> m_parameters;
std::vector<YulString> m_returns;
std::vector<CallSite> m_unresolved_calls;
FunctionScope() = delete;
void enterBlock(BlockType type);
void leaveBlock(BlockType type);
void copyConstness(YulString _upstream, YulString _downstream);
void influences(YulString _upstream, YulString _downstream);
void influences(Expression const& _upstream, YulString _downstream);
void visitBuiltin(FunctionCall const& _funCall, BuiltinFunctionForEVM const* _builtin);
void visitFunc(FunctionCall const&);
void visitMLoad(FunctionCall const& _funCall);
void visitMStore(FunctionCall const& _funCall);
void visitKeccak256(FunctionCall const& _funCall);
void embed(FunctionScope const&, std::vector<std::optional<YulString>> const&, std::vector<YulString> const&);
std::set<YulString> findDownstream(YulString const& _source) const;
};
FunctionScope::FunctionScope(std::weak_ptr<State> _state)
: m_state(_state), m_name(MAIN)
{
}
void FunctionScope::enterBlock(BlockType _type)
{
std::shared_ptr<State> state(m_state.lock());
YulString top = state->currentBlock().name();
state->enterBlock(_type);
YulString cur = state->currentBlock().name();
influences(top, cur);
}
void FunctionScope::leaveBlock(BlockType _type)
{
std::shared_ptr<State> state(m_state.lock());
state->leaveBlock(_type);
}
FunctionScope::FunctionScope(std::weak_ptr<State> _state, FunctionDefinition const& _funDef)
: m_state(_state), m_name(_funDef.name)
{
std::shared_ptr<State> state(_state.lock());
state->memory().clear();
for (TypedName const& tn: _funDef.parameters)
{
state->addVariable(tn.name).removeConstant();
m_parameters.emplace_back(tn.name);
}
for (TypedName const& tn: _funDef.returnVariables)
{
state->addVariable(tn.name);
m_returns.emplace_back(tn.name);
}
}
void FunctionScope::copyConstness(YulString _upstream, YulString _downstream)
{
std::shared_ptr<State> state(m_state.lock());
Variable const& upvar = state->variable(_upstream);
Variable& downvar = state->variable(_downstream);
if (upvar.isConstant())
{
std::cout << _upstream.str() << std::endl;
assertThrow(upvar.value().has_value(), OptimizerException, "const without value");
u256 v = *upvar.value();
downvar.value(v);
}
else
{
downvar.removeConstant();
}
}
void FunctionScope::influences(YulString _upstream, YulString _downstream)
{
m_data_flow[_upstream].emplace(_downstream);
}
void FunctionScope::influences(Expression const& _upstream, YulString _downstream)
{
if (holds_alternative<Literal>(_upstream))
{
return;
}
assertThrow(holds_alternative<Identifier>(_upstream), OptimizerException, "upstream expr must be literal or identifier");
Identifier const& upstream_ident = std::get<Identifier>(_upstream);
influences(upstream_ident.name, _downstream);
}
void FunctionScope::operator()(Switch const& _switch)
{
std::shared_ptr<State> state(m_state.lock());
const Expression& cond_expr = *_switch.expression;
assertThrow(holds_alternative<Identifier>(cond_expr), OptimizerException, "switch with non-identifier");
const Identifier& cond_ident = std::get<Identifier>(cond_expr);
enterBlock(BlockType::If);
influences(cond_ident.name, state->currentBlock().name());
std::cout << state->currentBlock().name().str() << " is switch (" << cond_ident.name.str() << ")" << std::endl;
for (auto const& _case: _switch.cases)
{
if (_case.value)
(*this)(*_case.value);
(*this)(_case.body);
}
leaveBlock(BlockType::If);
}
void FunctionScope::operator()(If const& _if)
{
std::shared_ptr<State> state(m_state.lock());
const Expression& cond_expr = *_if.condition;
assertThrow(holds_alternative<Identifier>(cond_expr), OptimizerException, "if with non-identifier");
const Identifier& cond_ident = std::get<Identifier>(cond_expr);
enterBlock(BlockType::If);
influences(cond_ident.name, state->currentBlock().name());
std::cout << state->currentBlock().name().str() << " is if (" << cond_ident.name.str() << ")" << std::endl;
(*this)(_if.body);
leaveBlock(BlockType::If);
}
void FunctionScope::operator()(Assignment const& _assignment)
{
std::shared_ptr<State> state(m_state.lock());
auto const &cb = state->currentBlock();
for (Identifier const& v: _assignment.variableNames)
{
influences(cb.name(), v.name);
}
if (holds_alternative<Identifier>(*_assignment.value))
{
assertThrow(
1 == _assignment.variableNames.size(),
OptimizerException,
"not enough variables to unpack (expected 1)"
);
Identifier const& ident = std::get<Identifier>(*_assignment.value);
influences(ident.name, _assignment.variableNames[0].name);
copyConstness(ident.name, _assignment.variableNames[0].name);
}
else if (holds_alternative<Literal>(*_assignment.value))
{
assertThrow(
1 == _assignment.variableNames.size(),
OptimizerException,
"not enough variables to unpack (expected 1)"
);
// TODO: Maybe setClean the variable
Literal const& literal = std::get<Literal>(*_assignment.value);
u256 value = valueOfLiteral(literal);
state->variable(_assignment.variableNames.at(0).name).value(value);
}
else if (holds_alternative<FunctionCall>(*_assignment.value))
{
FunctionCall const& call = std::get<FunctionCall>(*_assignment.value);
std::vector<YulString> vars;
for (Identifier const& ident: _assignment.variableNames)
{
state->variable(ident.name).removeConstant();
vars.push_back(ident.name);
}
state->enterAssignment(vars);
(*this)(call);
state->leaveAssignment(vars);
}
else
{
assertThrow(false, OptimizerException, "unexpected assignment value");
}
}
void FunctionScope::operator()(VariableDeclaration const& _varDecl)
{
std::shared_ptr<State> state(m_state.lock());
auto const &cb = state->currentBlock();
for (TypedName const& tn: _varDecl.variables)
{
state->addVariable(tn.name);
influences(cb.name(), tn.name);
}
if (holds_alternative<Identifier>(*_varDecl.value))
{
assertThrow(
1 == _varDecl.variables.size(),
OptimizerException,
"not enough variables to unpack (expected 1)"
);
Identifier const& ident = std::get<Identifier>(*_varDecl.value);
influences(ident.name, _varDecl.variables[0].name);
copyConstness(ident.name, _varDecl.variables[0].name);
}
else if (holds_alternative<Literal>(*_varDecl.value))
{
assertThrow(
1 == _varDecl.variables.size(),
OptimizerException,
"not enough variables to unpack (expected 1)"
);
// TODO: Possibly setClean on the variable.
Literal const& literal = std::get<Literal>(*_varDecl.value);
u256 value = valueOfLiteral(literal);
state->variable(_varDecl.variables.at(0).name).value(value);
}
else if (holds_alternative<FunctionCall>(*_varDecl.value))
{
FunctionCall const& call = std::get<FunctionCall>(*_varDecl.value);
std::vector<YulString> vars;
for (TypedName const& tn: _varDecl.variables)
{
state->variable(tn.name).removeConstant();
vars.push_back(tn.name);
}
state->enterAssignment(vars);
(*this)(call);
state->leaveAssignment(vars);
}
else
{
assertThrow(false, OptimizerException, "unexpected declaration value");
}
}
void FunctionScope::operator()(ForLoop const& _for)
{
assertThrow(holds_alternative<Literal>(*_for.condition), OptimizerException, "bad for");
enterBlock(BlockType::For);
(*this)(_for.pre);
visit(*_for.condition);
(*this)(_for.body);
(*this)(_for.post);
leaveBlock(BlockType::For);
}
void FunctionScope::operator()(Continue const&)
{
std::shared_ptr<State> state(m_state.lock());
TaintBlock const& loop = state->findBlock(BlockType::For);
TaintBlock const& current = state->currentBlock();
influences(current.name(), loop.name());
}
void FunctionScope::operator()(Break const&)
{
std::shared_ptr<State> state(m_state.lock());
TaintBlock const& loop = state->findBlock(BlockType::For);
TaintBlock const& current = state->currentBlock();
influences(current.name(), loop.name());
}
void FunctionScope::operator()(FunctionCall const& _funCall)
{
std::shared_ptr<State> state(m_state.lock());
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&state->dialect()))
{
if (auto const* builtin = dialect->builtin(_funCall.functionName.name))
{
visitBuiltin(_funCall, builtin);
}
else
{
visitFunc(_funCall);
}
}
else
{
assertThrow(false, OptimizerException, "only EVM dialect is supported");
}
}
void FunctionScope::visitFunc(FunctionCall const& _funCall)
{
std::cout << "visitFunc " << _funCall.functionName.name.str() << std::endl;
std::shared_ptr<State> state(m_state.lock());
std::vector<std::optional<YulString>> args;
for (Expression const& expr: _funCall.arguments)
{
if (holds_alternative<Literal>(expr))
{
std::optional<YulString> empty;
args.push_back(empty);
}
else if (holds_alternative<Identifier>(expr))
{
Identifier const& ident = std::get<Identifier>(expr);
args.push_back(ident.name);
}
else
{
assertThrow(false, OptimizerException, "unexpected function argument");
}
}
CallSite cs;
cs.callee = _funCall.functionName.name;
cs.arguments = args;
cs.returns = state->currentAssignment();
m_unresolved_calls.push_back(cs);
}
void FunctionScope::visitKeccak256(FunctionCall const& _funCall)
{
std::shared_ptr<State> state(m_state.lock());
std::vector<YulString> const& returns = state->currentAssignment();
Expression const& addr_expr = _funCall.arguments.at(0);
assertThrow(holds_alternative<Identifier>(addr_expr), OptimizerException, "non-ident");
Identifier const& addr_ident = std::get<Identifier>(addr_expr);
Variable const& addr_var = state->variable(addr_ident.name);
Expression const& len_expr = _funCall.arguments.at(1);
Identifier const& len_ident = std::get<Identifier>(len_expr);
Variable const& len_var = state->variable(len_ident.name);
std::set<YulString> mlocs = state->memory().read(addr_var, len_var);
for (auto const& mloc: mlocs)
{
state->addVariable(mloc, true);
influences(mloc, returns.at(0));
}
// TODO: Copy constness
}
void FunctionScope::visitMStore(FunctionCall const& _funCall)
{
std::shared_ptr<State> state(m_state.lock());
Expression const& addr_expr = _funCall.arguments.at(0);
assertThrow(holds_alternative<Identifier>(addr_expr), OptimizerException, "non-ident");
Identifier const& addr_ident = std::get<Identifier>(addr_expr);
Variable const& addr_var = state->variable(addr_ident.name);
Expression const& val_expr = _funCall.arguments.at(1);
YulString mloc = state->memory().write(addr_var, 32);
state->addVariable(mloc, true);
influences(val_expr, mloc);
std::cout << "mstore: " << mloc.str() << std::endl;
// TODO: Copy constness from val_expr to mloc
}
void FunctionScope::visitMLoad(FunctionCall const& _funCall)
{
std::shared_ptr<State> state(m_state.lock());
std::vector<YulString> const& returns = state->currentAssignment();
Expression const& addr_expr = _funCall.arguments.at(0);
assertThrow(holds_alternative<Identifier>(addr_expr), OptimizerException, "non-ident");
Identifier const& addr_ident = std::get<Identifier>(addr_expr);
Variable const& addr_var = state->variable(addr_ident.name);
std::set<YulString> mlocs = state->memory().read(addr_var, 32);
for (auto const& mloc: mlocs)
{
state->addVariable(mloc, true);
influences(mloc, returns.at(0));
}
}
void FunctionScope::visitBuiltin(FunctionCall const& _funCall, BuiltinFunctionForEVM const* _builtin)
{
if (!_builtin->instruction)
{
std::cout << "Builtin with no instruction: " << _funCall.functionName.name.str() << std::endl;
return;
}
std::shared_ptr<State> state(m_state.lock());
std::vector<YulString> const& returns = state->currentAssignment();
switch (*_builtin->instruction)
{
case evmasm::Instruction::STOP:
break;
case evmasm::Instruction::ADD:
case evmasm::Instruction::MUL:
case evmasm::Instruction::SUB:
case evmasm::Instruction::DIV:
case evmasm::Instruction::SDIV:
case evmasm::Instruction::MOD:
case evmasm::Instruction::SMOD:
case evmasm::Instruction::EXP:
case evmasm::Instruction::SIGNEXTEND:
case evmasm::Instruction::LT:
case evmasm::Instruction::GT:
case evmasm::Instruction::SLT:
case evmasm::Instruction::SGT:
case evmasm::Instruction::EQ:
case evmasm::Instruction::AND:
case evmasm::Instruction::OR: