Debian Codegraph @

js/src/wasm/WasmValidate.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 *
4 * Copyright 2016 Mozilla Foundation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "wasm/WasmValidate.h"
20
21 #include "mozilla/CheckedInt.h"
22 #include "mozilla/Span.h"
23 #include "mozilla/Utf8.h"
24
25 #include "js/Printf.h"
26 #include "js/String.h" // JS::MaxStringLength
27 #include "vm/JSContext.h"
28 #include "vm/Realm.h"
29 #include "wasm/WasmDump.h"
30 #include "wasm/WasmInitExpr.h"
31 #include "wasm/WasmOpIter.h"
32 #include "wasm/WasmTypeDecls.h"
33
34 using namespace js;
35 using namespace js::jit;
36 using namespace js::wasm;
37
38 using mozilla::AsChars;
39 using mozilla::CheckedInt;
40 using mozilla::IsUtf8;
41 using mozilla::Maybe;
42 using mozilla::Nothing;
43 using mozilla::Some;
44 using mozilla::Span;
45
46 // Misc helpers.
47
48 bool wasm::EncodeLocalEntries(Encoder& e, const ValTypeVector& locals) {
49 if (locals.length() > MaxLocals) {
50 return false;
51 }
52
53 uint32_t numLocalEntries = 0;
54 if (locals.length()) {
55 ValType prev = locals[0];
56 numLocalEntries++;
57 for (ValType t : locals) {
58 if (t != prev) {
59 numLocalEntries++;
60 prev = t;
61 }
62 }
63 }
64
65 if (!e.writeVarU32(numLocalEntries)) {
66 return false;
67 }
68
69 if (numLocalEntries) {
70 ValType prev = locals[0];
71 uint32_t count = 1;
72 for (uint32_t i = 1; i < locals.length(); i++, count++) {
73 if (prev != locals[i]) {
74 if (!e.writeVarU32(count)) {
75 return false;
76 }
77 if (!e.writeValType(prev)) {
78 return false;
79 }
80 prev = locals[i];
81 count = 0;
82 }
83 }
84 if (!e.writeVarU32(count)) {
85 return false;
86 }
87 if (!e.writeValType(prev)) {
88 return false;
89 }
90 }
91
92 return true;
93 }
94
95 bool wasm::DecodeLocalEntriesWithParams(Decoder& d,
96 const CodeMetadata& codeMeta,
97 uint32_t funcIndex,
98 ValTypeVector* locals) {
99 uint32_t numLocalEntries;
100 if (!d.readVarU32(&numLocalEntries)) {
101 return d.fail("failed to read number of local entries");
102 }
103
104 if (!locals->appendAll(codeMeta.getFuncType(funcIndex).args())) {
105 return false;
106 }
107
108 for (uint32_t i = 0; i < numLocalEntries; i++) {
109 uint32_t count;
110 if (!d.readVarU32(&count)) {
111 return d.fail("failed to read local entry count");
112 }
113
114 if (MaxLocals - locals->length() < count) {
115 return d.fail("too many locals");
116 }
117
118 ValType type;
119 if (!d.readValType(*codeMeta.types, codeMeta.features(), &type)) {
120 return false;
121 }
122
123 if (!locals->appendN(type, count)) {
124 return false;
125 }
126 }
127
128 return true;
129 }
130
131 bool wasm::DecodeValidatedLocalEntries(const TypeContext& types, Decoder& d,
132 ValTypeVector* locals) {
133 uint32_t numLocalEntries;
134 MOZ_ALWAYS_TRUE(d.readVarU32(&numLocalEntries));
135
136 for (uint32_t i = 0; i < numLocalEntries; i++) {
137 uint32_t count = d.uncheckedReadVarU32();
138 MOZ_ASSERT(MaxLocals - locals->length() >= count);
139 if (!locals->appendN(d.uncheckedReadValType(types), count)) {
140 return false;
141 }
142 }
143
144 return true;
145 }
146
147 bool wasm::CheckIsSubtypeOf(Decoder& d, const CodeMetadata& codeMeta,
148 size_t opcodeOffset, ResultType subType,
149 ResultType superType) {
150 if (subType.length() != superType.length()) {
151 UniqueChars error(
152 JS_smprintf("type mismatch: expected %zu values, got %zu values",
153 superType.length(), subType.length()));
154 if (!error) {
155 return false;
156 }
157 MOZ_ASSERT(!ResultType::isSubTypeOf(subType, superType));
158 return d.fail(opcodeOffset, error.get());
159 }
160 for (uint32_t i = 0; i < subType.length(); i++) {
161 StorageType sub = subType[i].storageType();
162 StorageType super = superType[i].storageType();
163 if (!CheckIsSubtypeOf(d, codeMeta, opcodeOffset, sub, super)) {
164 MOZ_ASSERT(!ResultType::isSubTypeOf(subType, superType));
165 return false;
166 }
167 }
168 MOZ_ASSERT(ResultType::isSubTypeOf(subType, superType));
169 return true;
170 }
171
172 bool wasm::CheckIsSubtypeOf(Decoder& d, const CodeMetadata& codeMeta,
173 size_t opcodeOffset, StorageType subType,
174 StorageType superType) {
175 if (StorageType::isSubTypeOf(subType, superType)) {
176 return true;
177 }
178
179 UniqueChars subText = ToString(subType, codeMeta.types);
180 if (!subText) {
181 return false;
182 }
183
184 UniqueChars superText = ToString(superType, codeMeta.types);
185 if (!superText) {
186 return false;
187 }
188
189 UniqueChars error(
190 JS_smprintf("type mismatch: expression has type %s but expected %s",
191 subText.get(), superText.get()));
192 if (!error) {
193 return false;
194 }
195
196 return d.fail(opcodeOffset, error.get());
197 }
198
199 // Function body validation.
200
201 template <class T>
202 bool wasm::ValidateOps(ValidatingOpIter& iter, T& dumper,
203 const CodeMetadata& codeMeta) {
204 while (true) {
205 OpBytes op;
206 if (!iter.readOp(&op)) {
207 return false;
208 }
209
210 // End instructions get handled differently since we don't actually want to
211 // dump the final `end`. Also, Else instructions need to have their
212 // indentation managed when dumping.
213 if (op.b0 != uint16_t(Op::End)) {
214 if (op.b0 == uint64_t(Op::Else)) {
215 dumper.endScope();
216 }
217 dumper.dumpOpBegin(op);
218 if (op.b0 == uint64_t(Op::Else)) {
219 dumper.startScope();
220 }
221 }
222
223 Nothing nothing;
224 NothingVector nothings{};
225 BlockType blockType;
226 ResultType resultType;
227
228 switch (op.b0) {
229 case uint16_t(Op::End): {
230 LabelKind unusedKind;
231 if (!iter.readEnd(&unusedKind, &resultType, &nothings, &nothings)) {
232 return false;
233 }
234 iter.popEnd();
235 if (iter.controlStackEmpty()) {
236 return true;
237 }
238
239 // Only dump `end` if it was not the final `end` of the expression.
240 dumper.endScope();
241 dumper.dumpOpBegin(op);
242
243 break;
244 }
245 case uint16_t(Op::Nop): {
246 if (!iter.readNop()) {
247 return false;
248 }
249 break;
250 }
251 case uint16_t(Op::Drop): {
252 if (!iter.readDrop()) {
253 return false;
254 }
255 break;
256 }
257 case uint16_t(Op::Call): {
258 uint32_t funcIndex;
259 NothingVector unusedArgs{};
260 if (!iter.readCall(&funcIndex, &unusedArgs)) {
261 return false;
262 }
263 dumper.dumpFuncIndex(funcIndex);
264 break;
265 }
266 case uint16_t(Op::CallIndirect): {
267 uint32_t funcTypeIndex, tableIndex;
268 NothingVector unusedArgs{};
269 if (!iter.readCallIndirect(&funcTypeIndex, &tableIndex, &nothing,
270 &unusedArgs)) {
271 return false;
272 }
273 dumper.dumpTableIndex(tableIndex);
274 dumper.dumpTypeIndex(funcTypeIndex, /*asTypeUse=*/true);
275 break;
276 }
277 case uint16_t(Op::ReturnCall): {
278 uint32_t funcIndex;
279 NothingVector unusedArgs{};
280 if (!iter.readReturnCall(&funcIndex, &unusedArgs)) {
281 return false;
282 }
283 dumper.dumpFuncIndex(funcIndex);
284 break;
285 }
286 case uint16_t(Op::ReturnCallIndirect): {
287 uint32_t funcTypeIndex, tableIndex;
288 NothingVector unusedArgs{};
289 if (!iter.readReturnCallIndirect(&funcTypeIndex, &tableIndex, &nothing,
290 &unusedArgs)) {
291 return false;
292 }
293 dumper.dumpTableIndex(tableIndex);
294 dumper.dumpTypeIndex(funcTypeIndex, /*asTypeUse=*/true);
295 break;
296 }
297 case uint16_t(Op::CallRef): {
298 uint32_t funcTypeIndex;
299 NothingVector unusedArgs{};
300 if (!iter.readCallRef(&funcTypeIndex, &nothing, &unusedArgs)) {
301 return false;
302 }
303 dumper.dumpTypeIndex(funcTypeIndex);
304 break;
305 }
306 case uint16_t(Op::ReturnCallRef): {
307 uint32_t funcTypeIndex;
308 NothingVector unusedArgs{};
309 if (!iter.readReturnCallRef(&funcTypeIndex, &nothing, &unusedArgs)) {
310 return false;
311 }
312 dumper.dumpTypeIndex(funcTypeIndex);
313 break;
314 }
315 case uint16_t(Op::I32Const): {
316 int32_t constant;
317 if (!iter.readI32Const(&constant)) {
318 return false;
319 }
320 dumper.dumpI32Const(constant);
321 break;
322 }
323 case uint16_t(Op::I64Const): {
324 int64_t constant;
325 if (!iter.readI64Const(&constant)) {
326 return false;
327 }
328 dumper.dumpI64Const(constant);
329 break;
330 }
331 case uint16_t(Op::F32Const): {
332 float constant;
333 if (!iter.readF32Const(&constant)) {
334 return false;
335 }
336 dumper.dumpF32Const(constant);
337 break;
338 }
339 case uint16_t(Op::F64Const): {
340 double constant;
341 if (!iter.readF64Const(&constant)) {
342 return false;
343 }
344 dumper.dumpF64Const(constant);
345 break;
346 }
347 case uint16_t(Op::LocalGet): {
348 uint32_t localIndex;
349 if (!iter.readGetLocal(&localIndex)) {
350 return false;
351 }
352 dumper.dumpLocalIndex(localIndex);
353 break;
354 }
355 case uint16_t(Op::LocalSet): {
356 uint32_t localIndex;
357 if (!iter.readSetLocal(&localIndex, &nothing)) {
358 return false;
359 }
360 dumper.dumpLocalIndex(localIndex);
361 break;
362 }
363 case uint16_t(Op::LocalTee): {
364 uint32_t localIndex;
365 if (!iter.readTeeLocal(&localIndex, &nothing)) {
366 return false;
367 }
368 dumper.dumpLocalIndex(localIndex);
369 break;
370 }
371 case uint16_t(Op::GlobalGet): {
372 uint32_t globalIndex;
373 if (!iter.readGetGlobal(&globalIndex)) {
374 return false;
375 }
376 dumper.dumpGlobalIndex(globalIndex);
377 break;
378 }
379 case uint16_t(Op::GlobalSet): {
380 uint32_t globalIndex;
381 if (!iter.readSetGlobal(&globalIndex, &nothing)) {
382 return false;
383 }
384 dumper.dumpGlobalIndex(globalIndex);
385 break;
386 }
387 case uint16_t(Op::TableGet): {
388 uint32_t tableIndex;
389 if (!iter.readTableGet(&tableIndex, &nothing)) {
390 return false;
391 }
392 dumper.dumpTableIndex(tableIndex);
393 break;
394 }
395 case uint16_t(Op::TableSet): {
396 uint32_t tableIndex;
397 if (!iter.readTableSet(&tableIndex, &nothing, &nothing)) {
398 return false;
399 }
400 dumper.dumpTableIndex(tableIndex);
401 break;
402 }
403 case uint16_t(Op::SelectNumeric): {
404 StackType unused;
405 if (!iter.readSelect(/*typed*/ false, &unused, &nothing, &nothing,
406 &nothing)) {
407 return false;
408 }
409 break;
410 }
411 case uint16_t(Op::SelectTyped): {
412 StackType type;
413 if (!iter.readSelect(/*typed*/ true, &type, &nothing, &nothing,
414 &nothing)) {
415 return false;
416 }
417 dumper.dumpValType(type.valType());
418 break;
419 }
420 case uint16_t(Op::Block): {
421 if (!iter.readBlock(&blockType)) {
422 return false;
423 }
424 dumper.dumpBlockType(blockType);
425 dumper.startScope();
426 break;
427 }
428 case uint16_t(Op::Loop): {
429 if (!iter.readLoop(&blockType)) {
430 return false;
431 }
432 dumper.dumpBlockType(blockType);
433 dumper.startScope();
434 break;
435 }
436 case uint16_t(Op::If): {
437 if (!iter.readIf(&blockType, &nothing)) {
438 return false;
439 }
440 dumper.dumpBlockType(blockType);
441 dumper.startScope();
442 break;
443 }
444 case uint16_t(Op::Else): {
445 if (!iter.readElse(&resultType, &resultType, &nothings)) {
446 return false;
447 }
448 break;
449 }
450 case uint16_t(Op::I32Clz):
451 case uint16_t(Op::I32Ctz):
452 case uint16_t(Op::I32Popcnt): {
453 if (!iter.readUnary(ValType::I32, &nothing)) {
454 return false;
455 }
456 break;
457 }
458 case uint16_t(Op::I64Clz):
459 case uint16_t(Op::I64Ctz):
460 case uint16_t(Op::I64Popcnt): {
461 if (!iter.readUnary(ValType::I64, &nothing)) {
462 return false;
463 }
464 break;
465 }
466 case uint16_t(Op::F32Abs):
467 case uint16_t(Op::F32Neg):
468 case uint16_t(Op::F32Ceil):
469 case uint16_t(Op::F32Floor):
470 case uint16_t(Op::F32Sqrt):
471 case uint16_t(Op::F32Trunc):
472 case uint16_t(Op::F32Nearest): {
473 if (!iter.readUnary(ValType::F32, &nothing)) {
474 return false;
475 }
476 break;
477 }
478 case uint16_t(Op::F64Abs):
479 case uint16_t(Op::F64Neg):
480 case uint16_t(Op::F64Ceil):
481 case uint16_t(Op::F64Floor):
482 case uint16_t(Op::F64Sqrt):
483 case uint16_t(Op::F64Trunc):
484 case uint16_t(Op::F64Nearest): {
485 if (!iter.readUnary(ValType::F64, &nothing)) {
486 return false;
487 }
488 break;
489 }
490 case uint16_t(Op::I32Add):
491 case uint16_t(Op::I32Sub):
492 case uint16_t(Op::I32Mul):
493 case uint16_t(Op::I32DivS):
494 case uint16_t(Op::I32DivU):
495 case uint16_t(Op::I32RemS):
496 case uint16_t(Op::I32RemU):
497 case uint16_t(Op::I32And):
498 case uint16_t(Op::I32Or):
499 case uint16_t(Op::I32Xor):
500 case uint16_t(Op::I32Shl):
501 case uint16_t(Op::I32ShrS):
502 case uint16_t(Op::I32ShrU):
503 case uint16_t(Op::I32Rotl):
504 case uint16_t(Op::I32Rotr): {
505 if (!iter.readBinary(ValType::I32, &nothing, &nothing)) {
506 return false;
507 }
508 break;
509 }
510 case uint16_t(Op::I64Add):
511 case uint16_t(Op::I64Sub):
512 case uint16_t(Op::I64Mul):
513 case uint16_t(Op::I64DivS):
514 case uint16_t(Op::I64DivU):
515 case uint16_t(Op::I64RemS):
516 case uint16_t(Op::I64RemU):
517 case uint16_t(Op::I64And):
518 case uint16_t(Op::I64Or):
519 case uint16_t(Op::I64Xor):
520 case uint16_t(Op::I64Shl):
521 case uint16_t(Op::I64ShrS):
522 case uint16_t(Op::I64ShrU):
523 case uint16_t(Op::I64Rotl):
524 case uint16_t(Op::I64Rotr): {
525 if (!iter.readBinary(ValType::I64, &nothing, &nothing)) {
526 return false;
527 }
528 break;
529 }
530 case uint16_t(Op::F32Add):
531 case uint16_t(Op::F32Sub):
532 case uint16_t(Op::F32Mul):
533 case uint16_t(Op::F32Div):
534 case uint16_t(Op::F32Min):
535 case uint16_t(Op::F32Max):
536 case uint16_t(Op::F32CopySign): {
537 if (!iter.readBinary(ValType::F32, &nothing, &nothing)) {
538 return false;
539 }
540 break;
541 }
542 case uint16_t(Op::F64Add):
543 case uint16_t(Op::F64Sub):
544 case uint16_t(Op::F64Mul):
545 case uint16_t(Op::F64Div):
546 case uint16_t(Op::F64Min):
547 case uint16_t(Op::F64Max):
548 case uint16_t(Op::F64CopySign): {
549 if (!iter.readBinary(ValType::F64, &nothing, &nothing)) {
550 return false;
551 }
552 break;
553 }
554 case uint16_t(Op::I32Eq):
555 case uint16_t(Op::I32Ne):
556 case uint16_t(Op::I32LtS):
557 case uint16_t(Op::I32LtU):
558 case uint16_t(Op::I32LeS):
559 case uint16_t(Op::I32LeU):
560 case uint16_t(Op::I32GtS):
561 case uint16_t(Op::I32GtU):
562 case uint16_t(Op::I32GeS):
563 case uint16_t(Op::I32GeU): {
564 if (!iter.readComparison(ValType::I32, &nothing, &nothing)) {
565 return false;
566 }
567 break;
568 }
569 case uint16_t(Op::I64Eq):
570 case uint16_t(Op::I64Ne):
571 case uint16_t(Op::I64LtS):
572 case uint16_t(Op::I64LtU):
573 case uint16_t(Op::I64LeS):
574 case uint16_t(Op::I64LeU):
575 case uint16_t(Op::I64GtS):
576 case uint16_t(Op::I64GtU):
577 case uint16_t(Op::I64GeS):
578 case uint16_t(Op::I64GeU): {
579 if (!iter.readComparison(ValType::I64, &nothing, &nothing)) {
580 return false;
581 }
582 break;
583 }
584 case uint16_t(Op::F32Eq):
585 case uint16_t(Op::F32Ne):
586 case uint16_t(Op::F32Lt):
587 case uint16_t(Op::F32Le):
588 case uint16_t(Op::F32Gt):
589 case uint16_t(Op::F32Ge): {
590 if (!iter.readComparison(ValType::F32, &nothing, &nothing)) {
591 return false;
592 }
593 break;
594 }
595 case uint16_t(Op::F64Eq):
596 case uint16_t(Op::F64Ne):
597 case uint16_t(Op::F64Lt):
598 case uint16_t(Op::F64Le):
599 case uint16_t(Op::F64Gt):
600 case uint16_t(Op::F64Ge): {
601 if (!iter.readComparison(ValType::F64, &nothing, &nothing)) {
602 return false;
603 }
604 break;
605 }
606 case uint16_t(Op::I32Eqz): {
607 if (!iter.readConversion(ValType::I32, ValType::I32, &nothing)) {
608 return false;
609 }
610 break;
611 }
612 case uint16_t(Op::I64Eqz):
613 case uint16_t(Op::I32WrapI64): {
614 if (!iter.readConversion(ValType::I64, ValType::I32, &nothing)) {
615 return false;
616 }
617 break;
618 }
619 case uint16_t(Op::I32TruncF32S):
620 case uint16_t(Op::I32TruncF32U):
621 case uint16_t(Op::I32ReinterpretF32): {
622 if (!iter.readConversion(ValType::F32, ValType::I32, &nothing)) {
623 return false;
624 }
625 break;
626 }
627 case uint16_t(Op::I32TruncF64S):
628 case uint16_t(Op::I32TruncF64U): {
629 if (!iter.readConversion(ValType::F64, ValType::I32, &nothing)) {
630 return false;
631 }
632 break;
633 }
634 case uint16_t(Op::I64ExtendI32S):
635 case uint16_t(Op::I64ExtendI32U): {
636 if (!iter.readConversion(ValType::I32, ValType::I64, &nothing)) {
637 return false;
638 }
639 break;
640 }
641 case uint16_t(Op::I64TruncF32S):
642 case uint16_t(Op::I64TruncF32U): {
643 if (!iter.readConversion(ValType::F32, ValType::I64, &nothing)) {
644 return false;
645 }
646 break;
647 }
648 case uint16_t(Op::I64TruncF64S):
649 case uint16_t(Op::I64TruncF64U):
650 case uint16_t(Op::I64ReinterpretF64): {
651 if (!iter.readConversion(ValType::F64, ValType::I64, &nothing)) {
652 return false;
653 }
654 break;
655 }
656 case uint16_t(Op::F32ConvertI32S):
657 case uint16_t(Op::F32ConvertI32U):
658 case uint16_t(Op::F32ReinterpretI32): {
659 if (!iter.readConversion(ValType::I32, ValType::F32, &nothing)) {
660 return false;
661 }
662 break;
663 }
664 case uint16_t(Op::F32ConvertI64S):
665 case uint16_t(Op::F32ConvertI64U): {
666 if (!iter.readConversion(ValType::I64, ValType::F32, &nothing)) {
667 return false;
668 }
669 break;
670 }
671 case uint16_t(Op::F32DemoteF64): {
672 if (!iter.readConversion(ValType::F64, ValType::F32, &nothing)) {
673 return false;
674 }
675 break;
676 }
677 case uint16_t(Op::F64ConvertI32S):
678 case uint16_t(Op::F64ConvertI32U): {
679 if (!iter.readConversion(ValType::I32, ValType::F64, &nothing)) {
680 return false;
681 }
682 break;
683 }
684 case uint16_t(Op::F64ConvertI64S):
685 case uint16_t(Op::F64ConvertI64U):
686 case uint16_t(Op::F64ReinterpretI64): {
687 if (!iter.readConversion(ValType::I64, ValType::F64, &nothing)) {
688 return false;
689 }
690 break;
691 }
692 case uint16_t(Op::F64PromoteF32): {
693 if (!iter.readConversion(ValType::F32, ValType::F64, &nothing)) {
694 return false;
695 }
696 break;
697 }
698 case uint16_t(Op::I32Extend8S):
699 case uint16_t(Op::I32Extend16S): {
700 if (!iter.readConversion(ValType::I32, ValType::I32, &nothing)) {
701 return false;
702 }
703 break;
704 }
705 case uint16_t(Op::I64Extend8S):
706 case uint16_t(Op::I64Extend16S):
707 case uint16_t(Op::I64Extend32S): {
708 if (!iter.readConversion(ValType::I64, ValType::I64, &nothing)) {
709 return false;
710 }
711 break;
712 }
713 case uint16_t(Op::I32Load8S):
714 case uint16_t(Op::I32Load8U): {
715 LinearMemoryAddress<Nothing> addr;
716 if (!iter.readLoad(ValType::I32, 1, &addr)) {
717 return false;
718 }
719 dumper.dumpLinearMemoryAddress(addr);
720 break;
721 }
722 case uint16_t(Op::I32Load16S):
723 case uint16_t(Op::I32Load16U): {
724 LinearMemoryAddress<Nothing> addr;
725 if (!iter.readLoad(ValType::I32, 2, &addr)) {
726 return false;
727 }
728 dumper.dumpLinearMemoryAddress(addr);
729 break;
730 }
731 case uint16_t(Op::I32Load): {
732 LinearMemoryAddress<Nothing> addr;
733 if (!iter.readLoad(ValType::I32, 4, &addr)) {
734 return false;
735 }
736 dumper.dumpLinearMemoryAddress(addr);
737 break;
738 }
739 case uint16_t(Op::I64Load8S):
740 case uint16_t(Op::I64Load8U): {
741 LinearMemoryAddress<Nothing> addr;
742 if (!iter.readLoad(ValType::I64, 1, &addr)) {
743 return false;
744 }
745 dumper.dumpLinearMemoryAddress(addr);
746 break;
747 }
748 case uint16_t(Op::I64Load16S):
749 case uint16_t(Op::I64Load16U): {
750 LinearMemoryAddress<Nothing> addr;
751 if (!iter.readLoad(ValType::I64, 2, &addr)) {
752 return false;
753 }
754 dumper.dumpLinearMemoryAddress(addr);
755 break;
756 }
757 case uint16_t(Op::I64Load32S):
758 case uint16_t(Op::I64Load32U): {
759 LinearMemoryAddress<Nothing> addr;
760 if (!iter.readLoad(ValType::I64, 4, &addr)) {
761 return false;
762 }
763 dumper.dumpLinearMemoryAddress(addr);
764 break;
765 }
766 case uint16_t(Op::I64Load): {
767 LinearMemoryAddress<Nothing> addr;
768 if (!iter.readLoad(ValType::I64, 8, &addr)) {
769 return false;
770 }
771 dumper.dumpLinearMemoryAddress(addr);
772 break;
773 }
774 case uint16_t(Op::F32Load): {
775 LinearMemoryAddress<Nothing> addr;
776 if (!iter.readLoad(ValType::F32, 4, &addr)) {
777 return false;
778 }
779 dumper.dumpLinearMemoryAddress(addr);
780 break;
781 }
782 case uint16_t(Op::F64Load): {
783 LinearMemoryAddress<Nothing> addr;
784 if (!iter.readLoad(ValType::F64, 8, &addr)) {
785 return false;
786 }
787 dumper.dumpLinearMemoryAddress(addr);
788 break;
789 }
790 case uint16_t(Op::I32Store8): {
791 LinearMemoryAddress<Nothing> addr;
792 if (!iter.readStore(ValType::I32, 1, &addr, &nothing)) {
793 return false;
794 }
795 dumper.dumpLinearMemoryAddress(addr);
796 break;
797 }
798 case uint16_t(Op::I32Store16): {
799 LinearMemoryAddress<Nothing> addr;
800 if (!iter.readStore(ValType::I32, 2, &addr, &nothing)) {
801 return false;
802 }
803 dumper.dumpLinearMemoryAddress(addr);
804 break;
805 }
806 case uint16_t(Op::I32Store): {
807 LinearMemoryAddress<Nothing> addr;
808 if (!iter.readStore(ValType::I32, 4, &addr, &nothing)) {
809 return false;
810 }
811 dumper.dumpLinearMemoryAddress(addr);
812 break;
813 }
814 case uint16_t(Op::I64Store8): {
815 LinearMemoryAddress<Nothing> addr;
816 if (!iter.readStore(ValType::I64, 1, &addr, &nothing)) {
817 return false;
818 }
819 dumper.dumpLinearMemoryAddress(addr);
820 break;
821 }
822 case uint16_t(Op::I64Store16): {
823 LinearMemoryAddress<Nothing> addr;
824 if (!iter.readStore(ValType::I64, 2, &addr, &nothing)) {
825 return false;
826 }
827 dumper.dumpLinearMemoryAddress(addr);
828 break;
829 }
830 case uint16_t(Op::I64Store32): {
831 LinearMemoryAddress<Nothing> addr;
832 if (!iter.readStore(ValType::I64, 4, &addr, &nothing)) {
833 return false;
834 }
835 dumper.dumpLinearMemoryAddress(addr);
836 break;
837 }
838 case uint16_t(Op::I64Store): {
839 LinearMemoryAddress<Nothing> addr;
840 if (!iter.readStore(ValType::I64, 8, &addr, &nothing)) {
841 return false;
842 }
843 dumper.dumpLinearMemoryAddress(addr);
844 break;
845 }
846 case uint16_t(Op::F32Store): {
847 LinearMemoryAddress<Nothing> addr;
848 if (!iter.readStore(ValType::F32, 4, &addr, &nothing)) {
849 return false;
850 }
851 dumper.dumpLinearMemoryAddress(addr);
852 break;
853 }
854 case uint16_t(Op::F64Store): {
855 LinearMemoryAddress<Nothing> addr;
856 if (!iter.readStore(ValType::F64, 8, &addr, &nothing)) {
857 return false;
858 }
859 dumper.dumpLinearMemoryAddress(addr);
860 break;
861 }
862 case uint16_t(Op::MemoryGrow): {
863 uint32_t memoryIndex;
864 if (!iter.readMemoryGrow(&memoryIndex, &nothing)) {
865 return false;
866 }
867 dumper.dumpMemoryIndex(memoryIndex);
868 break;
869 }
870 case uint16_t(Op::MemorySize): {
871 uint32_t memoryIndex;
872 if (!iter.readMemorySize(&memoryIndex)) {
873 return false;
874 }
875 dumper.dumpMemoryIndex(memoryIndex);
876 break;
877 }
878 case uint16_t(Op::Br): {
879 uint32_t depth;
880 if (!iter.readBr(&depth, &resultType, &nothings)) {
881 return false;
882 }
883 dumper.dumpBlockDepth(depth);
884 break;
885 }
886 case uint16_t(Op::BrIf): {
887 uint32_t depth;
888 if (!iter.readBrIf(&depth, &resultType, &nothings, &nothing)) {
889 return false;
890 }
891 dumper.dumpBlockDepth(depth);
892 break;
893 }
894 case uint16_t(Op::BrTable): {
895 Uint32Vector depths;
896 uint32_t defaultDepth;
897 if (!iter.readBrTable(&depths, &defaultDepth, &resultType, &nothings,
898 &nothing)) {
899 return false;
900 }
901 dumper.dumpBlockDepths(depths);
902 dumper.dumpBlockDepth(defaultDepth);
903 break;
904 }
905 case uint16_t(Op::Return): {
906 if (!iter.readReturn(&nothings)) {
907 return false;
908 }
909 break;
910 }
911 case uint16_t(Op::Unreachable): {
912 if (!iter.readUnreachable()) {
913 return false;
914 }
915 break;
916 }
917 case uint16_t(Op::GcPrefix): {
918 switch (op.b1) {
919 case uint32_t(GcOp::StructNew): {
920 uint32_t typeIndex;
921 NothingVector unusedArgs{};
922 if (!iter.readStructNew(&typeIndex, &unusedArgs)) {
923 return false;
924 }
925 dumper.dumpTypeIndex(typeIndex);
926 break;
927 }
928 case uint32_t(GcOp::StructNewDefault): {
929 uint32_t typeIndex;
930 if (!iter.readStructNewDefault(&typeIndex)) {
931 return false;
932 }
933 dumper.dumpTypeIndex(typeIndex);
934 break;
935 }
936 case uint32_t(GcOp::StructGet): {
937 uint32_t typeIndex, fieldIndex;
938 if (!iter.readStructGet(&typeIndex, &fieldIndex,
939 FieldWideningOp::None, &nothing)) {
940 return false;
941 }
942 dumper.dumpTypeIndex(typeIndex);
943 dumper.dumpFieldIndex(fieldIndex);
944 break;
945 }
946 case uint32_t(GcOp::StructGetS): {
947 uint32_t typeIndex, fieldIndex;
948 if (!iter.readStructGet(&typeIndex, &fieldIndex,
949 FieldWideningOp::Signed, &nothing)) {
950 return false;
951 }
952 dumper.dumpTypeIndex(typeIndex);
953 dumper.dumpFieldIndex(fieldIndex);
954 break;
955 }
956 case uint32_t(GcOp::StructGetU): {
957 uint32_t typeIndex, fieldIndex;
958 if (!iter.readStructGet(&typeIndex, &fieldIndex,
959 FieldWideningOp::Unsigned, &nothing)) {
960 return false;
961 }
962 dumper.dumpTypeIndex(typeIndex);
963 dumper.dumpFieldIndex(fieldIndex);
964 break;
965 }
966 case uint32_t(GcOp::StructSet): {
967 uint32_t typeIndex, fieldIndex;
968 if (!iter.readStructSet(&typeIndex, &fieldIndex, &nothing,
969 &nothing)) {
970 return false;
971 }
972 dumper.dumpTypeIndex(typeIndex);
973 dumper.dumpFieldIndex(fieldIndex);
974 break;
975 }
976 case uint32_t(GcOp::ArrayNew): {
977 uint32_t typeIndex;
978 if (!iter.readArrayNew(&typeIndex, &nothing, &nothing)) {
979 return false;
980 }
981 dumper.dumpTypeIndex(typeIndex);
982 break;
983 }
984 case uint32_t(GcOp::ArrayNewFixed): {
985 uint32_t typeIndex, numElements;
986 if (!iter.readArrayNewFixed(&typeIndex, &numElements, &nothings)) {
987 return false;
988 }
989 dumper.dumpTypeIndex(typeIndex);
990 dumper.dumpNumElements(numElements);
991 break;
992 }
993 case uint32_t(GcOp::ArrayNewDefault): {
994 uint32_t typeIndex;
995 if (!iter.readArrayNewDefault(&typeIndex, &nothing)) {
996 return false;
997 }
998 dumper.dumpTypeIndex(typeIndex);
999 break;
1000 }
1001 case uint32_t(GcOp::ArrayNewData): {
1002 uint32_t typeIndex, dataIndex;
1003 if (!iter.readArrayNewData(&typeIndex, &dataIndex, &nothing,
1004 &nothing)) {
1005 return false;
1006 }
1007 dumper.dumpTypeIndex(typeIndex);
1008 dumper.dumpDataIndex(dataIndex);
1009 break;
1010 }
1011 case uint32_t(GcOp::ArrayNewElem): {
1012 uint32_t typeIndex, elemIndex;
1013 if (!iter.readArrayNewElem(&typeIndex, &elemIndex, &nothing,
1014 &nothing)) {
1015 return false;
1016 }
1017 dumper.dumpTypeIndex(typeIndex);
1018 dumper.dumpElemIndex(elemIndex);
1019 break;
1020 }
1021 case uint32_t(GcOp::ArrayInitData): {
1022 uint32_t typeIndex, dataIndex;
1023 if (!iter.readArrayInitData(&typeIndex, &dataIndex, &nothing,
1024 &nothing, &nothing, &nothing)) {
1025 return false;
1026 }
1027 dumper.dumpTypeIndex(typeIndex);
1028 dumper.dumpDataIndex(dataIndex);
1029 break;
1030 }
1031 case uint32_t(GcOp::ArrayInitElem): {
1032 uint32_t typeIndex, elemIndex;
1033 if (!iter.readArrayInitElem(&typeIndex, &elemIndex, &nothing,
1034 &nothing, &nothing, &nothing)) {
1035 return false;
1036 }
1037 dumper.dumpTypeIndex(typeIndex);
1038 dumper.dumpElemIndex(elemIndex);
1039 break;
1040 }
1041 case uint32_t(GcOp::ArrayGet): {
1042 uint32_t typeIndex;
1043 if (!iter.readArrayGet(&typeIndex, FieldWideningOp::None, &nothing,
1044 &nothing)) {
1045 return false;
1046 }
1047 dumper.dumpTypeIndex(typeIndex);
1048 break;
1049 }
1050 case uint32_t(GcOp::ArrayGetS): {
1051 uint32_t typeIndex;
1052 if (!iter.readArrayGet(&typeIndex, FieldWideningOp::Signed,
1053 &nothing, &nothing)) {
1054 return false;
1055 }
1056 dumper.dumpTypeIndex(typeIndex);
1057 break;
1058 }
1059 case uint32_t(GcOp::ArrayGetU): {
1060 uint32_t typeIndex;
1061 if (!iter.readArrayGet(&typeIndex, FieldWideningOp::Unsigned,
1062 &nothing, &nothing)) {
1063 return false;
1064 }
1065 dumper.dumpTypeIndex(typeIndex);
1066 break;
1067 }
1068 case uint32_t(GcOp::ArraySet): {
1069 uint32_t typeIndex;
1070 if (!iter.readArraySet(&typeIndex, &nothing, &nothing, &nothing)) {
1071 return false;
1072 }
1073 dumper.dumpTypeIndex(typeIndex);
1074 break;
1075 }
1076 case uint32_t(GcOp::ArrayLen): {
1077 if (!iter.readArrayLen(&nothing)) {
1078 return false;
1079 }
1080 break;
1081 }
1082 case uint32_t(GcOp::ArrayCopy): {
1083 uint32_t dstArrayTypeIndex;
1084 uint32_t srcArrayTypeIndex;
1085 if (!iter.readArrayCopy(&dstArrayTypeIndex, &srcArrayTypeIndex,
1086 &nothing, &nothing, &nothing, &nothing,
1087 &nothing)) {
1088 return false;
1089 }
1090 dumper.dumpTypeIndex(dstArrayTypeIndex);
1091 dumper.dumpTypeIndex(srcArrayTypeIndex);
1092 break;
1093 }
1094 case uint32_t(GcOp::ArrayFill): {
1095 uint32_t typeIndex;
1096 if (!iter.readArrayFill(&typeIndex, &nothing, &nothing, &nothing,
1097 &nothing)) {
1098 return false;
1099 }
1100 dumper.dumpTypeIndex(typeIndex);
1101 break;
1102 }
1103 case uint32_t(GcOp::RefI31): {
1104 if (!iter.readConversion(ValType::I32,
1105 ValType(RefType::i31().asNonNullable()),
1106 &nothing)) {
1107 return false;
1108 }
1109 break;
1110 }
1111 case uint32_t(GcOp::I31GetS): {
1112 if (!iter.readConversion(ValType(RefType::i31()), ValType::I32,
1113 &nothing)) {
1114 return false;
1115 }
1116 break;
1117 }
1118 case uint32_t(GcOp::I31GetU): {
1119 if (!iter.readConversion(ValType(RefType::i31()), ValType::I32,
1120 &nothing)) {
1121 return false;
1122 }
1123 break;
1124 }
1125 case uint16_t(GcOp::RefTest): {
1126 RefType srcType;
1127 RefType destType;
1128 if (!iter.readRefTest(false, &srcType, &destType, &nothing)) {
1129 return false;
1130 }
1131 dumper.dumpRefType(destType);
1132 break;
1133 }
1134 case uint16_t(GcOp::RefTestNull): {
1135 RefType srcType;
1136 RefType destType;
1137 if (!iter.readRefTest(true, &srcType, &destType, &nothing)) {
1138 return false;
1139 }
1140 dumper.dumpRefType(srcType);
1141 dumper.dumpRefType(destType);
1142 break;
1143 }
1144 case uint16_t(GcOp::RefCast): {
1145 RefType srcType;
1146 RefType destType;
1147 if (!iter.readRefCast(false, &srcType, &destType, &nothing)) {
1148 return false;
1149 }
1150 dumper.dumpRefType(destType);
1151 break;
1152 }
1153 case uint16_t(GcOp::RefCastNull): {
1154 RefType srcType;
1155 RefType destType;
1156 if (!iter.readRefCast(true, &srcType, &destType, &nothing)) {
1157 return false;
1158 }
1159 dumper.dumpRefType(destType);
1160 break;
1161 }
1162 case uint16_t(GcOp::BrOnCast): {
1163 uint32_t relativeDepth;
1164 RefType srcType;
1165 RefType destType;
1166 if (!iter.readBrOnCast(true, &relativeDepth, &srcType, &destType,
1167 &resultType, &nothings)) {
1168 return false;
1169 }
1170 dumper.dumpBlockDepth(relativeDepth);
1171 dumper.dumpRefType(srcType);
1172 dumper.dumpRefType(destType);
1173 break;
1174 }
1175 case uint16_t(GcOp::BrOnCastFail): {
1176 uint32_t relativeDepth;
1177 RefType srcType;
1178 RefType destType;
1179 if (!iter.readBrOnCast(false, &relativeDepth, &srcType, &destType,
1180 &resultType, &nothings)) {
1181 return false;
1182 }
1183 dumper.dumpBlockDepth(relativeDepth);
1184 dumper.dumpRefType(srcType);
1185 dumper.dumpRefType(destType);
1186 break;
1187 }
1188 case uint16_t(GcOp::AnyConvertExtern): {
1189 if (!iter.readRefConversion(RefType::extern_(), RefType::any(),
1190 &nothing)) {
1191 return false;
1192 }
1193 break;
1194 }
1195 case uint16_t(GcOp::ExternConvertAny): {
1196 if (!iter.readRefConversion(RefType::any(), RefType::extern_(),
1197 &nothing)) {
1198 return false;
1199 }
1200 break;
1201 }
1202 default:
1203 return iter.unrecognizedOpcode(&op);
1204 }
1205 break;
1206 }
1207
1208 #ifdef ENABLE_WASM_SIMD
1209 case uint16_t(Op::SimdPrefix): {
1210 if (!codeMeta.simdAvailable()) {
1211 return iter.unrecognizedOpcode(&op);
1212 }
1213 uint32_t laneIndex;
1214 switch (op.b1) {
1215 case uint32_t(SimdOp::I8x16ExtractLaneS):
1216 case uint32_t(SimdOp::I8x16ExtractLaneU): {
1217 if (!iter.readExtractLane(ValType::I32, 16, &laneIndex, &nothing)) {
1218 return false;
1219 }
1220 dumper.dumpLaneIndex(laneIndex);
1221 break;
1222 }
1223 case uint32_t(SimdOp::I16x8ExtractLaneS):
1224 case uint32_t(SimdOp::I16x8ExtractLaneU): {
1225 if (!iter.readExtractLane(ValType::I32, 8, &laneIndex, &nothing)) {
1226 return false;
1227 }
1228 dumper.dumpLaneIndex(laneIndex);
1229 break;
1230 }
1231 case uint32_t(SimdOp::I32x4ExtractLane): {
1232 if (!iter.readExtractLane(ValType::I32, 4, &laneIndex, &nothing)) {
1233 return false;
1234 }
1235 dumper.dumpLaneIndex(laneIndex);
1236 break;
1237 }
1238 case uint32_t(SimdOp::I64x2ExtractLane): {
1239 if (!iter.readExtractLane(ValType::I64, 2, &laneIndex, &nothing)) {
1240 return false;
1241 }
1242 dumper.dumpLaneIndex(laneIndex);
1243 break;
1244 }
1245 case uint32_t(SimdOp::F32x4ExtractLane): {
1246 if (!iter.readExtractLane(ValType::F32, 4, &laneIndex, &nothing)) {
1247 return false;
1248 }
1249 dumper.dumpLaneIndex(laneIndex);
1250 break;
1251 }
1252 case uint32_t(SimdOp::F64x2ExtractLane): {
1253 if (!iter.readExtractLane(ValType::F64, 2, &laneIndex, &nothing)) {
1254 return false;
1255 }
1256 dumper.dumpLaneIndex(laneIndex);
1257 break;
1258 }
1259
1260 case uint32_t(SimdOp::I8x16Splat):
1261 case uint32_t(SimdOp::I16x8Splat):
1262 case uint32_t(SimdOp::I32x4Splat): {
1263 if (!iter.readConversion(ValType::I32, ValType::V128, &nothing)) {
1264 return false;
1265 }
1266 break;
1267 }
1268 case uint32_t(SimdOp::I64x2Splat): {
1269 if (!iter.readConversion(ValType::I64, ValType::V128, &nothing)) {
1270 return false;
1271 }
1272 break;
1273 }
1274 case uint32_t(SimdOp::F32x4Splat): {
1275 if (!iter.readConversion(ValType::F32, ValType::V128, &nothing)) {
1276 return false;
1277 }
1278 break;
1279 }
1280 case uint32_t(SimdOp::F64x2Splat): {
1281 if (!iter.readConversion(ValType::F64, ValType::V128, &nothing)) {
1282 return false;
1283 }
1284 break;
1285 }
1286
1287 case uint32_t(SimdOp::V128AnyTrue):
1288 case uint32_t(SimdOp::I8x16AllTrue):
1289 case uint32_t(SimdOp::I16x8AllTrue):
1290 case uint32_t(SimdOp::I32x4AllTrue):
1291 case uint32_t(SimdOp::I64x2AllTrue):
1292 case uint32_t(SimdOp::I8x16Bitmask):
1293 case uint32_t(SimdOp::I16x8Bitmask):
1294 case uint32_t(SimdOp::I32x4Bitmask):
1295 case uint32_t(SimdOp::I64x2Bitmask): {
1296 if (!iter.readConversion(ValType::V128, ValType::I32, &nothing)) {
1297 return false;
1298 }
1299 break;
1300 }
1301
1302 case uint32_t(SimdOp::I8x16ReplaceLane): {
1303 if (!iter.readReplaceLane(ValType::I32, 16, &laneIndex, &nothing,
1304 &nothing)) {
1305 return false;
1306 }
1307 dumper.dumpLaneIndex(laneIndex);
1308 break;
1309 }
1310 case uint32_t(SimdOp::I16x8ReplaceLane): {
1311 if (!iter.readReplaceLane(ValType::I32, 8, &laneIndex, &nothing,
1312 &nothing)) {
1313 return false;
1314 }
1315 dumper.dumpLaneIndex(laneIndex);
1316 break;
1317 }
1318 case uint32_t(SimdOp::I32x4ReplaceLane): {
1319 if (!iter.readReplaceLane(ValType::I32, 4, &laneIndex, &nothing,
1320 &nothing)) {
1321 return false;
1322 }
1323 dumper.dumpLaneIndex(laneIndex);
1324 break;
1325 }
1326 case uint32_t(SimdOp::I64x2ReplaceLane): {
1327 if (!iter.readReplaceLane(ValType::I64, 2, &laneIndex, &nothing,
1328 &nothing)) {
1329 return false;
1330 }
1331 dumper.dumpLaneIndex(laneIndex);
1332 break;
1333 }
1334 case uint32_t(SimdOp::F32x4ReplaceLane): {
1335 if (!iter.readReplaceLane(ValType::F32, 4, &laneIndex, &nothing,
1336 &nothing)) {
1337 return false;
1338 }
1339 dumper.dumpLaneIndex(laneIndex);
1340 break;
1341 }
1342 case uint32_t(SimdOp::F64x2ReplaceLane): {
1343 if (!iter.readReplaceLane(ValType::F64, 2, &laneIndex, &nothing,
1344 &nothing)) {
1345 return false;
1346 }
1347 dumper.dumpLaneIndex(laneIndex);
1348 break;
1349 }
1350
1351 case uint32_t(SimdOp::I8x16Eq):
1352 case uint32_t(SimdOp::I8x16Ne):
1353 case uint32_t(SimdOp::I8x16LtS):
1354 case uint32_t(SimdOp::I8x16LtU):
1355 case uint32_t(SimdOp::I8x16GtS):
1356 case uint32_t(SimdOp::I8x16GtU):
1357 case uint32_t(SimdOp::I8x16LeS):
1358 case uint32_t(SimdOp::I8x16LeU):
1359 case uint32_t(SimdOp::I8x16GeS):
1360 case uint32_t(SimdOp::I8x16GeU):
1361 case uint32_t(SimdOp::I16x8Eq):
1362 case uint32_t(SimdOp::I16x8Ne):
1363 case uint32_t(SimdOp::I16x8LtS):
1364 case uint32_t(SimdOp::I16x8LtU):
1365 case uint32_t(SimdOp::I16x8GtS):
1366 case uint32_t(SimdOp::I16x8GtU):
1367 case uint32_t(SimdOp::I16x8LeS):
1368 case uint32_t(SimdOp::I16x8LeU):
1369 case uint32_t(SimdOp::I16x8GeS):
1370 case uint32_t(SimdOp::I16x8GeU):
1371 case uint32_t(SimdOp::I32x4Eq):
1372 case uint32_t(SimdOp::I32x4Ne):
1373 case uint32_t(SimdOp::I32x4LtS):
1374 case uint32_t(SimdOp::I32x4LtU):
1375 case uint32_t(SimdOp::I32x4GtS):
1376 case uint32_t(SimdOp::I32x4GtU):
1377 case uint32_t(SimdOp::I32x4LeS):
1378 case uint32_t(SimdOp::I32x4LeU):
1379 case uint32_t(SimdOp::I32x4GeS):
1380 case uint32_t(SimdOp::I32x4GeU):
1381 case uint32_t(SimdOp::I64x2Eq):
1382 case uint32_t(SimdOp::I64x2Ne):
1383 case uint32_t(SimdOp::I64x2LtS):
1384 case uint32_t(SimdOp::I64x2GtS):
1385 case uint32_t(SimdOp::I64x2LeS):
1386 case uint32_t(SimdOp::I64x2GeS):
1387 case uint32_t(SimdOp::F32x4Eq):
1388 case uint32_t(SimdOp::F32x4Ne):
1389 case uint32_t(SimdOp::F32x4Lt):
1390 case uint32_t(SimdOp::F32x4Gt):
1391 case uint32_t(SimdOp::F32x4Le):
1392 case uint32_t(SimdOp::F32x4Ge):
1393 case uint32_t(SimdOp::F64x2Eq):
1394 case uint32_t(SimdOp::F64x2Ne):
1395 case uint32_t(SimdOp::F64x2Lt):
1396 case uint32_t(SimdOp::F64x2Gt):
1397 case uint32_t(SimdOp::F64x2Le):
1398 case uint32_t(SimdOp::F64x2Ge):
1399 case uint32_t(SimdOp::V128And):
1400 case uint32_t(SimdOp::V128Or):
1401 case uint32_t(SimdOp::V128Xor):
1402 case uint32_t(SimdOp::V128AndNot):
1403 case uint32_t(SimdOp::I8x16AvgrU):
1404 case uint32_t(SimdOp::I16x8AvgrU):
1405 case uint32_t(SimdOp::I8x16Add):
1406 case uint32_t(SimdOp::I8x16AddSatS):
1407 case uint32_t(SimdOp::I8x16AddSatU):
1408 case uint32_t(SimdOp::I8x16Sub):
1409 case uint32_t(SimdOp::I8x16SubSatS):
1410 case uint32_t(SimdOp::I8x16SubSatU):
1411 case uint32_t(SimdOp::I8x16MinS):
1412 case uint32_t(SimdOp::I8x16MinU):
1413 case uint32_t(SimdOp::I8x16MaxS):
1414 case uint32_t(SimdOp::I8x16MaxU):
1415 case uint32_t(SimdOp::I16x8Add):
1416 case uint32_t(SimdOp::I16x8AddSatS):
1417 case uint32_t(SimdOp::I16x8AddSatU):
1418 case uint32_t(SimdOp::I16x8Sub):
1419 case uint32_t(SimdOp::I16x8SubSatS):
1420 case uint32_t(SimdOp::I16x8SubSatU):
1421 case uint32_t(SimdOp::I16x8Mul):
1422 case uint32_t(SimdOp::I16x8MinS):
1423 case uint32_t(SimdOp::I16x8MinU):
1424 case uint32_t(SimdOp::I16x8MaxS):
1425 case uint32_t(SimdOp::I16x8MaxU):
1426 case uint32_t(SimdOp::I32x4Add):
1427 case uint32_t(SimdOp::I32x4Sub):
1428 case uint32_t(SimdOp::I32x4Mul):
1429 case uint32_t(SimdOp::I32x4MinS):
1430 case uint32_t(SimdOp::I32x4MinU):
1431 case uint32_t(SimdOp::I32x4MaxS):
1432 case uint32_t(SimdOp::I32x4MaxU):
1433 case uint32_t(SimdOp::I64x2Add):
1434 case uint32_t(SimdOp::I64x2Sub):
1435 case uint32_t(SimdOp::I64x2Mul):
1436 case uint32_t(SimdOp::F32x4Add):
1437 case uint32_t(SimdOp::F32x4Sub):
1438 case uint32_t(SimdOp::F32x4Mul):
1439 case uint32_t(SimdOp::F32x4Div):
1440 case uint32_t(SimdOp::F32x4Min):
1441 case uint32_t(SimdOp::F32x4Max):
1442 case uint32_t(SimdOp::F64x2Add):
1443 case uint32_t(SimdOp::F64x2Sub):
1444 case uint32_t(SimdOp::F64x2Mul):
1445 case uint32_t(SimdOp::F64x2Div):
1446 case uint32_t(SimdOp::F64x2Min):
1447 case uint32_t(SimdOp::F64x2Max):
1448 case uint32_t(SimdOp::I8x16NarrowI16x8S):
1449 case uint32_t(SimdOp::I8x16NarrowI16x8U):
1450 case uint32_t(SimdOp::I16x8NarrowI32x4S):
1451 case uint32_t(SimdOp::I16x8NarrowI32x4U):
1452 case uint32_t(SimdOp::I8x16Swizzle):
1453 case uint32_t(SimdOp::F32x4PMax):
1454 case uint32_t(SimdOp::F32x4PMin):
1455 case uint32_t(SimdOp::F64x2PMax):
1456 case uint32_t(SimdOp::F64x2PMin):
1457 case uint32_t(SimdOp::I32x4DotI16x8S):
1458 case uint32_t(SimdOp::I16x8ExtmulLowI8x16S):
1459 case uint32_t(SimdOp::I16x8ExtmulHighI8x16S):
1460 case uint32_t(SimdOp::I16x8ExtmulLowI8x16U):
1461 case uint32_t(SimdOp::I16x8ExtmulHighI8x16U):
1462 case uint32_t(SimdOp::I32x4ExtmulLowI16x8S):
1463 case uint32_t(SimdOp::I32x4ExtmulHighI16x8S):
1464 case uint32_t(SimdOp::I32x4ExtmulLowI16x8U):
1465 case uint32_t(SimdOp::I32x4ExtmulHighI16x8U):
1466 case uint32_t(SimdOp::I64x2ExtmulLowI32x4S):
1467 case uint32_t(SimdOp::I64x2ExtmulHighI32x4S):
1468 case uint32_t(SimdOp::I64x2ExtmulLowI32x4U):
1469 case uint32_t(SimdOp::I64x2ExtmulHighI32x4U):
1470 case uint32_t(SimdOp::I16x8Q15MulrSatS): {
1471 if (!iter.readBinary(ValType::V128, &nothing, &nothing)) {
1472 return false;
1473 }
1474 break;
1475 }
1476
1477 case uint32_t(SimdOp::I8x16Neg):
1478 case uint32_t(SimdOp::I16x8Neg):
1479 case uint32_t(SimdOp::I16x8ExtendLowI8x16S):
1480 case uint32_t(SimdOp::I16x8ExtendHighI8x16S):
1481 case uint32_t(SimdOp::I16x8ExtendLowI8x16U):
1482 case uint32_t(SimdOp::I16x8ExtendHighI8x16U):
1483 case uint32_t(SimdOp::I32x4Neg):
1484 case uint32_t(SimdOp::I32x4ExtendLowI16x8S):
1485 case uint32_t(SimdOp::I32x4ExtendHighI16x8S):
1486 case uint32_t(SimdOp::I32x4ExtendLowI16x8U):
1487 case uint32_t(SimdOp::I32x4ExtendHighI16x8U):
1488 case uint32_t(SimdOp::I32x4TruncSatF32x4S):
1489 case uint32_t(SimdOp::I32x4TruncSatF32x4U):
1490 case uint32_t(SimdOp::I64x2Neg):
1491 case uint32_t(SimdOp::I64x2ExtendLowI32x4S):
1492 case uint32_t(SimdOp::I64x2ExtendHighI32x4S):
1493 case uint32_t(SimdOp::I64x2ExtendLowI32x4U):
1494 case uint32_t(SimdOp::I64x2ExtendHighI32x4U):
1495 case uint32_t(SimdOp::F32x4Abs):
1496 case uint32_t(SimdOp::F32x4Neg):
1497 case uint32_t(SimdOp::F32x4Sqrt):
1498 case uint32_t(SimdOp::F32x4ConvertI32x4S):
1499 case uint32_t(SimdOp::F32x4ConvertI32x4U):
1500 case uint32_t(SimdOp::F64x2Abs):
1501 case uint32_t(SimdOp::F64x2Neg):
1502 case uint32_t(SimdOp::F64x2Sqrt):
1503 case uint32_t(SimdOp::V128Not):
1504 case uint32_t(SimdOp::I8x16Popcnt):
1505 case uint32_t(SimdOp::I8x16Abs):
1506 case uint32_t(SimdOp::I16x8Abs):
1507 case uint32_t(SimdOp::I32x4Abs):
1508 case uint32_t(SimdOp::I64x2Abs):
1509 case uint32_t(SimdOp::F32x4Ceil):
1510 case uint32_t(SimdOp::F32x4Floor):
1511 case uint32_t(SimdOp::F32x4Trunc):
1512 case uint32_t(SimdOp::F32x4Nearest):
1513 case uint32_t(SimdOp::F64x2Ceil):
1514 case uint32_t(SimdOp::F64x2Floor):
1515 case uint32_t(SimdOp::F64x2Trunc):
1516 case uint32_t(SimdOp::F64x2Nearest):
1517 case uint32_t(SimdOp::F32x4DemoteF64x2Zero):
1518 case uint32_t(SimdOp::F64x2PromoteLowF32x4):
1519 case uint32_t(SimdOp::F64x2ConvertLowI32x4S):
1520 case uint32_t(SimdOp::F64x2ConvertLowI32x4U):
1521 case uint32_t(SimdOp::I32x4TruncSatF64x2SZero):
1522 case uint32_t(SimdOp::I32x4TruncSatF64x2UZero):
1523 case uint32_t(SimdOp::I16x8ExtaddPairwiseI8x16S):
1524 case uint32_t(SimdOp::I16x8ExtaddPairwiseI8x16U):
1525 case uint32_t(SimdOp::I32x4ExtaddPairwiseI16x8S):
1526 case uint32_t(SimdOp::I32x4ExtaddPairwiseI16x8U): {
1527 if (!iter.readUnary(ValType::V128, &nothing)) {
1528 return false;
1529 }
1530 break;
1531 }
1532
1533 case uint32_t(SimdOp::I8x16Shl):
1534 case uint32_t(SimdOp::I8x16ShrS):
1535 case uint32_t(SimdOp::I8x16ShrU):
1536 case uint32_t(SimdOp::I16x8Shl):
1537 case uint32_t(SimdOp::I16x8ShrS):
1538 case uint32_t(SimdOp::I16x8ShrU):
1539 case uint32_t(SimdOp::I32x4Shl):
1540 case uint32_t(SimdOp::I32x4ShrS):
1541 case uint32_t(SimdOp::I32x4ShrU):
1542 case uint32_t(SimdOp::I64x2Shl):
1543 case uint32_t(SimdOp::I64x2ShrS):
1544 case uint32_t(SimdOp::I64x2ShrU): {
1545 if (!iter.readVectorShift(&nothing, &nothing)) {
1546 return false;
1547 }
1548 break;
1549 }
1550
1551 case uint32_t(SimdOp::V128Bitselect): {
1552 if (!iter.readTernary(ValType::V128, &nothing, &nothing,
1553 &nothing)) {
1554 return false;
1555 }
1556 break;
1557 }
1558
1559 case uint32_t(SimdOp::I8x16Shuffle): {
1560 V128 mask;
1561 if (!iter.readVectorShuffle(&nothing, &nothing, &mask)) {
1562 return false;
1563 }
1564 dumper.dumpVectorMask(mask);
1565 break;
1566 }
1567
1568 case uint32_t(SimdOp::V128Const): {
1569 V128 constant;
1570 if (!iter.readV128Const(&constant)) {
1571 return false;
1572 }
1573 dumper.dumpV128Const(constant);
1574 break;
1575 }
1576
1577 case uint32_t(SimdOp::V128Load): {
1578 LinearMemoryAddress<Nothing> addr;
1579 if (!iter.readLoad(ValType::V128, 16, &addr)) {
1580 return false;
1581 }
1582 dumper.dumpLinearMemoryAddress(addr);
1583 break;
1584 }
1585
1586 case uint32_t(SimdOp::V128Load8Splat): {
1587 LinearMemoryAddress<Nothing> addr;
1588 if (!iter.readLoadSplat(1, &addr)) {
1589 return false;
1590 }
1591 dumper.dumpLinearMemoryAddress(addr);
1592 break;
1593 }
1594
1595 case uint32_t(SimdOp::V128Load16Splat): {
1596 LinearMemoryAddress<Nothing> addr;
1597 if (!iter.readLoadSplat(2, &addr)) {
1598 return false;
1599 }
1600 dumper.dumpLinearMemoryAddress(addr);
1601 break;
1602 }
1603
1604 case uint32_t(SimdOp::V128Load32Splat): {
1605 LinearMemoryAddress<Nothing> addr;
1606 if (!iter.readLoadSplat(4, &addr)) {
1607 return false;
1608 }
1609 dumper.dumpLinearMemoryAddress(addr);
1610 break;
1611 }
1612
1613 case uint32_t(SimdOp::V128Load64Splat): {
1614 LinearMemoryAddress<Nothing> addr;
1615 if (!iter.readLoadSplat(8, &addr)) {
1616 return false;
1617 }
1618 dumper.dumpLinearMemoryAddress(addr);
1619 break;
1620 }
1621
1622 case uint32_t(SimdOp::V128Load8x8S):
1623 case uint32_t(SimdOp::V128Load8x8U): {
1624 LinearMemoryAddress<Nothing> addr;
1625 if (!iter.readLoadExtend(&addr)) {
1626 return false;
1627 }
1628 dumper.dumpLinearMemoryAddress(addr);
1629 break;
1630 }
1631
1632 case uint32_t(SimdOp::V128Load16x4S):
1633 case uint32_t(SimdOp::V128Load16x4U): {
1634 LinearMemoryAddress<Nothing> addr;
1635 if (!iter.readLoadExtend(&addr)) {
1636 return false;
1637 }
1638 dumper.dumpLinearMemoryAddress(addr);
1639 break;
1640 }
1641
1642 case uint32_t(SimdOp::V128Load32x2S):
1643 case uint32_t(SimdOp::V128Load32x2U): {
1644 LinearMemoryAddress<Nothing> addr;
1645 if (!iter.readLoadExtend(&addr)) {
1646 return false;
1647 }
1648 dumper.dumpLinearMemoryAddress(addr);
1649 break;
1650 }
1651
1652 case uint32_t(SimdOp::V128Store): {
1653 LinearMemoryAddress<Nothing> addr;
1654 if (!iter.readStore(ValType::V128, 16, &addr, &nothing)) {
1655 return false;
1656 }
1657 dumper.dumpLinearMemoryAddress(addr);
1658 break;
1659 }
1660
1661 case uint32_t(SimdOp::V128Load32Zero): {
1662 LinearMemoryAddress<Nothing> addr;
1663 if (!iter.readLoadSplat(4, &addr)) {
1664 return false;
1665 }
1666 dumper.dumpLinearMemoryAddress(addr);
1667 break;
1668 }
1669
1670 case uint32_t(SimdOp::V128Load64Zero): {
1671 LinearMemoryAddress<Nothing> addr;
1672 if (!iter.readLoadSplat(8, &addr)) {
1673 return false;
1674 }
1675 dumper.dumpLinearMemoryAddress(addr);
1676 break;
1677 }
1678
1679 case uint32_t(SimdOp::V128Load8Lane): {
1680 LinearMemoryAddress<Nothing> addr;
1681 if (!iter.readLoadLane(1, &addr, &laneIndex, &nothing)) {
1682 return false;
1683 }
1684 dumper.dumpLinearMemoryAddress(addr);
1685 dumper.dumpLaneIndex(laneIndex);
1686 break;
1687 }
1688
1689 case uint32_t(SimdOp::V128Load16Lane): {
1690 LinearMemoryAddress<Nothing> addr;
1691 if (!iter.readLoadLane(2, &addr, &laneIndex, &nothing)) {
1692 return false;
1693 }
1694 dumper.dumpLinearMemoryAddress(addr);
1695 dumper.dumpLaneIndex(laneIndex);
1696 break;
1697 }
1698
1699 case uint32_t(SimdOp::V128Load32Lane): {
1700 LinearMemoryAddress<Nothing> addr;
1701 if (!iter.readLoadLane(4, &addr, &laneIndex, &nothing)) {
1702 return false;
1703 }
1704 dumper.dumpLinearMemoryAddress(addr);
1705 dumper.dumpLaneIndex(laneIndex);
1706 break;
1707 }
1708
1709 case uint32_t(SimdOp::V128Load64Lane): {
1710 LinearMemoryAddress<Nothing> addr;
1711 if (!iter.readLoadLane(8, &addr, &laneIndex, &nothing)) {
1712 return false;
1713 }
1714 dumper.dumpLinearMemoryAddress(addr);
1715 dumper.dumpLaneIndex(laneIndex);
1716 break;
1717 }
1718
1719 case uint32_t(SimdOp::V128Store8Lane): {
1720 LinearMemoryAddress<Nothing> addr;
1721 if (!iter.readStoreLane(1, &addr, &laneIndex, &nothing)) {
1722 return false;
1723 }
1724 dumper.dumpLinearMemoryAddress(addr);
1725 dumper.dumpLaneIndex(laneIndex);
1726 break;
1727 }
1728
1729 case uint32_t(SimdOp::V128Store16Lane): {
1730 LinearMemoryAddress<Nothing> addr;
1731 if (!iter.readStoreLane(2, &addr, &laneIndex, &nothing)) {
1732 return false;
1733 }
1734 dumper.dumpLinearMemoryAddress(addr);
1735 dumper.dumpLaneIndex(laneIndex);
1736 break;
1737 }
1738
1739 case uint32_t(SimdOp::V128Store32Lane): {
1740 LinearMemoryAddress<Nothing> addr;
1741 if (!iter.readStoreLane(4, &addr, &laneIndex, &nothing)) {
1742 return false;
1743 }
1744 dumper.dumpLinearMemoryAddress(addr);
1745 dumper.dumpLaneIndex(laneIndex);
1746 break;
1747 }
1748
1749 case uint32_t(SimdOp::V128Store64Lane): {
1750 LinearMemoryAddress<Nothing> addr;
1751 if (!iter.readStoreLane(8, &addr, &laneIndex, &nothing)) {
1752 return false;
1753 }
1754 dumper.dumpLinearMemoryAddress(addr);
1755 dumper.dumpLaneIndex(laneIndex);
1756 break;
1757 }
1758
1759 # ifdef ENABLE_WASM_RELAXED_SIMD
1760 case uint32_t(SimdOp::F32x4RelaxedMadd):
1761 case uint32_t(SimdOp::F32x4RelaxedNmadd):
1762 case uint32_t(SimdOp::F64x2RelaxedMadd):
1763 case uint32_t(SimdOp::F64x2RelaxedNmadd):
1764 case uint32_t(SimdOp::I8x16RelaxedLaneSelect):
1765 case uint32_t(SimdOp::I16x8RelaxedLaneSelect):
1766 case uint32_t(SimdOp::I32x4RelaxedLaneSelect):
1767 case uint32_t(SimdOp::I64x2RelaxedLaneSelect):
1768 case uint32_t(SimdOp::I32x4RelaxedDotI8x16I7x16AddS): {
1769 if (!codeMeta.v128RelaxedEnabled()) {
1770 return iter.unrecognizedOpcode(&op);
1771 }
1772 if (!iter.readTernary(ValType::V128, &nothing, &nothing,
1773 &nothing)) {
1774 return false;
1775 }
1776 break;
1777 }
1778 case uint32_t(SimdOp::F32x4RelaxedMin):
1779 case uint32_t(SimdOp::F32x4RelaxedMax):
1780 case uint32_t(SimdOp::F64x2RelaxedMin):
1781 case uint32_t(SimdOp::F64x2RelaxedMax):
1782 case uint32_t(SimdOp::I16x8RelaxedQ15MulrS):
1783 case uint32_t(SimdOp::I16x8RelaxedDotI8x16I7x16S): {
1784 if (!codeMeta.v128RelaxedEnabled()) {
1785 return iter.unrecognizedOpcode(&op);
1786 }
1787 if (!iter.readBinary(ValType::V128, &nothing, &nothing)) {
1788 return false;
1789 }
1790 break;
1791 }
1792 case uint32_t(SimdOp::I32x4RelaxedTruncF32x4S):
1793 case uint32_t(SimdOp::I32x4RelaxedTruncF32x4U):
1794 case uint32_t(SimdOp::I32x4RelaxedTruncF64x2SZero):
1795 case uint32_t(SimdOp::I32x4RelaxedTruncF64x2UZero): {
1796 if (!codeMeta.v128RelaxedEnabled()) {
1797 return iter.unrecognizedOpcode(&op);
1798 }
1799 if (!iter.readUnary(ValType::V128, &nothing)) {
1800 return false;
1801 }
1802 break;
1803 }
1804 case uint32_t(SimdOp::I8x16RelaxedSwizzle): {
1805 if (!codeMeta.v128RelaxedEnabled()) {
1806 return iter.unrecognizedOpcode(&op);
1807 }
1808 if (!iter.readBinary(ValType::V128, &nothing, &nothing)) {
1809 return false;
1810 }
1811 break;
1812 }
1813 # endif
1814
1815 default:
1816 return iter.unrecognizedOpcode(&op);
1817 }
1818 break;
1819 }
1820 #endif // ENABLE_WASM_SIMD
1821
1822 case uint16_t(Op::MiscPrefix): {
1823 switch (op.b1) {
1824 case uint32_t(MiscOp::I32TruncSatF32S):
1825 case uint32_t(MiscOp::I32TruncSatF32U): {
1826 if (!iter.readConversion(ValType::F32, ValType::I32, &nothing)) {
1827 return false;
1828 }
1829 break;
1830 }
1831 case uint32_t(MiscOp::I32TruncSatF64S):
1832 case uint32_t(MiscOp::I32TruncSatF64U): {
1833 if (!iter.readConversion(ValType::F64, ValType::I32, &nothing)) {
1834 return false;
1835 }
1836 break;
1837 }
1838 case uint32_t(MiscOp::I64TruncSatF32S):
1839 case uint32_t(MiscOp::I64TruncSatF32U): {
1840 if (!iter.readConversion(ValType::F32, ValType::I64, &nothing)) {
1841 return false;
1842 }
1843 break;
1844 }
1845 case uint32_t(MiscOp::I64TruncSatF64S):
1846 case uint32_t(MiscOp::I64TruncSatF64U): {
1847 if (!iter.readConversion(ValType::F64, ValType::I64, &nothing)) {
1848 return false;
1849 }
1850 break;
1851 }
1852 case uint32_t(MiscOp::MemoryCopy): {
1853 uint32_t destMemIndex;
1854 uint32_t srcMemIndex;
1855 if (!iter.readMemOrTableCopy(/*isMem=*/true, &destMemIndex,
1856 &nothing, &srcMemIndex, &nothing,
1857 &nothing)) {
1858 return false;
1859 }
1860 dumper.dumpMemoryIndex(destMemIndex);
1861 dumper.dumpMemoryIndex(srcMemIndex);
1862 break;
1863 }
1864 case uint32_t(MiscOp::DataDrop): {
1865 uint32_t dataIndex;
1866 if (!iter.readDataOrElemDrop(/*isData=*/true, &dataIndex)) {
1867 return false;
1868 }
1869 dumper.dumpDataIndex(dataIndex);
1870 break;
1871 }
1872 case uint32_t(MiscOp::MemoryFill): {
1873 uint32_t memoryIndex;
1874 if (!iter.readMemFill(&memoryIndex, &nothing, &nothing, &nothing)) {
1875 return false;
1876 }
1877 dumper.dumpMemoryIndex(memoryIndex);
1878 break;
1879 }
1880 case uint32_t(MiscOp::MemoryInit): {
1881 uint32_t dataIndex;
1882 uint32_t memoryIndex;
1883 if (!iter.readMemOrTableInit(/*isMem=*/true, &dataIndex,
1884 &memoryIndex, &nothing, &nothing,
1885 &nothing)) {
1886 return false;
1887 }
1888 dumper.dumpMemoryIndex(memoryIndex);
1889 dumper.dumpDataIndex(dataIndex);
1890 break;
1891 }
1892 case uint32_t(MiscOp::TableCopy): {
1893 uint32_t destTableIndex;
1894 uint32_t srcTableIndex;
1895 if (!iter.readMemOrTableCopy(
1896 /*isMem=*/false, &destTableIndex, &nothing, &srcTableIndex,
1897 &nothing, &nothing)) {
1898 return false;
1899 }
1900 dumper.dumpTableIndex(destTableIndex);
1901 dumper.dumpTableIndex(srcTableIndex);
1902 break;
1903 }
1904 case uint32_t(MiscOp::ElemDrop): {
1905 uint32_t elemIndex;
1906 if (!iter.readDataOrElemDrop(/*isData=*/false, &elemIndex)) {
1907 return false;
1908 }
1909 dumper.dumpElemIndex(elemIndex);
1910 break;
1911 }
1912 case uint32_t(MiscOp::TableInit): {
1913 uint32_t elemIndex;
1914 uint32_t tableIndex;
1915 if (!iter.readMemOrTableInit(/*isMem=*/false, &elemIndex,
1916 &tableIndex, &nothing, &nothing,
1917 &nothing)) {
1918 return false;
1919 }
1920 dumper.dumpTableIndex(tableIndex);
1921 dumper.dumpElemIndex(elemIndex);
1922 break;
1923 }
1924 case uint32_t(MiscOp::TableFill): {
1925 uint32_t tableIndex;
1926 if (!iter.readTableFill(&tableIndex, &nothing, &nothing,
1927 &nothing)) {
1928 return false;
1929 }
1930 dumper.dumpTableIndex(tableIndex);
1931 break;
1932 }
1933 #ifdef ENABLE_WASM_MEMORY_CONTROL
1934 case uint32_t(MiscOp::MemoryDiscard): {
1935 if (!codeMeta.memoryControlEnabled()) {
1936 return iter.unrecognizedOpcode(&op);
1937 }
1938 uint32_t memoryIndex;
1939 if (!iter.readMemDiscard(&memoryIndex, &nothing, &nothing)) {
1940 return false;
1941 }
1942 dumper.dumpMemoryIndex(memoryIndex);
1943 break;
1944 }
1945 #endif
1946 case uint32_t(MiscOp::TableGrow): {
1947 uint32_t tableIndex;
1948 if (!iter.readTableGrow(&tableIndex, &nothing, &nothing)) {
1949 return false;
1950 }
1951 dumper.dumpTableIndex(tableIndex);
1952 break;
1953 }
1954 case uint32_t(MiscOp::TableSize): {
1955 uint32_t tableIndex;
1956 if (!iter.readTableSize(&tableIndex)) {
1957 return false;
1958 }
1959 dumper.dumpTableIndex(tableIndex);
1960 break;
1961 }
1962 default:
1963 return iter.unrecognizedOpcode(&op);
1964 }
1965 break;
1966 }
1967 case uint16_t(Op::RefAsNonNull): {
1968 if (!iter.readRefAsNonNull(&nothing)) {
1969 return false;
1970 }
1971 break;
1972 }
1973 case uint16_t(Op::BrOnNull): {
1974 uint32_t depth;
1975 if (!iter.readBrOnNull(&depth, &resultType, &nothings, &nothing)) {
1976 return false;
1977 }
1978 dumper.dumpBlockDepth(depth);
1979 break;
1980 }
1981 case uint16_t(Op::BrOnNonNull): {
1982 uint32_t depth;
1983 if (!iter.readBrOnNonNull(&depth, &resultType, &nothings, &nothing)) {
1984 return false;
1985 }
1986 dumper.dumpBlockDepth(depth);
1987 break;
1988 }
1989 case uint16_t(Op::RefEq): {
1990 if (!iter.readComparison(RefType::eq(), &nothing, &nothing)) {
1991 return false;
1992 }
1993 break;
1994 }
1995 case uint16_t(Op::RefFunc): {
1996 uint32_t funcIndex;
1997 if (!iter.readRefFunc(&funcIndex)) {
1998 return false;
1999 }
2000 dumper.dumpFuncIndex(funcIndex);
2001 break;
2002 }
2003 case uint16_t(Op::RefNull): {
2004 RefType type;
2005 if (!iter.readRefNull(&type)) {
2006 return false;
2007 }
2008 dumper.dumpHeapType(type);
2009 break;
2010 }
2011 case uint16_t(Op::RefIsNull): {
2012 Nothing nothing;
2013 if (!iter.readRefIsNull(&nothing)) {
2014 return false;
2015 }
2016 break;
2017 }
2018 case uint16_t(Op::Try): {
2019 if (!iter.readTry(&blockType)) {
2020 return false;
2021 }
2022 dumper.dumpBlockType(blockType);
2023 break;
2024 }
2025 case uint16_t(Op::Catch): {
2026 LabelKind unusedKind;
2027 uint32_t tagIndex;
2028 if (!iter.readCatch(&unusedKind, &tagIndex, &resultType, &resultType,
2029 &nothings)) {
2030 return false;
2031 }
2032 dumper.dumpTagIndex(tagIndex);
2033 break;
2034 }
2035 case uint16_t(Op::CatchAll): {
2036 LabelKind unusedKind;
2037 if (!iter.readCatchAll(&unusedKind, &resultType, &resultType,
2038 &nothings)) {
2039 return false;
2040 }
2041 break;
2042 }
2043 case uint16_t(Op::Delegate): {
2044 uint32_t depth;
2045 if (!iter.readDelegate(&depth, &resultType, &nothings)) {
2046 return false;
2047 }
2048 iter.popDelegate();
2049 dumper.dumpBlockDepth(depth);
2050 break;
2051 }
2052 case uint16_t(Op::Throw): {
2053 uint32_t tagIndex;
2054 if (!iter.readThrow(&tagIndex, &nothings)) {
2055 return false;
2056 }
2057 dumper.dumpTagIndex(tagIndex);
2058 break;
2059 }
2060 case uint16_t(Op::Rethrow): {
2061 uint32_t depth;
2062 if (!iter.readRethrow(&depth)) {
2063 return false;
2064 }
2065 dumper.dumpBlockDepth(depth);
2066 break;
2067 }
2068 case uint16_t(Op::ThrowRef): {
2069 if (!codeMeta.exnrefEnabled()) {
2070 return iter.unrecognizedOpcode(&op);
2071 }
2072 if (!iter.readThrowRef(&nothing)) {
2073 return false;
2074 }
2075 break;
2076 }
2077 case uint16_t(Op::TryTable): {
2078 if (!codeMeta.exnrefEnabled()) {
2079 return iter.unrecognizedOpcode(&op);
2080 }
2081 TryTableCatchVector catches;
2082 if (!iter.readTryTable(&blockType, &catches)) {
2083 return false;
2084 }
2085 dumper.dumpTryTableCatches(catches);
2086 break;
2087 }
2088 case uint16_t(Op::ThreadPrefix): {
2089 // Though thread ops can be used on nonshared memories, we make them
2090 // unavailable if shared memory has been disabled in the prefs, for
2091 // maximum predictability and safety and consistency with JS.
2092 if (codeMeta.sharedMemoryEnabled() == Shareable::False) {
2093 return iter.unrecognizedOpcode(&op);
2094 }
2095 switch (op.b1) {
2096 case uint32_t(ThreadOp::Notify): {
2097 LinearMemoryAddress<Nothing> addr;
2098 if (!iter.readNotify(&addr, &nothing)) {
2099 return false;
2100 }
2101 dumper.dumpLinearMemoryAddress(addr);
2102 break;
2103 }
2104 case uint32_t(ThreadOp::I32Wait): {
2105 LinearMemoryAddress<Nothing> addr;
2106 if (!iter.readWait(&addr, ValType::I32, 4, &nothing, &nothing)) {
2107 return false;
2108 }
2109 dumper.dumpLinearMemoryAddress(addr);
2110 break;
2111 }
2112 case uint32_t(ThreadOp::I64Wait): {
2113 LinearMemoryAddress<Nothing> addr;
2114 if (!iter.readWait(&addr, ValType::I64, 8, &nothing, &nothing)) {
2115 return false;
2116 }
2117 dumper.dumpLinearMemoryAddress(addr);
2118 break;
2119 }
2120 case uint32_t(ThreadOp::Fence): {
2121 if (!iter.readFence()) {
2122 return false;
2123 }
2124 break;
2125 }
2126 case uint32_t(ThreadOp::I32AtomicLoad): {
2127 LinearMemoryAddress<Nothing> addr;
2128 if (!iter.readAtomicLoad(&addr, ValType::I32, 4)) {
2129 return false;
2130 }
2131 dumper.dumpLinearMemoryAddress(addr);
2132 break;
2133 }
2134 case uint32_t(ThreadOp::I64AtomicLoad): {
2135 LinearMemoryAddress<Nothing> addr;
2136 if (!iter.readAtomicLoad(&addr, ValType::I64, 8)) {
2137 return false;
2138 }
2139 dumper.dumpLinearMemoryAddress(addr);
2140 break;
2141 }
2142 case uint32_t(ThreadOp::I32AtomicLoad8U): {
2143 LinearMemoryAddress<Nothing> addr;
2144 if (!iter.readAtomicLoad(&addr, ValType::I32, 1)) {
2145 return false;
2146 }
2147 dumper.dumpLinearMemoryAddress(addr);
2148 break;
2149 }
2150 case uint32_t(ThreadOp::I32AtomicLoad16U): {
2151 LinearMemoryAddress<Nothing> addr;
2152 if (!iter.readAtomicLoad(&addr, ValType::I32, 2)) {
2153 return false;
2154 }
2155 dumper.dumpLinearMemoryAddress(addr);
2156 break;
2157 }
2158 case uint32_t(ThreadOp::I64AtomicLoad8U): {
2159 LinearMemoryAddress<Nothing> addr;
2160 if (!iter.readAtomicLoad(&addr, ValType::I64, 1)) {
2161 return false;
2162 }
2163 dumper.dumpLinearMemoryAddress(addr);
2164 break;
2165 }
2166 case uint32_t(ThreadOp::I64AtomicLoad16U): {
2167 LinearMemoryAddress<Nothing> addr;
2168 if (!iter.readAtomicLoad(&addr, ValType::I64, 2)) {
2169 return false;
2170 }
2171 dumper.dumpLinearMemoryAddress(addr);
2172 break;
2173 }
2174 case uint32_t(ThreadOp::I64AtomicLoad32U): {
2175 LinearMemoryAddress<Nothing> addr;
2176 if (!iter.readAtomicLoad(&addr, ValType::I64, 4)) {
2177 return false;
2178 }
2179 dumper.dumpLinearMemoryAddress(addr);
2180 break;
2181 }
2182 case uint32_t(ThreadOp::I32AtomicStore): {
2183 LinearMemoryAddress<Nothing> addr;
2184 if (!iter.readAtomicStore(&addr, ValType::I32, 4, &nothing)) {
2185 return false;
2186 }
2187 dumper.dumpLinearMemoryAddress(addr);
2188 break;
2189 }
2190 case uint32_t(ThreadOp::I64AtomicStore): {
2191 LinearMemoryAddress<Nothing> addr;
2192 if (!iter.readAtomicStore(&addr, ValType::I64, 8, &nothing)) {
2193 return false;
2194 }
2195 dumper.dumpLinearMemoryAddress(addr);
2196 break;
2197 }
2198 case uint32_t(ThreadOp::I32AtomicStore8U): {
2199 LinearMemoryAddress<Nothing> addr;
2200 if (!iter.readAtomicStore(&addr, ValType::I32, 1, &nothing)) {
2201 return false;
2202 }
2203 dumper.dumpLinearMemoryAddress(addr);
2204 break;
2205 }
2206 case uint32_t(ThreadOp::I32AtomicStore16U): {
2207 LinearMemoryAddress<Nothing> addr;
2208 if (!iter.readAtomicStore(&addr, ValType::I32, 2, &nothing)) {
2209 return false;
2210 }
2211 dumper.dumpLinearMemoryAddress(addr);
2212 break;
2213 }
2214 case uint32_t(ThreadOp::I64AtomicStore8U): {
2215 LinearMemoryAddress<Nothing> addr;
2216 if (!iter.readAtomicStore(&addr, ValType::I64, 1, &nothing)) {
2217 return false;
2218 }
2219 dumper.dumpLinearMemoryAddress(addr);
2220 break;
2221 }
2222 case uint32_t(ThreadOp::I64AtomicStore16U): {
2223 LinearMemoryAddress<Nothing> addr;
2224 if (!iter.readAtomicStore(&addr, ValType::I64, 2, &nothing)) {
2225 return false;
2226 }
2227 dumper.dumpLinearMemoryAddress(addr);
2228 break;
2229 }
2230 case uint32_t(ThreadOp::I64AtomicStore32U): {
2231 LinearMemoryAddress<Nothing> addr;
2232 if (!iter.readAtomicStore(&addr, ValType::I64, 4, &nothing)) {
2233 return false;
2234 }
2235 dumper.dumpLinearMemoryAddress(addr);
2236 break;
2237 }
2238 case uint32_t(ThreadOp::I32AtomicAdd):
2239 case uint32_t(ThreadOp::I32AtomicSub):
2240 case uint32_t(ThreadOp::I32AtomicAnd):
2241 case uint32_t(ThreadOp::I32AtomicOr):
2242 case uint32_t(ThreadOp::I32AtomicXor):
2243 case uint32_t(ThreadOp::I32AtomicXchg): {
2244 LinearMemoryAddress<Nothing> addr;
2245 if (!iter.readAtomicRMW(&addr, ValType::I32, 4, &nothing)) {
2246 return false;
2247 }
2248 dumper.dumpLinearMemoryAddress(addr);
2249 break;
2250 }
2251 case uint32_t(ThreadOp::I64AtomicAdd):
2252 case uint32_t(ThreadOp::I64AtomicSub):
2253 case uint32_t(ThreadOp::I64AtomicAnd):
2254 case uint32_t(ThreadOp::I64AtomicOr):
2255 case uint32_t(ThreadOp::I64AtomicXor):
2256 case uint32_t(ThreadOp::I64AtomicXchg): {
2257 LinearMemoryAddress<Nothing> addr;
2258 if (!iter.readAtomicRMW(&addr, ValType::I64, 8, &nothing)) {
2259 return false;
2260 }
2261 dumper.dumpLinearMemoryAddress(addr);
2262 break;
2263 }
2264 case uint32_t(ThreadOp::I32AtomicAdd8U):
2265 case uint32_t(ThreadOp::I32AtomicSub8U):
2266 case uint32_t(ThreadOp::I32AtomicAnd8U):
2267 case uint32_t(ThreadOp::I32AtomicOr8U):
2268 case uint32_t(ThreadOp::I32AtomicXor8U):
2269 case uint32_t(ThreadOp::I32AtomicXchg8U): {
2270 LinearMemoryAddress<Nothing> addr;
2271 if (!iter.readAtomicRMW(&addr, ValType::I32, 1, &nothing)) {
2272 return false;
2273 }
2274 dumper.dumpLinearMemoryAddress(addr);
2275 break;
2276 }
2277 case uint32_t(ThreadOp::I32AtomicAdd16U):
2278 case uint32_t(ThreadOp::I32AtomicSub16U):
2279 case uint32_t(ThreadOp::I32AtomicAnd16U):
2280 case uint32_t(ThreadOp::I32AtomicOr16U):
2281 case uint32_t(ThreadOp::I32AtomicXor16U):
2282 case uint32_t(ThreadOp::I32AtomicXchg16U): {
2283 LinearMemoryAddress<Nothing> addr;
2284 if (!iter.readAtomicRMW(&addr, ValType::I32, 2, &nothing)) {
2285 return false;
2286 }
2287 dumper.dumpLinearMemoryAddress(addr);
2288 break;
2289 }
2290 case uint32_t(ThreadOp::I64AtomicAdd8U):
2291 case uint32_t(ThreadOp::I64AtomicSub8U):
2292 case uint32_t(ThreadOp::I64AtomicAnd8U):
2293 case uint32_t(ThreadOp::I64AtomicOr8U):
2294 case uint32_t(ThreadOp::I64AtomicXor8U):
2295 case uint32_t(ThreadOp::I64AtomicXchg8U): {
2296 LinearMemoryAddress<Nothing> addr;
2297 if (!iter.readAtomicRMW(&addr, ValType::I64, 1, &nothing)) {
2298 return false;
2299 }
2300 dumper.dumpLinearMemoryAddress(addr);
2301 break;
2302 }
2303 case uint32_t(ThreadOp::I64AtomicAdd16U):
2304 case uint32_t(ThreadOp::I64AtomicSub16U):
2305 case uint32_t(ThreadOp::I64AtomicAnd16U):
2306 case uint32_t(ThreadOp::I64AtomicOr16U):
2307 case uint32_t(ThreadOp::I64AtomicXor16U):
2308 case uint32_t(ThreadOp::I64AtomicXchg16U): {
2309 LinearMemoryAddress<Nothing> addr;
2310 if (!iter.readAtomicRMW(&addr, ValType::I64, 2, &nothing)) {
2311 return false;
2312 }
2313 dumper.dumpLinearMemoryAddress(addr);
2314 break;
2315 }
2316 case uint32_t(ThreadOp::I64AtomicAdd32U):
2317 case uint32_t(ThreadOp::I64AtomicSub32U):
2318 case uint32_t(ThreadOp::I64AtomicAnd32U):
2319 case uint32_t(ThreadOp::I64AtomicOr32U):
2320 case uint32_t(ThreadOp::I64AtomicXor32U):
2321 case uint32_t(ThreadOp::I64AtomicXchg32U): {
2322 LinearMemoryAddress<Nothing> addr;
2323 if (!iter.readAtomicRMW(&addr, ValType::I64, 4, &nothing)) {
2324 return false;
2325 }
2326 dumper.dumpLinearMemoryAddress(addr);
2327 break;
2328 }
2329 case uint32_t(ThreadOp::I32AtomicCmpXchg): {
2330 LinearMemoryAddress<Nothing> addr;
2331 if (!iter.readAtomicCmpXchg(&addr, ValType::I32, 4, &nothing,
2332 &nothing)) {
2333 return false;
2334 }
2335 dumper.dumpLinearMemoryAddress(addr);
2336 break;
2337 }
2338 case uint32_t(ThreadOp::I64AtomicCmpXchg): {
2339 LinearMemoryAddress<Nothing> addr;
2340 if (!iter.readAtomicCmpXchg(&addr, ValType::I64, 8, &nothing,
2341 &nothing)) {
2342 return false;
2343 }
2344 dumper.dumpLinearMemoryAddress(addr);
2345 break;
2346 }
2347 case uint32_t(ThreadOp::I32AtomicCmpXchg8U): {
2348 LinearMemoryAddress<Nothing> addr;
2349 if (!iter.readAtomicCmpXchg(&addr, ValType::I32, 1, &nothing,
2350 &nothing)) {
2351 return false;
2352 }
2353 dumper.dumpLinearMemoryAddress(addr);
2354 break;
2355 }
2356 case uint32_t(ThreadOp::I32AtomicCmpXchg16U): {
2357 LinearMemoryAddress<Nothing> addr;
2358 if (!iter.readAtomicCmpXchg(&addr, ValType::I32, 2, &nothing,
2359 &nothing)) {
2360 return false;
2361 }
2362 dumper.dumpLinearMemoryAddress(addr);
2363 break;
2364 }
2365 case uint32_t(ThreadOp::I64AtomicCmpXchg8U): {
2366 LinearMemoryAddress<Nothing> addr;
2367 if (!iter.readAtomicCmpXchg(&addr, ValType::I64, 1, &nothing,
2368 &nothing)) {
2369 return false;
2370 }
2371 dumper.dumpLinearMemoryAddress(addr);
2372 break;
2373 }
2374 case uint32_t(ThreadOp::I64AtomicCmpXchg16U): {
2375 LinearMemoryAddress<Nothing> addr;
2376 if (!iter.readAtomicCmpXchg(&addr, ValType::I64, 2, &nothing,
2377 &nothing)) {
2378 return false;
2379 }
2380 dumper.dumpLinearMemoryAddress(addr);
2381 break;
2382 }
2383 case uint32_t(ThreadOp::I64AtomicCmpXchg32U): {
2384 LinearMemoryAddress<Nothing> addr;
2385 if (!iter.readAtomicCmpXchg(&addr, ValType::I64, 4, &nothing,
2386 &nothing)) {
2387 return false;
2388 }
2389 dumper.dumpLinearMemoryAddress(addr);
2390 break;
2391 }
2392 default:
2393 return iter.unrecognizedOpcode(&op);
2394 }
2395 break;
2396 }
2397 case uint16_t(Op::MozPrefix):
2398 return iter.unrecognizedOpcode(&op);
2399 default:
2400 return iter.unrecognizedOpcode(&op);
2401 }
2402
2403 dumper.dumpOpEnd();
2404 }
2405
2406 MOZ_CRASH("unreachable");
2407 }
2408
2409 template bool wasm::ValidateOps<NopOpDumper>(ValidatingOpIter& iter,
2410 NopOpDumper& dumper,
2411 const CodeMetadata& codeMeta);
2412 template bool wasm::ValidateOps<OpDumper>(ValidatingOpIter& iter,
2413 OpDumper& dumper,
2414 const CodeMetadata& codeMeta);
2415
2416 bool wasm::ValidateFunctionBody(const CodeMetadata& codeMeta,
2417 uint32_t funcIndex, uint32_t bodySize,
2418 Decoder& d) {
2419 const uint8_t* bodyBegin = d.currentPosition();
2420 const uint8_t* bodyEnd = bodyBegin + bodySize;
2421
2422 ValTypeVector locals;
2423 if (!DecodeLocalEntriesWithParams(d, codeMeta, funcIndex, &locals)) {
2424 return false;
2425 }
2426
2427 ValidatingOpIter iter(codeMeta, d, locals);
2428 NopOpDumper visitor;
2429
2430 if (!iter.startFunction(funcIndex)) {
2431 return false;
2432 }
2433
2434 if (!ValidateOps(iter, visitor, codeMeta)) {
2435 return false;
2436 }
2437
2438 return iter.endFunction(bodyEnd);
2439 }
2440
2441 // Section macros.
2442
2443 static bool DecodePreamble(Decoder& d) {
2444 if (d.bytesRemain() > MaxModuleBytes) {
2445 return d.fail("module too big");
2446 }
2447
2448 uint32_t u32;
2449 if (!d.readFixedU32(&u32) || u32 != MagicNumber) {
2450 return d.fail("failed to match magic number");
2451 }
2452
2453 if (!d.readFixedU32(&u32) || u32 != EncodingVersion) {
2454 return d.failf("binary version 0x%" PRIx32
2455 " does not match expected version 0x%" PRIx32,
2456 u32, EncodingVersion);
2457 }
2458
2459 return true;
2460 }
2461
2462 static bool DecodeValTypeVector(Decoder& d, CodeMetadata* codeMeta,
2463 uint32_t count, ValTypeVector* valTypes) {
2464 if (!valTypes->resize(count)) {
2465 return false;
2466 }
2467
2468 for (uint32_t i = 0; i < count; i++) {
2469 if (!d.readValType(*codeMeta->types, codeMeta->features(),
2470 &(*valTypes)[i])) {
2471 return false;
2472 }
2473 }
2474 return true;
2475 }
2476
2477 static bool DecodeFuncType(Decoder& d, CodeMetadata* codeMeta,
2478 FuncType* funcType) {
2479 uint32_t numArgs;
2480 if (!d.readVarU32(&numArgs)) {
2481 return d.fail("bad number of function args");
2482 }
2483 if (numArgs > MaxParams) {
2484 return d.fail("too many arguments in signature");
2485 }
2486 ValTypeVector args;
2487 if (!DecodeValTypeVector(d, codeMeta, numArgs, &args)) {
2488 return false;
2489 }
2490
2491 uint32_t numResults;
2492 if (!d.readVarU32(&numResults)) {
2493 return d.fail("bad number of function returns");
2494 }
2495 if (numResults > MaxResults) {
2496 return d.fail("too many returns in signature");
2497 }
2498 ValTypeVector results;
2499 if (!DecodeValTypeVector(d, codeMeta, numResults, &results)) {
2500 return false;
2501 }
2502
2503 *funcType = FuncType(std::move(args), std::move(results));
2504 return true;
2505 }
2506
2507 static bool DecodeStructType(Decoder& d, CodeMetadata* codeMeta,
2508 StructType* structType) {
2509 uint32_t numFields;
2510 if (!d.readVarU32(&numFields)) {
2511 return d.fail("Bad number of fields");
2512 }
2513
2514 if (numFields > MaxStructFields) {
2515 return d.fail("too many fields in struct");
2516 }
2517
2518 FieldTypeVector fields;
2519 if (!fields.resize(numFields)) {
2520 return false;
2521 }
2522
2523 for (uint32_t i = 0; i < numFields; i++) {
2524 if (!d.readStorageType(*codeMeta->types, codeMeta->features(),
2525 &fields[i].type)) {
2526 return false;
2527 }
2528
2529 uint8_t flags;
2530 if (!d.readFixedU8(&flags)) {
2531 return d.fail("expected flag");
2532 }
2533 if ((flags & ~uint8_t(FieldFlags::AllowedMask)) != 0) {
2534 return d.fail("garbage flag bits");
2535 }
2536 fields[i].isMutable = flags & uint8_t(FieldFlags::Mutable);
2537 }
2538
2539 *structType = StructType(std::move(fields));
2540
2541 // Compute the struct layout, and fail if the struct is too large
2542 if (!structType->init()) {
2543 return d.fail("too many fields in struct");
2544 }
2545 return true;
2546 }
2547
2548 static bool DecodeArrayType(Decoder& d, CodeMetadata* codeMeta,
2549 ArrayType* arrayType) {
2550 StorageType elementType;
2551 if (!d.readStorageType(*codeMeta->types, codeMeta->features(),
2552 &elementType)) {
2553 return false;
2554 }
2555
2556 uint8_t flags;
2557 if (!d.readFixedU8(&flags)) {
2558 return d.fail("expected flag");
2559 }
2560 if ((flags & ~uint8_t(FieldFlags::AllowedMask)) != 0) {
2561 return d.fail("garbage flag bits");
2562 }
2563 bool isMutable = flags & uint8_t(FieldFlags::Mutable);
2564
2565 *arrayType = ArrayType(elementType, isMutable);
2566 return true;
2567 }
2568
2569 static bool DecodeTypeSection(Decoder& d, CodeMetadata* codeMeta) {
2570 MaybeBytecodeRange range;
2571 if (!d.startSection(SectionId::Type, codeMeta, &range, "type")) {
2572 return false;
2573 }
2574 if (!range) {
2575 return true;
2576 }
2577
2578 uint32_t numRecGroups;
2579 if (!d.readVarU32(&numRecGroups)) {
2580 return d.fail("expected number of types");
2581 }
2582
2583 // Check if we've reached our implementation defined limit of recursion
2584 // groups.
2585 if (numRecGroups > MaxRecGroups) {
2586 return d.fail("too many types");
2587 }
2588
2589 for (uint32_t recGroupIndex = 0; recGroupIndex < numRecGroups;
2590 recGroupIndex++) {
2591 uint32_t recGroupLength = 1;
2592
2593 uint8_t firstTypeCode;
2594 if (!d.peekByte(&firstTypeCode)) {
2595 return d.fail("expected type form");
2596 }
2597
2598 if (firstTypeCode == (uint8_t)TypeCode::RecGroup) {
2599 // Skip over the prefix byte that was peeked.
2600 d.uncheckedReadFixedU8();
2601
2602 // Read the number of types in this recursion group
2603 if (!d.readVarU32(&recGroupLength)) {
2604 return d.fail("expected recursion group length");
2605 }
2606 }
2607
2608 // Check if we've reached our implementation defined limit of type
2609 // definitions.
2610 mozilla::CheckedUint32 newNumTypes(codeMeta->types->length());
2611 newNumTypes += recGroupLength;
2612 if (!newNumTypes.isValid() || newNumTypes.value() > MaxTypes) {
2613 return d.fail("too many types");
2614 }
2615
2616 // Start a recursion group. This will extend the type context with empty
2617 // type definitions to be filled.
2618 MutableRecGroup recGroup = codeMeta->types->startRecGroup(recGroupLength);
2619 if (!recGroup) {
2620 return false;
2621 }
2622
2623 // First, iterate over the types, validate them and set super types.
2624 // Subtyping relationship will be checked in a second iteration.
2625 for (uint32_t recGroupTypeIndex = 0; recGroupTypeIndex < recGroupLength;
2626 recGroupTypeIndex++) {
2627 uint32_t typeIndex =
2628 codeMeta->types->length() - recGroupLength + recGroupTypeIndex;
2629
2630 // This is ensured by above
2631 MOZ_ASSERT(typeIndex < MaxTypes);
2632
2633 uint8_t form;
2634 const TypeDef* superTypeDef = nullptr;
2635
2636 // By default, all types are final unless the sub keyword is specified.
2637 bool finalTypeFlag = true;
2638
2639 // Decode an optional declared super type index.
2640 if (d.peekByte(&form) && (form == (uint8_t)TypeCode::SubNoFinalType ||
2641 form == (uint8_t)TypeCode::SubFinalType)) {
2642 if (form == (uint8_t)TypeCode::SubNoFinalType) {
2643 finalTypeFlag = false;
2644 }
2645
2646 // Skip over the `sub` or `final` prefix byte we peeked.
2647 d.uncheckedReadFixedU8();
2648
2649 // Decode the number of super types, which is currently limited to at
2650 // most one.
2651 uint32_t numSuperTypes;
2652 if (!d.readVarU32(&numSuperTypes)) {
2653 return d.fail("expected number of super types");
2654 }
2655 if (numSuperTypes > 1) {
2656 return d.fail("too many super types");
2657 }
2658
2659 // Decode the super type, if any.
2660 if (numSuperTypes == 1) {
2661 uint32_t superTypeDefIndex;
2662 if (!d.readVarU32(&superTypeDefIndex)) {
2663 return d.fail("expected super type index");
2664 }
2665
2666 // A super type index must be strictly less than the current type
2667 // index in order to avoid cycles.
2668 if (superTypeDefIndex >= typeIndex) {
2669 return d.fail("invalid super type index");
2670 }
2671
2672 superTypeDef = &codeMeta->types->type(superTypeDefIndex);
2673 }
2674 }
2675
2676 // Decode the kind of type definition
2677 if (!d.readFixedU8(&form)) {
2678 return d.fail("expected type form");
2679 }
2680
2681 TypeDef* typeDef = &recGroup->type(recGroupTypeIndex);
2682 switch (form) {
2683 case uint8_t(TypeCode::Func): {
2684 FuncType funcType;
2685 if (!DecodeFuncType(d, codeMeta, &funcType)) {
2686 return false;
2687 }
2688 *typeDef = std::move(funcType);
2689 break;
2690 }
2691 case uint8_t(TypeCode::Struct): {
2692 StructType structType;
2693 if (!DecodeStructType(d, codeMeta, &structType)) {
2694 return false;
2695 }
2696 *typeDef = std::move(structType);
2697 break;
2698 }
2699 case uint8_t(TypeCode::Array): {
2700 ArrayType arrayType;
2701 if (!DecodeArrayType(d, codeMeta, &arrayType)) {
2702 return false;
2703 }
2704 *typeDef = std::move(arrayType);
2705 break;
2706 }
2707 default:
2708 return d.fail("expected type form");
2709 }
2710
2711 typeDef->setFinal(finalTypeFlag);
2712 if (superTypeDef) {
2713 // Check that we aren't creating too deep of a subtyping chain
2714 if (superTypeDef->subTypingDepth() >= MaxSubTypingDepth) {
2715 return d.fail("type is too deep");
2716 }
2717
2718 typeDef->setSuperTypeDef(superTypeDef);
2719 }
2720
2721 if (typeDef->isFuncType()) {
2722 typeDef->funcType().initImmediateTypeId(typeDef->isFinal(),
2723 superTypeDef, recGroupLength);
2724 }
2725 }
2726
2727 // Check the super types to make sure they are compatible with their
2728 // subtypes. This is done in a second iteration to avoid dealing with not
2729 // yet loaded types.
2730 for (uint32_t recGroupTypeIndex = 0; recGroupTypeIndex < recGroupLength;
2731 recGroupTypeIndex++) {
2732 TypeDef* typeDef = &recGroup->type(recGroupTypeIndex);
2733 if (typeDef->superTypeDef()) {
2734 // Check that the super type is compatible with this type
2735 if (!TypeDef::canBeSubTypeOf(typeDef, typeDef->superTypeDef())) {
2736 return d.fail("incompatible super type");
2737 }
2738 }
2739 }
2740
2741 // Finish the recursion group, which will canonicalize the types.
2742 if (!codeMeta->types->endRecGroup()) {
2743 return false;
2744 }
2745 }
2746
2747 return d.finishSection(*range, "type");
2748 }
2749
2750 [[nodiscard]] static bool DecodeName(Decoder& d, CacheableName* name) {
2751 uint32_t numBytes;
2752 if (!d.readVarU32(&numBytes)) {
2753 return false;
2754 }
2755
2756 if (numBytes > MaxStringBytes) {
2757 return false;
2758 }
2759
2760 const uint8_t* bytes;
2761 if (!d.readBytes(numBytes, &bytes)) {
2762 return false;
2763 }
2764
2765 if (!IsUtf8(AsChars(Span(bytes, numBytes)))) {
2766 return false;
2767 }
2768
2769 UTF8Bytes utf8Bytes;
2770 if (!utf8Bytes.resizeUninitialized(numBytes)) {
2771 return false;
2772 }
2773 memcpy(utf8Bytes.begin(), bytes, numBytes);
2774
2775 *name = CacheableName(std::move(utf8Bytes));
2776 return true;
2777 }
2778
2779 static bool DecodeFuncTypeIndex(Decoder& d, const SharedTypeContext& types,
2780 uint32_t* funcTypeIndex) {
2781 if (!d.readVarU32(funcTypeIndex)) {
2782 return d.fail("expected signature index");
2783 }
2784
2785 if (*funcTypeIndex >= types->length()) {
2786 return d.fail("signature index out of range");
2787 }
2788
2789 const TypeDef& def = (*types)[*funcTypeIndex];
2790
2791 if (!def.isFuncType()) {
2792 return d.fail("signature index references non-signature");
2793 }
2794
2795 return true;
2796 }
2797
2798 static bool DecodeLimitBound(Decoder& d, AddressType addressType,
2799 uint64_t* bound) {
2800 if (addressType == AddressType::I64) {
2801 return d.readVarU64(bound);
2802 }
2803
2804 // Spec tests assert that we only decode a LEB32 when address type is I32.
2805 uint32_t bound32;
2806 if (!d.readVarU32(&bound32)) {
2807 return false;
2808 }
2809 *bound = bound32;
2810 return true;
2811 }
2812
2813 static bool DecodeLimits(Decoder& d, LimitsKind kind, Limits* limits) {
2814 uint8_t flags;
2815 if (!d.readFixedU8(&flags)) {
2816 return d.fail("expected flags");
2817 }
2818
2819 uint8_t mask = kind == LimitsKind::Memory ? uint8_t(LimitsMask::Memory)
2820 : uint8_t(LimitsMask::Table);
2821
2822 if (flags & ~uint8_t(mask)) {
2823 return d.failf("unexpected bits set in flags: %" PRIu32,
2824 uint32_t(flags & ~uint8_t(mask)));
2825 }
2826
2827 // Memory limits may be shared
2828 if (kind == LimitsKind::Memory) {
2829 if ((flags & uint8_t(LimitsFlags::IsShared)) &&
2830 !(flags & uint8_t(LimitsFlags::HasMaximum))) {
2831 return d.fail("maximum length required for shared memory");
2832 }
2833
2834 limits->shared = (flags & uint8_t(LimitsFlags::IsShared))
2835 ? Shareable::True
2836 : Shareable::False;
2837 } else {
2838 limits->shared = Shareable::False;
2839 }
2840
2841 #ifdef ENABLE_WASM_MEMORY64
2842 limits->addressType = (flags & uint8_t(LimitsFlags::IsI64))
2843 ? AddressType::I64
2844 : AddressType::I32;
2845 #else
2846 limits->addressType = AddressType::I32;
2847 if (flags & uint8_t(LimitsFlags::IsI64)) {
2848 return d.fail("i64 is not supported for memory or table limits");
2849 }
2850 #endif
2851
2852 uint64_t initial;
2853 if (!DecodeLimitBound(d, limits->addressType, &initial)) {
2854 return d.fail("expected initial length");
2855 }
2856 limits->initial = initial;
2857
2858 if (flags & uint8_t(LimitsFlags::HasMaximum)) {
2859 uint64_t maximum;
2860 if (!DecodeLimitBound(d, limits->addressType, &maximum)) {
2861 return d.fail("expected maximum length");
2862 }
2863
2864 if (limits->initial > maximum) {
2865 return d.failf(
2866 "%s size minimum must not be greater than maximum; "
2867 "maximum length %" PRIu64 " is less than initial length %" PRIu64,
2868 kind == LimitsKind::Memory ? "memory" : "table", maximum,
2869 limits->initial);
2870 }
2871
2872 limits->maximum.emplace(maximum);
2873 }
2874
2875 return true;
2876 }
2877
2878 // Combined decoding for both table types and the augmented form of table types
2879 // that can include init expressions:
2880 //
2881 // https://wasm-dsl.github.io/spectec/core/binary/types.html#table-types
2882 // https://wasm-dsl.github.io/spectec/core/binary/modules.html#table-section
2883 //
2884 // Only defined tables are therefore allowed to have init expressions, not
2885 // imported tables.
2886 static bool DecodeTableType(Decoder& d, CodeMetadata* codeMeta, bool isImport) {
2887 bool initExprPresent = false;
2888 uint8_t typeCode;
2889 if (!d.peekByte(&typeCode)) {
2890 return d.fail("expected type code");
2891 }
2892 if (typeCode == (uint8_t)TypeCode::TableHasInitExpr) {
2893 if (isImport) {
2894 return d.fail("imported tables cannot have initializer expressions");
2895 }
2896 d.uncheckedReadFixedU8();
2897 uint8_t flags;
2898 if (!d.readFixedU8(&flags) || flags != 0) {
2899 return d.fail("expected reserved byte to be 0");
2900 }
2901 initExprPresent = true;
2902 }
2903
2904 RefType tableElemType;
2905 if (!d.readRefType(*codeMeta->types, codeMeta->features(), &tableElemType)) {
2906 return false;
2907 }
2908
2909 Limits limits;
2910 if (!DecodeLimits(d, LimitsKind::Table, &limits)) {
2911 return false;
2912 }
2913
2914 if (limits.addressType == AddressType::I64 && !codeMeta->memory64Enabled()) {
2915 return d.fail("memory64 is disabled");
2916 }
2917
2918 // If there's a maximum, check it is in range. The check to exclude
2919 // initial > maximum is carried out by the DecodeLimits call above, so
2920 // we don't repeat it here.
2921 if (limits.initial > MaxTableElemsValidation(limits.addressType) ||
2922 ((limits.maximum.isSome() &&
2923 limits.maximum.value() >
2924 MaxTableElemsValidation(limits.addressType)))) {
2925 return d.fail("too many table elements");
2926 }
2927
2928 if (codeMeta->tables.length() >= MaxTables) {
2929 return d.fail("too many tables");
2930 }
2931
2932 Maybe<InitExpr> initExpr;
2933 if (initExprPresent) {
2934 InitExpr initializer;
2935 if (!InitExpr::decodeAndValidate(d, codeMeta, tableElemType,
2936 &initializer)) {
2937 return false;
2938 }
2939 initExpr = Some(std::move(initializer));
2940 } else {
2941 if (!tableElemType.isNullable() && !isImport) {
2942 return d.fail("table with non-nullable references requires initializer");
2943 }
2944 }
2945
2946 return codeMeta->tables.emplaceBack(limits, tableElemType,
2947 std::move(initExpr),
2948 /* isAsmJS */ false, isImport);
2949 }
2950
2951 static bool DecodeGlobalType(Decoder& d, const SharedTypeContext& types,
2952 const FeatureArgs& features, ValType* type,
2953 bool* isMutable) {
2954 if (!d.readValType(*types, features, type)) {
2955 return d.fail("expected global type");
2956 }
2957
2958 uint8_t flags;
2959 if (!d.readFixedU8(&flags)) {
2960 return d.fail("expected global flags");
2961 }
2962
2963 if (flags & ~uint8_t(GlobalTypeImmediate::AllowedMask)) {
2964 return d.fail("unexpected bits set in global flags");
2965 }
2966
2967 *isMutable = flags & uint8_t(GlobalTypeImmediate::IsMutable);
2968 return true;
2969 }
2970
2971 static bool DecodeMemoryTypeAndLimits(Decoder& d, CodeMetadata* codeMeta,
2972 MemoryDescVector* memories) {
2973 if (!codeMeta->features().multiMemory && codeMeta->numMemories() == 1) {
2974 return d.fail("already have default memory");
2975 }
2976
2977 if (codeMeta->numMemories() >= MaxMemories) {
2978 return d.fail("too many memories");
2979 }
2980
2981 Limits limits;
2982 if (!DecodeLimits(d, LimitsKind::Memory, &limits)) {
2983 return false;
2984 }
2985
2986 uint64_t maxField = MaxMemoryPagesValidation(limits.addressType);
2987
2988 if (limits.initial > maxField) {
2989 return d.fail("initial memory size too big");
2990 }
2991
2992 if (limits.maximum && *limits.maximum > maxField) {
2993 return d.fail("maximum memory size too big");
2994 }
2995
2996 if (limits.shared == Shareable::True &&
2997 codeMeta->sharedMemoryEnabled() == Shareable::False) {
2998 return d.fail("shared memory is disabled");
2999 }
3000
3001 if (limits.addressType == AddressType::I64 && !codeMeta->memory64Enabled()) {
3002 return d.fail("memory64 is disabled");
3003 }
3004
3005 return memories->emplaceBack(MemoryDesc(limits));
3006 }
3007
3008 static bool DecodeTag(Decoder& d, CodeMetadata* codeMeta, TagKind* tagKind,
3009 uint32_t* funcTypeIndex) {
3010 uint32_t tagCode;
3011 if (!d.readVarU32(&tagCode)) {
3012 return d.fail("expected tag kind");
3013 }
3014
3015 if (TagKind(tagCode) != TagKind::Exception) {
3016 return d.fail("illegal tag kind");
3017 }
3018 *tagKind = TagKind(tagCode);
3019
3020 if (!d.readVarU32(funcTypeIndex)) {
3021 return d.fail("expected function index in tag");
3022 }
3023 if (*funcTypeIndex >= codeMeta->numTypes()) {
3024 return d.fail("function type index in tag out of bounds");
3025 }
3026 if (!(*codeMeta->types)[*funcTypeIndex].isFuncType()) {
3027 return d.fail("function type index must index a function type");
3028 }
3029 if ((*codeMeta->types)[*funcTypeIndex].funcType().results().length() != 0) {
3030 return d.fail("tag function types must not return anything");
3031 }
3032 return true;
3033 }
3034
3035 static bool DecodeImport(Decoder& d, CodeMetadata* codeMeta,
3036 ModuleMetadata* moduleMeta) {
3037 CacheableName moduleName;
3038 if (!DecodeName(d, &moduleName)) {
3039 return d.fail("expected valid import module name");
3040 }
3041
3042 CacheableName fieldName;
3043 if (!DecodeName(d, &fieldName)) {
3044 return d.fail("expected valid import field name");
3045 }
3046
3047 uint8_t rawImportKind;
3048 if (!d.readFixedU8(&rawImportKind)) {
3049 return d.fail("failed to read import kind");
3050 }
3051
3052 DefinitionKind importKind = DefinitionKind(rawImportKind);
3053
3054 switch (importKind) {
3055 case DefinitionKind::Function: {
3056 uint32_t funcTypeIndex;
3057 if (!DecodeFuncTypeIndex(d, codeMeta->types, &funcTypeIndex)) {
3058 return false;
3059 }
3060 if (!codeMeta->funcs.append(FuncDesc(funcTypeIndex))) {
3061 return false;
3062 }
3063 if (codeMeta->funcs.length() > MaxFuncs) {
3064 return d.fail("too many functions");
3065 }
3066 break;
3067 }
3068 case DefinitionKind::Table: {
3069 if (!DecodeTableType(d, codeMeta, /*isImport=*/true)) {
3070 return false;
3071 }
3072 break;
3073 }
3074 case DefinitionKind::Memory: {
3075 if (!DecodeMemoryTypeAndLimits(d, codeMeta, &codeMeta->memories)) {
3076 return false;
3077 }
3078 break;
3079 }
3080 case DefinitionKind::Global: {
3081 ValType type;
3082 bool isMutable;
3083 if (!DecodeGlobalType(d, codeMeta->types, codeMeta->features(), &type,
3084 &isMutable)) {
3085 return false;
3086 }
3087 if (!codeMeta->globals.append(
3088 GlobalDesc(type, isMutable, codeMeta->globals.length()))) {
3089 return false;
3090 }
3091 if (codeMeta->globals.length() > MaxGlobals) {
3092 return d.fail("too many globals");
3093 }
3094 break;
3095 }
3096 case DefinitionKind::Tag: {
3097 TagKind tagKind;
3098 uint32_t funcTypeIndex;
3099 if (!DecodeTag(d, codeMeta, &tagKind, &funcTypeIndex)) {
3100 return false;
3101 }
3102 MutableTagType tagType = js_new<TagType>();
3103 if (!tagType ||
3104 !tagType->initialize(&(*codeMeta->types)[funcTypeIndex])) {
3105 return false;
3106 }
3107 if (!codeMeta->tags.emplaceBack(tagKind, tagType)) {
3108 return false;
3109 }
3110 if (codeMeta->tags.length() > MaxTags) {
3111 return d.fail("too many tags");
3112 }
3113 break;
3114 }
3115 default:
3116 return d.fail("unsupported import kind");
3117 }
3118
3119 return moduleMeta->imports.emplaceBack(std::move(moduleName),
3120 std::move(fieldName), importKind);
3121 }
3122
3123 static bool CheckImportsAgainstBuiltinModules(Decoder& d,
3124 CodeMetadata* codeMeta,
3125 ModuleMetadata* moduleMeta) {
3126 const BuiltinModuleIds& builtinModules = codeMeta->features().builtinModules;
3127
3128 // Skip this pass if there are no builtin modules enabled
3129 if (builtinModules.hasNone()) {
3130 return true;
3131 }
3132
3133 uint32_t importFuncIndex = 0;
3134 uint32_t importGlobalIndex = 0;
3135 for (auto& import : moduleMeta->imports) {
3136 Maybe<BuiltinModuleId> builtinModule =
3137 ImportMatchesBuiltinModule(import.module.utf8Bytes(), builtinModules);
3138
3139 switch (import.kind) {
3140 case DefinitionKind::Function: {
3141 const FuncDesc& func = codeMeta->funcs[importFuncIndex];
3142 uint32_t funcIndex = importFuncIndex;
3143 importFuncIndex += 1;
3144
3145 // Skip this import if it doesn't refer to a builtin module. We do have
3146 // to increment the import function index regardless though.
3147 if (!builtinModule) {
3148 continue;
3149 }
3150
3151 // Check if this import refers to a builtin module function
3152 const BuiltinModuleFunc* builtinFunc = nullptr;
3153 BuiltinModuleFuncId builtinFuncId;
3154 if (!ImportMatchesBuiltinModuleFunc(import.field.utf8Bytes(),
3155 *builtinModule, &builtinFunc,
3156 &builtinFuncId)) {
3157 return d.fail("unrecognized builtin module field");
3158 }
3159
3160 const TypeDef& importTypeDef = (*codeMeta->types)[func.typeIndex];
3161 if (!TypeDef::isSubTypeOf(builtinFunc->typeDef(), &importTypeDef)) {
3162 return d.failf("type mismatch in %s", builtinFunc->exportName());
3163 }
3164
3165 codeMeta->knownFuncImports[funcIndex] = builtinFuncId;
3166 break;
3167 }
3168 case DefinitionKind::Global: {
3169 const GlobalDesc& global = codeMeta->globals[importGlobalIndex];
3170 importGlobalIndex += 1;
3171
3172 // Skip this import if it doesn't refer to a builtin module. We do have
3173 // to increment the import global index regardless though.
3174 if (!builtinModule) {
3175 continue;
3176 }
3177
3178 // Only the imported string constants module has globals defined.
3179 if (*builtinModule != BuiltinModuleId::JSStringConstants) {
3180 return d.fail("unrecognized builtin module field");
3181 }
3182
3183 // All imported globals must match a provided global type of
3184 // `(global (ref extern))`.
3185 if (global.isMutable() ||
3186 !ValType::isSubTypeOf(ValType(RefType::extern_().asNonNullable()),
3187 global.type())) {
3188 return d.failf("type mismatch");
3189 }
3190
3191 break;
3192 }
3193 default: {
3194 if (!builtinModule) {
3195 continue;
3196 }
3197 return d.fail("unrecognized builtin import");
3198 }
3199 }
3200 }
3201
3202 return true;
3203 }
3204
3205 // A standalone validation pass that occurs after we have finished decoding all
3206 // memories and therefore can determine if any imported builtin functions are
3207 // invalid due to lack of memory.
3208 static bool CheckBuiltinImportsHaveMemory(Decoder& d, CodeMetadata* codeMeta) {
3209 // Skip this pass if there are no builtin modules enabled.
3210 if (codeMeta->features().builtinModules.hasNone()) {
3211 #ifdef DEBUG
3212 for (BuiltinModuleFuncId& id : codeMeta->knownFuncImports) {
3213 MOZ_ASSERT(id == BuiltinModuleFuncId::None);
3214 }
3215 #endif
3216 return true;
3217 }
3218
3219 for (size_t i = 0; i < codeMeta->knownFuncImports.length(); i++) {
3220 BuiltinModuleFuncId builtinFuncId = codeMeta->knownFuncImports[i];
3221 if (builtinFuncId == BuiltinModuleFuncId::None) {
3222 continue;
3223 }
3224
3225 const BuiltinModuleFunc& builtinModuleFunc =
3226 BuiltinModuleFuncs::getFromId(builtinFuncId);
3227 if (builtinModuleFunc.usesMemory()) {
3228 if (codeMeta->memories.length() == 0) {
3229 return d.failf("func %zu is a builtin function that requires a memory",
3230 i);
3231 }
3232
3233 // NOTE(bvisness): As of today, no builtins use shared memory. If this
3234 // changes in the future, you will need to update this to pick up the
3235 // expected shared-ness from the associated builtin module. Unfortunately
3236 // this is currently defined exclusively by which BuiltinMemory we
3237 // construct in CompileBuiltinModule in WasmBuiltinModule.cpp, and there
3238 // is no straightforward way to map from a BuiltinModuleFuncId to a
3239 // BuiltinModuleId, much less to know what kind of memory it expects. It
3240 // would be nice to have some kind of BuiltinModuleDesc that holistically
3241 // describes what kind of module to construct and what functions it
3242 // contains, but we are not building this today :)
3243 if (codeMeta->memories[0].isShared()) {
3244 return d.fail("builtin funcs are not compatible with shared memories");
3245 }
3246 }
3247 }
3248
3249 return true;
3250 }
3251
3252 static bool DecodeImportSection(Decoder& d, CodeMetadata* codeMeta,
3253 ModuleMetadata* moduleMeta) {
3254 MaybeBytecodeRange range;
3255 if (!d.startSection(SectionId::Import, codeMeta, &range, "import")) {
3256 return false;
3257 }
3258 if (!range) {
3259 return true;
3260 }
3261
3262 uint32_t numImports;
3263 if (!d.readVarU32(&numImports)) {
3264 return d.fail("failed to read number of imports");
3265 }
3266
3267 if (numImports > MaxImports) {
3268 return d.fail("too many imports");
3269 }
3270
3271 for (uint32_t i = 0; i < numImports; i++) {
3272 if (!DecodeImport(d, codeMeta, moduleMeta)) {
3273 return false;
3274 }
3275 }
3276
3277 if (!d.finishSection(*range, "import")) {
3278 return false;
3279 }
3280
3281 codeMeta->numFuncImports = codeMeta->funcs.length();
3282 if (!codeMeta->knownFuncImports.resize(codeMeta->numFuncImports)) {
3283 return false;
3284 }
3285 codeMeta->numGlobalImports = codeMeta->globals.length();
3286 return true;
3287 }
3288
3289 static bool DecodeFunctionSection(Decoder& d, CodeMetadata* codeMeta) {
3290 MaybeBytecodeRange range;
3291 if (!d.startSection(SectionId::Function, codeMeta, &range, "function")) {
3292 return false;
3293 }
3294 if (!range) {
3295 return true;
3296 }
3297
3298 uint32_t numDefs;
3299 if (!d.readVarU32(&numDefs)) {
3300 return d.fail("expected number of function definitions");
3301 }
3302
3303 CheckedInt<uint32_t> numFuncs = codeMeta->funcs.length();
3304 numFuncs += numDefs;
3305 if (!numFuncs.isValid() || numFuncs.value() > MaxFuncs) {
3306 return d.fail("too many functions");
3307 }
3308
3309 if (!codeMeta->funcs.reserve(numFuncs.value())) {
3310 return false;
3311 }
3312
3313 for (uint32_t i = 0; i < numDefs; i++) {
3314 uint32_t funcTypeIndex;
3315 if (!DecodeFuncTypeIndex(d, codeMeta->types, &funcTypeIndex)) {
3316 return false;
3317 }
3318 codeMeta->funcs.infallibleAppend(funcTypeIndex);
3319 }
3320
3321 return d.finishSection(*range, "function");
3322 }
3323
3324 static bool DecodeTableSection(Decoder& d, CodeMetadata* codeMeta) {
3325 MaybeBytecodeRange range;
3326 if (!d.startSection(SectionId::Table, codeMeta, &range, "table")) {
3327 return false;
3328 }
3329 if (!range) {
3330 return true;
3331 }
3332
3333 uint32_t numTables;
3334 if (!d.readVarU32(&numTables)) {
3335 return d.fail("failed to read number of tables");
3336 }
3337
3338 for (uint32_t i = 0; i < numTables; ++i) {
3339 if (!DecodeTableType(d, codeMeta, /*isImport=*/false)) {
3340 return false;
3341 }
3342 }
3343
3344 return d.finishSection(*range, "table");
3345 }
3346
3347 static bool DecodeMemorySection(Decoder& d, CodeMetadata* codeMeta) {
3348 MaybeBytecodeRange range;
3349 if (!d.startSection(SectionId::Memory, codeMeta, &range, "memory")) {
3350 return false;
3351 }
3352 if (!range) {
3353 return true;
3354 }
3355
3356 uint32_t numMemories;
3357 if (!d.readVarU32(&numMemories)) {
3358 return d.fail("failed to read number of memories");
3359 }
3360
3361 if (!codeMeta->features().multiMemory && numMemories > 1) {
3362 return d.fail("the number of memories must be at most one");
3363 }
3364
3365 for (uint32_t i = 0; i < numMemories; ++i) {
3366 if (!DecodeMemoryTypeAndLimits(d, codeMeta, &codeMeta->memories)) {
3367 return false;
3368 }
3369 }
3370
3371 return d.finishSection(*range, "memory");
3372 }
3373
3374 static bool DecodeGlobalSection(Decoder& d, CodeMetadata* codeMeta) {
3375 MaybeBytecodeRange range;
3376 if (!d.startSection(SectionId::Global, codeMeta, &range, "global")) {
3377 return false;
3378 }
3379 if (!range) {
3380 return true;
3381 }
3382
3383 uint32_t numDefs;
3384 if (!d.readVarU32(&numDefs)) {
3385 return d.fail("expected number of globals");
3386 }
3387
3388 CheckedInt<uint32_t> numGlobals = codeMeta->globals.length();
3389 numGlobals += numDefs;
3390 if (!numGlobals.isValid() || numGlobals.value() > MaxGlobals) {
3391 return d.fail("too many globals");
3392 }
3393
3394 if (!codeMeta->globals.reserve(numGlobals.value())) {
3395 return false;
3396 }
3397
3398 for (uint32_t i = 0; i < numDefs; i++) {
3399 ValType type;
3400 bool isMutable;
3401 if (!DecodeGlobalType(d, codeMeta->types, codeMeta->features(), &type,
3402 &isMutable)) {
3403 return false;
3404 }
3405
3406 InitExpr initializer;
3407 if (!InitExpr::decodeAndValidate(d, codeMeta, type, &initializer)) {
3408 return false;
3409 }
3410
3411 codeMeta->globals.infallibleAppend(
3412 GlobalDesc(std::move(initializer), isMutable));
3413 }
3414
3415 return d.finishSection(*range, "global");
3416 }
3417
3418 static bool DecodeTagSection(Decoder& d, CodeMetadata* codeMeta) {
3419 MaybeBytecodeRange range;
3420 if (!d.startSection(SectionId::Tag, codeMeta, &range, "tag")) {
3421 return false;
3422 }
3423 if (!range) {
3424 return true;
3425 }
3426
3427 uint32_t numDefs;
3428 if (!d.readVarU32(&numDefs)) {
3429 return d.fail("expected number of tags");
3430 }
3431
3432 CheckedInt<uint32_t> numTags = codeMeta->tags.length();
3433 numTags += numDefs;
3434 if (!numTags.isValid() || numTags.value() > MaxTags) {
3435 return d.fail("too many tags");
3436 }
3437
3438 if (!codeMeta->tags.reserve(numTags.value())) {
3439 return false;
3440 }
3441
3442 for (uint32_t i = 0; i < numDefs; i++) {
3443 TagKind tagKind;
3444 uint32_t funcTypeIndex;
3445 if (!DecodeTag(d, codeMeta, &tagKind, &funcTypeIndex)) {
3446 return false;
3447 }
3448 MutableTagType tagType = js_new<TagType>();
3449 if (!tagType || !tagType->initialize(&(*codeMeta->types)[funcTypeIndex])) {
3450 return false;
3451 }
3452 codeMeta->tags.infallibleEmplaceBack(tagKind, tagType);
3453 }
3454
3455 return d.finishSection(*range, "tag");
3456 }
3457
3458 using NameSet = HashSet<Span<char>, NameHasher, SystemAllocPolicy>;
3459
3460 [[nodiscard]] static bool DecodeExportName(Decoder& d, NameSet* dupSet,
3461 CacheableName* exportName) {
3462 if (!DecodeName(d, exportName)) {
3463 d.fail("expected valid export name");
3464 return false;
3465 }
3466
3467 NameSet::AddPtr p = dupSet->lookupForAdd(exportName->utf8Bytes());
3468 if (p) {
3469 d.fail("duplicate export");
3470 return false;
3471 }
3472
3473 return dupSet->add(p, exportName->utf8Bytes());
3474 }
3475
3476 static bool DecodeExport(Decoder& d, CodeMetadata* codeMeta,
3477 ModuleMetadata* moduleMeta, NameSet* dupSet) {
3478 CacheableName fieldName;
3479 if (!DecodeExportName(d, dupSet, &fieldName)) {
3480 return false;
3481 }
3482
3483 uint8_t exportKind;
3484 if (!d.readFixedU8(&exportKind)) {
3485 return d.fail("failed to read export kind");
3486 }
3487
3488 switch (DefinitionKind(exportKind)) {
3489 case DefinitionKind::Function: {
3490 uint32_t funcIndex;
3491 if (!d.readVarU32(&funcIndex)) {
3492 return d.fail("expected function index");
3493 }
3494
3495 if (funcIndex >= codeMeta->numFuncs()) {
3496 return d.fail("exported function index out of bounds");
3497 }
3498
3499 codeMeta->funcs[funcIndex].declareFuncExported(/* eager */ true,
3500 /* canRefFunc */ true);
3501 return moduleMeta->exports.emplaceBack(std::move(fieldName), funcIndex,
3502 DefinitionKind::Function);
3503 }
3504 case DefinitionKind::Table: {
3505 uint32_t tableIndex;
3506 if (!d.readVarU32(&tableIndex)) {
3507 return d.fail("expected table index");
3508 }
3509
3510 if (tableIndex >= codeMeta->tables.length()) {
3511 return d.fail("exported table index out of bounds");
3512 }
3513 codeMeta->tables[tableIndex].isExported = true;
3514 return moduleMeta->exports.emplaceBack(std::move(fieldName), tableIndex,
3515 DefinitionKind::Table);
3516 }
3517 case DefinitionKind::Memory: {
3518 uint32_t memoryIndex;
3519 if (!d.readVarU32(&memoryIndex)) {
3520 return d.fail("expected memory index");
3521 }
3522
3523 if (memoryIndex >= codeMeta->numMemories()) {
3524 return d.fail("exported memory index out of bounds");
3525 }
3526
3527 return moduleMeta->exports.emplaceBack(std::move(fieldName), memoryIndex,
3528 DefinitionKind::Memory);
3529 }
3530 case DefinitionKind::Global: {
3531 uint32_t globalIndex;
3532 if (!d.readVarU32(&globalIndex)) {
3533 return d.fail("expected global index");
3534 }
3535
3536 if (globalIndex >= codeMeta->globals.length()) {
3537 return d.fail("exported global index out of bounds");
3538 }
3539
3540 GlobalDesc* global = &codeMeta->globals[globalIndex];
3541 global->setIsExport();
3542
3543 return moduleMeta->exports.emplaceBack(std::move(fieldName), globalIndex,
3544 DefinitionKind::Global);
3545 }
3546 case DefinitionKind::Tag: {
3547 uint32_t tagIndex;
3548 if (!d.readVarU32(&tagIndex)) {
3549 return d.fail("expected tag index");
3550 }
3551 if (tagIndex >= codeMeta->tags.length()) {
3552 return d.fail("exported tag index out of bounds");
3553 }
3554
3555 codeMeta->tags[tagIndex].isExport = true;
3556 return moduleMeta->exports.emplaceBack(std::move(fieldName), tagIndex,
3557 DefinitionKind::Tag);
3558 }
3559 default:
3560 return d.fail("unexpected export kind");
3561 }
3562
3563 MOZ_CRASH("unreachable");
3564 }
3565
3566 static bool DecodeExportSection(Decoder& d, CodeMetadata* codeMeta,
3567 ModuleMetadata* moduleMeta) {
3568 MaybeBytecodeRange range;
3569 if (!d.startSection(SectionId::Export, codeMeta, &range, "export")) {
3570 return false;
3571 }
3572 if (!range) {
3573 return true;
3574 }
3575
3576 NameSet dupSet;
3577
3578 uint32_t numExports;
3579 if (!d.readVarU32(&numExports)) {
3580 return d.fail("failed to read number of exports");
3581 }
3582
3583 if (numExports > MaxExports) {
3584 return d.fail("too many exports");
3585 }
3586
3587 for (uint32_t i = 0; i < numExports; i++) {
3588 if (!DecodeExport(d, codeMeta, moduleMeta, &dupSet)) {
3589 return false;
3590 }
3591 }
3592
3593 return d.finishSection(*range, "export");
3594 }
3595
3596 static bool DecodeStartSection(Decoder& d, CodeMetadata* codeMeta,
3597 ModuleMetadata* moduleMeta) {
3598 MaybeBytecodeRange range;
3599 if (!d.startSection(SectionId::Start, codeMeta, &range, "start")) {
3600 return false;
3601 }
3602 if (!range) {
3603 return true;
3604 }
3605
3606 uint32_t funcIndex;
3607 if (!d.readVarU32(&funcIndex)) {
3608 return d.fail("failed to read start func index");
3609 }
3610
3611 if (funcIndex >= codeMeta->numFuncs()) {
3612 return d.fail("unknown start function");
3613 }
3614
3615 const FuncType& funcType = codeMeta->getFuncType(funcIndex);
3616 if (funcType.results().length() > 0) {
3617 return d.fail("start function must not return anything");
3618 }
3619
3620 if (funcType.args().length()) {
3621 return d.fail("start function must be nullary");
3622 }
3623
3624 codeMeta->funcs[funcIndex].declareFuncExported(/* eager */ true,
3625 /* canFuncRef */ false);
3626 codeMeta->startFuncIndex = Some(funcIndex);
3627
3628 return d.finishSection(*range, "start");
3629 }
3630
3631 static inline ModuleElemSegment::Kind NormalizeElemSegmentKind(
3632 ElemSegmentKind decodedKind) {
3633 switch (decodedKind) {
3634 case ElemSegmentKind::Active:
3635 case ElemSegmentKind::ActiveWithTableIndex: {
3636 return ModuleElemSegment::Kind::Active;
3637 }
3638 case ElemSegmentKind::Passive: {
3639 return ModuleElemSegment::Kind::Passive;
3640 }
3641 case ElemSegmentKind::Declared: {
3642 return ModuleElemSegment::Kind::Declared;
3643 }
3644 }
3645 MOZ_CRASH("unexpected elem segment kind");
3646 }
3647
3648 static bool DecodeElemSegment(Decoder& d, CodeMetadata* codeMeta,
3649 ModuleMetadata* moduleMeta) {
3650 uint32_t segmentFlags;
3651 if (!d.readVarU32(&segmentFlags)) {
3652 return d.fail("expected elem segment flags field");
3653 }
3654
3655 Maybe<ElemSegmentFlags> flags = ElemSegmentFlags::construct(segmentFlags);
3656 if (!flags) {
3657 return d.fail("invalid elem segment flags field");
3658 }
3659
3660 ModuleElemSegment seg = ModuleElemSegment();
3661
3662 ElemSegmentKind segmentKind = flags->kind();
3663 seg.kind = NormalizeElemSegmentKind(segmentKind);
3664
3665 if (segmentKind == ElemSegmentKind::Active ||
3666 segmentKind == ElemSegmentKind::ActiveWithTableIndex) {
3667 if (codeMeta->tables.length() == 0) {
3668 return d.fail("active elem segment requires a table");
3669 }
3670
3671 uint32_t tableIndex = 0;
3672 if (segmentKind == ElemSegmentKind::ActiveWithTableIndex &&
3673 !d.readVarU32(&tableIndex)) {
3674 return d.fail("expected table index");
3675 }
3676 if (tableIndex >= codeMeta->tables.length()) {
3677 return d.fail("table index out of range for element segment");
3678 }
3679 seg.tableIndex = tableIndex;
3680
3681 InitExpr offset;
3682 if (!InitExpr::decodeAndValidate(
3683 d, codeMeta, ToValType(codeMeta->tables[tableIndex].addressType()),
3684 &offset)) {
3685 return false;
3686 }
3687 seg.offsetIfActive.emplace(std::move(offset));
3688 } else {
3689 // Too many bugs result from keeping this value zero. For passive
3690 // or declared segments, there really is no table index, and we should
3691 // never touch the field.
3692 MOZ_ASSERT(segmentKind == ElemSegmentKind::Passive ||
3693 segmentKind == ElemSegmentKind::Declared);
3694 seg.tableIndex = (uint32_t)-1;
3695 }
3696
3697 ElemSegmentPayload payload = flags->payload();
3698 RefType elemType;
3699
3700 // `ActiveWithTableIndex`, `Declared`, and `Passive` element segments encode
3701 // the type or definition kind of the payload. `Active` element segments are
3702 // restricted to MVP behavior, which assumes only function indices.
3703 if (segmentKind == ElemSegmentKind::Active) {
3704 // Bizarrely, the spec prescribes that the default type is (ref func) when
3705 // encoding function indices, and (ref null func) when encoding expressions.
3706 elemType = payload == ElemSegmentPayload::Expressions
3707 ? RefType::func()
3708 : RefType::func().asNonNullable();
3709 } else {
3710 switch (payload) {
3711 case ElemSegmentPayload::Expressions: {
3712 if (!d.readRefType(*codeMeta->types, codeMeta->features(), &elemType)) {
3713 return false;
3714 }
3715 } break;
3716 case ElemSegmentPayload::Indices: {
3717 uint8_t elemKind;
3718 if (!d.readFixedU8(&elemKind)) {
3719 return d.fail("expected element kind");
3720 }
3721
3722 if (elemKind != uint8_t(DefinitionKind::Function)) {
3723 return d.fail("invalid element kind");
3724 }
3725 elemType = RefType::func().asNonNullable();
3726 } break;
3727 }
3728 }
3729
3730 // For active segments, check if the element type is compatible with the
3731 // destination table type.
3732 if (seg.active()) {
3733 RefType tblElemType = codeMeta->tables[seg.tableIndex].elemType;
3734 if (!CheckIsSubtypeOf(d, *codeMeta, d.currentOffset(),
3735 ValType(elemType).storageType(),
3736 ValType(tblElemType).storageType())) {
3737 return false;
3738 }
3739 }
3740 seg.elemType = elemType;
3741
3742 uint32_t numElems;
3743 if (!d.readVarU32(&numElems)) {
3744 return d.fail("expected element segment size");
3745 }
3746
3747 if (numElems > MaxElemSegmentLength) {
3748 return d.fail("too many elements in element segment");
3749 }
3750
3751 bool isAsmJS = seg.active() && codeMeta->tables[seg.tableIndex].isAsmJS;
3752
3753 switch (payload) {
3754 case ElemSegmentPayload::Indices: {
3755 seg.encoding = ModuleElemSegment::Encoding::Indices;
3756 if (!seg.elemIndices.reserve(numElems)) {
3757 return false;
3758 }
3759
3760 for (uint32_t i = 0; i < numElems; i++) {
3761 uint32_t elemIndex;
3762 if (!d.readVarU32(&elemIndex)) {
3763 return d.fail("failed to read element index");
3764 }
3765 // The only valid type of index right now is a function index.
3766 if (elemIndex >= codeMeta->numFuncs()) {
3767 return d.fail("element index out of range");
3768 }
3769
3770 seg.elemIndices.infallibleAppend(elemIndex);
3771 if (!isAsmJS) {
3772 codeMeta->funcs[elemIndex].declareFuncExported(/*eager=*/false,
3773 /*canRefFunc=*/true);
3774 }
3775 }
3776 } break;
3777 case ElemSegmentPayload::Expressions: {
3778 seg.encoding = ModuleElemSegment::Encoding::Expressions;
3779 const uint8_t* exprsStart = d.currentPosition();
3780 seg.elemExpressions.count = numElems;
3781 for (uint32_t i = 0; i < numElems; i++) {
3782 Maybe<LitVal> unusedLiteral;
3783 if (!DecodeConstantExpression(d, codeMeta, elemType, &unusedLiteral)) {
3784 return false;
3785 }
3786 }
3787 const uint8_t* exprsEnd = d.currentPosition();
3788 if (!seg.elemExpressions.exprBytes.append(exprsStart, exprsEnd)) {
3789 return false;
3790 }
3791 } break;
3792 }
3793
3794 codeMeta->elemSegmentTypes.infallibleAppend(seg.elemType);
3795 moduleMeta->elemSegments.infallibleAppend(std::move(seg));
3796
3797 return true;
3798 }
3799
3800 static bool DecodeElemSection(Decoder& d, CodeMetadata* codeMeta,
3801 ModuleMetadata* moduleMeta) {
3802 MaybeBytecodeRange range;
3803 if (!d.startSection(SectionId::Elem, codeMeta, &range, "elem")) {
3804 return false;
3805 }
3806 if (!range) {
3807 return true;
3808 }
3809
3810 uint32_t numSegments;
3811 if (!d.readVarU32(&numSegments)) {
3812 return d.fail("failed to read number of elem segments");
3813 }
3814
3815 if (numSegments > MaxElemSegments) {
3816 return d.fail("too many elem segments");
3817 }
3818
3819 if (!moduleMeta->elemSegments.reserve(numSegments) ||
3820 !codeMeta->elemSegmentTypes.reserve(numSegments)) {
3821 return false;
3822 }
3823
3824 for (uint32_t i = 0; i < numSegments; i++) {
3825 if (!DecodeElemSegment(d, codeMeta, moduleMeta)) {
3826 return false;
3827 }
3828 }
3829
3830 return d.finishSection(*range, "elem");
3831 }
3832
3833 static bool DecodeDataCountSection(Decoder& d, CodeMetadata* codeMeta) {
3834 MaybeBytecodeRange range;
3835 if (!d.startSection(SectionId::DataCount, codeMeta, &range, "datacount")) {
3836 return false;
3837 }
3838 if (!range) {
3839 return true;
3840 }
3841
3842 uint32_t dataCount;
3843 if (!d.readVarU32(&dataCount)) {
3844 return d.fail("expected data segment count");
3845 }
3846
3847 codeMeta->dataCount.emplace(dataCount);
3848
3849 return d.finishSection(*range, "datacount");
3850 }
3851
3852 bool wasm::StartsCodeSection(const uint8_t* begin, const uint8_t* end,
3853 BytecodeRange* codeSection) {
3854 UniqueChars unused;
3855 Decoder d(begin, end, 0, &unused);
3856
3857 if (!DecodePreamble(d)) {
3858 return false;
3859 }
3860
3861 while (!d.done()) {
3862 uint8_t id;
3863 BytecodeRange range;
3864 if (!d.readSectionHeader(&id, &range)) {
3865 return false;
3866 }
3867
3868 if (id == uint8_t(SectionId::Code)) {
3869 if (range.size() > MaxCodeSectionBytes) {
3870 return false;
3871 }
3872
3873 *codeSection = range;
3874 return true;
3875 }
3876
3877 if (!d.readBytes(range.size())) {
3878 return false;
3879 }
3880 }
3881
3882 return false;
3883 }
3884
3885 #ifdef ENABLE_WASM_BRANCH_HINTING
3886 static bool ParseBranchHintingSection(Decoder& d, CodeMetadata* codeMeta) {
3887 uint32_t functionCount;
3888 if (!d.readVarU32(&functionCount)) {
3889 return d.fail("failed to read function count");
3890 }
3891
3892 for (uint32_t i = 0; i < functionCount; i++) {
3893 uint32_t functionIndex;
3894 if (!d.readVarU32(&functionIndex)) {
3895 return d.fail("failed to read function index");
3896 }
3897
3898 // Disallow branch hints on imported functions.
3899 if ((functionIndex >= codeMeta->funcs.length()) ||
3900 (functionIndex < codeMeta->numFuncImports)) {
3901 return d.fail("invalid function index in branch hint");
3902 }
3903
3904 uint32_t hintCount;
3905 if (!d.readVarU32(&hintCount)) {
3906 return d.fail("failed to read hint count");
3907 }
3908
3909 BranchHintVector hintVector;
3910 if (!hintVector.reserve(hintCount)) {
3911 return false;
3912 }
3913
3914 // Branch hint offsets must appear in increasing byte offset order, at most
3915 // once for each offset.
3916 uint32_t prevOffsetPlus1 = 0;
3917 for (uint32_t hintIndex = 0; hintIndex < hintCount; hintIndex++) {
3918 uint32_t branchOffset;
3919 if (!d.readVarU32(&branchOffset)) {
3920 return d.fail("failed to read branch offset");
3921 }
3922 if (branchOffset <= prevOffsetPlus1) {
3923 return d.fail("Invalid offset in code hint");
3924 }
3925
3926 uint32_t reserved;
3927 if (!d.readVarU32(&reserved) || (reserved != 1)) {
3928 return d.fail("Invalid reserved value for code hint");
3929 }
3930
3931 uint32_t branchHintValue;
3932 if (!d.readVarU32(&branchHintValue) ||
3933 (branchHintValue >= MaxBranchHintValue)) {
3934 return d.fail("Invalid branch hint value");
3935 }
3936
3937 BranchHint branchHint = static_cast<BranchHint>(branchHintValue);
3938 BranchHintEntry entry(branchOffset, branchHint);
3939 hintVector.infallibleAppend(entry);
3940
3941 prevOffsetPlus1 = branchOffset;
3942 }
3943
3944 // Save this collection in the module
3945 if (!codeMeta->branchHints.addHintsForFunc(functionIndex,
3946 std::move(hintVector))) {
3947 return false;
3948 }
3949 }
3950
3951 return true;
3952 }
3953
3954 static bool DecodeBranchHintingSection(Decoder& d, CodeMetadata* codeMeta) {
3955 MaybeBytecodeRange range;
3956 if (!d.startCustomSection(BranchHintingSectionName, codeMeta, &range)) {
3957 return false;
3958 }
3959 if (!range) {
3960 return true;
3961 }
3962
3963 // Skip this custom section if errors are encountered during parsing.
3964 if (!ParseBranchHintingSection(d, codeMeta)) {
3965 codeMeta->branchHints.setFailedAndClear();
3966 }
3967
3968 if (!d.finishCustomSection(BranchHintingSectionName, *range)) {
3969 codeMeta->branchHints.setFailedAndClear();
3970 }
3971 return true;
3972 }
3973 #endif
3974
3975 bool wasm::DecodeModuleEnvironment(Decoder& d, CodeMetadata* codeMeta,
3976 ModuleMetadata* moduleMeta) {
3977 if (!DecodePreamble(d)) {
3978 return false;
3979 }
3980
3981 if (!DecodeTypeSection(d, codeMeta)) {
3982 return false;
3983 }
3984
3985 if (!DecodeImportSection(d, codeMeta, moduleMeta)) {
3986 return false;
3987 }
3988
3989 // Eagerly check imports for future link errors against any known builtin
3990 // module.
3991 if (!CheckImportsAgainstBuiltinModules(d, codeMeta, moduleMeta)) {
3992 return false;
3993 }
3994
3995 if (!DecodeFunctionSection(d, codeMeta)) {
3996 return false;
3997 }
3998
3999 if (!DecodeTableSection(d, codeMeta)) {
4000 return false;
4001 }
4002
4003 if (!DecodeMemorySection(d, codeMeta)) {
4004 return false;
4005 }
4006
4007 if (!CheckBuiltinImportsHaveMemory(d, codeMeta)) {
4008 return false;
4009 }
4010
4011 if (!DecodeTagSection(d, codeMeta)) {
4012 return false;
4013 }
4014
4015 if (!DecodeGlobalSection(d, codeMeta)) {
4016 return false;
4017 }
4018
4019 if (!DecodeExportSection(d, codeMeta, moduleMeta)) {
4020 return false;
4021 }
4022
4023 if (!DecodeStartSection(d, codeMeta, moduleMeta)) {
4024 return false;
4025 }
4026
4027 if (!DecodeElemSection(d, codeMeta, moduleMeta)) {
4028 return false;
4029 }
4030
4031 if (!DecodeDataCountSection(d, codeMeta)) {
4032 return false;
4033 }
4034
4035 #ifdef ENABLE_WASM_BRANCH_HINTING
4036 if (codeMeta->branchHintingEnabled() &&
4037 !DecodeBranchHintingSection(d, codeMeta)) {
4038 return false;
4039 }
4040 #endif
4041
4042 if (!d.startSection(SectionId::Code, codeMeta, &codeMeta->codeSectionRange,
4043 "code")) {
4044 return false;
4045 }
4046
4047 if (codeMeta->codeSectionRange &&
4048 codeMeta->codeSectionRange->size() > MaxCodeSectionBytes) {
4049 return d.fail("code section too big");
4050 }
4051
4052 return true;
4053 }
4054
4055 static bool DecodeFunctionBody(Decoder& d, const CodeMetadata& codeMeta,
4056 uint32_t funcIndex) {
4057 uint32_t bodySize;
4058 if (!d.readVarU32(&bodySize)) {
4059 return d.fail("expected number of function body bytes");
4060 }
4061
4062 if (bodySize > MaxFunctionBytes) {
4063 return d.fail("function body too big");
4064 }
4065
4066 if (d.bytesRemain() < bodySize) {
4067 return d.fail("function body length too big");
4068 }
4069
4070 return ValidateFunctionBody(codeMeta, funcIndex, bodySize, d);
4071 }
4072
4073 static bool DecodeCodeSection(Decoder& d, CodeMetadata* codeMeta) {
4074 if (!codeMeta->codeSectionRange) {
4075 if (codeMeta->numFuncDefs() != 0) {
4076 return d.fail("expected code section");
4077 }
4078 return true;
4079 }
4080
4081 uint32_t numFuncDefs;
4082 if (!d.readVarU32(&numFuncDefs)) {
4083 return d.fail("expected function body count");
4084 }
4085
4086 if (numFuncDefs != codeMeta->numFuncDefs()) {
4087 return d.fail(
4088 "function body count does not match function signature count");
4089 }
4090
4091 for (uint32_t funcDefIndex = 0; funcDefIndex < numFuncDefs; funcDefIndex++) {
4092 if (!DecodeFunctionBody(d, *codeMeta,
4093 codeMeta->numFuncImports + funcDefIndex)) {
4094 return false;
4095 }
4096 }
4097
4098 return d.finishSection(*codeMeta->codeSectionRange, "code");
4099 }
4100
4101 static bool DecodeDataSection(Decoder& d, CodeMetadata* codeMeta,
4102 ModuleMetadata* moduleMeta) {
4103 MaybeBytecodeRange range;
4104 if (!d.startSection(SectionId::Data, codeMeta, &range, "data")) {
4105 return false;
4106 }
4107 if (!range) {
4108 if (codeMeta->dataCount.isSome() && *codeMeta->dataCount > 0) {
4109 return d.fail("number of data segments does not match declared count");
4110 }
4111 return true;
4112 }
4113
4114 uint32_t numSegments;
4115 if (!d.readVarU32(&numSegments)) {
4116 return d.fail("failed to read number of data segments");
4117 }
4118
4119 if (numSegments > MaxDataSegments) {
4120 return d.fail("too many data segments");
4121 }
4122
4123 if (codeMeta->dataCount.isSome() && numSegments != *codeMeta->dataCount) {
4124 return d.fail("number of data segments does not match declared count");
4125 }
4126
4127 for (uint32_t i = 0; i < numSegments; i++) {
4128 uint32_t initializerKindVal;
4129 if (!d.readVarU32(&initializerKindVal)) {
4130 return d.fail("expected data initializer-kind field");
4131 }
4132
4133 switch (initializerKindVal) {
4134 case uint32_t(DataSegmentKind::Active):
4135 case uint32_t(DataSegmentKind::Passive):
4136 case uint32_t(DataSegmentKind::ActiveWithMemoryIndex):
4137 break;
4138 default:
4139 return d.fail("invalid data initializer-kind field");
4140 }
4141
4142 DataSegmentKind initializerKind = DataSegmentKind(initializerKindVal);
4143
4144 if (initializerKind != DataSegmentKind::Passive &&
4145 codeMeta->numMemories() == 0) {
4146 return d.fail("active data segment requires a memory section");
4147 }
4148
4149 DataSegmentRange segRange;
4150 if (initializerKind == DataSegmentKind::ActiveWithMemoryIndex) {
4151 if (!d.readVarU32(&segRange.memoryIndex)) {
4152 return d.fail("expected memory index");
4153 }
4154 } else if (initializerKind == DataSegmentKind::Active) {
4155 segRange.memoryIndex = 0;
4156 } else {
4157 segRange.memoryIndex = InvalidMemoryIndex;
4158 }
4159
4160 if (initializerKind == DataSegmentKind::Active ||
4161 initializerKind == DataSegmentKind::ActiveWithMemoryIndex) {
4162 if (segRange.memoryIndex >= codeMeta->numMemories()) {
4163 return d.fail("invalid memory index");
4164 }
4165
4166 InitExpr segOffset;
4167 ValType exprType =
4168 ToValType(codeMeta->memories[segRange.memoryIndex].addressType());
4169 if (!InitExpr::decodeAndValidate(d, codeMeta, exprType, &segOffset)) {
4170 return false;
4171 }
4172 segRange.offsetIfActive.emplace(std::move(segOffset));
4173 }
4174
4175 if (!d.readVarU32(&segRange.length)) {
4176 return d.fail("expected segment size");
4177 }
4178
4179 if (segRange.length > MaxDataSegmentLengthPages * PageSize) {
4180 return d.fail("segment size too big");
4181 }
4182
4183 segRange.bytecodeOffset = d.currentOffset();
4184
4185 if (!d.readBytes(segRange.length)) {
4186 return d.fail("data segment shorter than declared");
4187 }
4188
4189 if (!moduleMeta->dataSegmentRanges.append(std::move(segRange))) {
4190 return false;
4191 }
4192 }
4193
4194 return d.finishSection(*range, "data");
4195 }
4196
4197 static bool DecodeModuleNameSubsection(Decoder& d,
4198 const CustomSectionRange& nameSection,
4199 CodeMetadata* codeMeta,
4200 ModuleMetadata* moduleMeta) {
4201 Maybe<uint32_t> endOffset;
4202 if (!d.startNameSubsection(NameType::Module, &endOffset)) {
4203 return false;
4204 }
4205 if (!endOffset) {
4206 return true;
4207 }
4208
4209 Name moduleName;
4210 if (!d.readVarU32(&moduleName.length)) {
4211 return d.fail("failed to read module name length");
4212 }
4213
4214 MOZ_ASSERT(d.currentOffset() >= nameSection.payload.start);
4215 moduleName.offsetInNamePayload =
4216 d.currentOffset() - nameSection.payload.start;
4217
4218 const uint8_t* bytes;
4219 if (!d.readBytes(moduleName.length, &bytes)) {
4220 return d.fail("failed to read module name bytes");
4221 }
4222
4223 if (!d.finishNameSubsection(*endOffset)) {
4224 return false;
4225 }
4226
4227 // Only save the module name if the whole subsection validates.
4228 codeMeta->nameSection->moduleName = moduleName;
4229 return true;
4230 }
4231
4232 static bool DecodeFunctionNameSubsection(Decoder& d,
4233 const CustomSectionRange& nameSection,
4234 CodeMetadata* codeMeta,
4235 ModuleMetadata* moduleMeta) {
4236 Maybe<uint32_t> endOffset;
4237 if (!d.startNameSubsection(NameType::Function, &endOffset)) {
4238 return false;
4239 }
4240 if (!endOffset) {
4241 return true;
4242 }
4243
4244 uint32_t nameCount = 0;
4245 if (!d.readVarU32(&nameCount) || nameCount > MaxFuncs) {
4246 return d.fail("bad function name count");
4247 }
4248
4249 NameVector funcNames;
4250
4251 for (uint32_t i = 0; i < nameCount; ++i) {
4252 uint32_t funcIndex = 0;
4253 if (!d.readVarU32(&funcIndex)) {
4254 return d.fail("unable to read function index");
4255 }
4256
4257 // Names must refer to real functions and be given in ascending order.
4258 if (funcIndex >= codeMeta->numFuncs() || funcIndex < funcNames.length()) {
4259 return d.fail("invalid function index");
4260 }
4261
4262 Name funcName;
4263 if (!d.readVarU32(&funcName.length) ||
4264 funcName.length > JS::MaxStringLength) {
4265 return d.fail("unable to read function name length");
4266 }
4267
4268 if (!funcName.length) {
4269 continue;
4270 }
4271
4272 if (!funcNames.resize(funcIndex + 1)) {
4273 return false;
4274 }
4275
4276 MOZ_ASSERT(d.currentOffset() >= nameSection.payload.start);
4277 funcName.offsetInNamePayload =
4278 d.currentOffset() - nameSection.payload.start;
4279
4280 if (!d.readBytes(funcName.length)) {
4281 return d.fail("unable to read function name bytes");
4282 }
4283
4284 funcNames[funcIndex] = funcName;
4285 }
4286
4287 if (!d.finishNameSubsection(*endOffset)) {
4288 return false;
4289 }
4290
4291 // Only save names if the entire subsection decoded correctly.
4292 codeMeta->nameSection->funcNames = std::move(funcNames);
4293 return true;
4294 }
4295
4296 static bool DecodeNameSection(Decoder& d, CodeMetadata* codeMeta,
4297 ModuleMetadata* moduleMeta) {
4298 MaybeBytecodeRange range;
4299 if (!d.startCustomSection(NameSectionName, codeMeta, &range)) {
4300 return false;
4301 }
4302 if (!range) {
4303 return true;
4304 }
4305
4306 codeMeta->nameSection.emplace((NameSection){
4307 .customSectionIndex =
4308 uint32_t(codeMeta->customSectionRanges.length() - 1),
4309 });
4310 const CustomSectionRange& nameSection = codeMeta->customSectionRanges.back();
4311
4312 // Once started, custom sections do not report validation errors.
4313
4314 if (!DecodeModuleNameSubsection(d, nameSection, codeMeta, moduleMeta)) {
4315 goto finish;
4316 }
4317
4318 if (!DecodeFunctionNameSubsection(d, nameSection, codeMeta, moduleMeta)) {
4319 goto finish;
4320 }
4321
4322 while (d.currentOffset() < range->end) {
4323 if (!d.skipNameSubsection()) {
4324 goto finish;
4325 }
4326 }
4327
4328 finish:
4329 if (!d.finishCustomSection(NameSectionName, *range)) {
4330 codeMeta->nameSection = mozilla::Nothing();
4331 }
4332 return true;
4333 }
4334
4335 bool wasm::DecodeModuleTail(Decoder& d, CodeMetadata* codeMeta,
4336 ModuleMetadata* moduleMeta) {
4337 if (!DecodeDataSection(d, codeMeta, moduleMeta)) {
4338 return false;
4339 }
4340
4341 if (!DecodeNameSection(d, codeMeta, moduleMeta)) {
4342 return false;
4343 }
4344
4345 while (!d.done()) {
4346 if (!d.skipCustomSection(codeMeta)) {
4347 return false;
4348 }
4349 }
4350
4351 return true;
4352 }
4353
4354 // Validate algorithm.
4355
4356 bool wasm::Validate(JSContext* cx, const BytecodeSource& bytecode,
4357 const FeatureOptions& options, UniqueChars* error) {
4358 FeatureArgs features = FeatureArgs::build(cx, options);
4359 SharedCompileArgs compileArgs = CompileArgs::buildForValidation(features);
4360 if (!compileArgs) {
4361 return false;
4362 }
4363 MutableModuleMetadata moduleMeta = js_new<ModuleMetadata>();
4364 if (!moduleMeta || !moduleMeta->init(*compileArgs)) {
4365 return false;
4366 }
4367 MutableCodeMetadata codeMeta = moduleMeta->codeMeta;
4368
4369 Decoder envDecoder(bytecode.envSpan(), bytecode.envRange().start, error);
4370 if (!DecodeModuleEnvironment(envDecoder, codeMeta, moduleMeta)) {
4371 return false;
4372 }
4373
4374 if (bytecode.hasCodeSection()) {
4375 // DecodeModuleEnvironment will stop and return true if there is an unknown
4376 // section before the code section. We must check this and return an error.
4377 if (!moduleMeta->codeMeta->codeSectionRange) {
4378 envDecoder.fail("unknown section before code section");
4379 return false;
4380 }
4381
4382 // Our pre-parse that split the module should ensure that after we've
4383 // parsed the environment there are no bytes left.
4384 MOZ_RELEASE_ASSERT(envDecoder.done());
4385
4386 Decoder codeDecoder(bytecode.codeSpan(), bytecode.codeRange().start, error);
4387 if (!DecodeCodeSection(codeDecoder, codeMeta)) {
4388 return false;
4389 }
4390 // Our pre-parse that split the module should ensure that after we've
4391 // parsed the code section there are no bytes left.
4392 MOZ_RELEASE_ASSERT(codeDecoder.done());
4393
4394 Decoder tailDecoder(bytecode.tailSpan(), bytecode.tailRange().start, error);
4395 if (!DecodeModuleTail(tailDecoder, codeMeta, moduleMeta)) {
4396 return false;
4397 }
4398 // Decoding the module tail should consume all remaining bytes.
4399 MOZ_RELEASE_ASSERT(tailDecoder.done());
4400 } else {
4401 if (!DecodeCodeSection(envDecoder, codeMeta)) {
4402 return false;
4403 }
4404 if (!DecodeModuleTail(envDecoder, codeMeta, moduleMeta)) {
4405 return false;
4406 }
4407 // Decoding the module tail should consume all remaining bytes.
4408 MOZ_RELEASE_ASSERT(envDecoder.done());
4409 }
4410
4411 MOZ_ASSERT(!*error, "unreported error in decoding");
4412 return true;
4413 }