wasm_shrink

Struct WasmShrink

Source
pub struct WasmShrink { /* private fields */ }
Expand description

Shrink a Wasm file while maintaining a property of interest (such as triggering a compiler bug).

§Example

use wasm_shrink::WasmShrink;

// Get the Wasm you want to shrink from somewhere.
let my_input_wasm: Vec<u8> = todo!();

// Configure the shrinking task.
let shrink = WasmShrink::default()
    // Give up on shrinking after 999 failed attempts to shrink a given
    // Wasm test case any further.
    .attempts(999)
    // Optionally do something with each new smallest Wasm as it is discovered.
    .on_new_smallest(Some(Box::new(|new_smallest| {
        // Do stuff...
        Ok(())
    })));

// Run the configured shrinking task.
let info = shrink.run(
    my_input_wasm,
    // Predicate.
    &mut |wasm| {
        let is_interesting: bool = todo!(
            "check for whether the given Wasm is interesting"
        );
        Ok(is_interesting)
    }
)?;

// Get the shrunken Wasm and other information about the completed shrink
// task from the returned `ShrinkInfo`.
let shrunken_wasm = info.output;

Implementations§

Source§

impl WasmShrink

Source

pub fn attempts(self, attempts: u32) -> WasmShrink

Set the number of shrink attempts to try before considering a Wasm module as small as it will ever get.

Source

pub fn allow_empty(self, allow_empty: bool) -> WasmShrink

Is it okay to shrink the input down to an empty Wasm module?

This is usually not desired and typically reflects a bug in the predicate.

Source

pub fn seed(self, seed: u64) -> WasmShrink

Set the RNG seed for choosing which size-reducing mutation to attempt next.

Source

pub fn on_new_smallest( self, on_new_smallest: Option<Box<dyn FnMut(&[u8]) -> Result<()>>>, ) -> WasmShrink

Set the callback that is called each time we discover a new smallest test case that is interesting.

Source

pub fn run<P, I>(self, input: Vec<u8>, predicate: P) -> Result<ShrinkInfo>
where P: FnMut(&[u8]) -> Result<I>, I: IsInteresting,

Run this configured Wasm shrinking task.

The predicate function is called on each candidate Wasm to determine whether the Wasm is interesting or not. Returning true means that it is interesting, false means that it is not.

Returns the shrunken Wasm and information and metrics about the shrink task, such as the size of the input Wasm, the size of the output Wasm, etc.

Trait Implementations§

Source§

impl Default for WasmShrink

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V