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
impl WasmShrink
Sourcepub fn attempts(self, attempts: u32) -> WasmShrink
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.
Sourcepub fn allow_empty(self, allow_empty: bool) -> WasmShrink
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.
Sourcepub fn seed(self, seed: u64) -> WasmShrink
pub fn seed(self, seed: u64) -> WasmShrink
Set the RNG seed for choosing which size-reducing mutation to attempt next.
Sourcepub fn on_new_smallest(
self,
on_new_smallest: Option<Box<dyn FnMut(&[u8]) -> Result<()>>>,
) -> WasmShrink
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.
Sourcepub fn run<P, I>(self, input: Vec<u8>, predicate: P) -> Result<ShrinkInfo>
pub fn run<P, I>(self, input: Vec<u8>, predicate: P) -> Result<ShrinkInfo>
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.