Skip to content

Commit f06c721

Browse files
authored
Don't fail to print known custom sections (#2264)
This commit updates the `wasm-tools print` subcommand to print raw custom sections for some "known" custom sections if there is not printer registered for it. For example the `linking` and `reloc.CODE` custom sections are "known" but have no text format. Previously they would fail to print but now they will print the raw bytes.
1 parent a70eb29 commit f06c721

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

crates/wasmprinter/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,10 @@ impl Printer<'_, '_> {
569569
})
570570
.print_known_custom_section(c.clone())
571571
{
572-
Ok(()) => self.print_known_custom_section(c.clone())?,
572+
Ok(true) => {
573+
self.print_known_custom_section(c.clone())?;
574+
}
575+
Ok(false) => self.print_raw_custom_section(state, c.clone())?,
573576
Err(e) if !e.is::<BinaryReaderError>() => return Err(e),
574577
Err(e) => {
575578
let msg = format!(
@@ -1879,29 +1882,30 @@ impl Printer<'_, '_> {
18791882
Ok(())
18801883
}
18811884

1882-
fn print_known_custom_section(&mut self, section: CustomSectionReader<'_>) -> Result<()> {
1885+
fn print_known_custom_section(&mut self, section: CustomSectionReader<'_>) -> Result<bool> {
18831886
match section.as_known() {
18841887
// For now `wasmprinter` has invented syntax for `producers` and
18851888
// `dylink.0` below to use in tests. Note that this syntax is not
18861889
// official at this time.
18871890
KnownCustom::Producers(s) => {
18881891
self.newline(section.range().start)?;
1889-
self.print_producers_section(s)
1892+
self.print_producers_section(s)?;
1893+
Ok(true)
18901894
}
18911895
KnownCustom::Dylink0(s) => {
18921896
self.newline(section.range().start)?;
1893-
self.print_dylink0_section(s)
1897+
self.print_dylink0_section(s)?;
1898+
Ok(true)
18941899
}
18951900

18961901
// These are parsed during `read_names` and are part of
18971902
// printing elsewhere, so don't print them.
1898-
KnownCustom::Name(_) | KnownCustom::BranchHints(_) => Ok(()),
1903+
KnownCustom::Name(_) | KnownCustom::BranchHints(_) => Ok(true),
18991904
#[cfg(feature = "component-model")]
1900-
KnownCustom::ComponentName(_) => Ok(()),
1905+
KnownCustom::ComponentName(_) => Ok(true),
19011906

1902-
_ => bail!("unknown custom section"),
1903-
}?;
1904-
Ok(())
1907+
_ => Ok(false),
1908+
}
19051909
}
19061910

19071911
fn print_raw_custom_section(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
;; RUN: print %
2+
3+
(module
4+
(type (;0;) (func))
5+
(type (;1;) (func (param i32)))
6+
(import "env" "__linear_memory" (memory (;0;) 0))
7+
(import "env" "__stack_pointer" (global (;0;) (mut i32)))
8+
(import "env" "foo" (func (;0;) (type 1)))
9+
(import "env" "__stack_chk_fail" (func (;1;) (type 0)))
10+
(func (;2;) (type 0)
11+
(local i32)
12+
global.get 0
13+
i32.const 16
14+
i32.sub
15+
local.tee 0
16+
global.set 0
17+
local.get 0
18+
i32.const 0
19+
i32.load
20+
i32.store offset=12
21+
local.get 0
22+
i32.const 8
23+
i32.add
24+
call 0
25+
block ;; label = @1
26+
i32.const 0
27+
i32.load
28+
local.get 0
29+
i32.load offset=12
30+
i32.eq
31+
br_if 0 (;@1;)
32+
call 1
33+
unreachable
34+
end
35+
local.get 0
36+
i32.const 16
37+
i32.add
38+
global.set 0
39+
)
40+
(@custom "linking" (after code) "\02\08\a5\80\80\80\00\05\00\04\02\03bar\02\10\00\01\10\11__stack_chk_guard\00\10\00\00\10\01")
41+
(@custom "reloc.CODE" (after code) "\03\07\07\06\01\07\11\01\03\1c\02\00\00*\03\035\02\00\00C\04\07P\01")
42+
(@producers
43+
(processed-by "clang" "20.1.8-wasi-sdk (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)")
44+
)
45+
(@custom "target_features" (after code) "\08+\0bbulk-memory+\0fbulk-memory-opt+\16call-indirect-overlong+\0amultivalue+\0fmutable-globals+\13nontrapping-fptoint+\0freference-types+\08sign-ext")
46+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(module
2+
(type (;0;) (func))
3+
(type (;1;) (func (param i32)))
4+
(import "env" "__linear_memory" (memory (;0;) 0))
5+
(import "env" "__stack_pointer" (global (;0;) (mut i32)))
6+
(import "env" "foo" (func (;0;) (type 1)))
7+
(import "env" "__stack_chk_fail" (func (;1;) (type 0)))
8+
(func (;2;) (type 0)
9+
(local i32)
10+
global.get 0
11+
i32.const 16
12+
i32.sub
13+
local.tee 0
14+
global.set 0
15+
local.get 0
16+
i32.const 0
17+
i32.load
18+
i32.store offset=12
19+
local.get 0
20+
i32.const 8
21+
i32.add
22+
call 0
23+
block ;; label = @1
24+
i32.const 0
25+
i32.load
26+
local.get 0
27+
i32.load offset=12
28+
i32.eq
29+
br_if 0 (;@1;)
30+
call 1
31+
unreachable
32+
end
33+
local.get 0
34+
i32.const 16
35+
i32.add
36+
global.set 0
37+
)
38+
(@custom "linking" (after code) "/02/08/a5/80/80/80/00/05/00/04/02/03bar/02/10/00/01/10/11__stack_chk_guard/00/10/00/00/10/01")
39+
(@custom "reloc.CODE" (after code) "/03/07/07/06/01/07/11/01/03/1c/02/00/00*/03/035/02/00/00C/04/07P/01")
40+
(@custom "target_features" (after code) "/08+/0bbulk-memory+/0fbulk-memory-opt+/16call-indirect-overlong+/0amultivalue+/0fmutable-globals+/13nontrapping-fptoint+/0freference-types+/08sign-ext")
41+
(@producers
42+
(processed-by "clang" "20.1.8-wasi-sdk (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)")
43+
)
44+
)

0 commit comments

Comments
 (0)