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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
|
#include "WAVM/IR/Validate.h"
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include "WAVM/IR/FeatureSpec.h"
#include "WAVM/IR/IR.h"
#include "WAVM/IR/Module.h"
#include "WAVM/IR/OperatorPrinter.h"
#include "WAVM/IR/OperatorSignatures.h"
#include "WAVM/IR/Operators.h"
#include "WAVM/IR/Types.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Errors.h"
#include "WAVM/Inline/Hash.h"
#include "WAVM/Inline/HashSet.h"
#include "WAVM/Logging/Logging.h"
using namespace WAVM;
using namespace WAVM::IR;
#define VALIDATE_UNLESS(reason, comparison) \
if(comparison) { throw ValidationException(reason #comparison); }
#define VALIDATE_INDEX(index, arraySize) \
if(index >= arraySize) \
{ \
throw ValidationException( \
std::string("invalid index: " #index " must be less than " #arraySize " (" #index "=") \
+ std::to_string(index) + (", " #arraySize "=") + std::to_string(arraySize) + ')'); \
}
#define VALIDATE_FEATURE(context, feature) \
if(!module.featureSpec.feature) \
{ \
throw ValidationException(std::string(context) + " requires " \
+ getFeatureName(Feature::feature) + " feature"); \
}
namespace WAVM {
template<> struct Hash<KindAndIndex>
{
Uptr operator()(const KindAndIndex& kindAndIndex, Uptr seed = 0) const
{
Uptr hash = seed;
hash = Hash<Uptr>()(Uptr(kindAndIndex.kind), hash);
hash = Hash<Uptr>()(kindAndIndex.index, hash);
return hash;
}
};
}
namespace WAVM { namespace IR {
struct ModuleValidationState
{
const IR::Module& module;
HashSet<KindAndIndex> declaredExternRefs;
ModuleValidationState(const IR::Module& inModule) : module(inModule) {}
};
}}
std::shared_ptr<ModuleValidationState> IR::createModuleValidationState(const IR::Module& module)
{
return std::make_shared<ModuleValidationState>(module);
}
static void validate(const IR::Module& module, IR::ValueType valueType)
{
switch(valueType)
{
case ValueType::i32:
case ValueType::i64:
case ValueType::f32:
case ValueType::f64: WAVM_ASSERT(module.featureSpec.mvp); break;
case ValueType::v128: VALIDATE_FEATURE("v128 value type", simd) break;
case ValueType::externref:
case ValueType::funcref: VALIDATE_FEATURE(asString(valueType), referenceTypes) break;
case ValueType::none:
case ValueType::any:
default:
throw ValidationException("invalid value type (" + std::to_string((Uptr)valueType) + ")");
};
}
static void validate(SizeConstraints size, U64 maxMax)
{
U64 max = size.max == UINT64_MAX ? maxMax : size.max;
VALIDATE_UNLESS("disjoint size bounds: ", size.min > max);
VALIDATE_UNLESS("maximum size exceeds limit: ", max > maxMax);
}
static void validate(const IR::Module& module, ReferenceType type)
{
switch(type)
{
case ReferenceType::funcref: break;
case ReferenceType::externref: VALIDATE_FEATURE(asString(type), referenceTypes); break;
case ReferenceType::none:
default:
throw ValidationException("invalid reference type (" + std::to_string((Uptr)type) + ")");
}
}
static void validate(const Module& module, TableType type)
{
validate(module, type.elementType);
validate(type.size,
type.indexType == IndexType::i32 ? IR::maxTable32Elems : IR::maxTable64Elems);
if(type.isShared)
{
VALIDATE_FEATURE("shared table", sharedTables);
VALIDATE_UNLESS("shared tables must have a maximum size: ", type.size.max == UINT64_MAX);
}
if(type.indexType != IndexType::i32) { VALIDATE_FEATURE("64-bit table indices", table64); }
}
static void validate(const Module& module, MemoryType type)
{
validate(type.size,
type.indexType == IndexType::i32 ? IR::maxMemory32Pages : IR::maxMemory64Pages);
if(type.isShared)
{
VALIDATE_FEATURE("shared memory", atomics);
VALIDATE_UNLESS("shared memories must have a maximum size: ", type.size.max == UINT64_MAX);
}
if(type.indexType != IndexType::i32) { VALIDATE_FEATURE("64-bit memory addresses", memory64); }
}
static void validate(const Module& module, GlobalType type) { validate(module, type.valueType); }
static void validate(const Module& module, TypeTuple typeTuple)
{
for(ValueType valueType : typeTuple) { validate(module, valueType); }
}
template<typename Type> void validateType(Type expectedType, Type actualType, const char* context)
{
if(!isSubtype(actualType, expectedType))
{
throw ValidationException(std::string("type mismatch: expected ") + asString(expectedType)
+ " but got " + asString(actualType) + " in " + context);
}
}
static void validateExternKind(const Module& module, ExternKind externKind)
{
switch(externKind)
{
case ExternKind::function:
case ExternKind::table:
case ExternKind::memory:
case ExternKind::global: break;
case ExternKind::exceptionType:
VALIDATE_FEATURE("exception type extern", exceptionHandling);
break;
case ExternKind::invalid:
default:
throw ValidationException("invalid extern kind (" + std::to_string(Uptr(externKind)) + ")");
};
}
static ValueType validateGlobalIndex(const Module& module,
Uptr globalIndex,
bool mustBeMutable,
bool mustBeImmutable,
bool mustBeImport,
const char* context)
{
VALIDATE_INDEX(globalIndex, module.globals.size());
const GlobalType& globalType = module.globals.getType(globalIndex);
if(mustBeMutable && !globalType.isMutable)
{ throw ValidationException("attempting to mutate immutable global"); }
else if(mustBeImport && globalIndex >= module.globals.imports.size())
{
throw ValidationException(
"global variable initializer expression may only access imported globals");
}
else if(mustBeImmutable && globalType.isMutable)
{
throw ValidationException(
"global variable initializer expression may only access immutable globals");
}
return globalType.valueType;
}
static FunctionType validateFunctionIndex(const Module& module, Uptr functionIndex)
{
VALIDATE_INDEX(functionIndex, module.functions.size());
return module.types[module.functions.getType(functionIndex).index];
}
static void validateFunctionRef(const ModuleValidationState& state, Uptr functionIndex)
{
validateFunctionIndex(state.module, functionIndex);
}
static void validateFunctionRefIsDeclared(const ModuleValidationState& state, Uptr functionIndex)
{
if(!state.declaredExternRefs.contains(KindAndIndex{ExternKind::function, functionIndex}))
{
throw ValidationException(
"function " + std::to_string(functionIndex)
+ " must be an import, exported, or present in an elem segment or global initializer"
+ " to be used as the operand to ref.func");
}
}
static FunctionType validateBlockType(const Module& module, const IndexedBlockType& type)
{
switch(type.format)
{
case IndexedBlockType::noParametersOrResult: return FunctionType();
case IndexedBlockType::oneResult:
validate(module, type.resultType);
return FunctionType(TypeTuple(type.resultType));
case IndexedBlockType::functionType: {
VALIDATE_INDEX(type.index, module.types.size());
FunctionType functionType = module.types[type.index];
if(functionType.params().size() > 0)
{ VALIDATE_FEATURE("block with params", multipleResultsAndBlockParams); }
else if(functionType.results().size() > 1)
{
VALIDATE_FEATURE("block with multiple results", multipleResultsAndBlockParams);
}
else if(functionType.callingConvention() != CallingConvention::wasm)
{
throw ValidationException("invalid calling convention for block");
}
return functionType;
}
default: WAVM_UNREACHABLE();
}
}
static FunctionType validateFunctionType(const Module& module, const IndexedFunctionType& type)
{
VALIDATE_INDEX(type.index, module.types.size());
const FunctionType functionType = module.types[type.index];
if(functionType.results().size() > IR::maxReturnValues)
{ throw ValidationException("function has more return values than WAVM can support"); }
return functionType;
}
static void validateInitializer(const ModuleValidationState& state,
const InitializerExpression& expression,
ValueType expectedType,
const char* context)
{
const Module& module = state.module;
switch(expression.type)
{
case InitializerExpression::Type::i32_const:
validateType(expectedType, ValueType::i32, context);
break;
case InitializerExpression::Type::i64_const:
validateType(expectedType, ValueType::i64, context);
break;
case InitializerExpression::Type::f32_const:
validateType(expectedType, ValueType::f32, context);
break;
case InitializerExpression::Type::f64_const:
validateType(expectedType, ValueType::f64, context);
break;
case InitializerExpression::Type::v128_const:
validateType(expectedType, ValueType::v128, context);
break;
case InitializerExpression::Type::global_get: {
const ValueType globalValueType = validateGlobalIndex(
module, expression.ref, false, true, true, "initializer expression global index");
validateType(expectedType, globalValueType, context);
break;
}
case InitializerExpression::Type::ref_null:
validateType(expectedType, asValueType(expression.nullReferenceType), context);
break;
case InitializerExpression::Type::ref_func: {
validateFunctionRef(state, expression.ref);
validateType(expectedType, ValueType::funcref, context);
break;
}
case InitializerExpression::Type::invalid:
default: throw ValidationException("invalid initializer expression");
};
}
struct FunctionValidationContext
{
const bool enableTracing{Log::isCategoryEnabled(Log::traceValidation)};
FunctionValidationContext(ModuleValidationState& inModuleValidationState,
const FunctionDef& inFunctionDef)
: module(inModuleValidationState.module)
, functionDef(inFunctionDef)
, functionType(inModuleValidationState.module.types[inFunctionDef.type.index])
, moduleValidationState(inModuleValidationState)
{
// Validate the function's local types.
for(auto localType : functionDef.nonParameterLocalTypes) { validate(module, localType); }
// Initialize the local types.
locals.reserve(functionType.params().size() + functionDef.nonParameterLocalTypes.size());
locals.insert(locals.end(), functionType.params().begin(), functionType.params().end());
locals.insert(locals.end(),
functionDef.nonParameterLocalTypes.begin(),
functionDef.nonParameterLocalTypes.end());
// Log the start of the function and its signature+locals.
if(enableTracing)
{
traceOperator("func");
for(auto param : functionType.params())
{ traceOperator(std::string("param ") + asString(param)); }
for(auto result : functionType.results())
{ traceOperator(std::string("result ") + asString(result)); }
for(auto local : functionDef.nonParameterLocalTypes)
{ traceOperator(std::string("local ") + asString(local)); }
}
// Push the function context onto the control stack.
pushControlStack(
ControlContext::Type::function, functionType.results(), functionType.results());
}
Uptr getControlStackSize() { return controlStack.size(); }
void validateNonEmptyControlStack(const char* context)
{
if(controlStack.size() == 0)
{
throw ValidationException(std::string("Expected non-empty control stack in ")
+ context);
}
}
void traceOperator(const std::string& operatorDescription)
{
std::string controlStackString;
for(Uptr stackIndex = 0; stackIndex < controlStack.size(); ++stackIndex)
{
if(!controlStack[stackIndex].isReachable) { controlStackString += "("; }
switch(controlStack[stackIndex].type)
{
case ControlContext::Type::function: controlStackString += "F"; break;
case ControlContext::Type::block: controlStackString += "B"; break;
case ControlContext::Type::ifThen: controlStackString += "T"; break;
case ControlContext::Type::ifElse: controlStackString += "E"; break;
case ControlContext::Type::loop: controlStackString += "L"; break;
case ControlContext::Type::try_: controlStackString += "R"; break;
case ControlContext::Type::catch_: controlStackString += "C"; break;
default: WAVM_UNREACHABLE();
};
if(!controlStack[stackIndex].isReachable) { controlStackString += ")"; }
}
std::string stackString;
const Uptr stackBase = controlStack.size() == 0 ? 0 : controlStack.back().outerStackSize;
for(Uptr stackIndex = 0; stackIndex < stack.size(); ++stackIndex)
{
if(stackIndex == stackBase) { stackString += "| "; }
stackString += asString(stack[stackIndex]);
stackString += " ";
}
if(stack.size() == stackBase) { stackString += "|"; }
Log::printf(Log::traceValidation,
"%-50s %-50s %-50s\n",
controlStackString.c_str(),
operatorDescription.c_str(),
stackString.c_str());
}
// Operation dispatch methods.
void block(ControlStructureImm imm)
{
const FunctionType type = validateBlockType(module, imm.type);
popAndValidateTypeTuple("block arguments", type.params());
pushControlStack(ControlContext::Type::block, type.results(), type.results());
pushOperandTuple(type.params());
}
void loop(ControlStructureImm imm)
{
const FunctionType type = validateBlockType(module, imm.type);
popAndValidateTypeTuple("loop arguments", type.params());
pushControlStack(ControlContext::Type::loop, type.params(), type.results());
pushOperandTuple(type.params());
}
void if_(ControlStructureImm imm)
{
const FunctionType type = validateBlockType(module, imm.type);
popAndValidateOperand("if condition", ValueType::i32);
popAndValidateTypeTuple("if arguments", type.params());
pushControlStack(
ControlContext::Type::ifThen, type.results(), type.results(), type.params());
pushOperandTuple(type.params());
}
void else_(NoImm imm)
{
WAVM_ASSERT(controlStack.size());
if(controlStack.back().type != ControlContext::Type::ifThen)
{ throw ValidationException("else only allowed in if context"); }
popAndValidateTypeTuple("if result", controlStack.back().results);
validateStackEmptyAtEndOfControlStructure();
controlStack.back().type = ControlContext::Type::ifElse;
controlStack.back().isReachable = true;
pushOperandTuple(controlStack.back().elseParams);
}
void end(NoImm)
{
WAVM_ASSERT(controlStack.size());
if(controlStack.back().type == ControlContext::Type::try_)
{ throw ValidationException("end may not occur in try context"); }
TypeTuple results = controlStack.back().results;
if(controlStack.back().type == ControlContext::Type::ifThen
&& results != controlStack.back().elseParams)
{ throw ValidationException("else-less if must have identity signature"); }
popAndValidateTypeTuple("end result", controlStack.back().results);
validateStackEmptyAtEndOfControlStructure();
controlStack.pop_back();
if(controlStack.size()) { pushOperandTuple(results); }
}
void try_(ControlStructureImm imm)
{
const FunctionType type = validateBlockType(module, imm.type);
VALIDATE_FEATURE("try", exceptionHandling);
popAndValidateTypeTuple("try arguments", type.params());
pushControlStack(ControlContext::Type::try_, type.results(), type.results());
pushOperandTuple(type.params());
}
void validateCatch()
{
WAVM_ASSERT(controlStack.size());
popAndValidateTypeTuple("try result", controlStack.back().results);
validateStackEmptyAtEndOfControlStructure();
if(controlStack.back().type == ControlContext::Type::try_
|| controlStack.back().type == ControlContext::Type::catch_)
{
controlStack.back().type = ControlContext::Type::catch_;
controlStack.back().isReachable = true;
}
else
{
throw ValidationException("catch only allowed in try/catch context");
}
}
void catch_(ExceptionTypeImm imm)
{
VALIDATE_FEATURE("catch", exceptionHandling);
VALIDATE_INDEX(imm.exceptionTypeIndex, module.exceptionTypes.size());
const ExceptionType& type = module.exceptionTypes.getType(imm.exceptionTypeIndex);
validateCatch();
for(auto param : type.params) { pushOperand(param); }
}
void catch_all(NoImm)
{
VALIDATE_FEATURE("catch_all", exceptionHandling);
validateCatch();
}
void delegate(DelegateImm imm)
{
VALIDATE_FEATURE("delegate", exceptionHandling);
WAVM_ASSERT(controlStack.size());
if(controlStack.back().type != ControlContext::Type::try_)
{ throw ValidationException("delegate may occur only in try context"); }
TypeTuple results = controlStack.back().results;
popAndValidateTypeTuple("end result", controlStack.back().results);
validateStackEmptyAtEndOfControlStructure();
controlStack.pop_back();
if(controlStack.size()) { pushOperandTuple(results); }
}
void return_(NoImm)
{
popAndValidateTypeTuple("ret", functionType.results());
enterUnreachable();
}
void br(BranchImm imm)
{
popAndValidateTypeTuple("br argument", getBranchTargetByDepth(imm.targetDepth).params);
enterUnreachable();
}
void br_table(BranchTableImm imm)
{
popAndValidateOperand("br_table index", ValueType::i32);
const TypeTuple defaultTargetParams = getBranchTargetByDepth(imm.defaultTargetDepth).params;
// Validate that each target has the same number of parameters as the default target, and
// that the parameters for each target match the arguments provided.
WAVM_ASSERT(imm.branchTableIndex < functionDef.branchTables.size());
const std::vector<Uptr>& targetDepths = functionDef.branchTables[imm.branchTableIndex];
for(Uptr targetIndex = 0; targetIndex < targetDepths.size(); ++targetIndex)
{
const ControlContext& branchTarget = getBranchTargetByDepth(targetDepths[targetIndex]);
const TypeTuple targetParams = branchTarget.params;
if(targetParams.size() != defaultTargetParams.size())
{
throw ValidationException(
"br_table targets must all take the same number of parameters");
}
else
{
peekAndValidateTypeTuple("br_table case argument", targetParams);
}
}
popAndValidateTypeTuple("br_table argument", defaultTargetParams);
enterUnreachable();
}
void br_if(BranchImm imm)
{
const TypeTuple targetParams = getBranchTargetByDepth(imm.targetDepth).params;
popAndValidateOperand("br_if condition", ValueType::i32);
popAndValidateTypeTuple("br_if argument", targetParams);
pushOperandTuple(targetParams);
}
void unreachable(NoImm) { enterUnreachable(); }
void drop(NoImm) { popAndValidateOperand("drop", ValueType::any); }
void select(SelectImm imm)
{
popAndValidateOperand("select condition", ValueType::i32);
if(imm.type == ValueType::any)
{
const ValueType falseType = popAndValidateOperand("select false value", ValueType::any);
const ValueType trueType = popAndValidateOperand("select true value", ValueType::any);
VALIDATE_UNLESS("non-typed select operands must be numeric types: ",
(falseType != ValueType::none && !isNumericType(falseType))
|| (trueType != ValueType::none && !isNumericType(trueType)))
if(falseType == ValueType::none) { pushOperand(trueType); }
else if(trueType == ValueType::none)
{
pushOperand(falseType);
}
else
{
VALIDATE_UNLESS("non-typed select operands must have the same numeric type: ",
falseType != trueType);
pushOperand(falseType);
}
}
else
{
VALIDATE_FEATURE("typed select instruction (0x1c)", referenceTypes);
validate(module, imm.type);
popAndValidateOperand("select false value", imm.type);
popAndValidateOperand("select true value", imm.type);
pushOperand(imm.type);
}
}
void local_get(GetOrSetVariableImm<false> imm)
{
pushOperand(validateLocalIndex(imm.variableIndex));
}
void local_set(GetOrSetVariableImm<false> imm)
{
popAndValidateOperand("local.set", validateLocalIndex(imm.variableIndex));
}
void local_tee(GetOrSetVariableImm<false> imm)
{
const ValueType localType = validateLocalIndex(imm.variableIndex);
const ValueType operandType = popAndValidateOperand("local.tee", localType);
pushOperand(operandType);
}
void global_get(GetOrSetVariableImm<true> imm)
{
pushOperand(
validateGlobalIndex(module, imm.variableIndex, false, false, false, "global.get"));
}
void global_set(GetOrSetVariableImm<true> imm)
{
popAndValidateOperand(
"global.set",
validateGlobalIndex(module, imm.variableIndex, true, false, false, "global.set"));
}
void table_get(TableImm imm)
{
VALIDATE_INDEX(imm.tableIndex, module.tables.size());
const TableType& tableType = module.tables.getType(imm.tableIndex);
popAndValidateOperand("table.get", asValueType(tableType.indexType));
pushOperand(asValueType(tableType.elementType));
}
void table_set(TableImm imm)
{
VALIDATE_INDEX(imm.tableIndex, module.tables.size());
const TableType& tableType = module.tables.getType(imm.tableIndex);
popAndValidateOperands(
"table.get", asValueType(tableType.indexType), asValueType(tableType.elementType));
}
void table_grow(TableImm imm)
{
VALIDATE_INDEX(imm.tableIndex, module.tables.size());
const TableType& tableType = module.tables.getType(imm.tableIndex);
popAndValidateOperands(
"table.grow", asValueType(tableType.elementType), asValueType(tableType.indexType));
pushOperand(ValueType::i32);
}
void table_fill(TableImm imm)
{
VALIDATE_INDEX(imm.tableIndex, module.tables.size());
const TableType& tableType = module.tables.getType(imm.tableIndex);
popAndValidateOperands("table.fill",
ValueType::i32,
asValueType(tableType.elementType),
asValueType(tableType.indexType));
}
void throw_(ExceptionTypeImm imm)
{
VALIDATE_FEATURE("throw", exceptionHandling);
VALIDATE_INDEX(imm.exceptionTypeIndex, module.exceptionTypes.size());
const ExceptionType& exceptionType = module.exceptionTypes.getType(imm.exceptionTypeIndex);
popAndValidateTypeTuple("exception arguments", exceptionType.params);
enterUnreachable();
}
void rethrow(RethrowImm imm)
{
VALIDATE_FEATURE("rethrow", exceptionHandling);
VALIDATE_UNLESS(
"rethrow must target a catch: ",
getBranchTargetByDepth(imm.catchDepth).type != ControlContext::Type::catch_);
enterUnreachable();
}
void ref_null(ReferenceTypeImm imm)
{
validate(module, imm.referenceType);
pushOperand(asValueType(imm.referenceType));
}
void ref_is_null(NoImm)
{
const ValueType operandType = popAndValidateOperand("ref.is_null operand", ValueType::any);
if(!isReferenceType(operandType) && operandType != ValueType::none)
{
throw ValidationException(std::string("expected reference type but got")
+ asString(operandType) + " in ref.is_null operand");
}
pushOperand(ValueType::i32);
}
void call(FunctionImm imm)
{
FunctionType calleeType = validateFunctionIndex(module, imm.functionIndex);
popAndValidateTypeTuple("call arguments", calleeType.params());
pushOperandTuple(calleeType.results());
}
void call_indirect(CallIndirectImm imm)
{
VALIDATE_INDEX(imm.tableIndex, module.tables.size());
const TableType& tableType = module.tables.getType(imm.tableIndex);
VALIDATE_UNLESS("call_indirect requires a table element type of funcref: ",
tableType.elementType != ReferenceType::funcref);
FunctionType calleeType = validateFunctionType(module, imm.type);
popAndValidateOperand("call_indirect function index", asValueType(tableType.indexType));
popAndValidateTypeTuple("call_indirect arguments", calleeType.params());
pushOperandTuple(calleeType.results());
}
void validateImm(NoImm) {}
template<typename nativeType> void validateImm(LiteralImm<nativeType> imm) {}
template<Uptr naturalAlignmentLog2> void validateImm(LoadOrStoreImm<naturalAlignmentLog2> imm)
{
VALIDATE_UNLESS("load or store alignment greater than natural alignment: ",
imm.alignmentLog2 > naturalAlignmentLog2);
VALIDATE_INDEX(imm.memoryIndex, module.memories.size());
const MemoryType& memoryType = module.memories.getType(imm.memoryIndex);
switch(memoryType.indexType)
{
case IndexType::i32:
VALIDATE_UNLESS("load or store offset too large for i32 address",
imm.offset > UINT32_MAX);
break;
case IndexType::i64:
VALIDATE_UNLESS("load or store offset too large for i64 address",
imm.offset > UINT64_MAX);
break;
default: WAVM_UNREACHABLE();
};
}
template<Uptr naturalAlignmentLog2, Uptr numLanes>
void validateImm(LoadOrStoreLaneImm<naturalAlignmentLog2, numLanes> imm)
{
validateImm(static_cast<LoadOrStoreImm<naturalAlignmentLog2>&>(imm));
VALIDATE_UNLESS("invalid lane index: ", imm.laneIndex >= numLanes);
}
void validateImm(MemoryImm imm) { VALIDATE_INDEX(imm.memoryIndex, module.memories.size()); }
void validateImm(MemoryCopyImm imm)
{
VALIDATE_INDEX(imm.sourceMemoryIndex, module.memories.size());
VALIDATE_INDEX(imm.destMemoryIndex, module.memories.size());
}
void validateImm(TableImm imm) { VALIDATE_INDEX(imm.tableIndex, module.tables.size()); }
void validateImm(TableCopyImm imm)
{
VALIDATE_INDEX(imm.sourceTableIndex, module.tables.size());
VALIDATE_INDEX(imm.destTableIndex, module.tables.size());
VALIDATE_UNLESS(
"source table element type must be a subtype of the destination table element type",
!isSubtype(asValueType(module.tables.getType(imm.sourceTableIndex).elementType),
asValueType(module.tables.getType(imm.destTableIndex).elementType)));
}
void validateImm(FunctionRefImm imm)
{
validateFunctionRef(moduleValidationState, imm.functionIndex);
validateFunctionRefIsDeclared(moduleValidationState, imm.functionIndex);
}
template<Uptr numLanes> void validateImm(LaneIndexImm<numLanes> imm)
{
VALIDATE_UNLESS("invalid lane index: ", imm.laneIndex >= numLanes);
}
template<Uptr numLanes> void validateImm(ShuffleImm<numLanes> imm)
{
for(Uptr laneIndex = 0; laneIndex < numLanes; ++laneIndex)
{
VALIDATE_UNLESS("shuffle invalid lane index: ",
imm.laneIndices[laneIndex] >= numLanes * 2);
}
}
template<Uptr naturalAlignmentLog2>
void validateImm(AtomicLoadOrStoreImm<naturalAlignmentLog2> imm)
{
VALIDATE_UNLESS("atomic memory operators must have natural alignment: ",
imm.alignmentLog2 != naturalAlignmentLog2);
VALIDATE_INDEX(imm.memoryIndex, module.memories.size());
}
void validateImm(AtomicFenceImm imm)
{
WAVM_ASSERT(imm.order == MemoryOrder::sequentiallyConsistent);
}
void validateImm(DataSegmentAndMemImm imm)
{
VALIDATE_INDEX(imm.memoryIndex, module.memories.size());
VALIDATE_INDEX(imm.dataSegmentIndex, module.dataSegments.size());
}
void validateImm(DataSegmentImm imm)
{
VALIDATE_INDEX(imm.dataSegmentIndex, module.dataSegments.size());
}
void validateImm(ElemSegmentAndTableImm imm)
{
VALIDATE_INDEX(imm.elemSegmentIndex, module.elemSegments.size());
VALIDATE_INDEX(imm.tableIndex, module.tables.size());
// Validate that the elem type contained by the segment is compatible with the target table.
const TableType tableType = module.tables.getType(imm.tableIndex);
ElemSegment::Contents* contents = module.elemSegments[imm.elemSegmentIndex].contents.get();
switch(contents->encoding)
{
case IR::ElemSegment::Encoding::expr:
VALIDATE_UNLESS("table.init elem segment type is not a subtype of table element type",
!isSubtype(contents->elemType, tableType.elementType));
break;
case IR::ElemSegment::Encoding::index:
VALIDATE_UNLESS(
"table.init elem segment type is not a subtype of table element type",
!isSubtype(asReferenceType(contents->externKind), tableType.elementType));
break;
default: WAVM_UNREACHABLE();
};
}
void validateImm(ElemSegmentImm imm)
{
VALIDATE_INDEX(imm.elemSegmentIndex, module.elemSegments.size());
}
#define VALIDATE_OP(_1, name, nameString, Imm, Signature, requiredFeature) \
void name(Imm imm) \
{ \
VALIDATE_FEATURE(nameString, requiredFeature); \
const char* operatorName = nameString; \
WAVM_SUPPRESS_UNUSED(operatorName); \
validateImm(imm); \
const OpSignature& signature = IR::OpSignatures::Signature; \
popAndValidateOpParams(nameString, signature.params); \
pushOpResults(signature.results); \
}
WAVM_ENUM_NONCONTROL_NONPARAMETRIC_OPERATORS(VALIDATE_OP)
#undef VALIDATE_OP
#define VALIDATE_POLYMORPHIC_OP(_1, name, nameString, Imm, Signature, requiredFeature) \
void name(Imm imm) \
{ \
VALIDATE_FEATURE(nameString, requiredFeature); \
const char* operatorName = nameString; \
WAVM_SUPPRESS_UNUSED(operatorName); \
validateImm(imm); \
const OpSignature& signature = IR::OpSignatures::Signature(module, imm); \
popAndValidateOpParams(nameString, signature.params); \
pushOpResults(signature.results); \
}
WAVM_ENUM_INDEX_POLYMORPHIC_OPERATORS(VALIDATE_POLYMORPHIC_OP)
#undef DUMMY_VALIDATE_OP
private:
struct ControlContext
{
enum class Type : U8
{
function,
block,
ifThen,
ifElse,
loop,
try_,
catch_
};
Type type;
Uptr outerStackSize;
TypeTuple params;
TypeTuple results;
bool isReachable;
TypeTuple elseParams;
};
const Module& module;
const FunctionDef& functionDef;
FunctionType functionType;
ModuleValidationState& moduleValidationState;
std::vector<ValueType> locals;
std::vector<ControlContext> controlStack;
std::vector<ValueType> stack;
void pushControlStack(ControlContext::Type type,
TypeTuple params,
TypeTuple results,
TypeTuple elseParams = TypeTuple())
{
controlStack.push_back({type, stack.size(), params, results, true, elseParams});
}
void validateStackEmptyAtEndOfControlStructure()
{
WAVM_ASSERT(controlStack.size());
if(stack.size() != controlStack.back().outerStackSize)
{
std::string message = "stack was not empty at end of control structure: ";
for(Uptr stackIndex = controlStack.back().outerStackSize; stackIndex < stack.size();
++stackIndex)
{
if(stackIndex != controlStack.back().outerStackSize) { message += ", "; }
message += asString(stack[stackIndex]);
}
throw ValidationException(std::move(message));
}
}
void enterUnreachable()
{
WAVM_ASSERT(controlStack.size());
stack.resize(controlStack.back().outerStackSize);
controlStack.back().isReachable = false;
}
void validateBranchDepth(Uptr depth) const
{
VALIDATE_INDEX(depth, controlStack.size());
if(depth >= controlStack.size()) { throw ValidationException("invalid branch depth"); }
}
const ControlContext& getBranchTargetByDepth(Uptr depth) const
{
validateBranchDepth(depth);
return controlStack[controlStack.size() - depth - 1];
}
ValueType validateLocalIndex(Uptr localIndex)
{
VALIDATE_INDEX(localIndex, locals.size());
return locals[localIndex];
}
ValueType peekAndValidateOperand(const char* context,
Uptr operandDepth,
const ValueType expectedType)
{
WAVM_ASSERT(controlStack.size());
ValueType actualType;
if(stack.size() > controlStack.back().outerStackSize + operandDepth)
{ actualType = stack[stack.size() - operandDepth - 1]; }
else if(!controlStack.back().isReachable)
{
// If the current instruction is unreachable, then pop a bottom type that is a subtype
// of all other types.
actualType = ValueType::none;
}
else
{
// If the current instruction is reachable, but the operand stack is empty, then throw a
// validation exception.
throw ValidationException(std::string("type mismatch: expected ")
+ asString(expectedType) + " but stack was empty" + " in "
+ context + " operand");
}
if(!isSubtype(actualType, expectedType))
{
throw ValidationException(std::string("type mismatch: expected ")
+ asString(expectedType) + " but got " + asString(actualType)
+ " in " + context + " operand");
}
return actualType;
}
void popAndValidateOperandArray(const char* context, const ValueType* expectedTypes, Uptr num)
{
for(Uptr operandIndexFromEnd = 0; operandIndexFromEnd < num; ++operandIndexFromEnd)
{
const Uptr operandIndex = num - operandIndexFromEnd - 1;
popAndValidateOperand(context, expectedTypes[operandIndex]);
}
}
template<Uptr num>
void popAndValidateOperandArray(const char* context, const ValueType (&expectedTypes)[num])
{
popAndValidateOperandArray(context, expectedTypes, num);
}
template<typename... OperandTypes>
void popAndValidateOperands(const char* context, OperandTypes... operands)
{
ValueType operandTypes[] = {operands...};
popAndValidateOperandArray(context, operandTypes);
}
ValueType popAndValidateOperand(const char* context, const ValueType expectedType)
{
ValueType actualType = peekAndValidateOperand(context, 0, expectedType);
WAVM_ASSERT(controlStack.size());
if(stack.size() > controlStack.back().outerStackSize) { stack.pop_back(); }
return actualType;
}
void popAndValidateTypeTuple(const char* context, TypeTuple expectedTypes)
{
popAndValidateOperandArray(context, expectedTypes.data(), expectedTypes.size());
}
void popAndValidateOpParams(const char* context, OpTypeTuple expectedTypes)
{
popAndValidateOperandArray(context, expectedTypes.data(), expectedTypes.size());
}
void peekAndValidateTypeTuple(const char* context, TypeTuple expectedTypes)
{
for(Uptr operandIndex = 0; operandIndex < expectedTypes.size(); ++operandIndex)
{
peekAndValidateOperand(
context, expectedTypes.size() - operandIndex - 1, expectedTypes[operandIndex]);
}
}
void pushOperand(ValueType type) { stack.push_back(type); }
void pushOperandTuple(TypeTuple typeTuple)
{
for(ValueType type : typeTuple) { pushOperand(type); }
}
void pushOpResults(OpTypeTuple results)
{
for(ValueType type : results) { pushOperand(type); }
}
};
void IR::validateTypes(ModuleValidationState& state)
{
const Module& module = state.module;
for(Uptr typeIndex = 0; typeIndex < module.types.size(); ++typeIndex)
{
FunctionType functionType = module.types[typeIndex];
// Validate the function type parameters and results here, but don't check the limit on
// number of return values here, since they don't apply to block types that are also stored
// here. Instead, uses of a function type from the types array must call
// validateFunctionType to validate its use as a function type.
validate(module, functionType.params());
validate(module, functionType.results());
if(functionType.results().size() > 1)
{
VALIDATE_FEATURE("function type with multiple results", multipleResultsAndBlockParams);
}
if(functionType.callingConvention() != CallingConvention::wasm)
{ VALIDATE_FEATURE("non-WASM function type", nonWASMFunctionTypes); }
}
}
void IR::validateImports(ModuleValidationState& state)
{
const Module& module = state.module;
WAVM_ASSERT(module.imports.size()
== module.functions.imports.size() + module.tables.imports.size()
+ module.memories.imports.size() + module.globals.imports.size()
+ module.exceptionTypes.imports.size());
for(Uptr functionIndex = 0; functionIndex < module.functions.imports.size(); ++functionIndex)
{
const Import<IndexedFunctionType>& functionImport = module.functions.imports[functionIndex];
validateFunctionType(module, functionImport.type);
state.declaredExternRefs.add(KindAndIndex{ExternKind::function, functionIndex});
}
for(auto& tableImport : module.tables.imports) { validate(module, tableImport.type); }
for(auto& memoryImport : module.memories.imports) { validate(module, memoryImport.type); }
for(auto& globalImport : module.globals.imports)
{
validate(module, globalImport.type);
if(globalImport.type.isMutable)
{ VALIDATE_FEATURE("mutable imported global", importExportMutableGlobals); }
}
for(auto& exceptionTypeImport : module.exceptionTypes.imports)
{ validate(module, exceptionTypeImport.type.params); }
if(module.tables.size() > 1) { VALIDATE_FEATURE("multiple tables", referenceTypes); }
if(module.memories.size() > 1) { VALIDATE_FEATURE("multiple memories", multipleMemories); }
}
void IR::validateFunctionDeclarations(ModuleValidationState& state)
{
const Module& module = state.module;
for(Uptr functionDefIndex = 0; functionDefIndex < module.functions.defs.size();
++functionDefIndex)
{
const FunctionDef& functionDef = module.functions.defs[functionDefIndex];
const FunctionType functionType = validateFunctionType(module, functionDef.type);
if(functionType.callingConvention() != CallingConvention::wasm)
{ throw ValidationException("Function definitions must have WASM calling convention"); }
}
}
void IR::validateGlobalDefs(ModuleValidationState& state)
{
const Module& module = state.module;
for(auto& globalDef : module.globals.defs)
{
validate(module, globalDef.type);
validateInitializer(state,
globalDef.initializer,
globalDef.type.valueType,
"global initializer expression");
if(globalDef.initializer.type == InitializerExpression::Type::ref_func)
{
state.declaredExternRefs.add(
KindAndIndex{ExternKind::function, globalDef.initializer.ref});
}
}
}
void IR::validateExceptionTypeDefs(ModuleValidationState& state)
{
const Module& module = state.module;
for(auto& exceptionTypeDef : module.exceptionTypes.defs)
{ validate(module, exceptionTypeDef.type.params); }
}
void IR::validateTableDefs(ModuleValidationState& state)
{
const Module& module = state.module;
for(auto& tableDef : module.tables.defs) { validate(module, tableDef.type); }
if(module.tables.size() > 1) { VALIDATE_FEATURE("multiple tables", referenceTypes); }
}
void IR::validateMemoryDefs(ModuleValidationState& state)
{
const Module& module = state.module;
for(auto& memoryDef : module.memories.defs) { validate(module, memoryDef.type); }
if(module.memories.size() > 1) { VALIDATE_FEATURE("multiple memories", multipleMemories); }
}
void IR::validateExports(ModuleValidationState& state)
{
const Module& module = state.module;
HashSet<std::string> exportNameSet;
for(auto& exportIt : module.exports)
{
validateExternKind(module, exportIt.kind);
switch(exportIt.kind)
{
case ExternKind::function:
VALIDATE_INDEX(exportIt.index, module.functions.size());
state.declaredExternRefs.add(KindAndIndex{ExternKind::function, exportIt.index});
break;
case ExternKind::table: VALIDATE_INDEX(exportIt.index, module.tables.size()); break;
case ExternKind::memory: VALIDATE_INDEX(exportIt.index, module.memories.size()); break;
case ExternKind::global:
validateGlobalIndex(module,
exportIt.index,
false,
!module.featureSpec.importExportMutableGlobals,
false,
"exported global index");
break;
case ExternKind::exceptionType:
VALIDATE_INDEX(exportIt.index, module.exceptionTypes.size());
break;
case ExternKind::invalid:
default: WAVM_UNREACHABLE();
};
VALIDATE_UNLESS("duplicate export: ", exportNameSet.contains(exportIt.name));
exportNameSet.addOrFail(exportIt.name);
}
}
void IR::validateStartFunction(ModuleValidationState& state)
{
const Module& module = state.module;
if(module.startFunctionIndex != UINTPTR_MAX)
{
VALIDATE_INDEX(module.startFunctionIndex, module.functions.size());
FunctionType startFunctionType
= module.types[module.functions.getType(module.startFunctionIndex).index];
VALIDATE_UNLESS("start function must not have any parameters or results: ",
startFunctionType != FunctionType());
}
}
void IR::validateElemSegments(ModuleValidationState& state)
{
const Module& module = state.module;
for(auto& elemSegment : module.elemSegments)
{
if(elemSegment.contents->encoding == ElemSegment::Encoding::index)
{
validateExternKind(module, elemSegment.contents->externKind);
if(elemSegment.contents->externKind != ExternKind::function)
{
VALIDATE_FEATURE("elem segment reference non-function externs",
allowAnyExternKindElemSegments);
}
}
switch(elemSegment.type)
{
case ElemSegment::Type::active: {
VALIDATE_INDEX(elemSegment.tableIndex, module.tables.size());
const TableType& tableType = module.tables.getType(elemSegment.tableIndex);
ReferenceType segmentElemType;
switch(elemSegment.contents->encoding)
{
case ElemSegment::Encoding::expr:
segmentElemType = elemSegment.contents->elemType;
break;
case ElemSegment::Encoding::index:
segmentElemType = elemSegment.contents->externKind == ExternKind::function
? ReferenceType::funcref
: ReferenceType::externref;
break;
default: WAVM_UNREACHABLE();
};
const ReferenceType tableElemType = tableType.elementType;
if(!isSubtype(segmentElemType, tableType.elementType))
{
throw ValidationException(std::string("segment elem type (")
+ asString(segmentElemType)
+ ") is not a subtype of the table's elem type ("
+ asString(tableElemType) + ")");
}
validateInitializer(state,
elemSegment.baseOffset,
asValueType(tableType.indexType),
"elem segment base initializer");
break;
}
case ElemSegment::Type::passive: break;
case ElemSegment::Type::declared: break;
default: WAVM_UNREACHABLE();
};
switch(elemSegment.contents->encoding)
{
case ElemSegment::Encoding::expr:
for(const ElemExpr& elem : elemSegment.contents->elemExprs)
{
ReferenceType exprType = ReferenceType::none;
switch(elem.type)
{
case ElemExpr::Type::ref_null: exprType = elem.nullReferenceType; break;
case ElemExpr::Type::ref_func:
exprType = ReferenceType::funcref;
VALIDATE_INDEX(elem.index, module.functions.size());
state.declaredExternRefs.add(KindAndIndex{ExternKind::function, elem.index});
break;
case ElemExpr::Type::invalid:
default: WAVM_UNREACHABLE();
};
if(!isSubtype(exprType, elemSegment.contents->elemType))
{
throw ValidationException(std::string("elem expression type (")
+ asString(exprType)
+ ") is not a subtype of the segment's elem type ("
+ asString(elemSegment.contents->elemType) + ")");
}
}
break;
case ElemSegment::Encoding::index:
for(Uptr externIndex : elemSegment.contents->elemIndices)
{
switch(elemSegment.contents->externKind)
{
case ExternKind::function:
VALIDATE_INDEX(externIndex, module.functions.size());
break;
case ExternKind::table: VALIDATE_INDEX(externIndex, module.tables.size()); break;
case ExternKind::memory: VALIDATE_INDEX(externIndex, module.memories.size()); break;
case ExternKind::global: VALIDATE_INDEX(externIndex, module.globals.size()); break;
case ExternKind::exceptionType:
VALIDATE_INDEX(externIndex, module.exceptionTypes.size());
break;
case ExternKind::invalid:
default: WAVM_UNREACHABLE();
};
state.declaredExternRefs.add(
KindAndIndex{elemSegment.contents->externKind, externIndex});
}
break;
default: WAVM_UNREACHABLE();
};
}
}
void IR::validateDataSegments(ModuleValidationState& state)
{
const Module& module = state.module;
for(auto& dataSegment : module.dataSegments)
{
if(dataSegment.isActive)
{
VALIDATE_INDEX(dataSegment.memoryIndex, module.memories.size());
const MemoryType& memoryType = module.memories.getType(dataSegment.memoryIndex);
validateInitializer(state,
dataSegment.baseOffset,
asValueType(memoryType.indexType),
"data segment base initializer");
}
}
}
void IR::validateCodeSection(ModuleValidationState& state)
{
const Module& module = state.module;
for(const auto& functionDef : module.functions.defs)
{
CodeValidationStream validationStream(state, functionDef);
OperatorDecoderStream operatorDecoderStream(functionDef.code);
while(operatorDecoderStream) { operatorDecoderStream.decodeOp(validationStream); }
}
}
namespace WAVM { namespace IR {
struct CodeValidationStreamImpl
{
FunctionValidationContext functionContext;
OperatorPrinter operatorPrinter;
CodeValidationStreamImpl(ModuleValidationState& moduleValidationState,
const FunctionDef& functionDef)
: functionContext(moduleValidationState, functionDef)
, operatorPrinter(moduleValidationState.module, functionDef)
{
}
};
}}
IR::CodeValidationStream::CodeValidationStream(ModuleValidationState& moduleValidationState,
const FunctionDef& functionDef)
{
impl = new CodeValidationStreamImpl(moduleValidationState, functionDef);
}
IR::CodeValidationStream::~CodeValidationStream()
{
delete impl;
impl = nullptr;
}
void IR::CodeValidationStream::finish()
{
if(impl->functionContext.getControlStackSize())
{ throw ValidationException("end of code reached before end of function"); }
}
#define VISIT_OPCODE(_, name, nameString, Imm, ...) \
void IR::CodeValidationStream::name(Imm imm) \
{ \
if(impl->functionContext.enableTracing) \
{ impl->functionContext.traceOperator(impl->operatorPrinter.name(imm)); } \
impl->functionContext.validateNonEmptyControlStack(nameString); \
impl->functionContext.name(imm); \
}
WAVM_ENUM_OPERATORS(VISIT_OPCODE)
#undef VISIT_OPCODE
|