tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

wasi: imported and exported functions do not result in identical signatures.

Open james-lawrence opened this issue 3 years ago • 1 comments

I've created a standalone repository that reproduces this issue.

essentially functions that are imported from the runtime and functions that are exported do not generate the same function signature making them unusable.

//export github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean
func Boolean(fallback bool, keys ...string) bool {
	return false
}
// imported function by a wasm module
func Boolean(fallback bool, keys ...string) bool

the example repository will fail to execute with the following result (output is truncated for brevity):

exported github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean ( i32, i32, i32, i32 ) i32
imported github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean ( i32, i32, i32, i32, i32 ) i32
import[4] func[env.github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean]: signature mismatch: i32i32i32i32i32_i32 != i32i32i32i32_i32

reproduction steps from the repository above:

tinygo build -target wasi -wasm-abi generic -o module1.wasm ./impl/wasi/runtime/runtime.go
tinygo build -target wasi -wasm-abi generic -o module2.wasm main.go
go run ./cmd/wasibug/main.go

james-lawrence avatar Jul 17 '22 13:07 james-lawrence

Right, I see. The way to solve this is to mark them both as "exported" - which really means to make the function available externally. For example like this:

// imported function by a wasm module
//export github.com/james-lawrence/tinygo-wasi-bug/wasi/runtime/env.Boolean
func Boolean(fallback bool, keys ...string) bool

Functions without an //export pragma will use an internal calling convention that's not stable. Functions with an //export pragma will try to match the C calling convention.

aykevl avatar Jul 30 '22 00:07 aykevl

@james-lawrence is there anything else to do on this one?

ack that //export isn't intuitive to mark something as an import, but for today //export is how it is done.

codefromthecrypt avatar Sep 07 '22 06:09 codefromthecrypt

@codefromthecrypt that is very unintuitive. ;) i'll take it for a spin this weekend and see if it works. if it does I'll point out documentation that should probably be updated for others. ;)

james-lawrence avatar Sep 08 '22 23:09 james-lawrence

@james-lawrence opened https://github.com/tinygo-org/tinygo/issues/3147 for follow-up. maybe close this out unless there's something else to do here?

codefromthecrypt avatar Sep 09 '22 00:09 codefromthecrypt

Sounds good to me; will put my notes in that issue.

james-lawrence avatar Sep 10 '22 13:09 james-lawrence