-
Notifications
You must be signed in to change notification settings - Fork 6
/
spec_binary_format.py
1279 lines (1054 loc) · 36.4 KB
/
spec_binary_format.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
"""
PyWebAssembly - Implmentation of WebAssembly, and some tools.
Copyright (C) 2018-2019 Paul Dworzanski
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""
import spec_execution as execution
verbose = 0
###################
###################
# 5 BINARY FORMAT #
###################
###################
#Chapter 5 defines a binary syntax over the abstract syntax. The implementation is a recursive-descent parser which takes a `.wasm` file and builds an abstract syntax tree out of nested Python lists and dicts. Also implemented are inverses (up to a canonical form) which write an abstract syntax tree back to a `.wasm` file.
# key-value pairs of binary opcodes and their text reperesentation
opcodes_binary2text = {
0x00:'unreachable',
0x01:'nop',
0x02:'block', # blocktype in* end # begin block
0x03:'loop', # blocktype in* end # begin block
0x04:'if', # blocktype in1* else? end # begin block
0x05:'else', # in2* # end block & begin block
0x0b:'end', # end block
0x0c:'br', # labelidx # branch
0x0d:'br_if', # labelidx # branch
0x0e:'br_table', # labelidx* labelidx # branch
0x0f:'return', # end outermost block
0x10:'call', # funcidx # branch
0x11:'call_indirect', # typeidx 0x00 # branch
0x1a:'drop',
0x1b:'select',
0x20:'get_local', # localidx
0x21:'set_local', # localidx
0x22:'tee_local', # localidx
0x23:'get_global', # globalidx
0x24:'set_global', # globalidx
0x28:'i32.load', # memarg
0x29:'i64.load', # memarg
0x2a:'f32.load', # memarg
0x2b:'f64.load', # memarg
0x2c:'i32.load8_s', # memarg
0x2d:'i32.load8_u', # memarg
0x2e:'i32.load16_s', # memarg
0x2f:'i32.load16_u', # memarg
0x30:'i64.load8_s', # memarg
0x31:'i64.load8_u', # memarg
0x32:'i64.load16_s', # memarg
0x33:'i64.load16_u', # memarg
0x34:'i64.load32_s', # memarg
0x35:'i64.load32_u', # memarg
0x36:'i32.store', # memarg
0x37:'i64.store', # memarg
0x38:'f32.store', # memarg
0x39:'f64.store', # memarg
0x3a:'i32.store8', # memarg
0x3b:'i32.store16', # memarg
0x3c:'i64.store8', # memarg
0x3d:'i64.store16', # memarg
0x3e:'i64.store32', # memarg
0x3f:'memory.size',
0x40:'memory.grow',
0x41:'i32.const', # i32
0x42:'i64.const', # i64
0x43:'f32.const', # f32
0x44:'f64.const', # f64
0x45:'i32.eqz',
0x46:'i32.eq',
0x47:'i32.ne',
0x48:'i32.lt_s',
0x49:'i32.lt_u',
0x4a:'i32.gt_s',
0x4b:'i32.gt_u',
0x4c:'i32.le_s',
0x4d:'i32.le_u',
0x4e:'i32.ge_s',
0x4f:'i32.ge_u',
0x50:'i64.eqz',
0x51:'i64.eq',
0x52:'i64.ne',
0x53:'i64.lt_s',
0x54:'i64.lt_u',
0x55:'i64.gt_s',
0x56:'i64.gt_u',
0x57:'i64.le_s',
0x58:'i64.le_u',
0x59:'i64.ge_s',
0x5a:'i64.ge_u',
0x5b:'f32.eq',
0x5c:'f32.ne',
0x5d:'f32.lt',
0x5e:'f32.gt',
0x5f:'f32.le',
0x60:'f32.ge',
0x61:'f64.eq',
0x62:'f64.ne',
0x63:'f64.lt',
0x64:'f64.gt',
0x65:'f64.le',
0x66:'f64.ge',
0x67:'i32.clz',
0x68:'i32.ctz',
0x69:'i32.popcnt',
0x6a:'i32.add',
0x6b:'i32.sub',
0x6c:'i32.mul',
0x6d:'i32.div_s',
0x6e:'i32.div_u',
0x6f:'i32.rem_s',
0x70:'i32.rem_u',
0x71:'i32.and',
0x72:'i32.or',
0x73:'i32.xor',
0x74:'i32.shl',
0x75:'i32.shr_s',
0x76:'i32.shr_u',
0x77:'i32.rotl',
0x78:'i32.rotr',
0x79:'i64.clz',
0x7a:'i64.ctz',
0x7b:'i64.popcnt',
0x7c:'i64.add',
0x7d:'i64.sub',
0x7e:'i64.mul',
0x7f:'i64.div_s',
0x80:'i64.div_u',
0x81:'i64.rem_s',
0x82:'i64.rem_u',
0x83:'i64.and',
0x84:'i64.or',
0x85:'i64.xor',
0x86:'i64.shl',
0x87:'i64.shr_s',
0x88:'i64.shr_u',
0x89:'i64.rotl',
0x8a:'i64.rotr',
0x8b:'f32.abs',
0x8c:'f32.neg',
0x8d:'f32.ceil',
0x8e:'f32.floor',
0x8f:'f32.trunc',
0x90:'f32.nearest',
0x91:'f32.sqrt',
0x92:'f32.add',
0x93:'f32.sub',
0x94:'f32.mul',
0x95:'f32.div',
0x96:'f32.min',
0x97:'f32.max',
0x98:'f32.copysign',
0x99:'f64.abs',
0x9a:'f64.neg',
0x9b:'f64.ceil',
0x9c:'f64.floor',
0x9d:'f64.trunc',
0x9e:'f64.nearest',
0x9f:'f64.sqrt',
0xa0:'f64.add',
0xa1:'f64.sub',
0xa2:'f64.mul',
0xa3:'f64.div',
0xa4:'f64.min',
0xa5:'f64.max',
0xa6:'f64.copysign',
0xa7:'i32.wrap/i64',
0xa8:'i32.trunc_s/f32',
0xa9:'i32.trunc_u/f32',
0xaa:'i32.trunc_s/f64',
0xab:'i32.trunc_u/f64',
0xac:'i64.extend_s/i32',
0xad:'i64.extend_u/i32',
0xae:'i64.trunc_s/f32',
0xaf:'i64.trunc_u/f32',
0xb0:'i64.trunc_s/f64',
0xb1:'i64.trunc_u/f64',
0xb2:'f32.convert_s/i32',
0xb3:'f32.convert_u/i32',
0xb4:'f32.convert_s/i64',
0xb5:'f32.convert_u/i64',
0xb6:'f32.demote/f64',
0xb7:'f64.convert_s/i32',
0xb8:'f64.convert_u/i32',
0xb9:'f64.convert_s/i64',
0xba:'f64.convert_u/i64',
0xbb:'f64.promote/f32',
0xbc:'i32.reinterpret/f32',
0xbd:'i64.reinterpret/f64',
0xbe:'f32.reinterpret/i32',
0xbf:'f64.reinterpret/i64'
}
# key-value pairs of text opcodes and their binary reperesentation
opcodes_text2binary = {}
for opcode in opcodes_binary2text:
opcodes_text2binary[opcodes_binary2text[opcode]]=opcode
# 5.1.3 VECTORS
def spec_binary_vec(raw,idx,B):
if verbose: print("spec_binary_vec(",idx,")")
idx,n=spec_binary_uN(raw,idx,32)
xn = []
for i in range(n):
idx,x = B(raw,idx)
xn+=[x]
return idx,xn
def spec_binary_vec_inv(mynode,myfunc):
n_bytes=spec_binary_uN_inv(len(mynode),32)
xn_bytes=bytearray()
for x in mynode:
xn_bytes+=myfunc(x)
return n_bytes+xn_bytes
############
# 5.2 VALUES
############
# 5.2.1 BYTES
def spec_binary_byte(raw,idx):
if verbose: print("spec_binary_byte(",idx,")")
if len(raw)<=idx: raise Exception("malformed")
return idx+1,raw[idx]
def spec_binary_byte_inv(node):
return bytearray([node])
# 5.2.2 INTEGERS
#unsigned
def spec_binary_uN(raw,idx,N):
if verbose: print("spec_binary_uN(",idx,N,")")
idx,n=spec_binary_byte(raw,idx)
if n<2**7 and n<2**N:
return idx,n
elif n>=2**7 and N>7:
idx,m=spec_binary_uN(raw,idx,N-7)
return idx, (2**7)*m+(n-2**7)
else:
raise Exception("malformed")
def spec_binary_uN_inv(k,N):
#print("spec_binary_uN_inv(",k,N,")")
if k<2**7 and k<2**N:
return bytearray([k])
elif k>=2**7 and N>7:
return bytearray([k%(2**7)+2**7])+spec_binary_uN_inv(k//(2**7),N-7)
else:
raise Exception("malformed")
#signed
def spec_binary_sN(raw,idx,N):
if verbose: print("spec_binary_sN(",idx,N,")")
n=int(raw[idx])
idx+=1
if n<2**6 and n<2**(N-1):
return idx,n
elif 2**6<=n<2**7 and n>=2**7-2**(N-1):
return idx,n-2**7
elif n>=2**7 and N>7:
idx,m=spec_binary_sN(raw,idx,N-7)
return idx,2**7*m+(n-2**7)
else:
raise Exception("malformed")
def spec_binary_sN_inv(k,N):
if 0<=k<2**6 and k<2**N:
return bytearray([k])
elif 2**6<=k+2**7<2**7: # and k+2**7>=2**7-2**(N-1):
return bytearray([k+2**7])
elif (k>=2**6 or k<2**6) and N>7: #(k<0 and k+2**7>=2**6)) and N>7:
return bytearray([k%(2**7)+2**7])+spec_binary_sN_inv((k//(2**7)),N-7)
else:
raise Exception("malformed")
#uninterpretted integers
def spec_binary_iN(raw,idx,N):
if verbose: print("spec_binary_iN(",idx,N,")")
idx,n=spec_binary_sN(raw,idx,N)
i = execution.spec_signediN_inv(N,n)
#i = spec_signediN_inv(N,n)
return idx, i
def spec_binary_iN_inv(i,N):
return spec_binary_sN_inv(execution.spec_signediN(N,i),N)
#return spec_binary_sN_inv(spec_signediN(N,i),N)
# 5.2.3 FLOATING-POINT
#fN::= b*:byte^{N/8} => bytes_{fN}^{-1}(b*)
def spec_binary_fN(raw,idx,N):
if verbose: print("spec_binary_fN(",idx,N,")")
bstar = bytearray([])
for i in range(N//8):
bstar+= bytearray([raw[idx]])
idx+=1
return idx, execution.spec_bytest_inv("f"+str(N),bstar) #bytearray(bstar)
#return idx, spec_bytest_inv("f"+str(N),bstar) #bytearray(bstar)
def spec_binary_fN_inv(node,N):
return execution.spec_bytest("f"+str(N),node)
#return spec_bytest("f"+str(N),node)
# 5.2.4 NAMES
#name as UTF-8 codepoints
def spec_binary_name(raw,idx):
if verbose: print("spec_binary_name(",idx,")")
#print("spec_binary_name()")
idx,bstar = spec_binary_vec(raw,idx,spec_binary_byte)
#print("bstar",bstar)
nametxt=""
try:
nametxt=bytearray(bstar).decode()
except:
raise Exception("malformed")
return idx,nametxt
#rest is unused, for finding inverse of utf8(name)=b*, keep since want to correct spec doc
bstaridx=0
lenbstar = len(bstar)
name=[]
while bstaridx<lenbstar:
if bstaridx>=len(bstar): raise Exception("malformed")
b1=bstar[bstaridx]
bstaridx+=1
if b1<0x80:
name+=[b1]
continue
if bstaridx>=len(bstar): raise Exception("malformed")
b2=bstar[bstaridx]
if b2>>6 != 0b01: raise Exception("malformed")
bstaridx+=1
c=(2**6)*(b1-0xc0) + (b2-0x80)
#c_check = 2**6*(b1-192) + (b2-128)
if 0x80<=c<0x800:
name+=[c]
continue
if bstaridx>=len(bstar): raise Exception("malformed")
b3=bstar[bstaridx]
if b2>>5 != 0b011: raise Exception("malformed")
bstaridx+=1
c=(2**12)*(b1-0xe0) + (2**6)*(b2-0x80) + (b3-0x80)
if 0x800<=c<0x10000 and (b2>>6 == 0b01):
name+=[c]
continue
if bstaridx>=len(bstar):raise Exception("malformed")
b4=bstar[bstaridx]
if b2>>4 != 0b0111: raise Exception("malformed")
bstaridx+=1
c=2**18*(b1-0xf0) + 2**12*(b2-0x80) + 2**6*(b3-0x80) + (b4-0x80)
if 0x10000<=c<0x110000:
name+=[c]
else:
#print("malformed character")
raise Exception("malformed")
#convert each codepoint to utf8 character
#print("utf8 name",name, len(name), name=="")
nametxt = ""
for c in name:
#print(str(chr(c)))
#print(c)
nametxt+=chr(c)
#print("utf8 nametext",nametxt, len(nametxt), nametxt=="")
return idx,nametxt
def spec_binary_name_inv(chars):
name_bytes=bytearray()
for c in chars:
c = ord(c)
if c<0x80:
name_bytes += bytes([c])
elif 0x80<=c<0x800:
name_bytes += bytes([(c>>6)+0xc0,(c&0b111111)+0x80])
elif 0x800<=c<0x10000:
name_bytes += bytes([(c>>12)+0xe0,((c>>6)&0b111111)+0x80,(c&0b111111)+0x80])
elif 0x10000<=c<0x110000:
name_bytes += bytes([(c>>18)+0xf0,((c>>12)&0b111111)+0x80,((c>>6)&0b111111)+0x80,(c&0b111111)+0x80])
else:
return None #error
return spec_binary_uN_inv(len(name_bytes),64)+name_bytes
###########
# 5.3 TYPES
###########
# 5.3.1 VALUE TYPES
valtype2bin={"i32":0x7f,"i64":0x7e,"f32":0x7d,"f64":0x7c}
bin2valtype={val:key for key,val in valtype2bin.items()}
def spec_binary_valtype(raw,idx):
if verbose: print("spec_binary_valtype(",idx,")")
if idx>=len(raw): raise Exception("malformed")
if raw[idx] in bin2valtype:
return idx+1,bin2valtype[raw[idx]]
else:
raise Exception("malformed")
def spec_binary_valtype_inv(node):
#print("spec_binary_valtype_inv(",node,")")
if node in valtype2bin:
return bytearray([valtype2bin[node]])
else:
return bytearray([]) #error
# 5.3.2 RESULT TYPES
def spec_binary_blocktype(raw,idx):
if verbose: print("spec_binary_blocktype(",idx,")")
if raw[idx]==0x40:
return idx+1,[]
idx,t=spec_binary_valtype(raw,idx)
return idx, t
def spec_binary_blocktype_inv(node):
#print("spec_binary_blocktype_inv(",node,")")
if node==[]:
return bytearray([0x40])
else:
return spec_binary_valtype_inv(node)
# 5.3.3 FUNCTION TYPES
def spec_binary_functype(raw,idx):
if verbose: print("spec_binary_functype(",idx,")")
if raw[idx]!=0x60: raise Exception("malformed")
idx+=1
idx,t1star=spec_binary_vec(raw,idx,spec_binary_valtype)
idx,t2star=spec_binary_vec(raw,idx,spec_binary_valtype)
return idx,[t1star,t2star]
def spec_binary_functype_inv(node):
return bytearray([0x60])+spec_binary_vec_inv(node[0],spec_binary_valtype_inv)+spec_binary_vec_inv(node[1],spec_binary_valtype_inv)
# 5.3.4 LIMITS
def spec_binary_limits(raw,idx):
if verbose: print("spec_binary_limits(",idx,")")
if raw[idx]==0x00:
idx,n = spec_binary_uN(raw,idx+1,32)
return idx,{"min":n,"max":None}
elif raw[idx]==0x01:
idx,n = spec_binary_uN(raw,idx+1,32)
idx,m = spec_binary_uN(raw,idx,32)
return idx,{"min":n,"max":m}
else:
return idx,None #error
def spec_binary_limits_inv(node):
if node["max"]==None:
return bytearray([0x00])+spec_binary_uN_inv(node["min"],32)
else:
return bytearray([0x01])+spec_binary_uN_inv(node["min"],32)+spec_binary_uN_inv(node["max"],32)
# 5.3.5 MEMORY TYPES
def spec_binary_memtype(raw,idx):
if verbose: print("spec_binary_memtype(",idx,")")
return spec_binary_limits(raw,idx)
def spec_binary_memtype_inv(node):
return spec_binary_limits_inv(node)
# 5.3.6 TABLE TYPES
def spec_binary_tabletype(raw,idx):
if verbose: print("spec_binary_tabletype(",idx,")")
idx,et = spec_binary_elemtype(raw,idx)
idx,lim = spec_binary_limits(raw,idx)
return idx,[lim,et]
def spec_binary_elemtype(raw,idx):
if verbose: print("spec_binary_elemtype(",idx,")")
if raw[idx]==0x70:
return idx+1,"anyfunc"
else:
raise Exception("malformed")
def spec_binary_tabletype_inv(node):
return spec_binary_elemtype_inv(node[1])+spec_binary_limits_inv(node[0])
def spec_binary_elemtype_inv(node):
return bytearray([0x70])
# 5.3.7 GLOBAL TYPES
def spec_binary_globaltype(raw,idx):
if verbose: print("spec_binary_globaltype(",idx,")")
idx,t = spec_binary_valtype(raw,idx)
idx,m = spec_binary_mut(raw,idx)
return idx,[m,t]
def spec_binary_mut(raw,idx):
if verbose: print("spec_binary_mut(",idx,")")
if raw[idx]==0x00:
return idx+1,"const"
elif raw[idx]==0x01:
return idx+1,"var"
else:
raise Exception("malformed")
def spec_binary_globaltype_inv(node):
return spec_binary_valtype_inv(node[1])+spec_binary_mut_inv(node[0])
def spec_binary_mut_inv(node):
if node=="const":
return bytearray([0x00])
elif node=="var":
return bytearray([0x01])
else:
return bytearray([])
##################
# 5.4 INSTRUCTIONS
##################
# 5.4.1-5 VARIOUS INSTRUCTIONS
def spec_binary_memarg(raw,idx):
if verbose: print("spec_binary_memarg(",idx,")")
idx,a=spec_binary_uN(raw,idx,32)
idx,o=spec_binary_uN(raw,idx,32)
return idx,{"align":a,"offset":o}
def spec_binary_memarg_inv(node):
return spec_binary_uN_inv(node["align"],32) + spec_binary_uN_inv(node["offset"],32)
def spec_binary_instr(raw,idx):
if verbose: print("spec_binary_instr(",idx,")")
if raw[idx] not in opcodes_binary2text:
return idx, None #error
instr_binary = raw[idx]
instr_text = opcodes_binary2text[instr_binary]
idx+=1
if instr_text in {"block","loop","if"}: #block, loop, if
idx,rt=spec_binary_blocktype(raw,idx)
instar=[]
if instr_text=="if":
instar2=[]
while raw[idx] not in {0x05,0x0b}:
idx,ins=spec_binary_instr(raw,idx)
instar+=[ins]
if raw[idx]==0x05: #if with else
idx+=1
while raw[idx] != 0x0b:
idx,ins=spec_binary_instr(raw,idx)
instar2+=[ins]
#return idx+1, ["if",rt,instar+[["else"]],instar2+[["end"]]] #+[["end"]]
return idx+1, ["if",rt,instar+[["else"]],instar2+[["end"]]] #+[["end"]]
#return idx+1, ["if",rt,instar+[["end"]]] #+[["end"]]
else:
while raw[idx]!=0x0b:
idx,ins=spec_binary_instr(raw,idx)
instar+=[ins]
return idx+1, [instr_text,rt,instar+[["end"]]] #+[["end"]]
elif instr_text in {"br","br_if"}: # br, br_if
idx,l = spec_binary_labelidx(raw,idx)
return idx, [instr_text,l]
elif instr_text == "br_table": # br_table
idx,lstar=spec_binary_vec(raw,idx,spec_binary_labelidx)
idx,lN=spec_binary_labelidx(raw,idx)
return idx, ["br_table",lstar,lN]
elif instr_text in {"call","call_indirect"}: # call, call_indirect
if instr_text=="call":
idx,x=spec_binary_funcidx(raw,idx)
if instr_text=="call_indirect":
idx,x=spec_binary_typeidx(raw,idx)
if raw[idx]!=0x00: raise Exception("malformed")
idx+=1
return idx, [instr_text,x]
elif 0x20<=instr_binary<=0x22: # get_local, etc
idx,x=spec_binary_localidx(raw,idx)
return idx, [instr_text,x]
elif 0x23<=instr_binary<=0x24: # get_global, etc
idx,x=spec_binary_globalidx(raw,idx)
return idx, [instr_text,x]
elif 0x28<=instr_binary<=0x3e: # i32.load, i64.store, etc
idx,m = spec_binary_memarg(raw,idx)
return idx, [instr_text,m]
elif 0x3f<=instr_binary<=0x40: # current_memory, grow_memory
if raw[idx]!=0x00: raise Exception("malformed")
return idx+1, [instr_text,]
elif 0x41<=instr_binary<=0x42: # i32.const, etc
n=0
if instr_text=="i32.const":
idx,n = spec_binary_iN(raw,idx,32)
if instr_text=="i64.const":
idx,n = spec_binary_iN(raw,idx,64)
return idx, [instr_text,n]
elif 0x43<=instr_binary<=0x44: # f32.const, etc
z=0
if instr_text=="f32.const":
if len(raw)<=idx+4: raise Exception("malformed")
idx,z = spec_binary_fN(raw,idx,32)
if instr_text=="f64.const":
if len(raw)<=idx+8: raise Exception("malformed")
idx,z = spec_binary_fN(raw,idx,64)
return idx, [instr_text,z]
else:
#otherwise no immediate
return idx, [instr_text,]
def spec_binary_instr_inv(node):
instr_bytes = bytearray()
#print("spec_binary_instr_inv(",node,")")
if type(node[0])==str:
instr_bytes+=bytearray([opcodes_text2binary[node[0]]])
#the rest is for immediates
if node[0] in {"block","loop","if"}: #block, loop, if
instr_bytes+=spec_binary_blocktype_inv(node[1])
instar_bytes=bytearray()
for n in node[2][:-1]:
instar_bytes+=spec_binary_instr_inv(n)
if len(node)==4: #if with else
instar_bytes+=bytearray([0x05])
for n in node[3][:-1]:
instar_bytes+=spec_binary_instr_inv(n)
instar_bytes+=bytes([0x0b])
instr_bytes+=instar_bytes
elif node[0] in {"br","br_if"}: #br, br_if
instr_bytes+=spec_binary_labelidx_inv(node[1])
elif node[0] == "br_table": #br_table
instr_bytes+=spec_binary_vec_inv(node[1],spec_binary_labelidx_inv)
instr_bytes+=spec_binary_labelidx_inv(node[2])
elif node[0] == "call": #call
instr_bytes+=spec_binary_funcidx_inv(node[1])
elif node[0] == "call_indirect": #call_indirect
instr_bytes+=spec_binary_typeidx_inv(node[1])
instr_bytes+=bytearray([0x00])
elif 0x20<=opcodes_text2binary[node[0]]<=0x24: #get_local, set_local, tee_local
instr_bytes+=spec_binary_localidx_inv(node[1])
elif 0x20<=opcodes_text2binary[node[0]]<=0x24: #get_global, set_global
instr_bytes+=spec_binary_globalidx_inv(node[1])
elif 0x28<=opcodes_text2binary[node[0]]<=0x3e: #i32.load, i32.load8_s, i64.store, etc
instr_bytes+=spec_binary_memarg_inv(node[1])
elif 0x3f<=opcodes_text2binary[node[0]]<=0x40: #memory.size, memory.grow
instr_bytes+=bytearray([0x00])
elif node[0]=="i32.const": #i32.const
instr_bytes+=spec_binary_iN_inv(node[1],32)
elif node[0]=="i64.const": #i64.const
instr_bytes+=spec_binary_iN_inv(node[1],64)
elif node[0]=="f32.const": #i64.const
instr_bytes+=spec_binary_fN_inv(node[1],32)
elif node[0]=="f64.const": #i64.const
instr_bytes+=spec_binary_fN_inv(node[1],64)
return instr_bytes
# 5.4.6 EXPRESSIONS
def spec_binary_expr(raw,idx):
if verbose: print("spec_binary_expr(",idx,")")
instar = []
while raw[idx] != 0x0b:
idx,ins = spec_binary_instr(raw,idx)
instar+=[ins]
if raw[idx] != 0x0b: return idx,None #error
return idx+1, instar +[['end']]
def spec_binary_expr_inv(node):
instar_bytes=bytearray()
for n in node:
instar_bytes+=spec_binary_instr_inv(n)
#instar_bytes+=bytes([0x0b])
return instar_bytes
#############
# 5.5 MODULES
#############
# 5.5.1 INDICES
def spec_binary_typeidx(raw,idx):
if verbose: print("spec_binary_typeidx(",idx,")")
idx, x = spec_binary_uN(raw,idx,32)
return idx,x
def spec_binary_typeidx_inv(node):
return spec_binary_uN_inv(node,32)
def spec_binary_funcidx(raw,idx):
if verbose: print("spec_binary_funcidx(",idx,")")
idx,x = spec_binary_uN(raw,idx,32)
return idx,x
def spec_binary_funcidx_inv(node):
return spec_binary_uN_inv(node,32)
def spec_binary_tableidx(raw,idx):
if verbose: print("spec_binary_tableidx(",idx,")")
idx,x = spec_binary_uN(raw,idx,32)
return idx,x
def spec_binary_tableidx_inv(node):
return spec_binary_uN_inv(node,32)
def spec_binary_memidx(raw,idx):
if verbose: print("spec_binary_memidx(",idx,")")
idx,x = spec_binary_uN(raw,idx,32)
return idx,x
def spec_binary_memidx_inv(node):
return spec_binary_uN_inv(node,32)
def spec_binary_globalidx(raw,idx):
if verbose: print("spec_binary_globalidx(",idx,")")
idx,x = spec_binary_uN(raw,idx,32)
return idx,x
def spec_binary_globalidx_inv(node):
return spec_binary_uN_inv(node,32)
def spec_binary_localidx(raw,idx):
if verbose: print("spec_binary_localidx(",idx,")")
idx,x = spec_binary_uN(raw,idx,32)
return idx,x
def spec_binary_localidx_inv(node):
return spec_binary_uN_inv(node,32)
def spec_binary_labelidx(raw,idx):
if verbose: print("spec_binary_labelidx(",idx,")")
idx,l = spec_binary_uN(raw,idx,32)
return idx,l
def spec_binary_labelidx_inv(node):
return spec_binary_uN_inv(node,32)
# 5.5.2 SECTIONS
def spec_binary_sectionN(raw,idx,N,B,skip):
if verbose: print("spec_binary_sectionN(",idx,")")
if idx>=len(raw):
return idx,[] #already at end
if raw[idx]!=N:
return idx, [] #this sec not included
idx+=1
idx,size = spec_binary_uN(raw,idx,32)
idx_plus_size = idx+size
if skip:
return idx+size,[]
if N==0: # custom section
idx, ret = B(raw,idx,idx+size)
elif N==8: # start section
idx, ret = B(raw,idx)
else:
idx,ret = spec_binary_vec(raw,idx,B)
if idx != idx_plus_size: raise Exception("malformed")
return idx,ret
def spec_binary_sectionN_inv(cont,Binv,N):
if cont==None or cont==[]:
return bytearray([])
N_bytes=bytearray([N])
cont_bytes=bytearray()
if N==8: #startsec
cont_bytes=Binv(cont)
else:
cont_bytes=spec_binary_vec_inv(cont,Binv)
size_bytes=spec_binary_uN_inv(len(cont_bytes),32)
return N_bytes+size_bytes+cont_bytes
# 5.5.3 CUSTOM SECTION
def spec_binary_customsec(raw,idx,skip):
if verbose: print("spec_binary_customsec(",idx,")")
customsecstar = []
idx,customsec = spec_binary_sectionN(raw,idx,0,spec_binary_custom,skip)
return idx,customsec
def spec_binary_custom(raw,idx,endidx):
if verbose: print("spec_binary_custom(",idx,")")
bytestar=bytearray()
idx,name = spec_binary_name(raw,idx)
while idx<endidx:
idx,byte = spec_binary_byte(raw,idx)
bytestar+=bytearray([byte])
if idx!=endidx:
idx+=1
return idx,[name,bytestar]
def spec_binary_customsec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_custom_inv)
def spec_binary_custom_inv(node):
return spec_binary_name_inv(node[0]) + spec_binary_byte_inv(node[1]) #check this
# 5.5.4 TYPE SECTION
def spec_binary_typesec(raw,idx,skip=0):
if verbose: print("spec_binary_typesec(",idx,")")
return spec_binary_sectionN(raw,idx,1,spec_binary_functype,skip)
def spec_binary_typesec_inv(node):
#print("spec_binary_typesec_inv(",node,")")
return spec_binary_sectionN_inv(node,spec_binary_functype_inv,1)
# 5.5.5 IMPORT SECTION
def spec_binary_importsec(raw,idx,skip=0):
if verbose: print("spec_binary_importsec(",idx,")")
return spec_binary_sectionN(raw,idx,2,spec_binary_import,skip)
def spec_binary_import(raw,idx):
if verbose: print("spec_binary_import(",idx,")")
idx,mod = spec_binary_name(raw,idx)
idx,nm = spec_binary_name(raw,idx)
idx,d = spec_binary_importdesc(raw,idx)
return idx,{"module":mod,"name":nm,"desc":d}
def spec_binary_importdesc(raw,idx):
if verbose: print("spec_binary_importdesc(",idx,")")
if raw[idx]==0x00:
idx,x=spec_binary_typeidx(raw,idx+1)
return idx,["func",x]
elif raw[idx]==0x01:
idx,tt=spec_binary_tabletype(raw,idx+1)
return idx,["table",tt]
elif raw[idx]==0x02:
idx,mt=spec_binary_memtype(raw,idx+1)
return idx,["mem",mt]
elif raw[idx]==0x03:
idx,gt=spec_binary_globaltype(raw,idx+1)
return idx,["global",gt]
else:
return idx,None #error
def spec_binary_importsec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_import_inv,2)
def spec_binary_import_inv(node):
return spec_binary_name_inv(node["module"]) + spec_binary_name_inv(node["name"]) + spec_binary_importdesc_inv(node["desc"])
def spec_binary_importdesc_inv(node):
key=node[0]
if key=="func":
return bytearray([0x00]) + spec_binary_typeidx_inv(node[1])
elif key=="table":
return bytearray([0x01]) + spec_binary_tabletype_inv(node[1])
elif key=="mem":
return bytearray([0x02]) + spec_binary_memtype_inv(node[1])
elif key=="global":
return bytearray([0x03]) + spec_binary_globaltype_inv(node[1])
else:
return bytearray()
# 5.5.6 FUNCTION SECTION
def spec_binary_funcsec(raw,idx,skip=0):
if verbose: print("spec_binary_funcsec(",idx,")")
return spec_binary_sectionN(raw,idx,3,spec_binary_typeidx,skip)
def spec_binary_funcsec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_typeidx_inv,3)
# 5.5.7 TABLE SECTION
def spec_binary_tablesec(raw,idx,skip=0):
if verbose: print("spec_binary_tablesec(",idx,")")
return spec_binary_sectionN(raw,idx,4,spec_binary_table,skip)
def spec_binary_table(raw,idx):
if verbose: print("spec_binary_table(",idx,")")
idx,tt=spec_binary_tabletype(raw,idx)
return idx,{"type":tt}
def spec_binary_tablesec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_table_inv,4)
def spec_binary_table_inv(node):
return spec_binary_tabletype_inv(node["type"])
# 5.5.8 MEMORY SECTION
def spec_binary_memsec(raw,idx,skip=0):
if verbose: print("spec_binary_memsec(",idx,")")
return spec_binary_sectionN(raw,idx,5,spec_binary_mem,skip)
def spec_binary_mem(raw,idx):
if verbose: print("spec_binary_mem(",idx,")")
idx,mt = spec_binary_memtype(raw,idx)
return idx,{"type":mt}
def spec_binary_memsec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_mem_inv,5)
def spec_binary_mem_inv(node):
return spec_binary_memtype_inv(node["type"])
# 5.5.9 GLOBAL SECTION
def spec_binary_globalsec(raw,idx,skip=0):
if verbose: print("spec_binary_globalsec(",idx,")")
return spec_binary_sectionN(raw,idx,6,spec_binary_global,skip)
def spec_binary_global(raw,idx):
if verbose: print("spec_binary_global(",idx,")")
idx,gt=spec_binary_globaltype(raw,idx)
idx,e=spec_binary_expr(raw,idx)
return idx,{"type":gt,"init":e}
def spec_binary_globalsec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_global_inv,6)
def spec_binary_global_inv(node):
return spec_binary_globaltype_inv(node["type"]) + spec_binary_expr_inv(node["init"])
# 5.5.10 EXPORT SECTION
def spec_binary_exportsec(raw,idx,skip=0):
if verbose: print("spec_binary_exportsec(",idx,")")
return spec_binary_sectionN(raw,idx,7,spec_binary_export,skip)
def spec_binary_export(raw,idx):
if verbose: print("spec_binary_export(",idx,")")
idx,nm = spec_binary_name(raw,idx)
#print("nm",nm)
idx,d = spec_binary_exportdesc(raw,idx)
#print("d",d)
return idx,{"name":nm,"desc":d}
def spec_binary_exportdesc(raw,idx):
if verbose: print("spec_binary_exportdesc(",idx,")")
if raw[idx]==0x00:
idx,x=spec_binary_funcidx(raw,idx+1)
return idx,["func",x]
elif raw[idx]==0x01:
idx,x=spec_binary_tableidx(raw,idx+1)
return idx,["table",x]
elif raw[idx]==0x02:
idx,x=spec_binary_memidx(raw,idx+1)
return idx,["mem",x]
elif raw[idx]==0x03:
idx,x=spec_binary_globalidx(raw,idx+1)
return idx,["global",x]
else:
return idx,None #error
def spec_binary_exportsec_inv(node):
return spec_binary_sectionN_inv(node,spec_binary_export_inv,7)
def spec_binary_export_inv(node):