tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
14.72k stars 858 forks source link

Cannot compile WASM module in 0.32.0 but works in 0.31.2 #4318

Closed acouvreur closed 4 days ago

acouvreur commented 4 days ago

I've just updated TinyGo to 0.32.0 and tinygo fails to compile with the following error:

tinygo build -ldflags "-X 'main.Version=draft'" -o ./plugins/proxywasm/sablierproxywasm.wasm -scheduler=none -target=wasi ./plugins/proxywasm
# github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/internal
..\..\go\pkg\mod\github.com\tetratelabs\proxy-wasm-go-sdk@v0.23.0\proxywasm\internal\hostcall_utils_tinygo.go:31:9: cannot use uintptr(size) (value of type uintptr) as int value in struct literal
..\..\go\pkg\mod\github.com\tetratelabs\proxy-wasm-go-sdk@v0.23.0\proxywasm\internal\hostcall_utils_tinygo.go:32:9: cannot use uintptr(size) (value of type uintptr) as int value in struct literal
..\..\go\pkg\mod\github.com\tetratelabs\proxy-wasm-go-sdk@v0.23.0\proxywasm\internal\hostcall_utils_tinygo.go:39:9: cannot use uintptr(size) (value of type uintptr) as int value in struct literal
..\..\go\pkg\mod\github.com\tetratelabs\proxy-wasm-go-sdk@v0.23.0\proxywasm\internal\hostcall_utils_tinygo.go:40:9: cannot use uintptr(size) (value of type uintptr) as int value in struct literal

You can take a look at https://github.com/acouvreur/sablier/pull/284 for more details.

Reverting back to 0.31.2 works!

aykevl commented 4 days ago

This is https://github.com/tinygo-org/tinygo/pull/4156. This change is a backwards-incompatible change with the goal of being more compatible with existing Go code.

Looking at the code: https://github.com/tetratelabs/proxy-wasm-go-sdk/blob/v0.23.0/proxywasm/internal/hostcall_utils_tinygo.go#L29

func RawBytePtrToString(raw *byte, size int) string {
    return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
        Data: uintptr(unsafe.Pointer(raw)),
        Len:  uintptr(size),
        Cap:  uintptr(size),
    }))
}

func RawBytePtrToByteSlice(raw *byte, size int) []byte {
    return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
        Data: uintptr(unsafe.Pointer(raw)),
        Len:  uintptr(size),
        Cap:  uintptr(size),
    }))
}

The best way to do this nowadays is by using unsafe.String and unsafe.Slice.

(...perhaps this change was a bad idea in hindsight, maybe I should just have said "use unsafe.String and unsafe.Slice" because that's going to work on every compiler).

acouvreur commented 4 days ago

Ok, makes sense, thanks.

I'll update the proxy-wasm-go-sdk!

inliquid commented 1 day ago

Got same issue: https://github.com/knqyf263/go-plugin/issues/64