Skip to content

Commit f696728

Browse files
authored
Add support for the WASM_DYLINK_RUNTIME_PATH subsection of the dylink.0 custom section (#2283)
1 parent 9dd8df4 commit f696728

File tree

11 files changed

+43
-4
lines changed

11 files changed

+43
-4
lines changed

crates/wasmparser/src/readers/core/dylink0.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const WASM_DYLINK_MEM_INFO: u8 = 1;
1212
const WASM_DYLINK_NEEDED: u8 = 2;
1313
const WASM_DYLINK_EXPORT_INFO: u8 = 3;
1414
const WASM_DYLINK_IMPORT_INFO: u8 = 4;
15+
const WASM_DYLINK_RUNTIME_PATH: u8 = 5;
1516

1617
/// Represents a `WASM_DYLINK_MEM_INFO` field
1718
#[derive(Debug, Copy, Clone)]
@@ -56,6 +57,7 @@ pub enum Dylink0Subsection<'a> {
5657
Needed(Vec<&'a str>),
5758
ExportInfo(Vec<ExportInfo<'a>>),
5859
ImportInfo(Vec<ImportInfo<'a>>),
60+
RuntimePath(Vec<&'a str>),
5961
Unknown {
6062
ty: u8,
6163
data: &'a [u8],
@@ -100,6 +102,11 @@ impl<'a> Subsection<'a> for Dylink0Subsection<'a> {
100102
})
101103
.collect::<Result<_, _>>()?,
102104
),
105+
WASM_DYLINK_RUNTIME_PATH => Self::RuntimePath(
106+
(0..reader.read_var_u32()?)
107+
.map(|_| reader.read_string())
108+
.collect::<Result<_, _>>()?,
109+
),
103110
ty => Self::Unknown {
104111
ty,
105112
data,

crates/wasmprinter/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,15 @@ impl Printer<'_, '_> {
20062006
self.end_group()?;
20072007
}
20082008
}
2009+
Dylink0Subsection::RuntimePath(runtime_path) => {
2010+
self.newline(start)?;
2011+
self.start_group("runtime-path")?;
2012+
for s in runtime_path {
2013+
self.result.write_str(" ")?;
2014+
self.print_str(s)?;
2015+
}
2016+
self.end_group()?;
2017+
}
20092018
Dylink0Subsection::Unknown { ty, .. } => {
20102019
bail!("don't know how to print dylink.0 subsection id {ty}");
20112020
}

crates/wast/src/core/binary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ impl Encode for Dylink0Subsection<'_> {
13921392
Dylink0Subsection::Needed(libs) => libs.encode(e),
13931393
Dylink0Subsection::ExportInfo(list) => list.encode(e),
13941394
Dylink0Subsection::ImportInfo(list) => list.encode(e),
1395+
Dylink0Subsection::RuntimePath(list) => list.encode(e),
13951396
}
13961397
}
13971398
}

crates/wast/src/core/custom.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ pub enum Dylink0Subsection<'a> {
261261
Needed(Vec<&'a str>),
262262
ExportInfo(Vec<(&'a str, u32)>),
263263
ImportInfo(Vec<(&'a str, &'a str, u32)>),
264+
RuntimePath(Vec<&'a str>),
264265
}
265266

266267
impl<'a> Parse<'a> for Dylink0<'a> {
@@ -335,6 +336,13 @@ impl<'a> Dylink0<'a> {
335336
.subsections
336337
.push(Dylink0Subsection::ImportInfo(vec![(module, name, flags)])),
337338
}
339+
} else if l.peek::<kw::runtime_path>()? {
340+
parser.parse::<kw::runtime_path>()?;
341+
let mut names = Vec::new();
342+
while !parser.is_empty() {
343+
names.push(parser.parse()?);
344+
}
345+
self.subsections.push(Dylink0Subsection::RuntimePath(names));
338346
} else {
339347
return Err(l.error());
340348
}
@@ -388,6 +396,7 @@ impl Dylink0Subsection<'_> {
388396
Needed(..) => 2,
389397
ExportInfo(..) => 3,
390398
ImportInfo(..) => 4,
399+
RuntimePath(..) => 5,
391400
}
392401
}
393402
}

