-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathemulator.py
1914 lines (1609 loc) · 54.8 KB
/
emulator.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
The module provides simulation of import function.
"""
import bin_format
import utils
from global_variables import global_vars
import logger
import number
import z3
from random import randint
from runtime import Value, GlobalInstance
from bug_analyzer import check_block_dependence_dynamic
from bug_analyzer import check_reentrancy_bug
def get_generator():
"""A generator for symbolic variable generating.
Returns:
Generator: A generator.
"""
cur = 0
while True:
yield cur
cur += 1
class EosEmulator:
"""A emulator of library functions of EOS.IO blockchain platform.
"""
counter = get_generator()
def __init__(self):
pass
# Assert function
@classmethod
def assert_recover_key(cls, ctx, args):
# [TODO]
pass
@classmethod
def assert_sha256(cls, ctx, args):
# [TODO]
pass
@classmethod
def assert_sha1(cls, ctx, args):
# [TODO]
pass
@classmethod
def assert_sha512(cls, ctx, args):
# [TODO]
pass
@classmethod
def assert_ripemd160(cls, ctx, args):
# [TODO]
pass
@classmethod
def eosio_assert(cls, ctx, args):
expression = args[0]
ctx.solver.add(expression == 1)
@classmethod
def eosio_assert_message(cls, ctx, args):
cls.eosio_assert(cls, ctx, args)
@classmethod
def eosio_assert_code(cls, ctx, args):
cls.eosio_assert(cls, ctx, args)
@classmethod
def eosio_exit(cls, ctx, args):
ctx.solver.add(0 == 1) # add a false expression to pruning
@classmethod
def abort(cls, ctx, args):
cls.eosio_assert(cls, ctx, [0, 'abort()'])
# No effect function
@classmethod
def checktime(cls, ctx, args):
pass
@classmethod
def prints(cls, ctx, args):
pass
@classmethod
def prints_l(cls, ctx, args):
pass
@classmethod
def printi(cls, ctx, args):
pass
@classmethod
def printui(cls, ctx, args):
pass
@classmethod
def printi128(cls, ctx, args):
pass
@classmethod
def printui128(cls, ctx, args):
pass
@classmethod
def printsf(cls, ctx, args):
pass
@classmethod
def printdf(cls, ctx, args):
pass
@classmethod
def printqf(cls, ctx, args):
pass
@classmethod
def printn(cls, ctx, args):
pass
@classmethod
def printhex(cls, ctx, args):
pass
@classmethod
def set_resource_limits(cls, ctx, args):
pass
@classmethod
def set_blockchain_parameters_packed(cls, ctx, args):
pass
@classmethod
def set_privileged(cls, ctx, args):
pass
@classmethod
def preactivate_feature(cls, ctx, args):
pass
@classmethod
def is_feature_active(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'is_feature_active_{next(cls.counter)}')
@classmethod
def is_privileged(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'is_privileged_{next(cls.counter)}')
@classmethod
def transaction_size(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'transaction_size_{next(cls.counter)}')
@classmethod
def expiration(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'expiration_{next(cls.counter)}')
@classmethod
def tapos_block_num(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'tapos_block_num_{next(cls.counter)}')
@classmethod
def tapos_block_prefix(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'tapos_block_prefix_{next(cls.counter)}')
@classmethod
def get_permission_last_used(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'get_permission_last_used_{next(cls.counter)}')
@classmethod
def get_account_creation_time(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'get_account_creation_time_{next(cls.counter)}')
@classmethod
def current_time(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'current_time_{next(cls.counter)}')
@classmethod
def publication_time(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'publication_time_{next(cls.counter)}')
@classmethod
def is_feature_activated(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'is_feature_activated_{next(cls.counter)}')
@classmethod
def get_sender(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'get_sender_{next(cls.counter)}')
@classmethod
def action_data_size(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'action_data_size_{next(cls.counter)}')
@classmethod
def current_receiver(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'current_receiver_{next(cls.counter)}')
@classmethod
def has_auth(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'has_auth_{next(cls.counter)}')
@classmethod
def is_account(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'is_account_{next(cls.counter)}')
@classmethod
def cancel_deferred(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'cancel_deferred_{next(cls.counter)}')
@classmethod
def memcmp(cls, ctx, args):
# [TODO] side effect
return utils.gen_symbolic_value(bin_format.i32, f'memcmp_{next(cls.counter)}')
@classmethod
def set_proposed_producers(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'set_proposed_producers_{next(cls.counter)}')
@classmethod
def set_proposed_producers_ex(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'set_proposed_producers_ex_{next(cls.counter)}')
@classmethod
def get_blockchain_parameters_packed(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'get_blockchain_parameters_packed_{next(cls.counter)}')
@classmethod
def get_active_producers(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i64, f'get_active_producers_{next(cls.counter)}')
# Computation functions
@classmethod
def ashlti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def ashrti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def lshlti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def lshrti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def divti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def udivti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def modti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def umodti3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def multi3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def addtf3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def subtf3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def multf3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def divtf3(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def eqtf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def netf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def getf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def gttf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def lttf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def letf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def cmptf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def unordtf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def negtf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floatsitf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floatunsitf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floatditf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floatunditf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floattidf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floatuntidf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def floatsidf(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def extendsftf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def extenddftf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixtfti(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixtfdi(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixtfsi(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixunstfti(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixunstfdi(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixunstfsi(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixsfti(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixdfti(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixunssfti(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def fixunsdfti(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def trunctfdf2(cls, ctx, args):
# if utils.is_all_real(args):
# return int(bin(args[0]) + bin(args[1])[2:], base=2)
# args = [utils.to_symbolic(arg, 64) for arg in args]
# z3.Concat(*args)
pass
@classmethod
def trunctfsf2(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def sha1(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def sha256(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def sha512(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def ripemd160(cls, ctx, args):
# [TODO] Implement
pass
# Data writing functions
@classmethod
def get_resource_limits(cls, ctx, args):
# [TODO]
pass
@classmethod
def read_action_data(cls, ctx, args):
# [TODO]
pass
@classmethod
def read_transaction(cls, ctx, args):
# [TODO]
pass
@classmethod
def get_action(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def get_context_free_data(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def memcpy(cls, ctx, args):
# [TODO] Implement
pass
@classmethod
def memmove(cls, ctx, args):
# [TODO]
pass
@classmethod
def memset(cls, ctx, args):
# [TODO]
pass
# Permission function
@classmethod
def check_transaction_authorization(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'check_transaction_authorization_{next(cls.counter)}')
@classmethod
def check_permission_authorization(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'check_permission_authorization_{next(cls.counter)}')
@classmethod
def require_auth(cls, ctx, args):
pass
@classmethod
def require_auth2(cls, ctx, args):
pass
# Other
@classmethod
def activate_feature(cls, ctx, args):
cls.eosio_assert(cls, ctx, [0])
@classmethod
def db_store_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_store_i64_{next(cls.counter)}')
@classmethod
def db_update_i64(cls, ctx, args):
pass
@classmethod
def db_remove_i64(cls, ctx, args):
pass
@classmethod
def db_get_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_get_i64_{next(cls.counter)}')
@classmethod
def db_next_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_next_i64_{next(cls.counter)}')
@classmethod
def db_previous_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_previous_i64_{next(cls.counter)}')
@classmethod
def db_find_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_find_i64_{next(cls.counter)}')
@classmethod
def db_lowerbound_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_lowerbound_i64_{next(cls.counter)}')
@classmethod
def db_upperbound_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_upperbound_i64_{next(cls.counter)}')
@classmethod
def db_end_i64(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'db_end_i64_{next(cls.counter)}')
@classmethod
def recover_key(cls, ctx, args):
return utils.gen_symbolic_value(bin_format.i32, f'recover_key_{next(cls.counter)}')
@classmethod
def require_recipient(cls, ctx, args):
pass
@classmethod
def send_inline(cls, ctx, args):
pass
@classmethod
def send_context_free_inline(cls, ctx, args):
pass
@classmethod
def send_deferred(cls, ctx, args):
pass
class EthereumEmulator:
"""A emulator of library functions of Ethereum Wasm blockchain platform.
"""
counter = get_generator()
def __init__(self):
pass
@classmethod
def useGas(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def getAddress(cls, args, solver, memory):
return []
@classmethod
def getExternalBalance(cls, args, solver, memory):
# 0.7.5 solc has bug in this function
# address = args[0].n
address = 32
result = args[1].n
for i in range(4):
v = utils.gen_symbolic_value(bin_format.i64, f'getExternalBalance_{global_vars.num_getExternalBalance}')
memory.data[address:address + 8] = number.MemoryStore.pack_i64(v)
global_vars.add_num_getExternalBalance()
address += 8
return []
@classmethod
def getBlockHash(cls, args, solver, memory):
pass
@classmethod
def call(cls, args, solver, memory):
r = global_vars.add_flag_num_host()
r = utils.gen_symbolic_value(bin_format.i32, f'call_{r}')
global_vars.call_symbolic_ret[f'{r}'] = global_vars.cur_sum_pc
logger.infoln(f'save eth.call and cur_sum_pc{global_vars.call_symbolic_ret}')
logger.infoln(f'solver{solver}')
check_block_dependence_dynamic(solver)
check_reentrancy_bug(memory, solver)
return r
@classmethod
def callDataCopy(cls, args, solver, memory):
print(args[0])
address = args[0].n
# [TODO] Really simulate callDataCopy, such as setting several symbol values for callDataCopy,
# and setting the offset according to the offset required by the contract, etc.
for i in range(4):
v = utils.gen_symbolic_value(bin_format.i64, f'callDataCopy_{global_vars.num_callDataCopy}')
memory.data[address:address + 8] = number.MemoryStore.pack_i64(v)
global_vars.add_num_callDataCopy()
address += 8
return []
@classmethod
def getCallDataSize(cls, args, solver, memory):
global_vars.add_flag_getCallDataSize()
if len(solver.units()) > 1:
r = randint(68, 999)
return r
else:
r = utils.gen_symbolic_value(bin_format.i32, f'getCallDataSize_{global_vars.flag_getCallDataSize}')
return r
@classmethod
def callCode(cls, args, solver, memory):
pass
@classmethod
def callDelegate(cls, args, solver, memory):
r = global_vars.add_flag_num_host()
r = utils.gen_symbolic_value(bin_format.i32, f'callDelegate_{r}')
a = args[2].n # 数据内存地址
b = args[1].n # 发送地址内存地址
l = args[3].n
for i in range(4):
if utils.is_symbolic(memory.data[a]):
tmp_address = z3.simplify(number.MemoryLoad.i64(memory.data[a:a + 8]))
else:
tmp_address = number.MemoryLoad.i64(memory.data[a:a + 8])
if utils.is_symbolic(memory.data[b]):
tmp_address1 = z3.simplify(number.MemoryLoad.i64(memory.data[b:b + 8]))
else:
tmp_address1 = number.MemoryLoad.i64(memory.data[b:b + 8])
if utils.is_symbolic(tmp_address):
# [TODO] update to bug_analyzer
print('find sym')
global_vars.find_ethereum_delegate_call()
pass
for item in global_vars.dict_symbolic_address:
tmp = global_vars.dict_symbolic_address[item]
if isinstance(tmp, int):
print(a, global_vars.dict_symbolic_address[item])
l = max(l, 64)
if a <= tmp <= a + l:
print(a, item, 'find sym')
global_vars.find_ethereum_delegate_call()
return r
@classmethod
def callStatic(cls, args, solver, memory):
pass
@classmethod
def storageStore(cls, args, solver, memory):
a = 0
list_value = list()
for i in range(4):
if i == 0:
if utils.is_symbolic(memory.data[a]):
tmp_address = z3.simplify(number.MemoryLoad.i64(memory.data[a:a + 8]))
else:
tmp_address = number.MemoryLoad.i64(memory.data[a:a + 8])
else:
if utils.is_symbolic(memory.data[a]):
tmp_address += z3.simplify(number.MemoryLoad.i64(memory.data[a:a + 8]))
else:
tmp_address += number.MemoryLoad.i64(memory.data[a:a + 8])
if utils.is_symbolic(memory.data[a + 32]):
list_value.append(z3.simplify(number.MemoryLoad.i64(memory.data[a + 32:a + 32 + 8])))
else:
list_value.append(number.MemoryLoad.i64(memory.data[a + 32:a + 32 + 8]))
a += 8
for item in list_value:
if item in global_vars.dict_symbolic_address:
if global_vars.dict_symbolic_address[item] == tmp_address:
global_vars.list_storageStore.append(item)
return []
@classmethod
def storageLoad(cls, args, solver, memory):
a = 32
list_v = list()
for i in range(4):
v = utils.gen_symbolic_value(bin_format.i64, f'storageLoad_{global_vars.num_storageLoad}')
list_v.append(v)
global_vars.add_num_storageLoad()
memory.data[a:a + 8] = number.MemoryStore.pack_i64(v)
if i == 0:
if utils.is_symbolic(memory.data[a-32]):
tmp = z3.simplify(number.MemoryLoad.i64(memory.data[a-32:a + 8-32]))
else:
tmp = number.MemoryLoad.i64(memory.data[a-32:a + 8-32])
else:
if utils.is_symbolic(memory.data[a-32]):
tmp += z3.simplify(number.MemoryLoad.i64(memory.data[a-32:a + 8-32]))
else:
tmp += number.MemoryLoad.i64(memory.data[a-32:a + 8-32])
a += 8
for item in list_v:
global_vars.add_dict_symbolic_address(item, tmp)
return []
@classmethod
def getCaller(cls, args, solver, memory):
address = args[0].n
for i in range(4):
v = utils.gen_symbolic_value(bin_format.i64, f'getCaller_{global_vars.num_getCaller}')
memory.data[address:address + 8] = number.MemoryStore.pack_i64(v)
global_vars.add_num_getCaller()
address += 8
return []
@classmethod
def getCallValue(cls, args, solver, memory):
address = args[0].n
if global_vars.flag_getCallValue_in_function:
for i in range(4):
v = utils.gen_symbolic_value(bin_format.i64, f'getCallValue_{global_vars.num_getCallValue}')
memory.data[address:address + 8] = number.MemoryStore.pack_i64(v)
global_vars.add_num_getCallValue()
address += 8
else:
for i in range(32):
memory.data[address + i] = 0
return []
@classmethod
def codeCopy(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def getCodeSize(cls, args, solver, memory):
pass
@classmethod
def getBlockCoinbase(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def create(cls, args, solver, memory):
pass
@classmethod
def getBlockDifficulty(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def externalCodeCopy(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def getExternalCodeSize(cls, args, solver, memory):
r = randint(0,20)
r = utils.gen_symbolic_value(bin_format.i32, f'getExternalCodeSize_{r}')
return r
@classmethod
def getGasLeft(cls, args, solver, memory):
r = utils.gen_symbolic_value(bin_format.i64, f'getGasLeft')
return r
@classmethod
def getBlockGasLimit(cls, args, solver, memory):
pass
@classmethod
def getTxGasPrice(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def log(cls, args, solver, memory):
pass
@classmethod
def getBlockNumber(cls, args, solver, memory):
r = randint(0,20)
r = utils.gen_symbolic_value(bin_format.i64, f'getBlockNumber_{r}')
global_vars.add_dict_block_solver(str(r), solver)
return r
@classmethod
def getTxOrigin(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def finish(cls, args, solver, memory):
global_vars.add_flag_revert()
logger.infoln(f'eth.finish')
return []
@classmethod
def revert(cls, args, solver, memory):
global_vars.add_flag_revert()
logger.infoln(f'eth.revert')
return []
@classmethod
def getReturnDataSize(cls, args, solver, memory):
r = global_vars.add_flag_num_host()
r = utils.gen_symbolic_value(bin_format.i32, f'getReturnDataSize_{r}')
return r
@classmethod
def returnDataCopy(cls, args, solver, memory):
# [TODO]
return []
@classmethod
def selfDestruct(cls, args, solver, memory):
global_vars.add_flag_revert()
logger.infoln(f'eth.selfDestruct')
return []
@classmethod
def getBlockTimestamp(cls, args, solver, memory):
r = randint(0,20)
r = utils.gen_symbolic_value(bin_format.i64, f'getBlockTimestamp_{r}')
global_vars.add_dict_block_solver(str(r), solver)
return r
class EthereumWasmFunc:
""" A emulator of wasm functions of Ethereum Wasm blockchain platform.
Returns: 0, if do nothing
other, The return value will be placed on the top of the stack
"""
def __init__(self):
pass
@classmethod
def add_carry(cls, args, solver, store):
flag_ = 1
for v in valn:
if utils.is_symbolic(v.n):
flag_ = 0
if flag_ == 1:
t = valn[0].n + valn[1].n
r = t + valn[2].n
t1 = int(number.int2u64(t) < number.int2u64(valn[0].n))
t2 = int(number.int2u64(r) < number.int2u64(t))
r_c = number.int2u32(t1 | t2)
flag_skip = 1
store.globals[module.globaladdrs[0]] = GlobalInstance(Value(bin_format.i64, r_c), True)
return [Value.from_i64(r)]
return 0
@classmethod
def add(cls, args, solver, store):
# [TODO]
pass
@classmethod
def sub(cls, args, solver, store):
# [TODO]
pass
@classmethod
def sub320(cls, args, solver, store):
# [TODO]
pass
@classmethod
def sub512(cls, args, solver, store):
# [TODO]
pass
@classmethod
def mul_64x64_128(cls, args, solver, store):
# [TODO]
pass
@classmethod
def mul_128x128_256(cls, args, solver, store):
# [TODO]
pass
@classmethod
def mul_256x256_512(cls, args, solver, store):
# [TODO]
pass
@classmethod
def mul(cls, args, solver, store):
if utils.is_all_real(args[0].n, args[1].n, args[2].n, args[3].n, args[4].n, args[5].n, args[6].n, args[7].n):
return 0
else:
list_mul_symbolic = list()
for pos in range(8):
if utils.is_symbolic(args[pos].n):
list_mul_symbolic.append(args[pos].n)
r = list()
ret = utils.gen_symbolic_value(bin_format.i64, f'mul_{global_vars.num_mul}')
r.append(Value(bin_format.i64, ret))
global_vars.add_num_mul()
global_vars.add_dict_symbolic_address(ret, list_mul_symbolic)
for i in range(3):
ret = utils.gen_symbolic_value(bin_format.i64, f'mul_{global_vars.num_mul}')
r.append(GlobalInstance(Value(bin_format.i64, ret), True))
global_vars.add_num_mul()
global_vars.add_dict_symbolic_address(ret, list_mul_symbolic)
return r
@classmethod
def div(cls, args, solver, store):
if utils.is_all_real(args[0].n, args[1].n, args[2].n, args[3].n, args[4].n, args[5].n, args[6].n, args[7].n):
return 0
else:
list_div_symbolic = list()
for pos in range(8):
if utils.is_symbolic(args[pos].n):
list_div_symbolic.append(args[pos].n)
r = list()
ret = utils.gen_symbolic_value(bin_format.i64, f'div_{global_vars.num_div}')
r.append(Value(bin_format.i64, ret))
global_vars.add_num_div()
global_vars.add_dict_symbolic_address(ret, list_div_symbolic)
for i in range(3):
ret = utils.gen_symbolic_value(bin_format.i64, f'div_{global_vars.num_div}')
r.append(GlobalInstance(Value(bin_format.i64, ret), True))
global_vars.add_num_div()
global_vars.add_dict_symbolic_address(ret, list_div_symbolic)
return r
@classmethod
def sdiv(cls, args, solver, store):
# [TODO]
pass
@classmethod