Skip to content

Commit 6ed55f3

Browse files
authored
Strip component-type* custom sections during componentization (#397)
This commit leverages a new convenience API in `wasmparser` to strip the `component-type*` custom sections emitted by `wit-bindgen` guests during the componentization process for a core wasm module. These custom sections don't have any place in the core wasm module except as part of the build process and will end up just duplicating bits and pieces of the component itself, so there's no need for them to stick around once a component has been created.
1 parent 9d7b320 commit 6ed55f3

36 files changed

Lines changed: 227 additions & 207 deletions

File tree

Cargo.lock

Lines changed: 35 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ env_logger = "0.9.1"
2727

2828
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ["component-model"] }
2929
wasmtime-environ = { git = "https://github.com/bytecodealliance/wasmtime" }
30-
wasmprinter = "0.2.41"
31-
wasmparser = "0.92.0"
32-
wasm-encoder = "0.18.0"
33-
wat = "1.0.49"
30+
wasmprinter = "0.2.42"
31+
wasmparser = "0.93.0"
32+
wasm-encoder = "0.19.0"
33+
wat = "1.0.50"
3434

3535
wit-bindgen-core = { path = 'crates/bindgen-core', version = '0.3.0' }
3636
wit-bindgen-gen-guest-c = { path = 'crates/gen-guest-c', version = '0.3.0' }

crates/bindgen-core/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ wit-parser = { workspace = true }
1212
anyhow = { workspace = true }
1313
wit-component = { workspace = true }
1414
wasmtime-environ = { workspace = true, features = ['component-model'], optional = true }
15-
wasmparser = { workspace = true, optional = true }
1615

1716
[features]
18-
component-generator = ['dep:wasmtime-environ', 'dep:wasmparser']
17+
component-generator = ['dep:wasmtime-environ']

crates/bindgen-core/src/component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
1010
use crate::{Files, WorldGenerator};
1111
use anyhow::{Context, Result};
12-
use wasmparser::{Validator, WasmFeatures};
1312
use wasmtime_environ::component::{
1413
Component, ComponentTypesBuilder, StaticModuleIndex, Translator,
1514
};
15+
use wasmtime_environ::wasmparser::{Validator, WasmFeatures};
1616
use wasmtime_environ::{ModuleTranslation, PrimaryMap, ScopeVec, Tunables};
1717
use wit_component::ComponentInterfaces;
1818

