wasilibs / nottinygc

Higher-performance allocator for TinyGo WASI apps
MIT License
64 stars 6 forks source link

Getting integer divide by zero error when use compress/gzip #41

Closed qq906907952 closed 9 months ago

qq906907952 commented 9 months ago

I was use this library with a wasi module written in Go to enhance the performance, but i encounter an error with panic: runtime error: divide by zero when i use compress/gzip stdlib to decode gzip bytes.

the full compile command: tinygo build -o wasm.wasm -target wasi -gc=custom -tags=custommalloc -target=wasi -scheduler=none ./main.go

i run it with wasmtime: env WASMTIME_BACKTRACE_DETAILS=1 wasmtime wasm.wasm

panic: runtime error: divide by zero
Error: failed to run main module `wasm.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x24d61 - runtime.abort
                           at /root/software/tinygo/src/runtime/runtime_tinygowasm.go:70:6              - runtime.runtimePanicAt
                           at /root/software/tinygo/src/runtime/panic.go:71:7
           1: 0x27519 - runtime.divideByZeroPanic
                           at /root/software/tinygo/src/runtime/panic.go:177:16
           2: 0x1cf57 - runtime.alloc
                           at /root/workplace/go_poject/GOPATH/pkg/mod/github.com/wasilibs/nottinygc@v0.7.0/intmap.go:93:19
           3: 0x23408 - compress/flate.newHuffmanEncoder
                           at /root/.g/go/.versions/1.21.3/src/compress/flate/huffman_code.go:60:24
           4: 0x22609 - compress/flate.generateFixedOffsetEncoding
                           at /root/.g/go/.versions/1.21.3/src/compress/flate/huffman_code.go:95:24              - runtime.run
                           at /root/software/tinygo/src/runtime/scheduler_none.go:24:9              - _start
                           at /root/software/tinygo/src/runtime/runtime_wasm_wasi.go:21:5

when i remove the gzip lib, everything is working fine

tinygo version: tinygo version 0.30.0 linux/amd64 (using go version go1.21.3 and LLVM version 16.0.1)

anuraaga commented 9 months ago

Hi @qq906907952 - thanks for the report. I will try to reproduce using the gzip library, but are you able to provide a code reproduction? Thanks.

qq906907952 commented 9 months ago

Hi @qq906907952 - thanks for the report. I will try to reproduce using the gzip library, but are you able to provide a code reproduction? Thanks.

of course,there is the very simple code to reproduce the error

package main

import (
    "bytes"
    "compress/gzip"
    _ "github.com/wasilibs/nottinygc"
)

func main() {
    r := bytes.NewReader([]byte{})
    gzip.NewReader(r)
}

compile with tinygo build -o wasm.wasm -target wasi -gc=custom -tags=custommalloc -target=wasi -scheduler=none ./main.go

anuraaga commented 9 months ago

Thanks a lot for the repro @qq906907952! I have released 0.7.1 with a fix for the bug.