crates/wast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ pub mod kw {
555555
custom_keyword!(needed);
556556
custom_keyword!(export_info = "export-info");
557557
custom_keyword!(import_info = "import-info");
558+
custom_keyword!(runtime_path = "runtime-path");
558559
custom_keyword!(thread);
559560
custom_keyword!(thread_spawn_ref = "thread.spawn_ref");
560561
custom_keyword!(thread_spawn_indirect = "thread.spawn_indirect");

crates/wit-component/src/linking/metadata.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ pub struct Metadata<'a> {
169169
/// The `WASM_DYLINK_NEEDED` values, if any
170170
pub needed_libs: Vec<&'a str>,
171171

172+
/// The `WASM_DYLINK_RUNTIME_PATH` values, if any
173+
pub runtime_path: Vec<&'a str>,
174+
172175
/// Whether this module exports `__wasm_apply_data_relocs`
173176
pub has_data_relocs: bool,
174177

@@ -228,6 +231,7 @@ impl<'a> Metadata<'a> {
228231
table_alignment: 1,
229232
},
230233
needed_libs: Vec::new(),
234+
runtime_path: Vec::new(),
231235
has_data_relocs: false,
232236
has_ctors: false,
233237
has_initialize: false,
@@ -267,6 +271,9 @@ impl<'a> Metadata<'a> {
267271
.map(|info| ((info.module, info.field), info.flags)),
268272
);
269273
}
274+
Dylink0Subsection::RuntimePath(runtime_path) => {
275+
result.runtime_path.extend(runtime_path.iter());
276+
}
270277
Dylink0Subsection::Unknown { ty, .. } => {
271278
bail!("unrecognized `dylink.0` subsection: {ty}")
272279
}

tests/cli/dump-dylink0.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
(export-info "a" 0)
88
(import-info "a" "a" 0)
99
(export-info "a" 2 binding-local binding-weak 0 undefined)
10+
(runtime-path "a" "b")
1011
)
1112
)

tests/cli/dump-dylink0.wat.stdout

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
0x0 | 00 61 73 6d | version 1 (Module)
22
| 01 00 00 00
3-
0x8 | 00 2a | custom section
3+
0x8 | 00 31 | custom section
44
0xa | 08 64 79 6c | name: "dylink.0"
55
| 69 6e 6b 2e
66
| 30
@@ -13,4 +13,6 @@
1313
0x26 | 04 06 01 01 | ImportInfo([ImportInfo { module: "a", field: "a", flags: SymbolFlags(0x0) }])
1414
| 61 01 61 00
1515
0x2e | 03 04 01 01 | ExportInfo([ExportInfo { name: "a", flags: SymbolFlags(BINDING_WEAK | BINDING_LOCAL | UNDEFINED) }])
16-
| 61 13
16+
| 61 13
17+
0x34 | 05 05 02 01 | RuntimePath(["a", "b"])
18+
| 61 01 62

tests/cli/dylink0.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
(export-info "a" 0)
4848
(import-info "a" "a" 0)
4949
(import-info "b" "b" 1)
50+
(runtime-path "a" "b")
5051
)
5152
)
5253

tests/snapshots/cli/dylink0.wast.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@
4545
},
4646
{
4747
"type": "module",
48-
"line": 53,
48+
"line": 54,
4949
"filename": "dylink0.7.wasm",
5050
"module_type": "binary"
5151
},
5252
{
5353
"type": "module",
54-
"line": 66,
54+
"line": 67,
5555
"filename": "dylink0.8.wasm",
5656
"module_type": "binary"
5757
}

0 commit comments

Comments
 (0)