Repository: go
Issue: cmd/compile: incorrect compilation of specific functions in wasm
Labels: NeedsInvestigation,compiler/runtime

## **UPDATE: I found a much simpler way to reproduce this issue; see https://github.com/golang/go/issues/68156#issuecomment-2229153940**

### Go version

go version go1.22.4 darwin/arm64

### Output of `go env` in your module/workspace:

```shell
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/kaioreilly/Library/Caches/go-build'
GOENV='/Users/kaioreilly/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/kaioreilly/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/kaioreilly/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/kaioreilly/cogent/core/go.mod'
GOWORK='/Users/kaioreilly/cogent/go.work'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/pw/lntpc2cn37v41ct1t0t9t9880000gn/T/go-build988797772=/tmp/go-build -gno-record-gcc-switches -fno-common'
```


### What did you do?

Apologies for the convoluted steps for reproducing. I tried for several hours to get a more reproducible example, but I could not. Somehow there is some deleterious interaction between these various components, and removing any one of them stops the issue. This may seem like an issue to do with one of these third party libraries, but as you will see, this behavior seems to almost certainly be an issue with the compiler or Chromium itself.

I made a `go.work` workspace containing local clones of the main/master branches of Cogent Core (https://github.com/cogentcore/core) and goldmark (https://github.com/yuin/goldmark). Then, I went to Cogent Core (`cd core`) and installed the Cogent Core tool (`go install ./cmd/core`). Then, I went to the basic example in Cogent Core (`cd examples/basic`) and ran the program for web using `core run web`. `core run web` is a command that runs `go build` using `GOOS=js GOARCH=wasm` under the hood. Then, I went to port 8080 of my computer's local IP address (`http://192.168.1.x:8080`; you can run `ifconfig` to get yours) in Google Chrome on an Android phone (mine is a Pixel 7 Pro running Android 14 with Chrome 126.0.6478.72). I observed that everything worked as expected.

Then, I changed the following line in `basic.go` in `examples/basic` from

```go
import "cogentcore.org/core/core"
```

to

```go
import (
	"cogentcore.org/core/core"
	_ "github.com/yuin/goldmark"
)
```

Then, I repeated the `core run web` step from above, opened a new tab on the Android phone, and went to the same URL again. After waiting for some amount of time (no more than a minute), I observed the issue described below.

### What did you see happen?

After following all of the steps above, the browser tab reliably turns from correctly rendering the app to displaying an `Aw, Snap!` error screen (as in https://support.google.com/chrome/answer/95669). Nothing is printed to the console and no further information about a specific error is given.

### What did you expect to see?

I expected to see the browser tab continue to correctly render the app.

After several hours of debugging, I have determined that the cause of the issue can be narrowed down to a single function declared in goldmark: `util.ReadWhile`. When I change the body of this function to just be `panic("ReadWhile")`, not only does the issue described above cease to happen, the app *does not panic*. This means that we are actually not even calling this function in the first place, yet somehow its contents are causing bizarre app crashes. I tried various different variations of this function: `println("ReadWhile")` with a `return 0, false` afterward results in no crashing and no print statement, whereas the same but with `fmt.Println("ReadWhile")` results in crashing just like the original body of the function. Several variations on the argument and loop structure of the function have not produced any changes in the crashing behavior; you basically have to remove the entire body of the function for it to stop crashing. Changing the name to `SomethingElse` also does not stop the crashing. I also tried moving this `ReadWhile` function elsewhere, but somehow I could not replicate the crash without it being in the same place in goldmark.

The fact that the innocuous contents of a function that is never called lead the program to randomly crash without any further details suggests that this is not a coding error on the part of a third party library but rather some issue with the way that the compiler is building wasm files, or the way that Chromium on Android is handling them. Cogent Core has its own custom `wasm_exec.js` file, but I verified that the issue still occurs with the standard version contained in `misc`.

I have only been able to reproduce this issue in Chrome on Android and other Chromium browsers on Android such as Kiwi Browser. It does not occur in Firefox on Android, Safari on iOS, or Chrome on macOS, Ubuntu, or Windows.