From 7530629a3a127a1429b4837a1b1632f11d3dfeea Mon Sep 17 00:00:00 2001 From: Arsenii Lyashenko Date: Wed, 22 Jan 2025 19:58:06 +0300 Subject: [PATCH] Fix `std` feature in `wasm-encoder` (#1961) * Fix `std` feature in `wasm-encoder` * Update Cargo.lock * Some updates * Fix `std` features * Test `no_std` works for `wasm-encoder` in CI --- .github/workflows/main.yml | 2 ++ Cargo.lock | 12 ++++++++---- Cargo.toml | 10 +++++----- crates/c-api/Cargo.toml | 2 +- crates/wasm-compose/Cargo.toml | 4 ++-- crates/wasm-encoder/Cargo.toml | 4 ++-- crates/wasm-encoder/src/core/code.rs | 5 +---- crates/wasm-encoder/src/lib.rs | 21 +++++++++++++++------ crates/wasm-metadata/Cargo.toml | 4 ++-- crates/wasm-mutate-stats/Cargo.toml | 2 +- crates/wasm-mutate/Cargo.toml | 6 +++--- crates/wasm-shrink/Cargo.toml | 2 +- crates/wasm-smith/Cargo.toml | 8 +++----- crates/wast/Cargo.toml | 4 ++-- crates/wast/src/encode.rs | 12 ++++++++---- crates/wit-component/Cargo.toml | 6 +++--- crates/wit-parser/Cargo.toml | 2 +- fuzz/Cargo.toml | 2 +- 18 files changed, 61 insertions(+), 47 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 72462c24bc..b7e210547e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -259,6 +259,8 @@ jobs: - run: cargo check --no-default-features -p wat --features component-model - run: cargo check --no-default-features -p wat --features dwarf - run: cargo check --no-default-features -p wasm-encoder + - run: cargo check --no-default-features -p wasm-encoder --target x86_64-unknown-none + - run: cargo check --no-default-features -p wasm-encoder --target x86_64-unknown-none --features wasmparser,component-model - run: cargo check --no-default-features -p wasm-encoder --features component-model - run: cargo check --no-default-features -p wasm-encoder --features wasmparser - run: cargo check --no-default-features -p wasm-smith diff --git a/Cargo.lock b/Cargo.lock index fb6841df02..e0321f24aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1083,6 +1083,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "libc" version = "0.2.159" @@ -1895,7 +1901,7 @@ name = "wasm-encoder" version = "0.224.0" dependencies = [ "anyhow", - "leb128", + "leb128fmt", "tempfile", "wasmparser 0.224.0", "wasmprinter 0.224.0", @@ -1993,8 +1999,6 @@ dependencies = [ "clap", "criterion", "flagset", - "indexmap 2.7.0", - "leb128", "libfuzzer-sys", "rand", "serde", @@ -2371,7 +2375,7 @@ dependencies = [ "anyhow", "bumpalo", "gimli 0.30.0", - "leb128", + "leb128fmt", "libtest-mimic", "memchr", "rand", diff --git a/Cargo.toml b/Cargo.toml index b46a611533..58949b2d3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,7 @@ comfy-table = { version = "7.1.3", default-features = false } criterion = { version = "0.5.1", default-features = false } env_logger = "0.11" indexmap = { version = "2.7.0", default-features = false } -leb128 = "0.2.4" +leb128fmt = { version = "0.1.0", default-features = false } libfuzzer-sys = "0.4.0" log = "0.4.17" num_cpus = "1.13" @@ -103,12 +103,12 @@ gimli = "0.30.0" id-arena = "2" wasm-compose = { version = "0.224.0", path = "crates/wasm-compose" } -wasm-encoder = { version = "0.224.0", path = "crates/wasm-encoder", default-features = false, features = ["std"] } +wasm-encoder = { version = "0.224.0", path = "crates/wasm-encoder", default-features = false } wasm-metadata = { version = "0.224.0", path = "crates/wasm-metadata" } wasm-mutate = { version = "0.224.0", path = "crates/wasm-mutate" } wasm-shrink = { version = "0.224.0", path = "crates/wasm-shrink" } wasm-smith = { version = "0.224.0", path = "crates/wasm-smith" } -wasmparser = { version = "0.224.0", path = "crates/wasmparser", default-features = false, features = ['std', 'simd'] } +wasmparser = { version = "0.224.0", path = "crates/wasmparser", default-features = false, features = ['simd'] } wasmprinter = { version = "0.224.0", path = "crates/wasmprinter", default-features = false } wast = { version = "224.0.0", path = "crates/wast", default-features = false } wat = { version = "1.224.0", path = "crates/wat", default-features = false } @@ -129,7 +129,7 @@ wat = { workspace = true, features = ['dwarf', 'component-model'] } termcolor = { workspace = true } # Dependencies of `validate` -wasmparser = { workspace = true, optional = true, features = ['component-model', 'simd'] } +wasmparser = { workspace = true, optional = true, features = ['std', 'component-model', 'simd'] } rayon = { workspace = true, optional = true } bitflags = { workspace = true, optional = true } @@ -150,7 +150,7 @@ wasm-shrink = { workspace = true, features = ["clap"], optional = true } wasm-mutate = { workspace = true, features = ["clap"], optional = true } # Dependencies of `strip` -wasm-encoder = { workspace = true, optional = true } +wasm-encoder = { workspace = true, features = ["std"], optional = true } regex = { version = "1.6.0", optional = true } # Dependencies of `compose` diff --git a/crates/c-api/Cargo.toml b/crates/c-api/Cargo.toml index afff15631f..dad801e6d7 100644 --- a/crates/c-api/Cargo.toml +++ b/crates/c-api/Cargo.toml @@ -25,7 +25,7 @@ arbitrary = { workspace = true, features = ["derive"] } wasm-mutate = { workspace = true } wasm-shrink = { workspace = true } wasm-smith = { workspace = true } -wasmparser = { workspace = true } +wasmparser = { workspace = true, features = ["std"] } wasmprinter = { workspace = true } wast = { workspace = true } wat = { workspace = true } diff --git a/crates/wasm-compose/Cargo.toml b/crates/wasm-compose/Cargo.toml index 6c29798212..30ed8954ed 100644 --- a/crates/wasm-compose/Cargo.toml +++ b/crates/wasm-compose/Cargo.toml @@ -16,8 +16,8 @@ workspace = true [dependencies] wat = { workspace = true } -wasm-encoder = { workspace = true, features = ['wasmparser', 'component-model'] } -wasmparser = { workspace = true, features = ['validate', 'component-model', 'features'] } +wasm-encoder = { workspace = true, features = ['std', 'wasmparser', 'component-model'] } +wasmparser = { workspace = true, features = ['std', 'validate', 'component-model', 'features'] } indexmap = { workspace = true, features = ["serde"] } anyhow = { workspace = true } serde = { workspace = true } diff --git a/crates/wasm-encoder/Cargo.toml b/crates/wasm-encoder/Cargo.toml index 9332255e09..c94fa351b2 100644 --- a/crates/wasm-encoder/Cargo.toml +++ b/crates/wasm-encoder/Cargo.toml @@ -20,7 +20,7 @@ all-features = true workspace = true [dependencies] -leb128 = { workspace = true } +leb128fmt.workspace = true # Enable this dependency to get a bunch of `From for # wasm_encoder::Foo` impls. @@ -36,7 +36,7 @@ wasmprinter = { workspace = true } default = ['std', 'component-model'] # A feature which enables implementations of `std::error::Error` as appropriate. -std = [] +std = ["wasmparser?/std"] # On-by-default: conditional support for emitting components in addition to # core modules. diff --git a/crates/wasm-encoder/src/core/code.rs b/crates/wasm-encoder/src/core/code.rs index 0b544fe914..686b33cf59 100644 --- a/crates/wasm-encoder/src/core/code.rs +++ b/crates/wasm-encoder/src/core/code.rs @@ -4001,10 +4001,7 @@ impl ConstExpr { if prefix != 0xd2 { return None; } - leb128::read::unsigned(&mut &self.bytes[1..]) - .ok()? - .try_into() - .ok() + leb128fmt::decode_uint_slice::(&self.bytes[1..], &mut 0).ok() } } diff --git a/crates/wasm-encoder/src/lib.rs b/crates/wasm-encoder/src/lib.rs index 395e5cff55..2d17c3fb29 100644 --- a/crates/wasm-encoder/src/lib.rs +++ b/crates/wasm-encoder/src/lib.rs @@ -135,25 +135,29 @@ impl Encode for usize { impl Encode for u32 { fn encode(&self, sink: &mut Vec) { - leb128::write::unsigned(sink, (*self).into()).unwrap(); + let (value, pos) = leb128fmt::encode_u32(*self).unwrap(); + sink.extend_from_slice(&value[..pos]); } } impl Encode for i32 { fn encode(&self, sink: &mut Vec) { - leb128::write::signed(sink, (*self).into()).unwrap(); + let (value, pos) = leb128fmt::encode_s32(*self).unwrap(); + sink.extend_from_slice(&value[..pos]); } } impl Encode for u64 { fn encode(&self, sink: &mut Vec) { - leb128::write::unsigned(sink, *self).unwrap(); + let (value, pos) = leb128fmt::encode_u64(*self).unwrap(); + sink.extend_from_slice(&value[..pos]); } } impl Encode for i64 { fn encode(&self, sink: &mut Vec) { - leb128::write::signed(sink, *self).unwrap(); + let (value, pos) = leb128fmt::encode_s64(*self).unwrap(); + sink.extend_from_slice(&value[..pos]); } } @@ -200,8 +204,8 @@ where } fn encoding_size(n: u32) -> usize { - let mut buf = [0u8; 5]; - leb128::write::unsigned(&mut &mut buf[..], n.into()).unwrap() + let (_value, pos) = leb128fmt::encode_u32(n).unwrap(); + pos } fn encode_section(sink: &mut Vec, count: u32, bytes: &[u8]) { @@ -214,6 +218,11 @@ fn encode_section(sink: &mut Vec, count: u32, bytes: &[u8]) { mod test { use super::*; + #[test] + fn test_encoding_size() { + assert_eq!(encoding_size(624485), 3); + } + #[test] fn it_encodes_an_empty_module() { let bytes = Module::new().finish(); diff --git a/crates/wasm-metadata/Cargo.toml b/crates/wasm-metadata/Cargo.toml index 5b91ec02b9..f3593bb084 100644 --- a/crates/wasm-metadata/Cargo.toml +++ b/crates/wasm-metadata/Cargo.toml @@ -13,8 +13,8 @@ workspace = true [dependencies] clap = { workspace = true, optional = true } anyhow = { workspace = true } -wasmparser = { workspace = true, features = ['component-model', 'hash-collections'] } -wasm-encoder = { workspace = true, features = ['component-model'] } +wasmparser = { workspace = true, features = ['std', 'component-model', 'hash-collections'] } +wasm-encoder = { workspace = true, features = ['std', 'component-model'] } indexmap = { workspace = true, features = ["serde"] } serde = { workspace = true } serde_derive = { workspace = true } diff --git a/crates/wasm-mutate-stats/Cargo.toml b/crates/wasm-mutate-stats/Cargo.toml index cc6e380106..d77079a114 100644 --- a/crates/wasm-mutate-stats/Cargo.toml +++ b/crates/wasm-mutate-stats/Cargo.toml @@ -14,7 +14,7 @@ num_cpus = { workspace = true } rand = { workspace = true } wasm-mutate = { workspace = true } wasmprinter = { workspace = true } -wasmparser = { workspace = true, features = ['validate'] } +wasmparser = { workspace = true, features = ['std', 'validate'] } wasmtime = { workspace = true } env_logger = { workspace = true } itertools = "0.13.0" diff --git a/crates/wasm-mutate/Cargo.toml b/crates/wasm-mutate/Cargo.toml index 17f12a4a3f..5ec4b598d3 100644 --- a/crates/wasm-mutate/Cargo.toml +++ b/crates/wasm-mutate/Cargo.toml @@ -13,8 +13,8 @@ workspace = true [dependencies] clap = { workspace = true, optional = true } thiserror = "1.0.28" -wasmparser = { workspace = true, features = ['simd'] } -wasm-encoder = { workspace = true, features = ['wasmparser'] } +wasmparser = { workspace = true, features = ['std', 'simd'] } +wasm-encoder = { workspace = true, features = ['std', 'wasmparser'] } rand = { workspace = true } log = { workspace = true } egg = "0.6.0" @@ -24,4 +24,4 @@ anyhow = { workspace = true } wat = { workspace = true } wasmprinter = { workspace = true } env_logger = { workspace = true } -wasmparser = { workspace = true, features = ['validate', 'features', 'simd'] } +wasmparser = { workspace = true, features = ['std', 'validate', 'features', 'simd'] } diff --git a/crates/wasm-shrink/Cargo.toml b/crates/wasm-shrink/Cargo.toml index e614d5b65c..26a73f8598 100644 --- a/crates/wasm-shrink/Cargo.toml +++ b/crates/wasm-shrink/Cargo.toml @@ -21,7 +21,7 @@ log = { workspace = true } rand = { workspace = true } clap = { workspace = true, optional = true } wasm-mutate = { workspace = true } -wasmparser = { workspace = true, features = ['validate', 'features'] } +wasmparser = { workspace = true, features = ['std', 'validate', 'features'] } [dev-dependencies] env_logger = { workspace = true } diff --git a/crates/wasm-smith/Cargo.toml b/crates/wasm-smith/Cargo.toml index f432ffac88..d94573cda6 100644 --- a/crates/wasm-smith/Cargo.toml +++ b/crates/wasm-smith/Cargo.toml @@ -27,18 +27,16 @@ anyhow = { workspace = true } arbitrary = { workspace = true, features = ["derive"] } clap = { workspace = true, optional = true } flagset = "0.4" -indexmap = { workspace = true } -leb128 = { workspace = true } serde = { workspace = true, optional = true } serde_derive = { workspace = true, optional = true } -wasm-encoder = { workspace = true } -wasmparser = { workspace = true, optional = true, features = ['validate', 'features', 'simd'] } +wasm-encoder = { workspace = true, features = ['std'] } +wasmparser = { workspace = true, optional = true, features = ['std', 'validate', 'features', 'simd'] } wat = { workspace = true, optional = true } [dev-dependencies] criterion = { workspace = true } rand = { workspace = true } -wasmparser = { workspace = true, features = ['validate', 'features', 'simd'] } +wasmparser = { workspace = true, features = ['std', 'validate', 'features', 'simd'] } wasmprinter = { workspace = true } wat = { workspace = true } diff --git a/crates/wast/Cargo.toml b/crates/wast/Cargo.toml index 5798a0347d..3e087c2fd6 100644 --- a/crates/wast/Cargo.toml +++ b/crates/wast/Cargo.toml @@ -20,10 +20,10 @@ all-features = true workspace = true [dependencies] -leb128 = { workspace = true } +leb128fmt = { workspace = true } unicode-width = "0.2.0" memchr = "2.4.1" -wasm-encoder = { workspace = true } +wasm-encoder = { workspace = true, features = ['std'] } bumpalo = "3.14.0" gimli = { workspace = true, optional = true } diff --git a/crates/wast/src/encode.rs b/crates/wast/src/encode.rs index 2fa57d60f7..43db4f6bdc 100644 --- a/crates/wast/src/encode.rs +++ b/crates/wast/src/encode.rs @@ -51,25 +51,29 @@ impl Encode for u8 { impl Encode for u32 { fn encode(&self, e: &mut Vec) { - leb128::write::unsigned(e, (*self).into()).unwrap(); + let (value, pos) = leb128fmt::encode_u32(*self).unwrap(); + e.extend_from_slice(&value[..pos]); } } impl Encode for i32 { fn encode(&self, e: &mut Vec) { - leb128::write::signed(e, (*self).into()).unwrap(); + let (value, pos) = leb128fmt::encode_s32(*self).unwrap(); + e.extend_from_slice(&value[..pos]); } } impl Encode for u64 { fn encode(&self, e: &mut Vec) { - leb128::write::unsigned(e, *self).unwrap(); + let (value, pos) = leb128fmt::encode_u64(*self).unwrap(); + e.extend_from_slice(&value[..pos]); } } impl Encode for i64 { fn encode(&self, e: &mut Vec) { - leb128::write::signed(e, *self).unwrap(); + let (value, pos) = leb128fmt::encode_s64(*self).unwrap(); + e.extend_from_slice(&value[..pos]); } } diff --git a/crates/wit-component/Cargo.toml b/crates/wit-component/Cargo.toml index f6b9e78710..7697acff68 100644 --- a/crates/wit-component/Cargo.toml +++ b/crates/wit-component/Cargo.toml @@ -20,8 +20,8 @@ workspace = true all-features = true [dependencies] -wasmparser = { workspace = true, features = ['component-model', 'simd'] } -wasm-encoder = { workspace = true, features = ["wasmparser"] } +wasmparser = { workspace = true, features = ['std', 'component-model', 'simd'] } +wasm-encoder = { workspace = true, features = ['std', 'wasmparser'] } wasm-metadata = { workspace = true } wit-parser = { workspace = true, features = ['decoding', 'serde'] } anyhow = { workspace = true } @@ -35,7 +35,7 @@ serde_derive = { workspace = true } serde_json = { workspace = true } [dev-dependencies] -wasmparser = { workspace = true, features = ['component-model', 'features'] } +wasmparser = { workspace = true, features = ['std', 'component-model', 'features'] } wasmprinter = { workspace = true, features = ['component-model'] } glob = "0.3.0" pretty_assertions = "1.3.0" diff --git a/crates/wit-parser/Cargo.toml b/crates/wit-parser/Cargo.toml index 3084850dee..596c3689ca 100644 --- a/crates/wit-parser/Cargo.toml +++ b/crates/wit-parser/Cargo.toml @@ -25,7 +25,7 @@ log = { workspace = true } semver = { workspace = true } serde = { workspace = true, optional = true } serde_derive = { workspace = true, optional = true } -wasmparser = { workspace = true, optional = true, features = ['validate', 'component-model', 'features'] } +wasmparser = { workspace = true, optional = true, features = ['std', 'validate', 'component-model', 'features'] } serde_json = { workspace = true, optional = true } wat = { workspace = true, optional = true } diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index c040daf0cc..d6a60c31a7 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -19,7 +19,7 @@ log = { workspace = true } tempfile = "3.0" wasm-mutate = { workspace = true } wasm-smith = { workspace = true, features = ['component-model', 'wasmparser'] } -wasmparser = { workspace = true, features = ['features'] } +wasmparser = { workspace = true, features = ['std', 'features'] } wasmprinter = { workspace = true, features = ['component-model'] } wasmtime = { workspace = true, optional = true } wast = { workspace = true }