| 1 |
//! A small crate to generate arbitrary WIT documents. |
| 2 |
//! |
| 3 |
//! This crate is modeled after the `wasm-smith` crate but is used to generate |
| 4 |
//! WIT documents instead of WebAssembly modules. This crate is intended to |
| 5 |
//! generate "interesting" WIT package structures in addition to interesting |
| 6 |
//! type structures. |
| 7 |
|
| 8 |
use arbitrary::{Result, Unstructured}; |
| 9 |
use wit_parser::{InvalidTransitiveDependency, Resolve}; |
| 10 |
|
| 11 |
mod config; |
| 12 |
pub use self::config::Config; |
| 13 |
mod generate; |
| 14 |
|
| 15 |
/// Generates an arbitrary WIT document encoded as a WebAssembly binary. |
| 16 |
/// |
| 17 |
/// The `config` guides the generation of the document and the `u` bytes are |
| 18 |
/// used as input to construct the document. |
| 19 |
pub fn smith(config: &Config, u: &mut Unstructured<'_>) -> Result<Vec<u8>> { |
| 20 |
let pkgs = generate::Generator::new(config.clone()).generate(u)?; |
| 21 |
let mut resolve = Resolve::default(); |
| 22 |
resolve.all_features = true; |
| 23 |
let mut last = None; |
| 24 |
for pkg in pkgs { |
| 25 |
log::trace!("appending package {:?}", pkg.name); |
| 26 |
let group = pkg.sources.parse().unwrap(); |
| 27 |
let id = match resolve.push_group(group) { |
| 28 |
Ok(id) => id, |
| 29 |
Err(e) => { |
| 30 |
if e.is::<InvalidTransitiveDependency>() { |
| 31 |
return Err(arbitrary::Error::IncorrectFormat); |
| 32 |
} |
| 33 |
let err = e.to_string(); |
| 34 |
if err.contains("shadows previously") || err.contains("mismatch in stability") { |
| 35 |
log::error!("{e}"); |
| 36 |
return Err(arbitrary::Error::IncorrectFormat); |
| 37 |
} |
| 38 |
panic!("bad wit parse: {e:?}") |
| 39 |
} |
| 40 |
}; |
| 41 |
last = Some(id); |
| 42 |
} |
| 43 |
let pkg = last.unwrap(); |
| 44 |
|
| 45 |
Ok(wit_component::encode(&resolve, pkg).expect("failed to encode WIT document")) |
| 46 |
} |