Debian Codegraph @

third_party/rust/wasmparser/src/validator/operators.rs

raw
1 /* Copyright 2019 Mozilla Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 // The basic validation algorithm here is copied from the "Validation
17 // Algorithm" section of the WebAssembly specification -
18 // https://webassembly.github.io/spec/core/appendix/algorithm.html.
19 //
20 // That algorithm is followed pretty closely here, namely `push_operand`,
21 // `pop_operand`, `push_ctrl`, and `pop_ctrl`. If anything here is a bit
22 // confusing it's recommended to read over that section to see how it maps to
23 // the various methods here.
24
25 use crate::{
26 limits::MAX_WASM_FUNCTION_LOCALS, AbstractHeapType, BinaryReaderError, BlockType, BrTable,
27 Catch, ContType, FieldType, FrameKind, FuncType, GlobalType, Handle, HeapType, Ieee32, Ieee64,
28 MemArg, ModuleArity, RefType, Result, ResumeTable, StorageType, StructType, SubType, TableType,
29 TryTable, UnpackedIndex, ValType, VisitOperator, WasmFeatures, WasmModuleResources, V128,
30 };
31 use crate::{prelude::*, CompositeInnerType, Ordering};
32 use core::ops::{Deref, DerefMut};
33
34 pub(crate) struct OperatorValidator {
35 pub(super) locals: Locals,
36 pub(super) local_inits: Vec<bool>,
37
38 // This is a list of flags for wasm features which are used to gate various
39 // instructions.
40 pub(crate) features: WasmFeatures,
41
42 // Temporary storage used during `match_stack_operands`
43 popped_types_tmp: Vec<MaybeType>,
44
45 /// The `control` list is the list of blocks that we're currently in.
46 control: Vec<Frame>,
47 /// The `operands` is the current type stack.
48 operands: Vec<MaybeType>,
49 /// When local_inits is modified, the relevant index is recorded here to be
50 /// undone when control pops
51 inits: Vec<u32>,
52
53 /// Offset of the `end` instruction which emptied the `control` stack, which
54 /// must be the end of the function.
55 end_which_emptied_control: Option<usize>,
56
57 /// Whether validation is happening in a shared context.
58 shared: bool,
59
60 #[cfg(debug_assertions)]
61 pub(crate) pop_push_count: (u32, u32),
62 }
63
64 // No science was performed in the creation of this number, feel free to change
65 // it if you so like.
66 const MAX_LOCALS_TO_TRACK: usize = 50;
67
68 pub(super) struct Locals {
69 // Total number of locals in the function.
70 num_locals: u32,
71
72 // The first MAX_LOCALS_TO_TRACK locals in a function. This is used to
73 // optimize the theoretically common case where most functions don't have
74 // many locals and don't need a full binary search in the entire local space
75 // below.
76 first: Vec<ValType>,
77
78 // This is a "compressed" list of locals for this function. The list of
79 // locals are represented as a list of tuples. The second element is the
80 // type of the local, and the first element is monotonically increasing as
81 // you visit elements of this list. The first element is the maximum index
82 // of the local, after the previous index, of the type specified.
83 //
84 // This allows us to do a binary search on the list for a local's index for
85 // `local.{get,set,tee}`. We do a binary search for the index desired, and
86 // it either lies in a "hole" where the maximum index is specified later,
87 // or it's at the end of the list meaning it's out of bounds.
88 all: Vec<(u32, ValType)>,
89 }
90
91 /// A Wasm control flow block on the control flow stack during Wasm validation.
92 //
93 // # Dev. Note
94 //
95 // This structure corresponds to `ctrl_frame` as specified at in the validation
96 // appendix of the wasm spec
97 #[derive(Debug, Copy, Clone)]
98 pub struct Frame {
99 /// Indicator for what kind of instruction pushed this frame.
100 pub kind: FrameKind,
101 /// The type signature of this frame, represented as a singular return type
102 /// or a type index pointing into the module's types.
103 pub block_type: BlockType,
104 /// The index, below which, this frame cannot modify the operand stack.
105 pub height: usize,
106 /// Whether this frame is unreachable so far.
107 pub unreachable: bool,
108 /// The number of initializations in the stack at the time of its creation
109 pub init_height: usize,
110 }
111
112 struct OperatorValidatorTemp<'validator, 'resources, T> {
113 offset: usize,
114 inner: &'validator mut OperatorValidator,
115 resources: &'resources T,
116 }
117
118 #[derive(Default)]
119 pub struct OperatorValidatorAllocations {
120 popped_types_tmp: Vec<MaybeType>,
121 control: Vec<Frame>,
122 operands: Vec<MaybeType>,
123 local_inits: Vec<bool>,
124 inits: Vec<u32>,
125 locals_first: Vec<ValType>,
126 locals_all: Vec<(u32, ValType)>,
127 }
128
129 /// Type storage within the validator.
130 ///
131 /// When managing the operand stack in unreachable code, the validator may not
132 /// fully know an operand's type. this unknown state is known as the `bottom`
133 /// type in the WebAssembly specification. Validating further instructions may
134 /// give us more information; either partial (`PartialRef`) or fully known.
135 #[derive(Debug, Copy, Clone)]
136 enum MaybeType<T = ValType> {
137 /// The operand has no available type information due to unreachable code.
138 ///
139 /// This state represents "unknown" and corresponds to the `bottom` type in
140 /// the WebAssembly specification. There are no constraints on what this
141 /// type may be and it can match any other type during validation.
142 Bottom,
143 /// The operand is known to be a reference and we may know its abstract
144 /// type.
145 ///
146 /// This state is not fully `Known`, however, because its type can be
147 /// interpreted as either:
148 /// - `shared` or not-`shared`
149 /// - nullable or not nullable
150 ///
151 /// No further refinements are required for WebAssembly instructions today
152 /// but this may grow in the future.
153 UnknownRef(Option<AbstractHeapType>),
154 /// The operand is known to have type `T`.
155 Known(T),
156 }
157
158 // The validator is pretty performance-sensitive and `MaybeType` is the main
159 // unit of storage, so assert that it doesn't exceed 4 bytes which is the
160 // current expected size.
161 const _: () = {
162 assert!(core::mem::size_of::<MaybeType>() == 4);
163 };
164
165 impl core::fmt::Display for MaybeType {
166 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
167 match self {
168 MaybeType::Bottom => write!(f, "bot"),
169 MaybeType::UnknownRef(ty) => {
170 write!(f, "(ref shared? ")?;
171 match ty {
172 Some(ty) => write!(f, "{}bot", ty.as_str(true))?,
173 None => write!(f, "bot")?,
174 }
175 write!(f, ")")
176 }
177 MaybeType::Known(ty) => core::fmt::Display::fmt(ty, f),
178 }
179 }
180 }
181
182 impl From<ValType> for MaybeType {
183 fn from(ty: ValType) -> MaybeType {
184 MaybeType::Known(ty)
185 }
186 }
187
188 impl From<RefType> for MaybeType {
189 fn from(ty: RefType) -> MaybeType {
190 let ty: ValType = ty.into();
191 ty.into()
192 }
193 }
194 impl From<MaybeType<RefType>> for MaybeType<ValType> {
195 fn from(ty: MaybeType<RefType>) -> MaybeType<ValType> {
196 match ty {
197 MaybeType::Bottom => MaybeType::Bottom,
198 MaybeType::UnknownRef(ty) => MaybeType::UnknownRef(ty),
199 MaybeType::Known(t) => MaybeType::Known(t.into()),
200 }
201 }
202 }
203
204 impl MaybeType<RefType> {
205 fn as_non_null(&self) -> MaybeType<RefType> {
206 match self {
207 MaybeType::Bottom => MaybeType::Bottom,
208 MaybeType::UnknownRef(ty) => MaybeType::UnknownRef(*ty),
209 MaybeType::Known(ty) => MaybeType::Known(ty.as_non_null()),
210 }
211 }
212
213 fn is_maybe_shared(&self, resources: &impl WasmModuleResources) -> Option<bool> {
214 match self {
215 MaybeType::Bottom => None,
216 MaybeType::UnknownRef(_) => None,
217 MaybeType::Known(ty) => Some(resources.is_shared(*ty)),
218 }
219 }
220 }
221
222 impl OperatorValidator {
223 fn new(features: &WasmFeatures, allocs: OperatorValidatorAllocations) -> Self {
224 let OperatorValidatorAllocations {
225 popped_types_tmp,
226 control,
227 operands,
228 local_inits,
229 inits,
230 locals_first,
231 locals_all,
232 } = allocs;
233 debug_assert!(popped_types_tmp.is_empty());
234 debug_assert!(control.is_empty());
235 debug_assert!(operands.is_empty());
236 debug_assert!(local_inits.is_empty());
237 debug_assert!(inits.is_empty());
238 debug_assert!(locals_first.is_empty());
239 debug_assert!(locals_all.is_empty());
240 OperatorValidator {
241 locals: Locals {
242 num_locals: 0,
243 first: locals_first,
244 all: locals_all,
245 },
246 local_inits,
247 inits,
248 features: *features,
249 popped_types_tmp,
250 operands,
251 control,
252 end_which_emptied_control: None,
253 shared: false,
254 #[cfg(debug_assertions)]
255 pop_push_count: (0, 0),
256 }
257 }
258
259 /// Creates a new operator validator which will be used to validate a
260 /// function whose type is the `ty` index specified.
261 ///
262 /// The `resources` are used to learn about the function type underlying
263 /// `ty`.
264 pub fn new_func<T>(
265 ty: u32,
266 offset: usize,
267 features: &WasmFeatures,
268 resources: &T,
269 allocs: OperatorValidatorAllocations,
270 ) -> Result<Self>
271 where
272 T: WasmModuleResources,
273 {
274 let mut ret = OperatorValidator::new(features, allocs);
275 ret.control.push(Frame {
276 kind: FrameKind::Block,
277 block_type: BlockType::FuncType(ty),
278 height: 0,
279 unreachable: false,
280 init_height: 0,
281 });
282
283 // Retrieve the function's type via index (`ty`); the `offset` is
284 // necessary due to `sub_type_at`'s error messaging.
285 let sub_ty = OperatorValidatorTemp {
286 offset,
287 inner: &mut ret,
288 resources,
289 }
290 .sub_type_at(ty)?;
291
292 // Set up the function's locals.
293 if let CompositeInnerType::Func(func_ty) = &sub_ty.composite_type.inner {
294 for ty in func_ty.params() {
295 ret.locals.define(1, *ty);
296 ret.local_inits.push(true);
297 }
298 } else {
299 bail!(offset, "expected func type at index {ty}, found {sub_ty}")
300 }
301
302 // If we're in a shared function, ensure we do not access unshared
303 // objects.
304 if sub_ty.composite_type.shared {
305 ret.shared = true;
306 }
307 Ok(ret)
308 }
309
310 /// Creates a new operator validator which will be used to validate an
311 /// `init_expr` constant expression which should result in the `ty`
312 /// specified.
313 pub fn new_const_expr(
314 features: &WasmFeatures,
315 ty: ValType,
316 allocs: OperatorValidatorAllocations,
317 ) -> Self {
318 let mut ret = OperatorValidator::new(features, allocs);
319 ret.control.push(Frame {
320 kind: FrameKind::Block,
321 block_type: BlockType::Type(ty),
322 height: 0,
323 unreachable: false,
324 init_height: 0,
325 });
326 ret
327 }
328
329 pub fn define_locals(
330 &mut self,
331 offset: usize,
332 count: u32,
333 mut ty: ValType,
334 resources: &impl WasmModuleResources,
335 ) -> Result<()> {
336 resources.check_value_type(&mut ty, &self.features, offset)?;
337 if count == 0 {
338 return Ok(());
339 }
340 if !self.locals.define(count, ty) {
341 return Err(BinaryReaderError::new(
342 "too many locals: locals exceed maximum",
343 offset,
344 ));
345 }
346 self.local_inits
347 .resize(self.local_inits.len() + count as usize, ty.is_defaultable());
348 Ok(())
349 }
350
351 /// Returns the current operands stack height.
352 pub fn operand_stack_height(&self) -> usize {
353 self.operands.len()
354 }
355
356 /// Returns the optional value type of the value operand at the given
357 /// `depth` from the top of the operand stack.
358 ///
359 /// - Returns `None` if the `depth` is out of bounds.
360 /// - Returns `Some(None)` if there is a value with unknown type
361 /// at the given `depth`.
362 ///
363 /// # Note
364 ///
365 /// A `depth` of 0 will refer to the last operand on the stack.
366 pub fn peek_operand_at(&self, depth: usize) -> Option<Option<ValType>> {
367 Some(match self.operands.iter().rev().nth(depth)? {
368 MaybeType::Known(t) => Some(*t),
369 MaybeType::Bottom | MaybeType::UnknownRef(..) => None,
370 })
371 }
372
373 /// Returns the number of frames on the control flow stack.
374 pub fn control_stack_height(&self) -> usize {
375 self.control.len()
376 }
377
378 pub fn get_frame(&self, depth: usize) -> Option<&Frame> {
379 self.control.iter().rev().nth(depth)
380 }
381
382 /// Create a temporary [`OperatorValidatorTemp`] for validation.
383 pub fn with_resources<'a, 'validator, 'resources, T>(
384 &'validator mut self,
385 resources: &'resources T,
386 offset: usize,
387 ) -> impl VisitOperator<'a, Output = Result<()>> + ModuleArity + 'validator
388 where
389 T: WasmModuleResources,
390 'resources: 'validator,
391 {
392 WasmProposalValidator(OperatorValidatorTemp {
393 offset,
394 inner: self,
395 resources,
396 })
397 }
398
399 pub fn finish(&mut self, offset: usize) -> Result<()> {
400 if self.control.last().is_some() {
401 bail!(
402 offset,
403 "control frames remain at end of function: END opcode expected"
404 );
405 }
406
407 // The `end` opcode is one byte which means that the `offset` here
408 // should point just beyond the `end` opcode which emptied the control
409 // stack. If not that means more instructions were present after the
410 // control stack was emptied.
411 if offset != self.end_which_emptied_control.unwrap() + 1 {
412 return Err(self.err_beyond_end(offset));
413 }
414 Ok(())
415 }
416
417 fn err_beyond_end(&self, offset: usize) -> BinaryReaderError {
418 format_err!(offset, "operators remaining after end of function")
419 }
420
421 pub fn into_allocations(self) -> OperatorValidatorAllocations {
422 fn clear<T>(mut tmp: Vec<T>) -> Vec<T> {
423 tmp.clear();
424 tmp
425 }
426 OperatorValidatorAllocations {
427 popped_types_tmp: clear(self.popped_types_tmp),
428 control: clear(self.control),
429 operands: clear(self.operands),
430 local_inits: clear(self.local_inits),
431 inits: clear(self.inits),
432 locals_first: clear(self.locals.first),
433 locals_all: clear(self.locals.all),
434 }
435 }
436
437 fn record_pop(&mut self) {
438 #[cfg(debug_assertions)]
439 {
440 self.pop_push_count.0 += 1;
441 }
442 }
443
444 fn record_push(&mut self) {
445 #[cfg(debug_assertions)]
446 {
447 self.pop_push_count.1 += 1;
448 }
449 }
450 }
451
452 impl<R> Deref for OperatorValidatorTemp<'_, '_, R> {
453 type Target = OperatorValidator;
454 fn deref(&self) -> &OperatorValidator {
455 self.inner
456 }
457 }
458
459 impl<R> DerefMut for OperatorValidatorTemp<'_, '_, R> {
460 fn deref_mut(&mut self) -> &mut OperatorValidator {
461 self.inner
462 }
463 }
464
465 impl<'resources, R> OperatorValidatorTemp<'_, 'resources, R>
466 where
467 R: WasmModuleResources,
468 {
469 /// Pushes a type onto the operand stack.
470 ///
471 /// This is used by instructions to represent a value that is pushed to the
472 /// operand stack. This can fail, but only if `Type` is feature gated.
473 /// Otherwise the push operation always succeeds.
474 fn push_operand<T>(&mut self, ty: T) -> Result<()>
475 where
476 T: Into<MaybeType>,
477 {
478 let maybe_ty = ty.into();
479
480 if cfg!(debug_assertions) {
481 match maybe_ty {
482 MaybeType::Known(ValType::Ref(r)) => match r.heap_type() {
483 HeapType::Concrete(index) => {
484 debug_assert!(
485 matches!(index, UnpackedIndex::Id(_)),
486 "only ref types referencing `CoreTypeId`s can \
487 be pushed to the operand stack"
488 );
489 }
490 _ => {}
491 },
492 _ => {}
493 }
494 }
495
496 self.operands.push(maybe_ty);
497 self.record_push();
498 Ok(())
499 }
500
501 fn push_concrete_ref(&mut self, nullable: bool, type_index: u32) -> Result<()> {
502 let mut heap_ty = HeapType::Concrete(UnpackedIndex::Module(type_index));
503
504 // Canonicalize the module index into an id.
505 self.resources.check_heap_type(&mut heap_ty, self.offset)?;
506 debug_assert!(matches!(heap_ty, HeapType::Concrete(UnpackedIndex::Id(_))));
507
508 let ref_ty = RefType::new(nullable, heap_ty).ok_or_else(|| {
509 format_err!(self.offset, "implementation limit: type index too large")
510 })?;
511
512 self.push_operand(ref_ty)
513 }
514
515 fn pop_concrete_ref(&mut self, nullable: bool, type_index: u32) -> Result<MaybeType> {
516 let mut heap_ty = HeapType::Concrete(UnpackedIndex::Module(type_index));
517
518 // Canonicalize the module index into an id.
519 self.resources.check_heap_type(&mut heap_ty, self.offset)?;
520 debug_assert!(matches!(heap_ty, HeapType::Concrete(UnpackedIndex::Id(_))));
521
522 let ref_ty = RefType::new(nullable, heap_ty).ok_or_else(|| {
523 format_err!(self.offset, "implementation limit: type index too large")
524 })?;
525
526 self.pop_operand(Some(ref_ty.into()))
527 }
528
529 /// Pop the given label types, checking that they are indeed present on the
530 /// stack, and then push them back on again.
531 fn pop_push_label_types(
532 &mut self,
533 label_types: impl PreciseIterator<Item = ValType>,
534 ) -> Result<()> {
535 for ty in label_types.clone().rev() {
536 self.pop_operand(Some(ty))?;
537 }
538 for ty in label_types {
539 self.push_operand(ty)?;
540 }
541 Ok(())
542 }
543
544 /// Attempts to pop a type from the operand stack.
545 ///
546 /// This function is used to remove types from the operand stack. The
547 /// `expected` argument can be used to indicate that a type is required, or
548 /// simply that something is needed to be popped.
549 ///
550 /// If `expected` is `Some(T)` then this will be guaranteed to return
551 /// `T`, and it will only return success if the current block is
552 /// unreachable or if `T` was found at the top of the operand stack.
553 ///
554 /// If `expected` is `None` then it indicates that something must be on the
555 /// operand stack, but it doesn't matter what's on the operand stack. This
556 /// is useful for polymorphic instructions like `select`.
557 ///
558 /// If `Some(T)` is returned then `T` was popped from the operand stack and
559 /// matches `expected`. If `None` is returned then it means that `None` was
560 /// expected and a type was successfully popped, but its exact type is
561 /// indeterminate because the current block is unreachable.
562 fn pop_operand(&mut self, expected: Option<ValType>) -> Result<MaybeType> {
563 // This method is one of the hottest methods in the validator so to
564 // improve codegen this method contains a fast-path success case where
565 // if the top operand on the stack is as expected it's returned
566 // immediately. This is the most common case where the stack will indeed
567 // have the expected type and all we need to do is pop it off.
568 //
569 // Note that this still has to be careful to be correct, though. For
570 // efficiency an operand is unconditionally popped and on success it is
571 // matched against the state of the world to see if we could actually
572 // pop it. If we shouldn't have popped it then it's passed to the slow
573 // path to get pushed back onto the stack.
574 let popped = match self.operands.pop() {
575 Some(MaybeType::Known(actual_ty)) => {
576 if Some(actual_ty) == expected {
577 if let Some(control) = self.control.last() {
578 if self.operands.len() >= control.height {
579 self.record_pop();
580 return Ok(MaybeType::Known(actual_ty));
581 }
582 }
583 }
584 Some(MaybeType::Known(actual_ty))
585 }
586 other => other,
587 };
588
589 self._pop_operand(expected, popped)
590 }
591
592 // This is the "real" implementation of `pop_operand` which is 100%
593 // spec-compliant with little attention paid to efficiency since this is the
594 // slow-path from the actual `pop_operand` function above.
595 #[cold]
596 fn _pop_operand(
597 &mut self,
598 expected: Option<ValType>,
599 popped: Option<MaybeType>,
600 ) -> Result<MaybeType> {
601 self.operands.extend(popped);
602 let control = match self.control.last() {
603 Some(c) => c,
604 None => return Err(self.err_beyond_end(self.offset)),
605 };
606 let actual = if self.operands.len() == control.height && control.unreachable {
607 MaybeType::Bottom
608 } else {
609 if self.operands.len() == control.height {
610 let desc = match expected {
611 Some(ty) => ty_to_str(ty),
612 None => "a type".into(),
613 };
614 bail!(
615 self.offset,
616 "type mismatch: expected {desc} but nothing on stack"
617 )
618 } else {
619 self.operands.pop().unwrap()
620 }
621 };
622 if let Some(expected) = expected {
623 match (actual, expected) {
624 // The bottom type matches all expectations
625 (MaybeType::Bottom, _) => {}
626
627 // The "heap bottom" type only matches other references types,
628 // but not any integer types. Note that if the heap bottom is
629 // known to have a specific abstract heap type then a subtype
630 // check is performed against hte expected type.
631 (MaybeType::UnknownRef(actual_ty), ValType::Ref(expected)) => {
632 if let Some(actual) = actual_ty {
633 let expected_shared = self.resources.is_shared(expected);
634 let actual = RefType::new(
635 false,
636 HeapType::Abstract {
637 shared: expected_shared,
638 ty: actual,
639 },
640 )
641 .unwrap();
642 if !self.resources.is_subtype(actual.into(), expected.into()) {
643 bail!(
644 self.offset,
645 "type mismatch: expected {}, found {}",
646 ty_to_str(expected.into()),
647 ty_to_str(actual.into())
648 );
649 }
650 }
651 }
652
653 // Use the `is_subtype` predicate to test if a found type matches
654 // the expectation.
655 (MaybeType::Known(actual), expected) => {
656 if !self.resources.is_subtype(actual, expected) {
657 bail!(
658 self.offset,
659 "type mismatch: expected {}, found {}",
660 ty_to_str(expected),
661 ty_to_str(actual)
662 );
663 }
664 }
665
666 // A "heap bottom" type cannot match any numeric types.
667 (
668 MaybeType::UnknownRef(..),
669 ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128,
670 ) => {
671 bail!(
672 self.offset,
673 "type mismatch: expected {}, found heap type",
674 ty_to_str(expected)
675 )
676 }
677 }
678 }
679 self.record_pop();
680 Ok(actual)
681 }
682
683 /// Match expected vs. actual operand.
684 fn match_operand(
685 &mut self,
686 actual: ValType,
687 expected: ValType,
688 ) -> Result<(), BinaryReaderError> {
689 #[cfg(debug_assertions)]
690 let tmp = self.pop_push_count;
691 self.push_operand(actual)?;
692 self.pop_operand(Some(expected))?;
693 #[cfg(debug_assertions)]
694 {
695 self.pop_push_count = tmp;
696 }
697 Ok(())
698 }
699
700 /// Match a type sequence to the top of the stack.
701 fn match_stack_operands(
702 &mut self,
703 expected_tys: impl PreciseIterator<Item = ValType> + 'resources,
704 ) -> Result<()> {
705 debug_assert!(self.popped_types_tmp.is_empty());
706 self.popped_types_tmp.reserve(expected_tys.len());
707 #[cfg(debug_assertions)]
708 let tmp = self.pop_push_count;
709 for expected_ty in expected_tys.rev() {
710 let actual_ty = self.pop_operand(Some(expected_ty))?;
711 self.popped_types_tmp.push(actual_ty);
712 }
713 for ty in self.inner.popped_types_tmp.drain(..).rev() {
714 self.inner.operands.push(ty.into());
715 }
716 #[cfg(debug_assertions)]
717 {
718 self.pop_push_count = tmp;
719 }
720 Ok(())
721 }
722
723 /// Pop a reference type from the operand stack.
724 fn pop_ref(&mut self, expected: Option<RefType>) -> Result<MaybeType<RefType>> {
725 match self.pop_operand(expected.map(|t| t.into()))? {
726 MaybeType::Bottom => Ok(MaybeType::UnknownRef(None)),
727 MaybeType::UnknownRef(ty) => Ok(MaybeType::UnknownRef(ty)),
728 MaybeType::Known(ValType::Ref(rt)) => Ok(MaybeType::Known(rt)),
729 MaybeType::Known(ty) => bail!(
730 self.offset,
731 "type mismatch: expected ref but found {}",
732 ty_to_str(ty)
733 ),
734 }
735 }
736
737 /// Pop a reference type from the operand stack, checking if it is a subtype
738 /// of a nullable type of `expected` or the shared version of `expected`.
739 ///
740 /// This function returns the popped reference type and its `shared`-ness,
741 /// saving extra lookups for concrete types.
742 fn pop_maybe_shared_ref(&mut self, expected: AbstractHeapType) -> Result<MaybeType<RefType>> {
743 let actual = match self.pop_ref(None)? {
744 MaybeType::Bottom => return Ok(MaybeType::Bottom),
745 MaybeType::UnknownRef(None) => return Ok(MaybeType::UnknownRef(None)),
746 MaybeType::UnknownRef(Some(actual)) => {
747 if !actual.is_subtype_of(expected) {
748 bail!(
749 self.offset,
750 "type mismatch: expected subtype of {}, found {}",
751 expected.as_str(false),
752 actual.as_str(false),
753 )
754 }
755 return Ok(MaybeType::UnknownRef(Some(actual)));
756 }
757 MaybeType::Known(ty) => ty,
758 };
759 // Change our expectation based on whether we're dealing with an actual
760 // shared or unshared type.
761 let is_actual_shared = self.resources.is_shared(actual);
762 let expected = RefType::new(
763 true,
764 HeapType::Abstract {
765 shared: is_actual_shared,
766 ty: expected,
767 },
768 )
769 .unwrap();
770
771 // Check (again) that the actual type is a subtype of the expected type.
772 // Note that `_pop_operand` already does this kind of thing but we leave
773 // that for a future refactoring (TODO).
774 if !self.resources.is_subtype(actual.into(), expected.into()) {
775 bail!(
776 self.offset,
777 "type mismatch: expected subtype of {expected}, found {actual}",
778 )
779 }
780 Ok(MaybeType::Known(actual))
781 }
782
783 /// Fetches the type for the local at `idx`, returning an error if it's out
784 /// of bounds.
785 fn local(&self, idx: u32) -> Result<ValType> {
786 match self.locals.get(idx) {
787 Some(ty) => Ok(ty),
788 None => bail!(
789 self.offset,
790 "unknown local {}: local index out of bounds",
791 idx
792 ),
793 }
794 }
795
796 /// Flags the current control frame as unreachable, additionally truncating
797 /// the currently active operand stack.
798 fn unreachable(&mut self) -> Result<()> {
799 let control = match self.control.last_mut() {
800 Some(frame) => frame,
801 None => return Err(self.err_beyond_end(self.offset)),
802 };
803 control.unreachable = true;
804 let new_height = control.height;
805 self.operands.truncate(new_height);
806 Ok(())
807 }
808
809 /// Pushes a new frame onto the control stack.
810 ///
811 /// This operation is used when entering a new block such as an if, loop,
812 /// or block itself. The `kind` of block is specified which indicates how
813 /// breaks interact with this block's type. Additionally the type signature
814 /// of the block is specified by `ty`.
815 fn push_ctrl(&mut self, kind: FrameKind, ty: BlockType) -> Result<()> {
816 // Push a new frame which has a snapshot of the height of the current
817 // operand stack.
818 let height = self.operands.len();
819 let init_height = self.inits.len();
820 self.control.push(Frame {
821 kind,
822 block_type: ty,
823 height,
824 unreachable: false,
825 init_height,
826 });
827 // All of the parameters are now also available in this control frame,
828 // so we push them here in order.
829 for ty in self.params(ty)? {
830 self.push_operand(ty)?;
831 }
832 Ok(())
833 }
834
835 /// Pops a frame from the control stack.
836 ///
837 /// This function is used when exiting a block and leaves a block scope.
838 /// Internally this will validate that blocks have the correct result type.
839 fn pop_ctrl(&mut self) -> Result<Frame> {
840 // Read the expected type and expected height of the operand stack the
841 // end of the frame.
842 let frame = match self.control.last() {
843 Some(f) => f,
844 None => return Err(self.err_beyond_end(self.offset)),
845 };
846 let ty = frame.block_type;
847 let height = frame.height;
848 let init_height = frame.init_height;
849
850 // reset_locals in the spec
851 for init in self.inits.split_off(init_height) {
852 self.local_inits[init as usize] = false;
853 }
854
855 // Pop all the result types, in reverse order, from the operand stack.
856 // These types will, possibly, be transferred to the next frame.
857 for ty in self.results(ty)?.rev() {
858 self.pop_operand(Some(ty))?;
859 }
860
861 // Make sure that the operand stack has returned to is original
862 // height...
863 if self.operands.len() != height {
864 bail!(
865 self.offset,
866 "type mismatch: values remaining on stack at end of block"
867 );
868 }
869
870 // And then we can remove it!
871 Ok(self.control.pop().unwrap())
872 }
873
874 /// Validates a relative jump to the `depth` specified.
875 ///
876 /// Returns the type signature of the block that we're jumping to as well
877 /// as the kind of block if the jump is valid. Otherwise returns an error.
878 fn jump(&self, depth: u32) -> Result<(BlockType, FrameKind)> {
879 if self.control.is_empty() {
880 return Err(self.err_beyond_end(self.offset));
881 }
882 match (self.control.len() - 1).checked_sub(depth as usize) {
883 Some(i) => {
884 let frame = &self.control[i];
885 Ok((frame.block_type, frame.kind))
886 }
887 None => bail!(self.offset, "unknown label: branch depth too large"),
888 }
889 }
890
891 /// Validates that `memory_index` is valid in this module, and returns the
892 /// type of address used to index the memory specified.
893 fn check_memory_index(&self, memory_index: u32) -> Result<ValType> {
894 match self.resources.memory_at(memory_index) {
895 Some(mem) => Ok(mem.index_type()),
896 None => bail!(self.offset, "unknown memory {}", memory_index),
897 }
898 }
899
900 /// Validates a `memarg for alignment and such (also the memory it
901 /// references), and returns the type of index used to address the memory.
902 fn check_memarg(&self, memarg: MemArg) -> Result<ValType> {
903 let index_ty = self.check_memory_index(memarg.memory)?;
904 if memarg.align > memarg.max_align {
905 bail!(
906 self.offset,
907 "malformed memop alignment: alignment must not be larger than natural"
908 );
909 }
910 if index_ty == ValType::I32 && memarg.offset > u64::from(u32::MAX) {
911 bail!(self.offset, "offset out of range: must be <= 2**32");
912 }
913 Ok(index_ty)
914 }
915
916 fn check_floats_enabled(&self) -> Result<()> {
917 if !self.features.floats() {
918 bail!(self.offset, "floating-point instruction disallowed");
919 }
920 Ok(())
921 }
922
923 fn check_shared_memarg(&self, memarg: MemArg) -> Result<ValType> {
924 if memarg.align != memarg.max_align {
925 bail!(
926 self.offset,
927 "atomic instructions must always specify maximum alignment"
928 );
929 }
930 self.check_memory_index(memarg.memory)
931 }
932
933 fn check_simd_lane_index(&self, index: u8, max: u8) -> Result<()> {
934 if index >= max {
935 bail!(self.offset, "SIMD index out of bounds");
936 }
937 Ok(())
938 }
939
940 /// Validates a block type, primarily with various in-flight proposals.
941 fn check_block_type(&self, ty: &mut BlockType) -> Result<()> {
942 match ty {
943 BlockType::Empty => Ok(()),
944 BlockType::Type(t) => self
945 .resources
946 .check_value_type(t, &self.features, self.offset),
947 BlockType::FuncType(idx) => {
948 if !self.features.multi_value() {
949 bail!(
950 self.offset,
951 "blocks, loops, and ifs may only produce a resulttype \
952 when multi-value is not enabled",
953 );
954 }
955 self.func_type_at(*idx)?;
956 Ok(())
957 }
958 }
959 }
960
961 /// Returns the corresponding function type for the `func` item located at
962 /// `function_index`.
963 fn type_of_function(&self, function_index: u32) -> Result<&'resources FuncType> {
964 if let Some(type_index) = self.resources.type_index_of_function(function_index) {
965 self.func_type_at(type_index)
966 } else {
967 bail!(
968 self.offset,
969 "unknown function {function_index}: function index out of bounds",
970 )
971 }
972 }
973
974 /// Checks a call-style instruction which will be invoking the function `ty`
975 /// specified.
976 ///
977 /// This will pop parameters from the operand stack for the function's
978 /// parameters and then push the results of the function on the stack.
979 fn check_call_ty(&mut self, ty: &FuncType) -> Result<()> {
980 for &ty in ty.params().iter().rev() {
981 debug_assert_type_indices_are_ids(ty);
982 self.pop_operand(Some(ty))?;
983 }
984 for &ty in ty.results() {
985 debug_assert_type_indices_are_ids(ty);
986 self.push_operand(ty)?;
987 }
988 Ok(())
989 }
990
991 /// Similar to `check_call_ty` except used for tail-call instructions.
992 fn check_return_call_ty(&mut self, ty: &FuncType) -> Result<()> {
993 self.check_func_type_same_results(ty)?;
994 for &ty in ty.params().iter().rev() {
995 debug_assert_type_indices_are_ids(ty);
996 self.pop_operand(Some(ty))?;
997 }
998
999 // Match the results with this function's, but don't include in pop/push counts.
1000 #[cfg(debug_assertions)]
1001 let tmp = self.pop_push_count;
1002 for &ty in ty.results() {
1003 debug_assert_type_indices_are_ids(ty);
1004 self.push_operand(ty)?;
1005 }
1006 self.check_return()?;
1007 #[cfg(debug_assertions)]
1008 {
1009 self.pop_push_count = tmp;
1010 }
1011
1012 Ok(())
1013 }
1014
1015 /// Checks the immediate `type_index` of a `call_ref`-style instruction
1016 /// (also `return_call_ref`).
1017 ///
1018 /// This will validate that the value on the stack is a `(ref type_index)`
1019 /// or a subtype. This will then return the corresponding function type used
1020 /// for this call (to be used with `check_call_ty` or
1021 /// `check_return_call_ty`).
1022 fn check_call_ref_ty(&mut self, type_index: u32) -> Result<&'resources FuncType> {
1023 let unpacked_index = UnpackedIndex::Module(type_index);
1024 let mut hty = HeapType::Concrete(unpacked_index);
1025 self.resources.check_heap_type(&mut hty, self.offset)?;
1026 let expected = RefType::new(true, hty).expect("hty should be previously validated");
1027 self.pop_ref(Some(expected))?;
1028 self.func_type_at(type_index)
1029 }
1030
1031 /// Validates the immediate operands of a `call_indirect` or
1032 /// `return_call_indirect` instruction.
1033 ///
1034 /// This will validate that `table_index` is valid and a funcref table. It
1035 /// will additionally pop the index argument which is used to index into the
1036 /// table.
1037 ///
1038 /// The return value of this function is the function type behind
1039 /// `type_index` which must then be passed to `check_{call,return_call}_ty`.
1040 fn check_call_indirect_ty(
1041 &mut self,
1042 type_index: u32,
1043 table_index: u32,
1044 ) -> Result<&'resources FuncType> {
1045 let tab = self.table_type_at(table_index)?;
1046 if !self
1047 .resources
1048 .is_subtype(ValType::Ref(tab.element_type), ValType::FUNCREF)
1049 {
1050 bail!(
1051 self.offset,
1052 "indirect calls must go through a table with type <= funcref",
1053 );
1054 }
1055 self.pop_operand(Some(tab.index_type()))?;
1056 self.func_type_at(type_index)
1057 }
1058
1059 /// Validates a `return` instruction, popping types from the operand
1060 /// stack that the function needs.
1061 fn check_return(&mut self) -> Result<()> {
1062 if self.control.is_empty() {
1063 return Err(self.err_beyond_end(self.offset));
1064 }
1065 for ty in self.results(self.control[0].block_type)?.rev() {
1066 self.pop_operand(Some(ty))?;
1067 }
1068 self.unreachable()?;
1069 Ok(())
1070 }
1071
1072 /// Check that the given type has the same result types as the current
1073 /// function's results.
1074 fn check_func_type_same_results(&self, callee_ty: &FuncType) -> Result<()> {
1075 if self.control.is_empty() {
1076 return Err(self.err_beyond_end(self.offset));
1077 }
1078 let caller_rets = self.results(self.control[0].block_type)?;
1079 if callee_ty.results().len() != caller_rets.len()
1080 || !caller_rets
1081 .zip(callee_ty.results())
1082 .all(|(caller_ty, callee_ty)| self.resources.is_subtype(*callee_ty, caller_ty))
1083 {
1084 let caller_rets = self
1085 .results(self.control[0].block_type)?
1086 .map(|ty| format!("{ty}"))
1087 .collect::<Vec<_>>()
1088 .join(" ");
1089 let callee_rets = callee_ty
1090 .results()
1091 .iter()
1092 .map(|ty| format!("{ty}"))
1093 .collect::<Vec<_>>()
1094 .join(" ");
1095 bail!(
1096 self.offset,
1097 "type mismatch: current function requires result type \
1098 [{caller_rets}] but callee returns [{callee_rets}]"
1099 );
1100 }
1101 Ok(())
1102 }
1103
1104 /// Checks the validity of a common comparison operator.
1105 fn check_cmp_op(&mut self, ty: ValType) -> Result<()> {
1106 self.pop_operand(Some(ty))?;
1107 self.pop_operand(Some(ty))?;
1108 self.push_operand(ValType::I32)?;
1109 Ok(())
1110 }
1111
1112 /// Checks the validity of a common float comparison operator.
1113 fn check_fcmp_op(&mut self, ty: ValType) -> Result<()> {
1114 debug_assert!(matches!(ty, ValType::F32 | ValType::F64));
1115 self.check_floats_enabled()?;
1116 self.check_cmp_op(ty)
1117 }
1118
1119 /// Checks the validity of a common unary operator.
1120 fn check_unary_op(&mut self, ty: ValType) -> Result<()> {
1121 self.pop_operand(Some(ty))?;
1122 self.push_operand(ty)?;
1123 Ok(())
1124 }
1125
1126 /// Checks the validity of a common unary float operator.
1127 fn check_funary_op(&mut self, ty: ValType) -> Result<()> {
1128 debug_assert!(matches!(ty, ValType::F32 | ValType::F64));
1129 self.check_floats_enabled()?;
1130 self.check_unary_op(ty)
1131 }
1132
1133 /// Checks the validity of a common conversion operator.
1134 fn check_conversion_op(&mut self, into: ValType, from: ValType) -> Result<()> {
1135 self.pop_operand(Some(from))?;
1136 self.push_operand(into)?;
1137 Ok(())
1138 }
1139
1140 /// Checks the validity of a common float conversion operator.
1141 fn check_fconversion_op(&mut self, into: ValType, from: ValType) -> Result<()> {
1142 debug_assert!(matches!(into, ValType::F32 | ValType::F64));
1143 self.check_floats_enabled()?;
1144 self.check_conversion_op(into, from)
1145 }
1146
1147 /// Checks the validity of a common binary operator.
1148 fn check_binary_op(&mut self, ty: ValType) -> Result<()> {
1149 self.pop_operand(Some(ty))?;
1150 self.pop_operand(Some(ty))?;
1151 self.push_operand(ty)?;
1152 Ok(())
1153 }
1154
1155 /// Checks the validity of a common binary float operator.
1156 fn check_fbinary_op(&mut self, ty: ValType) -> Result<()> {
1157 debug_assert!(matches!(ty, ValType::F32 | ValType::F64));
1158 self.check_floats_enabled()?;
1159 self.check_binary_op(ty)
1160 }
1161
1162 /// Checks the validity of an atomic load operator.
1163 fn check_atomic_load(&mut self, memarg: MemArg, load_ty: ValType) -> Result<()> {
1164 let ty = self.check_shared_memarg(memarg)?;
1165 self.pop_operand(Some(ty))?;
1166 self.push_operand(load_ty)?;
1167 Ok(())
1168 }
1169
1170 /// Checks the validity of an atomic store operator.
1171 fn check_atomic_store(&mut self, memarg: MemArg, store_ty: ValType) -> Result<()> {
1172 let ty = self.check_shared_memarg(memarg)?;
1173 self.pop_operand(Some(store_ty))?;
1174 self.pop_operand(Some(ty))?;
1175 Ok(())
1176 }
1177
1178 /// Checks the validity of atomic binary operator on memory.
1179 fn check_atomic_binary_memory_op(&mut self, memarg: MemArg, op_ty: ValType) -> Result<()> {
1180 let ty = self.check_shared_memarg(memarg)?;
1181 self.pop_operand(Some(op_ty))?;
1182 self.pop_operand(Some(ty))?;
1183 self.push_operand(op_ty)?;
1184 Ok(())
1185 }
1186
1187 /// Checks the validity of an atomic compare exchange operator on memories.
1188 fn check_atomic_binary_memory_cmpxchg(&mut self, memarg: MemArg, op_ty: ValType) -> Result<()> {
1189 let ty = self.check_shared_memarg(memarg)?;
1190 self.pop_operand(Some(op_ty))?;
1191 self.pop_operand(Some(op_ty))?;
1192 self.pop_operand(Some(ty))?;
1193 self.push_operand(op_ty)?;
1194 Ok(())
1195 }
1196
1197 /// Checks a [`V128`] splat operator.
1198 fn check_v128_splat(&mut self, src_ty: ValType) -> Result<()> {
1199 self.pop_operand(Some(src_ty))?;
1200 self.push_operand(ValType::V128)?;
1201 Ok(())
1202 }
1203
1204 /// Checks a [`V128`] binary operator.
1205 fn check_v128_binary_op(&mut self) -> Result<()> {
1206 self.pop_operand(Some(ValType::V128))?;
1207 self.pop_operand(Some(ValType::V128))?;
1208 self.push_operand(ValType::V128)?;
1209 Ok(())
1210 }
1211
1212 /// Checks a [`V128`] binary float operator.
1213 fn check_v128_fbinary_op(&mut self) -> Result<()> {
1214 self.check_floats_enabled()?;
1215 self.check_v128_binary_op()
1216 }
1217
1218 /// Checks a [`V128`] unary operator.
1219 fn check_v128_unary_op(&mut self) -> Result<()> {
1220 self.pop_operand(Some(ValType::V128))?;
1221 self.push_operand(ValType::V128)?;
1222 Ok(())
1223 }
1224
1225 /// Checks a [`V128`] unary float operator.
1226 fn check_v128_funary_op(&mut self) -> Result<()> {
1227 self.check_floats_enabled()?;
1228 self.check_v128_unary_op()
1229 }
1230
1231 /// Checks a [`V128`] relaxed ternary operator.
1232 fn check_v128_ternary_op(&mut self) -> Result<()> {
1233 self.pop_operand(Some(ValType::V128))?;
1234 self.pop_operand(Some(ValType::V128))?;
1235 self.pop_operand(Some(ValType::V128))?;
1236 self.push_operand(ValType::V128)?;
1237 Ok(())
1238 }
1239
1240 /// Checks a [`V128`] test operator.
1241 fn check_v128_bitmask_op(&mut self) -> Result<()> {
1242 self.pop_operand(Some(ValType::V128))?;
1243 self.push_operand(ValType::I32)?;
1244 Ok(())
1245 }
1246
1247 /// Checks a [`V128`] shift operator.
1248 fn check_v128_shift_op(&mut self) -> Result<()> {
1249 self.pop_operand(Some(ValType::I32))?;
1250 self.pop_operand(Some(ValType::V128))?;
1251 self.push_operand(ValType::V128)?;
1252 Ok(())
1253 }
1254
1255 /// Checks a [`V128`] common load operator.
1256 fn check_v128_load_op(&mut self, memarg: MemArg) -> Result<()> {
1257 let idx = self.check_memarg(memarg)?;
1258 self.pop_operand(Some(idx))?;
1259 self.push_operand(ValType::V128)?;
1260 Ok(())
1261 }
1262
1263 /// Common helper for `ref.test` and `ref.cast` downcasting/checking
1264 /// instructions. Returns the given `heap_type` as a `ValType`.
1265 fn check_downcast(&mut self, nullable: bool, mut heap_type: HeapType) -> Result<RefType> {
1266 self.resources
1267 .check_heap_type(&mut heap_type, self.offset)?;
1268
1269 let sub_ty = RefType::new(nullable, heap_type).ok_or_else(|| {
1270 BinaryReaderError::new("implementation limit: type index too large", self.offset)
1271 })?;
1272 let sup_ty = RefType::new(true, self.resources.top_type(&heap_type))
1273 .expect("can't panic with non-concrete heap types");
1274
1275 self.pop_ref(Some(sup_ty))?;
1276 Ok(sub_ty)
1277 }
1278
1279 /// Common helper for both nullable and non-nullable variants of `ref.test`
1280 /// instructions.
1281 fn check_ref_test(&mut self, nullable: bool, heap_type: HeapType) -> Result<()> {
1282 self.check_downcast(nullable, heap_type)?;
1283 self.push_operand(ValType::I32)
1284 }
1285
1286 /// Common helper for both nullable and non-nullable variants of `ref.cast`
1287 /// instructions.
1288 fn check_ref_cast(&mut self, nullable: bool, heap_type: HeapType) -> Result<()> {
1289 let sub_ty = self.check_downcast(nullable, heap_type)?;
1290 self.push_operand(sub_ty)
1291 }
1292
1293 /// Common helper for checking the types of globals accessed with atomic RMW
1294 /// instructions, which only allow `i32` and `i64`.
1295 fn check_atomic_global_rmw_ty(&self, global_index: u32) -> Result<ValType> {
1296 let ty = self.global_type_at(global_index)?.content_type;
1297 if !(ty == ValType::I32 || ty == ValType::I64) {
1298 bail!(
1299 self.offset,
1300 "invalid type: `global.atomic.rmw.*` only allows `i32` and `i64`"
1301 );
1302 }
1303 Ok(ty)
1304 }
1305
1306 /// Common helper for checking the types of structs accessed with atomic RMW
1307 /// instructions, which only allow `i32` and `i64` types.
1308 fn check_struct_atomic_rmw(
1309 &mut self,
1310 op: &'static str,
1311 struct_type_index: u32,
1312 field_index: u32,
1313 ) -> Result<()> {
1314 let field = self.mutable_struct_field_at(struct_type_index, field_index)?;
1315 let field_ty = match field.element_type {
1316 StorageType::Val(ValType::I32) => ValType::I32,
1317 StorageType::Val(ValType::I64) => ValType::I64,
1318 _ => bail!(
1319 self.offset,
1320 "invalid type: `struct.atomic.rmw.{}` only allows `i32` and `i64`",
1321 op
1322 ),
1323 };
1324 self.pop_operand(Some(field_ty))?;
1325 self.pop_concrete_ref(true, struct_type_index)?;
1326 self.push_operand(field_ty)?;
1327 Ok(())
1328 }
1329
1330 /// Common helper for checking the types of arrays accessed with atomic RMW
1331 /// instructions, which only allow `i32` and `i64`.
1332 fn check_array_atomic_rmw(&mut self, op: &'static str, type_index: u32) -> Result<()> {
1333 let field = self.mutable_array_type_at(type_index)?;
1334 let elem_ty = match field.element_type {
1335 StorageType::Val(ValType::I32) => ValType::I32,
1336 StorageType::Val(ValType::I64) => ValType::I64,
1337 _ => bail!(
1338 self.offset,
1339 "invalid type: `array.atomic.rmw.{}` only allows `i32` and `i64`",
1340 op
1341 ),
1342 };
1343 self.pop_operand(Some(elem_ty))?;
1344 self.pop_operand(Some(ValType::I32))?;
1345 self.pop_concrete_ref(true, type_index)?;
1346 self.push_operand(elem_ty)?;
1347 Ok(())
1348 }
1349
1350 fn element_type_at(&self, elem_index: u32) -> Result<RefType> {
1351 match self.resources.element_type_at(elem_index) {
1352 Some(ty) => Ok(ty),
1353 None => bail!(
1354 self.offset,
1355 "unknown elem segment {}: segment index out of bounds",
1356 elem_index
1357 ),
1358 }
1359 }
1360
1361 fn sub_type_at(&self, at: u32) -> Result<&'resources SubType> {
1362 self.resources
1363 .sub_type_at(at)
1364 .ok_or_else(|| format_err!(self.offset, "unknown type: type index out of bounds"))
1365 }
1366
1367 fn struct_type_at(&self, at: u32) -> Result<&'resources StructType> {
1368 let sub_ty = self.sub_type_at(at)?;
1369 if let CompositeInnerType::Struct(struct_ty) = &sub_ty.composite_type.inner {
1370 if self.inner.shared && !sub_ty.composite_type.shared {
1371 bail!(
1372 self.offset,
1373 "shared functions cannot access unshared structs",
1374 );
1375 }
1376 Ok(struct_ty)
1377 } else {
1378 bail!(
1379 self.offset,
1380 "expected struct type at index {at}, found {sub_ty}"
1381 )
1382 }
1383 }
1384
1385 fn struct_field_at(&self, struct_type_index: u32, field_index: u32) -> Result<FieldType> {
1386 let field_index = usize::try_from(field_index).map_err(|_| {
1387 BinaryReaderError::new("unknown field: field index out of bounds", self.offset)
1388 })?;
1389 self.struct_type_at(struct_type_index)?
1390 .fields
1391 .get(field_index)
1392 .copied()
1393 .ok_or_else(|| {
1394 BinaryReaderError::new("unknown field: field index out of bounds", self.offset)
1395 })
1396 }
1397
1398 fn mutable_struct_field_at(
1399 &self,
1400 struct_type_index: u32,
1401 field_index: u32,
1402 ) -> Result<FieldType> {
1403 let field = self.struct_field_at(struct_type_index, field_index)?;
1404 if !field.mutable {
1405 bail!(
1406 self.offset,
1407 "invalid struct modification: struct field is immutable"
1408 )
1409 }
1410 Ok(field)
1411 }
1412
1413 fn array_type_at(&self, at: u32) -> Result<FieldType> {
1414 let sub_ty = self.sub_type_at(at)?;
1415 if let CompositeInnerType::Array(array_ty) = &sub_ty.composite_type.inner {
1416 if self.inner.shared && !sub_ty.composite_type.shared {
1417 bail!(
1418 self.offset,
1419 "shared functions cannot access unshared arrays",
1420 );
1421 }
1422 Ok(array_ty.0)
1423 } else {
1424 bail!(
1425 self.offset,
1426 "expected array type at index {at}, found {sub_ty}"
1427 )
1428 }
1429 }
1430
1431 fn mutable_array_type_at(&self, at: u32) -> Result<FieldType> {
1432 let field = self.array_type_at(at)?;
1433 if !field.mutable {
1434 bail!(
1435 self.offset,
1436 "invalid array modification: array is immutable"
1437 )
1438 }
1439 Ok(field)
1440 }
1441
1442 fn func_type_at(&self, at: u32) -> Result<&'resources FuncType> {
1443 let sub_ty = self.sub_type_at(at)?;
1444 if let CompositeInnerType::Func(func_ty) = &sub_ty.composite_type.inner {
1445 if self.inner.shared && !sub_ty.composite_type.shared {
1446 bail!(
1447 self.offset,
1448 "shared functions cannot access unshared functions",
1449 );
1450 }
1451 Ok(func_ty)
1452 } else {
1453 bail!(
1454 self.offset,
1455 "expected func type at index {at}, found {sub_ty}"
1456 )
1457 }
1458 }
1459
1460 fn cont_type_at(&self, at: u32) -> Result<&ContType> {
1461 let sub_ty = self.sub_type_at(at)?;
1462 if let CompositeInnerType::Cont(cont_ty) = &sub_ty.composite_type.inner {
1463 if self.inner.shared && !sub_ty.composite_type.shared {
1464 bail!(
1465 self.offset,
1466 "shared continuations cannot access unshared continuations",
1467 );
1468 }
1469 Ok(cont_ty)
1470 } else {
1471 bail!(self.offset, "non-continuation type {at}",)
1472 }
1473 }
1474
1475 fn func_type_of_cont_type(&self, cont_ty: &ContType) -> &'resources FuncType {
1476 let func_id = cont_ty.0.as_core_type_id().expect("valid core type id");
1477 self.resources.sub_type_at_id(func_id).unwrap_func()
1478 }
1479
1480 fn tag_at(&self, at: u32) -> Result<&'resources FuncType> {
1481 self.resources
1482 .tag_at(at)
1483 .ok_or_else(|| format_err!(self.offset, "unknown tag {}: tag index out of bounds", at))
1484 }
1485
1486 // Similar to `tag_at`, but checks that the result type is
1487 // empty. This is necessary when enabling the stack switching
1488 // feature as it allows non-empty result types on tags.
1489 fn exception_tag_at(&self, at: u32) -> Result<&'resources FuncType> {
1490 let func_ty = self.tag_at(at)?;
1491 if func_ty.results().len() != 0 {
1492 bail!(
1493 self.offset,
1494 "invalid exception type: non-empty tag result type"
1495 );
1496 }
1497 Ok(func_ty)
1498 }
1499
1500 fn global_type_at(&self, at: u32) -> Result<GlobalType> {
1501 if let Some(ty) = self.resources.global_at(at) {
1502 if self.inner.shared && !ty.shared {
1503 bail!(
1504 self.offset,
1505 "shared functions cannot access unshared globals",
1506 );
1507 }
1508 Ok(ty)
1509 } else {
1510 bail!(self.offset, "unknown global: global index out of bounds");
1511 }
1512 }
1513
1514 /// Validates that the `table` is valid and returns the type it points to.
1515 fn table_type_at(&self, table: u32) -> Result<TableType> {
1516 match self.resources.table_at(table) {
1517 Some(ty) => {
1518 if self.inner.shared && !ty.shared {
1519 bail!(
1520 self.offset,
1521 "shared functions cannot access unshared tables",
1522 );
1523 }
1524 Ok(ty)
1525 }
1526 None => bail!(
1527 self.offset,
1528 "unknown table {table}: table index out of bounds"
1529 ),
1530 }
1531 }
1532
1533 fn params(&self, ty: BlockType) -> Result<impl PreciseIterator<Item = ValType> + 'resources> {
1534 Ok(match ty {
1535 BlockType::Empty | BlockType::Type(_) => Either::B(None.into_iter()),
1536 BlockType::FuncType(t) => Either::A(self.func_type_at(t)?.params().iter().copied()),
1537 })
1538 }
1539
1540 fn results(&self, ty: BlockType) -> Result<impl PreciseIterator<Item = ValType> + 'resources> {
1541 Ok(match ty {
1542 BlockType::Empty => Either::B(None.into_iter()),
1543 BlockType::Type(t) => Either::B(Some(t).into_iter()),
1544 BlockType::FuncType(t) => Either::A(self.func_type_at(t)?.results().iter().copied()),
1545 })
1546 }
1547
1548 fn label_types(
1549 &self,
1550 ty: BlockType,
1551 kind: FrameKind,
1552 ) -> Result<impl PreciseIterator<Item = ValType> + 'resources> {
1553 Ok(match kind {
1554 FrameKind::Loop => Either::A(self.params(ty)?),
1555 _ => Either::B(self.results(ty)?),
1556 })
1557 }
1558
1559 fn check_data_segment(&self, data_index: u32) -> Result<()> {
1560 match self.resources.data_count() {
1561 None => bail!(self.offset, "data count section required"),
1562 Some(count) if data_index < count => Ok(()),
1563 Some(_) => bail!(self.offset, "unknown data segment {data_index}"),
1564 }
1565 }
1566
1567 fn check_resume_table(
1568 &mut self,
1569 table: ResumeTable,
1570 type_index: u32, // The type index annotation on the `resume` instruction, which `table` appears on.
1571 ) -> Result<&'resources FuncType> {
1572 let cont_ty = self.cont_type_at(type_index)?;
1573 // ts1 -> ts2
1574 let old_func_ty = self.func_type_of_cont_type(cont_ty);
1575 for handle in table.handlers {
1576 match handle {
1577 Handle::OnLabel { tag, label } => {
1578 // ts1' -> ts2'
1579 let tag_ty = self.tag_at(tag)?;
1580 // ts1'' (ref (cont $ft))
1581 let block = self.jump(label)?;
1582 // Pop the continuation reference.
1583 match self.label_types(block.0, block.1)?.last() {
1584 Some(ValType::Ref(rt)) if rt.is_concrete_type_ref() => {
1585 let sub_ty = self.resources.sub_type_at_id(rt.type_index().unwrap().as_core_type_id().expect("canonicalized index"));
1586 let new_cont =
1587 if let CompositeInnerType::Cont(cont) = &sub_ty.composite_type.inner {
1588 cont
1589 } else {
1590 bail!(self.offset, "non-continuation type");
1591 };
1592 let new_func_ty = self.func_type_of_cont_type(&new_cont);
1593 // Check that (ts2' -> ts2) <: $ft
1594 if new_func_ty.params().len() != tag_ty.results().len() || !self.is_subtype_many(new_func_ty.params(), tag_ty.results())
1595 || old_func_ty.results().len() != new_func_ty.results().len() || !self.is_subtype_many(old_func_ty.results(), new_func_ty.results()) {
1596 bail!(self.offset, "type mismatch in continuation type")
1597 }
1598 let expected_nargs = tag_ty.params().len() + 1;
1599 let actual_nargs = self
1600 .label_types(block.0, block.1)?
1601 .len();
1602 if actual_nargs != expected_nargs {
1603 bail!(self.offset, "type mismatch: expected {expected_nargs} label result(s), but label is annotated with {actual_nargs} results")
1604 }
1605
1606 let labeltys = self
1607 .label_types(block.0, block.1)?
1608 .take(expected_nargs - 1);
1609
1610 // Check that ts1'' <: ts1'.
1611 for (tagty, &lblty) in labeltys.zip(tag_ty.params()) {
1612 if !self.resources.is_subtype(lblty, tagty) {
1613 bail!(self.offset, "type mismatch between tag type and label type")
1614 }
1615 }
1616 }
1617 Some(ty) => {
1618 bail!(self.offset, "type mismatch: {}", ty_to_str(ty))
1619 }
1620 _ => bail!(self.offset,
1621 "type mismatch: instruction requires continuation reference type but label has none")
1622 }
1623 }
1624 Handle::OnSwitch { tag } => {
1625 let tag_ty = self.tag_at(tag)?;
1626 if tag_ty.params().len() != 0 {
1627 bail!(self.offset, "type mismatch: non-empty tag parameter type")
1628 }
1629 }
1630 }
1631 }
1632 Ok(old_func_ty)
1633 }
1634
1635 /// Applies `is_subtype` pointwise two equally sized collections
1636 /// (i.e. equally sized after skipped elements).
1637 fn is_subtype_many(&mut self, ts1: &[ValType], ts2: &[ValType]) -> bool {
1638 debug_assert!(ts1.len() == ts2.len());
1639 ts1.iter()
1640 .zip(ts2.iter())
1641 .all(|(ty1, ty2)| self.resources.is_subtype(*ty1, *ty2))
1642 }
1643
1644 fn check_binop128(&mut self) -> Result<()> {
1645 self.pop_operand(Some(ValType::I64))?;
1646 self.pop_operand(Some(ValType::I64))?;
1647 self.pop_operand(Some(ValType::I64))?;
1648 self.pop_operand(Some(ValType::I64))?;
1649 self.push_operand(ValType::I64)?;
1650 self.push_operand(ValType::I64)?;
1651 Ok(())
1652 }
1653
1654 fn check_i64_mul_wide(&mut self) -> Result<()> {
1655 self.pop_operand(Some(ValType::I64))?;
1656 self.pop_operand(Some(ValType::I64))?;
1657 self.push_operand(ValType::I64)?;
1658 self.push_operand(ValType::I64)?;
1659 Ok(())
1660 }
1661 }
1662
1663 pub fn ty_to_str(ty: ValType) -> &'static str {
1664 match ty {
1665 ValType::I32 => "i32",
1666 ValType::I64 => "i64",
1667 ValType::F32 => "f32",
1668 ValType::F64 => "f64",
1669 ValType::V128 => "v128",
1670 ValType::Ref(r) => r.wat(),
1671 }
1672 }
1673
1674 /// A wrapper "visitor" around the real operator validator internally which
1675 /// exists to check that the required wasm feature is enabled to proceed with
1676 /// validation.
1677 ///
1678 /// This validator is macro-generated to ensure that the proposal listed in this
1679 /// crate's macro matches the one that's validated here. Each instruction's
1680 /// visit method validates the specified proposal is enabled and then delegates
1681 /// to `OperatorValidatorTemp` to perform the actual opcode validation.
1682 struct WasmProposalValidator<'validator, 'resources, T>(
1683 OperatorValidatorTemp<'validator, 'resources, T>,
1684 );
1685
1686 impl<T> WasmProposalValidator<'_, '_, T> {
1687 fn check_enabled(&self, flag: bool, desc: &str) -> Result<()> {
1688 if flag {
1689 return Ok(());
1690 }
1691 bail!(self.0.offset, "{desc} support is not enabled");
1692 }
1693 }
1694
1695 macro_rules! validate_proposal {
1696 ($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {
1697 $(
1698 fn $visit(&mut self $($(,$arg: $argty)*)?) -> Result<()> {
1699 validate_proposal!(validate self $proposal);
1700 self.0.$visit($( $($arg),* )?)
1701 }
1702 )*
1703 };
1704
1705 (validate self mvp) => {};
1706 (validate $self:ident $proposal:ident) => {
1707 $self.check_enabled($self.0.features.$proposal(), validate_proposal!(desc $proposal))?
1708 };
1709
1710 (desc simd) => ("SIMD");
1711 (desc relaxed_simd) => ("relaxed SIMD");
1712 (desc threads) => ("threads");
1713 (desc shared_everything_threads) => ("shared-everything-threads");
1714 (desc saturating_float_to_int) => ("saturating float to int conversions");
1715 (desc reference_types) => ("reference types");
1716 (desc bulk_memory) => ("bulk memory");
1717 (desc sign_extension) => ("sign extension operations");
1718 (desc exceptions) => ("exceptions");
1719 (desc tail_call) => ("tail calls");
1720 (desc function_references) => ("function references");
1721 (desc memory_control) => ("memory control");
1722 (desc gc) => ("gc");
1723 (desc legacy_exceptions) => ("legacy exceptions");
1724 (desc stack_switching) => ("stack switching");
1725 (desc wide_arithmetic) => ("wide arithmetic");
1726 }
1727
1728 impl<'a, T> VisitOperator<'a> for WasmProposalValidator<'_, '_, T>
1729 where
1730 T: WasmModuleResources,
1731 {
1732 type Output = Result<()>;
1733
1734 for_each_operator!(validate_proposal);
1735 }
1736
1737 #[track_caller]
1738 #[inline]
1739 fn debug_assert_type_indices_are_ids(ty: ValType) {
1740 if cfg!(debug_assertions) {
1741 if let ValType::Ref(r) = ty {
1742 if let HeapType::Concrete(idx) = r.heap_type() {
1743 debug_assert!(
1744 matches!(idx, UnpackedIndex::Id(_)),
1745 "type reference should be a `CoreTypeId`, found {idx:?}"
1746 );
1747 }
1748 }
1749 }
1750 }
1751
1752 impl<'a, T> VisitOperator<'a> for OperatorValidatorTemp<'_, '_, T>
1753 where
1754 T: WasmModuleResources,
1755 {
1756 type Output = Result<()>;
1757
1758 fn visit_nop(&mut self) -> Self::Output {
1759 Ok(())
1760 }
1761 fn visit_unreachable(&mut self) -> Self::Output {
1762 self.unreachable()?;
1763 Ok(())
1764 }
1765 fn visit_block(&mut self, mut ty: BlockType) -> Self::Output {
1766 self.check_block_type(&mut ty)?;
1767 for ty in self.params(ty)?.rev() {
1768 self.pop_operand(Some(ty))?;
1769 }
1770 self.push_ctrl(FrameKind::Block, ty)?;
1771 Ok(())
1772 }
1773 fn visit_loop(&mut self, mut ty: BlockType) -> Self::Output {
1774 self.check_block_type(&mut ty)?;
1775 for ty in self.params(ty)?.rev() {
1776 self.pop_operand(Some(ty))?;
1777 }
1778 self.push_ctrl(FrameKind::Loop, ty)?;
1779 Ok(())
1780 }
1781 fn visit_if(&mut self, mut ty: BlockType) -> Self::Output {
1782 self.check_block_type(&mut ty)?;
1783 self.pop_operand(Some(ValType::I32))?;
1784 for ty in self.params(ty)?.rev() {
1785 self.pop_operand(Some(ty))?;
1786 }
1787 self.push_ctrl(FrameKind::If, ty)?;
1788 Ok(())
1789 }
1790 fn visit_else(&mut self) -> Self::Output {
1791 let frame = self.pop_ctrl()?;
1792 if frame.kind != FrameKind::If {
1793 bail!(self.offset, "else found outside of an `if` block");
1794 }
1795 self.push_ctrl(FrameKind::Else, frame.block_type)?;
1796 Ok(())
1797 }
1798 fn visit_try_table(&mut self, mut ty: TryTable) -> Self::Output {
1799 self.check_block_type(&mut ty.ty)?;
1800 for ty in self.params(ty.ty)?.rev() {
1801 self.pop_operand(Some(ty))?;
1802 }
1803 let exn_type = ValType::from(RefType::EXN);
1804 for catch in ty.catches {
1805 match catch {
1806 Catch::One { tag, label } => {
1807 let tag = self.exception_tag_at(tag)?;
1808 let (ty, kind) = self.jump(label)?;
1809 let params = tag.params();
1810 let types = self.label_types(ty, kind)?;
1811 if params.len() != types.len() {
1812 bail!(
1813 self.offset,
1814 "type mismatch: catch label must have same number of types as tag"
1815 );
1816 }
1817 for (expected, actual) in types.zip(params) {
1818 self.match_operand(*actual, expected)?;
1819 }
1820 }
1821 Catch::OneRef { tag, label } => {
1822 let tag = self.exception_tag_at(tag)?;
1823 let (ty, kind) = self.jump(label)?;
1824 let tag_params = tag.params().iter().copied();
1825 let label_types = self.label_types(ty, kind)?;
1826 if tag_params.len() + 1 != label_types.len() {
1827 bail!(
1828 self.offset,
1829 "type mismatch: catch_ref label must have one \
1830 more type than tag types",
1831 );
1832 }
1833 for (expected_label_type, actual_tag_param) in
1834 label_types.zip(tag_params.chain([exn_type]))
1835 {
1836 self.match_operand(actual_tag_param, expected_label_type)?;
1837 }
1838 }
1839
1840 Catch::All { label } => {
1841 let (ty, kind) = self.jump(label)?;
1842 if self.label_types(ty, kind)?.len() != 0 {
1843 bail!(
1844 self.offset,
1845 "type mismatch: catch_all label must have no result types"
1846 );
1847 }
1848 }
1849
1850 Catch::AllRef { label } => {
1851 let (ty, kind) = self.jump(label)?;
1852 let mut types = self.label_types(ty, kind)?;
1853 let ty = match (types.next(), types.next()) {
1854 (Some(ty), None) => ty,
1855 _ => {
1856 bail!(
1857 self.offset,
1858 "type mismatch: catch_all_ref label must have \
1859 exactly one result type"
1860 );
1861 }
1862 };
1863 if !self.resources.is_subtype(exn_type, ty) {
1864 bail!(
1865 self.offset,
1866 "type mismatch: catch_all_ref label must a \
1867 subtype of (ref exn)"
1868 );
1869 }
1870 }
1871 }
1872 }
1873 self.push_ctrl(FrameKind::TryTable, ty.ty)?;
1874 Ok(())
1875 }
1876 fn visit_throw(&mut self, index: u32) -> Self::Output {
1877 // Check values associated with the exception.
1878 let ty = self.exception_tag_at(index)?;
1879 for ty in ty.clone().params().iter().rev() {
1880 self.pop_operand(Some(*ty))?;
1881 }
1882 // this should be validated when the tag was defined in the module
1883 debug_assert!(ty.results().is_empty());
1884 self.unreachable()?;
1885 Ok(())
1886 }
1887 fn visit_throw_ref(&mut self) -> Self::Output {
1888 self.pop_operand(Some(ValType::EXNREF))?;
1889 self.unreachable()?;
1890 Ok(())
1891 }
1892 fn visit_end(&mut self) -> Self::Output {
1893 let mut frame = self.pop_ctrl()?;
1894
1895 // Note that this `if` isn't included in the appendix right
1896 // now, but it's used to allow for `if` statements that are
1897 // missing an `else` block which have the same parameter/return
1898 // types on the block (since that's valid).
1899 if frame.kind == FrameKind::If {
1900 self.push_ctrl(FrameKind::Else, frame.block_type)?;
1901 frame = self.pop_ctrl()?;
1902 }
1903 for ty in self.results(frame.block_type)? {
1904 self.push_operand(ty)?;
1905 }
1906
1907 if self.control.is_empty() && self.end_which_emptied_control.is_none() {
1908 assert_ne!(self.offset, 0);
1909 self.end_which_emptied_control = Some(self.offset);
1910 }
1911 Ok(())
1912 }
1913 fn visit_br(&mut self, relative_depth: u32) -> Self::Output {
1914 let (ty, kind) = self.jump(relative_depth)?;
1915 for ty in self.label_types(ty, kind)?.rev() {
1916 self.pop_operand(Some(ty))?;
1917 }
1918 self.unreachable()?;
1919 Ok(())
1920 }
1921 fn visit_br_if(&mut self, relative_depth: u32) -> Self::Output {
1922 self.pop_operand(Some(ValType::I32))?;
1923 let (ty, kind) = self.jump(relative_depth)?;
1924 let label_types = self.label_types(ty, kind)?;
1925 self.pop_push_label_types(label_types)?;
1926 Ok(())
1927 }
1928 fn visit_br_table(&mut self, table: BrTable) -> Self::Output {
1929 self.pop_operand(Some(ValType::I32))?;
1930 let default = self.jump(table.default())?;
1931 let default_types = self.label_types(default.0, default.1)?;
1932 for element in table.targets() {
1933 let relative_depth = element?;
1934 let block = self.jump(relative_depth)?;
1935 let label_tys = self.label_types(block.0, block.1)?;
1936 if label_tys.len() != default_types.len() {
1937 bail!(
1938 self.offset,
1939 "type mismatch: br_table target labels have different number of types"
1940 );
1941 }
1942 self.match_stack_operands(label_tys)?;
1943 }
1944 for ty in default_types.rev() {
1945 self.pop_operand(Some(ty))?;
1946 }
1947 self.unreachable()?;
1948 Ok(())
1949 }
1950 fn visit_return(&mut self) -> Self::Output {
1951 self.check_return()?;
1952 Ok(())
1953 }
1954 fn visit_call(&mut self, function_index: u32) -> Self::Output {
1955 let ty = self.type_of_function(function_index)?;
1956 self.check_call_ty(ty)?;
1957 Ok(())
1958 }
1959 fn visit_return_call(&mut self, function_index: u32) -> Self::Output {
1960 let ty = self.type_of_function(function_index)?;
1961 self.check_return_call_ty(ty)?;
1962 Ok(())
1963 }
1964 fn visit_call_ref(&mut self, type_index: u32) -> Self::Output {
1965 let ty = self.check_call_ref_ty(type_index)?;
1966 self.check_call_ty(ty)?;
1967 Ok(())
1968 }
1969 fn visit_return_call_ref(&mut self, type_index: u32) -> Self::Output {
1970 let ty = self.check_call_ref_ty(type_index)?;
1971 self.check_return_call_ty(ty)?;
1972 Ok(())
1973 }
1974 fn visit_call_indirect(&mut self, type_index: u32, table_index: u32) -> Self::Output {
1975 let ty = self.check_call_indirect_ty(type_index, table_index)?;
1976 self.check_call_ty(ty)?;
1977 Ok(())
1978 }
1979 fn visit_return_call_indirect(&mut self, type_index: u32, table_index: u32) -> Self::Output {
1980 let ty = self.check_call_indirect_ty(type_index, table_index)?;
1981 self.check_return_call_ty(ty)?;
1982 Ok(())
1983 }
1984 fn visit_drop(&mut self) -> Self::Output {
1985 self.pop_operand(None)?;
1986 Ok(())
1987 }
1988 fn visit_select(&mut self) -> Self::Output {
1989 self.pop_operand(Some(ValType::I32))?;
1990 let ty1 = self.pop_operand(None)?;
1991 let ty2 = self.pop_operand(None)?;
1992
1993 let ty = match (ty1, ty2) {
1994 // All heap-related types aren't allowed with the `select`
1995 // instruction
1996 (MaybeType::UnknownRef(..), _)
1997 | (_, MaybeType::UnknownRef(..))
1998 | (MaybeType::Known(ValType::Ref(_)), _)
1999 | (_, MaybeType::Known(ValType::Ref(_))) => {
2000 bail!(
2001 self.offset,
2002 "type mismatch: select only takes integral types"
2003 )
2004 }
2005
2006 // If one operand is the "bottom" type then whatever the other
2007 // operand is is the result of the `select`
2008 (MaybeType::Bottom, t) | (t, MaybeType::Bottom) => t,
2009
2010 // Otherwise these are two integral types and they must match for
2011 // `select` to typecheck.
2012 (t @ MaybeType::Known(t1), MaybeType::Known(t2)) => {
2013 if t1 != t2 {
2014 bail!(
2015 self.offset,
2016 "type mismatch: select operands have different types"
2017 );
2018 }
2019 t
2020 }
2021 };
2022 self.push_operand(ty)?;
2023 Ok(())
2024 }
2025 fn visit_typed_select(&mut self, mut ty: ValType) -> Self::Output {
2026 self.resources
2027 .check_value_type(&mut ty, &self.features, self.offset)?;
2028 self.pop_operand(Some(ValType::I32))?;
2029 self.pop_operand(Some(ty))?;
2030 self.pop_operand(Some(ty))?;
2031 self.push_operand(ty)?;
2032 Ok(())
2033 }
2034 fn visit_local_get(&mut self, local_index: u32) -> Self::Output {
2035 let ty = self.local(local_index)?;
2036 debug_assert_type_indices_are_ids(ty);
2037 if !self.local_inits[local_index as usize] {
2038 bail!(self.offset, "uninitialized local: {}", local_index);
2039 }
2040 self.push_operand(ty)?;
2041 Ok(())
2042 }
2043 fn visit_local_set(&mut self, local_index: u32) -> Self::Output {
2044 let ty = self.local(local_index)?;
2045 self.pop_operand(Some(ty))?;
2046 if !self.local_inits[local_index as usize] {
2047 self.local_inits[local_index as usize] = true;
2048 self.inits.push(local_index);
2049 }
2050 Ok(())
2051 }
2052 fn visit_local_tee(&mut self, local_index: u32) -> Self::Output {
2053 let expected_ty = self.local(local_index)?;
2054 self.pop_operand(Some(expected_ty))?;
2055 if !self.local_inits[local_index as usize] {
2056 self.local_inits[local_index as usize] = true;
2057 self.inits.push(local_index);
2058 }
2059
2060 self.push_operand(expected_ty)?;
2061 Ok(())
2062 }
2063 fn visit_global_get(&mut self, global_index: u32) -> Self::Output {
2064 let ty = self.global_type_at(global_index)?.content_type;
2065 debug_assert_type_indices_are_ids(ty);
2066 self.push_operand(ty)?;
2067 Ok(())
2068 }
2069 fn visit_global_atomic_get(&mut self, _ordering: Ordering, global_index: u32) -> Self::Output {
2070 self.visit_global_get(global_index)?;
2071 // No validation of `ordering` is needed because `global.atomic.get` can
2072 // be used on both shared and unshared globals. But we do need to limit
2073 // which types can be used with this instruction.
2074 let ty = self.global_type_at(global_index)?.content_type;
2075 let supertype = RefType::ANYREF.into();
2076 if !(ty == ValType::I32 || ty == ValType::I64 || self.resources.is_subtype(ty, supertype)) {
2077 bail!(self.offset, "invalid type: `global.atomic.get` only allows `i32`, `i64` and subtypes of `anyref`");
2078 }
2079 Ok(())
2080 }
2081 fn visit_global_set(&mut self, global_index: u32) -> Self::Output {
2082 let ty = self.global_type_at(global_index)?;
2083 if !ty.mutable {
2084 bail!(
2085 self.offset,
2086 "global is immutable: cannot modify it with `global.set`"
2087 );
2088 }
2089 self.pop_operand(Some(ty.content_type))?;
2090 Ok(())
2091 }
2092 fn visit_global_atomic_set(&mut self, _ordering: Ordering, global_index: u32) -> Self::Output {
2093 self.visit_global_set(global_index)?;
2094 // No validation of `ordering` is needed because `global.atomic.get` can
2095 // be used on both shared and unshared globals.
2096 let ty = self.global_type_at(global_index)?.content_type;
2097 let supertype = RefType::ANYREF.into();
2098 if !(ty == ValType::I32 || ty == ValType::I64 || self.resources.is_subtype(ty, supertype)) {
2099 bail!(self.offset, "invalid type: `global.atomic.set` only allows `i32`, `i64` and subtypes of `anyref`");
2100 }
2101 Ok(())
2102 }
2103 fn visit_global_atomic_rmw_add(
2104 &mut self,
2105 _ordering: crate::Ordering,
2106 global_index: u32,
2107 ) -> Self::Output {
2108 let ty = self.check_atomic_global_rmw_ty(global_index)?;
2109 self.check_unary_op(ty)
2110 }
2111 fn visit_global_atomic_rmw_sub(
2112 &mut self,
2113 _ordering: crate::Ordering,
2114 global_index: u32,
2115 ) -> Self::Output {
2116 let ty = self.check_atomic_global_rmw_ty(global_index)?;
2117 self.check_unary_op(ty)
2118 }
2119 fn visit_global_atomic_rmw_and(
2120 &mut self,
2121 _ordering: crate::Ordering,
2122 global_index: u32,
2123 ) -> Self::Output {
2124 let ty = self.check_atomic_global_rmw_ty(global_index)?;
2125 self.check_unary_op(ty)
2126 }
2127 fn visit_global_atomic_rmw_or(
2128 &mut self,
2129 _ordering: crate::Ordering,
2130 global_index: u32,
2131 ) -> Self::Output {
2132 let ty = self.check_atomic_global_rmw_ty(global_index)?;
2133 self.check_unary_op(ty)
2134 }
2135 fn visit_global_atomic_rmw_xor(
2136 &mut self,
2137 _ordering: crate::Ordering,
2138 global_index: u32,
2139 ) -> Self::Output {
2140 let ty = self.check_atomic_global_rmw_ty(global_index)?;
2141 self.check_unary_op(ty)
2142 }
2143 fn visit_global_atomic_rmw_xchg(
2144 &mut self,
2145 _ordering: crate::Ordering,
2146 global_index: u32,
2147 ) -> Self::Output {
2148 let ty = self.global_type_at(global_index)?.content_type;
2149 if !(ty == ValType::I32
2150 || ty == ValType::I64
2151 || self.resources.is_subtype(ty, RefType::ANYREF.into()))
2152 {
2153 bail!(self.offset, "invalid type: `global.atomic.rmw.xchg` only allows `i32`, `i64` and subtypes of `anyref`");
2154 }
2155 self.check_unary_op(ty)
2156 }
2157 fn visit_global_atomic_rmw_cmpxchg(
2158 &mut self,
2159 _ordering: crate::Ordering,
2160 global_index: u32,
2161 ) -> Self::Output {
2162 let ty = self.global_type_at(global_index)?.content_type;
2163 if !(ty == ValType::I32
2164 || ty == ValType::I64
2165 || self.resources.is_subtype(ty, RefType::EQREF.into()))
2166 {
2167 bail!(self.offset, "invalid type: `global.atomic.rmw.cmpxchg` only allows `i32`, `i64` and subtypes of `eqref`");
2168 }
2169 self.check_binary_op(ty)
2170 }
2171
2172 fn visit_i32_load(&mut self, memarg: MemArg) -> Self::Output {
2173 let ty = self.check_memarg(memarg)?;
2174 self.pop_operand(Some(ty))?;
2175 self.push_operand(ValType::I32)?;
2176 Ok(())
2177 }
2178 fn visit_i64_load(&mut self, memarg: MemArg) -> Self::Output {
2179 let ty = self.check_memarg(memarg)?;
2180 self.pop_operand(Some(ty))?;
2181 self.push_operand(ValType::I64)?;
2182 Ok(())
2183 }
2184 fn visit_f32_load(&mut self, memarg: MemArg) -> Self::Output {
2185 self.check_floats_enabled()?;
2186 let ty = self.check_memarg(memarg)?;
2187 self.pop_operand(Some(ty))?;
2188 self.push_operand(ValType::F32)?;
2189 Ok(())
2190 }
2191 fn visit_f64_load(&mut self, memarg: MemArg) -> Self::Output {
2192 self.check_floats_enabled()?;
2193 let ty = self.check_memarg(memarg)?;
2194 self.pop_operand(Some(ty))?;
2195 self.push_operand(ValType::F64)?;
2196 Ok(())
2197 }
2198 fn visit_i32_load8_s(&mut self, memarg: MemArg) -> Self::Output {
2199 let ty = self.check_memarg(memarg)?;
2200 self.pop_operand(Some(ty))?;
2201 self.push_operand(ValType::I32)?;
2202 Ok(())
2203 }
2204 fn visit_i32_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2205 self.visit_i32_load8_s(memarg)
2206 }
2207 fn visit_i32_load16_s(&mut self, memarg: MemArg) -> Self::Output {
2208 let ty = self.check_memarg(memarg)?;
2209 self.pop_operand(Some(ty))?;
2210 self.push_operand(ValType::I32)?;
2211 Ok(())
2212 }
2213 fn visit_i32_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2214 self.visit_i32_load16_s(memarg)
2215 }
2216 fn visit_i64_load8_s(&mut self, memarg: MemArg) -> Self::Output {
2217 let ty = self.check_memarg(memarg)?;
2218 self.pop_operand(Some(ty))?;
2219 self.push_operand(ValType::I64)?;
2220 Ok(())
2221 }
2222 fn visit_i64_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2223 self.visit_i64_load8_s(memarg)
2224 }
2225 fn visit_i64_load16_s(&mut self, memarg: MemArg) -> Self::Output {
2226 let ty = self.check_memarg(memarg)?;
2227 self.pop_operand(Some(ty))?;
2228 self.push_operand(ValType::I64)?;
2229 Ok(())
2230 }
2231 fn visit_i64_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2232 self.visit_i64_load16_s(memarg)
2233 }
2234 fn visit_i64_load32_s(&mut self, memarg: MemArg) -> Self::Output {
2235 let ty = self.check_memarg(memarg)?;
2236 self.pop_operand(Some(ty))?;
2237 self.push_operand(ValType::I64)?;
2238 Ok(())
2239 }
2240 fn visit_i64_load32_u(&mut self, memarg: MemArg) -> Self::Output {
2241 self.visit_i64_load32_s(memarg)
2242 }
2243 fn visit_i32_store(&mut self, memarg: MemArg) -> Self::Output {
2244 let ty = self.check_memarg(memarg)?;
2245 self.pop_operand(Some(ValType::I32))?;
2246 self.pop_operand(Some(ty))?;
2247 Ok(())
2248 }
2249 fn visit_i64_store(&mut self, memarg: MemArg) -> Self::Output {
2250 let ty = self.check_memarg(memarg)?;
2251 self.pop_operand(Some(ValType::I64))?;
2252 self.pop_operand(Some(ty))?;
2253 Ok(())
2254 }
2255 fn visit_f32_store(&mut self, memarg: MemArg) -> Self::Output {
2256 self.check_floats_enabled()?;
2257 let ty = self.check_memarg(memarg)?;
2258 self.pop_operand(Some(ValType::F32))?;
2259 self.pop_operand(Some(ty))?;
2260 Ok(())
2261 }
2262 fn visit_f64_store(&mut self, memarg: MemArg) -> Self::Output {
2263 self.check_floats_enabled()?;
2264 let ty = self.check_memarg(memarg)?;
2265 self.pop_operand(Some(ValType::F64))?;
2266 self.pop_operand(Some(ty))?;
2267 Ok(())
2268 }
2269 fn visit_i32_store8(&mut self, memarg: MemArg) -> Self::Output {
2270 let ty = self.check_memarg(memarg)?;
2271 self.pop_operand(Some(ValType::I32))?;
2272 self.pop_operand(Some(ty))?;
2273 Ok(())
2274 }
2275 fn visit_i32_store16(&mut self, memarg: MemArg) -> Self::Output {
2276 let ty = self.check_memarg(memarg)?;
2277 self.pop_operand(Some(ValType::I32))?;
2278 self.pop_operand(Some(ty))?;
2279 Ok(())
2280 }
2281 fn visit_i64_store8(&mut self, memarg: MemArg) -> Self::Output {
2282 let ty = self.check_memarg(memarg)?;
2283 self.pop_operand(Some(ValType::I64))?;
2284 self.pop_operand(Some(ty))?;
2285 Ok(())
2286 }
2287 fn visit_i64_store16(&mut self, memarg: MemArg) -> Self::Output {
2288 let ty = self.check_memarg(memarg)?;
2289 self.pop_operand(Some(ValType::I64))?;
2290 self.pop_operand(Some(ty))?;
2291 Ok(())
2292 }
2293 fn visit_i64_store32(&mut self, memarg: MemArg) -> Self::Output {
2294 let ty = self.check_memarg(memarg)?;
2295 self.pop_operand(Some(ValType::I64))?;
2296 self.pop_operand(Some(ty))?;
2297 Ok(())
2298 }
2299 fn visit_memory_size(&mut self, mem: u32) -> Self::Output {
2300 let index_ty = self.check_memory_index(mem)?;
2301 self.push_operand(index_ty)?;
2302 Ok(())
2303 }
2304 fn visit_memory_grow(&mut self, mem: u32) -> Self::Output {
2305 let index_ty = self.check_memory_index(mem)?;
2306 self.pop_operand(Some(index_ty))?;
2307 self.push_operand(index_ty)?;
2308 Ok(())
2309 }
2310 fn visit_i32_const(&mut self, _value: i32) -> Self::Output {
2311 self.push_operand(ValType::I32)?;
2312 Ok(())
2313 }
2314 fn visit_i64_const(&mut self, _value: i64) -> Self::Output {
2315 self.push_operand(ValType::I64)?;
2316 Ok(())
2317 }
2318 fn visit_f32_const(&mut self, _value: Ieee32) -> Self::Output {
2319 self.check_floats_enabled()?;
2320 self.push_operand(ValType::F32)?;
2321 Ok(())
2322 }
2323 fn visit_f64_const(&mut self, _value: Ieee64) -> Self::Output {
2324 self.check_floats_enabled()?;
2325 self.push_operand(ValType::F64)?;
2326 Ok(())
2327 }
2328 fn visit_i32_eqz(&mut self) -> Self::Output {
2329 self.pop_operand(Some(ValType::I32))?;
2330 self.push_operand(ValType::I32)?;
2331 Ok(())
2332 }
2333 fn visit_i32_eq(&mut self) -> Self::Output {
2334 self.check_cmp_op(ValType::I32)
2335 }
2336 fn visit_i32_ne(&mut self) -> Self::Output {
2337 self.check_cmp_op(ValType::I32)
2338 }
2339 fn visit_i32_lt_s(&mut self) -> Self::Output {
2340 self.check_cmp_op(ValType::I32)
2341 }
2342 fn visit_i32_lt_u(&mut self) -> Self::Output {
2343 self.check_cmp_op(ValType::I32)
2344 }
2345 fn visit_i32_gt_s(&mut self) -> Self::Output {
2346 self.check_cmp_op(ValType::I32)
2347 }
2348 fn visit_i32_gt_u(&mut self) -> Self::Output {
2349 self.check_cmp_op(ValType::I32)
2350 }
2351 fn visit_i32_le_s(&mut self) -> Self::Output {
2352 self.check_cmp_op(ValType::I32)
2353 }
2354 fn visit_i32_le_u(&mut self) -> Self::Output {
2355 self.check_cmp_op(ValType::I32)
2356 }
2357 fn visit_i32_ge_s(&mut self) -> Self::Output {
2358 self.check_cmp_op(ValType::I32)
2359 }
2360 fn visit_i32_ge_u(&mut self) -> Self::Output {
2361 self.check_cmp_op(ValType::I32)
2362 }
2363 fn visit_i64_eqz(&mut self) -> Self::Output {
2364 self.pop_operand(Some(ValType::I64))?;
2365 self.push_operand(ValType::I32)?;
2366 Ok(())
2367 }
2368 fn visit_i64_eq(&mut self) -> Self::Output {
2369 self.check_cmp_op(ValType::I64)
2370 }
2371 fn visit_i64_ne(&mut self) -> Self::Output {
2372 self.check_cmp_op(ValType::I64)
2373 }
2374 fn visit_i64_lt_s(&mut self) -> Self::Output {
2375 self.check_cmp_op(ValType::I64)
2376 }
2377 fn visit_i64_lt_u(&mut self) -> Self::Output {
2378 self.check_cmp_op(ValType::I64)
2379 }
2380 fn visit_i64_gt_s(&mut self) -> Self::Output {
2381 self.check_cmp_op(ValType::I64)
2382 }
2383 fn visit_i64_gt_u(&mut self) -> Self::Output {
2384 self.check_cmp_op(ValType::I64)
2385 }
2386 fn visit_i64_le_s(&mut self) -> Self::Output {
2387 self.check_cmp_op(ValType::I64)
2388 }
2389 fn visit_i64_le_u(&mut self) -> Self::Output {
2390 self.check_cmp_op(ValType::I64)
2391 }
2392 fn visit_i64_ge_s(&mut self) -> Self::Output {
2393 self.check_cmp_op(ValType::I64)
2394 }
2395 fn visit_i64_ge_u(&mut self) -> Self::Output {
2396 self.check_cmp_op(ValType::I64)
2397 }
2398 fn visit_f32_eq(&mut self) -> Self::Output {
2399 self.check_fcmp_op(ValType::F32)
2400 }
2401 fn visit_f32_ne(&mut self) -> Self::Output {
2402 self.check_fcmp_op(ValType::F32)
2403 }
2404 fn visit_f32_lt(&mut self) -> Self::Output {
2405 self.check_fcmp_op(ValType::F32)
2406 }
2407 fn visit_f32_gt(&mut self) -> Self::Output {
2408 self.check_fcmp_op(ValType::F32)
2409 }
2410 fn visit_f32_le(&mut self) -> Self::Output {
2411 self.check_fcmp_op(ValType::F32)
2412 }
2413 fn visit_f32_ge(&mut self) -> Self::Output {
2414 self.check_fcmp_op(ValType::F32)
2415 }
2416 fn visit_f64_eq(&mut self) -> Self::Output {
2417 self.check_fcmp_op(ValType::F64)
2418 }
2419 fn visit_f64_ne(&mut self) -> Self::Output {
2420 self.check_fcmp_op(ValType::F64)
2421 }
2422 fn visit_f64_lt(&mut self) -> Self::Output {
2423 self.check_fcmp_op(ValType::F64)
2424 }
2425 fn visit_f64_gt(&mut self) -> Self::Output {
2426 self.check_fcmp_op(ValType::F64)
2427 }
2428 fn visit_f64_le(&mut self) -> Self::Output {
2429 self.check_fcmp_op(ValType::F64)
2430 }
2431 fn visit_f64_ge(&mut self) -> Self::Output {
2432 self.check_fcmp_op(ValType::F64)
2433 }
2434 fn visit_i32_clz(&mut self) -> Self::Output {
2435 self.check_unary_op(ValType::I32)
2436 }
2437 fn visit_i32_ctz(&mut self) -> Self::Output {
2438 self.check_unary_op(ValType::I32)
2439 }
2440 fn visit_i32_popcnt(&mut self) -> Self::Output {
2441 self.check_unary_op(ValType::I32)
2442 }
2443 fn visit_i32_add(&mut self) -> Self::Output {
2444 self.check_binary_op(ValType::I32)
2445 }
2446 fn visit_i32_sub(&mut self) -> Self::Output {
2447 self.check_binary_op(ValType::I32)
2448 }
2449 fn visit_i32_mul(&mut self) -> Self::Output {
2450 self.check_binary_op(ValType::I32)
2451 }
2452 fn visit_i32_div_s(&mut self) -> Self::Output {
2453 self.check_binary_op(ValType::I32)
2454 }
2455 fn visit_i32_div_u(&mut self) -> Self::Output {
2456 self.check_binary_op(ValType::I32)
2457 }
2458 fn visit_i32_rem_s(&mut self) -> Self::Output {
2459 self.check_binary_op(ValType::I32)
2460 }
2461 fn visit_i32_rem_u(&mut self) -> Self::Output {
2462 self.check_binary_op(ValType::I32)
2463 }
2464 fn visit_i32_and(&mut self) -> Self::Output {
2465 self.check_binary_op(ValType::I32)
2466 }
2467 fn visit_i32_or(&mut self) -> Self::Output {
2468 self.check_binary_op(ValType::I32)
2469 }
2470 fn visit_i32_xor(&mut self) -> Self::Output {
2471 self.check_binary_op(ValType::I32)
2472 }
2473 fn visit_i32_shl(&mut self) -> Self::Output {
2474 self.check_binary_op(ValType::I32)
2475 }
2476 fn visit_i32_shr_s(&mut self) -> Self::Output {
2477 self.check_binary_op(ValType::I32)
2478 }
2479 fn visit_i32_shr_u(&mut self) -> Self::Output {
2480 self.check_binary_op(ValType::I32)
2481 }
2482 fn visit_i32_rotl(&mut self) -> Self::Output {
2483 self.check_binary_op(ValType::I32)
2484 }
2485 fn visit_i32_rotr(&mut self) -> Self::Output {
2486 self.check_binary_op(ValType::I32)
2487 }
2488 fn visit_i64_clz(&mut self) -> Self::Output {
2489 self.check_unary_op(ValType::I64)
2490 }
2491 fn visit_i64_ctz(&mut self) -> Self::Output {
2492 self.check_unary_op(ValType::I64)
2493 }
2494 fn visit_i64_popcnt(&mut self) -> Self::Output {
2495 self.check_unary_op(ValType::I64)
2496 }
2497 fn visit_i64_add(&mut self) -> Self::Output {
2498 self.check_binary_op(ValType::I64)
2499 }
2500 fn visit_i64_sub(&mut self) -> Self::Output {
2501 self.check_binary_op(ValType::I64)
2502 }
2503 fn visit_i64_mul(&mut self) -> Self::Output {
2504 self.check_binary_op(ValType::I64)
2505 }
2506 fn visit_i64_div_s(&mut self) -> Self::Output {
2507 self.check_binary_op(ValType::I64)
2508 }
2509 fn visit_i64_div_u(&mut self) -> Self::Output {
2510 self.check_binary_op(ValType::I64)
2511 }
2512 fn visit_i64_rem_s(&mut self) -> Self::Output {
2513 self.check_binary_op(ValType::I64)
2514 }
2515 fn visit_i64_rem_u(&mut self) -> Self::Output {
2516 self.check_binary_op(ValType::I64)
2517 }
2518 fn visit_i64_and(&mut self) -> Self::Output {
2519 self.check_binary_op(ValType::I64)
2520 }
2521 fn visit_i64_or(&mut self) -> Self::Output {
2522 self.check_binary_op(ValType::I64)
2523 }
2524 fn visit_i64_xor(&mut self) -> Self::Output {
2525 self.check_binary_op(ValType::I64)
2526 }
2527 fn visit_i64_shl(&mut self) -> Self::Output {
2528 self.check_binary_op(ValType::I64)
2529 }
2530 fn visit_i64_shr_s(&mut self) -> Self::Output {
2531 self.check_binary_op(ValType::I64)
2532 }
2533 fn visit_i64_shr_u(&mut self) -> Self::Output {
2534 self.check_binary_op(ValType::I64)
2535 }
2536 fn visit_i64_rotl(&mut self) -> Self::Output {
2537 self.check_binary_op(ValType::I64)
2538 }
2539 fn visit_i64_rotr(&mut self) -> Self::Output {
2540 self.check_binary_op(ValType::I64)
2541 }
2542 fn visit_f32_abs(&mut self) -> Self::Output {
2543 self.check_funary_op(ValType::F32)
2544 }
2545 fn visit_f32_neg(&mut self) -> Self::Output {
2546 self.check_funary_op(ValType::F32)
2547 }
2548 fn visit_f32_ceil(&mut self) -> Self::Output {
2549 self.check_funary_op(ValType::F32)
2550 }
2551 fn visit_f32_floor(&mut self) -> Self::Output {
2552 self.check_funary_op(ValType::F32)
2553 }
2554 fn visit_f32_trunc(&mut self) -> Self::Output {
2555 self.check_funary_op(ValType::F32)
2556 }
2557 fn visit_f32_nearest(&mut self) -> Self::Output {
2558 self.check_funary_op(ValType::F32)
2559 }
2560 fn visit_f32_sqrt(&mut self) -> Self::Output {
2561 self.check_funary_op(ValType::F32)
2562 }
2563 fn visit_f32_add(&mut self) -> Self::Output {
2564 self.check_fbinary_op(ValType::F32)
2565 }
2566 fn visit_f32_sub(&mut self) -> Self::Output {
2567 self.check_fbinary_op(ValType::F32)
2568 }
2569 fn visit_f32_mul(&mut self) -> Self::Output {
2570 self.check_fbinary_op(ValType::F32)
2571 }
2572 fn visit_f32_div(&mut self) -> Self::Output {
2573 self.check_fbinary_op(ValType::F32)
2574 }
2575 fn visit_f32_min(&mut self) -> Self::Output {
2576 self.check_fbinary_op(ValType::F32)
2577 }
2578 fn visit_f32_max(&mut self) -> Self::Output {
2579 self.check_fbinary_op(ValType::F32)
2580 }
2581 fn visit_f32_copysign(&mut self) -> Self::Output {
2582 self.check_fbinary_op(ValType::F32)
2583 }
2584 fn visit_f64_abs(&mut self) -> Self::Output {
2585 self.check_funary_op(ValType::F64)
2586 }
2587 fn visit_f64_neg(&mut self) -> Self::Output {
2588 self.check_funary_op(ValType::F64)
2589 }
2590 fn visit_f64_ceil(&mut self) -> Self::Output {
2591 self.check_funary_op(ValType::F64)
2592 }
2593 fn visit_f64_floor(&mut self) -> Self::Output {
2594 self.check_funary_op(ValType::F64)
2595 }
2596 fn visit_f64_trunc(&mut self) -> Self::Output {
2597 self.check_funary_op(ValType::F64)
2598 }
2599 fn visit_f64_nearest(&mut self) -> Self::Output {
2600 self.check_funary_op(ValType::F64)
2601 }
2602 fn visit_f64_sqrt(&mut self) -> Self::Output {
2603 self.check_funary_op(ValType::F64)
2604 }
2605 fn visit_f64_add(&mut self) -> Self::Output {
2606 self.check_fbinary_op(ValType::F64)
2607 }
2608 fn visit_f64_sub(&mut self) -> Self::Output {
2609 self.check_fbinary_op(ValType::F64)
2610 }
2611 fn visit_f64_mul(&mut self) -> Self::Output {
2612 self.check_fbinary_op(ValType::F64)
2613 }
2614 fn visit_f64_div(&mut self) -> Self::Output {
2615 self.check_fbinary_op(ValType::F64)
2616 }
2617 fn visit_f64_min(&mut self) -> Self::Output {
2618 self.check_fbinary_op(ValType::F64)
2619 }
2620 fn visit_f64_max(&mut self) -> Self::Output {
2621 self.check_fbinary_op(ValType::F64)
2622 }
2623 fn visit_f64_copysign(&mut self) -> Self::Output {
2624 self.check_fbinary_op(ValType::F64)
2625 }
2626 fn visit_i32_wrap_i64(&mut self) -> Self::Output {
2627 self.check_conversion_op(ValType::I32, ValType::I64)
2628 }
2629 fn visit_i32_trunc_f32_s(&mut self) -> Self::Output {
2630 self.check_conversion_op(ValType::I32, ValType::F32)
2631 }
2632 fn visit_i32_trunc_f32_u(&mut self) -> Self::Output {
2633 self.check_conversion_op(ValType::I32, ValType::F32)
2634 }
2635 fn visit_i32_trunc_f64_s(&mut self) -> Self::Output {
2636 self.check_conversion_op(ValType::I32, ValType::F64)
2637 }
2638 fn visit_i32_trunc_f64_u(&mut self) -> Self::Output {
2639 self.check_conversion_op(ValType::I32, ValType::F64)
2640 }
2641 fn visit_i64_extend_i32_s(&mut self) -> Self::Output {
2642 self.check_conversion_op(ValType::I64, ValType::I32)
2643 }
2644 fn visit_i64_extend_i32_u(&mut self) -> Self::Output {
2645 self.check_conversion_op(ValType::I64, ValType::I32)
2646 }
2647 fn visit_i64_trunc_f32_s(&mut self) -> Self::Output {
2648 self.check_conversion_op(ValType::I64, ValType::F32)
2649 }
2650 fn visit_i64_trunc_f32_u(&mut self) -> Self::Output {
2651 self.check_conversion_op(ValType::I64, ValType::F32)
2652 }
2653 fn visit_i64_trunc_f64_s(&mut self) -> Self::Output {
2654 self.check_conversion_op(ValType::I64, ValType::F64)
2655 }
2656 fn visit_i64_trunc_f64_u(&mut self) -> Self::Output {
2657 self.check_conversion_op(ValType::I64, ValType::F64)
2658 }
2659 fn visit_f32_convert_i32_s(&mut self) -> Self::Output {
2660 self.check_fconversion_op(ValType::F32, ValType::I32)
2661 }
2662 fn visit_f32_convert_i32_u(&mut self) -> Self::Output {
2663 self.check_fconversion_op(ValType::F32, ValType::I32)
2664 }
2665 fn visit_f32_convert_i64_s(&mut self) -> Self::Output {
2666 self.check_fconversion_op(ValType::F32, ValType::I64)
2667 }
2668 fn visit_f32_convert_i64_u(&mut self) -> Self::Output {
2669 self.check_fconversion_op(ValType::F32, ValType::I64)
2670 }
2671 fn visit_f32_demote_f64(&mut self) -> Self::Output {
2672 self.check_fconversion_op(ValType::F32, ValType::F64)
2673 }
2674 fn visit_f64_convert_i32_s(&mut self) -> Self::Output {
2675 self.check_fconversion_op(ValType::F64, ValType::I32)
2676 }
2677 fn visit_f64_convert_i32_u(&mut self) -> Self::Output {
2678 self.check_fconversion_op(ValType::F64, ValType::I32)
2679 }
2680 fn visit_f64_convert_i64_s(&mut self) -> Self::Output {
2681 self.check_fconversion_op(ValType::F64, ValType::I64)
2682 }
2683 fn visit_f64_convert_i64_u(&mut self) -> Self::Output {
2684 self.check_fconversion_op(ValType::F64, ValType::I64)
2685 }
2686 fn visit_f64_promote_f32(&mut self) -> Self::Output {
2687 self.check_fconversion_op(ValType::F64, ValType::F32)
2688 }
2689 fn visit_i32_reinterpret_f32(&mut self) -> Self::Output {
2690 self.check_conversion_op(ValType::I32, ValType::F32)
2691 }
2692 fn visit_i64_reinterpret_f64(&mut self) -> Self::Output {
2693 self.check_conversion_op(ValType::I64, ValType::F64)
2694 }
2695 fn visit_f32_reinterpret_i32(&mut self) -> Self::Output {
2696 self.check_fconversion_op(ValType::F32, ValType::I32)
2697 }
2698 fn visit_f64_reinterpret_i64(&mut self) -> Self::Output {
2699 self.check_fconversion_op(ValType::F64, ValType::I64)
2700 }
2701 fn visit_i32_trunc_sat_f32_s(&mut self) -> Self::Output {
2702 self.check_conversion_op(ValType::I32, ValType::F32)
2703 }
2704 fn visit_i32_trunc_sat_f32_u(&mut self) -> Self::Output {
2705 self.check_conversion_op(ValType::I32, ValType::F32)
2706 }
2707 fn visit_i32_trunc_sat_f64_s(&mut self) -> Self::Output {
2708 self.check_conversion_op(ValType::I32, ValType::F64)
2709 }
2710 fn visit_i32_trunc_sat_f64_u(&mut self) -> Self::Output {
2711 self.check_conversion_op(ValType::I32, ValType::F64)
2712 }
2713 fn visit_i64_trunc_sat_f32_s(&mut self) -> Self::Output {
2714 self.check_conversion_op(ValType::I64, ValType::F32)
2715 }
2716 fn visit_i64_trunc_sat_f32_u(&mut self) -> Self::Output {
2717 self.check_conversion_op(ValType::I64, ValType::F32)
2718 }
2719 fn visit_i64_trunc_sat_f64_s(&mut self) -> Self::Output {
2720 self.check_conversion_op(ValType::I64, ValType::F64)
2721 }
2722 fn visit_i64_trunc_sat_f64_u(&mut self) -> Self::Output {
2723 self.check_conversion_op(ValType::I64, ValType::F64)
2724 }
2725 fn visit_i32_extend8_s(&mut self) -> Self::Output {
2726 self.check_unary_op(ValType::I32)
2727 }
2728 fn visit_i32_extend16_s(&mut self) -> Self::Output {
2729 self.check_unary_op(ValType::I32)
2730 }
2731 fn visit_i64_extend8_s(&mut self) -> Self::Output {
2732 self.check_unary_op(ValType::I64)
2733 }
2734 fn visit_i64_extend16_s(&mut self) -> Self::Output {
2735 self.check_unary_op(ValType::I64)
2736 }
2737 fn visit_i64_extend32_s(&mut self) -> Self::Output {
2738 self.check_unary_op(ValType::I64)
2739 }
2740 fn visit_i32_atomic_load(&mut self, memarg: MemArg) -> Self::Output {
2741 self.check_atomic_load(memarg, ValType::I32)
2742 }
2743 fn visit_i32_atomic_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2744 self.check_atomic_load(memarg, ValType::I32)
2745 }
2746 fn visit_i32_atomic_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2747 self.check_atomic_load(memarg, ValType::I32)
2748 }
2749 fn visit_i64_atomic_load(&mut self, memarg: MemArg) -> Self::Output {
2750 self.check_atomic_load(memarg, ValType::I64)
2751 }
2752 fn visit_i64_atomic_load32_u(&mut self, memarg: MemArg) -> Self::Output {
2753 self.check_atomic_load(memarg, ValType::I64)
2754 }
2755 fn visit_i64_atomic_load16_u(&mut self, memarg: MemArg) -> Self::Output {
2756 self.check_atomic_load(memarg, ValType::I64)
2757 }
2758 fn visit_i64_atomic_load8_u(&mut self, memarg: MemArg) -> Self::Output {
2759 self.check_atomic_load(memarg, ValType::I64)
2760 }
2761 fn visit_i32_atomic_store(&mut self, memarg: MemArg) -> Self::Output {
2762 self.check_atomic_store(memarg, ValType::I32)
2763 }
2764 fn visit_i32_atomic_store16(&mut self, memarg: MemArg) -> Self::Output {
2765 self.check_atomic_store(memarg, ValType::I32)
2766 }
2767 fn visit_i32_atomic_store8(&mut self, memarg: MemArg) -> Self::Output {
2768 self.check_atomic_store(memarg, ValType::I32)
2769 }
2770 fn visit_i64_atomic_store(&mut self, memarg: MemArg) -> Self::Output {
2771 self.check_atomic_store(memarg, ValType::I64)
2772 }
2773 fn visit_i64_atomic_store32(&mut self, memarg: MemArg) -> Self::Output {
2774 self.check_atomic_store(memarg, ValType::I64)
2775 }
2776 fn visit_i64_atomic_store16(&mut self, memarg: MemArg) -> Self::Output {
2777 self.check_atomic_store(memarg, ValType::I64)
2778 }
2779 fn visit_i64_atomic_store8(&mut self, memarg: MemArg) -> Self::Output {
2780 self.check_atomic_store(memarg, ValType::I64)
2781 }
2782 fn visit_i32_atomic_rmw_add(&mut self, memarg: MemArg) -> Self::Output {
2783 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2784 }
2785 fn visit_i32_atomic_rmw_sub(&mut self, memarg: MemArg) -> Self::Output {
2786 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2787 }
2788 fn visit_i32_atomic_rmw_and(&mut self, memarg: MemArg) -> Self::Output {
2789 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2790 }
2791 fn visit_i32_atomic_rmw_or(&mut self, memarg: MemArg) -> Self::Output {
2792 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2793 }
2794 fn visit_i32_atomic_rmw_xor(&mut self, memarg: MemArg) -> Self::Output {
2795 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2796 }
2797 fn visit_i32_atomic_rmw16_add_u(&mut self, memarg: MemArg) -> Self::Output {
2798 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2799 }
2800 fn visit_i32_atomic_rmw16_sub_u(&mut self, memarg: MemArg) -> Self::Output {
2801 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2802 }
2803 fn visit_i32_atomic_rmw16_and_u(&mut self, memarg: MemArg) -> Self::Output {
2804 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2805 }
2806 fn visit_i32_atomic_rmw16_or_u(&mut self, memarg: MemArg) -> Self::Output {
2807 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2808 }
2809 fn visit_i32_atomic_rmw16_xor_u(&mut self, memarg: MemArg) -> Self::Output {
2810 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2811 }
2812 fn visit_i32_atomic_rmw8_add_u(&mut self, memarg: MemArg) -> Self::Output {
2813 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2814 }
2815 fn visit_i32_atomic_rmw8_sub_u(&mut self, memarg: MemArg) -> Self::Output {
2816 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2817 }
2818 fn visit_i32_atomic_rmw8_and_u(&mut self, memarg: MemArg) -> Self::Output {
2819 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2820 }
2821 fn visit_i32_atomic_rmw8_or_u(&mut self, memarg: MemArg) -> Self::Output {
2822 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2823 }
2824 fn visit_i32_atomic_rmw8_xor_u(&mut self, memarg: MemArg) -> Self::Output {
2825 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2826 }
2827 fn visit_i64_atomic_rmw_add(&mut self, memarg: MemArg) -> Self::Output {
2828 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2829 }
2830 fn visit_i64_atomic_rmw_sub(&mut self, memarg: MemArg) -> Self::Output {
2831 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2832 }
2833 fn visit_i64_atomic_rmw_and(&mut self, memarg: MemArg) -> Self::Output {
2834 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2835 }
2836 fn visit_i64_atomic_rmw_or(&mut self, memarg: MemArg) -> Self::Output {
2837 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2838 }
2839 fn visit_i64_atomic_rmw_xor(&mut self, memarg: MemArg) -> Self::Output {
2840 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2841 }
2842 fn visit_i64_atomic_rmw32_add_u(&mut self, memarg: MemArg) -> Self::Output {
2843 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2844 }
2845 fn visit_i64_atomic_rmw32_sub_u(&mut self, memarg: MemArg) -> Self::Output {
2846 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2847 }
2848 fn visit_i64_atomic_rmw32_and_u(&mut self, memarg: MemArg) -> Self::Output {
2849 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2850 }
2851 fn visit_i64_atomic_rmw32_or_u(&mut self, memarg: MemArg) -> Self::Output {
2852 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2853 }
2854 fn visit_i64_atomic_rmw32_xor_u(&mut self, memarg: MemArg) -> Self::Output {
2855 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2856 }
2857 fn visit_i64_atomic_rmw16_add_u(&mut self, memarg: MemArg) -> Self::Output {
2858 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2859 }
2860 fn visit_i64_atomic_rmw16_sub_u(&mut self, memarg: MemArg) -> Self::Output {
2861 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2862 }
2863 fn visit_i64_atomic_rmw16_and_u(&mut self, memarg: MemArg) -> Self::Output {
2864 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2865 }
2866 fn visit_i64_atomic_rmw16_or_u(&mut self, memarg: MemArg) -> Self::Output {
2867 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2868 }
2869 fn visit_i64_atomic_rmw16_xor_u(&mut self, memarg: MemArg) -> Self::Output {
2870 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2871 }
2872 fn visit_i64_atomic_rmw8_add_u(&mut self, memarg: MemArg) -> Self::Output {
2873 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2874 }
2875 fn visit_i64_atomic_rmw8_sub_u(&mut self, memarg: MemArg) -> Self::Output {
2876 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2877 }
2878 fn visit_i64_atomic_rmw8_and_u(&mut self, memarg: MemArg) -> Self::Output {
2879 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2880 }
2881 fn visit_i64_atomic_rmw8_or_u(&mut self, memarg: MemArg) -> Self::Output {
2882 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2883 }
2884 fn visit_i64_atomic_rmw8_xor_u(&mut self, memarg: MemArg) -> Self::Output {
2885 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2886 }
2887 fn visit_i32_atomic_rmw_xchg(&mut self, memarg: MemArg) -> Self::Output {
2888 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2889 }
2890 fn visit_i32_atomic_rmw16_xchg_u(&mut self, memarg: MemArg) -> Self::Output {
2891 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2892 }
2893 fn visit_i32_atomic_rmw8_xchg_u(&mut self, memarg: MemArg) -> Self::Output {
2894 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2895 }
2896 fn visit_i32_atomic_rmw_cmpxchg(&mut self, memarg: MemArg) -> Self::Output {
2897 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I32)
2898 }
2899 fn visit_i32_atomic_rmw16_cmpxchg_u(&mut self, memarg: MemArg) -> Self::Output {
2900 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I32)
2901 }
2902 fn visit_i32_atomic_rmw8_cmpxchg_u(&mut self, memarg: MemArg) -> Self::Output {
2903 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I32)
2904 }
2905 fn visit_i64_atomic_rmw_xchg(&mut self, memarg: MemArg) -> Self::Output {
2906 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2907 }
2908 fn visit_i64_atomic_rmw32_xchg_u(&mut self, memarg: MemArg) -> Self::Output {
2909 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2910 }
2911 fn visit_i64_atomic_rmw16_xchg_u(&mut self, memarg: MemArg) -> Self::Output {
2912 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2913 }
2914 fn visit_i64_atomic_rmw8_xchg_u(&mut self, memarg: MemArg) -> Self::Output {
2915 self.check_atomic_binary_memory_op(memarg, ValType::I64)
2916 }
2917 fn visit_i64_atomic_rmw_cmpxchg(&mut self, memarg: MemArg) -> Self::Output {
2918 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I64)
2919 }
2920 fn visit_i64_atomic_rmw32_cmpxchg_u(&mut self, memarg: MemArg) -> Self::Output {
2921 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I64)
2922 }
2923 fn visit_i64_atomic_rmw16_cmpxchg_u(&mut self, memarg: MemArg) -> Self::Output {
2924 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I64)
2925 }
2926 fn visit_i64_atomic_rmw8_cmpxchg_u(&mut self, memarg: MemArg) -> Self::Output {
2927 self.check_atomic_binary_memory_cmpxchg(memarg, ValType::I64)
2928 }
2929 fn visit_memory_atomic_notify(&mut self, memarg: MemArg) -> Self::Output {
2930 self.check_atomic_binary_memory_op(memarg, ValType::I32)
2931 }
2932 fn visit_memory_atomic_wait32(&mut self, memarg: MemArg) -> Self::Output {
2933 let ty = self.check_shared_memarg(memarg)?;
2934 self.pop_operand(Some(ValType::I64))?;
2935 self.pop_operand(Some(ValType::I32))?;
2936 self.pop_operand(Some(ty))?;
2937 self.push_operand(ValType::I32)?;
2938 Ok(())
2939 }
2940 fn visit_memory_atomic_wait64(&mut self, memarg: MemArg) -> Self::Output {
2941 let ty = self.check_shared_memarg(memarg)?;
2942 self.pop_operand(Some(ValType::I64))?;
2943 self.pop_operand(Some(ValType::I64))?;
2944 self.pop_operand(Some(ty))?;
2945 self.push_operand(ValType::I32)?;
2946 Ok(())
2947 }
2948 fn visit_atomic_fence(&mut self) -> Self::Output {
2949 Ok(())
2950 }
2951 fn visit_ref_null(&mut self, mut heap_type: HeapType) -> Self::Output {
2952 if let Some(ty) = RefType::new(true, heap_type) {
2953 self.features
2954 .check_ref_type(ty)
2955 .map_err(|e| BinaryReaderError::new(e, self.offset))?;
2956 }
2957 self.resources
2958 .check_heap_type(&mut heap_type, self.offset)?;
2959 let ty = ValType::Ref(
2960 RefType::new(true, heap_type).expect("existing heap types should be within our limits"),
2961 );
2962 self.push_operand(ty)?;
2963 Ok(())
2964 }
2965
2966 fn visit_ref_as_non_null(&mut self) -> Self::Output {
2967 let ty = self.pop_ref(None)?.as_non_null();
2968 self.push_operand(ty)?;
2969 Ok(())
2970 }
2971 fn visit_br_on_null(&mut self, relative_depth: u32) -> Self::Output {
2972 let ref_ty = self.pop_ref(None)?.as_non_null();
2973 let (ft, kind) = self.jump(relative_depth)?;
2974 let label_types = self.label_types(ft, kind)?;
2975 self.pop_push_label_types(label_types)?;
2976 self.push_operand(ref_ty)?;
2977 Ok(())
2978 }
2979 fn visit_br_on_non_null(&mut self, relative_depth: u32) -> Self::Output {
2980 let (ft, kind) = self.jump(relative_depth)?;
2981
2982 let mut label_types = self.label_types(ft, kind)?;
2983 let expected = match label_types.next_back() {
2984 None => bail!(
2985 self.offset,
2986 "type mismatch: br_on_non_null target has no label types",
2987 ),
2988 Some(ValType::Ref(ty)) => ty,
2989 Some(_) => bail!(
2990 self.offset,
2991 "type mismatch: br_on_non_null target does not end with heap type",
2992 ),
2993 };
2994 self.pop_ref(Some(expected.nullable()))?;
2995
2996 self.pop_push_label_types(label_types)?;
2997 Ok(())
2998 }
2999 fn visit_ref_is_null(&mut self) -> Self::Output {
3000 self.pop_ref(None)?;
3001 self.push_operand(ValType::I32)?;
3002 Ok(())
3003 }
3004 fn visit_ref_func(&mut self, function_index: u32) -> Self::Output {
3005 let type_id = match self.resources.type_id_of_function(function_index) {
3006 Some(id) => id,
3007 None => bail!(
3008 self.offset,
3009 "unknown function {}: function index out of bounds",
3010 function_index,
3011 ),
3012 };
3013 if !self.resources.is_function_referenced(function_index) {
3014 bail!(self.offset, "undeclared function reference");
3015 }
3016
3017 let index = UnpackedIndex::Id(type_id);
3018 let ty = ValType::Ref(
3019 RefType::new(false, HeapType::Concrete(index)).ok_or_else(|| {
3020 BinaryReaderError::new("implementation limit: type index too large", self.offset)
3021 })?,
3022 );
3023 self.push_operand(ty)?;
3024 Ok(())
3025 }
3026 fn visit_ref_eq(&mut self) -> Self::Output {
3027 let a = self.pop_maybe_shared_ref(AbstractHeapType::Eq)?;
3028 let b = self.pop_maybe_shared_ref(AbstractHeapType::Eq)?;
3029 let a_is_shared = a.is_maybe_shared(&self.resources);
3030 let b_is_shared = b.is_maybe_shared(&self.resources);
3031 match (a_is_shared, b_is_shared) {
3032 // One or both of the types are from unreachable code; assume
3033 // the shared-ness matches.
3034 (None, Some(_)) | (Some(_), None) | (None, None) => {}
3035
3036 (Some(is_a_shared), Some(is_b_shared)) => {
3037 if is_a_shared != is_b_shared {
3038 bail!(
3039 self.offset,
3040 "type mismatch: expected `ref.eq` types to match `shared`-ness"
3041 );
3042 }
3043 }
3044 }
3045 self.push_operand(ValType::I32)
3046 }
3047 fn visit_v128_load(&mut self, memarg: MemArg) -> Self::Output {
3048 let ty = self.check_memarg(memarg)?;
3049 self.pop_operand(Some(ty))?;
3050 self.push_operand(ValType::V128)?;
3051 Ok(())
3052 }
3053 fn visit_v128_store(&mut self, memarg: MemArg) -> Self::Output {
3054 let ty = self.check_memarg(memarg)?;
3055 self.pop_operand(Some(ValType::V128))?;
3056 self.pop_operand(Some(ty))?;
3057 Ok(())
3058 }
3059 fn visit_v128_const(&mut self, _value: V128) -> Self::Output {
3060 self.push_operand(ValType::V128)?;
3061 Ok(())
3062 }
3063 fn visit_i8x16_splat(&mut self) -> Self::Output {
3064 self.check_v128_splat(ValType::I32)
3065 }
3066 fn visit_i16x8_splat(&mut self) -> Self::Output {
3067 self.check_v128_splat(ValType::I32)
3068 }
3069 fn visit_i32x4_splat(&mut self) -> Self::Output {
3070 self.check_v128_splat(ValType::I32)
3071 }
3072 fn visit_i64x2_splat(&mut self) -> Self::Output {
3073 self.check_v128_splat(ValType::I64)
3074 }
3075 fn visit_f32x4_splat(&mut self) -> Self::Output {
3076 self.check_floats_enabled()?;
3077 self.check_v128_splat(ValType::F32)
3078 }
3079 fn visit_f64x2_splat(&mut self) -> Self::Output {
3080 self.check_floats_enabled()?;
3081 self.check_v128_splat(ValType::F64)
3082 }
3083 fn visit_i8x16_extract_lane_s(&mut self, lane: u8) -> Self::Output {
3084 self.check_simd_lane_index(lane, 16)?;
3085 self.pop_operand(Some(ValType::V128))?;
3086 self.push_operand(ValType::I32)?;
3087 Ok(())
3088 }
3089 fn visit_i8x16_extract_lane_u(&mut self, lane: u8) -> Self::Output {
3090 self.visit_i8x16_extract_lane_s(lane)
3091 }
3092 fn visit_i16x8_extract_lane_s(&mut self, lane: u8) -> Self::Output {
3093 self.check_simd_lane_index(lane, 8)?;
3094 self.pop_operand(Some(ValType::V128))?;
3095 self.push_operand(ValType::I32)?;
3096 Ok(())
3097 }
3098 fn visit_i16x8_extract_lane_u(&mut self, lane: u8) -> Self::Output {
3099 self.visit_i16x8_extract_lane_s(lane)
3100 }
3101 fn visit_i32x4_extract_lane(&mut self, lane: u8) -> Self::Output {
3102 self.check_simd_lane_index(lane, 4)?;
3103 self.pop_operand(Some(ValType::V128))?;
3104 self.push_operand(ValType::I32)?;
3105 Ok(())
3106 }
3107 fn visit_i8x16_replace_lane(&mut self, lane: u8) -> Self::Output {
3108 self.check_simd_lane_index(lane, 16)?;
3109 self.pop_operand(Some(ValType::I32))?;
3110 self.pop_operand(Some(ValType::V128))?;
3111 self.push_operand(ValType::V128)?;
3112 Ok(())
3113 }
3114 fn visit_i16x8_replace_lane(&mut self, lane: u8) -> Self::Output {
3115 self.check_simd_lane_index(lane, 8)?;
3116 self.pop_operand(Some(ValType::I32))?;
3117 self.pop_operand(Some(ValType::V128))?;
3118 self.push_operand(ValType::V128)?;
3119 Ok(())
3120 }
3121 fn visit_i32x4_replace_lane(&mut self, lane: u8) -> Self::Output {
3122 self.check_simd_lane_index(lane, 4)?;
3123 self.pop_operand(Some(ValType::I32))?;
3124 self.pop_operand(Some(ValType::V128))?;
3125 self.push_operand(ValType::V128)?;
3126 Ok(())
3127 }
3128 fn visit_i64x2_extract_lane(&mut self, lane: u8) -> Self::Output {
3129 self.check_simd_lane_index(lane, 2)?;
3130 self.pop_operand(Some(ValType::V128))?;
3131 self.push_operand(ValType::I64)?;
3132 Ok(())
3133 }
3134 fn visit_i64x2_replace_lane(&mut self, lane: u8) -> Self::Output {
3135 self.check_simd_lane_index(lane, 2)?;
3136 self.pop_operand(Some(ValType::I64))?;
3137 self.pop_operand(Some(ValType::V128))?;
3138 self.push_operand(ValType::V128)?;
3139 Ok(())
3140 }
3141 fn visit_f32x4_extract_lane(&mut self, lane: u8) -> Self::Output {
3142 self.check_floats_enabled()?;
3143 self.check_simd_lane_index(lane, 4)?;
3144 self.pop_operand(Some(ValType::V128))?;
3145 self.push_operand(ValType::F32)?;
3146 Ok(())
3147 }
3148 fn visit_f32x4_replace_lane(&mut self, lane: u8) -> Self::Output {
3149 self.check_floats_enabled()?;
3150 self.check_simd_lane_index(lane, 4)?;
3151 self.pop_operand(Some(ValType::F32))?;
3152 self.pop_operand(Some(ValType::V128))?;
3153 self.push_operand(ValType::V128)?;
3154 Ok(())
3155 }
3156 fn visit_f64x2_extract_lane(&mut self, lane: u8) -> Self::Output {
3157 self.check_floats_enabled()?;
3158 self.check_simd_lane_index(lane, 2)?;
3159 self.pop_operand(Some(ValType::V128))?;
3160 self.push_operand(ValType::F64)?;
3161 Ok(())
3162 }
3163 fn visit_f64x2_replace_lane(&mut self, lane: u8) -> Self::Output {
3164 self.check_floats_enabled()?;
3165 self.check_simd_lane_index(lane, 2)?;
3166 self.pop_operand(Some(ValType::F64))?;
3167 self.pop_operand(Some(ValType::V128))?;
3168 self.push_operand(ValType::V128)?;
3169 Ok(())
3170 }
3171 fn visit_f32x4_eq(&mut self) -> Self::Output {
3172 self.check_v128_fbinary_op()
3173 }
3174 fn visit_f32x4_ne(&mut self) -> Self::Output {
3175 self.check_v128_fbinary_op()
3176 }
3177 fn visit_f32x4_lt(&mut self) -> Self::Output {
3178 self.check_v128_fbinary_op()
3179 }
3180 fn visit_f32x4_gt(&mut self) -> Self::Output {
3181 self.check_v128_fbinary_op()
3182 }
3183 fn visit_f32x4_le(&mut self) -> Self::Output {
3184 self.check_v128_fbinary_op()
3185 }
3186 fn visit_f32x4_ge(&mut self) -> Self::Output {
3187 self.check_v128_fbinary_op()
3188 }
3189 fn visit_f64x2_eq(&mut self) -> Self::Output {
3190 self.check_v128_fbinary_op()
3191 }
3192 fn visit_f64x2_ne(&mut self) -> Self::Output {
3193 self.check_v128_fbinary_op()
3194 }
3195 fn visit_f64x2_lt(&mut self) -> Self::Output {
3196 self.check_v128_fbinary_op()
3197 }
3198 fn visit_f64x2_gt(&mut self) -> Self::Output {
3199 self.check_v128_fbinary_op()
3200 }
3201 fn visit_f64x2_le(&mut self) -> Self::Output {
3202 self.check_v128_fbinary_op()
3203 }
3204 fn visit_f64x2_ge(&mut self) -> Self::Output {
3205 self.check_v128_fbinary_op()
3206 }
3207 fn visit_f32x4_add(&mut self) -> Self::Output {
3208 self.check_v128_fbinary_op()
3209 }
3210 fn visit_f32x4_sub(&mut self) -> Self::Output {
3211 self.check_v128_fbinary_op()
3212 }
3213 fn visit_f32x4_mul(&mut self) -> Self::Output {
3214 self.check_v128_fbinary_op()
3215 }
3216 fn visit_f32x4_div(&mut self) -> Self::Output {
3217 self.check_v128_fbinary_op()
3218 }
3219 fn visit_f32x4_min(&mut self) -> Self::Output {
3220 self.check_v128_fbinary_op()
3221 }
3222 fn visit_f32x4_max(&mut self) -> Self::Output {
3223 self.check_v128_fbinary_op()
3224 }
3225 fn visit_f32x4_pmin(&mut self) -> Self::Output {
3226 self.check_v128_fbinary_op()
3227 }
3228 fn visit_f32x4_pmax(&mut self) -> Self::Output {
3229 self.check_v128_fbinary_op()
3230 }
3231 fn visit_f64x2_add(&mut self) -> Self::Output {
3232 self.check_v128_fbinary_op()
3233 }
3234 fn visit_f64x2_sub(&mut self) -> Self::Output {
3235 self.check_v128_fbinary_op()
3236 }
3237 fn visit_f64x2_mul(&mut self) -> Self::Output {
3238 self.check_v128_fbinary_op()
3239 }
3240 fn visit_f64x2_div(&mut self) -> Self::Output {
3241 self.check_v128_fbinary_op()
3242 }
3243 fn visit_f64x2_min(&mut self) -> Self::Output {
3244 self.check_v128_fbinary_op()
3245 }
3246 fn visit_f64x2_max(&mut self) -> Self::Output {
3247 self.check_v128_fbinary_op()
3248 }
3249 fn visit_f64x2_pmin(&mut self) -> Self::Output {
3250 self.check_v128_fbinary_op()
3251 }
3252 fn visit_f64x2_pmax(&mut self) -> Self::Output {
3253 self.check_v128_fbinary_op()
3254 }
3255 fn visit_i8x16_eq(&mut self) -> Self::Output {
3256 self.check_v128_binary_op()
3257 }
3258 fn visit_i8x16_ne(&mut self) -> Self::Output {
3259 self.check_v128_binary_op()
3260 }
3261 fn visit_i8x16_lt_s(&mut self) -> Self::Output {
3262 self.check_v128_binary_op()
3263 }
3264 fn visit_i8x16_lt_u(&mut self) -> Self::Output {
3265 self.check_v128_binary_op()
3266 }
3267 fn visit_i8x16_gt_s(&mut self) -> Self::Output {
3268 self.check_v128_binary_op()
3269 }
3270 fn visit_i8x16_gt_u(&mut self) -> Self::Output {
3271 self.check_v128_binary_op()
3272 }
3273 fn visit_i8x16_le_s(&mut self) -> Self::Output {
3274 self.check_v128_binary_op()
3275 }
3276 fn visit_i8x16_le_u(&mut self) -> Self::Output {
3277 self.check_v128_binary_op()
3278 }
3279 fn visit_i8x16_ge_s(&mut self) -> Self::Output {
3280 self.check_v128_binary_op()
3281 }
3282 fn visit_i8x16_ge_u(&mut self) -> Self::Output {
3283 self.check_v128_binary_op()
3284 }
3285 fn visit_i16x8_eq(&mut self) -> Self::Output {
3286 self.check_v128_binary_op()
3287 }
3288 fn visit_i16x8_ne(&mut self) -> Self::Output {
3289 self.check_v128_binary_op()
3290 }
3291 fn visit_i16x8_lt_s(&mut self) -> Self::Output {
3292 self.check_v128_binary_op()
3293 }
3294 fn visit_i16x8_lt_u(&mut self) -> Self::Output {
3295 self.check_v128_binary_op()
3296 }
3297 fn visit_i16x8_gt_s(&mut self) -> Self::Output {
3298 self.check_v128_binary_op()
3299 }
3300 fn visit_i16x8_gt_u(&mut self) -> Self::Output {
3301 self.check_v128_binary_op()
3302 }
3303 fn visit_i16x8_le_s(&mut self) -> Self::Output {
3304 self.check_v128_binary_op()
3305 }
3306 fn visit_i16x8_le_u(&mut self) -> Self::Output {
3307 self.check_v128_binary_op()
3308 }
3309 fn visit_i16x8_ge_s(&mut self) -> Self::Output {
3310 self.check_v128_binary_op()
3311 }
3312 fn visit_i16x8_ge_u(&mut self) -> Self::Output {
3313 self.check_v128_binary_op()
3314 }
3315 fn visit_i32x4_eq(&mut self) -> Self::Output {
3316 self.check_v128_binary_op()
3317 }
3318 fn visit_i32x4_ne(&mut self) -> Self::Output {
3319 self.check_v128_binary_op()
3320 }
3321 fn visit_i32x4_lt_s(&mut self) -> Self::Output {
3322 self.check_v128_binary_op()
3323 }
3324 fn visit_i32x4_lt_u(&mut self) -> Self::Output {
3325 self.check_v128_binary_op()
3326 }
3327 fn visit_i32x4_gt_s(&mut self) -> Self::Output {
3328 self.check_v128_binary_op()
3329 }
3330 fn visit_i32x4_gt_u(&mut self) -> Self::Output {
3331 self.check_v128_binary_op()
3332 }
3333 fn visit_i32x4_le_s(&mut self) -> Self::Output {
3334 self.check_v128_binary_op()
3335 }
3336 fn visit_i32x4_le_u(&mut self) -> Self::Output {
3337 self.check_v128_binary_op()
3338 }
3339 fn visit_i32x4_ge_s(&mut self) -> Self::Output {
3340 self.check_v128_binary_op()
3341 }
3342 fn visit_i32x4_ge_u(&mut self) -> Self::Output {
3343 self.check_v128_binary_op()
3344 }
3345 fn visit_i64x2_eq(&mut self) -> Self::Output {
3346 self.check_v128_binary_op()
3347 }
3348 fn visit_i64x2_ne(&mut self) -> Self::Output {
3349 self.check_v128_binary_op()
3350 }
3351 fn visit_i64x2_lt_s(&mut self) -> Self::Output {
3352 self.check_v128_binary_op()
3353 }
3354 fn visit_i64x2_gt_s(&mut self) -> Self::Output {
3355 self.check_v128_binary_op()
3356 }
3357 fn visit_i64x2_le_s(&mut self) -> Self::Output {
3358 self.check_v128_binary_op()
3359 }
3360 fn visit_i64x2_ge_s(&mut self) -> Self::Output {
3361 self.check_v128_binary_op()
3362 }
3363 fn visit_v128_and(&mut self) -> Self::Output {
3364 self.check_v128_binary_op()
3365 }
3366 fn visit_v128_andnot(&mut self) -> Self::Output {
3367 self.check_v128_binary_op()
3368 }
3369 fn visit_v128_or(&mut self) -> Self::Output {
3370 self.check_v128_binary_op()
3371 }
3372 fn visit_v128_xor(&mut self) -> Self::Output {
3373 self.check_v128_binary_op()
3374 }
3375 fn visit_i8x16_add(&mut self) -> Self::Output {
3376 self.check_v128_binary_op()
3377 }
3378 fn visit_i8x16_add_sat_s(&mut self) -> Self::Output {
3379 self.check_v128_binary_op()
3380 }
3381 fn visit_i8x16_add_sat_u(&mut self) -> Self::Output {
3382 self.check_v128_binary_op()
3383 }
3384 fn visit_i8x16_sub(&mut self) -> Self::Output {
3385 self.check_v128_binary_op()
3386 }
3387 fn visit_i8x16_sub_sat_s(&mut self) -> Self::Output {
3388 self.check_v128_binary_op()
3389 }
3390 fn visit_i8x16_sub_sat_u(&mut self) -> Self::Output {
3391 self.check_v128_binary_op()
3392 }
3393 fn visit_i8x16_min_s(&mut self) -> Self::Output {
3394 self.check_v128_binary_op()
3395 }
3396 fn visit_i8x16_min_u(&mut self) -> Self::Output {
3397 self.check_v128_binary_op()
3398 }
3399 fn visit_i8x16_max_s(&mut self) -> Self::Output {
3400 self.check_v128_binary_op()
3401 }
3402 fn visit_i8x16_max_u(&mut self) -> Self::Output {
3403 self.check_v128_binary_op()
3404 }
3405 fn visit_i16x8_add(&mut self) -> Self::Output {
3406 self.check_v128_binary_op()
3407 }
3408 fn visit_i16x8_add_sat_s(&mut self) -> Self::Output {
3409 self.check_v128_binary_op()
3410 }
3411 fn visit_i16x8_add_sat_u(&mut self) -> Self::Output {
3412 self.check_v128_binary_op()
3413 }
3414 fn visit_i16x8_sub(&mut self) -> Self::Output {
3415 self.check_v128_binary_op()
3416 }
3417 fn visit_i16x8_sub_sat_s(&mut self) -> Self::Output {
3418 self.check_v128_binary_op()
3419 }
3420 fn visit_i16x8_sub_sat_u(&mut self) -> Self::Output {
3421 self.check_v128_binary_op()
3422 }
3423 fn visit_i16x8_mul(&mut self) -> Self::Output {
3424 self.check_v128_binary_op()
3425 }
3426 fn visit_i16x8_min_s(&mut self) -> Self::Output {
3427 self.check_v128_binary_op()
3428 }
3429 fn visit_i16x8_min_u(&mut self) -> Self::Output {
3430 self.check_v128_binary_op()
3431 }
3432 fn visit_i16x8_max_s(&mut self) -> Self::Output {
3433 self.check_v128_binary_op()
3434 }
3435 fn visit_i16x8_max_u(&mut self) -> Self::Output {
3436 self.check_v128_binary_op()
3437 }
3438 fn visit_i32x4_add(&mut self) -> Self::Output {
3439 self.check_v128_binary_op()
3440 }
3441 fn visit_i32x4_sub(&mut self) -> Self::Output {
3442 self.check_v128_binary_op()
3443 }
3444 fn visit_i32x4_mul(&mut self) -> Self::Output {
3445 self.check_v128_binary_op()
3446 }
3447 fn visit_i32x4_min_s(&mut self) -> Self::Output {
3448 self.check_v128_binary_op()
3449 }
3450 fn visit_i32x4_min_u(&mut self) -> Self::Output {
3451 self.check_v128_binary_op()
3452 }
3453 fn visit_i32x4_max_s(&mut self) -> Self::Output {
3454 self.check_v128_binary_op()
3455 }
3456 fn visit_i32x4_max_u(&mut self) -> Self::Output {
3457 self.check_v128_binary_op()
3458 }
3459 fn visit_i32x4_dot_i16x8_s(&mut self) -> Self::Output {
3460 self.check_v128_binary_op()
3461 }
3462 fn visit_i64x2_add(&mut self) -> Self::Output {
3463 self.check_v128_binary_op()
3464 }
3465 fn visit_i64x2_sub(&mut self) -> Self::Output {
3466 self.check_v128_binary_op()
3467 }
3468 fn visit_i64x2_mul(&mut self) -> Self::Output {
3469 self.check_v128_binary_op()
3470 }
3471 fn visit_i8x16_avgr_u(&mut self) -> Self::Output {
3472 self.check_v128_binary_op()
3473 }
3474 fn visit_i16x8_avgr_u(&mut self) -> Self::Output {
3475 self.check_v128_binary_op()
3476 }
3477 fn visit_i8x16_narrow_i16x8_s(&mut self) -> Self::Output {
3478 self.check_v128_binary_op()
3479 }
3480 fn visit_i8x16_narrow_i16x8_u(&mut self) -> Self::Output {
3481 self.check_v128_binary_op()
3482 }
3483 fn visit_i16x8_narrow_i32x4_s(&mut self) -> Self::Output {
3484 self.check_v128_binary_op()
3485 }
3486 fn visit_i16x8_narrow_i32x4_u(&mut self) -> Self::Output {
3487 self.check_v128_binary_op()
3488 }
3489 fn visit_i16x8_extmul_low_i8x16_s(&mut self) -> Self::Output {
3490 self.check_v128_binary_op()
3491 }
3492 fn visit_i16x8_extmul_high_i8x16_s(&mut self) -> Self::Output {
3493 self.check_v128_binary_op()
3494 }
3495 fn visit_i16x8_extmul_low_i8x16_u(&mut self) -> Self::Output {
3496 self.check_v128_binary_op()
3497 }
3498 fn visit_i16x8_extmul_high_i8x16_u(&mut self) -> Self::Output {
3499 self.check_v128_binary_op()
3500 }
3501 fn visit_i32x4_extmul_low_i16x8_s(&mut self) -> Self::Output {
3502 self.check_v128_binary_op()
3503 }
3504 fn visit_i32x4_extmul_high_i16x8_s(&mut self) -> Self::Output {
3505 self.check_v128_binary_op()
3506 }
3507 fn visit_i32x4_extmul_low_i16x8_u(&mut self) -> Self::Output {
3508 self.check_v128_binary_op()
3509 }
3510 fn visit_i32x4_extmul_high_i16x8_u(&mut self) -> Self::Output {
3511 self.check_v128_binary_op()
3512 }
3513 fn visit_i64x2_extmul_low_i32x4_s(&mut self) -> Self::Output {
3514 self.check_v128_binary_op()
3515 }
3516 fn visit_i64x2_extmul_high_i32x4_s(&mut self) -> Self::Output {
3517 self.check_v128_binary_op()
3518 }
3519 fn visit_i64x2_extmul_low_i32x4_u(&mut self) -> Self::Output {
3520 self.check_v128_binary_op()
3521 }
3522 fn visit_i64x2_extmul_high_i32x4_u(&mut self) -> Self::Output {
3523 self.check_v128_binary_op()
3524 }
3525 fn visit_i16x8_q15mulr_sat_s(&mut self) -> Self::Output {
3526 self.check_v128_binary_op()
3527 }
3528 fn visit_f32x4_ceil(&mut self) -> Self::Output {
3529 self.check_v128_funary_op()
3530 }
3531 fn visit_f32x4_floor(&mut self) -> Self::Output {
3532 self.check_v128_funary_op()
3533 }
3534 fn visit_f32x4_trunc(&mut self) -> Self::Output {
3535 self.check_v128_funary_op()
3536 }
3537 fn visit_f32x4_nearest(&mut self) -> Self::Output {
3538 self.check_v128_funary_op()
3539 }
3540 fn visit_f64x2_ceil(&mut self) -> Self::Output {
3541 self.check_v128_funary_op()
3542 }
3543 fn visit_f64x2_floor(&mut self) -> Self::Output {
3544 self.check_v128_funary_op()
3545 }
3546 fn visit_f64x2_trunc(&mut self) -> Self::Output {
3547 self.check_v128_funary_op()
3548 }
3549 fn visit_f64x2_nearest(&mut self) -> Self::Output {
3550 self.check_v128_funary_op()
3551 }
3552 fn visit_f32x4_abs(&mut self) -> Self::Output {
3553 self.check_v128_funary_op()
3554 }
3555 fn visit_f32x4_neg(&mut self) -> Self::Output {
3556 self.check_v128_funary_op()
3557 }
3558 fn visit_f32x4_sqrt(&mut self) -> Self::Output {
3559 self.check_v128_funary_op()
3560 }
3561 fn visit_f64x2_abs(&mut self) -> Self::Output {
3562 self.check_v128_funary_op()
3563 }
3564 fn visit_f64x2_neg(&mut self) -> Self::Output {
3565 self.check_v128_funary_op()
3566 }
3567 fn visit_f64x2_sqrt(&mut self) -> Self::Output {
3568 self.check_v128_funary_op()
3569 }
3570 fn visit_f32x4_demote_f64x2_zero(&mut self) -> Self::Output {
3571 self.check_v128_funary_op()
3572 }
3573 fn visit_f64x2_promote_low_f32x4(&mut self) -> Self::Output {
3574 self.check_v128_funary_op()
3575 }
3576 fn visit_f64x2_convert_low_i32x4_s(&mut self) -> Self::Output {
3577 self.check_v128_funary_op()
3578 }
3579 fn visit_f64x2_convert_low_i32x4_u(&mut self) -> Self::Output {
3580 self.check_v128_funary_op()
3581 }
3582 fn visit_i32x4_trunc_sat_f32x4_s(&mut self) -> Self::Output {
3583 self.check_v128_funary_op()
3584 }
3585 fn visit_i32x4_trunc_sat_f32x4_u(&mut self) -> Self::Output {
3586 self.check_v128_funary_op()
3587 }
3588 fn visit_i32x4_trunc_sat_f64x2_s_zero(&mut self) -> Self::Output {
3589 self.check_v128_funary_op()
3590 }
3591 fn visit_i32x4_trunc_sat_f64x2_u_zero(&mut self) -> Self::Output {
3592 self.check_v128_funary_op()
3593 }
3594 fn visit_f32x4_convert_i32x4_s(&mut self) -> Self::Output {
3595 self.check_v128_funary_op()
3596 }
3597 fn visit_f32x4_convert_i32x4_u(&mut self) -> Self::Output {
3598 self.check_v128_funary_op()
3599 }
3600 fn visit_v128_not(&mut self) -> Self::Output {
3601 self.check_v128_unary_op()
3602 }
3603 fn visit_i8x16_abs(&mut self) -> Self::Output {
3604 self.check_v128_unary_op()
3605 }
3606 fn visit_i8x16_neg(&mut self) -> Self::Output {
3607 self.check_v128_unary_op()
3608 }
3609 fn visit_i8x16_popcnt(&mut self) -> Self::Output {
3610 self.check_v128_unary_op()
3611 }
3612 fn visit_i16x8_abs(&mut self) -> Self::Output {
3613 self.check_v128_unary_op()
3614 }
3615 fn visit_i16x8_neg(&mut self) -> Self::Output {
3616 self.check_v128_unary_op()
3617 }
3618 fn visit_i32x4_abs(&mut self) -> Self::Output {
3619 self.check_v128_unary_op()
3620 }
3621 fn visit_i32x4_neg(&mut self) -> Self::Output {
3622 self.check_v128_unary_op()
3623 }
3624 fn visit_i64x2_abs(&mut self) -> Self::Output {
3625 self.check_v128_unary_op()
3626 }
3627 fn visit_i64x2_neg(&mut self) -> Self::Output {
3628 self.check_v128_unary_op()
3629 }
3630 fn visit_i16x8_extend_low_i8x16_s(&mut self) -> Self::Output {
3631 self.check_v128_unary_op()
3632 }
3633 fn visit_i16x8_extend_high_i8x16_s(&mut self) -> Self::Output {
3634 self.check_v128_unary_op()
3635 }
3636 fn visit_i16x8_extend_low_i8x16_u(&mut self) -> Self::Output {
3637 self.check_v128_unary_op()
3638 }
3639 fn visit_i16x8_extend_high_i8x16_u(&mut self) -> Self::Output {
3640 self.check_v128_unary_op()
3641 }
3642 fn visit_i32x4_extend_low_i16x8_s(&mut self) -> Self::Output {
3643 self.check_v128_unary_op()
3644 }
3645 fn visit_i32x4_extend_high_i16x8_s(&mut self) -> Self::Output {
3646 self.check_v128_unary_op()
3647 }
3648 fn visit_i32x4_extend_low_i16x8_u(&mut self) -> Self::Output {
3649 self.check_v128_unary_op()
3650 }
3651 fn visit_i32x4_extend_high_i16x8_u(&mut self) -> Self::Output {
3652 self.check_v128_unary_op()
3653 }
3654 fn visit_i64x2_extend_low_i32x4_s(&mut self) -> Self::Output {
3655 self.check_v128_unary_op()
3656 }
3657 fn visit_i64x2_extend_high_i32x4_s(&mut self) -> Self::Output {
3658 self.check_v128_unary_op()
3659 }
3660 fn visit_i64x2_extend_low_i32x4_u(&mut self) -> Self::Output {
3661 self.check_v128_unary_op()
3662 }
3663 fn visit_i64x2_extend_high_i32x4_u(&mut self) -> Self::Output {
3664 self.check_v128_unary_op()
3665 }
3666 fn visit_i16x8_extadd_pairwise_i8x16_s(&mut self) -> Self::Output {
3667 self.check_v128_unary_op()
3668 }
3669 fn visit_i16x8_extadd_pairwise_i8x16_u(&mut self) -> Self::Output {
3670 self.check_v128_unary_op()
3671 }
3672 fn visit_i32x4_extadd_pairwise_i16x8_s(&mut self) -> Self::Output {
3673 self.check_v128_unary_op()
3674 }
3675 fn visit_i32x4_extadd_pairwise_i16x8_u(&mut self) -> Self::Output {
3676 self.check_v128_unary_op()
3677 }
3678 fn visit_v128_bitselect(&mut self) -> Self::Output {
3679 self.pop_operand(Some(ValType::V128))?;
3680 self.pop_operand(Some(ValType::V128))?;
3681 self.pop_operand(Some(ValType::V128))?;
3682 self.push_operand(ValType::V128)?;
3683 Ok(())
3684 }
3685 fn visit_i8x16_relaxed_swizzle(&mut self) -> Self::Output {
3686 self.pop_operand(Some(ValType::V128))?;
3687 self.pop_operand(Some(ValType::V128))?;
3688 self.push_operand(ValType::V128)?;
3689 Ok(())
3690 }
3691 fn visit_i32x4_relaxed_trunc_f32x4_s(&mut self) -> Self::Output {
3692 self.check_v128_unary_op()
3693 }
3694 fn visit_i32x4_relaxed_trunc_f32x4_u(&mut self) -> Self::Output {
3695 self.check_v128_unary_op()
3696 }
3697 fn visit_i32x4_relaxed_trunc_f64x2_s_zero(&mut self) -> Self::Output {
3698 self.check_v128_unary_op()
3699 }
3700 fn visit_i32x4_relaxed_trunc_f64x2_u_zero(&mut self) -> Self::Output {
3701 self.check_v128_unary_op()
3702 }
3703 fn visit_f32x4_relaxed_madd(&mut self) -> Self::Output {
3704 self.check_v128_ternary_op()
3705 }
3706 fn visit_f32x4_relaxed_nmadd(&mut self) -> Self::Output {
3707 self.check_v128_ternary_op()
3708 }
3709 fn visit_f64x2_relaxed_madd(&mut self) -> Self::Output {
3710 self.check_v128_ternary_op()
3711 }
3712 fn visit_f64x2_relaxed_nmadd(&mut self) -> Self::Output {
3713 self.check_v128_ternary_op()
3714 }
3715 fn visit_i8x16_relaxed_laneselect(&mut self) -> Self::Output {
3716 self.check_v128_ternary_op()
3717 }
3718 fn visit_i16x8_relaxed_laneselect(&mut self) -> Self::Output {
3719 self.check_v128_ternary_op()
3720 }
3721 fn visit_i32x4_relaxed_laneselect(&mut self) -> Self::Output {
3722 self.check_v128_ternary_op()
3723 }
3724 fn visit_i64x2_relaxed_laneselect(&mut self) -> Self::Output {
3725 self.check_v128_ternary_op()
3726 }
3727 fn visit_f32x4_relaxed_min(&mut self) -> Self::Output {
3728 self.check_v128_binary_op()
3729 }
3730 fn visit_f32x4_relaxed_max(&mut self) -> Self::Output {
3731 self.check_v128_binary_op()
3732 }
3733 fn visit_f64x2_relaxed_min(&mut self) -> Self::Output {
3734 self.check_v128_binary_op()
3735 }
3736 fn visit_f64x2_relaxed_max(&mut self) -> Self::Output {
3737 self.check_v128_binary_op()
3738 }
3739 fn visit_i16x8_relaxed_q15mulr_s(&mut self) -> Self::Output {
3740 self.check_v128_binary_op()
3741 }
3742 fn visit_i16x8_relaxed_dot_i8x16_i7x16_s(&mut self) -> Self::Output {
3743 self.check_v128_binary_op()
3744 }
3745 fn visit_i32x4_relaxed_dot_i8x16_i7x16_add_s(&mut self) -> Self::Output {
3746 self.check_v128_ternary_op()
3747 }
3748 fn visit_v128_any_true(&mut self) -> Self::Output {
3749 self.check_v128_bitmask_op()
3750 }
3751 fn visit_i8x16_all_true(&mut self) -> Self::Output {
3752 self.check_v128_bitmask_op()
3753 }
3754 fn visit_i8x16_bitmask(&mut self) -> Self::Output {
3755 self.check_v128_bitmask_op()
3756 }
3757 fn visit_i16x8_all_true(&mut self) -> Self::Output {
3758 self.check_v128_bitmask_op()
3759 }
3760 fn visit_i16x8_bitmask(&mut self) -> Self::Output {
3761 self.check_v128_bitmask_op()
3762 }
3763 fn visit_i32x4_all_true(&mut self) -> Self::Output {
3764 self.check_v128_bitmask_op()
3765 }
3766 fn visit_i32x4_bitmask(&mut self) -> Self::Output {
3767 self.check_v128_bitmask_op()
3768 }
3769 fn visit_i64x2_all_true(&mut self) -> Self::Output {
3770 self.check_v128_bitmask_op()
3771 }
3772 fn visit_i64x2_bitmask(&mut self) -> Self::Output {
3773 self.check_v128_bitmask_op()
3774 }
3775 fn visit_i8x16_shl(&mut self) -> Self::Output {
3776 self.check_v128_shift_op()
3777 }
3778 fn visit_i8x16_shr_s(&mut self) -> Self::Output {
3779 self.check_v128_shift_op()
3780 }
3781 fn visit_i8x16_shr_u(&mut self) -> Self::Output {
3782 self.check_v128_shift_op()
3783 }
3784 fn visit_i16x8_shl(&mut self) -> Self::Output {
3785 self.check_v128_shift_op()
3786 }
3787 fn visit_i16x8_shr_s(&mut self) -> Self::Output {
3788 self.check_v128_shift_op()
3789 }
3790 fn visit_i16x8_shr_u(&mut self) -> Self::Output {
3791 self.check_v128_shift_op()
3792 }
3793 fn visit_i32x4_shl(&mut self) -> Self::Output {
3794 self.check_v128_shift_op()
3795 }
3796 fn visit_i32x4_shr_s(&mut self) -> Self::Output {
3797 self.check_v128_shift_op()
3798 }
3799 fn visit_i32x4_shr_u(&mut self) -> Self::Output {
3800 self.check_v128_shift_op()
3801 }
3802 fn visit_i64x2_shl(&mut self) -> Self::Output {
3803 self.check_v128_shift_op()
3804 }
3805 fn visit_i64x2_shr_s(&mut self) -> Self::Output {
3806 self.check_v128_shift_op()
3807 }
3808 fn visit_i64x2_shr_u(&mut self) -> Self::Output {
3809 self.check_v128_shift_op()
3810 }
3811 fn visit_i8x16_swizzle(&mut self) -> Self::Output {
3812 self.pop_operand(Some(ValType::V128))?;
3813 self.pop_operand(Some(ValType::V128))?;
3814 self.push_operand(ValType::V128)?;
3815 Ok(())
3816 }
3817 fn visit_i8x16_shuffle(&mut self, lanes: [u8; 16]) -> Self::Output {
3818 self.pop_operand(Some(ValType::V128))?;
3819 self.pop_operand(Some(ValType::V128))?;
3820 for i in lanes {
3821 self.check_simd_lane_index(i, 32)?;
3822 }
3823 self.push_operand(ValType::V128)?;
3824 Ok(())
3825 }
3826 fn visit_v128_load8_splat(&mut self, memarg: MemArg) -> Self::Output {
3827 let ty = self.check_memarg(memarg)?;
3828 self.pop_operand(Some(ty))?;
3829 self.push_operand(ValType::V128)?;
3830 Ok(())
3831 }
3832 fn visit_v128_load16_splat(&mut self, memarg: MemArg) -> Self::Output {
3833 let ty = self.check_memarg(memarg)?;
3834 self.pop_operand(Some(ty))?;
3835 self.push_operand(ValType::V128)?;
3836 Ok(())
3837 }
3838 fn visit_v128_load32_splat(&mut self, memarg: MemArg) -> Self::Output {
3839 let ty = self.check_memarg(memarg)?;
3840 self.pop_operand(Some(ty))?;
3841 self.push_operand(ValType::V128)?;
3842 Ok(())
3843 }
3844 fn visit_v128_load32_zero(&mut self, memarg: MemArg) -> Self::Output {
3845 self.visit_v128_load32_splat(memarg)
3846 }
3847 fn visit_v128_load64_splat(&mut self, memarg: MemArg) -> Self::Output {
3848 self.check_v128_load_op(memarg)
3849 }
3850 fn visit_v128_load64_zero(&mut self, memarg: MemArg) -> Self::Output {
3851 self.check_v128_load_op(memarg)
3852 }
3853 fn visit_v128_load8x8_s(&mut self, memarg: MemArg) -> Self::Output {
3854 self.check_v128_load_op(memarg)
3855 }
3856 fn visit_v128_load8x8_u(&mut self, memarg: MemArg) -> Self::Output {
3857 self.check_v128_load_op(memarg)
3858 }
3859 fn visit_v128_load16x4_s(&mut self, memarg: MemArg) -> Self::Output {
3860 self.check_v128_load_op(memarg)
3861 }
3862 fn visit_v128_load16x4_u(&mut self, memarg: MemArg) -> Self::Output {
3863 self.check_v128_load_op(memarg)
3864 }
3865 fn visit_v128_load32x2_s(&mut self, memarg: MemArg) -> Self::Output {
3866 self.check_v128_load_op(memarg)
3867 }
3868 fn visit_v128_load32x2_u(&mut self, memarg: MemArg) -> Self::Output {
3869 self.check_v128_load_op(memarg)
3870 }
3871 fn visit_v128_load8_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3872 let idx = self.check_memarg(memarg)?;
3873 self.check_simd_lane_index(lane, 16)?;
3874 self.pop_operand(Some(ValType::V128))?;
3875 self.pop_operand(Some(idx))?;
3876 self.push_operand(ValType::V128)?;
3877 Ok(())
3878 }
3879 fn visit_v128_load16_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3880 let idx = self.check_memarg(memarg)?;
3881 self.check_simd_lane_index(lane, 8)?;
3882 self.pop_operand(Some(ValType::V128))?;
3883 self.pop_operand(Some(idx))?;
3884 self.push_operand(ValType::V128)?;
3885 Ok(())
3886 }
3887 fn visit_v128_load32_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3888 let idx = self.check_memarg(memarg)?;
3889 self.check_simd_lane_index(lane, 4)?;
3890 self.pop_operand(Some(ValType::V128))?;
3891 self.pop_operand(Some(idx))?;
3892 self.push_operand(ValType::V128)?;
3893 Ok(())
3894 }
3895 fn visit_v128_load64_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3896 let idx = self.check_memarg(memarg)?;
3897 self.check_simd_lane_index(lane, 2)?;
3898 self.pop_operand(Some(ValType::V128))?;
3899 self.pop_operand(Some(idx))?;
3900 self.push_operand(ValType::V128)?;
3901 Ok(())
3902 }
3903 fn visit_v128_store8_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3904 let idx = self.check_memarg(memarg)?;
3905 self.check_simd_lane_index(lane, 16)?;
3906 self.pop_operand(Some(ValType::V128))?;
3907 self.pop_operand(Some(idx))?;
3908 Ok(())
3909 }
3910 fn visit_v128_store16_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3911 let idx = self.check_memarg(memarg)?;
3912 self.check_simd_lane_index(lane, 8)?;
3913 self.pop_operand(Some(ValType::V128))?;
3914 self.pop_operand(Some(idx))?;
3915 Ok(())
3916 }
3917 fn visit_v128_store32_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3918 let idx = self.check_memarg(memarg)?;
3919 self.check_simd_lane_index(lane, 4)?;
3920 self.pop_operand(Some(ValType::V128))?;
3921 self.pop_operand(Some(idx))?;
3922 Ok(())
3923 }
3924 fn visit_v128_store64_lane(&mut self, memarg: MemArg, lane: u8) -> Self::Output {
3925 let idx = self.check_memarg(memarg)?;
3926 self.check_simd_lane_index(lane, 2)?;
3927 self.pop_operand(Some(ValType::V128))?;
3928 self.pop_operand(Some(idx))?;
3929 Ok(())
3930 }
3931 fn visit_memory_init(&mut self, segment: u32, mem: u32) -> Self::Output {
3932 let ty = self.check_memory_index(mem)?;
3933 self.check_data_segment(segment)?;
3934 self.pop_operand(Some(ValType::I32))?;
3935 self.pop_operand(Some(ValType::I32))?;
3936 self.pop_operand(Some(ty))?;
3937 Ok(())
3938 }
3939 fn visit_data_drop(&mut self, segment: u32) -> Self::Output {
3940 self.check_data_segment(segment)?;
3941 Ok(())
3942 }
3943 fn visit_memory_copy(&mut self, dst: u32, src: u32) -> Self::Output {
3944 let dst_ty = self.check_memory_index(dst)?;
3945 let src_ty = self.check_memory_index(src)?;
3946
3947 // The length operand here is the smaller of src/dst, which is
3948 // i32 if one is i32
3949 self.pop_operand(Some(match src_ty {
3950 ValType::I32 => ValType::I32,
3951 _ => dst_ty,
3952 }))?;
3953
3954 // ... and the offset into each memory is required to be
3955 // whatever the indexing type is for that memory
3956 self.pop_operand(Some(src_ty))?;
3957 self.pop_operand(Some(dst_ty))?;
3958 Ok(())
3959 }
3960 fn visit_memory_fill(&mut self, mem: u32) -> Self::Output {
3961 let ty = self.check_memory_index(mem)?;
3962 self.pop_operand(Some(ty))?;
3963 self.pop_operand(Some(ValType::I32))?;
3964 self.pop_operand(Some(ty))?;
3965 Ok(())
3966 }
3967 fn visit_memory_discard(&mut self, mem: u32) -> Self::Output {
3968 let ty = self.check_memory_index(mem)?;
3969 self.pop_operand(Some(ty))?;
3970 self.pop_operand(Some(ty))?;
3971 Ok(())
3972 }
3973 fn visit_table_init(&mut self, segment: u32, table: u32) -> Self::Output {
3974 let table = self.table_type_at(table)?;
3975 let segment_ty = self.element_type_at(segment)?;
3976 if !self
3977 .resources
3978 .is_subtype(ValType::Ref(segment_ty), ValType::Ref(table.element_type))
3979 {
3980 bail!(self.offset, "type mismatch");
3981 }
3982 self.pop_operand(Some(ValType::I32))?;
3983 self.pop_operand(Some(ValType::I32))?;
3984 self.pop_operand(Some(table.index_type()))?;
3985 Ok(())
3986 }
3987 fn visit_elem_drop(&mut self, segment: u32) -> Self::Output {
3988 self.element_type_at(segment)?;
3989 Ok(())
3990 }
3991 fn visit_table_copy(&mut self, dst_table: u32, src_table: u32) -> Self::Output {
3992 let src = self.table_type_at(src_table)?;
3993 let dst = self.table_type_at(dst_table)?;
3994 if !self.resources.is_subtype(
3995 ValType::Ref(src.element_type),
3996 ValType::Ref(dst.element_type),
3997 ) {
3998 bail!(self.offset, "type mismatch");
3999 }
4000
4001 // The length operand here is the smaller of src/dst, which is
4002 // i32 if one is i32
4003 self.pop_operand(Some(match src.index_type() {
4004 ValType::I32 => ValType::I32,
4005 _ => dst.index_type(),
4006 }))?;
4007
4008 // ... and the offset into each table is required to be
4009 // whatever the indexing type is for that table
4010 self.pop_operand(Some(src.index_type()))?;
4011 self.pop_operand(Some(dst.index_type()))?;
4012 Ok(())
4013 }
4014 fn visit_table_get(&mut self, table: u32) -> Self::Output {
4015 let table = self.table_type_at(table)?;
4016 debug_assert_type_indices_are_ids(table.element_type.into());
4017 self.pop_operand(Some(table.index_type()))?;
4018 self.push_operand(table.element_type)?;
4019 Ok(())
4020 }
4021 fn visit_table_atomic_get(&mut self, _ordering: Ordering, table: u32) -> Self::Output {
4022 self.visit_table_get(table)?;
4023 // No validation of `ordering` is needed because `table.atomic.get` can
4024 // be used on both shared and unshared tables. But we do need to limit
4025 // which types can be used with this instruction.
4026 let ty = self.table_type_at(table)?.element_type;
4027 let supertype = RefType::ANYREF.shared().unwrap();
4028 if !self.resources.is_subtype(ty.into(), supertype.into()) {
4029 bail!(
4030 self.offset,
4031 "invalid type: `table.atomic.get` only allows subtypes of `anyref`"
4032 );
4033 }
4034 Ok(())
4035 }
4036 fn visit_table_set(&mut self, table: u32) -> Self::Output {
4037 let table = self.table_type_at(table)?;
4038 debug_assert_type_indices_are_ids(table.element_type.into());
4039 self.pop_operand(Some(table.element_type.into()))?;
4040 self.pop_operand(Some(table.index_type()))?;
4041 Ok(())
4042 }
4043 fn visit_table_atomic_set(&mut self, _ordering: Ordering, table: u32) -> Self::Output {
4044 self.visit_table_set(table)?;
4045 // No validation of `ordering` is needed because `table.atomic.set` can
4046 // be used on both shared and unshared tables. But we do need to limit
4047 // which types can be used with this instruction.
4048 let ty = self.table_type_at(table)?.element_type;
4049 let supertype = RefType::ANYREF.shared().unwrap();
4050 if !self.resources.is_subtype(ty.into(), supertype.into()) {
4051 bail!(
4052 self.offset,
4053 "invalid type: `table.atomic.set` only allows subtypes of `anyref`"
4054 );
4055 }
4056 Ok(())
4057 }
4058 fn visit_table_grow(&mut self, table: u32) -> Self::Output {
4059 let table = self.table_type_at(table)?;
4060 debug_assert_type_indices_are_ids(table.element_type.into());
4061 self.pop_operand(Some(table.index_type()))?;
4062 self.pop_operand(Some(table.element_type.into()))?;
4063 self.push_operand(table.index_type())?;
4064 Ok(())
4065 }
4066 fn visit_table_size(&mut self, table: u32) -> Self::Output {
4067 let table = self.table_type_at(table)?;
4068 self.push_operand(table.index_type())?;
4069 Ok(())
4070 }
4071 fn visit_table_fill(&mut self, table: u32) -> Self::Output {
4072 let table = self.table_type_at(table)?;
4073 debug_assert_type_indices_are_ids(table.element_type.into());
4074 self.pop_operand(Some(table.index_type()))?;
4075 self.pop_operand(Some(table.element_type.into()))?;
4076 self.pop_operand(Some(table.index_type()))?;
4077 Ok(())
4078 }
4079 fn visit_table_atomic_rmw_xchg(&mut self, _ordering: Ordering, table: u32) -> Self::Output {
4080 let table = self.table_type_at(table)?;
4081 let elem_ty = table.element_type.into();
4082 debug_assert_type_indices_are_ids(elem_ty);
4083 let supertype = RefType::ANYREF.shared().unwrap();
4084 if !self.resources.is_subtype(elem_ty, supertype.into()) {
4085 bail!(
4086 self.offset,
4087 "invalid type: `table.atomic.rmw.xchg` only allows subtypes of `anyref`"
4088 );
4089 }
4090 self.pop_operand(Some(elem_ty))?;
4091 self.pop_operand(Some(table.index_type()))?;
4092 self.push_operand(elem_ty)?;
4093 Ok(())
4094 }
4095 fn visit_table_atomic_rmw_cmpxchg(&mut self, _ordering: Ordering, table: u32) -> Self::Output {
4096 let table = self.table_type_at(table)?;
4097 let elem_ty = table.element_type.into();
4098 debug_assert_type_indices_are_ids(elem_ty);
4099 let supertype = RefType::EQREF.shared().unwrap();
4100 if !self.resources.is_subtype(elem_ty, supertype.into()) {
4101 bail!(
4102 self.offset,
4103 "invalid type: `table.atomic.rmw.cmpxchg` only allows subtypes of `eqref`"
4104 );
4105 }
4106 self.pop_operand(Some(elem_ty))?;
4107 self.pop_operand(Some(elem_ty))?;
4108 self.pop_operand(Some(table.index_type()))?;
4109 self.push_operand(elem_ty)?;
4110 Ok(())
4111 }
4112 fn visit_struct_new(&mut self, struct_type_index: u32) -> Self::Output {
4113 let struct_ty = self.struct_type_at(struct_type_index)?;
4114 for ty in struct_ty.fields.iter().rev() {
4115 self.pop_operand(Some(ty.element_type.unpack()))?;
4116 }
4117 self.push_concrete_ref(false, struct_type_index)?;
4118 Ok(())
4119 }
4120 fn visit_struct_new_default(&mut self, type_index: u32) -> Self::Output {
4121 let ty = self.struct_type_at(type_index)?;
4122 for field in ty.fields.iter() {
4123 let val_ty = field.element_type.unpack();
4124 if !val_ty.is_defaultable() {
4125 bail!(
4126 self.offset,
4127 "invalid `struct.new_default`: {val_ty} field is not defaultable"
4128 );
4129 }
4130 }
4131 self.push_concrete_ref(false, type_index)?;
4132 Ok(())
4133 }
4134 fn visit_struct_get(&mut self, struct_type_index: u32, field_index: u32) -> Self::Output {
4135 let field_ty = self.struct_field_at(struct_type_index, field_index)?;
4136 if field_ty.element_type.is_packed() {
4137 bail!(
4138 self.offset,
4139 "can only use struct `get` with non-packed storage types"
4140 )
4141 }
4142 self.pop_concrete_ref(true, struct_type_index)?;
4143 self.push_operand(field_ty.element_type.unpack())
4144 }
4145 fn visit_struct_atomic_get(
4146 &mut self,
4147 _ordering: Ordering,
4148 struct_type_index: u32,
4149 field_index: u32,
4150 ) -> Self::Output {
4151 self.visit_struct_get(struct_type_index, field_index)?;
4152 // The `atomic` version has some additional type restrictions.
4153 let ty = self
4154 .struct_field_at(struct_type_index, field_index)?
4155 .element_type;
4156 let is_valid_type = match ty {
4157 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4158 StorageType::Val(v) => self
4159 .resources
4160 .is_subtype(v, RefType::ANYREF.shared().unwrap().into()),
4161 _ => false,
4162 };
4163 if !is_valid_type {
4164 bail!(
4165 self.offset,
4166 "invalid type: `struct.atomic.get` only allows `i32`, `i64` and subtypes of `anyref`"
4167 );
4168 }
4169 Ok(())
4170 }
4171 fn visit_struct_get_s(&mut self, struct_type_index: u32, field_index: u32) -> Self::Output {
4172 let field_ty = self.struct_field_at(struct_type_index, field_index)?;
4173 if !field_ty.element_type.is_packed() {
4174 bail!(
4175 self.offset,
4176 "cannot use struct.get_s with non-packed storage types"
4177 )
4178 }
4179 self.pop_concrete_ref(true, struct_type_index)?;
4180 self.push_operand(field_ty.element_type.unpack())
4181 }
4182 fn visit_struct_atomic_get_s(
4183 &mut self,
4184 _ordering: Ordering,
4185 struct_type_index: u32,
4186 field_index: u32,
4187 ) -> Self::Output {
4188 self.visit_struct_get_s(struct_type_index, field_index)?;
4189 // This instruction has the same type restrictions as the non-`atomic` version.
4190 debug_assert!(matches!(
4191 self.struct_field_at(struct_type_index, field_index)?
4192 .element_type,
4193 StorageType::I8 | StorageType::I16
4194 ));
4195 Ok(())
4196 }
4197 fn visit_struct_get_u(&mut self, struct_type_index: u32, field_index: u32) -> Self::Output {
4198 let field_ty = self.struct_field_at(struct_type_index, field_index)?;
4199 if !field_ty.element_type.is_packed() {
4200 bail!(
4201 self.offset,
4202 "cannot use struct.get_u with non-packed storage types"
4203 )
4204 }
4205 self.pop_concrete_ref(true, struct_type_index)?;
4206 self.push_operand(field_ty.element_type.unpack())
4207 }
4208 fn visit_struct_atomic_get_u(
4209 &mut self,
4210 _ordering: Ordering,
4211 struct_type_index: u32,
4212 field_index: u32,
4213 ) -> Self::Output {
4214 self.visit_struct_get_s(struct_type_index, field_index)?;
4215 // This instruction has the same type restrictions as the non-`atomic` version.
4216 debug_assert!(matches!(
4217 self.struct_field_at(struct_type_index, field_index)?
4218 .element_type,
4219 StorageType::I8 | StorageType::I16
4220 ));
4221 Ok(())
4222 }
4223 fn visit_struct_set(&mut self, struct_type_index: u32, field_index: u32) -> Self::Output {
4224 let field_ty = self.mutable_struct_field_at(struct_type_index, field_index)?;
4225 self.pop_operand(Some(field_ty.element_type.unpack()))?;
4226 self.pop_concrete_ref(true, struct_type_index)?;
4227 Ok(())
4228 }
4229 fn visit_struct_atomic_set(
4230 &mut self,
4231 _ordering: Ordering,
4232 struct_type_index: u32,
4233 field_index: u32,
4234 ) -> Self::Output {
4235 self.visit_struct_set(struct_type_index, field_index)?;
4236 // The `atomic` version has some additional type restrictions.
4237 let ty = self
4238 .struct_field_at(struct_type_index, field_index)?
4239 .element_type;
4240 let is_valid_type = match ty {
4241 StorageType::I8 | StorageType::I16 => true,
4242 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4243 StorageType::Val(v) => self
4244 .resources
4245 .is_subtype(v, RefType::ANYREF.shared().unwrap().into()),
4246 };
4247 if !is_valid_type {
4248 bail!(
4249 self.offset,
4250 "invalid type: `struct.atomic.set` only allows `i8`, `i16`, `i32`, `i64` and subtypes of `anyref`"
4251 );
4252 }
4253 Ok(())
4254 }
4255 fn visit_struct_atomic_rmw_add(
4256 &mut self,
4257 _ordering: Ordering,
4258 struct_type_index: u32,
4259 field_index: u32,
4260 ) -> Self::Output {
4261 self.check_struct_atomic_rmw("add", struct_type_index, field_index)
4262 }
4263 fn visit_struct_atomic_rmw_sub(
4264 &mut self,
4265 _ordering: Ordering,
4266 struct_type_index: u32,
4267 field_index: u32,
4268 ) -> Self::Output {
4269 self.check_struct_atomic_rmw("sub", struct_type_index, field_index)
4270 }
4271 fn visit_struct_atomic_rmw_and(
4272 &mut self,
4273 _ordering: Ordering,
4274 struct_type_index: u32,
4275 field_index: u32,
4276 ) -> Self::Output {
4277 self.check_struct_atomic_rmw("and", struct_type_index, field_index)
4278 }
4279 fn visit_struct_atomic_rmw_or(
4280 &mut self,
4281 _ordering: Ordering,
4282 struct_type_index: u32,
4283 field_index: u32,
4284 ) -> Self::Output {
4285 self.check_struct_atomic_rmw("or", struct_type_index, field_index)
4286 }
4287 fn visit_struct_atomic_rmw_xor(
4288 &mut self,
4289 _ordering: Ordering,
4290 struct_type_index: u32,
4291 field_index: u32,
4292 ) -> Self::Output {
4293 self.check_struct_atomic_rmw("xor", struct_type_index, field_index)
4294 }
4295 fn visit_struct_atomic_rmw_xchg(
4296 &mut self,
4297 _ordering: Ordering,
4298 struct_type_index: u32,
4299 field_index: u32,
4300 ) -> Self::Output {
4301 let field = self.mutable_struct_field_at(struct_type_index, field_index)?;
4302 let is_valid_type = match field.element_type {
4303 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4304 StorageType::Val(v) => self
4305 .resources
4306 .is_subtype(v, RefType::ANYREF.shared().unwrap().into()),
4307 _ => false,
4308 };
4309 if !is_valid_type {
4310 bail!(
4311 self.offset,
4312 "invalid type: `struct.atomic.rmw.xchg` only allows `i32`, `i64` and subtypes of `anyref`"
4313 );
4314 }
4315 let field_ty = field.element_type.unpack();
4316 self.pop_operand(Some(field_ty))?;
4317 self.pop_concrete_ref(true, struct_type_index)?;
4318 self.push_operand(field_ty)?;
4319 Ok(())
4320 }
4321 fn visit_struct_atomic_rmw_cmpxchg(
4322 &mut self,
4323 _ordering: Ordering,
4324 struct_type_index: u32,
4325 field_index: u32,
4326 ) -> Self::Output {
4327 let field = self.mutable_struct_field_at(struct_type_index, field_index)?;
4328 let is_valid_type = match field.element_type {
4329 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4330 StorageType::Val(v) => self
4331 .resources
4332 .is_subtype(v, RefType::EQREF.shared().unwrap().into()),
4333 _ => false,
4334 };
4335 if !is_valid_type {
4336 bail!(
4337 self.offset,
4338 "invalid type: `struct.atomic.rmw.cmpxchg` only allows `i32`, `i64` and subtypes of `eqref`"
4339 );
4340 }
4341 let field_ty = field.element_type.unpack();
4342 self.pop_operand(Some(field_ty))?;
4343 self.pop_operand(Some(field_ty))?;
4344 self.pop_concrete_ref(true, struct_type_index)?;
4345 self.push_operand(field_ty)?;
4346 Ok(())
4347 }
4348 fn visit_array_new(&mut self, type_index: u32) -> Self::Output {
4349 let array_ty = self.array_type_at(type_index)?;
4350 self.pop_operand(Some(ValType::I32))?;
4351 self.pop_operand(Some(array_ty.element_type.unpack()))?;
4352 self.push_concrete_ref(false, type_index)
4353 }
4354 fn visit_array_new_default(&mut self, type_index: u32) -> Self::Output {
4355 let ty = self.array_type_at(type_index)?;
4356 let val_ty = ty.element_type.unpack();
4357 if !val_ty.is_defaultable() {
4358 bail!(
4359 self.offset,
4360 "invalid `array.new_default`: {val_ty} field is not defaultable"
4361 );
4362 }
4363 self.pop_operand(Some(ValType::I32))?;
4364 self.push_concrete_ref(false, type_index)
4365 }
4366 fn visit_array_new_fixed(&mut self, type_index: u32, n: u32) -> Self::Output {
4367 let array_ty = self.array_type_at(type_index)?;
4368 let elem_ty = array_ty.element_type.unpack();
4369 for _ in 0..n {
4370 self.pop_operand(Some(elem_ty))?;
4371 }
4372 self.push_concrete_ref(false, type_index)
4373 }
4374 fn visit_array_new_data(&mut self, type_index: u32, data_index: u32) -> Self::Output {
4375 let array_ty = self.array_type_at(type_index)?;
4376 let elem_ty = array_ty.element_type.unpack();
4377 match elem_ty {
4378 ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => {}
4379 ValType::Ref(_) => bail!(
4380 self.offset,
4381 "type mismatch: array.new_data can only create arrays with numeric and vector elements"
4382 ),
4383 }
4384 self.check_data_segment(data_index)?;
4385 self.pop_operand(Some(ValType::I32))?;
4386 self.pop_operand(Some(ValType::I32))?;
4387 self.push_concrete_ref(false, type_index)
4388 }
4389 fn visit_array_new_elem(&mut self, type_index: u32, elem_index: u32) -> Self::Output {
4390 let array_ty = self.array_type_at(type_index)?;
4391 let array_ref_ty = match array_ty.element_type.unpack() {
4392 ValType::Ref(rt) => rt,
4393 ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => bail!(
4394 self.offset,
4395 "type mismatch: array.new_elem can only create arrays with reference elements"
4396 ),
4397 };
4398 let elem_ref_ty = self.element_type_at(elem_index)?;
4399 if !self
4400 .resources
4401 .is_subtype(elem_ref_ty.into(), array_ref_ty.into())
4402 {
4403 bail!(
4404 self.offset,
4405 "invalid array.new_elem instruction: element segment {elem_index} type mismatch: \
4406 expected {array_ref_ty}, found {elem_ref_ty}"
4407 )
4408 }
4409 self.pop_operand(Some(ValType::I32))?;
4410 self.pop_operand(Some(ValType::I32))?;
4411 self.push_concrete_ref(false, type_index)
4412 }
4413 fn visit_array_get(&mut self, type_index: u32) -> Self::Output {
4414 let array_ty = self.array_type_at(type_index)?;
4415 let elem_ty = array_ty.element_type;
4416 if elem_ty.is_packed() {
4417 bail!(
4418 self.offset,
4419 "cannot use array.get with packed storage types"
4420 )
4421 }
4422 self.pop_operand(Some(ValType::I32))?;
4423 self.pop_concrete_ref(true, type_index)?;
4424 self.push_operand(elem_ty.unpack())
4425 }
4426 fn visit_array_atomic_get(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4427 self.visit_array_get(type_index)?;
4428 // The `atomic` version has some additional type restrictions.
4429 let elem_ty = self.array_type_at(type_index)?.element_type;
4430 let is_valid_type = match elem_ty {
4431 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4432 StorageType::Val(v) => self
4433 .resources
4434 .is_subtype(v, RefType::ANYREF.shared().unwrap().into()),
4435 _ => false,
4436 };
4437 if !is_valid_type {
4438 bail!(
4439 self.offset,
4440 "invalid type: `array.atomic.get` only allows `i32`, `i64` and subtypes of `anyref`"
4441 );
4442 }
4443 Ok(())
4444 }
4445 fn visit_array_get_s(&mut self, type_index: u32) -> Self::Output {
4446 let array_ty = self.array_type_at(type_index)?;
4447 let elem_ty = array_ty.element_type;
4448 if !elem_ty.is_packed() {
4449 bail!(
4450 self.offset,
4451 "cannot use array.get_s with non-packed storage types"
4452 )
4453 }
4454 self.pop_operand(Some(ValType::I32))?;
4455 self.pop_concrete_ref(true, type_index)?;
4456 self.push_operand(elem_ty.unpack())
4457 }
4458 fn visit_array_atomic_get_s(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4459 self.visit_array_get_s(type_index)?;
4460 // This instruction has the same type restrictions as the non-`atomic` version.
4461 debug_assert!(matches!(
4462 self.array_type_at(type_index)?.element_type,
4463 StorageType::I8 | StorageType::I16
4464 ));
4465 Ok(())
4466 }
4467 fn visit_array_get_u(&mut self, type_index: u32) -> Self::Output {
4468 let array_ty = self.array_type_at(type_index)?;
4469 let elem_ty = array_ty.element_type;
4470 if !elem_ty.is_packed() {
4471 bail!(
4472 self.offset,
4473 "cannot use array.get_u with non-packed storage types"
4474 )
4475 }
4476 self.pop_operand(Some(ValType::I32))?;
4477 self.pop_concrete_ref(true, type_index)?;
4478 self.push_operand(elem_ty.unpack())
4479 }
4480 fn visit_array_atomic_get_u(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4481 self.visit_array_get_u(type_index)?;
4482 // This instruction has the same type restrictions as the non-`atomic` version.
4483 debug_assert!(matches!(
4484 self.array_type_at(type_index)?.element_type,
4485 StorageType::I8 | StorageType::I16
4486 ));
4487 Ok(())
4488 }
4489 fn visit_array_set(&mut self, type_index: u32) -> Self::Output {
4490 let array_ty = self.mutable_array_type_at(type_index)?;
4491 self.pop_operand(Some(array_ty.element_type.unpack()))?;
4492 self.pop_operand(Some(ValType::I32))?;
4493 self.pop_concrete_ref(true, type_index)?;
4494 Ok(())
4495 }
4496 fn visit_array_atomic_set(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4497 self.visit_array_set(type_index)?;
4498 // The `atomic` version has some additional type restrictions.
4499 let elem_ty = self.array_type_at(type_index)?.element_type;
4500 let is_valid_type = match elem_ty {
4501 StorageType::I8 | StorageType::I16 => true,
4502 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4503 StorageType::Val(v) => self
4504 .resources
4505 .is_subtype(v, RefType::ANYREF.shared().unwrap().into()),
4506 };
4507 if !is_valid_type {
4508 bail!(
4509 self.offset,
4510 "invalid type: `array.atomic.set` only allows `i8`, `i16`, `i32`, `i64` and subtypes of `anyref`"
4511 );
4512 }
4513 Ok(())
4514 }
4515 fn visit_array_len(&mut self) -> Self::Output {
4516 self.pop_maybe_shared_ref(AbstractHeapType::Array)?;
4517 self.push_operand(ValType::I32)
4518 }
4519 fn visit_array_fill(&mut self, array_type_index: u32) -> Self::Output {
4520 let array_ty = self.mutable_array_type_at(array_type_index)?;
4521 self.pop_operand(Some(ValType::I32))?;
4522 self.pop_operand(Some(array_ty.element_type.unpack()))?;
4523 self.pop_operand(Some(ValType::I32))?;
4524 self.pop_concrete_ref(true, array_type_index)?;
4525 Ok(())
4526 }
4527 fn visit_array_copy(&mut self, type_index_dst: u32, type_index_src: u32) -> Self::Output {
4528 let array_ty_dst = self.mutable_array_type_at(type_index_dst)?;
4529 let array_ty_src = self.array_type_at(type_index_src)?;
4530 match (array_ty_dst.element_type, array_ty_src.element_type) {
4531 (StorageType::I8, StorageType::I8) => {}
4532 (StorageType::I8, ty) => bail!(
4533 self.offset,
4534 "array types do not match: expected i8, found {ty}"
4535 ),
4536 (StorageType::I16, StorageType::I16) => {}
4537 (StorageType::I16, ty) => bail!(
4538 self.offset,
4539 "array types do not match: expected i16, found {ty}"
4540 ),
4541 (StorageType::Val(dst), StorageType::Val(src)) => {
4542 if !self.resources.is_subtype(src, dst) {
4543 bail!(
4544 self.offset,
4545 "array types do not match: expected {dst}, found {src}"
4546 )
4547 }
4548 }
4549 (StorageType::Val(dst), src) => {
4550 bail!(
4551 self.offset,
4552 "array types do not match: expected {dst}, found {src}"
4553 )
4554 }
4555 }
4556 self.pop_operand(Some(ValType::I32))?;
4557 self.pop_operand(Some(ValType::I32))?;
4558 self.pop_concrete_ref(true, type_index_src)?;
4559 self.pop_operand(Some(ValType::I32))?;
4560 self.pop_concrete_ref(true, type_index_dst)?;
4561 Ok(())
4562 }
4563 fn visit_array_init_data(
4564 &mut self,
4565 array_type_index: u32,
4566 array_data_index: u32,
4567 ) -> Self::Output {
4568 let array_ty = self.mutable_array_type_at(array_type_index)?;
4569 let val_ty = array_ty.element_type.unpack();
4570 match val_ty {
4571 ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => {}
4572 ValType::Ref(_) => bail!(
4573 self.offset,
4574 "invalid array.init_data: array type is not numeric or vector"
4575 ),
4576 }
4577 self.check_data_segment(array_data_index)?;
4578 self.pop_operand(Some(ValType::I32))?;
4579 self.pop_operand(Some(ValType::I32))?;
4580 self.pop_operand(Some(ValType::I32))?;
4581 self.pop_concrete_ref(true, array_type_index)?;
4582 Ok(())
4583 }
4584 fn visit_array_init_elem(&mut self, type_index: u32, elem_index: u32) -> Self::Output {
4585 let array_ty = self.mutable_array_type_at(type_index)?;
4586 let array_ref_ty = match array_ty.element_type.unpack() {
4587 ValType::Ref(rt) => rt,
4588 ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => bail!(
4589 self.offset,
4590 "type mismatch: array.init_elem can only create arrays with reference elements"
4591 ),
4592 };
4593 let elem_ref_ty = self.element_type_at(elem_index)?;
4594 if !self
4595 .resources
4596 .is_subtype(elem_ref_ty.into(), array_ref_ty.into())
4597 {
4598 bail!(
4599 self.offset,
4600 "invalid array.init_elem instruction: element segment {elem_index} type mismatch: \
4601 expected {array_ref_ty}, found {elem_ref_ty}"
4602 )
4603 }
4604 self.pop_operand(Some(ValType::I32))?;
4605 self.pop_operand(Some(ValType::I32))?;
4606 self.pop_operand(Some(ValType::I32))?;
4607 self.pop_concrete_ref(true, type_index)?;
4608 Ok(())
4609 }
4610 fn visit_array_atomic_rmw_add(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4611 self.check_array_atomic_rmw("add", type_index)
4612 }
4613 fn visit_array_atomic_rmw_sub(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4614 self.check_array_atomic_rmw("sub", type_index)
4615 }
4616 fn visit_array_atomic_rmw_and(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4617 self.check_array_atomic_rmw("and", type_index)
4618 }
4619 fn visit_array_atomic_rmw_or(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4620 self.check_array_atomic_rmw("or", type_index)
4621 }
4622 fn visit_array_atomic_rmw_xor(&mut self, _ordering: Ordering, type_index: u32) -> Self::Output {
4623 self.check_array_atomic_rmw("xor", type_index)
4624 }
4625 fn visit_array_atomic_rmw_xchg(
4626 &mut self,
4627 _ordering: Ordering,
4628 type_index: u32,
4629 ) -> Self::Output {
4630 let field = self.mutable_array_type_at(type_index)?;
4631 let is_valid_type = match field.element_type {
4632 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4633 StorageType::Val(v) => self
4634 .resources
4635 .is_subtype(v, RefType::ANYREF.shared().unwrap().into()),
4636 _ => false,
4637 };
4638 if !is_valid_type {
4639 bail!(
4640 self.offset,
4641 "invalid type: `array.atomic.rmw.xchg` only allows `i32`, `i64` and subtypes of `anyref`"
4642 );
4643 }
4644 let elem_ty = field.element_type.unpack();
4645 self.pop_operand(Some(elem_ty))?;
4646 self.pop_operand(Some(ValType::I32))?;
4647 self.pop_concrete_ref(true, type_index)?;
4648 self.push_operand(elem_ty)?;
4649 Ok(())
4650 }
4651 fn visit_array_atomic_rmw_cmpxchg(
4652 &mut self,
4653 _ordering: Ordering,
4654 type_index: u32,
4655 ) -> Self::Output {
4656 let field = self.mutable_array_type_at(type_index)?;
4657 let is_valid_type = match field.element_type {
4658 StorageType::Val(ValType::I32) | StorageType::Val(ValType::I64) => true,
4659 StorageType::Val(v) => self
4660 .resources
4661 .is_subtype(v, RefType::EQREF.shared().unwrap().into()),
4662 _ => false,
4663 };
4664 if !is_valid_type {
4665 bail!(
4666 self.offset,
4667 "invalid type: `array.atomic.rmw.cmpxchg` only allows `i32`, `i64` and subtypes of `eqref`"
4668 );
4669 }
4670 let elem_ty = field.element_type.unpack();
4671 self.pop_operand(Some(elem_ty))?;
4672 self.pop_operand(Some(elem_ty))?;
4673 self.pop_operand(Some(ValType::I32))?;
4674 self.pop_concrete_ref(true, type_index)?;
4675 self.push_operand(elem_ty)?;
4676 Ok(())
4677 }
4678 fn visit_any_convert_extern(&mut self) -> Self::Output {
4679 let any_ref = match self.pop_maybe_shared_ref(AbstractHeapType::Extern)? {
4680 MaybeType::Bottom | MaybeType::UnknownRef(_) => {
4681 MaybeType::UnknownRef(Some(AbstractHeapType::Any))
4682 }
4683 MaybeType::Known(ty) => {
4684 let shared = self.resources.is_shared(ty);
4685 let heap_type = HeapType::Abstract {
4686 shared,
4687 ty: AbstractHeapType::Any,
4688 };
4689 let any_ref = RefType::new(ty.is_nullable(), heap_type).unwrap();
4690 MaybeType::Known(any_ref)
4691 }
4692 };
4693 self.push_operand(any_ref)
4694 }
4695 fn visit_extern_convert_any(&mut self) -> Self::Output {
4696 let extern_ref = match self.pop_maybe_shared_ref(AbstractHeapType::Any)? {
4697 MaybeType::Bottom | MaybeType::UnknownRef(_) => {
4698 MaybeType::UnknownRef(Some(AbstractHeapType::Extern))
4699 }
4700 MaybeType::Known(ty) => {
4701 let shared = self.resources.is_shared(ty);
4702 let heap_type = HeapType::Abstract {
4703 shared,
4704 ty: AbstractHeapType::Extern,
4705 };
4706 let extern_ref = RefType::new(ty.is_nullable(), heap_type).unwrap();
4707 MaybeType::Known(extern_ref)
4708 }
4709 };
4710 self.push_operand(extern_ref)
4711 }
4712 fn visit_ref_test_non_null(&mut self, heap_type: HeapType) -> Self::Output {
4713 self.check_ref_test(false, heap_type)
4714 }
4715 fn visit_ref_test_nullable(&mut self, heap_type: HeapType) -> Self::Output {
4716 self.check_ref_test(true, heap_type)
4717 }
4718 fn visit_ref_cast_non_null(&mut self, heap_type: HeapType) -> Self::Output {
4719 self.check_ref_cast(false, heap_type)
4720 }
4721 fn visit_ref_cast_nullable(&mut self, heap_type: HeapType) -> Self::Output {
4722 self.check_ref_cast(true, heap_type)
4723 }
4724 fn visit_br_on_cast(
4725 &mut self,
4726 relative_depth: u32,
4727 mut from_ref_type: RefType,
4728 mut to_ref_type: RefType,
4729 ) -> Self::Output {
4730 self.resources
4731 .check_ref_type(&mut from_ref_type, self.offset)?;
4732 self.resources
4733 .check_ref_type(&mut to_ref_type, self.offset)?;
4734
4735 if !self
4736 .resources
4737 .is_subtype(to_ref_type.into(), from_ref_type.into())
4738 {
4739 bail!(
4740 self.offset,
4741 "type mismatch: expected {from_ref_type}, found {to_ref_type}"
4742 );
4743 }
4744
4745 let (block_ty, frame_kind) = self.jump(relative_depth)?;
4746 let mut label_types = self.label_types(block_ty, frame_kind)?;
4747
4748 match label_types.next_back() {
4749 Some(label_ty) if self.resources.is_subtype(to_ref_type.into(), label_ty) => {
4750 self.pop_operand(Some(from_ref_type.into()))?;
4751 }
4752 Some(label_ty) => bail!(
4753 self.offset,
4754 "type mismatch: casting to type {to_ref_type}, but it does not match \
4755 label result type {label_ty}"
4756 ),
4757 None => bail!(
4758 self.offset,
4759 "type mismatch: br_on_cast to label with empty types, must have a reference type"
4760 ),
4761 };
4762
4763 self.pop_push_label_types(label_types)?;
4764 let diff_ty = RefType::difference(from_ref_type, to_ref_type);
4765 self.push_operand(diff_ty)?;
4766 Ok(())
4767 }
4768 fn visit_br_on_cast_fail(
4769 &mut self,
4770 relative_depth: u32,
4771 mut from_ref_type: RefType,
4772 mut to_ref_type: RefType,
4773 ) -> Self::Output {
4774 self.resources
4775 .check_ref_type(&mut from_ref_type, self.offset)?;
4776 self.resources
4777 .check_ref_type(&mut to_ref_type, self.offset)?;
4778
4779 if !self
4780 .resources
4781 .is_subtype(to_ref_type.into(), from_ref_type.into())
4782 {
4783 bail!(
4784 self.offset,
4785 "type mismatch: expected {from_ref_type}, found {to_ref_type}"
4786 );
4787 }
4788
4789 let (block_ty, frame_kind) = self.jump(relative_depth)?;
4790 let mut label_tys = self.label_types(block_ty, frame_kind)?;
4791
4792 let diff_ty = RefType::difference(from_ref_type, to_ref_type);
4793 match label_tys.next_back() {
4794 Some(label_ty) if self.resources.is_subtype(diff_ty.into(), label_ty) => {
4795 self.pop_operand(Some(from_ref_type.into()))?;
4796 }
4797 Some(label_ty) => bail!(
4798 self.offset,
4799 "type mismatch: expected label result type {label_ty}, found {diff_ty}"
4800 ),
4801 None => bail!(
4802 self.offset,
4803 "type mismatch: expected a reference type, found nothing"
4804 ),
4805 }
4806
4807 self.pop_push_label_types(label_tys)?;
4808 self.push_operand(to_ref_type)?;
4809 Ok(())
4810 }
4811 fn visit_ref_i31(&mut self) -> Self::Output {
4812 self.pop_operand(Some(ValType::I32))?;
4813 self.push_operand(ValType::Ref(RefType::I31))
4814 }
4815 fn visit_ref_i31_shared(&mut self) -> Self::Output {
4816 self.pop_operand(Some(ValType::I32))?;
4817 self.push_operand(ValType::Ref(
4818 RefType::I31.shared().expect("i31 is abstract"),
4819 ))
4820 }
4821 fn visit_i31_get_s(&mut self) -> Self::Output {
4822 self.pop_maybe_shared_ref(AbstractHeapType::I31)?;
4823 self.push_operand(ValType::I32)
4824 }
4825 fn visit_i31_get_u(&mut self) -> Self::Output {
4826 self.pop_maybe_shared_ref(AbstractHeapType::I31)?;
4827 self.push_operand(ValType::I32)
4828 }
4829 fn visit_try(&mut self, mut ty: BlockType) -> Self::Output {
4830 self.check_block_type(&mut ty)?;
4831 for ty in self.params(ty)?.rev() {
4832 self.pop_operand(Some(ty))?;
4833 }
4834 self.push_ctrl(FrameKind::LegacyTry, ty)?;
4835 Ok(())
4836 }
4837 fn visit_catch(&mut self, index: u32) -> Self::Output {
4838 let frame = self.pop_ctrl()?;
4839 if frame.kind != FrameKind::LegacyTry && frame.kind != FrameKind::LegacyCatch {
4840 bail!(self.offset, "catch found outside of an `try` block");
4841 }
4842 // Start a new frame and push `exnref` value.
4843 let height = self.operands.len();
4844 let init_height = self.inits.len();
4845 self.control.push(Frame {
4846 kind: FrameKind::LegacyCatch,
4847 block_type: frame.block_type,
4848 height,
4849 unreachable: false,
4850 init_height,
4851 });
4852 // Push exception argument types.
4853 let ty = self.exception_tag_at(index)?;
4854 for ty in ty.params() {
4855 self.push_operand(*ty)?;
4856 }
4857 Ok(())
4858 }
4859 fn visit_rethrow(&mut self, relative_depth: u32) -> Self::Output {
4860 // This is not a jump, but we need to check that the `rethrow`
4861 // targets an actual `catch` to get the exception.
4862 let (_, kind) = self.jump(relative_depth)?;
4863 if kind != FrameKind::LegacyCatch && kind != FrameKind::LegacyCatchAll {
4864 bail!(
4865 self.offset,
4866 "invalid rethrow label: target was not a `catch` block"
4867 );
4868 }
4869 self.unreachable()?;
4870 Ok(())
4871 }
4872 fn visit_delegate(&mut self, relative_depth: u32) -> Self::Output {
4873 let frame = self.pop_ctrl()?;
4874 if frame.kind != FrameKind::LegacyTry {
4875 bail!(self.offset, "delegate found outside of an `try` block");
4876 }
4877 // This operation is not a jump, but we need to check the
4878 // depth for validity
4879 let _ = self.jump(relative_depth)?;
4880 for ty in self.results(frame.block_type)? {
4881 self.push_operand(ty)?;
4882 }
4883 Ok(())
4884 }
4885 fn visit_catch_all(&mut self) -> Self::Output {
4886 let frame = self.pop_ctrl()?;
4887 if frame.kind == FrameKind::LegacyCatchAll {
4888 bail!(self.offset, "only one catch_all allowed per `try` block");
4889 } else if frame.kind != FrameKind::LegacyTry && frame.kind != FrameKind::LegacyCatch {
4890 bail!(self.offset, "catch_all found outside of a `try` block");
4891 }
4892 let height = self.operands.len();
4893 let init_height = self.inits.len();
4894 self.control.push(Frame {
4895 kind: FrameKind::LegacyCatchAll,
4896 block_type: frame.block_type,
4897 height,
4898 unreachable: false,
4899 init_height,
4900 });
4901 Ok(())
4902 }
4903 fn visit_cont_new(&mut self, type_index: u32) -> Self::Output {
4904 let cont_ty = self.cont_type_at(type_index)?;
4905 let rt = RefType::concrete(true, cont_ty.0);
4906 self.pop_ref(Some(rt))?;
4907 self.push_concrete_ref(false, type_index)?;
4908 Ok(())
4909 }
4910 fn visit_cont_bind(&mut self, argument_index: u32, result_index: u32) -> Self::Output {
4911 // [ts1 ts1'] -> [ts2]
4912 let arg_cont = self.cont_type_at(argument_index)?;
4913 let arg_func = self.func_type_of_cont_type(arg_cont);
4914 // [ts1''] -> [ts2']
4915 let res_cont = self.cont_type_at(result_index)?;
4916 let res_func = self.func_type_of_cont_type(res_cont);
4917
4918 // Verify that the argument's domain is at least as large as the
4919 // result's domain.
4920 if arg_func.params().len() < res_func.params().len() {
4921 bail!(self.offset, "type mismatch in continuation arguments");
4922 }
4923
4924 let argcnt = arg_func.params().len() - res_func.params().len();
4925
4926 // Check that [ts1'] -> [ts2] <: [ts1''] -> [ts2']
4927 if !self.is_subtype_many(res_func.params(), &arg_func.params()[argcnt..])
4928 || arg_func.results().len() != res_func.results().len()
4929 || !self.is_subtype_many(arg_func.results(), res_func.results())
4930 {
4931 bail!(self.offset, "type mismatch in continuation types");
4932 }
4933
4934 // Check that the continuation is available on the stack.
4935 self.pop_concrete_ref(true, argument_index)?;
4936
4937 // Check that the argument prefix is available on the stack.
4938 for &ty in arg_func.params().iter().take(argcnt).rev() {
4939 self.pop_operand(Some(ty))?;
4940 }
4941
4942 // Construct the result type.
4943 self.push_concrete_ref(false, result_index)?;
4944
4945 Ok(())
4946 }
4947 fn visit_suspend(&mut self, tag_index: u32) -> Self::Output {
4948 let ft = &self.tag_at(tag_index)?;
4949 for &ty in ft.params().iter().rev() {
4950 self.pop_operand(Some(ty))?;
4951 }
4952 for &ty in ft.results() {
4953 self.push_operand(ty)?;
4954 }
4955 Ok(())
4956 }
4957 fn visit_resume(&mut self, type_index: u32, table: ResumeTable) -> Self::Output {
4958 // [ts1] -> [ts2]
4959 let ft = self.check_resume_table(table, type_index)?;
4960 self.pop_concrete_ref(true, type_index)?;
4961 // Check that ts1 are available on the stack.
4962 for &ty in ft.params().iter().rev() {
4963 self.pop_operand(Some(ty))?;
4964 }
4965
4966 // Make ts2 available on the stack.
4967 for &ty in ft.results() {
4968 self.push_operand(ty)?;
4969 }
4970 Ok(())
4971 }
4972 fn visit_resume_throw(
4973 &mut self,
4974 type_index: u32,
4975 tag_index: u32,
4976 table: ResumeTable,
4977 ) -> Self::Output {
4978 // [ts1] -> [ts2]
4979 let ft = self.check_resume_table(table, type_index)?;
4980 // [ts1'] -> []
4981 let tag_ty = self.exception_tag_at(tag_index)?;
4982 if tag_ty.results().len() != 0 {
4983 bail!(self.offset, "type mismatch: non-empty tag result type")
4984 }
4985 self.pop_concrete_ref(true, type_index)?;
4986 // Check that ts1' are available on the stack.
4987 for &ty in tag_ty.params().iter().rev() {
4988 self.pop_operand(Some(ty))?;
4989 }
4990
4991 // Make ts2 available on the stack.
4992 for &ty in ft.results() {
4993 self.push_operand(ty)?;
4994 }
4995 Ok(())
4996 }
4997 fn visit_switch(&mut self, type_index: u32, tag_index: u32) -> Self::Output {
4998 // [t1* (ref null $ct2)] -> [te1*]
4999 let cont_ty = self.cont_type_at(type_index)?;
5000 let func_ty = self.func_type_of_cont_type(cont_ty);
5001 // [] -> [t*]
5002 let tag_ty = self.tag_at(tag_index)?;
5003 if tag_ty.params().len() != 0 {
5004 bail!(self.offset, "type mismatch: non-empty tag parameter type")
5005 }
5006 // Extract the other continuation reference
5007 match func_ty.params().last() {
5008 Some(ValType::Ref(rt)) if rt.is_concrete_type_ref() => {
5009 let other_cont_id = rt
5010 .type_index()
5011 .unwrap()
5012 .unpack()
5013 .as_core_type_id()
5014 .expect("expected canonicalized index");
5015 let sub_ty = self.resources.sub_type_at_id(other_cont_id);
5016 let other_cont_ty =
5017 if let CompositeInnerType::Cont(cont) = &sub_ty.composite_type.inner {
5018 cont
5019 } else {
5020 bail!(self.offset, "non-continuation type");
5021 };
5022 let other_func_ty = self.func_type_of_cont_type(&other_cont_ty);
5023 if func_ty.results().len() != tag_ty.results().len()
5024 || !self.is_subtype_many(func_ty.results(), tag_ty.results())
5025 || other_func_ty.results().len() != tag_ty.results().len()
5026 || !self.is_subtype_many(tag_ty.results(), other_func_ty.results())
5027 {
5028 bail!(self.offset, "type mismatch in continuation types")
5029 }
5030
5031 // Pop the continuation reference.
5032 self.pop_concrete_ref(true, type_index)?;
5033
5034 // Check that the arguments t1* are available on the
5035 // stack.
5036 for &ty in func_ty.params().iter().rev().skip(1) {
5037 self.pop_operand(Some(ty))?;
5038 }
5039
5040 // Make the results t2* available on the stack.
5041 for &ty in other_func_ty.params() {
5042 self.push_operand(ty)?;
5043 }
5044 }
5045 Some(ty) => bail!(
5046 self.offset,
5047 "type mismatch: expected a continuation reference, found {}",
5048 ty_to_str(*ty)
5049 ),
5050 None => bail!(
5051 self.offset,
5052 "type mismatch: instruction requires a continuation reference"
5053 ),
5054 }
5055 Ok(())
5056 }
5057 fn visit_i64_add128(&mut self) -> Result<()> {
5058 self.check_binop128()
5059 }
5060 fn visit_i64_sub128(&mut self) -> Result<()> {
5061 self.check_binop128()
5062 }
5063 fn visit_i64_mul_wide_s(&mut self) -> Result<()> {
5064 self.check_i64_mul_wide()
5065 }
5066 fn visit_i64_mul_wide_u(&mut self) -> Result<()> {
5067 self.check_i64_mul_wide()
5068 }
5069 }
5070
5071 #[derive(Clone, Debug)]
5072 enum Either<A, B> {
5073 A(A),
5074 B(B),
5075 }
5076
5077 impl<A, B> Iterator for Either<A, B>
5078 where
5079 A: Iterator,
5080 B: Iterator<Item = A::Item>,
5081 {
5082 type Item = A::Item;
5083 fn next(&mut self) -> Option<A::Item> {
5084 match self {
5085 Either::A(a) => a.next(),
5086 Either::B(b) => b.next(),
5087 }
5088 }
5089 }
5090
5091 impl<A, B> DoubleEndedIterator for Either<A, B>
5092 where
5093 A: DoubleEndedIterator,
5094 B: DoubleEndedIterator<Item = A::Item>,
5095 {
5096 fn next_back(&mut self) -> Option<A::Item> {
5097 match self {
5098 Either::A(a) => a.next_back(),
5099 Either::B(b) => b.next_back(),
5100 }
5101 }
5102 }
5103
5104 impl<A, B> ExactSizeIterator for Either<A, B>
5105 where
5106 A: ExactSizeIterator,
5107 B: ExactSizeIterator<Item = A::Item>,
5108 {
5109 fn len(&self) -> usize {
5110 match self {
5111 Either::A(a) => a.len(),
5112 Either::B(b) => b.len(),
5113 }
5114 }
5115 }
5116
5117 trait PreciseIterator: ExactSizeIterator + DoubleEndedIterator + Clone + core::fmt::Debug {}
5118 impl<T: ExactSizeIterator + DoubleEndedIterator + Clone + core::fmt::Debug> PreciseIterator for T {}
5119
5120 impl Locals {
5121 /// Defines another group of `count` local variables of type `ty`.
5122 ///
5123 /// Returns `true` if the definition was successful. Local variable
5124 /// definition is unsuccessful in case the amount of total variables
5125 /// after definition exceeds the allowed maximum number.
5126 fn define(&mut self, count: u32, ty: ValType) -> bool {
5127 match self.num_locals.checked_add(count) {
5128 Some(n) => self.num_locals = n,
5129 None => return false,
5130 }
5131 if self.num_locals > (MAX_WASM_FUNCTION_LOCALS as u32) {
5132 return false;
5133 }
5134 for _ in 0..count {
5135 if self.first.len() >= MAX_LOCALS_TO_TRACK {
5136 break;
5137 }
5138 self.first.push(ty);
5139 }
5140 self.all.push((self.num_locals - 1, ty));
5141 true
5142 }
5143
5144 /// Returns the number of defined local variables.
5145 pub(super) fn len_locals(&self) -> u32 {
5146 self.num_locals
5147 }
5148
5149 /// Returns the type of the local variable at the given index if any.
5150 #[inline]
5151 pub(super) fn get(&self, idx: u32) -> Option<ValType> {
5152 match self.first.get(idx as usize) {
5153 Some(ty) => Some(*ty),
5154 None => self.get_bsearch(idx),
5155 }
5156 }
5157
5158 fn get_bsearch(&self, idx: u32) -> Option<ValType> {
5159 match self.all.binary_search_by_key(&idx, |(idx, _)| *idx) {
5160 // If this index would be inserted at the end of the list, then the
5161 // index is out of bounds and we return an error.
5162 Err(i) if i == self.all.len() => None,
5163
5164 // If `Ok` is returned we found the index exactly, or if `Err` is
5165 // returned the position is the one which is the least index
5166 // greater that `idx`, which is still the type of `idx` according
5167 // to our "compressed" representation. In both cases we access the
5168 // list at index `i`.
5169 Ok(i) | Err(i) => Some(self.all[i].1),
5170 }
5171 }
5172 }
5173
5174 impl<R> ModuleArity for WasmProposalValidator<'_, '_, R>
5175 where
5176 R: WasmModuleResources,
5177 {
5178 fn tag_type_arity(&self, at: u32) -> Option<(u32, u32)> {
5179 self.0
5180 .resources
5181 .tag_at(at)
5182 .map(|x| (x.params().len() as u32, x.results().len() as u32))
5183 }
5184
5185 fn type_index_of_function(&self, function_idx: u32) -> Option<u32> {
5186 self.0.resources.type_index_of_function(function_idx)
5187 }
5188
5189 fn sub_type_at(&self, type_idx: u32) -> Option<&SubType> {
5190 Some(self.0.sub_type_at(type_idx).ok()?)
5191 }
5192
5193 fn func_type_of_cont_type(&self, c: &ContType) -> Option<&FuncType> {
5194 Some(self.0.func_type_of_cont_type(c))
5195 }
5196
5197 fn sub_type_of_ref_type(&self, rt: &RefType) -> Option<&SubType> {
5198 let id = rt.type_index()?.as_core_type_id()?;
5199 Some(self.0.resources.sub_type_at_id(id))
5200 }
5201
5202 fn control_stack_height(&self) -> u32 {
5203 self.0.control.len() as u32
5204 }
5205
5206 fn label_block(&self, depth: u32) -> Option<(BlockType, FrameKind)> {
5207 self.0.jump(depth).ok()
5208 }
5209 }