binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

metadce errors when data or element section is initalized with a global variable

Open michelledaviest opened this issue 3 years ago • 3 comments

Hello!

When you run metadce on the following wasm code:

(module
    (import "host" "element_offset" (global $element_offset i32))
    (func $main (export "main")
        nop)
    (table $table 1 funcref)
    (elem $table (global.get $element_offset) $main)
)

and try to get the new reachability graph with the --dump flag, metadce crashes and gives you

=== graph ===
root: 

as output. The graph input that I supplied was,

[
    {
        "name": "main",
        "root": true,
        "export": "main"
    }
]

It seems to be that this happens every time you try to initialize a data or element section with a global variable, which is allowed by WebAssembly.

michelledaviest avatar Aug 18 '22 15:08 michelledaviest

I can't reproduce a crash with that. But the wat file you mention doesn't load in binaryen, so maybe there was another tool in the middle? I edited your file slightly to this so it loads:

(module
  (import "host" "element_offset" (global $element_offset i32))
  (func $main (export "main")
    (nop)
  )
  (table $table 1 funcref)
  (elem $table (global.get $element_offset) $main)
)

Then bin/wasm-metadce a.wat -f=a --dump (where a is that JSON you provided) does not crash, and prints

=== graph ===
root: func$main$0
root: main
node: func$main$0
  is function main
node: main
  is export main, main
  reaches: func$main$0
=============

kripken avatar Aug 18 '22 17:08 kripken

I load the .wat file through wat2wasm using wat2wasm foo.wat -o foo.wasm and then run wasm-metadce --dump -f graph foo.wasm -o foo-dce.wasm, which still crashes with the example you provided.

michelledaviest avatar Aug 18 '22 17:08 michelledaviest

Can you get a stack trace of the crash in a debug build? That might help.

kripken avatar Aug 18 '22 20:08 kripken