Debian Codegraph @

crates/wasm-shrink/README.md

raw
1 # `wasm-shrink`
2
3 **A WebAssembly test case shrinker.**
4
5 [![](https://docs.rs/wasm-shrink/badge.svg)](https://docs.rs/wasm-shrink/)
6 [![](https://img.shields.io/crates/v/wasm-shrink.svg)](https://crates.io/crates/wasm-shrink)
7 [![](https://img.shields.io/crates/d/wasm-shrink.svg)](https://crates.io/crates/wasm-shrink)
8
9 * [About](#about)
10 * [Usage](#usage)
11 * [Install](#install)
12 * [Writing a Predicate Script](#writing-a-predicate-script)
13 * [Run](#run)
14 * [Embed as a Library](#embed-as-a-library)
15
16 ## About
17
18 `wasm-shrink` is a test case shrinker for WebAssembly. It shrinks a Wasm file
19 while preserving an interesting property (such as triggering a bug in your Wasm
20 compiler).
21
22 ## Usage
23
24 ### Install
25
26 ```
27 $ cargo install wasm-tools
28 ```
29
30 ### Writing a Predicate Script
31
32 The predicate script tells `wasm-shrink` whether a Wasm file is "interesting" or
33 not, i.e. whether the Wasm file triggers the bug you are trying to isolate.
34
35 The interface between `wasm-shrink` and a predicate script is simple:
36
37 * The predicate script is given a Wasm file as its first and only argument.
38
39 * The predicate script must exit with a zero status code if the Wasm file is
40 interesting and a non-zero status code otherwise.
41
42 The predicate script must not depend on the current working directory nor that
43 only one predicate script process is running at any given time.
44
45 Here is an example predicate script that could be used to track down a panic in
46 Wasmtime that includes the panic message "assertion failed: invalid stack map":
47
48 ```bash
49 #!/usr/bin/env bash
50
51 # Exit the script if any subcommand fails.
52 set -e
53
54 # The Wasm file is given as the first and only argument to the script.
55 WASM=$1
56
57 # Run the Wasm in Wasmtime and `grep` for our target bug's panic
58 # message.
59 wasmtime run $WASM 2>&1 | grep --quiet 'assertion failed: invalid stack map'
60 ```
61
62 Note that passing `grep` the `--quiet` flag makes it avoid its usual
63 match-printing behavior and exit with status code zero if there is *any* match
64 and non-zero if there is *not* any match. This is useful for predicate scripts.
65
66 ### Run
67
68 To run `wasm-tools shrink` pass it the predicate and the initial test case:
69
70 ```bash
71 $ wasm-tools shrink predicate.sh test-case.wasm -o shrunken.wasm
72 ```
73
74 The shrunken Wasm file will be written to `shrunken.wasm` in this case, but if
75 the `-o` flag is not given an output name is generated based on the initial test
76 case's name.
77
78 You can see all options by passing `--help`:
79
80 ```bash
81 $ wasm-tools shrink --help
82 ```
83
84 ## Embed as a Library
85
86 `wasm-shrink` is not only a command-line tool, it also defines a library to
87 allow programmatic usage.
88
89 First, add a dependency to your `Cargo.toml`:
90
91 ```sh
92 $ cargo add wasm-shrink
93 ```
94
95 Then use the `wasm_shrink::WasmShrink` builder to configure and run a shrinking
96 task:
97
98 ```rust
99 use wasm_shrink::WasmShrink;
100
101 // Get the Wasm you want to shrink from somewhere.
102 let my_input_wasm: Vec<u8> = todo!();
103
104 // Configure the shrinking task.
105 let shrink = WasmShrink::default()
106 // Give up on shrinking after 999 failed attempts to shrink a given
107 // Wasm test case any further.
108 .attempts(999);
109
110 // Run the configured shrinking task.
111 let info = shrink.run(
112 my_input_wasm,
113
114 // Predicate.
115 &mut |wasm| {
116 let is_interesting: bool = todo!(
117 "check for whether the given Wasm is interesting"
118 );
119 Ok(is_interesting)
120 },
121
122 // Callback called each time we find a new smallest interesting
123 // Wasm.
124 &mut |new_smallest| {
125 // Optionally do something with the new smallest Wasm.
126 Ok(())
127 },
128 )?;
129
130 // Get the shrunken Wasm and other information about the completed shrink
131 // task from the returned `ShrinkInfo`.
132 let shrunken_wasm = info.output;
133 ```
134
135 For more details, see [the documentation on
136 `docs.rs`](https://docs.rs/wasm-shrink/).