-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrbr.py
1360 lines (1165 loc) · 48.3 KB
/
rbr.py
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
#Pablo Gordillo
from rbr_rule import RBRRule
import opcodes
from utils import getKey, orderRBR
import os
import saco
from timeit import default_timer as dtimer
'''
It initialize the globals variables.
-List opcodeX contains the evm bytecodes from set X.
-current_local_var has the max index of the local variables created.
-local_variables is a mapping address->local_variable if known.
-rbr_blocks is a mapping rbr_id->list of rbr rules (jumps contains 2 rules per rbr_id).
-stack_index is a mapping block_id->[stack_height_begin, stack_heigh_end].
-max_field_list keeps the id of the known fields accessed during the execution.
-bc_in_use keeps the contract data used during the execution.
-new fid keeps the index of the new fresh variable.
'''
def init_globals():
global opcodes0
opcodes0 = ["STOP", "ADD", "MUL", "SUB", "DIV", "SDIV", "MOD",
"SMOD", "ADDMOD", "MULMOD", "EXP", "SIGNEXTEND"]
global opcodes10
opcodes10 = ["LT", "GT", "SLT", "SGT", "EQ", "ISZERO", "AND", "OR",
"XOR", "NOT", "BYTE"]
global opcodes20
opcodes20 = ["SHA3"]
global opcodes30
opcodes30 = ["ADDRESS", "BALANCE", "ORIGIN", "CALLER", "CALLVALUE",
"CALLDATALOAD", "CALLDATASIZE", "CALLDATACOPY", "CODESIZE",
"CODECOPY", "GASPRICE", "EXTCODESIZE", "EXTCODECOPY", "MCOPY"]
global opcodes40
opcodes40 = ["BLOCKHASH", "COINBASE", "TIMESTAMP", "NUMBER",
"DIFFICULTY", "GASLIMIT"]
global opcodes50
opcodes50 = ["POP", "MLOAD", "MSTORE", "MSTORE8", "SLOAD",
"SSTORE", "JUMP", "JUMPI", "PC", "MSIZE", "GAS", "JUMPDEST",
"SLOADEXT", "SSTOREEXT", "SLOADBYTESEXT", "SSTOREBYTESEXT"]
global opcodes60
opcodes60 = ["PUSH"]
global opcodes80
opcodes80 = ["DUP"]
global opcodes90
opcodes90 = ["SWAP"]
global opcodesA
opcodesA = ["LOG0", "LOG1", "LOG2", "LOG3", "LOG4"]
global opcodesF
opcodesF = ["CREATE", "CALL", "CALLCODE", "RETURN", "REVERT",
"ASSERTFAIL", "DELEGATECALL", "BREAKPOINT", "RNGSEED", "SSIZEEXT",
"SLOADBYTES", "SSTOREBYTES", "SSIZE", "STATEROOT", "TXEXECGAS",
"CALLSTATIC", "INVALID", "SUICIDE"]
global opcodesZ
opcodesZ = ["RETURNDATACOPY","RETURNDATASIZE"]
global current_local_var
current_local_var = 0
global local_variables
local_variables = {}
global lvariables_per_block
lvariables_per_block = {}
global rbr_blocks
rbr_blocks = {}
global stack_index
stack_index = {}
# global max_field_list
# max_field_list = []
# global bc_in_use
# bc_in_use = []
global bc_per_block
bc_per_block = {}
global top_index
top_index = 0
global new_fid
new_fid = 0
global fields_per_block
fields_per_block = {}
'''
Given a block it returns a list containingn the height of its
stack when arriving and leaving the block.
-bock:block start address. int.
-It returns a list with 2 elements. [int, int].
'''
def get_stack_index(block):
try:
return stack_index[block]
except:
return [0,0]
def update_top_index(val):
global top_index
if top_index < val:
top_index = val
'''
It is used when a bytecode consume stack variables. It returns the
current stack variable (the top most one) and after that update the variable index.
index_variables contains the index of current stack variable.
-index_variables: int.
-It returns a tuple (stack variable, top stack index). (string, int).
'''
def get_consume_variable(index_variables):
current = index_variables
if current >= 0 :
variable = "s(" + str(current) + ")"
current = current-1
return variable, current
'''
It returns the next fresh stack variable and updates the current
index.
-index_variables: int.
-It returns a tuple (stack variable, top stack index). (string, int).
'''
def get_new_variable(index_variables):
new_current = index_variables + 1
update_top_index(new_current)
return "s(" + str(new_current) + ")", new_current
'''
It returns the variable palced in the top of stack.
-index_variables: int.
variable: stack variable returned. string.
'''
def get_current_variable(index_variables):
current = index_variables
if current >= 0 :
variable = "s(" + str(current) + ")"
return variable
'''
It returns a list that contains all the stack variables which are "active".
It goes from current to 0.
s_vars: [string].
'''
def get_stack_variables(index_variables):
current = index_variables
s_vars = []
for i in range(current,-1,-1):
s_vars.append("s("+str(i)+")")
return s_vars
'''
It returns the posth variable.
index_variables: top stack index. int.
pos: position of the variable required. int.
variable: stack variable returned. string.
'''
def get_ith_variable(index_variables, pos):
current = index_variables
if (current >= pos):
idx = current-pos
variable = "s(" + str(idx) + ")"
return variable
'''
It returns the local variable bound to argument address. If it
does not exist, the method creates and store it in the dictionary
local_variables.
-address: memory address. string.
-var: new local variable. string.
'''
def get_local_variable(address):
global current_local_var
global local_variables
try:
idx = local_variables[int(address)]
#var = "l(" + str(idx) + ")"
return idx
except KeyError:
local_variables[int(address)] = current_local_var
#var = "l(" + str(current_local_var) + ")"
current_local_var += 1
return current_local_var-1
'''
It adds to the list max_field_list the index of the field used.
-value: index_field. int.
'''
def update_field_index(value,block):
# global max_field_list
global fields_per_block
if block not in fields_per_block:
fields_per_block[block]=[value]
elif value not in fields_per_block[block]:
fields_per_block[block].append(value)
# if value not in max_field_list:
# max_field_list.append(value)
'''
It adds to the list bc_in_use the name of the contract variable used.
-value: contract variable name. string.
'''
def update_bc_in_use(value,block):
# global bc_in_use
global bc_per_block
if block not in bc_per_block:
bc_per_block[block]=[value]
elif value not in bc_per_block[block]:
bc_per_block[block].append(value)
# if value not in bc_in_use:
# bc_in_use.append(value)
def update_local_variables(value,block):
global lvariables_per_block
if block not in lvariables_per_block:
lvariables_per_block[block]=[value]
elif value not in lvariables_per_block[block]:
lvariables_per_block[block].append(value)
def process_tops(top1,top2):
top1_aux = 0 if top1 == float("inf") else top1
top2_aux = 0 if top2 == float("inf") else top2
return top1_aux, top2_aux
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodes0(opcode,index_variables):
if opcode == "ADD":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "+" + v2
elif opcode == "MUL":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "*" + v2
elif opcode == "SUB":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "-" + v2
elif opcode == "DIV":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "/" + v2
elif opcode == "SDIV":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "/" + v2
elif opcode == "MOD":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "%" + v2
elif opcode == "SMOD":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "%" + v2
elif opcode == "ADDMOD":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_consume_variable(updated_variables)
v4, updated_variables = get_new_variable(updated_variables)
instr = v4+" = (" + v1 + "+" + v2 + ") % " + v3
elif opcode == "MULMOD":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_consume_variable(updated_variables)
v4, updated_variables = get_new_variable(updated_variables)
instr = v4+" = (" + v1 + "*" + v2 + ") % " + v3
elif opcode == "EXP":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = " + v1 + "^" + v2
# elif opcode == "SIGNEXTEND":
# pass
elif opcode == "STOP":
instr = "skip"
updated_variables = index_variables
else:
instr = "Error opcodes0: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodes10(opcode, index_variables,cond):
if opcode == "LT":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3 , updated_variables = get_new_variable(updated_variables)
if cond :
instr = v3+ " = lt(" + v1 + ", "+v2+")"
else :
instr = "lt(" + v1 + ", "+v2+")"
elif opcode == "GT":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3 , updated_variables = get_new_variable(updated_variables)
if cond :
instr = v3+ " = gt(" + v1 + ", "+v2+")"
else :
instr = "gt(" + v1 + ", "+v2+")"
elif opcode == "SLT":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3 , updated_variables = get_new_variable(updated_variables)
if cond :
instr = v3+ " = lt(" + v1 + ", "+v2+")"
else :
instr = "lt(" + v1 + ", "+v2+")"
elif opcode == "SGT":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3 , updated_variables = get_new_variable(updated_variables)
if cond :
instr = v3+ " = gt(" + v1 + ", "+v2+")"
else :
instr = "gt(" + v1 + ", "+v2+")"
elif opcode == "EQ":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3 , updated_variables = get_new_variable(updated_variables)
if cond:
instr = v3+ "= eq(" + v1 + ", "+v2+")"
else:
instr = "eq(" + v1 + ", "+v2+")"
elif opcode == "ISZERO":
v1, updated_variables = get_consume_variable(index_variables)
v2 , updated_variables = get_new_variable(updated_variables)
if cond:
instr = v2+ "= eq(" + v1 + ", 0)"
else:
instr = "eq(" + v1 + ", 0)"
elif opcode == "AND":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = and(" + v1 + ", " + v2+")"
elif opcode == "OR":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = or(" + v1 + ", " + v2+")"
elif opcode == "XOR":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = xor(" + v1 + ", " + v2+")"
elif opcode == "NOT":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_new_variable(updated_variables)
instr = v2+" = not(" + v1 + ")"
elif opcode == "BYTE":
v0, updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_consume_variable(updated_variables)
v2, updated_variables = get_new_variable(updated_variables)
instr = v2+" = byte(" + v1 + " , " + v0 + ")"
else:
instr = "Error opcodes10: "+ opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodes20(opcode, index_variables):
if opcode == "SHA3":
v1, updated_variables = get_consume_variable(index_variables)
v2, updated_variables = get_consume_variable(updated_variables)
v3, updated_variables = get_new_variable(updated_variables)
instr = v3+" = sha3("+ v1+", "+v2+")"
else:
instr = "Error opcodes20: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodes30(opcode, value, index_variables,block):
if opcode == "ADDRESS":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = address"
update_bc_in_use("address",block)
elif opcode == "BALANCE":
_, updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_new_variable(updated_variables)
instr = v1+" = balance"
update_bc_in_use("balance",block)
elif opcode == "ORIGIN":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = origin"
update_bc_in_use("origin",block)
elif opcode == "CALLER":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = caller"
update_bc_in_use("caller",block)
elif opcode == "CALLVALUE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = callvalue"
update_bc_in_use("callvalue",block)
elif opcode == "CALLDATALOAD":
v0, updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_new_variable(updated_variables)
val = str(value).split("_")
if val[0] == "Id":
instr = v1+" = calldataload"
update_bc_in_use("calldataload",block)
else:
instr = v1+" = "+str(value).strip("_")
update_bc_in_use(str(value).strip("_"),block)
elif opcode == "CALLDATASIZE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = calldatasize"
update_bc_in_use("calldatasize",block)
elif opcode == "CALLDATACOPY":
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
instr = ""
elif opcode == "CODESIZE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = codesize"
update_bc_in_use("codesize",block)
elif opcode == "CODECOPY":
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
instr = ""
elif opcode == "GASPRICE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = gasprice"
update_bc_in_use("gasprice",block)
elif opcode == "EXTCODESIZE":
_, updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_new_variable(updated_variables)
instr = v1+" = extcodesize"
update_bc_in_use("extcodesize",block)
elif opcode == "EXTCODECOPY":
pass
elif opcode == "MCOPY":
pass
else:
instr = "Error opcodes30: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodes40(opcode, index_variables,block):
if opcode == "BLOCKHASH":
v0, updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_new_variable(updated_variables)
instr = v1+" = blockhash("+v0+")"
update_bc_in_use("blockhash",block)
elif opcode == "COINBASE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = coinbase"
update_bc_in_use("coinbase",block)
elif opcode == "TIMESTAMP":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = timestamp"
update_bc_in_use("timestamp",block)
elif opcode == "NUMBER":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = number"
update_bc_in_use("number",block)
elif opcode == "DIFFICULTY":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = difficulty"
update_bc_in_use("difficulty",block)
elif opcode == "GASLIMIT":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = gaslimit"
update_bc_in_use("gaslimit",block)
else:
instr = "Error opcodes40: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodes50(opcode, value, index_variables,block):
global new_fid
if opcode == "POP":
v1, updated_variables = get_consume_variable(index_variables)
instr=""
elif opcode == "MLOAD":
_ , updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_new_variable(updated_variables)
try:
l_idx = get_local_variable(value)
instr = v1+ " = " + "l("+str(l_idx)+")"
update_local_variables(l_idx,block)
except ValueError:
instr = ["ll = " + v1, v1 + " = fresh("+str(new_fid)+")"]
new_fid+=1
elif opcode == "MSTORE":
v0 , updated_variables = get_consume_variable(index_variables)
v1 , updated_variables = get_consume_variable(updated_variables)
try:
l_idx = get_local_variable(value)
instr = "l("+str(l_idx)+") = "+ v1
update_local_variables(l_idx,block)
except ValueError:
instr = ["ls(1) = "+ v1, "ls(2) = "+v0]
elif opcode == "MSTORE8":
v0 , updated_variables = get_consume_variable(index_variables)
v1 , updated_variables = get_consume_variable(updated_variables)
try:
l_idx = get_local_variable(value)
instr = "l("+str(l_idx)+") = "+ v1
except ValueError:
instr = ["ls(1) = "+ v1, "ls(2) = "+v0]
elif opcode == "SLOAD":
_ , updated_variables = get_consume_variable(index_variables)
v1, updated_variables = get_new_variable(updated_variables)
try:
idx = int(value)
instr = v1+" = " + "g(" + value + ")"
update_field_index(value,block)
except ValueError:
instr = ["gl = " + v1, v1 + " = fresh("+str(new_fid)+")"]
new_fid+=1
elif opcode == "SSTORE":
v0 , updated_variables = get_consume_variable(index_variables)
v1 , updated_variables = get_consume_variable(updated_variables)
try:
idx = int(value)
instr = "g(" + value + ") = " + v1
update_field_index(value,block)
except ValueError:
instr = ["gs(1) = "+ v1, "gs(2) = "+v0]
# elif opcode == "JUMP":
# pass
# elif opcode == "JUMPI":
# pass
# elif opcode == "PC":
# pass
elif opcode == "MSIZE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1 + " = msize"
update_bc_in_use("msize",block)
elif opcode == "GAS":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = "+"gas"
update_bc_in_use("gas",block)
elif opcode == "JUMPDEST":
instr = ""
updated_variables = index_variables
# elif opcode == "SLOADEXT":
# pass
# elif opcode == "SSTOREEXT":
# pass
# elif opcode == "SLOADBYTESEXT":
# pass
# elif opcode == "SSTOREBYTESEXT":
# pass
else:
instr = "Error opcodes50: "+ opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
They corresponds to LOGS opcodes.
'''
def translateOpcodesA(opcode, index_variables):
instr = ""
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
'''
def translateOpcodesF(opcode, index_variables, addr):
if opcode == "CREATE":
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
v1, updated_variables = get_new_variable(updated_variables)
instr=""
elif opcode == "CALL": #Suppose that all the calls are executed without errors
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
v1, updated_variables = get_new_variable(updated_variables)
instr = v1 +" = 1"
elif opcode == "CALLCODE":
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
_, updated_variables = get_consume_variable(updated_variables)
v1, updated_variables = get_new_variable(updated_variables)
instr = v1 +" = 1"
elif opcode == "RETURN":
# var = get_local_variable(addr)
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
# instr = "r "+var
instr = ""
elif opcode == "REVERT":
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
instr = ""
elif opcode == "ASSERTFAIL":
instr = ""
updated_variables = index_variables
# elif opcode == "DELEGATECALL":
# pass
# elif opcode == "BREAKPOINT":
# pass
# elif opcode == "RNGSEED":
# pass
# elif opcode == "SSIZEEXT":
# pass
# elif opcode == "SLOADBYTES":
# pass
# elif opcode == "SSTOREBYTES":
# pass
# elif opcode == "SSIZE":
# pass
# elif opcode == "STATEROOT":
# pass
# elif opcode == "TXEXECGAS":
# pass
# elif opcode == "CALLSTATIC":
# pass
# elif opcode == "INVALID":
# pass
elif opcode == "SUICIDE":
instr = ""
updated_variables = index_variables
else:
instr = "Error opcodesF: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
-value is astring that contains the number pushed to the stack.
'''
def translateOpcodes60(opcode, value, index_variables):
if opcode == "PUSH":
v1,updated_variables = get_new_variable(index_variables)
dec_value = int(value, 16) #convert hex to dec
instr = v1+" = " + str(dec_value)
else:
instr = "Error opcodes60: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of dup bytecode.
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
It duplicates what is stored in the stack at pos value (when
value == 1, it duplicates the top of the stack) .
-value refers to the position to be duplicated. string.
'''
def translateOpcodes80(opcode, value, index_variables):
if opcode == "DUP":
v1 = get_ith_variable(index_variables,int(value)-1)
v2, updated_variables= get_new_variable(index_variables)
instr = v2+" = "+v1
else:
instr = "Error opcodes80: "+opcode
updated_variables = index_variables
return instr, updated_variables
'''
It simulates the execution of swap bytecode.
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
-value refers to the position involved in the swap. string.
'''
def translateOpcodes90(opcode, value, index_variables):
if opcode == "SWAP":
v1 = get_ith_variable(index_variables,int(value))
v2 = get_current_variable(index_variables)
v3,_ = get_new_variable(index_variables)
instr1 = v3 + " = " + v1
instr2 = v1 + " = " + v2
instr3 = v2 + " = " + v3
instr = [instr1,instr2,instr3]
else:
instr = "Error opcodes90: "+opcode
return instr, index_variables
'''
It simulates the execution of evm bytecodes. It consumes or
generates variables depending on the bytecode and returns the
corresponding translated instruction and the variables's index
updated. It also updated the corresponding global variables.
Unclassified opcodes.
'''
def translateOpcodesZ(opcode, index_variables,block):
if opcode == "RETURNDATASIZE":
v1, updated_variables = get_new_variable(index_variables)
instr = v1+" = returndatasize"
update_bc_in_use("returndatasize",block)
elif opcode == "RETURNDATACOPY":
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(index_variables)
_, updated_variables = get_consume_variable(updated_variables)
instr = ""
else:
instr = "Error opcodesZ: "+opcode
return instr, updated_variables
'''
It checks if the list instr contains the element to generated a
guard,i.e., just conditional statements, push and ended with a jump
intruction.
-instr is a list with instructions.
-It returns a boolean.
'''
def is_conditional(instr):
valid = True
i = 1
if instr[0] in ["LT","GT","EQ","ISZERO"] and instr[-1] in ["JUMP","JUMPI"]:
while(i<len(instr)-2 and valid):
ins = instr[i].split()
if(ins[0] not in ["ISZERO","PUSH"]):
valid = False
i+=1
else:
valid = False
return valid
'''
It returns the opposite guard of the one given as parameter.
-guard is the guard to be "reversed". string.
-opposit = not(guard). string.
'''
def get_opposite_guard(guard):
if guard[:2] == "lt":
opposite = "geq"+guard[2:]
elif guard[:3] == "leq":
opposite = "gt"+guard[3:]
elif guard[:2] == "gt":
opposite = "leq"+guard[2:]
elif guard[:3] == "geq":
opposite = "lt"+guard[3:]
# elif guard == "SLT":
# pass
# elif guard == "SGT":
# pass
elif guard[:2] == "eq":
opposite = "neq"+guard[2:]
elif guard[:3] == "neq":
opposite = "eq"+guard[3:]
# elif guard[:6] == "isZero":
# opposite = "notZero"+guard[6:]
# elif guard[:7] == "notZero":
# opposite = "isZero"+guard[7:]
else:
opposite = None
return opposite
'''
It translates the bytecode corresponding to evm_opcode.
We mantain some empty instructions to insert the evm bytecodes.
They are remove when displaying.
-rule refers to the rule that is being built. rbr_rule instance.
-evm_opcode is the bytecode to be translated. string.
-list_jumps contains the addresses of next blocks.
-cond is True if the conditional statemnt refers to a guard. False otherwise.
-nop is True when generating nop annotations with the opcode. False otherwise.
-index_variables refers to the top stack index. int.
'''
def compile_instr(rule,evm_opcode,variables,list_jumps,cond,nop):
opcode = evm_opcode.split(" ")
opcode_name = opcode[0]
opcode_rest = ""
if len(opcode) > 1:
opcode_rest = opcode[1]
if opcode_name in opcodes0:
value, index_variables = translateOpcodes0(opcode_name, variables)
rule.add_instr(value)
elif opcode_name in opcodes10:
value, index_variables = translateOpcodes10(opcode_name, variables,cond)
rule.add_instr(value)
elif opcode_name in opcodes20:
value, index_variables = translateOpcodes20(opcode_name, variables)
rule.add_instr(value)
elif opcode_name in opcodes30:
value, index_variables = translateOpcodes30(opcode_name,opcode_rest,variables,rule.get_Id())
rule.add_instr(value)
elif opcode_name in opcodes40:
value, index_variables = translateOpcodes40(opcode_name,variables,rule.get_Id())
rule.add_instr(value)
elif opcode_name in opcodes50:
value, index_variables = translateOpcodes50(opcode_name, opcode_rest, variables,rule.get_Id())
if type(value) is list:
for ins in value:
rule.add_instr(ins)
else:
rule.add_instr(value)
elif opcode_name[:4] in opcodes60:
value, index_variables = translateOpcodes60(opcode_name[:4], opcode_rest, variables)
rule.add_instr(value)
elif opcode_name[:3] in opcodes80:
value, index_variables = translateOpcodes80(opcode_name[:3], opcode_name[3:], variables)
rule.add_instr(value)
elif opcode_name[:4] in opcodes90:
value, index_variables = translateOpcodes90(opcode_name[:4], opcode_name[4:], variables)
for ins in value: #SWAP returns a list (it is translated into 3 instructions)
rule.add_instr(ins)
elif opcode_name in opcodesA:
value, index_variables = translateOpcodesA(opcode_name, variables)
rule.add_instr(value)
elif opcode_name in opcodesF:
value, index_variables = translateOpcodesF(opcode_name,variables,opcode_rest)
#RETURN
rule.add_instr(value)
elif opcode_name in opcodesZ:
value, index_variables = translateOpcodesZ(opcode_name,variables,rule.get_Id())
rule.add_instr(value)
else:
value = "Error. No opcode matchs"
index_variables = variables
rule.add_instr(value)
if nop :
rule.add_instr("nop("+opcode_name+")")
return index_variables
'''
It creates the call to next block when the type of the current one is falls_to.
-index_variables refers to the top stack index. int.
-falls_to contains the address of the next block. int.
-instr contains the call instruction generated. string.
'''
def process_falls_to_blocks(index_variables, falls_to):
top = get_stack_index(falls_to)[0]
stack_variables = get_stack_variables(index_variables)[:top]
p_vars = ",".join(stack_variables)
instr = "call(block"+str(falls_to)+"("+p_vars+",globals, bc))"
return instr
'''
It translates the jump instruction.
If the len(jumps)==1, it corresponds to a uncondtional jump.
Otherwise we have to convert it into a conditional jump.
-block_id refers to the id of the current block. int.
-variables refers to the top stack index. int.
-jumps is a list with the addresses of the next blocks.
-It returns a tuple (rule1, rule2, instr) where rule1 and rule2
are rule_rbr instances corresponding to the guarded jump rules
(if it is the case), and instr is the called instruction to the
jump rule generated. If it is a jump, rule1 = rule2 = None.
'''
def create_uncond_jump(block_id,variables,jumps):
if (len(jumps)>1):
rule1, rule2 = create_uncond_jumpBlock(block_id,variables,jumps)
stack_variables = get_stack_variables(variables)
head = "jump"+str(block_id)
in_vars = len(stack_variables)
rule1.set_index_input(in_vars)
rule2.set_index_input(in_vars)
else:
_ , updated_variables = get_consume_variable(variables)
stack_variables = get_stack_variables(updated_variables)
top = get_stack_index(jumps[0])[0]
stack_variables = stack_variables[:top]
head = "block"+str(jumps[0])
rule1 = rule2 = None
if (len(stack_variables)!=0):