forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWasmFunctionParser.h
More file actions
2092 lines (1720 loc) · 94.4 KB
/
WasmFunctionParser.h
File metadata and controls
2092 lines (1720 loc) · 94.4 KB
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 (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if ENABLE(WEBASSEMBLY)
#include "WasmParser.h"
#include "WasmTypeDefinitionInlines.h"
#include <wtf/DataLog.h>
namespace JSC { namespace Wasm {
enum class BlockType {
If,
Block,
Loop,
TopLevel,
Try,
Catch,
};
enum class CatchKind {
Catch,
CatchAll,
};
template<typename EnclosingStack, typename NewStack>
void splitStack(BlockSignature signature, EnclosingStack& enclosingStack, NewStack& newStack)
{
newStack.reserveInitialCapacity(signature->as<FunctionSignature>()->argumentCount());
ASSERT(enclosingStack.size() >= signature->as<FunctionSignature>()->argumentCount());
unsigned offset = enclosingStack.size() - signature->as<FunctionSignature>()->argumentCount();
for (unsigned i = 0; i < signature->as<FunctionSignature>()->argumentCount(); ++i)
newStack.uncheckedAppend(enclosingStack.at(i + offset));
enclosingStack.shrink(offset);
}
template<typename Context>
class FunctionParser : public Parser<void> {
public:
struct ControlEntry;
using ControlType = typename Context::ControlType;
using ExpressionType = typename Context::ExpressionType;
class TypedExpression {
public:
TypedExpression()
: m_type(Types::Void)
{
}
TypedExpression(Type type, ExpressionType value)
: m_type(type)
, m_value(value)
{
}
Type type() const { return m_type; }
ExpressionType value() const { return m_value; }
operator ExpressionType() const { return m_value; }
ExpressionType operator->() const { return m_value; }
private:
Type m_type;
ExpressionType m_value;
};
using ControlStack = Vector<ControlEntry, 16>;
using ResultList = Vector<ExpressionType, 8>;
using Stack = Vector<TypedExpression, 16, UnsafeVectorOverflow>;
struct ControlEntry {
Stack enclosedExpressionStack;
Stack elseBlockStack;
ControlType controlData;
};
FunctionParser(Context&, const uint8_t* functionStart, size_t functionLength, const TypeDefinition&, const ModuleInformation&);
Result WARN_UNUSED_RETURN parse();
OpType currentOpcode() const { return m_currentOpcode; }
size_t currentOpcodeStartingOffset() const { return m_currentOpcodeStartingOffset; }
const TypeDefinition& signature() const { return m_signature; }
ControlStack& controlStack() { return m_controlStack; }
Stack& expressionStack() { return m_expressionStack; }
private:
static constexpr bool verbose = false;
PartialResult WARN_UNUSED_RETURN parseBody();
PartialResult WARN_UNUSED_RETURN parseExpression();
PartialResult WARN_UNUSED_RETURN parseUnreachableExpression();
PartialResult WARN_UNUSED_RETURN unifyControl(Vector<ExpressionType>&, unsigned level);
PartialResult WARN_UNUSED_RETURN checkBranchTarget(const ControlType&);
PartialResult WARN_UNUSED_RETURN unify(const ControlType&);
#define WASM_TRY_POP_EXPRESSION_STACK_INTO(result, what) do { \
WASM_PARSER_FAIL_IF(m_expressionStack.isEmpty(), "can't pop empty stack in " what); \
result = m_expressionStack.takeLast(); \
m_context.didPopValueFromStack(); \
} while (0)
template<OpType>
PartialResult WARN_UNUSED_RETURN unaryCase(Type returnType, Type operandType);
template<OpType>
PartialResult WARN_UNUSED_RETURN binaryCase(Type returnType, Type lhsType, Type rhsType);
PartialResult WARN_UNUSED_RETURN store(Type memoryType);
PartialResult WARN_UNUSED_RETURN load(Type memoryType);
PartialResult WARN_UNUSED_RETURN truncSaturated(Ext1OpType, Type returnType, Type operandType);
PartialResult WARN_UNUSED_RETURN atomicLoad(ExtAtomicOpType, Type memoryType);
PartialResult WARN_UNUSED_RETURN atomicStore(ExtAtomicOpType, Type memoryType);
PartialResult WARN_UNUSED_RETURN atomicBinaryRMW(ExtAtomicOpType, Type memoryType);
PartialResult WARN_UNUSED_RETURN atomicCompareExchange(ExtAtomicOpType, Type memoryType);
PartialResult WARN_UNUSED_RETURN atomicWait(ExtAtomicOpType, Type memoryType);
PartialResult WARN_UNUSED_RETURN atomicNotify(ExtAtomicOpType);
PartialResult WARN_UNUSED_RETURN atomicFence(ExtAtomicOpType);
PartialResult WARN_UNUSED_RETURN parseTableIndex(unsigned&);
PartialResult WARN_UNUSED_RETURN parseElementIndex(unsigned&);
PartialResult WARN_UNUSED_RETURN parseDataSegmentIndex(unsigned&);
PartialResult WARN_UNUSED_RETURN parseIndexForLocal(uint32_t&);
PartialResult WARN_UNUSED_RETURN parseIndexForGlobal(uint32_t&);
PartialResult WARN_UNUSED_RETURN parseFunctionIndex(uint32_t&);
PartialResult WARN_UNUSED_RETURN parseExceptionIndex(uint32_t&);
PartialResult WARN_UNUSED_RETURN parseBranchTarget(uint32_t&);
PartialResult WARN_UNUSED_RETURN parseDelegateTarget(uint32_t&, uint32_t);
struct TableInitImmediates {
unsigned elementIndex;
unsigned tableIndex;
};
PartialResult WARN_UNUSED_RETURN parseTableInitImmediates(TableInitImmediates&);
struct TableCopyImmediates {
unsigned srcTableIndex;
unsigned dstTableIndex;
};
PartialResult WARN_UNUSED_RETURN parseTableCopyImmediates(TableCopyImmediates&);
struct AnnotatedSelectImmediates {
unsigned sizeOfAnnotationVector;
Type targetType;
};
PartialResult WARN_UNUSED_RETURN parseAnnotatedSelectImmediates(AnnotatedSelectImmediates&);
PartialResult WARN_UNUSED_RETURN parseMemoryFillImmediate();
PartialResult WARN_UNUSED_RETURN parseMemoryCopyImmediates();
struct MemoryInitImmediates {
unsigned dataSegmentIndex;
unsigned unused;
};
PartialResult WARN_UNUSED_RETURN parseMemoryInitImmediates(MemoryInitImmediates&);
#define WASM_TRY_ADD_TO_CONTEXT(add_expression) WASM_FAIL_IF_HELPER_FAILS(m_context.add_expression)
template <typename ...Args>
NEVER_INLINE UnexpectedResult WARN_UNUSED_RETURN validationFail(const Args&... args) const
{
using namespace FailureHelper; // See ADL comment in WasmParser.h.
if (UNLIKELY(ASSERT_ENABLED && Options::crashOnFailedWebAssemblyValidate()))
WTFBreakpointTrap();
StringPrintStream out;
out.print("WebAssembly.Module doesn't validate: "_s, args...);
return UnexpectedResult(out.toString());
}
#define WASM_VALIDATOR_FAIL_IF(condition, ...) do { \
if (UNLIKELY(condition)) \
return validationFail(__VA_ARGS__); \
} while (0) \
// FIXME add a macro as above for WASM_TRY_APPEND_TO_CONTROL_STACK https://bugs.webkit.org/show_bug.cgi?id=165862
Context& m_context;
Stack m_expressionStack;
ControlStack m_controlStack;
Vector<Type, 16> m_locals;
const TypeDefinition& m_signature;
const ModuleInformation& m_info;
OpType m_currentOpcode;
size_t m_currentOpcodeStartingOffset { 0 };
unsigned m_unreachableBlocks { 0 };
unsigned m_loopIndex { 0 };
};
template<typename ControlType>
static bool isTryOrCatch(ControlType& data)
{
return ControlType::isTry(data) || ControlType::isCatch(data);
}
template<typename Context>
FunctionParser<Context>::FunctionParser(Context& context, const uint8_t* functionStart, size_t functionLength, const TypeDefinition& signature, const ModuleInformation& info)
: Parser(functionStart, functionLength)
, m_context(context)
, m_signature(signature)
, m_info(info)
{
if (verbose)
dataLogLn("Parsing function starting at: ", (uintptr_t)functionStart, " of length: ", functionLength, " with signature: ", signature);
m_context.setParser(this);
}
template<typename Context>
auto FunctionParser<Context>::parse() -> Result
{
uint32_t localGroupsCount;
const auto& signature = *m_signature.as<FunctionSignature>();
WASM_PARSER_FAIL_IF(!m_context.addArguments(m_signature), "can't add ", signature.argumentCount(), " arguments to Function");
WASM_PARSER_FAIL_IF(!parseVarUInt32(localGroupsCount), "can't get local groups count");
WASM_PARSER_FAIL_IF(!m_locals.tryReserveCapacity(signature.argumentCount()), "can't allocate enough memory for function's ", signature.argumentCount(), " arguments");
for (uint32_t i = 0; i < signature.argumentCount(); ++i)
m_locals.uncheckedAppend(signature.argumentType(i));
uint64_t totalNumberOfLocals = signature.argumentCount();
for (uint32_t i = 0; i < localGroupsCount; ++i) {
uint32_t numberOfLocals;
Type typeOfLocal;
WASM_PARSER_FAIL_IF(!parseVarUInt32(numberOfLocals), "can't get Function's number of locals in group ", i);
totalNumberOfLocals += numberOfLocals;
WASM_PARSER_FAIL_IF(totalNumberOfLocals > maxFunctionLocals, "Function's number of locals is too big ", totalNumberOfLocals, " maximum ", maxFunctionLocals);
WASM_PARSER_FAIL_IF(!parseValueType(m_info, typeOfLocal), "can't get Function local's type in group ", i);
WASM_PARSER_FAIL_IF(!isDefaultableType(typeOfLocal), "Function locals must have a defaultable type");
WASM_PARSER_FAIL_IF(!m_locals.tryReserveCapacity(totalNumberOfLocals), "can't allocate enough memory for function's ", totalNumberOfLocals, " locals");
for (uint32_t i = 0; i < numberOfLocals; ++i)
m_locals.uncheckedAppend(typeOfLocal);
WASM_TRY_ADD_TO_CONTEXT(addLocal(typeOfLocal, numberOfLocals));
}
m_context.didFinishParsingLocals();
WASM_FAIL_IF_HELPER_FAILS(parseBody());
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseBody() -> PartialResult
{
m_controlStack.append({ { }, { }, m_context.addTopLevel(&m_signature) });
uint8_t op = 0;
while (m_controlStack.size()) {
m_currentOpcodeStartingOffset = m_offset;
WASM_PARSER_FAIL_IF(!parseUInt8(op), "can't decode opcode");
WASM_PARSER_FAIL_IF(!isValidOpType(op), "invalid opcode ", op);
m_currentOpcode = static_cast<OpType>(op);
if (verbose) {
dataLogLn("processing op (", m_unreachableBlocks, "): ", RawHex(op), ", ", makeString(static_cast<OpType>(op)), " at offset: ", RawHex(m_offset));
m_context.dump(m_controlStack, &m_expressionStack);
}
if (m_unreachableBlocks)
WASM_FAIL_IF_HELPER_FAILS(parseUnreachableExpression());
else {
WASM_FAIL_IF_HELPER_FAILS(parseExpression());
}
}
WASM_FAIL_IF_HELPER_FAILS(m_context.endTopLevel(&m_signature, m_expressionStack));
ASSERT(op == OpType::End);
return { };
}
template<typename Context>
template<OpType op>
auto FunctionParser<Context>::binaryCase(Type returnType, Type lhsType, Type rhsType) -> PartialResult
{
TypedExpression right;
TypedExpression left;
WASM_TRY_POP_EXPRESSION_STACK_INTO(right, "binary right");
WASM_TRY_POP_EXPRESSION_STACK_INTO(left, "binary left");
WASM_VALIDATOR_FAIL_IF(left.type() != lhsType, op, " left value type mismatch");
WASM_VALIDATOR_FAIL_IF(right.type() != rhsType, op, " right value type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(template addOp<op>(left, right, result));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
template<OpType op>
auto FunctionParser<Context>::unaryCase(Type returnType, Type operandType) -> PartialResult
{
TypedExpression value;
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "unary");
WASM_VALIDATOR_FAIL_IF(value.type() != operandType, op, " value type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(template addOp<op>(value, result));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::load(Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "load instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment");
WASM_PARSER_FAIL_IF(alignment > memoryLog2Alignment(m_currentOpcode), "byte alignment ", 1ull << alignment, " exceeds load's natural alignment ", 1ull << memoryLog2Alignment(m_currentOpcode));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "load pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), m_currentOpcode, " pointer type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(load(static_cast<LoadOpType>(m_currentOpcode), pointer, result, offset));
m_expressionStack.constructAndAppend(memoryType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::store(Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "store instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression value;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get store alignment");
WASM_PARSER_FAIL_IF(alignment > memoryLog2Alignment(m_currentOpcode), "byte alignment ", 1ull << alignment, " exceeds store's natural alignment ", 1ull << memoryLog2Alignment(m_currentOpcode));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get store offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "store value");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "store pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), m_currentOpcode, " pointer type mismatch");
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, m_currentOpcode, " value type mismatch");
WASM_TRY_ADD_TO_CONTEXT(store(static_cast<StoreOpType>(m_currentOpcode), pointer, value, offset));
return { };
}
template<typename Context>
auto FunctionParser<Context>::truncSaturated(Ext1OpType op, Type returnType, Type operandType) -> PartialResult
{
TypedExpression value;
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "unary");
WASM_VALIDATOR_FAIL_IF(value.type() != operandType, "trunc-saturated value type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(truncSaturated(op, value, result, returnType, operandType));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicLoad(ExtAtomicOpType op, Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "atomic instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment");
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment ", 1ull << alignment, " does not match against atomic op's natural alignment ", 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "load pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(atomicLoad(op, memoryType, pointer, result, offset));
m_expressionStack.constructAndAppend(memoryType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicStore(ExtAtomicOpType op, Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "atomic instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression value;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get store alignment");
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment ", 1ull << alignment, " does not match against atomic op's natural alignment ", 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get store offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "store value");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "store pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), m_currentOpcode, " pointer type mismatch");
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, m_currentOpcode, " value type mismatch");
WASM_TRY_ADD_TO_CONTEXT(atomicStore(op, memoryType, pointer, value, offset));
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicBinaryRMW(ExtAtomicOpType op, Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "atomic instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression value;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment");
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment ", 1ull << alignment, " does not match against atomic op's natural alignment ", 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "value");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch");
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, static_cast<unsigned>(op), " value type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(atomicBinaryRMW(op, memoryType, pointer, value, result, offset));
m_expressionStack.constructAndAppend(memoryType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicCompareExchange(ExtAtomicOpType op, Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "atomic instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression expected;
TypedExpression value;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment");
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment ", 1ull << alignment, " does not match against atomic op's natural alignment ", 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "value");
WASM_TRY_POP_EXPRESSION_STACK_INTO(expected, "expected");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch");
WASM_VALIDATOR_FAIL_IF(expected.type() != memoryType, static_cast<unsigned>(op), " expected type mismatch");
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, static_cast<unsigned>(op), " value type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(atomicCompareExchange(op, memoryType, pointer, expected, value, result, offset));
m_expressionStack.constructAndAppend(memoryType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicWait(ExtAtomicOpType op, Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "atomic instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression value;
TypedExpression timeout;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment");
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment ", 1ull << alignment, " does not match against atomic op's natural alignment ", 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(timeout, "timeout");
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "value");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch");
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, static_cast<unsigned>(op), " value type mismatch");
WASM_VALIDATOR_FAIL_IF(!timeout.type().isI64(), static_cast<unsigned>(op), " timeout type mismatch");
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(atomicWait(op, pointer, value, timeout, result, offset));
m_expressionStack.constructAndAppend(Types::I32, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicNotify(ExtAtomicOpType op) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memory, "atomic instruction without memory");
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression count;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment");
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment ", 1ull << alignment, " does not match against atomic op's natural alignment ", 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset");
WASM_TRY_POP_EXPRESSION_STACK_INTO(count, "count");
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer");
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch");
WASM_VALIDATOR_FAIL_IF(!count.type().isI32(), static_cast<unsigned>(op), " count type mismatch"); // The spec's definition is saying i64, but all implementations (including tests) are using i32. So looks like the spec is wrong.
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(atomicNotify(op, pointer, count, result, offset));
m_expressionStack.constructAndAppend(Types::I32, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::atomicFence(ExtAtomicOpType op) -> PartialResult
{
uint8_t flags;
WASM_PARSER_FAIL_IF(!parseUInt8(flags), "can't get flags");
WASM_PARSER_FAIL_IF(flags != 0x0, "flags should be 0x0 but got ", flags);
WASM_TRY_ADD_TO_CONTEXT(atomicFence(op, flags));
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseTableIndex(unsigned& result) -> PartialResult
{
unsigned tableIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(tableIndex), "can't parse table index");
WASM_VALIDATOR_FAIL_IF(tableIndex >= m_info.tableCount(), "table index ", tableIndex, " is invalid, limit is ", m_info.tableCount());
result = tableIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseIndexForLocal(uint32_t& resultIndex) -> PartialResult
{
uint32_t index;
WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get index for local");
WASM_VALIDATOR_FAIL_IF(index >= m_locals.size(), "attempt to use unknown local ", index, " last one is ", m_locals.size());
resultIndex = index;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseIndexForGlobal(uint32_t& resultIndex) -> PartialResult
{
uint32_t index;
WASM_PARSER_FAIL_IF(!parseVarUInt32(index), "can't get global's index");
WASM_VALIDATOR_FAIL_IF(index >= m_info.globals.size(), index, " of unknown global, limit is ", m_info.globals.size());
resultIndex = index;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseFunctionIndex(uint32_t& resultIndex) -> PartialResult
{
uint32_t functionIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(functionIndex), "can't parse function index");
WASM_PARSER_FAIL_IF(functionIndex >= m_info.functionIndexSpaceSize(), "function index ", functionIndex, " exceeds function index space ", m_info.functionIndexSpaceSize());
resultIndex = functionIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseExceptionIndex(uint32_t& result) -> PartialResult
{
uint32_t exceptionIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(exceptionIndex), "can't parse exception index");
WASM_VALIDATOR_FAIL_IF(exceptionIndex >= m_info.exceptionIndexSpaceSize(), "exception index ", exceptionIndex, " is invalid, limit is ", m_info.exceptionIndexSpaceSize());
result = exceptionIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseBranchTarget(uint32_t& resultTarget) -> PartialResult
{
uint32_t target;
WASM_PARSER_FAIL_IF(!parseVarUInt32(target), "can't get br / br_if's target");
WASM_PARSER_FAIL_IF(target >= m_controlStack.size(), "br / br_if's target ", target, " exceeds control stack size ", m_controlStack.size());
resultTarget = target;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseDelegateTarget(uint32_t& resultTarget, uint32_t unreachableBlocks) -> PartialResult
{
// Right now, control stack includes try-delegate block, and delegate needs to specify outer scope.
uint32_t target;
WASM_PARSER_FAIL_IF(!parseVarUInt32(target), "can't get delegate target");
Checked<uint32_t, RecordOverflow> controlStackSize { m_controlStack.size() };
if (unreachableBlocks)
controlStackSize += (unreachableBlocks - 1); // The first block is in the control stack already.
controlStackSize -= 1; // delegate target does not include the current block.
WASM_PARSER_FAIL_IF(controlStackSize.hasOverflowed(), "invalid control stack size");
WASM_PARSER_FAIL_IF(target >= controlStackSize.value(), "delegate target ", target, " exceeds control stack size ", controlStackSize.value());
resultTarget = target;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseElementIndex(unsigned& result) -> PartialResult
{
unsigned elementIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(elementIndex), "can't parse element index");
WASM_VALIDATOR_FAIL_IF(elementIndex >= m_info.elementCount(), "element index ", elementIndex, " is invalid, limit is ", m_info.elementCount());
result = elementIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseDataSegmentIndex(unsigned& result) -> PartialResult
{
unsigned dataSegmentIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(dataSegmentIndex), "can't parse data segment index");
WASM_VALIDATOR_FAIL_IF(dataSegmentIndex >= m_info.dataSegmentsCount(), "data segment index ", dataSegmentIndex, " is invalid, limit is ", m_info.dataSegmentsCount());
result = dataSegmentIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseTableInitImmediates(TableInitImmediates& result) -> PartialResult
{
unsigned elementIndex;
WASM_FAIL_IF_HELPER_FAILS(parseElementIndex(elementIndex));
unsigned tableIndex;
WASM_FAIL_IF_HELPER_FAILS(parseTableIndex(tableIndex));
result.elementIndex = elementIndex;
result.tableIndex = tableIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseTableCopyImmediates(TableCopyImmediates& result) -> PartialResult
{
unsigned dstTableIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(dstTableIndex), "can't parse destination table index");
WASM_VALIDATOR_FAIL_IF(dstTableIndex >= m_info.tableCount(), "table index ", dstTableIndex, " is invalid, limit is ", m_info.tableCount());
unsigned srcTableIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(srcTableIndex), "can't parse source table index");
WASM_VALIDATOR_FAIL_IF(srcTableIndex >= m_info.tableCount(), "table index ", srcTableIndex, " is invalid, limit is ", m_info.tableCount());
result.dstTableIndex = dstTableIndex;
result.srcTableIndex = srcTableIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseAnnotatedSelectImmediates(AnnotatedSelectImmediates& result) -> PartialResult
{
uint32_t sizeOfAnnotationVector;
WASM_PARSER_FAIL_IF(!parseVarUInt32(sizeOfAnnotationVector), "select can't parse the size of annotation vector");
WASM_PARSER_FAIL_IF(sizeOfAnnotationVector != 1, "select invalid result arity for");
Type targetType;
WASM_PARSER_FAIL_IF(!parseValueType(m_info, targetType), "select can't parse annotations");
result.sizeOfAnnotationVector = sizeOfAnnotationVector;
result.targetType = targetType;
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseMemoryFillImmediate() -> PartialResult
{
uint8_t auxiliaryByte;
WASM_PARSER_FAIL_IF(!parseUInt8(auxiliaryByte), "can't parse auxiliary byte");
WASM_PARSER_FAIL_IF(!!auxiliaryByte, "auxiliary byte for memory.fill should be zero, but got ", auxiliaryByte);
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseMemoryCopyImmediates() -> PartialResult
{
uint8_t firstAuxiliaryByte;
WASM_PARSER_FAIL_IF(!parseUInt8(firstAuxiliaryByte), "can't parse auxiliary byte");
WASM_PARSER_FAIL_IF(!!firstAuxiliaryByte, "auxiliary byte for memory.copy should be zero, but got ", firstAuxiliaryByte);
uint8_t secondAuxiliaryByte;
WASM_PARSER_FAIL_IF(!parseUInt8(secondAuxiliaryByte), "can't parse auxiliary byte");
WASM_PARSER_FAIL_IF(!!secondAuxiliaryByte, "auxiliary byte for memory.copy should be zero, but got ", secondAuxiliaryByte);
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseMemoryInitImmediates(MemoryInitImmediates& result) -> PartialResult
{
unsigned dataSegmentIndex;
WASM_FAIL_IF_HELPER_FAILS(parseDataSegmentIndex(dataSegmentIndex));
unsigned unused;
WASM_PARSER_FAIL_IF(!parseVarUInt32(unused), "can't parse unused");
WASM_PARSER_FAIL_IF(!!unused, "memory.init invalid unsued byte");
result.unused = unused;
result.dataSegmentIndex = dataSegmentIndex;
return { };
}
template<typename Context>
auto FunctionParser<Context>::checkBranchTarget(const ControlType& target) -> PartialResult
{
if (!target.branchTargetArity())
return { };
WASM_VALIDATOR_FAIL_IF(m_expressionStack.size() < target.branchTargetArity(), ControlType::isTopLevel(target) ? "branch out of function" : "branch to block", " on expression stack of size ", m_expressionStack.size(), ", but block, ", target.signature()->toString() , " expects ", target.branchTargetArity(), " values");
unsigned offset = m_expressionStack.size() - target.branchTargetArity();
for (unsigned i = 0; i < target.branchTargetArity(); ++i)
WASM_VALIDATOR_FAIL_IF(!isSubtype(m_expressionStack[offset + i].type(), target.branchTargetType(i)), "branch's stack type is not a block's type branch target type. Stack value has type ", m_expressionStack[offset + i].type().kind, " but branch target expects a value of ", target.branchTargetType(i).kind, " at index ", i);
return { };
}
template<typename Context>
auto FunctionParser<Context>::unify(const ControlType& controlData) -> PartialResult
{
const TypeDefinition* typeDefinition = controlData.signature(); // just to avoid a weird compiler error with templates at the next line.
const FunctionSignature* signature = typeDefinition->as<FunctionSignature>();
WASM_VALIDATOR_FAIL_IF(signature->returnCount() != m_expressionStack.size(), " block with type: ", signature->toString(), " returns: ", signature->returnCount(), " but stack has: ", m_expressionStack.size(), " values");
for (unsigned i = 0; i < signature->returnCount(); ++i)
WASM_VALIDATOR_FAIL_IF(!isSubtype(m_expressionStack[i].type(), signature->returnType(i)), "control flow returns with unexpected type. ", m_expressionStack[i].type().kind, " is not a ", signature->returnType(i).kind);
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseExpression() -> PartialResult
{
switch (m_currentOpcode) {
#define CREATE_CASE(name, id, b3op, inc, lhsType, rhsType, returnType) case OpType::name: return binaryCase<OpType::name>(Types::returnType, Types::lhsType, Types::rhsType);
FOR_EACH_WASM_BINARY_OP(CREATE_CASE)
#undef CREATE_CASE
#define CREATE_CASE(name, id, b3op, inc, operandType, returnType) case OpType::name: return unaryCase<OpType::name>(Types::returnType, Types::operandType);
FOR_EACH_WASM_UNARY_OP(CREATE_CASE)
#undef CREATE_CASE
case Select: {
TypedExpression condition;
TypedExpression zero;
TypedExpression nonZero;
WASM_TRY_POP_EXPRESSION_STACK_INTO(condition, "select condition");
WASM_TRY_POP_EXPRESSION_STACK_INTO(zero, "select zero");
WASM_TRY_POP_EXPRESSION_STACK_INTO(nonZero, "select non-zero");
WASM_PARSER_FAIL_IF(isRefType(nonZero.type()) || isRefType(nonZero.type()), "can't use ref-types with unannotated select");
WASM_VALIDATOR_FAIL_IF(!condition.type().isI32(), "select condition must be i32, got ", condition.type().kind);
WASM_VALIDATOR_FAIL_IF(nonZero.type() != zero.type(), "select result types must match, got ", nonZero.type().kind, " and ", zero.type().kind);
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(addSelect(condition, nonZero, zero, result));
m_expressionStack.constructAndAppend(zero.type(), result);
return { };
}
case AnnotatedSelect: {
AnnotatedSelectImmediates immediates;
WASM_FAIL_IF_HELPER_FAILS(parseAnnotatedSelectImmediates(immediates));
TypedExpression condition;
TypedExpression zero;
TypedExpression nonZero;
WASM_TRY_POP_EXPRESSION_STACK_INTO(condition, "select condition");
WASM_TRY_POP_EXPRESSION_STACK_INTO(zero, "select zero");
WASM_TRY_POP_EXPRESSION_STACK_INTO(nonZero, "select non-zero");
WASM_VALIDATOR_FAIL_IF(!condition.type().isI32(), "select condition must be i32, got ", condition.type().kind);
WASM_VALIDATOR_FAIL_IF(nonZero.type() != immediates.targetType, "select result types must match, got ", nonZero.type().kind, " and ", immediates.targetType.kind);
WASM_VALIDATOR_FAIL_IF(zero.type() != immediates.targetType, "select result types must match, got ", zero.type().kind, " and ", immediates.targetType.kind);
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(addSelect(condition, nonZero, zero, result));
m_expressionStack.constructAndAppend(immediates.targetType, result);
return { };
}
#define CREATE_CASE(name, id, b3op, inc, memoryType) case OpType::name: return load(Types::memoryType);
FOR_EACH_WASM_MEMORY_LOAD_OP(CREATE_CASE)
#undef CREATE_CASE
#define CREATE_CASE(name, id, b3op, inc, memoryType) case OpType::name: return store(Types::memoryType);
FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_CASE)
#undef CREATE_CASE
case F32Const: {
uint32_t constant;
WASM_PARSER_FAIL_IF(!parseUInt32(constant), "can't parse 32-bit floating-point constant");
m_expressionStack.constructAndAppend(Types::F32, m_context.addConstant(Types::F32, constant));
return { };
}
case I32Const: {
int32_t constant;
WASM_PARSER_FAIL_IF(!parseVarInt32(constant), "can't parse 32-bit constant");
m_expressionStack.constructAndAppend(Types::I32, m_context.addConstant(Types::I32, constant));
return { };
}
case F64Const: {
uint64_t constant;
WASM_PARSER_FAIL_IF(!parseUInt64(constant), "can't parse 64-bit floating-point constant");
m_expressionStack.constructAndAppend(Types::F64, m_context.addConstant(Types::F64, constant));
return { };
}
case I64Const: {
int64_t constant;
WASM_PARSER_FAIL_IF(!parseVarInt64(constant), "can't parse 64-bit constant");
m_expressionStack.constructAndAppend(Types::I64, m_context.addConstant(Types::I64, constant));
return { };
}
case TableGet: {
unsigned tableIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(tableIndex), "can't parse table index");
WASM_VALIDATOR_FAIL_IF(tableIndex >= m_info.tableCount(), "table index ", tableIndex, " is invalid, limit is ", m_info.tableCount());
TypedExpression index;
WASM_TRY_POP_EXPRESSION_STACK_INTO(index, "table.get");
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != index.type().kind, "table.get index to type ", index.type().kind, " expected ", TypeKind::I32);
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(addTableGet(tableIndex, index, result));
Type resultType = m_info.tables[tableIndex].wasmType();
m_expressionStack.constructAndAppend(resultType, result);
return { };
}
case TableSet: {
unsigned tableIndex;
WASM_PARSER_FAIL_IF(!parseVarUInt32(tableIndex), "can't parse table index");
WASM_VALIDATOR_FAIL_IF(tableIndex >= m_info.tableCount(), "table index ", tableIndex, " is invalid, limit is ", m_info.tableCount());
TypedExpression value, index;
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "table.set");
WASM_TRY_POP_EXPRESSION_STACK_INTO(index, "table.set");
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != index.type().kind, "table.set index to type ", index.type().kind, " expected ", TypeKind::I32);
Type type = m_info.tables[tableIndex].wasmType();
WASM_VALIDATOR_FAIL_IF(!isSubtype(value.type(), type), "table.set value to type ", value.type().kind, " expected ", type.kind);
RELEASE_ASSERT(m_info.tables[tableIndex].type() == TableElementType::Externref || m_info.tables[tableIndex].type() == TableElementType::Funcref);
WASM_TRY_ADD_TO_CONTEXT(addTableSet(tableIndex, index, value));
return { };
}
case Ext1: {
uint8_t extOp;
WASM_PARSER_FAIL_IF(!parseUInt8(extOp), "can't parse 0xfc extended opcode");
Ext1OpType op = static_cast<Ext1OpType>(extOp);
switch (op) {
case Ext1OpType::TableInit: {
TableInitImmediates immediates;
WASM_FAIL_IF_HELPER_FAILS(parseTableInitImmediates(immediates));
TypedExpression dstOffset;
TypedExpression srcOffset;
TypedExpression lenght;
WASM_TRY_POP_EXPRESSION_STACK_INTO(lenght, "table.init");
WASM_TRY_POP_EXPRESSION_STACK_INTO(srcOffset, "table.init");
WASM_TRY_POP_EXPRESSION_STACK_INTO(dstOffset, "table.init");
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != dstOffset.type().kind, "table.init dst_offset to type ", dstOffset.type().kind, " expected ", TypeKind::I32);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != srcOffset.type().kind, "table.init src_offset to type ", srcOffset.type().kind, " expected ", TypeKind::I32);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != lenght.type().kind, "table.init length to type ", lenght.type().kind, " expected ", TypeKind::I32);
WASM_TRY_ADD_TO_CONTEXT(addTableInit(immediates.elementIndex, immediates.tableIndex, dstOffset, srcOffset, lenght));
break;
}
case Ext1OpType::ElemDrop: {
unsigned elementIndex;
WASM_FAIL_IF_HELPER_FAILS(parseElementIndex(elementIndex));
WASM_TRY_ADD_TO_CONTEXT(addElemDrop(elementIndex));
break;
}
case Ext1OpType::TableSize: {
unsigned tableIndex;
WASM_FAIL_IF_HELPER_FAILS(parseTableIndex(tableIndex));
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(addTableSize(tableIndex, result));
m_expressionStack.constructAndAppend(Types::I32, result);
break;
}
case Ext1OpType::TableGrow: {
unsigned tableIndex;
WASM_FAIL_IF_HELPER_FAILS(parseTableIndex(tableIndex));
TypedExpression fill;
TypedExpression delta;
WASM_TRY_POP_EXPRESSION_STACK_INTO(delta, "table.grow");
WASM_TRY_POP_EXPRESSION_STACK_INTO(fill, "table.grow");
Type tableType = m_info.tables[tableIndex].wasmType();
WASM_VALIDATOR_FAIL_IF(!isSubtype(fill.type(), tableType), "table.grow expects fill value of type ", tableType.kind, " got ", fill.type().kind);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != delta.type().kind, "table.grow expects an i32 delta value, got ", delta.type().kind);
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(addTableGrow(tableIndex, fill, delta, result));
m_expressionStack.constructAndAppend(Types::I32, result);
break;
}
case Ext1OpType::TableFill: {
unsigned tableIndex;
WASM_FAIL_IF_HELPER_FAILS(parseTableIndex(tableIndex));
TypedExpression offset, fill, count;
WASM_TRY_POP_EXPRESSION_STACK_INTO(count, "table.fill");
WASM_TRY_POP_EXPRESSION_STACK_INTO(fill, "table.fill");
WASM_TRY_POP_EXPRESSION_STACK_INTO(offset, "table.fill");
Type tableType = m_info.tables[tableIndex].wasmType();
WASM_VALIDATOR_FAIL_IF(!isSubtype(fill.type(), tableType), "table.fill expects fill value of type ", tableType.kind, " got ", fill.type().kind);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != offset.type().kind, "table.fill expects an i32 offset value, got ", offset.type().kind);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != count.type().kind, "table.fill expects an i32 count value, got ", count.type().kind);
WASM_TRY_ADD_TO_CONTEXT(addTableFill(tableIndex, offset, fill, count));
break;
}
case Ext1OpType::TableCopy: {
TableCopyImmediates immediates;
WASM_FAIL_IF_HELPER_FAILS(parseTableCopyImmediates(immediates));
const auto srcType = m_info.table(immediates.srcTableIndex).wasmType();
const auto dstType = m_info.table(immediates.dstTableIndex).wasmType();
WASM_VALIDATOR_FAIL_IF(srcType != dstType, "type mismatch at table.copy. got ", srcType.kind, " and ", dstType.kind);
TypedExpression dstOffset;
TypedExpression srcOffset;
TypedExpression length;
WASM_TRY_POP_EXPRESSION_STACK_INTO(length, "table.copy");
WASM_TRY_POP_EXPRESSION_STACK_INTO(srcOffset, "table.copy");
WASM_TRY_POP_EXPRESSION_STACK_INTO(dstOffset, "table.copy");
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != dstOffset.type().kind, "table.copy dst_offset to type ", dstOffset.type().kind, " expected ", TypeKind::I32);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != srcOffset.type().kind, "table.copy src_offset to type ", srcOffset.type().kind, " expected ", TypeKind::I32);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != length.type().kind, "table.copy length to type ", length.type().kind, " expected ", TypeKind::I32);
WASM_TRY_ADD_TO_CONTEXT(addTableCopy(immediates.dstTableIndex, immediates.srcTableIndex, dstOffset, srcOffset, length));
break;
}
case Ext1OpType::MemoryFill: {
WASM_FAIL_IF_HELPER_FAILS(parseMemoryFillImmediate());
WASM_VALIDATOR_FAIL_IF(!m_info.memoryCount(), "memory must be present");
TypedExpression dstAddress;
TypedExpression targetValue;
TypedExpression count;
WASM_TRY_POP_EXPRESSION_STACK_INTO(count, "memory.fill");
WASM_TRY_POP_EXPRESSION_STACK_INTO(targetValue, "memory.fill");
WASM_TRY_POP_EXPRESSION_STACK_INTO(dstAddress, "memory.fill");
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != dstAddress.type().kind, "memory.fill dstAddress to type ", dstAddress.type().kind, " expected ", TypeKind::I32);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != targetValue.type().kind, "memory.fill targetValue to type ", targetValue.type().kind, " expected ", TypeKind::I32);
WASM_VALIDATOR_FAIL_IF(TypeKind::I32 != count.type().kind, "memory.fill size to type ", count.type().kind, " expected ", TypeKind::I32);
WASM_TRY_ADD_TO_CONTEXT(addMemoryFill(dstAddress, targetValue, count));
break;