wasm4 icon indicating copy to clipboard operation
wasm4 copied to clipboard

Can't run Go template

Open raitonoberu opened this issue 3 years ago • 19 comments

Steps to reproduce:

  1. Create new Go template with w4 new foobar --go
  2. Build it: make
  3. Run it: w4 run build/cart.wasm
  4. See in the console:
LinkError: WebAssembly.instantiate(): Import #0 module="env" function="tinygo_getCurrentStackPointer" error: function import requires a callable

Environment:

  • Fedora 37
  • wasm4 version 2.5.3
  • tinygo version 0.26.0 linux/amd64 (using go version go1.19.3 and LLVM version 15.0.0)

raitonoberu avatar Nov 18 '22 21:11 raitonoberu

Downgrading tinygo to 0.25.0 fixed the issue.


Adding

//go:linkname getCurrentStackPointer tinygo_getCurrentStackPointer
func getCurrentStackPointer() uintptr {
	return 0
}

to the end of w4/wasm4.go fixed the issue for tinygo 0.26.0

raitonoberu avatar Nov 18 '22 21:11 raitonoberu

Hi, thanks for reporting this!

I'm actually not able to repro, but I wonder if something changed with the tinygo's default wasm.json in 0.26, and we need to mirror any changes into the wasm4 project's target.json. Eg: default-stack-size looks suspicious.

aduros avatar Nov 20 '22 17:11 aduros

BigAngryBob ran into other issues with the tinygo. Tinygo was invoking wasm-opt on its own, but not providing --enable-bulk-memory.

[wasm-validator error in function 0] unexpected false: Bulk memory operations require bulk memory [--enable-bulk-memory], on

His versions:

% w4 --version
2.5.4
% tinygo version
tinygo version 0.27.0 darwin/amd64 (using go version go1.20.4 and LLVM version 15.0.0)
% wasm-opt --version
wasm-opt version 113

DenialAdams avatar May 25 '23 01:05 DenialAdams

I ran into the issue mentioned above (unexpected false: Bulk memory operations require bulk memory) as well with the default Go template. I'm not sure how to get past to it work with Go. I'm on the same w4 version, 2.5.4.

$ tinygo version
tinygo version 0.28.1 linux/amd64 (using go version go1.20.5 and LLVM version 15.0.0)
$ wasm-opt --version
wasm-opt version 105

hrecker avatar Jun 19 '23 21:06 hrecker

Do you know if it's failing on tinygo compilation, or the wasm-opt post-build pass?

aduros avatar Jun 20 '23 00:06 aduros

It failed on tinygo. I tried removing the wasm-opt logic from the Makefile and saw the same error.

hrecker avatar Jun 20 '23 14:06 hrecker

As @DenialAdams mentioned, tinygo is internally calling wasm-opt with its own flags. This error happens during the tinygo compilation phase, before the wasm-opt post-build pass.

I was able to compile successfully by replacing the template's target.json with the one from the tinygo project: https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.json (this one contains the -mbulk-memory flag, which bypasses the current compiling error)

However, I am now facing the issue: Uncaught (in promise) TypeError: import object field 'wasi_snapshot_preview1' is not an Object when I open the game in the browser through w4 run build/cart.wasm. Not sure how to fix that one.

I'm using go 1.21.1, tinygo 0.30.0, and w4 2.5.4.

tl;dr

  1. Replace contents of target.json in template with https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.json
  2. Remove wasm-opt steps in Makefile, because tinygo already calls wasm-opt internally
  3. Since tinygo runs wasm-opt internally, the user needs to have wasm-opt installed 3.1) If tinygo was installed through a package manager, then a separate install of binaryen is needed to get wasm-opt 3.2) If tinygo was installed by downloading it from their GitHub releases, then wasm-opt already comes included in their bin folder. tinygo will automatically find this wasm-opt in their own code: https://github.com/tinygo-org/tinygo/blob/731532cd2b6353b60b443343b51296ec0fafae09/goenv/goenv.go#L143-L165

hchac avatar Sep 27 '23 01:09 hchac

Replacing all of target.json was probably too intrusive on my part. The contentious flag in the current target.json seems to be --strip-all https://github.com/aduros/wasm4/blob/aca37f98e509f96faa789efb9df8925d28f65bbf/cli/assets/templates/go/target.json#L21

