Skip to content

Commit f8716d4

Browse files
eqrionalexcrichton
andauthored
Make wasmparser an optional dependency of wasm-smith (#1301)
* Make wasmparser an optional dependency of wasm-smith * Add missing import to test * Remove usage of `KebabStr*` from wasm-smith Not actually needed in its current iteration of component support. * Reorganize cfgs to reduce `#[cfg]` quantities * Test `wasmparser` feature on CI Ensure that it continues to build and function correctly. * Enable wasmparser feature for `wasm-tools` CLI --------- Co-authored-by: Alex Crichton <alex@alexcrichton.com>
1 parent 56acc5a commit f8716d4

File tree

9 files changed

+279
-215
lines changed

9 files changed

+279
-215
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
- run: cargo test --locked --all
4545
- run: cargo test --locked -p wasmparser --benches
4646
- run: cargo test --locked -p wasm-encoder --all-features
47+
- run: cargo test -p wasm-smith --features wasmparser
4748
- run: cargo build --manifest-path crates/wast/Cargo.toml --no-default-features
4849
- run: cargo build --manifest-path crates/wast/Cargo.toml --no-default-features --features wasm-module
4950
- run: cmake -S ${{github.workspace}}/examples -B ${{github.workspace}}/examples/build -DCMAKE_BUILD_TYPE=Release

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ arbitrary = { workspace = true, optional = true }
8181
serde = { workspace = true, optional = true }
8282
serde_derive = { workspace = true, optional = true }
8383
serde_json = { workspace = true, optional = true }
84-
wasm-smith = { workspace = true, features = ["_internal_cli"], optional = true }
84+
wasm-smith = { workspace = true, features = ["_internal_cli", "wasmparser"], optional = true }
8585

8686
# Dependencies of `shrink`
8787
wasm-shrink = { workspace = true, features = ["clap"], optional = true }

crates/wasm-smith/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ leb128 = { workspace = true }
2323
serde = { workspace = true, optional = true }
2424
serde_derive = { workspace = true, optional = true }
2525
wasm-encoder = { workspace = true }
26-
wasmparser = { workspace = true }
26+
wasmparser = { workspace = true, optional = true }
2727

2828
[dev-dependencies]
2929
criterion = { workspace = true }
3030
rand = { workspace = true }
31+
wasmparser = { workspace = true }
3132
wasmprinter = { path = "../wasmprinter" }
3233
wat = { path = "../wat" }
3334

3435
[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
3536
libfuzzer-sys = { workspace = true }
3637

3738
[features]
38-
_internal_cli = ["serde", "serde_derive"]
39+
_internal_cli = ["serde", "serde_derive", "wasmparser"]

crates/wasm-smith/src/component.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::{
1414
rc::Rc,
1515
};
1616
use wasm_encoder::{ComponentTypeRef, ComponentValType, PrimitiveValType, TypeBounds, ValType};
17-
use wasmparser::names::KebabString;
1817

1918
mod encode;
2019

@@ -124,10 +123,10 @@ struct ComponentContext {
124123
num_imports: usize,
125124

126125
// The set of names of imports we've generated thus far.
127-
import_names: HashSet<KebabString>,
126+
import_names: HashSet<String>,
128127

129128
// The set of URLs of imports we've generated thus far.
130-
import_urls: HashSet<KebabString>,
129+
import_urls: HashSet<String>,
131130

132131
// This component's function index space.
133132
funcs: Vec<ComponentOrCoreFuncType>,
@@ -1095,15 +1094,15 @@ impl ComponentBuilder {
10951094
fn arbitrary_instance_type_def(
10961095
&mut self,
10971096
u: &mut Unstructured,
1098-
exports: &mut HashSet<KebabString>,
1099-
export_urls: &mut HashSet<KebabString>,
1097+
exports: &mut HashSet<String>,
1098+
export_urls: &mut HashSet<String>,
11001099
type_fuel: &mut u32,
11011100
) -> Result<InstanceTypeDecl> {
11021101
let mut choices: Vec<
11031102
fn(
11041103
&mut ComponentBuilder,
1105-
&mut HashSet<KebabString>,
1106-
&mut HashSet<KebabString>,
1104+
&mut HashSet<String>,
1105+
&mut HashSet<String>,
11071106
&mut Unstructured,
11081107
&mut u32,
11091108
) -> Result<InstanceTypeDecl>,
@@ -1498,7 +1497,7 @@ impl ComponentBuilder {
14981497
}
14991498
}
15001499

1501-
fn push_import(&mut self, name: KebabString, url: Option<String>, ty: ComponentTypeRef) {
1500+
fn push_import(&mut self, name: String, url: Option<String>, ty: ComponentTypeRef) {
15021501
let nth = match self.ensure_section(
15031502
|sec| matches!(sec, Section::Import(_)),
15041503
|| Section::Import(ImportSection { imports: vec![] }),
@@ -2018,7 +2017,7 @@ enum ComponentTypeDef {
20182017
Alias(Alias),
20192018
Import(Import),
20202019
Export {
2021-
name: KebabString,
2020+
name: String,
20222021
url: Option<String>,
20232022
ty: ComponentTypeRef,
20242023
},
@@ -2046,16 +2045,16 @@ enum InstanceTypeDecl {
20462045
Type(Rc<Type>),
20472046
Alias(Alias),
20482047
Export {
2049-
name: KebabString,
2048+
name: String,
20502049
url: Option<String>,
20512050
ty: ComponentTypeRef,
20522051
},
20532052
}
20542053

20552054
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
20562055
struct FuncType {
2057-
params: Vec<(KebabString, ComponentValType)>,
2058-
results: Vec<(Option<KebabString>, ComponentValType)>,
2056+
params: Vec<(String, ComponentValType)>,
2057+
results: Vec<(Option<String>, ComponentValType)>,
20592058
}
20602059

20612060
impl FuncType {
@@ -2112,12 +2111,12 @@ enum DefinedType {
21122111

21132112
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21142113
struct RecordType {
2115-
fields: Vec<(KebabString, ComponentValType)>,
2114+
fields: Vec<(String, ComponentValType)>,
21162115
}
21172116

21182117
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21192118
struct VariantType {
2120-
cases: Vec<(KebabString, Option<ComponentValType>, Option<u32>)>,
2119+
cases: Vec<(String, Option<ComponentValType>, Option<u32>)>,
21212120
}
21222121

21232122
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -2132,12 +2131,12 @@ struct TupleType {
21322131

21332132
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21342133
struct FlagsType {
2135-
fields: Vec<KebabString>,
2134+
fields: Vec<String>,
21362135
}
21372136

21382137
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21392138
struct EnumType {
2140-
variants: Vec<KebabString>,
2139+
variants: Vec<String>,
21412140
}
21422141

21432142
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -2158,7 +2157,7 @@ struct ImportSection {
21582157

21592158
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21602159
struct Import {
2161-
name: KebabString,
2160+
name: String,
21622161
url: Option<String>,
21632162
ty: ComponentTypeRef,
21642163
}

crates/wasm-smith/src/component/encode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22

33
use super::*;
44
use wasm_encoder::{ComponentExportKind, ComponentOuterAliasKind, ExportKind};
5-
use wasmparser::names::KebabStr;
65

76
impl Component {
87
/// Encode this Wasm component into bytes.
@@ -169,9 +168,10 @@ impl Type {
169168
f.result(ty);
170169
} else {
171170
f.results(
172-
func_ty.results.iter().map(|(name, ty)| {
173-
(name.as_deref().map(KebabStr::as_str).unwrap(), *ty)
174-
}),
171+
func_ty
172+
.results
173+
.iter()
174+
.map(|(name, ty)| (name.as_deref().unwrap(), *ty)),
175175
);
176176
}
177177
}

crates/wasm-smith/src/core.rs

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,29 @@ impl Module {
539539
/// the caller should generate arbitrary imports.
540540
fn arbitrary_imports_from_available(&mut self, u: &mut Unstructured) -> Result<bool> {
541541
let example_module = if let Some(wasm) = self.config.available_imports() {
542-
wasm
542+
wasm.into_owned()
543543
} else {
544544
return Ok(false);
545545
};
546546

547+
#[cfg(feature = "wasmparser")]
548+
{
549+
self._arbitrary_imports_from_available(u, &example_module)?;
550+
Ok(true)
551+
}
552+
#[cfg(not(feature = "wasmparser"))]
553+
{
554+
let _ = (example_module, u);
555+
panic!("support for `available_imports` was disabled at compile time");
556+
}
557+
}
558+
559+
#[cfg(feature = "wasmparser")]
560+
fn _arbitrary_imports_from_available(
561+
&mut self,
562+
u: &mut Unstructured,
563+
example_module: &[u8],
564+
) -> Result<()> {
547565
// First, parse the module-by-example to collect the types and imports.
548566
//
549567
// `available_types` will map from a signature index (which is the same as the index into
@@ -717,7 +735,41 @@ impl Module {
717735
self.types.extend(new_types);
718736
self.imports.extend(new_imports);
719737

720-
Ok(true)
738+
return Ok(());
739+
740+
/// Convert a wasmparser's `ValType` to a `wasm_encoder::ValType`.
741+
fn convert_type(parsed_type: wasmparser::ValType) -> ValType {
742+
use wasmparser::ValType::*;
743+
match parsed_type {
744+
I32 => ValType::I32,
745+
I64 => ValType::I64,
746+
F32 => ValType::F32,
747+
F64 => ValType::F64,
748+
V128 => ValType::V128,
749+
Ref(ty) => ValType::Ref(convert_reftype(ty)),
750+
}
751+
}
752+
753+
fn convert_reftype(ty: wasmparser::RefType) -> RefType {
754+
wasm_encoder::RefType {
755+
nullable: ty.is_nullable(),
756+
heap_type: match ty.heap_type() {
757+
wasmparser::HeapType::Func => HeapType::Func,
758+
wasmparser::HeapType::Extern => HeapType::Extern,
759+
wasmparser::HeapType::Any => HeapType::Any,
760+
wasmparser::HeapType::None => HeapType::None,
761+
wasmparser::HeapType::NoExtern => HeapType::NoExtern,
762+
wasmparser::HeapType::NoFunc => HeapType::NoFunc,
763+
wasmparser::HeapType::Eq => HeapType::Eq,
764+
wasmparser::HeapType::Struct => HeapType::Struct,
765+
wasmparser::HeapType::Array => HeapType::Array,
766+
wasmparser::HeapType::I31 => HeapType::I31,
767+
wasmparser::HeapType::Concrete(i) => {
768+
HeapType::Concrete(i.as_module_index().unwrap())
769+
}
770+
},
771+
}
772+
}
721773
}
722774

723775
fn type_of(&self, kind: ExportKind, index: u32) -> EntityType {
@@ -1627,38 +1679,6 @@ fn arbitrary_vec_u8(u: &mut Unstructured) -> Result<Vec<u8>> {
16271679
Ok(u.bytes(size)?.to_vec())
16281680
}
16291681

1630-
/// Convert a wasmparser's `ValType` to a `wasm_encoder::ValType`.
1631-
fn convert_type(parsed_type: wasmparser::ValType) -> ValType {
1632-
use wasmparser::ValType::*;
1633-
match parsed_type {
1634-
I32 => ValType::I32,
1635-
I64 => ValType::I64,
1636-
F32 => ValType::F32,
1637-
F64 => ValType::F64,
1638-
V128 => ValType::V128,
1639-
Ref(ty) => ValType::Ref(convert_reftype(ty)),
1640-
}
1641-
}
1642-
1643-
fn convert_reftype(ty: wasmparser::RefType) -> RefType {
1644-
wasm_encoder::RefType {
1645-
nullable: ty.is_nullable(),
1646-
heap_type: match ty.heap_type() {
1647-
wasmparser::HeapType::Func => HeapType::Func,
1648-
wasmparser::HeapType::Extern => HeapType::Extern,
1649-
wasmparser::HeapType::Any => HeapType::Any,
1650-
wasmparser::HeapType::None => HeapType::None,
1651-
wasmparser::HeapType::NoExtern => HeapType::NoExtern,
1652-
wasmparser::HeapType::NoFunc => HeapType::NoFunc,
1653-
wasmparser::HeapType::Eq => HeapType::Eq,
1654-
wasmparser::HeapType::Struct => HeapType::Struct,
1655-
wasmparser::HeapType::Array => HeapType::Array,
1656-
wasmparser::HeapType::I31 => HeapType::I31,
1657-
wasmparser::HeapType::Concrete(i) => HeapType::Concrete(i.as_module_index().unwrap()),
1658-
},
1659-
}
1660-
}
1661-
16621682
impl EntityType {
16631683
fn size(&self) -> u32 {
16641684
match self {

crates/wasm-smith/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use arbitrary::{Result, Unstructured};
6565
pub use component::{Component, ConfiguredComponent};
6666
pub use config::{Config, DefaultConfig, SwarmConfig};
6767
use std::{collections::HashSet, fmt::Write, str};
68-
use wasmparser::names::{KebabStr, KebabString};
6968

7069
/// Do something an arbitrary number of times.
7170
///
@@ -136,9 +135,9 @@ pub(crate) fn unique_string(
136135

137136
pub(crate) fn unique_kebab_string(
138137
max_size: usize,
139-
names: &mut HashSet<KebabString>,
138+
names: &mut HashSet<String>,
140139
u: &mut Unstructured,
141-
) -> Result<KebabString> {
140+
) -> Result<String> {
142141
let size = std::cmp::min(u.arbitrary_len::<u8>()?, max_size);
143142
let mut name = String::with_capacity(size);
144143
let mut require_alpha = true;
@@ -173,19 +172,18 @@ pub(crate) fn unique_kebab_string(
173172
name.push('a');
174173
}
175174

176-
while names.contains(KebabStr::new(&name).unwrap()) {
175+
while names.contains(&name) {
177176
write!(&mut name, "{}", names.len()).unwrap();
178177
}
179178

180-
let name = KebabString::new(name).unwrap();
181179
names.insert(name.clone());
182180

183181
Ok(name)
184182
}
185183

186184
pub(crate) fn unique_url(
187185
max_size: usize,
188-
names: &mut HashSet<KebabString>,
186+
names: &mut HashSet<String>,
189187
u: &mut Unstructured,
190188
) -> Result<String> {
191189
let path = unique_kebab_string(max_size, names, u)?;

0 commit comments

Comments
 (0)