diff --git a/ir/ir.rs b/ir/ir.rs index 2d9b60cc..bd23a03f 100644 --- a/ir/ir.rs +++ b/ir/ir.rs @@ -46,14 +46,18 @@ impl ItemsBuilder { /// assigned. pub fn add_item(&mut self, item: Item) -> Id { let id = item.id; - self.size_added += item.size; - self.items.insert(id, item); + let size = item.size; - let old_value = self.parsed.insert(id); - assert!( - old_value, - "should not parse the same key into multiple items" - ); + match self.items.get_mut(&id) { + None => { + self.size_added += size; + self.items.insert(id, item); + self.parsed.insert(id); + } + Some(prev) => { + prev.size += size; + } + } id } diff --git a/notes.txt b/notes.txt new file mode 100644 index 00000000..18443b41 --- /dev/null +++ b/notes.txt @@ -0,0 +1,74 @@ +``` +;; `threads.wat` fixture +;; +;; Built with `wat2wasm --enable-bulk-memory --enable-threads` +(module + (memory $0 1 1) + (data (i32.const 0) "") + (func $f (data.drop 0)) +) +``` + +``` +; wasm-objdump --headers twiggy/tests/all/fixtures/threads.wasm + +threads.wasm: file format wasm 0x1 + +Sections: + + Type start=0x0000000a end=0x0000000e (size=0x00000004) count: 1 + Function start=0x00000010 end=0x00000012 (size=0x00000002) count: 1 + Memory start=0x00000014 end=0x00000018 (size=0x00000004) count: 1 +DataCount start=0x0000001a end=0x0000001b (size=0x00000001) count: 1 + Code start=0x0000001d end=0x00000024 (size=0x00000007) count: 1 + Data start=0x00000026 end=0x0000002c (size=0x00000006) count: 1 +``` + +``` +; wasm-objdump --full-contents twiggy/tests/all/fixtures/threads.wasm + +threads.wasm: file format wasm 0x1 + +Contents of section Type: +000000a: 0160 0000 .`.. + +Contents of section Function: +0000010: 0100 .. + +Contents of section Memory: +0000014: 0101 0101 .... + +Contents of section DataCount: +000001a: 01 . + +Contents of section Code: +000001d: 0105 00fc 0900 0b ....... + +Contents of section Data: +0000026: 0100 4100 0b00 ..A... +``` + +``` +; target/debug/twiggy top twiggy/tests/all/fixtures/threads.wasm + + Shallow Bytes │ Shallow % │ Item +───────────────┼───────────┼─────────────────────── + 8 ┊ 18.18% ┊ wasm magic bytes + 7 ┊ 15.91% ┊ code[0] + 6 ┊ 13.64% ┊ code section headers + 5 ┊ 11.36% ┊ data[0] + 3 ┊ 6.82% ┊ type[0]: () -> nil + 3 ┊ 6.82% ┊ type section headers + 3 ┊ 6.82% ┊ memory[0] + 3 ┊ 6.82% ┊ memory section headers + 3 ┊ 6.82% ┊ "data count" section + 3 ┊ 6.82% ┊ data section headers + 44 ┊ 100.00% ┊ Σ [10 Total Rows] +``` + +``` +; exa -l --header twiggy/tests/all/fixtures/threads.wasm + +Permissions Size User Date Modified Name +.rw-rw-r-- 44 katelyn 21 Jun 23:07 twiggy/tests/all/fixtures/threads.wasm +``` diff --git a/parser/wasm_parse/mod.rs b/parser/wasm_parse/mod.rs index 634f3c97..2ae15a4f 100644 --- a/parser/wasm_parse/mod.rs +++ b/parser/wasm_parse/mod.rs @@ -792,6 +792,7 @@ struct DataCountSection<'a>(wasmparser::Section<'a>); impl<'a> Parse<'a> for DataCountSection<'a> { type ItemsExtra = usize; + // Worth looking here... fn parse_items( &mut self, items: &mut ir::ItemsBuilder, diff --git a/twiggy/tests/all/expectations/top_threaded_module b/twiggy/tests/all/expectations/top_threaded_module new file mode 100644 index 00000000..3f566e60 --- /dev/null +++ b/twiggy/tests/all/expectations/top_threaded_module @@ -0,0 +1,13 @@ + Shallow Bytes │ Shallow % │ Item +───────────────┼───────────┼─────────────────────── + 8 ┊ 18.18% ┊ wasm magic bytes + 7 ┊ 15.91% ┊ code[0] + 6 ┊ 13.64% ┊ code section headers + 5 ┊ 11.36% ┊ data[0] + 3 ┊ 6.82% ┊ type[0]: () -> nil + 3 ┊ 6.82% ┊ type section headers + 3 ┊ 6.82% ┊ memory[0] + 3 ┊ 6.82% ┊ memory section headers + 3 ┊ 6.82% ┊ "data count" section + 3 ┊ 6.82% ┊ data section headers + 44 ┊ 100.00% ┊ Σ [10 Total Rows] diff --git a/twiggy/tests/all/fixtures/threads.wasm b/twiggy/tests/all/fixtures/threads.wasm new file mode 100644 index 00000000..dbc75834 Binary files /dev/null and b/twiggy/tests/all/fixtures/threads.wasm differ diff --git a/twiggy/tests/all/fixtures/threads.wat b/twiggy/tests/all/fixtures/threads.wat new file mode 100644 index 00000000..fa5d5903 --- /dev/null +++ b/twiggy/tests/all/fixtures/threads.wat @@ -0,0 +1,6 @@ +;; Built with `wat2wasm --enable-bulk-memory --enable-threads` +(module + (memory $0 1 1) + (data (i32.const 0) "") + (func $f (data.drop 0)) +) diff --git a/twiggy/tests/all/fixtures/threads_test.wasm b/twiggy/tests/all/fixtures/threads_test.wasm new file mode 100644 index 00000000..1bd3ae2f Binary files /dev/null and b/twiggy/tests/all/fixtures/threads_test.wasm differ diff --git a/twiggy/tests/all/fixtures/threads_working.wasm b/twiggy/tests/all/fixtures/threads_working.wasm new file mode 100644 index 00000000..1bd3ae2f Binary files /dev/null and b/twiggy/tests/all/fixtures/threads_working.wasm differ diff --git a/twiggy/tests/all/top_tests.rs b/twiggy/tests/all/top_tests.rs index 6e8173ef..f32c4eb8 100644 --- a/twiggy/tests/all/top_tests.rs +++ b/twiggy/tests/all/top_tests.rs @@ -79,3 +79,12 @@ test!( // Regression test for https://github.com/rustwasm/twiggy/issues/151 test!(top_mono, "top", "./fixtures/mono.wasm", "-n", "10"); + +// Threaded modules should not cause a panic. +test!( + top_threaded_module, + "top", + "-n", + "25", + "./fixtures/threads.wasm" +);