-
Notifications
You must be signed in to change notification settings - Fork 10
/
EVMVM.sol
2280 lines (2275 loc) · 34.9 KB
/
EVMVM.sol
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
/*
Author: Chance Santana-Wees
Contact Email: [email protected]
This code is intended to allow a contract to execute arbitrary opcode input which has not been previously
committed to the blockchain as a contract. Effectively, it is an VM of the EVM which is running on the EVM.
An EVM-VM, as it were.
I decided to attempt to optimize this functionality via usage of assembly so that I would not get the gas
overhead that would come from storing the virtual stack in memory. I also used a program counter trick to
avoid having to do dozens of different jumpi (10 gas per!) operations, which should make the overall
execution overhead relatively low.
Notes:
This is the first functional draft of the EVMVM. It actually works!
For whatever reason I have to use an old compiler (0.4.6 seems to work) to get it to actually leave JUMPDESTs in.
Which means that there are a handful of EVM opcodes that aren't possible to use.
If you don't want to handcode a bytecode function to run against this contract, a good way to test
it is to run it in Remix (with compiler 0.4.6) and use the following input:
["0x30","0x60","0x40","0x51","0x52","0x60","0x14","0x60","0x40","0x51","0x60","0x0c","0x01","0xa0"]
This is a test function which loads ADDRESS, stores it to memory, and then does a log0 of it.
*/
pragma solidity ^0.4.0;
contract CodeRunner {
function RunCode(bytes script) public {
assembly{
0x1f
add //stack[0] becomes virtual program counter (vpc)
_VM: //program counter is 0xA0 here
/*PUSH1*/ ////The _VM subroutine reads a byte from
0x1 ////the input data, specified by the vpc,
add //increment vpc ////and jumps to the correct subroutine
dup1 ////based on mathematical offset.
mload ////The subroutines all perform the operation
/*PUSH1*/ ////specified and then jump back to _VM
0x0
byte
/*PUSH1*/
0x5
mul
/*PUSH1*/
_OPS
add //acquire jump destination from opcode
jump
_OPS: //program counter is 0xB0 here
//PUSH2
//...
STOP
jump
_0x01:
//PUSH2
//...
ADD
jump
_0x02:
//PUSH2
//...
MUL
jump
_0x03:
//PUSH2
//...
SUB
jump
_0x04:
//PUSH2
//...
DIV
jump
_0x05:
//PUSH2
//...
SDIV
jump
_0x06:
//PUSH2
//...
MOD
jump
_0x07:
//PUSH2
//...
SMOD
jump
_0x08:
//PUSH2
//...
ADDMOD
jump
_0x09:
//PUSH2
//...
MULMOD
jump
_0x0a:
//PUSH2
//...
EXP
jump
_0x0b:
//PUSH2
//...
SIGNEXTEND
jump
_0x0c:
//PUSH2
//...
INVALID
jump
_0x0d:
//PUSH2
//...
INVALID
jump
_0x0e:
//PUSH2
//...
INVALID
jump
_0x0f:
//PUSH2
//...
INVALID
jump
_0x10:
//PUSH2
//...
LT
jump
_0x11:
//PUSH2
//...
GT
jump
_0x12:
//PUSH2
//...
SLT
jump
_0x13:
//PUSH2
//...
SGT
jump
_0x14:
//PUSH2
//...
EQ
jump
_0x15:
//PUSH2
//...
ISZERO
jump
_0x16:
//PUSH2
//...
AND
jump
_0x17:
//PUSH2
//...
OR
jump
_0x18:
//PUSH2
//...
XOR
jump
_0x19:
//PUSH2
//...
NOT
jump
_0x1a:
//PUSH2
//...
BYTE
jump
_0x1b:
//PUSH2
//...
INVALID
jump
_0x1c:
//PUSH2
//...
INVALID
jump
_0x1d:
//PUSH2
//...
INVALID
jump
_0x1e:
//PUSH2
//...
INVALID
jump
_0x1f:
//PUSH2
//...
INVALID
jump
_0x20:
//PUSH2
//...
SHA3
jump
_0x21:
//PUSH2
//...
INVALID
jump
_0x22:
//PUSH2
//...
INVALID
jump
_0x23:
//PUSH2
//...
INVALID
jump
_0x24:
//PUSH2
//...
INVALID
jump
_0x25:
//PUSH2
//...
INVALID
jump
_0x26:
//PUSH2
//...
INVALID
jump
_0x27:
//PUSH2
//...
INVALID
jump
_0x28:
//PUSH2
//...
INVALID
jump
_0x29:
//PUSH2
//...
INVALID
jump
_0x2a:
//PUSH2
//...
INVALID
jump
_0x2b:
//PUSH2
//...
INVALID
jump
_0x2c:
//PUSH2
//...
INVALID
jump
_0x2d:
//PUSH2
//...
INVALID
jump
_0x2e:
//PUSH2
//...
INVALID
jump
_0x2f:
//PUSH2
//...
INVALID
jump
_0x30:
//PUSH2
//...
ADDRESS
jump
_0x31:
//PUSH2
//...
BALANCE
jump
_0x32:
//PUSH2
//...
ORIGIN
jump
_0x33:
//PUSH2
//...
CALLER
jump
_0x34:
//PUSH2
//...
CALLVALUE
jump
_0x35:
//PUSH2
//...
CALLDATALOAD
jump
_0x36:
//PUSH2
//...
CALLDATASIZE
jump
_0x37:
//PUSH2
//...
CALLDATACOPY
jump
_0x38:
//PUSH2
//...
CODESIZE
jump
_0x39:
//PUSH2
//...
CODECOPY
jump
_0x3a:
//PUSH2
//...
GASPRICE
jump
_0x3b:
//PUSH2
//...
EXTCODESIZE
jump
_0x3c:
//PUSH2
//...
EXTCODECOPY
jump
_0x3d:
//PUSH2
//...
RETURNDATASIZE
jump
_0x3e:
//PUSH2
//...
RETURNDATACOPY
jump
_0x3f:
//PUSH2
//...
INVALID
jump
_0x40:
//PUSH2
//...
BLOCKHASH
jump
_0x41:
//PUSH2
//...
COINBASE
jump
_0x42:
//PUSH2
//...
TIMESTAMP
jump
_0x43:
//PUSH2
//...
NUMBER
jump
_0x44:
//PUSH2
//...
DIFFICULTY
jump
_0x45:
//PUSH2
//...
GASLIMIT
jump
_0x46:
//PUSH2
//...
INVALID
jump
_0x47:
//PUSH2
//...
INVALID
jump
_0x48:
//PUSH2
//...
INVALID
jump
_0x49:
//PUSH2
//...
INVALID
jump
_0x4a:
//PUSH2
//...
INVALID
jump
_0x4b:
//PUSH2
//...
INVALID
jump
_0x4c:
//PUSH2
//...
INVALID
jump
_0x4d:
//PUSH2
//...
INVALID
jump
_0x4e:
//PUSH2
//...
INVALID
jump
_0x4f:
//PUSH2
//...
INVALID
jump
_0x50:
//PUSH2
//...
POP
jump
_0x51:
//PUSH2
//...
MLOAD
jump
_0x52:
//PUSH2
//...
MSTORE
jump
_0x53:
//PUSH2
//...
MSTORE8
jump
_0x54:
//PUSH2
//...
SLOAD
jump
_0x55:
//PUSH2
//...
SSTORE
jump
_0x56:
//PUSH2
//...
JUMP
jump
_0x57:
//PUSH2
//...
JUMPI
jump
_0x58:
//PUSH2
//...
PC
jump
_0x59:
//PUSH2
//...
MSIZE
jump
_0x5a:
//PUSH2
//...
GAS
jump
_0x5b:
//PUSH2
//...
JUMPDEST
jump
_0x5c:
//PUSH2
//...
INVALID
jump
_0x5d:
//PUSH2
//...
INVALID
jump
_0x5e:
//PUSH2
//...
INVALID
jump
_0x5f:
//PUSH2
//...
INVALID
jump
_0x60:
//PUSH2
//...
PUSH1
jump
_0x61:
//PUSH2
//...
PUSH2
jump
_0x62:
//PUSH2
//...
PUSH3
jump
_0x63:
//PUSH2
//...
PUSH4
jump
_0x64:
//PUSH2
//...
PUSH5
jump
_0x65:
//PUSH2
//...
PUSH6
jump
_0x66:
//PUSH2
//...
PUSH7
jump
_0x67:
//PUSH2
//...
PUSH8
jump
_0x68:
//PUSH2
//...
PUSH9
jump
_0x69:
//PUSH2
//...
PUSH10
jump
_0x6a:
//PUSH2
//...
PUSH11
jump
_0x6b:
//PUSH2
//...
PUSH12
jump
_0x6c:
//PUSH2
//...
PUSH13
jump
_0x6d:
//PUSH2
//...
PUSH14
jump
_0x6e:
//PUSH2
//...
PUSH15
jump
_0x6f:
//PUSH2
//...
PUSH16
jump
_0x70:
//PUSH2
//...
PUSH17
jump
_0x71:
//PUSH2
//...
PUSH18
jump
_0x72:
//PUSH2
//...
PUSH19
jump
_0x73:
//PUSH2
//...
PUSH20
jump
_0x74:
//PUSH2
//...
PUSH21
jump
_0x75:
//PUSH2
//...
PUSH22
jump
_0x76:
//PUSH2
//...
PUSH23
jump
_0x77:
//PUSH2
//...
PUSH24
jump
_0x78:
//PUSH2
//...
PUSH25
jump
_0x79:
//PUSH2
//...
PUSH26
jump
_0x7a:
//PUSH2
//...
PUSH27
jump
_0x7b:
//PUSH2
//...
PUSH28
jump
_0x7c:
//PUSH2
//...
PUSH29
jump
_0x7d:
//PUSH2
//...
PUSH30
jump
_0x7e:
//PUSH2
//...
PUSH31
jump
_0x7f:
//PUSH2
//...
PUSH32
jump
_0x80:
//PUSH2
//...
DUP1
jump
_0x81:
//PUSH2
//...
DUP2
jump
_0x82:
//PUSH2
//...
DUP3
jump
_0x83:
//PUSH2
//...
DUP4
jump
_0x84:
//PUSH2
//...
DUP5
jump
_0x85:
//PUSH2
//...
DUP6
jump
_0x86:
//PUSH2
//...
DUP7
jump
_0x87:
//PUSH2
//...
DUP8
jump
_0x88:
//PUSH2
//...
DUP9
jump
_0x89:
//PUSH2
//...
DUP10
jump
_0x8a:
//PUSH2
//...
DUP11
jump
_0x8b:
//PUSH2
//...
DUP12
jump
_0x8c:
//PUSH2
//...
DUP13
jump
_0x8d:
//PUSH2
//...
DUP14
jump
_0x8e:
//PUSH2
//...
DUP15
jump
_0x8f:
//PUSH2
//...
DUP16
jump
_0x90:
//PUSH2
//...
SWAP1
jump
_0x91:
//PUSH2
//...
SWAP2
jump
_0x92:
//PUSH2
//...
SWAP3
jump
_0x93:
//PUSH2
//...
SWAP4
jump
_0x94:
//PUSH2
//...
SWAP5
jump
_0x95:
//PUSH2
//...
SWAP6
jump
_0x96:
//PUSH2
//...
SWAP7
jump
_0x97:
//PUSH2
//...
SWAP8
jump
_0x98:
//PUSH2
//...
SWAP9
jump
_0x99:
//PUSH2
//...
SWAP10
jump
_0x9a:
//PUSH2
//...
SWAP11
jump
_0x9b:
//PUSH2
//...
SWAP12
jump
_0x9c:
//PUSH2
//...
SWAP13
jump
_0x9d:
//PUSH2
//...
SWAP14
jump
_0x9e:
//PUSH2
//...
SWAP15
jump
_0x9f:
//PUSH2
//...
SWAP16
jump
_0xa0:
//PUSH2
//...
LOG0
jump
_0xa1:
//PUSH2
//...
LOG1
jump
_0xa2:
//PUSH2
//...
LOG2
jump
_0xa3:
//PUSH2
//...
LOG3
jump
_0xa4:
//PUSH2
//...
LOG4
jump
_0xa5:
//PUSH2
//...
INVALID
jump
_0xa6:
//PUSH2
//...
INVALID
jump
_0xa7:
//PUSH2
//...
INVALID
jump
_0xa8:
//PUSH2
//...
INVALID
jump
_0xa9:
//PUSH2
//...
INVALID
jump
_0xaa:
//PUSH2
//...
INVALID
jump
_0xab:
//PUSH2
//...
INVALID
jump
_0xac:
//PUSH2
//...
INVALID
jump
_0xad:
//PUSH2
//...
INVALID
jump
_0xae:
//PUSH2
//...
INVALID
jump
_0xaf:
//PUSH2
//...
INVALID
jump
_0xb0:
//PUSH2
//...
INVALID
jump
_0xb1:
//PUSH2
//...
INVALID
jump
_0xb2:
//PUSH2
//...
INVALID
jump
_0xb3:
//PUSH2
//...
INVALID
jump
_0xb4:
//PUSH2
//...
INVALID
jump
_0xb5:
//PUSH2
//...
INVALID
jump
_0xb6:
//PUSH2
//...
INVALID
jump
_0xb7:
//PUSH2
//...
INVALID
jump
_0xb8:
//PUSH2
//...
INVALID
jump
_0xb9:
//PUSH2
//...
INVALID
jump
_0xba:
//PUSH2
//...
INVALID
jump
_0xbb:
//PUSH2
//...
INVALID
jump
_0xbc:
//PUSH2
//...
INVALID
jump
_0xbd:
//PUSH2
//...
INVALID
jump
_0xbe:
//PUSH2
//...
INVALID