w2c2 icon indicating copy to clipboard operation
w2c2 copied to clipboard

Segfault when compiling module

Open Heath123 opened this issue 3 years ago • 4 comments

If you compile the AssemblyScript program:

export declare function test(): void

into WebAssembly (or use this one I already compiled):

asc test.ts --binaryFile test.wasm

Then into C:

w2c2 -o test.c test.wasm

you get a segfault:

fish: Job 1, '~/w2c2/w2c2 -o test.c test.wasm' terminated by signal SIGSEGV (Address boundary error)

Backtrace:

(gdb) backtrace
#0  wasmCWriteExports (file=0x5615678c32a0, module=0x5615678c3480, pretty=false, external=true) at c.c:3187
#1  0x0000561565bb8338 in wasmCWriteModuleDeclarations (file=0x5615678c32a0, module=0x5615678c3480, parallel=false, pretty=false) at c.c:3626
#2  0x0000561565bb8566 in wasmCWriteDeclarations (module=0x5615678c3480, singleFile=0x5615678c32a0, pretty=false) at c.c:3687
#3  0x0000561565bb8960 in wasmCWriteModule (module=0x5615678c3480, options=...) at c.c:4100
#4  0x0000561565bba9f2 in main (argc=4, argv=0x7ffcf43a12f8) at main.c:184
(gdb) 

Happens here: https://github.com/turbolent/w2c2/blob/main/c.c#L3187

Heath123 avatar May 03 '22 20:05 Heath123

Wait, maybe I shouldn't have put the export, but it also crashes without that so I don't think that's the issue

Heath123 avatar May 03 '22 21:05 Heath123

functionIndex ends up being negative

Heath123 avatar May 04 '22 09:05 Heath123

This looks like it would be easy to fix but I don't understand the code properly so I can't

Heath123 avatar May 04 '22 12:05 Heath123

Actually the zip I posted doesn't seem to match my code, and I can't reproduce by compiling AssemblyScript now?

Heath123 avatar May 06 '22 14:05 Heath123

@Heath123 Did you end up being able to reproduce the problem? If so, could you provide a binary?

turbolent avatar Aug 22 '22 02:08 turbolent

(module
 (type $none_=>_none (func))
 (import "module" "test" (func $module/test))
 (memory $0 0)
 (export "test" (func $module/test))
 (export "memory" (memory $0))
)

binary: test.wasm.zip

MaxGraey avatar Aug 22 '22 08:08 MaxGraey

@Heath123 Did you end up being able to reproduce the problem? If so, could you provide a binary?

The zip I attached does reproduce the issue, I just meant that it doesn’t match the AssemblyScript code I posted

Heath123 avatar Aug 22 '22 17:08 Heath123

```webassembly
(module
 (type $none_=>_none (func))
 (import "module" "test" (func $module/test))
 (memory $0 0)
 (export "test" (func $module/test))
 (export "memory" (memory $0))
)

binary: test.wasm.zip

Thanks, it must be getting confused that it’s importing and exporting the same thing

Heath123 avatar Aug 22 '22 17:08 Heath123

@Heath123 @MaxGraey Thank you for reporting this bug and providing a reproducer

turbolent avatar Sep 11 '22 05:09 turbolent