crates/wit-component/src/extract.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ pub struct ModuleInterfaces {
2323
/// later on.
2424
pub fn extract_module_interfaces(wasm: &[u8]) -> Result<ModuleInterfaces> {
2525
let mut ret = ModuleInterfaces::default();
26+
let mut new_module = wasm_encoder::Module::new();
2627

2728
for payload in wasmparser::Parser::new(0).parse_all(wasm) {
28-
if let wasmparser::Payload::CustomSection(cs) =
29-
payload.context("decoding item in module")?
30-
{
31-
if !cs.name().starts_with("component-type") {
32-
continue;
29+
let payload = payload.context("decoding item in module")?;
30+
match payload {
31+
wasmparser::Payload::CustomSection(cs) if cs.name().starts_with("component-type") => {
32+
ret.decode(cs.data())
33+
.with_context(|| format!("decoding custom section {}", cs.name()))?;
34+
}
35+
_ => {
36+
if let Some((id, range)) = payload.as_section() {
37+
new_module.section(&wasm_encoder::RawSection {
38+
id,
39+
data: &wasm[range],
40+
});
41+
}
3342
}
34-
ret.decode(cs.data())
35-
.with_context(|| format!("decoding custom section {}", cs.name()))?;
3643
}
3744
}
3845

39-
// TODO: should remove the custom sections decoded above from the wasm binary
40-
// created here, and bytecodealliance/wasmparser#792 should help with that
41-
// to make the loop above pretty small.
42-
ret.wasm = wasm.to_vec();
46+
ret.wasm = new_module.finish();
4347

4448
Ok(ret)
4549
}

crates/wit-component/tests/components/adapt-empty-interface/component.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
(core instance (;0;) (instantiate 2))
2828
(alias core export 0 "0" (core func (;0;)))
29-
(core instance (;1;)
29+
(core instance (;1;)
3030
(export "thunk" (func 0))
3131
)
3232
(core instance (;2;) (instantiate 1
@@ -36,7 +36,7 @@
3636
(core instance (;3;) (instantiate 0))
3737
(alias core export 0 "$imports" (core table (;0;)))
3838
(alias core export 3 "thunk" (core func (;1;)))
39-
(core instance (;4;)
39+
(core instance (;4;)
4040
(export "$imports" (table 0))
4141
(export "0" (func 1))
4242
)

crates/wit-component/tests/components/adapt-inject-stack/component.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
local.tee 0
3333
i32.const -1
3434
i32.eq
35-
if ;; label = @1
35+
if ;; label = @1
3636
unreachable
3737
end
3838
local.get 0
@@ -48,7 +48,7 @@
4848
(start $initialize_stack_pointer)
4949
)
5050
(type (;0;) (func (result "a" u32) (result "b" u32)))
51-
(type (;1;)
51+
(type (;1;)
5252
(instance
5353
(alias outer 1 0 (type (;0;)))
5454
(export "get-two" (func (type 0)))
@@ -88,19 +88,19 @@
8888
)
8989
(core instance (;0;) (instantiate 2))
9090
(alias core export 0 "1" (core func (;0;)))
91-
(core instance (;1;)
91+
(core instance (;1;)
9292
(export "get_sum" (func 0))
9393
)
9494
(core instance (;2;) (instantiate 1
9595
(with "old" (instance 1))
9696
)
9797
)
9898
(alias core export 2 "memory" (core memory (;0;)))
99-
(core instance (;3;)
99+
(core instance (;3;)
100100
(export "memory" (memory 0))
101101
)
102102
(alias core export 0 "0" (core func (;1;)))
103-
(core instance (;4;)
103+
(core instance (;4;)
104104
(export "get-two" (func 1))
105105
)
106106
(core instance (;5;) (instantiate 0
@@ -112,7 +112,7 @@
112112
(alias export 0 "get-two" (func (;0;)))
113113
(core func (;2;) (canon lower (func 0) (memory 0)))
114114
(alias core export 5 "get_sum" (core func (;3;)))
115-
(core instance (;6;)
115+
(core instance (;6;)
116116
(export "$imports" (table 0))
117117
(export "0" (func 2))
118118
(export "1" (func 3))

crates/wit-component/tests/components/adapt-list-return/component.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818
(type (;0;) (list u8))
1919
(type (;1;) (func (result 0)))
20-
(type (;2;)
20+
(type (;2;)
2121
(instance
2222
(alias outer 1 1 (type (;0;)))
2323
(export "read" (func (type 0)))
@@ -59,7 +59,7 @@
5959
)
6060
(core instance (;0;) (instantiate 2))
6161
(alias core export 0 "1" (core func (;0;)))
62-
(core instance (;1;)
62+
(core instance (;1;)
6363
(export "read" (func 0))
6464
)
6565
(core instance (;2;) (instantiate 1
@@ -68,7 +68,7 @@
6868
)
6969
(alias core export 2 "memory" (core memory (;0;)))
7070
(alias core export 0 "0" (core func (;1;)))
71-
(core instance (;3;)
71+
(core instance (;3;)
7272
(export "read" (func 1))
7373
)
7474
(core instance (;4;) (instantiate 0
@@ -80,7 +80,7 @@
8080
(alias export 0 "read" (func (;0;)))
8181
(core func (;3;) (canon lower (func 0) (memory 0) (realloc 2)))
8282
(alias core export 4 "read" (core func (;4;)))
83-
(core instance (;5;)
83+
(core instance (;5;)
8484
(export "$imports" (table 0))
8585
(export "0" (func 3))
8686
(export "1" (func 4))

crates/wit-component/tests/components/adapt-memory-simple/component.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(export "log" (func $log))
66
)
77
(type (;0;) (func (param "s" string)))
8-
(type (;1;)
8+
(type (;1;)
99
(instance
1010
(alias outer 1 0 (type (;0;)))
1111
(export "log" (func (type 0)))
@@ -46,7 +46,7 @@
4646
)
4747
(core instance (;0;) (instantiate 2))
4848
(alias core export 0 "1" (core func (;0;)))
49-
(core instance (;1;)
49+
(core instance (;1;)
5050
(export "log" (func 0))
5151
)
5252
(core instance (;2;) (instantiate 1
@@ -55,7 +55,7 @@
5555
)
5656
(alias core export 2 "memory" (core memory (;0;)))
5757
(alias core export 0 "0" (core func (;1;)))
58-
(core instance (;3;)
58+
(core instance (;3;)
5959
(export "log" (func 1))
6060
)
6161
(core instance (;4;) (instantiate 0
@@ -66,7 +66,7 @@
6666
(alias export 0 "log" (func (;0;)))
6767
(core func (;2;) (canon lower (func 0) (memory 0) string-encoding=utf8))
6868
(alias core export 4 "log" (core func (;3;)))
69-
(core instance (;5;)
69+
(core instance (;5;)
7070
(export "$imports" (table 0))
7171
(export "0" (func 2))
7272
(export "1" (func 3))

0 commit comments

Comments
 (0)