-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathCodeValidator.v3
1773 lines (1733 loc) · 61.2 KB
/
CodeValidator.v3
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
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// Reusable validator that checks well-formedness of Wasm functions' code.
def OUT = Trace.OUT;
class CodeValidator(extensions: Extension.set, limits: Limits, module: Module, err: ErrorGen) {
def codeptr = CodePtr.new(null);
def parser = WasmParser.new(extensions, limits, module, err, codeptr);
def instrTracer = if(Trace.validation, InstrTracer.new());
def locals = Vector<ValueType>.new();
def val_stack = ArrayStack<ValueType>.new();
def init_status = Vector<InitStatus>.new();
def ctl_stack = ArrayStack<ControlEntry>.new();
def ctlxfer = SidetableBuilder.new();
def ex_handlers = Vector<ExHandlerEntry>.new();
def suspend_handlers = Vector<ExHandlerEntry>.new();
def switch_handlers = Vector<ExHandlerEntry>.new();
def frame_var_tags = Vector<byte>.new();
var ctl_top: ControlEntry; // FAST: cache of top of control stack
var func_start_pos: int;
var func: FuncDecl;
var sig: SigDecl;
var opcode_pos: int;
var validatingInitExpr = false;
var init_stack: ArrayStack<InitExpr>;
var probe: (CodeValidator, int, Opcode) -> void;
new() {
codeptr.onError = err.onDataReaderError;
}
def validateFunc(f: FuncDecl) -> CodeValidatorResult {
codeptr.reset(f.orig_bytecode, 0, f.orig_bytecode.length);
return validate(f, codeptr);
}
def validate(func: FuncDecl, d: DataReader) -> CodeValidatorResult {
return Metrics.validate_time_us.run(validateFunc0, (func, d));
}
def validateInitExpr(expected: SigDecl, d: DataReader) -> CodeValidatorResult {
return Metrics.validate_time_us.run(validateExpr0, (expected, d));
}
private def validateFunc0(func: FuncDecl, d: DataReader) -> CodeValidatorResult {
// Reset internal state.
if (Trace.validation) OUT.put2("==== begin validate: func %q: %q ========================", func.render(module.names, _), func.sig.render).ln();
this.func = func;
resetSig(func.sig);
resetCode(d);
// Read and initialize locals.
if (!readLocals(locals)) return reterr();
func.num_locals = u16.!(locals.length);
// Push initial control.
opcode_pos = codeptr.pos;
pushControl(Opcode.UNREACHABLE.code, sig.params, sig.results, 0);
// Run validation.
validatingInitExpr = false;
validateCode();
// Check function body is properly terminated.
if (ctl_stack.top != 0 && err.ok()) {
// TODO: double-check this error position
err.rel(codeptr, func.orig_bytecode.length).UnterminatedFunctionBody();
}
// Extract sidetable.
func.sidetable = ctlxfer.extract();
Debug.keepAlive(func.sidetable.entries);
if (ex_handlers.length > 0) func.handlers = ex_handlers.extract();
if (suspend_handlers.length > 0) func.suspend_handlers = suspend_handlers.extract();
if (switch_handlers.length > 0) func.switch_handlers = switch_handlers.extract();
if (frame_var_tags.length > 0) func.frame_var_tags = frame_var_tags.extract();
// Report metrics and return success.
if (err.ok()) {
// Metric collection.
Metrics.validate_bytes.val += u32.view(func.orig_bytecode.length);
// Return success.
return CodeValidatorResult.Ok;
}
return reterr();
}
private def validateExpr0(sig: SigDecl, d: DataReader) -> CodeValidatorResult {
if (Trace.validation) OUT.put1("validate(expr: %q)", sig.render).ln();
resetSig(sig);
resetCode(d);
opcode_pos = codeptr.pos;
pushControl(Opcode.UNREACHABLE.code, sig.params, sig.results, 0);
if (init_stack == null) init_stack = ArrayStack.new();
else init_stack.top = 0;
// Run validation.
validatingInitExpr = true;
validateCode();
d.reset(codeptr.data, codeptr.pos, d.limit);
if (ctl_stack.top != 0 && err.ok()) {
err.rel(codeptr, codeptr.pos).UnterminatedInitExpr();
}
if (err.ok()) {
// Metric collection.
Metrics.validate_bytes.val += u32.view(d.pos - func_start_pos);
// Return success.
return CodeValidatorResult.Ok;
}
return reterr();
}
private def resetSig(sig: SigDecl) {
this.sig = sig;
val_stack.clear();
locals.resize(0);
init_status.resize(0);
locals.puta(sig.params);
init_status.putn(InitStatus.INIT, sig.params.length);
}
private def resetCode(d: DataReader) {
codeptr.reset(d.data, d.pos, d.limit);
parser.reset(codeptr);
func_start_pos = d.pos;
ctl_stack.clear();
ctlxfer.reset(codeptr.pos);
ex_handlers.resize(0);
suspend_handlers.resize(0);
frame_var_tags.resize(0);
}
def readLocals(vec: Vector<ValueType>) -> bool {
var start = vec.length, max = limits.max_num_locals;
var dcount = parser.readU32("local decl count", max);
for (i < dcount) {
var pt = codeptr.pos;
var count = parser.readU32("local count", max);
var pt2 = codeptr.pos;
var ltype = parser.readValueType();
var init = if(ValueTypes.hasDefaultValue(ltype), InitStatus.INIT, InitStatus.UNINIT);
if (count <= limits.max_num_locals) init_status.putn(init, int.view(count));
if (!err.ok()) return false;
if (Trace.validation) traceLocals(count, ltype);
vec.putn(ltype, int.!(count));
var size = vec.length - start;
if (size > max) {
err.rel(codeptr, pt).QuantityExceededMaximumI("maximum total locals", size, max);
return false;
}
}
return true;
}
def reterr() -> CodeValidatorResult.Error {
return CodeValidatorResult.Error(err.error_code, int.!(err.error_pos), err.error_msg);
}
def traceLocals(count: u32, ltype: ValueType) {
OUT.put2(" locals %d: %q", count, ltype.render).ln();
}
def err_atpc() -> ErrorGen {
return err.rel(codeptr, opcode_pos);
}
def err_atpos(pos: int) -> ErrorGen {
return err.rel(codeptr, pos);
}
def validateCode() {
while (codeptr.pos < codeptr.limit) {
opcode_pos = codeptr.pos;
var opcode = codeptr.read_opcode();
if (probe != null) probe(this, opcode_pos, opcode);
if (Trace.validation) { traceOpcode(); traceStack(true); }
// FAST: Handle short operators (predictable direct branch)
if (Opcodes.attributes[opcode.tag].SHORT_OP) {
checkSignature(opcode.sig);
if (Trace.validation) traceStack(false);
if (validatingInitExpr && !Opcodes.isConstant(extensions, opcode)) err_atpc().UnexpectedOpcodeInInit(opcode.prefix, opcode.code);
if(validatingInitExpr) {
def arg2 = init_stack.pop();
def arg1 = init_stack.pop();
match (opcode) {
I32_ADD => init_stack.push(InitExpr.I32_ADD(arg1, arg2));
I32_SUB => init_stack.push(InitExpr.I32_SUB(arg1, arg2));
I32_MUL => init_stack.push(InitExpr.I32_MUL(arg1, arg2));
I64_ADD => init_stack.push(InitExpr.I64_ADD(arg1, arg2));
I64_SUB => init_stack.push(InitExpr.I64_SUB(arg1, arg2));
I64_MUL => init_stack.push(InitExpr.I64_MUL(arg1, arg2));
_ => {}
}
}
continue;
}
// Handle all other operators in the switch (indirect branch)
match (opcode) {
UNREACHABLE => {
setUnreachable();
}
BLOCK => {
var pr = parser.readBlockType();
checkArgsAndPushControl(opcode, pr.0, pr.1);
}
LOOP => {
var pr = parser.readBlockType();
checkArgsAndPushControl(opcode, pr.0, pr.1);
ctl_top.sidetable_pos = ctlxfer.sidetable.length;
}
IF => {
var pr = parser.readBlockType();
popE(ValueType.I32);
var ctl = checkArgsAndPushControl(opcode, pr.0, pr.1);
ctlxfer.refElse(ctl, opcode_pos);
ctl_top.reachable = true; // true block now reachable
}
ELSE => {
if (ctl_top.start_opcode != Opcode.IF.code) return err_atpc().MismatchedElse();
checkArgsAndTransfer();
resetInit();
// "else" implicitly goes to end
ctlxfer.ref0(ctl_top, opcode_pos);
// branch from "if" will go to instruction after else
ctlxfer.bindElse(ctl_top, opcode_pos + 1);
ctl_top.start_opcode = opcode.code;
ctl_top.reachable = true;
val_stack.top = ctl_top.val_stack_top;
pushTypes(ctl_top.params);
}
TRY => {
if (!checkExtension(Extension.LEGACY_EH, opcode)) return;
var pr = parser.readBlockType();
checkArgsAndPushControl(opcode, pr.0, pr.1);
ctl_top.sidetable_pos = ctlxfer.sidetable.length;
ctl_top.try_end = -1;
ctl_top.delegate_pos = opcode_pos + 1;
}
CATCH => {
if (!checkExtension(Extension.LEGACY_EH, opcode)) return;
if (ctl_top.start_opcode != Opcode.TRY.code && ctl_top.start_opcode != Opcode.CATCH.code) {
return err_atpc().MismatchedCatch();
}
var range = startExHandler();
ctl_top.start_opcode = opcode.code;
ctl_top.delegate_pos = opcode_pos;
var tag = parser.readTagRef();
if (tag == null) return;
ctlxfer.ref0(ctl_top, opcode_pos); // implicit jump to end of try block
var ex_slot = findExSlot(null).0;
var info = ExHandlerInfo.Direct(codeptr.pos - func_start_pos, ex_slot, ctl_top.val_stack_top, ctlxfer.sidetable.length);
ex_handlers.put(ExHandlerEntry(tag.tag_index, range.0, range.1, info));
checkArgsAndTransfer();
resetInit();
val_stack.top = ctl_top.val_stack_top;
val_stack.pusha(tag.fields);
}
THROW => {
if (!checkExtension(Extension.EXCEPTION_HANDLING | Extension.LEGACY_EH, opcode)) return;
var tag = parser.readTagRef();
if (tag == null) return;
checkAndPopArgs(tag.fields);
setUnreachable();
}
RETHROW => {
if (!checkExtension(Extension.LEGACY_EH, opcode)) return;
var depth = parser.readLabel();
var target = getControl(depth);
var t = findExSlot(target), ex_slot = t.0, found = t.1;
if (!found || (target.start_opcode != Opcode.CATCH.code &&
target.start_opcode != Opcode.CATCH_ALL.code)) {
err_atpc().RethrowNotInCatch();
}
var popcount = val_stack.top - ctl_top.val_stack_top;
ctlxfer.rethrow(ex_slot, popcount);
setUnreachable();
}
THROW_REF => {
if (!checkExtension(Extension.EXCEPTION_HANDLING, opcode)) return;
popE(ValueTypes.EXNREF);
setUnreachable();
}
END => {
if (ctl_stack.top == 0) return err_atpc().EmptyControlStack();
checkArgsAndTransfer();
var ctl = ctl_stack.peek();
match (ctl.start_opcode) {
Opcode.LOOP.code => {
ctlxfer.bind(ctl, ctl.start_pos, ctl.sidetable_pos);
}
Opcode.IF.code => {
// one-armed if; simulate an empty else clause
val_stack.top = ctl.val_stack_top;
ctl.reachable = true;
pushTypes(ctl.params);
checkArgsAndTransfer();
ctlxfer.bindElse(ctl, opcode_pos);
ctlxfer.bind(ctl, opcode_pos, ctlxfer.sidetable.length);
}
Opcode.TRY_TABLE.code => {
// finish exception handler entries
var sidetable_pos = ctl.sidetable_pos;
ctlxfer.bind(ctl, opcode_pos, ctlxfer.sidetable.length);
for (i < ctl.catches.length) {
var c = ctl.catches[i];
var sidetable_entry = sidetable_pos + (i * Sidetable_CatchEntry.size / 4);
var info = ExHandlerInfo.Sidetable(c.exnref, sidetable_entry);
var tag_index = if(c.tag != null, c.tag.tag_index, Modules.EX_TAG_ALL);
ex_handlers.put(ExHandlerEntry(tag_index, ctl.start_pos - func_start_pos, opcode_pos - func_start_pos, info));
}
}
_ => {
ctlxfer.bind(ctl, opcode_pos, ctlxfer.sidetable.length);
}
}
resetInit();
ctl_stack.pop();
if (validatingInitExpr && ctl_stack.empty()) { // END finished the init expr
codeptr.reset(codeptr.data, codeptr.pos, codeptr.pos);
} else {
ctl_top = ctl_stack.peek();
}
}
BR => {
var depth = parser.readLabel();
var target = getControl(depth);
if (target == null) return;
ctlxfer.refS(target, opcode_pos, val_stack.top);
checkAndPopArgs(labelArgs(target));
setUnreachable();
}
BR_IF => {
var depth = parser.readLabel();
var target = getControl(depth);
if (target == null) return;
popE(ValueType.I32);
ctlxfer.refS(target, opcode_pos, val_stack.top);
var args = labelArgs(target);
checkAndPopArgs(args);
pushTypes(args);
}
BR_TABLE => {
var labels = parser.readLabels();
popE(ValueType.I32);
ctlxfer.sidetable.put(labels.length).put(0).put(0).put(0);
// add refs for all labels and check args
var arity = -1;
for (i < labels.length) {
if (err.error()) return;
var target = getControl(labels[i]);
if (target == null) return;
var args = labelArgs(target);
if (arity < 0) arity = args.length;
else if (arity != args.length) err_atpc().BrTableArityMismatch(arity, i, args.length);
checkTargetArgs(target);
ctlxfer.refS(target, opcode_pos + i + 1, val_stack.top);
}
setUnreachable();
}
RETURN => {
checkAndPopArgs(sig.results);
setUnreachable();
}
CALL => {
var func = parser.readFuncRef();
if (func == null) return;
checkSignature(func.sig);
}
CALL_INDIRECT => {
var sig = parser.readSigRef();
var table = parser.readTableRef();
if (table == null) return;
if (!ValueTypes.isAssignable(table.elemtype, ValueTypes.FUNCREF)) err_atpc().IllegalTableTypeForIndirectCall(table.elemtype);
popE(table.size.indexType());
if (sig == null) return;
checkSignature(sig);
}
RETURN_CALL => {
if (!checkExtension(Extension.TAIL_CALL, opcode)) return;
var func = parser.readFuncRef();
if (func == null) return;
checkAndPopArgs(func.sig.params);
checkReturnSig(func.sig.results);
setUnreachable();
}
RETURN_CALL_INDIRECT => {
if (!checkExtension(Extension.TAIL_CALL, opcode)) return;
var sig = parser.readSigRef();
var table = parser.readTableRef();
if (table == null) return;
if (!ValueTypes.isAssignable(table.elemtype, ValueTypes.FUNCREF)) err_atpc().IllegalTableTypeForIndirectCall(table.elemtype);
popE(table.size.indexType());
if (sig == null) return;
checkAndPopArgs(sig.params);
checkReturnSig(sig.results);
setUnreachable();
}
CALL_REF => {
if (!checkExtension(Extension.FUNCTION_REFERENCES, opcode)) return;
var sig = parser.readSigRef();
if (sig == null) return;
popE(ValueTypes.Ref(true, sig)); // XXX: avoid allocation
checkSignature(sig);
}
RETURN_CALL_REF => {
if (!checkExtension(Extension.FUNCTION_REFERENCES, opcode)) return;
var sig = parser.readSigRef();
if (sig == null) return;
popE(ValueTypes.Ref(true, sig)); // XXX: avoid allocation
checkAndPopArgs(sig.params);
checkReturnSig(sig.results);
setUnreachable();
}
DELEGATE => {
if (!checkExtension(Extension.LEGACY_EH, opcode)) return;
if (ctl_top.start_opcode != Opcode.TRY.code) return err_atpc().MismatchedDelegate();
var range = startExHandler();
var depth = parser.readLabel();
var target = getControl(depth + 1);
if (target == null) return;
checkArgsAndTransfer();
var info = ExHandlerInfo.Direct(target.delegate_pos - func_start_pos, 0, ctl_top.val_stack_top, ctlxfer.sidetable.length);
ex_handlers.put(ExHandlerEntry(Modules.EX_TAG_DELEGATE, range.0, range.1, info));
ctlxfer.bind(ctl_top, opcode_pos, ctlxfer.sidetable.length);
resetInit();
ctl_stack.pop();
ctl_top = ctl_stack.peek();
}
CATCH_ALL => {
if (!checkExtension(Extension.LEGACY_EH, opcode)) return;
if (ctl_top.start_opcode != Opcode.TRY.code && ctl_top.start_opcode != Opcode.CATCH.code) {
return err_atpc().MismatchedCatch();
}
ctl_top.start_opcode = opcode.code;
ctl_top.delegate_pos = opcode_pos;
var range = startExHandler();
checkArgsAndTransfer();
ctlxfer.ref0(ctl_top, opcode_pos); // implicit jump to end of try block
var ex_slot = findExSlot(null).0;
var info = ExHandlerInfo.Direct(codeptr.pos - func_start_pos, ex_slot, ctl_top.val_stack_top, ctlxfer.sidetable.length);
ex_handlers.put(ExHandlerEntry(Modules.EX_TAG_ALL, range.0, range.1, info));
resetInit();
val_stack.top = ctl_top.val_stack_top;
}
DROP => {
popAny();
}
SELECT => {
popE(ValueType.I32);
var t = popAny();
var rt = t.0;
if (!ValueTypes.isNumeric(rt)) err_atpc().IllegalSelectType(rt);
popE(rt);
push(rt);
}
SELECT_T => {
var at = parser.readValueTypes("select count", limits.max_num_select_results);
if (at.length == 0) err_atpc().IllegalSelectCount();
popE(ValueType.I32);
checkAndPopArgs(at);
checkAndPopArgs(at);
pushTypes(at);
}
TRY_TABLE => {
var pr = parser.readBlockType();
var catches = parser.readCatches();
if (catches == null) return;
var sidetable_pos = ctlxfer.sidetable.length;
for (i < catches.length) {
var c = catches[i];
var target = getControl(c.depth);
if (target == null) return;
checkTargetCatch(i, c, target);
ctlxfer.refC(target, c.tag, c.exnref, "refC");
}
checkArgsAndPushControl(opcode, pr.0, pr.1);
ctl_top.catches = catches;
ctl_top.sidetable_pos = sidetable_pos;
ctl_top.try_end = -1;
ctl_top.delegate_pos = opcode_pos + 1;
}
LOCAL_GET => {
var index = parser.readLocalIndex();
var t = getLocalType(index);
checkInit(index);
push(t);
}
LOCAL_SET => {
var index = parser.readLocalIndex();
var t = getLocalType(index);
setInit(index);
popE(t);
}
LOCAL_TEE => {
var index = parser.readLocalIndex();
var t = getLocalType(index);
setInit(index);
popE(t);
push(t);
}
GLOBAL_GET => {
var g = parser.readGlobalRef();
if (g == null) return;
if (validatingInitExpr) {
if (g.mutable) err_atpc().ExpectedImmutableGlobalInInit(g);
else if (g.imp == null && !extensions.GC) err_atpc().ExpectedImportedGlobalInInit(g);
init_stack.push(InitExpr.Global(g.global_index, g));
}
push(g.valtype);
}
GLOBAL_SET => {
var g = parser.readGlobalRef();
if (g == null) return;
if (!g.mutable) {
err_atpc().IllegalAssignmentToImmutableGlobal(g.global_index);
}
popE(g.valtype);
}
TABLE_GET => {
var table = parser.readTableRef();
if (table == null) return;
popE(table.size.indexType());
push(table.elemtype);
}
TABLE_SET => {
var table = parser.readTableRef();
if (table == null) return;
popE(table.elemtype);
popE(table.size.indexType());
}
I32_LOAD8_S,
I32_LOAD8_U => checkLoad(opcode, 0, ValueType.I32);
I64_LOAD8_S,
I64_LOAD8_U => checkLoad(opcode, 0, ValueType.I64);
I32_STORE8 => checkStore(opcode, 0, ValueType.I32);
I64_STORE8 => checkStore(opcode, 0, ValueType.I64);
I32_LOAD16_S,
I32_LOAD16_U => checkLoad(opcode, 1, ValueType.I32);
I64_LOAD16_S,
I64_LOAD16_U => checkLoad(opcode, 1, ValueType.I64);
I32_STORE16 => checkStore(opcode, 1, ValueType.I32);
I64_STORE16 => checkStore(opcode, 1, ValueType.I64);
I32_LOAD => checkLoad(opcode, 2, ValueType.I32);
F32_LOAD => checkLoad(opcode, 2, ValueType.F32);
I64_LOAD32_S,
I64_LOAD32_U => checkLoad(opcode, 2, ValueType.I64);
I32_STORE => checkStore(opcode, 2, ValueType.I32);
F32_STORE => checkStore(opcode, 2, ValueType.F32);
I64_STORE32 => checkStore(opcode, 2, ValueType.I64);
I64_LOAD => checkLoad(opcode, 3, ValueType.I64);
F64_LOAD => checkLoad(opcode, 3, ValueType.F64);
I64_STORE => checkStore(opcode, 3, ValueType.I64);
F64_STORE => checkStore(opcode, 3, ValueType.F64);
MEMORY_SIZE => {
var dst = parser.readMemoryRef(extensions.MULTI_MEMORY);
if (dst == null) return;
var dit = dst.size.indexType();
push(dit);
}
MEMORY_GROW => {
var dst = parser.readMemoryRef(extensions.MULTI_MEMORY);
if (dst == null) return;
var dit = dst.size.indexType();
popE(dit);
push(dit);
}
I32_CONST => {
var val = codeptr.read_sleb32();
push(ValueType.I32);
if (validatingInitExpr) init_stack.push(InitExpr.I32(val));
}
I64_CONST => {
var val = codeptr.read_sleb64();
push(ValueType.I64);
if (validatingInitExpr) init_stack.push(InitExpr.I64(val));
}
F32_CONST => {
var val = codeptr.read_u32();
push(ValueType.F32);
if (validatingInitExpr) init_stack.push(InitExpr.F32(val));
}
F64_CONST => {
var val = codeptr.read_u64();
push(ValueType.F64);
if (validatingInitExpr) init_stack.push(InitExpr.F64(val));
}
REF_NULL => {
var ht = parser.readHeapType();
push(ValueType.Ref(true, ht));
if (validatingInitExpr) init_stack.push(InitExpr.RefNull(ht));
}
REF_IS_NULL => {
popRef();
push(ValueType.I32);
}
REF_FUNC => {
var func = parser.readFuncRef();
if (func == null) return;
if (validatingInitExpr) func.reffed = true;
else if (!func.reffed) err_atpc().IllegalFuncRef(func);
var ftype = if(extensions.FUNCTION_REFERENCES,
ValueTypes.RefFunc(false, func.sig),
ValueTypes.FUNCREF);
push(ftype);
if (validatingInitExpr) init_stack.push(InitExpr.FuncRef(func.func_index, func));
}
REF_AS_NON_NULL => {
if (!checkExtension(Extension.FUNCTION_REFERENCES, opcode)) return;
var t = popAny();
if (t.1) push(asNonNullRefType(t.0));
}
BR_ON_NULL,
BR_ON_NON_NULL => {
if (!checkExtension(Extension.FUNCTION_REFERENCES, opcode)) return;
var depth = parser.readLabel();
var target = getControl(depth);
if (target == null) return;
var t = popAny();
if (t.1) {
var rt = t.0, nonnull = asNonNullRefType(rt);
if (opcode.code == Opcode.BR_ON_NON_NULL.code) {
push(nonnull);
ctlxfer.refS(target, opcode_pos, val_stack.top);
var args = labelArgs(target);
checkAndPopArgs(args);
pushTypes(args);
popAny();
} else {
ctlxfer.refS(target, opcode_pos, val_stack.top);
var args = labelArgs(target);
checkAndPopArgs(args);
pushTypes(args);
push(nonnull);
}
}
}
REF_EQ => {
if (noGC(opcode)) return;
popE(ValueTypes.EQREF);
popE(ValueTypes.EQREF);
push(ValueType.I32);
}
STRUCT_NEW => {
if (noGC(opcode)) return;
var st = parser.readStructType();
if (st == null) return;
checkAndPopFields(st.field_types);
push(ValueTypes.RefStruct(false, st));
if (validatingInitExpr) {
var vals = Array<InitExpr>.new(st.field_types.length);
for (i = vals.length - 1; i >= 0; i--) vals[i] = init_stack.pop();
var ht = HeapType.Struct(st);
init_stack.push(InitExpr.Struct(ht, vals));
}
}
STRUCT_NEW_DEFAULT => {
if (noGC(opcode)) return;
var st = parser.readStructType();
if (st == null) return;
var stt = ValueTypes.RefStruct(false, st);
if (!st.defaultable) err_atpc().ExpectedDefaultableHeapType(stt);
push(stt);
if (validatingInitExpr) {
var vals = Array<InitExpr>.new(st.field_types.length);
for (i < vals.length) vals[i] = InitExpr.Const(Values.default(st.field_types[i].valtype));
var ht = HeapType.Struct(st);
init_stack.push(InitExpr.Struct(ht, vals));
}
}
STRUCT_GET => {
if (noGC(opcode)) return;
var st = parser.readStructType();
var index = parser.readFieldIndex(st);
if (index < 0) return;
popE(ValueTypes.RefStruct(true, st));
var ft = st.field_types[index];
if (ft.pack != Packedness.UNPACKED) err_atpc().ExpectedUnpackedType(ft);
push(ft.valtype);
}
STRUCT_GET_S, // fallthrough
STRUCT_GET_U => {
if (noGC(opcode)) return;
var st = parser.readStructType();
var index = parser.readFieldIndex(st);
if (index < 0) return;
popE(ValueTypes.RefStruct(true, st));
var ft = st.field_types[index];
if (ft.pack == Packedness.UNPACKED) err_atpc().ExpectedPackedType(ft);
push(ft.valtype);
}
STRUCT_SET => {
if (noGC(opcode)) return;
var st = parser.readStructType();
var index = parser.readFieldIndex(st);
if (index < 0) return;
var ft = st.field_types[index];
var stt = ValueTypes.RefStruct(true, st);
if (!ft.mutable) {
err_atpc().IllegalAssignmentToImmutableField(stt, u32.view(index));
}
popE(ft.valtype);
popE(stt);
}
ARRAY_NEW => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
popE(ValueType.I32);
checkAndPopFields(at.elem_types);
push(ValueTypes.RefArray(false, at));
if (validatingInitExpr) {
var len = init_stack.pop();
var elem = init_stack.pop();
var ht = HeapType.Array(at);
init_stack.push(InitExpr.Array(ht, len, elem));
}
}
ARRAY_NEW_DEFAULT => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
var att = ValueTypes.RefArray(false, at);
if (!at.defaultable) err_atpc().ExpectedDefaultableHeapType(att);
popE(ValueType.I32);
push(att);
if (validatingInitExpr) {
var len = init_stack.pop();
var elem = InitExpr.Const(Values.default(at.elem_types[0].valtype));
var ht = HeapType.Array(at);
init_stack.push(InitExpr.Array(ht, len, elem));
}
}
ARRAY_GET => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
popE(ValueType.I32);
popE(ValueTypes.RefArray(true, at));
for (et in at.elem_types) {
if (et.pack != Packedness.UNPACKED) err_atpc().ExpectedUnpackedType(et);
push(et.valtype);
}
}
ARRAY_GET_S, // fallthrough
ARRAY_GET_U => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
popE(ValueType.I32);
popE(ValueTypes.RefArray(true, at));
for (et in at.elem_types) {
if (et.pack == Packedness.UNPACKED) err_atpc().ExpectedPackedType(et);
push(et.valtype);
}
}
ARRAY_SET => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (!checkArrayIsMutable(at, true)) return;
popE(ValueType.I32);
popE(ValueTypes.RefArray(true, at));
}
ARRAY_LEN => {
if (noGC(opcode)) return;
popE(ValueTypes.ARRAYREF);
push(ValueType.I32);
}
ARRAY_FILL => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (!checkArrayIsMutable(at, false)) return;
popE(ValueType.I32);
for (et in at.elem_types) {
popE(et.valtype);
}
popE(ValueType.I32);
popE(ValueType.Ref(true, HeapType.Array(at)));
}
ARRAY_COPY => {
if (noGC(opcode)) return;
var at1 = parser.readArrayType(), at2 = parser.readArrayType();
if (!checkArrayIsMutable(at1, false) || at2 == null) return;
for (i < at1.elem_types.length) {
var dst = at1.elem_types[i], src = at2.elem_types[i];
if (dst.pack != src.pack || !ValueTypes.isAssignable(dst.valtype, src.valtype)) err_atpc().ElementTypeMismatch2(dst, src);
}
popE(ValueType.I32); // size
popE(ValueType.I32); // src_offset
popE(ValueType.Ref(true, HeapType.Array(at2))); // src
popE(ValueType.I32); // dst_offset
popE(ValueType.Ref(true, HeapType.Array(at1))); // dst
}
ARRAY_INIT_DATA => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
var index = parser.readDataIndex();
if (!checkArrayIsMutable(at, false)) return;
if (!checkArrayIsPrimitive(at, false)) return;
popE(ValueType.I32);
popE(ValueType.I32);
popE(ValueType.I32);
popE(ValueType.Ref(true, HeapType.Array(at)));
}
ARRAY_INIT_ELEM => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
var elem = parser.readElemRef();
if (elem == null) return;
if (!checkArrayIsMutable(at, false)) return;
if (!ValueTypes.isAssignable(elem.elemtype, at.elem_types[0].valtype)) {
err_atpc().ElementTypeMismatch(at.elem_types[0].valtype, elem.elemtype);
}
popE(ValueType.I32);
popE(ValueType.I32);
popE(ValueType.I32);
popE(ValueTypes.RefArray(true, at));
}
ARRAY_NEW_FIXED => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
var size = codeptr.read_uleb31();
for (i < size) checkAndPopFields(at.elem_types);
push(ValueTypes.RefArray(false, at));
if (validatingInitExpr) {
var vals = Array<InitExpr>.new(size);
for (i = vals.length - 1; i >= 0; i--) vals[i] = init_stack.pop();
init_stack.push(InitExpr.FixedArray(HeapType.Array(at), vals));
}
}
ARRAY_NEW_DATA => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
var index = parser.readDataIndex();
if (!checkArrayIsPrimitive(at, false)) return;
popE(ValueType.I32);
popE(ValueType.I32);
push(ValueTypes.RefArray(false, at));
if (validatingInitExpr) {
var len = init_stack.pop();
var offset = init_stack.pop();
init_stack.push(InitExpr.ArrayNewData(HeapType.Array(at), index, offset, len));
}
}
ARRAY_NEW_ELEM => {
if (noGC(opcode)) return;
var at = parser.readArrayType();
if (at == null) return;
var elem = parser.readElemRef();
if (elem == null) return;
if (!ValueTypes.isAssignable(elem.elemtype, at.elem_types[0].valtype)) {
err_atpc().ElementTypeMismatch(at.elem_types[0].valtype, elem.elemtype);
}
popE(ValueType.I32);
popE(ValueType.I32);
push(ValueTypes.RefArray(false, at));
if (validatingInitExpr) {
var len = init_stack.pop();
var offset = init_stack.pop();
init_stack.push(InitExpr.ArrayNewElem(HeapType.Array(at), elem.elem_index, offset, len));
}
}
REF_I31 => {
if (noGC(opcode)) return;
popE(ValueType.I32);
push(ValueTypes.I31REF_NONNULL);
if (validatingInitExpr) init_stack.push(InitExpr.I31(init_stack.pop()));
}
I31_GET_S => {
if (noGC(opcode)) return;
popE(ValueTypes.I31REF);
push(ValueType.I32);
}
I31_GET_U => {
if (noGC(opcode)) return;
popE(ValueTypes.I31REF);
push(ValueType.I32);
}
REF_TEST,
REF_TEST_NULL => {
if (noGC(opcode)) return;
var nullable = (opcode.code == Opcode.REF_TEST_NULL.code);
var ht = parser.readHeapType();
var ref = popRef();
push(ValueType.I32);
}
REF_CAST,
REF_CAST_NULL => {
if (noGC(opcode)) return;
var nullable = (opcode.code == Opcode.REF_CAST_NULL.code);
var ht = parser.readHeapType();
var ref = popRef();
push(ValueType.Ref(nullable, ht));
}
BR_ON_CAST => {
if (noGC(opcode)) return;
var t = parser.readBrCastImms(), depth = t.0, t1 = t.1, t2 = t.2;
var target = getControl(depth);
if (target == null) return;
if (!ValueTypes.isAssignable(t2, t1)) err_atpc().IllegalCast(t1, t2);
ctlxfer.refS(target, opcode_pos, val_stack.top);
popE(t1);
push(t2);
checkAndPopArgs(labelArgs(target));
push(ValueType.Ref(t1.nullable && !t2.nullable, t1.heap));
}
BR_ON_CAST_FAIL => {
if (noGC(opcode)) return;
var t = parser.readBrCastImms(), depth = t.0, t1 = t.1, t2 = t.2;
var target = getControl(depth);
if (target == null) return;
if (!ValueTypes.isAssignable(t2, t1)) err_atpc().IllegalCast(t1, t2);
ctlxfer.refS(target, opcode_pos, val_stack.top);
popE(t1);
push(ValueType.Ref(t1.nullable && !t2.nullable, t1.heap));
checkAndPopArgs(labelArgs(target));
push(t2);
}
ANY_CONVERT_EXTERN => {
if (noGC(opcode)) return;
var nullable = popRefE(ValueTypes.EXTERNREF);
var at = if(nullable, ValueTypes.ANYREF, ValueTypes.ANYREF_NONNULL);
push(at);
}
EXTERN_CONVERT_ANY => {
if (noGC(opcode)) return;
var nullable = popRefE(ValueTypes.ANYREF);
var et = if(nullable, ValueTypes.EXTERNREF, ValueTypes.EXTERNREF_NONNULL);
push(et);
}
I32_TRUNC_SAT_F32_S => checkSignature(Opcode.I32_TRUNC_SAT_F32_S.sig);
I32_TRUNC_SAT_F32_U => checkSignature(Opcode.I32_TRUNC_SAT_F32_U.sig);
I32_TRUNC_SAT_F64_S => checkSignature(Opcode.I32_TRUNC_SAT_F64_S.sig);
I32_TRUNC_SAT_F64_U => checkSignature(Opcode.I32_TRUNC_SAT_F64_U.sig);
I64_TRUNC_SAT_F32_S => checkSignature(Opcode.I64_TRUNC_SAT_F32_S.sig);
I64_TRUNC_SAT_F32_U => checkSignature(Opcode.I64_TRUNC_SAT_F32_U.sig);
I64_TRUNC_SAT_F64_S => checkSignature(Opcode.I64_TRUNC_SAT_F64_S.sig);
I64_TRUNC_SAT_F64_U => checkSignature(Opcode.I64_TRUNC_SAT_F64_U.sig);
MEMORY_INIT => {
if (module.explicit_data_count < 0) err_atpc().MissingDataCount();
var dindex = parser.readDataIndex();
var dst = parser.readMemoryRef(extensions.MULTI_MEMORY);
if (dst == null) return;
var it = dst.size.indexType();
popE(ValueType.I32);
popE(ValueType.I32);
popE(it);
}
DATA_DROP => {
if (module.explicit_data_count < 0) err_atpc().MissingDataCount();
var index = parser.readDataIndex();
}
MEMORY_COPY => {
var dst = parser.readMemoryRef(extensions.MULTI_MEMORY);
var src = parser.readMemoryRef(extensions.MULTI_MEMORY);
if (dst == null || src == null) return;
var dit = dst.size.indexType();
var sit = src.size.indexType();
popE(sit);
popE(sit);
popE(dit);
}
MEMORY_FILL => {
var dst = parser.readMemoryRef(extensions.MULTI_MEMORY);
if (dst == null) return;
var it = dst.size.indexType();
popE(it);
popE(ValueType.I32);
popE(it);
}
TABLE_INIT => {
var elem = parser.readElemRef();
var dst = parser.readTableRef();
if (dst == null || elem == null) return;
if (!ValueTypes.isAssignable(elem.elemtype, dst.elemtype)) {
err_atpc().ElementTypeMismatch(dst.elemtype, elem.elemtype);
}
var it = dst.size.indexType();
popE(ValueType.I32);
popE(ValueType.I32);
popE(it);
}
ELEM_DROP => {
var index = parser.readElemRef();
}
TABLE_COPY => {
var dst = parser.readTableRef();
var src = parser.readTableRef();
if (dst == null || src == null) return;
if (!ValueTypes.isAssignable(src.elemtype, dst.elemtype)) {
err_atpc().ElementTypeMismatch(dst.elemtype, src.elemtype);
}
var dit = dst.size.indexType();
var sit = src.size.indexType();
// length is minimum of source and destination type
var nit = if(dit == ValueType.I32, ValueType.I32, sit);
popE(nit);
popE(sit);
popE(dit);
}
TABLE_GROW => {
var table = parser.readTableRef();