Skip to content

Commit

Permalink
Add a new wit-smith subcommand (#992)
Browse files Browse the repository at this point in the history
* Add a new `wit-smith` subcommand

This commit extracts the WIT document generation from the
`roundtrip-wit` fuzzer into a dedicated crate and a dedicated
subcommand, `wasm-tools wit-smith`. This is intended to behave similarly
to the `wasm-tools smith` subcommand but it instead generates a
WebAssembly binary-encoded WIT document.

* Fix build of wit-smith in isolation
  • Loading branch information
alexcrichton authored Apr 22, 2023
1 parent f69b086 commit 95c6bf7
Show file tree
Hide file tree
Showing 10 changed files with 926 additions and 765 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ jobs:
- run: cargo check --no-default-features --features demangle
- run: cargo check --no-default-features --features component
- run: cargo check --no-default-features --features metadata
- run: cargo check --no-default-features --features wit-smith

doc:
runs-on: ubuntu-latest
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ wast = { version = "56.0.0", path = "crates/wast" }
wat = { version = "1.0.62", path = "crates/wat" }
wit-component = { version = "0.8.1", path = "crates/wit-component" }
wit-parser = { version = "0.7.0", path = "crates/wit-parser" }
wit-smith = { version = "0.1.0", path = "crates/wit-smith" }

[dependencies]
anyhow = { workspace = true }
Expand Down Expand Up @@ -103,6 +104,9 @@ wast = { workspace = true, optional = true }
# Dependencies of `metadata`
wasm-metadata = { workspace = true, features = ["clap"], optional = true }

# Dependencies of `wit-smith`
wit-smith = { workspace = true, features = ["clap"], optional = true }

[dev-dependencies]
serde_json = "1.0"
tempfile = "3.1"
Expand Down Expand Up @@ -134,6 +138,7 @@ default = [
'demangle',
'component',
'metadata',
'wit-smith',
]

# Each subcommand is gated behind a feature and lists the dependencies it needs
Expand All @@ -150,3 +155,4 @@ compose = ['wasm-compose']
demangle = ['rustc-demangle', 'cpp_demangle', 'wasmparser', 'wasm-encoder']
component = ['wit-component', 'wit-parser', 'wast', 'wasm-encoder', 'wasmparser']
metadata = ['wasmparser', 'wasm-metadata', 'serde_json' ]
wit-smith = ['dep:wit-smith', 'arbitrary']
14 changes: 14 additions & 0 deletions crates/wit-smith/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
description = "A WIT test case generator"
documentation = "https://docs.rs/wit-smith"
edition.workspace = true
license = "Apache-2.0 WITH LLVM-exception"
name = "wit-smith"
repository = "https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-smith"
version = "0.1.0"

[dependencies]
arbitrary = { workspace = true, features = ["derive"] }
wit-component = { workspace = true }
wit-parser = { workspace = true }
clap = { workspace = true, optional = true }
48 changes: 48 additions & 0 deletions crates/wit-smith/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use arbitrary::{Arbitrary, Result, Unstructured};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
pub struct Config {
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_packages))]
pub max_packages: usize,
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_type_size))]
pub max_type_size: usize,
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_interface_items))]
pub max_interface_items: usize,
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_world_items))]
pub max_world_items: usize,
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_doc_items))]
pub max_doc_items: usize,
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_documents))]
pub max_documents: usize,
#[cfg_attr(feature = "clap", clap(long, default_value_t = Config::default().max_type_parts))]
pub max_type_parts: usize,
}

impl Default for Config {
fn default() -> Config {
Config {
max_packages: 10,
max_type_size: 100,
max_interface_items: 10,
max_world_items: 10,
max_doc_items: 10,
max_documents: 10,
max_type_parts: 10,
}
}
}

impl Arbitrary<'_> for Config {
fn arbitrary(u: &mut Unstructured<'_>) -> Result<Config> {
Ok(Config {
max_packages: u.int_in_range(1..=20)?,
max_documents: u.int_in_range(1..=10)?,
max_type_size: u.int_in_range(0..=1000)?,
max_interface_items: u.int_in_range(0..=20)?,
max_world_items: u.int_in_range(0..=10)?,
max_doc_items: u.int_in_range(0..=10)?,
max_type_parts: u.int_in_range(1..=10)?,
})
}
}
Loading

0 comments on commit 95c6bf7

Please sign in to comment.