Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscall/js: running web assembly in Wasmer is failing on bad type flag #40366

Closed
martinkunc opened this issue Jul 23, 2020 · 8 comments
Closed
Labels
arch-wasm WebAssembly issues FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@martinkunc
Copy link

martinkunc commented Jul 23, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mkunc/Library/Caches/go-build"
GOENV="/Users/mkunc/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mkunc/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/mkunc/go/src/github.com/martinkunc/wagather/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nw/2584cxnn0_545f3kzf2g8mlw0000gn/T/go-build220972923=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I am running Golang WebAssembly in Wasmer. In Go 1.14 the application fails on panic: bad type flag, but in Go 1.13 it runs well. The application is importing "syscall/js".

What is failing:

I believe it is failing because throwing panic was added in Go 1.14:
https://github.com/golang/go/blob/master/src/syscall/js/js.go#L291

In Go 1.13.9, it by default considers the object type to be js object:
https://github.com/golang/go/blob/go1.13.9/src/syscall/js/js.go#L244

But I believe the real problem is to expect javascript runtime having fs.constants of some type,
which I believe is happening here:
https://github.com/golang/go/blob/master/src/syscall/fs_js.go#L21

Unfortunatelly this syscall variable requires the constant exists before initialization, so I cannot set it before importing "syscall/js"

My humble opinion would be that we could change constants to fsConstants function() {} so that it would be evaluated only when particular fs function would require it (and I would have chance to set it up upfront on another environment).

What did you expect to see?

Application runs fine.

What did you see instead?

Application crashes with:

panic: bad type flag

goroutine 1 [running]:
syscall/js.Value.Type(0x7ff800000000000b, 0x0, 0x0)
	/usr/local/go/src/syscall/js/js.go:291 +0x1b
syscall/js.Value.Get(0x7ff800000000000b, 0x0, 0x2873f8, 0x9, 0x7ff800000000000b, 0x0)
	/usr/local/go/src/syscall/js/js.go:298 +0x2
syscall.init()
	/usr/local/go/src/syscall/fs_js.go:21 +0xe
@martinkunc martinkunc changed the title Running web assembly compiled in go 1.14 is failing on Running web assembly in Wasmer is failing on bad type flag Jul 23, 2020
@agnivade agnivade changed the title Running web assembly in Wasmer is failing on bad type flag syscall/js: running web assembly in Wasmer is failing on bad type flag Jul 25, 2020
@agnivade agnivade added arch-wasm WebAssembly issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 25, 2020
@agnivade
Copy link
Contributor

/cc @neelance

@neelance
Copy link
Member

The interface between src/syscall/js/js.go and misc/wasm/wasm_exec.js is internal and not stable. Wasmer can not use wasm_exec.js since it has no JavaScript runtime. So if Wasmer has support for syscall/js, then it seems to be a hack on their end which is using the internal interface. If there is any incompatibility with the new Go version, then it needs to be resolved on Wasmer's end. Please file an issue with the Wasmer project.

@agnivade agnivade added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jul 25, 2020
@martinkunc
Copy link
Author

martinkunc commented Jul 27, 2020

The problem is that syscall/js alone is presuming existence fo Nodejs Wasm constants, and it is doing it during declaration of variables. I can guess that it is current limitation, but could we postpone loading of variables from here:

https://github.com/golang/go/blob/master/src/syscall/fs_js.go#L26

I tried to prepare minimal reproducible code in repo: https://github.com/martinkunc/gowasmer

To reproduce, you can run

make build-wasm-1-14 && make build && ./wasmcmd

and then compare it with

make build-wasm-1-13 && make build && ./wasmcmd

I hope I am not using something which is only intended to work this way. I understand that WASMI is not stable, but maybe it might allow at least run at other runtimes, and I hope the change would be small one.

@neelance
Copy link
Member

The error "bad type flag" is an internal error. It should not even be thrown if something is null or undefined.

The constants that you say are missing are in your own code: https://github.com/martinkunc/gowasmer/blob/master/wasm/bridge.go#L169
However, this "bridge" is a hack on an internal interface and apparently not compatible with the latest version of said interface.

@martinkunc
Copy link
Author

Thank you for the explanation Richard and for pointing to my code and sorry for a confused report. I think I can close the issue. I will try to fix my hack.

Btw, thank you for all your hard work on the Wasm support :)

@BruceChoca
Copy link

@martinkunc excuse me! Has this problem beem solved? How to solve it?

@martinkunc
Copy link
Author

@BruceChoca Unfortunatelly I didn't continued on implementation of fs constants. Instead of what I did was I let my webassembly binary compiled on go 1.13 and built my host binary in latest go.
You can look at what I prepared here: https://github.com/martinkunc/wagather
It is PoC of collecting data from OpenShift Api (list of operators), What I did was I implemented Authenticated Http Proxy on the host, which I called from the webassembly.

@BruceChoca
Copy link

@martinkunc thank u !
I found the reason, 1.14+ and 1.13+ typeflag is different. -> https://github.com/golang/go/blob/master/src/syscall/js/js.go#L280
u must modify the typeFlag to make is consistent.
modify function storeValue. -> https://github.com/martinkunc/gowasmer/blob/master/wasm/bridge.go#L352
https://github.com/martinkunc/gowasmer/blob/master/wasm/bridge.go#L420

typeFlag := 0
switch rt.Kind() {
case reflect.String:
	typeFlag = 1
case reflect.Func:
	typeFlag = 3
}

=>

switch rt.Kind() {
case reflect.String:
	typeFlag = 2
case reflect.Func:
	typeFlag = 4
case reflect.Struct:
 	typeFlag = 1
case reflect.Slice:
 	typeFlag = 1
}

u can try, hope it will solve your problem.

@golang golang locked and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly issues FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants