Struct wasmparser::FuncValidator
source · 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§
source§impl<T: WasmModuleResources> FuncValidator<T>
impl<T: WasmModuleResources> FuncValidator<T>
sourcepub fn validate(&mut self, body: &FunctionBody<'_>) -> Result<()>
pub fn validate(&mut self, body: &FunctionBody<'_>) -> Result<()>
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.
sourcepub fn read_locals(&mut self, reader: &mut BinaryReader<'_>) -> Result<()>
pub fn read_locals(&mut self, reader: &mut BinaryReader<'_>) -> Result<()>
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.
sourcepub fn define_locals(
&mut self,
offset: usize,
count: u32,
ty: ValType,
) -> Result<()>
pub fn define_locals( &mut self, offset: usize, count: u32, ty: ValType, ) -> Result<()>
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.
sourcepub fn op(&mut self, offset: usize, operator: &Operator<'_>) -> Result<()>
pub fn op(&mut self, offset: usize, operator: &Operator<'_>) -> Result<()>
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.
sourcepub fn visitor<'this, 'a: 'this>(
&'this mut self,
offset: usize,
) -> impl VisitOperator<'a, Output = Result<()>> + 'this
pub fn visitor<'this, 'a: 'this>( &'this mut self, offset: usize, ) -> impl VisitOperator<'a, Output = Result<()>> + '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();
while !operator_reader.eof() {
let mut visitor = validator.visitor(operator_reader.original_position());
operator_reader.visit_operator(&mut visitor)??;
}
validator.finish(operator_reader.original_position())
}
sourcepub fn finish(&mut self, offset: usize) -> Result<()>
pub fn finish(&mut self, offset: usize) -> Result<()>
Function that must be called after the last opcode has been processed.
This will validate that the function was properly terminated with the
end
opcode. If this function is not called then the function will not
be properly validated.
The offset
provided to this function will be used as a position for an
error if validation fails.
sourcepub fn resources(&self) -> &T
pub fn resources(&self) -> &T
Returns the underlying module resources that this validator is using.
sourcepub fn index(&self) -> u32
pub fn index(&self) -> u32
The index of the function within the module’s function index space that is being validated.
sourcepub fn len_locals(&self) -> u32
pub fn len_locals(&self) -> u32
Returns the number of defined local variables in the function.
sourcepub fn get_local_type(&self, index: u32) -> Option<ValType>
pub fn get_local_type(&self, index: u32) -> Option<ValType>
Returns the type of the local variable at the given index
if any.
sourcepub fn operand_stack_height(&self) -> u32
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.
sourcepub fn get_operand_type(&self, depth: usize) -> Option<Option<ValType>>
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 thedepth
is out of bounds. - Returns
Some(None)
if there is a value with unknown type at the givendepth
.
§Note
A depth
of 0 will refer to the last operand on the stack.
sourcepub fn control_stack_height(&self) -> u32
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.
sourcepub fn get_control_frame(&self, depth: usize) -> Option<&Frame>
pub fn get_control_frame(&self, depth: usize) -> Option<&Frame>
sourcepub fn into_allocations(self) -> FuncValidatorAllocations
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.