FuncValidator

Struct FuncValidator 

pub struct FuncValidator<T> { /* private fields */ }
Expand description

Validation context for a WebAssembly function.

This is a finalized validator which is ready to process a FunctionBody. This is created from the FuncToValidate::into_validator method.

Implementations§

§

impl<T> FuncValidator<T>

pub fn validate( &mut self, body: &FunctionBody<'_>, ) -> Result<(), BinaryReaderError>

Convenience function to validate an entire function’s body.

You may not end up using this in final implementations because you’ll often want to interleave validation with parsing.

pub fn read_locals( &mut self, reader: &mut BinaryReader<'_>, ) -> Result<(), BinaryReaderError>

Reads the local definitions from the given BinaryReader, often sourced from a FunctionBody.

This function will automatically advance the BinaryReader forward, leaving reading operators up to the caller afterwards.

pub fn define_locals( &mut self, offset: usize, count: u32, ty: ValType, ) -> Result<(), BinaryReaderError>

Defines locals into this validator.

This should be used if the application is already reading local definitions and there’s no need to re-parse the function again.

pub fn op( &mut self, offset: usize, operator: &Operator<'_>, ) -> Result<(), BinaryReaderError>

Validates the next operator in a function.

This functions is expected to be called once-per-operator in a WebAssembly function. Each operator’s offset in the original binary and the operator itself are passed to this function to provide more useful error messages.

pub fn visitor<'this, 'a>( &'this mut self, offset: usize, ) -> impl VisitOperator<'a, Output = Result<(), BinaryReaderError>> + ModuleArity + FrameStack + 'this
where 'a: 'this,

Get the operator visitor for the next operator in the function.

The returned visitor is intended to visit just one instruction at the offset.

§Example
pub fn validate<R>(validator: &mut FuncValidator<R>, body: &FunctionBody<'_>) -> Result<()>
where R: WasmModuleResources
{
    let mut operator_reader = body.get_binary_reader_for_operators()?;
    while !operator_reader.eof() {
        let mut visitor = validator.visitor(operator_reader.original_position());
        operator_reader.visit_operator(&mut visitor)??;
    }
    operator_reader.finish_expression(&validator.visitor(operator_reader.original_position()))
}

pub fn simd_visitor<'this, 'a>( &'this mut self, offset: usize, ) -> impl VisitSimdOperator<'a> + ModuleArity + 'this
where 'a: 'this,

Same as FuncValidator::visitor except that the returned type implements the VisitSimdOperator trait as well.

pub fn features(&self) -> &WasmFeatures

Returns the Wasm features enabled for this validator.

pub fn resources(&self) -> &T

Returns the underlying module resources that this validator is using.

pub fn index(&self) -> u32

The index of the function within the module’s function index space that is being validated.

pub fn len_locals(&self) -> u32

Returns the number of defined local variables in the function.

pub fn get_local_type(&self, index: u32) -> Option<ValType>

Returns the type of the local variable at the given index if any.

pub fn operand_stack_height(&self) -> u32

Get the current height of the operand stack.

This returns the height of the whole operand stack for this function, not just for the current control frame.

pub fn get_operand_type(&self, depth: usize) -> Option<Option<ValType>>

Returns the optional value type of the value operand at the given depth from the top of the operand stack.

  • Returns None if the depth is out of bounds.
  • Returns Some(None) if there is a value with unknown type at the given depth.
§Note

A depth of 0 will refer to the last operand on the stack.

pub fn control_stack_height(&self) -> u32

Returns the number of frames on the control flow stack.

This returns the height of the whole control stack for this function, not just for the current control frame.

pub fn get_control_frame(&self, depth: usize) -> Option<&Frame>

Returns a shared reference to the control flow Frame of the control flow stack at the given depth if any.

Returns None if the depth is out of bounds.

§Note

A depth of 0 will refer to the last frame on the stack.

pub fn into_allocations(self) -> FuncValidatorAllocations

Consumes this validator and returns the underlying allocations that were used during the validation process.

The returned value here can be paired with FuncToValidate::into_validator to reuse the allocations already created by this validator.

Trait Implementations§

§

impl<T> ModuleArity for FuncValidator<T>

§

fn sub_type_at(&self, type_idx: u32) -> Option<&SubType>

Type with given index
§

fn tag_type_arity(&self, at: u32) -> Option<(u32, u32)>

Arity (param and result counts) of tag with given index
§

fn type_index_of_function(&self, func_idx: u32) -> Option<u32>

Type index of function with given index
§

fn func_type_of_cont_type(&self, cont_ty: &ContType) -> Option<&FuncType>

Function type for a given continuation type
§

fn sub_type_of_ref_type(&self, rt: &RefType) -> Option<&SubType>

Sub type for a given reference type
§

fn control_stack_height(&self) -> u32

Current height of control stack
§

fn label_block(&self, depth: u32) -> Option<(BlockType, FrameKind)>

BlockType and FrameKind of label with given index
§

fn sub_type_arity(&self, t: &SubType) -> Option<(u32, u32)>

Computes arity of given SubType
§

fn block_type_arity(&self, ty: BlockType) -> Option<(u32, u32)>

Computes arity of given BlockType

Auto Trait Implementations§

§

impl<T> Freeze for FuncValidator<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for FuncValidator<T>
where T: RefUnwindSafe,

§

impl<T> Send for FuncValidator<T>
where T: Send,

§

impl<T> Sync for FuncValidator<T>
where T: Sync,

§

impl<T> Unpin for FuncValidator<T>
where T: Unpin,

§

impl<T> UnwindSafe for FuncValidator<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more