rustwasm/twiggy

Confusing error with wasm "a.out"

sunfishcode opened this issue ยท 1 comments

๐Ÿ› Bug Description

Using the wasi-sdk to compile a C hello world testcase to a wasm binary and examine it with twiggy:

$ clang hello.c
$ file a.out 
a.out: WebAssembly (wasm) binary module version 0x1 (MVP)
$ twiggy top a.out 
error: Gimli error: UnexpectedEof
  caused by: UnexpectedEof

It wasn't immediately clear what was causing this problem. After some experimenting, it appears that twiggy is determining the file type from its name. "a.out" is clang's default for executables, including wasm "executables".

$ mv a.out hello.wasm
$ twiggy top hello.wasm 
 Shallow Bytes โ”‚ Shallow % โ”‚ Item
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
          9699 โ”Š    25.80% โ”Š printf_core
          6143 โ”Š    16.34% โ”Š dlmalloc
          2364 โ”Š     6.29% โ”Š data[0]
...

Similar things happen with .o files, though the symptoms are slightly different:

$ clang -c hello.c
$ file hello.o
hello.o: WebAssembly (wasm) binary module version 0x1 (MVP)
$ twiggy top hello.o
 Shallow Bytes โ”‚ Shallow % โ”‚ Item
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
             0 โ”Š     0.00% โ”Š ฮฃ [0 Total Rows]
$ mv hello.o hello.o.wasm
$ twiggy top hello.o.wasm 
 Shallow Bytes โ”‚ Shallow % โ”‚ Item
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            86 โ”Š    22.93% โ”Š code[0]
            67 โ”Š    17.87% โ”Š custom section 'linking'
            34 โ”Š     9.07% โ”Š import env::__indirect_function_table
...

twiggy version: twiggy-opt 0.4.0

๐ŸŒ Test Case

Take any wasm module file and rename it to "a.out" or "hello.o".

๐Ÿ‘Ÿ Steps to Reproduce

Take any wasm module file, rename it to "a.out" or "hello.o", and run twiggy top on it.

๐Ÿ˜ฒ Actual Behavior

From the behavior and error messages, it appears twiggy is attempting to decode the wasm modules with a native object file decoder, and producing confusing output.

๐Ÿค” Expected Behavior

If it's a deliberate choice to use the filename to determine which decoder to use, a more descriptive error message would help. Otherwise, wasm has a magic cookie which can be used.

We actually fixed this on master: #260 and #226

Just need to cut a release, which I will do now.