Skip to content

Commit

Permalink
fix duplicate sections (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maaarcocr authored Feb 13, 2024
1 parent cadd23a commit c71232e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 45 deletions.
1 change: 0 additions & 1 deletion ir/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl ItemsBuilder {
let id = item.id;
self.size_added += item.size;
self.items.insert(id, item);

let old_value = self.parsed.insert(id);
assert!(
old_value,
Expand Down
52 changes: 8 additions & 44 deletions parser/wasm_parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<'a> Parse<'a> for ModuleReader<'a> {
wasmparser::Payload::StartSection { func, range } => {
StartSection {
function_index: func,
data: &self.data[range.start..range.end],
_data: &self.data[range.start..range.end],
}
.parse_items(items, idx)?;
}
Expand All @@ -194,12 +194,6 @@ impl<'a> Parse<'a> for ModuleReader<'a> {
wasmparser::Payload::DataSection(mut reader) => {
reader.parse_items(items, (idx, &names.data_names))?;
}
wasmparser::Payload::DataCountSection { range, .. } => {
DataCountSection {
size: range.end - range.start,
}
.parse_items(items, idx)?;
}
wasmparser::Payload::CodeSectionStart { .. }
| wasmparser::Payload::FunctionSection(_) => {
unreachable!("unexpected code or function section found");
Expand All @@ -212,7 +206,8 @@ impl<'a> Parse<'a> for ModuleReader<'a> {
| wasmparser::Payload::ModuleSectionStart { .. }
| wasmparser::Payload::ModuleSectionEntry { .. }
| wasmparser::Payload::UnknownSection { .. }
| wasmparser::Payload::End { .. } => {}
| wasmparser::Payload::End { .. }
| wasmparser::Payload::DataCountSection { .. } => {}
};
let id = Id::section(idx);
let added = items.size_added() - start;
Expand Down Expand Up @@ -365,7 +360,7 @@ impl<'a> Parse<'a> for ModuleReader<'a> {
wasmparser::Payload::StartSection { func, range } => {
StartSection {
function_index: func,
data: &self.data[range.start..range.end],
_data: &self.data[range.start..range.end],
}
.parse_edges(items, (&indices, idx))?;
}
Expand All @@ -375,12 +370,6 @@ impl<'a> Parse<'a> for ModuleReader<'a> {
wasmparser::Payload::DataSection(mut reader) => {
reader.parse_edges(items, ())?;
}
wasmparser::Payload::DataCountSection { range, .. } => {
DataCountSection {
size: range.end - range.start,
}
.parse_edges(items, ())?;
}
wasmparser::Payload::CodeSectionStart { .. }
| wasmparser::Payload::FunctionSection { .. } => {
unreachable!("unexpected code or function section found");
Expand All @@ -393,7 +382,8 @@ impl<'a> Parse<'a> for ModuleReader<'a> {
| wasmparser::Payload::ModuleSectionStart { .. }
| wasmparser::Payload::ModuleSectionEntry { .. }
| wasmparser::Payload::UnknownSection { .. }
| wasmparser::Payload::End { .. } => {}
| wasmparser::Payload::End { .. }
| wasmparser::Payload::DataCountSection { .. } => {}
}
}

Expand Down Expand Up @@ -889,17 +879,13 @@ impl<'a> Parse<'a> for wasmparser::ExportSectionReader<'a> {

struct StartSection<'a> {
function_index: u32,
data: &'a [u8], // We only need the size.
_data: &'a [u8], // We only need the size.
}

impl<'a> Parse<'a> for StartSection<'a> {
type ItemsExtra = usize;

fn parse_items(&mut self, items: &mut ir::ItemsBuilder, idx: usize) -> anyhow::Result<()> {
let size = self.data.len() as u32;
let id = Id::section(idx);
let name = "\"start\" section";
items.add_root(ir::Item::new(id, name, size, ir::Misc::new()));
fn parse_items(&mut self, _: &mut ir::ItemsBuilder, _: usize) -> anyhow::Result<()> {
Ok(())
}

Expand All @@ -918,28 +904,6 @@ impl<'a> Parse<'a> for StartSection<'a> {
}
}

struct DataCountSection {
size: usize,
}

impl<'a> Parse<'a> for DataCountSection {
type ItemsExtra = usize;

fn parse_items(&mut self, items: &mut ir::ItemsBuilder, idx: usize) -> anyhow::Result<()> {
let size = self.size as u32;
let id = Id::section(idx);
let name = "\"data count\" section";
items.add_root(ir::Item::new(id, name, size, ir::Misc::new()));
Ok(())
}

type EdgesExtra = ();

fn parse_edges(&mut self, _items: &mut ir::ItemsBuilder, (): ()) -> anyhow::Result<()> {
Ok(())
}
}

impl<'a> Parse<'a> for wasmparser::ElementSectionReader<'a> {
type ItemsExtra = usize;

Expand Down
14 changes: 14 additions & 0 deletions twiggy/tests/all/expectations/top_memory_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Shallow Bytes β”‚ Shallow % β”‚ Item
───────────────┼───────────┼──────────────────────────────
8 β”Š 12.70% β”Š wasm magic bytes
7 β”Š 11.11% β”Š f
7 β”Š 11.11% β”Š custom section 'name' headers
6 β”Š 9.52% β”Š code section headers
6 β”Š 9.52% β”Š "function names" subsection
6 β”Š 9.52% β”Š "memory names" subsection
5 β”Š 7.94% β”Š data[0]
3 β”Š 4.76% β”Š type[0]: () -> nil
3 β”Š 4.76% β”Š type section headers
3 β”Š 4.76% β”Š memory[0]
9 β”Š 14.29% β”Š ... and 3 more.
63 β”Š 100.00% β”Š Ξ£ [13 Total Rows]
Binary file added twiggy/tests/all/fixtures/memory.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions twiggy/tests/all/fixtures/memory.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module
(memory $0 1 1)
(data (i32.const 0) "")
(func $f (data.drop 0))
)
2 changes: 2 additions & 0 deletions twiggy/tests/all/top_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ test!(

// Regression test for https://github.com/rustwasm/twiggy/issues/151
test!(top_mono, "top", "./fixtures/mono.wasm", "-n", "10");

test!(top_memory_module, "top", "./fixtures/memory.wasm", "-n", "10");

0 comments on commit c71232e

Please sign in to comment.