Skip to content

Commit fcb4f18

Browse files
authored
Turn WasmFeatures into a bitflags type (#1496)
* turn WasmFeatures into a bitflags type * applied code review suggestions * apply rustfmt * refactor parse_features as requested
1 parent 1942157 commit fcb4f18

File tree

29 files changed

+349
-464
lines changed

29 files changed

+349
-464
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pretty_assertions = "1.3.0"
5858
semver = "1.0.0"
5959
smallvec = "1.11.1"
6060
libtest-mimic = "0.7.0"
61+
bitflags = "2.5.0"
6162

6263
wasm-compose = { version = "0.203.0", path = "crates/wasm-compose" }
6364
wasm-encoder = { version = "0.203.0", path = "crates/wasm-encoder" }

crates/wasm-compose/src/graph.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ impl<'a> Component<'a> {
9898
fn parse(name: String, path: Option<PathBuf>, bytes: Cow<'a, [u8]>) -> Result<Self> {
9999
let mut parser = Parser::new(0);
100100
let mut parsers = Vec::new();
101-
let mut validator = Validator::new_with_features(WasmFeatures {
102-
component_model: true,
103-
..Default::default()
104-
});
101+
let mut validator =
102+
Validator::new_with_features(WasmFeatures::default() | WasmFeatures::COMPONENT_MODEL);
105103
let mut imports = IndexMap::new();
106104
let mut exports = IndexMap::new();
107105

@@ -992,12 +990,9 @@ impl<'a> CompositionGraph<'a> {
992990
let bytes = CompositionGraphEncoder::new(options, self).encode()?;
993991

994992
if options.validate {
995-
Validator::new_with_features(WasmFeatures {
996-
component_model: true,
997-
..Default::default()
998-
})
999-
.validate_all(&bytes)
1000-
.context("failed to validate encoded graph bytes")?;
993+
Validator::new_with_features(WasmFeatures::default() | WasmFeatures::COMPONENT_MODEL)
994+
.validate_all(&bytes)
995+
.context("failed to validate encoded graph bytes")?;
1001996
}
1002997

1003998
Ok(bytes)

crates/wasm-compose/tests/compose.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,14 @@ fn component_composing() -> Result<()> {
8282
let bytes =
8383
r.with_context(|| format!("failed to encode for test case `{}`", test_case))?;
8484

85-
Validator::new_with_features(WasmFeatures {
86-
component_model: true,
87-
..Default::default()
88-
})
89-
.validate_all(&bytes)
90-
.with_context(|| {
91-
format!(
92-
"failed to validate component bytes for test case `{}`",
93-
test_case
94-
)
95-
})?;
85+
Validator::new_with_features(WasmFeatures::default() | WasmFeatures::COMPONENT_MODEL)
86+
.validate_all(&bytes)
87+
.with_context(|| {
88+
format!(
89+
"failed to validate component bytes for test case `{}`",
90+
test_case
91+
)
92+
})?;
9693

9794
wit_component::decode(&bytes).with_context(|| {
9895
format!(

crates/wasm-encoder/src/core/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ impl Section for TypeSection {
650650

651651
#[cfg(test)]
652652
mod tests {
653+
use wasmparser::WasmFeatures;
654+
653655
use super::*;
654656
use crate::Module;
655657

@@ -666,10 +668,8 @@ mod tests {
666668
module.section(&types);
667669
let wasm_bytes = module.finish();
668670

669-
let mut validator = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures {
670-
gc: false,
671-
..Default::default()
672-
});
671+
let mut validator =
672+
wasmparser::Validator::new_with_features(WasmFeatures::default() & !WasmFeatures::GC);
673673

674674
validator.validate_all(&wasm_bytes).expect(
675675
"Encoding pre Wasm GC type should not accidentally use Wasm GC specific encoding",

crates/wasm-mutate/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ impl<'wasm> WasmMutate<'wasm> {
320320

321321
#[cfg(test)]
322322
pub(crate) fn validate(bytes: &[u8]) {
323-
let mut validator = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures {
324-
memory64: true,
325-
multi_memory: true,
326-
..Default::default()
327-
});
323+
use wasmparser::WasmFeatures;
324+
325+
let mut validator = wasmparser::Validator::new_with_features(
326+
WasmFeatures::default() | WasmFeatures::MEMORY64 | WasmFeatures::MULTI_MEMORY,
327+
);
328328
let err = match validator.validate_all(bytes) {
329329
Ok(_) => return,
330330
Err(e) => e,

crates/wasm-shrink/src/lib.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::collections::HashSet;
88
use anyhow::{Context, Result};
99
use rand::{rngs::SmallRng, Rng, SeedableRng};
1010
use wasm_mutate::WasmMutate;
11+
use wasmparser::WasmFeatures;
1112

1213
#[rustfmt::skip]
1314
static EMPTY_WASM: &'static [u8] = &[
@@ -216,33 +217,24 @@ impl ShrinkRun {
216217
}
217218

218219
fn validate_wasm(&self, wasm: &[u8]) -> Result<()> {
219-
let mut validator = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures {
220-
// TODO: we should have CLI flags for each Wasm proposal.
221-
reference_types: true,
222-
multi_value: true,
223-
bulk_memory: true,
224-
simd: true,
225-
threads: true,
226-
shared_everything_threads: false,
227-
tail_call: true,
228-
multi_memory: true,
229-
exceptions: true,
230-
memory64: true,
231-
relaxed_simd: true,
232-
extended_const: true,
233-
mutable_global: true,
234-
saturating_float_to_int: true,
235-
sign_extension: true,
236-
component_model: false,
237-
function_references: false,
238-
gc: false,
239-
custom_page_sizes: false,
240-
component_model_values: false,
241-
component_model_nested_names: false,
242-
243-
floats: true,
244-
memory_control: true,
245-
});
220+
let mut validator = wasmparser::Validator::new_with_features(
221+
WasmFeatures::REFERENCE_TYPES
222+
| WasmFeatures::MULTI_VALUE
223+
| WasmFeatures::BULK_MEMORY
224+
| WasmFeatures::SIMD
225+
| WasmFeatures::THREADS
226+
| WasmFeatures::TAIL_CALL
227+
| WasmFeatures::MULTI_MEMORY
228+
| WasmFeatures::EXCEPTIONS
229+
| WasmFeatures::MEMORY64
230+
| WasmFeatures::RELAXED_SIMD
231+
| WasmFeatures::EXTENDED_CONST
232+
| WasmFeatures::MUTABLE_GLOBAL
233+
| WasmFeatures::SATURATING_FLOAT_TO_INT
234+
| WasmFeatures::SIGN_EXTENSION
235+
| WasmFeatures::FLOATS
236+
| WasmFeatures::MEMORY_CONTROL,
237+
);
246238

247239
validator.validate_all(wasm)?;
248240
Ok(())

crates/wasm-smith/tests/common/mod.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@ use wasm_smith::Config;
22
use wasmparser::{types::Types, Validator, WasmFeatures};
33

44
pub fn parser_features_from_config(config: &Config) -> WasmFeatures {
5-
WasmFeatures {
6-
mutable_global: true,
7-
saturating_float_to_int: config.saturating_float_to_int_enabled,
8-
sign_extension: config.sign_extension_ops_enabled,
9-
reference_types: config.reference_types_enabled,
10-
multi_value: config.multi_value_enabled,
11-
bulk_memory: config.bulk_memory_enabled,
12-
simd: config.simd_enabled,
13-
relaxed_simd: config.relaxed_simd_enabled,
14-
multi_memory: config.max_memories > 1,
15-
exceptions: config.exceptions_enabled,
16-
memory64: config.memory64_enabled,
17-
tail_call: config.tail_call_enabled,
18-
function_references: config.gc_enabled,
19-
gc: config.gc_enabled,
20-
custom_page_sizes: config.custom_page_sizes_enabled,
21-
22-
threads: false,
23-
shared_everything_threads: false,
24-
floats: true,
25-
extended_const: false,
26-
component_model: false,
27-
memory_control: false,
28-
component_model_values: false,
29-
component_model_nested_names: false,
30-
}
5+
let mut features = WasmFeatures::MUTABLE_GLOBAL | WasmFeatures::FLOATS;
6+
features.set(
7+
WasmFeatures::SATURATING_FLOAT_TO_INT,
8+
config.saturating_float_to_int_enabled,
9+
);
10+
features.set(
11+
WasmFeatures::SIGN_EXTENSION,
12+
config.sign_extension_ops_enabled,
13+
);
14+
features.set(
15+
WasmFeatures::REFERENCE_TYPES,
16+
config.reference_types_enabled,
17+
);
18+
features.set(WasmFeatures::MULTI_VALUE, config.multi_value_enabled);
19+
features.set(WasmFeatures::BULK_MEMORY, config.bulk_memory_enabled);
20+
features.set(WasmFeatures::SIMD, config.simd_enabled);
21+
features.set(WasmFeatures::RELAXED_SIMD, config.relaxed_simd_enabled);
22+
features.set(WasmFeatures::MULTI_MEMORY, config.max_memories > 1);
23+
features.set(WasmFeatures::EXCEPTIONS, config.exceptions_enabled);
24+
features.set(WasmFeatures::MEMORY64, config.memory64_enabled);
25+
features.set(WasmFeatures::TAIL_CALL, config.tail_call_enabled);
26+
features.set(WasmFeatures::FUNCTION_REFERENCES, config.gc_enabled);
27+
features.set(WasmFeatures::GC, config.gc_enabled);
28+
features.set(
29+
WasmFeatures::CUSTOM_PAGE_SIZE,
30+
config.custom_page_sizes_enabled,
31+
);
32+
features
3133
}
3234

3335
pub fn validate(validator: &mut Validator, bytes: &[u8]) -> Types {

crates/wasm-smith/tests/component.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ fn smoke_test_component() {
1818
ok_count += 1;
1919
let component = component.to_bytes();
2020

21-
let mut validator =
22-
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures {
23-
component_model: true,
24-
..Default::default()
25-
});
21+
let mut validator = wasmparser::Validator::new_with_features(
22+
wasmparser::WasmFeatures::default() | wasmparser::WasmFeatures::COMPONENT_MODEL,
23+
);
2624
if let Err(e) = validator.validate_all(&component) {
2725
std::fs::write("component.wasm", &component).unwrap();
2826
panic!(

crates/wasm-smith/tests/core.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn multi_value_disabled() {
6969
if let Ok(module) = Module::new(cfg, &mut u) {
7070
let wasm_bytes = module.to_bytes();
7171
let mut features = wasm_features();
72-
features.multi_value = false;
72+
features.remove(WasmFeatures::MULTI_VALUE);
7373
let mut validator = Validator::new_with_features(features);
7474
validate(&mut validator, &wasm_bytes);
7575
}
@@ -143,14 +143,5 @@ fn smoke_test_wasm_gc() {
143143
}
144144

145145
fn wasm_features() -> WasmFeatures {
146-
WasmFeatures {
147-
multi_memory: true,
148-
relaxed_simd: true,
149-
memory64: true,
150-
exceptions: true,
151-
tail_call: true,
152-
function_references: true,
153-
gc: true,
154-
..WasmFeatures::default()
155-
}
146+
WasmFeatures::all()
156147
}

0 commit comments

Comments
 (0)