pub struct BinaryReader<'a> { /* private fields */ }
Expand description
A binary reader of the WebAssembly structures and types.
Implementations§
Source§impl BinaryReader<'_>
impl BinaryReader<'_>
Sourcepub fn operator_arity(&self, module: &impl ModuleArity) -> Result<(u32, u32)>
pub fn operator_arity(&self, module: &impl ModuleArity) -> Result<(u32, u32)>
Read the next operator and compute its arity (param and result counts)
Source§impl<'a> BinaryReader<'a>
impl<'a> BinaryReader<'a>
Sourcepub fn new(data: &[u8], original_offset: usize) -> BinaryReader<'_>
pub fn new(data: &[u8], original_offset: usize) -> BinaryReader<'_>
Creates a new binary reader which will parse the data
provided.
The original_offset
provided is used for byte offsets in errors that
are generated. That offset is added to the current position in data
.
This can be helpful when data
is just a window of a view into a larger
wasm binary perhaps not even entirely stored locally.
The returned binary reader will have all features known to this crate
enabled. To reject binaries that aren’t valid unless a certain feature
is enabled use the BinaryReader::new_features
constructor instead.
Sourcepub fn new_features(
data: &[u8],
original_offset: usize,
features: WasmFeatures,
) -> BinaryReader<'_>
Available on crate feature features
only.
pub fn new_features( data: &[u8], original_offset: usize, features: WasmFeatures, ) -> BinaryReader<'_>
features
only.Creates a new binary reader which will parse the data
provided.
The original_offset
provided is used for byte offsets in errors that
are generated. That offset is added to the current position in data
.
This can be helpful when data
is just a window of a view into a larger
wasm binary perhaps not even entirely stored locally.
The features
argument provided controls which WebAssembly features are
active when parsing this data. Wasm features typically don’t affect
parsing too too much and are generally more applicable during
validation, but features and proposals will often reinterpret
previously-invalid constructs as now-valid things meaning something
slightly different. This means that invalid bytes before a feature may
now be interpreted differently after a feature is implemented. This
means that the set of activated features can affect what errors are
generated and when they are generated.
In general it’s safe to pass WasmFeatures::all()
here. There’s no
downside to enabling all features while parsing and only enabling a
subset of features during validation.
Note that the activated set of features does not guarantee that
BinaryReader
will return an error for disabled features. For example
if SIMD is disabled then SIMD instructions will still be parsed via
BinaryReader::visit_operator
. Validation must still be performed to
provide a strict guarantee that if a feature is disabled that a binary
doesn’t leverage the feature. The activated set of features here instead
only affects locations where preexisting bytes are reinterpreted in
different ways with future proposals, such as the memarg
moving from a
32-bit offset to a 64-bit offset with the memory64
proposal.
Sourcepub fn original_position(&self) -> usize
pub fn original_position(&self) -> usize
Gets the original position of the binary reader.
Sourcepub fn features(&self) -> WasmFeatures
Available on crate feature features
only.
pub fn features(&self) -> WasmFeatures
features
only.Returns the currently active set of wasm features that this reader is using while parsing.
For more information see BinaryReader::new
.
Sourcepub fn set_features(&mut self, features: WasmFeatures)
Available on crate feature features
only.
pub fn set_features(&mut self, features: WasmFeatures)
features
only.Sets the wasm features active while parsing to the features
specified.
For more information see BinaryReader::new
.
Sourcepub fn range(&self) -> Range<usize>
pub fn range(&self) -> Range<usize>
Returns a range from the starting offset to the end of the buffer.
Sourcepub fn read<T>(&mut self) -> Result<T>where
T: FromReader<'a>,
pub fn read<T>(&mut self) -> Result<T>where
T: FromReader<'a>,
Reads a value of type T
from this binary reader, advancing the
internal position in this reader forward as data is read.
Sourcepub fn read_size(&mut self, limit: usize, desc: &str) -> Result<usize>
pub fn read_size(&mut self, limit: usize, desc: &str) -> Result<usize>
Reads a variable-length 32-bit size from the byte stream while checking against a limit.
Sourcepub fn read_iter<'me, T>(
&'me mut self,
limit: usize,
desc: &str,
) -> Result<BinaryReaderIter<'a, 'me, T>>where
T: FromReader<'a>,
pub fn read_iter<'me, T>(
&'me mut self,
limit: usize,
desc: &str,
) -> Result<BinaryReaderIter<'a, 'me, T>>where
T: FromReader<'a>,
Reads a variable-length 32-bit size from the byte stream while checking against a limit.
Then reads that many values of type T
and returns them as an iterator.
Note that regardless of how many items are read from the returned iterator the items will still be parsed from this reader.
Sourcepub fn current_position(&self) -> usize
pub fn current_position(&self) -> usize
Returns the BinaryReader
’s current position.
Sourcepub fn bytes_remaining(&self) -> usize
pub fn bytes_remaining(&self) -> usize
Returns the number of bytes remaining in the BinaryReader
.
Sourcepub fn read_bytes(&mut self, size: usize) -> Result<&'a [u8]>
pub fn read_bytes(&mut self, size: usize) -> Result<&'a [u8]>
Advances the BinaryReader
size
bytes, and returns a slice from the
current position of size
length.
§Errors
If size
exceeds the remaining length in BinaryReader
.
Sourcepub fn read_reader(&mut self) -> Result<BinaryReader<'a>>
pub fn read_reader(&mut self) -> Result<BinaryReader<'a>>
Reads a length-prefixed list of bytes from this reader and returns a
new BinaryReader
to read that list of bytes.
Sourcepub fn read_u32(&mut self) -> Result<u32>
pub fn read_u32(&mut self) -> Result<u32>
Advances the BinaryReader
four bytes and returns a u32
.
§Errors
If BinaryReader
has less than four bytes remaining.
Sourcepub fn read_u64(&mut self) -> Result<u64>
pub fn read_u64(&mut self) -> Result<u64>
Advances the BinaryReader
eight bytes and returns a u64
.
§Errors
If BinaryReader
has less than eight bytes remaining.
Sourcepub fn read_var_u32(&mut self) -> Result<u32>
pub fn read_var_u32(&mut self) -> Result<u32>
Advances the BinaryReader
up to four bytes to parse a variable
length integer as a u32
.
§Errors
If BinaryReader
has less than one or up to four bytes remaining, or
the integer is larger than 32 bits.
Sourcepub fn read_var_u64(&mut self) -> Result<u64>
pub fn read_var_u64(&mut self) -> Result<u64>
Advances the BinaryReader
up to four bytes to parse a variable
length integer as a u64
.
§Errors
If BinaryReader
has less than one or up to eight bytes remaining, or
the integer is larger than 64 bits.
Sourcepub fn skip(&mut self, f: impl FnOnce(&mut Self) -> Result<()>) -> Result<Self>
pub fn skip(&mut self, f: impl FnOnce(&mut Self) -> Result<()>) -> Result<Self>
Executes f
to skip some data in this binary reader and then returns a
reader which will read the skipped data.
Sourcepub fn skip_string(&mut self) -> Result<()>
pub fn skip_string(&mut self) -> Result<()>
Advances the BinaryReader
past a WebAssembly string. This method does
not perform any utf-8 validation.
§Errors
If BinaryReader
has less than four bytes, the string’s length exceeds
the remaining bytes, or the string length
exceeds limits::MAX_WASM_STRING_SIZE
.
Sourcepub fn read_var_i32(&mut self) -> Result<i32>
pub fn read_var_i32(&mut self) -> Result<i32>
Advances the BinaryReader
up to four bytes to parse a variable
length integer as a i32
.
§Errors
If BinaryReader
has less than one or up to four bytes remaining, or
the integer is larger than 32 bits.
Sourcepub fn read_var_s33(&mut self) -> Result<i64>
pub fn read_var_s33(&mut self) -> Result<i64>
Advances the BinaryReader
up to four bytes to parse a variable
length integer as a signed 33 bit integer, returned as a i64
.
§Errors
If BinaryReader
has less than one or up to five bytes remaining, or
the integer is larger than 33 bits.
Sourcepub fn read_var_i64(&mut self) -> Result<i64>
pub fn read_var_i64(&mut self) -> Result<i64>
Advances the BinaryReader
up to eight bytes to parse a variable
length integer as a 64 bit integer, returned as a i64
.
§Errors
If BinaryReader
has less than one or up to eight bytes remaining, or
the integer is larger than 64 bits.
Sourcepub fn read_f32(&mut self) -> Result<Ieee32>
pub fn read_f32(&mut self) -> Result<Ieee32>
Advances the BinaryReader
four bytes to parse a 32 bit floating point
number, returned as Ieee32
.
§Errors
If BinaryReader
has less than four bytes remaining.
Sourcepub fn read_f64(&mut self) -> Result<Ieee64>
pub fn read_f64(&mut self) -> Result<Ieee64>
Advances the BinaryReader
eight bytes to parse a 64 bit floating point
number, returned as Ieee64
.
§Errors
If BinaryReader
has less than eight bytes remaining.
Sourcepub fn read_string(&mut self) -> Result<&'a str>
pub fn read_string(&mut self) -> Result<&'a str>
Reads a WebAssembly string from the module.
§Errors
If BinaryReader
has less than up to four bytes remaining, the string’s
length exceeds the remaining bytes, the string’s length exceeds
limits::MAX_WASM_STRING_SIZE
, or the string contains invalid utf-8.
Sourcepub fn read_unlimited_string(&mut self) -> Result<&'a str>
pub fn read_unlimited_string(&mut self) -> Result<&'a str>
Reads a unlimited WebAssembly string from the module.
Note that this is similar to BinaryReader::read_string
except that
it will not limit the size of the returned string by
limits::MAX_WASM_STRING_SIZE
.
Sourcepub fn visit_operator<T>(
&mut self,
visitor: &mut T,
) -> Result<<T as VisitOperator<'a>>::Output>where
T: VisitOperator<'a>,
pub fn visit_operator<T>(
&mut self,
visitor: &mut T,
) -> Result<<T as VisitOperator<'a>>::Output>where
T: VisitOperator<'a>,
Visit the next available operator with the specified VisitOperator
instance.
Note that this does not implicitly propagate any additional information such as instruction offsets. In order to do so, consider storing such data within the visitor before visiting.
§Errors
If BinaryReader
has less bytes remaining than required to parse the Operator
.
§Examples
Store an offset for use in diagnostics or any other purposes:
pub fn dump(mut reader: BinaryReader) -> Result<()> {
let mut visitor = Dumper { offset: 0 };
while !reader.eof() {
visitor.offset = reader.original_position();
reader.visit_operator(&mut visitor)?;
}
Ok(())
}
struct Dumper {
offset: usize
}
macro_rules! define_visit_operator {
($(@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {
$(
fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output {
println!("{}: {}", self.offset, stringify!($visit));
}
)*
}
}
impl<'a> VisitOperator<'a> for Dumper {
type Output = ();
for_each_operator!(define_visit_operator);
}
Sourcepub fn read_operator(&mut self) -> Result<Operator<'a>>
pub fn read_operator(&mut self) -> Result<Operator<'a>>
Reads the next available Operator
.
§Errors
If BinaryReader
has less bytes remaining than required to parse
the Operator
.
Sourcepub fn is_end_then_eof(&self) -> bool
pub fn is_end_then_eof(&self) -> bool
Returns whether there is an end
opcode followed by eof remaining in
this reader.
Trait Implementations§
Source§impl<'a> Clone for BinaryReader<'a>
impl<'a> Clone for BinaryReader<'a>
Source§fn clone(&self) -> BinaryReader<'a>
fn clone(&self) -> BinaryReader<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a> Debug for BinaryReader<'a>
impl<'a> Debug for BinaryReader<'a>
Auto Trait Implementations§
impl<'a> Freeze for BinaryReader<'a>
impl<'a> RefUnwindSafe for BinaryReader<'a>
impl<'a> Send for BinaryReader<'a>
impl<'a> Sync for BinaryReader<'a>
impl<'a> Unpin for BinaryReader<'a>
impl<'a> UnwindSafe for BinaryReader<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)