Looks like this flag isn't playing nicely with the latest tinygo. If you remove that flag you can get past the unexpected false: Bulk memory operation (bulk memory is disabled) build errors. However, you still have the Uncaught (in promise) TypeError: import object field 'wasi_snapshot_preview1' is not an Object error in the console once you run the cart.

Unfortunately I don't currently know anything about tinygo or wasm to figure out if any of these changes are lining up to produce a correct wasm4 cart.

hchac avatar Sep 29 '23 03:09 hchac

I haven't had time to look at this, but more experienced Go developers may have some insight. Ping @peterhellberg @christopher-kleine

aduros avatar Nov 15 '23 17:11 aduros

There's a small game I'd like to implement using Go anyway. I'll look into this in the next days, I guess. I'm currently busy with work.

christopher-kleine avatar Nov 16 '23 15:11 christopher-kleine

@christopher-kleine Cool, I have primarily used Zig :zap: lately for fantasy console related stuff, so have no up to date experience with TinyGo together with WASM-4

peterhellberg avatar Nov 16 '23 15:11 peterhellberg

So far I was able to confirm the problem. I tried the default target.json supplied by WASM4 and also the target.json directly from TinyGo. Both failed.

Digging a little, it's stated, that we need the wasm_exec.js from TinyGo instead of the one from Go itself. I tried this, but was unsuccessful as of now. I might have to ask on their Repo about this.

christopher-kleine avatar Nov 30 '23 07:11 christopher-kleine

This is still an issue, was trying the same thing and same error on the console. Any leads?

PrimalPimmy avatar Feb 05 '24 14:02 PrimalPimmy

Sorry, I didn't find anything new. Currently I don't have the time to investigate further, since other things popped up.

My best advice so far would be: Either try to downgrade to an earlier version of Tinygo or - if an older version of WASM4 is fine - try the Docker Images.

Reminds me: Should I take them down, @aduros ? I can't maintain the docker images for quite some time, I'm afraid. And I don't want people come running to you because of bugs in those older versions.

christopher-kleine avatar Feb 09 '24 10:02 christopher-kleine

ok so for now here is how to use wasm-4 with go

  1. installing proper version of tinygo (0.21.0) https://github.com/tinygo-org/tinygo/releases/tag/v0.21.0 You will need to ensure that the path to the tinygo executable file is in your PATH variable.

  2. installing proper go version (1.17) I use gvm. Atm I have both go1.17 and go1.22.4 installed with gvm.

  3. After creating the w4 new --go project. You should write in terminal gvm use go1.17 and for double check you write tinygo version which must say: tinygo version 0.21.0 linux/amd64 (using go version go1.17 and LLVM version 11.0.0)

After all of these, everything should work

0riginaln0 avatar Apr 03 '24 10:04 0riginaln0

It seems that the problem with the latest versions of Go and TinyGo is still in the incorrect target.json. With these settings, Wasm-4 works for me without errors. In my case, versions Go 1.22.2 and TinyGo 0.31.2

target.json content: { "llvm-target": "wasm32-unknown-unknown", "cpu": "generic", "features": "+mutable-globals,+nontrapping-fptoint,+sign-ext,+bulk-memory", "build-tags": [ "tinygo.wasm", "wasm_unknown" ], "goos": "linux", "goarch": "arm", "linker": "wasm-ld", "rtlib": "compiler-rt", "scheduler": "none", "cflags": [ "-mno-bulk-memory", "-mnontrapping-fptoint", "-msign-ext" ], "ldflags": [ "--allow-undefined", "--no-demangle", "--import-memory", "--initial-memory=65536", "--max-memory=65536", "--stack-first", "--no-entry", "-zstack-size=14752" ] }

AlRado avatar Apr 09 '24 07:04 AlRado

I can partially confirm this. The "Hello World" example works. But I also tried to compile the snake demo and my Tic-Tac-Toe source. Both compile just fine. But I can't run them.

Uncaught (in promise) LinkError: import object field 'tinygo_getCurrentStackPointer' is not a Function

// Edit: I'll try to go through the snake tutorial to figure out where it breaks.

christopher-kleine avatar Apr 10 '24 08:04 christopher-kleine