wasmparser/validator/
func.rs

1use super::operators::{Frame, OperatorValidator, OperatorValidatorAllocations};
2use crate::{BinaryReader, OperatorsReader, Result, ValType, VisitOperator};
3use crate::{FunctionBody, ModuleArity, Operator, WasmFeatures, WasmModuleResources};
4
5/// Resources necessary to perform validation of a function.
6///
7/// This structure is created by
8/// [`Validator::code_section_entry`](crate::Validator::code_section_entry) and
9/// is created per-function in a WebAssembly module. This structure is suitable
10/// for sending to other threads while the original
11/// [`Validator`](crate::Validator) continues processing other functions.
12#[derive(Debug)]
13pub struct FuncToValidate<T> {
14    /// Reusable, heap allocated resources to drive the Wasm validation.
15    pub resources: T,
16    /// The core Wasm function index being validated.
17    pub index: u32,
18    /// The core Wasm type index of the function being validated,
19    /// defining the results and parameters to the function.
20    pub ty: u32,
21    /// The Wasm features enabled to validate the function.
22    pub features: WasmFeatures,
23}
24
25impl<T: WasmModuleResources> FuncToValidate<T> {
26    /// Converts this [`FuncToValidate`] into a [`FuncValidator`] using the
27    /// `allocs` provided.
28    ///
29    /// This method, in conjunction with [`FuncValidator::into_allocations`],
30    /// provides a means to reuse allocations across validation of each
31    /// individual function. Note that it is also sufficient to call this
32    /// method with `Default::default()` if no prior allocations are
33    /// available.
34    ///
35    /// # Panics
36    ///
37    /// If a `FuncToValidate` was created with an invalid `ty` index then this
38    /// function will panic.
39    pub fn into_validator(self, allocs: FuncValidatorAllocations) -> FuncValidator<T> {
40        let FuncToValidate {
41            resources,
42            index,
43            ty,
44            features,
45        } = self;
46        let validator =
47            OperatorValidator::new_func(ty, 0, &features, &resources, allocs.0).unwrap();
48        FuncValidator {
49            validator,
50            resources,
51            index,
52        }
53    }
54}
55
56/// Validation context for a WebAssembly function.
57///
58/// This is a finalized validator which is ready to process a [`FunctionBody`].
59/// This is created from the [`FuncToValidate::into_validator`] method.
60pub struct FuncValidator<T> {
61    validator: OperatorValidator,
62    resources: T,
63    index: u32,
64}
65
66impl<T: WasmModuleResources> ModuleArity for FuncValidator<T> {
67    fn sub_type_at(&self, type_idx: u32) -> Option<&crate::SubType> {
68        self.resources.sub_type_at(type_idx)
69    }
70
71    fn tag_type_arity(&self, at: u32) -> Option<(u32, u32)> {
72        let ty = self.resources.tag_at(at)?;
73        Some((
74            u32::try_from(ty.params().len()).unwrap(),
75            u32::try_from(ty.results().len()).unwrap(),
76        ))
77    }
78
79    fn type_index_of_function(&self, func_idx: u32) -> Option<u32> {
80        self.resources.type_index_of_function(func_idx)
81    }
82
83    fn func_type_of_cont_type(&self, cont_ty: &crate::ContType) -> Option<&crate::FuncType> {
84        let id = cont_ty.0.as_core_type_id()?;
85        Some(self.resources.sub_type_at_id(id).unwrap_func())
86    }
87
88    fn sub_type_of_ref_type(&self, rt: &crate::RefType) -> Option<&crate::SubType> {
89        let id = rt.type_index()?.as_core_type_id()?;
90        Some(self.resources.sub_type_at_id(id))
91    }
92
93    fn control_stack_height(&self) -> u32 {
94        u32::try_from(self.validator.control_stack_height()).unwrap()
95    }
96
97    fn label_block(&self, depth: u32) -> Option<(crate::BlockType, crate::FrameKind)> {
98        self.validator.jump(depth)
99    }
100}
101
102/// External handle to the internal allocations used during function validation.
103///
104/// This is created with either the `Default` implementation or with
105/// [`FuncValidator::into_allocations`]. It is then passed as an argument to
106/// [`FuncToValidate::into_validator`] to provide a means of reusing allocations
107/// between each function.
108#[derive(Default)]
109pub struct FuncValidatorAllocations(OperatorValidatorAllocations);
110
111impl<T: WasmModuleResources> FuncValidator<T> {
112    /// Convenience function to validate an entire function's body.
113    ///
114    /// You may not end up using this in final implementations because you'll
115    /// often want to interleave validation with parsing.
116    pub fn validate(&mut self, body: &FunctionBody<'_>) -> Result<()> {
117        let mut reader = body.get_binary_reader();
118        self.read_locals(&mut reader)?;
119        #[cfg(feature = "features")]
120        {
121            reader.set_features(self.validator.features);
122        }
123        let mut ops = OperatorsReader::new(reader);
124        while !ops.eof() {
125            // In a debug build, verify that the validator's pops and pushes to and from
126            // the operand stack match the operator's arity.
127            #[cfg(debug_assertions)]
128            let (mut ops_before, arity) = {
129                let op = ops.clone().read()?;
130                let arity = op.operator_arity(&self.visitor(ops.original_position()));
131                (ops.clone(), arity)
132            };
133
134            ops.visit_operator(&mut self.visitor(ops.original_position()))??;
135
136            #[cfg(debug_assertions)]
137            {
138                let (params, results) = arity.ok_or(format_err!(
139                    ops.original_position(),
140                    "could not calculate operator arity"
141                ))?;
142
143                // Analyze the log to determine the actual, externally visible
144                // pop/push count. This allows us to hide the fact that we might
145                // push and then pop a temporary while validating an
146                // instruction, which shouldn't be visible from the outside.
147                let mut pop_count = 0;
148                let mut push_count = 0;
149                for op in self.validator.pop_push_log.drain(..) {
150                    match op {
151                        true => push_count += 1,
152                        false if push_count > 0 => push_count -= 1,
153                        false => pop_count += 1,
154                    }
155                }
156
157                if pop_count != params || push_count != results {
158                    panic!(
159                        "\
160arity mismatch in validation
161    operator: {:?}
162    expected: {params} -> {results}
163    got       {pop_count} -> {push_count}",
164                        ops_before.read()?,
165                    );
166                }
167            }
168        }
169        ops.finish()
170    }
171
172    /// Reads the local definitions from the given `BinaryReader`, often sourced
173    /// from a `FunctionBody`.
174    ///
175    /// This function will automatically advance the `BinaryReader` forward,
176    /// leaving reading operators up to the caller afterwards.
177    pub fn read_locals(&mut self, reader: &mut BinaryReader<'_>) -> Result<()> {
178        for _ in 0..reader.read_var_u32()? {
179            let offset = reader.original_position();
180            let cnt = reader.read()?;
181            let ty = reader.read()?;
182            self.define_locals(offset, cnt, ty)?;
183        }
184        Ok(())
185    }
186
187    /// Defines locals into this validator.
188    ///
189    /// This should be used if the application is already reading local
190    /// definitions and there's no need to re-parse the function again.
191    pub fn define_locals(&mut self, offset: usize, count: u32, ty: ValType) -> Result<()> {
192        self.validator
193            .define_locals(offset, count, ty, &self.resources)
194    }
195
196    /// Validates the next operator in a function.
197    ///
198    /// This functions is expected to be called once-per-operator in a
199    /// WebAssembly function. Each operator's offset in the original binary and
200    /// the operator itself are passed to this function to provide more useful
201    /// error messages.
202    pub fn op(&mut self, offset: usize, operator: &Operator<'_>) -> Result<()> {
203        self.visitor(offset).visit_operator(operator)
204    }
205
206    /// Get the operator visitor for the next operator in the function.
207    ///
208    /// The returned visitor is intended to visit just one instruction at the `offset`.
209    ///
210    /// # Example
211    ///
212    /// ```
213    /// # use wasmparser::{WasmModuleResources, FuncValidator, FunctionBody, Result};
214    /// pub fn validate<R>(validator: &mut FuncValidator<R>, body: &FunctionBody<'_>) -> Result<()>
215    /// where R: WasmModuleResources
216    /// {
217    ///     let mut operator_reader = body.get_operators_reader()?;
218    ///     while !operator_reader.eof() {
219    ///         let mut visitor = validator.visitor(operator_reader.original_position());
220    ///         operator_reader.visit_operator(&mut visitor)??;
221    ///     }
222    ///     operator_reader.finish()
223    /// }
224    /// ```
225    pub fn visitor<'this, 'a: 'this>(
226        &'this mut self,
227        offset: usize,
228    ) -> impl VisitOperator<'a, Output = Result<()>> + ModuleArity + 'this {
229        self.validator.with_resources(&self.resources, offset)
230    }
231
232    /// Same as [`FuncValidator::visitor`] except that the returned type
233    /// implements the [`VisitSimdOperator`](crate::VisitSimdOperator) trait as
234    /// well.
235    #[cfg(feature = "simd")]
236    pub fn simd_visitor<'this, 'a: 'this>(
237        &'this mut self,
238        offset: usize,
239    ) -> impl crate::VisitSimdOperator<'a, Output = Result<()>> + ModuleArity + 'this {
240        self.validator.with_resources_simd(&self.resources, offset)
241    }
242
243    /// Returns the Wasm features enabled for this validator.
244    pub fn features(&self) -> &WasmFeatures {
245        &self.validator.features
246    }
247
248    /// Returns the underlying module resources that this validator is using.
249    pub fn resources(&self) -> &T {
250        &self.resources
251    }
252
253    /// The index of the function within the module's function index space that
254    /// is being validated.
255    pub fn index(&self) -> u32 {
256        self.index
257    }
258
259    /// Returns the number of defined local variables in the function.
260    pub fn len_locals(&self) -> u32 {
261        self.validator.locals.len_locals()
262    }
263
264    /// Returns the type of the local variable at the given `index` if any.
265    pub fn get_local_type(&self, index: u32) -> Option<ValType> {
266        self.validator.locals.get(index)
267    }
268
269    /// Get the current height of the operand stack.
270    ///
271    /// This returns the height of the whole operand stack for this function,
272    /// not just for the current control frame.
273    pub fn operand_stack_height(&self) -> u32 {
274        self.validator.operand_stack_height() as u32
275    }
276
277    /// Returns the optional value type of the value operand at the given
278    /// `depth` from the top of the operand stack.
279    ///
280    /// - Returns `None` if the `depth` is out of bounds.
281    /// - Returns `Some(None)` if there is a value with unknown type
282    /// at the given `depth`.
283    ///
284    /// # Note
285    ///
286    /// A `depth` of 0 will refer to the last operand on the stack.
287    pub fn get_operand_type(&self, depth: usize) -> Option<Option<ValType>> {
288        self.validator.peek_operand_at(depth)
289    }
290
291    /// Returns the number of frames on the control flow stack.
292    ///
293    /// This returns the height of the whole control stack for this function,
294    /// not just for the current control frame.
295    pub fn control_stack_height(&self) -> u32 {
296        self.validator.control_stack_height() as u32
297    }
298
299    /// Returns a shared reference to the control flow [`Frame`] of the
300    /// control flow stack at the given `depth` if any.
301    ///
302    /// Returns `None` if the `depth` is out of bounds.
303    ///
304    /// # Note
305    ///
306    /// A `depth` of 0 will refer to the last frame on the stack.
307    pub fn get_control_frame(&self, depth: usize) -> Option<&Frame> {
308        self.validator.get_frame(depth)
309    }
310
311    /// Consumes this validator and returns the underlying allocations that
312    /// were used during the validation process.
313    ///
314    /// The returned value here can be paired with
315    /// [`FuncToValidate::into_validator`] to reuse the allocations already
316    /// created by this validator.
317    pub fn into_allocations(self) -> FuncValidatorAllocations {
318        FuncValidatorAllocations(self.validator.into_allocations())
319    }
320}
321
322#[cfg(test)]
323mod tests {
324    use super::*;
325    use crate::types::CoreTypeId;
326    use crate::{HeapType, Parser, RefType, Validator};
327    use alloc::vec::Vec;
328
329    struct EmptyResources(crate::SubType);
330
331    impl Default for EmptyResources {
332        fn default() -> Self {
333            EmptyResources(crate::SubType {
334                supertype_idx: None,
335                is_final: true,
336                composite_type: crate::CompositeType {
337                    inner: crate::CompositeInnerType::Func(crate::FuncType::new([], [])),
338                    shared: false,
339                },
340            })
341        }
342    }
343
344    impl WasmModuleResources for EmptyResources {
345        fn table_at(&self, _at: u32) -> Option<crate::TableType> {
346            todo!()
347        }
348        fn memory_at(&self, _at: u32) -> Option<crate::MemoryType> {
349            todo!()
350        }
351        fn tag_at(&self, _at: u32) -> Option<&crate::FuncType> {
352            todo!()
353        }
354        fn global_at(&self, _at: u32) -> Option<crate::GlobalType> {
355            todo!()
356        }
357        fn sub_type_at(&self, _type_idx: u32) -> Option<&crate::SubType> {
358            Some(&self.0)
359        }
360        fn sub_type_at_id(&self, _id: CoreTypeId) -> &crate::SubType {
361            todo!()
362        }
363        fn type_id_of_function(&self, _at: u32) -> Option<CoreTypeId> {
364            todo!()
365        }
366        fn type_index_of_function(&self, _at: u32) -> Option<u32> {
367            todo!()
368        }
369        fn check_heap_type(&self, _t: &mut HeapType, _offset: usize) -> Result<()> {
370            Ok(())
371        }
372        fn top_type(&self, _heap_type: &HeapType) -> HeapType {
373            todo!()
374        }
375        fn element_type_at(&self, _at: u32) -> Option<crate::RefType> {
376            todo!()
377        }
378        fn is_subtype(&self, _t1: ValType, _t2: ValType) -> bool {
379            todo!()
380        }
381        fn is_shared(&self, _ty: RefType) -> bool {
382            todo!()
383        }
384        fn element_count(&self) -> u32 {
385            todo!()
386        }
387        fn data_count(&self) -> Option<u32> {
388            todo!()
389        }
390        fn is_function_referenced(&self, _idx: u32) -> bool {
391            todo!()
392        }
393    }
394
395    #[test]
396    fn operand_stack_height() {
397        let mut v = FuncToValidate {
398            index: 0,
399            ty: 0,
400            resources: EmptyResources::default(),
401            features: Default::default(),
402        }
403        .into_validator(Default::default());
404
405        // Initially zero values on the stack.
406        assert_eq!(v.operand_stack_height(), 0);
407
408        // Pushing a constant value makes use have one value on the stack.
409        assert!(v.op(0, &Operator::I32Const { value: 0 }).is_ok());
410        assert_eq!(v.operand_stack_height(), 1);
411
412        // Entering a new control block does not affect the stack height.
413        assert!(
414            v.op(
415                1,
416                &Operator::Block {
417                    blockty: crate::BlockType::Empty
418                }
419            )
420            .is_ok()
421        );
422        assert_eq!(v.operand_stack_height(), 1);
423
424        // Pushing another constant value makes use have two values on the stack.
425        assert!(v.op(2, &Operator::I32Const { value: 99 }).is_ok());
426        assert_eq!(v.operand_stack_height(), 2);
427    }
428
429    fn assert_arity(wat: &str, expected: Vec<Vec<(u32, u32)>>) {
430        let wasm = wat::parse_str(wat).unwrap();
431        assert!(Validator::new().validate_all(&wasm).is_ok());
432
433        let parser = Parser::new(0);
434        let mut validator = Validator::new();
435
436        let mut actual = vec![];
437
438        for payload in parser.parse_all(&wasm) {
439            let payload = payload.unwrap();
440            match payload {
441                crate::Payload::CodeSectionEntry(body) => {
442                    let mut arity = vec![];
443                    let mut func_validator = validator
444                        .code_section_entry(&body)
445                        .unwrap()
446                        .into_validator(FuncValidatorAllocations::default());
447                    let ops = body.get_operators_reader().unwrap();
448                    for op in ops.into_iter() {
449                        let op = op.unwrap();
450                        arity.push(
451                            op.operator_arity(&func_validator)
452                                .expect("valid operators should have arity"),
453                        );
454                        func_validator.op(usize::MAX, &op).expect("should be valid");
455                    }
456                    actual.push(arity);
457                }
458                p => {
459                    validator.payload(&p).unwrap();
460                }
461            }
462        }
463
464        assert_eq!(actual, expected);
465    }
466
467    #[test]
468    fn arity_smoke_test() {
469        let wasm = r#"
470            (module
471                (type $pair (struct (field i32) (field i32)))
472
473                (func $add (param i32 i32) (result i32)
474                    local.get 0
475                    local.get 1
476                    i32.add
477                )
478
479                (func $f (param i32 i32) (result (ref null $pair))
480                    local.get 0
481                    local.get 1
482                    call $add
483                    if (result (ref null $pair))
484                    local.get 0
485                    local.get 1
486                      struct.new $pair
487                    else
488                      unreachable
489                      i32.add
490                      unreachable
491                    end
492                )
493            )
494        "#;
495
496        assert_arity(
497            wasm,
498            vec![
499                // $add
500                vec![
501                    // local.get 0
502                    (0, 1),
503                    // local.get 1
504                    (0, 1),
505                    // i32.add
506                    (2, 1),
507                    // end
508                    (1, 1),
509                ],
510                // $f
511                vec![
512                    // local.get 0
513                    (0, 1),
514                    // local.get 1
515                    (0, 1),
516                    // call $add
517                    (2, 1),
518                    // if
519                    (1, 0),
520                    // local.get 0
521                    (0, 1),
522                    // local.get 1
523                    (0, 1),
524                    // struct.new $pair
525                    (2, 1),
526                    // else
527                    (1, 0),
528                    // unreachable,
529                    (0, 0),
530                    // i32.add
531                    (2, 1),
532                    // unreachable
533                    (0, 0),
534                    // end
535                    (1, 1),
536                    // implicit end
537                    (1, 1),
538                ],
539            ],
540        );
541    }
542
543    #[test]
544    fn arity_if_no_else_same_params_and_results() {
545        let wasm = r#"
546            (module
547                (func (export "f") (param i64 i32) (result i64)
548                    (local.get 0)
549                    (local.get 1)
550                    ;; If with no else. Same number of params and results.
551                    if (param i64) (result i64)
552                        drop
553                        i64.const -1
554                    end
555                )
556            )
557        "#;
558
559        assert_arity(
560            wasm,
561            vec![vec![
562                // local.get 0
563                (0, 1),
564                // local.get 1
565                (0, 1),
566                // if
567                (2, 1),
568                // drop
569                (1, 0),
570                // i64.const -1
571                (0, 1),
572                // end
573                (1, 1),
574                // implicit end
575                (1, 1),
576            ]],
577        );
578    }
579
580    #[test]
581    fn arity_br_table() {
582        let wasm = r#"
583            (module
584                (func (export "f") (result i32 i32)
585                    i32.const 0
586                    i32.const 1
587                    i32.const 2
588                    br_table 0 0
589                )
590            )
591        "#;
592
593        assert_arity(
594            wasm,
595            vec![vec![
596                // i32.const 0
597                (0, 1),
598                // i32.const 1
599                (0, 1),
600                // i32.const 2
601                (0, 1),
602                // br_table
603                (3, 0),
604                // implicit end
605                (2, 2),
606            ]],
607        );
608    }
609}