-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathWasmFunctionParser.h
More file actions
4488 lines (3763 loc) · 210 KB
/
WasmFunctionParser.h
File metadata and controls
4488 lines (3763 loc) · 210 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-2024 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
#include <wtf/Platform.h>
#if ENABLE(WEBASSEMBLY)
#include "AirArg.h"
#include "WasmOpcodeCounter.h"
#include "WasmParser.h"
#include "WasmTypeDefinitionInlines.h"
#include <wtf/DataLog.h>
#include <wtf/ListDump.h>
#include <wtf/TZoneMalloc.h>
#include <wtf/text/MakeString.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC { namespace Wasm {
class ConstExprGenerator;
enum class BlockType {
If,
Else,
Block,
Loop,
TopLevel,
Try,
TryTable,
Catch,
};
enum class CatchKind {
Catch = 0x00,
CatchRef = 0x01,
CatchAll = 0x02,
CatchAllRef = 0x03,
};
OVERLOAD_RELATIONAL_OPERATORS_FOR_ENUM_CLASS_WITH_INTEGRALS(CatchKind);
template<typename EnclosingStack, typename NewStack>
void splitStack(const BlockSignature& signature, EnclosingStack& enclosingStack, NewStack& newStack)
{
ASSERT(enclosingStack.size() >= signature.argumentCount());
unsigned offset = enclosingStack.size() - signature.argumentCount();
newStack = NewStack(signature.argumentCount(), [&](size_t i) {
return enclosingStack.at(i + offset);
});
enclosingStack.shrink(offset);
}
struct ControlRef {
size_t m_index { 0 };
};
template<typename Control, typename Expression, typename Call>
struct FunctionParserTypes {
using ControlType = Control;
using ExpressionType = Expression;
using CallType = Call;
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; }
void setType(Type type) { m_type = type; }
ExpressionType& value() LIFETIME_BOUND { return m_value; }
ExpressionType value() const { return m_value; }
operator ExpressionType() const { return m_value; }
ExpressionType operator->() const { return m_value; }
void dump(PrintStream& out) const
{
out.print(m_value, ": "_s, m_type);
}
private:
Type m_type;
ExpressionType m_value;
};
using Stack = Vector<TypedExpression, 16, UnsafeVectorOverflow>;
struct ControlEntry {
Stack enclosedExpressionStack;
Stack elseBlockStack;
uint32_t localInitStackHeight;
ControlType controlData;
};
using ControlStack = Vector<ControlEntry, 16>;
using ResultList = Vector<ExpressionType, 8>;
using ArgumentList = Vector<TypedExpression, 8>;
struct CatchHandler {
CatchKind type;
uint32_t tag;
const TypeDefinition* exceptionSignature;
ControlRef target;
};
};
template<typename Context>
class FunctionParser : public Parser<void>, public FunctionParserTypes<typename Context::ControlType, typename Context::ExpressionType, typename Context::CallType> {
WTF_MAKE_TZONE_ALLOCATED_TEMPLATE(FunctionParser);
public:
using CallType = typename FunctionParser::CallType;
using ControlType = typename FunctionParser::ControlType;
using ControlEntry = typename FunctionParser::ControlEntry;
using ControlStack = typename FunctionParser::ControlStack;
using ExpressionType = typename FunctionParser::ExpressionType;
using TypedExpression = typename FunctionParser::TypedExpression;
using Stack = typename FunctionParser::Stack;
using ResultList = typename FunctionParser::ResultList;
using ArgumentList = typename FunctionParser::ArgumentList;
using CatchHandler = typename FunctionParser::CatchHandler;
FunctionParser(Context&, std::span<const uint8_t> function, const TypeDefinition&, const ModuleInformation&);
[[nodiscard]] Result parse();
[[nodiscard]] Result parseConstantExpression();
OpType currentOpcode() const { return m_currentOpcode; }
uint32_t currentExtendedOpcode() const { return m_currentExtOp; }
size_t currentOpcodeStartingOffset() const { return m_currentOpcodeStartingOffset; }
const TypeDefinition& signature() const { return m_signature; }
const Type& typeOfLocal(uint32_t localIndex) const { return m_locals[localIndex]; }
bool unreachableBlocks() const { return m_unreachableBlocks; }
ControlStack& controlStack() LIFETIME_BOUND { return m_controlStack; }
Stack& expressionStack() LIFETIME_BOUND { return m_expressionStack; }
ControlEntry& resolveControlRef(ControlRef ref) { return m_controlStack[ref.m_index]; }
void pushLocalInitialized(uint32_t index)
{
if (!isDefaultableType(typeOfLocal(index)) && !localIsInitialized(index)) {
m_localInitStack.append(index);
m_localInitFlags.quickSet(index);
}
}
uint32_t getLocalInitStackHeight() const { return m_localInitStack.size(); }
void resetLocalInitStackToHeight(uint32_t height)
{
uint32_t limit = m_localInitStack.size();
for (uint32_t i = height; i < limit; ++i)
m_localInitFlags.quickClear(m_localInitStack.takeLast());
};
bool localIsInitialized(uint32_t localIndex) { return m_localInitFlags.quickGet(localIndex); }
uint32_t getStackHeightInValues() const
{
return m_expressionStack.size() + getControlEntryStackHeightInValues();
}
uint32_t getControlEntryStackHeightInValues() const
{
uint32_t result = 0;
for (const ControlEntry& entry : m_controlStack)
result += entry.enclosedExpressionStack.size();
return result;
}
uint32_t numCallProfiles() const { return m_callProfileIndex; }
private:
static constexpr bool verbose = false;
[[nodiscard]] PartialResult parseBody();
[[nodiscard]] PartialResult parseExpression();
[[nodiscard]] PartialResult parseUnreachableExpression();
[[nodiscard]] PartialResult unifyControl(ArgumentList&, unsigned level);
[[nodiscard]] PartialResult checkLocalInitialized(uint32_t);
[[nodiscard]] PartialResult checkExpressionStack(const ControlType&, bool forceSignature = false);
enum BranchConditionalityTag {
Unconditional,
Conditional
};
[[nodiscard]] PartialResult checkBranchTarget(const ControlType&, BranchConditionalityTag);
[[nodiscard]] PartialResult parseImmLaneIdx(uint8_t laneCount, uint8_t&);
[[nodiscard]] PartialResult parseBlockSignature(const ModuleInformation&, BlockSignature&);
[[nodiscard]] PartialResult parseReftypeSignature(const ModuleInformation&, BlockSignature&);
[[nodiscard]] PartialResult parseNestedBlocksEagerly(bool&);
void switchToBlock(ControlType&&, Stack&&);
#define WASM_TRY_POP_EXPRESSION_STACK_INTO(result, what) do { \
WASM_PARSER_FAIL_IF(m_expressionStack.isEmpty(), "can't pop empty stack in "_s, what); \
result = m_expressionStack.takeLast(); \
m_context.didPopValueFromStack(result, "WasmFunctionParser.h " STRINGIZE_VALUE_OF(__LINE__) ""_s); \
} while (0)
using UnaryOperationHandler = PartialResult (Context::*)(ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult unaryCase(OpType, UnaryOperationHandler, Type returnType, Type operandType);
[[nodiscard]] PartialResult unaryCompareCase(OpType, UnaryOperationHandler, Type returnType, Type operandType);
using BinaryOperationHandler = PartialResult (Context::*)(ExpressionType, ExpressionType, ExpressionType&);
[[nodiscard]] PartialResult binaryCase(OpType, BinaryOperationHandler, Type returnType, Type lhsType, Type rhsType);
[[nodiscard]] PartialResult binaryCompareCase(OpType, BinaryOperationHandler, Type returnType, Type lhsType, Type rhsType);
[[nodiscard]] PartialResult store(Type memoryType);
[[nodiscard]] PartialResult load(Type memoryType);
[[nodiscard]] PartialResult truncSaturated(Ext1OpType, Type returnType, Type operandType);
[[nodiscard]] PartialResult atomicLoad(ExtAtomicOpType, Type memoryType);
[[nodiscard]] PartialResult atomicStore(ExtAtomicOpType, Type memoryType);
[[nodiscard]] PartialResult atomicBinaryRMW(ExtAtomicOpType, Type memoryType);
[[nodiscard]] PartialResult atomicCompareExchange(ExtAtomicOpType, Type memoryType);
[[nodiscard]] PartialResult atomicWait(ExtAtomicOpType, Type memoryType);
[[nodiscard]] PartialResult atomicNotify(ExtAtomicOpType);
[[nodiscard]] PartialResult atomicFence(ExtAtomicOpType);
#if ENABLE(B3_JIT)
template<bool isReachable, typename = void>
[[nodiscard]] PartialResult simd(SIMDLaneOperation, SIMDLane, SIMDSignMode, B3::Air::Arg optionalRelation = { });
#endif
[[nodiscard]] PartialResult parseTableIndex(unsigned&);
[[nodiscard]] PartialResult parseElementIndex(unsigned&);
[[nodiscard]] PartialResult parseDataSegmentIndex(unsigned&);
[[nodiscard]] PartialResult parseIndexForLocal(uint32_t&);
[[nodiscard]] PartialResult parseIndexForGlobal(uint32_t&);
[[nodiscard]] PartialResult parseFunctionIndex(FunctionSpaceIndex&);
[[nodiscard]] PartialResult parseExceptionIndex(uint32_t&);
[[nodiscard]] PartialResult parseBranchTarget(uint32_t&, uint32_t = 0);
[[nodiscard]] PartialResult parseDelegateTarget(uint32_t&, uint32_t);
struct TableInitImmediates {
unsigned elementIndex;
unsigned tableIndex;
};
[[nodiscard]] PartialResult parseTableInitImmediates(TableInitImmediates&);
struct TableCopyImmediates {
unsigned srcTableIndex;
unsigned dstTableIndex;
};
[[nodiscard]] PartialResult parseTableCopyImmediates(TableCopyImmediates&);
struct AnnotatedSelectImmediates {
unsigned sizeOfAnnotationVector;
Type targetType;
};
[[nodiscard]] PartialResult parseAnnotatedSelectImmediates(AnnotatedSelectImmediates&);
[[nodiscard]] PartialResult parseMemoryFillImmediate(uint8_t&);
[[nodiscard]] PartialResult parseMemoryCopyImmediates(uint8_t&, uint8_t&);
struct MemoryInitImmediates {
unsigned dataSegmentIndex;
unsigned memoryIndex;
};
[[nodiscard]] PartialResult parseMemoryInitImmediates(MemoryInitImmediates&);
[[nodiscard]] PartialResult parseStructTypeIndex(uint32_t& structTypeIndex, ASCIILiteral operation);
[[nodiscard]] PartialResult parseStructFieldIndex(uint32_t& structFieldIndex, const StructType&, ASCIILiteral operation);
struct StructTypeIndexAndFieldIndex {
uint32_t structTypeIndex;
uint32_t fieldIndex;
};
[[nodiscard]] PartialResult parseStructTypeIndexAndFieldIndex(StructTypeIndexAndFieldIndex& result, ASCIILiteral operation);
struct StructFieldManipulation {
StructTypeIndexAndFieldIndex indices;
TypedExpression structReference;
FieldType field;
};
[[nodiscard]] PartialResult parseStructFieldManipulation(StructFieldManipulation& result, ASCIILiteral operation);
#define WASM_TRY_ADD_TO_CONTEXT(add_expression) WASM_FAIL_IF_HELPER_FAILS(m_context.add_expression)
template <typename ...Args>
[[nodiscard]] NEVER_INLINE UnexpectedResult validationFail(const Args&... args) const
{
using namespace FailureHelper; // See ADL comment in WasmParser.h.
if (ASSERT_ENABLED && Options::crashOnFailedWasmValidate())
WTFBreakpointTrap();
StringPrintStream out;
out.print("WebAssembly.Module doesn't validate: "_s, validationFailHelper(args)...);
return UnexpectedResult(out.toString());
}
template <typename Arg>
[[nodiscard]] String validationFailHelper(const Arg& arg) const
{
if constexpr (std::is_same<Arg, Type>())
return typeToStringModuleRelative(arg);
else
return FailureHelper::makeString(arg);
}
String typeToStringModuleRelative(const Type& type) const
{
if (isRefType(type)) {
StringPrintStream out;
out.print("(ref "_s);
if (type.isNullable())
out.print("null "_s);
if (typeIndexIsType(type.index))
out.print(heapTypeKindAsString(static_cast<TypeKind>(type.index)));
// FIXME: use name section if it exists to provide a nicer name.
else {
const auto& typeDefinition = TypeInformation::get(type.index);
const auto& expandedDefinition = typeDefinition.expand();
if (expandedDefinition.is<FunctionSignature>())
out.print("<func:"_s);
else if (expandedDefinition.is<ArrayType>())
out.print("<array:"_s);
else {
ASSERT(expandedDefinition.is<StructType>());
out.print("<struct:"_s);
}
ASSERT(m_info.typeSignatures.contains(Ref { typeDefinition }));
out.print(m_info.typeSignatures.findIf([&](auto& sig) {
return sig.get() == typeDefinition;
}));
out.print(">"_s);
}
out.print(")"_s);
return out.toString();
}
return FailureHelper::makeString(type);
}
#define WASM_VALIDATOR_FAIL_IF(condition, ...) do { \
if (condition) [[unlikely]] \
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
void addReferencedFunctions(const Element&);
[[nodiscard]] PartialResult parseArrayTypeDefinition(ASCIILiteral, bool, uint32_t&, FieldType&, Type&);
[[nodiscard]] PartialResult parseBlockSignatureAndNotifySIMDUseIfNeeded(BlockSignature&);
Context& m_context;
Stack m_expressionStack;
ControlStack m_controlStack;
Vector<Type, 16> m_locals;
const Ref<const TypeDefinition> m_signature;
const ModuleInformation& m_info;
Vector<uint32_t> m_localInitStack;
BitVector m_localInitFlags;
OpType m_currentOpcode { 0 };
uint32_t m_currentExtOp { 0 };
size_t m_currentOpcodeStartingOffset { 0 };
unsigned m_unreachableBlocks { 0 };
unsigned m_loopIndex { 0 };
unsigned m_callProfileIndex { 0 };
};
WTF_MAKE_TZONE_ALLOCATED_TEMPLATE_IMPL(template<typename Context>, FunctionParser<Context>);
template<typename Context>
auto FunctionParser<Context>::parseBlockSignatureAndNotifySIMDUseIfNeeded(BlockSignature& signature) -> PartialResult
{
auto result = parseBlockSignature(m_info, signature);
if (result && signature.hasReturnVector())
m_context.notifyFunctionUsesSIMD();
return result;
}
template<typename ControlType>
static bool isTryOrCatch(ControlType& data)
{
return ControlType::isTry(data) || ControlType::isCatch(data);
}
template<typename Context>
FunctionParser<Context>::FunctionParser(Context& context, std::span<const uint8_t> function, const TypeDefinition& signature, const ModuleInformation& info)
: Parser(function)
, m_context(context)
, m_signature(signature.expand())
, m_info(info)
{
if (verbose)
dataLogLn("Parsing function starting at: ", (uintptr_t)function.data(), " of length: ", function.size(), " with signature: ", signature);
m_context.setParser(this);
}
template<typename Context>
auto FunctionParser<Context>::parse() -> Result
{
uint32_t localGroupsCount;
WASM_PARSER_FAIL_IF(!m_signature->template is<FunctionSignature>(), "type signature was not a function signature"_s);
const auto& signature = *m_signature->template as<FunctionSignature>();
if (signature.numVectors() || signature.numReturnVectors())
m_context.notifyFunctionUsesSIMD();
WASM_ALLOCATOR_FAIL_IF(!m_context.addArguments(m_signature), "can't add "_s, signature.argumentCount(), " arguments to Function"_s);
WASM_PARSER_FAIL_IF(!parseVarUInt32(localGroupsCount), "can't get local groups count"_s);
WASM_ALLOCATOR_FAIL_IF(!m_locals.tryReserveCapacity(signature.argumentCount()), "can't allocate enough memory for function's "_s, signature.argumentCount(), " arguments"_s);
m_locals.appendUsingFunctor(signature.argumentCount(), [&](size_t i) {
return signature.argumentType(i);
});
uint64_t totalNumberOfLocals = signature.argumentCount();
uint64_t totalNonDefaultableLocals = 0;
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 "_s, i);
totalNumberOfLocals += numberOfLocals;
WASM_PARSER_FAIL_IF(totalNumberOfLocals > maxFunctionLocals, "Function's number of locals is too big "_s, totalNumberOfLocals, " maximum "_s, maxFunctionLocals);
WASM_PARSER_FAIL_IF(!parseValueType(m_info, typeOfLocal), "can't get Function local's type in group "_s, i);
if (!isDefaultableType(typeOfLocal)) [[unlikely]]
totalNonDefaultableLocals++;
if (typeOfLocal.isV128())
m_context.notifyFunctionUsesSIMD();
WASM_ALLOCATOR_FAIL_IF(!m_locals.tryReserveCapacity(totalNumberOfLocals), "can't allocate enough memory for function's "_s, totalNumberOfLocals, " locals"_s);
m_locals.appendUsingFunctor(numberOfLocals, [&](size_t) { return typeOfLocal; });
WASM_TRY_ADD_TO_CONTEXT(addLocal(typeOfLocal, numberOfLocals));
}
WASM_ALLOCATOR_FAIL_IF(!m_localInitStack.tryReserveCapacity(totalNonDefaultableLocals), "can't allocate enough memory for tracking function's local initialization"_s);
m_localInitFlags.ensureSize(totalNumberOfLocals);
// Param locals are always considered initialized, so we need to pre-set them.
for (uint32_t i = 0; i < signature.argumentCount(); ++i) {
if (!isDefaultableType(signature.argumentType(i)))
m_localInitFlags.quickSet(i);
}
m_context.didFinishParsingLocals();
WASM_FAIL_IF_HELPER_FAILS(parseBody());
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseConstantExpression() -> Result
{
WASM_PARSER_FAIL_IF(!m_signature->template is<FunctionSignature>(), "type signature was not a function signature"_s);
const auto& signature = *m_signature->template as<FunctionSignature>();
if (signature.numVectors() || signature.numReturnVectors())
m_context.notifyFunctionUsesSIMD();
ASSERT(!signature.argumentCount());
WASM_FAIL_IF_HELPER_FAILS(parseBody());
return { };
}
template<typename Context>
auto FunctionParser<Context>::parseBody() -> PartialResult
{
const auto& functionSignature = *m_signature->template as<FunctionSignature>();
m_controlStack.append({ { }, { }, 0, m_context.addTopLevel(BlockSignature { functionSignature }) });
uint8_t op = 0;
while (m_controlStack.size()) {
m_currentOpcodeStartingOffset = m_offset;
WASM_PARSER_FAIL_IF(!parseUInt8(op), "can't decode opcode"_s);
WASM_PARSER_FAIL_IF(!isValidOpType(op), "invalid opcode "_s, op);
m_currentOpcode = static_cast<OpType>(op);
#if ENABLE(WEBASSEMBLY_OMGJIT)
if (Options::dumpWasmOpcodeStatistics()) [[unlikely]]
WasmOpcodeCounter::singleton().increment(m_currentOpcode);
#endif
if constexpr (verbose) {
String extOpString;
auto computeExtOpString = [&]<typename ExtOpEnum>() {
uint32_t extOpBits;
if (peekVarUInt32(extOpBits))
extOpString = makeString("::"_s, makeString(static_cast<ExtOpEnum>(extOpBits)));
else
extOpString = makeString(" with invalid extension: "_s, extOpBits);
};
#define HANDLE_EXT_OP_PREFIX(name, id, b3, hasExtra, ExtOpEnumType) \
if (m_currentOpcode == OpType::name) \
computeExtOpString.template operator()<ExtOpEnumType>();
FOR_EACH_WASM_EXT_PREFIX_OP_WITH_ENUM(HANDLE_EXT_OP_PREFIX)
#undef HANDLE_EXT_OP_PREFIX
dataLogLn("processing op (", m_unreachableBlocks, "): ", RawHex(m_currentOpcode), ", ", makeString(static_cast<OpType>(m_currentOpcode)), extOpString, " at offset: ", RawHex(m_currentOpcodeStartingOffset));
m_context.dump(m_controlStack, &m_expressionStack);
}
m_context.willParseOpcode();
if (m_unreachableBlocks)
WASM_FAIL_IF_HELPER_FAILS(parseUnreachableExpression());
else
WASM_FAIL_IF_HELPER_FAILS(parseExpression());
m_context.didParseOpcode();
}
WASM_FAIL_IF_HELPER_FAILS(m_context.endTopLevel(m_expressionStack));
if (Context::validateFunctionBodySize)
WASM_PARSER_FAIL_IF(m_offset != source().size(), "function body size doesn't match the expected size");
ASSERT(op == OpType::End);
return { };
}
template<typename Context>
auto FunctionParser<Context>::binaryCase(OpType op, BinaryOperationHandler handler, Type returnType, Type lhsType, Type rhsType) -> PartialResult
{
TypedExpression right;
TypedExpression left;
WASM_TRY_POP_EXPRESSION_STACK_INTO(right, "binary right"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(left, "binary left"_s);
WASM_VALIDATOR_FAIL_IF(left.type() != lhsType, op, " left value type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(right.type() != rhsType, op, " right value type mismatch"_s);
ExpressionType result;
WASM_FAIL_IF_HELPER_FAILS((m_context.*handler)(left, right, result));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::binaryCompareCase(OpType op, BinaryOperationHandler handler, Type returnType, Type lhsType, Type rhsType) -> PartialResult
{
TypedExpression right;
TypedExpression left;
WASM_TRY_POP_EXPRESSION_STACK_INTO(right, "binary right"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(left, "binary left"_s);
WASM_VALIDATOR_FAIL_IF(left.type() != lhsType, op, " left value type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(right.type() != rhsType, op, " right value type mismatch"_s);
uint8_t nextOpcode;
if (Context::shouldFuseBranchCompare && peekUInt8(nextOpcode)) {
if (nextOpcode == OpType::BrIf) {
m_currentOpcodeStartingOffset = m_offset;
m_currentOpcode = static_cast<OpType>(nextOpcode);
bool didParseNextOpcode = parseUInt8(nextOpcode); // We're going to fuse this compare to the branch, so we consume the opcode.
ASSERT_UNUSED(didParseNextOpcode, didParseNextOpcode);
m_context.willParseOpcode();
uint32_t target;
WASM_FAIL_IF_HELPER_FAILS(parseBranchTarget(target));
ControlType& data = m_controlStack[m_controlStack.size() - 1 - target].controlData;
WASM_FAIL_IF_HELPER_FAILS(checkBranchTarget(data, Conditional));
WASM_TRY_ADD_TO_CONTEXT(addFusedBranchCompare(op, data, left, right, m_expressionStack));
m_context.didParseOpcode();
return { };
}
if (nextOpcode == OpType::If) {
m_currentOpcodeStartingOffset = m_offset;
m_currentOpcode = static_cast<OpType>(nextOpcode);
bool didParseNextOpcode = parseUInt8(nextOpcode); // We're going to fuse this compare to the branch, so we consume the opcode.
ASSERT_UNUSED(didParseNextOpcode, didParseNextOpcode);
m_context.willParseOpcode();
BlockSignature inlineSignature;
WASM_PARSER_FAIL_IF(!parseBlockSignatureAndNotifySIMDUseIfNeeded(inlineSignature), "can't get if's signature"_s);
WASM_VALIDATOR_FAIL_IF(m_expressionStack.size() < inlineSignature.argumentCount(), "Too few arguments on stack for if block. If expects ", inlineSignature.argumentCount(), ", but only ", m_expressionStack.size(), " were present. If block has signature: ", inlineSignature);
unsigned offset = m_expressionStack.size() - inlineSignature.argumentCount();
for (unsigned i = 0; i < inlineSignature.argumentCount(); ++i)
WASM_VALIDATOR_FAIL_IF(!isSubtype(m_expressionStack[offset + i].type(), inlineSignature.argumentType(i)), "Loop expects the argument at index", i, " to be ", inlineSignature.argumentType(i), " but argument has type ", m_expressionStack[i].type());
int64_t oldSize = m_expressionStack.size();
Stack newStack;
ControlType control;
WASM_TRY_ADD_TO_CONTEXT(addFusedIfCompare(op, left, right, WTF::move(inlineSignature), m_expressionStack, control, newStack));
ASSERT_UNUSED(oldSize, oldSize - m_expressionStack.size() == control.signature().argumentCount());
ASSERT(newStack.size() == control.signature().argumentCount());
m_controlStack.append({ WTF::move(m_expressionStack), newStack, getLocalInitStackHeight(), WTF::move(control) });
m_expressionStack = WTF::move(newStack);
m_context.didParseOpcode();
return { };
}
}
ExpressionType result;
WASM_FAIL_IF_HELPER_FAILS((m_context.*handler)(left, right, result));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::unaryCase(OpType op, UnaryOperationHandler handler, Type returnType, Type operandType) -> PartialResult
{
TypedExpression value;
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "unary"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != operandType, op, " value type mismatch"_s);
ExpressionType result;
WASM_FAIL_IF_HELPER_FAILS((m_context.*handler)(value, result));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::unaryCompareCase(OpType op, UnaryOperationHandler handler, Type returnType, Type operandType) -> PartialResult
{
TypedExpression value;
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "unary"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != operandType, op, " value type mismatch"_s);
uint8_t nextOpcode;
if (Context::shouldFuseBranchCompare && peekUInt8(nextOpcode)) {
if (nextOpcode == OpType::BrIf) {
bool didParseNextOpcode = parseUInt8(nextOpcode); // We're going to fuse this compare to the branch, so we consume the opcode.
ASSERT_UNUSED(didParseNextOpcode, didParseNextOpcode);
uint32_t target;
WASM_FAIL_IF_HELPER_FAILS(parseBranchTarget(target));
ControlType& data = m_controlStack[m_controlStack.size() - 1 - target].controlData;
WASM_FAIL_IF_HELPER_FAILS(checkBranchTarget(data, Conditional));
WASM_TRY_ADD_TO_CONTEXT(addFusedBranchCompare(op, data, value, m_expressionStack));
return { };
}
if (nextOpcode == OpType::If) {
bool didParseNextOpcode = parseUInt8(nextOpcode); // We're going to fuse this compare to the if, so we consume the opcode.
ASSERT_UNUSED(didParseNextOpcode, didParseNextOpcode);
BlockSignature inlineSignature;
WASM_PARSER_FAIL_IF(!parseBlockSignatureAndNotifySIMDUseIfNeeded(inlineSignature), "can't get if's signature"_s);
WASM_VALIDATOR_FAIL_IF(m_expressionStack.size() < inlineSignature.argumentCount(), "Too few arguments on stack for if block. If expects ", inlineSignature.argumentCount(), ", but only ", m_expressionStack.size(), " were present. If block has signature: ", inlineSignature);
unsigned offset = m_expressionStack.size() - inlineSignature.argumentCount();
for (unsigned i = 0; i < inlineSignature.argumentCount(); ++i)
WASM_VALIDATOR_FAIL_IF(!isSubtype(m_expressionStack[offset + i].type(), inlineSignature.argumentType(i)), "Loop expects the argument at index", i, " to be ", inlineSignature.argumentType(i), " but argument has type ", m_expressionStack[i].type());
int64_t oldSize = m_expressionStack.size();
Stack newStack;
ControlType control;
WASM_TRY_ADD_TO_CONTEXT(addFusedIfCompare(op, value, WTF::move(inlineSignature), m_expressionStack, control, newStack));
ASSERT_UNUSED(oldSize, oldSize - m_expressionStack.size() == control.signature().argumentCount());
ASSERT(newStack.size() == control.signature().argumentCount());
m_controlStack.append({ WTF::move(m_expressionStack), newStack, getLocalInitStackHeight(), WTF::move(control) });
m_expressionStack = WTF::move(newStack);
return { };
}
}
ExpressionType result;
WASM_FAIL_IF_HELPER_FAILS((m_context.*handler)(value, result));
m_expressionStack.constructAndAppend(returnType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::load(Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memoryCount(), "load instruction without memory"_s);
uint32_t alignment;
uint64_t offset;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment"_s);
bool hasMemoryIndex = alignment & (1 << 6);
alignment = alignment & 0b111111;
WASM_PARSER_FAIL_IF(alignment > memoryLog2Alignment(m_currentOpcode), "byte alignment "_s, 1ull << alignment, " exceeds load's natural alignment "_s, 1ull << memoryLog2Alignment(m_currentOpcode));
uint32_t memoryIndex = 0;
if (hasMemoryIndex) {
WASM_PARSER_FAIL_IF(!parseVarUInt32(memoryIndex), "can't get memory index"_s);
RELEASE_ASSERT(memoryIndex < 256);
WASM_VALIDATOR_FAIL_IF(memoryIndex >= m_info.memoryCount(), "memory index "_s, memoryIndex, " is out of range"_s);
}
if (m_info.memory(memoryIndex).isMemory64())
WASM_PARSER_FAIL_IF(!parseVarUInt64(offset), "can't get load offset"_s);
else {
uint32_t offset32;
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset32), "can't get load offset"_s);
offset = offset32;
}
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "load pointer"_s);
if (m_info.memory(memoryIndex).isMemory64())
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI64(), m_currentOpcode, " pointer type mismatch"_s);
else
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), m_currentOpcode, " pointer type mismatch"_s);
ExpressionType result;
WASM_TRY_ADD_TO_CONTEXT(load(static_cast<LoadOpType>(m_currentOpcode), pointer, result, offset, static_cast<uint8_t>(memoryIndex)));
m_expressionStack.constructAndAppend(memoryType, result);
return { };
}
template<typename Context>
auto FunctionParser<Context>::store(Type memoryType) -> PartialResult
{
WASM_VALIDATOR_FAIL_IF(!m_info.memoryCount(), "store instruction without memory"_s);
uint32_t alignment;
uint64_t offset;
TypedExpression value;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get store alignment"_s);
bool hasMemoryIndex = alignment & (1 << 6);
alignment = alignment & 0b111111;
WASM_PARSER_FAIL_IF(alignment > memoryLog2Alignment(m_currentOpcode), "byte alignment "_s, 1ull << alignment, " exceeds store's natural alignment "_s, 1ull << memoryLog2Alignment(m_currentOpcode));
uint32_t memoryIndex = 0;
if (hasMemoryIndex) {
WASM_PARSER_FAIL_IF(!parseVarUInt32(memoryIndex), "can't get memory index"_s);
RELEASE_ASSERT(memoryIndex < 256);
WASM_VALIDATOR_FAIL_IF(memoryIndex >= m_info.memoryCount(), "memory index "_s, memoryIndex, " is out of range"_s);
}
if (m_info.memory(memoryIndex).isMemory64())
WASM_PARSER_FAIL_IF(!parseVarUInt64(offset), "can't get store offset"_s);
else {
uint32_t offset32;
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset32), "can't get store offset"_s);
offset = offset32;
}
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "store value"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "store pointer"_s);
if (m_info.memory(memoryIndex).isMemory64())
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI64(), m_currentOpcode, " pointer type mismatch"_s);
else
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), m_currentOpcode, " pointer type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, m_currentOpcode, " value type mismatch"_s);
WASM_TRY_ADD_TO_CONTEXT(store(static_cast<StoreOpType>(m_currentOpcode), pointer, value, offset, static_cast<uint8_t>(memoryIndex)));
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"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != operandType, "trunc-saturated value type mismatch. Expected: "_s, operandType, " but expression stack has "_s, value.type());
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.memoryCount(), "atomic instruction without memory"_s);
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment"_s);
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment "_s, 1ull << alignment, " does not match against atomic op's natural alignment "_s, 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "load pointer"_s);
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch"_s);
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.memoryCount(), "atomic instruction without memory"_s);
uint32_t alignment;
uint32_t offset;
TypedExpression value;
TypedExpression pointer;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get store alignment"_s);
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment "_s, 1ull << alignment, " does not match against atomic op's natural alignment "_s, 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get store offset"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "store value"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "store pointer"_s);
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), m_currentOpcode, " pointer type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, m_currentOpcode, " value type mismatch"_s);
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.memoryCount(), "atomic instruction without memory"_s);
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression value;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment"_s);
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment "_s, 1ull << alignment, " does not match against atomic op's natural alignment "_s, 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "value"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer"_s);
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, static_cast<unsigned>(op), " value type mismatch"_s);
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.memoryCount(), "atomic instruction without memory"_s);
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression expected;
TypedExpression value;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment"_s);
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment "_s, 1ull << alignment, " does not match against atomic op's natural alignment "_s, 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "value"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(expected, "expected"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer"_s);
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(expected.type() != memoryType, static_cast<unsigned>(op), " expected type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, static_cast<unsigned>(op), " value type mismatch"_s);
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.memoryCount(), "atomic instruction without memory"_s);
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression value;
TypedExpression timeout;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment"_s);
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment "_s, 1ull << alignment, " does not match against atomic op's natural alignment "_s, 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(timeout, "timeout"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(value, "value"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer"_s);
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(value.type() != memoryType, static_cast<unsigned>(op), " value type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(!timeout.type().isI64(), static_cast<unsigned>(op), " timeout type mismatch"_s);
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.memoryCount(), "atomic instruction without memory"_s);
uint32_t alignment;
uint32_t offset;
TypedExpression pointer;
TypedExpression count;
WASM_PARSER_FAIL_IF(!parseVarUInt32(alignment), "can't get load alignment"_s);
WASM_PARSER_FAIL_IF(alignment != memoryLog2Alignment(op), "byte alignment "_s, 1ull << alignment, " does not match against atomic op's natural alignment "_s, 1ull << memoryLog2Alignment(op));
WASM_PARSER_FAIL_IF(!parseVarUInt32(offset), "can't get load offset"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(count, "count"_s);
WASM_TRY_POP_EXPRESSION_STACK_INTO(pointer, "pointer"_s);
WASM_VALIDATOR_FAIL_IF(!pointer.type().isI32(), static_cast<unsigned>(op), " pointer type mismatch"_s);
WASM_VALIDATOR_FAIL_IF(!count.type().isI32(), static_cast<unsigned>(op), " count type mismatch"_s); // 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"_s);
WASM_PARSER_FAIL_IF(flags != 0x0, "flags should be 0x0 but got "_s, flags);
WASM_TRY_ADD_TO_CONTEXT(atomicFence(op, flags));
return { };
}
#if ENABLE(B3_JIT)
template<typename Context>
template<bool isReachable, typename>
auto FunctionParser<Context>::simd(SIMDLaneOperation op, SIMDLane lane, SIMDSignMode signMode, B3::Air::Arg optionalRelation) -> PartialResult
{
UNUSED_PARAM(signMode);
m_context.notifyFunctionUsesSIMD();
auto pushUnreachable = [&](auto type) -> PartialResult {
ASSERT(isReachable);
// Appease generators without SIMD support.
m_expressionStack.constructAndAppend(type, m_context.addConstant(Types::F64, 0));
return { };
};
// only used in some specializations
UNUSED_VARIABLE(pushUnreachable);
UNUSED_PARAM(optionalRelation);
auto parseMemOp = [&] (uint32_t& offset, TypedExpression& pointer) -> PartialResult {
uint32_t maxAlignment;
switch (op) {
case SIMDLaneOperation::LoadLane8:
case SIMDLaneOperation::StoreLane8:
case SIMDLaneOperation::LoadSplat8:
maxAlignment = 0;
break;
case SIMDLaneOperation::LoadLane16:
case SIMDLaneOperation::StoreLane16:
case SIMDLaneOperation::LoadSplat16:
maxAlignment = 1;
break;
case SIMDLaneOperation::LoadLane32:
case SIMDLaneOperation::StoreLane32:
case SIMDLaneOperation::LoadSplat32:
case SIMDLaneOperation::LoadPad32:
maxAlignment = 2;
break;
case SIMDLaneOperation::LoadLane64:
case SIMDLaneOperation::StoreLane64:
case SIMDLaneOperation::LoadSplat64:
case SIMDLaneOperation::LoadPad64:
case SIMDLaneOperation::LoadExtend8U: