Struct wasm::core::reader::WasmReader

source ·
pub struct WasmReader<'a> {
    pub full_wasm_binary: &'a [u8],
    pub pc: usize,
}
Expand description

A struct for managing and reading WASM bytecode

Its purpose is to abstract parsing basic WASM values from the bytecode.

Fields§

§full_wasm_binary: &'a [u8]

Entire WASM binary as slice

§pc: usize

Current program counter, i. e. index of the next byte to be consumed from the WASM binary

§Correctness Note

The pc points to the next byte to be consumed from the WASM binary. Therefore, after consuming last byte, this cursor will advance past the last byte; for a WASM binary that is 100 bytes long (valid indexes start with 0 and end with 99), the pc therefore can become 100. However, it can not advance further.

The table below illustrates this with an example for a WASM binary that is 5 bytes long:

Index0123456
full_wasm_binary[index]0xaa0xbb0xcc0xee0xff--
Valid pc position?

Implementations§

source§

impl WasmReader<'_>

source

pub fn read_u8(&mut self) -> Result<u8>

Note: If Err, the WasmReader object is no longer guaranteed to be in a valid state

source

pub fn read_var_u32(&mut self) -> Result<u32>

Parses a variable-length u32 as specified by LEB128. Note: If Err, the WasmReader object is no longer guaranteed to be in a valid state

source

pub fn read_var_f64(&mut self) -> Result<u64>

source

pub fn read_var_i32(&mut self) -> Result<i32>

source

pub fn read_var_f32(&mut self) -> Result<u32>

source

pub fn read_var_i64(&mut self) -> Result<i64>

source

pub fn read_name(&mut self) -> Result<&str>

Note: If Err, the WasmReader object is no longer guaranteed to be in a valid state

source

pub fn read_vec_enumerated<T, F>(&mut self, read_element: F) -> Result<Vec<T>>
where F: FnMut(&mut WasmReader<'_>, usize) -> Result<T>,

source

pub fn read_vec<T, F>(&mut self, read_element: F) -> Result<Vec<T>>
where F: FnMut(&mut WasmReader<'_>) -> Result<T>,

Note: If Err, the WasmReader object is no longer guaranteed to be in a valid state

source§

impl<'a> WasmReader<'a>

source

pub const fn new(wasm: &'a [u8]) -> Self

Initialize a new WasmReader from a WASM byte slice

source

pub fn move_start_to(&mut self, span: Span) -> Result<()>

Advance the cursor to the first byte of the provided Span and validates that entire Span fits the WASM binary

§Note

This allows setting the pc to one byte past the end of full_wasm_binary, if the Span’s length is 0. For further information, refer to the field documentation of pc.

source

pub fn remaining_bytes(&self) -> &[u8]

Byte slice to the remainder of the WASM binary, beginning from the current pc

source

pub fn make_span(&self, len: usize) -> Result<Span>

Create a Span starting from pc for the next len bytes

Verifies the span to fit the WASM binary, i.e. using this span to index the WASM binary will not yield an error.

source

pub fn strip_bytes<const N: usize>(&mut self) -> Result<[u8; N]>

Take N bytes starting from pc, then advance the pc by N

This yields back an array of the correct length

§Note

This allows setting the pc to one byte past the end of full_wasm_binary, if N equals the remaining bytes slice’s length. For further information, refer to the [field documentation of pc] (WasmReader::pc).

source

pub fn peek_u8(&self) -> Result<u8>

Read the current byte without advancing the pc

May yield an error if the pc advanced past the end of the WASM binary slice

source

pub fn measure_num_read_bytes<T>( &mut self, f: impl FnOnce(&mut WasmReader<'_>) -> Result<T> ) -> Result<(T, usize)>

Call a closure that may mutate the WasmReader

Returns a tuple of the closure’s return value and the number of bytes that the WasmReader was advanced by.

§Panics

May panic if the closure moved the pc backwards, e.g. when move_start_to is called.

source

pub fn skip(&mut self, num_bytes: usize) -> Result<()>

Skip num_bytes, advancing the pc accordingly

§Note

This can move the pc past the last byte of the WASM binary, so that reading more than 0 further bytes would panick. However, it can not move the pc any further than that, instead an error is returned. For further information, refer to the [field documentation of pc] (WasmReader::pc).

source

pub fn into_inner(self) -> &'a [u8]

Consumes Self, yielding back the internal reference to the WASM binary

source

pub fn handle_transaction<T, E>( &mut self, f: impl FnOnce(&mut WasmReader<'a>) -> Result<T, E> ) -> Result<T, E>

A wrapper function for reads with transaction-like behavior.

The provided closure will be called with &mut self and its result will be returned. However if the closure returns Err(_), self will be reset as if the closure was never called.

Trait Implementations§

source§

impl<'a> Clone for WasmReader<'a>

source§

fn clone(&self) -> WasmReader<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Index<Span> for WasmReader<'a>

§

type Output = [u8]

The returned type after indexing.
source§

fn index(&self, index: Span) -> &'a Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for WasmReader<'a>

§

impl<'a> RefUnwindSafe for WasmReader<'a>

§

impl<'a> Send for WasmReader<'a>

§

impl<'a> Sync for WasmReader<'a>

§

impl<'a> Unpin for WasmReader<'a>

§

impl<'a> UnwindSafe for WasmReader<'a>

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.

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.