Skip to content

Commit b1ee957

Browse files
authored
Gate WebAssembly validation behind a new 'validate' feature (#1532)
* Gate WebAssembly validation behind a new 'validate' feature This commit moves all of the validation logic of `wasmparser` behind a new Cargo features named `validate`. This cuts down the dependency tree for users who only need to parse wasm files. For example `wasmprinter` doesn't need to validate, it only needs to parse. Closes #1528 * Fix wasm-smith build and tests
1 parent c2e06c3 commit b1ee957

13 files changed

Lines changed: 71 additions & 20 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ jobs:
190190
- run: cargo check --no-default-features -p wasmparser
191191
- run: cargo check --no-default-features -p wasmparser --target x86_64-unknown-none
192192
- run: cargo check --no-default-features -p wasmparser --features std
193+
- run: cargo check --no-default-features -p wasmparser --features validate
193194
- run: |
194195
if cargo tree -p wasm-smith --no-default-features -e no-dev | grep wasmparser; then
195196
echo wasm-smith without default features should not depend on wasmparser

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ wasm-metadata = { version = "0.206.0", path = "crates/wasm-metadata" }
6868
wasm-mutate = { version = "0.206.0", path = "crates/wasm-mutate" }
6969
wasm-shrink = { version = "0.206.0", path = "crates/wasm-shrink" }
7070
wasm-smith = { version = "0.206.0", path = "crates/wasm-smith" }
71-
wasmparser = { version = "0.206.0", path = "crates/wasmparser" }
71+
wasmparser = { version = "0.206.0", path = "crates/wasmparser", default-features = false, features = ['std'] }
7272
wasmprinter = { version = "0.206.0", path = "crates/wasmprinter" }
7373
wast = { version = "206.0.0", path = "crates/wast" }
7474
wat = { version = "1.206.0", path = "crates/wat" }
@@ -87,7 +87,7 @@ wat = { workspace = true }
8787
termcolor = "1.2.0"
8888

8989
# Dependencies of `validate`
90-
wasmparser = { workspace = true, optional = true }
90+
wasmparser = { workspace = true, optional = true, features = ['validate'] }
9191
rayon = { workspace = true, optional = true }
9292

9393
# Dependencies of `print`

crates/wasm-compose/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ workspace = true
1616
[dependencies]
1717
wat = { workspace = true }
1818
wasm-encoder = { workspace = true, features = ['wasmparser'] }
19-
wasmparser = { workspace = true }
19+
wasmparser = { workspace = true, features = ['validate'] }
2020
indexmap = { workspace = true, features = ["serde"] }
2121
anyhow = { workspace = true }
2222
serde = { workspace = true }

crates/wasm-shrink/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ log = { workspace = true }
2020
rand = { workspace = true }
2121
clap = { workspace = true, optional = true }
2222
wasm-mutate = { workspace = true }
23-
wasmparser = { workspace = true }
23+
wasmparser = { workspace = true, features = ['validate'] }
2424

2525
[dev-dependencies]
2626
env_logger = { workspace = true }

crates/wasm-smith/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ leb128 = { workspace = true }
2828
serde = { workspace = true, optional = true }
2929
serde_derive = { workspace = true, optional = true }
3030
wasm-encoder = { workspace = true }
31-
wasmparser = { workspace = true, optional = true }
31+
wasmparser = { workspace = true, optional = true, features = ['validate'] }
3232
wat = { workspace = true, optional = true }
3333

3434
[dev-dependencies]

crates/wasmparser/Cargo.toml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ workspace = true
1717

1818
[dependencies]
1919
bitflags = "2.4.1"
20-
indexmap = { workspace = true }
21-
semver = { workspace = true }
22-
hashbrown = { workspace = true }
23-
ahash = { workspace = true }
20+
indexmap = { workspace = true, optional = true }
21+
semver = { workspace = true, optional = true }
22+
hashbrown = { workspace = true, optional = true }
23+
ahash = { workspace = true, optional = true }
2424

2525
[dev-dependencies]
2626
anyhow = { workspace = true }
@@ -38,5 +38,19 @@ name = "benchmark"
3838
harness = false
3939

4040
[features]
41-
default = ['std']
41+
default = ['std', 'validate']
42+
43+
# A feature which enables implementations of `std::error::Error` as appropriate
44+
# along with other convenience APIs. This additionally uses the standard
45+
# library's source of randomness for seeding hash maps.
4246
std = ['indexmap/std']
47+
48+
# A feature that enables validating WebAssembly files. This is enabled by
49+
# default but not required if you're only parsing a file, for example, as
50+
# opposed to validating all of its contents.
51+
validate = [
52+
'dep:indexmap',
53+
'dep:semver',
54+
'dep:hashbrown',
55+
'dep:ahash',
56+
]

crates/wasmparser/src/binary_reader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl BinaryReaderError {
9393
self.inner.offset
9494
}
9595

96+
#[cfg(feature = "validate")]
9697
pub(crate) fn add_context(&mut self, mut context: String) {
9798
context.push_str("\n");
9899
self.inner.message.insert_str(0, &context);

crates/wasmparser/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
#![deny(missing_docs)]
3030
#![no_std]
31+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3132

3233
extern crate alloc;
3334
#[cfg(feature = "std")]
@@ -48,6 +49,7 @@ mod prelude {
4849
pub use alloc::vec;
4950
pub use alloc::vec::Vec;
5051

52+
#[cfg(feature = "validate")]
5153
pub use crate::map::{HashMap, HashSet, IndexMap, IndexSet};
5254
}
5355

@@ -784,14 +786,20 @@ macro_rules! bail {
784786
pub use crate::binary_reader::{BinaryReader, BinaryReaderError, Result};
785787
pub use crate::parser::*;
786788
pub use crate::readers::*;
787-
pub use crate::resources::*;
788-
pub use crate::validator::*;
789789

790790
mod binary_reader;
791791
mod limits;
792792
mod parser;
793793
mod readers;
794+
795+
#[cfg(feature = "validate")]
794796
mod resources;
797+
#[cfg(feature = "validate")]
795798
mod validator;
799+
#[cfg(feature = "validate")]
800+
pub use crate::resources::*;
801+
#[cfg(feature = "validate")]
802+
pub use crate::validator::*;
796803

804+
#[cfg(feature = "validate")]
797805
pub mod map;

crates/wasmparser/src/limits.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* limitations under the License.
1414
*/
1515

16+
#![cfg_attr(not(feature = "validate"), allow(dead_code))]
17+
1618
// The following limits are imposed by wasmparser on WebAssembly modules.
1719
// The limits are agreed upon with other engines for consistency.
1820
pub const MAX_WASM_TYPES: usize = 1_000_000;

crates/wasmparser/src/readers/component/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ impl PrimitiveValType {
194194
})
195195
}
196196

197+
#[cfg(feature = "validate")]
197198
pub(crate) fn contains_ptr(&self) -> bool {
198199
matches!(self, Self::String)
199200
}

0 commit comments

Comments
 (0)